Change docx folder to more appropriate "file" folder
This commit is contained in:
16
src/file/paragraph/formatting/alignment.ts
Normal file
16
src/file/paragraph/formatting/alignment.ts
Normal file
@ -0,0 +1,16 @@
|
||||
// http://officeopenxml.com/WPalignment.php
|
||||
import { XmlAttributeComponent, XmlComponent } from "../../xml-components";
|
||||
|
||||
export type AlignmentOptions = "left" | "center" | "right" | "both";
|
||||
|
||||
export class AlignmentAttributes extends XmlAttributeComponent<{val: AlignmentOptions}> {
|
||||
protected xmlKeys = {val: "w:val"};
|
||||
}
|
||||
|
||||
export class Alignment extends XmlComponent {
|
||||
|
||||
constructor(type: AlignmentOptions) {
|
||||
super("w:jc");
|
||||
this.root.push(new AlignmentAttributes({val: type}));
|
||||
}
|
||||
}
|
41
src/file/paragraph/formatting/border.spec.ts
Normal file
41
src/file/paragraph/formatting/border.spec.ts
Normal file
@ -0,0 +1,41 @@
|
||||
import { assert } from "chai";
|
||||
|
||||
import { Utility } from "../../../tests/utility";
|
||||
import { ThematicBreak } from "./border";
|
||||
|
||||
describe("Border", () => {
|
||||
// TODO: Need tests here
|
||||
});
|
||||
|
||||
describe("ThematicBreak", () => {
|
||||
let thematicBreak: ThematicBreak;
|
||||
|
||||
beforeEach(() => {
|
||||
thematicBreak = new ThematicBreak();
|
||||
});
|
||||
|
||||
describe("#constructor()", () => {
|
||||
it("should create valid JSON", () => {
|
||||
const stringifiedJson = JSON.stringify(thematicBreak);
|
||||
let newJson;
|
||||
|
||||
try {
|
||||
newJson = JSON.parse(stringifiedJson);
|
||||
} catch (e) {
|
||||
assert.isTrue(false);
|
||||
}
|
||||
assert.isTrue(true);
|
||||
});
|
||||
|
||||
it("should create a Thematic Break with correct border properties", () => {
|
||||
const newJson = Utility.jsonify(thematicBreak);
|
||||
const attributes = {
|
||||
color: "auto",
|
||||
space: "1",
|
||||
val: "single",
|
||||
sz: "6",
|
||||
};
|
||||
assert.equal(JSON.stringify(newJson.root[0].root[0].root), JSON.stringify(attributes));
|
||||
});
|
||||
});
|
||||
});
|
23
src/file/paragraph/formatting/border.ts
Normal file
23
src/file/paragraph/formatting/border.ts
Normal file
@ -0,0 +1,23 @@
|
||||
// http://officeopenxml.com/WPborders.php
|
||||
import { Attributes, XmlComponent } from "../../xml-components";
|
||||
|
||||
class Border extends XmlComponent {
|
||||
|
||||
constructor() {
|
||||
super("w:bottom");
|
||||
this.root.push(new Attributes({
|
||||
color: "auto",
|
||||
space: "1",
|
||||
val: "single",
|
||||
sz: "6",
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
export class ThematicBreak extends XmlComponent {
|
||||
|
||||
constructor() {
|
||||
super("w:pBdr");
|
||||
this.root.push(new Border());
|
||||
}
|
||||
}
|
28
src/file/paragraph/formatting/indent.ts
Normal file
28
src/file/paragraph/formatting/indent.ts
Normal file
@ -0,0 +1,28 @@
|
||||
// http://officeopenxml.com/WPindentation.php
|
||||
import { XmlAttributeComponent, XmlComponent } from "../../xml-components";
|
||||
|
||||
interface IIndentAttributesProperties {
|
||||
left?: number;
|
||||
hanging?: number;
|
||||
firstLine?: number;
|
||||
start?: number;
|
||||
end?: number;
|
||||
}
|
||||
|
||||
class IndentAttributes extends XmlAttributeComponent<IIndentAttributesProperties> {
|
||||
protected xmlKeys = {
|
||||
left: "w:left",
|
||||
hanging: "w:hanging",
|
||||
firstLine: "w:firstLine",
|
||||
start: "w:start",
|
||||
end: "w:end",
|
||||
};
|
||||
}
|
||||
|
||||
export class Indent extends XmlComponent {
|
||||
|
||||
constructor(attrs: object) {
|
||||
super("w:ind");
|
||||
this.root.push(new IndentAttributes(attrs));
|
||||
}
|
||||
}
|
9
src/file/paragraph/formatting/index.ts
Normal file
9
src/file/paragraph/formatting/index.ts
Normal file
@ -0,0 +1,9 @@
|
||||
export * from "./alignment";
|
||||
export * from "./border";
|
||||
export * from "./indent";
|
||||
export * from "./keep";
|
||||
export * from "./page-break";
|
||||
export * from "./spacing";
|
||||
export * from "./style";
|
||||
export * from "./tab-stop";
|
||||
export * from "./unordered-list";
|
13
src/file/paragraph/formatting/keep.ts
Normal file
13
src/file/paragraph/formatting/keep.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { XmlComponent } from "../../xml-components";
|
||||
|
||||
export class KeepLines extends XmlComponent {
|
||||
constructor() {
|
||||
super("w:keepLines");
|
||||
}
|
||||
}
|
||||
|
||||
export class KeepNext extends XmlComponent {
|
||||
constructor() {
|
||||
super("w:keepNext");
|
||||
}
|
||||
}
|
32
src/file/paragraph/formatting/page-break.spec.ts
Normal file
32
src/file/paragraph/formatting/page-break.spec.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { assert } from "chai";
|
||||
|
||||
import { Utility } from "../../../tests/utility";
|
||||
import { PageBreak } from "./page-break";
|
||||
|
||||
describe("PageBreak", () => {
|
||||
let pageBreak: PageBreak;
|
||||
|
||||
beforeEach(() => {
|
||||
pageBreak = new PageBreak();
|
||||
});
|
||||
|
||||
describe("#constructor()", () => {
|
||||
it("should create a Page Break with correct attributes", () => {
|
||||
const newJson = Utility.jsonify(pageBreak);
|
||||
const attributes = {
|
||||
type: "page",
|
||||
};
|
||||
assert.equal(JSON.stringify(newJson.root[1].root[0].root), JSON.stringify(attributes));
|
||||
});
|
||||
|
||||
it("should create a Page Break with w:r", () => {
|
||||
const newJson = Utility.jsonify(pageBreak);
|
||||
assert.equal(newJson.rootKey, "w:r");
|
||||
});
|
||||
|
||||
it("should create a Page Break with a Break inside", () => {
|
||||
const newJson = Utility.jsonify(pageBreak);
|
||||
assert.equal(newJson.root[1].rootKey, "w:br");
|
||||
});
|
||||
});
|
||||
});
|
21
src/file/paragraph/formatting/page-break.ts
Normal file
21
src/file/paragraph/formatting/page-break.ts
Normal file
@ -0,0 +1,21 @@
|
||||
// http://officeopenxml.com/WPtextSpecialContent-break.php
|
||||
import { Attributes, XmlComponent } from "../../xml-components";
|
||||
import { Run } from "../run";
|
||||
|
||||
class Break extends XmlComponent {
|
||||
|
||||
constructor() {
|
||||
super("w:br");
|
||||
this.root.push(new Attributes({
|
||||
type: "page",
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
export class PageBreak extends Run {
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.root.push(new Break());
|
||||
}
|
||||
}
|
24
src/file/paragraph/formatting/spacing.spec.ts
Normal file
24
src/file/paragraph/formatting/spacing.spec.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import { Formatter } from "../../../export/formatter";
|
||||
import { Spacing } from "./spacing";
|
||||
|
||||
describe("Spacing", () => {
|
||||
describe("#constructor", () => {
|
||||
it("should set the properties given", () => {
|
||||
const spacing = new Spacing({before: 100, after: 120, line: 150});
|
||||
const tree = new Formatter().format(spacing);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:spacing": [{_attr: {"w:after": 120, "w:before": 100, "w:line": 150}}],
|
||||
});
|
||||
});
|
||||
|
||||
it("should only set the given properties", () => {
|
||||
const spacing = new Spacing({before: 100});
|
||||
const tree = new Formatter().format(spacing);
|
||||
expect(tree).to.deep.equal({
|
||||
"w:spacing": [{_attr: {"w:before": 100}}],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
23
src/file/paragraph/formatting/spacing.ts
Normal file
23
src/file/paragraph/formatting/spacing.ts
Normal file
@ -0,0 +1,23 @@
|
||||
// http://officeopenxml.com/WPspacing.php
|
||||
import { XmlAttributeComponent, XmlComponent } from "../../xml-components";
|
||||
|
||||
export interface ISpacingProperties {
|
||||
after?: number;
|
||||
before?: number;
|
||||
line?: number;
|
||||
}
|
||||
|
||||
class SpacingAttributes extends XmlAttributeComponent<ISpacingProperties> {
|
||||
protected xmlKeys = {
|
||||
after: "w:after",
|
||||
before: "w:before",
|
||||
line: "w:line",
|
||||
};
|
||||
}
|
||||
|
||||
export class Spacing extends XmlComponent {
|
||||
constructor(opts: ISpacingProperties) {
|
||||
super("w:spacing");
|
||||
this.root.push(new SpacingAttributes(opts));
|
||||
}
|
||||
}
|
22
src/file/paragraph/formatting/style.spec.ts
Normal file
22
src/file/paragraph/formatting/style.spec.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { assert } from "chai";
|
||||
|
||||
import { Utility } from "../../../tests/utility";
|
||||
import { Style } from "./style";
|
||||
|
||||
describe("ParagraphStyle", () => {
|
||||
let style: Style;
|
||||
|
||||
describe("#constructor()", () => {
|
||||
it("should create a style with given value", () => {
|
||||
style = new Style("test");
|
||||
const newJson = Utility.jsonify(style);
|
||||
assert.equal(newJson.root[0].root.val, "test");
|
||||
});
|
||||
|
||||
it("should create a style with blank val", () => {
|
||||
style = new Style("");
|
||||
const newJson = Utility.jsonify(style);
|
||||
assert.equal(newJson.root[0].root.val, "");
|
||||
});
|
||||
});
|
||||
});
|
11
src/file/paragraph/formatting/style.ts
Normal file
11
src/file/paragraph/formatting/style.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { Attributes, XmlComponent } from "../../xml-components";
|
||||
|
||||
export class Style extends XmlComponent {
|
||||
|
||||
constructor(type: string) {
|
||||
super("w:pStyle");
|
||||
this.root.push(new Attributes({
|
||||
val: type,
|
||||
}));
|
||||
}
|
||||
}
|
57
src/file/paragraph/formatting/tab-stop.spec.ts
Normal file
57
src/file/paragraph/formatting/tab-stop.spec.ts
Normal file
@ -0,0 +1,57 @@
|
||||
import { assert } from "chai";
|
||||
|
||||
import { Utility } from "../../../tests/utility";
|
||||
import { LeftTabStop, MaxRightTabStop } from "./tab-stop";
|
||||
|
||||
describe("LeftTabStop", () => {
|
||||
let tabStop: LeftTabStop;
|
||||
|
||||
beforeEach(() => {
|
||||
tabStop = new LeftTabStop(100);
|
||||
});
|
||||
|
||||
describe("#constructor()", () => {
|
||||
it("should create a Tab Stop with correct attributes", () => {
|
||||
const newJson = Utility.jsonify(tabStop);
|
||||
const attributes = {
|
||||
val: "left",
|
||||
pos: 100,
|
||||
};
|
||||
assert.equal(JSON.stringify(newJson.root[0].root[0].root), JSON.stringify(attributes));
|
||||
});
|
||||
|
||||
it("should create a Tab Stop with w:tab", () => {
|
||||
const newJson = Utility.jsonify(tabStop);
|
||||
assert.equal(newJson.root[0].rootKey, "w:tab");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("RightTabStop", () => {
|
||||
// TODO
|
||||
});
|
||||
|
||||
describe("MaxRightTabStop", () => {
|
||||
let tabStop: MaxRightTabStop;
|
||||
|
||||
beforeEach(() => {
|
||||
tabStop = new MaxRightTabStop();
|
||||
});
|
||||
|
||||
describe("#constructor()", () => {
|
||||
it("should create a Tab Stop with correct attributes", () => {
|
||||
const newJson = Utility.jsonify(tabStop);
|
||||
|
||||
const attributes = {
|
||||
val: "right",
|
||||
pos: 9026,
|
||||
};
|
||||
assert.equal(JSON.stringify(newJson.root[0].root[0].root), JSON.stringify(attributes));
|
||||
});
|
||||
|
||||
it("should create a Tab Stop with w:tab", () => {
|
||||
const newJson = Utility.jsonify(tabStop);
|
||||
assert.equal(newJson.root[0].rootKey, "w:tab");
|
||||
});
|
||||
});
|
||||
});
|
51
src/file/paragraph/formatting/tab-stop.ts
Normal file
51
src/file/paragraph/formatting/tab-stop.ts
Normal file
@ -0,0 +1,51 @@
|
||||
// http://officeopenxml.com/WPtab.php
|
||||
import { XmlAttributeComponent, XmlComponent } from "../../xml-components";
|
||||
|
||||
export class TabStop extends XmlComponent {
|
||||
|
||||
constructor(tab: Tab) {
|
||||
super("w:tabs");
|
||||
this.root.push(tab);
|
||||
}
|
||||
}
|
||||
|
||||
export type TabValue = "left" | "right" | "center" | "bar" | "clear" | "decimal" | "end" | "num" | "start";
|
||||
|
||||
export class TabAttributes extends XmlAttributeComponent<{val: TabValue, pos: string | number}> {
|
||||
protected xmlKeys = {val: "w:val", pos: "w:pos"};
|
||||
}
|
||||
|
||||
export class Tab extends XmlComponent {
|
||||
|
||||
constructor(value: TabValue, position: string | number) {
|
||||
super("w:tab");
|
||||
this.root.push(new TabAttributes({
|
||||
val: value,
|
||||
pos: position,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
export class MaxRightTabStop extends TabStop {
|
||||
constructor() {
|
||||
super(new Tab("right", 9026));
|
||||
}
|
||||
}
|
||||
|
||||
export class LeftTabStop extends TabStop {
|
||||
constructor(position: number) {
|
||||
super(new Tab("left", position));
|
||||
}
|
||||
}
|
||||
|
||||
export class RightTabStop extends TabStop {
|
||||
constructor(position: number) {
|
||||
super(new Tab("right", position));
|
||||
}
|
||||
}
|
||||
|
||||
export class CenterTabStop extends TabStop {
|
||||
constructor(position: number) {
|
||||
super(new Tab("center", position));
|
||||
}
|
||||
}
|
31
src/file/paragraph/formatting/unordered-list.spec.ts
Normal file
31
src/file/paragraph/formatting/unordered-list.spec.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import { assert } from "chai";
|
||||
|
||||
import { Utility } from "../../../tests/utility";
|
||||
import { NumberProperties } from "./unordered-list";
|
||||
|
||||
describe("NumberProperties", () => {
|
||||
let numberProperties: NumberProperties;
|
||||
|
||||
beforeEach(() => {
|
||||
numberProperties = new NumberProperties(5, 10);
|
||||
});
|
||||
|
||||
describe("#constructor()", () => {
|
||||
it("should create a Number Properties with correct root key", () => {
|
||||
const newJson = Utility.jsonify(numberProperties);
|
||||
assert.equal(newJson.rootKey, "w:numPr");
|
||||
});
|
||||
|
||||
it("should create a Page Break with a Indent Level inside", () => {
|
||||
const newJson = Utility.jsonify(numberProperties);
|
||||
assert.equal(newJson.root[0].rootKey, "w:ilvl");
|
||||
assert.equal(newJson.root[0].root[0].root.val, 10);
|
||||
});
|
||||
|
||||
it("should create a Page Break with a Number Id inside", () => {
|
||||
const newJson = Utility.jsonify(numberProperties);
|
||||
assert.equal(newJson.root[1].rootKey, "w:numId");
|
||||
assert.equal(newJson.root[1].root[0].root.val, 5);
|
||||
});
|
||||
});
|
||||
});
|
29
src/file/paragraph/formatting/unordered-list.ts
Normal file
29
src/file/paragraph/formatting/unordered-list.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import { Attributes, XmlComponent } from "../../xml-components";
|
||||
|
||||
export class NumberProperties extends XmlComponent {
|
||||
|
||||
constructor(numberId: number, indentLevel: number) {
|
||||
super("w:numPr");
|
||||
this.root.push(new IndentLevel(indentLevel));
|
||||
this.root.push(new NumberId(numberId));
|
||||
}
|
||||
}
|
||||
|
||||
class IndentLevel extends XmlComponent {
|
||||
|
||||
constructor(level: number) {
|
||||
super("w:ilvl");
|
||||
this.root.push(new Attributes({
|
||||
val: level,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
class NumberId extends XmlComponent {
|
||||
constructor(id: number) {
|
||||
super("w:numId");
|
||||
this.root.push(new Attributes({
|
||||
val: id,
|
||||
}));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user