added document attributes
This commit is contained in:
@ -1,4 +1,5 @@
|
|||||||
import {XmlComponent, Attributes} from "../xml-components";
|
import {XmlComponent} from "../xml-components";
|
||||||
|
import {DocumentAttributes} from "../xml-components/document-attributes"
|
||||||
import {Body} from "./body";
|
import {Body} from "./body";
|
||||||
import {Paragraph} from "../paragraph";
|
import {Paragraph} from "../paragraph";
|
||||||
|
|
||||||
@ -8,7 +9,25 @@ export class Document {
|
|||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.document = new Array<XmlComponent>();
|
this.document = new Array<XmlComponent>();
|
||||||
this.document.push(new Attributes({}));
|
this.document.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.body = new Body();
|
||||||
this.document.push(this.body);
|
this.document.push(this.body);
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
export {Document} from "./document";
|
export {Document} from "./document";
|
||||||
export {Paragraph} from "./paragraph";
|
export {Paragraph} from "./paragraph";
|
||||||
export {Run} from "./run"
|
export {Run} from "./run"
|
||||||
|
export {TextRun} from "./run/text-run"
|
54
ts/docx/xml-components/document-attributes.ts
Normal file
54
ts/docx/xml-components/document-attributes.ts
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
import {XmlComponent} from "./";
|
||||||
|
|
||||||
|
interface DocumentAttributesProperties {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class DocumentAttributes implements XmlComponent {
|
||||||
|
private _attrs: Object;
|
||||||
|
|
||||||
|
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'
|
||||||
|
};
|
||||||
|
|
||||||
|
constructor(properties?: DocumentAttributesProperties) {
|
||||||
|
this._attrs = properties
|
||||||
|
|
||||||
|
if (!properties) {
|
||||||
|
this._attrs = {};
|
||||||
|
}
|
||||||
|
this._attrs["xmlKeys"] = this.xmlKeys;
|
||||||
|
}
|
||||||
|
}
|
@ -13,20 +13,39 @@ interface AttributesProperties {
|
|||||||
rsidSect?: string;
|
rsidSect?: string;
|
||||||
w?: string;
|
w?: string;
|
||||||
h?: string;
|
h?: string;
|
||||||
top?: string,
|
top?: string;
|
||||||
right?: string,
|
right?: string;
|
||||||
bottom?: string,
|
bottom?: string;
|
||||||
left?: string,
|
left?: string;
|
||||||
header?: string,
|
header?: string;
|
||||||
footer?: string,
|
footer?: string;
|
||||||
gutter?: string,
|
gutter?: string;
|
||||||
linePitch?: string
|
linePitch?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Attributes implements XmlComponent {
|
export class Attributes implements XmlComponent {
|
||||||
private _attrs: Object;
|
private _attrs: Object;
|
||||||
|
|
||||||
xmlKeys = {};
|
xmlKeys = {
|
||||||
|
val: "w:val",
|
||||||
|
color: "w:color",
|
||||||
|
space: "w:space",
|
||||||
|
sz: "w:sz",
|
||||||
|
type: "w:type",
|
||||||
|
rsidR: "w:rsidR",
|
||||||
|
rsidRPr: "w:rsidRPr",
|
||||||
|
rsidSect: "w:rsidSect",
|
||||||
|
w: "w:w",
|
||||||
|
h: "w:h",
|
||||||
|
top: "w:top",
|
||||||
|
right: "w:right",
|
||||||
|
bottom: "w:bottom",
|
||||||
|
left: "w:left",
|
||||||
|
header: "w:header",
|
||||||
|
footer: "w:footer",
|
||||||
|
gutter: "w:gutter",
|
||||||
|
linePitch: "w:linePitch"
|
||||||
|
};
|
||||||
|
|
||||||
constructor(properties?: AttributesProperties) {
|
constructor(properties?: AttributesProperties) {
|
||||||
this._attrs = properties
|
this._attrs = properties
|
||||||
@ -34,6 +53,8 @@ export class Attributes implements XmlComponent {
|
|||||||
if (!properties) {
|
if (!properties) {
|
||||||
this._attrs = {};
|
this._attrs = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this._attrs["xmlKeys"] = this.xmlKeys;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,3 +1,18 @@
|
|||||||
|
import {XmlComponent} from "../docx/xml-components";
|
||||||
|
import {DocumentAttributes} from "../docx/xml-components/document-attributes"
|
||||||
|
|
||||||
export class Style {
|
export class Style {
|
||||||
|
private styles: Array<XmlComponent>;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.styles = new Array<XmlComponent>();
|
||||||
|
this.styles.push(new DocumentAttributes({
|
||||||
|
mc: 'http://schemas.openxmlformats.org/markup-compatibility/2006',
|
||||||
|
r: 'http://schemas.openxmlformats.org/officeDocument/2006/relationships',
|
||||||
|
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',
|
||||||
|
Ignorable: 'w14 w15'
|
||||||
|
}))
|
||||||
|
}
|
||||||
}
|
}
|
@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
import {Formatter} from "../export/Formatter";
|
import {Formatter} from "../export/Formatter";
|
||||||
import * as docx from "../docx";
|
import * as docx from "../docx";
|
||||||
|
import {Attributes} from "../docx/xml-components";
|
||||||
|
|
||||||
import {assert} from "chai";
|
import {assert} from "chai";
|
||||||
|
|
||||||
function jsonify(obj: Object) {
|
function jsonify(obj: Object) {
|
||||||
@ -11,7 +13,7 @@ function jsonify(obj: Object) {
|
|||||||
return JSON.parse(stringifiedJson);
|
return JSON.parse(stringifiedJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('Formatter', () => {
|
describe.only('Formatter', () => {
|
||||||
var formatter: Formatter;
|
var formatter: Formatter;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@ -19,15 +21,41 @@ describe('Formatter', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('#format()', () => {
|
describe('#format()', () => {
|
||||||
it.only("should format simple paragraph", () => {
|
it("should format simple paragraph", () => {
|
||||||
var paragraph = new docx.Paragraph();
|
var paragraph = new docx.Paragraph();
|
||||||
var newJson = formatter.format(paragraph);
|
var newJson = formatter.format(paragraph);
|
||||||
newJson = jsonify(newJson);
|
newJson = jsonify(newJson);
|
||||||
console.log(newJson);
|
assert.isDefined(newJson["w:p"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should format simple paragraph with bold text", () => {
|
||||||
|
var paragraph = new docx.Paragraph();
|
||||||
|
paragraph.addText(new docx.TextRun("test").bold());
|
||||||
|
var newJson = formatter.format(paragraph);
|
||||||
|
newJson = jsonify(newJson);
|
||||||
|
assert.isDefined(newJson["w:p"][3]["w:r"][0]["w:rPr"][0]["w:b"][0]["_attrs"]["w:val"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should format attributes (rsidSect)", () => {
|
||||||
|
var attributes = new Attributes({
|
||||||
|
rsidSect: "test2"
|
||||||
|
});
|
||||||
|
var newJson = formatter.format(attributes);
|
||||||
|
newJson = jsonify(newJson);
|
||||||
|
assert.isDefined(newJson["_attrs"]["w:rsidSect"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should format attributes (val)", () => {
|
||||||
|
var attributes = new Attributes({
|
||||||
|
val: "test"
|
||||||
|
});
|
||||||
|
var newJson = formatter.format(attributes);
|
||||||
|
newJson = jsonify(newJson);
|
||||||
|
assert.isDefined(newJson["_attrs"]["w:val"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should should change 'p' tag into 'w:p' tag", () => {
|
it("should should change 'p' tag into 'w:p' tag", () => {
|
||||||
var newJson = formatter.format({ "p": "test" });
|
var newJson = formatter.format({ "p": "test", "xmlKeys": { "p": "w:p" } });
|
||||||
assert.isDefined(newJson["w:p"]);
|
assert.isDefined(newJson["w:p"]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user