Compare commits

...

27 Commits
4.0.0 ... 4.1.0

Author SHA1 Message Date
90f3da74cf Version bump 2018-09-20 21:04:21 +01:00
28d8659cb5 Merge pull request #161 from bre7/patch-1
Improve fluency (dsl like)
2018-09-20 21:03:38 +01:00
5d1740a2ef Style fixes 2018-09-20 16:54:58 -03:00
7aa44568d8 Improve fluency (dsl like)
addRunProperty / addParagraphProperty should also return this
2018-09-20 15:51:57 -03:00
78ad3e340a Merge pull request #160 from bre7/patch-1
Added charSpacing to docs
2018-09-20 18:46:30 +01:00
63a965beab Added charSpacing 2018-09-20 14:00:49 -03:00
815354e06f Merge pull request #159 from bre7/master
Added character spacing attribute
2018-09-20 16:27:21 +01:00
6da3efdacc Added character spacing attribute 2018-09-19 18:41:55 -03:00
a9c69664c7 Merge pull request #156 from bre7/master
Added mirror margins option
2018-09-19 12:40:31 +01:00
50569224c3 Fixed tests (included mirrorMargins attr) 2018-09-18 23:50:10 -03:00
877aa325bb Merge pull request #153 from felipeochoa/fix-109
Fix 109
2018-09-19 01:25:32 +01:00
eb797d8986 Ignore Yarn lock & IntelliJ ide 2018-09-18 21:03:50 -03:00
fcb542471b Added mirror margins options 2018-09-18 21:03:20 -03:00
53b24965aa Merge pull request #152 from felipeochoa/fix-typo-table-properties
Fix typo in private property
2018-09-17 19:04:58 +01:00
edce1bef92 Allow creating Styles and assigning them to File.Styles
Closes #109
2018-09-17 12:48:21 -05:00
e0d54d3af3 Fix typo in private property 2018-09-17 12:41:17 -05:00
5b28eb0d00 Update logo to be blinking 2018-09-12 21:19:33 +01:00
8b667b8d4c Merge pull request #148 from dolanmiu/feat/logo
Updated logo dimensions
2018-09-11 19:18:31 +01:00
a601a82a37 Updated logo dimensions 2018-09-11 19:16:47 +01:00
dcf3767a05 Merge pull request #147 from dolanmiu/feat/logo
Add logo
2018-09-11 19:08:27 +01:00
a9278009f8 Merge branch 'feat/logo' of https://github.com/dolanmiu/docx into feat/logo 2018-09-11 19:04:19 +01:00
5220f9e07c Add correct image 2018-09-11 19:03:53 +01:00
0b88064ca2 Update README.md 2018-09-11 19:03:29 +01:00
2233ccb968 Add logo 2018-09-11 19:00:52 +01:00
311fde01a1 Update Readme 2018-09-10 22:44:44 +01:00
0303a9f553 Merge pull request #141 from XappMedia/min-eight
Minimum Node v8 Support
2018-09-04 18:26:34 +01:00
2dab11e8b3 minimum 8 2018-08-31 11:25:05 -04:00
21 changed files with 146 additions and 68 deletions

4
.gitignore vendored
View File

@ -48,8 +48,12 @@ docs/.nojekyll
!.vscode/extensions.json
.history
# IntelliJ
.idea
# Lock files
package-lock.json
yarn.lock
# Documents
My Document.docx

1
.nvmrc Normal file
View File

@ -0,0 +1 @@
v8

View File

@ -1,5 +1,5 @@
<p align="center">
<img alt="clippy the assistant" src="http://i60.tinypic.com/339pvtt.png">
<img alt="clippy the assistant" src="https://i.imgur.com/37uBGhO.gif">
</p>
<p align="center">
@ -17,11 +17,20 @@
[![PRs Welcome][pr-image]][pr-url]
<p align="center">
<img src="https://thumbs.gfycat.com/ComplexEminentEquine-size_restricted.gif" alt="drawing" width="800"/>
<img src="https://i.imgur.com/H5FA1Qy.gif" alt="drawing" width="800"/>
</p>
# Demo
## Browser
Here are examples of `docx` being used with basic `HTML/JS` in a browser environment.
* https://codepen.io/anon/pen/dqoVgQ
* https://jsfiddle.net/3xhezb5w/2
## Node
Press `endpoint` on the `RunKit` website:
![RunKit Instructions](https://user-images.githubusercontent.com/2917613/38582539-f84311b6-3d07-11e8-90db-5885ae02c3c4.png)
@ -33,9 +42,11 @@ Press `endpoint` on the `RunKit` website:
* https://runkit.com/dolanmiu/docx-demo5 - Images
* https://runkit.com/dolanmiu/docx-demo6 - Margins
* https://runkit.com/dolanmiu/docx-demo7 - Landscape
* https://runkit.com/dolanmiu/docx-demo8/1.0.1 - Header and Footer
* https://runkit.com/dolanmiu/docx-demo8 - Header and Footer
* https://runkit.com/dolanmiu/docx-demo10 - **My CV generated with docx**
More [here](https://docx.js.org/#/examples) and [here](https://github.com/dolanmiu/docx/tree/master/demo)
# How to use & Documentation
Please refer to the [documentation at https://docx.js.org/](https://docx.js.org/) for details on how to use this library, examples and much more!

34
demo/demo27.ts Normal file
View File

@ -0,0 +1,34 @@
import * as fs from "fs";
import { Document, Packer } from "../build";
const doc = new Document();
const myStyles = doc.Styles;
// The first argument is an ID you use to apply the style to paragraphs
// The second argument is a human-friendly name to show in the UI
myStyles.createParagraphStyle("myWonkyStyle", "My Wonky Style")
.basedOn("Normal")
.next("Normal")
.color("990000")
.italics()
.indent({left: 720}) // 720 TWIP === 720 / 20 pt === .5 in
.spacing({line: 276}); // 276 / 240 = 1.15x line spacing
myStyles.createParagraphStyle("Heading2", "Heading 2")
.basedOn("Normal")
.next("Normal")
.quickFormat()
.size(26) // 26 half-points === 13pt font
.bold()
.underline("double", "FF0000")
.spacing({before: 240, after: 120}); // TWIP for both
doc.createParagraph("Hello").style("myWonkyStyle");
doc.createParagraph("World").heading2(); // Uses the Heading2 style
const packer = new Packer();
packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
console.log("Document created successfully at project root!");
});

View File

@ -1,5 +1,5 @@
<p align="center">
<img alt="clippy the assistant" src="http://i60.tinypic.com/339pvtt.png">
<img alt="clippy the assistant" src="https://i.imgur.com/pwCV6L8.png">
</p>
<p align="center">
@ -54,6 +54,10 @@ exporter.pack("My First Document");
[@h4buli](https://github.com/h4buli)
<p align="center">
<img alt="clippy the assistant" src="http://i60.tinypic.com/339pvtt.png">
</p>
---
Made with 💖

View File

@ -20,6 +20,7 @@ const name = new TextRun("Name:")
* `.size(halfPts)`: Set the font size, measured in half-points
* `.font(name)`: Set the run's font
* `.style(name)`: Apply a named run style
* `.characterSpacing(value)`: Set the character spacing adjustment (in TWIPs)
* For paragraph formatting:
* `.heading1()`, `.heading2()`, `.heading3()`, `.heading4()`, `.heading5()`, `.title()`: apply the appropriate style to the paragraph
* `.left()`, `.center()`, `.right()`, `.justified()`: set the paragraph's alignment

BIN
logo/logo-small.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

BIN
logo/logo-small.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

BIN
logo/logo-small.psd Normal file

Binary file not shown.

BIN
logo/logo.psd Normal file

Binary file not shown.

View File

@ -1,6 +1,6 @@
{
"name": "docx",
"version": "4.0.0",
"version": "4.1.0",
"description": "Generate .docx documents with JavaScript (formerly Office-Clippy)",
"main": "build/index.js",
"scripts": {
@ -82,5 +82,8 @@
"typedoc": "^0.11.1",
"typescript": "2.9.2",
"webpack": "^3.10.0"
},
"engines": {
"node": ">=8"
}
}

View File

@ -8,6 +8,7 @@ export interface IPageMarginAttributes {
header?: number;
footer?: number;
gutter?: number;
mirror?: boolean;
}
export class PageMarginAttributes extends XmlAttributeComponent<IPageMarginAttributes> {
@ -19,5 +20,6 @@ export class PageMarginAttributes extends XmlAttributeComponent<IPageMarginAttri
header: "w:header",
footer: "w:footer",
gutter: "w:gutter",
mirror: "w:mirrorMargins",
};
}

View File

@ -2,7 +2,7 @@ import { XmlComponent } from "file/xml-components";
import { PageMarginAttributes } from "./page-margin-attributes";
export class PageMargin extends XmlComponent {
constructor(top: number, right: number, bottom: number, left: number, header: number, footer: number, gutter: number) {
constructor(top: number, right: number, bottom: number, left: number, header: number, footer: number, gutter: number, mirror: boolean) {
super("w:pgMar");
this.root.push(
new PageMarginAttributes({
@ -13,6 +13,7 @@ export class PageMargin extends XmlComponent {
header: header,
footer: footer,
gutter: gutter,
mirror: mirror,
}),
);
}

View File

@ -17,6 +17,7 @@ describe("SectionProperties", () => {
header: 708,
footer: 708,
gutter: 0,
mirror: false,
space: 708,
linePitch: 360,
headerId: 100,
@ -40,6 +41,7 @@ describe("SectionProperties", () => {
"w:left": 1440,
"w:header": 708,
"w:gutter": 0,
"w:mirrorMargins": false,
},
},
],
@ -69,6 +71,7 @@ describe("SectionProperties", () => {
"w:left": 1440,
"w:header": 708,
"w:gutter": 0,
"w:mirrorMargins": false,
},
},
],
@ -99,6 +102,7 @@ describe("SectionProperties", () => {
"w:left": 1440,
"w:header": 708,
"w:gutter": 0,
"w:mirrorMargins": false,
},
},
],
@ -124,6 +128,7 @@ describe("SectionProperties", () => {
"w:left": 1440,
"w:header": 708,
"w:gutter": 0,
"w:mirrorMargins": false,
},
},
],
@ -150,6 +155,7 @@ describe("SectionProperties", () => {
"w:left": 1440,
"w:header": 708,
"w:gutter": 0,
"w:mirrorMargins": false,
},
},
],

View File

@ -38,6 +38,7 @@ export class SectionProperties extends XmlComponent {
header: 708,
footer: 708,
gutter: 0,
mirror: false,
space: 708,
linePitch: 360,
orientation: PageOrientation.PORTRAIT,
@ -69,6 +70,7 @@ export class SectionProperties extends XmlComponent {
mergedOptions.header,
mergedOptions.footer,
mergedOptions.gutter,
mergedOptions.mirror,
),
);
this.root.push(new Columns(mergedOptions.space));

View File

@ -1,2 +1,3 @@
export * from "./document";
export * from "./document-attributes";
export * from "./body";

View File

@ -17,7 +17,7 @@ import { Table } from "./table";
export class File {
private readonly document: Document;
private readonly styles: Styles;
private styles: Styles;
private readonly coreProperties: CoreProperties;
private readonly numbering: Numbering;
private readonly media: Media;
@ -214,6 +214,10 @@ export class File {
return this.styles;
}
public set Styles(styles: Styles) {
this.styles = styles;
}
public get CoreProperties(): CoreProperties {
return this.coreProperties;
}

View File

@ -25,6 +25,17 @@ export class BoldComplexScript extends XmlComponent {
}
}
export class CharacterSpacing extends XmlComponent {
constructor(value: number) {
super("w:spacing");
this.root.push(
new Attributes({
val: value,
}),
);
}
}
export class Italics extends XmlComponent {
constructor() {
super("w:i");

View File

@ -47,12 +47,14 @@ export class ParagraphStyle extends Style {
this.root.push(this.runProperties);
}
public addParagraphProperty(property: XmlComponent): void {
public addParagraphProperty(property: XmlComponent): ParagraphStyle {
this.paragraphProperties.push(property);
return this;
}
public addRunProperty(property: XmlComponent): void {
public addRunProperty(property: XmlComponent): ParagraphStyle {
this.runProperties.push(property);
return this;
}
public basedOn(parentId: string): ParagraphStyle {
@ -73,121 +75,101 @@ export class ParagraphStyle extends Style {
// ---------- Run formatting ---------------------- //
public size(twips: number): ParagraphStyle {
this.addRunProperty(new formatting.Size(twips));
this.addRunProperty(new formatting.SizeComplexScript(twips));
return this;
return this.addRunProperty(new formatting.Size(twips)).addRunProperty(new formatting.SizeComplexScript(twips));
}
public bold(): ParagraphStyle {
this.addRunProperty(new formatting.Bold());
return this;
return this.addRunProperty(new formatting.Bold());
}
public italics(): ParagraphStyle {
this.addRunProperty(new formatting.Italics());
return this;
return this.addRunProperty(new formatting.Italics());
}
public smallCaps(): ParagraphStyle {
this.addRunProperty(new formatting.SmallCaps());
return this;
return this.addRunProperty(new formatting.SmallCaps());
}
public allCaps(): ParagraphStyle {
this.addRunProperty(new formatting.Caps());
return this;
return this.addRunProperty(new formatting.Caps());
}
public strike(): ParagraphStyle {
this.addRunProperty(new formatting.Strike());
return this;
return this.addRunProperty(new formatting.Strike());
}
public doubleStrike(): ParagraphStyle {
this.addRunProperty(new formatting.DoubleStrike());
return this;
return this.addRunProperty(new formatting.DoubleStrike());
}
public subScript(): ParagraphStyle {
this.addRunProperty(new formatting.SubScript());
return this;
return this.addRunProperty(new formatting.SubScript());
}
public superScript(): ParagraphStyle {
this.addRunProperty(new formatting.SuperScript());
return this;
return this.addRunProperty(new formatting.SuperScript());
}
public underline(underlineType?: string, color?: string): ParagraphStyle {
this.addRunProperty(new formatting.Underline(underlineType, color));
return this;
return this.addRunProperty(new formatting.Underline(underlineType, color));
}
public color(color: string): ParagraphStyle {
this.addRunProperty(new formatting.Color(color));
return this;
return this.addRunProperty(new formatting.Color(color));
}
public font(fontName: string): ParagraphStyle {
this.addRunProperty(new formatting.RunFonts(fontName));
return this;
return this.addRunProperty(new formatting.RunFonts(fontName));
}
public characterSpacing(value: number): ParagraphStyle {
return this.addRunProperty(new formatting.CharacterSpacing(value));
}
// --------------------- Paragraph formatting ------------------------ //
public center(): ParagraphStyle {
this.addParagraphProperty(new paragraph.Alignment("center"));
return this;
return this.addParagraphProperty(new paragraph.Alignment("center"));
}
public left(): ParagraphStyle {
this.addParagraphProperty(new paragraph.Alignment("left"));
return this;
return this.addParagraphProperty(new paragraph.Alignment("left"));
}
public right(): ParagraphStyle {
this.addParagraphProperty(new paragraph.Alignment("right"));
return this;
return this.addParagraphProperty(new paragraph.Alignment("right"));
}
public justified(): ParagraphStyle {
this.addParagraphProperty(new paragraph.Alignment("both"));
return this;
return this.addParagraphProperty(new paragraph.Alignment("both"));
}
public thematicBreak(): ParagraphStyle {
this.addParagraphProperty(new paragraph.ThematicBreak());
return this;
return this.addParagraphProperty(new paragraph.ThematicBreak());
}
public maxRightTabStop(): ParagraphStyle {
this.addParagraphProperty(new paragraph.MaxRightTabStop());
return this;
return this.addParagraphProperty(new paragraph.MaxRightTabStop());
}
public leftTabStop(position: number): ParagraphStyle {
this.addParagraphProperty(new paragraph.LeftTabStop(position));
return this;
return this.addParagraphProperty(new paragraph.LeftTabStop(position));
}
public indent(attrs: object): ParagraphStyle {
this.addParagraphProperty(new paragraph.Indent(attrs));
return this;
return this.addParagraphProperty(new paragraph.Indent(attrs));
}
public spacing(params: paragraph.ISpacingProperties): ParagraphStyle {
this.addParagraphProperty(new paragraph.Spacing(params));
return this;
return this.addParagraphProperty(new paragraph.Spacing(params));
}
public keepNext(): ParagraphStyle {
this.addParagraphProperty(new paragraph.KeepNext());
return this;
return this.addParagraphProperty(new paragraph.KeepNext());
}
public keepLines(): ParagraphStyle {
this.addParagraphProperty(new paragraph.KeepLines());
return this;
return this.addParagraphProperty(new paragraph.KeepLines());
}
}
@ -267,24 +249,21 @@ export class CharacterStyle extends Style {
return this;
}
public addRunProperty(property: XmlComponent): void {
public addRunProperty(property: XmlComponent): CharacterStyle {
this.runProperties.push(property);
return this;
}
public color(color: string): CharacterStyle {
this.addRunProperty(new formatting.Color(color));
return this;
return this.addRunProperty(new formatting.Color(color));
}
public underline(underlineType?: string, color?: string): CharacterStyle {
this.addRunProperty(new formatting.Underline(underlineType, color));
return this;
return this.addRunProperty(new formatting.Underline(underlineType, color));
}
public size(twips: number): CharacterStyle {
this.addRunProperty(new formatting.Size(twips));
this.addRunProperty(new formatting.SizeComplexScript(twips));
return this;
return this.addRunProperty(new formatting.Size(twips)).addRunProperty(new formatting.SizeComplexScript(twips));
}
}

View File

@ -219,6 +219,20 @@ describe("ParagraphStyle", () => {
});
});
it("#character spacing", () => {
const style = new ParagraphStyle("myStyleId").characterSpacing(24);
const tree = new Formatter().format(style);
expect(tree).to.deep.equal({
"w:style": [
{ _attr: { "w:type": "paragraph", "w:styleId": "myStyleId" } },
{ "w:pPr": [] },
{
"w:rPr": [{ "w:spacing": [{ _attr: { "w:val": 24 } }] }],
},
],
});
});
it("#left", () => {
const style = new ParagraphStyle("myStyleId").left();
const tree = new Formatter().format(style);

View File

@ -3,13 +3,13 @@ import { WidthType } from "./table-cell";
import { TableCellMargin } from "./table-cell-margin";
export class TableProperties extends XmlComponent {
private readonly cellMargain: TableCellMargin;
private readonly cellMargin: TableCellMargin;
constructor() {
super("w:tblPr");
this.cellMargain = new TableCellMargin();
this.root.push(this.cellMargain);
this.cellMargin = new TableCellMargin();
this.root.push(this.cellMargin);
}
public setWidth(type: WidthType, w: number | string): TableProperties {
@ -28,7 +28,7 @@ export class TableProperties extends XmlComponent {
}
public get CellMargin(): TableCellMargin {
return this.cellMargain;
return this.cellMargin;
}
}