Use context in prep xml

This commit is contained in:
Dolan
2021-03-11 01:06:55 +00:00
parent cf6c4998d0
commit 566ac03f9a
16 changed files with 216 additions and 87 deletions

View File

@ -1,4 +1,4 @@
import { IXmlableObject, XmlComponent } from "file/xml-components";
import { IContext, IXmlableObject, XmlComponent } from "file/xml-components";
import { CustomPropertiesAttributes } from "./custom-properties-attributes";
import { CustomProperty, ICustomPropertyOptions } from "./custom-property";
@ -26,9 +26,9 @@ export class CustomProperties extends XmlComponent {
}
}
public prepForXml(): IXmlableObject | undefined {
public prepForXml(context: IContext): IXmlableObject | undefined {
this.properties.forEach((x) => this.root.push(x));
return super.prepForXml();
return super.prepForXml(context);
}
public addCustomProperty(property: ICustomPropertyOptions): void {

View File

@ -1,5 +1,4 @@
import { IViewWrapper } from "file/document-wrapper";
import { IXmlableObject, XmlComponent } from "file/xml-components";
import { IContext, IXmlableObject, XmlComponent } from "file/xml-components";
import { Paragraph, ParagraphProperties, TableOfContents } from "../..";
import { SectionProperties, SectionPropertiesOptions } from "./section-properties/section-properties";
@ -26,13 +25,13 @@ export class Body extends XmlComponent {
this.sections.push(new SectionProperties(options));
}
public prepForXml(file?: IViewWrapper): IXmlableObject | undefined {
public prepForXml(context: IContext): IXmlableObject | undefined {
if (this.sections.length === 1) {
this.root.splice(0, 1);
this.root.push(this.sections.pop() as SectionProperties);
}
return super.prepForXml(file);
return super.prepForXml(context);
}
public push(component: XmlComponent): void {

View File

@ -1,7 +1,7 @@
// http://officeopenxml.com/WPnumbering.php
import { convertInchesToTwip } from "convenience-functions";
import { AlignmentType } from "file/paragraph";
import { IXmlableObject, XmlComponent } from "file/xml-components";
import { IContext, IXmlableObject, XmlComponent } from "file/xml-components";
import { DocumentAttributes } from "../document/document-attributes";
import { AbstractNumbering } from "./abstract-numbering";
@ -158,10 +158,10 @@ export class Numbering extends XmlComponent {
}
}
public prepForXml(): IXmlableObject | undefined {
public prepForXml(context: IContext): IXmlableObject | undefined {
this.abstractNumbering.forEach((x) => this.root.push(x));
this.concreteNumbering.forEach((x) => this.root.push(x));
return super.prepForXml();
return super.prepForXml(context);
}
private createConcreteNumbering(abstractNumbering: AbstractNumbering, reference?: string): ConcreteNumbering {

View File

@ -6,6 +6,7 @@ import { Formatter } from "export/formatter";
import { EMPTY_OBJECT } from "file/xml-components";
import { IViewWrapper } from "../document-wrapper";
import { File } from "../file";
import { ShadingType } from "../table/shading";
import { AlignmentType, HeadingLevel, LeaderType, PageBreak, TabStopPosition, TabStopType } from "./formatting";
import { Bookmark, ExternalHyperlink } from "./links";
@ -830,12 +831,17 @@ describe("Paragraph", () => {
}),
],
});
const fileMock = ({
const viewWrapperMock = ({
Relationships: {
createRelationship: () => ({}),
},
} as unknown) as IViewWrapper;
paragraph.prepForXml(fileMock);
const file = ({} as unknown) as File;
paragraph.prepForXml({
viewWrapper: viewWrapperMock,
file: file,
});
const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({
"w:p": [

View File

@ -2,9 +2,8 @@
import * as shortid from "shortid";
import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run";
import { IXmlableObject, XmlComponent } from "file/xml-components";
import { IContext, IXmlableObject, XmlComponent } from "file/xml-components";
import { IViewWrapper } from "../document-wrapper";
import { TargetModeType } from "../relationships/relationship/relationship";
import { DeletedTextRun, InsertedTextRun } from "../track-revision";
import { PageBreak } from "./formatting/page-break";
@ -76,12 +75,12 @@ export class Paragraph extends XmlComponent {
}
}
public prepForXml(file: IViewWrapper): IXmlableObject | undefined {
public prepForXml(context: IContext): IXmlableObject | undefined {
for (const element of this.root) {
if (element instanceof ExternalHyperlink) {
const index = this.root.indexOf(element);
const concreteHyperlink = new ConcreteHyperlink(element.options.child, shortid.generate().toLowerCase());
file.Relationships.createRelationship(
context.viewWrapper.Relationships.createRelationship(
concreteHyperlink.linkId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
element.options.link,
@ -91,7 +90,7 @@ export class Paragraph extends XmlComponent {
}
}
return super.prepForXml();
return super.prepForXml(context);
}
public addRunToFront(run: Run): Paragraph {

View File

@ -1,8 +1,7 @@
// http://officeopenxml.com/WPtableGrid.php
import { IViewWrapper } from "file/document-wrapper";
import { Paragraph } from "file/paragraph";
import { BorderStyle } from "file/styles";
import { IXmlableObject, XmlComponent } from "file/xml-components";
import { IContext, IXmlableObject, XmlComponent } from "file/xml-components";
import { ITableShadingAttributesProperties } from "../shading";
import { Table } from "../table";
@ -105,11 +104,11 @@ export class TableCell extends XmlComponent {
}
}
public prepForXml(file?: IViewWrapper): IXmlableObject | undefined {
public prepForXml(context: IContext): IXmlableObject | undefined {
// Cells must end with a paragraph
if (!(this.root[this.root.length - 1] instanceof Paragraph)) {
this.root.push(new Paragraph({}));
}
return super.prepForXml(file);
return super.prepForXml(context);
}
}

View File

@ -1,6 +1,12 @@
import { IViewWrapper } from "../document-wrapper";
import { File } from "../file";
import { IXmlableObject } from "./xmlable-object";
export interface IContext {
readonly file: File;
readonly viewWrapper: IViewWrapper;
}
export abstract class BaseXmlComponent {
protected readonly rootKey: string;
// tslint:disable-next-line:readonly-keyword
@ -10,7 +16,7 @@ export abstract class BaseXmlComponent {
this.rootKey = rootKey;
}
public abstract prepForXml(file?: IViewWrapper): IXmlableObject | undefined;
public abstract prepForXml(context: IContext): IXmlableObject | undefined;
public get IsDeleted(): boolean {
return this.deleted;

View File

@ -1,4 +1,4 @@
import { BaseXmlComponent } from "./base";
import { BaseXmlComponent, IContext } from "./base";
import { IXmlableObject } from "./xmlable-object";
export type AttributeMap<T> = { [P in keyof T]: string };
@ -13,7 +13,7 @@ export abstract class XmlAttributeComponent<T> extends BaseXmlComponent {
this.root = properties;
}
public prepForXml(): IXmlableObject {
public prepForXml(_: IContext): IXmlableObject {
const attrs = {};
Object.keys(this.root).forEach((key) => {
const value = this.root[key];

View File

@ -2,6 +2,7 @@ import { expect } from "chai";
import { Element, xml2js } from "xml-js";
import { EMPTY_OBJECT, ImportedXmlComponent } from "./";
import { IContext } from "./base";
import { convertToXmlComponent } from "./imported-xml-component";
const xmlString = `
@ -63,7 +64,8 @@ describe("ImportedXmlComponent", () => {
describe("#prepForXml()", () => {
it("should transform for xml", () => {
const converted = importedXmlComponent.prepForXml();
// tslint:disable-next-line: no-object-literal-type-assertion
const converted = importedXmlComponent.prepForXml({} as IContext);
expect(converted).to.deep.equal({
"w:test": [
{

View File

@ -1,6 +1,7 @@
// tslint:disable:no-any
import { Element as XmlElement, xml2js } from "xml-js";
import { IXmlableObject, XmlAttributeComponent, XmlComponent } from ".";
import { IContext } from "./base";
/**
* Converts the given xml element (in json format) into XmlComponent.
@ -72,7 +73,7 @@ export class ImportedRootElementAttributes extends XmlComponent {
super("");
}
public prepForXml(): IXmlableObject {
public prepForXml(_: IContext): IXmlableObject {
return {
_attr: this._attr,
};

View File

@ -2,6 +2,7 @@ import { expect } from "chai";
import { Formatter } from "export/formatter";
import { EMPTY_OBJECT, XmlComponent } from "./";
import { IContext } from "./base";
class TestComponent extends XmlComponent {}
@ -27,7 +28,8 @@ describe("XmlComponent", () => {
child.delete();
xmlComponent.addChildElement(child);
const xml = xmlComponent.prepForXml();
// tslint:disable-next-line: no-object-literal-type-assertion
const xml = xmlComponent.prepForXml({} as IContext);
if (!xml) {
return;

View File

@ -1,5 +1,4 @@
import { IViewWrapper } from "../document-wrapper";
import { BaseXmlComponent } from "./base";
import { BaseXmlComponent, IContext } from "./base";
import { IXmlableObject } from "./xmlable-object";
export const EMPTY_OBJECT = Object.seal({});
@ -13,7 +12,7 @@ export abstract class XmlComponent extends BaseXmlComponent {
this.root = new Array<BaseXmlComponent | string>();
}
public prepForXml(file?: IViewWrapper): IXmlableObject | undefined {
public prepForXml(context: IContext): IXmlableObject | undefined {
const children = this.root
.filter((c) => {
if (c instanceof BaseXmlComponent) {
@ -23,7 +22,7 @@ export abstract class XmlComponent extends BaseXmlComponent {
})
.map((comp) => {
if (comp instanceof BaseXmlComponent) {
return comp.prepForXml(file);
return comp.prepForXml(context);
}
return comp;
})
@ -52,8 +51,8 @@ export abstract class XmlComponent extends BaseXmlComponent {
}
export abstract class IgnoreIfEmptyXmlComponent extends XmlComponent {
public prepForXml(): IXmlableObject | undefined {
const result = super.prepForXml();
public prepForXml(context: IContext): IXmlableObject | undefined {
const result = super.prepForXml(context);
// Ignore the object if its falsey or is an empty object (would produce
// an empty XML element if allowed to be included in the output).
if (result && (typeof result[this.rootKey] !== "object" || Object.keys(result[this.rootKey]).length)) {