Make create footnote declarative

This commit is contained in:
Dolan
2019-12-03 23:04:48 +00:00
parent 2b0953bb19
commit 2bece0bb61
4 changed files with 216 additions and 13 deletions

View File

@ -3,7 +3,16 @@
import * as fs from "fs"; import * as fs from "fs";
import { Document, FootnoteReferenceRun, Packer, Paragraph, TextRun } from "../build"; import { Document, FootnoteReferenceRun, Packer, Paragraph, TextRun } from "../build";
const doc = new Document(); const doc = new Document({
footnotes: [
new Paragraph("Foo"),
new Paragraph("Test"),
new Paragraph("My amazing reference"),
new Paragraph("Foo1"),
new Paragraph("Test1"),
new Paragraph("My amazing reference1"),
],
});
doc.addSection({ doc.addSection({
children: [ children: [
@ -23,9 +32,23 @@ doc.addSection({
], ],
}); });
doc.createFootnote(new Paragraph("Foo")); doc.addSection({
doc.createFootnote(new Paragraph("Test")); children: [
doc.createFootnote(new Paragraph("My amazing reference")); new Paragraph({
children: [
new TextRun({
children: ["Hello", new FootnoteReferenceRun(4)],
}),
new TextRun({
children: [" World!", new FootnoteReferenceRun(5)],
}),
],
}),
new Paragraph({
children: [new TextRun("Hello World"), new FootnoteReferenceRun(6)],
}),
],
});
Packer.toBuffer(doc).then((buffer) => { Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer); fs.writeFileSync("My Document.docx", buffer);

View File

@ -2,6 +2,7 @@ import { XmlComponent } from "file/xml-components";
import { DocumentAttributes } from "../document/document-attributes"; import { DocumentAttributes } from "../document/document-attributes";
import { INumberingOptions } from "../numbering"; import { INumberingOptions } from "../numbering";
import { Paragraph } from "../paragraph";
import { IStylesOptions } from "../styles"; import { IStylesOptions } from "../styles";
import { Created, Creator, Description, Keywords, LastModifiedBy, Modified, Revision, Subject, Title } from "./components"; import { Created, Creator, Description, Keywords, LastModifiedBy, Modified, Revision, Subject, Title } from "./components";
@ -16,6 +17,7 @@ export interface IPropertiesOptions {
readonly externalStyles?: string; readonly externalStyles?: string;
readonly styles?: IStylesOptions; readonly styles?: IStylesOptions;
readonly numbering?: INumberingOptions; readonly numbering?: INumberingOptions;
readonly footnotes?: Paragraph[];
} }
export class CoreProperties extends XmlComponent { export class CoreProperties extends XmlComponent {

View File

@ -149,12 +149,187 @@ describe("File", () => {
}); });
describe("#createFootnote", () => { describe("#createFootnote", () => {
it("should call the underlying document's createFootnote", () => { it("should create footnote", () => {
const wrapper = new File(); const wrapper = new File({
const spy = sinon.spy(wrapper.FootNotes, "createFootNote"); footnotes: [new Paragraph("hello")],
wrapper.createFootnote(new Paragraph("")); });
expect(spy.called).to.equal(true); const tree = new Formatter().format(wrapper.FootNotes);
expect(tree).to.deep.equal({
"w:footnotes": [
{
_attr: {
"mc:Ignorable": "w14 w15 wp14",
"xmlns:m": "http://schemas.openxmlformats.org/officeDocument/2006/math",
"xmlns:mc": "http://schemas.openxmlformats.org/markup-compatibility/2006",
"xmlns:o": "urn:schemas-microsoft-com:office:office",
"xmlns:r": "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
"xmlns:v": "urn:schemas-microsoft-com:vml",
"xmlns:w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
"xmlns:w10": "urn:schemas-microsoft-com:office:word",
"xmlns:w14": "http://schemas.microsoft.com/office/word/2010/wordml",
"xmlns:w15": "http://schemas.microsoft.com/office/word/2012/wordml",
"xmlns:wne": "http://schemas.microsoft.com/office/word/2006/wordml",
"xmlns:wp": "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
"xmlns:wp14": "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing",
"xmlns:wpc": "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
"xmlns:wpg": "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup",
"xmlns:wpi": "http://schemas.microsoft.com/office/word/2010/wordprocessingInk",
"xmlns:wps": "http://schemas.microsoft.com/office/word/2010/wordprocessingShape",
},
},
{
"w:footnote": [
{
_attr: {
"w:id": -1,
"w:type": "separator",
},
},
{
"w:p": [
{
"w:pPr": [
{
"w:spacing": {
_attr: {
"w:after": 0,
"w:line": 240,
"w:lineRule": "auto",
},
},
},
],
},
{
"w:r": [
{
"w:rPr": [
{
"w:rStyle": {
_attr: {
"w:val": "FootnoteReference",
},
},
},
],
},
{
"w:footnoteRef": {},
},
],
},
{
"w:r": [
{
"w:separator": {},
},
],
},
],
},
],
},
{
"w:footnote": [
{
_attr: {
"w:id": 0,
"w:type": "continuationSeparator",
},
},
{
"w:p": [
{
"w:pPr": [
{
"w:spacing": {
_attr: {
"w:after": 0,
"w:line": 240,
"w:lineRule": "auto",
},
},
},
],
},
{
"w:r": [
{
"w:rPr": [
{
"w:rStyle": {
_attr: {
"w:val": "FootnoteReference",
},
},
},
],
},
{
"w:footnoteRef": {},
},
],
},
{
"w:r": [
{
"w:continuationSeparator": {},
},
],
},
],
},
],
},
{
"w:footnote": [
{
_attr: {
"w:id": 1,
},
},
{
"w:p": [
{
"w:r": [
{
"w:rPr": [
{
"w:rStyle": {
_attr: {
"w:val": "FootnoteReference",
},
},
},
],
},
{
"w:footnoteRef": {},
},
],
},
{
"w:r": [
{
"w:t": [
{
_attr: {
"xml:space": "preserve",
},
},
"hello",
],
},
],
},
],
},
],
},
],
});
}); });
}); });
}); });

View File

@ -67,6 +67,7 @@ export class File {
creator: "Un-named", creator: "Un-named",
revision: "1", revision: "1",
lastModifiedBy: "Un-named", lastModifiedBy: "Un-named",
footnotes: [],
}, },
fileProperties: IFileProperties = {}, fileProperties: IFileProperties = {},
sections: ISectionOptions[] = [], sections: ISectionOptions[] = [],
@ -136,6 +137,12 @@ export class File {
this.document.add(child); this.document.add(child);
} }
} }
if (options.footnotes) {
for (const paragraph of options.footnotes) {
this.footNotes.createFootNote(paragraph);
}
}
} }
public createHyperlink(link: string, text?: string): Hyperlink { public createHyperlink(link: string, text?: string): Hyperlink {
@ -191,10 +198,6 @@ export class File {
} }
} }
public createFootnote(paragraph: Paragraph): void {
this.footNotes.createFootNote(paragraph);
}
public verifyUpdateFields(): void { public verifyUpdateFields(): void {
if (this.document.getTablesOfContents().length) { if (this.document.getTablesOfContents().length) {
this.settings.addUpdateFields(); this.settings.addUpdateFields();