Change docx folder to more appropriate "file" folder
This commit is contained in:
50
src/file/document/body/body.spec.ts
Normal file
50
src/file/document/body/body.spec.ts
Normal file
@ -0,0 +1,50 @@
|
||||
import { assert } from "chai";
|
||||
|
||||
import { Utility } from "../../../tests/utility";
|
||||
import { Body } from "./";
|
||||
import { Columns } from "./columns";
|
||||
import { DocumentGrid } from "./doc-grid";
|
||||
import { PageMargin } from "./page-margin";
|
||||
import { PageSize } from "./page-size";
|
||||
import { SectionProperties } from "./section-properties";
|
||||
|
||||
describe("Body", () => {
|
||||
let body: Body;
|
||||
|
||||
beforeEach(() => {
|
||||
body = new Body();
|
||||
body.push(new SectionProperties());
|
||||
body.push(new PageSize());
|
||||
body.push(new PageMargin());
|
||||
body.push(new Columns());
|
||||
body.push(new DocumentGrid());
|
||||
});
|
||||
|
||||
describe("#constructor()", () => {
|
||||
|
||||
it("should create the Section Properties", () => {
|
||||
const newJson = Utility.jsonify(body);
|
||||
assert.equal(newJson.root[0].rootKey, "w:sectPr");
|
||||
});
|
||||
|
||||
it("should create the Page Size", () => {
|
||||
const newJson = Utility.jsonify(body);
|
||||
assert.equal(newJson.root[1].rootKey, "w:pgSz");
|
||||
});
|
||||
|
||||
it("should create the Page Margin", () => {
|
||||
const newJson = Utility.jsonify(body);
|
||||
assert.equal(newJson.root[2].rootKey, "w:pgMar");
|
||||
});
|
||||
|
||||
it("should create the Columns", () => {
|
||||
const newJson = Utility.jsonify(body);
|
||||
assert.equal(newJson.root[3].rootKey, "w:cols");
|
||||
});
|
||||
|
||||
it("should create the Document Grid", () => {
|
||||
const newJson = Utility.jsonify(body);
|
||||
assert.equal(newJson.root[4].rootKey, "w:docGrid");
|
||||
});
|
||||
});
|
||||
});
|
12
src/file/document/body/body.ts
Normal file
12
src/file/document/body/body.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { XmlComponent } from "../../xml-components";
|
||||
|
||||
export class Body extends XmlComponent {
|
||||
|
||||
constructor() {
|
||||
super("w:body");
|
||||
}
|
||||
|
||||
public push(component: XmlComponent): void {
|
||||
this.root.push(component);
|
||||
}
|
||||
}
|
11
src/file/document/body/columns.ts
Normal file
11
src/file/document/body/columns.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { Attributes, XmlComponent } from "../../xml-components";
|
||||
|
||||
export class Columns extends XmlComponent {
|
||||
|
||||
constructor() {
|
||||
super("w:cols");
|
||||
this.root.push(new Attributes({
|
||||
space: "708",
|
||||
}));
|
||||
}
|
||||
}
|
11
src/file/document/body/doc-grid.ts
Normal file
11
src/file/document/body/doc-grid.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { Attributes, XmlComponent } from "../../xml-components";
|
||||
|
||||
export class DocumentGrid extends XmlComponent {
|
||||
|
||||
constructor() {
|
||||
super("w:docGrid");
|
||||
this.root.push(new Attributes({
|
||||
linePitch: "360",
|
||||
}));
|
||||
}
|
||||
}
|
1
src/file/document/body/index.ts
Normal file
1
src/file/document/body/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from "./body";
|
17
src/file/document/body/page-margin.ts
Normal file
17
src/file/document/body/page-margin.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { Attributes, XmlComponent } from "../../xml-components";
|
||||
|
||||
export class PageMargin extends XmlComponent {
|
||||
|
||||
constructor() {
|
||||
super("w:pgMar");
|
||||
this.root.push(new Attributes({
|
||||
top: "1440",
|
||||
right: "1440",
|
||||
bottom: "1440",
|
||||
left: "1440",
|
||||
header: "708",
|
||||
footer: "708",
|
||||
gutter: "0",
|
||||
}));
|
||||
}
|
||||
}
|
12
src/file/document/body/page-size.ts
Normal file
12
src/file/document/body/page-size.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { Attributes, XmlComponent } from "../../xml-components";
|
||||
|
||||
export class PageSize extends XmlComponent {
|
||||
|
||||
constructor() {
|
||||
super("w:pgSz");
|
||||
this.root.push(new Attributes({
|
||||
w: "11906",
|
||||
h: "16838",
|
||||
}));
|
||||
}
|
||||
}
|
21
src/file/document/body/section-properties.ts
Normal file
21
src/file/document/body/section-properties.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { Attributes, XmlComponent } from "../../xml-components";
|
||||
import { Columns } from "./columns";
|
||||
import { DocumentGrid } from "./doc-grid";
|
||||
import { PageMargin } from "./page-margin";
|
||||
import { PageSize } from "./page-size";
|
||||
|
||||
export class SectionProperties extends XmlComponent {
|
||||
|
||||
constructor() {
|
||||
super("w:sectPr");
|
||||
this.root.push(new Attributes({
|
||||
rsidR: "00B64E8F",
|
||||
rsidRPr: "00D842E4",
|
||||
rsidSect: "000A6AD0",
|
||||
}));
|
||||
this.root.push(new PageSize());
|
||||
this.root.push(new PageMargin());
|
||||
this.root.push(new Columns());
|
||||
this.root.push(new DocumentGrid());
|
||||
}
|
||||
}
|
55
src/file/document/document-attributes.ts
Normal file
55
src/file/document/document-attributes.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import { XmlAttributeComponent } from "../xml-components";
|
||||
|
||||
export interface IDocumentAttributesProperties {
|
||||
wpc?: string;
|
||||
mc?: string;
|
||||
o?: string;
|
||||
r?: string;
|
||||
m?: string;
|
||||
v?: string;
|
||||
wp14?: string;
|
||||
wp?: string;
|
||||
w10?: string;
|
||||
w?: string;
|
||||
w14?: string;
|
||||
w15?: string;
|
||||
wpg?: string;
|
||||
wpi?: string;
|
||||
wne?: string;
|
||||
wps?: string;
|
||||
Ignorable?: string;
|
||||
cp?: string;
|
||||
dc?: string;
|
||||
dcterms?: string;
|
||||
dcmitype?: string;
|
||||
xsi?: string;
|
||||
type?: string;
|
||||
}
|
||||
|
||||
export class DocumentAttributes extends XmlAttributeComponent<IDocumentAttributesProperties> {
|
||||
protected xmlKeys = {
|
||||
wpc: "xmlns:wpc",
|
||||
mc: "xmlns:mc",
|
||||
o: "xmlns:o",
|
||||
r: "xmlns:r",
|
||||
m: "xmlns:m",
|
||||
v: "xmlns:v",
|
||||
wp14: "xmlns:wp14",
|
||||
wp: "xmlns:wp",
|
||||
w10: "xmlns:w10",
|
||||
w: "xmlns:w",
|
||||
w14: "xmlns:w14",
|
||||
w15: "xmlns:w15",
|
||||
wpg: "xmlns:wpg",
|
||||
wpi: "xmlns:wpi",
|
||||
wne: "xmlns:wne",
|
||||
wps: "xmlns:wps",
|
||||
Ignorable: "mc:Ignorable",
|
||||
cp: "xmlns:cp",
|
||||
dc: "xmlns:dc",
|
||||
dcterms: "xmlns:dcterms",
|
||||
dcmitype: "xmlns:dcmitype",
|
||||
xsi: "xmlns:xsi",
|
||||
type: "xsi:type",
|
||||
};
|
||||
}
|
74
src/file/document/document.spec.ts
Normal file
74
src/file/document/document.spec.ts
Normal file
@ -0,0 +1,74 @@
|
||||
import { assert, expect } from "chai";
|
||||
|
||||
import * as docx from "../../";
|
||||
import { Formatter } from "../../export/formatter";
|
||||
|
||||
describe("Document", () => {
|
||||
let document: docx.Document;
|
||||
|
||||
beforeEach(() => {
|
||||
document = new docx.Document();
|
||||
});
|
||||
|
||||
describe("#constructor()", () => {
|
||||
|
||||
it("should create valid JSON", () => {
|
||||
const stringifiedJson = JSON.stringify(document);
|
||||
let newJson;
|
||||
|
||||
try {
|
||||
newJson = JSON.parse(stringifiedJson);
|
||||
} catch (e) {
|
||||
assert.isTrue(false);
|
||||
}
|
||||
assert.isTrue(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("#createParagraph", () => {
|
||||
it("should create a new paragraph and append it to body", () => {
|
||||
const para = document.createParagraph();
|
||||
expect(para).to.be.an.instanceof(docx.Paragraph);
|
||||
const body = new Formatter().format(document)["w:document"][1]["w:body"];
|
||||
expect(body).to.be.an("array").which.has.length.at.least(1);
|
||||
expect(body[0]).to.have.property("w:p");
|
||||
});
|
||||
|
||||
it("should use the text given to create a run in the paragraph", () => {
|
||||
const para = document.createParagraph("sample paragraph text");
|
||||
expect(para).to.be.an.instanceof(docx.Paragraph);
|
||||
const body = new Formatter().format(document)["w:document"][1]["w:body"];
|
||||
expect(body).to.be.an("array").which.has.length.at.least(1);
|
||||
expect(body[0]).to.have.property("w:p").which.includes({
|
||||
"w:r": [
|
||||
{"w:rPr": []},
|
||||
{"w:t": [{_attr: {"xml:space": "preserve"}}, "sample paragraph text"]},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("#createTable", () => {
|
||||
it("should create a new table and append it to body", () => {
|
||||
const table = document.createTable(2, 3);
|
||||
expect(table).to.be.an.instanceof(docx.Table);
|
||||
const body = new Formatter().format(document)["w:document"][1]["w:body"];
|
||||
expect(body).to.be.an("array").which.has.length.at.least(1);
|
||||
expect(body[0]).to.have.property("w:tbl");
|
||||
});
|
||||
|
||||
it("should create a table with the correct dimensions", () => {
|
||||
document.createTable(2, 3);
|
||||
const body = new Formatter().format(document)["w:document"][1]["w:body"];
|
||||
expect(body).to.be.an("array").which.has.length.at.least(1);
|
||||
expect(body[0]).to.have.property("w:tbl").which.includes({
|
||||
"w:tblGrid": [
|
||||
{"w:gridCol": [{_attr: {"w:w": 1}}]},
|
||||
{"w:gridCol": [{_attr: {"w:w": 1}}]},
|
||||
{"w:gridCol": [{_attr: {"w:w": 1}}]},
|
||||
],
|
||||
});
|
||||
expect(body[0]["w:tbl"].filter((x) => x["w:tr"])).to.have.length(2);
|
||||
});
|
||||
});
|
||||
});
|
55
src/file/document/document.ts
Normal file
55
src/file/document/document.ts
Normal file
@ -0,0 +1,55 @@
|
||||
// http://officeopenxml.com/WPdocument.php
|
||||
import { Paragraph } from "../paragraph";
|
||||
import { Table } from "../table";
|
||||
import { XmlComponent } from "../xml-components";
|
||||
import { Body } from "./body";
|
||||
import { DocumentAttributes } from "./document-attributes";
|
||||
|
||||
export class Document extends XmlComponent {
|
||||
private body: Body;
|
||||
|
||||
constructor() {
|
||||
super("w:document");
|
||||
this.root.push(new DocumentAttributes({
|
||||
wpc: "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
|
||||
mc: "http://schemas.openxmlformats.org/markup-compatibility/2006",
|
||||
o: "urn:schemas-microsoft-com:office:office",
|
||||
r: "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
|
||||
m: "http://schemas.openxmlformats.org/officeDocument/2006/math",
|
||||
v: "urn:schemas-microsoft-com:vml",
|
||||
wp14: "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing",
|
||||
wp: "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
|
||||
w10: "urn:schemas-microsoft-com:office:word",
|
||||
w: "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
|
||||
w14: "http://schemas.microsoft.com/office/word/2010/wordml",
|
||||
w15: "http://schemas.microsoft.com/office/word/2012/wordml",
|
||||
wpg: "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup",
|
||||
wpi: "http://schemas.microsoft.com/office/word/2010/wordprocessingInk",
|
||||
wne: "http://schemas.microsoft.com/office/word/2006/wordml",
|
||||
wps: "http://schemas.microsoft.com/office/word/2010/wordprocessingShape",
|
||||
Ignorable: "w14 w15 wp14",
|
||||
}));
|
||||
this.body = new Body();
|
||||
this.root.push(this.body);
|
||||
}
|
||||
|
||||
public addParagraph(paragraph: Paragraph): void {
|
||||
this.body.push(paragraph);
|
||||
}
|
||||
|
||||
public createParagraph(text?: string): Paragraph {
|
||||
const para = new Paragraph(text);
|
||||
this.addParagraph(para);
|
||||
return para;
|
||||
}
|
||||
|
||||
public addTable(table: Table): void {
|
||||
this.body.push(table);
|
||||
}
|
||||
|
||||
public createTable(rows: number, cols: number): Table {
|
||||
const table = new Table(rows, cols);
|
||||
this.addTable(table);
|
||||
return table;
|
||||
}
|
||||
}
|
1
src/file/document/index.ts
Normal file
1
src/file/document/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from "./document";
|
Reference in New Issue
Block a user