Multiple paragraphs in footnotes

This commit is contained in:
Dolan
2020-01-01 20:22:42 +00:00
parent 97824f1bb5
commit 9c11653313
9 changed files with 86 additions and 61 deletions

View File

@ -4,14 +4,14 @@ 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: [ footnotes: {
new Paragraph("Foo"), 1: { children: [new Paragraph("Foo"), new Paragraph("Bar")] },
new Paragraph("Test"), 2: { children: [new Paragraph("Test")] },
new Paragraph("My amazing reference"), 3: { children: [new Paragraph("My amazing reference")] },
new Paragraph("Foo1"), 4: { children: [new Paragraph("Foo1")] },
new Paragraph("Test1"), 5: { children: [new Paragraph("Test1")] },
new Paragraph("My amazing reference1"), 6: { children: [new Paragraph("My amazing reference1")] },
], },
}); });
doc.addSection({ doc.addSection({

View File

@ -28,7 +28,11 @@ export interface IPropertiesOptions {
readonly externalStyles?: string; readonly externalStyles?: string;
readonly styles?: IStylesOptions; readonly styles?: IStylesOptions;
readonly numbering?: INumberingOptions; readonly numbering?: INumberingOptions;
readonly footnotes?: Paragraph[]; readonly footnotes?: {
readonly [key: string]: {
readonly children: Paragraph[];
};
};
readonly hyperlinks?: { readonly hyperlinks?: {
readonly [key: string]: IInternalHyperlinkDefinition | IExternalHyperlinkDefinition; readonly [key: string]: IInternalHyperlinkDefinition | IExternalHyperlinkDefinition;
}; };

View File

@ -254,7 +254,11 @@ describe("File", () => {
describe("#createFootnote", () => { describe("#createFootnote", () => {
it("should create footnote", () => { it("should create footnote", () => {
const wrapper = new File({ const wrapper = new File({
footnotes: [new Paragraph("hello")], footnotes: {
1: {
children: [new Paragraph("hello")],
},
},
}); });
const tree = new Formatter().format(wrapper.FootNotes); const tree = new Formatter().format(wrapper.FootNotes);

View File

@ -1,4 +1,5 @@
import * as shortid from "shortid"; import * as shortid from "shortid";
import { AppProperties } from "./app-properties/app-properties"; import { AppProperties } from "./app-properties/app-properties";
import { ContentTypes } from "./content-types/content-types"; import { ContentTypes } from "./content-types/content-types";
import { CoreProperties, IPropertiesOptions } from "./core-properties"; import { CoreProperties, IPropertiesOptions } from "./core-properties";
@ -145,8 +146,12 @@ export class File {
} }
if (options.footnotes) { if (options.footnotes) {
for (const paragraph of options.footnotes) { for (const key in options.footnotes) {
this.footNotes.createFootNote(paragraph); if (!options.footnotes[key]) {
continue;
}
this.footNotes.createFootNote(parseFloat(key), options.footnotes[key].children);
} }
} }

View File

@ -1,11 +1,9 @@
import { XmlAttributeComponent } from "file/xml-components"; import { XmlAttributeComponent } from "file/xml-components";
export interface IFootnoteAttributesProperties { export class FootnoteAttributes extends XmlAttributeComponent<{
readonly type?: string; readonly type?: string;
readonly id: number; readonly id: number;
} }> {
export class FootnoteAttributes extends XmlAttributeComponent<IFootnoteAttributesProperties> {
protected readonly xmlKeys = { protected readonly xmlKeys = {
type: "w:type", type: "w:type",
id: "w:id", id: "w:id",

View File

@ -7,7 +7,11 @@ import { Footnote, FootnoteType } from "./footnote";
describe("Footnote", () => { describe("Footnote", () => {
describe("#constructor", () => { describe("#constructor", () => {
it("should create a footnote with a footnote type", () => { it("should create a footnote with a footnote type", () => {
const footnote = new Footnote(1, FootnoteType.SEPERATOR); const footnote = new Footnote({
id: 1,
type: FootnoteType.SEPERATOR,
children: [],
});
const tree = new Formatter().format(footnote); const tree = new Formatter().format(footnote);
expect(Object.keys(tree)).to.deep.equal(["w:footnote"]); expect(Object.keys(tree)).to.deep.equal(["w:footnote"]);
@ -15,7 +19,10 @@ describe("Footnote", () => {
}); });
it("should create a footnote without a footnote type", () => { it("should create a footnote without a footnote type", () => {
const footnote = new Footnote(1); const footnote = new Footnote({
id: 1,
children: [],
});
const tree = new Formatter().format(footnote); const tree = new Formatter().format(footnote);
expect(Object.keys(tree)).to.deep.equal(["w:footnote"]); expect(Object.keys(tree)).to.deep.equal(["w:footnote"]);

View File

@ -10,18 +10,23 @@ export enum FootnoteType {
} }
export class Footnote extends XmlComponent { export class Footnote extends XmlComponent {
constructor(id: number, type?: FootnoteType) { constructor(options: { readonly id: number; readonly type?: FootnoteType; readonly children: Paragraph[] }) {
super("w:footnote"); super("w:footnote");
this.root.push( this.root.push(
new FootnoteAttributes({ new FootnoteAttributes({
type: type, type: options.type,
id: id, id: options.id,
}), }),
); );
for (let i = 0; i < options.children.length; i++) {
const child = options.children[i];
if (i === 0) {
child.addRunToFront(new FootnoteRefRun());
} }
public add(paragraph: Paragraph): void { this.root.push(child);
paragraph.addRunToFront(new FootnoteRefRun()); }
this.root.push(paragraph);
} }
} }

View File

@ -2,11 +2,9 @@ import { Run } from "file/paragraph/run";
import { Style } from "file/paragraph/run/style"; import { Style } from "file/paragraph/run/style";
import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; import { XmlAttributeComponent, XmlComponent } from "file/xml-components";
export interface IFootNoteReferenceRunAttributesProperties { export class FootNoteReferenceRunAttributes extends XmlAttributeComponent<{
readonly id: number; readonly id: number;
} }> {
export class FootNoteReferenceRunAttributes extends XmlAttributeComponent<IFootNoteReferenceRunAttributesProperties> {
protected readonly xmlKeys = { protected readonly xmlKeys = {
id: "w:id", id: "w:id",
}; };

View File

@ -1,4 +1,5 @@
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { Paragraph } from "../paragraph"; import { Paragraph } from "../paragraph";
import { Footnote, FootnoteType } from "./footnote/footnote"; import { Footnote, FootnoteType } from "./footnote/footnote";
import { ContinuationSeperatorRun } from "./footnote/run/continuation-seperator-run"; import { ContinuationSeperatorRun } from "./footnote/run/continuation-seperator-run";
@ -6,14 +7,9 @@ import { SeperatorRun } from "./footnote/run/seperator-run";
import { FootnotesAttributes } from "./footnotes-attributes"; import { FootnotesAttributes } from "./footnotes-attributes";
export class FootNotes extends XmlComponent { export class FootNotes extends XmlComponent {
// tslint:disable-next-line:readonly-keyword
private currentId: number;
constructor() { constructor() {
super("w:footnotes"); super("w:footnotes");
this.currentId = 1;
this.root.push( this.root.push(
new FootnotesAttributes({ new FootnotesAttributes({
wpc: "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas", wpc: "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
@ -36,8 +32,10 @@ export class FootNotes extends XmlComponent {
}), }),
); );
const begin = new Footnote(-1, FootnoteType.SEPERATOR); const begin = new Footnote({
begin.add( id: -1,
type: FootnoteType.SEPERATOR,
children: [
new Paragraph({ new Paragraph({
spacing: { spacing: {
after: 0, after: 0,
@ -46,11 +44,15 @@ export class FootNotes extends XmlComponent {
}, },
children: [new SeperatorRun()], children: [new SeperatorRun()],
}), }),
); ],
});
this.root.push(begin); this.root.push(begin);
const spacing = new Footnote(0, FootnoteType.CONTINUATION_SEPERATOR); const spacing = new Footnote({
spacing.add( id: 0,
type: FootnoteType.CONTINUATION_SEPERATOR,
children: [
new Paragraph({ new Paragraph({
spacing: { spacing: {
after: 0, after: 0,
@ -59,15 +61,17 @@ export class FootNotes extends XmlComponent {
}, },
children: [new ContinuationSeperatorRun()], children: [new ContinuationSeperatorRun()],
}), }),
); ],
});
this.root.push(spacing); this.root.push(spacing);
} }
public createFootNote(paragraph: Paragraph): void { public createFootNote(id: number, paragraph: Paragraph[]): void {
const footnote = new Footnote(this.currentId); const footnote = new Footnote({
footnote.add(paragraph); id: id,
children: paragraph,
});
this.root.push(footnote); this.root.push(footnote);
this.currentId++;
} }
} }