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

@ -17,7 +17,7 @@ import { Footer, Header } from "./header";
import { HeaderWrapper, IDocumentHeader } from "./header-wrapper";
import { Media } from "./media";
import { Numbering } from "./numbering";
import { Bookmark, Hyperlink, HyperlinkRef, HyperlinkType, Paragraph } from "./paragraph";
import { Hyperlink, HyperlinkRef, HyperlinkType, Paragraph } from "./paragraph";
import { Relationships } from "./relationships";
import { TargetModeType } from "./relationships/relationship/relationship";
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({
headers = { 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";
@ -8,7 +8,7 @@ describe("Bookmark", () => {
let bookmark: Bookmark;
beforeEach(() => {
bookmark = new Bookmark("anchor", "Internal Link", 0);
bookmark = new Bookmark("anchor", "Internal Link");
});
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", () => {
const newJson = Utility.jsonify(bookmark);
const attributes = {
name: "anchor",
id: "1",
};
assert.equal(JSON.stringify(newJson.start.root[0].root), JSON.stringify(attributes));
assert.equal(newJson.start.root[0].root.name, "anchor");
});
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", () => {
const newJson = Utility.jsonify(bookmark);
const attributes = {
id: "1",
};
assert.equal(JSON.stringify(newJson.end.root[0].root), JSON.stringify(attributes));
expect(newJson.end.root[0].root.id).to.be.a("string");
});
});

View File

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