Skip to content

Commit

Permalink
Merge pull request #16628 from Snuffleupagus/app-webViewerInitialized…
Browse files Browse the repository at this point in the history
…-inline

Inline the `webViewerInitialized` function in `PDFViewerApplication.run`
  • Loading branch information
timvandermeij committed Jul 2, 2023
2 parents 73b6ee5 + 58252a5 commit 2c74323
Showing 1 changed file with 104 additions and 103 deletions.
207 changes: 104 additions & 103 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,8 +685,107 @@ const PDFViewerApplication = {
}
},

run(config) {
this.initialize(config).then(webViewerInitialized);
async run(config) {
await this.initialize(config);

const { appConfig, eventBus, l10n } = this;
let file;
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
const queryString = document.location.search.substring(1);
const params = parseQueryString(queryString);
file = params.get("file") ?? AppOptions.get("defaultUrl");
validateFileURL(file);
} else if (PDFJSDev.test("MOZCENTRAL")) {
file = window.location.href;
} else if (PDFJSDev.test("CHROME")) {
file = AppOptions.get("defaultUrl");
}

if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
const fileInput = appConfig.openFileInput;
fileInput.value = null;

fileInput.addEventListener("change", function (evt) {
const { files } = evt.target;
if (!files || files.length === 0) {
return;
}
eventBus.dispatch("fileinputchange", {
source: this,
fileInput: evt.target,
});
});

// Enable dragging-and-dropping a new PDF file onto the viewerContainer.
appConfig.mainContainer.addEventListener("dragover", function (evt) {
evt.preventDefault();

evt.dataTransfer.dropEffect =
evt.dataTransfer.effectAllowed === "copy" ? "copy" : "move";
});
appConfig.mainContainer.addEventListener("drop", function (evt) {
evt.preventDefault();

const { files } = evt.dataTransfer;
if (!files || files.length === 0) {
return;
}
eventBus.dispatch("fileinputchange", {
source: this,
fileInput: evt.dataTransfer,
});
});
}

if (!this.supportsDocumentFonts) {
AppOptions.set("disableFontFace", true);
l10n.get("web_fonts_disabled").then(msg => {
console.warn(msg);
});
}

if (!this.supportsPrinting) {
appConfig.toolbar?.print?.classList.add("hidden");
appConfig.secondaryToolbar?.printButton.classList.add("hidden");
}

if (!this.supportsFullscreen) {
appConfig.secondaryToolbar?.presentationModeButton.classList.add(
"hidden"
);
}

if (this.supportsIntegratedFind) {
appConfig.toolbar?.viewFind?.classList.add("hidden");
}

appConfig.mainContainer.addEventListener(
"transitionend",
function (evt) {
if (evt.target === /* mainContainer */ this) {
eventBus.dispatch("resize", { source: this });
}
},
true
);

try {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
if (file) {
this.open({ url: file });
} else {
this._hideViewBookmark();
}
} else if (PDFJSDev.test("MOZCENTRAL || CHROME")) {
this.initPassiveLoading(file);
} else {
throw new Error("Not implemented: run");
}
} catch (reason) {
l10n.get("loading_error").then(msg => {
this._documentError(msg, reason);
});
}
},

get initialized() {
Expand Down Expand Up @@ -768,13 +867,15 @@ const PDFViewerApplication = {
return this.externalServices.supportedMouseWheelZoomModifierKeys;
},

initPassiveLoading() {
initPassiveLoading(file) {
if (
typeof PDFJSDev === "undefined" ||
!PDFJSDev.test("MOZCENTRAL || CHROME")
) {
throw new Error("Not implemented: initPassiveLoading");
}
this.setTitleUsingUrl(file, /* downloadUrl = */ file);

this.externalServices.initPassiveLoading({
onOpenWithTransport: range => {
this.open({ range });
Expand Down Expand Up @@ -2197,106 +2298,6 @@ function reportPageStatsPDFBug({ pageNumber }) {
globalThis.Stats.add(pageNumber, pageView?.pdfPage?.stats);
}

function webViewerInitialized() {
const { appConfig, eventBus, l10n } = PDFViewerApplication;
let file;
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
const queryString = document.location.search.substring(1);
const params = parseQueryString(queryString);
file = params.get("file") ?? AppOptions.get("defaultUrl");
validateFileURL(file);
} else if (PDFJSDev.test("MOZCENTRAL")) {
file = window.location.href;
} else if (PDFJSDev.test("CHROME")) {
file = AppOptions.get("defaultUrl");
}

if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
const fileInput = appConfig.openFileInput;
fileInput.value = null;

fileInput.addEventListener("change", function (evt) {
const { files } = evt.target;
if (!files || files.length === 0) {
return;
}
eventBus.dispatch("fileinputchange", {
source: this,
fileInput: evt.target,
});
});

// Enable dragging-and-dropping a new PDF file onto the viewerContainer.
appConfig.mainContainer.addEventListener("dragover", function (evt) {
evt.preventDefault();

evt.dataTransfer.dropEffect =
evt.dataTransfer.effectAllowed === "copy" ? "copy" : "move";
});
appConfig.mainContainer.addEventListener("drop", function (evt) {
evt.preventDefault();

const { files } = evt.dataTransfer;
if (!files || files.length === 0) {
return;
}
eventBus.dispatch("fileinputchange", {
source: this,
fileInput: evt.dataTransfer,
});
});
}

if (!PDFViewerApplication.supportsDocumentFonts) {
AppOptions.set("disableFontFace", true);
l10n.get("web_fonts_disabled").then(msg => {
console.warn(msg);
});
}

if (!PDFViewerApplication.supportsPrinting) {
appConfig.toolbar?.print?.classList.add("hidden");
appConfig.secondaryToolbar?.printButton.classList.add("hidden");
}

if (!PDFViewerApplication.supportsFullscreen) {
appConfig.secondaryToolbar?.presentationModeButton.classList.add("hidden");
}

if (PDFViewerApplication.supportsIntegratedFind) {
appConfig.toolbar?.viewFind?.classList.add("hidden");
}

appConfig.mainContainer.addEventListener(
"transitionend",
function (evt) {
if (evt.target === /* mainContainer */ this) {
eventBus.dispatch("resize", { source: this });
}
},
true
);

try {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
if (file) {
PDFViewerApplication.open({ url: file });
} else {
PDFViewerApplication._hideViewBookmark();
}
} else if (PDFJSDev.test("MOZCENTRAL || CHROME")) {
PDFViewerApplication.setTitleUsingUrl(file, /* downloadUrl = */ file);
PDFViewerApplication.initPassiveLoading();
} else {
throw new Error("Not implemented: webViewerInitialized");
}
} catch (reason) {
l10n.get("loading_error").then(msg => {
PDFViewerApplication._documentError(msg, reason);
});
}
}

function webViewerPageRender({ pageNumber }) {
// If the page is (the most) visible when it starts rendering,
// ensure that the page number input loading indicator is displayed.
Expand Down

0 comments on commit 2c74323

Please sign in to comment.