Merge pull request #18829 from Snuffleupagus/PDFLayerViewer-update-UI

Re-factor how `PDFLayerViewer` decides if the UI needs to updated on "optionalcontentconfigchanged" events
This commit is contained in:
Jonas Jenwald 2024-10-01 15:17:00 +02:00 committed by GitHub
commit 35a9a6a7b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -50,7 +50,9 @@ class PDFLayerViewer extends BaseTreeViewer {
reset() { reset() {
super.reset(); super.reset();
this._optionalContentConfig = null; this._optionalContentConfig = null;
this._optionalContentHash = null;
this._optionalContentVisibility?.clear();
this._optionalContentVisibility = null;
} }
/** /**
@ -68,8 +70,13 @@ class PDFLayerViewer extends BaseTreeViewer {
*/ */
_bindLink(element, { groupId, input }) { _bindLink(element, { groupId, input }) {
const setVisibility = () => { const setVisibility = () => {
this._optionalContentConfig.setVisibility(groupId, input.checked); const visible = input.checked;
this._optionalContentHash = this._optionalContentConfig.getHash(); this._optionalContentConfig.setVisibility(groupId, visible);
const cached = this._optionalContentVisibility.get(groupId);
if (cached) {
cached.visible = visible;
}
this.eventBus.dispatch("optionalcontentconfig", { this.eventBus.dispatch("optionalcontentconfig", {
source: this, source: this,
@ -137,7 +144,7 @@ class PDFLayerViewer extends BaseTreeViewer {
this._dispatchEvent(/* layersCount = */ 0); this._dispatchEvent(/* layersCount = */ 0);
return; return;
} }
this._optionalContentHash = optionalContentConfig.getHash(); this._optionalContentVisibility = new Map();
const fragment = document.createDocumentFragment(), const fragment = document.createDocumentFragment(),
queue = [{ parent: fragment, groups }]; queue = [{ parent: fragment, groups }];
@ -170,6 +177,11 @@ class PDFLayerViewer extends BaseTreeViewer {
input.type = "checkbox"; input.type = "checkbox";
input.checked = group.visible; input.checked = group.visible;
this._optionalContentVisibility.set(groupId, {
input,
visible: input.checked,
});
const label = document.createElement("label"); const label = document.createElement("label");
label.textContent = this._normalizeTextContent(group.name); label.textContent = this._normalizeTextContent(group.name);
@ -197,15 +209,20 @@ class PDFLayerViewer extends BaseTreeViewer {
return; // The document was closed while the optional content resolved. return; // The document was closed while the optional content resolved.
} }
if (promise) { if (promise) {
if (optionalContentConfig.getHash() === this._optionalContentHash) { // Ensure that the UI displays the correct state (e.g. with RBGroups).
return; // The optional content didn't change, hence no need to reset the UI. for (const [groupId, cached] of this._optionalContentVisibility) {
const group = optionalContentConfig.getGroup(groupId);
if (group && cached.visible !== group.visible) {
cached.input.checked = cached.visible = !cached.visible;
}
} }
} else { return;
this.eventBus.dispatch("optionalcontentconfig", {
source: this,
promise: Promise.resolve(optionalContentConfig),
});
} }
this.eventBus.dispatch("optionalcontentconfig", {
source: this,
promise: Promise.resolve(optionalContentConfig),
});
// Reset the sidebarView to the new state. // Reset the sidebarView to the new state.
this.render({ this.render({