Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api-minor] JS -- Add listener for sandbox events only if there are some actions #12546

Merged
merged 1 commit into from
Nov 9, 2020

Conversation

calixteman
Copy link
Contributor

  • When no actions then set it to null instead of empty object
  • Even if a field has no actions, it needs to listen to events from the sandbox in order to be updated if an action changes something in it.

@brendandahl brendandahl changed the title JS -- Add listener for sandbox events only if there are some actions [api-minor] JS -- Add listener for sandbox events only if there are some actions Oct 29, 2020
Copy link
Contributor

@brendandahl brendandahl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, but I'd like to hear if it's possible to address the comment.

web/base_viewer.js Outdated Show resolved Hide resolved
Copy link
Collaborator

@Snuffleupagus Snuffleupagus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we actually delay this completely until Annotation parsing actually occurs, since in most documents there's either no annotations present or at least no JavaScript actions present!?

That'd probably reduce the overall size of this, and also simplify the implementation (especially on the viewer side). One quick idea that I had, was to simply include this property in the annotation data itself such that you'll only need to pass around the enableScripting-value to the various viewer-components.

To illustrate my idea, perhaps something along these lines (purposely incomplete, but only to illustrate my idea):

diff --git a/src/core/annotation.js b/src/core/annotation.js
index 252e1529b..f28806726 100644
--- a/src/core/annotation.js
+++ b/src/core/annotation.js
@@ -62,13 +62,17 @@ class AnnotationFactory {
    *   instance.
    */
   static create(xref, ref, pdfManager, idFactory) {
-    return pdfManager.ensureCatalog("acroForm").then(acroForm => {
+    return Promise.all([
+      pdfManager.ensureCatalog("acroForm"),
+      pdfManager.ensureDoc("hasJSActions"),
+    ]).then(([acroForm, hasJSActions]) => {
       return pdfManager.ensure(this, "_create", [
         xref,
         ref,
         pdfManager,
         idFactory,
         acroForm,
+        hasJSActions,
       ]);
     });
   }
@@ -76,7 +80,14 @@ class AnnotationFactory {
   /**
    * @private
    */
-  static _create(xref, ref, pdfManager, idFactory, acroForm) {
+  static _create(
+    xref,
+    ref,
+    pdfManager,
+    idFactory,
+    acroForm,
+    hasJSActions = false
+  ) {
     const dict = xref.fetchIfRef(ref);
     if (!isDict(dict)) {
       return undefined;
@@ -97,6 +108,7 @@ class AnnotationFactory {
       id,
       pdfManager,
       acroForm: acroForm instanceof Dict ? acroForm : Dict.empty,
+      hasJSActions,
     };
 
     switch (subtype) {
@@ -281,6 +293,7 @@ class Annotation {
       modificationDate: this.modificationDate,
       rect: this.rectangle,
       subtype: params.subtype,
+      documentHasJSActions: params.hasJSActions,
     };
 
     this._fallbackFontDict = null;
diff --git a/src/display/annotation_layer.js b/src/display/annotation_layer.js
index effa122b5..4e07676fe 100644
--- a/src/display/annotation_layer.js
+++ b/src/display/annotation_layer.js
@@ -478,7 +478,8 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
         event.target.setSelectionRange(0, 0);
       });
 
-      if (this.data.actions) {
+      // Assuming that the `enableScripting`-value is passed in:
+      if (this.enableScripting && this.data.documentHasJSActions) {
         element.addEventListener("updateFromSandbox", function (event) {
           const data = event.detail;
           if ("value" in data) {

@calixteman
Copy link
Contributor Author

I think this is a very good idea: I like it, thank you.

@calixteman calixteman force-pushed the hasjs branch 3 times, most recently from 10d1ce7 to 10d1965 Compare November 3, 2020 12:14
Copy link
Collaborator

@Snuffleupagus Snuffleupagus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks much better this way; I've added a few comments, after which this should be good to go (assuming all tests pass of course :-)

src/core/annotation.js Outdated Show resolved Hide resolved
src/core/annotation.js Outdated Show resolved Hide resolved
src/core/document.js Outdated Show resolved Hide resolved
src/display/api.js Outdated Show resolved Hide resolved
web/base_viewer.js Outdated Show resolved Hide resolved
web/pdf_page_view.js Outdated Show resolved Hide resolved
src/display/annotation_layer.js Show resolved Hide resolved
Copy link
Contributor

@timvandermeij timvandermeij left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good but I'd really like to see the property comment addressed since it doesn't feel like the right place to put it.

src/core/annotation.js Outdated Show resolved Hide resolved
src/core/document.js Outdated Show resolved Hide resolved
@calixteman calixteman force-pushed the hasjs branch 2 times, most recently from 4aeff66 to de4b0d5 Compare November 9, 2020 10:29
web/pdf_page_view.js Outdated Show resolved Hide resolved
* When no actions then set it to null instead of empty object
* Even if a field has no actions, it needs to listen to events from the sandbox in order to be updated if an action changes something in it.
@brendandahl
Copy link
Contributor

/botio test

@pdfjsbot
Copy link

pdfjsbot commented Nov 9, 2020

From: Bot.io (Linux m4)


Received

Command cmd_test from @brendandahl received. Current queue size: 0

Live output at: http://54.67.70.0:8877/f4cd0e18f636d93/output.txt

@pdfjsbot
Copy link

pdfjsbot commented Nov 9, 2020

From: Bot.io (Windows)


Received

Command cmd_test from @brendandahl received. Current queue size: 0

Live output at: http://3.101.106.178:8877/e38ecbda1eeccf1/output.txt

@pdfjsbot
Copy link

pdfjsbot commented Nov 9, 2020

From: Bot.io (Linux m4)


Failed

Full output at http://54.67.70.0:8877/f4cd0e18f636d93/output.txt

Total script time: 24.90 mins

  • Font tests: Passed
  • Unit tests: FAILED
  • Regression tests: FAILED

Image differences available at: http://54.67.70.0:8877/f4cd0e18f636d93/reftest-analyzer.html#web=eq.log

@pdfjsbot
Copy link

pdfjsbot commented Nov 9, 2020

From: Bot.io (Windows)


Failed

Full output at http://3.101.106.178:8877/e38ecbda1eeccf1/output.txt

Total script time: 28.28 mins

  • Font tests: Passed
  • Unit tests: Passed
  • Regression tests: FAILED

Image differences available at: http://3.101.106.178:8877/e38ecbda1eeccf1/reftest-analyzer.html#web=eq.log

@timvandermeij
Copy link
Contributor

/botio-linux preview

@pdfjsbot
Copy link

pdfjsbot commented Nov 9, 2020

From: Bot.io (Linux m4)


Received

Command cmd_preview from @timvandermeij received. Current queue size: 0

Live output at: http://54.67.70.0:8877/4ae43562e91da07/output.txt

@pdfjsbot
Copy link

pdfjsbot commented Nov 9, 2020

From: Bot.io (Linux m4)


Success

Full output at http://54.67.70.0:8877/4ae43562e91da07/output.txt

Total script time: 4.14 mins

Published

@timvandermeij timvandermeij merged commit 1c17e07 into mozilla:master Nov 9, 2020
@timvandermeij
Copy link
Contributor

Looks good to me too now. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants