react-iiif-vault 0.9.3 → 0.9.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +1527 -1
- package/dist/esm/index.mjs +1462 -1
- package/dist/index.d.ts +38 -8
- package/dist/index.umd.js +1541 -2
- package/package.json +15 -15
package/dist/esm/index.mjs
CHANGED
|
@@ -1 +1,1462 @@
|
|
|
1
|
-
import g,{useContext as O,useMemo as m,useState as S,useEffect as $,createContext as Qe,useCallback as w,useRef as Q,useLayoutEffect as ke}from"react";import{globalVault as X,Vault as q}from"@iiif/vault";import{useQuery as k}from"react-query";import{ImageServiceLoader as ze,getImageServices as z}from"@atlas-viewer/iiif-image-api";import{createEventsHelper as J,createStylesHelper as K,createThumbnailHelper as Ye}from"@iiif/vault-helpers";import{createAction as y}from"typesafe-actions";import{emptyAnnotationPage as We}from"@iiif/parser";var Ge=Object.defineProperty,Z=Object.getOwnPropertySymbols,Xe=Object.prototype.hasOwnProperty,qe=Object.prototype.propertyIsEnumerable,ee=(e,t,n)=>t in e?Ge(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,te=(e,t)=>{for(var n in t||(t={}))Xe.call(t,n)&&ee(e,n,t[n]);if(Z)for(var n of Z(t))qe.call(t,n)&&ee(e,n,t[n]);return e};const Je={collection:void 0,manifest:void 0,range:void 0,canvas:void 0,annotation:void 0},D=g.createContext(Je),b=()=>O(D),x=({value:e,children:t})=>{const n=b(),r=m(()=>te(te({},n),e),[e,n]);return g.createElement(D.Provider,{value:r},t)},Ke=({annotation:e,children:t})=>g.createElement(x,{value:{annotation:e}},t),ne=({canvas:e,children:t})=>g.createElement(x,{value:{canvas:e}},t),Ze=({collection:e,children:t})=>g.createElement(x,{value:{collection:e}},t),re=({manifest:e,children:t})=>g.createElement(x,{value:{manifest:e}},t),et=({range:e,children:t})=>g.createElement(x,{value:{range:e}},t),T=g.createContext({vault:null,setVaultInstance:e=>{}}),oe=({vault:e,vaultOptions:t,useGlobal:n,resources:r,children:c})=>{const[i,a]=S(()=>e||(n?X(t):t?new q(t):new q));return g.createElement(T.Provider,{value:{vault:i,setVaultInstance:a}},g.createElement(x,{value:r||{}},c))},h=()=>{const{vault:e}=O(T);if(e===null)throw new Error("Vault not found. Ensure you have your provider set up correctly.");return e};var tt=Object.defineProperty,ie=Object.getOwnPropertySymbols,nt=Object.prototype.hasOwnProperty,rt=Object.prototype.propertyIsEnumerable,ae=(e,t,n)=>t in e?tt(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,ot=(e,t)=>{for(var n in t||(t={}))nt.call(t,n)&&ae(e,n,t[n]);if(ie)for(var n of ie(t))rt.call(t,n)&&ae(e,n,t[n]);return e},it=(e,t,n)=>new Promise((r,c)=>{var i=s=>{try{o(n.next(s))}catch(l){c(l)}},a=s=>{try{o(n.throw(s))}catch(l){c(l)}},o=s=>s.done?r(s.value):Promise.resolve(s.value).then(i,a);o((n=n.apply(e,t)).next())});const se=(e,t={})=>{const n=h(),[r,c]=S(e),i=m(()=>n.get(e)||void 0,[e,n]),{data:a,error:o}=k(["manifest",e],()=>it(void 0,null,function*(){const s=i||(yield n.load(e));return s&&r!==s.id&&c(s.id),s}),ot({refetchIntervalInBackground:!1,refetchOnMount:!1,refetchOnWindowFocus:!1,refetchInterval:!1,retry:!1,initialData:i},t));return{isLoaded:!!a,id:r,error:o,manifest:a}};function P(e,t=[]){const n=h(),[r,c]=S(()=>e(n.getState(),n));return $(()=>n.subscribe(i=>e(i,n),i=>{c(i)},!0),t),r}const M=g.createContext([]);function ce(){const e=O(M);return P(t=>e.map(n=>t.iiif.entities.Canvas[n]).filter(Boolean),[e])}const F=()=>{},V=Qe({setCurrentCanvasId:F,setCurrentCanvasIndex:F,nextCanvas:F,previousCanvas:F,currentCanvasIndex:-1,totalCanvases:0,pagingView:!0}),at=e=>{const t=se(e.manifest),[n,r]=S(""),[c,i]=S([]),a=(typeof e.pagingEnabled>"u"||e.pagingEnabled)&&t.manifest&&t.manifest.behavior&&t.manifest.behavior.includes("paged");$(()=>{var u,v;t.manifest&&(r((u=t.manifest.items[0])==null?void 0:u.id),i([(v=t.manifest.items[0])==null?void 0:v.id]))},[t.manifest,e.manifest]);const o=m(()=>{var u;return((u=t.manifest)==null?void 0:u.items.map(v=>v.id))||[]},[t.manifest,e.manifest]),s=m(()=>o.indexOf(n),[o,n]),l=w(()=>{if(o.length&&n){if(s===-1||(o[s+2]?s+2===o.length:s===o.length))return;const u=a&&s!==0?o[s+2]:o[s+1],v=a?s!==0?o[s+3]:o[s+2]:null;u&&(r(u),i(v?[u,v]:[u]))}},[a,o,n,s]),p=w(()=>{if(o.length&&n){if(s===0||s===-1)return;const u=a&&s!==1?o[s-2]:o[s-1],v=a&&s!==1?o[s-1]:null;u&&(r(u),i(v?[u,v]:[u]))}},[a,o,n,s]),f=w(u=>{const v=a&&u%2===1?u-1:u,C=o[v],G=a&&v!==0?o[v+1]:null;C&&(r(C),i(A=>{const H=G?[C,G]:[C];if(A.length===A.length){for(let j=0;j<A.length;j++)if(A[j]!==H[j])return H;return A}return H}))},[a,o]),d=w(u=>{const v=o.indexOf(u);v!==-1&&f(v)},[o,f]),_=m(()=>({setCurrentCanvasId:d,nextCanvas:l,previousCanvas:p,currentCanvasIndex:s,totalCanvases:o.length,setCurrentCanvasIndex:f,pagingView:!0}),[l,p,s,o,f,d]);return!t.isLoaded||!t.manifest?g.createElement("div",null,"Loading..."):g.createElement(V.Provider,{value:_},g.createElement(M.Provider,{value:c},g.createElement(re,{manifest:t.manifest.id},g.createElement(ne,{canvas:n},e.children))))};function st(){return O(V)}function ct(){return{VaultContext:O(T),ResourceContext:O(D),SimpleViewerReactContext:O(V),VisibleCanvasReactContext:O(M)}}const lt=e=>g.createElement(oe,{vault:e.bridge.VaultContext.vault||void 0,resources:e.bridge.ResourceContext},g.createElement(M.Provider,{value:e.bridge.VisibleCanvasReactContext},g.createElement(V.Provider,{value:e.bridge.SimpleViewerReactContext},e.children))),le=g.createContext(new ze);function N(){return O(le)}const ut=/&?(xywh=)?(pixel:|percent:)?([0-9]+(?:\.[0-9]+)?),([0-9]+(?:\.[0-9]+)?),([0-9]+(?:\.[0-9]+)?),([0-9]+(?:\.[0-9]+)?)/,ft=/&?(t=)(npt:)?([0-9]+(.[0-9]+)?)?(,([0-9]+(.[0-9]+)?))?/;function B(e){if(Array.isArray(e))return e.reduce((t,n)=>{const{selector:r,selectors:c}=B(n);return r&&(t.selector||(t.selector=r),t.selectors.push(...c)),t},{selector:null,selectors:[]});if(!e)return{selector:null,selectors:[]};if(typeof e=="string"){const[t,n]=e.split("#");return n?B({type:"FragmentSelector",value:n}):{selector:null,selectors:[]}}if(e.type==="PointSelector"&&(e.t||e.t===0)){const t={type:"TemporalSelector",temporal:{startTime:e.t}};return{selector:t,selectors:[t]}}if(e.type==="FragmentSelector"){const t=ut.exec(e.value);if(t){const r={type:"BoxSelector",spatial:{unit:t[2]==="percent:"?"percent":"pixel",x:parseFloat(t[3]),y:parseFloat(t[4]),width:parseFloat(t[5]),height:parseFloat(t[6])}};return{selector:r,selectors:[r]}}const n=e.value.match(ft);if(n){const r={type:"TemporalSelector",temporal:{startTime:n[4]?parseFloat(n[4]):0,endTime:n[7]?parseFloat(n[7]):void 0}};return{selector:r,selectors:[r]}}return{selector:null,selectors:[]}}return{selector:null,selectors:[]}}var pt=Object.defineProperty,dt=Object.defineProperties,vt=Object.getOwnPropertyDescriptors,ue=Object.getOwnPropertySymbols,mt=Object.prototype.hasOwnProperty,gt=Object.prototype.propertyIsEnumerable,fe=(e,t,n)=>t in e?pt(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,pe=(e,t)=>{for(var n in t||(t={}))mt.call(t,n)&&fe(e,n,t[n]);if(ue)for(var n of ue(t))gt.call(t,n)&&fe(e,n,t[n]);return e},de=(e,t)=>dt(e,vt(t));function I(e,t={}){if(Array.isArray(e))return I(e[0]);if(typeof e=="string"){const[n,r]=e.split("#");return r?I({type:"SpecificResource",source:{id:n,type:"Unknown"},selector:{type:"FragmentSelector",value:r}}):{type:"SpecificResource",source:{id:n,type:t.typeMap&&t.typeMap[n]||"Unknown"},selector:null,selectors:[]}}if(e.type==="Choice"||e.type==="List"||e.type==="Composite"||e.type==="Independents")return I(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"}]);const{selector:n,selectors:r}=e.selector?B(e.selector):{selector:null,selectors:[]};return{type:"SpecificResource",source:e.source,selector:n,selectors:r}}if(e.id){e.type==="Canvas"&&e.partOf&&typeof e.partOf=="string"&&(e.partOf=[{id:e.partOf,type:"Manifest"}]);const[n,r]=e.id.split("#");return r?I({type:"SpecificResource",source:de(pe({},e),{id:n}),selector:{type:"FragmentSelector",value:r}}):{type:"SpecificResource",source:de(pe({},e),{id:n}),selector:null,selectors:[]}}return{type:"SpecificResource",source:e,selector:null,selectors:[]}}function ve(e){return e.type==="SpecificResource"?[e.source,{selector:e.selector}]:[e,{selector:null}]}function me(e,t,n){const r=[];let c=null;const i=[];for(const a of t){const o=e.get(a.body);for(const s of o){const[l,{selector:p}]=ve(s),f=(l.type||"unknown").toLowerCase();if(f==="choice"){const d=e.get(l.items),_=n.length?n.map(u=>d.find(v=>v.id===u)).filter(Boolean):[d[0]];_.length===0&&_.push(d[0]),c={type:"single-choice",items:d.map(u=>({id:u.id,label:u.label,selected:_.indexOf(u)!==-1})),label:s.label},o.push(..._);continue}r.indexOf(f)===-1&&r.push(f),i.push({type:f,resource:l,target:a.target,selector:p})}}return{types:r,items:i,choice:c}}const ge={makeChoice:()=>{}},Y={type:"unknown"},E=e=>({type:"unknown",reason:e,annotations:{pages:[]}});function he(e,t,n){const r=[];for(const c of t.items){const i=c.resource&&c.resource.type==="SpecificResource"?c.resource.source:c.resource;if(!i.id)return E("No resource Identifier");let a;if(i.service){const v=z(i);v[0]&&(a=n(v[0],e))}const{selector:o,source:s}=I(c.target);if(s.id!==e.id)continue;const l={type:"BoxSelector",spatial:{x:0,y:0,width:e.width,height:e.height}},p=o?o.type==="TemporalSelector"?{type:"TemporalBoxSelector",temporal:o.temporal,spatial:l.spatial}:o:null,f={type:"BoxSelector",spatial:{x:0,y:0,width:e.width,height:e.height}},d=c.resource.type==="SpecificResource"?I(c.resource):null,_=d&&d.selector&&(d.selector.type==="BoxSelector"||d.selector.type==="TemporalBoxSelector")?{type:"BoxSelector",spatial:{x:d.selector.spatial.x,y:d.selector.spatial.y,width:d.selector.spatial.width,height:d.selector.spatial.height}}:f,u={id:i.id,type:"Image",width:p?i.width:e.width,height:p?i.height:e.height,service:a,sizes:a&&a.sizes?a.sizes:i.width&&i.height?[{width:i.width,height:i.height}]:[],target:p&&p.type!=="PointSelector"?p:l,selector:_};r.push(u)}return{type:"images",image:r[0],images:r,choice:t.choice}}var ht=Object.defineProperty,yt=Object.defineProperties,_t=Object.getOwnPropertyDescriptors,ye=Object.getOwnPropertySymbols,Pt=Object.prototype.hasOwnProperty,Ot=Object.prototype.propertyIsEnumerable,_e=(e,t,n)=>t in e?ht(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,wt=(e,t)=>{for(var n in t||(t={}))Pt.call(t,n)&&_e(e,n,t[n]);if(ye)for(var n of ye(t))Ot.call(t,n)&&_e(e,n,t[n]);return e},St=(e,t)=>yt(e,_t(t));function Et(e={},t=[]){const{id:n,selector:r}=e,c=b(),i=h(),a=n||c.annotation,o=P(l=>a?l.iiif.entities.Annotation[a]:void 0,[a]),s=P(l=>o&&o.body?o.body.map(p=>p?l.iiif.entities[p.type][p.id]:null).filter(Boolean):[],[o]);return m(()=>{if(!o)return;const l=St(wt({},o),{body:s,target:I(o.target,{typeMap:i.getState().iiif.mapping})});return r?r(l):l},[o,r,s,...t])}function L(e={},t=[]){const{id:n,selector:r}=e,c=b();h();const i=n||c.manifest,a=P(o=>i?o.iiif.entities.Manifest[i]:void 0,[i]);return m(()=>{if(!!a)return r?r(a):a},[a,r,...t])}function R(e={},t=[]){const{id:n,selector:r}=e,c=b(),i=n||c.canvas,a=P(o=>i?o.iiif.entities.Canvas[i]:void 0,[i]);return m(()=>{if(!!a)return r?r(a):a},[a,r,...t])}function Ct(e,t){var n;const r=(n=e==null?void 0:e.iiif)==null?void 0:n.meta[t];return r?r.annotationPageManager:null}function It(e,t){return P(n=>{const r=[];if(!e)return r;const c=Object.keys(n.iiif.entities.AnnotationPage);for(const i of c)if(!t||t.indexOf(i)!==-1){const a=Ct(n,i);a&&a.views&&a.views[e]&&r.push(i)}return r},[e])}function Pe({canvas:e,manifest:t,all:n,canvases:r}){const c=[];if(t)for(const i of t.annotations)c.indexOf(i.id)===-1&&c.push(i.id);if(n){if(r&&r.length)for(const i of r)for(const a of i.annotations)c.indexOf(a.id)===-1&&c.push(a.id)}else if(e)for(const i of e.annotations)c.indexOf(i.id)===-1&&c.push(i.id);return c}var bt=Object.defineProperty,xt=Object.defineProperties,$t=Object.getOwnPropertyDescriptors,Oe=Object.getOwnPropertySymbols,Rt=Object.prototype.hasOwnProperty,At=Object.prototype.propertyIsEnumerable,we=(e,t,n)=>t in e?bt(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,Se=(e,t)=>{for(var n in t||(t={}))Rt.call(t,n)&&we(e,n,t[n]);if(Oe)for(var n of Oe(t))At.call(t,n)&&we(e,n,t[n]);return e},Ee=(e,t)=>xt(e,$t(t));function Tt(e,t){var n;const r=(n=e==null?void 0:e.iiif)==null?void 0:n.meta[t];return r?r.annotationPageManager:null}function Ce(e,t={}){const n=h(),r=L(),c=R(),i=ce(),a=m(()=>Pe({all:t.all,manifest:r,canvas:c,canvases:i}),[t.all,c,i,r]),o=It(e,t.all?void 0:a),s=w(p=>{!e||n.setMetaValue([p,"annotationPageManager","views"],f=>f&&!f[e]?f:Ee(Se({},f||{}),{[e]:!1}))},[e,n]),l=w((p,f={})=>{if(!e)return;const d=n.getState(),_=[];if(f==null?void 0:f.deselectOthers){const u=Object.keys(d.iiif.entities.AnnotationPage);for(const v of u){const C=Tt(d,v);C&&C.views&&C.views[e]&&_.push(v)}}for(const u of _)s(u);n.setMetaValue([p,"annotationPageManager","views"],u=>u&&u[e]?u:Ee(Se({},u||{}),{[e]:!0}))},[e,s,n]);return{availablePageIds:a,enabledPageIds:o,setPageEnabled:l,setPageDisabled:s}}function U(e={}){const t=R(e.canvasId?{id:e.canvasId}:void 0);return P((n,r)=>{if(!t)return[];const c=r.get(t.items),i=[];for(const a of c)i.push(...r.get(a.items));return i},[t])}function Mt(e,t={}){return U(t)}function Vt(e,t=!1){}function Lt(e,t=[]){const{id:n,selector:r}=e,c=b(),i=n||c.collection,a=P(o=>i?o.iiif.entities.Collection[i]:void 0,[i]);return m(()=>{if(!!a)return r?r(a):a},[a,r,...t])}function Ie(){const t=h().getStore();return m(()=>n=>t.dispatch(n),[t])}function jt(e,t,n,r,c=[]){const i=h(),a=m(()=>J(i),[i]);$(()=>{const o=e;return o?(a.addEventListener(o,t,n,r),()=>{a.removeEventListener(o,t,n)}):()=>{}},[a,e,t,...c])}function Dt(){const e=O(T);return e&&e.vault?e.vault:X()}var Ft=Object.defineProperty,be=Object.getOwnPropertySymbols,Nt=Object.prototype.hasOwnProperty,Bt=Object.prototype.propertyIsEnumerable,xe=(e,t,n)=>t in e?Ft(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,Ut=(e,t)=>{for(var n in t||(t={}))Nt.call(t,n)&&xe(e,n,t[n]);if(be)for(var n of be(t))Bt.call(t,n)&&xe(e,n,t[n]);return e},Ht=(e,t,n)=>new Promise((r,c)=>{var i=s=>{try{o(n.next(s))}catch(l){c(l)}},a=s=>{try{o(n.throw(s))}catch(l){c(l)}},o=s=>s.done?r(s.value):Promise.resolve(s.value).then(i,a);o((n=n.apply(e,t)).next())});const Qt=(e,t={})=>{const n=h(),[r,c]=S(e),i=m(()=>n.get(e),[e,n]),{data:a,isFetching:o}=k(`collection:${e}`,()=>Ht(void 0,null,function*(){const s=i||(yield n.loadCollection(e));return s&&c(s.id),s}),Ut({refetchIntervalInBackground:!1,refetchOnMount:!1,refetchOnWindowFocus:!1,refetchInterval:!1,initialData:i},t));return{isLoaded:!o,id:r,collection:a}};var kt=(e,t,n)=>new Promise((r,c)=>{var i=s=>{try{o(n.next(s))}catch(l){c(l)}},a=s=>{try{o(n.throw(s))}catch(l){c(l)}},o=s=>s.done?r(s.value):Promise.resolve(s.value).then(i,a);o((n=n.apply(e,t)).next())});function $e(){const e=R(),t=U(),n=h(),r=N();return k(`canvas-first-image-service:${e?e.id:"undefined"}`,()=>kt(this,null,function*(){if(e&&t.length){const c=t[0],i=n.get(c.body[0]),o=z(i)[0];return o&&(yield r.loadService({id:o.id||o["@id"],width:o.width||e.width,height:o.height||e.height}))||void 0}}),{refetchOnWindowFocus:!1,refetchOnMount:!1,refetchIntervalInBackground:!1,refetchInterval:!1,initialData:()=>{if(e&&t.length){const c=t[0],i=n.get(c.body[0]),o=z(i)[0];return o&&r.loadServiceSync({id:o.id||o["@id"],width:o.width||e.width,height:o.height||e.height})||void 0}}})}function zt(){const e=$e();return{isLoading:e.isFetching,tile:e.data?{id:e.data.id||e.data["@id"],width:e.data.width,height:e.data.height,imageService:e.data,thumbnail:void 0}:null}}function Re(e,t=[]){const n=h(),r=U(),[c,i]=S((e==null?void 0:e.defaultChoices)||[]),a=m(()=>me(n,r,c),[n,r,c,...t]),s={makeChoice:w((l,{deselectOthers:p=!0,deselect:f=!1}={})=>{if(a.choice){if(a.choice.type!=="single-choice")throw new Error("Complex choice not supported yet");i(d=>{if(f){const u=d.filter(v=>v!==l);if(u.length===0){const v=a.items[0].resource.id;return v?[v]:[]}return u}if(p)return[l];const _=[...d];if(_.length===0&&a.items.length){const u=a.items[0].resource.id;u&&_.push(u)}return d.indexOf(l)!==-1?d:[...d,l]})}},[a.choice])};return[a,s]}function Yt(e={},t=[]){const{id:n,selector:r}=e,c=b(),i=n||c.range,a=P(o=>i?o.iiif.entities.Range[i]:void 0,[i]);return m(()=>{if(!!a)return r?r(a):a},[a,r,...t])}function Wt(e,t){const n=h(),r=m(()=>J(n),[n]),c=P(()=>e&&e.id?n.getResourceMeta(e.id,"eventManager"):null,[e]);return m(()=>e?r.getListenersAsProps(e,t):{},[c,e,n,t])}function Ae(e,t){return P((n,r)=>r.get(e.map(c=>({id:c,type:t}))),[e,t])}function Gt(){const e=L();return e?e.service.find(t=>t.profile==="SearchService1"||t.profile==="http://iiif.io/api/search/1/search"):void 0}function Xt(){const e=h();return m(()=>K(e),[e])}function qt(e,t){const n=h(),r=m(()=>K(n),[n]);return P(()=>{if(!e)return null;const c=r.getAppliedStyles(e.id);return c?t?c[t]:c:void 0},[e,t])}const Te=(e,t=[])=>{const n=h();$(()=>{e(n)},[n,...t])};function Jt(e,t,{canvasId:n,manifestId:r}={}){const c=h(),i=N(),a=m(()=>Ye(c,{imageServiceLoader:i}),[c,i]),[o,s]=S(),l=L(r?{id:r}:void 0),p=R(n?{id:n}:void 0),f=p||l,d=Q(!1);if($(()=>()=>{d.current=!0},[]),!f)throw new Error("Must be called under a manifest or canvas context.");return Te(_=>{a.getBestThumbnailAtSize(f,e,t).then(u=>{u.best&&!d.current&&s(u.best)})},[f]),o}var Kt=Object.defineProperty,Zt=Object.defineProperties,en=Object.getOwnPropertyDescriptors,Me=Object.getOwnPropertySymbols,tn=Object.prototype.hasOwnProperty,nn=Object.prototype.propertyIsEnumerable,Ve=(e,t,n)=>t in e?Kt(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,Le=(e,t)=>{for(var n in t||(t={}))tn.call(t,n)&&Ve(e,n,t[n]);if(Me)for(var n of Me(t))nn.call(t,n)&&Ve(e,n,t[n]);return e},je=(e,t)=>Zt(e,en(t));function De(){const e=N(),[t,n]=S({}),r=Q(!1);return $(()=>()=>{r.current=!0},[]),[w((i,{height:a,width:o})=>{if(i){const s=i.id||i["@id"],l=e.loadServiceSync({id:s,width:i.width||o,height:i.height||a});l?i=l:t[s]||(r.current||n(p=>je(Le({},p),{[s]:"loading"})),e.loadService({id:s,width:i.width||o,height:i.height||a}).then(()=>{r.current||n(p=>je(Le({},p),{[s]:"done"}))}))}return i},[e,t]),t]}const rn="@iiif/IMPORT_ENTITIES",on="@iiif/MODIFY_ENTITY_FIELD",an="@iiif/REORDER_ENTITY_FIELD",sn="@iiif/ADD_REFERENCE",cn="@iiif/REMOVE_REFERENCE",ln=y(rn)(),un=y(on)(),fn=y(an)(),pn=y(sn)(),dn=y(cn)(),W={importEntities:ln,modifyEntityField:un,reorderEntityField:fn,addReference:pn,removeReference:dn},vn="@iiif/ADD_MAPPING",mn="@iiif/ADD_MAPPINGS";y(vn)(),y(mn)();const gn="@iiif/SET_META_VALUE",hn="@iiif/SET_META_VALUE_DYNAMIC",yn="@iiif/UNSET_META_VALUE";y(gn)(),y(hn)(),y(yn)();const _n="@iiif/REQUEST_RESOURCE",Pn="@iiif/REQUEST_ERROR",On="@iiif/REQUEST_MISMATCH",wn="@iiif/REQUEST_COMPLETE",Sn="@iiif/REQUEST_OFFLINE_RESOURCE";y(_n)(),y(Pn)(),y(On)(),y(wn)(),y(Sn)();const En="@iiif/BATCH";y(En)();var Cn=Object.defineProperty,In=Object.defineProperties,bn=Object.getOwnPropertyDescriptors,Fe=Object.getOwnPropertySymbols,xn=Object.prototype.hasOwnProperty,$n=Object.prototype.propertyIsEnumerable,Ne=(e,t,n)=>t in e?Cn(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,Rn=(e,t)=>{for(var n in t||(t={}))xn.call(t,n)&&Ne(e,n,t[n]);if(Fe)for(var n of Fe(t))$n.call(t,n)&&Ne(e,n,t[n]);return e},An=(e,t)=>In(e,bn(t));function Be(e){return typeof e!="string"&&e&&e.bindToVault}function Tn(){const e=h(),t=Q([]),n=Ie(),r=m(()=>`vault://annotation-page/${new Date().getTime()}/${Math.round(Math.random()*1e9).toString(16)}`,[]);ke(()=>{const o=An(Rn({},We),{id:r,items:[]});n(W.importEntities({entities:{AnnotationPage:{[o.id]:o}}}))},[r]);const c=P(o=>r?o.iiif.entities.AnnotationPage[r]:null,[r]),i=w((o,s)=>{if(r){if(Be(o)){const f=o;f.__vault||f.bindToVault(e),o=typeof f.source=="string"?f.source:f.source.id,t.current[o]=f}else typeof o!="string"&&(o=o.id);const l=e.get({id:r,type:"AnnotationPage"}),p=e.get({id:o,type:"Annotation"});l&&p&&(l.items.find(f=>f.id===p.id)||n(W.addReference({id:r,type:"AnnotationPage",key:"items",reference:{id:o,type:"Annotation"},index:s})))}},[r]),a=w(o=>{r&&(Be(o)?o=typeof o.source=="string"?o.source:o.source.id:typeof o!="string"&&(o=o.id),t.current[o]&&t.current[o].beforeRemove(),e.get({id:r,type:"AnnotationPage"})&&n(W.removeReference({id:r,type:"AnnotationPage",key:"items",reference:{id:o,type:"Annotation"}})))},[r]);return[c,{addAnnotation:i,removeAnnotation:a}]}var Mn=Object.defineProperty,Vn=Object.defineProperties,Ln=Object.getOwnPropertyDescriptors,Ue=Object.getOwnPropertySymbols,jn=Object.prototype.hasOwnProperty,Dn=Object.prototype.propertyIsEnumerable,He=(e,t,n)=>t in e?Mn(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,Fn=(e,t)=>{for(var n in t||(t={}))jn.call(t,n)&&He(e,n,t[n]);if(Ue)for(var n of Ue(t))Dn.call(t,n)&&He(e,n,t[n]);return e},Nn=(e,t)=>Vn(e,Ln(t));function Bn(e){const t=L(),n=R(),r=h(),[c,i]=De(),{enabledPageIds:a}=Ce((e==null?void 0:e.annotationPageManagerId)||(t==null?void 0:t.id),{all:!1}),o=Ae(a,"AnnotationPage"),s=(e==null?void 0:e.strategies)||["images","media","complex-timeline"],[l,p]=Re(e,[i]),f=m(()=>{if(!n||l.types.length===0)return Y;if(l.types.length!==1)return s.indexOf("complex-timeline")===-1?E("Complex timeline not supported"):E("ComplexTimelineStrategy not yet supported");const d=l.types[0];return d==="image"?s.indexOf("images")===-1?E("Image not supported"):he(n,l,c):d==="audio"?s.indexOf("media")===-1?E("Media not supported"):E("Audio strategy not yet supported"):d==="video"?s.indexOf("media")===-1?E("Media not supported"):E("Video strategy not yet supported"):Y},[n,l,r,p.makeChoice]);return m(()=>f.type==="unknown"?[f,ge]:[Nn(Fn({},f),{annotations:{pages:o}}),p],[f,o])}export{Ke as AnnotationContext,ne as CanvasContext,Ze as CollectionContext,lt as ContextBridge,le as ImageServiceLoaderContext,re as ManifestContext,et as RangeContext,T as ReactVaultContext,x as ResourceProvider,D as ResourceReactContext,at as SimpleViewerProvider,V as SimpleViewerReactContext,oe as VaultProvider,M as VisibleCanvasReactContext,ge as emptyActions,I as expandTarget,Pe as flattenAnnotationPageIds,he as getImageStrategy,me as getPaintables,B as parseSelector,ve as parseSpecificResource,Y as unknownResponse,E as unsupportedStrategy,Et as useAnnotation,Ce as useAnnotationPageManager,Mt as useAnnotationsAtTime,R as useCanvas,Vt as useCanvasClock,Lt as useCollection,ct as useContextBridge,Ie as useDispatch,jt as useEventListener,Dt as useExistingVault,Qt as useExternalCollection,se as useExternalManifest,$e as useImageService,N as useImageServiceLoader,zt as useImageTile,De as useLoadImageService,L as useManifest,Re as usePaintables,U as usePaintingAnnotations,Yt as useRange,Bn as useRenderingStrategy,b as useResourceContext,Wt as useResourceEvents,Ae as useResources,Gt as useSearchService,st as useSimpleViewer,Xt as useStyleHelper,qt as useStyles,Jt as useThumbnail,h as useVault,Te as useVaultEffect,P as useVaultSelector,Tn as useVirtualAnnotationPage,ce as useVisibleCanvases};
|
|
1
|
+
import React, { useContext, useMemo, useState, useEffect, createContext, useCallback, useRef, useLayoutEffect } from 'react';
|
|
2
|
+
import { globalVault, Vault } from '@iiif/vault';
|
|
3
|
+
import { ImageServiceLoader, getImageServices } from '@atlas-viewer/iiif-image-api';
|
|
4
|
+
import { createEventsHelper, createStylesHelper, createThumbnailHelper } from '@iiif/vault-helpers';
|
|
5
|
+
import { createAction } from 'typesafe-actions';
|
|
6
|
+
|
|
7
|
+
var __defProp$5 = Object.defineProperty;
|
|
8
|
+
var __getOwnPropSymbols$5 = Object.getOwnPropertySymbols;
|
|
9
|
+
var __hasOwnProp$5 = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __propIsEnum$5 = Object.prototype.propertyIsEnumerable;
|
|
11
|
+
var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
12
|
+
var __spreadValues$5 = (a, b) => {
|
|
13
|
+
for (var prop in b || (b = {}))
|
|
14
|
+
if (__hasOwnProp$5.call(b, prop))
|
|
15
|
+
__defNormalProp$5(a, prop, b[prop]);
|
|
16
|
+
if (__getOwnPropSymbols$5)
|
|
17
|
+
for (var prop of __getOwnPropSymbols$5(b)) {
|
|
18
|
+
if (__propIsEnum$5.call(b, prop))
|
|
19
|
+
__defNormalProp$5(a, prop, b[prop]);
|
|
20
|
+
}
|
|
21
|
+
return a;
|
|
22
|
+
};
|
|
23
|
+
const defaultResourceContext = {
|
|
24
|
+
collection: void 0,
|
|
25
|
+
manifest: void 0,
|
|
26
|
+
range: void 0,
|
|
27
|
+
canvas: void 0,
|
|
28
|
+
annotation: void 0
|
|
29
|
+
};
|
|
30
|
+
const ResourceReactContext = React.createContext(defaultResourceContext);
|
|
31
|
+
const useResourceContext = () => {
|
|
32
|
+
return useContext(ResourceReactContext);
|
|
33
|
+
};
|
|
34
|
+
const ResourceProvider = ({ value, children }) => {
|
|
35
|
+
const parentContext = useResourceContext();
|
|
36
|
+
const newContext = useMemo(() => {
|
|
37
|
+
return __spreadValues$5(__spreadValues$5({}, parentContext), value);
|
|
38
|
+
}, [value, parentContext]);
|
|
39
|
+
return /* @__PURE__ */ React.createElement(ResourceReactContext.Provider, {
|
|
40
|
+
value: newContext
|
|
41
|
+
}, children);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const AnnotationContext = ({ annotation, children }) => {
|
|
45
|
+
return /* @__PURE__ */ React.createElement(ResourceProvider, {
|
|
46
|
+
value: { annotation }
|
|
47
|
+
}, children);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const CanvasContext = ({ canvas, children }) => {
|
|
51
|
+
return /* @__PURE__ */ React.createElement(ResourceProvider, {
|
|
52
|
+
value: { canvas }
|
|
53
|
+
}, children);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const CollectionContext = ({ collection, children }) => {
|
|
57
|
+
return /* @__PURE__ */ React.createElement(ResourceProvider, {
|
|
58
|
+
value: { collection }
|
|
59
|
+
}, children);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const ManifestContext = ({ manifest, children }) => {
|
|
63
|
+
return /* @__PURE__ */ React.createElement(ResourceProvider, {
|
|
64
|
+
value: { manifest }
|
|
65
|
+
}, children);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const RangeContext = ({ range, children }) => {
|
|
69
|
+
return /* @__PURE__ */ React.createElement(ResourceProvider, {
|
|
70
|
+
value: { range }
|
|
71
|
+
}, children);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const ReactVaultContext = React.createContext({
|
|
75
|
+
vault: null,
|
|
76
|
+
setVaultInstance: (vault) => {
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
const VaultProvider = ({ vault, vaultOptions, useGlobal, resources, children }) => {
|
|
80
|
+
const [vaultInstance, setVaultInstance] = useState(() => {
|
|
81
|
+
if (vault) {
|
|
82
|
+
return vault;
|
|
83
|
+
}
|
|
84
|
+
if (useGlobal) {
|
|
85
|
+
return globalVault(vaultOptions);
|
|
86
|
+
}
|
|
87
|
+
if (vaultOptions) {
|
|
88
|
+
return new Vault(vaultOptions);
|
|
89
|
+
}
|
|
90
|
+
return new Vault();
|
|
91
|
+
});
|
|
92
|
+
return /* @__PURE__ */ React.createElement(ReactVaultContext.Provider, {
|
|
93
|
+
value: { vault: vaultInstance, setVaultInstance }
|
|
94
|
+
}, /* @__PURE__ */ React.createElement(ResourceProvider, {
|
|
95
|
+
value: resources || {}
|
|
96
|
+
}, children));
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const useVault = () => {
|
|
100
|
+
const { vault } = useContext(ReactVaultContext);
|
|
101
|
+
if (vault === null) {
|
|
102
|
+
throw new Error("Vault not found. Ensure you have your provider set up correctly.");
|
|
103
|
+
}
|
|
104
|
+
return vault;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
var __async$1 = (__this, __arguments, generator) => {
|
|
108
|
+
return new Promise((resolve, reject) => {
|
|
109
|
+
var fulfilled = (value) => {
|
|
110
|
+
try {
|
|
111
|
+
step(generator.next(value));
|
|
112
|
+
} catch (e) {
|
|
113
|
+
reject(e);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
var rejected = (value) => {
|
|
117
|
+
try {
|
|
118
|
+
step(generator.throw(value));
|
|
119
|
+
} catch (e) {
|
|
120
|
+
reject(e);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
124
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
125
|
+
});
|
|
126
|
+
};
|
|
127
|
+
function useExternalResource(idOrRef, { noCache = false } = {}) {
|
|
128
|
+
const id = typeof idOrRef === "string" ? idOrRef : idOrRef.id;
|
|
129
|
+
const vault = useVault();
|
|
130
|
+
const [realId, setRealId] = useState(id);
|
|
131
|
+
const [error, setError] = useState(void 0);
|
|
132
|
+
const initialData = useMemo(() => {
|
|
133
|
+
return vault.get(id, { skipSelfReturn: true }) || void 0;
|
|
134
|
+
}, [id, vault]);
|
|
135
|
+
const [resource, setResource] = useState(initialData);
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
(() => __async$1(this, null, function* () {
|
|
138
|
+
try {
|
|
139
|
+
const fetchedResource = initialData && !noCache ? initialData : yield vault.load(id);
|
|
140
|
+
const _realId = fetchedResource ? fetchedResource.id || fetchedResource["@id"] : null;
|
|
141
|
+
if (fetchedResource && realId !== _realId) {
|
|
142
|
+
setRealId(_realId);
|
|
143
|
+
}
|
|
144
|
+
setResource(fetchedResource);
|
|
145
|
+
} catch (err) {
|
|
146
|
+
setError(err);
|
|
147
|
+
}
|
|
148
|
+
}))();
|
|
149
|
+
}, [id, noCache]);
|
|
150
|
+
return {
|
|
151
|
+
isLoaded: !!resource,
|
|
152
|
+
id: realId,
|
|
153
|
+
requestId: id,
|
|
154
|
+
error,
|
|
155
|
+
resource,
|
|
156
|
+
cached: !!(resource && resource === initialData)
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function useExternalManifest(idOrRef, options) {
|
|
161
|
+
const { id, isLoaded, error, resource, requestId, cached } = useExternalResource(idOrRef, options);
|
|
162
|
+
return { id, isLoaded, error, manifest: resource, requestId, cached };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function useVaultSelector(selector, deps = []) {
|
|
166
|
+
const vault = useVault();
|
|
167
|
+
const [selectedState, setSelectedState] = useState(() => selector(vault.getState(), vault));
|
|
168
|
+
useEffect(() => {
|
|
169
|
+
return vault.subscribe((s) => selector(s, vault), (state) => {
|
|
170
|
+
setSelectedState(state);
|
|
171
|
+
}, false);
|
|
172
|
+
}, deps);
|
|
173
|
+
return selectedState;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const VisibleCanvasReactContext = React.createContext([]);
|
|
177
|
+
function useVisibleCanvases() {
|
|
178
|
+
const ids = useContext(VisibleCanvasReactContext);
|
|
179
|
+
return useVaultSelector((state) => {
|
|
180
|
+
return ids.map((id) => state.iiif.entities.Canvas[id]).filter(Boolean);
|
|
181
|
+
}, [ids]);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const noop = () => {
|
|
185
|
+
};
|
|
186
|
+
const SimpleViewerReactContext = createContext({
|
|
187
|
+
setCurrentCanvasId: noop,
|
|
188
|
+
setCurrentCanvasIndex: noop,
|
|
189
|
+
nextCanvas: noop,
|
|
190
|
+
previousCanvas: noop,
|
|
191
|
+
currentCanvasIndex: -1,
|
|
192
|
+
totalCanvases: 0,
|
|
193
|
+
pagingView: true
|
|
194
|
+
});
|
|
195
|
+
const SimpleViewerProvider = (props) => {
|
|
196
|
+
const manifest = useExternalManifest(props.manifest);
|
|
197
|
+
const [currentCanvasId, setCurrentCanvasId] = useState("");
|
|
198
|
+
const [visible, setVisible] = useState([]);
|
|
199
|
+
const pagingView = (typeof props.pagingEnabled === "undefined" || props.pagingEnabled) && manifest.manifest && manifest.manifest.behavior && manifest.manifest.behavior.includes("paged");
|
|
200
|
+
useEffect(() => {
|
|
201
|
+
var _a, _b;
|
|
202
|
+
if (manifest.manifest) {
|
|
203
|
+
setCurrentCanvasId((_a = manifest.manifest.items[0]) == null ? void 0 : _a.id);
|
|
204
|
+
setVisible([(_b = manifest.manifest.items[0]) == null ? void 0 : _b.id]);
|
|
205
|
+
}
|
|
206
|
+
}, [manifest.manifest, props.manifest]);
|
|
207
|
+
const canvasList = useMemo(() => {
|
|
208
|
+
var _a;
|
|
209
|
+
return ((_a = manifest.manifest) == null ? void 0 : _a.items.map((c) => c.id)) || [];
|
|
210
|
+
}, [manifest.manifest, props.manifest]);
|
|
211
|
+
const currentCanvasIndex = useMemo(() => canvasList.indexOf(currentCanvasId), [canvasList, currentCanvasId]);
|
|
212
|
+
const nextCanvas = useCallback(() => {
|
|
213
|
+
if (canvasList.length && currentCanvasId) {
|
|
214
|
+
if (currentCanvasIndex === -1) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (canvasList[currentCanvasIndex + 2] ? currentCanvasIndex + 2 === canvasList.length : currentCanvasIndex === canvasList.length) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
const newCanvas = pagingView && currentCanvasIndex !== 0 ? canvasList[currentCanvasIndex + 2] : canvasList[currentCanvasIndex + 1];
|
|
221
|
+
const pageCanvas = pagingView ? currentCanvasIndex !== 0 ? canvasList[currentCanvasIndex + 3] : canvasList[currentCanvasIndex + 2] : null;
|
|
222
|
+
if (newCanvas) {
|
|
223
|
+
setCurrentCanvasId(newCanvas);
|
|
224
|
+
setVisible(pageCanvas ? [newCanvas, pageCanvas] : [newCanvas]);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}, [pagingView, canvasList, currentCanvasId, currentCanvasIndex]);
|
|
228
|
+
const previousCanvas = useCallback(() => {
|
|
229
|
+
if (canvasList.length && currentCanvasId) {
|
|
230
|
+
if (currentCanvasIndex === 0 || currentCanvasIndex === -1) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
const newCanvas = pagingView && currentCanvasIndex !== 1 ? canvasList[currentCanvasIndex - 2] : canvasList[currentCanvasIndex - 1];
|
|
234
|
+
const pageCanvas = pagingView && currentCanvasIndex !== 1 ? canvasList[currentCanvasIndex - 1] : null;
|
|
235
|
+
if (newCanvas) {
|
|
236
|
+
setCurrentCanvasId(newCanvas);
|
|
237
|
+
setVisible(pageCanvas ? [newCanvas, pageCanvas] : [newCanvas]);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}, [pagingView, canvasList, currentCanvasId, currentCanvasIndex]);
|
|
241
|
+
const setCurrentCanvasIndex = useCallback((idx) => {
|
|
242
|
+
const realIdx = pagingView && idx % 2 === 1 ? idx - 1 : idx;
|
|
243
|
+
const newId = canvasList[realIdx];
|
|
244
|
+
const newNextId = pagingView && realIdx !== 0 ? canvasList[realIdx + 1] : null;
|
|
245
|
+
if (newId) {
|
|
246
|
+
setCurrentCanvasId(newId);
|
|
247
|
+
setVisible((prevValue) => {
|
|
248
|
+
const newValue = newNextId ? [newId, newNextId] : [newId];
|
|
249
|
+
if (prevValue.length === prevValue.length) {
|
|
250
|
+
for (let i = 0; i < prevValue.length; i++) {
|
|
251
|
+
if (prevValue[i] !== newValue[i]) {
|
|
252
|
+
return newValue;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return prevValue;
|
|
256
|
+
}
|
|
257
|
+
return newValue;
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
}, [pagingView, canvasList]);
|
|
261
|
+
const internalSetCurrentCanvasId = useCallback((nextId) => {
|
|
262
|
+
const idx = canvasList.indexOf(nextId);
|
|
263
|
+
if (idx !== -1) {
|
|
264
|
+
setCurrentCanvasIndex(idx);
|
|
265
|
+
}
|
|
266
|
+
}, [canvasList, setCurrentCanvasIndex]);
|
|
267
|
+
const ctx = useMemo(() => ({
|
|
268
|
+
setCurrentCanvasId: internalSetCurrentCanvasId,
|
|
269
|
+
nextCanvas,
|
|
270
|
+
previousCanvas,
|
|
271
|
+
currentCanvasIndex,
|
|
272
|
+
totalCanvases: canvasList.length,
|
|
273
|
+
setCurrentCanvasIndex,
|
|
274
|
+
pagingView: true
|
|
275
|
+
}), [nextCanvas, previousCanvas, currentCanvasIndex, canvasList, setCurrentCanvasIndex, internalSetCurrentCanvasId]);
|
|
276
|
+
if (!manifest.manifest) {
|
|
277
|
+
console.warn("The manifest passed to the provider is not a valid IIIF manifest.");
|
|
278
|
+
return /* @__PURE__ */ React.createElement("div", null, "Sorry, something went wrong.");
|
|
279
|
+
}
|
|
280
|
+
if (!manifest.isLoaded) {
|
|
281
|
+
return /* @__PURE__ */ React.createElement("div", null, "Loading...");
|
|
282
|
+
}
|
|
283
|
+
return /* @__PURE__ */ React.createElement(SimpleViewerReactContext.Provider, {
|
|
284
|
+
value: ctx
|
|
285
|
+
}, /* @__PURE__ */ React.createElement(VisibleCanvasReactContext.Provider, {
|
|
286
|
+
value: visible
|
|
287
|
+
}, /* @__PURE__ */ React.createElement(ManifestContext, {
|
|
288
|
+
manifest: manifest.manifest.id
|
|
289
|
+
}, /* @__PURE__ */ React.createElement(CanvasContext, {
|
|
290
|
+
canvas: currentCanvasId
|
|
291
|
+
}, props.children))));
|
|
292
|
+
};
|
|
293
|
+
function useSimpleViewer() {
|
|
294
|
+
return useContext(SimpleViewerReactContext);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function useContextBridge() {
|
|
298
|
+
return {
|
|
299
|
+
VaultContext: useContext(ReactVaultContext),
|
|
300
|
+
ResourceContext: useContext(ResourceReactContext),
|
|
301
|
+
SimpleViewerReactContext: useContext(SimpleViewerReactContext),
|
|
302
|
+
VisibleCanvasReactContext: useContext(VisibleCanvasReactContext)
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
const ContextBridge = (props) => {
|
|
306
|
+
return /* @__PURE__ */ React.createElement(VaultProvider, {
|
|
307
|
+
vault: props.bridge.VaultContext.vault || void 0,
|
|
308
|
+
resources: props.bridge.ResourceContext
|
|
309
|
+
}, /* @__PURE__ */ React.createElement(VisibleCanvasReactContext.Provider, {
|
|
310
|
+
value: props.bridge.VisibleCanvasReactContext
|
|
311
|
+
}, /* @__PURE__ */ React.createElement(SimpleViewerReactContext.Provider, {
|
|
312
|
+
value: props.bridge.SimpleViewerReactContext
|
|
313
|
+
}, props.children)));
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
const ImageServiceLoaderContext = React.createContext(new ImageServiceLoader());
|
|
317
|
+
function useImageServiceLoader() {
|
|
318
|
+
return useContext(ImageServiceLoaderContext);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const BOX_SELECTOR = /&?(xywh=)?(pixel:|percent:)?([0-9]+(?:\.[0-9]+)?),([0-9]+(?:\.[0-9]+)?),([0-9]+(?:\.[0-9]+)?),([0-9]+(?:\.[0-9]+)?)/;
|
|
322
|
+
const TEMPORAL_SELECTOR = /&?(t=)(npt:)?([0-9]+(.[0-9]+)?)?(,([0-9]+(.[0-9]+)?))?/;
|
|
323
|
+
function parseSelector(source) {
|
|
324
|
+
if (Array.isArray(source)) {
|
|
325
|
+
return source.reduce((data, nextSource) => {
|
|
326
|
+
const { selector, selectors } = parseSelector(nextSource);
|
|
327
|
+
if (selector) {
|
|
328
|
+
if (!data.selector) {
|
|
329
|
+
data.selector = selector;
|
|
330
|
+
}
|
|
331
|
+
data.selectors.push(...selectors);
|
|
332
|
+
}
|
|
333
|
+
return data;
|
|
334
|
+
}, {
|
|
335
|
+
selector: null,
|
|
336
|
+
selectors: []
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
if (!source) {
|
|
340
|
+
return {
|
|
341
|
+
selector: null,
|
|
342
|
+
selectors: []
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
if (typeof source === "string") {
|
|
346
|
+
const [id, fragment] = source.split("#");
|
|
347
|
+
if (!fragment) {
|
|
348
|
+
return {
|
|
349
|
+
selector: null,
|
|
350
|
+
selectors: []
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
return parseSelector({ type: "FragmentSelector", value: fragment });
|
|
354
|
+
}
|
|
355
|
+
if (source.type === "PointSelector" && (source.t || source.t === 0)) {
|
|
356
|
+
const selector = {
|
|
357
|
+
type: "TemporalSelector",
|
|
358
|
+
temporal: {
|
|
359
|
+
startTime: source.t
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
return {
|
|
363
|
+
selector,
|
|
364
|
+
selectors: [selector]
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
if (source.type === "FragmentSelector") {
|
|
368
|
+
const matchBoxSelector = BOX_SELECTOR.exec(source.value);
|
|
369
|
+
if (matchBoxSelector) {
|
|
370
|
+
const selector = {
|
|
371
|
+
type: "BoxSelector",
|
|
372
|
+
spatial: {
|
|
373
|
+
unit: matchBoxSelector[2] === "percent:" ? "percent" : "pixel",
|
|
374
|
+
x: parseFloat(matchBoxSelector[3]),
|
|
375
|
+
y: parseFloat(matchBoxSelector[4]),
|
|
376
|
+
width: parseFloat(matchBoxSelector[5]),
|
|
377
|
+
height: parseFloat(matchBoxSelector[6])
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
return {
|
|
381
|
+
selector,
|
|
382
|
+
selectors: [selector]
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
const matchTimeSelector = source.value.match(TEMPORAL_SELECTOR);
|
|
386
|
+
if (matchTimeSelector) {
|
|
387
|
+
const selector = {
|
|
388
|
+
type: "TemporalSelector",
|
|
389
|
+
temporal: {
|
|
390
|
+
startTime: matchTimeSelector[4] ? parseFloat(matchTimeSelector[4]) : 0,
|
|
391
|
+
endTime: matchTimeSelector[7] ? parseFloat(matchTimeSelector[7]) : void 0
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
return {
|
|
395
|
+
selector,
|
|
396
|
+
selectors: [selector]
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
return {
|
|
400
|
+
selector: null,
|
|
401
|
+
selectors: []
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
return {
|
|
405
|
+
selector: null,
|
|
406
|
+
selectors: []
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
var __defProp$4 = Object.defineProperty;
|
|
411
|
+
var __defProps$4 = Object.defineProperties;
|
|
412
|
+
var __getOwnPropDescs$4 = Object.getOwnPropertyDescriptors;
|
|
413
|
+
var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
|
|
414
|
+
var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
|
|
415
|
+
var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
|
|
416
|
+
var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
417
|
+
var __spreadValues$4 = (a, b) => {
|
|
418
|
+
for (var prop in b || (b = {}))
|
|
419
|
+
if (__hasOwnProp$4.call(b, prop))
|
|
420
|
+
__defNormalProp$4(a, prop, b[prop]);
|
|
421
|
+
if (__getOwnPropSymbols$4)
|
|
422
|
+
for (var prop of __getOwnPropSymbols$4(b)) {
|
|
423
|
+
if (__propIsEnum$4.call(b, prop))
|
|
424
|
+
__defNormalProp$4(a, prop, b[prop]);
|
|
425
|
+
}
|
|
426
|
+
return a;
|
|
427
|
+
};
|
|
428
|
+
var __spreadProps$4 = (a, b) => __defProps$4(a, __getOwnPropDescs$4(b));
|
|
429
|
+
function expandTarget(target, options = {}) {
|
|
430
|
+
if (Array.isArray(target)) {
|
|
431
|
+
return expandTarget(target[0]);
|
|
432
|
+
}
|
|
433
|
+
if (typeof target === "string") {
|
|
434
|
+
const [id, fragment] = target.split("#");
|
|
435
|
+
if (!fragment) {
|
|
436
|
+
return {
|
|
437
|
+
type: "SpecificResource",
|
|
438
|
+
source: { id, type: options.typeMap && options.typeMap[id] || "Unknown" },
|
|
439
|
+
selector: null,
|
|
440
|
+
selectors: []
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
return expandTarget({
|
|
444
|
+
type: "SpecificResource",
|
|
445
|
+
source: { id, type: "Unknown" },
|
|
446
|
+
selector: {
|
|
447
|
+
type: "FragmentSelector",
|
|
448
|
+
value: fragment
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
if (target.type === "Choice" || target.type === "List" || target.type === "Composite" || target.type === "Independents") {
|
|
453
|
+
return expandTarget(target.items[0]);
|
|
454
|
+
}
|
|
455
|
+
if (target.type === "SpecificResource") {
|
|
456
|
+
if (target.source.type === "Canvas" && target.source.partOf && typeof target.source.partOf === "string") {
|
|
457
|
+
target.source.partOf = [
|
|
458
|
+
{
|
|
459
|
+
id: target.source.partOf,
|
|
460
|
+
type: "Manifest"
|
|
461
|
+
}
|
|
462
|
+
];
|
|
463
|
+
}
|
|
464
|
+
const { selector, selectors } = target.selector ? parseSelector(target.selector) : { selector: null, selectors: [] };
|
|
465
|
+
return {
|
|
466
|
+
type: "SpecificResource",
|
|
467
|
+
source: target.source,
|
|
468
|
+
selector,
|
|
469
|
+
selectors
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
if (target.id) {
|
|
473
|
+
if (target.type === "Canvas" && target.partOf && typeof target.partOf === "string") {
|
|
474
|
+
target.partOf = [
|
|
475
|
+
{
|
|
476
|
+
id: target.partOf,
|
|
477
|
+
type: "Manifest"
|
|
478
|
+
}
|
|
479
|
+
];
|
|
480
|
+
}
|
|
481
|
+
const [id, fragment] = target.id.split("#");
|
|
482
|
+
if (!fragment) {
|
|
483
|
+
return {
|
|
484
|
+
type: "SpecificResource",
|
|
485
|
+
source: __spreadProps$4(__spreadValues$4({}, target), {
|
|
486
|
+
id
|
|
487
|
+
}),
|
|
488
|
+
selector: null,
|
|
489
|
+
selectors: []
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
return expandTarget({
|
|
493
|
+
type: "SpecificResource",
|
|
494
|
+
source: __spreadProps$4(__spreadValues$4({}, target), {
|
|
495
|
+
id
|
|
496
|
+
}),
|
|
497
|
+
selector: {
|
|
498
|
+
type: "FragmentSelector",
|
|
499
|
+
value: fragment
|
|
500
|
+
}
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
return {
|
|
504
|
+
type: "SpecificResource",
|
|
505
|
+
source: target,
|
|
506
|
+
selector: null,
|
|
507
|
+
selectors: []
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
function parseSpecificResource(resource) {
|
|
512
|
+
if (resource.type === "SpecificResource") {
|
|
513
|
+
return [resource.source, { selector: resource.selector }];
|
|
514
|
+
}
|
|
515
|
+
return [resource, { selector: null }];
|
|
516
|
+
}
|
|
517
|
+
function getPaintables(vault, paintingAnnotations, enabledChoices) {
|
|
518
|
+
const types = [];
|
|
519
|
+
let choice = null;
|
|
520
|
+
const items = [];
|
|
521
|
+
for (const annotation of paintingAnnotations) {
|
|
522
|
+
const bodies = vault.get(annotation.body);
|
|
523
|
+
for (const unknownBody of bodies) {
|
|
524
|
+
const [body, { selector }] = parseSpecificResource(unknownBody);
|
|
525
|
+
const type = (body.type || "unknown").toLowerCase();
|
|
526
|
+
if (type === "choice") {
|
|
527
|
+
const nestedBodies = vault.get(body.items);
|
|
528
|
+
const selected = enabledChoices.length ? enabledChoices.map((cid) => nestedBodies.find((b) => b.id === cid)).filter(Boolean) : [nestedBodies[0]];
|
|
529
|
+
if (selected.length === 0) {
|
|
530
|
+
selected.push(nestedBodies[0]);
|
|
531
|
+
}
|
|
532
|
+
choice = {
|
|
533
|
+
type: "single-choice",
|
|
534
|
+
items: nestedBodies.map((b) => ({
|
|
535
|
+
id: b.id,
|
|
536
|
+
label: b.label,
|
|
537
|
+
selected: selected.indexOf(b) !== -1
|
|
538
|
+
})),
|
|
539
|
+
label: unknownBody.label
|
|
540
|
+
};
|
|
541
|
+
bodies.push(...selected);
|
|
542
|
+
continue;
|
|
543
|
+
}
|
|
544
|
+
if (types.indexOf(type) === -1) {
|
|
545
|
+
types.push(type);
|
|
546
|
+
}
|
|
547
|
+
items.push({
|
|
548
|
+
type,
|
|
549
|
+
resource: body,
|
|
550
|
+
target: annotation.target,
|
|
551
|
+
selector
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
return {
|
|
556
|
+
types,
|
|
557
|
+
items,
|
|
558
|
+
choice
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
const emptyActions = {
|
|
562
|
+
makeChoice: () => {
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
const unknownResponse = { type: "unknown" };
|
|
566
|
+
const unsupportedStrategy = (reason) => {
|
|
567
|
+
return { type: "unknown", reason, annotations: { pages: [] } };
|
|
568
|
+
};
|
|
569
|
+
|
|
570
|
+
function getImageStrategy(canvas, paintables, loadImageService) {
|
|
571
|
+
const imageTypes = [];
|
|
572
|
+
for (const singleImage of paintables.items) {
|
|
573
|
+
const resource = singleImage.resource && singleImage.resource.type === "SpecificResource" ? singleImage.resource.source : singleImage.resource;
|
|
574
|
+
if (!resource.id) {
|
|
575
|
+
return unsupportedStrategy("No resource Identifier");
|
|
576
|
+
}
|
|
577
|
+
let imageService = void 0;
|
|
578
|
+
if (resource.service) {
|
|
579
|
+
const imageServices = getImageServices(resource);
|
|
580
|
+
if (imageServices[0]) {
|
|
581
|
+
imageService = loadImageService(imageServices[0], canvas);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
const { selector: imageTarget, source } = expandTarget(singleImage.target);
|
|
585
|
+
if (source.id !== canvas.id) {
|
|
586
|
+
continue;
|
|
587
|
+
}
|
|
588
|
+
const defaultTarget = {
|
|
589
|
+
type: "BoxSelector",
|
|
590
|
+
spatial: {
|
|
591
|
+
x: 0,
|
|
592
|
+
y: 0,
|
|
593
|
+
width: canvas.width,
|
|
594
|
+
height: canvas.height
|
|
595
|
+
}
|
|
596
|
+
};
|
|
597
|
+
const target = imageTarget ? imageTarget.type === "TemporalSelector" ? {
|
|
598
|
+
type: "TemporalBoxSelector",
|
|
599
|
+
temporal: imageTarget.temporal,
|
|
600
|
+
spatial: defaultTarget.spatial
|
|
601
|
+
} : imageTarget : null;
|
|
602
|
+
const defaultImageSelector = {
|
|
603
|
+
type: "BoxSelector",
|
|
604
|
+
spatial: {
|
|
605
|
+
x: 0,
|
|
606
|
+
y: 0,
|
|
607
|
+
width: canvas.width,
|
|
608
|
+
height: canvas.height
|
|
609
|
+
}
|
|
610
|
+
};
|
|
611
|
+
const imageSelector = singleImage.resource.type === "SpecificResource" ? expandTarget(singleImage.resource) : null;
|
|
612
|
+
const selector = imageSelector && imageSelector.selector && (imageSelector.selector.type === "BoxSelector" || imageSelector.selector.type === "TemporalBoxSelector") ? {
|
|
613
|
+
type: "BoxSelector",
|
|
614
|
+
spatial: {
|
|
615
|
+
x: imageSelector.selector.spatial.x,
|
|
616
|
+
y: imageSelector.selector.spatial.y,
|
|
617
|
+
width: imageSelector.selector.spatial.width,
|
|
618
|
+
height: imageSelector.selector.spatial.height
|
|
619
|
+
}
|
|
620
|
+
} : defaultImageSelector;
|
|
621
|
+
const imageType = {
|
|
622
|
+
id: resource.id,
|
|
623
|
+
type: "Image",
|
|
624
|
+
width: target ? resource.width : canvas.width,
|
|
625
|
+
height: target ? resource.height : canvas.height,
|
|
626
|
+
service: imageService,
|
|
627
|
+
sizes: imageService && imageService.sizes ? imageService.sizes : resource.width && resource.height ? [{ width: resource.width, height: resource.height }] : [],
|
|
628
|
+
target: target && target.type !== "PointSelector" ? target : defaultTarget,
|
|
629
|
+
selector
|
|
630
|
+
};
|
|
631
|
+
imageTypes.push(imageType);
|
|
632
|
+
}
|
|
633
|
+
return {
|
|
634
|
+
type: "images",
|
|
635
|
+
image: imageTypes[0],
|
|
636
|
+
images: imageTypes,
|
|
637
|
+
choice: paintables.choice
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
var __defProp$3 = Object.defineProperty;
|
|
642
|
+
var __defProps$3 = Object.defineProperties;
|
|
643
|
+
var __getOwnPropDescs$3 = Object.getOwnPropertyDescriptors;
|
|
644
|
+
var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
|
|
645
|
+
var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
|
|
646
|
+
var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
|
|
647
|
+
var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
648
|
+
var __spreadValues$3 = (a, b) => {
|
|
649
|
+
for (var prop in b || (b = {}))
|
|
650
|
+
if (__hasOwnProp$3.call(b, prop))
|
|
651
|
+
__defNormalProp$3(a, prop, b[prop]);
|
|
652
|
+
if (__getOwnPropSymbols$3)
|
|
653
|
+
for (var prop of __getOwnPropSymbols$3(b)) {
|
|
654
|
+
if (__propIsEnum$3.call(b, prop))
|
|
655
|
+
__defNormalProp$3(a, prop, b[prop]);
|
|
656
|
+
}
|
|
657
|
+
return a;
|
|
658
|
+
};
|
|
659
|
+
var __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b));
|
|
660
|
+
function useAnnotation(options = {}, deps = []) {
|
|
661
|
+
const { id, selector } = options;
|
|
662
|
+
const ctx = useResourceContext();
|
|
663
|
+
const vault = useVault();
|
|
664
|
+
const annotationId = id ? id : ctx.annotation;
|
|
665
|
+
const annotation = useVaultSelector((s) => annotationId ? s.iiif.entities.Annotation[annotationId] : void 0, [annotationId]);
|
|
666
|
+
const body = useVaultSelector((s) => annotation && annotation.body ? annotation.body.map((singleBody) => singleBody ? s.iiif.entities[singleBody.type][singleBody.id] : null).filter(Boolean) : [], [annotation]);
|
|
667
|
+
return useMemo(() => {
|
|
668
|
+
if (!annotation) {
|
|
669
|
+
return void 0;
|
|
670
|
+
}
|
|
671
|
+
const newAnnotation = __spreadProps$3(__spreadValues$3({}, annotation), {
|
|
672
|
+
body,
|
|
673
|
+
target: expandTarget(annotation.target, { typeMap: vault.getState().iiif.mapping })
|
|
674
|
+
});
|
|
675
|
+
if (selector) {
|
|
676
|
+
return selector(newAnnotation);
|
|
677
|
+
}
|
|
678
|
+
return newAnnotation;
|
|
679
|
+
}, [annotation, selector, body, ...deps]);
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
function useManifest(options = {}, deps = []) {
|
|
683
|
+
const { id, selector } = options;
|
|
684
|
+
const ctx = useResourceContext();
|
|
685
|
+
useVault();
|
|
686
|
+
const manifestId = id ? id : ctx.manifest;
|
|
687
|
+
const manifest = useVaultSelector((s) => manifestId ? s.iiif.entities.Manifest[manifestId] : void 0, [manifestId]);
|
|
688
|
+
return useMemo(() => {
|
|
689
|
+
if (!manifest) {
|
|
690
|
+
return void 0;
|
|
691
|
+
}
|
|
692
|
+
if (selector) {
|
|
693
|
+
return selector(manifest);
|
|
694
|
+
}
|
|
695
|
+
return manifest;
|
|
696
|
+
}, [manifest, selector, ...deps]);
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
function useCanvas(options = {}, deps = []) {
|
|
700
|
+
const { id, selector } = options;
|
|
701
|
+
const ctx = useResourceContext();
|
|
702
|
+
const canvasId = id ? id : ctx.canvas;
|
|
703
|
+
const canvas = useVaultSelector((s) => canvasId ? s.iiif.entities.Canvas[canvasId] : void 0, [canvasId]);
|
|
704
|
+
return useMemo(() => {
|
|
705
|
+
if (!canvas) {
|
|
706
|
+
return void 0;
|
|
707
|
+
}
|
|
708
|
+
if (selector) {
|
|
709
|
+
return selector(canvas);
|
|
710
|
+
}
|
|
711
|
+
return canvas;
|
|
712
|
+
}, [canvas, selector, ...deps]);
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
function getMeta$1(state, resourceId) {
|
|
716
|
+
var _a;
|
|
717
|
+
const resourceMeta = (_a = state == null ? void 0 : state.iiif) == null ? void 0 : _a.meta[resourceId];
|
|
718
|
+
if (!resourceMeta) {
|
|
719
|
+
return null;
|
|
720
|
+
}
|
|
721
|
+
return resourceMeta.annotationPageManager;
|
|
722
|
+
}
|
|
723
|
+
function useEnabledAnnotationPageIds(resourceId, availablePageIds) {
|
|
724
|
+
return useVaultSelector((state) => {
|
|
725
|
+
const pageIds = [];
|
|
726
|
+
if (!resourceId) {
|
|
727
|
+
return pageIds;
|
|
728
|
+
}
|
|
729
|
+
const allAnnotationListIds = Object.keys(state.iiif.entities.AnnotationPage);
|
|
730
|
+
for (const annotationListId of allAnnotationListIds) {
|
|
731
|
+
if (!availablePageIds || availablePageIds.indexOf(annotationListId) !== -1) {
|
|
732
|
+
const annotationListMeta = getMeta$1(state, annotationListId);
|
|
733
|
+
if (annotationListMeta && annotationListMeta.views && annotationListMeta.views[resourceId]) {
|
|
734
|
+
pageIds.push(annotationListId);
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
return pageIds;
|
|
739
|
+
}, [resourceId, availablePageIds]);
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
function flattenAnnotationPageIds({
|
|
743
|
+
canvas,
|
|
744
|
+
manifest,
|
|
745
|
+
all,
|
|
746
|
+
canvases
|
|
747
|
+
}) {
|
|
748
|
+
const foundIds = [];
|
|
749
|
+
if (manifest) {
|
|
750
|
+
for (const page of manifest.annotations) {
|
|
751
|
+
if (foundIds.indexOf(page.id) === -1) {
|
|
752
|
+
foundIds.push(page.id);
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
if (all) {
|
|
757
|
+
if (canvases && canvases.length) {
|
|
758
|
+
for (const canvas_ of canvases) {
|
|
759
|
+
for (const page of canvas_.annotations) {
|
|
760
|
+
if (foundIds.indexOf(page.id) === -1) {
|
|
761
|
+
foundIds.push(page.id);
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
} else if (canvas) {
|
|
767
|
+
for (const page of canvas.annotations) {
|
|
768
|
+
if (foundIds.indexOf(page.id) === -1) {
|
|
769
|
+
foundIds.push(page.id);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
return foundIds;
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
var __defProp$2 = Object.defineProperty;
|
|
777
|
+
var __defProps$2 = Object.defineProperties;
|
|
778
|
+
var __getOwnPropDescs$2 = Object.getOwnPropertyDescriptors;
|
|
779
|
+
var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
|
|
780
|
+
var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
|
|
781
|
+
var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
|
|
782
|
+
var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
783
|
+
var __spreadValues$2 = (a, b) => {
|
|
784
|
+
for (var prop in b || (b = {}))
|
|
785
|
+
if (__hasOwnProp$2.call(b, prop))
|
|
786
|
+
__defNormalProp$2(a, prop, b[prop]);
|
|
787
|
+
if (__getOwnPropSymbols$2)
|
|
788
|
+
for (var prop of __getOwnPropSymbols$2(b)) {
|
|
789
|
+
if (__propIsEnum$2.call(b, prop))
|
|
790
|
+
__defNormalProp$2(a, prop, b[prop]);
|
|
791
|
+
}
|
|
792
|
+
return a;
|
|
793
|
+
};
|
|
794
|
+
var __spreadProps$2 = (a, b) => __defProps$2(a, __getOwnPropDescs$2(b));
|
|
795
|
+
function getMeta(state, resourceId) {
|
|
796
|
+
var _a;
|
|
797
|
+
const resourceMeta = (_a = state == null ? void 0 : state.iiif) == null ? void 0 : _a.meta[resourceId];
|
|
798
|
+
if (!resourceMeta) {
|
|
799
|
+
return null;
|
|
800
|
+
}
|
|
801
|
+
return resourceMeta.annotationPageManager;
|
|
802
|
+
}
|
|
803
|
+
function useAnnotationPageManager(resourceId, options = {}) {
|
|
804
|
+
const vault = useVault();
|
|
805
|
+
const manifest = useManifest();
|
|
806
|
+
const canvas = useCanvas();
|
|
807
|
+
const canvases = useVisibleCanvases();
|
|
808
|
+
const availablePageIds = useMemo(() => {
|
|
809
|
+
return flattenAnnotationPageIds({
|
|
810
|
+
all: options.all,
|
|
811
|
+
manifest,
|
|
812
|
+
canvas,
|
|
813
|
+
canvases
|
|
814
|
+
});
|
|
815
|
+
}, [options.all, canvas, canvases, manifest]);
|
|
816
|
+
const enabledPageIds = useEnabledAnnotationPageIds(resourceId, options.all ? void 0 : availablePageIds);
|
|
817
|
+
const setPageDisabled = useCallback((deselectId) => {
|
|
818
|
+
if (!resourceId) {
|
|
819
|
+
return;
|
|
820
|
+
}
|
|
821
|
+
vault.setMetaValue([deselectId, "annotationPageManager", "views"], (existingResources) => {
|
|
822
|
+
if (existingResources && !existingResources[resourceId]) {
|
|
823
|
+
return existingResources;
|
|
824
|
+
}
|
|
825
|
+
return __spreadProps$2(__spreadValues$2({}, existingResources || {}), {
|
|
826
|
+
[resourceId]: false
|
|
827
|
+
});
|
|
828
|
+
});
|
|
829
|
+
}, [resourceId, vault]);
|
|
830
|
+
const setPageEnabled = useCallback((id, opt = {}) => {
|
|
831
|
+
if (!resourceId) {
|
|
832
|
+
return;
|
|
833
|
+
}
|
|
834
|
+
const state = vault.getState();
|
|
835
|
+
const toDeselect = [];
|
|
836
|
+
if (opt == null ? void 0 : opt.deselectOthers) {
|
|
837
|
+
const allAnnotationListIds = Object.keys(state.iiif.entities.AnnotationPage);
|
|
838
|
+
for (const annotationPageId of allAnnotationListIds) {
|
|
839
|
+
const annotationListMeta = getMeta(state, annotationPageId);
|
|
840
|
+
if (annotationListMeta && annotationListMeta.views && annotationListMeta.views[resourceId]) {
|
|
841
|
+
toDeselect.push(annotationPageId);
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
for (const deselectId of toDeselect) {
|
|
846
|
+
setPageDisabled(deselectId);
|
|
847
|
+
}
|
|
848
|
+
vault.setMetaValue([id, "annotationPageManager", "views"], (existingResources) => {
|
|
849
|
+
if (existingResources && existingResources[resourceId]) {
|
|
850
|
+
return existingResources;
|
|
851
|
+
}
|
|
852
|
+
return __spreadProps$2(__spreadValues$2({}, existingResources || {}), {
|
|
853
|
+
[resourceId]: true
|
|
854
|
+
});
|
|
855
|
+
});
|
|
856
|
+
}, [resourceId, setPageDisabled, vault]);
|
|
857
|
+
return {
|
|
858
|
+
availablePageIds,
|
|
859
|
+
enabledPageIds,
|
|
860
|
+
setPageEnabled,
|
|
861
|
+
setPageDisabled
|
|
862
|
+
};
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
function usePaintingAnnotations(options = {}) {
|
|
866
|
+
const canvas = useCanvas(options.canvasId ? { id: options.canvasId } : void 0);
|
|
867
|
+
return useVaultSelector((state, vault) => {
|
|
868
|
+
if (!canvas) {
|
|
869
|
+
return [];
|
|
870
|
+
}
|
|
871
|
+
const annotationPages = vault.get(canvas.items);
|
|
872
|
+
const flatAnnotations = [];
|
|
873
|
+
for (const page of annotationPages) {
|
|
874
|
+
flatAnnotations.push(...vault.get(page.items));
|
|
875
|
+
}
|
|
876
|
+
return flatAnnotations;
|
|
877
|
+
}, [canvas]);
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
function useAnnotationsAtTime(time, options = {}) {
|
|
881
|
+
const allAnnotations = usePaintingAnnotations(options);
|
|
882
|
+
return allAnnotations;
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
function useCanvasClock(canvasId, autoplay = false) {
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
function useCollection(options, deps = []) {
|
|
889
|
+
const { id, selector } = options;
|
|
890
|
+
const ctx = useResourceContext();
|
|
891
|
+
const collectionId = id ? id : ctx.collection;
|
|
892
|
+
const collection = useVaultSelector((s) => collectionId ? s.iiif.entities.Collection[collectionId] : void 0, [collectionId]);
|
|
893
|
+
return useMemo(() => {
|
|
894
|
+
if (!collection) {
|
|
895
|
+
return void 0;
|
|
896
|
+
}
|
|
897
|
+
if (selector) {
|
|
898
|
+
return selector(collection);
|
|
899
|
+
}
|
|
900
|
+
return collection;
|
|
901
|
+
}, [collection, selector, ...deps]);
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
function useDispatch() {
|
|
905
|
+
const vault = useVault();
|
|
906
|
+
const store = vault.getStore();
|
|
907
|
+
return useMemo(() => {
|
|
908
|
+
return (action) => store.dispatch(action);
|
|
909
|
+
}, [store]);
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
function useEventListener(resource, name, listener, scope, deps = []) {
|
|
913
|
+
const vault = useVault();
|
|
914
|
+
const helper = useMemo(() => createEventsHelper(vault), [vault]);
|
|
915
|
+
useEffect(() => {
|
|
916
|
+
const currentResource = resource;
|
|
917
|
+
if (!currentResource) {
|
|
918
|
+
return () => {
|
|
919
|
+
};
|
|
920
|
+
}
|
|
921
|
+
helper.addEventListener(currentResource, name, listener, scope);
|
|
922
|
+
return () => {
|
|
923
|
+
helper.removeEventListener(currentResource, name, listener);
|
|
924
|
+
};
|
|
925
|
+
}, [helper, resource, name, ...deps]);
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
function useExistingVault() {
|
|
929
|
+
const oldContext = useContext(ReactVaultContext);
|
|
930
|
+
return oldContext && oldContext.vault ? oldContext.vault : globalVault();
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
function useExternalCollection(idOrRef, options) {
|
|
934
|
+
const { id, isLoaded, error, resource, requestId, cached } = useExternalResource(idOrRef, options);
|
|
935
|
+
return { id, isLoaded, error, manifest: resource, requestId, cached };
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
var __async = (__this, __arguments, generator) => {
|
|
939
|
+
return new Promise((resolve, reject) => {
|
|
940
|
+
var fulfilled = (value) => {
|
|
941
|
+
try {
|
|
942
|
+
step(generator.next(value));
|
|
943
|
+
} catch (e) {
|
|
944
|
+
reject(e);
|
|
945
|
+
}
|
|
946
|
+
};
|
|
947
|
+
var rejected = (value) => {
|
|
948
|
+
try {
|
|
949
|
+
step(generator.throw(value));
|
|
950
|
+
} catch (e) {
|
|
951
|
+
reject(e);
|
|
952
|
+
}
|
|
953
|
+
};
|
|
954
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
955
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
956
|
+
});
|
|
957
|
+
};
|
|
958
|
+
function useImageService({ cacheKey } = {}) {
|
|
959
|
+
const canvas = useCanvas();
|
|
960
|
+
const annotations = usePaintingAnnotations();
|
|
961
|
+
const vault = useVault();
|
|
962
|
+
const imageService = useImageServiceLoader();
|
|
963
|
+
const [_data, setData] = useState(void 0);
|
|
964
|
+
const [isFetching, setIsFetching] = useState(false);
|
|
965
|
+
const [status, setStatus] = useState("idle");
|
|
966
|
+
const [error, setError] = useState(void 0);
|
|
967
|
+
const key = canvas ? canvas.id : "undefined";
|
|
968
|
+
const initialData = useMemo(() => {
|
|
969
|
+
try {
|
|
970
|
+
if (canvas && annotations.length) {
|
|
971
|
+
const annotation = annotations[0];
|
|
972
|
+
const resource = vault.get(annotation.body[0]);
|
|
973
|
+
const imageServices = getImageServices(resource);
|
|
974
|
+
const firstImageService = imageServices[0];
|
|
975
|
+
if (!firstImageService) {
|
|
976
|
+
return void 0;
|
|
977
|
+
}
|
|
978
|
+
return imageService.loadServiceSync({
|
|
979
|
+
id: firstImageService.id || firstImageService["@id"],
|
|
980
|
+
width: firstImageService.width || canvas.width,
|
|
981
|
+
height: firstImageService.height || canvas.height
|
|
982
|
+
}) || void 0;
|
|
983
|
+
}
|
|
984
|
+
} catch (e) {
|
|
985
|
+
console.error(e);
|
|
986
|
+
}
|
|
987
|
+
return void 0;
|
|
988
|
+
}, [key, cacheKey, canvas]);
|
|
989
|
+
const data = status === "success" && _data ? _data : initialData;
|
|
990
|
+
useEffect(() => {
|
|
991
|
+
(() => __async(this, null, function* () {
|
|
992
|
+
try {
|
|
993
|
+
if (canvas && annotations.length) {
|
|
994
|
+
const annotation = annotations[0];
|
|
995
|
+
const resource = vault.get(annotation.body[0]);
|
|
996
|
+
const imageServices = getImageServices(resource);
|
|
997
|
+
const firstImageService = imageServices[0];
|
|
998
|
+
if (!firstImageService) {
|
|
999
|
+
return;
|
|
1000
|
+
}
|
|
1001
|
+
setIsFetching(true);
|
|
1002
|
+
setStatus("loading");
|
|
1003
|
+
try {
|
|
1004
|
+
const loadedService = (yield imageService.loadService({
|
|
1005
|
+
id: firstImageService.id || firstImageService["@id"],
|
|
1006
|
+
width: firstImageService.width || canvas.width,
|
|
1007
|
+
height: firstImageService.height || canvas.height
|
|
1008
|
+
})) || void 0;
|
|
1009
|
+
setData(loadedService);
|
|
1010
|
+
setStatus("success");
|
|
1011
|
+
setIsFetching(false);
|
|
1012
|
+
} catch (err) {
|
|
1013
|
+
setStatus("error");
|
|
1014
|
+
setError(err);
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
} catch (err) {
|
|
1018
|
+
setStatus("error");
|
|
1019
|
+
setError(err);
|
|
1020
|
+
}
|
|
1021
|
+
}))();
|
|
1022
|
+
}, [key, cacheKey]);
|
|
1023
|
+
return useMemo(() => {
|
|
1024
|
+
return {
|
|
1025
|
+
data,
|
|
1026
|
+
isFetching,
|
|
1027
|
+
status,
|
|
1028
|
+
error
|
|
1029
|
+
};
|
|
1030
|
+
}, [data, isFetching, status, error]);
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
function useImageTile() {
|
|
1034
|
+
const imageService = useImageService();
|
|
1035
|
+
return {
|
|
1036
|
+
isLoading: imageService.isFetching,
|
|
1037
|
+
tile: imageService.data ? {
|
|
1038
|
+
id: imageService.data.id || imageService.data["@id"],
|
|
1039
|
+
width: imageService.data.width,
|
|
1040
|
+
height: imageService.data.height,
|
|
1041
|
+
imageService: imageService.data,
|
|
1042
|
+
thumbnail: void 0
|
|
1043
|
+
} : null
|
|
1044
|
+
};
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
function usePaintables(options, deps = []) {
|
|
1048
|
+
const vault = useVault();
|
|
1049
|
+
const paintingAnnotations = usePaintingAnnotations();
|
|
1050
|
+
const [enabledChoices, setEnabledChoices] = useState((options == null ? void 0 : options.defaultChoices) || []);
|
|
1051
|
+
const paintables = useMemo(() => getPaintables(vault, paintingAnnotations, enabledChoices), [vault, paintingAnnotations, enabledChoices, ...deps]);
|
|
1052
|
+
const makeChoice = useCallback((id, { deselectOthers = true, deselect = false } = {}) => {
|
|
1053
|
+
if (paintables.choice) {
|
|
1054
|
+
if (paintables.choice.type !== "single-choice") {
|
|
1055
|
+
throw new Error("Complex choice not supported yet");
|
|
1056
|
+
}
|
|
1057
|
+
setEnabledChoices((prevChoices) => {
|
|
1058
|
+
if (deselect) {
|
|
1059
|
+
const without = prevChoices.filter((e) => e !== id);
|
|
1060
|
+
if (without.length === 0) {
|
|
1061
|
+
const defaultId = paintables.items[0].resource.id;
|
|
1062
|
+
if (defaultId) {
|
|
1063
|
+
return [defaultId];
|
|
1064
|
+
} else {
|
|
1065
|
+
return [];
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
return without;
|
|
1069
|
+
}
|
|
1070
|
+
if (deselectOthers) {
|
|
1071
|
+
return [id];
|
|
1072
|
+
}
|
|
1073
|
+
const newChoices = [...prevChoices];
|
|
1074
|
+
if (newChoices.length === 0 && paintables.items.length) {
|
|
1075
|
+
const defaultId = paintables.items[0].resource.id;
|
|
1076
|
+
if (defaultId) {
|
|
1077
|
+
newChoices.push(defaultId);
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
if (prevChoices.indexOf(id) !== -1) {
|
|
1081
|
+
return prevChoices;
|
|
1082
|
+
}
|
|
1083
|
+
return [...prevChoices, id];
|
|
1084
|
+
});
|
|
1085
|
+
}
|
|
1086
|
+
}, [paintables.choice]);
|
|
1087
|
+
const actions = { makeChoice };
|
|
1088
|
+
return [paintables, actions];
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
function useRange(options = {}, deps = []) {
|
|
1092
|
+
const { id, selector } = options;
|
|
1093
|
+
const ctx = useResourceContext();
|
|
1094
|
+
const rangeId = id ? id : ctx.range;
|
|
1095
|
+
const range = useVaultSelector((s) => rangeId ? s.iiif.entities.Range[rangeId] : void 0, [rangeId]);
|
|
1096
|
+
return useMemo(() => {
|
|
1097
|
+
if (!range) {
|
|
1098
|
+
return void 0;
|
|
1099
|
+
}
|
|
1100
|
+
if (selector) {
|
|
1101
|
+
return selector(range);
|
|
1102
|
+
}
|
|
1103
|
+
return range;
|
|
1104
|
+
}, [range, selector, ...deps]);
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
function useResourceEvents(resource, scope) {
|
|
1108
|
+
const vault = useVault();
|
|
1109
|
+
const helper = useMemo(() => createEventsHelper(vault), [vault]);
|
|
1110
|
+
const hooks = useVaultSelector(() => {
|
|
1111
|
+
if (resource && resource.id) {
|
|
1112
|
+
return vault.getResourceMeta(resource.id, "eventManager");
|
|
1113
|
+
}
|
|
1114
|
+
return null;
|
|
1115
|
+
}, [resource]);
|
|
1116
|
+
return useMemo(() => {
|
|
1117
|
+
return resource ? helper.getListenersAsProps(resource, scope) : {};
|
|
1118
|
+
}, [hooks, resource, vault, scope]);
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
function useResources(ids, type) {
|
|
1122
|
+
return useVaultSelector((state, vault) => vault.get(ids.map((id) => ({ id, type }))), [ids, type]);
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
function useSearchService() {
|
|
1126
|
+
const manifest = useManifest();
|
|
1127
|
+
return manifest ? manifest.service.find((service) => service.profile === "SearchService1" || service.profile === "http://iiif.io/api/search/1/search") : void 0;
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
function useStyleHelper() {
|
|
1131
|
+
const vault = useVault();
|
|
1132
|
+
return useMemo(() => createStylesHelper(vault), [vault]);
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
function useStyles(resource, scope) {
|
|
1136
|
+
const vault = useVault();
|
|
1137
|
+
const helper = useMemo(() => createStylesHelper(vault), [vault]);
|
|
1138
|
+
return useVaultSelector(() => {
|
|
1139
|
+
if (!resource) {
|
|
1140
|
+
return null;
|
|
1141
|
+
}
|
|
1142
|
+
const styles = helper.getAppliedStyles(resource.id);
|
|
1143
|
+
return styles ? scope ? styles[scope] : styles : void 0;
|
|
1144
|
+
}, [resource, scope]);
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
const useVaultEffect = (callback, deps = []) => {
|
|
1148
|
+
const vault = useVault();
|
|
1149
|
+
useEffect(() => {
|
|
1150
|
+
callback(vault);
|
|
1151
|
+
}, [vault, ...deps]);
|
|
1152
|
+
};
|
|
1153
|
+
|
|
1154
|
+
function useThumbnail(request, dereference, { canvasId, manifestId } = {}) {
|
|
1155
|
+
const vault = useVault();
|
|
1156
|
+
const loader = useImageServiceLoader();
|
|
1157
|
+
const helper = useMemo(() => createThumbnailHelper(vault, { imageServiceLoader: loader }), [vault, loader]);
|
|
1158
|
+
const [thumbnail, setThumbnail] = useState();
|
|
1159
|
+
const manifest = useManifest(manifestId ? { id: manifestId } : void 0);
|
|
1160
|
+
const canvas = useCanvas(canvasId ? { id: canvasId } : void 0);
|
|
1161
|
+
const subject = canvas ? canvas : manifest;
|
|
1162
|
+
const didUnmount = useRef(false);
|
|
1163
|
+
useEffect(() => {
|
|
1164
|
+
return () => {
|
|
1165
|
+
didUnmount.current = true;
|
|
1166
|
+
};
|
|
1167
|
+
}, []);
|
|
1168
|
+
if (!subject) {
|
|
1169
|
+
throw new Error("Must be called under a manifest or canvas context.");
|
|
1170
|
+
}
|
|
1171
|
+
useVaultEffect((v) => {
|
|
1172
|
+
helper.getBestThumbnailAtSize(subject, request, dereference).then((thumb) => {
|
|
1173
|
+
if (thumb.best && !didUnmount.current) {
|
|
1174
|
+
setThumbnail(thumb.best);
|
|
1175
|
+
}
|
|
1176
|
+
});
|
|
1177
|
+
}, [subject]);
|
|
1178
|
+
return thumbnail;
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
var __defProp$1 = Object.defineProperty;
|
|
1182
|
+
var __defProps$1 = Object.defineProperties;
|
|
1183
|
+
var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
|
|
1184
|
+
var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
|
|
1185
|
+
var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
|
|
1186
|
+
var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
|
|
1187
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1188
|
+
var __spreadValues$1 = (a, b) => {
|
|
1189
|
+
for (var prop in b || (b = {}))
|
|
1190
|
+
if (__hasOwnProp$1.call(b, prop))
|
|
1191
|
+
__defNormalProp$1(a, prop, b[prop]);
|
|
1192
|
+
if (__getOwnPropSymbols$1)
|
|
1193
|
+
for (var prop of __getOwnPropSymbols$1(b)) {
|
|
1194
|
+
if (__propIsEnum$1.call(b, prop))
|
|
1195
|
+
__defNormalProp$1(a, prop, b[prop]);
|
|
1196
|
+
}
|
|
1197
|
+
return a;
|
|
1198
|
+
};
|
|
1199
|
+
var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
|
|
1200
|
+
function useLoadImageService() {
|
|
1201
|
+
const loader = useImageServiceLoader();
|
|
1202
|
+
const [imageServiceStatus, setImageServiceStatus] = useState({});
|
|
1203
|
+
const didUnmount = useRef(false);
|
|
1204
|
+
useEffect(() => {
|
|
1205
|
+
return () => {
|
|
1206
|
+
didUnmount.current = true;
|
|
1207
|
+
};
|
|
1208
|
+
}, []);
|
|
1209
|
+
const loadImageService = useCallback((imageService, { height, width }) => {
|
|
1210
|
+
if (imageService) {
|
|
1211
|
+
const imageServiceId = imageService.id || imageService["@id"];
|
|
1212
|
+
const syncLoaded = loader.loadServiceSync({
|
|
1213
|
+
id: imageServiceId,
|
|
1214
|
+
width: imageService.width || width,
|
|
1215
|
+
height: imageService.height || height
|
|
1216
|
+
});
|
|
1217
|
+
if (syncLoaded) {
|
|
1218
|
+
imageService = syncLoaded;
|
|
1219
|
+
} else if (!imageServiceStatus[imageServiceId]) {
|
|
1220
|
+
if (!didUnmount.current) {
|
|
1221
|
+
setImageServiceStatus((r) => {
|
|
1222
|
+
return __spreadProps$1(__spreadValues$1({}, r), {
|
|
1223
|
+
[imageServiceId]: "loading"
|
|
1224
|
+
});
|
|
1225
|
+
});
|
|
1226
|
+
}
|
|
1227
|
+
loader.loadService({
|
|
1228
|
+
id: imageServiceId,
|
|
1229
|
+
width: imageService.width || width,
|
|
1230
|
+
height: imageService.height || height
|
|
1231
|
+
}).then(() => {
|
|
1232
|
+
if (!didUnmount.current) {
|
|
1233
|
+
setImageServiceStatus((r) => {
|
|
1234
|
+
return __spreadProps$1(__spreadValues$1({}, r), {
|
|
1235
|
+
[imageServiceId]: "done"
|
|
1236
|
+
});
|
|
1237
|
+
});
|
|
1238
|
+
}
|
|
1239
|
+
});
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
return imageService;
|
|
1243
|
+
}, [loader, imageServiceStatus]);
|
|
1244
|
+
return [loadImageService, imageServiceStatus];
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
const IMPORT_ENTITIES = "@iiif/IMPORT_ENTITIES";
|
|
1248
|
+
const MODIFY_ENTITY_FIELD = "@iiif/MODIFY_ENTITY_FIELD";
|
|
1249
|
+
const REORDER_ENTITY_FIELD = "@iiif/REORDER_ENTITY_FIELD";
|
|
1250
|
+
const ADD_REFERENCE = "@iiif/ADD_REFERENCE";
|
|
1251
|
+
const REMOVE_REFERENCE = "@iiif/REMOVE_REFERENCE";
|
|
1252
|
+
const importEntities = createAction(IMPORT_ENTITIES)();
|
|
1253
|
+
const modifyEntityField = createAction(MODIFY_ENTITY_FIELD)();
|
|
1254
|
+
const reorderEntityField = createAction(REORDER_ENTITY_FIELD)();
|
|
1255
|
+
const addReference = createAction(ADD_REFERENCE)();
|
|
1256
|
+
const removeReference = createAction(REMOVE_REFERENCE)();
|
|
1257
|
+
const entityActions = { importEntities, modifyEntityField, reorderEntityField, addReference, removeReference };
|
|
1258
|
+
|
|
1259
|
+
const ADD_MAPPING = "@iiif/ADD_MAPPING";
|
|
1260
|
+
const ADD_MAPPINGS = "@iiif/ADD_MAPPINGS";
|
|
1261
|
+
createAction(ADD_MAPPING)();
|
|
1262
|
+
createAction(ADD_MAPPINGS)();
|
|
1263
|
+
|
|
1264
|
+
const SET_META_VALUE = "@iiif/SET_META_VALUE";
|
|
1265
|
+
const SET_META_VALUE_DYNAMIC = "@iiif/SET_META_VALUE_DYNAMIC";
|
|
1266
|
+
const UNSET_META_VALUE = "@iiif/UNSET_META_VALUE";
|
|
1267
|
+
createAction(SET_META_VALUE)();
|
|
1268
|
+
createAction(SET_META_VALUE_DYNAMIC)();
|
|
1269
|
+
createAction(UNSET_META_VALUE)();
|
|
1270
|
+
const REQUEST_RESOURCE = "@iiif/REQUEST_RESOURCE";
|
|
1271
|
+
const REQUEST_ERROR = "@iiif/REQUEST_ERROR";
|
|
1272
|
+
const REQUEST_MISMATCH = "@iiif/REQUEST_MISMATCH";
|
|
1273
|
+
const REQUEST_COMPLETE = "@iiif/REQUEST_COMPLETE";
|
|
1274
|
+
const REQUEST_OFFLINE_RESOURCE = "@iiif/REQUEST_OFFLINE_RESOURCE";
|
|
1275
|
+
createAction(REQUEST_RESOURCE)();
|
|
1276
|
+
createAction(REQUEST_ERROR)();
|
|
1277
|
+
createAction(REQUEST_MISMATCH)();
|
|
1278
|
+
createAction(REQUEST_COMPLETE)();
|
|
1279
|
+
createAction(REQUEST_OFFLINE_RESOURCE)();
|
|
1280
|
+
|
|
1281
|
+
const BATCH_ACTIONS = "@iiif/BATCH";
|
|
1282
|
+
const BATCH_IMPORT = "@iiif/BATCH_IMPORT";
|
|
1283
|
+
createAction(BATCH_ACTIONS)();
|
|
1284
|
+
createAction(BATCH_IMPORT)();
|
|
1285
|
+
|
|
1286
|
+
function isVaultActivated(obj) {
|
|
1287
|
+
return typeof obj !== "string" && obj && obj.bindToVault;
|
|
1288
|
+
}
|
|
1289
|
+
function useVirtualAnnotationPage() {
|
|
1290
|
+
const vault = useVault();
|
|
1291
|
+
const sources = useRef([]);
|
|
1292
|
+
const dispatch = useDispatch();
|
|
1293
|
+
const virtualId = useMemo(() => {
|
|
1294
|
+
return `vault://annotation-page/${new Date().getTime()}/${Math.round(Math.random() * 1e9).toString(16)}`;
|
|
1295
|
+
}, []);
|
|
1296
|
+
useLayoutEffect(() => {
|
|
1297
|
+
const page = {
|
|
1298
|
+
id: virtualId,
|
|
1299
|
+
type: "AnnotationPage",
|
|
1300
|
+
behavior: [],
|
|
1301
|
+
motivation: null,
|
|
1302
|
+
label: null,
|
|
1303
|
+
thumbnail: [],
|
|
1304
|
+
summary: null,
|
|
1305
|
+
requiredStatement: null,
|
|
1306
|
+
metadata: [],
|
|
1307
|
+
rights: null,
|
|
1308
|
+
provider: [],
|
|
1309
|
+
items: [],
|
|
1310
|
+
seeAlso: [],
|
|
1311
|
+
homepage: [],
|
|
1312
|
+
logo: [],
|
|
1313
|
+
rendering: [],
|
|
1314
|
+
service: []
|
|
1315
|
+
};
|
|
1316
|
+
dispatch(entityActions.importEntities({
|
|
1317
|
+
entities: {
|
|
1318
|
+
AnnotationPage: {
|
|
1319
|
+
[page.id]: page
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
}));
|
|
1323
|
+
}, [virtualId]);
|
|
1324
|
+
const fullPage = useVaultSelector((state) => virtualId ? state.iiif.entities.AnnotationPage[virtualId] : null, [virtualId]);
|
|
1325
|
+
const addAnnotation = useCallback((id, atIndex) => {
|
|
1326
|
+
if (virtualId) {
|
|
1327
|
+
if (isVaultActivated(id)) {
|
|
1328
|
+
const display = id;
|
|
1329
|
+
if (!display.__vault) {
|
|
1330
|
+
display.bindToVault(vault);
|
|
1331
|
+
}
|
|
1332
|
+
id = typeof display.source === "string" ? display.source : display.source.id;
|
|
1333
|
+
sources.current[id] = display;
|
|
1334
|
+
} else if (typeof id !== "string") {
|
|
1335
|
+
id = id.id;
|
|
1336
|
+
}
|
|
1337
|
+
const full = vault.get({ id: virtualId, type: "AnnotationPage" });
|
|
1338
|
+
const annotation = vault.get({ id, type: "Annotation" });
|
|
1339
|
+
if (full && annotation) {
|
|
1340
|
+
if (!full.items.find((r) => r.id === annotation.id)) {
|
|
1341
|
+
dispatch(entityActions.addReference({
|
|
1342
|
+
id: virtualId,
|
|
1343
|
+
type: "AnnotationPage",
|
|
1344
|
+
key: "items",
|
|
1345
|
+
reference: {
|
|
1346
|
+
id,
|
|
1347
|
+
type: "Annotation"
|
|
1348
|
+
},
|
|
1349
|
+
index: atIndex
|
|
1350
|
+
}));
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
}, [virtualId]);
|
|
1355
|
+
const removeAnnotation = useCallback((id) => {
|
|
1356
|
+
if (virtualId) {
|
|
1357
|
+
if (isVaultActivated(id)) {
|
|
1358
|
+
id = typeof id.source === "string" ? id.source : id.source.id;
|
|
1359
|
+
} else if (typeof id !== "string") {
|
|
1360
|
+
id = id.id;
|
|
1361
|
+
}
|
|
1362
|
+
if (sources.current[id]) {
|
|
1363
|
+
sources.current[id].beforeRemove();
|
|
1364
|
+
}
|
|
1365
|
+
const full = vault.get({ id: virtualId, type: "AnnotationPage" });
|
|
1366
|
+
if (full) {
|
|
1367
|
+
dispatch(entityActions.removeReference({
|
|
1368
|
+
id: virtualId,
|
|
1369
|
+
type: "AnnotationPage",
|
|
1370
|
+
key: "items",
|
|
1371
|
+
reference: {
|
|
1372
|
+
id,
|
|
1373
|
+
type: "Annotation"
|
|
1374
|
+
}
|
|
1375
|
+
}));
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
}, [virtualId]);
|
|
1379
|
+
return [
|
|
1380
|
+
fullPage,
|
|
1381
|
+
{
|
|
1382
|
+
addAnnotation,
|
|
1383
|
+
removeAnnotation
|
|
1384
|
+
}
|
|
1385
|
+
];
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
var __defProp = Object.defineProperty;
|
|
1389
|
+
var __defProps = Object.defineProperties;
|
|
1390
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
1391
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
1392
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
1393
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
1394
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1395
|
+
var __spreadValues = (a, b) => {
|
|
1396
|
+
for (var prop in b || (b = {}))
|
|
1397
|
+
if (__hasOwnProp.call(b, prop))
|
|
1398
|
+
__defNormalProp(a, prop, b[prop]);
|
|
1399
|
+
if (__getOwnPropSymbols)
|
|
1400
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
1401
|
+
if (__propIsEnum.call(b, prop))
|
|
1402
|
+
__defNormalProp(a, prop, b[prop]);
|
|
1403
|
+
}
|
|
1404
|
+
return a;
|
|
1405
|
+
};
|
|
1406
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
1407
|
+
function useRenderingStrategy(options) {
|
|
1408
|
+
const manifest = useManifest();
|
|
1409
|
+
const canvas = useCanvas();
|
|
1410
|
+
const vault = useVault();
|
|
1411
|
+
const [loadImageService, imageServiceStatus] = useLoadImageService();
|
|
1412
|
+
const { enabledPageIds } = useAnnotationPageManager((options == null ? void 0 : options.annotationPageManagerId) || (manifest == null ? void 0 : manifest.id) || (canvas == null ? void 0 : canvas.id), {
|
|
1413
|
+
all: false
|
|
1414
|
+
});
|
|
1415
|
+
const enabledPages = useResources(enabledPageIds, "AnnotationPage");
|
|
1416
|
+
const supports = (options == null ? void 0 : options.strategies) || ["images", "media", "complex-timeline"];
|
|
1417
|
+
const [paintables, actions] = usePaintables(options, [imageServiceStatus]);
|
|
1418
|
+
const strategy = useMemo(() => {
|
|
1419
|
+
if (!canvas || paintables.types.length === 0) {
|
|
1420
|
+
return unknownResponse;
|
|
1421
|
+
}
|
|
1422
|
+
if (paintables.types.length !== 1) {
|
|
1423
|
+
if (supports.indexOf("complex-timeline") === -1) {
|
|
1424
|
+
return unsupportedStrategy("Complex timeline not supported");
|
|
1425
|
+
}
|
|
1426
|
+
return unsupportedStrategy("ComplexTimelineStrategy not yet supported");
|
|
1427
|
+
}
|
|
1428
|
+
const mainType = paintables.types[0];
|
|
1429
|
+
if (mainType === "image") {
|
|
1430
|
+
if (supports.indexOf("images") === -1) {
|
|
1431
|
+
return unsupportedStrategy("Image not supported");
|
|
1432
|
+
}
|
|
1433
|
+
return getImageStrategy(canvas, paintables, loadImageService);
|
|
1434
|
+
}
|
|
1435
|
+
if (mainType === "audio") {
|
|
1436
|
+
if (supports.indexOf("media") === -1) {
|
|
1437
|
+
return unsupportedStrategy("Media not supported");
|
|
1438
|
+
}
|
|
1439
|
+
return unsupportedStrategy("Audio strategy not yet supported");
|
|
1440
|
+
}
|
|
1441
|
+
if (mainType === "video") {
|
|
1442
|
+
if (supports.indexOf("media") === -1) {
|
|
1443
|
+
return unsupportedStrategy("Media not supported");
|
|
1444
|
+
}
|
|
1445
|
+
return unsupportedStrategy("Video strategy not yet supported");
|
|
1446
|
+
}
|
|
1447
|
+
return unknownResponse;
|
|
1448
|
+
}, [canvas, paintables, vault, actions.makeChoice]);
|
|
1449
|
+
return useMemo(() => {
|
|
1450
|
+
if (strategy.type === "unknown") {
|
|
1451
|
+
return [strategy, emptyActions];
|
|
1452
|
+
}
|
|
1453
|
+
return [
|
|
1454
|
+
__spreadProps(__spreadValues({}, strategy), {
|
|
1455
|
+
annotations: { pages: enabledPages }
|
|
1456
|
+
}),
|
|
1457
|
+
actions
|
|
1458
|
+
];
|
|
1459
|
+
}, [strategy, enabledPages]);
|
|
1460
|
+
}
|
|
1461
|
+
|
|
1462
|
+
export { AnnotationContext, CanvasContext, CollectionContext, ContextBridge, ImageServiceLoaderContext, ManifestContext, RangeContext, ReactVaultContext, ResourceProvider, ResourceReactContext, SimpleViewerProvider, SimpleViewerReactContext, VaultProvider, VisibleCanvasReactContext, emptyActions, expandTarget, flattenAnnotationPageIds, getImageStrategy, getPaintables, parseSelector, parseSpecificResource, unknownResponse, unsupportedStrategy, useAnnotation, useAnnotationPageManager, useAnnotationsAtTime, useCanvas, useCanvasClock, useCollection, useContextBridge, useDispatch, useEventListener, useExistingVault, useExternalCollection, useExternalManifest, useExternalResource, useImageService, useImageServiceLoader, useImageTile, useLoadImageService, useManifest, usePaintables, usePaintingAnnotations, useRange, useRenderingStrategy, useResourceContext, useResourceEvents, useResources, useSearchService, useSimpleViewer, useStyleHelper, useStyles, useThumbnail, useVault, useVaultEffect, useVaultSelector, useVirtualAnnotationPage, useVisibleCanvases };
|