UNPKG

@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.39 kB
import{a as F}from"./chunk-DSIJRHAY.js";import{a as b}from"./chunk-DVYM4KA5.js";import{a as d}from"./chunk-M4MN2QTO.js";import{p as g}from"./chunk-IRIM6TPQ.js";import{f as E,g as m}from"./chunk-5XPHF4E5.js";import"./chunk-L67DAW3A.js";var x=(o=>(o.IIIFLEGACY="manifest",o.IIIF="iiifManifestId",o.YOUTUBE="youTubeVideoId",o.UNKNOWN="unknown",o))(x||{});var h=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]+)?)/,R=/&?(t=)(npt:)?([0-9]+(.[0-9]+)?)?(,([0-9]+(.[0-9]+)?))?/;function S(e){if(Array.isArray(e))return e.reduce((n,t)=>{let{selector:r,selectors:o}=S(t);return r&&(n.selector||(n.selector=r),n.selectors.push(...o)),n},{selector:null,selectors:[]});if(!e)return{selector:null,selectors:[]};if(typeof e=="string"){let[n,t]=e.split("#");return t?S({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(R);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 f(e,n={}){if(Array.isArray(e))return f(e[0]);if(typeof e=="string"){let[t,r]=e.split("#");return r?f({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 f(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?S(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?f({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 I(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 I(U(e),n)}function U(e){let t=N(e).replace(/-/g,"+").replace(/_/g,"/"),r=typeof atob>"u"?Buffer.from(t,"base64").toString("utf-8"):atob(t);return decodeURIComponent(r).trim()}function N(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 v(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 o of e){if(typeof o=="string")throw new Error("Content state is a [String] type and cannot be inferred");if(o.type==="Annotation"){if(n=o.id,Array.isArray(o.motivation))for(let i of o.motivation)t.indexOf(i)===-1&&t.push(i);if(Array.isArray(o.target))for(let i of o.target){let a=f(i);r.push(a)}else{let i=f(o.target);r.push(i)}continue}let l=f(o);r.push(l)}return{id:n,type:"Annotation",motivation:["contentState",...e.motivation||[]],target:r,extensions:{}}}function T(e){if(!e)return null;if(typeof e=="string"&&e.startsWith("http"))return{type:"remote-content-state",id:e};try{return v(typeof e=="string"?I(e):e)}catch{return null}}var C=class extends h{constructor(n=!1){super(n)}get(n,t){let r=g.getHashParameter(n,document);return r===null?t:r}getFragment(n,t){let o=new RegExp("#.*"+n+"=([^&]+)(&|$)").exec(t);return o?decodeURIComponent(o[1].replace(/\+/g," ")):null}set(n,t){this.readonly||(t?g.setHashParameter(n,t,document):g.getHashParameter(n)!==null&&g.setHashParameter(n,"",document))}getInitialData(n){let t=[],r=this.get("locales","");if(r){let i=r.split(",");for(let a in i){let c=String(i[a]).split(":");t[a]={name:c[0],label:c[1]}}}else t.push(E);function o(i){if(i!==void 0)return Number(i)}let l=this.get("iiif-content","");if(l){let i="",a="",c="",u=T(l);if(u.type==="remote-content-state")i=u.id;else if(u&&u.target.length){let s=u.target[0];if(s.type==="SpecificResource"&&s.source.type==="Canvas"){let p=(s.source.partOf||[]).find(w=>w.type==="Manifest");s.selector&&s.selector.type==="BoxSelector"&&(a=s.source.id,c=s.selector.x+","+s.selector.y+","+s.selector.width+","+s.selector.height),p&&(i=p.id)}}return{iiifManifestId:i,collectionIndex:void 0,manifestIndex:0,canvasId:a,canvasIndex:0,rotation:0,rangeId:"",xywh:c,target:"",locales:t.length?t:void 0,...n}}return{iiifManifestId:this.get("iiifManifestId")||this.get("manifest"),collectionIndex:o(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(d.COLLECTION_INDEX_CHANGE,t=>{this.set("c",t)},!1),n.on(d.MANIFEST_INDEX_CHANGE,t=>{this.set("m",t)},!1),n.on(d.CANVAS_INDEX_CHANGE,t=>{this.set("cv",t)},!1),n.on(d.RANGE_CHANGE,t=>{let r=!t||typeof t=="string"?t:t.id;this.set("rid",r)},!1),n.on(d.TARGET_CHANGE,t=>{this.set("xywh",this.getFragment("xywh",t))},!1)}};var O={iiifManifestId:()=>import("./IIIFContentHandler-SFE3BKDK.js"),youTubeVideoId:()=>import("./YouTubeContentHandler-4RSYJABN.js")},y=class extends b{constructor(t){super(t);this.options=t;this.contentType="unknown";this._contentType=this.contentType;this._externalEventListeners=[];this._assignContentHandler(this.options.data)}get(){return this.assignedContentHandler}on(t,r,o){this._externalEventListeners.push({name:t,cb:r,ctx:o})}async _assignContentHandler(t){let r;t.manifest?(t.iiifManifestId=t.manifest,delete t.manifest,r="iiifManifestId"):t.iiifManifestId?r="iiifManifestId":t.youTubeVideoId?r="youTubeVideoId":this.contentType?r=this.contentType:r="unknown";let o=this.contentType!==r;if(r==="unknown")console.error("Unknown content type");else if(o){this.contentType=this._contentType=r,this.assignedContentHandler?.dispose();let l=await O[r]();this.showSpinner(),this.assignedContentHandler=this._assignedContentHandler=new l.default({target:this._el,data:t},this.adapter,this._externalEventListeners)}return o}set(t,r){this._assignContentHandler(t).then(o=>{o?this.showSpinner():this.assignedContentHandler.set(t,r)})}exitFullScreen(){this.assignedContentHandler?.exitFullScreen()}resize(){this.assignedContentHandler?.resize()}dispose(){this.assignedContentHandler?.dispose()}};var L=(e,n)=>{let t=!1,r=!1,o=typeof e=="string"?document.getElementById(e):e;if(!o)throw new Error("UV target element not found");o.innerHTML="";let l=document.createElement("div");o.appendChild(l);let i=document.createElement("div");l.appendChild(i);let a=new y({target:i,data:n}),c=()=>{a&&(t&&!r?(l.style.width=window.innerWidth+"px",l.style.height=window.innerHeight+"px"):(l.style.width=o.offsetWidth+"px",l.style.height=o.offsetHeight+"px"),a.resize())};window.addEventListener("resize",function(){c()}),window.addEventListener("orientationchange",function(){setTimeout(function(){c()},100)}),a.on(m.CREATED,function(s){c()},!1),a.on(m.EXTERNAL_RESOURCE_OPENED,function(s){setTimeout(function(){c()},100)},!1),a.on(m.TOGGLE_FULLSCREEN,function(s){if(t=s.isFullScreen,r=s.overrideFullScreen,!s.overrideFullScreen)if(t){let p=H(l);p&&p.call(l)}else{let p=_();p&&p.call(document)}setTimeout(function(){c()},100)},!1),a.on(m.ERROR,function(s){console.error(s)},!1);function u(s){(s.type==="webkitfullscreenchange"&&!document.webkitIsFullScreen||s.type==="fullscreenchange"&&!document.fullscreenElement||s.type==="mozfullscreenchange"&&!document.mozFullScreen||s.type==="MSFullscreenChange"&&document.msFullscreenElement===null)&&a.exitFullScreen()}return document.addEventListener("fullscreenchange",u,!1),document.addEventListener("webkitfullscreenchange",u,!1),document.addEventListener("mozfullscreenchange",u,!1),document.addEventListener("MSFullscreenChange",u,!1),a};function H(e){return e.webkitRequestFullscreen?e.webkitRequestFullscreen:e.mozRequestFullScreen?e.mozRequestFullScreen:e.msRequestFullscreen?e.msRequestFullscreen:e.requestFullscreen?e.requestFullscreen:!1}function _(){return document.webkitExitFullscreen?document.webkitExitFullscreen:document.msExitFullscreen?document.msExitFullscreen:document.mozCancelFullScreen?document.mozCancelFullScreen:document.exitFullscreen?document.exitFullscreen:!1}export{x as ContentType,m as Events,d as IIIFEvents,C as IIIFURLAdapter,C as IIIFURLAdaptor,y as Viewer,F as YouTubeEvents,L as init};