Merge pull request #16569 from Snuffleupagus/PDFScriptingManager-private
[api-minor] Re-factor the `PDFScriptingManager` class to use private fields/methods
This commit is contained in:
		
						commit
						fa95bbce12
					
				
							
								
								
									
										36
									
								
								web/app.js
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								web/app.js
									
									
									
									
									
								
							@ -487,8 +487,8 @@ const PDFViewerApplication = {
 | 
				
			|||||||
        typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC || CHROME")
 | 
					        typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC || CHROME")
 | 
				
			||||||
          ? AppOptions.get("sandboxBundleSrc")
 | 
					          ? AppOptions.get("sandboxBundleSrc")
 | 
				
			||||||
          : null,
 | 
					          : null,
 | 
				
			||||||
      scriptingFactory: externalServices,
 | 
					      externalServices,
 | 
				
			||||||
      docPropertiesLookup: this._scriptingDocProperties.bind(this),
 | 
					      docProperties: this._scriptingDocProperties.bind(this),
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
    this.pdfScriptingManager = pdfScriptingManager;
 | 
					    this.pdfScriptingManager = pdfScriptingManager;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -1281,28 +1281,20 @@ const PDFViewerApplication = {
 | 
				
			|||||||
              spreadMode = stored.spreadMode | 0;
 | 
					              spreadMode = stored.spreadMode | 0;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
          // NOTE: Ignore the pageMode/pageLayout in GeckoView since there's no
 | 
					          // Always let the user preference/view history take precedence.
 | 
				
			||||||
          // sidebar available, nor any UI for changing the Scroll/Spread modes.
 | 
					          if (pageMode && sidebarView === SidebarView.UNKNOWN) {
 | 
				
			||||||
 | 
					            sidebarView = apiPageModeToSidebarView(pageMode);
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
          if (
 | 
					          if (
 | 
				
			||||||
            typeof PDFJSDev === "undefined"
 | 
					            pageLayout &&
 | 
				
			||||||
              ? !window.isGECKOVIEW
 | 
					            scrollMode === ScrollMode.UNKNOWN &&
 | 
				
			||||||
              : !PDFJSDev.test("GECKOVIEW")
 | 
					            spreadMode === SpreadMode.UNKNOWN
 | 
				
			||||||
          ) {
 | 
					          ) {
 | 
				
			||||||
            // Always let the user preference/view history take precedence.
 | 
					            const modes = apiPageLayoutToViewerModes(pageLayout);
 | 
				
			||||||
            if (pageMode && sidebarView === SidebarView.UNKNOWN) {
 | 
					            // TODO: Try to improve page-switching when using the mouse-wheel
 | 
				
			||||||
              sidebarView = apiPageModeToSidebarView(pageMode);
 | 
					            // and/or arrow-keys before allowing the document to control this.
 | 
				
			||||||
            }
 | 
					            // scrollMode = modes.scrollMode;
 | 
				
			||||||
            if (
 | 
					            spreadMode = modes.spreadMode;
 | 
				
			||||||
              pageLayout &&
 | 
					 | 
				
			||||||
              scrollMode === ScrollMode.UNKNOWN &&
 | 
					 | 
				
			||||||
              spreadMode === SpreadMode.UNKNOWN
 | 
					 | 
				
			||||||
            ) {
 | 
					 | 
				
			||||||
              const modes = apiPageLayoutToViewerModes(pageLayout);
 | 
					 | 
				
			||||||
              // TODO: Try to improve page-switching when using the mouse-wheel
 | 
					 | 
				
			||||||
              // and/or arrow-keys before allowing the document to control this.
 | 
					 | 
				
			||||||
              // scrollMode = modes.scrollMode;
 | 
					 | 
				
			||||||
              spreadMode = modes.spreadMode;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
          this.setInitialView(hash, {
 | 
					          this.setInitialView(hash, {
 | 
				
			||||||
 | 
				
			|||||||
@ -15,7 +15,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import { getPdfFilenameFromUrl, loadScript } from "pdfjs-lib";
 | 
					import { getPdfFilenameFromUrl, loadScript } from "pdfjs-lib";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function docPropertiesLookup(pdfDocument) {
 | 
					async function docProperties(pdfDocument) {
 | 
				
			||||||
  const url = "",
 | 
					  const url = "",
 | 
				
			||||||
    baseUrl = url.split("#")[0];
 | 
					    baseUrl = url.split("#")[0];
 | 
				
			||||||
  // eslint-disable-next-line prefer-const
 | 
					  // eslint-disable-next-line prefer-const
 | 
				
			||||||
@ -65,4 +65,4 @@ class GenericScripting {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export { docPropertiesLookup, GenericScripting };
 | 
					export { docProperties, GenericScripting };
 | 
				
			||||||
 | 
				
			|||||||
@ -23,61 +23,84 @@ import { PromiseCapability, shadow } from "pdfjs-lib";
 | 
				
			|||||||
 * @property {EventBus} eventBus - The application event bus.
 | 
					 * @property {EventBus} eventBus - The application event bus.
 | 
				
			||||||
 * @property {string} sandboxBundleSrc - The path and filename of the scripting
 | 
					 * @property {string} sandboxBundleSrc - The path and filename of the scripting
 | 
				
			||||||
 *   bundle.
 | 
					 *   bundle.
 | 
				
			||||||
 * @property {Object} [scriptingFactory] - The factory that is used when
 | 
					 * @property {Object} [externalServices] - The factory that is used when
 | 
				
			||||||
 *   initializing scripting; must contain a `createScripting` method.
 | 
					 *   initializing scripting; must contain a `createScripting` method.
 | 
				
			||||||
 *   PLEASE NOTE: Primarily intended for the default viewer use-case.
 | 
					 *   PLEASE NOTE: Primarily intended for the default viewer use-case.
 | 
				
			||||||
 * @property {function} [docPropertiesLookup] - The function that is used to
 | 
					 * @property {function} [docProperties] - The function that is used to lookup
 | 
				
			||||||
 *   lookup the necessary document properties.
 | 
					 *   the necessary document properties.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class PDFScriptingManager {
 | 
					class PDFScriptingManager {
 | 
				
			||||||
 | 
					  #closeCapability = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  #destroyCapability = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  #docProperties = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  #eventBus = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  #externalServices = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  #pdfDocument = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  #pdfViewer = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  #ready = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  #sandboxBundleSrc = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  #scripting = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
   * @param {PDFScriptingManagerOptions} options
 | 
					   * @param {PDFScriptingManagerOptions} options
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  constructor({
 | 
					  constructor({
 | 
				
			||||||
    eventBus,
 | 
					    eventBus,
 | 
				
			||||||
    sandboxBundleSrc = null,
 | 
					    sandboxBundleSrc = null,
 | 
				
			||||||
    scriptingFactory = null,
 | 
					    externalServices = null,
 | 
				
			||||||
    docPropertiesLookup = null,
 | 
					    docProperties = null,
 | 
				
			||||||
  }) {
 | 
					  }) {
 | 
				
			||||||
    this._pdfDocument = null;
 | 
					    this.#eventBus = eventBus;
 | 
				
			||||||
    this._pdfViewer = null;
 | 
					    if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC || CHROME")) {
 | 
				
			||||||
    this._closeCapability = null;
 | 
					      this.#sandboxBundleSrc = sandboxBundleSrc;
 | 
				
			||||||
    this._destroyCapability = null;
 | 
					    }
 | 
				
			||||||
 | 
					    this.#externalServices = externalServices;
 | 
				
			||||||
 | 
					    this.#docProperties = docProperties;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this._scripting = null;
 | 
					    if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("COMPONENTS")) {
 | 
				
			||||||
    this._ready = false;
 | 
					      const gs = require("./generic_scripting.js");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this._eventBus = eventBus;
 | 
					      this.#externalServices ||= {
 | 
				
			||||||
    this._sandboxBundleSrc = sandboxBundleSrc;
 | 
					        createScripting: options => {
 | 
				
			||||||
    this._scriptingFactory = scriptingFactory;
 | 
					          return new gs.GenericScripting(options.sandboxBundleSrc);
 | 
				
			||||||
    this._docPropertiesLookup = docPropertiesLookup;
 | 
					        },
 | 
				
			||||||
 | 
					      };
 | 
				
			||||||
 | 
					      this.#docProperties ||= pdfDocument => {
 | 
				
			||||||
 | 
					        return gs.docProperties(pdfDocument);
 | 
				
			||||||
 | 
					      };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // The default viewer already handles adding/removing of DOM events,
 | 
					      // The default viewer already handles adding/removing of DOM events,
 | 
				
			||||||
    // hence limit this to only the viewer components.
 | 
					      // hence limit this to only the viewer components.
 | 
				
			||||||
    if (
 | 
					      if (!externalServices) {
 | 
				
			||||||
      typeof PDFJSDev !== "undefined" &&
 | 
					        window.addEventListener("updatefromsandbox", event => {
 | 
				
			||||||
      PDFJSDev.test("COMPONENTS") &&
 | 
					          this.#eventBus.dispatch("updatefromsandbox", {
 | 
				
			||||||
      !this._scriptingFactory
 | 
					            source: window,
 | 
				
			||||||
    ) {
 | 
					            detail: event.detail,
 | 
				
			||||||
      window.addEventListener("updatefromsandbox", event => {
 | 
					          });
 | 
				
			||||||
        this._eventBus.dispatch("updatefromsandbox", {
 | 
					 | 
				
			||||||
          source: window,
 | 
					 | 
				
			||||||
          detail: event.detail,
 | 
					 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
      });
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  setViewer(pdfViewer) {
 | 
					  setViewer(pdfViewer) {
 | 
				
			||||||
    this._pdfViewer = pdfViewer;
 | 
					    this.#pdfViewer = pdfViewer;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async setDocument(pdfDocument) {
 | 
					  async setDocument(pdfDocument) {
 | 
				
			||||||
    if (this._pdfDocument) {
 | 
					    if (this.#pdfDocument) {
 | 
				
			||||||
      await this._destroyScripting();
 | 
					      await this.#destroyScripting();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    this._pdfDocument = pdfDocument;
 | 
					    this.#pdfDocument = pdfDocument;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!pdfDocument) {
 | 
					    if (!pdfDocument) {
 | 
				
			||||||
      return;
 | 
					      return;
 | 
				
			||||||
@ -90,69 +113,68 @@ class PDFScriptingManager {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    if (!objects && !docActions) {
 | 
					    if (!objects && !docActions) {
 | 
				
			||||||
      // No FieldObjects or JavaScript actions were found in the document.
 | 
					      // No FieldObjects or JavaScript actions were found in the document.
 | 
				
			||||||
      await this._destroyScripting();
 | 
					      await this.#destroyScripting();
 | 
				
			||||||
      return;
 | 
					      return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if (pdfDocument !== this._pdfDocument) {
 | 
					    if (pdfDocument !== this.#pdfDocument) {
 | 
				
			||||||
      return; // The document was closed while the data resolved.
 | 
					      return; // The document was closed while the data resolved.
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
      this._scripting = this._createScripting();
 | 
					      this.#scripting = this.#initScripting();
 | 
				
			||||||
    } catch (error) {
 | 
					    } catch (error) {
 | 
				
			||||||
      console.error(`PDFScriptingManager.setDocument: "${error?.message}".`);
 | 
					      console.error(`setDocument: "${error.message}".`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      await this._destroyScripting();
 | 
					      await this.#destroyScripting();
 | 
				
			||||||
      return;
 | 
					      return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this._internalEvents.set("updatefromsandbox", event => {
 | 
					    this._internalEvents.set("updatefromsandbox", event => {
 | 
				
			||||||
      if (event?.source !== window) {
 | 
					      if (event?.source === window) {
 | 
				
			||||||
        return;
 | 
					        this.#updateFromSandbox(event.detail);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      this._updateFromSandbox(event.detail);
 | 
					 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
    this._internalEvents.set("dispatcheventinsandbox", event => {
 | 
					    this._internalEvents.set("dispatcheventinsandbox", event => {
 | 
				
			||||||
      this._scripting?.dispatchEventInSandbox(event.detail);
 | 
					      this.#scripting?.dispatchEventInSandbox(event.detail);
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this._internalEvents.set("pagechanging", ({ pageNumber, previous }) => {
 | 
					    this._internalEvents.set("pagechanging", ({ pageNumber, previous }) => {
 | 
				
			||||||
      if (pageNumber === previous) {
 | 
					      if (pageNumber === previous) {
 | 
				
			||||||
        return; // The current page didn't change.
 | 
					        return; // The current page didn't change.
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      this._dispatchPageClose(previous);
 | 
					      this.#dispatchPageClose(previous);
 | 
				
			||||||
      this._dispatchPageOpen(pageNumber);
 | 
					      this.#dispatchPageOpen(pageNumber);
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
    this._internalEvents.set("pagerendered", ({ pageNumber }) => {
 | 
					    this._internalEvents.set("pagerendered", ({ pageNumber }) => {
 | 
				
			||||||
      if (!this._pageOpenPending.has(pageNumber)) {
 | 
					      if (!this._pageOpenPending.has(pageNumber)) {
 | 
				
			||||||
        return; // No pending "PageOpen" event for the newly rendered page.
 | 
					        return; // No pending "PageOpen" event for the newly rendered page.
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      if (pageNumber !== this._pdfViewer.currentPageNumber) {
 | 
					      if (pageNumber !== this.#pdfViewer.currentPageNumber) {
 | 
				
			||||||
        return; // The newly rendered page is no longer the current one.
 | 
					        return; // The newly rendered page is no longer the current one.
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      this._dispatchPageOpen(pageNumber);
 | 
					      this.#dispatchPageOpen(pageNumber);
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
    this._internalEvents.set("pagesdestroy", async event => {
 | 
					    this._internalEvents.set("pagesdestroy", async () => {
 | 
				
			||||||
      await this._dispatchPageClose(this._pdfViewer.currentPageNumber);
 | 
					      await this.#dispatchPageClose(this.#pdfViewer.currentPageNumber);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      await this._scripting?.dispatchEventInSandbox({
 | 
					      await this.#scripting?.dispatchEventInSandbox({
 | 
				
			||||||
        id: "doc",
 | 
					        id: "doc",
 | 
				
			||||||
        name: "WillClose",
 | 
					        name: "WillClose",
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      this._closeCapability?.resolve();
 | 
					      this.#closeCapability?.resolve();
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (const [name, listener] of this._internalEvents) {
 | 
					    for (const [name, listener] of this._internalEvents) {
 | 
				
			||||||
      this._eventBus._on(name, listener);
 | 
					      this.#eventBus._on(name, listener);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
      const docProperties = await this._getDocProperties();
 | 
					      const docProperties = await this.#docProperties(pdfDocument);
 | 
				
			||||||
      if (pdfDocument !== this._pdfDocument) {
 | 
					      if (pdfDocument !== this.#pdfDocument) {
 | 
				
			||||||
        return; // The document was closed while the properties resolved.
 | 
					        return; // The document was closed while the properties resolved.
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      await this._scripting.createSandbox({
 | 
					      await this.#scripting.createSandbox({
 | 
				
			||||||
        objects,
 | 
					        objects,
 | 
				
			||||||
        calculationOrder,
 | 
					        calculationOrder,
 | 
				
			||||||
        appInfo: {
 | 
					        appInfo: {
 | 
				
			||||||
@ -165,65 +187,65 @@ class PDFScriptingManager {
 | 
				
			|||||||
        },
 | 
					        },
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      this._eventBus.dispatch("sandboxcreated", { source: this });
 | 
					      this.#eventBus.dispatch("sandboxcreated", { source: this });
 | 
				
			||||||
    } catch (error) {
 | 
					    } catch (error) {
 | 
				
			||||||
      console.error(`PDFScriptingManager.setDocument: "${error?.message}".`);
 | 
					      console.error(`setDocument: "${error.message}".`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      await this._destroyScripting();
 | 
					      await this.#destroyScripting();
 | 
				
			||||||
      return;
 | 
					      return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    await this._scripting?.dispatchEventInSandbox({
 | 
					    await this.#scripting?.dispatchEventInSandbox({
 | 
				
			||||||
      id: "doc",
 | 
					      id: "doc",
 | 
				
			||||||
      name: "Open",
 | 
					      name: "Open",
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
    await this._dispatchPageOpen(
 | 
					    await this.#dispatchPageOpen(
 | 
				
			||||||
      this._pdfViewer.currentPageNumber,
 | 
					      this.#pdfViewer.currentPageNumber,
 | 
				
			||||||
      /* initialize = */ true
 | 
					      /* initialize = */ true
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Defer this slightly, to ensure that scripting is *fully* initialized.
 | 
					    // Defer this slightly, to ensure that scripting is *fully* initialized.
 | 
				
			||||||
    Promise.resolve().then(() => {
 | 
					    Promise.resolve().then(() => {
 | 
				
			||||||
      if (pdfDocument === this._pdfDocument) {
 | 
					      if (pdfDocument === this.#pdfDocument) {
 | 
				
			||||||
        this._ready = true;
 | 
					        this.#ready = true;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async dispatchWillSave(detail) {
 | 
					  async dispatchWillSave() {
 | 
				
			||||||
    return this._scripting?.dispatchEventInSandbox({
 | 
					    return this.#scripting?.dispatchEventInSandbox({
 | 
				
			||||||
      id: "doc",
 | 
					      id: "doc",
 | 
				
			||||||
      name: "WillSave",
 | 
					      name: "WillSave",
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async dispatchDidSave(detail) {
 | 
					  async dispatchDidSave() {
 | 
				
			||||||
    return this._scripting?.dispatchEventInSandbox({
 | 
					    return this.#scripting?.dispatchEventInSandbox({
 | 
				
			||||||
      id: "doc",
 | 
					      id: "doc",
 | 
				
			||||||
      name: "DidSave",
 | 
					      name: "DidSave",
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async dispatchWillPrint(detail) {
 | 
					  async dispatchWillPrint() {
 | 
				
			||||||
    return this._scripting?.dispatchEventInSandbox({
 | 
					    return this.#scripting?.dispatchEventInSandbox({
 | 
				
			||||||
      id: "doc",
 | 
					      id: "doc",
 | 
				
			||||||
      name: "WillPrint",
 | 
					      name: "WillPrint",
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async dispatchDidPrint(detail) {
 | 
					  async dispatchDidPrint() {
 | 
				
			||||||
    return this._scripting?.dispatchEventInSandbox({
 | 
					    return this.#scripting?.dispatchEventInSandbox({
 | 
				
			||||||
      id: "doc",
 | 
					      id: "doc",
 | 
				
			||||||
      name: "DidPrint",
 | 
					      name: "DidPrint",
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  get destroyPromise() {
 | 
					  get destroyPromise() {
 | 
				
			||||||
    return this._destroyCapability?.promise || null;
 | 
					    return this.#destroyCapability?.promise || null;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  get ready() {
 | 
					  get ready() {
 | 
				
			||||||
    return this._ready;
 | 
					    return this.#ready;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  /**
 | 
				
			||||||
@ -247,14 +269,11 @@ class PDFScriptingManager {
 | 
				
			|||||||
    return shadow(this, "_visitedPages", new Map());
 | 
					    return shadow(this, "_visitedPages", new Map());
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  async #updateFromSandbox(detail) {
 | 
				
			||||||
   * @private
 | 
					    const pdfViewer = this.#pdfViewer;
 | 
				
			||||||
   */
 | 
					 | 
				
			||||||
  async _updateFromSandbox(detail) {
 | 
					 | 
				
			||||||
    // Ignore some events, see below, that don't make sense in PresentationMode.
 | 
					    // Ignore some events, see below, that don't make sense in PresentationMode.
 | 
				
			||||||
    const isInPresentationMode =
 | 
					    const isInPresentationMode =
 | 
				
			||||||
      this._pdfViewer.isInPresentationMode ||
 | 
					      pdfViewer.isInPresentationMode || pdfViewer.isChangingPresentationMode;
 | 
				
			||||||
      this._pdfViewer.isChangingPresentationMode;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const { id, siblings, command, value } = detail;
 | 
					    const { id, siblings, command, value } = detail;
 | 
				
			||||||
    if (!id) {
 | 
					    if (!id) {
 | 
				
			||||||
@ -265,72 +284,58 @@ class PDFScriptingManager {
 | 
				
			|||||||
        case "error":
 | 
					        case "error":
 | 
				
			||||||
          console.error(value);
 | 
					          console.error(value);
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
        case "layout": {
 | 
					        case "layout":
 | 
				
			||||||
          // NOTE: Always ignore the pageLayout in GeckoView since there's
 | 
					          if (!isInPresentationMode) {
 | 
				
			||||||
          // no UI available to change Scroll/Spread modes for the user.
 | 
					            const modes = apiPageLayoutToViewerModes(value);
 | 
				
			||||||
          if (
 | 
					            pdfViewer.spreadMode = modes.spreadMode;
 | 
				
			||||||
            (typeof PDFJSDev === "undefined"
 | 
					 | 
				
			||||||
              ? window.isGECKOVIEW
 | 
					 | 
				
			||||||
              : PDFJSDev.test("GECKOVIEW")) ||
 | 
					 | 
				
			||||||
            isInPresentationMode
 | 
					 | 
				
			||||||
          ) {
 | 
					 | 
				
			||||||
            return;
 | 
					 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
          const modes = apiPageLayoutToViewerModes(value);
 | 
					 | 
				
			||||||
          this._pdfViewer.spreadMode = modes.spreadMode;
 | 
					 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        case "page-num":
 | 
					        case "page-num":
 | 
				
			||||||
          this._pdfViewer.currentPageNumber = value + 1;
 | 
					          pdfViewer.currentPageNumber = value + 1;
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
        case "print":
 | 
					        case "print":
 | 
				
			||||||
          await this._pdfViewer.pagesPromise;
 | 
					          await pdfViewer.pagesPromise;
 | 
				
			||||||
          this._eventBus.dispatch("print", { source: this });
 | 
					          this.#eventBus.dispatch("print", { source: this });
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
        case "println":
 | 
					        case "println":
 | 
				
			||||||
          console.log(value);
 | 
					          console.log(value);
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
        case "zoom":
 | 
					        case "zoom":
 | 
				
			||||||
          if (isInPresentationMode) {
 | 
					          if (!isInPresentationMode) {
 | 
				
			||||||
            return;
 | 
					            pdfViewer.currentScaleValue = value;
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
          this._pdfViewer.currentScaleValue = value;
 | 
					 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
        case "SaveAs":
 | 
					        case "SaveAs":
 | 
				
			||||||
          this._eventBus.dispatch("download", { source: this });
 | 
					          this.#eventBus.dispatch("download", { source: this });
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
        case "FirstPage":
 | 
					        case "FirstPage":
 | 
				
			||||||
          this._pdfViewer.currentPageNumber = 1;
 | 
					          pdfViewer.currentPageNumber = 1;
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
        case "LastPage":
 | 
					        case "LastPage":
 | 
				
			||||||
          this._pdfViewer.currentPageNumber = this._pdfViewer.pagesCount;
 | 
					          pdfViewer.currentPageNumber = pdfViewer.pagesCount;
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
        case "NextPage":
 | 
					        case "NextPage":
 | 
				
			||||||
          this._pdfViewer.nextPage();
 | 
					          pdfViewer.nextPage();
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
        case "PrevPage":
 | 
					        case "PrevPage":
 | 
				
			||||||
          this._pdfViewer.previousPage();
 | 
					          pdfViewer.previousPage();
 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
        case "ZoomViewIn":
 | 
					        case "ZoomViewIn":
 | 
				
			||||||
          if (isInPresentationMode) {
 | 
					          if (!isInPresentationMode) {
 | 
				
			||||||
            return;
 | 
					            pdfViewer.increaseScale();
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
          this._pdfViewer.increaseScale();
 | 
					 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
        case "ZoomViewOut":
 | 
					        case "ZoomViewOut":
 | 
				
			||||||
          if (isInPresentationMode) {
 | 
					          if (!isInPresentationMode) {
 | 
				
			||||||
            return;
 | 
					            pdfViewer.decreaseScale();
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
          this._pdfViewer.decreaseScale();
 | 
					 | 
				
			||||||
          break;
 | 
					          break;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      return;
 | 
					      return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (isInPresentationMode) {
 | 
					    if (isInPresentationMode && detail.focus) {
 | 
				
			||||||
      if (detail.focus) {
 | 
					      return;
 | 
				
			||||||
        return;
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    delete detail.id;
 | 
					    delete detail.id;
 | 
				
			||||||
    delete detail.siblings;
 | 
					    delete detail.siblings;
 | 
				
			||||||
@ -344,25 +349,22 @@ class PDFScriptingManager {
 | 
				
			|||||||
        element.dispatchEvent(new CustomEvent("updatefromsandbox", { detail }));
 | 
					        element.dispatchEvent(new CustomEvent("updatefromsandbox", { detail }));
 | 
				
			||||||
      } else {
 | 
					      } else {
 | 
				
			||||||
        // The element hasn't been rendered yet, use the AnnotationStorage.
 | 
					        // The element hasn't been rendered yet, use the AnnotationStorage.
 | 
				
			||||||
        this._pdfDocument?.annotationStorage.setValue(elementId, detail);
 | 
					        this.#pdfDocument?.annotationStorage.setValue(elementId, detail);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  async #dispatchPageOpen(pageNumber, initialize = false) {
 | 
				
			||||||
   * @private
 | 
					    const pdfDocument = this.#pdfDocument,
 | 
				
			||||||
   */
 | 
					 | 
				
			||||||
  async _dispatchPageOpen(pageNumber, initialize = false) {
 | 
					 | 
				
			||||||
    const pdfDocument = this._pdfDocument,
 | 
					 | 
				
			||||||
      visitedPages = this._visitedPages;
 | 
					      visitedPages = this._visitedPages;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (initialize) {
 | 
					    if (initialize) {
 | 
				
			||||||
      this._closeCapability = new PromiseCapability();
 | 
					      this.#closeCapability = new PromiseCapability();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if (!this._closeCapability) {
 | 
					    if (!this.#closeCapability) {
 | 
				
			||||||
      return; // Scripting isn't fully initialized yet.
 | 
					      return; // Scripting isn't fully initialized yet.
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    const pageView = this._pdfViewer.getPageView(/* index = */ pageNumber - 1);
 | 
					    const pageView = this.#pdfViewer.getPageView(/* index = */ pageNumber - 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (pageView?.renderingState !== RenderingStates.FINISHED) {
 | 
					    if (pageView?.renderingState !== RenderingStates.FINISHED) {
 | 
				
			||||||
      this._pageOpenPending.add(pageNumber);
 | 
					      this._pageOpenPending.add(pageNumber);
 | 
				
			||||||
@ -375,11 +377,11 @@ class PDFScriptingManager {
 | 
				
			|||||||
      const actions = await (!visitedPages.has(pageNumber)
 | 
					      const actions = await (!visitedPages.has(pageNumber)
 | 
				
			||||||
        ? pageView.pdfPage?.getJSActions()
 | 
					        ? pageView.pdfPage?.getJSActions()
 | 
				
			||||||
        : null);
 | 
					        : null);
 | 
				
			||||||
      if (pdfDocument !== this._pdfDocument) {
 | 
					      if (pdfDocument !== this.#pdfDocument) {
 | 
				
			||||||
        return; // The document was closed while the actions resolved.
 | 
					        return; // The document was closed while the actions resolved.
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      await this._scripting?.dispatchEventInSandbox({
 | 
					      await this.#scripting?.dispatchEventInSandbox({
 | 
				
			||||||
        id: "page",
 | 
					        id: "page",
 | 
				
			||||||
        name: "PageOpen",
 | 
					        name: "PageOpen",
 | 
				
			||||||
        pageNumber,
 | 
					        pageNumber,
 | 
				
			||||||
@ -389,14 +391,11 @@ class PDFScriptingManager {
 | 
				
			|||||||
    visitedPages.set(pageNumber, actionsPromise);
 | 
					    visitedPages.set(pageNumber, actionsPromise);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  async #dispatchPageClose(pageNumber) {
 | 
				
			||||||
   * @private
 | 
					    const pdfDocument = this.#pdfDocument,
 | 
				
			||||||
   */
 | 
					 | 
				
			||||||
  async _dispatchPageClose(pageNumber) {
 | 
					 | 
				
			||||||
    const pdfDocument = this._pdfDocument,
 | 
					 | 
				
			||||||
      visitedPages = this._visitedPages;
 | 
					      visitedPages = this._visitedPages;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!this._closeCapability) {
 | 
					    if (!this.#closeCapability) {
 | 
				
			||||||
      return; // Scripting isn't fully initialized yet.
 | 
					      return; // Scripting isn't fully initialized yet.
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if (this._pageOpenPending.has(pageNumber)) {
 | 
					    if (this._pageOpenPending.has(pageNumber)) {
 | 
				
			||||||
@ -410,97 +409,65 @@ class PDFScriptingManager {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    // Ensure that the "PageOpen" event is dispatched first.
 | 
					    // Ensure that the "PageOpen" event is dispatched first.
 | 
				
			||||||
    await actionsPromise;
 | 
					    await actionsPromise;
 | 
				
			||||||
    if (pdfDocument !== this._pdfDocument) {
 | 
					    if (pdfDocument !== this.#pdfDocument) {
 | 
				
			||||||
      return; // The document was closed while the actions resolved.
 | 
					      return; // The document was closed while the actions resolved.
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    await this._scripting?.dispatchEventInSandbox({
 | 
					    await this.#scripting?.dispatchEventInSandbox({
 | 
				
			||||||
      id: "page",
 | 
					      id: "page",
 | 
				
			||||||
      name: "PageClose",
 | 
					      name: "PageClose",
 | 
				
			||||||
      pageNumber,
 | 
					      pageNumber,
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  #initScripting() {
 | 
				
			||||||
   * @returns {Promise<Object>} A promise that is resolved with an {Object}
 | 
					    this.#destroyCapability = new PromiseCapability();
 | 
				
			||||||
   *   containing the necessary document properties; please find the expected
 | 
					 | 
				
			||||||
   *   format in `PDFViewerApplication._scriptingDocProperties`.
 | 
					 | 
				
			||||||
   * @private
 | 
					 | 
				
			||||||
   */
 | 
					 | 
				
			||||||
  async _getDocProperties() {
 | 
					 | 
				
			||||||
    if (this._docPropertiesLookup) {
 | 
					 | 
				
			||||||
      return this._docPropertiesLookup(this._pdfDocument);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("COMPONENTS")) {
 | 
					 | 
				
			||||||
      const { docPropertiesLookup } = require("./generic_scripting.js");
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      return docPropertiesLookup(this._pdfDocument);
 | 
					    if (this.#scripting) {
 | 
				
			||||||
 | 
					      throw new Error("#initScripting: Scripting already exists.");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    throw new Error("_getDocProperties: Unable to lookup properties.");
 | 
					    return this.#externalServices.createScripting({
 | 
				
			||||||
 | 
					      sandboxBundleSrc: this.#sandboxBundleSrc,
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /**
 | 
					  async #destroyScripting() {
 | 
				
			||||||
   * @private
 | 
					    if (!this.#scripting) {
 | 
				
			||||||
   */
 | 
					      this.#pdfDocument = null;
 | 
				
			||||||
  _createScripting() {
 | 
					 | 
				
			||||||
    this._destroyCapability = new PromiseCapability();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (this._scripting) {
 | 
					      this.#destroyCapability?.resolve();
 | 
				
			||||||
      throw new Error("_createScripting: Scripting already exists.");
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    if (this._scriptingFactory) {
 | 
					 | 
				
			||||||
      return this._scriptingFactory.createScripting({
 | 
					 | 
				
			||||||
        sandboxBundleSrc: this._sandboxBundleSrc,
 | 
					 | 
				
			||||||
      });
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("COMPONENTS")) {
 | 
					 | 
				
			||||||
      const { GenericScripting } = require("./generic_scripting.js");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      return new GenericScripting(this._sandboxBundleSrc);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    throw new Error("_createScripting: Cannot create scripting.");
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  /**
 | 
					 | 
				
			||||||
   * @private
 | 
					 | 
				
			||||||
   */
 | 
					 | 
				
			||||||
  async _destroyScripting() {
 | 
					 | 
				
			||||||
    if (!this._scripting) {
 | 
					 | 
				
			||||||
      this._pdfDocument = null;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      this._destroyCapability?.resolve();
 | 
					 | 
				
			||||||
      return;
 | 
					      return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if (this._closeCapability) {
 | 
					    if (this.#closeCapability) {
 | 
				
			||||||
      await Promise.race([
 | 
					      await Promise.race([
 | 
				
			||||||
        this._closeCapability.promise,
 | 
					        this.#closeCapability.promise,
 | 
				
			||||||
        new Promise(resolve => {
 | 
					        new Promise(resolve => {
 | 
				
			||||||
          // Avoid the scripting/sandbox-destruction hanging indefinitely.
 | 
					          // Avoid the scripting/sandbox-destruction hanging indefinitely.
 | 
				
			||||||
          setTimeout(resolve, 1000);
 | 
					          setTimeout(resolve, 1000);
 | 
				
			||||||
        }),
 | 
					        }),
 | 
				
			||||||
      ]).catch(reason => {
 | 
					      ]).catch(() => {
 | 
				
			||||||
        // Ignore any errors, to ensure that the sandbox is always destroyed.
 | 
					        // Ignore any errors, to ensure that the sandbox is always destroyed.
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
      this._closeCapability = null;
 | 
					      this.#closeCapability = null;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    this._pdfDocument = null;
 | 
					    this.#pdfDocument = null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
      await this._scripting.destroySandbox();
 | 
					      await this.#scripting.destroySandbox();
 | 
				
			||||||
    } catch {}
 | 
					    } catch {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (const [name, listener] of this._internalEvents) {
 | 
					    for (const [name, listener] of this._internalEvents) {
 | 
				
			||||||
      this._eventBus._off(name, listener);
 | 
					      this.#eventBus._off(name, listener);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    this._internalEvents.clear();
 | 
					    this._internalEvents.clear();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this._pageOpenPending.clear();
 | 
					    this._pageOpenPending.clear();
 | 
				
			||||||
    this._visitedPages.clear();
 | 
					    this._visitedPages.clear();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this._scripting = null;
 | 
					    this.#scripting = null;
 | 
				
			||||||
    this._ready = false;
 | 
					    this.#ready = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this._destroyCapability?.resolve();
 | 
					    this.#destroyCapability?.resolve();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -1842,6 +1842,15 @@ class PDFViewer {
 | 
				
			|||||||
   *   The constants from {ScrollMode} should be used.
 | 
					   *   The constants from {ScrollMode} should be used.
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  set scrollMode(mode) {
 | 
					  set scrollMode(mode) {
 | 
				
			||||||
 | 
					    if (
 | 
				
			||||||
 | 
					      typeof PDFJSDev === "undefined"
 | 
				
			||||||
 | 
					        ? window.isGECKOVIEW
 | 
				
			||||||
 | 
					        : PDFJSDev.test("GECKOVIEW")
 | 
				
			||||||
 | 
					    ) {
 | 
				
			||||||
 | 
					      // NOTE: Always ignore the pageLayout in GeckoView since there's
 | 
				
			||||||
 | 
					      // no UI available to change Scroll/Spread modes for the user.
 | 
				
			||||||
 | 
					      return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    if (this._scrollMode === mode) {
 | 
					    if (this._scrollMode === mode) {
 | 
				
			||||||
      return; // The Scroll mode didn't change.
 | 
					      return; // The Scroll mode didn't change.
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -1903,6 +1912,15 @@ class PDFViewer {
 | 
				
			|||||||
   *   The constants from {SpreadMode} should be used.
 | 
					   *   The constants from {SpreadMode} should be used.
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
  set spreadMode(mode) {
 | 
					  set spreadMode(mode) {
 | 
				
			||||||
 | 
					    if (
 | 
				
			||||||
 | 
					      typeof PDFJSDev === "undefined"
 | 
				
			||||||
 | 
					        ? window.isGECKOVIEW
 | 
				
			||||||
 | 
					        : PDFJSDev.test("GECKOVIEW")
 | 
				
			||||||
 | 
					    ) {
 | 
				
			||||||
 | 
					      // NOTE: Always ignore the pageLayout in GeckoView since there's
 | 
				
			||||||
 | 
					      // no UI available to change Scroll/Spread modes for the user.
 | 
				
			||||||
 | 
					      return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    if (this._spreadMode === mode) {
 | 
					    if (this._spreadMode === mode) {
 | 
				
			||||||
      return; // The Spread mode didn't change.
 | 
					      return; // The Spread mode didn't change.
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user