Create CheckBox element and supporting child elements
This commit is contained in:
45
src/file/checkbox/checkbox.ts
Normal file
45
src/file/checkbox/checkbox.ts
Normal file
@ -0,0 +1,45 @@
|
||||
import { SymbolRun } from "@file/paragraph/run/symbol-run";
|
||||
import { StructuredDocumentTagProperties } from "@file/table-of-contents/sdt-properties";
|
||||
import { StructuredDocumentTagContent } from "@file/table-of-contents/sdt-content";
|
||||
import { XmlComponent } from "@file/xml-components";
|
||||
import { CheckBoxUtil, ICheckboxSymbolOptions } from "./checkbox-util";
|
||||
|
||||
export class CheckBox extends XmlComponent {
|
||||
// default values per Microsoft
|
||||
private readonly DEFAULT_UNCHECKED_SYMBOL: string = "2610";
|
||||
private readonly DEFAULT_CHECKED_SYMBOL: string = "2612";
|
||||
private readonly DEFAULT_FONT: string = "MS Gothic";
|
||||
public constructor(options?: ICheckboxSymbolOptions) {
|
||||
super("w:sdt");
|
||||
|
||||
this.root.push(
|
||||
new StructuredDocumentTagProperties(undefined, {
|
||||
children: [new CheckBoxUtil(options)],
|
||||
}),
|
||||
);
|
||||
|
||||
const content = new StructuredDocumentTagContent();
|
||||
const checkedFont: string | undefined = options?.checkedState?.font;
|
||||
const checkedText: string | undefined = options?.checkedState?.value;
|
||||
const uncheckedFont: string | undefined = options?.uncheckedState?.font;
|
||||
const uncheckedText: string | undefined = options?.uncheckedState?.value;
|
||||
let symbolFont: string;
|
||||
let char: string;
|
||||
|
||||
if (options?.checked) {
|
||||
symbolFont = checkedFont ? checkedFont : this.DEFAULT_FONT;
|
||||
char = checkedText ? checkedText : this.DEFAULT_CHECKED_SYMBOL;
|
||||
} else {
|
||||
symbolFont = uncheckedFont ? uncheckedFont : this.DEFAULT_FONT;
|
||||
char = uncheckedText ? uncheckedText : this.DEFAULT_UNCHECKED_SYMBOL;
|
||||
}
|
||||
|
||||
const initialRenderedChar = new SymbolRun({
|
||||
char: char,
|
||||
symbolfont: symbolFont,
|
||||
});
|
||||
|
||||
content.addChildElement(initialRenderedChar);
|
||||
this.root.push(content);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user