Make Bookmark declarative

This commit is contained in:
Dolan Miu
2019-12-21 03:59:40 +00:00
parent ee5425bef7
commit b83d2c388f
4 changed files with 17 additions and 38 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 { Document, HeadingLevel, HyperlinkRef, HyperlinkType, Packer, PageBreak, Paragraph } from "../build"; import { Bookmark, Document, HeadingLevel, HyperlinkRef, HyperlinkType, Packer, PageBreak, Paragraph } 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.";
@ -18,14 +18,11 @@ const doc = new Document({
}, },
}); });
// First create the bookmark
const bookmark = doc.createBookmark("myAnchorId", "Lorem Ipsum");
doc.addSection({ doc.addSection({
children: [ children: [
new Paragraph({ new Paragraph({
heading: HeadingLevel.HEADING_1, heading: HeadingLevel.HEADING_1,
children: [bookmark], children: [new Bookmark("myAnchorId", "Lorem Ipsum")],
}), }),
new Paragraph("\n"), new Paragraph("\n"),
new Paragraph(LOREM_IPSUM), new Paragraph(LOREM_IPSUM),

View File

@ -17,7 +17,7 @@ import { Footer, Header } from "./header";
import { HeaderWrapper, IDocumentHeader } from "./header-wrapper"; import { HeaderWrapper, IDocumentHeader } from "./header-wrapper";
import { Media } from "./media"; import { Media } from "./media";
import { Numbering } from "./numbering"; import { Numbering } from "./numbering";
import { Bookmark, Hyperlink, HyperlinkRef, HyperlinkType, Paragraph } from "./paragraph"; import { Hyperlink, HyperlinkRef, HyperlinkType, Paragraph } from "./paragraph";
import { Relationships } from "./relationships"; import { Relationships } from "./relationships";
import { TargetModeType } from "./relationships/relationship/relationship"; import { TargetModeType } from "./relationships/relationship/relationship";
import { Settings } from "./settings"; import { Settings } from "./settings";
@ -172,10 +172,6 @@ export class File {
} }
} }
public createBookmark(name: string, text: string = name): Bookmark {
return new Bookmark(name, text, this.docRelationships.RelationshipCount);
}
public addSection({ public addSection({
headers = { default: new Header() }, headers = { default: new Header() },
footers = { default: new Header() }, footers = { default: new Header() },

View File

@ -1,4 +1,4 @@
import { assert } from "chai"; import { assert, expect } from "chai";
import { Utility } from "tests/utility"; import { Utility } from "tests/utility";
@ -8,7 +8,7 @@ describe("Bookmark", () => {
let bookmark: Bookmark; let bookmark: Bookmark;
beforeEach(() => { beforeEach(() => {
bookmark = new Bookmark("anchor", "Internal Link", 0); bookmark = new Bookmark("anchor", "Internal Link");
}); });
it("should create a bookmark with three root elements", () => { it("should create a bookmark with three root elements", () => {
@ -21,11 +21,8 @@ describe("Bookmark", () => {
it("should create a bookmark with the correct attributes on the bookmark start element", () => { it("should create a bookmark with the correct attributes on the bookmark start element", () => {
const newJson = Utility.jsonify(bookmark); const newJson = Utility.jsonify(bookmark);
const attributes = {
name: "anchor", assert.equal(newJson.start.root[0].root.name, "anchor");
id: "1",
};
assert.equal(JSON.stringify(newJson.start.root[0].root), JSON.stringify(attributes));
}); });
it("should create a bookmark with the correct attributes on the text element", () => { it("should create a bookmark with the correct attributes on the text element", () => {
@ -35,9 +32,6 @@ describe("Bookmark", () => {
it("should create a bookmark with the correct attributes on the bookmark end element", () => { it("should create a bookmark with the correct attributes on the bookmark end element", () => {
const newJson = Utility.jsonify(bookmark); const newJson = Utility.jsonify(bookmark);
const attributes = { expect(newJson.end.root[0].root.id).to.be.a("string");
id: "1",
};
assert.equal(JSON.stringify(newJson.end.root[0].root), JSON.stringify(attributes));
}); });
}); });

View File

@ -1,5 +1,6 @@
// http://officeopenxml.com/WPbookmark.php // http://officeopenxml.com/WPbookmark.php
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import * as shortid from "shortid";
import { TextRun } from "../run"; import { TextRun } from "../run";
import { BookmarkEndAttributes, BookmarkStartAttributes } from "./bookmark-attributes"; import { BookmarkEndAttributes, BookmarkStartAttributes } from "./bookmark-attributes";
@ -8,46 +9,37 @@ export class BookmarkRef {
} }
export class Bookmark { export class Bookmark {
public readonly linkId: number;
public readonly start: BookmarkStart; public readonly start: BookmarkStart;
public readonly text: TextRun; public readonly text: TextRun;
public readonly end: BookmarkEnd; public readonly end: BookmarkEnd;
constructor(name: string, text: string, relationshipsCount: number) { constructor(name: string, text: string) {
this.linkId = relationshipsCount + 1; const linkId = shortid.generate().toLowerCase();
this.start = new BookmarkStart(name, this.linkId); this.start = new BookmarkStart(name, linkId);
this.text = new TextRun(text); this.text = new TextRun(text);
this.end = new BookmarkEnd(this.linkId); this.end = new BookmarkEnd(linkId);
} }
} }
export class BookmarkStart extends XmlComponent { export class BookmarkStart extends XmlComponent {
public readonly linkId: number; constructor(name: string, linkId: string) {
constructor(name: string, relationshipsCount: number) {
super("w:bookmarkStart"); super("w:bookmarkStart");
this.linkId = relationshipsCount;
const id = `${this.linkId}`;
const attributes = new BookmarkStartAttributes({ const attributes = new BookmarkStartAttributes({
name, name,
id, id: linkId,
}); });
this.root.push(attributes); this.root.push(attributes);
} }
} }
export class BookmarkEnd extends XmlComponent { export class BookmarkEnd extends XmlComponent {
public readonly linkId: number; constructor(linkId: string) {
constructor(relationshipsCount: number) {
super("w:bookmarkEnd"); super("w:bookmarkEnd");
this.linkId = relationshipsCount;
const id = `${this.linkId}`;
const attributes = new BookmarkEndAttributes({ const attributes = new BookmarkEndAttributes({
id, id: linkId,
}); });
this.root.push(attributes); this.root.push(attributes);
} }