Use context in prep xml
This commit is contained in:
@ -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;
|
||||
|
@ -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];
|
||||
|
@ -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": [
|
||||
{
|
||||
|
@ -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,
|
||||
};
|
||||
|
@ -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;
|
||||
|
@ -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)) {
|
||||
|
Reference in New Issue
Block a user