Merge pull request #789 from dolanmiu/feat/compat-mode
#627 Add footnotes relationships
This commit is contained in:
@ -1,9 +1,24 @@
|
||||
// Example on how to add hyperlinks to websites
|
||||
// Import from 'docx' rather than '../build' if you install from npm
|
||||
import * as fs from "fs";
|
||||
import { Document, ExternalHyperlink, Footer, Media, Packer, Paragraph, TextRun } from "../build";
|
||||
import { Document, ExternalHyperlink, Footer, FootnoteReferenceRun, Media, Packer, Paragraph, TextRun } from "../build";
|
||||
|
||||
const doc = new Document({});
|
||||
const doc = new Document({
|
||||
footnotes: [
|
||||
new Paragraph({
|
||||
children: [
|
||||
new TextRun("Click here for the "),
|
||||
new ExternalHyperlink({
|
||||
child: new TextRun({
|
||||
text: "Footnotes external hyperlink",
|
||||
style: "Hyperlink",
|
||||
}),
|
||||
link: "http://www.example.com",
|
||||
}),
|
||||
],
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
const image1 = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg"));
|
||||
|
||||
@ -54,6 +69,7 @@ doc.addSection({
|
||||
}),
|
||||
link: "http://www.example.com",
|
||||
}),
|
||||
new FootnoteReferenceRun(1)
|
||||
],
|
||||
}),
|
||||
new Paragraph({
|
||||
|
@ -30,6 +30,7 @@ describe("Compiler", () => {
|
||||
expect(fileNames).to.include("docProps/app.xml");
|
||||
expect(fileNames).to.include("word/numbering.xml");
|
||||
expect(fileNames).to.include("word/footnotes.xml");
|
||||
expect(fileNames).to.include("word/_rels/footnotes.xml.rels");
|
||||
expect(fileNames).to.include("word/settings.xml");
|
||||
expect(fileNames).to.include("word/_rels/document.xml.rels");
|
||||
expect(fileNames).to.include("[Content_Types].xml");
|
||||
|
@ -26,6 +26,7 @@ interface IXmlifyedFileMapping {
|
||||
readonly CustomProperties: IXmlifyedFile;
|
||||
readonly AppProperties: IXmlifyedFile;
|
||||
readonly FootNotes: IXmlifyedFile;
|
||||
readonly FootNotesRelationships: IXmlifyedFile;
|
||||
readonly Settings: IXmlifyedFile;
|
||||
}
|
||||
|
||||
@ -189,9 +190,13 @@ export class Compiler {
|
||||
path: "docProps/app.xml",
|
||||
},
|
||||
FootNotes: {
|
||||
data: xml(this.formatter.format(file.FootNotes, file.Document), prettify),
|
||||
data: xml(this.formatter.format(file.FootNotes.View, file.FootNotes), prettify),
|
||||
path: "word/footnotes.xml",
|
||||
},
|
||||
FootNotesRelationships: {
|
||||
data: xml(this.formatter.format(file.FootNotes.Relationships, file.FootNotes), prettify),
|
||||
path: "word/_rels/footnotes.xml.rels",
|
||||
},
|
||||
Settings: {
|
||||
data: xml(this.formatter.format(file.Settings, file.Document), prettify),
|
||||
path: "word/settings.xml",
|
||||
|
@ -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;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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": [
|
||||
|
@ -12,7 +12,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";
|
||||
@ -54,7 +54,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 customProperties: CustomProperties;
|
||||
@ -79,11 +79,10 @@ export class File {
|
||||
config: [],
|
||||
},
|
||||
);
|
||||
// this.documentWrapper.Relationships = new Relationships();
|
||||
this.fileRelationships = new Relationships();
|
||||
this.customProperties = new CustomProperties(customProperties);
|
||||
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({
|
||||
@ -142,7 +141,7 @@ export class File {
|
||||
|
||||
if (options.footnotes) {
|
||||
for (const paragraph of options.footnotes) {
|
||||
this.footNotes.createFootNote(paragraph);
|
||||
this.footnotesWrapper.View.createFootNote(paragraph);
|
||||
}
|
||||
}
|
||||
|
||||
@ -318,8 +317,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 {
|
||||
|
16
src/file/footnotes-wrapper.spec.ts
Normal file
16
src/file/footnotes-wrapper.spec.ts
Normal 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;
|
||||
});
|
||||
});
|
||||
});
|
21
src/file/footnotes-wrapper.ts
Normal file
21
src/file/footnotes-wrapper.ts
Normal 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;
|
||||
}
|
||||
}
|
@ -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"]);
|
||||
|
@ -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,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
Reference in New Issue
Block a user