Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
5ae02c3342 | |||
258adba94c | |||
190208d5df | |||
32cda4dfb3 | |||
b2c3dd2f7b | |||
58eca3ff5b | |||
d5c04f9042 | |||
67ea7c95de | |||
e57fd8fc57 | |||
411c0dadb5 | |||
ee81f3c502 | |||
8263b93c36 | |||
be709d082c | |||
70c4e89a65 | |||
8f632d4ecd | |||
6784dc1f3d | |||
fd63a30298 |
35
demo/demo3.js
Normal file
35
demo/demo3.js
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
const docx = require('../build');
|
||||||
|
|
||||||
|
var doc = new docx.Document();
|
||||||
|
|
||||||
|
const numbering = new docx.Numbering();
|
||||||
|
|
||||||
|
const abstractNum = numbering.createAbstractNumbering();
|
||||||
|
abstractNum.createLevel(0, "upperRoman", "%1", "start")
|
||||||
|
.addParagraphProperty(new docx.Indent(720, 260));
|
||||||
|
abstractNum.createLevel(1, "decimal", "%2.", "start")
|
||||||
|
.addParagraphProperty(new docx.Indent(1440, 980));
|
||||||
|
abstractNum.createLevel(2, "lowerLetter", "%3)", "start")
|
||||||
|
.addParagraphProperty(new docx.Indent(2160, 1700));
|
||||||
|
|
||||||
|
const concrete = numbering.createConcreteNumbering(abstractNum);
|
||||||
|
|
||||||
|
var topLevelP = new docx.Paragraph("Hey you");
|
||||||
|
var subP = new docx.Paragraph("What's up fam");
|
||||||
|
var secondSubP = new docx.Paragraph("Hello World 2");
|
||||||
|
var subSubP = new docx.Paragraph("Yeah boi");
|
||||||
|
|
||||||
|
topLevelP.setNumbering(concrete, 0);
|
||||||
|
subP.setNumbering(concrete, 1);
|
||||||
|
secondSubP.setNumbering(concrete, 1);
|
||||||
|
subSubP.setNumbering(concrete, 2);
|
||||||
|
|
||||||
|
doc.addParagraph(topLevelP);
|
||||||
|
doc.addParagraph(subP);
|
||||||
|
doc.addParagraph(secondSubP);
|
||||||
|
doc.addParagraph(subSubP);
|
||||||
|
|
||||||
|
var exporter = new docx.LocalPacker(doc);
|
||||||
|
exporter.pack('My Document');
|
||||||
|
|
||||||
|
console.log('Document created succesfully at project root!');
|
12
demo/demo4.js
Normal file
12
demo/demo4.js
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
const docx = require('../build');
|
||||||
|
|
||||||
|
var doc = new docx.Document();
|
||||||
|
|
||||||
|
const table = doc.createTable(4, 4);
|
||||||
|
table.getCell(2, 2).addContent(new docx.Paragraph('Hello'));
|
||||||
|
|
||||||
|
|
||||||
|
var exporter = new docx.LocalPacker(doc);
|
||||||
|
exporter.pack('My Document');
|
||||||
|
|
||||||
|
console.log('Document created succesfully at project root!');
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "docx",
|
"name": "docx",
|
||||||
"version": "2.1.0",
|
"version": "2.1.3",
|
||||||
"description": "Generate .docx documents with JavaScript (formerly Office-Clippy)",
|
"description": "Generate .docx documents with JavaScript (formerly Office-Clippy)",
|
||||||
"main": "build/index.js",
|
"main": "build/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@ -34,7 +34,7 @@
|
|||||||
],
|
],
|
||||||
"types": "./build/index.d.ts",
|
"types": "./build/index.d.ts",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/archiver": "^1.3.2",
|
"@types/archiver": "^1.3.4",
|
||||||
"@types/express": "^4.0.35",
|
"@types/express": "^4.0.35",
|
||||||
"archiver": "^1.3.0",
|
"archiver": "^1.3.0",
|
||||||
"xml": "^1.0.1"
|
"xml": "^1.0.1"
|
||||||
@ -55,6 +55,6 @@
|
|||||||
"shelljs": "^0.7.7",
|
"shelljs": "^0.7.7",
|
||||||
"tslint": "^5.1.0",
|
"tslint": "^5.1.0",
|
||||||
"typedoc": "^0.5.10",
|
"typedoc": "^0.5.10",
|
||||||
"typescript": "^2.2.1"
|
"typescript": "2.4.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
export { Document } from "./document";
|
export { Document } from "./document";
|
||||||
export { Paragraph } from "./paragraph";
|
export * from "./paragraph";
|
||||||
export { Run } from "./run";
|
export { Run } from "./run";
|
||||||
export { TextRun } from "./run/text-run";
|
export { TextRun } from "./run/text-run";
|
||||||
export { PictureRun } from "./run/picture-run";
|
export { PictureRun } from "./run/picture-run";
|
||||||
|
@ -16,6 +16,8 @@ import { Style } from "./style";
|
|||||||
import { LeftTabStop, MaxRightTabStop } from "./tab-stop";
|
import { LeftTabStop, MaxRightTabStop } from "./tab-stop";
|
||||||
import { NumberProperties } from "./unordered-list";
|
import { NumberProperties } from "./unordered-list";
|
||||||
|
|
||||||
|
export * from "./formatting";
|
||||||
|
|
||||||
export class Paragraph extends XmlComponent {
|
export class Paragraph extends XmlComponent {
|
||||||
private properties: ParagraphProperties;
|
private properties: ParagraphProperties;
|
||||||
|
|
||||||
@ -101,7 +103,7 @@ export class Paragraph extends XmlComponent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public pageBreak(): Paragraph {
|
public pageBreak(): Paragraph {
|
||||||
this.properties.push(new PageBreak());
|
this.root.push(new PageBreak());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { Paragraph } from "../paragraph";
|
import { Paragraph } from "../paragraph";
|
||||||
import { XmlComponent } from "../xml-components";
|
import { XmlComponent } from "../xml-components";
|
||||||
|
import { IXmlableObject } from "../xml-components/xmlable-object";
|
||||||
import { TableGrid } from "./grid";
|
import { TableGrid } from "./grid";
|
||||||
import { TableProperties, WidthTypes } from "./properties";
|
import { TableProperties, WidthTypes } from "./properties";
|
||||||
|
|
||||||
@ -99,7 +99,7 @@ export class TableCell extends XmlComponent {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public prepForXml(): XmlableObject {
|
public prepForXml(): IXmlableObject {
|
||||||
// Cells must end with a paragraph
|
// Cells must end with a paragraph
|
||||||
const retval = super.prepForXml();
|
const retval = super.prepForXml();
|
||||||
const content = retval["w:tc"];
|
const content = retval["w:tc"];
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import { IXmlableObject } from "./xmlable-object";
|
||||||
|
|
||||||
export abstract class BaseXmlComponent {
|
export abstract class BaseXmlComponent {
|
||||||
protected rootKey: string;
|
protected rootKey: string;
|
||||||
|
|
||||||
@ -5,5 +7,5 @@ export abstract class BaseXmlComponent {
|
|||||||
this.rootKey = rootKey;
|
this.rootKey = rootKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract prepForXml(): XmlableObject;
|
public abstract prepForXml(): IXmlableObject;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { BaseXmlComponent } from "./base";
|
import { BaseXmlComponent } from "./base";
|
||||||
|
import { IXmlableObject } from "./xmlable-object";
|
||||||
|
|
||||||
export type AttributeMap<T> = {[P in keyof T]: string};
|
export type AttributeMap<T> = {[P in keyof T]: string};
|
||||||
|
|
||||||
@ -11,7 +12,7 @@ export abstract class XmlAttributeComponent<T> extends BaseXmlComponent {
|
|||||||
this.root = properties;
|
this.root = properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
public prepForXml(): XmlableObject {
|
public prepForXml(): IXmlableObject {
|
||||||
const attrs = {};
|
const attrs = {};
|
||||||
Object.keys(this.root).forEach((key) => {
|
Object.keys(this.root).forEach((key) => {
|
||||||
const value = this.root[key];
|
const value = this.root[key];
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { BaseXmlComponent } from "./base";
|
import { BaseXmlComponent } from "./base";
|
||||||
|
import { IXmlableObject } from "./xmlable-object";
|
||||||
export { BaseXmlComponent };
|
export { BaseXmlComponent };
|
||||||
|
|
||||||
export abstract class XmlComponent extends BaseXmlComponent {
|
export abstract class XmlComponent extends BaseXmlComponent {
|
||||||
@ -9,7 +10,7 @@ export abstract class XmlComponent extends BaseXmlComponent {
|
|||||||
this.root = new Array<BaseXmlComponent>();
|
this.root = new Array<BaseXmlComponent>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public prepForXml(): XmlableObject {
|
public prepForXml(): IXmlableObject {
|
||||||
const children = this.root.map((comp) => {
|
const children = this.root.map((comp) => {
|
||||||
if (comp instanceof BaseXmlComponent) {
|
if (comp instanceof BaseXmlComponent) {
|
||||||
return comp.prepForXml();
|
return comp.prepForXml();
|
||||||
|
3
ts/docx/xml-components/xmlable-object.d.ts
vendored
3
ts/docx/xml-components/xmlable-object.d.ts
vendored
@ -1,3 +0,0 @@
|
|||||||
declare interface XmlableObject extends Object {
|
|
||||||
_attr?: { [key: string]: (string | number | boolean) }
|
|
||||||
}
|
|
3
ts/docx/xml-components/xmlable-object.ts
Normal file
3
ts/docx/xml-components/xmlable-object.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export interface IXmlableObject extends Object {
|
||||||
|
_attr?: { [key: string]: (string | number | boolean) };
|
||||||
|
}
|
@ -1,7 +1,8 @@
|
|||||||
import { BaseXmlComponent } from "../docx/xml-components";
|
import { BaseXmlComponent } from "../docx/xml-components";
|
||||||
|
import { IXmlableObject } from "../docx/xml-components/xmlable-object";
|
||||||
|
|
||||||
export class Formatter {
|
export class Formatter {
|
||||||
public format(input: BaseXmlComponent): XmlableObject {
|
public format(input: BaseXmlComponent): IXmlableObject {
|
||||||
return input.prepForXml();
|
return input.prepForXml();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,24 @@
|
|||||||
import { RunProperties } from "../../docx/run/properties";
|
import { RunProperties } from "../../docx/run/properties";
|
||||||
import { XmlComponent } from "../../docx/xml-components";
|
import { XmlComponent } from "../../docx/xml-components";
|
||||||
|
import { RunFonts } from "../../docx/run/run-fonts";
|
||||||
|
import { Size } from "../../docx/run/formatting";
|
||||||
|
|
||||||
export class RunPropertiesDefaults extends XmlComponent {
|
export class RunPropertiesDefaults extends XmlComponent {
|
||||||
|
private properties: RunProperties;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super("w:rPrDefault");
|
super("w:rPrDefault");
|
||||||
this.root.push(new RunProperties());
|
this.properties = new RunProperties();
|
||||||
|
this.root.push(this.properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
public size(size: number): RunPropertiesDefaults {
|
||||||
|
this.properties.push(new Size(size));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public font(fontName: string): RunPropertiesDefaults {
|
||||||
|
this.properties.push(new RunFonts(fontName));
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ export class DefaultStylesFactory {
|
|||||||
|
|
||||||
public newInstance(): Styles {
|
public newInstance(): Styles {
|
||||||
const styles = new Styles();
|
const styles = new Styles();
|
||||||
styles.push(new DocumentDefaults());
|
styles.createDocumentDefaults();
|
||||||
|
|
||||||
const titleStyle = new TitleStyle();
|
const titleStyle = new TitleStyle();
|
||||||
titleStyle.addRunProperty(new Size(56));
|
titleStyle.addRunProperty(new Size(56));
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { DocumentAttributes } from "../docx/document/document-attributes";
|
import { DocumentAttributes } from "../docx/document/document-attributes";
|
||||||
import { XmlComponent } from "../docx/xml-components";
|
import { XmlComponent } from "../docx/xml-components";
|
||||||
import { ParagraphStyle } from "./style";
|
import { ParagraphStyle } from "./style";
|
||||||
|
import { DocumentDefaults } from "./defaults";
|
||||||
|
|
||||||
export class Styles extends XmlComponent {
|
export class Styles extends XmlComponent {
|
||||||
|
|
||||||
@ -14,11 +15,7 @@ export class Styles extends XmlComponent {
|
|||||||
w15: "http://schemas.microsoft.com/office/word/2012/wordml",
|
w15: "http://schemas.microsoft.com/office/word/2012/wordml",
|
||||||
Ignorable: "w14 w15",
|
Ignorable: "w14 w15",
|
||||||
}));
|
}));
|
||||||
// let latentStyles = new LatentStyles();
|
|
||||||
// latentStyles.push(new LatentStyleException(new LatentStyleExceptionAttributes({
|
|
||||||
// name: "Normal"
|
|
||||||
// })));
|
|
||||||
// this.root.push(latentStyles);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public push(style: XmlComponent): Styles {
|
public push(style: XmlComponent): Styles {
|
||||||
@ -26,6 +23,12 @@ export class Styles extends XmlComponent {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public createDocumentDefaults(): DocumentDefaults {
|
||||||
|
const defaults = new DocumentDefaults();
|
||||||
|
this.push(defaults);
|
||||||
|
return defaults;
|
||||||
|
}
|
||||||
|
|
||||||
public createParagraphStyle(styleId: string, name?: string): ParagraphStyle {
|
public createParagraphStyle(styleId: string, name?: string): ParagraphStyle {
|
||||||
const para = new ParagraphStyle(styleId, name);
|
const para = new ParagraphStyle(styleId, name);
|
||||||
this.push(para);
|
this.push(para);
|
||||||
|
@ -144,13 +144,13 @@ describe("Paragraph", () => {
|
|||||||
const tree = new Formatter().format(paragraph);
|
const tree = new Formatter().format(paragraph);
|
||||||
expect(tree).to.deep.equal({
|
expect(tree).to.deep.equal({
|
||||||
"w:p": [{
|
"w:p": [{
|
||||||
"w:pPr": [{
|
"w:pPr": [],
|
||||||
|
},{
|
||||||
"w:r": [
|
"w:r": [
|
||||||
{"w:rPr": []},
|
{"w:rPr": []},
|
||||||
{"w:br": [{_attr: {"w:type": "page"}}]},
|
{"w:br": [{_attr: {"w:type": "page"}}]},
|
||||||
],
|
],
|
||||||
}],
|
}],
|
||||||
}],
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user