From c1cc211c7f84b9a675a8dd86c00b0ffc50ac636d Mon Sep 17 00:00:00 2001 From: Rowan Cockett Date: Fri, 27 Aug 2021 16:53:11 -0600 Subject: [PATCH] Add children to hyperlink --- src/file/paragraph/links/hyperlink.spec.ts | 22 +++++++++++++--------- src/file/paragraph/links/hyperlink.ts | 10 ++++++---- src/file/paragraph/paragraph.spec.ts | 2 +- src/file/paragraph/paragraph.ts | 2 +- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/file/paragraph/links/hyperlink.spec.ts b/src/file/paragraph/links/hyperlink.spec.ts index 50d6aa76aa..8b6fefb691 100644 --- a/src/file/paragraph/links/hyperlink.spec.ts +++ b/src/file/paragraph/links/hyperlink.spec.ts @@ -10,10 +10,12 @@ describe("ConcreteHyperlink", () => { beforeEach(() => { hyperlink = new ConcreteHyperlink( - new TextRun({ - text: "https://example.com", - style: "Hyperlink", - }), + [ + new TextRun({ + text: "https://example.com", + style: "Hyperlink", + }), + ], "superid", ); }); @@ -42,10 +44,12 @@ describe("ConcreteHyperlink", () => { describe("with optional anchor parameter", () => { beforeEach(() => { hyperlink = new ConcreteHyperlink( - new TextRun({ - text: "Anchor Text", - style: "Hyperlink", - }), + [ + new TextRun({ + text: "Anchor Text", + style: "Hyperlink", + }), + ], "superid2", "anchor", ); @@ -78,7 +82,7 @@ describe("ExternalHyperlink", () => { describe("#constructor()", () => { it("should create", () => { const externalHyperlink = new ExternalHyperlink({ - child: new TextRun("test"), + children: [new TextRun("test")], link: "http://www.google.com", }); diff --git a/src/file/paragraph/links/hyperlink.ts b/src/file/paragraph/links/hyperlink.ts index 7ced10d07c..7ee4409298 100644 --- a/src/file/paragraph/links/hyperlink.ts +++ b/src/file/paragraph/links/hyperlink.ts @@ -13,7 +13,7 @@ export enum HyperlinkType { export class ConcreteHyperlink extends XmlComponent { public readonly linkId: string; - constructor(child: ParagraphChild, relationshipId: string, anchor?: string) { + constructor(children: ParagraphChild[], relationshipId: string, anchor?: string) { super("w:hyperlink"); this.linkId = relationshipId; @@ -26,16 +26,18 @@ export class ConcreteHyperlink extends XmlComponent { const attributes = new HyperlinkAttributes(props); this.root.push(attributes); - this.root.push(child); + children.forEach((child) => { + this.root.push(child); + }); } } export class InternalHyperlink extends ConcreteHyperlink { constructor(options: { readonly child: ParagraphChild; readonly anchor: string }) { - super(options.child, uniqueId(), options.anchor); + super([options.child], uniqueId(), options.anchor); } } export class ExternalHyperlink { - constructor(public readonly options: { readonly child: ParagraphChild; readonly link: string }) {} + constructor(public readonly options: { readonly children: ParagraphChild[]; readonly link: string }) {} } diff --git a/src/file/paragraph/paragraph.spec.ts b/src/file/paragraph/paragraph.spec.ts index 90c6c8bedd..ab06d59ac1 100644 --- a/src/file/paragraph/paragraph.spec.ts +++ b/src/file/paragraph/paragraph.spec.ts @@ -927,7 +927,7 @@ describe("Paragraph", () => { const paragraph = new Paragraph({ children: [ new ExternalHyperlink({ - child: new TextRun("test"), + children: [new TextRun("test")], link: "http://www.google.com", }), ], diff --git a/src/file/paragraph/paragraph.ts b/src/file/paragraph/paragraph.ts index afd9da1b7d..2f73f7cc65 100644 --- a/src/file/paragraph/paragraph.ts +++ b/src/file/paragraph/paragraph.ts @@ -75,7 +75,7 @@ export class Paragraph extends XmlComponent { for (const element of this.root) { if (element instanceof ExternalHyperlink) { const index = this.root.indexOf(element); - const concreteHyperlink = new ConcreteHyperlink(element.options.child, uniqueId()); + const concreteHyperlink = new ConcreteHyperlink(element.options.children, uniqueId()); context.viewWrapper.Relationships.createRelationship( concreteHyperlink.linkId, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",