Re-factor Catalog._collectJavaScript to use a Map rather than an Object

Given that this only an internal helper method, used by the `Catalog.{javaScript, jsActions}` getters, this change simplifies iteration of the returned data.
We can also (slightly) re-factor the code of the `jsActions` getter, and remove an obsolete[1] JSDoc-comment from the `openAction` getter.

---
[1] Not really relevant now that we've got proper scripting support.
This commit is contained in:
Jonas Jenwald 2021-04-13 12:02:16 +02:00
parent 6cf3070008
commit 4aa27cc645
2 changed files with 9 additions and 14 deletions

View File

@ -517,7 +517,6 @@ class Page {
this.pageDict, this.pageDict,
PageActionEventType PageActionEventType
); );
return shadow(this, "jsActions", actions); return shadow(this, "jsActions", actions);
} }
} }

View File

@ -856,9 +856,6 @@ class Catalog {
return shadow(this, "viewerPreferences", prefs); return shadow(this, "viewerPreferences", prefs);
} }
/**
* NOTE: "JavaScript" actions are, for now, handled by `get javaScript` below.
*/
get openAction() { get openAction() {
const obj = this._catDict.get("OpenAction"); const obj = this._catDict.get("OpenAction");
const openAction = Object.create(null); const openAction = Object.create(null);
@ -923,9 +920,9 @@ class Catalog {
} }
if (javaScript === null) { if (javaScript === null) {
javaScript = Object.create(null); javaScript = new Map();
} }
javaScript[name] = stringToPDFString(js); javaScript.set(name, stringToPDFString(js));
} }
if (obj && obj.has("JavaScript")) { if (obj && obj.has("JavaScript")) {
@ -955,23 +952,23 @@ class Catalog {
return shadow( return shadow(
this, this,
"javaScript", "javaScript",
javaScript ? Object.values(javaScript) : null javaScript ? [...javaScript.values()] : null
); );
} }
get jsActions() { get jsActions() {
const js = this._collectJavaScript(); const javaScript = this._collectJavaScript();
let actions = collectActions( let actions = collectActions(
this.xref, this.xref,
this._catDict, this._catDict,
DocumentActionEventType DocumentActionEventType
); );
if (!actions && js) { if (javaScript) {
actions = Object.create(null); if (!actions) {
} actions = Object.create(null);
if (actions && js) { }
for (const [key, val] of Object.entries(js)) { for (const [key, val] of javaScript) {
if (key in actions) { if (key in actions) {
actions[key].push(val); actions[key].push(val);
} else { } else {
@ -979,7 +976,6 @@ class Catalog {
} }
} }
} }
return shadow(this, "jsActions", actions); return shadow(this, "jsActions", actions);
} }