#627 Add footnotes relationships

This commit is contained in:
Dolan Miu
2021-03-01 23:35:52 +00:00
parent c6e9696be0
commit f90e84a88d
12 changed files with 107 additions and 66 deletions

View File

@ -1,50 +1,16 @@
import { expect } from "chai";
import * as sinon from "sinon";
import { FooterWrapper } from "./footer-wrapper";
import { Media } from "./media";
import { Paragraph } from "./paragraph";
import { Table, TableCell, TableRow } from "./table";
import { DocumentWrapper } from "./document-wrapper";
describe("FooterWrapper", () => {
describe("#add", () => {
it("should call the underlying footer's addParagraph", () => {
const file = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(file.View, "add");
file.add(new Paragraph({}));
describe("DocumentWrapper", () => {
describe("#constructor", () => {
it("should create", () => {
const file = new DocumentWrapper({ background: {} });
expect(spy.called).to.equal(true);
});
it("should call the underlying footer's addParagraph", () => {
const file = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(file.View, "add");
file.add(
new Table({
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph("hello")],
}),
],
}),
],
}),
);
expect(spy.called).to.equal(true);
});
});
describe("#addChildElement", () => {
it("should call the underlying footer's addChildElement", () => {
const file = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(file.View, "addChildElement");
// tslint:disable-next-line:no-any
file.addChildElement({} as any);
expect(spy.called).to.equal(true);
// tslint:disable-next-line: no-unused-expression
expect(file.View).to.be.ok;
// tslint:disable-next-line: no-unused-expression
expect(file.Relationships).to.be.ok;
});
});
});

View File

@ -1,10 +1,11 @@
import { Document, IDocumentOptions } from "./document";
import { Footer } from "./footer";
import { FootNotes } from "./footnotes";
import { Header } from "./header/header";
import { Relationships } from "./relationships";
export interface IViewWrapper {
readonly View: Document | Footer | Header;
readonly View: Document | Footer | Header | FootNotes;
readonly Relationships: Relationships;
}

View File

@ -242,7 +242,7 @@ describe("File", () => {
footnotes: [new Paragraph("hello")],
});
const tree = new Formatter().format(wrapper.FootNotes);
const tree = new Formatter().format(wrapper.FootNotes.View);
expect(tree).to.deep.equal({
"w:footnotes": [

View File

@ -11,7 +11,7 @@ import {
import { IPageMarginAttributes } from "./document/body/section-properties/page-margin/page-margin-attributes";
import { IFileProperties } from "./file-properties";
import { FooterWrapper, IDocumentFooter } from "./footer-wrapper";
import { FootNotes } from "./footnotes";
import { FootnotesWrapper } from "./footnotes-wrapper";
import { Footer, Header } from "./header";
import { HeaderWrapper, IDocumentHeader } from "./header-wrapper";
import { Media } from "./media";
@ -53,7 +53,7 @@ export class File {
private readonly numbering: Numbering;
private readonly media: Media;
private readonly fileRelationships: Relationships;
private readonly footNotes: FootNotes;
private readonly footnotesWrapper: FootnotesWrapper;
private readonly settings: Settings;
private readonly contentTypes: ContentTypes;
private readonly appProperties: AppProperties;
@ -76,10 +76,9 @@ export class File {
config: [],
},
);
// this.documentWrapper.Relationships = new Relationships();
this.fileRelationships = new Relationships();
this.appProperties = new AppProperties();
this.footNotes = new FootNotes();
this.footnotesWrapper = new FootnotesWrapper();
this.contentTypes = new ContentTypes();
this.documentWrapper = new DocumentWrapper({ background: options.background || {} });
this.settings = new Settings({
@ -138,7 +137,7 @@ export class File {
if (options.footnotes) {
for (const paragraph of options.footnotes) {
this.footNotes.createFootNote(paragraph);
this.footnotesWrapper.View.createFootNote(paragraph);
}
}
@ -305,8 +304,8 @@ export class File {
return this.appProperties;
}
public get FootNotes(): FootNotes {
return this.footNotes;
public get FootNotes(): FootnotesWrapper {
return this.footnotesWrapper;
}
public get Settings(): Settings {

View File

@ -0,0 +1,16 @@
import { expect } from "chai";
import { FootnotesWrapper } from "./footnotes-wrapper";
describe("FootnotesWrapper", () => {
describe("#constructor", () => {
it("should create", () => {
const file = new FootnotesWrapper();
// tslint:disable-next-line: no-unused-expression
expect(file.View).to.be.ok;
// tslint:disable-next-line: no-unused-expression
expect(file.Relationships).to.be.ok;
});
});
});

View File

@ -0,0 +1,21 @@
import { IViewWrapper } from "./document-wrapper";
import { FootNotes } from "./footnotes/footnotes";
import { Relationships } from "./relationships";
export class FootnotesWrapper implements IViewWrapper {
private readonly footnotess: FootNotes;
private readonly relationships: Relationships;
constructor() {
this.footnotess = new FootNotes();
this.relationships = new Relationships();
}
public get View(): FootNotes {
return this.footnotess;
}
public get Relationships(): Relationships {
return this.relationships;
}
}

View File

@ -7,7 +7,10 @@ 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,
});
const tree = new Formatter().format(footnote);
expect(Object.keys(tree)).to.deep.equal(["w:footnote"]);
@ -15,7 +18,9 @@ describe("Footnote", () => {
});
it("should create a footnote without a footnote type", () => {
const footnote = new Footnote(1);
const footnote = new Footnote({
id: 1,
});
const tree = new Formatter().format(footnote);
expect(Object.keys(tree)).to.deep.equal(["w:footnote"]);

View File

@ -9,13 +9,18 @@ export enum FootnoteType {
CONTINUATION_SEPERATOR = "continuationSeparator",
}
export interface IFootnoteOptions {
readonly id: number;
readonly type?: FootnoteType;
}
export class Footnote extends XmlComponent {
constructor(id: number, type?: FootnoteType) {
constructor(options: IFootnoteOptions) {
super("w:footnote");
this.root.push(
new FootnoteAttributes({
type: type,
id: id,
type: options.type,
id: options.id,
}),
);
}

View File

@ -36,7 +36,10 @@ export class FootNotes extends XmlComponent {
}),
);
const begin = new Footnote(-1, FootnoteType.SEPERATOR);
const begin = new Footnote({
id: -1,
type: FootnoteType.SEPERATOR,
});
begin.add(
new Paragraph({
spacing: {
@ -49,7 +52,10 @@ export class FootNotes extends XmlComponent {
);
this.root.push(begin);
const spacing = new Footnote(0, FootnoteType.CONTINUATION_SEPERATOR);
const spacing = new Footnote({
id: 0,
type: FootnoteType.CONTINUATION_SEPERATOR,
});
spacing.add(
new Paragraph({
spacing: {
@ -64,7 +70,7 @@ export class FootNotes extends XmlComponent {
}
public createFootNote(paragraph: Paragraph): void {
const footnote = new Footnote(this.currentId);
const footnote = new Footnote({ id: this.currentId });
footnote.add(paragraph);
this.root.push(footnote);