Add ability to add multiple text runs to a bookmark
This commit is contained in:
@ -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", () => {
|
||||
|
@ -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", () => {
|
||||
|
@ -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);
|
||||
|
@ -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({
|
||||
|
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user