From db7c4da609190950f7baab788c26dedbffcd45aa Mon Sep 17 00:00:00 2001 From: Dolan Date: Wed, 1 Jan 2020 22:54:42 +0000 Subject: [PATCH] Add ability to add multiple text runs to a bookmark --- demo/21-bookmarks.ts | 9 +++++++-- package-lock.json | 2 +- src/file/file.spec.ts | 16 +++++++++++++++- src/file/paragraph/links/bookmark.spec.ts | 6 +++++- src/file/paragraph/links/bookmark.ts | 12 ++++++------ src/file/paragraph/paragraph.spec.ts | 8 +++++++- src/file/paragraph/paragraph.ts | 4 +++- 7 files changed, 44 insertions(+), 13 deletions(-) diff --git a/demo/21-bookmarks.ts b/demo/21-bookmarks.ts index 1ad50eb9b1..689ff30ae4 100644 --- a/demo/21-bookmarks.ts +++ b/demo/21-bookmarks.ts @@ -1,7 +1,7 @@ // This demo shows how to create bookmarks then link to them with internal hyperlinks // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { Bookmark, Document, HeadingLevel, HyperlinkRef, HyperlinkType, Packer, PageBreak, Paragraph } from "../build"; +import { Bookmark, Document, HeadingLevel, HyperlinkRef, HyperlinkType, Packer, PageBreak, Paragraph, TextRun } from "../build"; const LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam mi velit, convallis convallis scelerisque nec, faucibus nec leo. Phasellus at posuere mauris, tempus dignissim velit. Integer et tortor dolor. Duis auctor efficitur mattis. Vivamus ut metus accumsan tellus auctor sollicitudin venenatis et nibh. Cras quis massa ac metus fringilla venenatis. Proin rutrum mauris purus, ut suscipit magna consectetur id. Integer consectetur sollicitudin ante, vitae faucibus neque efficitur in. Praesent ultricies nibh lectus. Mauris pharetra id odio eget iaculis. Duis dictum, risus id pellentesque rutrum, lorem quam malesuada massa, quis ullamcorper turpis urna a diam. Cras vulputate metus vel massa porta ullamcorper. Etiam porta condimentum nulla nec tristique. Sed nulla urna, pharetra non tortor sed, sollicitudin molestie diam. Maecenas enim leo, feugiat eget vehicula id, sollicitudin vitae ante."; @@ -22,7 +22,12 @@ doc.addSection({ children: [ new Paragraph({ heading: HeadingLevel.HEADING_1, - children: [new Bookmark("myAnchorId", "Lorem Ipsum")], + children: [ + new Bookmark({ + id: "myAnchorId", + children: [new TextRun("Lorem Ipsum")], + }), + ], }), new Paragraph("\n"), new Paragraph(LOREM_IPSUM), diff --git a/package-lock.json b/package-lock.json index 792ca7e14b..974c0fcb0d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "docx", - "version": "5.0.0-rc7", + "version": "5.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/file/file.spec.ts b/src/file/file.spec.ts index 8864f62d25..d9beb46a3c 100644 --- a/src/file/file.spec.ts +++ b/src/file/file.spec.ts @@ -5,7 +5,7 @@ import { Formatter } from "export/formatter"; import { File } from "./file"; import { Footer, Header } from "./header"; -import { HyperlinkRef, Paragraph } from "./paragraph"; +import { HyperlinkRef, HyperlinkType, Paragraph } from "./paragraph"; import { Table, TableCell, TableRow } from "./table"; import { TableOfContents } from "./table-of-contents"; @@ -241,6 +241,20 @@ describe("File", () => { expect(spy.called).to.equal(true); }); + + it.only("should create hyperlinks", () => { + const wrapper = new File({ + hyperlinks: { + myHyperLink: { + link: "test.com", + text: "test", + type: HyperlinkType.EXTERNAL, + }, + }, + }); + + expect(wrapper.HyperlinkCache.myHyperLink).to.not.be.undefined(""); + }); }); describe("#HyperlinkCache", () => { diff --git a/src/file/paragraph/links/bookmark.spec.ts b/src/file/paragraph/links/bookmark.spec.ts index fe342fc374..7975fefc96 100644 --- a/src/file/paragraph/links/bookmark.spec.ts +++ b/src/file/paragraph/links/bookmark.spec.ts @@ -2,13 +2,17 @@ import { assert, expect } from "chai"; import { Utility } from "tests/utility"; +import { TextRun } from "../run"; import { Bookmark } from "./bookmark"; describe("Bookmark", () => { let bookmark: Bookmark; beforeEach(() => { - bookmark = new Bookmark("anchor", "Internal Link"); + bookmark = new Bookmark({ + id: "anchor", + children: [new TextRun("Internal Link")], + }); }); it("should create a bookmark with three root elements", () => { diff --git a/src/file/paragraph/links/bookmark.ts b/src/file/paragraph/links/bookmark.ts index 261a756864..cf217155a2 100644 --- a/src/file/paragraph/links/bookmark.ts +++ b/src/file/paragraph/links/bookmark.ts @@ -6,24 +6,24 @@ import { BookmarkEndAttributes, BookmarkStartAttributes } from "./bookmark-attri export class Bookmark { public readonly start: BookmarkStart; - public readonly text: TextRun; + public readonly children: TextRun[]; public readonly end: BookmarkEnd; - constructor(name: string, text: string) { + constructor(options: { readonly id: string; readonly children: TextRun[] }) { const linkId = shortid.generate().toLowerCase(); - this.start = new BookmarkStart(name, linkId); - this.text = new TextRun(text); + this.start = new BookmarkStart(options.id, linkId); + this.children = options.children; this.end = new BookmarkEnd(linkId); } } export class BookmarkStart extends XmlComponent { - constructor(name: string, linkId: string) { + constructor(id: string, linkId: string) { super("w:bookmarkStart"); const attributes = new BookmarkStartAttributes({ - name, + name: id, id: linkId, }); this.root.push(attributes); diff --git a/src/file/paragraph/paragraph.spec.ts b/src/file/paragraph/paragraph.spec.ts index cdbe99c7d8..2bbe3c79b1 100644 --- a/src/file/paragraph/paragraph.spec.ts +++ b/src/file/paragraph/paragraph.spec.ts @@ -8,6 +8,7 @@ import { EMPTY_OBJECT } from "file/xml-components"; import { AlignmentType, HeadingLevel, LeaderType, PageBreak, TabStopPosition, TabStopType } from "./formatting"; import { Bookmark } from "./links"; import { Paragraph } from "./paragraph"; +import { TextRun } from "./run"; describe("Paragraph", () => { describe("#constructor()", () => { @@ -646,7 +647,12 @@ describe("Paragraph", () => { return "test-unique-id"; }); const paragraph = new Paragraph({ - children: [new Bookmark("test-id", "test")], + children: [ + new Bookmark({ + id: "test-id", + children: [new TextRun("test")], + }), + ], }); const tree = new Formatter().format(paragraph); expect(tree).to.deep.equal({ diff --git a/src/file/paragraph/paragraph.ts b/src/file/paragraph/paragraph.ts index c97d8d34fd..a61e827be6 100644 --- a/src/file/paragraph/paragraph.ts +++ b/src/file/paragraph/paragraph.ts @@ -150,7 +150,9 @@ export class Paragraph extends XmlComponent { for (const child of options.children) { if (child instanceof Bookmark) { this.root.push(child.start); - this.root.push(child.text); + for (const textRun of child.children) { + this.root.push(textRun); + } this.root.push(child.end); continue; }