Merge pull request #52 from dolanmiu/feat/file-wrapper
Feat/file wrapper
This commit is contained in:
13
.editorconfig
Normal file
13
.editorconfig
Normal file
@ -0,0 +1,13 @@
|
||||
# Editor configuration, see http://editorconfig.org
|
||||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
[*.md]
|
||||
max_line_length = off
|
||||
trim_trailing_whitespace = false
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -38,3 +38,6 @@ build-tests
|
||||
|
||||
# vscode
|
||||
.vscode
|
||||
|
||||
# Lock files
|
||||
package-lock.json
|
@ -1,6 +1,6 @@
|
||||
const docx = require('../build');
|
||||
|
||||
var doc = new docx.Document();
|
||||
var doc = new docx.File();
|
||||
|
||||
var paragraph = new docx.Paragraph("Hello World");
|
||||
var institutionText = new docx.TextRun("University College London").bold();
|
||||
|
@ -1,7 +1,12 @@
|
||||
const docx = require('../build');
|
||||
|
||||
const styles = new docx.Styles();
|
||||
styles.createParagraphStyle('Heading1', 'Heading 1')
|
||||
const doc = new docx.File({
|
||||
creator: 'Clippy',
|
||||
title: 'Sample Document',
|
||||
description: 'A brief example of using docx',
|
||||
});
|
||||
|
||||
doc.Styles.createParagraphStyle('Heading1', 'Heading 1')
|
||||
.basedOn("Normal")
|
||||
.next("Normal")
|
||||
.quickFormat()
|
||||
@ -10,7 +15,7 @@ styles.createParagraphStyle('Heading1', 'Heading 1')
|
||||
.italics()
|
||||
.spacing({after: 120});
|
||||
|
||||
styles.createParagraphStyle('Heading2', 'Heading 2')
|
||||
doc.Styles.createParagraphStyle('Heading2', 'Heading 2')
|
||||
.basedOn("Normal")
|
||||
.next("Normal")
|
||||
.quickFormat()
|
||||
@ -19,7 +24,7 @@ styles.createParagraphStyle('Heading2', 'Heading 2')
|
||||
.underline('double', 'FF0000')
|
||||
.spacing({before: 240, after: 120});
|
||||
|
||||
styles.createParagraphStyle('aside', 'Aside')
|
||||
doc.Styles.createParagraphStyle('aside', 'Aside')
|
||||
.basedOn('Normal')
|
||||
.next('Normal')
|
||||
.color('999999')
|
||||
@ -27,31 +32,24 @@ styles.createParagraphStyle('aside', 'Aside')
|
||||
.indent(720)
|
||||
.spacing({line: 276});
|
||||
|
||||
styles.createParagraphStyle('wellSpaced', 'Well Spaced')
|
||||
doc.Styles.createParagraphStyle('wellSpaced', 'Well Spaced')
|
||||
.basedOn('Normal')
|
||||
.spacing({line: 276, before: 20 * 72 * .1, after: 20 * 72 * .05});
|
||||
|
||||
styles.createParagraphStyle('ListParagraph', 'List Paragraph')
|
||||
doc.Styles.createParagraphStyle('ListParagraph', 'List Paragraph')
|
||||
.quickFormat()
|
||||
.basedOn('Normal');
|
||||
|
||||
|
||||
const numbering = new docx.Numbering();
|
||||
const numberedAbstract = numbering.createAbstractNumbering();
|
||||
const numberedAbstract = doc.Numbering.createAbstractNumbering();
|
||||
numberedAbstract.createLevel(0, "lowerLetter", "%1)", "left");
|
||||
|
||||
const doc = new docx.Document({
|
||||
creator: 'Clippy',
|
||||
title: 'Sample Document',
|
||||
description: 'A brief example of using docx',
|
||||
});
|
||||
|
||||
doc.createParagraph('Test heading1, bold and italicized').heading1();
|
||||
doc.createParagraph('Some simple content');
|
||||
doc.createParagraph('Test heading2 with double red underline').heading2();
|
||||
|
||||
const letterNumbering = numbering.createConcreteNumbering(numberedAbstract);
|
||||
const letterNumbering5 = numbering.createConcreteNumbering(numberedAbstract);
|
||||
const letterNumbering = doc.Numbering.createConcreteNumbering(numberedAbstract);
|
||||
const letterNumbering5 = doc.Numbering.createConcreteNumbering(numberedAbstract);
|
||||
letterNumbering5.overrideLevel(0, 5);
|
||||
|
||||
doc.createParagraph('Option1').setNumbering(letterNumbering, 0);
|
||||
@ -70,5 +68,5 @@ para.createTextRun(' switching to normal ');
|
||||
para.createTextRun('and then underlined ').underline();
|
||||
para.createTextRun('and back to normal.');
|
||||
|
||||
const exporter = new docx.LocalPacker(doc, styles, undefined, numbering);
|
||||
exporter.pack('test.docx');
|
||||
const exporter = new docx.LocalPacker(doc);
|
||||
exporter.pack('My Document');
|
||||
|
@ -1,6 +1,6 @@
|
||||
const docx = require('../build');
|
||||
|
||||
var doc = new docx.Document();
|
||||
var doc = new docx.File();
|
||||
|
||||
const numbering = new docx.Numbering();
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
const docx = require('../build');
|
||||
|
||||
var doc = new docx.Document();
|
||||
var doc = new docx.File();
|
||||
|
||||
const table = doc.createTable(4, 4);
|
||||
table.getCell(2, 2).addContent(new docx.Paragraph('Hello'));
|
||||
|
19
package.json
19
package.json
@ -4,13 +4,15 @@
|
||||
"description": "Generate .docx documents with JavaScript (formerly Office-Clippy)",
|
||||
"main": "build/index.js",
|
||||
"scripts": {
|
||||
"pretest": "rimraf ./build-tests && tsc -p src/test-tsconfig.json",
|
||||
"test": "mocha ./build-tests --recursive",
|
||||
"pretest": "rimraf ./build",
|
||||
"test": "mocha-webpack \"src/**/*.ts\"",
|
||||
"prepublishOnly": "npm run build",
|
||||
"lint": "tslint --project ./src",
|
||||
"build": "rimraf ./build && tsc -p src",
|
||||
"lint": "tslint --project .",
|
||||
"build": "npm run webpack",
|
||||
"tsc": "rimraf ./build && tsc -p .",
|
||||
"webpack": "rimraf ./build && webpack",
|
||||
"demo": "npm run build && node ./demo",
|
||||
"typedoc": "typedoc --out docs/ src/ --module commonjs --target ES6 --disableOutputCheck"
|
||||
"typedoc": "npm run build && typedoc --out docs/ src/ --module commonjs --target ES6 --disableOutputCheck"
|
||||
},
|
||||
"files": [
|
||||
"src",
|
||||
@ -53,13 +55,16 @@
|
||||
"devDependencies": {
|
||||
"@types/chai": "^3.4.35",
|
||||
"@types/mocha": "^2.2.39",
|
||||
"awesome-typescript-loader": "^3.4.1",
|
||||
"chai": "^3.5.0",
|
||||
"mocha": "^3.2.0",
|
||||
"mocha-webpack": "^1.0.1",
|
||||
"prompt": "^1.0.0",
|
||||
"rimraf": "^2.5.2",
|
||||
"shelljs": "^0.7.7",
|
||||
"tslint": "^5.1.0",
|
||||
"typedoc": "^0.5.10",
|
||||
"typescript": "2.4.1"
|
||||
"typedoc": "^0.9.0",
|
||||
"typescript": "2.4.1",
|
||||
"webpack": "^3.10.0"
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +0,0 @@
|
||||
export * from "./document";
|
||||
export * from "./paragraph";
|
||||
export * from "./run";
|
||||
export { Table } from "./table";
|
@ -1,9 +1,9 @@
|
||||
import { assert } from "chai";
|
||||
|
||||
import * as docx from "../docx";
|
||||
import { Attributes } from "../docx/xml-components";
|
||||
import { Formatter } from "../export/formatter";
|
||||
import { Properties } from "../properties";
|
||||
import * as file from "../file";
|
||||
import { Properties } from "../file/properties";
|
||||
import { Attributes } from "../file/xml-components";
|
||||
import { Utility } from "../tests/utility";
|
||||
|
||||
describe("Formatter", () => {
|
||||
@ -15,21 +15,21 @@ describe("Formatter", () => {
|
||||
|
||||
describe("#format()", () => {
|
||||
it("should format simple paragraph", () => {
|
||||
const paragraph = new docx.Paragraph();
|
||||
const paragraph = new file.Paragraph();
|
||||
const newJson = formatter.format(paragraph);
|
||||
assert.isDefined(newJson["w:p"]);
|
||||
});
|
||||
|
||||
it("should remove xmlKeys", () => {
|
||||
const paragraph = new docx.Paragraph();
|
||||
const paragraph = new file.Paragraph();
|
||||
const newJson = formatter.format(paragraph);
|
||||
const stringifiedJson = JSON.stringify(newJson);
|
||||
assert(stringifiedJson.indexOf("xmlKeys") < 0);
|
||||
});
|
||||
|
||||
it("should format simple paragraph with bold text", () => {
|
||||
const paragraph = new docx.Paragraph();
|
||||
paragraph.addRun(new docx.TextRun("test").bold());
|
||||
const paragraph = new file.Paragraph();
|
||||
paragraph.addRun(new file.TextRun("test").bold());
|
||||
const newJson = formatter.format(paragraph);
|
||||
assert.isDefined(newJson["w:p"][1]["w:r"][0]["w:rPr"][0]["w:b"][0]._attr["w:val"]);
|
||||
});
|
||||
@ -61,7 +61,7 @@ describe("Formatter", () => {
|
||||
});
|
||||
|
||||
it("should should change 'p' tag into 'w:p' tag", () => {
|
||||
const paragraph = new docx.Paragraph();
|
||||
const paragraph = new file.Paragraph();
|
||||
const newJson = formatter.format(paragraph);
|
||||
assert.isDefined(newJson["w:p"]);
|
||||
});
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { BaseXmlComponent } from "../docx/xml-components";
|
||||
import { IXmlableObject } from "../docx/xml-components/xmlable-object";
|
||||
import { BaseXmlComponent } from "file/xml-components";
|
||||
import { IXmlableObject } from "file/xml-components/xmlable-object";
|
||||
|
||||
export class Formatter {
|
||||
public format(input: BaseXmlComponent): IXmlableObject {
|
||||
|
@ -4,12 +4,7 @@ import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import * as xml from "xml";
|
||||
|
||||
import { Document } from "../../docx";
|
||||
import { Media } from "../../media";
|
||||
import { Numbering } from "../../numbering";
|
||||
import { Properties } from "../../properties";
|
||||
import { Styles } from "../../styles";
|
||||
import { DefaultStylesFactory } from "../../styles/factory";
|
||||
import { File } from "file";
|
||||
import { Formatter } from "../formatter";
|
||||
|
||||
const TEMPLATE_PATH = path.resolve(__dirname, "../../../template");
|
||||
@ -17,29 +12,11 @@ const TEMPLATE_PATH = path.resolve(__dirname, "../../../template");
|
||||
export class Compiler {
|
||||
protected archive: archiver.Archiver;
|
||||
private formatter: Formatter;
|
||||
private style: Styles;
|
||||
|
||||
constructor(
|
||||
protected document: Document,
|
||||
style?: Styles,
|
||||
private properties: Properties = new Properties({
|
||||
creator: "Un-named",
|
||||
revision: "1",
|
||||
lastModifiedBy: "Un-named",
|
||||
}),
|
||||
private numbering: Numbering = new Numbering(),
|
||||
private media: Media = new Media(),
|
||||
) {
|
||||
constructor(private file: File) {
|
||||
this.formatter = new Formatter();
|
||||
this.archive = archiver.create("zip", {});
|
||||
|
||||
if (style) {
|
||||
this.style = style;
|
||||
} else {
|
||||
const stylesFactory = new DefaultStylesFactory();
|
||||
this.style = stylesFactory.newInstance();
|
||||
}
|
||||
|
||||
this.archive.on("error", (err) => {
|
||||
throw err;
|
||||
});
|
||||
@ -55,15 +32,15 @@ export class Compiler {
|
||||
cwd: TEMPLATE_PATH,
|
||||
});
|
||||
|
||||
const xmlDocument = xml(this.formatter.format(this.document));
|
||||
const xmlStyles = xml(this.formatter.format(this.style));
|
||||
const xmlProperties = xml(this.formatter.format(this.properties), {
|
||||
const xmlDocument = xml(this.formatter.format(this.file.Document));
|
||||
const xmlStyles = xml(this.formatter.format(this.file.Styles));
|
||||
const xmlProperties = xml(this.formatter.format(this.file.Properties), {
|
||||
declaration: {
|
||||
standalone: "yes",
|
||||
encoding: "UTF-8",
|
||||
},
|
||||
});
|
||||
const xmlNumbering = xml(this.formatter.format(this.numbering));
|
||||
const xmlNumbering = xml(this.formatter.format(this.file.Numbering));
|
||||
|
||||
this.archive.append(xmlDocument, {
|
||||
name: "word/document.xml",
|
||||
@ -81,7 +58,7 @@ export class Compiler {
|
||||
name: "word/numbering.xml",
|
||||
});
|
||||
|
||||
for (const data of this.media.array) {
|
||||
for (const data of this.file.Media.array) {
|
||||
this.archive.append(data.stream, {
|
||||
name: `media/${data.fileName}`,
|
||||
});
|
||||
|
@ -1,10 +1,6 @@
|
||||
import * as express from "express";
|
||||
|
||||
import { Document } from "../../docx/document";
|
||||
import { Media } from "../../media";
|
||||
import { Numbering } from "../../numbering";
|
||||
import { Properties } from "../../properties";
|
||||
import { Styles } from "../../styles";
|
||||
import { File } from "file";
|
||||
import { Compiler } from "./compiler";
|
||||
import { IPacker } from "./packer";
|
||||
|
||||
@ -12,8 +8,8 @@ export class ExpressPacker implements IPacker {
|
||||
private res: express.Response;
|
||||
private packer: Compiler;
|
||||
|
||||
constructor(document: Document, res: express.Response, styles?: Styles, properties?: Properties, numbering?: Numbering, media?: Media) {
|
||||
this.packer = new Compiler(document, styles, properties, numbering, media);
|
||||
constructor(file: File, res: express.Response) {
|
||||
this.packer = new Compiler(file);
|
||||
|
||||
this.res = res;
|
||||
|
||||
|
@ -1,38 +1,33 @@
|
||||
/* tslint:disable:typedef space-before-function-paren */
|
||||
import * as fs from "fs";
|
||||
|
||||
import { Document } from "../../docx/document";
|
||||
import { Paragraph } from "../../docx/paragraph";
|
||||
import { LocalPacker } from "../../export/packer/local";
|
||||
import { Properties } from "../../properties";
|
||||
import { DefaultStylesFactory } from "../../styles/factory";
|
||||
import { File, Paragraph } from "../../file";
|
||||
|
||||
describe("LocalPacker", () => {
|
||||
let packer: LocalPacker;
|
||||
let stylesFactory: DefaultStylesFactory;
|
||||
|
||||
beforeEach(() => {
|
||||
const document = new Document();
|
||||
const paragraph = new Paragraph("test text");
|
||||
const heading = new Paragraph("Hello world").heading1();
|
||||
document.addParagraph(new Paragraph("title").title());
|
||||
document.addParagraph(heading);
|
||||
document.addParagraph(new Paragraph("heading 2").heading2());
|
||||
document.addParagraph(paragraph);
|
||||
const properties = new Properties({
|
||||
const file = new File({
|
||||
creator: "Dolan Miu",
|
||||
revision: "1",
|
||||
lastModifiedBy: "Dolan Miu",
|
||||
});
|
||||
stylesFactory = new DefaultStylesFactory();
|
||||
packer = new LocalPacker(document, stylesFactory.newInstance(), properties);
|
||||
const paragraph = new Paragraph("test text");
|
||||
const heading = new Paragraph("Hello world").heading1();
|
||||
file.addParagraph(new Paragraph("title").title());
|
||||
file.addParagraph(heading);
|
||||
file.addParagraph(new Paragraph("heading 2").heading2());
|
||||
file.addParagraph(paragraph);
|
||||
|
||||
packer = new LocalPacker(file);
|
||||
});
|
||||
|
||||
describe("#pack()", () => {
|
||||
it("should create a standard docx file", async function () {
|
||||
this.timeout(99999999);
|
||||
await packer.pack("build-tests/tests/test");
|
||||
fs.statSync("build-tests/tests/test.docx");
|
||||
await packer.pack("build/tests/test");
|
||||
fs.statSync("build/tests/test.docx");
|
||||
});
|
||||
});
|
||||
|
||||
@ -40,8 +35,8 @@ describe("LocalPacker", () => {
|
||||
it("should create a standard PDF file", async function () {
|
||||
this.timeout(99999999);
|
||||
|
||||
await packer.packPdf("build-tests/tests/pdf-test");
|
||||
fs.statSync("build-tests/tests/pdf-test.pdf");
|
||||
await packer.packPdf("build/tests/pdf-test");
|
||||
fs.statSync("build/tests/pdf-test.pdf");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -2,11 +2,7 @@ import * as fs from "fs";
|
||||
import * as os from "os";
|
||||
import * as path from "path";
|
||||
|
||||
import { Document } from "../../docx/document";
|
||||
import { Media } from "../../media";
|
||||
import { Numbering } from "../../numbering";
|
||||
import { Properties } from "../../properties";
|
||||
import { Styles } from "../../styles";
|
||||
import { File } from "../../file";
|
||||
import { Compiler } from "./compiler";
|
||||
import { IPacker } from "./packer";
|
||||
import { PdfConvertWrapper } from "./pdf-convert-wrapper";
|
||||
@ -16,9 +12,9 @@ export class LocalPacker implements IPacker {
|
||||
private pdfConverter: PdfConvertWrapper;
|
||||
private packer: Compiler;
|
||||
|
||||
constructor(document: Document, styles?: Styles, properties?: Properties, numbering?: Numbering, media?: Media) {
|
||||
constructor(file: File) {
|
||||
this.pdfConverter = new PdfConvertWrapper();
|
||||
this.packer = new Compiler(document, styles, properties, numbering, media);
|
||||
this.packer = new Compiler(file);
|
||||
}
|
||||
|
||||
public async pack(filePath: string): Promise<void> {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { XmlComponent } from "../../xml-components";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
export class Body extends XmlComponent {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Attributes, XmlComponent } from "../../xml-components";
|
||||
import { Attributes, XmlComponent } from "file/xml-components";
|
||||
|
||||
export class Columns extends XmlComponent {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Attributes, XmlComponent } from "../../xml-components";
|
||||
import { Attributes, XmlComponent } from "file/xml-components";
|
||||
|
||||
export class DocumentGrid extends XmlComponent {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Attributes, XmlComponent } from "../../xml-components";
|
||||
import { Attributes, XmlComponent } from "file/xml-components";
|
||||
|
||||
export class PageMargin extends XmlComponent {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Attributes, XmlComponent } from "../../xml-components";
|
||||
import { Attributes, XmlComponent } from "file/xml-components";
|
||||
|
||||
export class PageSize extends XmlComponent {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Attributes, XmlComponent } from "../../xml-components";
|
||||
import { Attributes, XmlComponent } from "file/xml-components";
|
||||
import { Columns } from "./columns";
|
||||
import { DocumentGrid } from "./doc-grid";
|
||||
import { PageMargin } from "./page-margin";
|
@ -1,4 +1,4 @@
|
||||
import { XmlAttributeComponent } from "../xml-components";
|
||||
import { XmlAttributeComponent } from "file/xml-components";
|
||||
|
||||
export interface IDocumentAttributesProperties {
|
||||
wpc?: string;
|
@ -1,7 +1,7 @@
|
||||
// http://officeopenxml.com/WPdocument.php
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { Paragraph } from "../paragraph";
|
||||
import { Table } from "../table";
|
||||
import { XmlComponent } from "../xml-components";
|
||||
import { Body } from "./body";
|
||||
import { DocumentAttributes } from "./document-attributes";
|
||||
|
||||
@ -52,5 +52,4 @@ export class Document extends XmlComponent {
|
||||
this.addTable(table);
|
||||
return table;
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
import { assert } from "chai";
|
||||
import * as fs from "fs";
|
||||
|
||||
import { Utility } from "../../../../tests/utility";
|
||||
import { Utility } from "../../tests/utility";
|
||||
import { Drawing } from "./";
|
||||
|
||||
describe("Drawing", () => {
|
@ -1,5 +1,5 @@
|
||||
import { IData } from "../../../../media/data";
|
||||
import { XmlComponent } from "../../../xml-components";
|
||||
import { IData } from "file/media";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { Inline } from "./inline";
|
||||
|
||||
export class Drawing extends XmlComponent {
|
@ -1,4 +1,4 @@
|
||||
import { XmlComponent } from "../../../../../../xml-components";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { Pic } from "./pic";
|
||||
|
||||
export class GraphicData extends XmlComponent {
|
@ -1,4 +1,4 @@
|
||||
import { XmlComponent } from "../../../../../../../../xml-components";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { Blip } from "./blip";
|
||||
import { SourceRectangle } from "./source-rectangle";
|
||||
import { Stretch } from "./stretch";
|
@ -1,4 +1,4 @@
|
||||
import { XmlAttributeComponent, XmlComponent } from "../../../../../../../../xml-components";
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
|
||||
interface IBlipProperties {
|
||||
embed: string;
|
@ -1,4 +1,4 @@
|
||||
import { XmlComponent } from "../../../../../../../../xml-components";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
export class SourceRectangle extends XmlComponent {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { XmlComponent } from "../../../../../../../../xml-components";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
class FillRectangle extends XmlComponent {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { XmlComponent } from "../../../../../../../xml-components";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { BlipFill } from "./blip/blip-fill";
|
||||
|
||||
export class Pic extends XmlComponent {
|
@ -1,4 +1,4 @@
|
||||
import { XmlAttributeComponent, XmlComponent } from "../../../../../xml-components";
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
import { GraphicData } from "./graphic-data";
|
||||
|
||||
interface IGraphicProperties {
|
@ -1,4 +1,4 @@
|
||||
import { XmlComponent } from "../../../../xml-components";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { Graphic } from "./graphic";
|
||||
|
||||
export class Inline extends XmlComponent {
|
71
src/file/file.ts
Normal file
71
src/file/file.ts
Normal file
@ -0,0 +1,71 @@
|
||||
import { Document } from "./document";
|
||||
import { Media } from "./media";
|
||||
import { Numbering } from "./numbering";
|
||||
import { Paragraph } from "./paragraph";
|
||||
import { IPropertiesOptions, Properties } from "./properties";
|
||||
import { Styles } from "./styles";
|
||||
import { DefaultStylesFactory } from "./styles/factory";
|
||||
import { Table } from "./table";
|
||||
|
||||
export class File {
|
||||
|
||||
private document: Document;
|
||||
private styles: Styles;
|
||||
private properties: Properties;
|
||||
private numbering: Numbering;
|
||||
private media: Media;
|
||||
|
||||
constructor(options?: IPropertiesOptions) {
|
||||
this.document = new Document();
|
||||
const stylesFactory = new DefaultStylesFactory();
|
||||
this.styles = stylesFactory.newInstance();
|
||||
|
||||
if (!options) {
|
||||
options = {
|
||||
creator: "Un-named",
|
||||
revision: "1",
|
||||
lastModifiedBy: "Un-named",
|
||||
};
|
||||
}
|
||||
|
||||
this.properties = new Properties(options);
|
||||
this.numbering = new Numbering();
|
||||
this.media = new Media();
|
||||
}
|
||||
|
||||
public addParagraph(paragraph: Paragraph): void {
|
||||
this.document.addParagraph(paragraph);
|
||||
}
|
||||
|
||||
public createParagraph(text?: string): Paragraph {
|
||||
return this.document.createParagraph();
|
||||
}
|
||||
|
||||
public addTable(table: Table): void {
|
||||
return this.document.addTable(table);
|
||||
}
|
||||
|
||||
public createTable(rows: number, cols: number): Table {
|
||||
return this.document.createTable(rows, cols);
|
||||
}
|
||||
|
||||
public get Document(): Document {
|
||||
return this.document;
|
||||
}
|
||||
|
||||
public get Styles(): Styles {
|
||||
return this.styles;
|
||||
}
|
||||
|
||||
public get Properties(): Properties {
|
||||
return this.properties;
|
||||
}
|
||||
|
||||
public get Numbering(): Numbering {
|
||||
return this.numbering;
|
||||
}
|
||||
|
||||
public get Media(): Media {
|
||||
return this.media;
|
||||
}
|
||||
}
|
8
src/file/index.ts
Normal file
8
src/file/index.ts
Normal file
@ -0,0 +1,8 @@
|
||||
export * from "./document";
|
||||
export * from "./paragraph";
|
||||
export * from "./table";
|
||||
export * from "./file";
|
||||
export * from "./numbering";
|
||||
export * from "./media";
|
||||
export * from "./drawing";
|
||||
export * from "./styles";
|
2
src/file/media/index.ts
Normal file
2
src/file/media/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from "./media";
|
||||
export * from "./data";
|
@ -1,4 +1,4 @@
|
||||
import { XmlAttributeComponent, XmlComponent } from "../docx/xml-components";
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
import { Level } from "./level";
|
||||
import { MultiLevelType } from "./multi-level-type";
|
||||
|
@ -1,8 +1,8 @@
|
||||
import * as paragraph from "../docx/paragraph/formatting";
|
||||
import { ParagraphProperties } from "../docx/paragraph/properties";
|
||||
import * as formatting from "../docx/run/formatting";
|
||||
import { RunProperties } from "../docx/run/properties";
|
||||
import { Attributes, XmlAttributeComponent, XmlComponent } from "../docx/xml-components";
|
||||
import { Attributes, XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
import * as paragraph from "../paragraph/formatting";
|
||||
import { ParagraphProperties } from "../paragraph/properties";
|
||||
import * as formatting from "../paragraph/run/formatting";
|
||||
import { RunProperties } from "../paragraph/run/properties";
|
||||
|
||||
interface ILevelAttributesProperties {
|
||||
ilvl?: number;
|
@ -1,4 +1,4 @@
|
||||
import { Attributes, XmlComponent } from "../docx/xml-components";
|
||||
import { Attributes, XmlComponent } from "file/xml-components";
|
||||
|
||||
export class MultiLevelType extends XmlComponent {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Attributes, XmlAttributeComponent, XmlComponent } from "../docx/xml-components";
|
||||
import { Attributes, XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
import { LevelForOverride } from "./level";
|
||||
|
||||
class AbstractNumId extends XmlComponent {
|
@ -1,5 +1,5 @@
|
||||
import { expect } from "chai";
|
||||
import { Formatter } from "../export/formatter";
|
||||
import { Formatter } from "../../export/formatter";
|
||||
import { Numbering } from "./";
|
||||
import { AbstractNumbering } from "./abstract-numbering";
|
||||
import { LevelForOverride } from "./level";
|
@ -1,7 +1,7 @@
|
||||
import { DocumentAttributes } from "../docx/document/document-attributes";
|
||||
import { Indent } from "../docx/paragraph/formatting";
|
||||
import { RunFonts } from "../docx/run/run-fonts";
|
||||
import { XmlComponent } from "../docx/xml-components";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { DocumentAttributes } from "../document/document-attributes";
|
||||
import { Indent } from "../paragraph/formatting";
|
||||
import { RunFonts } from "../paragraph/run/run-fonts";
|
||||
import { AbstractNumbering } from "./abstract-numbering";
|
||||
import { Num } from "./num";
|
||||
|
@ -1,5 +1,5 @@
|
||||
// http://officeopenxml.com/WPalignment.php
|
||||
import { XmlAttributeComponent, XmlComponent } from "../../xml-components";
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
|
||||
export type AlignmentOptions = "left" | "center" | "right" | "both";
|
||||
|
@ -1,5 +1,5 @@
|
||||
// http://officeopenxml.com/WPborders.php
|
||||
import { Attributes, XmlComponent } from "../../xml-components";
|
||||
import { Attributes, XmlComponent } from "file/xml-components";
|
||||
|
||||
class Border extends XmlComponent {
|
||||
|
@ -1,5 +1,5 @@
|
||||
// http://officeopenxml.com/WPindentation.php
|
||||
import { XmlAttributeComponent, XmlComponent } from "../../xml-components";
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
|
||||
interface IIndentAttributesProperties {
|
||||
left?: number;
|
@ -1,4 +1,4 @@
|
||||
import { XmlComponent } from "../../xml-components";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
export class KeepLines extends XmlComponent {
|
||||
constructor() {
|
@ -1,6 +1,6 @@
|
||||
// http://officeopenxml.com/WPtextSpecialContent-break.php
|
||||
import { Run } from "../../run";
|
||||
import { Attributes, XmlComponent } from "../../xml-components";
|
||||
import { Attributes, XmlComponent } from "file/xml-components";
|
||||
import { Run } from "../run";
|
||||
|
||||
class Break extends XmlComponent {
|
||||
|
@ -1,5 +1,5 @@
|
||||
// http://officeopenxml.com/WPspacing.php
|
||||
import { XmlAttributeComponent, XmlComponent } from "../../xml-components";
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
|
||||
export interface ISpacingProperties {
|
||||
after?: number;
|
@ -1,4 +1,4 @@
|
||||
import { Attributes, XmlComponent } from "../../xml-components";
|
||||
import { Attributes, XmlComponent } from "file/xml-components";
|
||||
|
||||
export class Style extends XmlComponent {
|
||||
|
@ -1,5 +1,5 @@
|
||||
// http://officeopenxml.com/WPtab.php
|
||||
import { XmlAttributeComponent, XmlComponent } from "../../xml-components";
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
|
||||
export class TabStop extends XmlComponent {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Attributes, XmlComponent } from "../../xml-components";
|
||||
import { Attributes, XmlComponent } from "file/xml-components";
|
||||
|
||||
export class NumberProperties extends XmlComponent {
|
||||
|
@ -1,3 +1,4 @@
|
||||
export * from "./formatting";
|
||||
export * from "./paragraph";
|
||||
export * from "./properties";
|
||||
export * from "./run";
|
@ -1,14 +1,14 @@
|
||||
import { assert, expect } from "chai";
|
||||
|
||||
import * as docx from "../../docx";
|
||||
import { Formatter } from "../../export/formatter";
|
||||
import { Numbering } from "../../numbering";
|
||||
import * as file from "../../file";
|
||||
import { Numbering } from "../numbering";
|
||||
|
||||
describe("Paragraph", () => {
|
||||
let paragraph: docx.Paragraph;
|
||||
let paragraph: file.Paragraph;
|
||||
|
||||
beforeEach(() => {
|
||||
paragraph = new docx.Paragraph();
|
||||
paragraph = new file.Paragraph();
|
||||
});
|
||||
|
||||
describe("#constructor()", () => {
|
||||
@ -35,7 +35,7 @@ describe("Paragraph", () => {
|
||||
describe("#createTextRun", () => {
|
||||
it("should add a new run to the paragraph and return it", () => {
|
||||
const run = paragraph.createTextRun("this is a test run");
|
||||
expect(run).to.be.instanceof(docx.TextRun);
|
||||
expect(run).to.be.instanceof(file.TextRun);
|
||||
const tree = new Formatter().format(paragraph)["w:p"];
|
||||
expect(tree).to.be.an("array").which.includes({
|
||||
"w:r": [
|
@ -1,10 +1,8 @@
|
||||
// http://officeopenxml.com/WPparagraph.php
|
||||
import { IData } from "../../media/data";
|
||||
import { Num } from "../../numbering/num";
|
||||
import { Run } from "../run";
|
||||
import { PictureRun } from "../run/picture-run";
|
||||
import { TextRun } from "../run/text-run";
|
||||
import { XmlComponent } from "../xml-components";
|
||||
import { IData } from "file/media";
|
||||
import { Num } from "file/numbering/num";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { PictureRun, Run, TextRun } from "./run";
|
||||
|
||||
import { Alignment } from "./formatting/alignment";
|
||||
import { ThematicBreak } from "./formatting/border";
|
@ -1,5 +1,5 @@
|
||||
// http://officeopenxml.com/WPparagraphProperties.php
|
||||
import { XmlComponent } from "../xml-components";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
export class ParagraphProperties extends XmlComponent {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { assert } from "chai";
|
||||
|
||||
import { Utility } from "../../tests/utility";
|
||||
import { Utility } from "../../../tests/utility";
|
||||
import { Break } from "./break";
|
||||
|
||||
describe("Break", () => {
|
@ -1,5 +1,5 @@
|
||||
// http://officeopenxml.com/WPtextSpecialContent-break.php
|
||||
import { XmlComponent } from "../xml-components";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
export class Break extends XmlComponent {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { XmlComponent } from "../xml-components";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
export class SmallCaps extends XmlComponent {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Attributes, XmlComponent } from "../xml-components";
|
||||
import { Attributes, XmlComponent } from "file/xml-components";
|
||||
export { Underline } from "./underline";
|
||||
export { SubScript, SuperScript } from "./script";
|
||||
export { RunFonts } from "./run-fonts";
|
@ -1,6 +1,6 @@
|
||||
import { Drawing } from "../../drawing";
|
||||
import { IData } from "../../media/data";
|
||||
import { Run } from "../run";
|
||||
import { Drawing } from "./run-components/drawing";
|
||||
|
||||
export class PictureRun extends Run {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { XmlComponent } from "../xml-components";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
export class RunProperties extends XmlComponent {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "../../../export/formatter";
|
||||
import { Formatter } from "../../../../export/formatter";
|
||||
import { Text } from "./text";
|
||||
|
||||
describe("Text", () => {
|
@ -1,4 +1,4 @@
|
||||
import { XmlAttributeComponent, XmlComponent } from "../../xml-components";
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
|
||||
class TextAttributes extends XmlAttributeComponent<{space: "default" | "preserve"}> {
|
||||
protected xmlKeys = {space: "xml:space"};
|
@ -1,6 +1,6 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "../../export/formatter";
|
||||
import { Formatter } from "../../../export/formatter";
|
||||
import { RunFonts } from "./run-fonts";
|
||||
|
||||
describe("RunFonts", () => {
|
@ -1,4 +1,4 @@
|
||||
import { XmlAttributeComponent, XmlComponent } from "../xml-components";
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
|
||||
interface IRunFontAttributesProperties {
|
||||
ascii: string;
|
@ -1,7 +1,7 @@
|
||||
import { assert, expect } from "chai";
|
||||
|
||||
import { Formatter } from "../../export/formatter";
|
||||
import { Utility } from "../../tests/utility";
|
||||
import { Formatter } from "../../../export/formatter";
|
||||
import { Utility } from "../../../tests/utility";
|
||||
import { Run } from "./";
|
||||
|
||||
describe("Run", () => {
|
@ -9,7 +9,7 @@ import { Style } from "./style";
|
||||
import { Tab } from "./tab";
|
||||
import { Underline } from "./underline";
|
||||
|
||||
import { XmlComponent } from "../xml-components";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
export class Run extends XmlComponent {
|
||||
private properties: RunProperties;
|
@ -1,6 +1,6 @@
|
||||
import { assert } from "chai";
|
||||
|
||||
import { Utility } from "../../tests/utility";
|
||||
import { Utility } from "../../../tests/utility";
|
||||
import { SubScript, SuperScript } from "./script";
|
||||
|
||||
describe("SubScript", () => {
|
@ -1,4 +1,4 @@
|
||||
import { Attributes, XmlComponent } from "../xml-components";
|
||||
import { Attributes, XmlComponent } from "file/xml-components";
|
||||
|
||||
export abstract class VerticalAlign extends XmlComponent {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { assert } from "chai";
|
||||
|
||||
import { Utility } from "../../tests/utility";
|
||||
import { Utility } from "../../../tests/utility";
|
||||
import { DoubleStrike, Strike } from "./formatting";
|
||||
|
||||
describe("Strike", () => {
|
@ -1,4 +1,4 @@
|
||||
import { XmlAttributeComponent, XmlComponent } from "../xml-components";
|
||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||
|
||||
class StyleAttributes extends XmlAttributeComponent<{val: string}> {
|
||||
protected xmlKeys = {val: "w:val"};
|
@ -1,6 +1,6 @@
|
||||
import { assert } from "chai";
|
||||
|
||||
import { Utility } from "../../tests/utility";
|
||||
import { Utility } from "../../../tests/utility";
|
||||
import { Tab } from "./tab";
|
||||
|
||||
describe("Tab", () => {
|
@ -1,4 +1,4 @@
|
||||
import { XmlComponent } from "../xml-components";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
|
||||
export class Tab extends XmlComponent {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "../../export/formatter";
|
||||
import { Formatter } from "../../../export/formatter";
|
||||
import { TextRun } from "./text-run";
|
||||
|
||||
describe("TextRun", () => {
|
@ -1,7 +1,7 @@
|
||||
import { assert, expect } from "chai";
|
||||
|
||||
import { Formatter } from "../../export/formatter";
|
||||
import { Utility } from "../../tests/utility";
|
||||
import { Formatter } from "../../../export/formatter";
|
||||
import { Utility } from "../../../tests/utility";
|
||||
import * as u from "./underline";
|
||||
|
||||
describe("Underline", () => {
|
@ -1,4 +1,4 @@
|
||||
import { Attributes, XmlComponent } from "../xml-components";
|
||||
import { Attributes, XmlComponent } from "file/xml-components";
|
||||
|
||||
export abstract class BaseUnderline extends XmlComponent {
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { DocumentAttributes } from "../docx/document/document-attributes";
|
||||
import { XmlComponent } from "../docx/xml-components";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { DocumentAttributes } from "../document/document-attributes";
|
||||
|
||||
export class Title extends XmlComponent {
|
||||
|
1
src/file/properties/index.ts
Normal file
1
src/file/properties/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from "./properties";
|
@ -1,7 +1,7 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "../export/formatter";
|
||||
import { Properties } from "./";
|
||||
import { Formatter } from "../../export/formatter";
|
||||
import { Properties } from "./properties";
|
||||
|
||||
describe("Properties", () => {
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { DocumentAttributes } from "../docx/document/document-attributes";
|
||||
import { XmlComponent } from "../docx/xml-components";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { DocumentAttributes } from "../document/document-attributes";
|
||||
import { Created, Creator, Description, Keywords, LastModifiedBy, Modified, Revision, Subject, Title } from "./components";
|
||||
|
||||
export interface IPropertiesOptions {
|
@ -1,4 +1,4 @@
|
||||
import { XmlAttributeComponent } from "../docx/xml-components";
|
||||
import { XmlAttributeComponent } from "file/xml-components";
|
||||
|
||||
export interface IRelationshipsAttributesProperties {
|
||||
xmlns: string;
|
1
src/file/relationships/index.ts
Normal file
1
src/file/relationships/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from "./relationships";
|
@ -1,4 +1,4 @@
|
||||
import { XmlComponent } from "../docx/xml-components";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { RelationshipsAttributes } from "./attributes";
|
||||
|
||||
export class Relationships extends XmlComponent {
|
@ -1,4 +1,4 @@
|
||||
import { XmlComponent } from "../../docx/xml-components";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { ParagraphPropertiesDefaults } from "./paragraph-properties";
|
||||
import { RunPropertiesDefaults } from "./run-properties";
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { ParagraphProperties } from "../../docx/paragraph/properties";
|
||||
import { XmlComponent } from "../../docx/xml-components";
|
||||
import { XmlComponent } from "file/xml-components";
|
||||
import { ParagraphProperties } from "../../paragraph/properties";
|
||||
|
||||
export class ParagraphPropertiesDefaults extends XmlComponent {
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user