docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
100 lines (99 loc) • 3.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Table = void 0;
// Import without assignment ensures Deno does not tree-shake this component. To avoid circular
// definitions, components register themselves in a side-effect of their module.
require("./Row.js");
require("./RowAddition.js");
require("./RowDeletion.js");
const parameter_checking_js_1 = require("../utilities/parameter-checking.js");
const Component_js_1 = require("../classes/Component.js");
const table_properties_js_1 = require("../properties/table-properties.js");
const components_js_1 = require("../utilities/components.js");
const dom_js_1 = require("../utilities/dom.js");
const length_js_1 = require("../utilities/length.js");
const namespaces_js_1 = require("../utilities/namespaces.js");
const tables_js_1 = require("../utilities/tables.js");
const xquery_js_1 = require("../utilities/xquery.js");
/**
* A component that represents a table.
*/
class Table extends Component_js_1.Component {
constructor(tableProps, ...tableChildren) {
(0, parameter_checking_js_1.checkForForbiddenParameters)(tableProps, parameter_checking_js_1.isValidNumber, true);
super(tableProps, ...tableChildren);
/**
* A conceptual description of how the cells, columns, rows and spans of this table make sense.
*
* Exposed so it can be accessed by {@link Row} and {@link Cell} descendants, but not meant
* to be used otherwise.
*/
Object.defineProperty(this, "model", {
enumerable: true,
configurable: true,
writable: true,
value: new tables_js_1.TableGridModel(this)
});
}
/**
* Creates an XML DOM node for this component instance.
*/
async toNode(ancestry) {
const node = (0, dom_js_1.create)(`
element ${namespaces_js_1.QNS.w}tbl {
$tablePropertiesNode,
if (exists($columnWidths)) then element ${namespaces_js_1.QNS.w}tblGrid {
for $columnWidth in array:flatten($columnWidths) return element ${namespaces_js_1.QNS.w}gridCol {
attribute ${namespaces_js_1.QNS.w}w { $columnWidth }
}
} else (),
$children
}
`, {
tablePropertiesNode: (0, table_properties_js_1.tablePropertiesToNode)(this.props),
columnWidths: this.props.columnWidths?.length
? this.props.columnWidths.map((width) => Math.round(width.twip))
: null,
children: await this.childrenToNode(ancestry),
});
return node;
}
/**
* Asserts whether or not a given XML node correlates with this component.
*/
static matchesNode(node) {
return node.nodeName === 'w:tbl';
}
/**
* Instantiate this component from the XML in an existing DOCX file.
*/
static fromNode(node, context) {
const { children, tblpr, ...props } = (0, xquery_js_1.evaluateXPathToMap)(`
map {
"tblpr": ./${namespaces_js_1.QNS.w}tblPr,
"columnWidths": array {
./${namespaces_js_1.QNS.w}tblGrid/${namespaces_js_1.QNS.w}gridCol/@${namespaces_js_1.QNS.w}w/number()
},
"children": array{ ./(${namespaces_js_1.QNS.w}tr) }
}
`, node);
return new Table({
columnWidths: props.columnWidths.map((size) => (0, length_js_1.twip)(size)),
...(0, table_properties_js_1.tablePropertiesFromNode)(tblpr),
}, ...(0, components_js_1.createChildComponentsFromNodes)(this.children, children, context));
}
}
exports.Table = Table;
Object.defineProperty(Table, "children", {
enumerable: true,
configurable: true,
writable: true,
value: ['Row', 'RowAddition', 'RowDeletion']
});
Object.defineProperty(Table, "mixed", {
enumerable: true,
configurable: true,
writable: true,
value: false
});
(0, components_js_1.registerComponent)(Table);