From 210d9c58f23d33fa149fa1b3d3f35207115768da Mon Sep 17 00:00:00 2001 From: Dolan Miu Date: Sun, 25 Jun 2023 03:09:29 +0100 Subject: [PATCH] Improve API for check boxes --- demo/90-check-boxes.ts | 44 ++++++++++++++++++++ src/file/checkbox/checkbox-util.ts | 1 + src/file/checkbox/checkbox.spec.ts | 8 ++-- src/file/checkbox/checkbox.ts | 4 +- src/file/table-of-contents/sdt-properties.ts | 3 +- 5 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 demo/90-check-boxes.ts diff --git a/demo/90-check-boxes.ts b/demo/90-check-boxes.ts new file mode 100644 index 0000000000..7c5d4145ed --- /dev/null +++ b/demo/90-check-boxes.ts @@ -0,0 +1,44 @@ +// Simple example to add check boxes to a document +import * as fs from "fs"; +import { Document, Packer, Paragraph, TextRun, CheckBox } from "docx"; + +const doc = new Document({ + sections: [ + { + properties: {}, + children: [ + new Paragraph({ + children: [ + new TextRun("Hello World"), + new TextRun({ break: 1 }), + new CheckBox(), + new TextRun({ break: 1 }), + new CheckBox({ checked: true }), + new TextRun({ break: 1 }), + new CheckBox({ checked: true, checkedState: { value: "2611" } }), + new TextRun({ break: 1 }), + new CheckBox({ checked: true, checkedState: { value: "2611", font: "MS Gothic" } }), + new TextRun({ break: 1 }), + new CheckBox({ + checked: true, + checkedState: { value: "2611", font: "MS Gothic" }, + uncheckedState: { value: "2610", font: "MS Gothic" }, + }), + new TextRun({ break: 1 }), + new CheckBox({ + checked: true, + checkedState: { value: "2611", font: "MS Gothic" }, + uncheckedState: { value: "2610", font: "MS Gothic" }, + }), + new TextRun({ text: "Are you ok?", break: 1 }), + new CheckBox({ checked: true, alias: "Are you ok?" }), + ], + }), + ], + }, + ], +}); + +Packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/src/file/checkbox/checkbox-util.ts b/src/file/checkbox/checkbox-util.ts index a3732c282e..d6abb2a3b3 100644 --- a/src/file/checkbox/checkbox-util.ts +++ b/src/file/checkbox/checkbox-util.ts @@ -16,6 +16,7 @@ export interface ICheckboxSymbolProperties { } export interface ICheckboxSymbolOptions { + readonly alias?: string; readonly checked?: boolean; readonly checkedState?: ICheckboxSymbolProperties; readonly uncheckedState?: ICheckboxSymbolProperties; diff --git a/src/file/checkbox/checkbox.spec.ts b/src/file/checkbox/checkbox.spec.ts index 8599e60f42..e6553ac459 100644 --- a/src/file/checkbox/checkbox.spec.ts +++ b/src/file/checkbox/checkbox.spec.ts @@ -1,6 +1,7 @@ import { describe, expect, it } from "vitest"; import { Formatter } from "@export/formatter"; -import { CheckBox } from "."; + +import { CheckBox } from "./checkbox"; describe("CheckBox", () => { describe("#constructor()", () => { @@ -66,7 +67,8 @@ describe("CheckBox", () => { ["2713", "Segoe UI Symbol", "2713", "Segoe UI Symbol"], [undefined, undefined, "2612", "MS Gothic"], ])("should create a CheckBox with proper root and custom values", (inputChar, inputFont, actualChar, actualFont) => { - const checkBox = new CheckBox("Custom Checkbox", { + const checkBox = new CheckBox({ + alias: "Custom Checkbox", checked: true, checkedState: { value: inputChar, @@ -141,7 +143,7 @@ describe("CheckBox", () => { }); it("should create a CheckBox with proper root, custom state, and no alias", () => { - const checkBox = new CheckBox(undefined, { + const checkBox = new CheckBox({ checked: false, checkedState: { value: "2713", diff --git a/src/file/checkbox/checkbox.ts b/src/file/checkbox/checkbox.ts index c60690e3af..9b75ee7f6c 100644 --- a/src/file/checkbox/checkbox.ts +++ b/src/file/checkbox/checkbox.ts @@ -9,10 +9,10 @@ export class CheckBox extends XmlComponent { private readonly DEFAULT_UNCHECKED_SYMBOL: string = "2610"; private readonly DEFAULT_CHECKED_SYMBOL: string = "2612"; private readonly DEFAULT_FONT: string = "MS Gothic"; - public constructor(alias?: string, options?: ICheckboxSymbolOptions) { + public constructor(options?: ICheckboxSymbolOptions) { super("w:sdt"); - const properties = new StructuredDocumentTagProperties(alias); + const properties = new StructuredDocumentTagProperties(options?.alias); properties.addChildElement(new CheckBoxUtil(options)); this.root.push(properties); diff --git a/src/file/table-of-contents/sdt-properties.ts b/src/file/table-of-contents/sdt-properties.ts index 4ee856eda3..ba69d5e97a 100644 --- a/src/file/table-of-contents/sdt-properties.ts +++ b/src/file/table-of-contents/sdt-properties.ts @@ -4,7 +4,8 @@ import { StringValueElement, XmlComponent } from "@file/xml-components"; export class StructuredDocumentTagProperties extends XmlComponent { public constructor(alias?: string) { super("w:sdtPr"); - if (typeof alias === "string") { + + if (alias) { this.root.push(new StringValueElement("w:alias", alias)); } }