docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
111 lines (109 loc) • 3.66 kB
JavaScript
// 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.
//
// Add items to this list that would otherwise only be depended on as a type definition.
import { Component } from '../classes/Component.js';
import { registerComponent } from '../utilities/components.js';
import { create } from '../utilities/dom.js';
import { cm, pt } from '../utilities/length.js';
import { QNS } from '../utilities/namespaces.js';
import { evaluateXPathToBoolean, evaluateXPathToMap } from '../utilities/xquery.js';
/**
*
*/
export class WatermarkText extends Component {
/**
* Creates an XML DOM node for this component instance.
*
*
*
*
*/
toNode() {
return create(`
element ${QNS.w}p {
element ${QNS.w}r {
element ${QNS.w}pict {
element ${QNS.v}shape {
attribute type { "#_x0000_t136" },
attribute style { concat(
"position:absolute;",
"margin-left:0;",
"margin-top:0;",
"width:", $boxWidth, "pt;",
"height:", $boxHeight, "pt;",
"z-index:-251651072;",
"mso-wrap-edited:f;",
"mso-width-percent:0;",
"mso-height-percent:0;",
"mso-position-horizontal:", $horizontalAlign, ";",
"mso-position-horizontal-relative:page;",
"mso-position-vertical:", $verticalAlign, ";",
"mso-position-vertical-relative:page;",
"mso-width-percent:0;",
"mso-height-percent:0"
) },
attribute ${QNS.o}allowincell { "f" },
attribute fillcolor { concat("#", $color) },
attribute stroked { "f" },
element ${QNS.v}fill {
attribute opacity { $opacity }
},
element ${QNS.v}textpath {
attribute style { concat(
"font-family:"",$font,"";",
"font-size:", $minFontSize, "pt;",
if ($isBold) then "font-weight:bold;" else (),
if ($isItalic) then "font-style:italic" else ()
) },
attribute string { $text }
}
}
}
}
}
`, {
text: this.props.text,
horizontalAlign: this.props.horizontalAlign || 'center',
verticalAlign: this.props.verticalAlign || 'center',
boxWidth: (this.props.boxWidth || cm(21.6)).pt,
boxHeight: (this.props.boxHeight || cm(23.9)).pt,
minFontSize: (this.props.minFontSize || pt(10)).pt,
color: this.props.color || '000000',
font: this.props.font || 'Arial',
opacity: this.props.opacity === null || this.props.opacity === undefined
? '100%'
: this.props.opacity * 100 + '%',
isBold: !!this.props.isBold,
isItalic: !!this.props.isItalic,
});
}
/**
* Asserts whether or not a given XML node correlates with this component.
*/
static matchesNode(node) {
// This will possibly have false positives on "real" Word content
return evaluateXPathToBoolean('self::w:p[./w:r/w:pict]', node);
}
/**
* Instantiate this component from the XML in an existing DOCX file.
*/
static fromNode(node) {
const props = evaluateXPathToMap(`map {
}`, node);
return new WatermarkText(props);
}
}
Object.defineProperty(WatermarkText, "children", {
enumerable: true,
configurable: true,
writable: true,
value: []
});
Object.defineProperty(WatermarkText, "mixed", {
enumerable: true,
configurable: true,
writable: true,
value: false
});
registerComponent(WatermarkText);