app.js 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.PDFViewerApplication = exports.PDFPrintServiceFactory = exports.DefaultExternalServices = void 0;
  27. var _ui_utils = require("./ui_utils.js");
  28. var _app_options = require("./app_options.js");
  29. var _pdf = require("../pdf");
  30. var _pdf_cursor_tools = require("./pdf_cursor_tools.js");
  31. var _pdf_rendering_queue = require("./pdf_rendering_queue.js");
  32. var _overlay_manager = require("./overlay_manager.js");
  33. var _password_prompt = require("./password_prompt.js");
  34. var _pdf_attachment_viewer = require("./pdf_attachment_viewer.js");
  35. var _pdf_document_properties = require("./pdf_document_properties.js");
  36. var _pdf_find_bar = require("./pdf_find_bar.js");
  37. var _pdf_find_controller = require("./pdf_find_controller.js");
  38. var _pdf_history = require("./pdf_history.js");
  39. var _pdf_layer_viewer = require("./pdf_layer_viewer.js");
  40. var _pdf_link_service = require("./pdf_link_service.js");
  41. var _pdf_outline_viewer = require("./pdf_outline_viewer.js");
  42. var _pdf_presentation_mode = require("./pdf_presentation_mode.js");
  43. var _pdf_scripting_manager = require("./pdf_scripting_manager.js");
  44. var _pdf_sidebar = require("./pdf_sidebar.js");
  45. var _pdf_sidebar_resizer = require("./pdf_sidebar_resizer.js");
  46. var _pdf_thumbnail_viewer = require("./pdf_thumbnail_viewer.js");
  47. var _pdf_viewer = require("./pdf_viewer.js");
  48. var _secondary_toolbar = require("./secondary_toolbar.js");
  49. var _toolbar = require("./toolbar.js");
  50. var _viewer_compatibility = require("./viewer_compatibility.js");
  51. var _view_history = require("./view_history.js");
  52. const DEFAULT_SCALE_DELTA = 1.1;
  53. const DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT = 5000;
  54. const FORCE_PAGES_LOADED_TIMEOUT = 10000;
  55. const WHEEL_ZOOM_DISABLED_TIMEOUT = 1000;
  56. const ENABLE_PERMISSIONS_CLASS = "enablePermissions";
  57. const ViewOnLoad = {
  58. UNKNOWN: -1,
  59. PREVIOUS: 0,
  60. INITIAL: 1
  61. };
  62. const ViewerCssTheme = {
  63. AUTOMATIC: 0,
  64. LIGHT: 1,
  65. DARK: 2
  66. };
  67. const KNOWN_VERSIONS = ["1.0", "1.1", "1.2", "1.3", "1.4", "1.5", "1.6", "1.7", "1.8", "1.9", "2.0", "2.1", "2.2", "2.3"];
  68. const KNOWN_GENERATORS = ["acrobat distiller", "acrobat pdfwriter", "adobe livecycle", "adobe pdf library", "adobe photoshop", "ghostscript", "tcpdf", "cairo", "dvipdfm", "dvips", "pdftex", "pdfkit", "itext", "prince", "quarkxpress", "mac os x", "microsoft", "openoffice", "oracle", "luradocument", "pdf-xchange", "antenna house", "aspose.cells", "fpdf"];
  69. class DefaultExternalServices {
  70. constructor() {
  71. throw new Error("Cannot initialize DefaultExternalServices.");
  72. }
  73. static updateFindControlState(data) {}
  74. static updateFindMatchesCount(data) {}
  75. static initPassiveLoading(callbacks) {}
  76. static async fallback(data) {}
  77. static reportTelemetry(data) {}
  78. static createDownloadManager(options) {
  79. throw new Error("Not implemented: createDownloadManager");
  80. }
  81. static createPreferences() {
  82. throw new Error("Not implemented: createPreferences");
  83. }
  84. static createL10n(options) {
  85. throw new Error("Not implemented: createL10n");
  86. }
  87. static createScripting(options) {
  88. throw new Error("Not implemented: createScripting");
  89. }
  90. static get supportsIntegratedFind() {
  91. return (0, _pdf.shadow)(this, "supportsIntegratedFind", false);
  92. }
  93. static get supportsDocumentFonts() {
  94. return (0, _pdf.shadow)(this, "supportsDocumentFonts", true);
  95. }
  96. static get supportedMouseWheelZoomModifierKeys() {
  97. return (0, _pdf.shadow)(this, "supportedMouseWheelZoomModifierKeys", {
  98. ctrlKey: true,
  99. metaKey: true
  100. });
  101. }
  102. static get isInAutomation() {
  103. return (0, _pdf.shadow)(this, "isInAutomation", false);
  104. }
  105. }
  106. exports.DefaultExternalServices = DefaultExternalServices;
  107. const PDFViewerApplication = {
  108. initialBookmark: document.location.hash.substring(1),
  109. _initializedCapability: (0, _pdf.createPromiseCapability)(),
  110. _fellback: false,
  111. appConfig: null,
  112. pdfDocument: null,
  113. pdfLoadingTask: null,
  114. printService: null,
  115. pdfViewer: null,
  116. pdfThumbnailViewer: null,
  117. pdfRenderingQueue: null,
  118. pdfPresentationMode: null,
  119. pdfDocumentProperties: null,
  120. pdfLinkService: null,
  121. pdfHistory: null,
  122. pdfSidebar: null,
  123. pdfSidebarResizer: null,
  124. pdfOutlineViewer: null,
  125. pdfAttachmentViewer: null,
  126. pdfLayerViewer: null,
  127. pdfCursorTools: null,
  128. pdfScriptingManager: null,
  129. store: null,
  130. downloadManager: null,
  131. overlayManager: null,
  132. preferences: null,
  133. toolbar: null,
  134. secondaryToolbar: null,
  135. eventBus: null,
  136. l10n: null,
  137. isInitialViewSet: false,
  138. downloadComplete: false,
  139. isViewerEmbedded: window.parent !== window,
  140. url: "",
  141. baseUrl: "",
  142. externalServices: DefaultExternalServices,
  143. _boundEvents: Object.create(null),
  144. documentInfo: null,
  145. metadata: null,
  146. _contentDispositionFilename: null,
  147. _contentLength: null,
  148. _saveInProgress: false,
  149. _wheelUnusedTicks: 0,
  150. _idleCallbacks: new Set(),
  151. async initialize(appConfig) {
  152. this.preferences = this.externalServices.createPreferences();
  153. this.appConfig = appConfig;
  154. await this._readPreferences();
  155. await this._parseHashParameters();
  156. this._forceCssTheme();
  157. await this._initializeL10n();
  158. if (this.isViewerEmbedded && _app_options.AppOptions.get("externalLinkTarget") === _pdf.LinkTarget.NONE) {
  159. _app_options.AppOptions.set("externalLinkTarget", _pdf.LinkTarget.TOP);
  160. }
  161. await this._initializeViewerComponents();
  162. this.bindEvents();
  163. this.bindWindowEvents();
  164. const appContainer = appConfig.appContainer || document.documentElement;
  165. this.l10n.translate(appContainer).then(() => {
  166. this.eventBus.dispatch("localized", {
  167. source: this
  168. });
  169. });
  170. this._initializedCapability.resolve();
  171. },
  172. async _readPreferences() {
  173. if (_app_options.AppOptions.get("disablePreferences")) {
  174. return;
  175. }
  176. try {
  177. _app_options.AppOptions.setAll(await this.preferences.getAll());
  178. } catch (reason) {
  179. console.error(`_readPreferences: "${reason?.message}".`);
  180. }
  181. },
  182. async _parseHashParameters() {
  183. if (!_app_options.AppOptions.get("pdfBugEnabled")) {
  184. return undefined;
  185. }
  186. const hash = document.location.hash.substring(1);
  187. if (!hash) {
  188. return undefined;
  189. }
  190. const hashParams = (0, _ui_utils.parseQueryString)(hash),
  191. waitOn = [];
  192. if ("disableworker" in hashParams && hashParams.disableworker === "true") {
  193. waitOn.push(loadFakeWorker());
  194. }
  195. if ("disablerange" in hashParams) {
  196. _app_options.AppOptions.set("disableRange", hashParams.disablerange === "true");
  197. }
  198. if ("disablestream" in hashParams) {
  199. _app_options.AppOptions.set("disableStream", hashParams.disablestream === "true");
  200. }
  201. if ("disableautofetch" in hashParams) {
  202. _app_options.AppOptions.set("disableAutoFetch", hashParams.disableautofetch === "true");
  203. }
  204. if ("disablefontface" in hashParams) {
  205. _app_options.AppOptions.set("disableFontFace", hashParams.disablefontface === "true");
  206. }
  207. if ("disablehistory" in hashParams) {
  208. _app_options.AppOptions.set("disableHistory", hashParams.disablehistory === "true");
  209. }
  210. if ("verbosity" in hashParams) {
  211. _app_options.AppOptions.set("verbosity", hashParams.verbosity | 0);
  212. }
  213. if ("textlayer" in hashParams) {
  214. switch (hashParams.textlayer) {
  215. case "off":
  216. _app_options.AppOptions.set("textLayerMode", _ui_utils.TextLayerMode.DISABLE);
  217. break;
  218. case "visible":
  219. case "shadow":
  220. case "hover":
  221. const viewer = this.appConfig.viewerContainer;
  222. viewer.classList.add("textLayer-" + hashParams.textlayer);
  223. break;
  224. }
  225. }
  226. if ("pdfbug" in hashParams) {
  227. _app_options.AppOptions.set("pdfBug", true);
  228. _app_options.AppOptions.set("fontExtraProperties", true);
  229. const enabled = hashParams.pdfbug.split(",");
  230. waitOn.push(loadAndEnablePDFBug(enabled));
  231. }
  232. if ("locale" in hashParams) {
  233. _app_options.AppOptions.set("locale", hashParams.locale);
  234. }
  235. if (waitOn.length === 0) {
  236. return undefined;
  237. }
  238. return Promise.all(waitOn).catch(reason => {
  239. console.error(`_parseHashParameters: "${reason.message}".`);
  240. });
  241. },
  242. async _initializeL10n() {
  243. this.l10n = this.externalServices.createL10n({
  244. locale: _app_options.AppOptions.get("locale")
  245. });
  246. const dir = await this.l10n.getDirection();
  247. document.getElementsByTagName("html")[0].dir = dir;
  248. },
  249. _forceCssTheme() {
  250. const cssTheme = _app_options.AppOptions.get("viewerCssTheme");
  251. if (cssTheme === ViewerCssTheme.AUTOMATIC || !Object.values(ViewerCssTheme).includes(cssTheme)) {
  252. return;
  253. }
  254. try {
  255. const styleSheet = document.styleSheets[0];
  256. const cssRules = styleSheet?.cssRules || [];
  257. const mediaMatcher = "prefers-color-scheme";
  258. const mediaRule = `(${mediaMatcher}: dark)`;
  259. const mediaRegex = new RegExp(`^@media \\(${mediaMatcher}: dark\\) {\\n\\s*([\\w\\s-.,:;/\\\\{}()]+)\\n}$`);
  260. for (let i = 0, ii = cssRules.length; i < ii; i++) {
  261. const rule = cssRules[i];
  262. if (rule instanceof CSSMediaRule && rule.media?.[0] === mediaRule) {
  263. if (cssTheme === ViewerCssTheme.LIGHT) {
  264. styleSheet.deleteRule(i);
  265. return;
  266. }
  267. const darkRules = mediaRegex.exec(rule.cssText);
  268. if (darkRules?.[1]) {
  269. styleSheet.deleteRule(i);
  270. styleSheet.insertRule(darkRules[1], i);
  271. }
  272. return;
  273. }
  274. }
  275. } catch (reason) {
  276. console.error(`_forceCssTheme: "${reason?.message}".`);
  277. }
  278. },
  279. async _initializeViewerComponents() {
  280. const appConfig = this.appConfig;
  281. const eventBus = appConfig.eventBus || new _ui_utils.EventBus({
  282. isInAutomation: this.externalServices.isInAutomation
  283. });
  284. this.eventBus = eventBus;
  285. this.overlayManager = new _overlay_manager.OverlayManager();
  286. const pdfRenderingQueue = new _pdf_rendering_queue.PDFRenderingQueue();
  287. pdfRenderingQueue.onIdle = this._cleanup.bind(this);
  288. this.pdfRenderingQueue = pdfRenderingQueue;
  289. const pdfLinkService = new _pdf_link_service.PDFLinkService({
  290. eventBus,
  291. externalLinkTarget: _app_options.AppOptions.get("externalLinkTarget"),
  292. externalLinkRel: _app_options.AppOptions.get("externalLinkRel"),
  293. ignoreDestinationZoom: _app_options.AppOptions.get("ignoreDestinationZoom")
  294. });
  295. this.pdfLinkService = pdfLinkService;
  296. const downloadManager = this.externalServices.createDownloadManager();
  297. this.downloadManager = downloadManager;
  298. const findController = new _pdf_find_controller.PDFFindController({
  299. linkService: pdfLinkService,
  300. eventBus
  301. });
  302. this.findController = findController;
  303. const pdfScriptingManager = new _pdf_scripting_manager.PDFScriptingManager({
  304. eventBus,
  305. sandboxBundleSrc: _app_options.AppOptions.get("sandboxBundleSrc"),
  306. scriptingFactory: this.externalServices,
  307. docPropertiesLookup: this._scriptingDocProperties.bind(this)
  308. });
  309. this.pdfScriptingManager = pdfScriptingManager;
  310. const container = appConfig.mainContainer;
  311. const viewer = appConfig.viewerContainer;
  312. this.pdfViewer = new _pdf_viewer.PDFViewer({
  313. container,
  314. viewer,
  315. eventBus,
  316. renderingQueue: pdfRenderingQueue,
  317. linkService: pdfLinkService,
  318. downloadManager,
  319. findController,
  320. scriptingManager: pdfScriptingManager,
  321. renderer: _app_options.AppOptions.get("renderer"),
  322. l10n: this.l10n,
  323. textLayerMode: _app_options.AppOptions.get("textLayerMode"),
  324. imageResourcesPath: _app_options.AppOptions.get("imageResourcesPath"),
  325. renderInteractiveForms: _app_options.AppOptions.get("renderInteractiveForms"),
  326. enablePrintAutoRotate: _app_options.AppOptions.get("enablePrintAutoRotate"),
  327. useOnlyCssZoom: _app_options.AppOptions.get("useOnlyCssZoom"),
  328. maxCanvasPixels: _app_options.AppOptions.get("maxCanvasPixels"),
  329. enableScripting: _app_options.AppOptions.get("enableScripting")
  330. });
  331. pdfRenderingQueue.setViewer(this.pdfViewer);
  332. pdfLinkService.setViewer(this.pdfViewer);
  333. pdfScriptingManager.setViewer(this.pdfViewer);
  334. this.pdfThumbnailViewer = new _pdf_thumbnail_viewer.PDFThumbnailViewer({
  335. container: appConfig.sidebar.thumbnailView,
  336. eventBus,
  337. renderingQueue: pdfRenderingQueue,
  338. linkService: pdfLinkService,
  339. l10n: this.l10n
  340. });
  341. pdfRenderingQueue.setThumbnailViewer(this.pdfThumbnailViewer);
  342. this.pdfHistory = new _pdf_history.PDFHistory({
  343. linkService: pdfLinkService,
  344. eventBus
  345. });
  346. pdfLinkService.setHistory(this.pdfHistory);
  347. if (!this.supportsIntegratedFind) {
  348. this.findBar = new _pdf_find_bar.PDFFindBar(appConfig.findBar, eventBus, this.l10n);
  349. }
  350. this.pdfDocumentProperties = new _pdf_document_properties.PDFDocumentProperties(appConfig.documentProperties, this.overlayManager, eventBus, this.l10n);
  351. this.pdfCursorTools = new _pdf_cursor_tools.PDFCursorTools({
  352. container,
  353. eventBus,
  354. cursorToolOnLoad: _app_options.AppOptions.get("cursorToolOnLoad")
  355. });
  356. this.toolbar = new _toolbar.Toolbar(appConfig.toolbar, eventBus, this.l10n);
  357. this.secondaryToolbar = new _secondary_toolbar.SecondaryToolbar(appConfig.secondaryToolbar, container, eventBus);
  358. if (this.supportsFullscreen) {
  359. this.pdfPresentationMode = new _pdf_presentation_mode.PDFPresentationMode({
  360. container,
  361. pdfViewer: this.pdfViewer,
  362. eventBus
  363. });
  364. }
  365. this.passwordPrompt = new _password_prompt.PasswordPrompt(appConfig.passwordOverlay, this.overlayManager, this.l10n, this.isViewerEmbedded);
  366. this.pdfOutlineViewer = new _pdf_outline_viewer.PDFOutlineViewer({
  367. container: appConfig.sidebar.outlineView,
  368. eventBus,
  369. linkService: pdfLinkService
  370. });
  371. this.pdfAttachmentViewer = new _pdf_attachment_viewer.PDFAttachmentViewer({
  372. container: appConfig.sidebar.attachmentsView,
  373. eventBus,
  374. downloadManager
  375. });
  376. this.pdfLayerViewer = new _pdf_layer_viewer.PDFLayerViewer({
  377. container: appConfig.sidebar.layersView,
  378. eventBus,
  379. l10n: this.l10n
  380. });
  381. this.pdfSidebar = new _pdf_sidebar.PDFSidebar({
  382. elements: appConfig.sidebar,
  383. pdfViewer: this.pdfViewer,
  384. pdfThumbnailViewer: this.pdfThumbnailViewer,
  385. eventBus,
  386. l10n: this.l10n
  387. });
  388. this.pdfSidebar.onToggled = this.forceRendering.bind(this);
  389. this.pdfSidebarResizer = new _pdf_sidebar_resizer.PDFSidebarResizer(appConfig.sidebarResizer, eventBus, this.l10n);
  390. },
  391. run(config) {
  392. this.initialize(config).then(webViewerInitialized);
  393. },
  394. get initialized() {
  395. return this._initializedCapability.settled;
  396. },
  397. get initializedPromise() {
  398. return this._initializedCapability.promise;
  399. },
  400. zoomIn(ticks) {
  401. if (this.pdfViewer.isInPresentationMode) {
  402. return;
  403. }
  404. let newScale = this.pdfViewer.currentScale;
  405. do {
  406. newScale = (newScale * DEFAULT_SCALE_DELTA).toFixed(2);
  407. newScale = Math.ceil(newScale * 10) / 10;
  408. newScale = Math.min(_ui_utils.MAX_SCALE, newScale);
  409. } while (--ticks > 0 && newScale < _ui_utils.MAX_SCALE);
  410. this.pdfViewer.currentScaleValue = newScale;
  411. },
  412. zoomOut(ticks) {
  413. if (this.pdfViewer.isInPresentationMode) {
  414. return;
  415. }
  416. let newScale = this.pdfViewer.currentScale;
  417. do {
  418. newScale = (newScale / DEFAULT_SCALE_DELTA).toFixed(2);
  419. newScale = Math.floor(newScale * 10) / 10;
  420. newScale = Math.max(_ui_utils.MIN_SCALE, newScale);
  421. } while (--ticks > 0 && newScale > _ui_utils.MIN_SCALE);
  422. this.pdfViewer.currentScaleValue = newScale;
  423. },
  424. zoomReset() {
  425. if (this.pdfViewer.isInPresentationMode) {
  426. return;
  427. }
  428. this.pdfViewer.currentScaleValue = _ui_utils.DEFAULT_SCALE_VALUE;
  429. },
  430. get pagesCount() {
  431. return this.pdfDocument ? this.pdfDocument.numPages : 0;
  432. },
  433. get page() {
  434. return this.pdfViewer.currentPageNumber;
  435. },
  436. set page(val) {
  437. this.pdfViewer.currentPageNumber = val;
  438. },
  439. get supportsPrinting() {
  440. return PDFPrintServiceFactory.instance.supportsPrinting;
  441. },
  442. get supportsFullscreen() {
  443. const doc = document.documentElement;
  444. let support = !!(doc.requestFullscreen || doc.mozRequestFullScreen || doc.webkitRequestFullScreen);
  445. if (document.fullscreenEnabled === false || document.mozFullScreenEnabled === false || document.webkitFullscreenEnabled === false) {
  446. support = false;
  447. }
  448. return (0, _pdf.shadow)(this, "supportsFullscreen", support);
  449. },
  450. get supportsIntegratedFind() {
  451. return this.externalServices.supportsIntegratedFind;
  452. },
  453. get supportsDocumentFonts() {
  454. return this.externalServices.supportsDocumentFonts;
  455. },
  456. get loadingBar() {
  457. const bar = new _ui_utils.ProgressBar("#loadingBar");
  458. return (0, _pdf.shadow)(this, "loadingBar", bar);
  459. },
  460. get supportedMouseWheelZoomModifierKeys() {
  461. return this.externalServices.supportedMouseWheelZoomModifierKeys;
  462. },
  463. initPassiveLoading() {
  464. throw new Error("Not implemented: initPassiveLoading");
  465. },
  466. setTitleUsingUrl(url = "") {
  467. this.url = url;
  468. this.baseUrl = url.split("#")[0];
  469. let title = (0, _pdf.getPdfFilenameFromUrl)(url, "");
  470. if (!title) {
  471. try {
  472. title = decodeURIComponent((0, _pdf.getFilenameFromUrl)(url)) || url;
  473. } catch (ex) {
  474. title = url;
  475. }
  476. }
  477. this.setTitle(title);
  478. },
  479. setTitle(title) {
  480. if (this.isViewerEmbedded) {
  481. return;
  482. }
  483. document.title = title;
  484. },
  485. get _docFilename() {
  486. return this._contentDispositionFilename || (0, _pdf.getPdfFilenameFromUrl)(this.url);
  487. },
  488. _cancelIdleCallbacks() {
  489. if (!this._idleCallbacks.size) {
  490. return;
  491. }
  492. for (const callback of this._idleCallbacks) {
  493. window.cancelIdleCallback(callback);
  494. }
  495. this._idleCallbacks.clear();
  496. },
  497. async close() {
  498. this._unblockDocumentLoadEvent();
  499. const {
  500. container
  501. } = this.appConfig.errorWrapper;
  502. container.hidden = true;
  503. if (!this.pdfLoadingTask) {
  504. return;
  505. }
  506. if (this.pdfDocument?.annotationStorage.size > 0 && this._annotationStorageModified) {
  507. try {
  508. await this.save({
  509. sourceEventType: "save"
  510. });
  511. } catch (reason) {}
  512. }
  513. const promises = [];
  514. promises.push(this.pdfLoadingTask.destroy());
  515. this.pdfLoadingTask = null;
  516. if (this.pdfDocument) {
  517. this.pdfDocument = null;
  518. this.pdfThumbnailViewer.setDocument(null);
  519. this.pdfViewer.setDocument(null);
  520. this.pdfLinkService.setDocument(null);
  521. this.pdfDocumentProperties.setDocument(null);
  522. }
  523. webViewerResetPermissions();
  524. this._fellback = false;
  525. this.store = null;
  526. this.isInitialViewSet = false;
  527. this.downloadComplete = false;
  528. this.url = "";
  529. this.baseUrl = "";
  530. this.documentInfo = null;
  531. this.metadata = null;
  532. this._contentDispositionFilename = null;
  533. this._contentLength = null;
  534. this._saveInProgress = false;
  535. this._cancelIdleCallbacks();
  536. promises.push(this.pdfScriptingManager.destroyPromise);
  537. this.pdfSidebar.reset();
  538. this.pdfOutlineViewer.reset();
  539. this.pdfAttachmentViewer.reset();
  540. this.pdfLayerViewer.reset();
  541. if (this.pdfHistory) {
  542. this.pdfHistory.reset();
  543. }
  544. if (this.findBar) {
  545. this.findBar.reset();
  546. }
  547. this.toolbar.reset();
  548. this.secondaryToolbar.reset();
  549. if (typeof PDFBug !== "undefined") {
  550. PDFBug.cleanup();
  551. }
  552. await Promise.all(promises);
  553. },
  554. async open(file, args) {
  555. if (this.pdfLoadingTask) {
  556. await this.close();
  557. }
  558. const workerParameters = _app_options.AppOptions.getAll(_app_options.OptionKind.WORKER);
  559. for (const key in workerParameters) {
  560. _pdf.GlobalWorkerOptions[key] = workerParameters[key];
  561. }
  562. const parameters = Object.create(null);
  563. if (typeof file === "string") {
  564. this.setTitleUsingUrl(file);
  565. parameters.url = file;
  566. } else if (file && "byteLength" in file) {
  567. parameters.data = file;
  568. } else if (file.url && file.originalUrl) {
  569. this.setTitleUsingUrl(file.originalUrl);
  570. parameters.url = file.url;
  571. }
  572. const apiParameters = _app_options.AppOptions.getAll(_app_options.OptionKind.API);
  573. for (const key in apiParameters) {
  574. let value = apiParameters[key];
  575. if (key === "docBaseUrl" && !value) {}
  576. parameters[key] = value;
  577. }
  578. if (args) {
  579. for (const key in args) {
  580. parameters[key] = args[key];
  581. }
  582. }
  583. const loadingTask = (0, _pdf.getDocument)(parameters);
  584. this.pdfLoadingTask = loadingTask;
  585. loadingTask.onPassword = (updateCallback, reason) => {
  586. this.pdfLinkService.externalLinkEnabled = false;
  587. this.passwordPrompt.setUpdateCallback(updateCallback, reason);
  588. this.passwordPrompt.open();
  589. };
  590. loadingTask.onProgress = ({
  591. loaded,
  592. total
  593. }) => {
  594. this.progress(loaded / total);
  595. };
  596. loadingTask.onUnsupportedFeature = this.fallback.bind(this);
  597. return loadingTask.promise.then(pdfDocument => {
  598. this.load(pdfDocument);
  599. }, exception => {
  600. if (loadingTask !== this.pdfLoadingTask) {
  601. return undefined;
  602. }
  603. let key = "loading_error";
  604. if (exception instanceof _pdf.InvalidPDFException) {
  605. key = "invalid_file_error";
  606. } else if (exception instanceof _pdf.MissingPDFException) {
  607. key = "missing_file_error";
  608. } else if (exception instanceof _pdf.UnexpectedResponseException) {
  609. key = "unexpected_response_error";
  610. }
  611. return this.l10n.get(key).then(msg => {
  612. this._documentError(msg, {
  613. message: exception?.message
  614. });
  615. throw exception;
  616. });
  617. });
  618. },
  619. _ensureDownloadComplete() {
  620. if (this.pdfDocument && this.downloadComplete) {
  621. return;
  622. }
  623. throw new Error("PDF document not downloaded.");
  624. },
  625. async download({
  626. sourceEventType = "download"
  627. } = {}) {
  628. const url = this.baseUrl,
  629. filename = this._docFilename;
  630. try {
  631. this._ensureDownloadComplete();
  632. const data = await this.pdfDocument.getData();
  633. const blob = new Blob([data], {
  634. type: "application/pdf"
  635. });
  636. await this.downloadManager.download(blob, url, filename, sourceEventType);
  637. } catch (reason) {
  638. await this.downloadManager.downloadUrl(url, filename);
  639. }
  640. },
  641. async save({
  642. sourceEventType = "download"
  643. } = {}) {
  644. if (this._saveInProgress) {
  645. return;
  646. }
  647. this._saveInProgress = true;
  648. await this.pdfScriptingManager.dispatchWillSave();
  649. const url = this.baseUrl,
  650. filename = this._docFilename;
  651. try {
  652. this._ensureDownloadComplete();
  653. const data = await this.pdfDocument.saveDocument();
  654. const blob = new Blob([data], {
  655. type: "application/pdf"
  656. });
  657. await this.downloadManager.download(blob, url, filename, sourceEventType);
  658. } catch (reason) {
  659. await this.download({
  660. sourceEventType
  661. });
  662. } finally {
  663. await this.pdfScriptingManager.dispatchDidSave();
  664. this._saveInProgress = false;
  665. }
  666. },
  667. downloadOrSave(options) {
  668. if (this.pdfDocument?.annotationStorage.size > 0) {
  669. this.save(options);
  670. } else {
  671. this.download(options);
  672. }
  673. },
  674. fallback(featureId) {
  675. this.externalServices.reportTelemetry({
  676. type: "unsupportedFeature",
  677. featureId
  678. });
  679. if (this._fellback) {
  680. return;
  681. }
  682. this._fellback = true;
  683. this.externalServices.fallback({
  684. featureId,
  685. url: this.baseUrl
  686. }).then(download => {
  687. if (!download) {
  688. return;
  689. }
  690. this.download({
  691. sourceEventType: "download"
  692. });
  693. });
  694. },
  695. _documentError(message, moreInfo = null) {
  696. this._unblockDocumentLoadEvent();
  697. this._otherError(message, moreInfo);
  698. },
  699. _otherError(message, moreInfo = null) {
  700. const moreInfoText = [this.l10n.get("error_version_info", {
  701. version: _pdf.version || "?",
  702. build: _pdf.build || "?"
  703. })];
  704. if (moreInfo) {
  705. moreInfoText.push(this.l10n.get("error_message", {
  706. message: moreInfo.message
  707. }));
  708. if (moreInfo.stack) {
  709. moreInfoText.push(this.l10n.get("error_stack", {
  710. stack: moreInfo.stack
  711. }));
  712. } else {
  713. if (moreInfo.filename) {
  714. moreInfoText.push(this.l10n.get("error_file", {
  715. file: moreInfo.filename
  716. }));
  717. }
  718. if (moreInfo.lineNumber) {
  719. moreInfoText.push(this.l10n.get("error_line", {
  720. line: moreInfo.lineNumber
  721. }));
  722. }
  723. }
  724. }
  725. const errorWrapperConfig = this.appConfig.errorWrapper;
  726. const errorWrapper = errorWrapperConfig.container;
  727. errorWrapper.hidden = false;
  728. const errorMessage = errorWrapperConfig.errorMessage;
  729. errorMessage.textContent = message;
  730. const closeButton = errorWrapperConfig.closeButton;
  731. closeButton.onclick = function () {
  732. errorWrapper.hidden = true;
  733. };
  734. const errorMoreInfo = errorWrapperConfig.errorMoreInfo;
  735. const moreInfoButton = errorWrapperConfig.moreInfoButton;
  736. const lessInfoButton = errorWrapperConfig.lessInfoButton;
  737. moreInfoButton.onclick = function () {
  738. errorMoreInfo.hidden = false;
  739. moreInfoButton.hidden = true;
  740. lessInfoButton.hidden = false;
  741. errorMoreInfo.style.height = errorMoreInfo.scrollHeight + "px";
  742. };
  743. lessInfoButton.onclick = function () {
  744. errorMoreInfo.hidden = true;
  745. moreInfoButton.hidden = false;
  746. lessInfoButton.hidden = true;
  747. };
  748. moreInfoButton.oncontextmenu = _ui_utils.noContextMenuHandler;
  749. lessInfoButton.oncontextmenu = _ui_utils.noContextMenuHandler;
  750. closeButton.oncontextmenu = _ui_utils.noContextMenuHandler;
  751. moreInfoButton.hidden = false;
  752. lessInfoButton.hidden = true;
  753. Promise.all(moreInfoText).then(parts => {
  754. errorMoreInfo.value = parts.join("\n");
  755. });
  756. },
  757. progress(level) {
  758. if (this.downloadComplete) {
  759. return;
  760. }
  761. const percent = Math.round(level * 100);
  762. if (percent > this.loadingBar.percent || isNaN(percent)) {
  763. this.loadingBar.percent = percent;
  764. const disableAutoFetch = this.pdfDocument ? this.pdfDocument.loadingParams.disableAutoFetch : _app_options.AppOptions.get("disableAutoFetch");
  765. if (disableAutoFetch && percent) {
  766. if (this.disableAutoFetchLoadingBarTimeout) {
  767. clearTimeout(this.disableAutoFetchLoadingBarTimeout);
  768. this.disableAutoFetchLoadingBarTimeout = null;
  769. }
  770. this.loadingBar.show();
  771. this.disableAutoFetchLoadingBarTimeout = setTimeout(() => {
  772. this.loadingBar.hide();
  773. this.disableAutoFetchLoadingBarTimeout = null;
  774. }, DISABLE_AUTO_FETCH_LOADING_BAR_TIMEOUT);
  775. }
  776. }
  777. },
  778. load(pdfDocument) {
  779. this.pdfDocument = pdfDocument;
  780. pdfDocument.getDownloadInfo().then(({
  781. length
  782. }) => {
  783. this._contentLength = length;
  784. this.downloadComplete = true;
  785. this.loadingBar.hide();
  786. firstPagePromise.then(() => {
  787. this.eventBus.dispatch("documentloaded", {
  788. source: this
  789. });
  790. });
  791. });
  792. const pageLayoutPromise = pdfDocument.getPageLayout().catch(function () {});
  793. const pageModePromise = pdfDocument.getPageMode().catch(function () {});
  794. const openActionPromise = pdfDocument.getOpenAction().catch(function () {});
  795. this.toolbar.setPagesCount(pdfDocument.numPages, false);
  796. this.secondaryToolbar.setPagesCount(pdfDocument.numPages);
  797. let baseDocumentUrl;
  798. baseDocumentUrl = null;
  799. this.pdfLinkService.setDocument(pdfDocument, baseDocumentUrl);
  800. this.pdfDocumentProperties.setDocument(pdfDocument, this.url);
  801. const pdfViewer = this.pdfViewer;
  802. pdfViewer.setDocument(pdfDocument);
  803. const {
  804. firstPagePromise,
  805. onePageRendered,
  806. pagesPromise
  807. } = pdfViewer;
  808. const pdfThumbnailViewer = this.pdfThumbnailViewer;
  809. pdfThumbnailViewer.setDocument(pdfDocument);
  810. const storedPromise = (this.store = new _view_history.ViewHistory(pdfDocument.fingerprint)).getMultiple({
  811. page: null,
  812. zoom: _ui_utils.DEFAULT_SCALE_VALUE,
  813. scrollLeft: "0",
  814. scrollTop: "0",
  815. rotation: null,
  816. sidebarView: _ui_utils.SidebarView.UNKNOWN,
  817. scrollMode: _ui_utils.ScrollMode.UNKNOWN,
  818. spreadMode: _ui_utils.SpreadMode.UNKNOWN
  819. }).catch(() => {
  820. return Object.create(null);
  821. });
  822. firstPagePromise.then(pdfPage => {
  823. this.loadingBar.setWidth(this.appConfig.viewerContainer);
  824. this._initializeAnnotationStorageCallbacks(pdfDocument);
  825. Promise.all([_ui_utils.animationStarted, storedPromise, pageLayoutPromise, pageModePromise, openActionPromise]).then(async ([timeStamp, stored, pageLayout, pageMode, openAction]) => {
  826. const viewOnLoad = _app_options.AppOptions.get("viewOnLoad");
  827. this._initializePdfHistory({
  828. fingerprint: pdfDocument.fingerprint,
  829. viewOnLoad,
  830. initialDest: openAction?.dest
  831. });
  832. const initialBookmark = this.initialBookmark;
  833. const zoom = _app_options.AppOptions.get("defaultZoomValue");
  834. let hash = zoom ? `zoom=${zoom}` : null;
  835. let rotation = null;
  836. let sidebarView = _app_options.AppOptions.get("sidebarViewOnLoad");
  837. let scrollMode = _app_options.AppOptions.get("scrollModeOnLoad");
  838. let spreadMode = _app_options.AppOptions.get("spreadModeOnLoad");
  839. if (stored.page && viewOnLoad !== ViewOnLoad.INITIAL) {
  840. hash = `page=${stored.page}&zoom=${zoom || stored.zoom},` + `${stored.scrollLeft},${stored.scrollTop}`;
  841. rotation = parseInt(stored.rotation, 10);
  842. if (sidebarView === _ui_utils.SidebarView.UNKNOWN) {
  843. sidebarView = stored.sidebarView | 0;
  844. }
  845. if (scrollMode === _ui_utils.ScrollMode.UNKNOWN) {
  846. scrollMode = stored.scrollMode | 0;
  847. }
  848. if (spreadMode === _ui_utils.SpreadMode.UNKNOWN) {
  849. spreadMode = stored.spreadMode | 0;
  850. }
  851. }
  852. if (pageMode && sidebarView === _ui_utils.SidebarView.UNKNOWN) {
  853. sidebarView = (0, _ui_utils.apiPageModeToSidebarView)(pageMode);
  854. }
  855. if (pageLayout && spreadMode === _ui_utils.SpreadMode.UNKNOWN) {
  856. spreadMode = (0, _ui_utils.apiPageLayoutToSpreadMode)(pageLayout);
  857. }
  858. this.setInitialView(hash, {
  859. rotation,
  860. sidebarView,
  861. scrollMode,
  862. spreadMode
  863. });
  864. this.eventBus.dispatch("documentinit", {
  865. source: this
  866. });
  867. if (!this.isViewerEmbedded) {
  868. pdfViewer.focus();
  869. }
  870. this._initializePermissions(pdfDocument);
  871. await Promise.race([pagesPromise, new Promise(resolve => {
  872. setTimeout(resolve, FORCE_PAGES_LOADED_TIMEOUT);
  873. })]);
  874. if (!initialBookmark && !hash) {
  875. return;
  876. }
  877. if (pdfViewer.hasEqualPageSizes) {
  878. return;
  879. }
  880. this.initialBookmark = initialBookmark;
  881. pdfViewer.currentScaleValue = pdfViewer.currentScaleValue;
  882. this.setInitialView(hash);
  883. }).catch(() => {
  884. this.setInitialView();
  885. }).then(function () {
  886. pdfViewer.update();
  887. });
  888. });
  889. pagesPromise.then(() => {
  890. this._unblockDocumentLoadEvent();
  891. this._initializeAutoPrint(pdfDocument, openActionPromise);
  892. });
  893. onePageRendered.then(() => {
  894. pdfDocument.getOutline().then(outline => {
  895. if (pdfDocument !== this.pdfDocument) {
  896. return;
  897. }
  898. this.pdfOutlineViewer.render({
  899. outline,
  900. pdfDocument
  901. });
  902. });
  903. pdfDocument.getAttachments().then(attachments => {
  904. if (pdfDocument !== this.pdfDocument) {
  905. return;
  906. }
  907. this.pdfAttachmentViewer.render({
  908. attachments
  909. });
  910. });
  911. pdfViewer.optionalContentConfigPromise.then(optionalContentConfig => {
  912. if (pdfDocument !== this.pdfDocument) {
  913. return;
  914. }
  915. this.pdfLayerViewer.render({
  916. optionalContentConfig,
  917. pdfDocument
  918. });
  919. });
  920. if ("requestIdleCallback" in window) {
  921. const callback = window.requestIdleCallback(() => {
  922. this._collectTelemetry(pdfDocument);
  923. this._idleCallbacks.delete(callback);
  924. }, {
  925. timeout: 1000
  926. });
  927. this._idleCallbacks.add(callback);
  928. }
  929. });
  930. this._initializePageLabels(pdfDocument);
  931. this._initializeMetadata(pdfDocument);
  932. },
  933. async _scriptingDocProperties(pdfDocument) {
  934. if (!this.documentInfo) {
  935. await new Promise(resolve => {
  936. this.eventBus._on("metadataloaded", resolve, {
  937. once: true
  938. });
  939. });
  940. if (pdfDocument !== this.pdfDocument) {
  941. return null;
  942. }
  943. }
  944. if (!this._contentLength) {
  945. await new Promise(resolve => {
  946. this.eventBus._on("documentloaded", resolve, {
  947. once: true
  948. });
  949. });
  950. if (pdfDocument !== this.pdfDocument) {
  951. return null;
  952. }
  953. }
  954. return { ...this.documentInfo,
  955. baseURL: this.baseUrl,
  956. filesize: this._contentLength,
  957. filename: this._docFilename,
  958. metadata: this.metadata?.getRaw(),
  959. authors: this.metadata?.get("dc:creator"),
  960. numPages: this.pagesCount,
  961. URL: this.url
  962. };
  963. },
  964. async _collectTelemetry(pdfDocument) {
  965. const markInfo = await this.pdfDocument.getMarkInfo();
  966. if (pdfDocument !== this.pdfDocument) {
  967. return;
  968. }
  969. const tagged = markInfo?.Marked || false;
  970. this.externalServices.reportTelemetry({
  971. type: "tagged",
  972. tagged
  973. });
  974. },
  975. async _initializeAutoPrint(pdfDocument, openActionPromise) {
  976. const [openAction, javaScript] = await Promise.all([openActionPromise, !this.pdfViewer.enableScripting ? pdfDocument.getJavaScript() : null]);
  977. if (pdfDocument !== this.pdfDocument) {
  978. return;
  979. }
  980. let triggerAutoPrint = false;
  981. if (openAction?.action === "Print") {
  982. triggerAutoPrint = true;
  983. }
  984. if (javaScript) {
  985. javaScript.some(js => {
  986. if (!js) {
  987. return false;
  988. }
  989. console.warn("Warning: JavaScript support is not enabled");
  990. this.fallback(_pdf.UNSUPPORTED_FEATURES.javaScript);
  991. return true;
  992. });
  993. if (!triggerAutoPrint) {
  994. for (const js of javaScript) {
  995. if (js && _ui_utils.AutoPrintRegExp.test(js)) {
  996. triggerAutoPrint = true;
  997. break;
  998. }
  999. }
  1000. }
  1001. }
  1002. if (triggerAutoPrint) {
  1003. this.triggerPrinting();
  1004. }
  1005. },
  1006. async _initializeMetadata(pdfDocument) {
  1007. const {
  1008. info,
  1009. metadata,
  1010. contentDispositionFilename,
  1011. contentLength
  1012. } = await pdfDocument.getMetadata();
  1013. if (pdfDocument !== this.pdfDocument) {
  1014. return;
  1015. }
  1016. this.documentInfo = info;
  1017. this.metadata = metadata;
  1018. this._contentDispositionFilename ?? (this._contentDispositionFilename = contentDispositionFilename);
  1019. this._contentLength ?? (this._contentLength = contentLength);
  1020. console.log(`PDF ${pdfDocument.fingerprint} [${info.PDFFormatVersion} ` + `${(info.Producer || "-").trim()} / ${(info.Creator || "-").trim()}] ` + `(PDF.js: ${_pdf.version || "-"})`);
  1021. let pdfTitle = info?.Title;
  1022. const metadataTitle = metadata?.get("dc:title");
  1023. if (metadataTitle) {
  1024. if (metadataTitle !== "Untitled" && !/[\uFFF0-\uFFFF]/g.test(metadataTitle)) {
  1025. pdfTitle = metadataTitle;
  1026. }
  1027. }
  1028. if (pdfTitle) {
  1029. this.setTitle(`${pdfTitle} - ${contentDispositionFilename || document.title}`);
  1030. } else if (contentDispositionFilename) {
  1031. this.setTitle(contentDispositionFilename);
  1032. }
  1033. if (info.IsXFAPresent && !info.IsAcroFormPresent && !pdfDocument.isPureXfa) {
  1034. console.warn("Warning: XFA is not enabled");
  1035. this.fallback(_pdf.UNSUPPORTED_FEATURES.forms);
  1036. } else if ((info.IsAcroFormPresent || info.IsXFAPresent) && !this.pdfViewer.renderInteractiveForms) {
  1037. console.warn("Warning: Interactive form support is not enabled");
  1038. this.fallback(_pdf.UNSUPPORTED_FEATURES.forms);
  1039. }
  1040. if (info.IsSignaturesPresent) {
  1041. console.warn("Warning: Digital signatures validation is not supported");
  1042. this.fallback(_pdf.UNSUPPORTED_FEATURES.signatures);
  1043. }
  1044. let versionId = "other";
  1045. if (KNOWN_VERSIONS.includes(info.PDFFormatVersion)) {
  1046. versionId = `v${info.PDFFormatVersion.replace(".", "_")}`;
  1047. }
  1048. let generatorId = "other";
  1049. if (info.Producer) {
  1050. const producer = info.Producer.toLowerCase();
  1051. KNOWN_GENERATORS.some(function (generator) {
  1052. if (!producer.includes(generator)) {
  1053. return false;
  1054. }
  1055. generatorId = generator.replace(/[ .-]/g, "_");
  1056. return true;
  1057. });
  1058. }
  1059. let formType = null;
  1060. if (info.IsXFAPresent) {
  1061. formType = "xfa";
  1062. } else if (info.IsAcroFormPresent) {
  1063. formType = "acroform";
  1064. }
  1065. this.externalServices.reportTelemetry({
  1066. type: "documentInfo",
  1067. version: versionId,
  1068. generator: generatorId,
  1069. formType
  1070. });
  1071. this.eventBus.dispatch("metadataloaded", {
  1072. source: this
  1073. });
  1074. },
  1075. async _initializePageLabels(pdfDocument) {
  1076. const labels = await pdfDocument.getPageLabels();
  1077. if (pdfDocument !== this.pdfDocument) {
  1078. return;
  1079. }
  1080. if (!labels || _app_options.AppOptions.get("disablePageLabels")) {
  1081. return;
  1082. }
  1083. const numLabels = labels.length;
  1084. if (numLabels !== this.pagesCount) {
  1085. console.error("The number of Page Labels does not match the number of pages in the document.");
  1086. return;
  1087. }
  1088. let i = 0;
  1089. while (i < numLabels && labels[i] === (i + 1).toString()) {
  1090. i++;
  1091. }
  1092. if (i === numLabels) {
  1093. return;
  1094. }
  1095. const {
  1096. pdfViewer,
  1097. pdfThumbnailViewer,
  1098. toolbar
  1099. } = this;
  1100. pdfViewer.setPageLabels(labels);
  1101. pdfThumbnailViewer.setPageLabels(labels);
  1102. toolbar.setPagesCount(numLabels, true);
  1103. toolbar.setPageNumber(pdfViewer.currentPageNumber, pdfViewer.currentPageLabel);
  1104. },
  1105. _initializePdfHistory({
  1106. fingerprint,
  1107. viewOnLoad,
  1108. initialDest = null
  1109. }) {
  1110. if (this.isViewerEmbedded || _app_options.AppOptions.get("disableHistory")) {
  1111. return;
  1112. }
  1113. this.pdfHistory.initialize({
  1114. fingerprint,
  1115. resetHistory: viewOnLoad === ViewOnLoad.INITIAL,
  1116. updateUrl: _app_options.AppOptions.get("historyUpdateUrl")
  1117. });
  1118. if (this.pdfHistory.initialBookmark) {
  1119. this.initialBookmark = this.pdfHistory.initialBookmark;
  1120. this.initialRotation = this.pdfHistory.initialRotation;
  1121. }
  1122. if (initialDest && !this.initialBookmark && viewOnLoad === ViewOnLoad.UNKNOWN) {
  1123. this.initialBookmark = JSON.stringify(initialDest);
  1124. this.pdfHistory.push({
  1125. explicitDest: initialDest,
  1126. pageNumber: null
  1127. });
  1128. }
  1129. },
  1130. async _initializePermissions(pdfDocument) {
  1131. const permissions = await pdfDocument.getPermissions();
  1132. if (pdfDocument !== this.pdfDocument) {
  1133. return;
  1134. }
  1135. if (!permissions || !_app_options.AppOptions.get("enablePermissions")) {
  1136. return;
  1137. }
  1138. if (!permissions.includes(_pdf.PermissionFlag.COPY)) {
  1139. this.appConfig.viewerContainer.classList.add(ENABLE_PERMISSIONS_CLASS);
  1140. }
  1141. },
  1142. _initializeAnnotationStorageCallbacks(pdfDocument) {
  1143. if (pdfDocument !== this.pdfDocument) {
  1144. return;
  1145. }
  1146. const {
  1147. annotationStorage
  1148. } = pdfDocument;
  1149. annotationStorage.onSetModified = () => {
  1150. window.addEventListener("beforeunload", beforeUnload);
  1151. this._annotationStorageModified = true;
  1152. };
  1153. annotationStorage.onResetModified = () => {
  1154. window.removeEventListener("beforeunload", beforeUnload);
  1155. delete this._annotationStorageModified;
  1156. };
  1157. },
  1158. setInitialView(storedHash, {
  1159. rotation,
  1160. sidebarView,
  1161. scrollMode,
  1162. spreadMode
  1163. } = {}) {
  1164. const setRotation = angle => {
  1165. if ((0, _ui_utils.isValidRotation)(angle)) {
  1166. this.pdfViewer.pagesRotation = angle;
  1167. }
  1168. };
  1169. const setViewerModes = (scroll, spread) => {
  1170. if ((0, _ui_utils.isValidScrollMode)(scroll)) {
  1171. this.pdfViewer.scrollMode = scroll;
  1172. }
  1173. if ((0, _ui_utils.isValidSpreadMode)(spread)) {
  1174. this.pdfViewer.spreadMode = spread;
  1175. }
  1176. };
  1177. this.isInitialViewSet = true;
  1178. this.pdfSidebar.setInitialView(sidebarView);
  1179. setViewerModes(scrollMode, spreadMode);
  1180. if (this.initialBookmark) {
  1181. setRotation(this.initialRotation);
  1182. delete this.initialRotation;
  1183. this.pdfLinkService.setHash(this.initialBookmark);
  1184. this.initialBookmark = null;
  1185. } else if (storedHash) {
  1186. setRotation(rotation);
  1187. this.pdfLinkService.setHash(storedHash);
  1188. }
  1189. this.toolbar.setPageNumber(this.pdfViewer.currentPageNumber, this.pdfViewer.currentPageLabel);
  1190. this.secondaryToolbar.setPageNumber(this.pdfViewer.currentPageNumber);
  1191. if (!this.pdfViewer.currentScaleValue) {
  1192. this.pdfViewer.currentScaleValue = _ui_utils.DEFAULT_SCALE_VALUE;
  1193. }
  1194. },
  1195. _cleanup() {
  1196. if (!this.pdfDocument) {
  1197. return;
  1198. }
  1199. this.pdfViewer.cleanup();
  1200. this.pdfThumbnailViewer.cleanup();
  1201. this.pdfDocument.cleanup(this.pdfViewer.renderer === _ui_utils.RendererType.SVG);
  1202. },
  1203. forceRendering() {
  1204. this.pdfRenderingQueue.printing = !!this.printService;
  1205. this.pdfRenderingQueue.isThumbnailViewEnabled = this.pdfSidebar.isThumbnailViewVisible;
  1206. this.pdfRenderingQueue.renderHighestPriority();
  1207. },
  1208. beforePrint() {
  1209. this.pdfScriptingManager.dispatchWillPrint();
  1210. if (this.printService) {
  1211. return;
  1212. }
  1213. if (!this.supportsPrinting) {
  1214. this.l10n.get("printing_not_supported").then(msg => {
  1215. this._otherError(msg);
  1216. });
  1217. return;
  1218. }
  1219. if (!this.pdfViewer.pageViewsReady) {
  1220. this.l10n.get("printing_not_ready").then(msg => {
  1221. window.alert(msg);
  1222. });
  1223. return;
  1224. }
  1225. const pagesOverview = this.pdfViewer.getPagesOverview();
  1226. const printContainer = this.appConfig.printContainer;
  1227. const printResolution = _app_options.AppOptions.get("printResolution");
  1228. const optionalContentConfigPromise = this.pdfViewer.optionalContentConfigPromise;
  1229. const printService = PDFPrintServiceFactory.instance.createPrintService(this.pdfDocument, pagesOverview, printContainer, printResolution, optionalContentConfigPromise, this.l10n);
  1230. this.printService = printService;
  1231. this.forceRendering();
  1232. printService.layout();
  1233. this.externalServices.reportTelemetry({
  1234. type: "print"
  1235. });
  1236. },
  1237. afterPrint() {
  1238. this.pdfScriptingManager.dispatchDidPrint();
  1239. if (this.printService) {
  1240. this.printService.destroy();
  1241. this.printService = null;
  1242. if (this.pdfDocument) {
  1243. this.pdfDocument.annotationStorage.resetModified();
  1244. }
  1245. }
  1246. this.forceRendering();
  1247. },
  1248. rotatePages(delta) {
  1249. this.pdfViewer.pagesRotation += delta;
  1250. },
  1251. requestPresentationMode() {
  1252. if (!this.pdfPresentationMode) {
  1253. return;
  1254. }
  1255. this.pdfPresentationMode.request();
  1256. },
  1257. triggerPrinting() {
  1258. if (!this.supportsPrinting) {
  1259. return;
  1260. }
  1261. window.print();
  1262. },
  1263. bindEvents() {
  1264. const {
  1265. eventBus,
  1266. _boundEvents
  1267. } = this;
  1268. _boundEvents.beforePrint = this.beforePrint.bind(this);
  1269. _boundEvents.afterPrint = this.afterPrint.bind(this);
  1270. eventBus._on("resize", webViewerResize);
  1271. eventBus._on("hashchange", webViewerHashchange);
  1272. eventBus._on("beforeprint", _boundEvents.beforePrint);
  1273. eventBus._on("afterprint", _boundEvents.afterPrint);
  1274. eventBus._on("pagerendered", webViewerPageRendered);
  1275. eventBus._on("updateviewarea", webViewerUpdateViewarea);
  1276. eventBus._on("pagechanging", webViewerPageChanging);
  1277. eventBus._on("scalechanging", webViewerScaleChanging);
  1278. eventBus._on("rotationchanging", webViewerRotationChanging);
  1279. eventBus._on("sidebarviewchanged", webViewerSidebarViewChanged);
  1280. eventBus._on("pagemode", webViewerPageMode);
  1281. eventBus._on("namedaction", webViewerNamedAction);
  1282. eventBus._on("presentationmodechanged", webViewerPresentationModeChanged);
  1283. eventBus._on("presentationmode", webViewerPresentationMode);
  1284. eventBus._on("print", webViewerPrint);
  1285. eventBus._on("download", webViewerDownload);
  1286. eventBus._on("save", webViewerSave);
  1287. eventBus._on("firstpage", webViewerFirstPage);
  1288. eventBus._on("lastpage", webViewerLastPage);
  1289. eventBus._on("nextpage", webViewerNextPage);
  1290. eventBus._on("previouspage", webViewerPreviousPage);
  1291. eventBus._on("zoomin", webViewerZoomIn);
  1292. eventBus._on("zoomout", webViewerZoomOut);
  1293. eventBus._on("zoomreset", webViewerZoomReset);
  1294. eventBus._on("pagenumberchanged", webViewerPageNumberChanged);
  1295. eventBus._on("scalechanged", webViewerScaleChanged);
  1296. eventBus._on("rotatecw", webViewerRotateCw);
  1297. eventBus._on("rotateccw", webViewerRotateCcw);
  1298. eventBus._on("optionalcontentconfig", webViewerOptionalContentConfig);
  1299. eventBus._on("switchscrollmode", webViewerSwitchScrollMode);
  1300. eventBus._on("scrollmodechanged", webViewerScrollModeChanged);
  1301. eventBus._on("switchspreadmode", webViewerSwitchSpreadMode);
  1302. eventBus._on("spreadmodechanged", webViewerSpreadModeChanged);
  1303. eventBus._on("documentproperties", webViewerDocumentProperties);
  1304. eventBus._on("find", webViewerFind);
  1305. eventBus._on("findfromurlhash", webViewerFindFromUrlHash);
  1306. eventBus._on("updatefindmatchescount", webViewerUpdateFindMatchesCount);
  1307. eventBus._on("updatefindcontrolstate", webViewerUpdateFindControlState);
  1308. if (_app_options.AppOptions.get("pdfBug")) {
  1309. _boundEvents.reportPageStatsPDFBug = reportPageStatsPDFBug;
  1310. eventBus._on("pagerendered", _boundEvents.reportPageStatsPDFBug);
  1311. eventBus._on("pagechanging", _boundEvents.reportPageStatsPDFBug);
  1312. }
  1313. eventBus._on("fileinputchange", webViewerFileInputChange);
  1314. eventBus._on("openfile", webViewerOpenFile);
  1315. },
  1316. bindWindowEvents() {
  1317. const {
  1318. eventBus,
  1319. _boundEvents
  1320. } = this;
  1321. _boundEvents.windowResize = () => {
  1322. eventBus.dispatch("resize", {
  1323. source: window
  1324. });
  1325. };
  1326. _boundEvents.windowHashChange = () => {
  1327. eventBus.dispatch("hashchange", {
  1328. source: window,
  1329. hash: document.location.hash.substring(1)
  1330. });
  1331. };
  1332. _boundEvents.windowBeforePrint = () => {
  1333. eventBus.dispatch("beforeprint", {
  1334. source: window
  1335. });
  1336. };
  1337. _boundEvents.windowAfterPrint = () => {
  1338. eventBus.dispatch("afterprint", {
  1339. source: window
  1340. });
  1341. };
  1342. _boundEvents.windowUpdateFromSandbox = event => {
  1343. eventBus.dispatch("updatefromsandbox", {
  1344. source: window,
  1345. detail: event.detail
  1346. });
  1347. };
  1348. window.addEventListener("visibilitychange", webViewerVisibilityChange);
  1349. window.addEventListener("wheel", webViewerWheel, {
  1350. passive: false
  1351. });
  1352. window.addEventListener("touchstart", webViewerTouchStart, {
  1353. passive: false
  1354. });
  1355. window.addEventListener("click", webViewerClick);
  1356. window.addEventListener("keydown", webViewerKeyDown);
  1357. window.addEventListener("resize", _boundEvents.windowResize);
  1358. window.addEventListener("hashchange", _boundEvents.windowHashChange);
  1359. window.addEventListener("beforeprint", _boundEvents.windowBeforePrint);
  1360. window.addEventListener("afterprint", _boundEvents.windowAfterPrint);
  1361. window.addEventListener("updatefromsandbox", _boundEvents.windowUpdateFromSandbox);
  1362. },
  1363. unbindEvents() {
  1364. const {
  1365. eventBus,
  1366. _boundEvents
  1367. } = this;
  1368. eventBus._off("resize", webViewerResize);
  1369. eventBus._off("hashchange", webViewerHashchange);
  1370. eventBus._off("beforeprint", _boundEvents.beforePrint);
  1371. eventBus._off("afterprint", _boundEvents.afterPrint);
  1372. eventBus._off("pagerendered", webViewerPageRendered);
  1373. eventBus._off("updateviewarea", webViewerUpdateViewarea);
  1374. eventBus._off("pagechanging", webViewerPageChanging);
  1375. eventBus._off("scalechanging", webViewerScaleChanging);
  1376. eventBus._off("rotationchanging", webViewerRotationChanging);
  1377. eventBus._off("sidebarviewchanged", webViewerSidebarViewChanged);
  1378. eventBus._off("pagemode", webViewerPageMode);
  1379. eventBus._off("namedaction", webViewerNamedAction);
  1380. eventBus._off("presentationmodechanged", webViewerPresentationModeChanged);
  1381. eventBus._off("presentationmode", webViewerPresentationMode);
  1382. eventBus._off("print", webViewerPrint);
  1383. eventBus._off("download", webViewerDownload);
  1384. eventBus._off("save", webViewerSave);
  1385. eventBus._off("firstpage", webViewerFirstPage);
  1386. eventBus._off("lastpage", webViewerLastPage);
  1387. eventBus._off("nextpage", webViewerNextPage);
  1388. eventBus._off("previouspage", webViewerPreviousPage);
  1389. eventBus._off("zoomin", webViewerZoomIn);
  1390. eventBus._off("zoomout", webViewerZoomOut);
  1391. eventBus._off("zoomreset", webViewerZoomReset);
  1392. eventBus._off("pagenumberchanged", webViewerPageNumberChanged);
  1393. eventBus._off("scalechanged", webViewerScaleChanged);
  1394. eventBus._off("rotatecw", webViewerRotateCw);
  1395. eventBus._off("rotateccw", webViewerRotateCcw);
  1396. eventBus._off("optionalcontentconfig", webViewerOptionalContentConfig);
  1397. eventBus._off("switchscrollmode", webViewerSwitchScrollMode);
  1398. eventBus._off("scrollmodechanged", webViewerScrollModeChanged);
  1399. eventBus._off("switchspreadmode", webViewerSwitchSpreadMode);
  1400. eventBus._off("spreadmodechanged", webViewerSpreadModeChanged);
  1401. eventBus._off("documentproperties", webViewerDocumentProperties);
  1402. eventBus._off("find", webViewerFind);
  1403. eventBus._off("findfromurlhash", webViewerFindFromUrlHash);
  1404. eventBus._off("updatefindmatchescount", webViewerUpdateFindMatchesCount);
  1405. eventBus._off("updatefindcontrolstate", webViewerUpdateFindControlState);
  1406. if (_boundEvents.reportPageStatsPDFBug) {
  1407. eventBus._off("pagerendered", _boundEvents.reportPageStatsPDFBug);
  1408. eventBus._off("pagechanging", _boundEvents.reportPageStatsPDFBug);
  1409. _boundEvents.reportPageStatsPDFBug = null;
  1410. }
  1411. eventBus._off("fileinputchange", webViewerFileInputChange);
  1412. eventBus._off("openfile", webViewerOpenFile);
  1413. _boundEvents.beforePrint = null;
  1414. _boundEvents.afterPrint = null;
  1415. },
  1416. unbindWindowEvents() {
  1417. const {
  1418. _boundEvents
  1419. } = this;
  1420. window.removeEventListener("visibilitychange", webViewerVisibilityChange);
  1421. window.removeEventListener("wheel", webViewerWheel, {
  1422. passive: false
  1423. });
  1424. window.removeEventListener("touchstart", webViewerTouchStart, {
  1425. passive: false
  1426. });
  1427. window.removeEventListener("click", webViewerClick);
  1428. window.removeEventListener("keydown", webViewerKeyDown);
  1429. window.removeEventListener("resize", _boundEvents.windowResize);
  1430. window.removeEventListener("hashchange", _boundEvents.windowHashChange);
  1431. window.removeEventListener("beforeprint", _boundEvents.windowBeforePrint);
  1432. window.removeEventListener("afterprint", _boundEvents.windowAfterPrint);
  1433. window.removeEventListener("updatefromsandbox", _boundEvents.windowUpdateFromSandbox);
  1434. _boundEvents.windowResize = null;
  1435. _boundEvents.windowHashChange = null;
  1436. _boundEvents.windowBeforePrint = null;
  1437. _boundEvents.windowAfterPrint = null;
  1438. _boundEvents.windowUpdateFromSandbox = null;
  1439. },
  1440. accumulateWheelTicks(ticks) {
  1441. if (this._wheelUnusedTicks > 0 && ticks < 0 || this._wheelUnusedTicks < 0 && ticks > 0) {
  1442. this._wheelUnusedTicks = 0;
  1443. }
  1444. this._wheelUnusedTicks += ticks;
  1445. const wholeTicks = Math.sign(this._wheelUnusedTicks) * Math.floor(Math.abs(this._wheelUnusedTicks));
  1446. this._wheelUnusedTicks -= wholeTicks;
  1447. return wholeTicks;
  1448. },
  1449. _unblockDocumentLoadEvent() {
  1450. if (document.blockUnblockOnload) {
  1451. document.blockUnblockOnload(false);
  1452. }
  1453. this._unblockDocumentLoadEvent = () => {};
  1454. },
  1455. get scriptingReady() {
  1456. return this.pdfScriptingManager.ready;
  1457. }
  1458. };
  1459. exports.PDFViewerApplication = PDFViewerApplication;
  1460. let validateFileURL;
  1461. {
  1462. const HOSTED_VIEWER_ORIGINS = ["null", "http://mozilla.github.io", "https://mozilla.github.io"];
  1463. validateFileURL = function (file) {
  1464. if (file === undefined) {
  1465. return;
  1466. }
  1467. try {
  1468. const viewerOrigin = new URL(window.location.href).origin || "null";
  1469. if (HOSTED_VIEWER_ORIGINS.includes(viewerOrigin)) {
  1470. return;
  1471. }
  1472. const {
  1473. origin,
  1474. protocol
  1475. } = new URL(file, window.location.href);
  1476. if (origin !== viewerOrigin && protocol !== "blob:") {
  1477. throw new Error("file origin does not match viewer's");
  1478. }
  1479. } catch (ex) {
  1480. PDFViewerApplication.l10n.get("loading_error").then(msg => {
  1481. PDFViewerApplication._documentError(msg, {
  1482. message: ex?.message
  1483. });
  1484. });
  1485. throw ex;
  1486. }
  1487. };
  1488. }
  1489. async function loadFakeWorker() {
  1490. if (!_pdf.GlobalWorkerOptions.workerSrc) {
  1491. _pdf.GlobalWorkerOptions.workerSrc = _app_options.AppOptions.get("workerSrc");
  1492. }
  1493. return (0, _pdf.loadScript)(_pdf.PDFWorker.getWorkerSrc());
  1494. }
  1495. function loadAndEnablePDFBug(enabledTabs) {
  1496. const appConfig = PDFViewerApplication.appConfig;
  1497. return (0, _pdf.loadScript)(appConfig.debuggerScriptPath).then(function () {
  1498. PDFBug.enable(enabledTabs);
  1499. PDFBug.init({
  1500. OPS: _pdf.OPS
  1501. }, appConfig.mainContainer);
  1502. });
  1503. }
  1504. function reportPageStatsPDFBug({
  1505. pageNumber
  1506. }) {
  1507. if (typeof Stats === "undefined" || !Stats.enabled) {
  1508. return;
  1509. }
  1510. const pageView = PDFViewerApplication.pdfViewer.getPageView(pageNumber - 1);
  1511. const pageStats = pageView?.pdfPage?.stats;
  1512. if (!pageStats) {
  1513. return;
  1514. }
  1515. Stats.add(pageNumber, pageStats);
  1516. }
  1517. function webViewerInitialized() {
  1518. const appConfig = PDFViewerApplication.appConfig;
  1519. let file;
  1520. const queryString = document.location.search.substring(1);
  1521. const params = (0, _ui_utils.parseQueryString)(queryString);
  1522. file = "file" in params ? params.file : _app_options.AppOptions.get("defaultUrl");
  1523. validateFileURL(file);
  1524. const fileInput = document.createElement("input");
  1525. fileInput.id = appConfig.openFileInputName;
  1526. fileInput.className = "fileInput";
  1527. fileInput.setAttribute("type", "file");
  1528. fileInput.oncontextmenu = _ui_utils.noContextMenuHandler;
  1529. document.body.appendChild(fileInput);
  1530. if (!window.File || !window.FileReader || !window.FileList || !window.Blob) {
  1531. appConfig.toolbar.openFile.hidden = true;
  1532. appConfig.secondaryToolbar.openFileButton.hidden = true;
  1533. } else {
  1534. fileInput.value = null;
  1535. }
  1536. fileInput.addEventListener("change", function (evt) {
  1537. const files = evt.target.files;
  1538. if (!files || files.length === 0) {
  1539. return;
  1540. }
  1541. PDFViewerApplication.eventBus.dispatch("fileinputchange", {
  1542. source: this,
  1543. fileInput: evt.target
  1544. });
  1545. });
  1546. appConfig.mainContainer.addEventListener("dragover", function (evt) {
  1547. evt.preventDefault();
  1548. evt.dataTransfer.dropEffect = "move";
  1549. });
  1550. appConfig.mainContainer.addEventListener("drop", function (evt) {
  1551. evt.preventDefault();
  1552. const files = evt.dataTransfer.files;
  1553. if (!files || files.length === 0) {
  1554. return;
  1555. }
  1556. PDFViewerApplication.eventBus.dispatch("fileinputchange", {
  1557. source: this,
  1558. fileInput: evt.dataTransfer
  1559. });
  1560. });
  1561. if (!PDFViewerApplication.supportsDocumentFonts) {
  1562. _app_options.AppOptions.set("disableFontFace", true);
  1563. PDFViewerApplication.l10n.get("web_fonts_disabled").then(msg => {
  1564. console.warn(msg);
  1565. });
  1566. }
  1567. if (!PDFViewerApplication.supportsPrinting) {
  1568. appConfig.toolbar.print.classList.add("hidden");
  1569. appConfig.secondaryToolbar.printButton.classList.add("hidden");
  1570. }
  1571. if (!PDFViewerApplication.supportsFullscreen) {
  1572. appConfig.toolbar.presentationModeButton.classList.add("hidden");
  1573. appConfig.secondaryToolbar.presentationModeButton.classList.add("hidden");
  1574. }
  1575. if (PDFViewerApplication.supportsIntegratedFind) {
  1576. appConfig.toolbar.viewFind.classList.add("hidden");
  1577. }
  1578. appConfig.mainContainer.addEventListener("transitionend", function (evt) {
  1579. if (evt.target === this) {
  1580. PDFViewerApplication.eventBus.dispatch("resize", {
  1581. source: this
  1582. });
  1583. }
  1584. }, true);
  1585. try {
  1586. webViewerOpenFileViaURL(file);
  1587. } catch (reason) {
  1588. PDFViewerApplication.l10n.get("loading_error").then(msg => {
  1589. PDFViewerApplication._documentError(msg, reason);
  1590. });
  1591. }
  1592. }
  1593. function webViewerOpenFileViaURL(file) {
  1594. if (file) {
  1595. PDFViewerApplication.open(file);
  1596. }
  1597. }
  1598. function webViewerResetPermissions() {
  1599. const {
  1600. appConfig
  1601. } = PDFViewerApplication;
  1602. if (!appConfig) {
  1603. return;
  1604. }
  1605. appConfig.viewerContainer.classList.remove(ENABLE_PERMISSIONS_CLASS);
  1606. }
  1607. function webViewerPageRendered({
  1608. pageNumber,
  1609. timestamp,
  1610. error
  1611. }) {
  1612. if (pageNumber === PDFViewerApplication.page) {
  1613. PDFViewerApplication.toolbar.updateLoadingIndicatorState(false);
  1614. }
  1615. if (PDFViewerApplication.pdfSidebar.isThumbnailViewVisible) {
  1616. const pageView = PDFViewerApplication.pdfViewer.getPageView(pageNumber - 1);
  1617. const thumbnailView = PDFViewerApplication.pdfThumbnailViewer.getThumbnail(pageNumber - 1);
  1618. if (pageView && thumbnailView) {
  1619. thumbnailView.setImage(pageView);
  1620. }
  1621. }
  1622. if (error) {
  1623. PDFViewerApplication.l10n.get("rendering_error").then(msg => {
  1624. PDFViewerApplication._otherError(msg, error);
  1625. });
  1626. }
  1627. PDFViewerApplication.externalServices.reportTelemetry({
  1628. type: "pageInfo",
  1629. timestamp
  1630. });
  1631. PDFViewerApplication.pdfDocument.getStats().then(function (stats) {
  1632. PDFViewerApplication.externalServices.reportTelemetry({
  1633. type: "documentStats",
  1634. stats
  1635. });
  1636. });
  1637. }
  1638. function webViewerPageMode({
  1639. mode
  1640. }) {
  1641. let view;
  1642. switch (mode) {
  1643. case "thumbs":
  1644. view = _ui_utils.SidebarView.THUMBS;
  1645. break;
  1646. case "bookmarks":
  1647. case "outline":
  1648. view = _ui_utils.SidebarView.OUTLINE;
  1649. break;
  1650. case "attachments":
  1651. view = _ui_utils.SidebarView.ATTACHMENTS;
  1652. break;
  1653. case "layers":
  1654. view = _ui_utils.SidebarView.LAYERS;
  1655. break;
  1656. case "none":
  1657. view = _ui_utils.SidebarView.NONE;
  1658. break;
  1659. default:
  1660. console.error('Invalid "pagemode" hash parameter: ' + mode);
  1661. return;
  1662. }
  1663. PDFViewerApplication.pdfSidebar.switchView(view, true);
  1664. }
  1665. function webViewerNamedAction(evt) {
  1666. switch (evt.action) {
  1667. case "GoToPage":
  1668. PDFViewerApplication.appConfig.toolbar.pageNumber.select();
  1669. break;
  1670. case "Find":
  1671. if (!PDFViewerApplication.supportsIntegratedFind) {
  1672. PDFViewerApplication.findBar.toggle();
  1673. }
  1674. break;
  1675. case "Print":
  1676. PDFViewerApplication.triggerPrinting();
  1677. break;
  1678. case "SaveAs":
  1679. webViewerSave();
  1680. break;
  1681. }
  1682. }
  1683. function webViewerPresentationModeChanged(evt) {
  1684. PDFViewerApplication.pdfViewer.presentationModeState = evt.state;
  1685. }
  1686. function webViewerSidebarViewChanged(evt) {
  1687. PDFViewerApplication.pdfRenderingQueue.isThumbnailViewEnabled = PDFViewerApplication.pdfSidebar.isThumbnailViewVisible;
  1688. const store = PDFViewerApplication.store;
  1689. if (store && PDFViewerApplication.isInitialViewSet) {
  1690. store.set("sidebarView", evt.view).catch(function () {});
  1691. }
  1692. }
  1693. function webViewerUpdateViewarea(evt) {
  1694. const location = evt.location,
  1695. store = PDFViewerApplication.store;
  1696. if (store && PDFViewerApplication.isInitialViewSet) {
  1697. store.setMultiple({
  1698. page: location.pageNumber,
  1699. zoom: location.scale,
  1700. scrollLeft: location.left,
  1701. scrollTop: location.top,
  1702. rotation: location.rotation
  1703. }).catch(function () {});
  1704. }
  1705. const href = PDFViewerApplication.pdfLinkService.getAnchorUrl(location.pdfOpenParams);
  1706. PDFViewerApplication.appConfig.toolbar.viewBookmark.href = href;
  1707. PDFViewerApplication.appConfig.secondaryToolbar.viewBookmarkButton.href = href;
  1708. const currentPage = PDFViewerApplication.pdfViewer.getPageView(PDFViewerApplication.page - 1);
  1709. const loading = currentPage?.renderingState !== _pdf_rendering_queue.RenderingStates.FINISHED;
  1710. PDFViewerApplication.toolbar.updateLoadingIndicatorState(loading);
  1711. }
  1712. function webViewerScrollModeChanged(evt) {
  1713. const store = PDFViewerApplication.store;
  1714. if (store && PDFViewerApplication.isInitialViewSet) {
  1715. store.set("scrollMode", evt.mode).catch(function () {});
  1716. }
  1717. }
  1718. function webViewerSpreadModeChanged(evt) {
  1719. const store = PDFViewerApplication.store;
  1720. if (store && PDFViewerApplication.isInitialViewSet) {
  1721. store.set("spreadMode", evt.mode).catch(function () {});
  1722. }
  1723. }
  1724. function webViewerResize() {
  1725. const {
  1726. pdfDocument,
  1727. pdfViewer
  1728. } = PDFViewerApplication;
  1729. if (!pdfDocument) {
  1730. return;
  1731. }
  1732. const currentScaleValue = pdfViewer.currentScaleValue;
  1733. if (currentScaleValue === "auto" || currentScaleValue === "page-fit" || currentScaleValue === "page-width") {
  1734. pdfViewer.currentScaleValue = currentScaleValue;
  1735. }
  1736. pdfViewer.update();
  1737. }
  1738. function webViewerHashchange(evt) {
  1739. const hash = evt.hash;
  1740. if (!hash) {
  1741. return;
  1742. }
  1743. if (!PDFViewerApplication.isInitialViewSet) {
  1744. PDFViewerApplication.initialBookmark = hash;
  1745. } else if (!PDFViewerApplication.pdfHistory.popStateInProgress) {
  1746. PDFViewerApplication.pdfLinkService.setHash(hash);
  1747. }
  1748. }
  1749. let webViewerFileInputChange, webViewerOpenFile;
  1750. {
  1751. webViewerFileInputChange = function (evt) {
  1752. if (PDFViewerApplication.pdfViewer?.isInPresentationMode) {
  1753. return;
  1754. }
  1755. const file = evt.fileInput.files[0];
  1756. if (!_viewer_compatibility.viewerCompatibilityParams.disableCreateObjectURL) {
  1757. let url = URL.createObjectURL(file);
  1758. if (file.name) {
  1759. url = {
  1760. url,
  1761. originalUrl: file.name
  1762. };
  1763. }
  1764. PDFViewerApplication.open(url);
  1765. } else {
  1766. PDFViewerApplication.setTitleUsingUrl(file.name);
  1767. const fileReader = new FileReader();
  1768. fileReader.onload = function webViewerChangeFileReaderOnload(event) {
  1769. const buffer = event.target.result;
  1770. PDFViewerApplication.open(new Uint8Array(buffer));
  1771. };
  1772. fileReader.readAsArrayBuffer(file);
  1773. }
  1774. const appConfig = PDFViewerApplication.appConfig;
  1775. appConfig.toolbar.viewBookmark.hidden = true;
  1776. appConfig.secondaryToolbar.viewBookmarkButton.hidden = true;
  1777. appConfig.toolbar.download.hidden = true;
  1778. appConfig.secondaryToolbar.downloadButton.hidden = true;
  1779. };
  1780. webViewerOpenFile = function (evt) {
  1781. const openFileInputName = PDFViewerApplication.appConfig.openFileInputName;
  1782. document.getElementById(openFileInputName).click();
  1783. };
  1784. }
  1785. function webViewerPresentationMode() {
  1786. PDFViewerApplication.requestPresentationMode();
  1787. }
  1788. function webViewerPrint() {
  1789. PDFViewerApplication.triggerPrinting();
  1790. }
  1791. function webViewerDownload() {
  1792. PDFViewerApplication.downloadOrSave({
  1793. sourceEventType: "download"
  1794. });
  1795. }
  1796. function webViewerSave() {
  1797. PDFViewerApplication.downloadOrSave({
  1798. sourceEventType: "save"
  1799. });
  1800. }
  1801. function webViewerFirstPage() {
  1802. if (PDFViewerApplication.pdfDocument) {
  1803. PDFViewerApplication.page = 1;
  1804. }
  1805. }
  1806. function webViewerLastPage() {
  1807. if (PDFViewerApplication.pdfDocument) {
  1808. PDFViewerApplication.page = PDFViewerApplication.pagesCount;
  1809. }
  1810. }
  1811. function webViewerNextPage() {
  1812. PDFViewerApplication.pdfViewer.nextPage();
  1813. }
  1814. function webViewerPreviousPage() {
  1815. PDFViewerApplication.pdfViewer.previousPage();
  1816. }
  1817. function webViewerZoomIn() {
  1818. PDFViewerApplication.zoomIn();
  1819. }
  1820. function webViewerZoomOut() {
  1821. PDFViewerApplication.zoomOut();
  1822. }
  1823. function webViewerZoomReset() {
  1824. PDFViewerApplication.zoomReset();
  1825. }
  1826. function webViewerPageNumberChanged(evt) {
  1827. const pdfViewer = PDFViewerApplication.pdfViewer;
  1828. if (evt.value !== "") {
  1829. PDFViewerApplication.pdfLinkService.goToPage(evt.value);
  1830. }
  1831. if (evt.value !== pdfViewer.currentPageNumber.toString() && evt.value !== pdfViewer.currentPageLabel) {
  1832. PDFViewerApplication.toolbar.setPageNumber(pdfViewer.currentPageNumber, pdfViewer.currentPageLabel);
  1833. }
  1834. }
  1835. function webViewerScaleChanged(evt) {
  1836. PDFViewerApplication.pdfViewer.currentScaleValue = evt.value;
  1837. }
  1838. function webViewerRotateCw() {
  1839. PDFViewerApplication.rotatePages(90);
  1840. }
  1841. function webViewerRotateCcw() {
  1842. PDFViewerApplication.rotatePages(-90);
  1843. }
  1844. function webViewerOptionalContentConfig(evt) {
  1845. PDFViewerApplication.pdfViewer.optionalContentConfigPromise = evt.promise;
  1846. }
  1847. function webViewerSwitchScrollMode(evt) {
  1848. PDFViewerApplication.pdfViewer.scrollMode = evt.mode;
  1849. }
  1850. function webViewerSwitchSpreadMode(evt) {
  1851. PDFViewerApplication.pdfViewer.spreadMode = evt.mode;
  1852. }
  1853. function webViewerDocumentProperties() {
  1854. PDFViewerApplication.pdfDocumentProperties.open();
  1855. }
  1856. function webViewerFind(evt) {
  1857. PDFViewerApplication.findController.executeCommand("find" + evt.type, {
  1858. query: evt.query,
  1859. phraseSearch: evt.phraseSearch,
  1860. caseSensitive: evt.caseSensitive,
  1861. entireWord: evt.entireWord,
  1862. highlightAll: evt.highlightAll,
  1863. findPrevious: evt.findPrevious
  1864. });
  1865. }
  1866. function webViewerFindFromUrlHash(evt) {
  1867. PDFViewerApplication.findController.executeCommand("find", {
  1868. query: evt.query,
  1869. phraseSearch: evt.phraseSearch,
  1870. caseSensitive: false,
  1871. entireWord: false,
  1872. highlightAll: true,
  1873. findPrevious: false
  1874. });
  1875. }
  1876. function webViewerUpdateFindMatchesCount({
  1877. matchesCount
  1878. }) {
  1879. if (PDFViewerApplication.supportsIntegratedFind) {
  1880. PDFViewerApplication.externalServices.updateFindMatchesCount(matchesCount);
  1881. } else {
  1882. PDFViewerApplication.findBar.updateResultsCount(matchesCount);
  1883. }
  1884. }
  1885. function webViewerUpdateFindControlState({
  1886. state,
  1887. previous,
  1888. matchesCount,
  1889. rawQuery
  1890. }) {
  1891. if (PDFViewerApplication.supportsIntegratedFind) {
  1892. PDFViewerApplication.externalServices.updateFindControlState({
  1893. result: state,
  1894. findPrevious: previous,
  1895. matchesCount,
  1896. rawQuery
  1897. });
  1898. } else {
  1899. PDFViewerApplication.findBar.updateUIState(state, previous, matchesCount);
  1900. }
  1901. }
  1902. function webViewerScaleChanging(evt) {
  1903. PDFViewerApplication.toolbar.setPageScale(evt.presetValue, evt.scale);
  1904. PDFViewerApplication.pdfViewer.update();
  1905. }
  1906. function webViewerRotationChanging(evt) {
  1907. PDFViewerApplication.pdfThumbnailViewer.pagesRotation = evt.pagesRotation;
  1908. PDFViewerApplication.forceRendering();
  1909. PDFViewerApplication.pdfViewer.currentPageNumber = evt.pageNumber;
  1910. }
  1911. function webViewerPageChanging({
  1912. pageNumber,
  1913. pageLabel
  1914. }) {
  1915. PDFViewerApplication.toolbar.setPageNumber(pageNumber, pageLabel);
  1916. PDFViewerApplication.secondaryToolbar.setPageNumber(pageNumber);
  1917. if (PDFViewerApplication.pdfSidebar.isThumbnailViewVisible) {
  1918. PDFViewerApplication.pdfThumbnailViewer.scrollThumbnailIntoView(pageNumber);
  1919. }
  1920. }
  1921. function webViewerVisibilityChange(evt) {
  1922. if (document.visibilityState === "visible") {
  1923. setZoomDisabledTimeout();
  1924. }
  1925. }
  1926. let zoomDisabledTimeout = null;
  1927. function setZoomDisabledTimeout() {
  1928. if (zoomDisabledTimeout) {
  1929. clearTimeout(zoomDisabledTimeout);
  1930. }
  1931. zoomDisabledTimeout = setTimeout(function () {
  1932. zoomDisabledTimeout = null;
  1933. }, WHEEL_ZOOM_DISABLED_TIMEOUT);
  1934. }
  1935. function webViewerWheel(evt) {
  1936. const {
  1937. pdfViewer,
  1938. supportedMouseWheelZoomModifierKeys
  1939. } = PDFViewerApplication;
  1940. if (pdfViewer.isInPresentationMode) {
  1941. return;
  1942. }
  1943. if (evt.ctrlKey && supportedMouseWheelZoomModifierKeys.ctrlKey || evt.metaKey && supportedMouseWheelZoomModifierKeys.metaKey) {
  1944. evt.preventDefault();
  1945. if (zoomDisabledTimeout || document.visibilityState === "hidden") {
  1946. return;
  1947. }
  1948. const previousScale = pdfViewer.currentScale;
  1949. const delta = (0, _ui_utils.normalizeWheelEventDirection)(evt);
  1950. let ticks = 0;
  1951. if (evt.deltaMode === WheelEvent.DOM_DELTA_LINE || evt.deltaMode === WheelEvent.DOM_DELTA_PAGE) {
  1952. if (Math.abs(delta) >= 1) {
  1953. ticks = Math.sign(delta);
  1954. } else {
  1955. ticks = PDFViewerApplication.accumulateWheelTicks(delta);
  1956. }
  1957. } else {
  1958. const PIXELS_PER_LINE_SCALE = 30;
  1959. ticks = PDFViewerApplication.accumulateWheelTicks(delta / PIXELS_PER_LINE_SCALE);
  1960. }
  1961. if (ticks < 0) {
  1962. PDFViewerApplication.zoomOut(-ticks);
  1963. } else if (ticks > 0) {
  1964. PDFViewerApplication.zoomIn(ticks);
  1965. }
  1966. const currentScale = pdfViewer.currentScale;
  1967. if (previousScale !== currentScale) {
  1968. const scaleCorrectionFactor = currentScale / previousScale - 1;
  1969. const rect = pdfViewer.container.getBoundingClientRect();
  1970. const dx = evt.clientX - rect.left;
  1971. const dy = evt.clientY - rect.top;
  1972. pdfViewer.container.scrollLeft += dx * scaleCorrectionFactor;
  1973. pdfViewer.container.scrollTop += dy * scaleCorrectionFactor;
  1974. }
  1975. } else {
  1976. setZoomDisabledTimeout();
  1977. }
  1978. }
  1979. function webViewerTouchStart(evt) {
  1980. if (evt.touches.length > 1) {
  1981. evt.preventDefault();
  1982. }
  1983. }
  1984. function webViewerClick(evt) {
  1985. if (!PDFViewerApplication.secondaryToolbar.isOpen) {
  1986. return;
  1987. }
  1988. const appConfig = PDFViewerApplication.appConfig;
  1989. if (PDFViewerApplication.pdfViewer.containsElement(evt.target) || appConfig.toolbar.container.contains(evt.target) && evt.target !== appConfig.secondaryToolbar.toggleButton) {
  1990. PDFViewerApplication.secondaryToolbar.close();
  1991. }
  1992. }
  1993. function webViewerKeyDown(evt) {
  1994. if (PDFViewerApplication.overlayManager.active) {
  1995. return;
  1996. }
  1997. let handled = false,
  1998. ensureViewerFocused = false;
  1999. const cmd = (evt.ctrlKey ? 1 : 0) | (evt.altKey ? 2 : 0) | (evt.shiftKey ? 4 : 0) | (evt.metaKey ? 8 : 0);
  2000. const pdfViewer = PDFViewerApplication.pdfViewer;
  2001. const isViewerInPresentationMode = pdfViewer?.isInPresentationMode;
  2002. if (cmd === 1 || cmd === 8 || cmd === 5 || cmd === 12) {
  2003. switch (evt.keyCode) {
  2004. case 70:
  2005. if (!PDFViewerApplication.supportsIntegratedFind && !evt.shiftKey) {
  2006. PDFViewerApplication.findBar.open();
  2007. handled = true;
  2008. }
  2009. break;
  2010. case 71:
  2011. if (!PDFViewerApplication.supportsIntegratedFind) {
  2012. const findState = PDFViewerApplication.findController.state;
  2013. if (findState) {
  2014. PDFViewerApplication.findController.executeCommand("findagain", {
  2015. query: findState.query,
  2016. phraseSearch: findState.phraseSearch,
  2017. caseSensitive: findState.caseSensitive,
  2018. entireWord: findState.entireWord,
  2019. highlightAll: findState.highlightAll,
  2020. findPrevious: cmd === 5 || cmd === 12
  2021. });
  2022. }
  2023. handled = true;
  2024. }
  2025. break;
  2026. case 61:
  2027. case 107:
  2028. case 187:
  2029. case 171:
  2030. if (!isViewerInPresentationMode) {
  2031. PDFViewerApplication.zoomIn();
  2032. }
  2033. handled = true;
  2034. break;
  2035. case 173:
  2036. case 109:
  2037. case 189:
  2038. if (!isViewerInPresentationMode) {
  2039. PDFViewerApplication.zoomOut();
  2040. }
  2041. handled = true;
  2042. break;
  2043. case 48:
  2044. case 96:
  2045. if (!isViewerInPresentationMode) {
  2046. setTimeout(function () {
  2047. PDFViewerApplication.zoomReset();
  2048. });
  2049. handled = false;
  2050. }
  2051. break;
  2052. case 38:
  2053. if (isViewerInPresentationMode || PDFViewerApplication.page > 1) {
  2054. PDFViewerApplication.page = 1;
  2055. handled = true;
  2056. ensureViewerFocused = true;
  2057. }
  2058. break;
  2059. case 40:
  2060. if (isViewerInPresentationMode || PDFViewerApplication.page < PDFViewerApplication.pagesCount) {
  2061. PDFViewerApplication.page = PDFViewerApplication.pagesCount;
  2062. handled = true;
  2063. ensureViewerFocused = true;
  2064. }
  2065. break;
  2066. }
  2067. }
  2068. const {
  2069. eventBus
  2070. } = PDFViewerApplication;
  2071. if (cmd === 1 || cmd === 8) {
  2072. switch (evt.keyCode) {
  2073. case 83:
  2074. eventBus.dispatch("download", {
  2075. source: window
  2076. });
  2077. handled = true;
  2078. break;
  2079. case 79:
  2080. {
  2081. eventBus.dispatch("openfile", {
  2082. source: window
  2083. });
  2084. handled = true;
  2085. }
  2086. break;
  2087. }
  2088. }
  2089. if (cmd === 3 || cmd === 10) {
  2090. switch (evt.keyCode) {
  2091. case 80:
  2092. PDFViewerApplication.requestPresentationMode();
  2093. handled = true;
  2094. break;
  2095. case 71:
  2096. PDFViewerApplication.appConfig.toolbar.pageNumber.select();
  2097. handled = true;
  2098. break;
  2099. }
  2100. }
  2101. if (handled) {
  2102. if (ensureViewerFocused && !isViewerInPresentationMode) {
  2103. pdfViewer.focus();
  2104. }
  2105. evt.preventDefault();
  2106. return;
  2107. }
  2108. const curElement = (0, _ui_utils.getActiveOrFocusedElement)();
  2109. const curElementTagName = curElement?.tagName.toUpperCase();
  2110. if (curElementTagName === "INPUT" || curElementTagName === "TEXTAREA" || curElementTagName === "SELECT" || curElement?.isContentEditable) {
  2111. if (evt.keyCode !== 27) {
  2112. return;
  2113. }
  2114. }
  2115. if (cmd === 0) {
  2116. let turnPage = 0,
  2117. turnOnlyIfPageFit = false;
  2118. switch (evt.keyCode) {
  2119. case 38:
  2120. case 33:
  2121. if (pdfViewer.isVerticalScrollbarEnabled) {
  2122. turnOnlyIfPageFit = true;
  2123. }
  2124. turnPage = -1;
  2125. break;
  2126. case 8:
  2127. if (!isViewerInPresentationMode) {
  2128. turnOnlyIfPageFit = true;
  2129. }
  2130. turnPage = -1;
  2131. break;
  2132. case 37:
  2133. if (pdfViewer.isHorizontalScrollbarEnabled) {
  2134. turnOnlyIfPageFit = true;
  2135. }
  2136. case 75:
  2137. case 80:
  2138. turnPage = -1;
  2139. break;
  2140. case 27:
  2141. if (PDFViewerApplication.secondaryToolbar.isOpen) {
  2142. PDFViewerApplication.secondaryToolbar.close();
  2143. handled = true;
  2144. }
  2145. if (!PDFViewerApplication.supportsIntegratedFind && PDFViewerApplication.findBar.opened) {
  2146. PDFViewerApplication.findBar.close();
  2147. handled = true;
  2148. }
  2149. break;
  2150. case 40:
  2151. case 34:
  2152. if (pdfViewer.isVerticalScrollbarEnabled) {
  2153. turnOnlyIfPageFit = true;
  2154. }
  2155. turnPage = 1;
  2156. break;
  2157. case 13:
  2158. case 32:
  2159. if (!isViewerInPresentationMode) {
  2160. turnOnlyIfPageFit = true;
  2161. }
  2162. turnPage = 1;
  2163. break;
  2164. case 39:
  2165. if (pdfViewer.isHorizontalScrollbarEnabled) {
  2166. turnOnlyIfPageFit = true;
  2167. }
  2168. case 74:
  2169. case 78:
  2170. turnPage = 1;
  2171. break;
  2172. case 36:
  2173. if (isViewerInPresentationMode || PDFViewerApplication.page > 1) {
  2174. PDFViewerApplication.page = 1;
  2175. handled = true;
  2176. ensureViewerFocused = true;
  2177. }
  2178. break;
  2179. case 35:
  2180. if (isViewerInPresentationMode || PDFViewerApplication.page < PDFViewerApplication.pagesCount) {
  2181. PDFViewerApplication.page = PDFViewerApplication.pagesCount;
  2182. handled = true;
  2183. ensureViewerFocused = true;
  2184. }
  2185. break;
  2186. case 83:
  2187. PDFViewerApplication.pdfCursorTools.switchTool(_pdf_cursor_tools.CursorTool.SELECT);
  2188. break;
  2189. case 72:
  2190. PDFViewerApplication.pdfCursorTools.switchTool(_pdf_cursor_tools.CursorTool.HAND);
  2191. break;
  2192. case 82:
  2193. PDFViewerApplication.rotatePages(90);
  2194. break;
  2195. case 115:
  2196. PDFViewerApplication.pdfSidebar.toggle();
  2197. break;
  2198. }
  2199. if (turnPage !== 0 && (!turnOnlyIfPageFit || pdfViewer.currentScaleValue === "page-fit")) {
  2200. if (turnPage > 0) {
  2201. pdfViewer.nextPage();
  2202. } else {
  2203. pdfViewer.previousPage();
  2204. }
  2205. handled = true;
  2206. }
  2207. }
  2208. if (cmd === 4) {
  2209. switch (evt.keyCode) {
  2210. case 13:
  2211. case 32:
  2212. if (!isViewerInPresentationMode && pdfViewer.currentScaleValue !== "page-fit") {
  2213. break;
  2214. }
  2215. if (PDFViewerApplication.page > 1) {
  2216. PDFViewerApplication.page--;
  2217. }
  2218. handled = true;
  2219. break;
  2220. case 82:
  2221. PDFViewerApplication.rotatePages(-90);
  2222. break;
  2223. }
  2224. }
  2225. if (!handled && !isViewerInPresentationMode) {
  2226. if (evt.keyCode >= 33 && evt.keyCode <= 40 || evt.keyCode === 32 && curElementTagName !== "BUTTON") {
  2227. ensureViewerFocused = true;
  2228. }
  2229. }
  2230. if (ensureViewerFocused && !pdfViewer.containsElement(curElement)) {
  2231. pdfViewer.focus();
  2232. }
  2233. if (handled) {
  2234. evt.preventDefault();
  2235. }
  2236. }
  2237. function beforeUnload(evt) {
  2238. evt.preventDefault();
  2239. evt.returnValue = "";
  2240. return false;
  2241. }
  2242. const PDFPrintServiceFactory = {
  2243. instance: {
  2244. supportsPrinting: false,
  2245. createPrintService() {
  2246. throw new Error("Not implemented: createPrintService");
  2247. }
  2248. }
  2249. };
  2250. exports.PDFPrintServiceFactory = PDFPrintServiceFactory;