Saving User Selected DisplayMode

I’m attempting to save the FitMode, Zoom, DisplayMode and Them chosen by the user into our user settings/preference, so that they can be restored when a new document is chosen - and persisted between usage of our application.

I’ve got the FitMode and Zoom working, but I am having trouble with DisplayMode. I’ve sent a separate issue to this group regarding the theme.

My existing code to retrieve the values is as follows:

setupEventListeners() {
var self = this;
self.docViewer.on(“fitModeUpdated”, function (fitMode: any) {
self.docViewer.defaults.FitMode = fitMode;
self.fitMode = JSONfn.stringify(fitMode);
});
self.docViewer.on(“displayModeUpdated”, function () {
let displayMode = self.docViewer.getDisplayModeManager().getDisplayMode();
let displayModeDeepClone = clone(displayMode);
self.displayMode = displayModeDeepClone;//JSONfn.stringify(displayMode);
});
self.docViewer.on(“zoomUpdated”, function (zoom: any) {
self.docViewer.defaults.Zoom = zoom;
self.zoom = zoom;
});
}

I store these values in PouchDb and retrieve them and set them when a new document is selected.

setViewerDefaults() {
let self = this;
this.dataService.getSettings().then((settingsDoc: any) => {
if (settingsDoc.fitMode) {
var fitMode = JSONfn.parse(settingsDoc.fitMode);
//self.docViewer.defaults.FitMode = fitMode;
self.docViewer.setFitMode(fitMode);
}
if (settingsDoc.zoom) {
//self.docViewer.defaults.zoom = settingsDoc.zoom;
self.docViewer.zoomTo(settingsDoc.zoom);
}
if (settingsDoc.displayMode) {
//var displayMode = JSONfn.parse(settingsDoc.displayMode);
//self.docViewer.defaults.displayMode = settingsDoc.displayMode;
self.docViewer.getDisplayModeManager().setDisplayMode(settingsDoc.displayMode);
}
});

The problem that I am having with the DisplayMode is that if I try to stringify it, or store it as is, I get an error about circular references. So then I tried to clone it, but now I get the following error:

TypeError: Buffer.isBuffer is not a function
at _clone (clone.js:106)

Can you recommend the right way to store and retrieve the display mode.

Kind regards,

David

Hi David,

DisplayMode object contains other information as well and as such is not meant to be serialized to JSON. However you can store the display mode string to your database and then use setLayoutMode (https://www.pdftron.com/api/web/WebViewerInstance.html#setLayoutMode) to set it back.

Hope this helps you forward.

Best Regards,
Jussi Nieminen
Senior Software Developer
PDFTron Systems, Inc.
www.pdftron.com

Hi Jussi,

That did what I needed - thank you. I was a bit confused because of the interchangeable terminology - displayMode and layoutMode.

Kind regards,

David

Hi David,

Glad to hear that you got it working. We had discussion this with the dev team and we will be adding support for docViewer.getDisplayModeManager().setDisplayMode(), so you can pass either the object or the displayMode string to unify and making it easier to use.

Best Regards,
Jussi Nieminen
Senior Software Developer
PDFTron Systems, Inc.
www.pdftron.com