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 {Paragraph} from "../paragraph";
|
||||
|
||||
@ -8,7 +9,25 @@ export class Document {
|
||||
|
||||
constructor() {
|
||||
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.document.push(this.body);
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
export {Document} from "./document";
|
||||
export {Paragraph} from "./paragraph";
|
||||
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;
|
||||
w?: string;
|
||||
h?: string;
|
||||
top?: string,
|
||||
right?: string,
|
||||
bottom?: string,
|
||||
left?: string,
|
||||
header?: string,
|
||||
footer?: string,
|
||||
gutter?: string,
|
||||
linePitch?: string
|
||||
top?: string;
|
||||
right?: string;
|
||||
bottom?: string;
|
||||
left?: string;
|
||||
header?: string;
|
||||
footer?: string;
|
||||
gutter?: string;
|
||||
linePitch?: string;
|
||||
}
|
||||
|
||||
export class Attributes implements XmlComponent {
|
||||
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) {
|
||||
this._attrs = properties
|
||||
@ -34,6 +53,8 @@ export class Attributes implements XmlComponent {
|
||||
if (!properties) {
|
||||
this._attrs = {};
|
||||
}
|
||||
|
||||
this._attrs["xmlKeys"] = this.xmlKeys;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,18 @@
|
||||
export class Style {
|
||||
import {XmlComponent} from "../docx/xml-components";
|
||||
import {DocumentAttributes} from "../docx/xml-components/document-attributes"
|
||||
|
||||
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 * as docx from "../docx";
|
||||
import {Attributes} from "../docx/xml-components";
|
||||
|
||||
import {assert} from "chai";
|
||||
|
||||
function jsonify(obj: Object) {
|
||||
@ -11,7 +13,7 @@ function jsonify(obj: Object) {
|
||||
return JSON.parse(stringifiedJson);
|
||||
}
|
||||
|
||||
describe('Formatter', () => {
|
||||
describe.only('Formatter', () => {
|
||||
var formatter: Formatter;
|
||||
|
||||
beforeEach(() => {
|
||||
@ -19,15 +21,41 @@ describe('Formatter', () => {
|
||||
});
|
||||
|
||||
describe('#format()', () => {
|
||||
it.only("should format simple paragraph", () => {
|
||||
it("should format simple paragraph", () => {
|
||||
var paragraph = new docx.Paragraph();
|
||||
var newJson = formatter.format(paragraph);
|
||||
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", () => {
|
||||
var newJson = formatter.format({ "p": "test" });
|
||||
var newJson = formatter.format({ "p": "test", "xmlKeys": { "p": "w:p" } });
|
||||
assert.isDefined(newJson["w:p"]);
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user