UNPKG

@nutrient-sdk/viewer

Version:

View and annotate PDF files in your web app. Full support for mobile and desktop. Runs in the browser using WASM.

13 lines 8.36 kB
/*! * Nutrient Web SDK 1.20.0 (https://www.nutrient.io/sdk/web) * * Copyright (c) 2016-2026 PSPDFKit GmbH. All rights reserved. * * THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW * AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE PSPDFKIT LICENSE AGREEMENT. * UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES. * This notice may not be removed from this file. * * PSPDFKit uses several open source third-party components: https://www.nutrient.io/legal/acknowledgements/web-acknowledgements/ */ "use strict";(globalThis.webpackChunkNutrientViewer=globalThis.webpackChunkNutrientViewer||[]).push([[269],{5873(t,e,a){a.d(e,{O:()=>i,f:()=>o});var s=a(34724);a(81806);class o{constructor(t){(0,s.A)(this,"endpointUnavailable",!1),(0,s.A)(this,"_pagesPromise",null),this._fetchResponse=t}getPages(){return this._pagesPromise||(this._pagesPromise=this._fetchPages()),this._pagesPromise}async _fetchPages(){try{const t=await this._fetchResponse();return 404===t.status?(this.endpointUnavailable=!0,null):t.ok?(await t.json()).pages??null:(this._pagesPromise=null,null)}catch(t){return this._pagesPromise=null,null}}}function i(t,e,a){const s=new globalThis.Map,o=new globalThis.Map;return a.map(a=>{let i=s.get(a);i||(i=t?.getPageAnnotationIdsForPdfObjectId(e,a)??[],s.set(a,i));const n=o.get(a)??0;o.set(a,n+1);const r=i[n];return r?{id:r,pdfObjectId:a}:null}).filter(Boolean)}},85808(t,e,a){a.d(e,{RESTProvider:()=>m});var s=a(34724),o=a(49555),i=a(81806),n=a(72835),r=a(69455);class l extends(o.mS({alreadyLoadedPages:(0,o.T5)(),serverURL:null,authPayload:null,isLoaded:!1,isFormsEnabled:!0,features:o.B8(),loadBookmarksPromise:null,ignoredFormFieldNames:null})){}var d=a(25541),h=a(45072),c=a(47718),u=a(5873);class m{get tabOrderEndpointUnavailable(){return!this._tabOrderSupported||this._tabOrderFetcher.endpointUnavailable}constructor(t,e,a){let{isFormsEnabled:o,features:i,tabOrderSupported:n=!0}=a;(0,s.A)(this,"canCreateBackendOrphanWidgets",!0),(0,s.A)(this,"hasWarnedMeasurementAnnotationFallback",!1),(0,s.A)(this,"_tabOrderFetcher",new u.f(()=>this._request("/annotations-tab-order","GET"))),(0,s.A)(this,"_tabOrderSupported",!0),(0,s.A)(this,"_isDestroyed",!1),(0,s.A)(this,"setDocumentHandleConflictCallback",()=>{}),this._tabOrderSupported=n,this.state=new l({serverURL:t,authPayload:e,isFormsEnabled:o,features:i}),this._setReadStateCallbacksPromise=new Promise(t=>{this._setReadStateCallbacksPromiseResolve=t})}async load(){return this.state=this.state.set("isLoaded",!0),this.state.isFormsEnabled&&await this._initializeFormFieldValues(),this}destroy(){this._isDestroyed=!0}setReadStateCallbacks(t){this._readStateCallbacks=t,this._setReadStateCallbacksPromiseResolve?.()}setAnnotationCallbacks(t){this.annotationCallbacks=t}setBookmarkCallbacks(t){this.bookmarkCallbacks=t}setFormFieldValueCallbacks(t){this.formFieldValueCallbacks=t}async createAnnotation(t,e){this._verifyLoaded();const{id:a,...s}=(0,n.eq)(t),o={id:a,content:s},r=await this._request("/annotations","POST",o);if(200!==r.status)throw new i.uE("Nutrient Document Engine returned an error, when saving an annotation.");if("attachment_missing"===(await r.json()).error){const t=function(t,e){const a=new FormData;return a.append("annotation",JSON.stringify(t)),e.forEach((t,e)=>{e&&t.data&&a.append(e,t.data)}),a}(o,e);if(200!==(await this._request("/annotations","POST",t)).status)throw new i.uE("Nutrient Document Engine returned an error, when saving an annotation attachment.")}}async updateAnnotation(t,e,a){this._verifyLoaded();const{id:s,...o}=(0,n.eq)(t,a);await this._request(`/annotations/${s}`,"PUT",{id:s,content:o})}async deleteAnnotation(t){this._verifyLoaded(),await this._request(`/annotations/${t.id}`,"DELETE")}async createBookmark(t){this._verifyLoaded(),await this.loadBookmarks();const{id:e,...a}=(0,r.U)(t);if(200!==(await this._request("/bookmarks","POST",{id:e,content:a})).status)throw new i.uE("Nutrient Document Engine returned an error, when saving an bookmark.")}async updateBookmark(t){this._verifyLoaded(),await this.loadBookmarks();const{id:e,...a}=(0,r.U)(t);await this._request(`/bookmarks/${e}`,"PUT",{id:e,content:a})}async deleteBookmark(t){this._verifyLoaded(),await this.loadBookmarks(),await this._request(`/bookmarks/${t}`,"DELETE")}async setFormFieldValue(t){this._verifyLoaded();const e={id:(0,d.B)(t),content:(0,n.cA)(t)};await this._request("/form-field-values","POST",{formFieldValues:[e]})}async createFormFieldValue(){}async deleteFormFieldValue(){}async loadAnnotationsForPageIndex(t){if(this._verifyLoaded(),this.state.alreadyLoadedPages.has(t))await this.state.alreadyLoadedPages.get(t);else try{const e=this._request(`/page-${t}-annotations`,"GET").then(t=>t.json()).catch(t=>{throw t});this.state=this.state.setIn(["alreadyLoadedPages",t],e);const a=await e;this.state=this.state.setIn(["alreadyLoadedPages",t],Promise.resolve());const s=(0,o.B8)().withMutations(t=>{a.annotations.forEach(e=>{try{const a=(0,n.Nz)(e.content,this.state.features);this.hasWarnedMeasurementAnnotationFallback||a===e.content||((0,i.R8)(n.ve),this.hasWarnedMeasurementAnnotationFallback=!0),t.push((0,n.h8)(e.id,a))}catch(t){(0,i.pq)(`Skipped creating annotation #${e.id} from payload because an error occurred while deserializing.`,String(e.content)),(0,i.pq)(t)}})});s.size>0&&((0,i.V1)(this.annotationCallbacks),this.annotationCallbacks.createAnnotations(s,(0,o.T5)(),h.n)),this._readStateCallbacks?.hasFocusableAnnotationsOnPage(t)?this.updateTabOrder(t):this._readStateCallbacks?.hasTabOrderableAnnotationsOnPage(t)||this.annotationCallbacks?.setPageTabOrder(t,[])}catch(t){this._handleError(t,"annotations")}}async loadBookmarks(){if(this._verifyLoaded(),this.state.loadBookmarksPromise)await this.state.loadBookmarksPromise;else try{const t=this._request("/bookmarks","GET").then(t=>t.json()).then(t=>t.data).catch(t=>{throw t});this.state=this.state.set("loadBookmarksPromise",t);const e=await t;this.state=this.state.set("loadBookmarksPromise",Promise.resolve()),(0,i.V1)(Array.isArray(e.bookmarks),"Unexpected reply from bookmarks endpoint.");const a=(0,o.B8)().withMutations(t=>{e.bookmarks.forEach(e=>{try{t.push((0,r.r)(e.id,e.content))}catch(t){(0,i.pq)(`Skipped creating bookmark #${e.id} from payload because an error occurred while deserializing.`,e),(0,i.pq)(t)}})});a.size>0&&((0,i.V1)(this.bookmarkCallbacks),this.bookmarkCallbacks.createBookmarks(a,h.n))}catch(t){this._handleError(t,"bookmarks")}}async syncChanges(){}async flushChanges(){}async _initializeFormFieldValues(){const t=await this._request("/form-field-values","GET"),e=await t.json();(0,i.V1)(Array.isArray(e.formFieldValues),"Unexpected reply from form-values endpoint.");const a=(0,o.B8)(e.formFieldValues.map(t=>{let{content:e}=t;try{return(0,n.R5)(e)}catch(t){return(0,i.pq)(`Skipped form field value ${e.name} from payload because an error occurred while deserializing.`,String(e)),(0,i.pq)(t),null}}).filter(Boolean));(0,i.V1)(this.formFieldValueCallbacks),this.state.ignoredFormFieldNames&&this.state.ignoredFormFieldNames.size?this.formFieldValueCallbacks.setFormFieldValues(a.filter(t=>!this.state.ignoredFormFieldNames?.includes(t.name))):this.formFieldValueCallbacks.setFormFieldValues(a)}_handleError(t,e){(0,i.z3)(`Loading or updating ${e} failed:\n\n${t.message}`)}_request(t,e,a){(0,i.V1)(null!=this.state.authPayload,"Cannot call request without authPayload");const s=a instanceof FormData||"object"!=typeof a?null:{"Content-Type":"application/json"},o={"X-PSPDFKit-Token":this.state.authPayload.token,"PSPDFKit-Platform":"web","PSPDFKit-Version":(0,c._q)(),...s};return fetch(`${this.state.serverURL}${t}`,{method:e,headers:o,body:a instanceof FormData?a:"object"==typeof a?JSON.stringify(a):void 0,credentials:"include"})}_verifyLoaded(){if(!this.state.isLoaded)throw new Error("not loaded")}setIgnoredFormFieldNames(t){this.state=this.state.set("ignoredFormFieldNames",t)}async updateTabOrder(t){if(!this._tabOrderSupported)return;const e=await this._tabOrderFetcher.getPages(),a=e?.[String(t)]?.order;a&&!this._isDestroyed&&((0,i.V1)(this.annotationCallbacks),this.annotationCallbacks.setPageTabOrder(t,(0,u.O)(this._readStateCallbacks,t,a)))}async setTabOrder(){throw new i.uE("setTabOrder is not supported on this backend.")}}}}]);