Skip to content

[api-minor] Move the viewer scripting initialization/handling into a new PDFScriptingManager class #13042

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

Merged
merged 2 commits into from
Mar 5, 2021

Conversation

Snuffleupagus
Copy link
Collaborator

The main purpose of this patch is to allow scripting to be used together with the viewer components, note the updated "simpleviewer"/"singlepageviewer" examples, in addition to the full default viewer.
Given how the scripting functionality is currently implemented in the default viewer, trying to re-use this with the standalone viewer components would be very hard and ideally you'd want it to work out-of-the-box.

For an initial implementation, in the default viewer, of the scripting functionality it probably made sense to simply dump all of the code in the app.js file, however that cannot be used with the viewer components.
To address this, the functionality is moved into a new PDFScriptingManager class which can thus be handled in the same way as all other viewer components (and e.g. be passed to the BaseViewer-implementations).

Obviously the scripting functionality needs quite a lot of data, during its initialization, and for the default viewer we want to maintain the current way of doing the lookups since that helps avoid a number of redundant API-calls.
To that end, the PDFScriptingManager implementation accepts (optional) factories/functions such that we can maintain the current behaviour for the default viewer. For the viewer components specifically, fallback code-paths are provided to ensure that scripting will "just work"[1].

Besides moving the viewer handling of the scripting code to its own file/class, this patch also takes the opportunity to re-factor the functionality into a number of helper methods to improve overall readability[2].
Note that it's definitely possible that the PDFScriptingManager class could be improved even further (e.g. for general re-use), since it's still heavily tailored to the default viewer use-case, however I believe that this patch is still a good step forward overall.


[1] Obviously all the relevant document properties might not be available in the viewer components use-case (e.g. the various URLs), but most things should work just fine.

[2] The old PDFViewerApplication._initializeJavaScript method, where everything was simply inlined, have over time (in my opinion) become quite large and somewhat difficult to easily reason about.

@pdfjsbot
Copy link

pdfjsbot commented Mar 2, 2021

From: Bot.io (Linux m4)


Received

Command cmd_integrationtest from @Snuffleupagus received. Current queue size: 0

Live output at: http://54.67.70.0:8877/049a274195b771a/output.txt

@pdfjsbot
Copy link

pdfjsbot commented Mar 2, 2021

From: Bot.io (Windows)


Received

Command cmd_integrationtest from @Snuffleupagus received. Current queue size: 0

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

@pdfjsbot
Copy link

pdfjsbot commented Mar 2, 2021

From: Bot.io (Linux m4)


Success

Full output at http://54.67.70.0:8877/049a274195b771a/output.txt

Total script time: 3.09 mins

  • Integration Tests: Passed

@pdfjsbot
Copy link

pdfjsbot commented Mar 2, 2021

From: Bot.io (Windows)


Success

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

Total script time: 5.93 mins

  • Integration Tests: Passed

@Snuffleupagus Snuffleupagus force-pushed the PDFScriptingManager branch 4 times, most recently from 398e68a to 7560fef Compare March 2, 2021 17:04
@pdfjsbot
Copy link

pdfjsbot commented Mar 2, 2021

From: Bot.io (Linux m4)


Received

Command cmd_integrationtest from @Snuffleupagus received. Current queue size: 0

Live output at: http://54.67.70.0:8877/11a0934514d9d65/output.txt

@pdfjsbot
Copy link

pdfjsbot commented Mar 2, 2021

From: Bot.io (Windows)


Received

Command cmd_integrationtest from @Snuffleupagus received. Current queue size: 0

Live output at: http://3.101.106.178:8877/46f41697f8a4e95/output.txt

@pdfjsbot
Copy link

pdfjsbot commented Mar 2, 2021

From: Bot.io (Linux m4)


Success

Full output at http://54.67.70.0:8877/11a0934514d9d65/output.txt

Total script time: 3.09 mins

  • Integration Tests: Passed

@pdfjsbot
Copy link

pdfjsbot commented Mar 2, 2021

From: Bot.io (Windows)


Success

Full output at http://3.101.106.178:8877/46f41697f8a4e95/output.txt

Total script time: 4.63 mins

  • Integration Tests: Passed

@Snuffleupagus
Copy link
Collaborator Author

/botio-linux preview

@pdfjsbot
Copy link

pdfjsbot commented Mar 2, 2021

From: Bot.io (Linux m4)


Received

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

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

@pdfjsbot
Copy link

pdfjsbot commented Mar 2, 2021

From: Bot.io (Linux m4)


Success

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

Total script time: 4.10 mins

Published

@Snuffleupagus
Copy link
Collaborator Author

Snuffleupagus commented Mar 3, 2021

We could most likely simplify (or possibly even remove) the pageopen/pageclose events now, but I decided against doing that here since the PR is already quite large; see https://github.com/Snuffleupagus/pdf.js/compare/PDFScriptingManager...Snuffleupagus:scripting-pageOpenClose?w=1

@Snuffleupagus Snuffleupagus marked this pull request as draft March 4, 2021 20:58
@timvandermeij
Copy link
Contributor

Perhaps it would be good to split this up a bit, for example first a PR with the first two commits and maybe the app_utils move so this one becomes smaller?

@pdfjsbot
Copy link

pdfjsbot commented Mar 5, 2021

From: Bot.io (Windows)


Received

Command cmd_integrationtest from @Snuffleupagus received. Current queue size: 0

Live output at: http://3.101.106.178:8877/057838ac87c1c3d/output.txt

@pdfjsbot
Copy link

pdfjsbot commented Mar 5, 2021

From: Bot.io (Linux m4)


Received

Command cmd_integrationtest from @Snuffleupagus received. Current queue size: 0

Live output at: http://54.67.70.0:8877/8d9a63b7e8e48d9/output.txt

@pdfjsbot
Copy link

pdfjsbot commented Mar 5, 2021

From: Bot.io (Linux m4)


Success

Full output at http://54.67.70.0:8877/8d9a63b7e8e48d9/output.txt

Total script time: 3.10 mins

  • Integration Tests: Passed

@pdfjsbot
Copy link

pdfjsbot commented Mar 5, 2021

From: Bot.io (Windows)


Success

Full output at http://3.101.106.178:8877/057838ac87c1c3d/output.txt

Total script time: 5.02 mins

  • Integration Tests: Passed

@Snuffleupagus Snuffleupagus force-pushed the PDFScriptingManager branch 3 times, most recently from 064dbb2 to afa6ad1 Compare March 5, 2021 12:40
@pdfjsbot
Copy link

pdfjsbot commented Mar 5, 2021

From: Bot.io (Linux m4)


Received

Command cmd_integrationtest from @Snuffleupagus received. Current queue size: 0

Live output at: http://54.67.70.0:8877/683363d36997428/output.txt

@pdfjsbot
Copy link

pdfjsbot commented Mar 5, 2021

From: Bot.io (Windows)


Received

Command cmd_integrationtest from @Snuffleupagus received. Current queue size: 0

Live output at: http://3.101.106.178:8877/214c5978482e589/output.txt

@pdfjsbot
Copy link

pdfjsbot commented Mar 5, 2021

From: Bot.io (Linux m4)


Failed

Full output at http://54.67.70.0:8877/683363d36997428/output.txt

Total script time: 3.15 mins

  • Integration Tests: FAILED

@pdfjsbot
Copy link

pdfjsbot commented Mar 5, 2021

From: Bot.io (Windows)


Success

Full output at http://3.101.106.178:8877/214c5978482e589/output.txt

Total script time: 5.21 mins

  • Integration Tests: Passed

…`web/app.js` and into `web/ui_utils.js`

These changes will be necessary for the next patch, since we don't want to accidentally pull in the entire default viewer in the standalone viewer components.
…new `PDFScriptingManager` class

The *main* purpose of this patch is to allow scripting to be used together with the viewer components, note the updated "simpleviewer"/"singlepageviewer" examples, in addition to the full default viewer.
Given how the scripting functionality is currently implemented in the default viewer, trying to re-use this with the standalone viewer components would be *very* hard and ideally you'd want it to work out-of-the-box.

For an initial implementation, in the default viewer, of the scripting functionality it probably made sense to simply dump all of the code in the `app.js` file, however that cannot be used with the viewer components.
To address this, the functionality is moved into a new `PDFScriptingManager` class which can thus be handled in the same way as all other viewer components (and e.g. be passed to the `BaseViewer`-implementations).

Obviously the scripting functionality needs quite a lot of data, during its initialization, and for the default viewer we want to maintain the current way of doing the lookups since that helps avoid a number of redundant API-calls.
To that end, the `PDFScriptingManager` implementation accepts (optional) factories/functions such that we can maintain the current behaviour for the default viewer. For the viewer components specifically, fallback code-paths are provided to ensure that scripting will "just work"[1].

Besides moving the viewer handling of the scripting code to its own file/class, this patch also takes the opportunity to re-factor the functionality into a number of helper methods to improve overall readability[2].
Note that it's definitely possible that the `PDFScriptingManager` class could be improved even further (e.g. for general re-use), since it's still heavily tailored to the default viewer use-case, however I believe that this patch is still a good step forward overall.

---

[1] Obviously *all* the relevant document properties might not be available in the viewer components use-case (e.g. the various URLs), but most things should work just fine.

[2] The old `PDFViewerApplication._initializeJavaScript` method, where everything was simply inlined, have over time (in my opinion) become quite large and somewhat difficult to *easily* reason about.
@Snuffleupagus
Copy link
Collaborator Author

/botio integrationtest

@pdfjsbot
Copy link

pdfjsbot commented Mar 5, 2021

From: Bot.io (Linux m4)


Received

Command cmd_integrationtest from @Snuffleupagus received. Current queue size: 0

Live output at: http://54.67.70.0:8877/5cb8aa933c0e45e/output.txt

@pdfjsbot
Copy link

pdfjsbot commented Mar 5, 2021

From: Bot.io (Windows)


Received

Command cmd_integrationtest from @Snuffleupagus received. Current queue size: 0

Live output at: http://3.101.106.178:8877/875003d48002ba7/output.txt

@pdfjsbot
Copy link

pdfjsbot commented Mar 5, 2021

From: Bot.io (Linux m4)


Failed

Full output at http://54.67.70.0:8877/5cb8aa933c0e45e/output.txt

Total script time: 3.10 mins

  • Integration Tests: FAILED

@Snuffleupagus
Copy link
Collaborator Author

/botio-linux integrationtest

@pdfjsbot
Copy link

pdfjsbot commented Mar 5, 2021

From: Bot.io (Linux m4)


Received

Command cmd_integrationtest from @Snuffleupagus received. Current queue size: 0

Live output at: http://54.67.70.0:8877/5cdd9972887ca8d/output.txt

@pdfjsbot
Copy link

pdfjsbot commented Mar 5, 2021

From: Bot.io (Windows)


Success

Full output at http://3.101.106.178:8877/875003d48002ba7/output.txt

Total script time: 6.27 mins

  • Integration Tests: Passed

@pdfjsbot
Copy link

pdfjsbot commented Mar 5, 2021

From: Bot.io (Linux m4)


Success

Full output at http://54.67.70.0:8877/5cdd9972887ca8d/output.txt

Total script time: 3.09 mins

  • Integration Tests: Passed

@Snuffleupagus Snuffleupagus marked this pull request as ready for review March 5, 2021 19:48
@Snuffleupagus
Copy link
Collaborator Author

/botio-linux preview

@pdfjsbot
Copy link

pdfjsbot commented Mar 5, 2021

From: Bot.io (Linux m4)


Received

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

Live output at: http://54.67.70.0:8877/191620b2c08468c/output.txt

@pdfjsbot
Copy link

pdfjsbot commented Mar 5, 2021

From: Bot.io (Linux m4)


Success

Full output at http://54.67.70.0:8877/191620b2c08468c/output.txt

Total script time: 4.13 mins

Published

@timvandermeij timvandermeij merged commit fb87704 into mozilla:master Mar 5, 2021
@timvandermeij
Copy link
Contributor

Looks great; thank you for doing this!

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.

3 participants