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 { export class Docx {
children: (Paragraph | Table)[] = []; children: (Paragraph | Table)[] = [];
hasNumberings = false;
abstractNumberings: AbstractNumbering[] = []; abstractNumberings: AbstractNumbering[] = [];
numberings: Numbering[] = []; numberings: Numbering[] = [];
addParagraph(p: Paragraph) { addParagraph(p: Paragraph) {
if (p.hasNumberings) {
this.hasNumberings = true;
}
this.children.push(p); this.children.push(p);
return this; return this;
} }
addTable(t: Table) { addTable(t: Table) {
if (t.hasNumberings) {
this.hasNumberings = true;
}
this.children.push(t); this.children.push(t);
return this; return this;
} }
@ -380,7 +387,22 @@ export class Docx {
this.abstractNumberings.forEach((n) => { this.abstractNumberings.forEach((n) => {
let num = wasm.createAbstractNumbering(n.id); let num = wasm.createAbstractNumbering(n.id);
n.levels.forEach((l) => { 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); num = num.add_level(level);
}); });
docx = docx.add_abstract_numbering(num); docx = docx.add_abstract_numbering(num);
@ -391,7 +413,7 @@ export class Docx {
docx = docx.add_numbering(num); docx = docx.add_numbering(num);
}); });
const buf = docx.build(); const buf = docx.build(this.hasNumberings);
docx.free(); docx.free();
return buf; return buf;
} }

View File

@ -34,6 +34,7 @@ export type ParagraphProperty = {
}; };
export class Paragraph { export class Paragraph {
hasNumberings = false;
children: ParagraphChild[] = []; children: ParagraphChild[] = [];
property: ParagraphProperty = {}; property: ParagraphProperty = {};
@ -92,6 +93,7 @@ export class Paragraph {
} }
numbering(id: number, level: number) { numbering(id: number, level: number) {
this.hasNumberings = true;
this.property.numbering = { id, level }; this.property.numbering = { id, level };
return this; return this;
} }

View File

@ -16,11 +16,15 @@ export type CellProperty = {
export class TableCell { export class TableCell {
children: Paragraph[] = []; children: Paragraph[] = [];
hasNumberings = false;
property: CellProperty = { property: CellProperty = {
borders: new TableCellBorders(), borders: new TableCellBorders(),
}; };
addParagraph(p: Paragraph) { addParagraph(p: Paragraph) {
if (p.hasNumberings) {
this.hasNumberings = true;
}
this.children.push(p); this.children.push(p);
return this; return this;
} }

View File

@ -2,8 +2,12 @@ import { TableCell } from "./table-cell";
export class TableRow { export class TableRow {
cells: TableCell[] = []; cells: TableCell[] = [];
hasNumberings = false;
addCell(cell: TableCell) { addCell(cell: TableCell) {
if (cell.hasNumberings) {
this.hasNumberings = true;
}
this.cells.push(cell); this.cells.push(cell);
this; this;
} }

View File

@ -9,11 +9,15 @@ export type TableProperty = {
}; };
export class Table { export class Table {
hasNumberings = false;
rows: TableRow[] = []; rows: TableRow[] = [];
grid: number[] = []; grid: number[] = [];
property: TableProperty = {}; property: TableProperty = {};
addRow(row: TableRow) { addRow(row: TableRow) {
if (row.hasNumberings) {
this.hasNumberings = true;
}
this.rows.push(row); this.rows.push(row);
return this; return this;
} }

View File

@ -33,9 +33,12 @@ impl Docx {
self 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 buf = Vec::new();
let mut cur = std::io::Cursor::new(buf); 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); let res = self.0.build().pack(&mut cur);
if res.is_err() { if res.is_err() {
return Err(format!("{:?}", res).into()); return Err(format!("{:?}", res).into());