From 4aeca719044cb22f4809632201bcc180d841981d Mon Sep 17 00:00:00 2001 From: bokuweb Date: Wed, 26 Feb 2025 14:42:28 +0900 Subject: [PATCH] feat: Support snaptogrid --- docx-core/src/documents/elements/paragraph.rs | 6 ++++ .../documents/elements/table_of_contents.rs | 6 +++- docx-wasm/js/paragraph-property.ts | 15 ++++++--- docx-wasm/js/paragraph.ts | 2 +- .../test/__snapshots__/index.test.js.snap | 16 ++++++---- docx-wasm/test/index.test.js | 32 +++++++++++++++++++ 6 files changed, 64 insertions(+), 13 deletions(-) diff --git a/docx-core/src/documents/elements/paragraph.rs b/docx-core/src/documents/elements/paragraph.rs index f22bc8ff..a150ef69 100644 --- a/docx-core/src/documents/elements/paragraph.rs +++ b/docx-core/src/documents/elements/paragraph.rs @@ -359,6 +359,12 @@ impl Paragraph { self } + // internal + pub(crate) fn paragraph_property(mut self, p: ParagraphProperty) -> Self { + self.property = p; + self + } + pub fn raw_text(&self) -> String { let mut s = "".to_string(); // For now support only run and ins. diff --git a/docx-core/src/documents/elements/table_of_contents.rs b/docx-core/src/documents/elements/table_of_contents.rs index c7f18329..8a356f19 100644 --- a/docx-core/src/documents/elements/table_of_contents.rs +++ b/docx-core/src/documents/elements/table_of_contents.rs @@ -194,7 +194,7 @@ impl BuildXML for TableOfContents { } } - let p1 = if let Some(ref del) = self.delete { + let mut p1 = if let Some(ref del) = self.delete { Paragraph::new().add_delete( Delete::new().author(&del.author).date(&del.date).add_run( Run::new() @@ -212,6 +212,10 @@ impl BuildXML for TableOfContents { ) }; + if let Some(ref p) = self.paragraph_property { + p1 = p1.paragraph_property(p.clone()); + } + b = b.add_child(&p1)?; let p2 = Paragraph::new().add_run(Run::new().add_field_char(FieldCharType::End, false)); diff --git a/docx-wasm/js/paragraph-property.ts b/docx-wasm/js/paragraph-property.ts index 614097da..0e9d3197 100644 --- a/docx-wasm/js/paragraph-property.ts +++ b/docx-wasm/js/paragraph-property.ts @@ -84,7 +84,7 @@ export class ParagraphProperty { widowControl: boolean; paragraphPropertyChange?: ParagraphPropertyChange; outlineLvl?: number | null; - snapToGrid?: boolean; + _snapToGrid?: boolean; _adjustRightInd?: number; _tabs?: Tab[]; frameProperty?: FrameProperty; @@ -117,6 +117,11 @@ export class ParagraphProperty { return this; } + snapToGrid(v: boolean) { + this._snapToGrid = v; + return this; + } + style(id: string) { this.styleId = id; return this; @@ -375,8 +380,8 @@ export const setParagraphProperty = ( target = target.keep_lines(true) as T; } - if (property.snapToGrid != null) { - target = target.snap_to_grid(!!property.snapToGrid) as T; + if (property._snapToGrid != null) { + target = target.snap_to_grid(!!property._snapToGrid) as T; } if (property.keepNext) { @@ -586,8 +591,8 @@ export const createParagraphProperty = ( p = p.keep_lines(true); } - if (property.snapToGrid != null) { - p = p.snap_to_grid(!!property.snapToGrid); + if (property._snapToGrid != null) { + p = p.snap_to_grid(!!property._snapToGrid); } if (property.keepNext) { diff --git a/docx-wasm/js/paragraph.ts b/docx-wasm/js/paragraph.ts index 5af0c2e9..0199462f 100644 --- a/docx-wasm/js/paragraph.ts +++ b/docx-wasm/js/paragraph.ts @@ -151,7 +151,7 @@ export class Paragraph { } snapToGrid(v: boolean) { - this.property.snapToGrid = v; + this.property._snapToGrid = v; return this; } diff --git a/docx-wasm/test/__snapshots__/index.test.js.snap b/docx-wasm/test/__snapshots__/index.test.js.snap index 7f375e09..d5474576 100644 --- a/docx-wasm/test/__snapshots__/index.test.js.snap +++ b/docx-wasm/test/__snapshots__/index.test.js.snap @@ -171035,7 +171035,7 @@ Object { exports[`writer should write ToC with TC 1`] = `""`; -exports[`writer should write ToC with TC 2`] = `"TOC \\\\fHello!!TC \\"Hello!!TC\\" \\\\l 1WorldTC \\"World!!TC\\" \\\\l 1"`; +exports[`writer should write ToC with TC 2`] = `"TOC \\\\fHello!!TC \\"Hello!!TC\\" \\\\l 1WorldTC \\"World!!TC\\" \\\\l 1"`; exports[`writer should write ToC with instrText 1`] = `""`; @@ -171043,15 +171043,15 @@ exports[`writer should write ToC with instrText 2`] = `""`; -exports[`writer should write ToC with instrText TC 2`] = `"TOC \\\\f \\\\h \\\\u \\\\zHello!!TC \\"Hello!!TC\\" \\\\l 1WorldTC \\"World!!TC\\" \\\\l 1"`; +exports[`writer should write ToC with instrText TC 2`] = `"TOC \\\\f \\\\h \\\\u \\\\zHello!!TC \\"Hello!!TC\\" \\\\l 1WorldTC \\"World!!TC\\" \\\\l 1"`; exports[`writer should write ToC with instrText TC escaped text 1`] = `""`; -exports[`writer should write ToC with instrText TC escaped text 2`] = `"TOC \\\\f "abc" \\\\h \\\\u \\\\zHello!!TC \\"Hello!!<div>\\" \\\\f abc \\\\l 1WorldTC \\"World!!TC\\" \\\\l 1"`; +exports[`writer should write ToC with instrText TC escaped text 2`] = `"TOC \\\\f "abc" \\\\h \\\\u \\\\zHello!!TC \\"Hello!!<div>\\" \\\\f abc \\\\l 1WorldTC \\"World!!TC\\" \\\\l 1"`; exports[`writer should write ToC with instrText TC with id 1`] = `""`; -exports[`writer should write ToC with instrText TC with id 2`] = `"TOC \\\\f "abc" \\\\h \\\\u \\\\zHello!!TC \\"Hello!!TC\\" \\\\f abc \\\\l 1WorldTC \\"World!!TC\\" \\\\l 1"`; +exports[`writer should write ToC with instrText TC with id 2`] = `"TOC \\\\f "abc" \\\\h \\\\u \\\\zHello!!TC \\"Hello!!TC\\" \\\\f abc \\\\l 1WorldTC \\"World!!TC\\" \\\\l 1"`; exports[`writer should write ToC with items 1`] = `""`; @@ -171059,11 +171059,15 @@ exports[`writer should write ToC with items 2`] = `""`; -exports[`writer should write ToC with paragraphProperty 2`] = `"TOCHello!!World"`; +exports[`writer should write ToC with paragraphProperty 2`] = `"TOCHello!!World"`; + +exports[`writer should write ToC with paragraphProperty 3`] = `""`; + +exports[`writer should write ToC with paragraphProperty 4`] = `"TOC \\\\f "abc" \\\\h \\\\u \\\\zHello!!TC \\"Hello!!<div>\\" \\\\f abc \\\\l 1WorldTC \\"World!!TC\\" \\\\l 1"`; exports[`writer should write ToC with paragraphProperty without sdt 1`] = `""`; -exports[`writer should write ToC with paragraphProperty without sdt 2`] = `"TOCHello!!World"`; +exports[`writer should write ToC with paragraphProperty without sdt 2`] = `"TOCHello!!World"`; exports[`writer should write align 1`] = `""`; diff --git a/docx-wasm/test/index.test.js b/docx-wasm/test/index.test.js index 3581185a..9f46ef19 100644 --- a/docx-wasm/test/index.test.js +++ b/docx-wasm/test/index.test.js @@ -1279,4 +1279,36 @@ describe("writer", () => { } } }); + + test("should write ToC with paragraphProperty", () => { + const p1 = new w.Paragraph() + .addRun(new w.Run().addText("Hello!!")) + .addRun( + new w.Run().addTc(new w.Tc("Hello!!
").level(1).identifier("abc")) + ) + .pageBreakBefore(true); + const p2 = new w.Paragraph() + .addRun(new w.Run().addText("World")) + .addRun(new w.Run().addTc(new w.Tc("World!!TC").level(1))) + .pageBreakBefore(true); + const buffer = new w.Docx() + .addTableOfContents( + new w.TableOfContents("TOC \\f abc \\h \\z \\u") + .alias("Table of contents") + .dirty() + .paragraphProperty( + new w.ParagraphProperty().style("11").snapToGrid(false) + ) + ) + .addParagraph(p1) + .addParagraph(p2) + .build(); + writeFileSync("../output/js/toc_with_paragraph_property.docx", buffer); + const z = new Zip(Buffer.from(buffer)); + for (const e of z.getEntries()) { + if (e.entryName.match(/document.xml/)) { + expect(z.readAsText(e)).toMatchSnapshot(); + } + } + }); });