2022-01-17 08:27:24 +02:00
|
|
|
import { Paragraph } from "./paragraph";
|
2024-01-17 15:40:47 +02:00
|
|
|
import { LineSpacing, ParagraphProperty } from "./paragraph-property";
|
2022-12-13 06:33:48 +02:00
|
|
|
import { Table } from "./table";
|
2022-01-06 09:13:45 +02:00
|
|
|
import { TableOfContents } from "./table-of-contents";
|
2022-12-13 06:33:48 +02:00
|
|
|
import { RunFonts } from "./run";
|
2021-11-24 18:49:27 +02:00
|
|
|
import { AbstractNumbering } from "./abstract-numbering";
|
|
|
|
import { Numbering } from "./numbering";
|
|
|
|
import { BookmarkStart } from "./bookmark-start";
|
|
|
|
import { BookmarkEnd } from "./bookmark-end";
|
2023-06-23 07:56:04 +03:00
|
|
|
import { CharacterSpacingValues, Settings } from "./settings";
|
2021-11-24 18:49:27 +02:00
|
|
|
import { DocProps } from "./doc-props";
|
2022-01-06 09:13:45 +02:00
|
|
|
import { Style } from "./style";
|
2021-11-24 18:49:27 +02:00
|
|
|
import { Styles } from "./styles";
|
|
|
|
import { WebExtension } from "./webextension";
|
|
|
|
import { Footer } from "./footer";
|
2021-11-25 17:06:16 +02:00
|
|
|
import { Header } from "./header";
|
2023-02-06 11:42:25 +02:00
|
|
|
import { build } from "./builder";
|
2022-05-23 10:24:44 +03:00
|
|
|
|
2021-03-24 09:51:11 +02:00
|
|
|
import {
|
2021-11-24 18:49:27 +02:00
|
|
|
SectionProperty,
|
|
|
|
PageMargin,
|
|
|
|
PageOrientationType,
|
2021-03-24 09:51:11 +02:00
|
|
|
} from "./section-property";
|
2022-03-25 06:20:09 +02:00
|
|
|
import { DocGridType, DocxJSON } from "./json";
|
2020-01-30 16:14:25 +02:00
|
|
|
|
2020-02-13 12:04:45 +02:00
|
|
|
import * as wasm from "./pkg";
|
2021-11-24 18:49:27 +02:00
|
|
|
import { Level } from "./level";
|
2020-01-30 16:14:25 +02:00
|
|
|
|
|
|
|
export class Docx {
|
2022-01-06 09:13:45 +02:00
|
|
|
children: (
|
|
|
|
| Paragraph
|
|
|
|
| Table
|
|
|
|
| BookmarkStart
|
|
|
|
| BookmarkEnd
|
|
|
|
| TableOfContents
|
|
|
|
)[] = [];
|
2022-12-13 06:33:48 +02:00
|
|
|
|
2021-11-24 18:49:27 +02:00
|
|
|
hasNumberings = false;
|
|
|
|
abstractNumberings: AbstractNumbering[] = [];
|
|
|
|
numberings: Numbering[] = [];
|
|
|
|
settings: Settings = new Settings();
|
|
|
|
docProps: DocProps = new DocProps();
|
|
|
|
sectionProperty: SectionProperty = new SectionProperty();
|
|
|
|
_taskpanes: boolean = false;
|
|
|
|
webextensions: WebExtension[] = [];
|
|
|
|
customItems: { id: string; xml: string }[] = [];
|
|
|
|
styles = new Styles();
|
|
|
|
|
2022-01-06 09:13:45 +02:00
|
|
|
addTableOfContents(t: TableOfContents) {
|
|
|
|
this.children.push(t);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
addStyle(s: Style) {
|
|
|
|
this.styles.styles.push(s);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-24 18:49:27 +02:00
|
|
|
addParagraph(p: Paragraph) {
|
|
|
|
if (p.hasNumberings) {
|
|
|
|
this.hasNumberings = true;
|
|
|
|
}
|
|
|
|
this.children.push(p);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
addBookmarkStart(id: number, name: string) {
|
|
|
|
this.children.push(new BookmarkStart(id, name));
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
addBookmarkEnd(id: number) {
|
|
|
|
this.children.push(new BookmarkEnd(id));
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
addTable(t: Table) {
|
|
|
|
if (t.hasNumberings) {
|
|
|
|
this.hasNumberings = true;
|
|
|
|
}
|
|
|
|
this.children.push(t);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
addAbstractNumbering(num: AbstractNumbering) {
|
|
|
|
this.abstractNumberings.push(num);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
addNumbering(num: Numbering) {
|
|
|
|
this.numberings.push(num);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
docId(id: string) {
|
|
|
|
this.settings.docId(id);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultTabStop(stop: number) {
|
|
|
|
this.settings.defaultTabStop(stop);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
createdAt(date: string) {
|
|
|
|
this.docProps.createdAt(date);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
customProperty(name: string, item: string) {
|
|
|
|
this.docProps.customProperty(name, item);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
updatedAt(date: string) {
|
|
|
|
this.docProps.updatedAt(date);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
addDocVar(name: string, val: string) {
|
|
|
|
this.settings.addDocVar(name, val);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-25 17:06:16 +02:00
|
|
|
header(f: Header) {
|
|
|
|
this.sectionProperty._header = f;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-12-01 17:39:50 +02:00
|
|
|
firstHeader(h: Header) {
|
|
|
|
this.sectionProperty._firstHeader = h;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
evenHeader(h: Header) {
|
|
|
|
this.sectionProperty._evenHeader = h;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-24 18:49:27 +02:00
|
|
|
footer(f: Footer) {
|
|
|
|
this.sectionProperty._footer = f;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-12-01 17:39:50 +02:00
|
|
|
firstFooter(f: Footer) {
|
|
|
|
this.sectionProperty._firstFooter = f;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
evenFooter(f: Footer) {
|
|
|
|
this.sectionProperty._evenFooter = f;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-24 18:49:27 +02:00
|
|
|
pageSize(w: number, h: number) {
|
|
|
|
this.sectionProperty.pageSize(w, h);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
pageMargin(margin: Partial<PageMargin>) {
|
|
|
|
this.sectionProperty.pageMargin(margin);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
pageOrientation(o: PageOrientationType) {
|
|
|
|
this.sectionProperty.pageOrientation(o);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
docGrid(type: DocGridType, linePitch?: number, charSpace?: number) {
|
|
|
|
this.sectionProperty.docGrid(type, linePitch, charSpace);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-08-26 05:14:16 +03:00
|
|
|
adjustLineHeightInTable() {
|
2022-12-02 06:35:43 +02:00
|
|
|
this.settings.adjustLineHeightInTable();
|
2022-08-26 05:14:16 +03:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2023-06-23 07:56:04 +03:00
|
|
|
characterSpacingControl(v: CharacterSpacingValues) {
|
|
|
|
this.settings._characterSpacingControl = v;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-24 18:49:27 +02:00
|
|
|
defaultSize(size: number) {
|
|
|
|
this.styles.defaultSize(size);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultFonts(fonts: RunFonts) {
|
|
|
|
this.styles.defaultFonts(fonts);
|
|
|
|
return this;
|
|
|
|
}
|
2021-06-30 16:37:13 +03:00
|
|
|
|
2023-06-23 13:09:27 +03:00
|
|
|
defaultCharacterSpacing(spacing: number) {
|
|
|
|
this.styles.defaultCharacterSpacing(spacing);
|
2021-11-24 18:49:27 +02:00
|
|
|
return this;
|
|
|
|
}
|
2021-07-13 12:46:15 +03:00
|
|
|
|
2024-01-17 15:40:47 +02:00
|
|
|
defaultLineSpacing(spacing: LineSpacing) {
|
|
|
|
this.styles.defaultLineSpacing(spacing);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2021-11-24 18:49:27 +02:00
|
|
|
taskpanes() {
|
|
|
|
this._taskpanes = true;
|
|
|
|
return this;
|
|
|
|
}
|
2021-09-29 03:03:39 +03:00
|
|
|
|
2021-11-24 18:49:27 +02:00
|
|
|
webextension(e: WebExtension) {
|
|
|
|
this.webextensions.push(e);
|
|
|
|
return this;
|
|
|
|
}
|
2021-09-29 03:03:39 +03:00
|
|
|
|
2021-11-24 18:49:27 +02:00
|
|
|
addCustomItem(id: string, xml: string) {
|
|
|
|
this.customItems.push({ id, xml });
|
|
|
|
return this;
|
|
|
|
}
|
2021-09-29 03:03:39 +03:00
|
|
|
|
2021-11-24 18:49:27 +02:00
|
|
|
buildRunFonts = (fonts: RunFonts | undefined) => {
|
|
|
|
let f = wasm.createRunFonts();
|
|
|
|
if (fonts?._ascii) {
|
|
|
|
f = f.ascii(fonts._ascii);
|
2020-12-14 08:01:23 +02:00
|
|
|
}
|
2021-11-24 18:49:27 +02:00
|
|
|
if (fonts?._hiAnsi) {
|
|
|
|
f = f.hi_ansi(fonts._hiAnsi);
|
2021-09-29 03:03:39 +03:00
|
|
|
}
|
2021-11-24 18:49:27 +02:00
|
|
|
if (fonts?._cs) {
|
|
|
|
f = f.cs(fonts._cs);
|
2021-09-29 03:03:39 +03:00
|
|
|
}
|
2021-11-24 18:49:27 +02:00
|
|
|
if (fonts?._eastAsia) {
|
|
|
|
f = f.east_asia(fonts._eastAsia);
|
2020-01-30 16:14:25 +02:00
|
|
|
}
|
2021-11-24 18:49:27 +02:00
|
|
|
return f;
|
|
|
|
};
|
2020-01-30 16:14:25 +02:00
|
|
|
|
2021-11-25 12:42:06 +02:00
|
|
|
buildLineSpacing(p: ParagraphProperty): wasm.LineSpacing | null {
|
|
|
|
const { lineSpacing } = p;
|
|
|
|
if (lineSpacing == null) return null;
|
|
|
|
let kind;
|
|
|
|
switch (lineSpacing._lineRule) {
|
|
|
|
case "atLeast": {
|
|
|
|
kind = wasm.LineSpacingType.AtLeast;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "auto": {
|
|
|
|
kind = wasm.LineSpacingType.Auto;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "exact": {
|
|
|
|
kind = wasm.LineSpacingType.Exact;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let spacing = wasm.createLineSpacing();
|
|
|
|
if (lineSpacing._before != null) {
|
|
|
|
spacing = spacing.before(lineSpacing._before);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lineSpacing._after != null) {
|
|
|
|
spacing = spacing.after(lineSpacing._after);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lineSpacing._beforeLines != null) {
|
|
|
|
spacing = spacing.before_lines(lineSpacing._beforeLines);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lineSpacing._afterLines != null) {
|
|
|
|
spacing = spacing.after_lines(lineSpacing._afterLines);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lineSpacing._line != null) {
|
|
|
|
spacing = spacing.line(lineSpacing._line);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kind != null) {
|
|
|
|
spacing = spacing.line_rule(kind);
|
|
|
|
}
|
|
|
|
return spacing;
|
|
|
|
}
|
|
|
|
|
2021-11-24 18:49:27 +02:00
|
|
|
buildLevel(l: Level) {
|
|
|
|
let level = wasm.createLevel(l.id, l.start, l.format, l.text, l.jc);
|
|
|
|
|
|
|
|
if (l.levelSuffix === "nothing") {
|
|
|
|
level = level.suffix(wasm.LevelSuffixType.Nothing);
|
|
|
|
} else if (l.levelSuffix === "space") {
|
|
|
|
level = level.suffix(wasm.LevelSuffixType.Space);
|
|
|
|
} else {
|
|
|
|
level = level.suffix(wasm.LevelSuffixType.Tab);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l.runProperty.bold) {
|
|
|
|
level = level.bold();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l.runProperty.italic) {
|
|
|
|
level = level.italic();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l.runProperty.size) {
|
|
|
|
level = level.size(l.runProperty.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l.runProperty.fonts) {
|
|
|
|
let f = wasm.createRunFonts();
|
|
|
|
if (l.runProperty.fonts._ascii) {
|
|
|
|
f = f.ascii(l.runProperty.fonts._ascii);
|
|
|
|
}
|
|
|
|
if (l.runProperty.fonts._hiAnsi) {
|
|
|
|
f = f.hi_ansi(l.runProperty.fonts._hiAnsi);
|
|
|
|
}
|
|
|
|
if (l.runProperty.fonts._cs) {
|
|
|
|
f = f.cs(l.runProperty.fonts._cs);
|
|
|
|
}
|
|
|
|
if (l.runProperty.fonts._eastAsia) {
|
|
|
|
f = f.east_asia(l.runProperty.fonts._eastAsia);
|
|
|
|
}
|
|
|
|
level = level.fonts(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return level;
|
|
|
|
}
|
|
|
|
|
|
|
|
createDocx(): wasm.Docx {
|
|
|
|
let docx = wasm.createDocx();
|
|
|
|
|
|
|
|
this.children.forEach((child) => {
|
|
|
|
if (child instanceof Paragraph) {
|
2023-02-06 11:42:25 +02:00
|
|
|
docx = docx.add_paragraph(build(child));
|
2021-11-24 18:49:27 +02:00
|
|
|
} else if (child instanceof Table) {
|
2022-12-13 06:33:48 +02:00
|
|
|
let t = child.build();
|
2021-11-24 18:49:27 +02:00
|
|
|
docx = docx.add_table(t);
|
|
|
|
} else if (child instanceof BookmarkStart) {
|
|
|
|
docx = docx.add_bookmark_start(child.id, child.name);
|
|
|
|
} else if (child instanceof BookmarkEnd) {
|
|
|
|
docx = docx.add_bookmark_end(child.id);
|
2022-01-06 09:13:45 +02:00
|
|
|
} else if (child instanceof TableOfContents) {
|
|
|
|
docx = docx.add_table_of_contents(child.buildWasmObject());
|
2021-11-24 18:49:27 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.abstractNumberings.forEach((n) => {
|
|
|
|
let num = wasm.createAbstractNumbering(n.id);
|
|
|
|
n.levels.forEach((l) => {
|
|
|
|
const level = this.buildLevel(l);
|
|
|
|
num = num.add_level(level);
|
|
|
|
});
|
|
|
|
docx = docx.add_abstract_numbering(num);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.numberings.forEach((n) => {
|
|
|
|
let num = wasm.createNumbering(n.id, n.abstractNumId);
|
|
|
|
n.overrides.forEach((o) => {
|
|
|
|
let levelOverride = wasm.createLevelOverride(o.level);
|
|
|
|
if (o.startOverride !== null) {
|
|
|
|
levelOverride = levelOverride.start(o.startOverride);
|
|
|
|
}
|
|
|
|
if (o.levelOverride !== null) {
|
|
|
|
let level = wasm.createLevel(
|
|
|
|
o.levelOverride.level,
|
|
|
|
o.levelOverride.start,
|
|
|
|
o.levelOverride.format,
|
|
|
|
o.levelOverride.text,
|
|
|
|
o.levelOverride.jc
|
|
|
|
);
|
|
|
|
levelOverride = levelOverride.level(level);
|
|
|
|
}
|
|
|
|
num = num.add_override(levelOverride);
|
|
|
|
});
|
|
|
|
docx = docx.add_numbering(num);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.settings._docId) {
|
|
|
|
docx = docx.doc_id(this.settings._docId);
|
|
|
|
}
|
|
|
|
|
2022-12-02 06:35:43 +02:00
|
|
|
if (this.settings._adjustLineHeightInTable) {
|
|
|
|
docx = docx.set_adjust_line_height_in_table();
|
|
|
|
}
|
|
|
|
|
2023-06-23 07:41:36 +03:00
|
|
|
if (this.settings._characterSpacingControl) {
|
|
|
|
if (this.settings._characterSpacingControl === "compressPunctuation") {
|
|
|
|
docx = docx.character_spacing_control(
|
|
|
|
wasm.CharacterSpacingValues.CompressPunctuation
|
|
|
|
);
|
|
|
|
} else if (this.settings._characterSpacingControl === "doNotCompress") {
|
|
|
|
docx = docx.character_spacing_control(
|
|
|
|
wasm.CharacterSpacingValues.DoNotCompress
|
|
|
|
);
|
|
|
|
} else if (
|
|
|
|
this.settings._characterSpacingControl ===
|
|
|
|
"compressPunctuationAndJapaneseKana"
|
|
|
|
) {
|
|
|
|
docx = docx.character_spacing_control(
|
|
|
|
wasm.CharacterSpacingValues.CompressPunctuationAndJapaneseKana
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-24 18:49:27 +02:00
|
|
|
docx = docx.default_tab_stop(this.settings._defaultTabStop);
|
|
|
|
|
|
|
|
this.settings._docVars.forEach((v) => {
|
|
|
|
docx = docx.add_doc_var(v.name, v.val);
|
|
|
|
});
|
|
|
|
|
2021-11-25 17:06:16 +02:00
|
|
|
if (this.sectionProperty._header) {
|
|
|
|
let header = wasm.createHeader();
|
|
|
|
this.sectionProperty._header.children.forEach((c) => {
|
2021-12-01 17:39:50 +02:00
|
|
|
if (c instanceof Paragraph) {
|
2023-02-06 11:42:25 +02:00
|
|
|
header = header.add_paragraph(build(c));
|
2024-03-18 10:28:52 +02:00
|
|
|
} else if (c instanceof Table) {
|
2022-12-13 06:33:48 +02:00
|
|
|
header = header.add_table(c.build());
|
2021-12-01 17:39:50 +02:00
|
|
|
}
|
2021-11-25 17:06:16 +02:00
|
|
|
});
|
|
|
|
docx = docx.header(header);
|
|
|
|
}
|
|
|
|
|
2021-11-27 12:12:06 +02:00
|
|
|
if (this.sectionProperty._firstHeader) {
|
|
|
|
let header = wasm.createHeader();
|
|
|
|
this.sectionProperty._firstHeader.children.forEach((c) => {
|
2021-12-01 17:39:50 +02:00
|
|
|
if (c instanceof Paragraph) {
|
2023-02-06 11:42:25 +02:00
|
|
|
header = header.add_paragraph(build(c));
|
2024-03-18 10:28:52 +02:00
|
|
|
} else if (c instanceof Table) {
|
2022-12-13 06:33:48 +02:00
|
|
|
header = header.add_table(c.build());
|
2021-12-01 17:39:50 +02:00
|
|
|
}
|
2021-11-27 12:12:06 +02:00
|
|
|
});
|
|
|
|
docx = docx.first_header(header);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.sectionProperty._evenHeader) {
|
|
|
|
let header = wasm.createHeader();
|
|
|
|
this.sectionProperty._evenHeader.children.forEach((c) => {
|
2021-12-01 17:39:50 +02:00
|
|
|
if (c instanceof Paragraph) {
|
2023-02-06 11:42:25 +02:00
|
|
|
header = header.add_paragraph(build(c));
|
2024-03-18 10:28:52 +02:00
|
|
|
} else if (c instanceof Table) {
|
2022-12-13 06:33:48 +02:00
|
|
|
header = header.add_table(c.build());
|
2021-12-01 17:39:50 +02:00
|
|
|
}
|
2021-11-27 12:12:06 +02:00
|
|
|
});
|
|
|
|
docx = docx.even_header(header);
|
|
|
|
}
|
|
|
|
|
2021-11-24 18:49:27 +02:00
|
|
|
if (this.sectionProperty._footer) {
|
|
|
|
let footer = wasm.createFooter();
|
|
|
|
this.sectionProperty._footer.children.forEach((c) => {
|
2021-12-01 17:39:50 +02:00
|
|
|
if (c instanceof Paragraph) {
|
2023-02-06 11:42:25 +02:00
|
|
|
footer = footer.add_paragraph(build(c));
|
2024-03-18 10:28:52 +02:00
|
|
|
} else if (c instanceof Table) {
|
2022-12-13 06:33:48 +02:00
|
|
|
footer = footer.add_table(c.build());
|
2021-12-01 17:39:50 +02:00
|
|
|
}
|
2021-11-24 18:49:27 +02:00
|
|
|
});
|
|
|
|
docx = docx.footer(footer);
|
|
|
|
}
|
|
|
|
|
2021-11-27 12:12:06 +02:00
|
|
|
if (this.sectionProperty._firstFooter) {
|
|
|
|
let footer = wasm.createFooter();
|
|
|
|
this.sectionProperty._firstFooter.children.forEach((c) => {
|
2021-12-01 17:39:50 +02:00
|
|
|
if (c instanceof Paragraph) {
|
2023-02-06 11:42:25 +02:00
|
|
|
footer = footer.add_paragraph(build(c));
|
2024-03-18 10:28:52 +02:00
|
|
|
} else if (c instanceof Table) {
|
2022-12-13 06:33:48 +02:00
|
|
|
footer = footer.add_table(c.build());
|
2021-12-01 17:39:50 +02:00
|
|
|
}
|
2021-11-27 12:12:06 +02:00
|
|
|
});
|
|
|
|
docx = docx.first_footer(footer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.sectionProperty._evenFooter) {
|
|
|
|
let footer = wasm.createFooter();
|
|
|
|
this.sectionProperty._evenFooter.children.forEach((c) => {
|
2021-12-01 17:39:50 +02:00
|
|
|
if (c instanceof Paragraph) {
|
2023-02-06 11:42:25 +02:00
|
|
|
footer = footer.add_paragraph(build(c));
|
2024-03-18 10:28:52 +02:00
|
|
|
} else if (c instanceof Table) {
|
2022-12-13 06:33:48 +02:00
|
|
|
footer = footer.add_table(c.build());
|
2021-12-01 17:39:50 +02:00
|
|
|
}
|
2021-11-27 12:12:06 +02:00
|
|
|
});
|
|
|
|
docx = docx.even_footer(footer);
|
|
|
|
}
|
|
|
|
|
2021-11-24 18:49:27 +02:00
|
|
|
if (this.sectionProperty._pageMargin) {
|
|
|
|
const { top, left, right, bottom, header, footer, gutter } =
|
|
|
|
this.sectionProperty._pageMargin;
|
|
|
|
const margin = wasm
|
|
|
|
.createPageMargin()
|
|
|
|
.top(top)
|
|
|
|
.left(left)
|
|
|
|
.right(right)
|
|
|
|
.bottom(bottom)
|
|
|
|
.header(header)
|
|
|
|
.footer(footer)
|
|
|
|
.gutter(gutter);
|
|
|
|
docx = docx.page_margin(margin);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.sectionProperty._pageSize) {
|
|
|
|
const { w, h, orient } = this.sectionProperty._pageSize;
|
|
|
|
docx = docx.page_size(w, h);
|
|
|
|
switch (orient) {
|
|
|
|
case "landscape":
|
|
|
|
docx = docx.page_orient(wasm.PageOrientationType.Landscape);
|
|
|
|
break;
|
|
|
|
case "portrait":
|
|
|
|
docx = docx.page_orient(wasm.PageOrientationType.Portrait);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-06 12:13:11 +02:00
|
|
|
if (this.sectionProperty._pageTypeNum) {
|
|
|
|
const { start, chapStyle } = this.sectionProperty._pageTypeNum;
|
|
|
|
const p = wasm.createPageNumType(start, chapStyle);
|
|
|
|
docx = docx.page_num_type(p);
|
|
|
|
}
|
|
|
|
|
2021-11-24 18:49:27 +02:00
|
|
|
if (this.sectionProperty._docGrid) {
|
|
|
|
const { gridType, charSpace, linePitch } = this.sectionProperty._docGrid;
|
|
|
|
let type = wasm.DocGridType.Default;
|
|
|
|
switch (gridType) {
|
|
|
|
case "lines":
|
|
|
|
type = wasm.DocGridType.Lines;
|
|
|
|
break;
|
|
|
|
case "linesAndChars":
|
|
|
|
type = wasm.DocGridType.LinesAndChars;
|
|
|
|
break;
|
|
|
|
case "snapToChars":
|
|
|
|
type = wasm.DocGridType.SnapToChars;
|
|
|
|
break;
|
|
|
|
case "default":
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
docx = docx.doc_grid(type, linePitch, charSpace);
|
|
|
|
}
|
|
|
|
|
2022-01-06 09:13:45 +02:00
|
|
|
for (const s of this.styles?.styles) {
|
|
|
|
docx = docx.add_style(s.buildWasmObject());
|
|
|
|
}
|
|
|
|
|
2021-11-24 18:49:27 +02:00
|
|
|
if (this.styles?.docDefaults) {
|
2024-01-17 15:40:47 +02:00
|
|
|
if (this.styles.docDefaults.runProperty) {
|
|
|
|
if (this.styles.docDefaults.runProperty.fonts) {
|
|
|
|
const fonts = this.buildRunFonts(
|
|
|
|
this.styles.docDefaults.runProperty.fonts
|
|
|
|
);
|
|
|
|
docx = docx.default_fonts(fonts);
|
|
|
|
}
|
2021-11-24 18:49:27 +02:00
|
|
|
|
2024-01-17 15:40:47 +02:00
|
|
|
if (this.styles.docDefaults.runProperty.size) {
|
|
|
|
docx = docx.default_size(this.styles.docDefaults.runProperty.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.styles.docDefaults.runProperty.characterSpacing) {
|
|
|
|
docx = docx.default_spacing(
|
|
|
|
this.styles.docDefaults.runProperty.characterSpacing
|
|
|
|
);
|
|
|
|
}
|
2021-11-24 18:49:27 +02:00
|
|
|
}
|
|
|
|
|
2024-01-17 15:40:47 +02:00
|
|
|
if (this.styles.docDefaults.paragraphProperty) {
|
|
|
|
if (this.styles.docDefaults.paragraphProperty.lineSpacing) {
|
|
|
|
const spacing = this.buildLineSpacing(
|
|
|
|
this.styles.docDefaults.paragraphProperty
|
|
|
|
);
|
|
|
|
if (spacing) {
|
|
|
|
docx = docx.default_line_spacing(spacing);
|
|
|
|
}
|
|
|
|
}
|
2021-11-24 18:49:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.docProps._createdAt) {
|
|
|
|
docx = docx.created_at(this.docProps._createdAt);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.docProps._updatedAt) {
|
|
|
|
docx = docx.updated_at(this.docProps._updatedAt);
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.entries(this.docProps._customProperties).forEach(([key, item]) => {
|
|
|
|
docx = docx.custom_property(key, item);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.docProps._updatedAt) {
|
|
|
|
docx = docx.updated_at(this.docProps._updatedAt);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._taskpanes) {
|
|
|
|
docx = docx.taskpanes();
|
|
|
|
|
|
|
|
for (const e of this.webextensions) {
|
|
|
|
let ext = wasm.createWebExtension(
|
|
|
|
e._id,
|
|
|
|
e._referenceId,
|
|
|
|
e._version,
|
|
|
|
e._store,
|
|
|
|
e._storeType
|
|
|
|
);
|
|
|
|
for (const [name, value] of Object.entries(e.properties)) {
|
|
|
|
ext = ext.property(name, value);
|
|
|
|
}
|
|
|
|
docx = docx.web_extension(ext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const item of this.customItems) {
|
|
|
|
docx = docx.add_custom_item(item.id, item.xml);
|
|
|
|
}
|
|
|
|
|
|
|
|
return docx;
|
|
|
|
}
|
|
|
|
|
|
|
|
json() {
|
|
|
|
const docx = this.createDocx();
|
|
|
|
const json = docx.json_with_update_comments();
|
|
|
|
docx.free();
|
|
|
|
return JSON.parse(json) as DocxJSON;
|
|
|
|
}
|
|
|
|
|
|
|
|
build() {
|
|
|
|
const docx = this.createDocx();
|
|
|
|
const buf = docx.build(this.hasNumberings);
|
2022-01-02 19:18:04 +02:00
|
|
|
// docx.free();
|
2021-11-24 18:49:27 +02:00
|
|
|
return buf;
|
|
|
|
}
|
2020-01-30 16:14:25 +02:00
|
|
|
}
|
|
|
|
|
2020-02-12 19:57:48 +02:00
|
|
|
export const readDocx = (buf: Uint8Array) => {
|
2021-11-24 18:49:27 +02:00
|
|
|
return JSON.parse(wasm.readDocx(buf)) as DocxJSON;
|
2020-02-12 19:57:48 +02:00
|
|
|
};
|
|
|
|
|
2020-01-30 16:14:25 +02:00
|
|
|
export * from "./paragraph";
|
2022-01-17 08:27:24 +02:00
|
|
|
export * from "./paragraph-property";
|
2020-01-30 16:14:25 +02:00
|
|
|
export * from "./insert";
|
|
|
|
export * from "./delete";
|
2021-03-20 17:16:43 +02:00
|
|
|
export * from "./border";
|
2020-01-30 16:14:25 +02:00
|
|
|
export * from "./table";
|
|
|
|
export * from "./table-cell";
|
2020-04-30 08:38:56 +03:00
|
|
|
export * from "./table-cell-border";
|
|
|
|
export * from "./table-cell-borders";
|
2022-01-06 09:13:45 +02:00
|
|
|
export * from "./table-of-contents";
|
|
|
|
export * from "./table-of-contents-item";
|
2020-01-30 16:14:25 +02:00
|
|
|
export * from "./table-row";
|
|
|
|
export * from "./run";
|
|
|
|
export * from "./text";
|
2020-12-14 08:01:23 +02:00
|
|
|
export * from "./style";
|
|
|
|
export * from "./styles";
|
2021-12-17 18:03:02 +02:00
|
|
|
export * from "./hyperlink";
|
2020-01-30 16:14:25 +02:00
|
|
|
export * from "./comment";
|
|
|
|
export * from "./comment-end";
|
|
|
|
export * from "./numbering";
|
2020-05-15 06:54:59 +03:00
|
|
|
export * from "./abstract-numbering";
|
2020-01-30 16:14:25 +02:00
|
|
|
export * from "./bookmark-start";
|
|
|
|
export * from "./bookmark-end";
|
|
|
|
export * from "./break";
|
|
|
|
export * from "./delete-text";
|
|
|
|
export * from "./level";
|
|
|
|
export * from "./tab";
|
2020-02-13 09:14:06 +02:00
|
|
|
export * from "./json";
|
2021-06-30 16:37:13 +03:00
|
|
|
export * from "./webextension";
|
2021-11-25 17:06:16 +02:00
|
|
|
export * from "./header";
|
2024-03-18 10:28:52 +02:00
|
|
|
export * from "./page-num";
|
2024-06-09 03:01:52 +03:00
|
|
|
export * from "./num-pages";
|
2021-11-24 18:49:27 +02:00
|
|
|
export * from "./footer";
|
2022-05-23 10:24:44 +03:00
|
|
|
export * from "./image";
|
2024-10-15 12:30:05 +03:00
|
|
|
export * from "./positional-tab";
|