From ceb4f8a6ab9e794b5e3757cf33991971d28c0509 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 24 Jul 2022 13:14:54 +0200 Subject: [PATCH] [api-minor] Add a new method, in `OptionalContentConfig`, to detect the initial Optional Content visibility state This will allow us to improve the `PDFThumbnailView.setImage` handling in the viewer, and thanks to the added caching this should be reasonbly efficient. --- src/display/optional_content_config.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/display/optional_content_config.js b/src/display/optional_content_config.js index 370ffecf1..baa4aeb5c 100644 --- a/src/display/optional_content_config.js +++ b/src/display/optional_content_config.js @@ -44,8 +44,12 @@ class OptionalContentGroup { } class OptionalContentConfig { + #cachedHasInitialVisibility = true; + #groups = new Map(); + #initialVisibility = null; + #order = null; constructor(data) { @@ -78,6 +82,12 @@ class OptionalContentConfig { for (const off of data.off) { this.#groups.get(off)._setVisible(INTERNAL, false); } + + // The following code must always run *last* in the constructor. + this.#initialVisibility = new Map(); + for (const [id, group] of this.#groups) { + this.#initialVisibility.set(id, group.visible); + } } #evaluateVisibilityExpression(array) { @@ -195,6 +205,21 @@ class OptionalContentConfig { return; } this.#groups.get(id)._setVisible(INTERNAL, !!visible); + + this.#cachedHasInitialVisibility = null; + } + + get hasInitialVisibility() { + if (this.#cachedHasInitialVisibility !== null) { + return this.#cachedHasInitialVisibility; + } + for (const [id, group] of this.#groups) { + const visible = this.#initialVisibility.get(id); + if (group.visible !== visible) { + return (this.#cachedHasInitialVisibility = false); + } + } + return (this.#cachedHasInitialVisibility = true); } getOrder() {