Add ability to add multiple text runs to a bookmark

This commit is contained in:
Dolan
2020-01-01 22:54:42 +00:00
parent 9c11653313
commit db7c4da609
7 changed files with 44 additions and 13 deletions

View File

@ -1,7 +1,7 @@
// This demo shows how to create bookmarks then link to them with internal hyperlinks // 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 from 'docx' rather than '../build' if you install from npm
import * as fs from "fs"; 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 = 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."; "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: [ children: [
new Paragraph({ new Paragraph({
heading: HeadingLevel.HEADING_1, 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("\n"),
new Paragraph(LOREM_IPSUM), new Paragraph(LOREM_IPSUM),

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "docx", "name": "docx",
"version": "5.0.0-rc7", "version": "5.0.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -5,7 +5,7 @@ import { Formatter } from "export/formatter";
import { File } from "./file"; import { File } from "./file";
import { Footer, Header } from "./header"; import { Footer, Header } from "./header";
import { HyperlinkRef, Paragraph } from "./paragraph"; import { HyperlinkRef, HyperlinkType, Paragraph } from "./paragraph";
import { Table, TableCell, TableRow } from "./table"; import { Table, TableCell, TableRow } from "./table";
import { TableOfContents } from "./table-of-contents"; import { TableOfContents } from "./table-of-contents";
@ -241,6 +241,20 @@ describe("File", () => {
expect(spy.called).to.equal(true); 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", () => { describe("#HyperlinkCache", () => {

View File

@ -2,13 +2,17 @@ import { assert, expect } from "chai";
import { Utility } from "tests/utility"; import { Utility } from "tests/utility";
import { TextRun } from "../run";
import { Bookmark } from "./bookmark"; import { Bookmark } from "./bookmark";
describe("Bookmark", () => { describe("Bookmark", () => {
let bookmark: Bookmark; let bookmark: Bookmark;
beforeEach(() => { 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", () => { it("should create a bookmark with three root elements", () => {

View File

@ -6,24 +6,24 @@ import { BookmarkEndAttributes, BookmarkStartAttributes } from "./bookmark-attri
export class Bookmark { export class Bookmark {
public readonly start: BookmarkStart; public readonly start: BookmarkStart;
public readonly text: TextRun; public readonly children: TextRun[];
public readonly end: BookmarkEnd; public readonly end: BookmarkEnd;
constructor(name: string, text: string) { constructor(options: { readonly id: string; readonly children: TextRun[] }) {
const linkId = shortid.generate().toLowerCase(); const linkId = shortid.generate().toLowerCase();
this.start = new BookmarkStart(name, linkId); this.start = new BookmarkStart(options.id, linkId);
this.text = new TextRun(text); this.children = options.children;
this.end = new BookmarkEnd(linkId); this.end = new BookmarkEnd(linkId);
} }
} }
export class BookmarkStart extends XmlComponent { export class BookmarkStart extends XmlComponent {
constructor(name: string, linkId: string) { constructor(id: string, linkId: string) {
super("w:bookmarkStart"); super("w:bookmarkStart");
const attributes = new BookmarkStartAttributes({ const attributes = new BookmarkStartAttributes({
name, name: id,
id: linkId, id: linkId,
}); });
this.root.push(attributes); this.root.push(attributes);

View File

@ -8,6 +8,7 @@ import { EMPTY_OBJECT } from "file/xml-components";
import { AlignmentType, HeadingLevel, LeaderType, PageBreak, TabStopPosition, TabStopType } from "./formatting"; import { AlignmentType, HeadingLevel, LeaderType, PageBreak, TabStopPosition, TabStopType } from "./formatting";
import { Bookmark } from "./links"; import { Bookmark } from "./links";
import { Paragraph } from "./paragraph"; import { Paragraph } from "./paragraph";
import { TextRun } from "./run";
describe("Paragraph", () => { describe("Paragraph", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
@ -646,7 +647,12 @@ describe("Paragraph", () => {
return "test-unique-id"; return "test-unique-id";
}); });
const paragraph = new Paragraph({ 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); const tree = new Formatter().format(paragraph);
expect(tree).to.deep.equal({ expect(tree).to.deep.equal({

View File

@ -150,7 +150,9 @@ export class Paragraph extends XmlComponent {
for (const child of options.children) { for (const child of options.children) {
if (child instanceof Bookmark) { if (child instanceof Bookmark) {
this.root.push(child.start); 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); this.root.push(child.end);
continue; continue;
} }