Update ppr (#276)

* fix: writer

* fix: reader

* fix: spec

* fix: js

* 0.0.190
main
bokuweb 2021-04-14 12:01:38 +09:00 committed by GitHub
parent b0a7de6b69
commit 5402fe2190
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
51 changed files with 891 additions and 55 deletions

View File

@ -59,7 +59,7 @@ mod tests {
);
assert_eq!(
serde_json::to_string(&graphic).unwrap(),
r#"{"children":[{"dataType":"wpShape","children":[{"type":"shape","data":{"children":[{"type":"textbox","data":{"children":[{"children":[{"type":"paragraph","data":{"id":"12345678","children":[{"type":"run","data":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"children":[{"type":"text","data":{"preserveSpace":true,"text":"pattern1"}}]}}],"property":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"style":null,"numberingProperty":null,"alignment":null,"indent":null,"lineHeight":null,"divId":null},"hasNumbering":false}}],"has_numbering":false}],"hasNumbering":false}}]}}]}]}"#
r#"{"children":[{"dataType":"wpShape","children":[{"type":"shape","data":{"children":[{"type":"textbox","data":{"children":[{"children":[{"type":"paragraph","data":{"id":"12345678","children":[{"type":"run","data":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"children":[{"type":"text","data":{"preserveSpace":true,"text":"pattern1"}}]}}],"property":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"style":null,"numberingProperty":null,"alignment":null,"indent":null,"lineHeight":null,"keepNext":false,"keepLines":false,"pageBreakBefore":false,"windowControl":false,"divId":null},"hasNumbering":false}}],"has_numbering":false}],"hasNumbering":false}}]}}]}]}"#
);
}
}

View File

@ -90,7 +90,7 @@ mod tests {
.num_style_link("style1");
assert_eq!(
serde_json::to_string(&c).unwrap(),
r#"{"id":0,"styleLink":null,"numStyleLink":"style1","levels":[{"level":1,"start":1,"format":"decimal","text":"%4.","jc":"left","pstyle":null,"paragraphProperty":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"style":null,"numberingProperty":null,"alignment":null,"indent":null,"lineHeight":null,"divId":null},"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"suffix":"tab"}]}"#
r#"{"id":0,"styleLink":null,"numStyleLink":"style1","levels":[{"level":1,"start":1,"format":"decimal","text":"%4.","jc":"left","pstyle":null,"paragraphProperty":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"style":null,"numberingProperty":null,"alignment":null,"indent":null,"lineHeight":null,"keepNext":false,"keepLines":false,"pageBreakBefore":false,"windowControl":false,"divId":null},"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"suffix":"tab"}]}"#
);
}
}

View File

@ -167,6 +167,26 @@ impl Paragraph {
self
}
pub fn keep_next(mut self, v: bool) -> Self {
self.property = self.property.keep_next(v);
self
}
pub fn keep_lines(mut self, v: bool) -> Self {
self.property = self.property.keep_lines(v);
self
}
pub fn page_break_before(mut self, v: bool) -> Self {
self.property = self.property.page_break_before(v);
self
}
pub fn window_control(mut self, v: bool) -> Self {
self.property = self.property.window_control(v);
self
}
pub fn indent(
mut self,
left: Option<i32>,
@ -305,7 +325,7 @@ mod tests {
let p = Paragraph::new().add_run(run);
assert_eq!(
serde_json::to_string(&p).unwrap(),
r#"{"id":"12345678","children":[{"type":"run","data":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"children":[{"type":"text","data":{"preserveSpace":true,"text":"Hello"}}]}}],"property":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"style":null,"numberingProperty":null,"alignment":null,"indent":null,"lineHeight":null,"divId":null},"hasNumbering":false}"#
r#"{"id":"12345678","children":[{"type":"run","data":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"children":[{"type":"text","data":{"preserveSpace":true,"text":"Hello"}}]}}],"property":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"style":null,"numberingProperty":null,"alignment":null,"indent":null,"lineHeight":null,"keepNext":false,"keepLines":false,"pageBreakBefore":false,"windowControl":false,"divId":null},"hasNumbering":false}"#
);
}
@ -316,7 +336,7 @@ mod tests {
let p = Paragraph::new().add_insert(ins);
assert_eq!(
serde_json::to_string(&p).unwrap(),
r#"{"id":"12345678","children":[{"type":"insert","data":{"children":[{"type":"run","data":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"children":[{"type":"text","data":{"preserveSpace":true,"text":"Hello"}}]}}],"author":"unnamed","date":"1970-01-01T00:00:00Z"}}],"property":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"style":null,"numberingProperty":null,"alignment":null,"indent":null,"lineHeight":null,"divId":null},"hasNumbering":false}"#
r#"{"id":"12345678","children":[{"type":"insert","data":{"children":[{"type":"run","data":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"children":[{"type":"text","data":{"preserveSpace":true,"text":"Hello"}}]}}],"author":"unnamed","date":"1970-01-01T00:00:00Z"}}],"property":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"style":null,"numberingProperty":null,"alignment":null,"indent":null,"lineHeight":null,"keepNext":false,"keepLines":false,"pageBreakBefore":false,"windowControl":false,"divId":null},"hasNumbering":false}"#
);
}
}

View File

@ -14,6 +14,10 @@ pub struct ParagraphProperty {
pub alignment: Option<Justification>,
pub indent: Option<Indent>,
pub line_height: Option<u32>,
pub keep_next: bool,
pub keep_lines: bool,
pub page_break_before: bool,
pub window_control: bool,
// read only
pub(crate) div_id: Option<String>,
}
@ -27,6 +31,10 @@ impl Default for ParagraphProperty {
alignment: None,
indent: None,
line_height: None,
keep_next: false,
keep_lines: false,
page_break_before: false,
window_control: false,
div_id: None,
}
}
@ -73,6 +81,26 @@ impl ParagraphProperty {
self
}
pub fn keep_next(mut self, v: bool) -> Self {
self.keep_next = v;
self
}
pub fn keep_lines(mut self, v: bool) -> Self {
self.keep_lines = v;
self
}
pub fn page_break_before(mut self, v: bool) -> Self {
self.page_break_before = v;
self
}
pub fn window_control(mut self, v: bool) -> Self {
self.window_control = v;
self
}
pub(crate) fn hanging_chars(mut self, chars: i32) -> Self {
if let Some(indent) = self.indent {
self.indent = Some(indent.hanging_chars(chars));
@ -95,16 +123,32 @@ impl BuildXML for ParagraphProperty {
} else {
None
};
XMLBuilder::new()
let mut b = XMLBuilder::new()
.open_paragraph_property()
.add_child(&self.run_property)
.add_optional_child(&self.style)
.add_optional_child(&self.numbering_property)
.add_optional_child(&self.alignment)
.add_optional_child(&self.indent)
.add_optional_child(&spacing)
.close()
.build()
.add_optional_child(&spacing);
if self.keep_next {
b = b.keep_next()
}
if self.keep_lines {
b = b.keep_lines()
}
if self.page_break_before {
b = b.page_break_before()
}
if self.window_control {
b = b.window_control()
}
b.close().build()
}
}
@ -143,13 +187,24 @@ mod tests {
);
}
#[test]
fn test_keep_next() {
let c = ParagraphProperty::new();
let b = c.keep_next(true).build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:pPr><w:rPr /><w:keepNext />
</w:pPr>"#
);
}
#[test]
fn test_indent_json() {
let c = ParagraphProperty::new();
let b = c.indent(Some(20), Some(SpecialIndentType::FirstLine(10)), None, None);
assert_eq!(
serde_json::to_string(&b).unwrap(),
r#"{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"style":null,"numberingProperty":null,"alignment":null,"indent":{"start":20,"startChars":null,"end":null,"specialIndent":{"type":"firstLine","val":10},"hangingChars":null,"firstLineChars":null},"lineHeight":null,"divId":null}"#
r#"{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"style":null,"numberingProperty":null,"alignment":null,"indent":{"start":20,"startChars":null,"end":null,"specialIndent":{"type":"firstLine","val":10},"hangingChars":null,"firstLineChars":null},"lineHeight":null,"keepNext":false,"keepLines":false,"pageBreakBefore":false,"windowControl":false,"divId":null}"#
);
}
}

View File

@ -185,7 +185,7 @@ mod tests {
.grid_span(2);
assert_eq!(
serde_json::to_string(&c).unwrap(),
r#"{"children":[{"type":"paragraph","data":{"id":"12345678","children":[{"type":"run","data":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"children":[{"type":"text","data":{"preserveSpace":true,"text":"Hello"}}]}}],"property":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"style":null,"numberingProperty":null,"alignment":null,"indent":null,"lineHeight":null,"divId":null},"hasNumbering":false}}],"property":{"width":null,"borders":null,"gridSpan":2,"verticalMerge":null,"verticalAlign":null,"textDirection":null,"shading":null},"hasNumbering":false}"#
r#"{"children":[{"type":"paragraph","data":{"id":"12345678","children":[{"type":"run","data":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"children":[{"type":"text","data":{"preserveSpace":true,"text":"Hello"}}]}}],"property":{"runProperty":{"sz":null,"szCs":null,"color":null,"highlight":null,"underline":null,"bold":null,"boldCs":null,"italic":null,"italicCs":null,"vanish":null,"spacing":null,"fonts":null,"textBorder":null},"style":null,"numberingProperty":null,"alignment":null,"indent":null,"lineHeight":null,"keepNext":false,"keepLines":false,"pageBreakBefore":false,"windowControl":false,"divId":null},"hasNumbering":false}}],"property":{"width":null,"borders":null,"gridSpan":2,"verticalMerge":null,"verticalAlign":null,"textDirection":null,"shading":null},"hasNumbering":false}"#
);
}
}

View File

@ -126,6 +126,18 @@ impl ElementReader for Paragraph {
p = p.run_property(run_pr);
continue;
}
XMLElement::KeepNext => {
p.property.keep_next = true;
}
XMLElement::KeepLines => {
p.property.keep_lines = true;
}
XMLElement::PageBreakBefore => {
p.property.page_break_before = true;
}
XMLElement::WindowControl => {
p.property.window_control = true;
}
_ => {}
}
}

View File

@ -40,6 +40,10 @@ pub enum XMLElement {
NumberingId,
Justification,
Insert,
KeepNext,
KeepLines,
PageBreakBefore,
WindowControl,
DivId,
Div,
DivsChild,
@ -310,6 +314,10 @@ impl FromStr for XMLElement {
"marTop" => Ok(XMLElement::MarginTop),
"marBottom" => Ok(XMLElement::MarginBottom),
"webSettings" => Ok(XMLElement::WebSettings),
"keepNext" => Ok(XMLElement::KeepNext),
"keepLines" => Ok(XMLElement::KeepLines),
"pageBreakBefore" => Ok(XMLElement::PageBreakBefore),
"windowControl" => Ok(XMLElement::WindowControl),
_ => Ok(XMLElement::Unsupported),
}
}

View File

@ -309,6 +309,11 @@ impl XMLBuilder {
"w:val"
);
closed!(keep_next, "w:keepNext");
closed!(keep_lines, "w:keepLines");
closed!(page_break_before, "w:pageBreakBefore");
closed!(window_control, "w:windowControl");
/*
<w:lvlOverride w:ilvl="0">
<w:startOverride w:val="1"/>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -399,6 +399,22 @@ export class Docx {
paragraph = paragraph.fonts(f);
}
if (p.property.keepLines) {
paragraph = paragraph.keep_lines(true);
}
if (p.property.keepNext) {
paragraph = paragraph.keep_next(true);
}
if (p.property.pageBreakBefore) {
paragraph = paragraph.page_break_before(true);
}
if (p.property.windowControl) {
paragraph = paragraph.window_control(true);
}
return paragraph;
}

View File

@ -24,6 +24,10 @@ export type ParagraphPropertyJSON = {
indent: IndentJSON | null;
lineHeight: number | null;
divId: string | null;
keepNext: boolean;
keepLines: boolean;
pageBreakBefore: boolean;
windowControl: boolean;
};
export type ParagraphJSON = {

View File

@ -1,5 +1,9 @@
import { LevelJSON } from "./json";
import { ParagraphProperty, SpecialIndentKind } from "./paragraph";
import {
createDefaultParagraphProperty,
ParagraphProperty,
SpecialIndentKind,
} from "./paragraph";
import { RunFonts, RunProperty } from "./run";
export type LevelSuffixType = "nothing" | "tab" | "space";
@ -10,7 +14,7 @@ export class Level {
format: string;
text: string;
jc: string;
paragraphProperty: ParagraphProperty = { runProperty: {} };
paragraphProperty: ParagraphProperty = createDefaultParagraphProperty();
runProperty: RunProperty = {};
levelSuffix: LevelSuffixType;

View File

@ -1,4 +1,4 @@
import { Run, RunProperty, RunFonts } from "./run";
import { Run, RunProperty, RunFonts, createDefaultRunProperty } from "./run";
import { Insert } from "./insert";
import { Delete } from "./delete";
import { BookmarkStart } from "./bookmark-start";
@ -40,14 +40,26 @@ export type ParagraphProperty = {
};
lineHeight?: number;
runProperty: RunProperty;
keepNext: boolean;
keepLines: boolean;
pageBreakBefore: boolean;
windowControl: boolean;
};
export const createDefaultParagraphProperty = (): ParagraphProperty => {
return {
runProperty: createDefaultRunProperty(),
keepNext: false,
keepLines: false,
pageBreakBefore: false,
windowControl: false,
};
};
export class Paragraph {
hasNumberings = false;
children: ParagraphChild[] = [];
property: ParagraphProperty = {
runProperty: {},
};
property: ParagraphProperty = createDefaultParagraphProperty();
addRun(run: Run) {
this.children.push(run);
@ -114,6 +126,26 @@ export class Paragraph {
return this;
}
keepNext(v: boolean) {
this.property = { ...this.property, keepNext: v };
return this;
}
keepLines(v: boolean) {
this.property = { ...this.property, keepLines: v };
return this;
}
pageBreakBefore(v: boolean) {
this.property = { ...this.property, pageBreakBefore: v };
return this;
}
windowControl(v: boolean) {
this.property = { ...this.property, windowControl: v };
return this;
}
// run property
size(size: number) {
this.property.runProperty = { ...this.property.runProperty, size };

View File

@ -26,6 +26,10 @@ export type RunProperty = {
textBorder?: TextBorder;
};
export const createDefaultRunProperty = (): RunProperty => {
return {};
};
export class RunFonts {
_ascii?: string;
_hiAnsi?: string;

View File

@ -1,5 +1,5 @@
import { RunProperty } from "./run";
import { ParagraphProperty } from "./paragraph";
import { RunProperty, createDefaultRunProperty } from "./run";
import { createDefaultParagraphProperty, ParagraphProperty } from "./paragraph";
import { TableProperty } from "./table";
export type StyleType =
@ -22,8 +22,8 @@ export class Style {
this._styleId = id;
this._styleType = type;
this._name = "";
this._runProperty = {};
this._paragraphProperty = { runProperty: {} };
this._runProperty = createDefaultRunProperty();
this._paragraphProperty = createDefaultParagraphProperty();
this._tableProperty = {};
this._basedOn = null;
}

View File

@ -1,6 +1,6 @@
{
"name": "docx-wasm",
"version": "0.0.189",
"version": "0.0.190",
"main": "dist/node/index.js",
"browser": "dist/web/index.js",
"author": "bokuweb <bokuweb12@gmail.com>",

View File

@ -116,6 +116,26 @@ impl Paragraph {
self.0 = self.0.line_height(line_height);
self
}
pub fn keep_next(mut self, v: bool) -> Self {
self.0 = self.0.keep_next(v);
self
}
pub fn keep_lines(mut self, v: bool) -> Self {
self.0 = self.0.keep_lines(v);
self
}
pub fn page_break_before(mut self, v: bool) -> Self {
self.0 = self.0.page_break_before(v);
self
}
pub fn window_control(mut self, v: bool) -> Self {
self.0 = self.0.window_control(v);
self
}
}
impl Paragraph {

File diff suppressed because it is too large Load Diff