Rework the way attributes are stored in ImportedXmlComponent to match elsewhere (required allowing for a null xmlKeys in the XmlAttributeComponent interface). Rework the way paragraphs get added to the end of table cells if needed. The goal in both reworks is to not mess around with the objects output from `prepForXml` if we can avoid it. Made the output of RunProperties, ParagraphProperties, TableCellProperties, TableRowProperties, and TableProperties all optional based on whether they contain any attributes or children. Changed code in PageBorders, TableCellMargin, and TableCellBorders that implemented this same thing by overriding `prepForXml` so that it uses the new XmlComponent subclass instead. Removed commented out code that attempted to fix-up XML output and make proper empty elements. Fixed all affected tests. Turn off `no-null-keyword` in the linter as we need to use null to signal to the `xml` library to create an empty element with no attributes (`undefined` will not work in its place). Fixes #306
101 lines
2.8 KiB
TypeScript
101 lines
2.8 KiB
TypeScript
// http://officeopenxml.com/WPsectionBorders.php
|
|
import { BorderStyle } from "file/styles";
|
|
import { IgnoreIfEmptyXmlComponent, XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
|
|
|
export enum PageBorderDisplay {
|
|
ALL_PAGES = "allPages",
|
|
FIRST_PAGE = "firstPage",
|
|
NOT_FIRST_PAGE = "notFirstPage",
|
|
}
|
|
|
|
export enum PageBorderOffsetFrom {
|
|
PAGE = "page",
|
|
TEXT = "text",
|
|
}
|
|
|
|
export enum PageBorderZOrder {
|
|
BACK = "back",
|
|
FRONT = "front",
|
|
}
|
|
|
|
export interface IPageBorderAttributes {
|
|
readonly display?: PageBorderDisplay;
|
|
readonly offsetFrom?: PageBorderOffsetFrom;
|
|
readonly zOrder?: PageBorderZOrder;
|
|
}
|
|
|
|
export interface IPageBorderConfiguration {
|
|
readonly style?: BorderStyle;
|
|
readonly size?: number;
|
|
readonly color?: string;
|
|
readonly space?: number;
|
|
}
|
|
|
|
export interface IPageBordersOptions {
|
|
readonly pageBorders?: IPageBorderAttributes;
|
|
readonly pageBorderTop?: IPageBorderConfiguration;
|
|
readonly pageBorderRight?: IPageBorderConfiguration;
|
|
readonly pageBorderBottom?: IPageBorderConfiguration;
|
|
readonly pageBorderLeft?: IPageBorderConfiguration;
|
|
}
|
|
|
|
class PageBordeAttributes extends XmlAttributeComponent<IPageBorderConfiguration> {
|
|
protected readonly xmlKeys = {
|
|
style: "w:val",
|
|
size: "w:size",
|
|
color: "w:color",
|
|
space: "w:space",
|
|
};
|
|
}
|
|
|
|
class PageBorder extends XmlComponent {
|
|
constructor(key: string, options: IPageBorderConfiguration) {
|
|
super(key);
|
|
|
|
this.root.push(new PageBordeAttributes(options));
|
|
}
|
|
}
|
|
|
|
class PageBordersAttributes extends XmlAttributeComponent<IPageBorderAttributes> {
|
|
protected readonly xmlKeys = {
|
|
display: "w:display",
|
|
offsetFrom: "w:offsetFrom",
|
|
zOrder: "w:zOrder",
|
|
};
|
|
}
|
|
|
|
export class PageBorders extends IgnoreIfEmptyXmlComponent {
|
|
constructor(options?: IPageBordersOptions) {
|
|
super("w:pgBorders");
|
|
|
|
if (!options) {
|
|
return;
|
|
}
|
|
|
|
let pageBordersAttributes = {};
|
|
|
|
if (options.pageBorders) {
|
|
pageBordersAttributes = {
|
|
display: options.pageBorders.display,
|
|
offsetFrom: options.pageBorders.offsetFrom,
|
|
zOrder: options.pageBorders.zOrder,
|
|
};
|
|
}
|
|
|
|
this.root.push(new PageBordersAttributes(pageBordersAttributes));
|
|
|
|
if (options.pageBorderTop) {
|
|
this.root.push(new PageBorder("w:top", options.pageBorderTop));
|
|
}
|
|
if (options.pageBorderRight) {
|
|
this.root.push(new PageBorder("w:right", options.pageBorderRight));
|
|
}
|
|
if (options.pageBorderBottom) {
|
|
this.root.push(new PageBorder("w:bottom", options.pageBorderBottom));
|
|
}
|
|
if (options.pageBorderLeft) {
|
|
this.root.push(new PageBorder("w:left", options.pageBorderLeft));
|
|
}
|
|
}
|
|
}
|