fix: numbering for js (#71)
parent
ed212793c5
commit
c12ca9d042
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -33,9 +33,12 @@ impl Docx {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn build(&mut self) -> Result<Vec<u8>, JsValue> {
|
||||
pub fn build(&mut self, has_numberings: bool) -> Result<Vec<u8>, 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());
|
||||
|
|
Loading…
Reference in New Issue