# Upgrade Guide for Bookmark APIs
This guide is intended to help developers upgrade from an older version of the Bookmark APIs to the newest version. Starting from version 10.0.0, we have refactored the Bookmark APIs, which provides more powerful features and performance improvements. Next, we will further discuss which old Bookmark APIs are being deprecated and how to migrate to the new APIs.
# Confirm the current version of the API in use
If your application was developed based on versions prior to 10.0.0, when upgrading to versions after 10.0.0, please note that you will need to search for relevant API calls in your code, or check if the old bookmark component has been used in your component templates. The names of the old APIs and components will be listed subsequently.
# Deprecated APIs
The table below lists the deprecated bookmark APIs and alternative solutions.
Below are the deprecated bookmark UIExtension components and their alternatives:
Old Component | Description | Alternative Component |
---|---|---|
<bookmark-sidebar-panel> | Sidebar bookmark panel component | <bookmark-v2:sidebar-panel> |
<bookmark-contextmenu> | Bookmark contextmenu component | <bookmark-v2:bookmark-contextmenu> |
When these old components are replaced with new ones, the previously registered bookmark events will not be triggered. At this time, it is necessary to replace the registration method of these events. Examples will be provided later to illustrate.
If your application requires a custom bookmark UI, please refer to Customize bookmark.
# Example
The following will provide examples to demonstrate how to use the new bookmark APIs to replace the old ones, as well as compare the differences between the two versions.
# Example 1 - Listen to the event of adding a bookmark
This example demonstrates how to listen to the event of adding a bookmark, and modify the properties of the bookmark through the instance of BookmarkDataService (opens new window).
After running the above example and opening a document, then adding a new bookmark through the bookmark panel, you will find that the font color of the new bookmark is set to red, and the font style is set to bold and italic.
For versions 9.*
and earlier, bookmark events are registered in the following way:
pdfui.addViewerEventListener(PDFViewCtrl.ViewerEvents.bookmarkAdded, bookmark => {
// Here operates the bookmark object
})
# Example 2 - Get the nodes of bookmark tree
In versions 9.*
and earlier, bookmarks were loaded into memory all at once and inserted into the DOM tree, loading speed could be slow, even to the point of lag, when there were many bookmarks. After refactoring, bookmarks are loaded and rendered based on the number of bookmarks visible to the user, which speeds up the loading process. Furthermore, bookmarks are rendered through the TreeComponent (opens new window) component, reserving some space for user customization. The following example shows how to get the nodes of the bookmark tree and modify the node styles:
Unlike the old version, all DOM nodes of the old bookmarks can be obtained through querySelector
once the bookmarks were loaded. However, the new version requires listening to the create-tree-node
event to get these nodes.
# Example 3 - Support for undo/redo
In version 10.0.0, a BookmarkUIService (opens new window) instance is provided, which is available only when the application layer is based on the implementation of UIExtension. It offers the same API as BookmarkDataService (opens new window), but supports undo/redo operations.
This example inserts a custom menu item at the end of the bookmark contextmenu. Clicking this item allows you to set the currently selected bookmark to red. Moreover, this action can be undone or redone using Ctrl+Z
/Ctrl+Y
. Please note, to enable the undo/redo feature, you need to include libPath + '/uix-addons/undo-redo'
in the addons
configuration when initializing PDFUI, or you can simply configure addons: libPath + '/uix-addons/allInOne.js'
to load all addons.