Merge pull request #828 from dolanmiu/feat/even-odd-headers
Feat/even odd headers
This commit is contained in:
70
demo/63-odd-even-header-footer.ts
Normal file
70
demo/63-odd-even-header-footer.ts
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
// Move + offset header and footer
|
||||||
|
// Import from 'docx' rather than '../build' if you install from npm
|
||||||
|
import * as fs from "fs";
|
||||||
|
import { Document, Footer, Header, Packer, PageBreak, Paragraph, TextRun } from "../build";
|
||||||
|
|
||||||
|
const doc = new Document({
|
||||||
|
evenAndOddHeaderAndFooters: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
doc.addSection({
|
||||||
|
headers: {
|
||||||
|
default: new Header({
|
||||||
|
children: [
|
||||||
|
new Paragraph({
|
||||||
|
text: "Odd Header text",
|
||||||
|
}),
|
||||||
|
new Paragraph({
|
||||||
|
text: "Odd - Some more header text",
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
even: new Header({
|
||||||
|
children: [
|
||||||
|
new Paragraph({
|
||||||
|
text: "Even header text",
|
||||||
|
}),
|
||||||
|
new Paragraph({
|
||||||
|
text: "Even - Some more header text",
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
footers: {
|
||||||
|
default: new Footer({
|
||||||
|
children: [
|
||||||
|
new Paragraph({
|
||||||
|
text: "Odd Footer text",
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
even: new Footer({
|
||||||
|
children: [
|
||||||
|
new Paragraph({
|
||||||
|
text: "Even Cool Footer text",
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
children: [
|
||||||
|
new Paragraph({
|
||||||
|
children: [new TextRun("Hello World 1"), new PageBreak()],
|
||||||
|
}),
|
||||||
|
new Paragraph({
|
||||||
|
children: [new TextRun("Hello World 2"), new PageBreak()],
|
||||||
|
}),
|
||||||
|
new Paragraph({
|
||||||
|
children: [new TextRun("Hello World 3"), new PageBreak()],
|
||||||
|
}),
|
||||||
|
new Paragraph({
|
||||||
|
children: [new TextRun("Hello World 4"), new PageBreak()],
|
||||||
|
}),
|
||||||
|
new Paragraph({
|
||||||
|
children: [new TextRun("Hello World 5"), new PageBreak()],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
Packer.toBuffer(doc).then((buffer) => {
|
||||||
|
fs.writeFileSync("My Document.docx", buffer);
|
||||||
|
});
|
@ -30,6 +30,7 @@ export interface IPropertiesOptions {
|
|||||||
};
|
};
|
||||||
readonly compatabilityModeVersion?: number;
|
readonly compatabilityModeVersion?: number;
|
||||||
readonly customProperties?: ICustomPropertyOptions[];
|
readonly customProperties?: ICustomPropertyOptions[];
|
||||||
|
readonly evenAndOddHeaderAndFooters?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class CoreProperties extends XmlComponent {
|
export class CoreProperties extends XmlComponent {
|
||||||
|
@ -451,4 +451,14 @@ describe("File", () => {
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should create with even and odd headers and footers", () => {
|
||||||
|
const doc = new File({
|
||||||
|
evenAndOddHeaderAndFooters: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
const tree = new Formatter().format(doc.Settings);
|
||||||
|
|
||||||
|
expect(tree["w:settings"][2]).to.deep.equal({ "w:evenAndOddHeaders": {} });
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -86,6 +86,7 @@ export class File {
|
|||||||
this.documentWrapper = new DocumentWrapper({ background: options.background || {} });
|
this.documentWrapper = new DocumentWrapper({ background: options.background || {} });
|
||||||
this.settings = new Settings({
|
this.settings = new Settings({
|
||||||
compatabilityModeVersion: options.compatabilityModeVersion,
|
compatabilityModeVersion: options.compatabilityModeVersion,
|
||||||
|
evenAndOddHeaders: options.evenAndOddHeaderAndFooters ? true : false,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.media = fileProperties.template && fileProperties.template.media ? fileProperties.template.media : new Media();
|
this.media = fileProperties.template && fileProperties.template.media ? fileProperties.template.media : new Media();
|
||||||
|
9
src/file/settings/even-odd-headers.ts
Normal file
9
src/file/settings/even-odd-headers.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
// http://officeopenxml.com/WPSectionFooterReference.php
|
||||||
|
// https://c-rex.net/projects/samples/ooxml/e1/Part4/OOXML_P4_DOCX_evenAndOddHeaders_topic_ID0ET1WU.html
|
||||||
|
import { XmlComponent } from "file/xml-components";
|
||||||
|
|
||||||
|
export class EvenAndOddHeadersAndFooters extends XmlComponent {
|
||||||
|
constructor() {
|
||||||
|
super("w:evenAndOddHeaders");
|
||||||
|
}
|
||||||
|
}
|
@ -7,7 +7,9 @@ import { Settings } from "./settings";
|
|||||||
describe("Settings", () => {
|
describe("Settings", () => {
|
||||||
describe("#constructor", () => {
|
describe("#constructor", () => {
|
||||||
it("should create a empty Settings with correct rootKey", () => {
|
it("should create a empty Settings with correct rootKey", () => {
|
||||||
const settings = new Settings({});
|
const settings = new Settings({
|
||||||
|
evenAndOddHeaders: false,
|
||||||
|
});
|
||||||
const tree = new Formatter().format(settings);
|
const tree = new Formatter().format(settings);
|
||||||
let keys = Object.keys(tree);
|
let keys = Object.keys(tree);
|
||||||
expect(keys).is.an.instanceof(Array);
|
expect(keys).is.an.instanceof(Array);
|
||||||
@ -45,12 +47,16 @@ describe("Settings", () => {
|
|||||||
expect(updateFieldsAttr["w:val"]).to.be.equal(true);
|
expect(updateFieldsAttr["w:val"]).to.be.equal(true);
|
||||||
};
|
};
|
||||||
it("should add a UpdateFields with value true", () => {
|
it("should add a UpdateFields with value true", () => {
|
||||||
const settings = new Settings({});
|
const settings = new Settings({
|
||||||
|
evenAndOddHeaders: false,
|
||||||
|
});
|
||||||
settings.addUpdateFields();
|
settings.addUpdateFields();
|
||||||
assertSettingsWithUpdateFields(settings);
|
assertSettingsWithUpdateFields(settings);
|
||||||
});
|
});
|
||||||
it("should add a UpdateFields with value true only once", () => {
|
it("should add a UpdateFields with value true only once", () => {
|
||||||
const settings = new Settings({});
|
const settings = new Settings({
|
||||||
|
evenAndOddHeaders: false,
|
||||||
|
});
|
||||||
settings.addUpdateFields();
|
settings.addUpdateFields();
|
||||||
assertSettingsWithUpdateFields(settings);
|
assertSettingsWithUpdateFields(settings);
|
||||||
settings.addUpdateFields();
|
settings.addUpdateFields();
|
||||||
@ -59,7 +65,9 @@ describe("Settings", () => {
|
|||||||
});
|
});
|
||||||
describe("#addCompatibility", () => {
|
describe("#addCompatibility", () => {
|
||||||
it("should add an empty Compatibility by default", () => {
|
it("should add an empty Compatibility by default", () => {
|
||||||
const settings = new Settings({});
|
const settings = new Settings({
|
||||||
|
evenAndOddHeaders: false,
|
||||||
|
});
|
||||||
|
|
||||||
const tree = new Formatter().format(settings);
|
const tree = new Formatter().format(settings);
|
||||||
let keys: string[] = Object.keys(tree);
|
let keys: string[] = Object.keys(tree);
|
||||||
@ -88,7 +96,9 @@ describe("Settings", () => {
|
|||||||
});
|
});
|
||||||
describe("#addTrackRevisions", () => {
|
describe("#addTrackRevisions", () => {
|
||||||
it("should add an empty Track Revisions", () => {
|
it("should add an empty Track Revisions", () => {
|
||||||
const settings = new Settings({});
|
const settings = new Settings({
|
||||||
|
evenAndOddHeaders: false,
|
||||||
|
});
|
||||||
settings.addTrackRevisions();
|
settings.addTrackRevisions();
|
||||||
|
|
||||||
const tree = new Formatter().format(settings);
|
const tree = new Formatter().format(settings);
|
||||||
@ -109,7 +119,9 @@ describe("Settings", () => {
|
|||||||
});
|
});
|
||||||
describe("#addTrackRevisionsTwice", () => {
|
describe("#addTrackRevisionsTwice", () => {
|
||||||
it("should add an empty Track Revisions if called twice", () => {
|
it("should add an empty Track Revisions if called twice", () => {
|
||||||
const settings = new Settings({});
|
const settings = new Settings({
|
||||||
|
evenAndOddHeaders: false,
|
||||||
|
});
|
||||||
settings.addTrackRevisions();
|
settings.addTrackRevisions();
|
||||||
settings.addTrackRevisions();
|
settings.addTrackRevisions();
|
||||||
|
|
||||||
@ -129,4 +141,33 @@ describe("Settings", () => {
|
|||||||
expect(keys[0]).to.be.equal("w:trackRevisions");
|
expect(keys[0]).to.be.equal("w:trackRevisions");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("#addTrackRevisionsTwice", () => {
|
||||||
|
it("should add an empty Track Revisions if called twice", () => {
|
||||||
|
const settings = new Settings({
|
||||||
|
evenAndOddHeaders: true,
|
||||||
|
});
|
||||||
|
settings.addTrackRevisions();
|
||||||
|
settings.addTrackRevisions();
|
||||||
|
|
||||||
|
const tree = new Formatter().format(settings);
|
||||||
|
let keys: string[] = Object.keys(tree);
|
||||||
|
expect(keys[0]).to.be.equal("w:settings");
|
||||||
|
const rootArray = tree["w:settings"];
|
||||||
|
expect(rootArray).is.an.instanceof(Array);
|
||||||
|
expect(rootArray).has.length(5);
|
||||||
|
keys = Object.keys(rootArray[0]);
|
||||||
|
expect(keys).is.an.instanceof(Array);
|
||||||
|
expect(keys).has.length(1);
|
||||||
|
expect(keys[0]).to.be.equal("_attr");
|
||||||
|
keys = Object.keys(rootArray[4]);
|
||||||
|
expect(keys).is.an.instanceof(Array);
|
||||||
|
expect(keys).has.length(1);
|
||||||
|
expect(keys[0]).to.be.equal("w:trackRevisions");
|
||||||
|
keys = Object.keys(rootArray[2]);
|
||||||
|
expect(keys).is.an.instanceof(Array);
|
||||||
|
expect(keys).has.length(1);
|
||||||
|
expect(keys[0]).to.be.equal("w:evenAndOddHeaders");
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
|
||||||
|
|
||||||
import { Compatibility } from "./compatibility";
|
import { Compatibility } from "./compatibility";
|
||||||
import { DisplayBackgroundShape } from "./display-background-shape";
|
import { DisplayBackgroundShape } from "./display-background-shape";
|
||||||
|
import { EvenAndOddHeadersAndFooters } from "./even-odd-headers";
|
||||||
import { TrackRevisions } from "./track-revisions";
|
import { TrackRevisions } from "./track-revisions";
|
||||||
import { UpdateFields } from "./update-fields";
|
import { UpdateFields } from "./update-fields";
|
||||||
|
|
||||||
@ -46,10 +48,10 @@ export class SettingsAttributes extends XmlAttributeComponent<{
|
|||||||
|
|
||||||
export interface ISettingsOptions {
|
export interface ISettingsOptions {
|
||||||
readonly compatabilityModeVersion?: number;
|
readonly compatabilityModeVersion?: number;
|
||||||
|
readonly evenAndOddHeaders: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Settings extends XmlComponent {
|
export class Settings extends XmlComponent {
|
||||||
private readonly compatibility: Compatibility;
|
|
||||||
private readonly trackRevisions: TrackRevisions;
|
private readonly trackRevisions: TrackRevisions;
|
||||||
|
|
||||||
constructor(options: ISettingsOptions) {
|
constructor(options: ISettingsOptions) {
|
||||||
@ -76,11 +78,16 @@ export class Settings extends XmlComponent {
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
this.compatibility = new Compatibility({
|
this.root.push(
|
||||||
version: options.compatabilityModeVersion || 15,
|
new Compatibility({
|
||||||
});
|
version: options.compatabilityModeVersion || 15,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (options.evenAndOddHeaders) {
|
||||||
|
this.root.push(new EvenAndOddHeadersAndFooters());
|
||||||
|
}
|
||||||
|
|
||||||
this.root.push(this.compatibility);
|
|
||||||
this.trackRevisions = new TrackRevisions();
|
this.trackRevisions = new TrackRevisions();
|
||||||
|
|
||||||
this.root.push(new DisplayBackgroundShape());
|
this.root.push(new DisplayBackgroundShape());
|
||||||
|
Reference in New Issue
Block a user