Skip to content

Commit

Permalink
feat: Support snaptogrid
Browse files Browse the repository at this point in the history
  • Loading branch information
bokuweb committed Feb 26, 2025
1 parent 5468036 commit 4aeca71
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 13 deletions.
6 changes: 6 additions & 0 deletions docx-core/src/documents/elements/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion docx-core/src/documents/elements/table_of_contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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));
Expand Down
15 changes: 10 additions & 5 deletions docx-wasm/js/paragraph-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class ParagraphProperty {
widowControl: boolean;
paragraphPropertyChange?: ParagraphPropertyChange;
outlineLvl?: number | null;
snapToGrid?: boolean;
_snapToGrid?: boolean;
_adjustRightInd?: number;
_tabs?: Tab[];
frameProperty?: FrameProperty;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -375,8 +380,8 @@ export const setParagraphProperty = <T extends wasm.Paragraph | wasm.Style>(
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) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion docx-wasm/js/paragraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class Paragraph {
}

snapToGrid(v: boolean) {
this.property.snapToGrid = v;
this.property._snapToGrid = v;
return this;
}

Expand Down
16 changes: 10 additions & 6 deletions docx-wasm/test/__snapshots__/index.test.js.snap

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions docx-wasm/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!!<div>").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();
}
}
});
});

0 comments on commit 4aeca71

Please sign in to comment.