From 22c6724d057aa4cc1cb83c4c9e5832238831abdf Mon Sep 17 00:00:00 2001 From: bokuweb Date: Mon, 17 Jan 2022 15:27:24 +0900 Subject: [PATCH] fix: js ppr change if (#408) --- docx-core/src/documents/elements/paragraph.rs | 19 +++- .../elements/paragraph_property_change.rs | 1 + docx-wasm/js/index.ts | 19 +++- docx-wasm/js/level.ts | 2 +- docx-wasm/js/paragraph-property.ts | 100 +++++++++++++++++ docx-wasm/js/paragraph.ts | 102 +++++------------- docx-wasm/js/run.ts | 22 ++++ docx-wasm/js/style.ts | 2 +- docx-wasm/src/paragraph.rs | 40 +++++++ .../test/__snapshots__/index.test.js.snap | 27 +++++ docx-wasm/test/index.test.js | 24 +++++ 11 files changed, 275 insertions(+), 83 deletions(-) create mode 100644 docx-wasm/js/paragraph-property.ts diff --git a/docx-core/src/documents/elements/paragraph.rs b/docx-core/src/documents/elements/paragraph.rs index 3f411fb..3004493 100644 --- a/docx-core/src/documents/elements/paragraph.rs +++ b/docx-core/src/documents/elements/paragraph.rs @@ -297,6 +297,21 @@ impl Paragraph { self } + pub fn delete(mut self, author: impl Into, date: impl Into) -> Self { + self.property.run_property.del = Some(Delete::new().author(author).date(date)); + self + } + + pub fn insert(mut self, author: impl Into, date: impl Into) -> Self { + self.property.run_property.ins = Some(Insert::new_with_empty().author(author).date(date)); + self + } + + pub fn paragraph_property_change(mut self, p: ParagraphPropertyChange) -> Self { + self.property = self.property.paragraph_property_change(p); + self + } + pub fn raw_text(&self) -> String { let mut s = "".to_string(); // For now support only run and ins. @@ -447,5 +462,5 @@ mod tests { .add_delete(Delete::new().add_run(Run::new().add_delete_text("!!!!!"))) .raw_text(); assert_eq!(b, "HelloWorld".to_owned()); - }} - + } +} diff --git a/docx-core/src/documents/elements/paragraph_property_change.rs b/docx-core/src/documents/elements/paragraph_property_change.rs index 1c10291..4763c75 100644 --- a/docx-core/src/documents/elements/paragraph_property_change.rs +++ b/docx-core/src/documents/elements/paragraph_property_change.rs @@ -4,6 +4,7 @@ use crate::documents::*; use crate::xml_builder::*; #[derive(Serialize, Debug, Clone, PartialEq)] +#[serde(rename_all = "camelCase")] pub struct ParagraphPropertyChange { pub author: String, pub date: String, diff --git a/docx-wasm/js/index.ts b/docx-wasm/js/index.ts index e2753a0..0bbe33a 100644 --- a/docx-wasm/js/index.ts +++ b/docx-wasm/js/index.ts @@ -1,4 +1,5 @@ -import { Paragraph, ParagraphProperty } from "./paragraph"; +import { Paragraph } from "./paragraph"; +import { ParagraphProperty } from "./paragraph-property"; import { Insert } from "./insert"; import { Delete } from "./delete"; import { Hyperlink } from "./hyperlink"; @@ -607,6 +608,21 @@ export class Docx { paragraph = paragraph.widow_control(true); } + if (p.property.paragraphPropertyChange) { + let change = wasm.createParagraphPropertyChange(); + change = change + .author(p.property.paragraphPropertyChange._author) + .date(p.property.paragraphPropertyChange._date); + + if (p.property.paragraphPropertyChange._property.numbering) { + change = change.numbering( + p.property.paragraphPropertyChange._property.numbering.id, + p.property.paragraphPropertyChange._property.numbering.level + ); + } + paragraph = paragraph.paragraph_property_change(change); + } + return paragraph; } @@ -1145,6 +1161,7 @@ export const readDocx = (buf: Uint8Array) => { }; export * from "./paragraph"; +export * from "./paragraph-property"; export * from "./insert"; export * from "./delete"; export * from "./border"; diff --git a/docx-wasm/js/level.ts b/docx-wasm/js/level.ts index 88aabd3..7636040 100644 --- a/docx-wasm/js/level.ts +++ b/docx-wasm/js/level.ts @@ -3,7 +3,7 @@ import { createDefaultParagraphProperty, ParagraphProperty, SpecialIndentKind, -} from "./paragraph"; +} from "./paragraph-property"; import { RunFonts, RunProperty } from "./run"; export type LevelSuffixType = "nothing" | "tab" | "space"; diff --git a/docx-wasm/js/paragraph-property.ts b/docx-wasm/js/paragraph-property.ts new file mode 100644 index 0000000..cdbd680 --- /dev/null +++ b/docx-wasm/js/paragraph-property.ts @@ -0,0 +1,100 @@ +import { RunProperty, createDefaultRunProperty } from "./run"; + +export type AlignmentType = + | "center" + | "left" + | "right" + | "both" + | "justified" + | "distribute" + | "end"; + +export type SpecialIndentKind = "firstLine" | "hanging"; + +export type LineSpacingType = "atLeast" | "auto" | "exact"; + +export class LineSpacing { + _before?: number; + _after?: number; + _beforeLines?: number; + _afterLines?: number; + _line?: number; + _lineRule?: LineSpacingType; + + before(v: number) { + this._before = v; + return this; + } + after(v: number) { + this._after = v; + return this; + } + beforeLines(v: number) { + this._beforeLines = v; + return this; + } + afterLines(v: number) { + this._afterLines = v; + return this; + } + line(v: number) { + this._line = v; + return this; + } + lineRule(v: LineSpacingType) { + this._lineRule = v; + return this; + } +} + +export type ParagraphProperty = { + align?: AlignmentType; + styleId?: string; + indent?: { + left: number; + specialIndentKind?: SpecialIndentKind; + specialIndentSize?: number; + }; + numbering?: { + id: number; + level: number; + }; + lineSpacing?: LineSpacing; + runProperty: RunProperty; + keepNext: boolean; + keepLines: boolean; + pageBreakBefore: boolean; + widowControl: boolean; + paragraphPropertyChange?: ParagraphPropertyChange; +}; + +export const createDefaultParagraphProperty = (): ParagraphProperty => { + return { + runProperty: createDefaultRunProperty(), + keepNext: false, + keepLines: false, + pageBreakBefore: false, + widowControl: false, + }; +}; + +export class ParagraphPropertyChange { + _author: string = ""; + _date: string = ""; + _property: ParagraphProperty = createDefaultParagraphProperty(); + + author(a: string) { + this._author = a; + return this; + } + + date(a: string) { + this._date = a; + return this; + } + + property(p: ParagraphProperty) { + this._property = p; + return this; + } +} diff --git a/docx-wasm/js/paragraph.ts b/docx-wasm/js/paragraph.ts index f5b236f..ae22696 100644 --- a/docx-wasm/js/paragraph.ts +++ b/docx-wasm/js/paragraph.ts @@ -1,4 +1,12 @@ -import { Run, RunProperty, RunFonts, createDefaultRunProperty } from "./run"; +import { Run, RunFonts } from "./run"; +import { + createDefaultParagraphProperty, + ParagraphProperty, + LineSpacing, + AlignmentType, + SpecialIndentKind, + ParagraphPropertyChange, +} from "./paragraph-property"; import { Insert } from "./insert"; import { Delete } from "./delete"; import { BookmarkStart } from "./bookmark-start"; @@ -17,83 +25,6 @@ export type ParagraphChild = | Comment | CommentEnd; -export type AlignmentType = - | "center" - | "left" - | "right" - | "both" - | "justified" - | "distribute" - | "end"; - -export type SpecialIndentKind = "firstLine" | "hanging"; - -export type LineSpacingType = "atLeast" | "auto" | "exact"; - -export class LineSpacing { - _before?: number; - _after?: number; - _beforeLines?: number; - _afterLines?: number; - _line?: number; - _lineRule?: LineSpacingType; - - before(v: number) { - this._before = v; - return this; - } - after(v: number) { - this._after = v; - return this; - } - beforeLines(v: number) { - this._beforeLines = v; - return this; - } - afterLines(v: number) { - this._afterLines = v; - return this; - } - line(v: number) { - this._line = v; - return this; - } - lineRule(v: LineSpacingType) { - this._lineRule = v; - return this; - } -} - -export type ParagraphProperty = { - align?: AlignmentType; - styleId?: string; - indent?: { - left: number; - specialIndentKind?: SpecialIndentKind; - specialIndentSize?: number; - }; - numbering?: { - id: number; - level: number; - }; - lineSpacing?: LineSpacing; - runProperty: RunProperty; - keepNext: boolean; - keepLines: boolean; - pageBreakBefore: boolean; - widowControl: boolean; -}; - -export const createDefaultParagraphProperty = (): ParagraphProperty => { - return { - runProperty: createDefaultRunProperty(), - keepNext: false, - keepLines: false, - pageBreakBefore: false, - widowControl: false, - }; -}; - export class Paragraph { hasNumberings = false; children: ParagraphChild[] = []; @@ -209,4 +140,19 @@ export class Paragraph { this.property.runProperty = { ...this.property.runProperty, fonts }; return this; } + + delete(author: string, date: string) { + this.property.runProperty.del = { author, date }; + return this; + } + + insert(author: string, date: string) { + this.property.runProperty.ins = { author, date }; + return this; + } + + paragraphPropertyChange(propertyChange: ParagraphPropertyChange) { + this.property.paragraphPropertyChange = propertyChange; + return this; + } } diff --git a/docx-wasm/js/run.ts b/docx-wasm/js/run.ts index 6e41ffe..e6a9e21 100644 --- a/docx-wasm/js/run.ts +++ b/docx-wasm/js/run.ts @@ -17,6 +17,16 @@ export type TextBorder = { export type VertAlignType = "baseline" | "superscript" | "subscript"; +export type RunPropertyDel = { + author: string; + date: string; +}; + +export type RunPropertyIns = { + author: string; + date: string; +}; + export type RunProperty = { size?: number; color?: string; @@ -30,6 +40,8 @@ export type RunProperty = { fonts?: RunFonts; spacing?: number; textBorder?: TextBorder; + ins?: RunPropertyIns; + del?: RunPropertyDel; }; export const createDefaultRunProperty = (): RunProperty => { @@ -183,6 +195,16 @@ export class Run { return this; } + delete(author: string, date: string) { + this.property = { ...this.property, del: { author, date } }; + return this; + } + + insert(author: string, date: string) { + this.property = { ...this.property, ins: { author, date } }; + return this; + } + textBorder(type: BorderType, size: number, space: number, color: string) { this.property = { ...this.property, diff --git a/docx-wasm/js/style.ts b/docx-wasm/js/style.ts index 96060b7..d4f42ff 100644 --- a/docx-wasm/js/style.ts +++ b/docx-wasm/js/style.ts @@ -2,7 +2,7 @@ import * as wasm from "./pkg"; import { createDefaultTableCellMargins, TableProperty } from "./table"; import { RunProperty, createDefaultRunProperty } from "./run"; -import { createDefaultParagraphProperty, ParagraphProperty } from "./paragraph"; +import { createDefaultParagraphProperty, ParagraphProperty } from "./paragraph-property"; export type StyleType = | "paragraph" diff --git a/docx-wasm/src/paragraph.rs b/docx-wasm/src/paragraph.rs index 9922c75..fdb0d3f 100644 --- a/docx-wasm/src/paragraph.rs +++ b/docx-wasm/src/paragraph.rs @@ -1,6 +1,41 @@ use super::*; use wasm_bindgen::prelude::*; +#[wasm_bindgen] +pub struct ParagraphPropertyChange(docx_rs::ParagraphPropertyChange); + +#[wasm_bindgen(js_name = createParagraphPropertyChange)] +pub fn create_paragraph_property_change() -> ParagraphPropertyChange { + ParagraphPropertyChange(docx_rs::ParagraphPropertyChange::new()) +} + +#[wasm_bindgen] +impl ParagraphPropertyChange { + pub fn author(mut self, author: &str) -> Self { + self.0 = self.0.author(author); + self + } + + pub fn date(mut self, date: &str) -> Self { + self.0 = self.0.date(date); + self + } + + // TODO: For now only numbering supported. + pub fn numbering(mut self, id: usize, level: usize) -> Self { + let id = docx_rs::NumberingId::new(id); + let level = docx_rs::IndentLevel::new(level); + self.0.property = Box::new(self.0.property.numbering(id, level)); + self + } +} + +impl ParagraphPropertyChange { + pub fn take(self) -> docx_rs::ParagraphPropertyChange { + self.0 + } +} + #[wasm_bindgen] #[derive(Debug)] pub struct Paragraph(docx_rs::Paragraph); @@ -141,6 +176,11 @@ impl Paragraph { self.0 = self.0.widow_control(v); self } + + pub fn paragraph_property_change(mut self, p: ParagraphPropertyChange) -> Self { + self.0.property = self.0.property.paragraph_property_change(p.take()); + self + } } impl Paragraph { diff --git a/docx-wasm/test/__snapshots__/index.test.js.snap b/docx-wasm/test/__snapshots__/index.test.js.snap index 2033057..7413f1b 100644 --- a/docx-wasm/test/__snapshots__/index.test.js.snap +++ b/docx-wasm/test/__snapshots__/index.test.js.snap @@ -22583,6 +22583,33 @@ exports[`writer should write nested table 3`] = ` " `; +exports[`writer should write pPrChange with inserted numbering 1`] = ` +" + + + + + + +" +`; + +exports[`writer should write pPrChange with inserted numbering 2`] = ` +" + + Hello world!! +" +`; + +exports[`writer should write pPrChange with inserted numbering 3`] = ` +" + + + + +" +`; + exports[`writer should write page margin 1`] = ` " diff --git a/docx-wasm/test/index.test.js b/docx-wasm/test/index.test.js index d6fbb7c..df2a388 100644 --- a/docx-wasm/test/index.test.js +++ b/docx-wasm/test/index.test.js @@ -596,4 +596,28 @@ describe("writer", () => { } } }); + + test("should write pPrChange with inserted numbering", () => { + const p = new w.Paragraph() + .addRun(new w.Run().addText("Hello world!!")) + .numbering(1, 0) + .paragraphPropertyChange( + new w.ParagraphPropertyChange().author("bokuweb") + ); + + const num = new w.Numbering(1, 0); + const buffer = new w.Docx().addParagraph(p).addNumbering(num).build(); + + writeFileSync( + "../output/js/pprchange_with_inserted_numbering.docx", + buffer + ); + + const z = new Zip(Buffer.from(buffer)); + for (const e of z.getEntries()) { + if (e.entryName.match(/document.xml|numbering.xml/)) { + expect(z.readAsText(e)).toMatchSnapshot(); + } + } + }); });