mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 16:18:08 +02:00
[api-minor] Add support for toggling of Optional Content in the viewer (issue 12096)
*Besides, obviously, adding viewer support:* This patch attempts to improve the general API for Optional Content Groups slightly, by adding a couple of new methods for interacting with the (more complex) data structures of `OptionalContentConfig`-instances. (Thus allowing us to mark some of the data as "private", given that it probably shouldn't be manipulated directly.) By utilizing not just the "raw" Optional Content Groups, but the data from the `/Order` array when available, we can thus display the Layers in a proper tree-structure with collapsible headings for PDF documents that utilizes that feature. Note that it's possible to reset all Optional Content Groups to their default visibility state, simply by double-clicking on the Layers-button in the sidebar. (Currently that's indicated in the Layers-button tooltip, which is obviously easy to overlook, however it's probably the best we can do for now without adding more buttons, or even a dropdown-toolbar, to the sidebar.) Also, the current Layers-button icons are a little rough around the edges, quite literally, but given that the viewer will soon have its UI modernized anyway they hopefully suffice in the meantime. To give users *full* control of the visibility of the various Optional Content Groups, even those which according to the `/Order` array should not (by default) be toggleable in the UI, this patch will place those under a *custom* heading which: - Is collapsed by default, and placed at the bottom of the Layers-tree, to be a bit less obtrusive. - Uses a slightly different formatting, compared to the "regular" headings. - Is localizable. Finally, note that the thumbnails are *purposely* always rendered with all Optional Content Groups at their default visibility state, since that seems the most useful and it's also consistent with other viewers. To ensure that this works as intended, we'll thus disable the `PDFThumbnailView.setImage` functionality when the Optional Content Groups have been changed in the viewer. (This obviously means that we'll re-render thumbnails instead of using the rendered pages. However, this situation ought to be rare enough for this to not really be a problem.)
This commit is contained in:
parent
2393443e73
commit
66aabe3ec7
17 changed files with 485 additions and 36 deletions
|
@ -27,7 +27,7 @@ class OptionalContentConfig {
|
|||
this.name = null;
|
||||
this.creator = null;
|
||||
this._order = null;
|
||||
this.groups = new Map();
|
||||
this._groups = new Map();
|
||||
|
||||
if (data === null) {
|
||||
return;
|
||||
|
@ -36,34 +36,34 @@ class OptionalContentConfig {
|
|||
this.creator = data.creator;
|
||||
this._order = data.order;
|
||||
for (const group of data.groups) {
|
||||
this.groups.set(
|
||||
this._groups.set(
|
||||
group.id,
|
||||
new OptionalContentGroup(group.name, group.intent)
|
||||
);
|
||||
}
|
||||
|
||||
if (data.baseState === "OFF") {
|
||||
for (const group of this.groups) {
|
||||
for (const group of this._groups) {
|
||||
group.visible = false;
|
||||
}
|
||||
}
|
||||
|
||||
for (const on of data.on) {
|
||||
this.groups.get(on).visible = true;
|
||||
this._groups.get(on).visible = true;
|
||||
}
|
||||
|
||||
for (const off of data.off) {
|
||||
this.groups.get(off).visible = false;
|
||||
this._groups.get(off).visible = false;
|
||||
}
|
||||
}
|
||||
|
||||
isVisible(group) {
|
||||
if (group.type === "OCG") {
|
||||
if (!this.groups.has(group.id)) {
|
||||
if (!this._groups.has(group.id)) {
|
||||
warn(`Optional content group not found: ${group.id}`);
|
||||
return true;
|
||||
}
|
||||
return this.groups.get(group.id).visible;
|
||||
return this._groups.get(group.id).visible;
|
||||
} else if (group.type === "OCMD") {
|
||||
// Per the spec, the expression should be preferred if available. Until
|
||||
// we implement this, just fallback to using the group policy for now.
|
||||
|
@ -73,44 +73,44 @@ class OptionalContentConfig {
|
|||
if (!group.policy || group.policy === "AnyOn") {
|
||||
// Default
|
||||
for (const id of group.ids) {
|
||||
if (!this.groups.has(id)) {
|
||||
if (!this._groups.has(id)) {
|
||||
warn(`Optional content group not found: ${id}`);
|
||||
return true;
|
||||
}
|
||||
if (this.groups.get(id).visible) {
|
||||
if (this._groups.get(id).visible) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} else if (group.policy === "AllOn") {
|
||||
for (const id of group.ids) {
|
||||
if (!this.groups.has(id)) {
|
||||
if (!this._groups.has(id)) {
|
||||
warn(`Optional content group not found: ${id}`);
|
||||
return true;
|
||||
}
|
||||
if (!this.groups.get(id).visible) {
|
||||
if (!this._groups.get(id).visible) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} else if (group.policy === "AnyOff") {
|
||||
for (const id of group.ids) {
|
||||
if (!this.groups.has(id)) {
|
||||
if (!this._groups.has(id)) {
|
||||
warn(`Optional content group not found: ${id}`);
|
||||
return true;
|
||||
}
|
||||
if (!this.groups.get(id).visible) {
|
||||
if (!this._groups.get(id).visible) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} else if (group.policy === "AllOff") {
|
||||
for (const id of group.ids) {
|
||||
if (!this.groups.has(id)) {
|
||||
if (!this._groups.has(id)) {
|
||||
warn(`Optional content group not found: ${id}`);
|
||||
return true;
|
||||
}
|
||||
if (this.groups.get(id).visible) {
|
||||
if (this._groups.get(id).visible) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -122,6 +122,35 @@ class OptionalContentConfig {
|
|||
warn(`Unknown group type ${group.type}.`);
|
||||
return true;
|
||||
}
|
||||
|
||||
setVisibility(id, visible = true) {
|
||||
if (!this._groups.has(id)) {
|
||||
warn(`Optional content group not found: ${id}`);
|
||||
return;
|
||||
}
|
||||
this._groups.get(id).visible = !!visible;
|
||||
}
|
||||
|
||||
getOrder() {
|
||||
if (!this._groups.size) {
|
||||
return null;
|
||||
}
|
||||
if (this._order) {
|
||||
return this._order.slice();
|
||||
}
|
||||
return Array.from(this._groups.keys());
|
||||
}
|
||||
|
||||
getGroups() {
|
||||
if (!this._groups.size) {
|
||||
return null;
|
||||
}
|
||||
return Object.fromEntries(this._groups);
|
||||
}
|
||||
|
||||
getGroup(id) {
|
||||
return this._groups.get(id) || null;
|
||||
}
|
||||
}
|
||||
|
||||
export { OptionalContentConfig };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue