From c12ca9d0425022f906bfd217d35514e4c6dd4955 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Fri, 15 May 2020 15:51:45 +0900 Subject: [PATCH] fix: numbering for js (#71) --- docx-wasm/js/index.ts | 26 ++++++++++++++++++++++++-- docx-wasm/js/paragraph.ts | 2 ++ docx-wasm/js/table-cell.ts | 4 ++++ docx-wasm/js/table-row.ts | 4 ++++ docx-wasm/js/table.ts | 4 ++++ docx-wasm/src/doc.rs | 5 ++++- 6 files changed, 42 insertions(+), 3 deletions(-) diff --git a/docx-wasm/js/index.ts b/docx-wasm/js/index.ts index ac6e840..f34a81f 100644 --- a/docx-wasm/js/index.ts +++ b/docx-wasm/js/index.ts @@ -48,15 +48,22 @@ const convertBorderType = (t: BorderType) => { export class Docx { children: (Paragraph | Table)[] = []; + hasNumberings = false; abstractNumberings: AbstractNumbering[] = []; numberings: Numbering[] = []; addParagraph(p: Paragraph) { + if (p.hasNumberings) { + this.hasNumberings = true; + } this.children.push(p); return this; } addTable(t: Table) { + if (t.hasNumberings) { + this.hasNumberings = true; + } this.children.push(t); return this; } @@ -380,7 +387,22 @@ export class Docx { this.abstractNumberings.forEach((n) => { let num = wasm.createAbstractNumbering(n.id); n.levels.forEach((l) => { - const level = wasm.createLevel(l.id, l.start, l.format, l.text, l.jc); + let level = wasm.createLevel(l.id, l.start, l.format, l.text, l.jc); + if (l.paragraphProperty.indent) { + let kind; + if (l.paragraphProperty.indent.specialIndentKind === "firstLine") { + kind = wasm.SpecialIndentKind.FirstLine; + } else if ( + l.paragraphProperty.indent.specialIndentKind === "hanging" + ) { + kind = wasm.SpecialIndentKind.Hanging; + } + level = level.indent( + l.paragraphProperty.indent.left, + kind, + l.paragraphProperty.indent.specialIndentSize + ); + } num = num.add_level(level); }); docx = docx.add_abstract_numbering(num); @@ -391,7 +413,7 @@ export class Docx { docx = docx.add_numbering(num); }); - const buf = docx.build(); + const buf = docx.build(this.hasNumberings); docx.free(); return buf; } diff --git a/docx-wasm/js/paragraph.ts b/docx-wasm/js/paragraph.ts index d5f4e0a..ce7d6df 100644 --- a/docx-wasm/js/paragraph.ts +++ b/docx-wasm/js/paragraph.ts @@ -34,6 +34,7 @@ export type ParagraphProperty = { }; export class Paragraph { + hasNumberings = false; children: ParagraphChild[] = []; property: ParagraphProperty = {}; @@ -92,6 +93,7 @@ export class Paragraph { } numbering(id: number, level: number) { + this.hasNumberings = true; this.property.numbering = { id, level }; return this; } diff --git a/docx-wasm/js/table-cell.ts b/docx-wasm/js/table-cell.ts index c91c271..aac2598 100644 --- a/docx-wasm/js/table-cell.ts +++ b/docx-wasm/js/table-cell.ts @@ -16,11 +16,15 @@ export type CellProperty = { export class TableCell { children: Paragraph[] = []; + hasNumberings = false; property: CellProperty = { borders: new TableCellBorders(), }; addParagraph(p: Paragraph) { + if (p.hasNumberings) { + this.hasNumberings = true; + } this.children.push(p); return this; } diff --git a/docx-wasm/js/table-row.ts b/docx-wasm/js/table-row.ts index 1f12b25..de9e806 100644 --- a/docx-wasm/js/table-row.ts +++ b/docx-wasm/js/table-row.ts @@ -2,8 +2,12 @@ import { TableCell } from "./table-cell"; export class TableRow { cells: TableCell[] = []; + hasNumberings = false; addCell(cell: TableCell) { + if (cell.hasNumberings) { + this.hasNumberings = true; + } this.cells.push(cell); this; } diff --git a/docx-wasm/js/table.ts b/docx-wasm/js/table.ts index b3a7668..bd79fdf 100644 --- a/docx-wasm/js/table.ts +++ b/docx-wasm/js/table.ts @@ -9,11 +9,15 @@ export type TableProperty = { }; export class Table { + hasNumberings = false; rows: TableRow[] = []; grid: number[] = []; property: TableProperty = {}; addRow(row: TableRow) { + if (row.hasNumberings) { + this.hasNumberings = true; + } this.rows.push(row); return this; } diff --git a/docx-wasm/src/doc.rs b/docx-wasm/src/doc.rs index 4ee6db0..9dac7ee 100644 --- a/docx-wasm/src/doc.rs +++ b/docx-wasm/src/doc.rs @@ -33,9 +33,12 @@ impl Docx { self } - pub fn build(&mut self) -> Result, JsValue> { + pub fn build(&mut self, has_numberings: bool) -> Result, JsValue> { let buf = Vec::new(); let mut cur = std::io::Cursor::new(buf); + if has_numberings { + self.0.document_rels.has_numberings = true; + } let res = self.0.build().pack(&mut cur); if res.is_err() { return Err(format!("{:?}", res).into());