fix: numbering for js (#71)

main
bokuweb 2020-05-15 15:51:45 +09:00 committed by GitHub
parent ed212793c5
commit c12ca9d042
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 3 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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());