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

View File

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

View File

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

View File

@ -1,4 +1,5 @@
import * as shortid from "shortid";
import { AppProperties } from "./app-properties/app-properties";
import { ContentTypes } from "./content-types/content-types";
import { CoreProperties, IPropertiesOptions } from "./core-properties";
@ -145,8 +146,12 @@ export class File {
}
if (options.footnotes) {
for (const paragraph of options.footnotes) {
this.footNotes.createFootNote(paragraph);
for (const key in options.footnotes) {
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";
export interface IFootnoteAttributesProperties {
export class FootnoteAttributes extends XmlAttributeComponent<{
readonly type?: string;
readonly id: number;
}
export class FootnoteAttributes extends XmlAttributeComponent<IFootnoteAttributesProperties> {
}> {
protected readonly xmlKeys = {
type: "w:type",
id: "w:id",

View File

@ -7,7 +7,11 @@ import { Footnote, FootnoteType } from "./footnote";
describe("Footnote", () => {
describe("#constructor", () => {
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);
expect(Object.keys(tree)).to.deep.equal(["w:footnote"]);
@ -15,7 +19,10 @@ describe("Footnote", () => {
});
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);
expect(Object.keys(tree)).to.deep.equal(["w:footnote"]);

View File

@ -10,18 +10,23 @@ export enum FootnoteType {
}
export class Footnote extends XmlComponent {
constructor(id: number, type?: FootnoteType) {
constructor(options: { readonly id: number; readonly type?: FootnoteType; readonly children: Paragraph[] }) {
super("w:footnote");
this.root.push(
new FootnoteAttributes({
type: type,
id: id,
type: options.type,
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 {
paragraph.addRunToFront(new FootnoteRefRun());
this.root.push(paragraph);
this.root.push(child);
}
}
}

View File

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

View File

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