docxml
Version:
TypeScript (component) library for building and parsing a DOCX file
38 lines (37 loc) • 1.63 kB
JavaScript
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _Bookmarks_bookmarks;
import { NumberMap } from './NumberMap.js';
export class Bookmarks {
constructor() {
_Bookmarks_bookmarks.set(this, new NumberMap(0));
}
/**
* Marks a unique identifier as taken.
*
* @todo When loading an existing document, bookmarks are not registered from it yet.
*/
registerIdentifier(id, name) {
if (__classPrivateFieldGet(this, _Bookmarks_bookmarks, "f").has(id)) {
throw new Error(`Bookmark with identifier "${id}" already exists.`);
}
__classPrivateFieldGet(this, _Bookmarks_bookmarks, "f").set(id, name || null);
}
/**
* Create a unique ID and name for a new bookmark.
*
* @remarks
* Not using a GUID because this causes Word to not make the link clickable. A much shorter
* identifier works as expected.
*/
create() {
const id = __classPrivateFieldGet(this, _Bookmarks_bookmarks, "f").getNextAvailableKey();
const name = `__docxml_bookmark_${id}`;
this.registerIdentifier(id);
return { id, name };
}
}
_Bookmarks_bookmarks = new WeakMap();