1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122 |
- import {
- AnnotationEditorType,
- AnnotationEditorUIManager,
- AnnotationMode,
- createPromiseCapability,
- PermissionFlag,
- PixelsPerInch,
- version,
- } from "pdfjs-lib";
- import {
- DEFAULT_SCALE,
- DEFAULT_SCALE_DELTA,
- DEFAULT_SCALE_VALUE,
- docStyle,
- getVisibleElements,
- isPortraitOrientation,
- isValidRotation,
- isValidScrollMode,
- isValidSpreadMode,
- MAX_AUTO_SCALE,
- MAX_SCALE,
- MIN_SCALE,
- PresentationModeState,
- RendererType,
- RenderingStates,
- SCROLLBAR_PADDING,
- scrollIntoView,
- ScrollMode,
- SpreadMode,
- TextLayerMode,
- UNKNOWN_SCALE,
- VERTICAL_PADDING,
- watchScroll,
- } from "./ui_utils.js";
- import { NullL10n } from "./l10n_utils.js";
- import { PDFPageView } from "./pdf_page_view.js";
- import { PDFRenderingQueue } from "./pdf_rendering_queue.js";
- import { SimpleLinkService } from "./pdf_link_service.js";
- const DEFAULT_CACHE_SIZE = 10;
- const ENABLE_PERMISSIONS_CLASS = "enablePermissions";
- const PagesCountLimit = {
- FORCE_SCROLL_MODE_PAGE: 15000,
- FORCE_LAZY_PAGE_INIT: 7500,
- PAUSE_EAGER_PAGE_INIT: 250,
- };
- function isValidAnnotationEditorMode(mode) {
- return (
- Object.values(AnnotationEditorType).includes(mode) &&
- mode !== AnnotationEditorType.DISABLE
- );
- }
- class PDFPageViewBuffer {
-
- #buf = new Set();
- #size = 0;
- constructor(size) {
- this.#size = size;
- }
- push(view) {
- const buf = this.#buf;
- if (buf.has(view)) {
- buf.delete(view);
- }
- buf.add(view);
- if (buf.size > this.#size) {
- this.#destroyFirstView();
- }
- }
-
- resize(newSize, idsToKeep = null) {
- this.#size = newSize;
- const buf = this.#buf;
- if (idsToKeep) {
- const ii = buf.size;
- let i = 1;
- for (const view of buf) {
- if (idsToKeep.has(view.id)) {
- buf.delete(view);
- buf.add(view);
- }
- if (++i > ii) {
- break;
- }
- }
- }
- while (buf.size > this.#size) {
- this.#destroyFirstView();
- }
- }
- has(view) {
- return this.#buf.has(view);
- }
- [Symbol.iterator]() {
- return this.#buf.keys();
- }
- #destroyFirstView() {
- const firstView = this.#buf.keys().next().value;
- firstView?.destroy();
- this.#buf.delete(firstView);
- }
- }
- class PDFViewer {
- #buffer = null;
- #annotationEditorMode = AnnotationEditorType.NONE;
- #annotationEditorUIManager = null;
- #annotationMode = AnnotationMode.ENABLE_FORMS;
- #containerTopLeft = null;
- #enablePermissions = false;
- #previousContainerHeight = 0;
- #resizeObserver = new ResizeObserver(this.#resizeObserverCallback.bind(this));
- #scrollModePageState = null;
- #onVisibilityChange = null;
- #scaleTimeoutId = null;
-
- constructor(options) {
- const viewerVersion =
- typeof PDFJSDev !== "undefined" ? PDFJSDev.eval("BUNDLE_VERSION") : null;
- if (version !== viewerVersion) {
- throw new Error(
- `The API version "${version}" does not match the Viewer version "${viewerVersion}".`
- );
- }
- this.container = options.container;
- this.#resizeObserver.observe(this.container);
- this.viewer = options.viewer || options.container.firstElementChild;
- if (
- typeof PDFJSDev === "undefined" ||
- PDFJSDev.test("!PRODUCTION || GENERIC")
- ) {
- if (
- !(
- this.container?.tagName.toUpperCase() === "DIV" &&
- this.viewer?.tagName.toUpperCase() === "DIV"
- )
- ) {
- throw new Error("Invalid `container` and/or `viewer` option.");
- }
- if (
- this.container.offsetParent &&
- getComputedStyle(this.container).position !== "absolute"
- ) {
- throw new Error("The `container` must be absolutely positioned.");
- }
- }
- this.eventBus = options.eventBus;
- this.linkService = options.linkService || new SimpleLinkService();
- this.downloadManager = options.downloadManager || null;
- this.findController = options.findController || null;
- this._scriptingManager = options.scriptingManager || null;
- this.removePageBorders = options.removePageBorders || false;
- this.textLayerMode = options.textLayerMode ?? TextLayerMode.ENABLE;
- this.#annotationMode =
- options.annotationMode ?? AnnotationMode.ENABLE_FORMS;
- this.#annotationEditorMode =
- options.annotationEditorMode ?? AnnotationEditorType.NONE;
- this.imageResourcesPath = options.imageResourcesPath || "";
- this.enablePrintAutoRotate = options.enablePrintAutoRotate || false;
- if (
- typeof PDFJSDev === "undefined" ||
- PDFJSDev.test("!PRODUCTION || GENERIC")
- ) {
- this.renderer = options.renderer || RendererType.CANVAS;
- }
- this.useOnlyCssZoom = options.useOnlyCssZoom || false;
- this.isOffscreenCanvasSupported =
- options.isOffscreenCanvasSupported ?? true;
- this.maxCanvasPixels = options.maxCanvasPixels;
- this.l10n = options.l10n || NullL10n;
- this.#enablePermissions = options.enablePermissions || false;
- this.pageColors = options.pageColors || null;
- if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
- if (
- this.pageColors &&
- !(
- CSS.supports("color", this.pageColors.background) &&
- CSS.supports("color", this.pageColors.foreground)
- )
- ) {
- if (this.pageColors.background || this.pageColors.foreground) {
- console.warn(
- "PDFViewer: Ignoring `pageColors`-option, since the browser doesn't support the values used."
- );
- }
- this.pageColors = null;
- }
- }
- this.defaultRenderingQueue = !options.renderingQueue;
- if (this.defaultRenderingQueue) {
-
- this.renderingQueue = new PDFRenderingQueue();
- this.renderingQueue.setViewer(this);
- } else {
- this.renderingQueue = options.renderingQueue;
- }
- this.scroll = watchScroll(this.container, this._scrollUpdate.bind(this));
- this.presentationModeState = PresentationModeState.UNKNOWN;
- this._onBeforeDraw = this._onAfterDraw = null;
- this._resetView();
- if (this.removePageBorders) {
- this.viewer.classList.add("removePageBorders");
- }
- this.#updateContainerHeightCss();
- }
- get pagesCount() {
- return this._pages.length;
- }
- getPageView(index) {
- return this._pages[index];
- }
-
- get pageViewsReady() {
- if (!this._pagesCapability.settled) {
- return false;
- }
-
-
- return this._pages.every(function (pageView) {
- return pageView?.pdfPage;
- });
- }
-
- get renderForms() {
- return this.#annotationMode === AnnotationMode.ENABLE_FORMS;
- }
-
- get enableScripting() {
- return !!this._scriptingManager;
- }
-
- get currentPageNumber() {
- return this._currentPageNumber;
- }
-
- set currentPageNumber(val) {
- if (!Number.isInteger(val)) {
- throw new Error("Invalid page number.");
- }
- if (!this.pdfDocument) {
- return;
- }
-
- if (!this._setCurrentPageNumber(val, true)) {
- console.error(`currentPageNumber: "${val}" is not a valid page.`);
- }
- }
-
- _setCurrentPageNumber(val, resetCurrentPageView = false) {
- if (this._currentPageNumber === val) {
- if (resetCurrentPageView) {
- this.#resetCurrentPageView();
- }
- return true;
- }
- if (!(0 < val && val <= this.pagesCount)) {
- return false;
- }
- const previous = this._currentPageNumber;
- this._currentPageNumber = val;
- this.eventBus.dispatch("pagechanging", {
- source: this,
- pageNumber: val,
- pageLabel: this._pageLabels?.[val - 1] ?? null,
- previous,
- });
- if (resetCurrentPageView) {
- this.#resetCurrentPageView();
- }
- return true;
- }
-
- get currentPageLabel() {
- return this._pageLabels?.[this._currentPageNumber - 1] ?? null;
- }
-
- set currentPageLabel(val) {
- if (!this.pdfDocument) {
- return;
- }
- let page = val | 0;
- if (this._pageLabels) {
- const i = this._pageLabels.indexOf(val);
- if (i >= 0) {
- page = i + 1;
- }
- }
-
- if (!this._setCurrentPageNumber(page, true)) {
- console.error(`currentPageLabel: "${val}" is not a valid page.`);
- }
- }
-
- get currentScale() {
- return this._currentScale !== UNKNOWN_SCALE
- ? this._currentScale
- : DEFAULT_SCALE;
- }
-
- set currentScale(val) {
- if (isNaN(val)) {
- throw new Error("Invalid numeric scale.");
- }
- if (!this.pdfDocument) {
- return;
- }
- this._setScale(val, { noScroll: false });
- }
-
- get currentScaleValue() {
- return this._currentScaleValue;
- }
-
- set currentScaleValue(val) {
- if (!this.pdfDocument) {
- return;
- }
- this._setScale(val, { noScroll: false });
- }
-
- get pagesRotation() {
- return this._pagesRotation;
- }
-
- set pagesRotation(rotation) {
- if (!isValidRotation(rotation)) {
- throw new Error("Invalid pages rotation angle.");
- }
- if (!this.pdfDocument) {
- return;
- }
-
- rotation %= 360;
- if (rotation < 0) {
- rotation += 360;
- }
- if (this._pagesRotation === rotation) {
- return;
- }
- this._pagesRotation = rotation;
- const pageNumber = this._currentPageNumber;
- this.refresh(true, { rotation });
-
-
- if (this._currentScaleValue) {
- this._setScale(this._currentScaleValue, { noScroll: true });
- }
- this.eventBus.dispatch("rotationchanging", {
- source: this,
- pagesRotation: rotation,
- pageNumber,
- });
- if (this.defaultRenderingQueue) {
- this.update();
- }
- }
- get firstPagePromise() {
- return this.pdfDocument ? this._firstPageCapability.promise : null;
- }
- get onePageRendered() {
- return this.pdfDocument ? this._onePageRenderedCapability.promise : null;
- }
- get pagesPromise() {
- return this.pdfDocument ? this._pagesCapability.promise : null;
- }
- #layerProperties() {
- const self = this;
- return {
- get annotationEditorUIManager() {
- return self.#annotationEditorUIManager;
- },
- get annotationStorage() {
- return self.pdfDocument?.annotationStorage;
- },
- get downloadManager() {
- return self.downloadManager;
- },
- get enableScripting() {
- return !!self._scriptingManager;
- },
- get fieldObjectsPromise() {
- return self.pdfDocument?.getFieldObjects();
- },
- get findController() {
- return self.findController;
- },
- get hasJSActionsPromise() {
- return self.pdfDocument?.hasJSActions();
- },
- get linkService() {
- return self.linkService;
- },
- };
- }
-
- #initializePermissions(permissions) {
- const params = {
- annotationEditorMode: this.#annotationEditorMode,
- annotationMode: this.#annotationMode,
- textLayerMode: this.textLayerMode,
- };
- if (!permissions) {
- return params;
- }
- if (!permissions.includes(PermissionFlag.COPY)) {
- this.viewer.classList.add(ENABLE_PERMISSIONS_CLASS);
- }
- if (!permissions.includes(PermissionFlag.MODIFY_CONTENTS)) {
- params.annotationEditorMode = AnnotationEditorType.DISABLE;
- }
- if (
- !permissions.includes(PermissionFlag.MODIFY_ANNOTATIONS) &&
- !permissions.includes(PermissionFlag.FILL_INTERACTIVE_FORMS) &&
- this.#annotationMode === AnnotationMode.ENABLE_FORMS
- ) {
- params.annotationMode = AnnotationMode.ENABLE;
- }
- return params;
- }
- #onePageRenderedOrForceFetch() {
-
-
-
-
-
-
-
-
-
-
- if (
- document.visibilityState === "hidden" ||
- !this.container.offsetParent ||
- this._getVisiblePages().views.length === 0
- ) {
- return Promise.resolve();
- }
-
-
- const visibilityChangePromise = new Promise(resolve => {
- this.#onVisibilityChange = () => {
- if (document.visibilityState !== "hidden") {
- return;
- }
- resolve();
- document.removeEventListener(
- "visibilitychange",
- this.#onVisibilityChange
- );
- this.#onVisibilityChange = null;
- };
- document.addEventListener("visibilitychange", this.#onVisibilityChange);
- });
- return Promise.race([
- this._onePageRenderedCapability.promise,
- visibilityChangePromise,
- ]);
- }
-
- setDocument(pdfDocument) {
- if (this.pdfDocument) {
- this.eventBus.dispatch("pagesdestroy", { source: this });
- this._cancelRendering();
- this._resetView();
- this.findController?.setDocument(null);
- this._scriptingManager?.setDocument(null);
- if (this.#annotationEditorUIManager) {
- this.#annotationEditorUIManager.destroy();
- this.#annotationEditorUIManager = null;
- }
- }
- this.pdfDocument = pdfDocument;
- if (!pdfDocument) {
- return;
- }
- const pagesCount = pdfDocument.numPages;
- const firstPagePromise = pdfDocument.getPage(1);
-
- const optionalContentConfigPromise = pdfDocument.getOptionalContentConfig();
- const permissionsPromise = this.#enablePermissions
- ? pdfDocument.getPermissions()
- : Promise.resolve();
-
-
- if (pagesCount > PagesCountLimit.FORCE_SCROLL_MODE_PAGE) {
- console.warn(
- "Forcing PAGE-scrolling for performance reasons, given the length of the document."
- );
- const mode = (this._scrollMode = ScrollMode.PAGE);
- this.eventBus.dispatch("scrollmodechanged", { source: this, mode });
- }
- this._pagesCapability.promise.then(
- () => {
- this.eventBus.dispatch("pagesloaded", { source: this, pagesCount });
- },
- () => {
-
- }
- );
- this._onBeforeDraw = evt => {
- const pageView = this._pages[evt.pageNumber - 1];
- if (!pageView) {
- return;
- }
-
-
- this.#buffer.push(pageView);
- };
- this.eventBus._on("pagerender", this._onBeforeDraw);
- this._onAfterDraw = evt => {
- if (evt.cssTransform || this._onePageRenderedCapability.settled) {
- return;
- }
- this._onePageRenderedCapability.resolve({ timestamp: evt.timestamp });
- this.eventBus._off("pagerendered", this._onAfterDraw);
- this._onAfterDraw = null;
- if (this.#onVisibilityChange) {
- document.removeEventListener(
- "visibilitychange",
- this.#onVisibilityChange
- );
- this.#onVisibilityChange = null;
- }
- };
- this.eventBus._on("pagerendered", this._onAfterDraw);
-
-
- Promise.all([firstPagePromise, permissionsPromise])
- .then(([firstPdfPage, permissions]) => {
- if (pdfDocument !== this.pdfDocument) {
- return;
- }
- this._firstPageCapability.resolve(firstPdfPage);
- this._optionalContentConfigPromise = optionalContentConfigPromise;
- const { annotationEditorMode, annotationMode, textLayerMode } =
- this.#initializePermissions(permissions);
- if (annotationEditorMode !== AnnotationEditorType.DISABLE) {
- const mode = annotationEditorMode;
- if (pdfDocument.isPureXfa) {
- console.warn("Warning: XFA-editing is not implemented.");
- } else if (isValidAnnotationEditorMode(mode)) {
- this.#annotationEditorUIManager = new AnnotationEditorUIManager(
- this.container,
- this.eventBus,
- pdfDocument?.annotationStorage
- );
- if (mode !== AnnotationEditorType.NONE) {
- this.#annotationEditorUIManager.updateMode(mode);
- }
- } else {
- console.error(`Invalid AnnotationEditor mode: ${mode}`);
- }
- }
- const layerProperties = this.#layerProperties.bind(this);
- const viewerElement =
- this._scrollMode === ScrollMode.PAGE ? null : this.viewer;
- const scale = this.currentScale;
- const viewport = firstPdfPage.getViewport({
- scale: scale * PixelsPerInch.PDF_TO_CSS_UNITS,
- });
-
-
- docStyle.setProperty("--scale-factor", viewport.scale);
- for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) {
- const pageView = new PDFPageView({
- container: viewerElement,
- eventBus: this.eventBus,
- id: pageNum,
- scale,
- defaultViewport: viewport.clone(),
- optionalContentConfigPromise,
- renderingQueue: this.renderingQueue,
- textLayerMode,
- annotationMode,
- imageResourcesPath: this.imageResourcesPath,
- renderer:
- typeof PDFJSDev === "undefined" ||
- PDFJSDev.test("!PRODUCTION || GENERIC")
- ? this.renderer
- : null,
- useOnlyCssZoom: this.useOnlyCssZoom,
- isOffscreenCanvasSupported: this.isOffscreenCanvasSupported,
- maxCanvasPixels: this.maxCanvasPixels,
- pageColors: this.pageColors,
- l10n: this.l10n,
- layerProperties,
- });
- this._pages.push(pageView);
- }
-
-
-
- const firstPageView = this._pages[0];
- if (firstPageView) {
- firstPageView.setPdfPage(firstPdfPage);
- this.linkService.cachePageRef(1, firstPdfPage.ref);
- }
- if (this._scrollMode === ScrollMode.PAGE) {
-
- this.#ensurePageViewVisible();
- } else if (this._spreadMode !== SpreadMode.NONE) {
- this._updateSpreadMode();
- }
-
-
-
- this.#onePageRenderedOrForceFetch().then(async () => {
- this.findController?.setDocument(pdfDocument);
- this._scriptingManager?.setDocument(pdfDocument);
- if (this.#annotationEditorUIManager) {
-
- this.eventBus.dispatch("annotationeditormodechanged", {
- source: this,
- mode: this.#annotationEditorMode,
- });
- }
-
-
- if (
- pdfDocument.loadingParams.disableAutoFetch ||
- pagesCount > PagesCountLimit.FORCE_LAZY_PAGE_INIT
- ) {
-
- this._pagesCapability.resolve();
- return;
- }
- let getPagesLeft = pagesCount - 1;
- if (getPagesLeft <= 0) {
- this._pagesCapability.resolve();
- return;
- }
- for (let pageNum = 2; pageNum <= pagesCount; ++pageNum) {
- const promise = pdfDocument.getPage(pageNum).then(
- pdfPage => {
- const pageView = this._pages[pageNum - 1];
- if (!pageView.pdfPage) {
- pageView.setPdfPage(pdfPage);
- }
- this.linkService.cachePageRef(pageNum, pdfPage.ref);
- if (--getPagesLeft === 0) {
- this._pagesCapability.resolve();
- }
- },
- reason => {
- console.error(
- `Unable to get page ${pageNum} to initialize viewer`,
- reason
- );
- if (--getPagesLeft === 0) {
- this._pagesCapability.resolve();
- }
- }
- );
- if (pageNum % PagesCountLimit.PAUSE_EAGER_PAGE_INIT === 0) {
- await promise;
- }
- }
- });
- this.eventBus.dispatch("pagesinit", { source: this });
- pdfDocument.getMetadata().then(({ info }) => {
- if (pdfDocument !== this.pdfDocument) {
- return;
- }
- if (info.Language) {
- this.viewer.lang = info.Language;
- }
- });
- if (this.defaultRenderingQueue) {
- this.update();
- }
- })
- .catch(reason => {
- console.error("Unable to initialize viewer", reason);
- this._pagesCapability.reject(reason);
- });
- }
-
- setPageLabels(labels) {
- if (!this.pdfDocument) {
- return;
- }
- if (!labels) {
- this._pageLabels = null;
- } else if (
- !(Array.isArray(labels) && this.pdfDocument.numPages === labels.length)
- ) {
- this._pageLabels = null;
- console.error(`setPageLabels: Invalid page labels.`);
- } else {
- this._pageLabels = labels;
- }
-
- for (let i = 0, ii = this._pages.length; i < ii; i++) {
- this._pages[i].setPageLabel(this._pageLabels?.[i] ?? null);
- }
- }
- _resetView() {
- this._pages = [];
- this._currentPageNumber = 1;
- this._currentScale = UNKNOWN_SCALE;
- this._currentScaleValue = null;
- this._pageLabels = null;
- this.#buffer = new PDFPageViewBuffer(DEFAULT_CACHE_SIZE);
- this._location = null;
- this._pagesRotation = 0;
- this._optionalContentConfigPromise = null;
- this._firstPageCapability = createPromiseCapability();
- this._onePageRenderedCapability = createPromiseCapability();
- this._pagesCapability = createPromiseCapability();
- this._scrollMode = ScrollMode.VERTICAL;
- this._previousScrollMode = ScrollMode.UNKNOWN;
- this._spreadMode = SpreadMode.NONE;
- this.#scrollModePageState = {
- previousPageNumber: 1,
- scrollDown: true,
- pages: [],
- };
- if (this._onBeforeDraw) {
- this.eventBus._off("pagerender", this._onBeforeDraw);
- this._onBeforeDraw = null;
- }
- if (this._onAfterDraw) {
- this.eventBus._off("pagerendered", this._onAfterDraw);
- this._onAfterDraw = null;
- }
- if (this.#onVisibilityChange) {
- document.removeEventListener(
- "visibilitychange",
- this.#onVisibilityChange
- );
- this.#onVisibilityChange = null;
- }
-
- this.viewer.textContent = "";
-
- this._updateScrollMode();
- this.viewer.removeAttribute("lang");
-
- this.viewer.classList.remove(ENABLE_PERMISSIONS_CLASS);
- }
- #ensurePageViewVisible() {
- if (this._scrollMode !== ScrollMode.PAGE) {
- throw new Error("#ensurePageViewVisible: Invalid scrollMode value.");
- }
- const pageNumber = this._currentPageNumber,
- state = this.#scrollModePageState,
- viewer = this.viewer;
-
- viewer.textContent = "";
-
- state.pages.length = 0;
- if (this._spreadMode === SpreadMode.NONE && !this.isInPresentationMode) {
-
- const pageView = this._pages[pageNumber - 1];
- viewer.append(pageView.div);
- state.pages.push(pageView);
- } else {
- const pageIndexSet = new Set(),
- parity = this._spreadMode - 1;
-
- if (parity === -1) {
-
- pageIndexSet.add(pageNumber - 1);
- } else if (pageNumber % 2 !== parity) {
-
- pageIndexSet.add(pageNumber - 1);
- pageIndexSet.add(pageNumber);
- } else {
-
- pageIndexSet.add(pageNumber - 2);
- pageIndexSet.add(pageNumber - 1);
- }
-
- const spread = document.createElement("div");
- spread.className = "spread";
- if (this.isInPresentationMode) {
- const dummyPage = document.createElement("div");
- dummyPage.className = "dummyPage";
- spread.append(dummyPage);
- }
- for (const i of pageIndexSet) {
- const pageView = this._pages[i];
- if (!pageView) {
- continue;
- }
- spread.append(pageView.div);
- state.pages.push(pageView);
- }
- viewer.append(spread);
- }
- state.scrollDown = pageNumber >= state.previousPageNumber;
- state.previousPageNumber = pageNumber;
- }
- _scrollUpdate() {
- if (this.pagesCount === 0) {
- return;
- }
- this.update();
- }
- #scrollIntoView(pageView, pageSpot = null) {
- const { div, id } = pageView;
-
-
- if (this._currentPageNumber !== id) {
- this._setCurrentPageNumber(id);
- }
- if (this._scrollMode === ScrollMode.PAGE) {
- this.#ensurePageViewVisible();
-
-
- this.update();
- }
- if (!pageSpot && !this.isInPresentationMode) {
- const left = div.offsetLeft + div.clientLeft,
- right = left + div.clientWidth;
- const { scrollLeft, clientWidth } = this.container;
- if (
- this._scrollMode === ScrollMode.HORIZONTAL ||
- left < scrollLeft ||
- right > scrollLeft + clientWidth
- ) {
- pageSpot = { left: 0, top: 0 };
- }
- }
- scrollIntoView(div, pageSpot);
-
-
-
-
-
- if (!this._currentScaleValue && this._location) {
- this._location = null;
- }
- }
-
- #isSameScale(newScale) {
- return (
- newScale === this._currentScale ||
- Math.abs(newScale - this._currentScale) < 1e-15
- );
- }
- _setScaleUpdatePages(
- newScale,
- newValue,
- { noScroll = false, preset = false, delay: drawingDelay = -1 }
- ) {
- this._currentScaleValue = newValue.toString();
- if (this.#isSameScale(newScale)) {
- if (preset) {
- this.eventBus.dispatch("scalechanging", {
- source: this,
- scale: newScale,
- presetValue: newValue,
- });
- }
- return;
- }
- docStyle.setProperty(
- "--scale-factor",
- newScale * PixelsPerInch.PDF_TO_CSS_UNITS
- );
- const mustPostponeDrawing = drawingDelay >= 0 && drawingDelay < 1000;
- const updateArgs = {
- scale: newScale,
- };
- if (mustPostponeDrawing) {
- updateArgs.drawingDelay = drawingDelay;
- }
- this.refresh(true, updateArgs);
- if (mustPostponeDrawing) {
- this.#scaleTimeoutId = setTimeout(() => {
- this.#scaleTimeoutId = null;
- this.refresh();
- }, drawingDelay);
- }
- this._currentScale = newScale;
- if (!noScroll) {
- let page = this._currentPageNumber,
- dest;
- if (
- this._location &&
- !(this.isInPresentationMode || this.isChangingPresentationMode)
- ) {
- page = this._location.pageNumber;
- dest = [
- null,
- { name: "XYZ" },
- this._location.left,
- this._location.top,
- null,
- ];
- }
- this.scrollPageIntoView({
- pageNumber: page,
- destArray: dest,
- allowNegativeOffset: true,
- });
- }
- this.eventBus.dispatch("scalechanging", {
- source: this,
- scale: newScale,
- presetValue: preset ? newValue : undefined,
- });
- if (this.defaultRenderingQueue) {
- this.update();
- }
- }
-
- get _pageWidthScaleFactor() {
- if (
- this._spreadMode !== SpreadMode.NONE &&
- this._scrollMode !== ScrollMode.HORIZONTAL
- ) {
- return 2;
- }
- return 1;
- }
- _setScale(value, options) {
- let scale = parseFloat(value);
- if (scale > 0) {
- options.preset = false;
- this._setScaleUpdatePages(scale, value, options);
- } else {
- const currentPage = this._pages[this._currentPageNumber - 1];
- if (!currentPage) {
- return;
- }
- let hPadding = SCROLLBAR_PADDING,
- vPadding = VERTICAL_PADDING;
- if (this.isInPresentationMode) {
-
-
- hPadding = vPadding = 4;
- if (this._spreadMode !== SpreadMode.NONE) {
-
-
- hPadding *= 2;
- }
- } else if (this.removePageBorders) {
- hPadding = vPadding = 0;
- } else if (this._scrollMode === ScrollMode.HORIZONTAL) {
- [hPadding, vPadding] = [vPadding, hPadding];
- }
- const pageWidthScale =
- (((this.container.clientWidth - hPadding) / currentPage.width) *
- currentPage.scale) /
- this._pageWidthScaleFactor;
- const pageHeightScale =
- ((this.container.clientHeight - vPadding) / currentPage.height) *
- currentPage.scale;
- switch (value) {
- case "page-actual":
- scale = 1;
- break;
- case "page-width":
- scale = pageWidthScale;
- break;
- case "page-height":
- scale = pageHeightScale;
- break;
- case "page-fit":
- scale = Math.min(pageWidthScale, pageHeightScale);
- break;
- case "auto":
-
-
- const horizontalScale = isPortraitOrientation(currentPage)
- ? pageWidthScale
- : Math.min(pageHeightScale, pageWidthScale);
- scale = Math.min(MAX_AUTO_SCALE, horizontalScale);
- break;
- default:
- console.error(`_setScale: "${value}" is an unknown zoom value.`);
- return;
- }
- options.preset = true;
- this._setScaleUpdatePages(scale, value, options);
- }
- }
-
- #resetCurrentPageView() {
- const pageView = this._pages[this._currentPageNumber - 1];
- if (this.isInPresentationMode) {
-
- this._setScale(this._currentScaleValue, { noScroll: true });
- }
- this.#scrollIntoView(pageView);
- }
-
- pageLabelToPageNumber(label) {
- if (!this._pageLabels) {
- return null;
- }
- const i = this._pageLabels.indexOf(label);
- if (i < 0) {
- return null;
- }
- return i + 1;
- }
-
-
- scrollPageIntoView({
- pageNumber,
- destArray = null,
- allowNegativeOffset = false,
- ignoreDestinationZoom = false,
- }) {
- if (!this.pdfDocument) {
- return;
- }
- const pageView =
- Number.isInteger(pageNumber) && this._pages[pageNumber - 1];
- if (!pageView) {
- console.error(
- `scrollPageIntoView: "${pageNumber}" is not a valid pageNumber parameter.`
- );
- return;
- }
- if (this.isInPresentationMode || !destArray) {
- this._setCurrentPageNumber(pageNumber, true);
- return;
- }
- let x = 0,
- y = 0;
- let width = 0,
- height = 0,
- widthScale,
- heightScale;
- const changeOrientation = pageView.rotation % 180 !== 0;
- const pageWidth =
- (changeOrientation ? pageView.height : pageView.width) /
- pageView.scale /
- PixelsPerInch.PDF_TO_CSS_UNITS;
- const pageHeight =
- (changeOrientation ? pageView.width : pageView.height) /
- pageView.scale /
- PixelsPerInch.PDF_TO_CSS_UNITS;
- let scale = 0;
- switch (destArray[1].name) {
- case "XYZ":
- x = destArray[2];
- y = destArray[3];
- scale = destArray[4];
-
-
-
-
- x = x !== null ? x : 0;
- y = y !== null ? y : pageHeight;
- break;
- case "Fit":
- case "FitB":
- scale = "page-fit";
- break;
- case "FitH":
- case "FitBH":
- y = destArray[2];
- scale = "page-width";
-
-
- if (y === null && this._location) {
- x = this._location.left;
- y = this._location.top;
- } else if (typeof y !== "number" || y < 0) {
-
-
- y = pageHeight;
- }
- break;
- case "FitV":
- case "FitBV":
- x = destArray[2];
- width = pageWidth;
- height = pageHeight;
- scale = "page-height";
- break;
- case "FitR":
- x = destArray[2];
- y = destArray[3];
- width = destArray[4] - x;
- height = destArray[5] - y;
- const hPadding = this.removePageBorders ? 0 : SCROLLBAR_PADDING;
- const vPadding = this.removePageBorders ? 0 : VERTICAL_PADDING;
- widthScale =
- (this.container.clientWidth - hPadding) /
- width /
- PixelsPerInch.PDF_TO_CSS_UNITS;
- heightScale =
- (this.container.clientHeight - vPadding) /
- height /
- PixelsPerInch.PDF_TO_CSS_UNITS;
- scale = Math.min(Math.abs(widthScale), Math.abs(heightScale));
- break;
- default:
- console.error(
- `scrollPageIntoView: "${destArray[1].name}" is not a valid destination type.`
- );
- return;
- }
- if (!ignoreDestinationZoom) {
- if (scale && scale !== this._currentScale) {
- this.currentScaleValue = scale;
- } else if (this._currentScale === UNKNOWN_SCALE) {
- this.currentScaleValue = DEFAULT_SCALE_VALUE;
- }
- }
- if (scale === "page-fit" && !destArray[4]) {
- this.#scrollIntoView(pageView);
- return;
- }
- const boundingRect = [
- pageView.viewport.convertToViewportPoint(x, y),
- pageView.viewport.convertToViewportPoint(x + width, y + height),
- ];
- let left = Math.min(boundingRect[0][0], boundingRect[1][0]);
- let top = Math.min(boundingRect[0][1], boundingRect[1][1]);
- if (!allowNegativeOffset) {
-
-
-
- left = Math.max(left, 0);
- top = Math.max(top, 0);
- }
- this.#scrollIntoView(pageView, { left, top });
- }
- _updateLocation(firstPage) {
- const currentScale = this._currentScale;
- const currentScaleValue = this._currentScaleValue;
- const normalizedScaleValue =
- parseFloat(currentScaleValue) === currentScale
- ? Math.round(currentScale * 10000) / 100
- : currentScaleValue;
- const pageNumber = firstPage.id;
- const currentPageView = this._pages[pageNumber - 1];
- const container = this.container;
- const topLeft = currentPageView.getPagePoint(
- container.scrollLeft - firstPage.x,
- container.scrollTop - firstPage.y
- );
- const intLeft = Math.round(topLeft[0]);
- const intTop = Math.round(topLeft[1]);
- let pdfOpenParams = `#page=${pageNumber}`;
- if (!this.isInPresentationMode) {
- pdfOpenParams += `&zoom=${normalizedScaleValue},${intLeft},${intTop}`;
- }
- this._location = {
- pageNumber,
- scale: normalizedScaleValue,
- top: intTop,
- left: intLeft,
- rotation: this._pagesRotation,
- pdfOpenParams,
- };
- }
- update() {
- const visible = this._getVisiblePages();
- const visiblePages = visible.views,
- numVisiblePages = visiblePages.length;
- if (numVisiblePages === 0) {
- return;
- }
- const newCacheSize = Math.max(DEFAULT_CACHE_SIZE, 2 * numVisiblePages + 1);
- this.#buffer.resize(newCacheSize, visible.ids);
- this.renderingQueue.renderHighestPriority(visible);
- const isSimpleLayout =
- this._spreadMode === SpreadMode.NONE &&
- (this._scrollMode === ScrollMode.PAGE ||
- this._scrollMode === ScrollMode.VERTICAL);
- const currentId = this._currentPageNumber;
- let stillFullyVisible = false;
- for (const page of visiblePages) {
- if (page.percent < 100) {
- break;
- }
- if (page.id === currentId && isSimpleLayout) {
- stillFullyVisible = true;
- break;
- }
- }
- this._setCurrentPageNumber(
- stillFullyVisible ? currentId : visiblePages[0].id
- );
- this._updateLocation(visible.first);
- this.eventBus.dispatch("updateviewarea", {
- source: this,
- location: this._location,
- });
- }
- containsElement(element) {
- return this.container.contains(element);
- }
- focus() {
- this.container.focus();
- }
- get _isContainerRtl() {
- return getComputedStyle(this.container).direction === "rtl";
- }
- get isInPresentationMode() {
- return this.presentationModeState === PresentationModeState.FULLSCREEN;
- }
- get isChangingPresentationMode() {
- return this.presentationModeState === PresentationModeState.CHANGING;
- }
- get isHorizontalScrollbarEnabled() {
- return this.isInPresentationMode
- ? false
- : this.container.scrollWidth > this.container.clientWidth;
- }
- get isVerticalScrollbarEnabled() {
- return this.isInPresentationMode
- ? false
- : this.container.scrollHeight > this.container.clientHeight;
- }
- _getVisiblePages() {
- const views =
- this._scrollMode === ScrollMode.PAGE
- ? this.#scrollModePageState.pages
- : this._pages,
- horizontal = this._scrollMode === ScrollMode.HORIZONTAL,
- rtl = horizontal && this._isContainerRtl;
- return getVisibleElements({
- scrollEl: this.container,
- views,
- sortByVisibility: true,
- horizontal,
- rtl,
- });
- }
-
- isPageVisible(pageNumber) {
- if (!this.pdfDocument) {
- return false;
- }
- if (
- !(
- Number.isInteger(pageNumber) &&
- pageNumber > 0 &&
- pageNumber <= this.pagesCount
- )
- ) {
- console.error(`isPageVisible: "${pageNumber}" is not a valid page.`);
- return false;
- }
- return this._getVisiblePages().ids.has(pageNumber);
- }
-
- isPageCached(pageNumber) {
- if (!this.pdfDocument) {
- return false;
- }
- if (
- !(
- Number.isInteger(pageNumber) &&
- pageNumber > 0 &&
- pageNumber <= this.pagesCount
- )
- ) {
- console.error(`isPageCached: "${pageNumber}" is not a valid page.`);
- return false;
- }
- const pageView = this._pages[pageNumber - 1];
- return this.#buffer.has(pageView);
- }
- cleanup() {
- for (const pageView of this._pages) {
- if (pageView.renderingState !== RenderingStates.FINISHED) {
- pageView.reset();
- }
- }
- }
-
- _cancelRendering() {
- for (const pageView of this._pages) {
- pageView.cancelRendering();
- }
- }
-
- async #ensurePdfPageLoaded(pageView) {
- if (pageView.pdfPage) {
- return pageView.pdfPage;
- }
- try {
- const pdfPage = await this.pdfDocument.getPage(pageView.id);
- if (!pageView.pdfPage) {
- pageView.setPdfPage(pdfPage);
- }
- if (!this.linkService._cachedPageNumber?.(pdfPage.ref)) {
- this.linkService.cachePageRef(pageView.id, pdfPage.ref);
- }
- return pdfPage;
- } catch (reason) {
- console.error("Unable to get page for page view", reason);
- return null;
- }
- }
- #getScrollAhead(visible) {
- if (visible.first?.id === 1) {
- return true;
- } else if (visible.last?.id === this.pagesCount) {
- return false;
- }
- switch (this._scrollMode) {
- case ScrollMode.PAGE:
- return this.#scrollModePageState.scrollDown;
- case ScrollMode.HORIZONTAL:
- return this.scroll.right;
- }
- return this.scroll.down;
- }
- forceRendering(currentlyVisiblePages) {
- const visiblePages = currentlyVisiblePages || this._getVisiblePages();
- const scrollAhead = this.#getScrollAhead(visiblePages);
- const preRenderExtra =
- this._spreadMode !== SpreadMode.NONE &&
- this._scrollMode !== ScrollMode.HORIZONTAL;
- const pageView = this.renderingQueue.getHighestPriority(
- visiblePages,
- this._pages,
- scrollAhead,
- preRenderExtra
- );
- if (pageView) {
- this.#ensurePdfPageLoaded(pageView).then(() => {
- this.renderingQueue.renderView(pageView);
- });
- return true;
- }
- return false;
- }
-
- get hasEqualPageSizes() {
- const firstPageView = this._pages[0];
- for (let i = 1, ii = this._pages.length; i < ii; ++i) {
- const pageView = this._pages[i];
- if (
- pageView.width !== firstPageView.width ||
- pageView.height !== firstPageView.height
- ) {
- return false;
- }
- }
- return true;
- }
-
- getPagesOverview() {
- return this._pages.map(pageView => {
- const viewport = pageView.pdfPage.getViewport({ scale: 1 });
- if (!this.enablePrintAutoRotate || isPortraitOrientation(viewport)) {
- return {
- width: viewport.width,
- height: viewport.height,
- rotation: viewport.rotation,
- };
- }
-
- return {
- width: viewport.height,
- height: viewport.width,
- rotation: (viewport.rotation - 90) % 360,
- };
- });
- }
-
- get optionalContentConfigPromise() {
- if (!this.pdfDocument) {
- return Promise.resolve(null);
- }
- if (!this._optionalContentConfigPromise) {
- console.error("optionalContentConfigPromise: Not initialized yet.");
-
-
- return this.pdfDocument.getOptionalContentConfig();
- }
- return this._optionalContentConfigPromise;
- }
-
- set optionalContentConfigPromise(promise) {
- if (!(promise instanceof Promise)) {
- throw new Error(`Invalid optionalContentConfigPromise: ${promise}`);
- }
- if (!this.pdfDocument) {
- return;
- }
- if (!this._optionalContentConfigPromise) {
-
-
- return;
- }
- this._optionalContentConfigPromise = promise;
- this.refresh(false, { optionalContentConfigPromise: promise });
- this.eventBus.dispatch("optionalcontentconfigchanged", {
- source: this,
- promise,
- });
- }
-
- get scrollMode() {
- return this._scrollMode;
- }
-
- set scrollMode(mode) {
- if (this._scrollMode === mode) {
- return;
- }
- if (!isValidScrollMode(mode)) {
- throw new Error(`Invalid scroll mode: ${mode}`);
- }
- if (this.pagesCount > PagesCountLimit.FORCE_SCROLL_MODE_PAGE) {
- return;
- }
- this._previousScrollMode = this._scrollMode;
- this._scrollMode = mode;
- this.eventBus.dispatch("scrollmodechanged", { source: this, mode });
- this._updateScrollMode( this._currentPageNumber);
- }
- _updateScrollMode(pageNumber = null) {
- const scrollMode = this._scrollMode,
- viewer = this.viewer;
- viewer.classList.toggle(
- "scrollHorizontal",
- scrollMode === ScrollMode.HORIZONTAL
- );
- viewer.classList.toggle("scrollWrapped", scrollMode === ScrollMode.WRAPPED);
- if (!this.pdfDocument || !pageNumber) {
- return;
- }
- if (scrollMode === ScrollMode.PAGE) {
- this.#ensurePageViewVisible();
- } else if (this._previousScrollMode === ScrollMode.PAGE) {
-
-
- this._updateSpreadMode();
- }
-
-
-
- if (this._currentScaleValue && isNaN(this._currentScaleValue)) {
- this._setScale(this._currentScaleValue, { noScroll: true });
- }
- this._setCurrentPageNumber(pageNumber, true);
- this.update();
- }
-
- get spreadMode() {
- return this._spreadMode;
- }
-
- set spreadMode(mode) {
- if (this._spreadMode === mode) {
- return;
- }
- if (!isValidSpreadMode(mode)) {
- throw new Error(`Invalid spread mode: ${mode}`);
- }
- this._spreadMode = mode;
- this.eventBus.dispatch("spreadmodechanged", { source: this, mode });
- this._updateSpreadMode( this._currentPageNumber);
- }
- _updateSpreadMode(pageNumber = null) {
- if (!this.pdfDocument) {
- return;
- }
- const viewer = this.viewer,
- pages = this._pages;
- if (this._scrollMode === ScrollMode.PAGE) {
- this.#ensurePageViewVisible();
- } else {
-
- viewer.textContent = "";
- if (this._spreadMode === SpreadMode.NONE) {
- for (const pageView of this._pages) {
- viewer.append(pageView.div);
- }
- } else {
- const parity = this._spreadMode - 1;
- let spread = null;
- for (let i = 0, ii = pages.length; i < ii; ++i) {
- if (spread === null) {
- spread = document.createElement("div");
- spread.className = "spread";
- viewer.append(spread);
- } else if (i % 2 === parity) {
- spread = spread.cloneNode(false);
- viewer.append(spread);
- }
- spread.append(pages[i].div);
- }
- }
- }
- if (!pageNumber) {
- return;
- }
-
-
-
- if (this._currentScaleValue && isNaN(this._currentScaleValue)) {
- this._setScale(this._currentScaleValue, { noScroll: true });
- }
- this._setCurrentPageNumber(pageNumber, true);
- this.update();
- }
-
- _getPageAdvance(currentPageNumber, previous = false) {
- switch (this._scrollMode) {
- case ScrollMode.WRAPPED: {
- const { views } = this._getVisiblePages(),
- pageLayout = new Map();
-
- for (const { id, y, percent, widthPercent } of views) {
- if (percent === 0 || widthPercent < 100) {
- continue;
- }
- let yArray = pageLayout.get(y);
- if (!yArray) {
- pageLayout.set(y, (yArray ||= []));
- }
- yArray.push(id);
- }
-
- for (const yArray of pageLayout.values()) {
- const currentIndex = yArray.indexOf(currentPageNumber);
- if (currentIndex === -1) {
- continue;
- }
- const numPages = yArray.length;
- if (numPages === 1) {
- break;
- }
-
- if (previous) {
- for (let i = currentIndex - 1, ii = 0; i >= ii; i--) {
- const currentId = yArray[i],
- expectedId = yArray[i + 1] - 1;
- if (currentId < expectedId) {
- return currentPageNumber - expectedId;
- }
- }
- } else {
- for (let i = currentIndex + 1, ii = numPages; i < ii; i++) {
- const currentId = yArray[i],
- expectedId = yArray[i - 1] + 1;
- if (currentId > expectedId) {
- return expectedId - currentPageNumber;
- }
- }
- }
-
- if (previous) {
- const firstId = yArray[0];
- if (firstId < currentPageNumber) {
- return currentPageNumber - firstId + 1;
- }
- } else {
- const lastId = yArray[numPages - 1];
- if (lastId > currentPageNumber) {
- return lastId - currentPageNumber + 1;
- }
- }
- break;
- }
- break;
- }
- case ScrollMode.HORIZONTAL: {
- break;
- }
- case ScrollMode.PAGE:
- case ScrollMode.VERTICAL: {
- if (this._spreadMode === SpreadMode.NONE) {
- break;
- }
- const parity = this._spreadMode - 1;
- if (previous && currentPageNumber % 2 !== parity) {
- break;
- } else if (!previous && currentPageNumber % 2 === parity) {
- break;
- }
- const { views } = this._getVisiblePages(),
- expectedId = previous ? currentPageNumber - 1 : currentPageNumber + 1;
- for (const { id, percent, widthPercent } of views) {
- if (id !== expectedId) {
- continue;
- }
- if (percent > 0 && widthPercent === 100) {
- return 2;
- }
- break;
- }
- break;
- }
- }
- return 1;
- }
-
- nextPage() {
- const currentPageNumber = this._currentPageNumber,
- pagesCount = this.pagesCount;
- if (currentPageNumber >= pagesCount) {
- return false;
- }
- const advance =
- this._getPageAdvance(currentPageNumber, false) || 1;
- this.currentPageNumber = Math.min(currentPageNumber + advance, pagesCount);
- return true;
- }
-
- previousPage() {
- const currentPageNumber = this._currentPageNumber;
- if (currentPageNumber <= 1) {
- return false;
- }
- const advance =
- this._getPageAdvance(currentPageNumber, true) || 1;
- this.currentPageNumber = Math.max(currentPageNumber - advance, 1);
- return true;
- }
-
- increaseScale(steps = 1, options = null) {
- let newScale = this._currentScale;
- do {
- newScale = (newScale * DEFAULT_SCALE_DELTA).toFixed(2);
- newScale = Math.ceil(newScale * 10) / 10;
- newScale = Math.min(MAX_SCALE, newScale);
- } while (--steps > 0 && newScale < MAX_SCALE);
- options ||= Object.create(null);
- options.noScroll = false;
- this._setScale(newScale, options);
- }
-
- decreaseScale(steps = 1, options = null) {
- let newScale = this._currentScale;
- do {
- newScale = (newScale / DEFAULT_SCALE_DELTA).toFixed(2);
- newScale = Math.floor(newScale * 10) / 10;
- newScale = Math.max(MIN_SCALE, newScale);
- } while (--steps > 0 && newScale > MIN_SCALE);
- options ||= Object.create(null);
- options.noScroll = false;
- this._setScale(newScale, options);
- }
- #updateContainerHeightCss(height = this.container.clientHeight) {
- if (height !== this.#previousContainerHeight) {
- this.#previousContainerHeight = height;
- docStyle.setProperty("--viewer-container-height", `${height}px`);
- }
- }
- #resizeObserverCallback(entries) {
- for (const entry of entries) {
- if (entry.target === this.container) {
- this.#updateContainerHeightCss(
- Math.floor(entry.borderBoxSize[0].blockSize)
- );
- this.#containerTopLeft = null;
- break;
- }
- }
- }
- get containerTopLeft() {
- return (this.#containerTopLeft ||= [
- this.container.offsetTop,
- this.container.offsetLeft,
- ]);
- }
-
- get annotationEditorMode() {
- return this.#annotationEditorUIManager
- ? this.#annotationEditorMode
- : AnnotationEditorType.DISABLE;
- }
-
- set annotationEditorMode(mode) {
- if (!this.#annotationEditorUIManager) {
- throw new Error(`The AnnotationEditor is not enabled.`);
- }
- if (this.#annotationEditorMode === mode) {
- return;
- }
- if (!isValidAnnotationEditorMode(mode)) {
- throw new Error(`Invalid AnnotationEditor mode: ${mode}`);
- }
- if (!this.pdfDocument) {
- return;
- }
- this.#annotationEditorMode = mode;
- this.eventBus.dispatch("annotationeditormodechanged", {
- source: this,
- mode,
- });
- this.#annotationEditorUIManager.updateMode(mode);
- }
-
- set annotationEditorParams({ type, value }) {
- if (!this.#annotationEditorUIManager) {
- throw new Error(`The AnnotationEditor is not enabled.`);
- }
- this.#annotationEditorUIManager.updateParams(type, value);
- }
- refresh(noUpdate = false, updateArgs = Object.create(null)) {
- if (!this.pdfDocument) {
- return;
- }
- for (const pageView of this._pages) {
- pageView.update(updateArgs);
- }
- if (this.#scaleTimeoutId !== null) {
- clearTimeout(this.#scaleTimeoutId);
- this.#scaleTimeoutId = null;
- }
- if (!noUpdate) {
- this.update();
- }
- }
- }
- export { PagesCountLimit, PDFPageViewBuffer, PDFViewer };
|