Merge branch 'master' into feat/right-indent
# Conflicts: # demo/21-bookmarks.ts # package-lock.json # src/file/core-properties/properties.ts # src/file/file.spec.ts # src/file/file.ts # src/file/footnotes/footnote/footnote.spec.ts # src/file/footnotes/footnote/footnote.ts # src/file/footnotes/footnotes.ts
This commit is contained in:
@ -54,6 +54,14 @@ describe("ContentTypes", () => {
|
||||
},
|
||||
});
|
||||
expect(tree["Types"][11]).to.deep.equal({
|
||||
Override: {
|
||||
_attr: {
|
||||
ContentType: "application/vnd.openxmlformats-officedocument.custom-properties+xml",
|
||||
PartName: "/docProps/custom.xml",
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(tree["Types"][12]).to.deep.equal({
|
||||
Override: {
|
||||
_attr: {
|
||||
ContentType: "application/vnd.openxmlformats-officedocument.extended-properties+xml",
|
||||
@ -61,7 +69,7 @@ describe("ContentTypes", () => {
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(tree["Types"][12]).to.deep.equal({
|
||||
expect(tree["Types"][13]).to.deep.equal({
|
||||
Override: {
|
||||
_attr: {
|
||||
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml",
|
||||
@ -69,7 +77,7 @@ describe("ContentTypes", () => {
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(tree["Types"][13]).to.deep.equal({
|
||||
expect(tree["Types"][14]).to.deep.equal({
|
||||
Override: {
|
||||
_attr: {
|
||||
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml",
|
||||
@ -77,7 +85,7 @@ describe("ContentTypes", () => {
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(tree["Types"][14]).to.deep.equal({
|
||||
expect(tree["Types"][15]).to.deep.equal({
|
||||
Override: {
|
||||
_attr: {
|
||||
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml",
|
||||
@ -94,7 +102,7 @@ describe("ContentTypes", () => {
|
||||
contentTypes.addFooter(102);
|
||||
const tree = new Formatter().format(contentTypes);
|
||||
|
||||
expect(tree["Types"][15]).to.deep.equal({
|
||||
expect(tree["Types"][16]).to.deep.equal({
|
||||
Override: {
|
||||
_attr: {
|
||||
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml",
|
||||
@ -103,7 +111,7 @@ describe("ContentTypes", () => {
|
||||
},
|
||||
});
|
||||
|
||||
expect(tree["Types"][16]).to.deep.equal({
|
||||
expect(tree["Types"][17]).to.deep.equal({
|
||||
Override: {
|
||||
_attr: {
|
||||
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml",
|
||||
@ -120,7 +128,7 @@ describe("ContentTypes", () => {
|
||||
contentTypes.addHeader(202);
|
||||
const tree = new Formatter().format(contentTypes);
|
||||
|
||||
expect(tree["Types"][15]).to.deep.equal({
|
||||
expect(tree["Types"][16]).to.deep.equal({
|
||||
Override: {
|
||||
_attr: {
|
||||
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml",
|
||||
@ -129,7 +137,7 @@ describe("ContentTypes", () => {
|
||||
},
|
||||
});
|
||||
|
||||
expect(tree["Types"][16]).to.deep.equal({
|
||||
expect(tree["Types"][17]).to.deep.equal({
|
||||
Override: {
|
||||
_attr: {
|
||||
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml",
|
||||
|
@ -27,6 +27,7 @@ export class ContentTypes extends XmlComponent {
|
||||
|
||||
this.root.push(new Override("application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml", "/word/styles.xml"));
|
||||
this.root.push(new Override("application/vnd.openxmlformats-package.core-properties+xml", "/docProps/core.xml"));
|
||||
this.root.push(new Override("application/vnd.openxmlformats-officedocument.custom-properties+xml", "/docProps/custom.xml"));
|
||||
this.root.push(new Override("application/vnd.openxmlformats-officedocument.extended-properties+xml", "/docProps/app.xml"));
|
||||
this.root.push(new Override("application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml", "/word/numbering.xml"));
|
||||
this.root.push(new Override("application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml", "/word/footnotes.xml"));
|
||||
|
@ -1,22 +1,13 @@
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { ICustomPropertyOptions } from "../custom-properties";
|
||||
import { IDocumentBackgroundOptions } from "../document";
|
||||
|
||||
import { DocumentAttributes } from "../document/document-attributes";
|
||||
import { INumberingOptions } from "../numbering";
|
||||
import { HyperlinkType, Paragraph } from "../paragraph";
|
||||
import { Paragraph } from "../paragraph";
|
||||
import { IStylesOptions } from "../styles";
|
||||
import { Created, Creator, Description, Keywords, LastModifiedBy, Modified, Revision, Subject, Title } from "./components";
|
||||
|
||||
export interface IInternalHyperlinkDefinition {
|
||||
readonly text: string;
|
||||
readonly type: HyperlinkType.INTERNAL;
|
||||
}
|
||||
|
||||
export interface IExternalHyperlinkDefinition {
|
||||
readonly link: string;
|
||||
readonly text: string;
|
||||
readonly type: HyperlinkType.EXTERNAL;
|
||||
}
|
||||
|
||||
export interface IPropertiesOptions {
|
||||
readonly title?: string;
|
||||
readonly subject?: string;
|
||||
@ -33,9 +24,12 @@ export interface IPropertiesOptions {
|
||||
readonly children: Paragraph[];
|
||||
};
|
||||
};
|
||||
readonly hyperlinks?: {
|
||||
readonly [key: string]: IInternalHyperlinkDefinition | IExternalHyperlinkDefinition;
|
||||
readonly background?: IDocumentBackgroundOptions;
|
||||
readonly features?: {
|
||||
readonly trackRevisions?: boolean;
|
||||
};
|
||||
readonly compatabilityModeVersion?: number;
|
||||
readonly customProperties?: ICustomPropertyOptions[];
|
||||
}
|
||||
|
||||
export class CoreProperties extends XmlComponent {
|
||||
|
13
src/file/custom-properties/custom-properties-attributes.ts
Normal file
13
src/file/custom-properties/custom-properties-attributes.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { XmlAttributeComponent } from "file/xml-components";
|
||||
|
||||
export interface ICustomPropertiesAttributes {
|
||||
readonly xmlns: string;
|
||||
readonly vt: string;
|
||||
}
|
||||
|
||||
export class CustomPropertiesAttributes extends XmlAttributeComponent<ICustomPropertiesAttributes> {
|
||||
protected readonly xmlKeys = {
|
||||
xmlns: "xmlns",
|
||||
vt: "xmlns:vt",
|
||||
};
|
||||
}
|
66
src/file/custom-properties/custom-properties.spec.ts
Normal file
66
src/file/custom-properties/custom-properties.spec.ts
Normal file
@ -0,0 +1,66 @@
|
||||
import { expect } from "chai";
|
||||
import { Formatter } from "export/formatter";
|
||||
import { CustomProperties } from "./custom-properties";
|
||||
|
||||
describe("CustomProperties", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("sets the appropriate attributes on the top-level", () => {
|
||||
const properties = new CustomProperties([]);
|
||||
const tree = new Formatter().format(properties);
|
||||
expect(tree).to.deep.equal({
|
||||
Properties: {
|
||||
_attr: {
|
||||
xmlns: "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties",
|
||||
"xmlns:vt": "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes",
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("should create custom properties with all the attributes given", () => {
|
||||
const properties = new CustomProperties([
|
||||
{ name: "Address", value: "123" },
|
||||
{ name: "Author", value: "456" },
|
||||
]);
|
||||
const tree = new Formatter().format(properties);
|
||||
expect(tree).to.deep.equal({
|
||||
Properties: [
|
||||
{
|
||||
_attr: {
|
||||
xmlns: "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties",
|
||||
"xmlns:vt": "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes",
|
||||
},
|
||||
},
|
||||
{
|
||||
property: [
|
||||
{
|
||||
_attr: {
|
||||
fmtid: "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}",
|
||||
pid: "2",
|
||||
name: "Address",
|
||||
},
|
||||
},
|
||||
{
|
||||
"vt:lpwstr": ["123"],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
property: [
|
||||
{
|
||||
_attr: {
|
||||
fmtid: "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}",
|
||||
pid: "3",
|
||||
name: "Author",
|
||||
},
|
||||
},
|
||||
{
|
||||
"vt:lpwstr": ["456"],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
37
src/file/custom-properties/custom-properties.ts
Normal file
37
src/file/custom-properties/custom-properties.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import { IXmlableObject, XmlComponent } from "file/xml-components";
|
||||
import { CustomPropertiesAttributes } from "./custom-properties-attributes";
|
||||
import { CustomProperty, ICustomPropertyOptions } from "./custom-property";
|
||||
|
||||
export class CustomProperties extends XmlComponent {
|
||||
// tslint:disable-next-line:readonly-keyword
|
||||
private nextId: number;
|
||||
private readonly properties: CustomProperty[] = [];
|
||||
|
||||
constructor(properties: ICustomPropertyOptions[]) {
|
||||
super("Properties");
|
||||
|
||||
this.root.push(
|
||||
new CustomPropertiesAttributes({
|
||||
xmlns: "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties",
|
||||
vt: "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes",
|
||||
}),
|
||||
);
|
||||
|
||||
// I'm not sure why, but every example I have seen starts with 2
|
||||
// https://docs.microsoft.com/en-us/office/open-xml/how-to-set-a-custom-property-in-a-word-processing-document
|
||||
this.nextId = 2;
|
||||
|
||||
for (const property of properties) {
|
||||
this.addCustomProperty(property);
|
||||
}
|
||||
}
|
||||
|
||||
public prepForXml(): IXmlableObject | undefined {
|
||||
this.properties.forEach((x) => this.root.push(x));
|
||||
return super.prepForXml();
|
||||
}
|
||||
|
||||
public addCustomProperty(property: ICustomPropertyOptions): void {
|
||||
this.properties.push(new CustomProperty(this.nextId++, property));
|
||||
}
|
||||
}
|
15
src/file/custom-properties/custom-property-attributes.ts
Normal file
15
src/file/custom-properties/custom-property-attributes.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { XmlAttributeComponent } from "file/xml-components";
|
||||
|
||||
export interface ICustomPropertyAttributes {
|
||||
readonly fmtid: string;
|
||||
readonly pid: string;
|
||||
readonly name: string;
|
||||
}
|
||||
|
||||
export class CustomPropertyAttributes extends XmlAttributeComponent<ICustomPropertyAttributes> {
|
||||
protected readonly xmlKeys = {
|
||||
fmtid: "fmtid",
|
||||
pid: "pid",
|
||||
name: "name",
|
||||
};
|
||||
}
|
28
src/file/custom-properties/custom-property.ts
Normal file
28
src/file/custom-properties/custom-property.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { CustomPropertyAttributes } from "./custom-property-attributes";
|
||||
|
||||
export interface ICustomPropertyOptions {
|
||||
readonly name: string;
|
||||
readonly value: string;
|
||||
}
|
||||
|
||||
export class CustomProperty extends XmlComponent {
|
||||
constructor(id: number, properties: ICustomPropertyOptions) {
|
||||
super("property");
|
||||
this.root.push(
|
||||
new CustomPropertyAttributes({
|
||||
fmtid: "{D5CDD505-2E9C-101B-9397-08002B2CF9AE}",
|
||||
pid: id.toString(),
|
||||
name: properties.name,
|
||||
}),
|
||||
);
|
||||
this.root.push(new CustomPropertyValue(properties.value));
|
||||
}
|
||||
}
|
||||
|
||||
export class CustomPropertyValue extends XmlComponent {
|
||||
constructor(value: string) {
|
||||
super("vt:lpwstr");
|
||||
this.root.push(value);
|
||||
}
|
||||
}
|
2
src/file/custom-properties/index.ts
Normal file
2
src/file/custom-properties/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from "./custom-properties";
|
||||
export * from "./custom-property";
|
16
src/file/document-wrapper.spec.ts
Normal file
16
src/file/document-wrapper.spec.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { DocumentWrapper } from "./document-wrapper";
|
||||
|
||||
describe("DocumentWrapper", () => {
|
||||
describe("#constructor", () => {
|
||||
it("should create", () => {
|
||||
const file = new DocumentWrapper({ background: {} });
|
||||
|
||||
// tslint:disable-next-line: no-unused-expression
|
||||
expect(file.View).to.be.ok;
|
||||
// tslint:disable-next-line: no-unused-expression
|
||||
expect(file.Relationships).to.be.ok;
|
||||
});
|
||||
});
|
||||
});
|
28
src/file/document-wrapper.ts
Normal file
28
src/file/document-wrapper.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { Document, IDocumentOptions } from "./document";
|
||||
import { Footer } from "./footer";
|
||||
import { FootNotes } from "./footnotes";
|
||||
import { Header } from "./header/header";
|
||||
import { Relationships } from "./relationships";
|
||||
|
||||
export interface IViewWrapper {
|
||||
readonly View: Document | Footer | Header | FootNotes;
|
||||
readonly Relationships: Relationships;
|
||||
}
|
||||
|
||||
export class DocumentWrapper implements IViewWrapper {
|
||||
private readonly document: Document;
|
||||
private readonly relationships: Relationships;
|
||||
|
||||
constructor(options: IDocumentOptions) {
|
||||
this.document = new Document(options);
|
||||
this.relationships = new Relationships();
|
||||
}
|
||||
|
||||
public get View(): Document {
|
||||
return this.document;
|
||||
}
|
||||
|
||||
public get Relationships(): Relationships {
|
||||
return this.relationships;
|
||||
}
|
||||
}
|
@ -39,7 +39,7 @@ describe("Body", () => {
|
||||
},
|
||||
},
|
||||
},
|
||||
{ "w:cols": { _attr: { "w:space": 708, "w:num": 1 } } },
|
||||
{ "w:cols": { _attr: { "w:space": 708, "w:sep": false, "w:num": 1 } } },
|
||||
{ "w:docGrid": { _attr: { "w:linePitch": 360 } } },
|
||||
],
|
||||
},
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { IViewWrapper } from "file/document-wrapper";
|
||||
import { IXmlableObject, XmlComponent } from "file/xml-components";
|
||||
|
||||
import { Paragraph, ParagraphProperties, TableOfContents } from "../..";
|
||||
import { File } from "../../../file";
|
||||
import { SectionProperties, SectionPropertiesOptions } from "./section-properties/section-properties";
|
||||
|
||||
export class Body extends XmlComponent {
|
||||
@ -25,7 +26,7 @@ export class Body extends XmlComponent {
|
||||
this.sections.push(new SectionProperties(options));
|
||||
}
|
||||
|
||||
public prepForXml(file?: File): IXmlableObject | undefined {
|
||||
public prepForXml(file?: IViewWrapper): IXmlableObject | undefined {
|
||||
if (this.sections.length === 1) {
|
||||
this.root.splice(0, 1);
|
||||
this.root.push(this.sections.pop() as SectionProperties);
|
||||
@ -45,7 +46,7 @@ export class Body extends XmlComponent {
|
||||
private createSectionParagraph(section: SectionProperties): Paragraph {
|
||||
const paragraph = new Paragraph({});
|
||||
const properties = new ParagraphProperties({});
|
||||
properties.addChildElement(section);
|
||||
properties.push(section);
|
||||
paragraph.addChildElement(properties);
|
||||
return paragraph;
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
import { XmlAttributeComponent } from "file/xml-components";
|
||||
|
||||
export interface IColumnsAttributes {
|
||||
export class ColumnsAttributes extends XmlAttributeComponent<{
|
||||
readonly space?: number;
|
||||
readonly num?: number;
|
||||
}
|
||||
|
||||
export class ColumnsAttributes extends XmlAttributeComponent<IColumnsAttributes> {
|
||||
readonly separate?: boolean;
|
||||
}> {
|
||||
protected readonly xmlKeys = {
|
||||
space: "w:space",
|
||||
num: "w:num",
|
||||
separate: "w:sep",
|
||||
};
|
||||
}
|
||||
|
@ -2,12 +2,13 @@ import { XmlComponent } from "file/xml-components";
|
||||
import { ColumnsAttributes } from "./columns-attributes";
|
||||
|
||||
export class Columns extends XmlComponent {
|
||||
constructor(space: number, num: number) {
|
||||
constructor(space: number, num: number, separate: boolean) {
|
||||
super("w:cols");
|
||||
this.root.push(
|
||||
new ColumnsAttributes({
|
||||
space: space,
|
||||
num: num,
|
||||
separate: separate,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
@ -6,3 +6,4 @@ export * from "./page-number";
|
||||
export * from "./page-border";
|
||||
export * from "./line-number";
|
||||
export * from "./vertical-align";
|
||||
export * from "./type";
|
||||
|
@ -14,6 +14,7 @@ export enum PageNumberFormat {
|
||||
ORDINAL_TEXT = "ordinalText",
|
||||
UPPER_LETTER = "upperLetter",
|
||||
UPPER_ROMAN = "upperRoman",
|
||||
DECIMAL_FULL_WIDTH = "decimalFullWidth",
|
||||
}
|
||||
|
||||
export interface IPageNumberTypeAttributes {
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { convertInchesToTwip } from "convenience-functions";
|
||||
import { Formatter } from "export/formatter";
|
||||
import { FooterWrapper } from "file/footer-wrapper";
|
||||
import { HeaderWrapper } from "file/header-wrapper";
|
||||
@ -8,6 +9,7 @@ import { Media } from "file/media";
|
||||
import { PageBorderOffsetFrom } from "./page-border";
|
||||
import { PageNumberFormat } from "./page-number";
|
||||
import { SectionProperties } from "./section-properties";
|
||||
import { SectionType } from "./type/section-type-attributes";
|
||||
import { SectionVerticalAlignValue } from "./vertical-align";
|
||||
|
||||
describe("SectionProperties", () => {
|
||||
@ -18,10 +20,10 @@ describe("SectionProperties", () => {
|
||||
const properties = new SectionProperties({
|
||||
width: 11906,
|
||||
height: 16838,
|
||||
top: 1440,
|
||||
right: 1440,
|
||||
bottom: 1440,
|
||||
left: 1440,
|
||||
top: convertInchesToTwip(1),
|
||||
right: convertInchesToTwip(1),
|
||||
bottom: convertInchesToTwip(1),
|
||||
left: convertInchesToTwip(1),
|
||||
header: 708,
|
||||
footer: 708,
|
||||
gutter: 0,
|
||||
@ -29,8 +31,9 @@ describe("SectionProperties", () => {
|
||||
column: {
|
||||
space: 708,
|
||||
count: 1,
|
||||
separate: true,
|
||||
},
|
||||
linePitch: 360,
|
||||
linePitch: convertInchesToTwip(0.25),
|
||||
headers: {
|
||||
default: new HeaderWrapper(media, 100),
|
||||
},
|
||||
@ -61,7 +64,7 @@ describe("SectionProperties", () => {
|
||||
},
|
||||
});
|
||||
|
||||
expect(tree["w:sectPr"][2]).to.deep.equal({ "w:cols": { _attr: { "w:space": 708, "w:num": 1 } } });
|
||||
expect(tree["w:sectPr"][2]).to.deep.equal({ "w:cols": { _attr: { "w:space": 708, "w:sep": true, "w:num": 1 } } });
|
||||
expect(tree["w:sectPr"][3]).to.deep.equal({ "w:docGrid": { _attr: { "w:linePitch": 360 } } });
|
||||
expect(tree["w:sectPr"][4]).to.deep.equal({ "w:headerReference": { _attr: { "r:id": "rId100", "w:type": "default" } } });
|
||||
expect(tree["w:sectPr"][5]).to.deep.equal({ "w:footerReference": { _attr: { "r:id": "rId200", "w:type": "even" } } });
|
||||
@ -88,7 +91,7 @@ describe("SectionProperties", () => {
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(tree["w:sectPr"][2]).to.deep.equal({ "w:cols": { _attr: { "w:space": 708, "w:num": 1 } } });
|
||||
expect(tree["w:sectPr"][2]).to.deep.equal({ "w:cols": { _attr: { "w:space": 708, "w:sep": false, "w:num": 1 } } });
|
||||
expect(tree["w:sectPr"][3]).to.deep.equal({ "w:docGrid": { _attr: { "w:linePitch": 360 } } });
|
||||
});
|
||||
|
||||
@ -198,5 +201,17 @@ describe("SectionProperties", () => {
|
||||
const pgNumType = tree["w:sectPr"].find((item) => item["w:pgNumType"] !== undefined);
|
||||
expect(pgNumType).to.equal(undefined);
|
||||
});
|
||||
|
||||
it("should create section properties with section type", () => {
|
||||
const properties = new SectionProperties({
|
||||
type: SectionType.CONTINUOUS,
|
||||
});
|
||||
const tree = new Formatter().format(properties);
|
||||
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
|
||||
const type = tree["w:sectPr"].find((item) => item["w:type"] !== undefined);
|
||||
expect(type).to.deep.equal({
|
||||
"w:type": { _attr: { "w:val": "continuous" } },
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -1,4 +1,5 @@
|
||||
// http://officeopenxml.com/WPsection.php
|
||||
import { convertInchesToTwip } from "convenience-functions";
|
||||
import { FooterWrapper } from "file/footer-wrapper";
|
||||
import { HeaderWrapper } from "file/header-wrapper";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
@ -18,6 +19,8 @@ import { IPageNumberTypeAttributes, PageNumberType } from "./page-number";
|
||||
import { PageSize } from "./page-size/page-size";
|
||||
import { IPageSizeAttributes, PageOrientation } from "./page-size/page-size-attributes";
|
||||
import { TitlePage } from "./title-page/title-page";
|
||||
import { Type } from "./type/section-type";
|
||||
import { SectionType } from "./type/section-type-attributes";
|
||||
import { ISectionVerticalAlignAttributes, SectionVerticalAlign } from "./vertical-align";
|
||||
|
||||
export interface IHeaderFooterGroup<T> {
|
||||
@ -51,12 +54,16 @@ export type SectionPropertiesOptions = IPageSizeAttributes &
|
||||
readonly column?: {
|
||||
readonly space?: number;
|
||||
readonly count?: number;
|
||||
readonly separate?: boolean;
|
||||
};
|
||||
readonly type?: SectionType;
|
||||
};
|
||||
// Need to decouple this from the attributes
|
||||
|
||||
export class SectionProperties extends XmlComponent {
|
||||
private readonly options: SectionPropertiesOptions;
|
||||
public readonly width: number;
|
||||
public readonly rightMargin: number;
|
||||
public readonly leftMargin: number;
|
||||
|
||||
constructor(options: SectionPropertiesOptions = { column: {} }) {
|
||||
super("w:sectPr");
|
||||
@ -64,10 +71,10 @@ export class SectionProperties extends XmlComponent {
|
||||
const {
|
||||
width = 11906,
|
||||
height = 16838,
|
||||
top = 1440,
|
||||
right = 1440,
|
||||
bottom = 1440,
|
||||
left = 1440,
|
||||
top = convertInchesToTwip(1),
|
||||
right = convertInchesToTwip(1),
|
||||
bottom = convertInchesToTwip(1),
|
||||
left = convertInchesToTwip(1),
|
||||
header = 708,
|
||||
footer = 708,
|
||||
gutter = 0,
|
||||
@ -90,12 +97,16 @@ export class SectionProperties extends XmlComponent {
|
||||
pageBorderLeft,
|
||||
titlePage = false,
|
||||
verticalAlign,
|
||||
type,
|
||||
} = options;
|
||||
|
||||
this.options = options;
|
||||
this.leftMargin = left;
|
||||
this.rightMargin = right;
|
||||
this.width = width;
|
||||
|
||||
this.root.push(new PageSize(width, height, orientation));
|
||||
this.root.push(new PageMargin(top, right, bottom, left, header, footer, gutter, mirror));
|
||||
this.root.push(new Columns(column.space ? column.space : 708, column.count ? column.count : 1));
|
||||
this.root.push(new Columns(column.space ? column.space : 708, column.count ? column.count : 1, column.separate ?? false));
|
||||
this.root.push(new DocumentGrid(linePitch));
|
||||
|
||||
this.addHeaders(headers);
|
||||
@ -128,6 +139,10 @@ export class SectionProperties extends XmlComponent {
|
||||
if (verticalAlign) {
|
||||
this.root.push(new SectionVerticalAlign(verticalAlign));
|
||||
}
|
||||
|
||||
if (type) {
|
||||
this.root.push(new Type(type));
|
||||
}
|
||||
}
|
||||
|
||||
private addHeaders(headers?: IHeaderFooterGroup<HeaderWrapper>): void {
|
||||
@ -136,7 +151,7 @@ export class SectionProperties extends XmlComponent {
|
||||
this.root.push(
|
||||
new HeaderReference({
|
||||
headerType: HeaderReferenceType.DEFAULT,
|
||||
headerId: headers.default.Header.ReferenceId,
|
||||
headerId: headers.default.View.ReferenceId,
|
||||
}),
|
||||
);
|
||||
}
|
||||
@ -145,7 +160,7 @@ export class SectionProperties extends XmlComponent {
|
||||
this.root.push(
|
||||
new HeaderReference({
|
||||
headerType: HeaderReferenceType.FIRST,
|
||||
headerId: headers.first.Header.ReferenceId,
|
||||
headerId: headers.first.View.ReferenceId,
|
||||
}),
|
||||
);
|
||||
}
|
||||
@ -154,7 +169,7 @@ export class SectionProperties extends XmlComponent {
|
||||
this.root.push(
|
||||
new HeaderReference({
|
||||
headerType: HeaderReferenceType.EVEN,
|
||||
headerId: headers.even.Header.ReferenceId,
|
||||
headerId: headers.even.View.ReferenceId,
|
||||
}),
|
||||
);
|
||||
}
|
||||
@ -167,7 +182,7 @@ export class SectionProperties extends XmlComponent {
|
||||
this.root.push(
|
||||
new FooterReference({
|
||||
footerType: FooterReferenceType.DEFAULT,
|
||||
footerId: footers.default.Footer.ReferenceId,
|
||||
footerId: footers.default.View.ReferenceId,
|
||||
}),
|
||||
);
|
||||
}
|
||||
@ -176,7 +191,7 @@ export class SectionProperties extends XmlComponent {
|
||||
this.root.push(
|
||||
new FooterReference({
|
||||
footerType: FooterReferenceType.FIRST,
|
||||
footerId: footers.first.Footer.ReferenceId,
|
||||
footerId: footers.first.View.ReferenceId,
|
||||
}),
|
||||
);
|
||||
}
|
||||
@ -185,14 +200,10 @@ export class SectionProperties extends XmlComponent {
|
||||
this.root.push(
|
||||
new FooterReference({
|
||||
footerType: FooterReferenceType.EVEN,
|
||||
footerId: footers.even.Footer.ReferenceId,
|
||||
footerId: footers.even.View.ReferenceId,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public get Options(): SectionPropertiesOptions {
|
||||
return this.options;
|
||||
}
|
||||
}
|
||||
|
2
src/file/document/body/section-properties/type/index.ts
Normal file
2
src/file/document/body/section-properties/type/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from "./section-type";
|
||||
export * from "./section-type-attributes";
|
@ -0,0 +1,17 @@
|
||||
import { XmlAttributeComponent } from "file/xml-components";
|
||||
|
||||
export enum SectionType {
|
||||
CONTINUOUS = "continuous",
|
||||
EVEN_PAGE = "evenPage",
|
||||
NEXT_COLUMN = "nextColumn",
|
||||
NEXT_PAGE = "nextPage",
|
||||
ODD_PAGE = "oddPage",
|
||||
}
|
||||
|
||||
export class SectionTypeAttributes extends XmlAttributeComponent<{
|
||||
readonly val: SectionType;
|
||||
}> {
|
||||
protected readonly xmlKeys = {
|
||||
val: "w:val",
|
||||
};
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
import { Type } from "./section-type";
|
||||
import { SectionType } from "./section-type-attributes";
|
||||
|
||||
describe("Type", () => {
|
||||
it("should create with even page section type", () => {
|
||||
const sectionType = new Type(SectionType.EVEN_PAGE);
|
||||
|
||||
const tree = new Formatter().format(sectionType);
|
||||
|
||||
expect(tree).to.deep.equal({
|
||||
"w:type": {
|
||||
_attr: {
|
||||
"w:val": "evenPage",
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("should create with continuous section type", () => {
|
||||
const sectionType = new Type(SectionType.CONTINUOUS);
|
||||
|
||||
const tree = new Formatter().format(sectionType);
|
||||
|
||||
expect(tree).to.deep.equal({
|
||||
"w:type": {
|
||||
_attr: {
|
||||
"w:val": "continuous",
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
@ -0,0 +1,10 @@
|
||||
// http://officeopenxml.com/WPsection.php
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { SectionType, SectionTypeAttributes } from "./section-type-attributes";
|
||||
|
||||
export class Type extends XmlComponent {
|
||||
constructor(value: SectionType) {
|
||||
super("w:type");
|
||||
this.root.push(new SectionTypeAttributes({ val: value }));
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { DocumentBackground } from "./document-background";
|
||||
|
||||
describe("DocumentBackground", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create a DocumentBackground with no options and set color to auto", () => {
|
||||
const documentBackground = new DocumentBackground({});
|
||||
const tree = new Formatter().format(documentBackground);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:background": {
|
||||
_attr: {
|
||||
"w:color": "FFFFFF",
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("should create a DocumentBackground with no options and set color to value", () => {
|
||||
const documentBackground = new DocumentBackground({ color: "ffff00" });
|
||||
const tree = new Formatter().format(documentBackground);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:background": {
|
||||
_attr: {
|
||||
"w:color": "ffff00",
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("should create a DocumentBackground with no options and set other values", () => {
|
||||
const documentBackground = new DocumentBackground({
|
||||
color: "ffff00",
|
||||
themeColor: "test",
|
||||
themeShade: "test",
|
||||
themeTint: "test",
|
||||
});
|
||||
const tree = new Formatter().format(documentBackground);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:background": {
|
||||
_attr: {
|
||||
"w:color": "ffff00",
|
||||
"w:themeColor": "test",
|
||||
"w:themeShade": "test",
|
||||
"w:themeTint": "test",
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
39
src/file/document/document-background/document-background.ts
Normal file
39
src/file/document/document-background/document-background.ts
Normal file
@ -0,0 +1,39 @@
|
||||
// http://officeopenxml.com/WPdocument.php
|
||||
// http://www.datypic.com/sc/ooxml/e-w_background-1.html
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
|
||||
export class DocumentBackgroundAttributes extends XmlAttributeComponent<{
|
||||
readonly color: string;
|
||||
readonly themeColor?: string;
|
||||
readonly themeShade?: string;
|
||||
readonly themeTint?: string;
|
||||
}> {
|
||||
protected readonly xmlKeys = {
|
||||
color: "w:color",
|
||||
themeColor: "w:themeColor",
|
||||
themeShade: "w:themeShade",
|
||||
themeTint: "w:themeTint",
|
||||
};
|
||||
}
|
||||
|
||||
export interface IDocumentBackgroundOptions {
|
||||
readonly color?: string;
|
||||
readonly themeColor?: string;
|
||||
readonly themeShade?: string;
|
||||
readonly themeTint?: string;
|
||||
}
|
||||
|
||||
export class DocumentBackground extends XmlComponent {
|
||||
constructor(options: IDocumentBackgroundOptions) {
|
||||
super("w:background");
|
||||
|
||||
this.root.push(
|
||||
new DocumentBackgroundAttributes({
|
||||
color: options.color ? options.color : "FFFFFF",
|
||||
themeColor: options.themeColor,
|
||||
themeShade: options.themeShade,
|
||||
themeTint: options.themeTint,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
1
src/file/document/document-background/index.ts
Normal file
1
src/file/document/document-background/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from "./document-background";
|
@ -8,7 +8,7 @@ describe("Document", () => {
|
||||
let document: Document;
|
||||
|
||||
beforeEach(() => {
|
||||
document = new Document();
|
||||
document = new Document({ background: {} });
|
||||
});
|
||||
|
||||
describe("#constructor()", () => {
|
||||
@ -38,6 +38,13 @@ describe("Document", () => {
|
||||
"mc:Ignorable": "w14 w15 wp14",
|
||||
},
|
||||
},
|
||||
{
|
||||
"w:background": {
|
||||
_attr: {
|
||||
"w:color": "FFFFFF",
|
||||
},
|
||||
},
|
||||
},
|
||||
{ "w:body": {} },
|
||||
],
|
||||
});
|
||||
|
@ -1,15 +1,20 @@
|
||||
// http://officeopenxml.com/WPdocument.php
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { Hyperlink, Paragraph } from "../paragraph";
|
||||
import { ConcreteHyperlink, Paragraph } from "../paragraph";
|
||||
import { Table } from "../table";
|
||||
import { TableOfContents } from "../table-of-contents";
|
||||
import { Body } from "./body";
|
||||
import { DocumentAttributes } from "./document-attributes";
|
||||
import { DocumentBackground, IDocumentBackgroundOptions } from "./document-background";
|
||||
|
||||
export interface IDocumentOptions {
|
||||
readonly background: IDocumentBackgroundOptions;
|
||||
}
|
||||
|
||||
export class Document extends XmlComponent {
|
||||
private readonly body: Body;
|
||||
|
||||
constructor() {
|
||||
constructor(options: IDocumentOptions) {
|
||||
super("w:document");
|
||||
this.root.push(
|
||||
new DocumentAttributes({
|
||||
@ -33,10 +38,11 @@ export class Document extends XmlComponent {
|
||||
}),
|
||||
);
|
||||
this.body = new Body();
|
||||
this.root.push(new DocumentBackground(options.background));
|
||||
this.root.push(this.body);
|
||||
}
|
||||
|
||||
public add(item: Paragraph | Table | TableOfContents | Hyperlink): Document {
|
||||
public add(item: Paragraph | Table | TableOfContents | ConcreteHyperlink): Document {
|
||||
this.body.push(item);
|
||||
return this;
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
export * from "./document";
|
||||
export * from "./document-attributes";
|
||||
export * from "./body";
|
||||
export * from "./document-background";
|
||||
|
@ -218,5 +218,150 @@ describe("Anchor", () => {
|
||||
const textWrap = newJson.root[6];
|
||||
assert.equal(textWrap.rootKey, "wp:wrapTopAndBottom");
|
||||
});
|
||||
|
||||
it("should create a Drawing with a margin", () => {
|
||||
anchor = createAnchor({
|
||||
floating: {
|
||||
verticalPosition: {
|
||||
offset: 0,
|
||||
},
|
||||
horizontalPosition: {
|
||||
offset: 0,
|
||||
},
|
||||
margins: {
|
||||
top: 10,
|
||||
left: 10,
|
||||
bottom: 10,
|
||||
right: 10,
|
||||
},
|
||||
},
|
||||
});
|
||||
const newJson = Utility.jsonify(anchor);
|
||||
const anchorAttributes = newJson.root[0].root;
|
||||
assert.include(anchorAttributes, {
|
||||
distT: 10,
|
||||
distB: 10,
|
||||
distL: 10,
|
||||
distR: 10,
|
||||
});
|
||||
});
|
||||
|
||||
it("should create a Drawing with a default margin", () => {
|
||||
anchor = createAnchor({
|
||||
floating: {
|
||||
verticalPosition: {
|
||||
offset: 0,
|
||||
},
|
||||
horizontalPosition: {
|
||||
offset: 0,
|
||||
},
|
||||
margins: {},
|
||||
},
|
||||
});
|
||||
const newJson = Utility.jsonify(anchor);
|
||||
const anchorAttributes = newJson.root[0].root;
|
||||
assert.include(anchorAttributes, {
|
||||
distT: 0,
|
||||
distB: 0,
|
||||
distL: 0,
|
||||
distR: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it("should create a Drawing with allowOverlap being false", () => {
|
||||
anchor = createAnchor({
|
||||
floating: {
|
||||
verticalPosition: {
|
||||
offset: 0,
|
||||
},
|
||||
horizontalPosition: {
|
||||
offset: 0,
|
||||
},
|
||||
allowOverlap: false,
|
||||
},
|
||||
});
|
||||
const newJson = Utility.jsonify(anchor);
|
||||
const anchorAttributes = newJson.root[0].root;
|
||||
assert.include(anchorAttributes, {
|
||||
allowOverlap: "0",
|
||||
});
|
||||
});
|
||||
|
||||
it("should create a Drawing with behindDocument being true", () => {
|
||||
anchor = createAnchor({
|
||||
floating: {
|
||||
verticalPosition: {
|
||||
offset: 0,
|
||||
},
|
||||
horizontalPosition: {
|
||||
offset: 0,
|
||||
},
|
||||
behindDocument: true,
|
||||
},
|
||||
});
|
||||
const newJson = Utility.jsonify(anchor);
|
||||
const anchorAttributes = newJson.root[0].root;
|
||||
assert.include(anchorAttributes, {
|
||||
behindDoc: "1",
|
||||
});
|
||||
});
|
||||
|
||||
it("should create a Drawing with locked being true", () => {
|
||||
anchor = createAnchor({
|
||||
floating: {
|
||||
verticalPosition: {
|
||||
offset: 0,
|
||||
},
|
||||
horizontalPosition: {
|
||||
offset: 0,
|
||||
},
|
||||
lockAnchor: true,
|
||||
},
|
||||
});
|
||||
const newJson = Utility.jsonify(anchor);
|
||||
const anchorAttributes = newJson.root[0].root;
|
||||
assert.include(anchorAttributes, {
|
||||
locked: "1",
|
||||
});
|
||||
});
|
||||
|
||||
it("should create a Drawing with locked being false", () => {
|
||||
anchor = createAnchor({
|
||||
floating: {
|
||||
verticalPosition: {
|
||||
offset: 0,
|
||||
},
|
||||
horizontalPosition: {
|
||||
offset: 0,
|
||||
},
|
||||
layoutInCell: false,
|
||||
},
|
||||
});
|
||||
const newJson = Utility.jsonify(anchor);
|
||||
const anchorAttributes = newJson.root[0].root;
|
||||
assert.include(anchorAttributes, {
|
||||
layoutInCell: "0",
|
||||
});
|
||||
});
|
||||
|
||||
it("should create a Drawing with a certain z-index", () => {
|
||||
anchor = createAnchor({
|
||||
floating: {
|
||||
verticalPosition: {
|
||||
offset: 0,
|
||||
},
|
||||
horizontalPosition: {
|
||||
offset: 0,
|
||||
},
|
||||
zIndex: 120,
|
||||
},
|
||||
});
|
||||
const newJson = Utility.jsonify(anchor);
|
||||
const anchorAttributes = newJson.root[0].root;
|
||||
|
||||
assert.include(anchorAttributes, {
|
||||
relativeHeight: 120,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -11,42 +11,32 @@ import { Extent } from "./../extent/extent";
|
||||
import { GraphicFrameProperties } from "./../graphic-frame/graphic-frame-properties";
|
||||
import { AnchorAttributes } from "./anchor-attributes";
|
||||
|
||||
const defaultOptions: IFloating = {
|
||||
allowOverlap: true,
|
||||
behindDocument: false,
|
||||
lockAnchor: false,
|
||||
layoutInCell: true,
|
||||
verticalPosition: {},
|
||||
horizontalPosition: {},
|
||||
};
|
||||
|
||||
export class Anchor extends XmlComponent {
|
||||
constructor(mediaData: IMediaData, dimensions: IMediaDataDimensions, drawingOptions: IDrawingOptions) {
|
||||
super("wp:anchor");
|
||||
|
||||
const floating = {
|
||||
margins: {
|
||||
top: 0,
|
||||
bottom: 0,
|
||||
left: 0,
|
||||
right: 0,
|
||||
},
|
||||
...defaultOptions,
|
||||
const floating: IFloating = {
|
||||
allowOverlap: true,
|
||||
behindDocument: false,
|
||||
lockAnchor: false,
|
||||
layoutInCell: true,
|
||||
verticalPosition: {},
|
||||
horizontalPosition: {},
|
||||
...drawingOptions.floating,
|
||||
};
|
||||
|
||||
this.root.push(
|
||||
new AnchorAttributes({
|
||||
distT: floating.margins.top || 0,
|
||||
distB: floating.margins.bottom || 0,
|
||||
distL: floating.margins.left || 0,
|
||||
distR: floating.margins.right || 0,
|
||||
distT: floating.margins ? floating.margins.top || 0 : 0,
|
||||
distB: floating.margins ? floating.margins.bottom || 0 : 0,
|
||||
distL: floating.margins ? floating.margins.left || 0 : 0,
|
||||
distR: floating.margins ? floating.margins.right || 0 : 0,
|
||||
simplePos: "0", // note: word doesn't fully support - so we use 0
|
||||
allowOverlap: floating.allowOverlap === true ? "1" : "0",
|
||||
behindDoc: floating.behindDocument === true ? "1" : "0",
|
||||
locked: floating.lockAnchor === true ? "1" : "0",
|
||||
layoutInCell: floating.layoutInCell === true ? "1" : "0",
|
||||
relativeHeight: dimensions.emus.y,
|
||||
relativeHeight: floating.zIndex ? floating.zIndex : dimensions.emus.y,
|
||||
}),
|
||||
);
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
// http://officeopenxml.com/drwPicFloating-position.php
|
||||
// http://officeopenxml.com/drwPicFloating.php
|
||||
import { ITextWrapping } from "../text-wrap";
|
||||
|
||||
export enum HorizontalPositionRelativeFrom {
|
||||
@ -67,4 +68,5 @@ export interface IFloating {
|
||||
readonly layoutInCell?: boolean;
|
||||
readonly margins?: IMargins;
|
||||
readonly wrap?: ITextWrapping;
|
||||
readonly zIndex?: number;
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ export class Inline extends XmlComponent {
|
||||
private readonly extent: Extent;
|
||||
private readonly graphic: Graphic;
|
||||
|
||||
constructor(readonly mediaData: IMediaData, private readonly dimensions: IMediaDataDimensions) {
|
||||
constructor(mediaData: IMediaData, private readonly dimensions: IMediaDataDimensions) {
|
||||
super("wp:inline");
|
||||
|
||||
this.root.push(
|
||||
|
@ -5,7 +5,7 @@ import { Formatter } from "export/formatter";
|
||||
|
||||
import { File } from "./file";
|
||||
import { Footer, Header } from "./header";
|
||||
import { HyperlinkRef, HyperlinkType, Paragraph } from "./paragraph";
|
||||
import { Paragraph } from "./paragraph";
|
||||
import { Table, TableCell, TableRow } from "./table";
|
||||
import { TableOfContents } from "./table-of-contents";
|
||||
|
||||
@ -18,7 +18,7 @@ describe("File", () => {
|
||||
children: [],
|
||||
});
|
||||
|
||||
const tree = new Formatter().format(doc.Document.Body);
|
||||
const tree = new Formatter().format(doc.Document.View.Body);
|
||||
|
||||
expect(tree["w:body"][0]["w:sectPr"][4]["w:headerReference"]._attr["w:type"]).to.equal("default");
|
||||
expect(tree["w:body"][0]["w:sectPr"][5]["w:footerReference"]._attr["w:type"]).to.equal("default");
|
||||
@ -37,7 +37,7 @@ describe("File", () => {
|
||||
children: [],
|
||||
});
|
||||
|
||||
const tree = new Formatter().format(doc.Document.Body);
|
||||
const tree = new Formatter().format(doc.Document.View.Body);
|
||||
|
||||
expect(tree["w:body"][0]["w:sectPr"][4]["w:headerReference"]._attr["w:type"]).to.equal("default");
|
||||
expect(tree["w:body"][0]["w:sectPr"][5]["w:footerReference"]._attr["w:type"]).to.equal("default");
|
||||
@ -56,7 +56,7 @@ describe("File", () => {
|
||||
children: [],
|
||||
});
|
||||
|
||||
const tree = new Formatter().format(doc.Document.Body);
|
||||
const tree = new Formatter().format(doc.Document.View.Body);
|
||||
|
||||
expect(tree["w:body"][0]["w:sectPr"][5]["w:headerReference"]._attr["w:type"]).to.equal("first");
|
||||
expect(tree["w:body"][0]["w:sectPr"][7]["w:footerReference"]._attr["w:type"]).to.equal("first");
|
||||
@ -79,7 +79,7 @@ describe("File", () => {
|
||||
children: [],
|
||||
});
|
||||
|
||||
const tree = new Formatter().format(doc.Document.Body);
|
||||
const tree = new Formatter().format(doc.Document.View.Body);
|
||||
|
||||
expect(tree["w:body"][0]["w:sectPr"][4]["w:headerReference"]._attr["w:type"]).to.equal("default");
|
||||
expect(tree["w:body"][0]["w:sectPr"][5]["w:headerReference"]._attr["w:type"]).to.equal("first");
|
||||
@ -97,7 +97,7 @@ describe("File", () => {
|
||||
},
|
||||
]);
|
||||
|
||||
const tree = new Formatter().format(doc.Document.Body);
|
||||
const tree = new Formatter().format(doc.Document.View.Body);
|
||||
|
||||
expect(tree).to.deep.equal({
|
||||
"w:body": [
|
||||
@ -148,6 +148,7 @@ describe("File", () => {
|
||||
"w:cols": {
|
||||
_attr: {
|
||||
"w:num": 1,
|
||||
"w:sep": false,
|
||||
"w:space": 708,
|
||||
},
|
||||
},
|
||||
@ -164,22 +165,12 @@ describe("File", () => {
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("should add hyperlink child", () => {
|
||||
const doc = new File(undefined, undefined, [
|
||||
{
|
||||
children: [new HyperlinkRef("test")],
|
||||
},
|
||||
]);
|
||||
|
||||
expect(doc.HyperlinkCache).to.deep.equal({});
|
||||
});
|
||||
});
|
||||
|
||||
describe("#addSection", () => {
|
||||
it("should call the underlying document's add a Paragraph", () => {
|
||||
const file = new File();
|
||||
const spy = sinon.spy(file.Document, "add");
|
||||
const spy = sinon.spy(file.Document.View, "add");
|
||||
file.addSection({
|
||||
children: [new Paragraph({})],
|
||||
});
|
||||
@ -187,19 +178,9 @@ describe("File", () => {
|
||||
expect(spy.called).to.equal(true);
|
||||
});
|
||||
|
||||
it("should add hyperlink child", () => {
|
||||
const doc = new File();
|
||||
|
||||
doc.addSection({
|
||||
children: [new HyperlinkRef("test")],
|
||||
});
|
||||
|
||||
expect(doc.HyperlinkCache).to.deep.equal({});
|
||||
});
|
||||
|
||||
it("should call the underlying document's add when adding a Table", () => {
|
||||
const file = new File();
|
||||
const spy = sinon.spy(file.Document, "add");
|
||||
const spy = sinon.spy(file.Document.View, "add");
|
||||
file.addSection({
|
||||
children: [
|
||||
new Table({
|
||||
@ -221,7 +202,7 @@ describe("File", () => {
|
||||
|
||||
it("should call the underlying document's add when adding an Image (paragraph)", () => {
|
||||
const file = new File();
|
||||
const spy = sinon.spy(file.Document, "add");
|
||||
const spy = sinon.spy(file.Document.View, "add");
|
||||
// tslint:disable-next-line:no-any
|
||||
file.addSection({
|
||||
children: [new Paragraph("")],
|
||||
@ -234,34 +215,25 @@ describe("File", () => {
|
||||
describe("#addSection", () => {
|
||||
it("should call the underlying document's add", () => {
|
||||
const file = new File();
|
||||
const spy = sinon.spy(file.Document, "add");
|
||||
const spy = sinon.spy(file.Document.View, "add");
|
||||
file.addSection({
|
||||
children: [new TableOfContents()],
|
||||
});
|
||||
|
||||
expect(spy.called).to.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
it.only("should create hyperlinks", () => {
|
||||
const wrapper = new File({
|
||||
hyperlinks: {
|
||||
myHyperLink: {
|
||||
link: "test.com",
|
||||
text: "test",
|
||||
type: HyperlinkType.EXTERNAL,
|
||||
},
|
||||
describe("#addTrackRevisionsFeature", () => {
|
||||
it("should call the underlying document's add", () => {
|
||||
const file = new File({
|
||||
features: {
|
||||
trackRevisions: true,
|
||||
},
|
||||
});
|
||||
|
||||
expect(wrapper.HyperlinkCache.myHyperLink).to.not.be.undefined("");
|
||||
});
|
||||
});
|
||||
|
||||
describe("#HyperlinkCache", () => {
|
||||
it("should initially have empty hyperlink cache", () => {
|
||||
const file = new File();
|
||||
|
||||
expect(file.HyperlinkCache).to.deep.equal({});
|
||||
// tslint:disable-next-line: no-unused-expression no-string-literal
|
||||
expect(file.Settings["trackRevisions"]).to.exist;
|
||||
});
|
||||
});
|
||||
|
||||
@ -275,7 +247,7 @@ describe("File", () => {
|
||||
},
|
||||
});
|
||||
|
||||
const tree = new Formatter().format(wrapper.FootNotes);
|
||||
const tree = new Formatter().format(wrapper.FootNotes.View);
|
||||
|
||||
expect(tree).to.deep.equal({
|
||||
"w:footnotes": [
|
||||
|
135
src/file/file.ts
135
src/file/file.ts
@ -1,9 +1,8 @@
|
||||
import * as shortid from "shortid";
|
||||
|
||||
import { AppProperties } from "./app-properties/app-properties";
|
||||
import { ContentTypes } from "./content-types/content-types";
|
||||
import { CoreProperties, IPropertiesOptions } from "./core-properties";
|
||||
import { Document } from "./document";
|
||||
import { CustomProperties } from "./custom-properties";
|
||||
import { DocumentWrapper } from "./document-wrapper";
|
||||
import {
|
||||
FooterReferenceType,
|
||||
HeaderReferenceType,
|
||||
@ -13,14 +12,13 @@ import {
|
||||
import { IPageMarginAttributes } from "./document/body/section-properties/page-margin/page-margin-attributes";
|
||||
import { IFileProperties } from "./file-properties";
|
||||
import { FooterWrapper, IDocumentFooter } from "./footer-wrapper";
|
||||
import { FootNotes } from "./footnotes";
|
||||
import { FootnotesWrapper } from "./footnotes-wrapper";
|
||||
import { Footer, Header } from "./header";
|
||||
import { HeaderWrapper, IDocumentHeader } from "./header-wrapper";
|
||||
import { Media } from "./media";
|
||||
import { Numbering } from "./numbering";
|
||||
import { Hyperlink, HyperlinkRef, HyperlinkType, Paragraph } from "./paragraph";
|
||||
import { Paragraph } from "./paragraph";
|
||||
import { Relationships } from "./relationships";
|
||||
import { TargetModeType } from "./relationships/relationship/relationship";
|
||||
import { Settings } from "./settings";
|
||||
import { Styles } from "./styles";
|
||||
import { ExternalStylesFactory } from "./styles/external-styles-factory";
|
||||
@ -42,27 +40,26 @@ export interface ISectionOptions {
|
||||
readonly size?: IPageSizeAttributes;
|
||||
readonly margins?: IPageMarginAttributes;
|
||||
readonly properties?: SectionPropertiesOptions;
|
||||
readonly children: Array<Paragraph | Table | TableOfContents | HyperlinkRef>;
|
||||
readonly children: (Paragraph | Table | TableOfContents)[];
|
||||
}
|
||||
|
||||
export class File {
|
||||
// tslint:disable-next-line:readonly-keyword
|
||||
private currentRelationshipId: number = 1;
|
||||
|
||||
private readonly document: Document;
|
||||
private readonly documentWrapper: DocumentWrapper;
|
||||
private readonly headers: IDocumentHeader[] = [];
|
||||
private readonly footers: IDocumentFooter[] = [];
|
||||
private readonly docRelationships: Relationships;
|
||||
private readonly coreProperties: CoreProperties;
|
||||
private readonly numbering: Numbering;
|
||||
private readonly media: Media;
|
||||
private readonly fileRelationships: Relationships;
|
||||
private readonly footNotes: FootNotes;
|
||||
private readonly footnotesWrapper: FootnotesWrapper;
|
||||
private readonly settings: Settings;
|
||||
private readonly contentTypes: ContentTypes;
|
||||
private readonly customProperties: CustomProperties;
|
||||
private readonly appProperties: AppProperties;
|
||||
private readonly styles: Styles;
|
||||
private readonly hyperlinkCache: { readonly [key: string]: Hyperlink } = {};
|
||||
|
||||
constructor(
|
||||
options: IPropertiesOptions = {
|
||||
@ -81,13 +78,15 @@ export class File {
|
||||
config: [],
|
||||
},
|
||||
);
|
||||
this.docRelationships = new Relationships();
|
||||
this.fileRelationships = new Relationships();
|
||||
this.customProperties = new CustomProperties(options.customProperties ?? []);
|
||||
this.appProperties = new AppProperties();
|
||||
this.footNotes = new FootNotes();
|
||||
this.footnotesWrapper = new FootnotesWrapper();
|
||||
this.contentTypes = new ContentTypes();
|
||||
this.document = new Document();
|
||||
this.settings = new Settings();
|
||||
this.documentWrapper = new DocumentWrapper({ background: options.background || {} });
|
||||
this.settings = new Settings({
|
||||
compatabilityModeVersion: options.compatabilityModeVersion,
|
||||
});
|
||||
|
||||
this.media = fileProperties.template && fileProperties.template.media ? fileProperties.template.media : new Media();
|
||||
|
||||
@ -107,7 +106,7 @@ export class File {
|
||||
this.styles = stylesFactory.newInstance(options.externalStyles);
|
||||
} else if (options.styles) {
|
||||
const stylesFactory = new DefaultStylesFactory();
|
||||
const defaultStyles = stylesFactory.newInstance();
|
||||
const defaultStyles = stylesFactory.newInstance(options.styles.default);
|
||||
this.styles = new Styles({
|
||||
...defaultStyles,
|
||||
...options.styles,
|
||||
@ -132,16 +131,10 @@ export class File {
|
||||
}
|
||||
|
||||
for (const section of sections) {
|
||||
this.document.Body.addSection(section.properties ? section.properties : {});
|
||||
this.documentWrapper.View.Body.addSection(section.properties ? section.properties : {});
|
||||
|
||||
for (const child of section.children) {
|
||||
if (child instanceof HyperlinkRef) {
|
||||
const hyperlink = this.hyperlinkCache[child.id];
|
||||
this.document.add(hyperlink);
|
||||
continue;
|
||||
}
|
||||
|
||||
this.document.add(child);
|
||||
this.documentWrapper.View.add(child);
|
||||
}
|
||||
}
|
||||
|
||||
@ -151,29 +144,14 @@ export class File {
|
||||
continue;
|
||||
}
|
||||
|
||||
this.footNotes.createFootNote(parseFloat(key), options.footnotes[key].children);
|
||||
this.footnotesWrapper.View.createFootNote(parseFloat(key), options.footnotes[key].children);
|
||||
}
|
||||
}
|
||||
|
||||
if (options.hyperlinks) {
|
||||
const cache = {};
|
||||
|
||||
for (const key in options.hyperlinks) {
|
||||
if (!options.hyperlinks[key]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const hyperlinkRef = options.hyperlinks[key];
|
||||
|
||||
const hyperlink =
|
||||
hyperlinkRef.type === HyperlinkType.EXTERNAL
|
||||
? this.createHyperlink(hyperlinkRef.link, hyperlinkRef.text)
|
||||
: this.createInternalHyperLink(key, hyperlinkRef.text);
|
||||
|
||||
cache[key] = hyperlink;
|
||||
if (options.features) {
|
||||
if (options.features.trackRevisions) {
|
||||
this.settings.addTrackRevisions();
|
||||
}
|
||||
|
||||
this.hyperlinkCache = cache;
|
||||
}
|
||||
}
|
||||
|
||||
@ -185,7 +163,7 @@ export class File {
|
||||
properties,
|
||||
children,
|
||||
}: ISectionOptions): void {
|
||||
this.document.Body.addSection({
|
||||
this.documentWrapper.View.Body.addSection({
|
||||
...properties,
|
||||
headers: {
|
||||
default: headers.default ? this.createHeader(headers.default) : this.createHeader(new Header()),
|
||||
@ -202,40 +180,16 @@ export class File {
|
||||
});
|
||||
|
||||
for (const child of children) {
|
||||
if (child instanceof HyperlinkRef) {
|
||||
const hyperlink = this.hyperlinkCache[child.id];
|
||||
this.document.add(hyperlink);
|
||||
continue;
|
||||
}
|
||||
|
||||
this.document.add(child);
|
||||
this.documentWrapper.View.add(child);
|
||||
}
|
||||
}
|
||||
|
||||
public verifyUpdateFields(): void {
|
||||
if (this.document.getTablesOfContents().length) {
|
||||
if (this.documentWrapper.View.getTablesOfContents().length) {
|
||||
this.settings.addUpdateFields();
|
||||
}
|
||||
}
|
||||
|
||||
private createHyperlink(link: string, text: string = link): Hyperlink {
|
||||
const hyperlink = new Hyperlink(text, shortid.generate().toLowerCase());
|
||||
this.docRelationships.createRelationship(
|
||||
hyperlink.linkId,
|
||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
|
||||
link,
|
||||
TargetModeType.EXTERNAL,
|
||||
);
|
||||
return hyperlink;
|
||||
}
|
||||
|
||||
private createInternalHyperLink(anchor: string, text: string = anchor): Hyperlink {
|
||||
const hyperlink = new Hyperlink(text, shortid.generate().toLowerCase(), anchor);
|
||||
// NOTE: unlike File#createHyperlink(), since the link is to an internal bookmark
|
||||
// we don't need to create a new relationship.
|
||||
return hyperlink;
|
||||
}
|
||||
|
||||
private createHeader(header: Header): HeaderWrapper {
|
||||
const wrapper = new HeaderWrapper(this.media, this.currentRelationshipId++);
|
||||
|
||||
@ -260,8 +214,8 @@ export class File {
|
||||
|
||||
private addHeaderToDocument(header: HeaderWrapper, type: HeaderReferenceType = HeaderReferenceType.DEFAULT): void {
|
||||
this.headers.push({ header, type });
|
||||
this.docRelationships.createRelationship(
|
||||
header.Header.ReferenceId,
|
||||
this.documentWrapper.Relationships.createRelationship(
|
||||
header.View.ReferenceId,
|
||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/header",
|
||||
`header${this.headers.length}.xml`,
|
||||
);
|
||||
@ -270,8 +224,8 @@ export class File {
|
||||
|
||||
private addFooterToDocument(footer: FooterWrapper, type: FooterReferenceType = FooterReferenceType.DEFAULT): void {
|
||||
this.footers.push({ footer, type });
|
||||
this.docRelationships.createRelationship(
|
||||
footer.Footer.ReferenceId,
|
||||
this.documentWrapper.Relationships.createRelationship(
|
||||
footer.View.ReferenceId,
|
||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer",
|
||||
`footer${this.footers.length}.xml`,
|
||||
);
|
||||
@ -294,31 +248,36 @@ export class File {
|
||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties",
|
||||
"docProps/app.xml",
|
||||
);
|
||||
this.fileRelationships.createRelationship(
|
||||
4,
|
||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties",
|
||||
"docProps/custom.xml",
|
||||
);
|
||||
|
||||
this.docRelationships.createRelationship(
|
||||
this.documentWrapper.Relationships.createRelationship(
|
||||
this.currentRelationshipId++,
|
||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
|
||||
"styles.xml",
|
||||
);
|
||||
this.docRelationships.createRelationship(
|
||||
this.documentWrapper.Relationships.createRelationship(
|
||||
this.currentRelationshipId++,
|
||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering",
|
||||
"numbering.xml",
|
||||
);
|
||||
this.docRelationships.createRelationship(
|
||||
this.documentWrapper.Relationships.createRelationship(
|
||||
this.currentRelationshipId++,
|
||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes",
|
||||
"footnotes.xml",
|
||||
);
|
||||
this.docRelationships.createRelationship(
|
||||
this.documentWrapper.Relationships.createRelationship(
|
||||
this.currentRelationshipId++,
|
||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings",
|
||||
"settings.xml",
|
||||
);
|
||||
}
|
||||
|
||||
public get Document(): Document {
|
||||
return this.document;
|
||||
public get Document(): DocumentWrapper {
|
||||
return this.documentWrapper;
|
||||
}
|
||||
|
||||
public get Styles(): Styles {
|
||||
@ -337,10 +296,6 @@ export class File {
|
||||
return this.media;
|
||||
}
|
||||
|
||||
public get DocumentRelationships(): Relationships {
|
||||
return this.docRelationships;
|
||||
}
|
||||
|
||||
public get FileRelationships(): Relationships {
|
||||
return this.fileRelationships;
|
||||
}
|
||||
@ -357,19 +312,19 @@ export class File {
|
||||
return this.contentTypes;
|
||||
}
|
||||
|
||||
public get CustomProperties(): CustomProperties {
|
||||
return this.customProperties;
|
||||
}
|
||||
|
||||
public get AppProperties(): AppProperties {
|
||||
return this.appProperties;
|
||||
}
|
||||
|
||||
public get FootNotes(): FootNotes {
|
||||
return this.footNotes;
|
||||
public get FootNotes(): FootnotesWrapper {
|
||||
return this.footnotesWrapper;
|
||||
}
|
||||
|
||||
public get Settings(): Settings {
|
||||
return this.settings;
|
||||
}
|
||||
|
||||
public get HyperlinkCache(): { readonly [key: string]: Hyperlink } {
|
||||
return this.hyperlinkCache;
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ describe("FooterWrapper", () => {
|
||||
describe("#add", () => {
|
||||
it("should call the underlying footer's addParagraph", () => {
|
||||
const file = new FooterWrapper(new Media(), 1);
|
||||
const spy = sinon.spy(file.Footer, "add");
|
||||
const spy = sinon.spy(file.View, "add");
|
||||
file.add(new Paragraph({}));
|
||||
|
||||
expect(spy.called).to.equal(true);
|
||||
@ -18,7 +18,7 @@ describe("FooterWrapper", () => {
|
||||
|
||||
it("should call the underlying footer's addParagraph", () => {
|
||||
const file = new FooterWrapper(new Media(), 1);
|
||||
const spy = sinon.spy(file.Footer, "add");
|
||||
const spy = sinon.spy(file.View, "add");
|
||||
file.add(
|
||||
new Table({
|
||||
rows: [
|
||||
@ -40,7 +40,7 @@ describe("FooterWrapper", () => {
|
||||
describe("#addChildElement", () => {
|
||||
it("should call the underlying footer's addChildElement", () => {
|
||||
const file = new FooterWrapper(new Media(), 1);
|
||||
const spy = sinon.spy(file.Footer, "addChildElement");
|
||||
const spy = sinon.spy(file.View, "addChildElement");
|
||||
// tslint:disable-next-line:no-any
|
||||
file.addChildElement({} as any);
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { FooterReferenceType } from "./document";
|
||||
import { IViewWrapper } from "./document-wrapper";
|
||||
import { Footer } from "./footer/footer";
|
||||
import { Media } from "./media";
|
||||
import { Paragraph } from "./paragraph";
|
||||
@ -12,7 +13,7 @@ export interface IDocumentFooter {
|
||||
readonly type: FooterReferenceType;
|
||||
}
|
||||
|
||||
export class FooterWrapper {
|
||||
export class FooterWrapper implements IViewWrapper {
|
||||
private readonly footer: Footer;
|
||||
private readonly relationships: Relationships;
|
||||
|
||||
@ -29,7 +30,7 @@ export class FooterWrapper {
|
||||
this.footer.addChildElement(childElement);
|
||||
}
|
||||
|
||||
public get Footer(): Footer {
|
||||
public get View(): Footer {
|
||||
return this.footer;
|
||||
}
|
||||
|
||||
|
16
src/file/footnotes-wrapper.spec.ts
Normal file
16
src/file/footnotes-wrapper.spec.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { FootnotesWrapper } from "./footnotes-wrapper";
|
||||
|
||||
describe("FootnotesWrapper", () => {
|
||||
describe("#constructor", () => {
|
||||
it("should create", () => {
|
||||
const file = new FootnotesWrapper();
|
||||
|
||||
// tslint:disable-next-line: no-unused-expression
|
||||
expect(file.View).to.be.ok;
|
||||
// tslint:disable-next-line: no-unused-expression
|
||||
expect(file.Relationships).to.be.ok;
|
||||
});
|
||||
});
|
||||
});
|
21
src/file/footnotes-wrapper.ts
Normal file
21
src/file/footnotes-wrapper.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { IViewWrapper } from "./document-wrapper";
|
||||
import { FootNotes } from "./footnotes/footnotes";
|
||||
import { Relationships } from "./relationships";
|
||||
|
||||
export class FootnotesWrapper implements IViewWrapper {
|
||||
private readonly footnotess: FootNotes;
|
||||
private readonly relationships: Relationships;
|
||||
|
||||
constructor() {
|
||||
this.footnotess = new FootNotes();
|
||||
this.relationships = new Relationships();
|
||||
}
|
||||
|
||||
public get View(): FootNotes {
|
||||
return this.footnotess;
|
||||
}
|
||||
|
||||
public get Relationships(): Relationships {
|
||||
return this.relationships;
|
||||
}
|
||||
}
|
@ -9,8 +9,14 @@ export enum FootnoteType {
|
||||
CONTINUATION_SEPERATOR = "continuationSeparator",
|
||||
}
|
||||
|
||||
export interface IFootnoteOptions {
|
||||
readonly id: number;
|
||||
readonly type?: FootnoteType;
|
||||
readonly children: Paragraph[];
|
||||
}
|
||||
|
||||
export class Footnote extends XmlComponent {
|
||||
constructor(options: { readonly id: number; readonly type?: FootnoteType; readonly children: Paragraph[] }) {
|
||||
constructor(options: IFootnoteOptions) {
|
||||
super("w:footnote");
|
||||
this.root.push(
|
||||
new FootnoteAttributes({
|
||||
|
@ -72,6 +72,7 @@ export class FootNotes extends XmlComponent {
|
||||
id: id,
|
||||
children: paragraph,
|
||||
});
|
||||
|
||||
this.root.push(footnote);
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ describe("HeaderWrapper", () => {
|
||||
describe("#add", () => {
|
||||
it("should call the underlying header's addChildElement for Paragraph", () => {
|
||||
const wrapper = new HeaderWrapper(new Media(), 1);
|
||||
const spy = sinon.spy(wrapper.Header, "add");
|
||||
const spy = sinon.spy(wrapper.View, "add");
|
||||
wrapper.add(new Paragraph({}));
|
||||
|
||||
expect(spy.called).to.equal(true);
|
||||
@ -18,7 +18,7 @@ describe("HeaderWrapper", () => {
|
||||
|
||||
it("should call the underlying header's addChildElement for Table", () => {
|
||||
const wrapper = new HeaderWrapper(new Media(), 1);
|
||||
const spy = sinon.spy(wrapper.Header, "add");
|
||||
const spy = sinon.spy(wrapper.View, "add");
|
||||
wrapper.add(
|
||||
new Table({
|
||||
rows: [
|
||||
@ -40,7 +40,7 @@ describe("HeaderWrapper", () => {
|
||||
describe("#addChildElement", () => {
|
||||
it("should call the underlying header's addChildElement", () => {
|
||||
const file = new HeaderWrapper(new Media(), 1);
|
||||
const spy = sinon.spy(file.Header, "addChildElement");
|
||||
const spy = sinon.spy(file.View, "addChildElement");
|
||||
// tslint:disable-next-line:no-any
|
||||
file.addChildElement({} as any);
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { HeaderReferenceType } from "./document";
|
||||
import { IViewWrapper } from "./document-wrapper";
|
||||
import { Header } from "./header/header";
|
||||
import { Media } from "./media";
|
||||
import { Paragraph } from "./paragraph";
|
||||
@ -12,7 +13,7 @@ export interface IDocumentHeader {
|
||||
readonly type: HeaderReferenceType;
|
||||
}
|
||||
|
||||
export class HeaderWrapper {
|
||||
export class HeaderWrapper implements IViewWrapper {
|
||||
private readonly header: Header;
|
||||
private readonly relationships: Relationships;
|
||||
|
||||
@ -31,7 +32,7 @@ export class HeaderWrapper {
|
||||
this.header.addChildElement(childElement);
|
||||
}
|
||||
|
||||
public get Header(): Header {
|
||||
public get View(): Header {
|
||||
return this.header;
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,7 @@ import { Paragraph } from "./paragraph";
|
||||
import { Table } from "./table";
|
||||
|
||||
export interface IHeaderOptions {
|
||||
readonly children: Array<Paragraph | Table>;
|
||||
readonly children: (Paragraph | Table)[];
|
||||
}
|
||||
|
||||
export class Header {
|
||||
|
@ -13,3 +13,4 @@ export * from "./header-wrapper";
|
||||
export * from "./footer-wrapper";
|
||||
export * from "./header";
|
||||
export * from "./footnotes";
|
||||
export * from "./track-revision";
|
||||
|
@ -1,14 +1,12 @@
|
||||
import { IDrawingOptions } from "../drawing";
|
||||
import { File } from "../file";
|
||||
import { FooterWrapper } from "../footer-wrapper";
|
||||
import { HeaderWrapper } from "../header-wrapper";
|
||||
import { PictureRun } from "../paragraph";
|
||||
import { IMediaData } from "./data";
|
||||
// import { Image } from "./image";
|
||||
|
||||
export class Media {
|
||||
public static addImage(
|
||||
file: File | HeaderWrapper | FooterWrapper,
|
||||
file: File,
|
||||
buffer: Buffer | string | Uint8Array | ArrayBuffer,
|
||||
width?: number,
|
||||
height?: number,
|
||||
@ -21,14 +19,7 @@ export class Media {
|
||||
|
||||
private static generateId(): string {
|
||||
// https://gist.github.com/6174/6062387
|
||||
return (
|
||||
Math.random()
|
||||
.toString(36)
|
||||
.substring(2, 15) +
|
||||
Math.random()
|
||||
.toString(36)
|
||||
.substring(2, 15)
|
||||
);
|
||||
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
||||
}
|
||||
|
||||
private readonly map: Map<string, IMediaData>;
|
||||
@ -89,7 +80,7 @@ export class Media {
|
||||
return imageData;
|
||||
}
|
||||
|
||||
public get Array(): IMediaData[] {
|
||||
public get Array(): readonly IMediaData[] {
|
||||
const array = new Array<IMediaData>();
|
||||
|
||||
this.map.forEach((data) => {
|
||||
|
@ -3,10 +3,11 @@ import { expect } from "chai";
|
||||
import { Formatter } from "export/formatter";
|
||||
import { EMPTY_OBJECT } from "file/xml-components";
|
||||
|
||||
import { AlignmentType, TabStopPosition } from "../paragraph";
|
||||
import { AlignmentType, EmphasisMarkType, TabStopPosition } from "../paragraph";
|
||||
import { UnderlineType } from "../paragraph/run/underline";
|
||||
import { ShadingType } from "../table";
|
||||
import { AbstractNumbering } from "./abstract-numbering";
|
||||
import { LevelFormat, LevelSuffix } from "./level";
|
||||
|
||||
describe("AbstractNumbering", () => {
|
||||
it("stores its ID at its .id property", () => {
|
||||
@ -19,7 +20,7 @@ describe("AbstractNumbering", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 3,
|
||||
format: "lowerLetter",
|
||||
format: LevelFormat.LOWER_LETTER,
|
||||
text: "%1)",
|
||||
alignment: AlignmentType.END,
|
||||
},
|
||||
@ -28,7 +29,7 @@ describe("AbstractNumbering", () => {
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ _attr: { "w:ilvl": 3, "w15:tentative": 1 } });
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ "w:start": { _attr: { "w:val": 1 } } });
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ "w:lvlJc": { _attr: { "w:val": "end" } } });
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ "w:numFmt": { _attr: { "w:val": "lowerLetter" } } });
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ "w:numFmt": { _attr: { "w:val": LevelFormat.LOWER_LETTER } } });
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ "w:lvlText": { _attr: { "w:val": "%1)" } } });
|
||||
});
|
||||
|
||||
@ -36,7 +37,7 @@ describe("AbstractNumbering", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 3,
|
||||
format: "lowerLetter",
|
||||
format: LevelFormat.LOWER_LETTER,
|
||||
text: "%1)",
|
||||
},
|
||||
]);
|
||||
@ -44,16 +45,30 @@ describe("AbstractNumbering", () => {
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ _attr: { "w:ilvl": 3, "w15:tentative": 1 } });
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ "w:start": { _attr: { "w:val": 1 } } });
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ "w:lvlJc": { _attr: { "w:val": "start" } } });
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ "w:numFmt": { _attr: { "w:val": "lowerLetter" } } });
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ "w:numFmt": { _attr: { "w:val": LevelFormat.LOWER_LETTER } } });
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ "w:lvlText": { _attr: { "w:val": "%1)" } } });
|
||||
});
|
||||
|
||||
it("has suffix", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 3,
|
||||
format: LevelFormat.LOWER_LETTER,
|
||||
text: "%1)",
|
||||
alignment: AlignmentType.END,
|
||||
suffix: LevelSuffix.SPACE,
|
||||
},
|
||||
]);
|
||||
const tree = new Formatter().format(abstractNumbering);
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ "w:suff": { _attr: { "w:val": "space" } } });
|
||||
});
|
||||
|
||||
describe("formatting methods: paragraph properties", () => {
|
||||
it("#indent", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
paragraph: {
|
||||
@ -72,7 +87,7 @@ describe("AbstractNumbering", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
paragraph: {
|
||||
@ -91,7 +106,7 @@ describe("AbstractNumbering", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
paragraph: {
|
||||
@ -110,7 +125,7 @@ describe("AbstractNumbering", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
paragraph: {
|
||||
@ -129,7 +144,7 @@ describe("AbstractNumbering", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
paragraph: {
|
||||
@ -148,7 +163,7 @@ describe("AbstractNumbering", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
paragraph: {
|
||||
@ -167,7 +182,7 @@ describe("AbstractNumbering", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
paragraph: {
|
||||
@ -201,7 +216,7 @@ describe("AbstractNumbering", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
paragraph: {
|
||||
@ -224,7 +239,7 @@ describe("AbstractNumbering", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
paragraph: {
|
||||
@ -247,7 +262,7 @@ describe("AbstractNumbering", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
paragraph: {
|
||||
@ -266,7 +281,7 @@ describe("AbstractNumbering", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
paragraph: {
|
||||
@ -283,22 +298,41 @@ describe("AbstractNumbering", () => {
|
||||
});
|
||||
|
||||
describe("formatting methods: run properties", () => {
|
||||
it("#size", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: {
|
||||
size: 24,
|
||||
const sizeTests = [
|
||||
{
|
||||
size: 24,
|
||||
expected: [{ "w:sz": { _attr: { "w:val": 24 } } }, { "w:szCs": { _attr: { "w:val": 24 } } }],
|
||||
},
|
||||
{
|
||||
size: 24,
|
||||
sizeComplexScript: true,
|
||||
expected: [{ "w:sz": { _attr: { "w:val": 24 } } }, { "w:szCs": { _attr: { "w:val": 24 } } }],
|
||||
},
|
||||
{
|
||||
size: 24,
|
||||
sizeComplexScript: false,
|
||||
expected: [{ "w:sz": { _attr: { "w:val": 24 } } }],
|
||||
},
|
||||
{
|
||||
size: 24,
|
||||
sizeComplexScript: 26,
|
||||
expected: [{ "w:sz": { _attr: { "w:val": 24 } } }, { "w:szCs": { _attr: { "w:val": 26 } } }],
|
||||
},
|
||||
];
|
||||
sizeTests.forEach(({ size, sizeComplexScript, expected }) => {
|
||||
it(`#size ${size} cs ${sizeComplexScript}`, () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: { size, sizeComplexScript },
|
||||
},
|
||||
},
|
||||
},
|
||||
]);
|
||||
const tree = new Formatter().format(abstractNumbering);
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
|
||||
"w:rPr": [{ "w:sz": { _attr: { "w:val": 24 } } }],
|
||||
]);
|
||||
const tree = new Formatter().format(abstractNumbering);
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ "w:rPr": expected });
|
||||
});
|
||||
});
|
||||
|
||||
@ -306,7 +340,7 @@ describe("AbstractNumbering", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: {
|
||||
@ -325,7 +359,7 @@ describe("AbstractNumbering", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: {
|
||||
@ -344,7 +378,7 @@ describe("AbstractNumbering", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: {
|
||||
@ -364,7 +398,7 @@ describe("AbstractNumbering", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: {
|
||||
@ -383,7 +417,7 @@ describe("AbstractNumbering", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: {
|
||||
@ -402,7 +436,7 @@ describe("AbstractNumbering", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: {
|
||||
@ -417,11 +451,11 @@ describe("AbstractNumbering", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("#font", () => {
|
||||
it("#font by name", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: {
|
||||
@ -433,80 +467,31 @@ describe("AbstractNumbering", () => {
|
||||
const tree = new Formatter().format(abstractNumbering);
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
|
||||
"w:rPr": [
|
||||
{ "w:rFonts": { _attr: { "w:ascii": "Times", "w:cs": "Times", "w:eastAsia": "Times", "w:hAnsi": "Times" } } },
|
||||
{
|
||||
"w:rFonts": {
|
||||
_attr: {
|
||||
"w:ascii": "Times",
|
||||
"w:cs": "Times",
|
||||
"w:eastAsia": "Times",
|
||||
"w:hAnsi": "Times",
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("#bold", () => {
|
||||
it("#font for ascii and eastAsia", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: {
|
||||
bold: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
]);
|
||||
const tree = new Formatter().format(abstractNumbering);
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
|
||||
"w:rPr": [{ "w:b": { _attr: { "w:val": true } } }],
|
||||
});
|
||||
});
|
||||
|
||||
it("#italics", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: {
|
||||
italics: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
]);
|
||||
const tree = new Formatter().format(abstractNumbering);
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
|
||||
"w:rPr": [{ "w:i": { _attr: { "w:val": true } } }],
|
||||
});
|
||||
});
|
||||
|
||||
it("#highlight", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: {
|
||||
highlight: "005599",
|
||||
},
|
||||
},
|
||||
},
|
||||
]);
|
||||
const tree = new Formatter().format(abstractNumbering);
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
|
||||
"w:rPr": [{ "w:highlight": { _attr: { "w:val": "005599" } } }],
|
||||
});
|
||||
});
|
||||
|
||||
it("#shadow", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: {
|
||||
shadow: {
|
||||
type: ShadingType.PERCENT_10,
|
||||
fill: "00FFFF",
|
||||
color: "FF0000",
|
||||
font: {
|
||||
ascii: "Times",
|
||||
eastAsia: "KaiTi",
|
||||
},
|
||||
},
|
||||
},
|
||||
@ -514,7 +499,198 @@ describe("AbstractNumbering", () => {
|
||||
]);
|
||||
const tree = new Formatter().format(abstractNumbering);
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
|
||||
"w:rPr": [{ "w:shd": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } }],
|
||||
"w:rPr": [
|
||||
{
|
||||
"w:rFonts": {
|
||||
_attr: {
|
||||
"w:ascii": "Times",
|
||||
"w:eastAsia": "KaiTi",
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
const boldTests = [
|
||||
{
|
||||
bold: true,
|
||||
expected: [{ "w:b": { _attr: { "w:val": true } } }, { "w:bCs": { _attr: { "w:val": true } } }],
|
||||
},
|
||||
{
|
||||
bold: true,
|
||||
boldComplexScript: true,
|
||||
expected: [{ "w:b": { _attr: { "w:val": true } } }, { "w:bCs": { _attr: { "w:val": true } } }],
|
||||
},
|
||||
{
|
||||
bold: true,
|
||||
boldComplexScript: false,
|
||||
expected: [{ "w:b": { _attr: { "w:val": true } } }],
|
||||
},
|
||||
];
|
||||
boldTests.forEach(({ bold, boldComplexScript, expected }) => {
|
||||
it(`#bold ${bold} cs ${boldComplexScript}`, () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: { bold, boldComplexScript },
|
||||
},
|
||||
},
|
||||
]);
|
||||
const tree = new Formatter().format(abstractNumbering);
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ "w:rPr": expected });
|
||||
});
|
||||
});
|
||||
|
||||
const italicsTests = [
|
||||
{
|
||||
italics: true,
|
||||
expected: [{ "w:i": { _attr: { "w:val": true } } }, { "w:iCs": { _attr: { "w:val": true } } }],
|
||||
},
|
||||
{
|
||||
italics: true,
|
||||
italicsComplexScript: true,
|
||||
expected: [{ "w:i": { _attr: { "w:val": true } } }, { "w:iCs": { _attr: { "w:val": true } } }],
|
||||
},
|
||||
{
|
||||
italics: true,
|
||||
italicsComplexScript: false,
|
||||
expected: [{ "w:i": { _attr: { "w:val": true } } }],
|
||||
},
|
||||
];
|
||||
italicsTests.forEach(({ italics, italicsComplexScript, expected }) => {
|
||||
it(`#italics ${italics} cs ${italicsComplexScript}`, () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: { italics, italicsComplexScript },
|
||||
},
|
||||
},
|
||||
]);
|
||||
const tree = new Formatter().format(abstractNumbering);
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ "w:rPr": expected });
|
||||
});
|
||||
});
|
||||
|
||||
const highlightTests = [
|
||||
{
|
||||
highlight: "005599",
|
||||
expected: [{ "w:highlight": { _attr: { "w:val": "005599" } } }, { "w:highlightCs": { _attr: { "w:val": "005599" } } }],
|
||||
},
|
||||
{
|
||||
highlight: "005599",
|
||||
highlightComplexScript: true,
|
||||
expected: [{ "w:highlight": { _attr: { "w:val": "005599" } } }, { "w:highlightCs": { _attr: { "w:val": "005599" } } }],
|
||||
},
|
||||
{
|
||||
highlight: "005599",
|
||||
highlightComplexScript: false,
|
||||
expected: [{ "w:highlight": { _attr: { "w:val": "005599" } } }],
|
||||
},
|
||||
{
|
||||
highlight: "005599",
|
||||
highlightComplexScript: "550099",
|
||||
expected: [{ "w:highlight": { _attr: { "w:val": "005599" } } }, { "w:highlightCs": { _attr: { "w:val": "550099" } } }],
|
||||
},
|
||||
];
|
||||
highlightTests.forEach(({ highlight, highlightComplexScript, expected }) => {
|
||||
it(`#highlight ${highlight} cs ${highlightComplexScript}`, () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: { highlight, highlightComplexScript },
|
||||
},
|
||||
},
|
||||
]);
|
||||
const tree = new Formatter().format(abstractNumbering);
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ "w:rPr": expected });
|
||||
});
|
||||
});
|
||||
|
||||
const shadingTests = [
|
||||
{
|
||||
shadow: {
|
||||
type: ShadingType.PERCENT_10,
|
||||
fill: "00FFFF",
|
||||
color: "FF0000",
|
||||
},
|
||||
expected: [
|
||||
{ "w:shd": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } },
|
||||
{ "w:shdCs": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } },
|
||||
],
|
||||
},
|
||||
{
|
||||
shading: {
|
||||
type: ShadingType.PERCENT_10,
|
||||
fill: "00FFFF",
|
||||
color: "FF0000",
|
||||
},
|
||||
expected: [
|
||||
{ "w:shd": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } },
|
||||
{ "w:shdCs": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } },
|
||||
],
|
||||
},
|
||||
{
|
||||
shading: {
|
||||
type: ShadingType.PERCENT_10,
|
||||
fill: "00FFFF",
|
||||
color: "FF0000",
|
||||
},
|
||||
shadingComplexScript: true,
|
||||
expected: [
|
||||
{ "w:shd": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } },
|
||||
{ "w:shdCs": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } },
|
||||
],
|
||||
},
|
||||
{
|
||||
shading: {
|
||||
type: ShadingType.PERCENT_10,
|
||||
fill: "00FFFF",
|
||||
color: "FF0000",
|
||||
},
|
||||
shadingComplexScript: false,
|
||||
expected: [{ "w:shd": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } }],
|
||||
},
|
||||
{
|
||||
shading: {
|
||||
type: ShadingType.PERCENT_10,
|
||||
fill: "00FFFF",
|
||||
color: "FF0000",
|
||||
},
|
||||
shadingComplexScript: {
|
||||
type: ShadingType.PERCENT_10,
|
||||
fill: "00FFFF",
|
||||
color: "00FF00",
|
||||
},
|
||||
expected: [
|
||||
{ "w:shd": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } },
|
||||
{ "w:shdCs": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "00FF00" } } },
|
||||
],
|
||||
},
|
||||
];
|
||||
shadingTests.forEach(({ shadow, shading, shadingComplexScript, expected }) => {
|
||||
it("#shadow correctly", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: { shadow, shading, shadingComplexScript },
|
||||
},
|
||||
},
|
||||
]);
|
||||
const tree = new Formatter().format(abstractNumbering);
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({ "w:rPr": expected });
|
||||
});
|
||||
});
|
||||
|
||||
@ -523,7 +699,7 @@ describe("AbstractNumbering", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: {
|
||||
@ -542,7 +718,7 @@ describe("AbstractNumbering", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: {
|
||||
@ -563,7 +739,7 @@ describe("AbstractNumbering", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: {
|
||||
@ -582,11 +758,53 @@ describe("AbstractNumbering", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("#emphasisMark", () => {
|
||||
it("should set emphasisMark to 'dot' if no arguments are given", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: {
|
||||
emphasisMark: {},
|
||||
},
|
||||
},
|
||||
},
|
||||
]);
|
||||
const tree = new Formatter().format(abstractNumbering);
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
|
||||
"w:rPr": [{ "w:em": { _attr: { "w:val": "dot" } } }],
|
||||
});
|
||||
});
|
||||
|
||||
it("should set the style if given", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: {
|
||||
emphasisMark: {
|
||||
type: EmphasisMarkType.DOT,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
]);
|
||||
const tree = new Formatter().format(abstractNumbering);
|
||||
expect(tree["w:abstractNum"][2]["w:lvl"]).to.include({
|
||||
"w:rPr": [{ "w:em": { _attr: { "w:val": "dot" } } }],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it("#color", () => {
|
||||
const abstractNumbering = new AbstractNumbering(1, [
|
||||
{
|
||||
level: 0,
|
||||
format: "lowerRoman",
|
||||
format: LevelFormat.LOWER_ROMAN,
|
||||
text: "%0.",
|
||||
style: {
|
||||
run: {
|
||||
|
@ -1,19 +1,25 @@
|
||||
// http://officeopenxml.com/WPnumbering-numFmt.php
|
||||
import { Attributes, XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
import {
|
||||
Alignment,
|
||||
AlignmentType,
|
||||
Indent,
|
||||
KeepLines,
|
||||
KeepNext,
|
||||
Spacing,
|
||||
TabStop,
|
||||
TabStopType,
|
||||
ThematicBreak,
|
||||
} from "../paragraph/formatting";
|
||||
import { ParagraphProperties } from "../paragraph/properties";
|
||||
import * as formatting from "../paragraph/run/formatting";
|
||||
import { RunProperties } from "../paragraph/run/properties";
|
||||
import { IParagraphStyleOptions2, IRunStyleOptions } from "../styles/style-options";
|
||||
import { AlignmentType } from "../paragraph/formatting";
|
||||
import { IParagraphStylePropertiesOptions, ParagraphProperties } from "../paragraph/properties";
|
||||
import { IRunStylePropertiesOptions, RunProperties } from "../paragraph/run/properties";
|
||||
|
||||
export enum LevelFormat {
|
||||
BULLET = "bullet",
|
||||
CARDINAL_TEXT = "cardinalText",
|
||||
CHICAGO = "chicago",
|
||||
DECIMAL = "decimal",
|
||||
DECIMAL_ENCLOSED_CIRCLE = "decimalEnclosedCircle",
|
||||
DECIMAL_ENCLOSED_FULLSTOP = "decimalEnclosedFullstop",
|
||||
DECIMAL_ENCLOSED_PARENTHESES = "decimalEnclosedParen",
|
||||
DECIMAL_ZERO = "decimalZero",
|
||||
LOWER_LETTER = "lowerLetter",
|
||||
LOWER_ROMAN = "lowerRoman",
|
||||
NONE = "none",
|
||||
ORDINAL_TEXT = "ordinalText",
|
||||
UPPER_LETTER = "upperLetter",
|
||||
UPPER_ROMAN = "upperRoman",
|
||||
}
|
||||
|
||||
interface ILevelAttributesProperties {
|
||||
readonly ilvl?: number;
|
||||
@ -79,14 +85,14 @@ export enum LevelSuffix {
|
||||
|
||||
export interface ILevelsOptions {
|
||||
readonly level: number;
|
||||
readonly format?: string;
|
||||
readonly format?: LevelFormat;
|
||||
readonly text?: string;
|
||||
readonly alignment?: AlignmentType;
|
||||
readonly start?: number;
|
||||
readonly suffix?: LevelSuffix;
|
||||
readonly style?: {
|
||||
readonly run?: IRunStyleOptions;
|
||||
readonly paragraph?: IParagraphStyleOptions2;
|
||||
readonly run?: IRunStylePropertiesOptions;
|
||||
readonly paragraph?: IParagraphStylePropertiesOptions;
|
||||
};
|
||||
}
|
||||
|
||||
@ -125,8 +131,8 @@ export class LevelBase extends XmlComponent {
|
||||
this.root.push(new LevelText(text));
|
||||
}
|
||||
|
||||
this.paragraphProperties = new ParagraphProperties({});
|
||||
this.runProperties = new RunProperties();
|
||||
this.paragraphProperties = new ParagraphProperties(style && style.paragraph);
|
||||
this.runProperties = new RunProperties(style && style.run);
|
||||
|
||||
this.root.push(this.paragraphProperties);
|
||||
this.root.push(this.runProperties);
|
||||
@ -134,100 +140,6 @@ export class LevelBase extends XmlComponent {
|
||||
if (suffix) {
|
||||
this.root.push(new Suffix(suffix));
|
||||
}
|
||||
|
||||
if (style) {
|
||||
if (style.run) {
|
||||
if (style.run.size) {
|
||||
this.runProperties.push(new formatting.Size(style.run.size));
|
||||
}
|
||||
|
||||
if (style.run.bold) {
|
||||
this.runProperties.push(new formatting.Bold());
|
||||
}
|
||||
|
||||
if (style.run.italics) {
|
||||
this.runProperties.push(new formatting.Italics());
|
||||
}
|
||||
|
||||
if (style.run.smallCaps) {
|
||||
this.runProperties.push(new formatting.SmallCaps());
|
||||
}
|
||||
|
||||
if (style.run.allCaps) {
|
||||
this.runProperties.push(new formatting.Caps());
|
||||
}
|
||||
|
||||
if (style.run.strike) {
|
||||
this.runProperties.push(new formatting.Strike());
|
||||
}
|
||||
|
||||
if (style.run.doubleStrike) {
|
||||
this.runProperties.push(new formatting.DoubleStrike());
|
||||
}
|
||||
|
||||
if (style.run.subScript) {
|
||||
this.runProperties.push(new formatting.SubScript());
|
||||
}
|
||||
|
||||
if (style.run.superScript) {
|
||||
this.runProperties.push(new formatting.SuperScript());
|
||||
}
|
||||
|
||||
if (style.run.underline) {
|
||||
this.runProperties.push(new formatting.Underline(style.run.underline.type, style.run.underline.color));
|
||||
}
|
||||
|
||||
if (style.run.color) {
|
||||
this.runProperties.push(new formatting.Color(style.run.color));
|
||||
}
|
||||
|
||||
if (style.run.font) {
|
||||
this.runProperties.push(new formatting.RunFonts(style.run.font));
|
||||
}
|
||||
|
||||
if (style.run.highlight) {
|
||||
this.runProperties.push(new formatting.Highlight(style.run.highlight));
|
||||
}
|
||||
|
||||
if (style.run.shadow) {
|
||||
this.runProperties.push(new formatting.Shading(style.run.shadow.type, style.run.shadow.fill, style.run.shadow.color));
|
||||
}
|
||||
}
|
||||
|
||||
if (style.paragraph) {
|
||||
if (style.paragraph.alignment) {
|
||||
this.paragraphProperties.push(new Alignment(style.paragraph.alignment));
|
||||
}
|
||||
|
||||
if (style.paragraph.thematicBreak) {
|
||||
this.paragraphProperties.push(new ThematicBreak());
|
||||
}
|
||||
|
||||
if (style.paragraph.rightTabStop) {
|
||||
this.paragraphProperties.push(new TabStop(TabStopType.RIGHT, style.paragraph.rightTabStop));
|
||||
}
|
||||
|
||||
if (style.paragraph.leftTabStop) {
|
||||
this.paragraphProperties.push(new TabStop(TabStopType.LEFT, style.paragraph.leftTabStop));
|
||||
}
|
||||
|
||||
if (style.paragraph.indent) {
|
||||
this.paragraphProperties.push(new Indent(style.paragraph.indent));
|
||||
}
|
||||
|
||||
if (style.paragraph.spacing) {
|
||||
this.paragraphProperties.push(new Spacing(style.paragraph.spacing));
|
||||
}
|
||||
|
||||
if (style.paragraph.keepNext) {
|
||||
this.paragraphProperties.push(new KeepNext());
|
||||
}
|
||||
|
||||
if (style.paragraph.keepLines) {
|
||||
this.paragraphProperties.push(new KeepLines());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,17 +1,18 @@
|
||||
// http://officeopenxml.com/WPnumbering.php
|
||||
import { convertInchesToTwip } from "convenience-functions";
|
||||
import { AlignmentType } from "file/paragraph";
|
||||
import { IXmlableObject, XmlComponent } from "file/xml-components";
|
||||
|
||||
import { DocumentAttributes } from "../document/document-attributes";
|
||||
import { AbstractNumbering } from "./abstract-numbering";
|
||||
import { ILevelsOptions } from "./level";
|
||||
import { ILevelsOptions, LevelFormat } from "./level";
|
||||
import { ConcreteNumbering } from "./num";
|
||||
|
||||
export interface INumberingOptions {
|
||||
readonly config: Array<{
|
||||
readonly config: {
|
||||
readonly levels: ILevelsOptions[];
|
||||
readonly reference: string;
|
||||
}>;
|
||||
}[];
|
||||
}
|
||||
|
||||
export class Numbering extends XmlComponent {
|
||||
@ -50,100 +51,100 @@ export class Numbering extends XmlComponent {
|
||||
const abstractNumbering = this.createAbstractNumbering([
|
||||
{
|
||||
level: 0,
|
||||
format: "bullet",
|
||||
format: LevelFormat.BULLET,
|
||||
text: "\u25CF",
|
||||
alignment: AlignmentType.LEFT,
|
||||
style: {
|
||||
paragraph: {
|
||||
indent: { left: 720, hanging: 360 },
|
||||
indent: { left: convertInchesToTwip(0.5), hanging: convertInchesToTwip(0.25) },
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
level: 1,
|
||||
format: "bullet",
|
||||
format: LevelFormat.BULLET,
|
||||
text: "\u25CB",
|
||||
alignment: AlignmentType.LEFT,
|
||||
style: {
|
||||
paragraph: {
|
||||
indent: { left: 1440, hanging: 360 },
|
||||
indent: { left: convertInchesToTwip(1), hanging: convertInchesToTwip(0.25) },
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
level: 2,
|
||||
format: "bullet",
|
||||
format: LevelFormat.BULLET,
|
||||
text: "\u25A0",
|
||||
alignment: AlignmentType.LEFT,
|
||||
style: {
|
||||
paragraph: {
|
||||
indent: { left: 2160, hanging: 360 },
|
||||
indent: { left: 2160, hanging: convertInchesToTwip(0.25) },
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
level: 3,
|
||||
format: "bullet",
|
||||
format: LevelFormat.BULLET,
|
||||
text: "\u25CF",
|
||||
alignment: AlignmentType.LEFT,
|
||||
style: {
|
||||
paragraph: {
|
||||
indent: { left: 2880, hanging: 360 },
|
||||
indent: { left: 2880, hanging: convertInchesToTwip(0.25) },
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
level: 4,
|
||||
format: "bullet",
|
||||
format: LevelFormat.BULLET,
|
||||
text: "\u25CB",
|
||||
alignment: AlignmentType.LEFT,
|
||||
style: {
|
||||
paragraph: {
|
||||
indent: { left: 3600, hanging: 360 },
|
||||
indent: { left: 3600, hanging: convertInchesToTwip(0.25) },
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
level: 5,
|
||||
format: "bullet",
|
||||
format: LevelFormat.BULLET,
|
||||
text: "\u25A0",
|
||||
alignment: AlignmentType.LEFT,
|
||||
style: {
|
||||
paragraph: {
|
||||
indent: { left: 4320, hanging: 360 },
|
||||
indent: { left: 4320, hanging: convertInchesToTwip(0.25) },
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
level: 6,
|
||||
format: "bullet",
|
||||
format: LevelFormat.BULLET,
|
||||
text: "\u25CF",
|
||||
alignment: AlignmentType.LEFT,
|
||||
style: {
|
||||
paragraph: {
|
||||
indent: { left: 5040, hanging: 360 },
|
||||
indent: { left: 5040, hanging: convertInchesToTwip(0.25) },
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
level: 7,
|
||||
format: "bullet",
|
||||
format: LevelFormat.BULLET,
|
||||
text: "\u25CF",
|
||||
alignment: AlignmentType.LEFT,
|
||||
style: {
|
||||
paragraph: {
|
||||
indent: { left: 5760, hanging: 360 },
|
||||
indent: { left: 5760, hanging: convertInchesToTwip(0.25) },
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
level: 8,
|
||||
format: "bullet",
|
||||
format: LevelFormat.BULLET,
|
||||
text: "\u25CF",
|
||||
alignment: AlignmentType.LEFT,
|
||||
style: {
|
||||
paragraph: {
|
||||
indent: { left: 6480, hanging: 360 },
|
||||
indent: { left: 6480, hanging: convertInchesToTwip(0.25) },
|
||||
},
|
||||
},
|
||||
},
|
||||
|
15
src/file/paragraph/formatting/bidirectional.spec.ts
Normal file
15
src/file/paragraph/formatting/bidirectional.spec.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { Bidirectional } from "./bidirectional";
|
||||
|
||||
describe("Bidirectional", () => {
|
||||
it("should create", () => {
|
||||
const bidirectional = new Bidirectional();
|
||||
const tree = new Formatter().format(bidirectional);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:bidi": {},
|
||||
});
|
||||
});
|
||||
});
|
@ -3,3 +3,4 @@ export * from "./paragraph";
|
||||
export * from "./properties";
|
||||
export * from "./run";
|
||||
export * from "./links";
|
||||
export * from "./math";
|
||||
|
@ -19,7 +19,7 @@ describe("Bookmark", () => {
|
||||
const newJson = Utility.jsonify(bookmark);
|
||||
assert.equal(newJson.rootKey, undefined);
|
||||
assert.equal(newJson.start.rootKey, "w:bookmarkStart");
|
||||
assert.equal(newJson.text.rootKey, "w:r");
|
||||
assert.equal(newJson.children[0].rootKey, "w:r");
|
||||
assert.equal(newJson.end.rootKey, "w:bookmarkEnd");
|
||||
});
|
||||
|
||||
@ -31,7 +31,7 @@ describe("Bookmark", () => {
|
||||
|
||||
it("should create a bookmark with the correct attributes on the text element", () => {
|
||||
const newJson = Utility.jsonify(bookmark);
|
||||
assert.equal(JSON.stringify(newJson.text.root[1].root[1]), JSON.stringify("Internal Link"));
|
||||
assert.equal(JSON.stringify(newJson.children[0].root[1].root[1]), JSON.stringify("Internal Link"));
|
||||
});
|
||||
|
||||
it("should create a bookmark with the correct attributes on the bookmark end element", () => {
|
||||
|
@ -2,14 +2,20 @@ import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { Hyperlink } from "./";
|
||||
import { HyperlinkRef } from "./hyperlink";
|
||||
import { TextRun } from "../run";
|
||||
import { ConcreteHyperlink, ExternalHyperlink, InternalHyperlink } from "./hyperlink";
|
||||
|
||||
describe("Hyperlink", () => {
|
||||
let hyperlink: Hyperlink;
|
||||
describe("ConcreteHyperlink", () => {
|
||||
let hyperlink: ConcreteHyperlink;
|
||||
|
||||
beforeEach(() => {
|
||||
hyperlink = new Hyperlink("https://example.com", "superid");
|
||||
hyperlink = new ConcreteHyperlink(
|
||||
new TextRun({
|
||||
text: "https://example.com",
|
||||
style: "Hyperlink",
|
||||
}),
|
||||
"superid",
|
||||
);
|
||||
});
|
||||
|
||||
describe("#constructor()", () => {
|
||||
@ -35,7 +41,14 @@ describe("Hyperlink", () => {
|
||||
|
||||
describe("with optional anchor parameter", () => {
|
||||
beforeEach(() => {
|
||||
hyperlink = new Hyperlink("Anchor Text", "superid2", "anchor");
|
||||
hyperlink = new ConcreteHyperlink(
|
||||
new TextRun({
|
||||
text: "Anchor Text",
|
||||
style: "Hyperlink",
|
||||
}),
|
||||
"superid2",
|
||||
"anchor",
|
||||
);
|
||||
});
|
||||
|
||||
it("should create an internal link with anchor tag", () => {
|
||||
@ -61,10 +74,53 @@ describe("Hyperlink", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("HyperlinkRef", () => {
|
||||
describe("ExternalHyperlink", () => {
|
||||
describe("#constructor()", () => {
|
||||
const hyperlinkRef = new HyperlinkRef("test-id");
|
||||
it("should create", () => {
|
||||
const externalHyperlink = new ExternalHyperlink({
|
||||
child: new TextRun("test"),
|
||||
link: "http://www.google.com",
|
||||
});
|
||||
|
||||
expect(hyperlinkRef.id).to.equal("test-id");
|
||||
expect(externalHyperlink.options.link).to.equal("http://www.google.com");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("InternalHyperlink", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create", () => {
|
||||
const internalHyperlink = new InternalHyperlink({
|
||||
child: new TextRun("test"),
|
||||
anchor: "test-id",
|
||||
});
|
||||
|
||||
const tree = new Formatter().format(internalHyperlink);
|
||||
|
||||
expect(tree).to.deep.equal({
|
||||
"w:hyperlink": [
|
||||
{
|
||||
_attr: {
|
||||
"w:anchor": "test-id",
|
||||
"w:history": 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
"w:r": [
|
||||
{
|
||||
"w:t": [
|
||||
{
|
||||
_attr: {
|
||||
"xml:space": "preserve",
|
||||
},
|
||||
},
|
||||
"test",
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -1,6 +1,9 @@
|
||||
// http://officeopenxml.com/WPhyperlink.php
|
||||
import * as shortid from "shortid";
|
||||
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { TextRun } from "../run";
|
||||
|
||||
import { ParagraphChild } from "../paragraph";
|
||||
import { HyperlinkAttributes, IHyperlinkAttributesProperties } from "./hyperlink-attributes";
|
||||
|
||||
export enum HyperlinkType {
|
||||
@ -8,15 +11,10 @@ export enum HyperlinkType {
|
||||
EXTERNAL = "EXTERNAL",
|
||||
}
|
||||
|
||||
export class HyperlinkRef {
|
||||
constructor(public readonly id: string) {}
|
||||
}
|
||||
|
||||
export class Hyperlink extends XmlComponent {
|
||||
export class ConcreteHyperlink extends XmlComponent {
|
||||
public readonly linkId: string;
|
||||
private readonly textRun: TextRun;
|
||||
|
||||
constructor(text: string, relationshipId: string, anchor?: string) {
|
||||
constructor(child: ParagraphChild, relationshipId: string, anchor?: string) {
|
||||
super("w:hyperlink");
|
||||
|
||||
this.linkId = relationshipId;
|
||||
@ -29,14 +27,16 @@ export class Hyperlink extends XmlComponent {
|
||||
|
||||
const attributes = new HyperlinkAttributes(props);
|
||||
this.root.push(attributes);
|
||||
this.textRun = new TextRun({
|
||||
text: text,
|
||||
style: "Hyperlink",
|
||||
});
|
||||
this.root.push(this.textRun);
|
||||
}
|
||||
|
||||
public get TextRun(): TextRun {
|
||||
return this.textRun;
|
||||
this.root.push(child);
|
||||
}
|
||||
}
|
||||
|
||||
export class InternalHyperlink extends ConcreteHyperlink {
|
||||
constructor(options: { readonly child: ParagraphChild; readonly anchor: string }) {
|
||||
super(options.child, shortid.generate().toLowerCase(), options.anchor);
|
||||
}
|
||||
}
|
||||
|
||||
export class ExternalHyperlink {
|
||||
constructor(public readonly options: { readonly child: ParagraphChild; readonly link: string }) {}
|
||||
}
|
||||
|
4
src/file/paragraph/math/brackets/index.ts
Normal file
4
src/file/paragraph/math/brackets/index.ts
Normal file
@ -0,0 +1,4 @@
|
||||
export * from "./math-round-brackets";
|
||||
export * from "./math-square-brackets";
|
||||
export * from "./math-curly-brackets";
|
||||
export * from "./math-angled-brackets";
|
@ -0,0 +1,51 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { MathRun } from "../math-run";
|
||||
import { MathAngledBrackets } from "./math-angled-brackets";
|
||||
|
||||
describe("MathAngledBrackets", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create a MathAngledBrackets with correct root key", () => {
|
||||
const mathAngledBrackets = new MathAngledBrackets({
|
||||
children: [new MathRun("60")],
|
||||
});
|
||||
|
||||
const tree = new Formatter().format(mathAngledBrackets);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:d": [
|
||||
{
|
||||
"m:dPr": [
|
||||
{
|
||||
"m:begChr": {
|
||||
_attr: {
|
||||
"m:val": "〈",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"m:endChr": {
|
||||
_attr: {
|
||||
"m:val": "〉",
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
"m:e": [
|
||||
{
|
||||
"m:r": [
|
||||
{
|
||||
"m:t": ["60"],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
20
src/file/paragraph/math/brackets/math-angled-brackets.ts
Normal file
20
src/file/paragraph/math/brackets/math-angled-brackets.ts
Normal file
@ -0,0 +1,20 @@
|
||||
// http://www.datypic.com/sc/ooxml/e-m_d-1.html
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { MathComponent } from "../math-component";
|
||||
import { MathBase } from "../n-ary";
|
||||
import { MathBracketProperties } from "./math-bracket-properties";
|
||||
|
||||
export class MathAngledBrackets extends XmlComponent {
|
||||
constructor(options: { readonly children: MathComponent[] }) {
|
||||
super("m:d");
|
||||
|
||||
this.root.push(
|
||||
new MathBracketProperties({
|
||||
beginningCharacter: "〈",
|
||||
endingCharacter: "〉",
|
||||
}),
|
||||
);
|
||||
this.root.push(new MathBase(options.children));
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { MathBeginningCharacter } from "./math-beginning-character";
|
||||
|
||||
describe("MathBeginningCharacter", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create a MathBeginningCharacter with correct root key", () => {
|
||||
const mathBeginningCharacter = new MathBeginningCharacter("[");
|
||||
|
||||
const tree = new Formatter().format(mathBeginningCharacter);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:begChr": {
|
||||
_attr: {
|
||||
"m:val": "[",
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
14
src/file/paragraph/math/brackets/math-beginning-character.ts
Normal file
14
src/file/paragraph/math/brackets/math-beginning-character.ts
Normal file
@ -0,0 +1,14 @@
|
||||
// http://www.datypic.com/sc/ooxml/e-m_begChr-1.html
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
|
||||
class MathBeginningCharacterAttributes extends XmlAttributeComponent<{ readonly character: string }> {
|
||||
protected readonly xmlKeys = { character: "m:val" };
|
||||
}
|
||||
|
||||
export class MathBeginningCharacter extends XmlComponent {
|
||||
constructor(character: string) {
|
||||
super("m:begChr");
|
||||
|
||||
this.root.push(new MathBeginningCharacterAttributes({ character }));
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { MathBracketProperties } from "./math-bracket-properties";
|
||||
|
||||
describe("MathBracketProperties", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create a MathBracketProperties with correct root key", () => {
|
||||
const mathBracketProperties = new MathBracketProperties();
|
||||
|
||||
const tree = new Formatter().format(mathBracketProperties);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:dPr": {},
|
||||
});
|
||||
});
|
||||
|
||||
it("should create a MathBracketProperties with correct root key and add brackets", () => {
|
||||
const mathBracketProperties = new MathBracketProperties({
|
||||
beginningCharacter: "[",
|
||||
endingCharacter: "]",
|
||||
});
|
||||
|
||||
const tree = new Formatter().format(mathBracketProperties);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:dPr": [
|
||||
{
|
||||
"m:begChr": {
|
||||
_attr: {
|
||||
"m:val": "[",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"m:endChr": {
|
||||
_attr: {
|
||||
"m:val": "]",
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
16
src/file/paragraph/math/brackets/math-bracket-properties.ts
Normal file
16
src/file/paragraph/math/brackets/math-bracket-properties.ts
Normal file
@ -0,0 +1,16 @@
|
||||
// http://www.datypic.com/sc/ooxml/e-m_dPr-1.html
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { MathBeginningCharacter } from "./math-beginning-character";
|
||||
import { MathEndingCharacter } from "./math-ending-char";
|
||||
|
||||
export class MathBracketProperties extends XmlComponent {
|
||||
constructor(options?: { readonly beginningCharacter: string; readonly endingCharacter: string }) {
|
||||
super("m:dPr");
|
||||
|
||||
if (!!options) {
|
||||
this.root.push(new MathBeginningCharacter(options.beginningCharacter));
|
||||
this.root.push(new MathEndingCharacter(options.endingCharacter));
|
||||
}
|
||||
}
|
||||
}
|
51
src/file/paragraph/math/brackets/math-curly-brackets.spec.ts
Normal file
51
src/file/paragraph/math/brackets/math-curly-brackets.spec.ts
Normal file
@ -0,0 +1,51 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { MathRun } from "../math-run";
|
||||
import { MathCurlyBrackets } from "./math-curly-brackets";
|
||||
|
||||
describe("MathCurlyBrackets", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create a MathCurlyBrackets with correct root key", () => {
|
||||
const mathCurlyBrackets = new MathCurlyBrackets({
|
||||
children: [new MathRun("60")],
|
||||
});
|
||||
|
||||
const tree = new Formatter().format(mathCurlyBrackets);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:d": [
|
||||
{
|
||||
"m:dPr": [
|
||||
{
|
||||
"m:begChr": {
|
||||
_attr: {
|
||||
"m:val": "{",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"m:endChr": {
|
||||
_attr: {
|
||||
"m:val": "}",
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
"m:e": [
|
||||
{
|
||||
"m:r": [
|
||||
{
|
||||
"m:t": ["60"],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
20
src/file/paragraph/math/brackets/math-curly-brackets.ts
Normal file
20
src/file/paragraph/math/brackets/math-curly-brackets.ts
Normal file
@ -0,0 +1,20 @@
|
||||
// http://www.datypic.com/sc/ooxml/e-m_d-1.html
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { MathComponent } from "../math-component";
|
||||
import { MathBase } from "../n-ary";
|
||||
import { MathBracketProperties } from "./math-bracket-properties";
|
||||
|
||||
export class MathCurlyBrackets extends XmlComponent {
|
||||
constructor(options: { readonly children: MathComponent[] }) {
|
||||
super("m:d");
|
||||
|
||||
this.root.push(
|
||||
new MathBracketProperties({
|
||||
beginningCharacter: "{",
|
||||
endingCharacter: "}",
|
||||
}),
|
||||
);
|
||||
this.root.push(new MathBase(options.children));
|
||||
}
|
||||
}
|
14
src/file/paragraph/math/brackets/math-ending-char.ts
Normal file
14
src/file/paragraph/math/brackets/math-ending-char.ts
Normal file
@ -0,0 +1,14 @@
|
||||
// http://www.datypic.com/sc/ooxml/e-m_endChr-1.html
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
|
||||
class MathEndingCharacterAttributes extends XmlAttributeComponent<{ readonly character: string }> {
|
||||
protected readonly xmlKeys = { character: "m:val" };
|
||||
}
|
||||
|
||||
export class MathEndingCharacter extends XmlComponent {
|
||||
constructor(character: string) {
|
||||
super("m:endChr");
|
||||
|
||||
this.root.push(new MathEndingCharacterAttributes({ character }));
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { MathEndingCharacter } from "./math-ending-char";
|
||||
|
||||
describe("MathEndingCharacter", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create a MathEndingCharacter with correct root key", () => {
|
||||
const mathEndingCharacter = new MathEndingCharacter("]");
|
||||
|
||||
const tree = new Formatter().format(mathEndingCharacter);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:endChr": {
|
||||
_attr: {
|
||||
"m:val": "]",
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
36
src/file/paragraph/math/brackets/math-round-brackets.spec.ts
Normal file
36
src/file/paragraph/math/brackets/math-round-brackets.spec.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { MathRun } from "../math-run";
|
||||
import { MathRoundBrackets } from "./math-round-brackets";
|
||||
|
||||
describe("MathRoundBrackets", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create a MathRoundBrackets with correct root key", () => {
|
||||
const mathRoundBrackets = new MathRoundBrackets({
|
||||
children: [new MathRun("60")],
|
||||
});
|
||||
|
||||
const tree = new Formatter().format(mathRoundBrackets);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:d": [
|
||||
{
|
||||
"m:dPr": {},
|
||||
},
|
||||
{
|
||||
"m:e": [
|
||||
{
|
||||
"m:r": [
|
||||
{
|
||||
"m:t": ["60"],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
15
src/file/paragraph/math/brackets/math-round-brackets.ts
Normal file
15
src/file/paragraph/math/brackets/math-round-brackets.ts
Normal file
@ -0,0 +1,15 @@
|
||||
// http://www.datypic.com/sc/ooxml/e-m_d-1.html
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { MathComponent } from "../math-component";
|
||||
import { MathBase } from "../n-ary";
|
||||
import { MathBracketProperties } from "./math-bracket-properties";
|
||||
|
||||
export class MathRoundBrackets extends XmlComponent {
|
||||
constructor(options: { readonly children: MathComponent[] }) {
|
||||
super("m:d");
|
||||
|
||||
this.root.push(new MathBracketProperties());
|
||||
this.root.push(new MathBase(options.children));
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { MathRun } from "../math-run";
|
||||
import { MathSquareBrackets } from "./math-square-brackets";
|
||||
|
||||
describe("MathSquareBrackets", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create a MathSquareBrackets with correct root key", () => {
|
||||
const mathSquareBrackets = new MathSquareBrackets({
|
||||
children: [new MathRun("60")],
|
||||
});
|
||||
|
||||
const tree = new Formatter().format(mathSquareBrackets);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:d": [
|
||||
{
|
||||
"m:dPr": [
|
||||
{
|
||||
"m:begChr": {
|
||||
_attr: {
|
||||
"m:val": "[",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"m:endChr": {
|
||||
_attr: {
|
||||
"m:val": "]",
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
"m:e": [
|
||||
{
|
||||
"m:r": [
|
||||
{
|
||||
"m:t": ["60"],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
20
src/file/paragraph/math/brackets/math-square-brackets.ts
Normal file
20
src/file/paragraph/math/brackets/math-square-brackets.ts
Normal file
@ -0,0 +1,20 @@
|
||||
// http://www.datypic.com/sc/ooxml/e-m_d-1.html
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { MathComponent } from "../math-component";
|
||||
import { MathBase } from "../n-ary";
|
||||
import { MathBracketProperties } from "./math-bracket-properties";
|
||||
|
||||
export class MathSquareBrackets extends XmlComponent {
|
||||
constructor(options: { readonly children: MathComponent[] }) {
|
||||
super("m:d");
|
||||
|
||||
this.root.push(
|
||||
new MathBracketProperties({
|
||||
beginningCharacter: "[",
|
||||
endingCharacter: "]",
|
||||
}),
|
||||
);
|
||||
this.root.push(new MathBase(options.children));
|
||||
}
|
||||
}
|
3
src/file/paragraph/math/fraction/index.ts
Normal file
3
src/file/paragraph/math/fraction/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export * from "./math-fraction";
|
||||
export * from "./math-denominator";
|
||||
export * from "./math-numerator";
|
26
src/file/paragraph/math/fraction/math-denominator.spec.ts
Normal file
26
src/file/paragraph/math/fraction/math-denominator.spec.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { MathRun } from "../math-run";
|
||||
import { MathDenominator } from "./math-denominator";
|
||||
|
||||
describe("MathDenominator", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create a MathDenominator with correct root key", () => {
|
||||
const mathDenominator = new MathDenominator([new MathRun("2+2")]);
|
||||
const tree = new Formatter().format(mathDenominator);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:den": [
|
||||
{
|
||||
"m:r": [
|
||||
{
|
||||
"m:t": ["2+2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
13
src/file/paragraph/math/fraction/math-denominator.ts
Normal file
13
src/file/paragraph/math/fraction/math-denominator.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { MathComponent } from "../math-component";
|
||||
|
||||
export class MathDenominator extends XmlComponent {
|
||||
constructor(children: MathComponent[]) {
|
||||
super("m:den");
|
||||
|
||||
for (const child of children) {
|
||||
this.root.push(child);
|
||||
}
|
||||
}
|
||||
}
|
44
src/file/paragraph/math/fraction/math-fraction.spec.ts
Normal file
44
src/file/paragraph/math/fraction/math-fraction.spec.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { MathRun } from "../math-run";
|
||||
import { MathFraction } from "./math-fraction";
|
||||
|
||||
describe("MathFraction", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create a MathFraction with correct root key", () => {
|
||||
const mathFraction = new MathFraction({
|
||||
numerator: [new MathRun("2")],
|
||||
denominator: [new MathRun("2")],
|
||||
});
|
||||
const tree = new Formatter().format(mathFraction);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:f": [
|
||||
{
|
||||
"m:num": [
|
||||
{
|
||||
"m:r": [
|
||||
{
|
||||
"m:t": ["2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
"m:den": [
|
||||
{
|
||||
"m:r": [
|
||||
{
|
||||
"m:t": ["2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
19
src/file/paragraph/math/fraction/math-fraction.ts
Normal file
19
src/file/paragraph/math/fraction/math-fraction.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { MathComponent } from "../math-component";
|
||||
import { MathDenominator } from "./math-denominator";
|
||||
import { MathNumerator } from "./math-numerator";
|
||||
|
||||
export interface IMathFractionOptions {
|
||||
readonly numerator: MathComponent[];
|
||||
readonly denominator: MathComponent[];
|
||||
}
|
||||
|
||||
export class MathFraction extends XmlComponent {
|
||||
constructor(options: IMathFractionOptions) {
|
||||
super("m:f");
|
||||
|
||||
this.root.push(new MathNumerator(options.numerator));
|
||||
this.root.push(new MathDenominator(options.denominator));
|
||||
}
|
||||
}
|
26
src/file/paragraph/math/fraction/math-numerator.spec.ts
Normal file
26
src/file/paragraph/math/fraction/math-numerator.spec.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { MathRun } from "../math-run";
|
||||
import { MathNumerator } from "./math-numerator";
|
||||
|
||||
describe("MathNumerator", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create a MathNumerator with correct root key", () => {
|
||||
const mathNumerator = new MathNumerator([new MathRun("2+2")]);
|
||||
const tree = new Formatter().format(mathNumerator);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:num": [
|
||||
{
|
||||
"m:r": [
|
||||
{
|
||||
"m:t": ["2+2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
13
src/file/paragraph/math/fraction/math-numerator.ts
Normal file
13
src/file/paragraph/math/fraction/math-numerator.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { MathComponent } from "../math-component";
|
||||
|
||||
export class MathNumerator extends XmlComponent {
|
||||
constructor(children: MathComponent[]) {
|
||||
super("m:num");
|
||||
|
||||
for (const child of children) {
|
||||
this.root.push(child);
|
||||
}
|
||||
}
|
||||
}
|
3
src/file/paragraph/math/function/index.ts
Normal file
3
src/file/paragraph/math/function/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export * from "./math-function";
|
||||
export * from "./math-function-name";
|
||||
export * from "./math-function-properties";
|
27
src/file/paragraph/math/function/math-function-name.spec.ts
Normal file
27
src/file/paragraph/math/function/math-function-name.spec.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { MathRun } from "../math-run";
|
||||
import { MathFunctionName } from "./math-function-name";
|
||||
|
||||
describe("MathFunctionName", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create a MathFunctionName with correct root key", () => {
|
||||
const mathFunctionName = new MathFunctionName([new MathRun("2")]);
|
||||
|
||||
const tree = new Formatter().format(mathFunctionName);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:fName": [
|
||||
{
|
||||
"m:r": [
|
||||
{
|
||||
"m:t": ["2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
13
src/file/paragraph/math/function/math-function-name.ts
Normal file
13
src/file/paragraph/math/function/math-function-name.ts
Normal file
@ -0,0 +1,13 @@
|
||||
// http://www.datypic.com/sc/ooxml/e-m_fName-1.html
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { MathComponent } from "../math-component";
|
||||
|
||||
export class MathFunctionName extends XmlComponent {
|
||||
constructor(children: MathComponent[]) {
|
||||
super("m:fName");
|
||||
|
||||
for (const child of children) {
|
||||
this.root.push(child);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { MathFunctionProperties } from "./math-function-properties";
|
||||
|
||||
describe("MathFunctionProperties", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create a MathFunctionProperties with correct root key", () => {
|
||||
const mathFunctionProperties = new MathFunctionProperties();
|
||||
|
||||
const tree = new Formatter().format(mathFunctionProperties);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:funcPr": {},
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
@ -0,0 +1,8 @@
|
||||
// http://www.datypic.com/sc/ooxml/e-m_radPr-1.html
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
export class MathFunctionProperties extends XmlComponent {
|
||||
constructor() {
|
||||
super("m:funcPr");
|
||||
}
|
||||
}
|
48
src/file/paragraph/math/function/math-function.spec.ts
Normal file
48
src/file/paragraph/math/function/math-function.spec.ts
Normal file
@ -0,0 +1,48 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { MathRun } from "../math-run";
|
||||
import { MathFunction } from "./math-function";
|
||||
|
||||
describe("MathFunction", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create a MathFunction with correct root key", () => {
|
||||
const mathFunction = new MathFunction({
|
||||
name: [new MathRun("sin")],
|
||||
children: [new MathRun("60")],
|
||||
});
|
||||
|
||||
const tree = new Formatter().format(mathFunction);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:func": [
|
||||
{
|
||||
"m:funcPr": {},
|
||||
},
|
||||
{
|
||||
"m:fName": [
|
||||
{
|
||||
"m:r": [
|
||||
{
|
||||
"m:t": ["sin"],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
"m:e": [
|
||||
{
|
||||
"m:r": [
|
||||
{
|
||||
"m:t": ["60"],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
22
src/file/paragraph/math/function/math-function.ts
Normal file
22
src/file/paragraph/math/function/math-function.ts
Normal file
@ -0,0 +1,22 @@
|
||||
// http://www.datypic.com/sc/ooxml/e-m_func-1.html
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { MathComponent } from "../math-component";
|
||||
import { MathBase } from "../n-ary";
|
||||
import { MathFunctionName } from "./math-function-name";
|
||||
import { MathFunctionProperties } from "./math-function-properties";
|
||||
|
||||
export interface IMathFunctionOptions {
|
||||
readonly children: MathComponent[];
|
||||
readonly name: MathComponent[];
|
||||
}
|
||||
|
||||
export class MathFunction extends XmlComponent {
|
||||
constructor(options: IMathFunctionOptions) {
|
||||
super("m:func");
|
||||
|
||||
this.root.push(new MathFunctionProperties());
|
||||
this.root.push(new MathFunctionName(options.name));
|
||||
this.root.push(new MathBase(options.children));
|
||||
}
|
||||
}
|
9
src/file/paragraph/math/index.ts
Normal file
9
src/file/paragraph/math/index.ts
Normal file
@ -0,0 +1,9 @@
|
||||
export * from "./math";
|
||||
export * from "./math-run";
|
||||
export * from "./fraction";
|
||||
export * from "./n-ary";
|
||||
export * from "./script";
|
||||
export * from "./math-component";
|
||||
export * from "./radical";
|
||||
export * from "./function";
|
||||
export * from "./brackets";
|
27
src/file/paragraph/math/math-component.ts
Normal file
27
src/file/paragraph/math/math-component.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import { MathAngledBrackets, MathCurlyBrackets, MathRoundBrackets, MathSquareBrackets } from "./brackets";
|
||||
import { MathFraction } from "./fraction";
|
||||
import { MathFunction } from "./function";
|
||||
import { MathRun } from "./math-run";
|
||||
import { MathSum } from "./n-ary";
|
||||
import { MathRadical } from "./radical";
|
||||
import { MathSubScript, MathSubSuperScript, MathSuperScript } from "./script";
|
||||
|
||||
export type MathComponent =
|
||||
| MathRun
|
||||
| MathFraction
|
||||
| MathSum
|
||||
| MathSuperScript
|
||||
| MathSubScript
|
||||
| MathSubSuperScript
|
||||
| MathRadical
|
||||
| MathFunction
|
||||
| MathRoundBrackets
|
||||
| MathCurlyBrackets
|
||||
| MathAngledBrackets
|
||||
| MathSquareBrackets;
|
||||
|
||||
// Needed because of: https://github.com/s-panferov/awesome-typescript-loader/issues/432
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
export const WORKAROUND4 = "";
|
21
src/file/paragraph/math/math-run.spec.ts
Normal file
21
src/file/paragraph/math/math-run.spec.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { MathRun } from "./math-run";
|
||||
|
||||
describe("MathRun", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create a MathRun with correct root key", () => {
|
||||
const mathRun = new MathRun("2+2");
|
||||
const tree = new Formatter().format(mathRun);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:r": [
|
||||
{
|
||||
"m:t": ["2+2"],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
12
src/file/paragraph/math/math-run.ts
Normal file
12
src/file/paragraph/math/math-run.ts
Normal file
@ -0,0 +1,12 @@
|
||||
// http://www.datypic.com/sc/ooxml/e-m_r-1.html
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { MathText } from "./math-text";
|
||||
|
||||
export class MathRun extends XmlComponent {
|
||||
constructor(text: string) {
|
||||
super("m:r");
|
||||
|
||||
this.root.push(new MathText(text));
|
||||
}
|
||||
}
|
17
src/file/paragraph/math/math-text.spec.ts
Normal file
17
src/file/paragraph/math/math-text.spec.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { MathText } from "./math-text";
|
||||
|
||||
describe("MathText", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create a MathText with correct root key", () => {
|
||||
const mathText = new MathText("2+2");
|
||||
const tree = new Formatter().format(mathText);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:t": ["2+2"],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
9
src/file/paragraph/math/math-text.ts
Normal file
9
src/file/paragraph/math/math-text.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
export class MathText extends XmlComponent {
|
||||
constructor(text: string) {
|
||||
super("m:t");
|
||||
|
||||
this.root.push(text);
|
||||
}
|
||||
}
|
38
src/file/paragraph/math/math.spec.ts
Normal file
38
src/file/paragraph/math/math.spec.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { Math } from "./math";
|
||||
import { MathRun } from "./math-run";
|
||||
|
||||
describe("Math", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create a Math with correct root key", () => {
|
||||
const math = new Math({
|
||||
children: [],
|
||||
});
|
||||
const tree = new Formatter().format(math);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:oMath": {},
|
||||
});
|
||||
});
|
||||
|
||||
it("should be able to add children", () => {
|
||||
const math = new Math({
|
||||
children: [new MathRun("2+2")],
|
||||
});
|
||||
const tree = new Formatter().format(math);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:oMath": [
|
||||
{
|
||||
"m:r": [
|
||||
{
|
||||
"m:t": ["2+2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
18
src/file/paragraph/math/math.ts
Normal file
18
src/file/paragraph/math/math.ts
Normal file
@ -0,0 +1,18 @@
|
||||
// http://www.datypic.com/sc/ooxml/e-m_oMath-1.html
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { MathComponent } from "./math-component";
|
||||
|
||||
export interface IMathOptions {
|
||||
readonly children: MathComponent[];
|
||||
}
|
||||
|
||||
export class Math extends XmlComponent {
|
||||
constructor(options: IMathOptions) {
|
||||
super("m:oMath");
|
||||
|
||||
for (const child of options.children) {
|
||||
this.root.push(child);
|
||||
}
|
||||
}
|
||||
}
|
7
src/file/paragraph/math/n-ary/index.ts
Normal file
7
src/file/paragraph/math/n-ary/index.ts
Normal file
@ -0,0 +1,7 @@
|
||||
export * from "./math-accent-character";
|
||||
export * from "./math-base";
|
||||
export * from "./math-limit-location";
|
||||
export * from "./math-naray-properties";
|
||||
export * from "./math-sub-script";
|
||||
export * from "./math-sum";
|
||||
export * from "./math-super-script";
|
22
src/file/paragraph/math/n-ary/math-accent-character.spec.ts
Normal file
22
src/file/paragraph/math/n-ary/math-accent-character.spec.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { MathAccentCharacter } from "./math-accent-character";
|
||||
|
||||
describe("MathAccentCharacter", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create a MathAccentCharacter with correct root key", () => {
|
||||
const mathAccentCharacter = new MathAccentCharacter("∑");
|
||||
|
||||
const tree = new Formatter().format(mathAccentCharacter);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:chr": {
|
||||
_attr: {
|
||||
"m:val": "∑",
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
14
src/file/paragraph/math/n-ary/math-accent-character.ts
Normal file
14
src/file/paragraph/math/n-ary/math-accent-character.ts
Normal file
@ -0,0 +1,14 @@
|
||||
// http://www.datypic.com/sc/ooxml/e-m_chr-1.html
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
|
||||
class MathAccentCharacterAttributes extends XmlAttributeComponent<{ readonly accent: string }> {
|
||||
protected readonly xmlKeys = { accent: "m:val" };
|
||||
}
|
||||
|
||||
export class MathAccentCharacter extends XmlComponent {
|
||||
constructor(accent: string) {
|
||||
super("m:chr");
|
||||
|
||||
this.root.push(new MathAccentCharacterAttributes({ accent }));
|
||||
}
|
||||
}
|
27
src/file/paragraph/math/n-ary/math-base.spec.ts
Normal file
27
src/file/paragraph/math/n-ary/math-base.spec.ts
Normal file
@ -0,0 +1,27 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { MathRun } from "../math-run";
|
||||
import { MathBase } from "./math-base";
|
||||
|
||||
describe("MathBase", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create a MathBase with correct root key", () => {
|
||||
const mathBase = new MathBase([new MathRun("2+2")]);
|
||||
|
||||
const tree = new Formatter().format(mathBase);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:e": [
|
||||
{
|
||||
"m:r": [
|
||||
{
|
||||
"m:t": ["2+2"],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
14
src/file/paragraph/math/n-ary/math-base.ts
Normal file
14
src/file/paragraph/math/n-ary/math-base.ts
Normal file
@ -0,0 +1,14 @@
|
||||
// http://www.datypic.com/sc/ooxml/e-m_e-1.html
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { MathComponent } from "../math-component";
|
||||
|
||||
export class MathBase extends XmlComponent {
|
||||
constructor(children: MathComponent[]) {
|
||||
super("m:e");
|
||||
|
||||
for (const child of children) {
|
||||
this.root.push(child);
|
||||
}
|
||||
}
|
||||
}
|
22
src/file/paragraph/math/n-ary/math-limit-location.spec.ts
Normal file
22
src/file/paragraph/math/n-ary/math-limit-location.spec.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { MathLimitLocation } from "./math-limit-location";
|
||||
|
||||
describe("MathLimitLocation", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create a MathLimitLocation with correct root key", () => {
|
||||
const mathLimitLocation = new MathLimitLocation();
|
||||
|
||||
const tree = new Formatter().format(mathLimitLocation);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:limLoc": {
|
||||
_attr: {
|
||||
"m:val": "undOvr",
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
14
src/file/paragraph/math/n-ary/math-limit-location.ts
Normal file
14
src/file/paragraph/math/n-ary/math-limit-location.ts
Normal file
@ -0,0 +1,14 @@
|
||||
// http://www.datypic.com/sc/ooxml/e-m_limLoc-1.html
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
|
||||
class MathLimitLocationAttributes extends XmlAttributeComponent<{ readonly value: string }> {
|
||||
protected readonly xmlKeys = { value: "m:val" };
|
||||
}
|
||||
|
||||
export class MathLimitLocation extends XmlComponent {
|
||||
constructor() {
|
||||
super("m:limLoc");
|
||||
|
||||
this.root.push(new MathLimitLocationAttributes({ value: "undOvr" }));
|
||||
}
|
||||
}
|
133
src/file/paragraph/math/n-ary/math-naray-properties.spec.ts
Normal file
133
src/file/paragraph/math/n-ary/math-naray-properties.spec.ts
Normal file
@ -0,0 +1,133 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "export/formatter";
|
||||
|
||||
import { MathNArayProperties } from "./math-naray-properties";
|
||||
|
||||
describe("MathNArayProperties", () => {
|
||||
describe("#constructor()", () => {
|
||||
it("should create a MathNArayProperties with correct root key", () => {
|
||||
const mathNArayProperties = new MathNArayProperties("∑", true, true);
|
||||
|
||||
const tree = new Formatter().format(mathNArayProperties);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:naryPr": [
|
||||
{
|
||||
"m:chr": {
|
||||
_attr: {
|
||||
"m:val": "∑",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"m:limLoc": {
|
||||
_attr: {
|
||||
"m:val": "undOvr",
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("should add super-script hide attributes", () => {
|
||||
const mathNArayProperties = new MathNArayProperties("∑", false, true);
|
||||
|
||||
const tree = new Formatter().format(mathNArayProperties);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:naryPr": [
|
||||
{
|
||||
"m:chr": {
|
||||
_attr: {
|
||||
"m:val": "∑",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"m:limLoc": {
|
||||
_attr: {
|
||||
"m:val": "undOvr",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"m:supHide": {
|
||||
_attr: {
|
||||
"m:val": 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("should add sub-script hide attributes", () => {
|
||||
const mathNArayProperties = new MathNArayProperties("∑", true, false);
|
||||
|
||||
const tree = new Formatter().format(mathNArayProperties);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:naryPr": [
|
||||
{
|
||||
"m:chr": {
|
||||
_attr: {
|
||||
"m:val": "∑",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"m:limLoc": {
|
||||
_attr: {
|
||||
"m:val": "undOvr",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"m:subHide": {
|
||||
_attr: {
|
||||
"m:val": 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("should add both super-script and sub-script hide attributes", () => {
|
||||
const mathNArayProperties = new MathNArayProperties("∑", false, false);
|
||||
|
||||
const tree = new Formatter().format(mathNArayProperties);
|
||||
expect(tree).to.deep.equal({
|
||||
"m:naryPr": [
|
||||
{
|
||||
"m:chr": {
|
||||
_attr: {
|
||||
"m:val": "∑",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"m:limLoc": {
|
||||
_attr: {
|
||||
"m:val": "undOvr",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"m:supHide": {
|
||||
_attr: {
|
||||
"m:val": 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"m:subHide": {
|
||||
_attr: {
|
||||
"m:val": 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
24
src/file/paragraph/math/n-ary/math-naray-properties.ts
Normal file
24
src/file/paragraph/math/n-ary/math-naray-properties.ts
Normal file
@ -0,0 +1,24 @@
|
||||
// http://www.datypic.com/sc/ooxml/e-m_naryPr-1.html
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
import { MathAccentCharacter } from "./math-accent-character";
|
||||
import { MathLimitLocation } from "./math-limit-location";
|
||||
import { MathSubScriptHide } from "./math-sub-script-hide";
|
||||
import { MathSuperScriptHide } from "./math-super-script-hide";
|
||||
|
||||
export class MathNArayProperties extends XmlComponent {
|
||||
constructor(accent: string, hasSuperScript: boolean, hasSubScript: boolean) {
|
||||
super("m:naryPr");
|
||||
|
||||
this.root.push(new MathAccentCharacter(accent));
|
||||
this.root.push(new MathLimitLocation());
|
||||
|
||||
if (!hasSuperScript) {
|
||||
this.root.push(new MathSuperScriptHide());
|
||||
}
|
||||
|
||||
if (!hasSubScript) {
|
||||
this.root.push(new MathSubScriptHide());
|
||||
}
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user