@tindtechnologies/universalviewer
Version:
The Universal Viewer is a community-developed open source project on a mission to help you share your 📚📜📰📽️📻🗿 with the 🌎
2 lines (1 loc) • 9.25 kB
JavaScript
import{a as T}from"./chunk-Q2W43J2O.js";import{a as v}from"./chunk-DWNTPCPY.js";import{o as y,p as m}from"./chunk-2YE6NNA6.js";import{f as E,g}from"./chunk-R6HAKAEO.js";import"./chunk-YAXTLTJC.js";var I=class{constructor(n){this.readonly=!1;this.readonly=n}get(n,t){}set(n,t){}dispose(){}};var A=/&?(xywh=)?(pixel:|percent:)?([0-9]+(?:\.[0-9]+)?),([0-9]+(?:\.[0-9]+)?),([0-9]+(?:\.[0-9]+)?),([0-9]+(?:\.[0-9]+)?)/,U=/&?(t=)(npt:)?([0-9]+(.[0-9]+)?)?(,([0-9]+(.[0-9]+)?))?/;function h(e){if(Array.isArray(e))return e.reduce((n,t)=>{let{selector:r,selectors:s}=h(t);return r&&(n.selector||(n.selector=r),n.selectors.push(...s)),n},{selector:null,selectors:[]});if(!e)return{selector:null,selectors:[]};if(typeof e=="string"){let[n,t]=e.split("#");return t?h({type:"FragmentSelector",value:t}):{selector:null,selectors:[]}}if(e.type==="PointSelector"&&(e.t||e.t===0)){let n={type:"TemporalSelector",startTime:e.t};return{selector:n,selectors:[n]}}if(e.type==="FragmentSelector"){let n=A.exec(e.value);if(n){let r={type:"BoxSelector",unit:n[2]==="percent:"?"percent":"pixel",x:parseFloat(n[3]),y:parseFloat(n[4]),width:parseFloat(n[5]),height:parseFloat(n[6])};return{selector:r,selectors:[r]}}let t=e.value.match(U);if(t){let r={type:"TemporalSelector",startTime:t[4]?parseFloat(t[4]):0,endTime:t[7]?parseFloat(t[7]):void 0};return{selector:r,selectors:[r]}}return{selector:null,selectors:[]}}return{selector:null,selectors:[]}}function d(e,n={}){if(Array.isArray(e))return d(e[0]);if(typeof e=="string"){let[t,r]=e.split("#");return r?d({type:"SpecificResource",source:{id:t,type:"Unknown"},selector:{type:"FragmentSelector",value:r}}):{type:"SpecificResource",source:{id:t,type:n.typeMap&&n.typeMap[t]||"Unknown"},selector:null,selectors:[]}}if(e.type==="Choice"||e.type==="List"||e.type==="Composite"||e.type==="Independents")return d(e.items[0]);if(e.type==="SpecificResource"){e.source.type==="Canvas"&&e.source.partOf&&typeof e.source.partOf=="string"&&(e.source.partOf=[{id:e.source.partOf,type:"Manifest"}]);let{selector:t,selectors:r}=e.selector?h(e.selector):{selector:null,selectors:[]};return{type:"SpecificResource",source:e.source,selector:t,selectors:r}}if(e.id){e.type==="Canvas"&&e.partOf&&typeof e.partOf=="string"&&(e.partOf=[{id:e.partOf,type:"Manifest"}]);let[t,r]=e.id.split("#");return r?d({type:"SpecificResource",source:{...e,id:t},selector:{type:"FragmentSelector",value:r}}):{type:"SpecificResource",source:{...e,id:t},selector:null,selectors:[]}}return{type:"SpecificResource",source:e,selector:null,selectors:[]}}function x(e,n){if(e=e.trim(),e[0]==="{")return n?Promise.resolve(JSON.parse(e)):JSON.parse(e);if(e.startsWith("http")){if(!n)throw new Error("Cannot fetch remote fetch with async=false in parseContentState");return fetch(e).then(t=>t.json())}return x(R(e),n)}function R(e){let t=_(e).replace(/-/g,"+").replace(/_/g,"/"),r=typeof atob=="undefined"?Buffer.from(t,"base64").toString("utf-8"):atob(t);return decodeURIComponent(r).trim()}function _(e){let n=e.length%4;if(n===1)throw new Error("InvalidLengthError: Input base64url string is the wrong length to determine padding");return e+(n?"====".slice(0,4-n):"")}function b(e){if(!e)throw new Error("Content state is empty");Array.isArray(e)||(e=[e]);let n="vault://virtual-annotation/"+new Date().getTime(),t=["contentState"],r=[];for(let s of e){if(typeof s=="string")throw new Error("Content state is a [String] type and cannot be inferred");if(s.type==="Annotation"){if(n=s.id,Array.isArray(s.motivation))for(let o of s.motivation)t.indexOf(o)===-1&&t.push(o);if(Array.isArray(s.target))for(let o of s.target){let u=d(o);r.push(u)}else{let o=d(s.target);r.push(o)}continue}let c=d(s);r.push(c)}return{id:n,type:"Annotation",motivation:["contentState",...e.motivation||[]],target:r,extensions:{}}}function w(e){if(!e)return null;if(typeof e=="string"&&e.startsWith("http"))return{type:"remote-content-state",id:e};try{return b(typeof e=="string"?x(e):e)}catch{return null}}var C=class extends I{constructor(n=!1){super(n)}get(n,t){let r=y.getHashParameter(n,document);return r===null?t:r}getFragment(n,t){let s=new RegExp("#.*"+n+"=([^&]+)(&|$)").exec(t);return s?decodeURIComponent(s[1].replace(/\+/g," ")):null}set(n,t){this.readonly||(t?y.setHashParameter(n,t,document):y.getHashParameter(n)!==null&&y.setHashParameter(n,"",document))}getInitialData(n){let t=[],r=this.get("locales","");if(r){let o=r.split(",");for(let u in o){let l=String(o[u]).split(":");t[u]={name:l[0],label:l[1]}}}else t.push(E);function s(o){if(o!==void 0)return Number(o)}let c=this.get("iiif-content","");if(c){let o="",u="",l="",p=w(c);if(p.type==="remote-content-state")o=p.id;else if(p&&p.target.length){let i=p.target[0];if(i.type==="SpecificResource"&&i.source.type==="Canvas"){let f=(i.source.partOf||[]).find(F=>F.type==="Manifest");i.selector&&i.selector.type==="BoxSelector"&&(u=i.source.id,l=i.selector.x+","+i.selector.y+","+i.selector.width+","+i.selector.height),f&&(o=f.id)}}return{iiifManifestId:o,collectionIndex:void 0,manifestIndex:0,canvasId:u,canvasIndex:0,rotation:0,rangeId:"",xywh:l,target:"",locales:t.length?t:void 0,...n}}return{iiifManifestId:this.get("iiifManifestId")||this.get("manifest"),collectionIndex:s(this.get("c")),manifestIndex:Number(this.get("m",0)),canvasIndex:Number(this.get("cv",0)),rotation:Number(this.get("r",0)),rangeId:this.get("rid",""),xywh:this.get("xywh",""),target:this.get("target",""),locales:t.length?t:void 0,...n}}dispose(){history.pushState("",document.title,window.location.pathname+window.location.search)}bindTo(n){n.adapter=this,n.on(m.COLLECTION_INDEX_CHANGE,t=>{this.set("c",t)},!1),n.on(m.MANIFEST_INDEX_CHANGE,t=>{this.set("m",t)},!1),n.on(m.CANVAS_INDEX_CHANGE,t=>{this.set("cv",t)},!1),n.on(m.RANGE_CHANGE,t=>{let r=!t||typeof t=="string"?t:t.id;this.set("rid",r)},!1),n.on(m.TARGET_CHANGE,t=>{this.set("xywh",this.getFragment("xywh",t))},!1)}};var a;(function(s){s.IIIFLEGACY="manifest",s.IIIF="iiifManifestId",s.YOUTUBE="youTubeVideoId",s.UNKNOWN="unknown"})(a||(a={}));var N={[a.IIIF]:()=>import("./IIIFContentHandler-YIVVZ2RP.js"),[a.YOUTUBE]:()=>import("./YouTubeContentHandler-UW3RE2WR.js")},S=class extends v{constructor(n){super(n);this.options=n;this._contentType=a.UNKNOWN;this._externalEventListeners=[];this._assignContentHandler(this.options.data)}get(){return this._assignedContentHandler}on(n,t,r){this._externalEventListeners.push({name:n,cb:t,ctx:r})}async _assignContentHandler(n){let t;n[a.IIIFLEGACY]?(n.iiifManifestId=n[a.IIIFLEGACY],delete n[a.IIIFLEGACY],t=a.IIIF):n[a.IIIF]?t=a.IIIF:n[a.YOUTUBE]?t=a.YOUTUBE:this._contentType?t=this._contentType:t=a.UNKNOWN;let r=this._contentType!==t;if(t===a.UNKNOWN)console.error("Unknown content type");else if(r){this._contentType=t,this._assignedContentHandler?.dispose();let s=await N[t]();this.showSpinner(),this._assignedContentHandler=new s.default({target:this._el,data:n},this.adapter,this._externalEventListeners)}return r}set(n,t){this._assignContentHandler(n).then(r=>{r?this.showSpinner():this._assignedContentHandler.set(n,t)})}exitFullScreen(){this._assignedContentHandler?.exitFullScreen()}resize(){this._assignedContentHandler?.resize()}dispose(){this._assignedContentHandler?.dispose()}};var O=(e,n)=>{let t,r=!1,s=!1,c=typeof e=="string"?document.getElementById(e):e;if(!c)throw new Error("UV target element not found");c.innerHTML="";let o=document.createElement("div");c.appendChild(o);let u=document.createElement("div");o.appendChild(u);let l=()=>{t&&(r&&!s?(o.style.width=window.innerWidth+"px",o.style.height=window.innerHeight+"px"):(o.style.width=c.offsetWidth+"px",o.style.height=c.offsetHeight+"px"),t.resize())};window.addEventListener("resize",function(){l()}),window.addEventListener("orientationchange",function(){setTimeout(function(){l()},100)}),t=new S({target:u,data:n}),t.on(g.CREATED,function(i){l()},!1),t.on(g.EXTERNAL_RESOURCE_OPENED,function(i){setTimeout(function(){l()},100)},!1),t.on(g.TOGGLE_FULLSCREEN,function(i){if(r=i.isFullScreen,s=i.overrideFullScreen,!i.overrideFullScreen)if(r){let f=L(o);f&&f.call(o)}else{let f=H();f&&f.call(document)}setTimeout(function(){l()},100)},!1),t.on(g.ERROR,function(i){console.error(i)},!1);function p(i){(i.type==="webkitfullscreenchange"&&!document.webkitIsFullScreen||i.type==="fullscreenchange"&&!document.fullscreenElement||i.type==="mozfullscreenchange"&&!document.mozFullScreen||i.type==="MSFullscreenChange"&&document.msFullscreenElement===null)&&t.exitFullScreen()}return document.addEventListener("fullscreenchange",p,!1),document.addEventListener("webkitfullscreenchange",p,!1),document.addEventListener("mozfullscreenchange",p,!1),document.addEventListener("MSFullscreenChange",p,!1),t};function L(e){return e.webkitRequestFullscreen?e.webkitRequestFullscreen:e.mozRequestFullScreen?e.mozRequestFullScreen:e.msRequestFullscreen?e.msRequestFullscreen:e.requestFullscreen?e.requestFullscreen:!1}function H(){return document.webkitExitFullscreen?document.webkitExitFullscreen:document.msExitFullscreen?document.msExitFullscreen:document.mozCancelFullScreen?document.mozCancelFullScreen:document.exitFullscreen?document.exitFullscreen:!1}export{g as Events,m as IIIFEvents,C as IIIFURLAdapter,C as IIIFURLAdaptor,S as Viewer,T as YouTubeEvents,O as init};