Convert web/debugger.js to a *basic* module

The various functionality in `web/debugger.js` is currently *indirectly* added to the global scope, since that's how `var` works when it's used outside of any functions/closures.
Given how this functionality is being accessed/used, not just in the viewer but also in the API and textLayer, simply converting the entire file to a module isn't trivial[1]. However, we can at least export the `PDFBug`-part properly and then `import` that dynamically in the viewer.
Also, to improve the code a little bit, we're now *explicitly* exporting the necessary functionality globally.

According to MDN, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#browser_compatibility, all the browsers that we now support have dynamic `imports` implementations.

---
[1] We could probably pass around references to the necessary functionality, but given how this is being used I'm just not sure it's worth the effort. Also, adding *official* support for these viewer-specific debugging tools in the API feels both unnecessary and unfortunate.
This commit is contained in:
Jonas Jenwald 2022-04-03 11:51:49 +02:00
parent 7c8a92da77
commit 8fa73dbfab
3 changed files with 41 additions and 27 deletions

View file

@ -12,7 +12,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals PDFBug, Stats */
import {
animationStarted,
@ -254,6 +253,7 @@ const PDFViewerApplication = {
_docStats: null,
_wheelUnusedTicks: 0,
_idleCallbacks: new Set(),
_PDFBug: null,
// Called once when the document is loaded.
async initialize(appConfig) {
@ -847,10 +847,8 @@ const PDFViewerApplication = {
this.findBar?.reset();
this.toolbar.reset();
this.secondaryToolbar.reset();
this._PDFBug?.cleanup();
if (typeof PDFBug !== "undefined") {
PDFBug.cleanup();
}
await Promise.all(promises);
},
@ -2126,22 +2124,23 @@ async function loadFakeWorker() {
async function initPDFBug(enabledTabs) {
const { debuggerScriptPath, mainContainer } = PDFViewerApplication.appConfig;
await loadScript(debuggerScriptPath);
const { PDFBug } =
typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")
? await import(debuggerScriptPath) // eslint-disable-line no-unsanitized/method
: await __non_webpack_import__(debuggerScriptPath); // eslint-disable-line no-undef
PDFBug.init({ OPS }, mainContainer, enabledTabs);
PDFViewerApplication._PDFBug = PDFBug;
}
function reportPageStatsPDFBug({ pageNumber }) {
if (typeof Stats === "undefined" || !Stats.enabled) {
if (!globalThis.Stats?.enabled) {
return;
}
const pageView = PDFViewerApplication.pdfViewer.getPageView(
/* index = */ pageNumber - 1
);
const pageStats = pageView?.pdfPage?.stats;
if (!pageStats) {
return;
}
Stats.add(pageNumber, pageStats);
globalThis.Stats.add(pageNumber, pageView?.pdfPage?.stats);
}
function webViewerInitialized() {