@prose-reader/core 1.220.0 → 1.222.0

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.
@@ -244,7 +244,7 @@
244
244
  overflow: hidden;
245
245
  max-width: 300px;
246
246
  width: 80%;
247
- `,e.appendChild(i),e.appendChild(n),e},ui=e=>t=>{const{loadingElementCreate:i=ci}=t,n=e(t),s=u=>o.of(u.reduce((l,{item:d,element:g})=>{g.style.zIndex="0";const p=g.querySelector(`.${Ht}`);if(p instanceof HTMLElement)return l[d.id]=p,l;const h=i({container:ai(g,n.context),item:d});return g.appendChild(h),l[d.id]=h,l},{})),r=u=>o.combineLatest([n.spine.spineLayout.layout$,n.theme.$.theme$]).pipe(b.map(([,l])=>({width:n.context.state.visibleAreaRect.width,theme:l})),b.distinctUntilChanged(M.isShallowEqual),b.tap(({width:l,theme:d})=>{Object.values(u).forEach(g=>{g.style.setProperty("max-width",`${l}px`),g.style.setProperty("color",d==="sepia"?"#939393":"rgb(202, 202, 202)")})})),a=u=>n.spineItemsObserver.itemIsReady$.pipe(b.tap(({item:l,isReady:d})=>{var g,p;(g=u[l.item.id])==null||g.style.setProperty("visibility",d?"hidden":"visible"),(p=u[l.item.id])==null||p.style.setProperty("z-index",d?"0":"1")})),c=n.spineItemsManager.items$.pipe(b.switchMap(u=>s(u)),b.shareReplay(1),b.takeUntil(n.context.destroy$));return c.pipe(b.switchMap(u=>o.merge(r(u),a(u))),b.takeUntil(n.$.destroy$)).subscribe(),{...n,loading:{$:{items$:c}}}};class li extends z{getImageElement(){const t=this.documentContainer;if(t instanceof HTMLImageElement)return t}onCreateDocument(){const t=this.containerElement.ownerDocument.createElement("img");return o.from(this.resourcesHandler.getResource()).pipe(o.switchMap(i=>{if(this.setDocumentContainer(t),t.style.objectFit="contain",t.style.userSelect="none",i instanceof URL)return o.of(i.href);if(i instanceof Response)return o.from(i.blob()).pipe(o.map(n=>URL.createObjectURL(n)));throw new Error("Invalid resource")}),o.map(i=>{const n=this.getImageElement();return n&&(n.src=i),t}))}onLoadDocument(){const t=this.getImageElement();if(!t)throw new Error("invalid element");return this.attach(),o.fromEvent(t,"load")}onUnload(){const t=this.getImageElement();return t&&URL.revokeObjectURL(t.src),this.detach(),o.EMPTY}onLayout({spreadPosition:t}){const i=this.getImageElement(),{height:n,width:s}=this.context.getPageSize();let r=n;const a=s;if(!i)return o.of(void 0);const c=i.naturalWidth||1,u=i.naturalHeight||1,l=c/u;return this.settings.values.computedPageTurnDirection==="vertical"&&this.settings.values.computedPageTurnMode==="scrollable"&&!this.context.state.isUsingSpreadMode&&(r=Math.ceil(s/l)),i.style.height=`${r}px`,i.style.width=`${a}px`,i.style.objectPosition=t==="left"?"right":t==="right"?"left":"center",o.of({width:a,height:r})}onRenderHeadless(){return o.EMPTY}getDocumentFrame(){}}const di=e=>t=>{const i=e({...t,getRenderer(a){var d;const c=(d=t.getRenderer)==null?void 0:d.call(t,a),u=a.mediaType??M.detectMimeTypeFromName(a.href),l=!!(u!=null&&u.startsWith("image/"));return!c&&l?g=>new li(g):c}}),n=new IntersectionObserver(a=>{a.forEach(c=>{var d;const u=c.target,l=Array.from(((d=u.contentDocument)==null?void 0:d.body.getElementsByTagName("audio"))||[]);c.isIntersecting?l.forEach(g=>{g.hasAttribute("autoplay")&&g.paused&&g.readyState>=2&&g.play().catch(console.error)}):l.forEach(g=>{g.pause(),g.currentTime=0})})},{threshold:.01}),s=new IntersectionObserver(a=>{a.forEach(c=>{if(c.target.tagName==="video"){const u=c.target;c.isIntersecting?u.hasAttribute("autoplay")&&u.paused&&u.readyState>=2&&u.play().catch(console.error):(u.pause(),u.currentTime=0)}})},{threshold:.5});return i.hookManager.register("item.onDocumentLoad",({destroy:a,itemId:c})=>{var g,p;const u=(g=i.spineItemsManager.get(c))==null?void 0:g.renderer.getDocumentFrame();if(!u)return;n.observe(u);const l=(p=u.contentDocument)==null?void 0:p.body.getElementsByTagName("video"),d=Array.from(l||[]).map(h=>(s.observe(h),()=>s.unobserve(h)));a(()=>{n.unobserve(u),d.forEach(h=>h())})}),{...i,destroy:()=>{n.disconnect(),s.disconnect(),i.destroy()}}},gi=(e,t)=>e.links$.pipe(o.tap(i=>{var a;if(!Ot(i.target,"a")||i.type!=="click")return;const n=new URL(i.target.href),s=`${n.origin}${n.pathname}`;((a=e.context.manifest)==null?void 0:a.spineItems.some(c=>c.href===s))&&t.goToUrl(n)})),U=A.namespace("navigation");class C{constructor(t){this.__symbol=Symbol("SpineItemPosition"),this.x=t.x,this.y=t.y}}const pi=({position:e,spineItem:t,pageHeight:i,pageWidth:n,spineItemLocator:s})=>{let r=new C({x:e.x-n,y:e.y});return t.isUsingVerticalWriting()&&(r=new C({x:e.x,y:e.y+i})),s.getSpineItemClosestPositionFromUnsafePosition(r,t)},Bt=({position:e,navigationResolver:t,computedPageTurnDirection:i,spineItemsManager:n,spineLocator:s,context:r})=>{const a=i,c=s.getSpineItemFromPosition(e)||n.get(0),u=e;if(!c)return u;const l=s.getSpineItemPositionFromSpinePosition(e,c),d=pi({position:l,spineItem:c,pageHeight:r.getPageSize().height,pageWidth:r.getPageSize().width,spineItemLocator:s.spineItemLocator});return t.arePositionsDifferent(d,l)?s.getSpinePositionFromSpineItemPosition({spineItemPosition:d,spineItem:c}):t.getAdjustedPositionWithSafeEdge(a==="horizontal"?new T({x:e.x-r.getPageSize().width,y:0}):new T({y:e.y-r.getPageSize().height,x:0}))},hi=({position:e,spineItem:t,context:i,navigationResolver:n,spineItemsManager:s,spineLocator:r,computedPageTurnDirection:a})=>{const c=Bt({position:e,context:i,navigationResolver:n,computedPageTurnDirection:a,spineItemsManager:s,spineLocator:r});if(t!=null&&t.isUsingVerticalWriting()&&e.x===c.x)return n.getAdjustedPositionForSpread(c);if(i.state.isUsingSpreadMode){if(t!=null&&t.isUsingVerticalWriting()&&e.x!==c.x)return n.getAdjustedPositionForSpread(n.getAdjustedPositionWithSafeEdge(i.isRTL()?{...c,x:c.x+i.getPageSize().width}:{...c,x:c.x-i.getPageSize().width}));if(a==="vertical"&&e.y!==c.y)return n.getAdjustedPositionForSpread(c);const u=Bt({position:c,context:i,navigationResolver:n,computedPageTurnDirection:a,spineItemsManager:s,spineLocator:r});return n.getAdjustedPositionForSpread(u)}return n.getAdjustedPositionForSpread(c)},mi=({position:e,spineItem:t,pageHeight:i,pageWidth:n,spineItemLocator:s})=>{let r=new C({x:e.x+n,y:e.y});return t.isUsingVerticalWriting()&&(r=new C({x:e.x,y:e.y-i})),s.getSpineItemClosestPositionFromUnsafePosition(r,t)},_t=({position:e,navigationResolver:t,computedPageTurnDirection:i,spineItemsManager:n,spineLocator:s,context:r})=>{const a=i,c=s.getSpineItemFromPosition(e)||n.get(0),u=e;if(!c)return u;const l=s.getSpineItemPositionFromSpinePosition(e,c),d=mi({position:l,spineItem:c,pageHeight:r.getPageSize().height,pageWidth:r.getPageSize().width,spineItemLocator:s.spineItemLocator});return t.arePositionsDifferent(d,l)?s.getSpinePositionFromSpineItemPosition({spineItemPosition:d,spineItem:c}):t.getAdjustedPositionWithSafeEdge(a==="horizontal"?new T({x:e.x+r.getPageSize().width,y:0}):new T({y:e.y+r.getPageSize().height,x:0}))},fi=({position:e,spineItem:t,context:i,navigationResolver:n,spineItemsManager:s,spineLocator:r,computedPageTurnDirection:a})=>{const c=_t({position:e,context:i,navigationResolver:n,computedPageTurnDirection:a,spineItemsManager:s,spineLocator:r});if(t!=null&&t.isUsingVerticalWriting()&&e.x===c.x)return n.getAdjustedPositionForSpread(c);if(i.state.isUsingSpreadMode){if(t!=null&&t.isUsingVerticalWriting()&&e.x!==c.x)return n.getAdjustedPositionForSpread(n.getAdjustedPositionWithSafeEdge(i.isRTL()?{...c,x:c.x-i.getPageSize().width}:{...c,x:c.x+i.getPageSize().width}));if(a==="vertical"&&e.y!==c.y)return n.getAdjustedPositionForSpread(c);const u=_t({position:c,context:i,navigationResolver:n,computedPageTurnDirection:a,spineItemsManager:s,spineLocator:r});return n.getAdjustedPositionForSpread(u)}return n.getAdjustedPositionForSpread(c)};class Si{constructor(t){this.reader=t,this.movingLastDelta={x:0,y:0},this.movingLastPosition=new T({x:0,y:0}),this.unlock=void 0}turnRight(){return this.turnRightOrBottom()}turnLeft(){return this.turnLeftOrTop()}turnTop(){return this.turnLeftOrTop()}turnBottom(){return this.turnRightOrBottom()}turnRightOrBottom(){const t=this.reader.navigation.getNavigation(),i=this.reader.spineItemsManager.get(t.spineItem);if(!i)return;const n=fi({context:this.reader.context,navigationResolver:this.reader.navigation.navigationResolver,position:t.position,computedPageTurnDirection:this.reader.settings.values.computedPageTurnDirection,spineItem:i,spineItemsManager:this.reader.spineItemsManager,spineLocator:this.reader.spine.locator});return this.reader.navigation.navigate({position:n})}turnLeftOrTop(){const t=this.reader.navigation.getNavigation(),i=this.reader.spineItemsManager.get(t.spineItem);if(!i)return;const n=hi({context:this.reader.context,navigationResolver:this.reader.navigation.navigationResolver,position:t.position,computedPageTurnDirection:this.reader.settings.values.computedPageTurnDirection,spineItem:i,spineItemsManager:this.reader.spineItemsManager,spineLocator:this.reader.spine.locator});return this.reader.navigation.navigate({position:n})}goToCfi(t,i={animate:!0}){return this.reader.navigation.navigate({animation:i.animate?"turn":!1,cfi:t})}goToSpineItem({indexOrId:t,...i}){const n=this.reader.spineItemsManager.get(t);if(n===void 0){U.warn("goToSpineItem",`Ignore navigation to ${t} since the item does not exist`);return}this.reader.navigation.navigate({spineItem:n.index,...i})}goToNextSpineItem(){const{endIndex:t=0}=this.reader.spine.locator.getVisibleSpineItemsFromPosition({position:this.reader.navigation.getNavigation().position,threshold:.5})||{};this.goToSpineItem({indexOrId:t+1})}goToPreviousSpineItem(){const{beginIndex:t=0}=this.reader.spine.locator.getVisibleSpineItemsFromPosition({position:this.reader.navigation.getNavigation().position,threshold:.5})??{};this.goToSpineItem({indexOrId:t-1})}goToUrl(t){this.reader.navigation.navigate({url:t,animation:!1})}goToRightSpineItem(){if(this.reader.settings.values.computedPageTurnDirection==="vertical"){U.warn("You cannot call this navigation method on vertical direction");return}return this.reader.context.isRTL()?this.goToPreviousSpineItem():this.goToNextSpineItem()}goToRightOrBottomSpineItem(){return this.reader.settings.values.computedPageTurnDirection==="vertical"?this.goToBottomSpineItem():this.goToRightSpineItem()}goToLeftOrTopSpineItem(){return this.reader.settings.values.computedPageTurnDirection==="vertical"?this.goToTopSpineItem():this.goToLeftSpineItem()}goToLeftSpineItem(){if(this.reader.settings.values.computedPageTurnDirection==="vertical"){U.warn("You cannot call this navigation method on vertical direction");return}return this.reader.context.isRTL()?this.goToNextSpineItem():this.goToPreviousSpineItem()}goToTopSpineItem(){if(this.reader.settings.values.computedPageTurnDirection==="horizontal"){U.warn("You cannot call this navigation method on horizontal direction");return}return this.goToPreviousSpineItem()}goToBottomSpineItem(){if(this.reader.settings.values.computedPageTurnDirection==="horizontal"){U.warn("You cannot call this navigation method on horizontal direction");return}return this.goToNextSpineItem()}goToPageOfSpineItem({pageIndex:t,spineItemId:i,...n}){const s=this.reader.navigation.navigationResolver.getNavigationForSpineItemPage({pageIndex:t,spineItemId:i});U.debug(".goToPageOfSpineItem()",{pageIndex:t,spineItemId:i,...n,position:s}),this.reader.navigation.navigate({position:s,...n})}goToAbsolutePageIndex({absolutePageIndex:t,...i}){const n=this.reader.spine.locator.getSpineInfoFromAbsolutePageIndex({absolutePageIndex:t});if(U.debug(".goToAbsolutePageIndex()",{absolutePageIndex:t,...i,foundInfo:n}),n){const s=this.reader.navigation.navigationResolver.getNavigationForSpineItemPage({pageIndex:n.pageIndex,spineItemId:n.itemIndex});return this.reader.navigation.navigate({position:s,...i})}}}class bi{constructor(t){this.reader=t,this.lastDelta={x:0,y:0},this.lastPosition=new T({x:0,y:0}),this.lastStartPosition=new T({x:0,y:0}),this.unlock=void 0}moveTo(t,{final:i,start:n}={}){var a,c,u,l;if(this.reader.settings.values.computedPageTurnMode==="scrollable"){U.warn("pan control is not available on free page turn mode");return}const s=this.reader.settings.values.computedPageTurnDirection;n&&((a=this.unlock)==null||a.call(this),this.unlock=this.reader.navigation.lock(),this.lastDelta={x:0,y:0},this.lastStartPosition=this.reader.navigation.controlledNavigationController.viewportPosition,this.lastPosition=this.lastStartPosition);let r=this.reader.navigation.getNavigation().position;if(t){const d=this.reader.viewport.absoluteViewport.width/this.reader.viewport.relativeViewport.width,g=Math.floor(t.x)-(((c=this.lastDelta)==null?void 0:c.x)||0),p=Math.floor(t.y)-(((u=this.lastDelta)==null?void 0:u.y)||0),h=Math.floor(s==="horizontal"?this.lastPosition.x-g/d:0),m=Math.floor(s==="horizontal"?0:this.lastPosition.y-p/d);r=new T({x:h,y:m}),this.lastDelta=t}else r=this.lastPosition;if(this.lastPosition=r,i){this.lastDelta={x:0,y:0},this.reader.navigation.navigate({position:r,animation:!1}),(l=this.unlock)==null||l.call(this);return}this.reader.navigation.navigate({position:r,animation:!1})}}const yi=e=>e.pagination.state$.pipe(o.withLatestFrom(e.context.manifest$,e.settings.values$),o.map(([t,{spineItems:i,readingDirection:n},{computedPageTurnDirection:s}])=>{const r=i.length??0,a=t.beginSpineItemIndex===0,c=t.endSpineItemIndex===Math.max(r-1,0),u=t.endSpineItemIndex===Math.max(r-1,0),l=t.beginSpineItemIndex===0,d=t.beginPageIndexInSpineItem===0,g=t.endPageIndexInSpineItem===t.endNumberOfPagesInSpineItem-1;return{canTurnLeft:s==="vertical"?!1:!d,canTurnRight:s==="vertical"?!1:!g,canGoTopSpineItem:s==="vertical"&&!a,canGoBottomSpineItem:s==="vertical"&&!c,canGoLeftSpineItem:s!=="vertical"&&(n==="ltr"&&!a||n==="rtl"&&!u),canGoRightSpineItem:s!=="vertical"&&(n==="ltr"&&!c||n==="rtl"&&!l)}}),o.distinctUntilChanged(M.isShallowEqual)),wi=({reader:e,duration:t})=>i=>{let n;const s=()=>{n==null||n(),n=void 0};return i.pipe(o.tap(()=>{n||(n=e==null?void 0:e.navigation.lock())}),o.throttleTime(t,o.animationFrameScheduler,{trailing:!0,leading:!0}),o.tap(s),o.finalize(s))},Ii=e=>t=>{const i=e(t),n=yi(i),s=new Si(i),r=new bi(i),a=u=>{const{cfi:l,...d}=u;i.load(d),l&&s.goToCfi(l,{animate:!1})};return gi(i,s).pipe(o.takeUntil(i.$.destroy$)).subscribe(),{...i,load:a,navigation:{...i.navigation,state$:n,throttleLock:({duration:u,trigger:l})=>l.pipe(wi({duration:u,reader:i})),moveTo:r.moveTo.bind(r),turnBottom:s.turnBottom.bind(s),turnTop:s.turnTop.bind(s),turnLeftOrTop:s.turnLeftOrTop.bind(s),turnRightOrBottom:s.turnRightOrBottom.bind(s),turnLeft:s.turnLeft.bind(s),turnRight:s.turnRight.bind(s),goToCfi:s.goToCfi.bind(s),goToUrl:s.goToUrl.bind(s),goToSpineItem:s.goToSpineItem.bind(s),goToNextSpineItem:s.goToNextSpineItem.bind(s),goToPreviousSpineItem:s.goToPreviousSpineItem.bind(s),goToLeftOrTopSpineItem:s.goToLeftOrTopSpineItem.bind(s),goToRightOrBottomSpineItem:s.goToRightOrBottomSpineItem.bind(s),goToTopSpineItem:s.goToTopSpineItem.bind(s),goToBottomSpineItem:s.goToBottomSpineItem.bind(s),goToLeftSpineItem:s.goToLeftSpineItem.bind(s),goToRightSpineItem:s.goToRightSpineItem.bind(s),goToPageOfSpineItem:s.goToPageOfSpineItem.bind(s),goToAbsolutePageIndex:s.goToAbsolutePageIndex.bind(s)}}},q=e=>E.generate({spineIndex:e.index,spineId:e.id}),x=(e,t,i)=>{var n;return!e.ownerDocument||!((n=e.ownerDocument)!=null&&n.documentElement)||e===e.ownerDocument?q(i):E.generate({node:e,offset:t,spineIndex:i.index,spineId:i.id})},ht=({pageIndex:e,spineItem:t,spineItemLocator:i})=>{var r;const n=i.getFirstNodeOrRangeAtPage(e,t),s=t.renderer.getDocumentFrame();return n&&s instanceof HTMLIFrameElement&&((r=s.contentWindow)!=null&&r.document)?x(n.node,n.offset,t.item).trim():q(t.item)},Pi=(e,t)=>{const i=x(e.startContainer,e.startOffset,t),n=x(e.endContainer,e.endOffset,t);return{start:i,end:n}},vi=({item:e,selection:t})=>{const i=t.anchorNode?x(t.anchorNode,t.anchorOffset,e):void 0,n=t.focusNode?x(t.focusNode,t.focusOffset,e):void 0;return{anchorCfi:i,focusCfi:n}},qt=e=>{var t;return((t=e[0])==null?void 0:t.index)===6&&e.length>1},mt=e=>{const t=E.parse(e);return E.isIndirectionOnly(t)},$i=e=>Array.isArray(e)?e[0]&&qt(e[0])?e[0]:void 0:e.parent[0]&&qt(e.parent[0])?e.parent[0]:void 0,ft=e=>{var c,u,l,d;const t=E.parse(e),n=($i(t)??[])[1],r=((n==null?void 0:n.index)??2)/2-1,a=E.isParsedCfiRange(t)?(u=(c=t.end.at(-1))==null?void 0:c.at(-1))==null?void 0:u.offset:(d=(l=t.at(-1))==null?void 0:l.at(-1))==null?void 0:d.offset;return{cleanedCfi:e,itemIndex:r,offset:a??0}},xt=({cfi:e,spineItemsManager:t})=>{var r,a;if(!e)return;const{itemIndex:i}=ft(e),n=t.get(i);if(!n)return;const s=n.renderer.getDocumentFrame();if(s instanceof HTMLIFrameElement){console.log("FIII",e);const c=(r=s.contentWindow)==null?void 0:r.document;if(c)try{const u=E.resolve(e,c,{throwOnError:!0});return{node:u.isRange?(a=u.node)==null?void 0:a.startContainer:u.node,offset:Array.isArray(u.offset)?u.offset.at(-1):u.offset,spineItem:n}}catch(u){return A.error(u),{spineItem:n}}}return{spineItem:n}},Mi=(e,t)=>{if("cfi"in t)return t;const i=e.spineItemsManager.get(t);if(!i)throw new Error("Spine item not found");return{cfi:q(i.item)}},Ti=(e,t)=>{let i=e==null?void 0:e.itemPageIndex;const{itemIndex:n}=t.cfi.parseCfi(e.cfi),s=t.spineItemsManager.get(n);return s?lt().pipe(o.withLatestFrom(s.isReady$),o.map(([,r])=>{var g;let a;const{node:c,offset:u}=t.cfi.resolveCfi({cfi:e.cfi})??{};if(s.renditionLayout!=="pre-paginated"&&c&&(i=t.spine.locator.spineItemLocator.getSpineItemPageIndexFromNode(c,u??0,s)??i),c&&e.endCfi){const{node:p,offset:h}=t.cfi.resolveCfi({cfi:e.cfi})??{};p&&r&&(a=(g=c==null?void 0:c.ownerDocument)==null?void 0:g.createRange(),a==null||a.setStart(c,u??0),a==null||a.setEnd(p,h??0))}let d=e==null?void 0:e.absolutePageIndex;return i===void 0&&(i=0),d=t.spine.locator.getAbsolutePageIndexFromPageIndex({pageIndex:i,spineItemOrId:s})??(e==null?void 0:e.absolutePageIndex),{...e,range:a,itemIndex:n,startNode:c??void 0,startOffset:u,absolutePageIndex:d,itemPageIndex:i}})):o.of({...e,itemIndex:n})};class Fi{constructor(t){this.reader=t,this.locate=(i,n)=>{const s={resource:i,meta:Mi(this.reader,i)};return dt(()=>{var d;const r=this.reader.spine.spineLayout.layout$.pipe(o.debounceTime(10),o.startWith(s),o.switchScan(g=>Ti(g.meta,this.reader).pipe(o.map(p=>({...g,meta:p}))),s)),a=(d=this.reader.cfi.parseCfi(s.meta.cfi))==null?void 0:d.itemIndex,c=this.reader.spineItemsManager.get(a),u=(c==null?void 0:c.renditionLayout)==="reflowable",l=n.mode==="shallow"||!u||!c?()=>{}:this.reader.spine.spineItemsLoader.forceOpen([c.index]);return r.pipe(o.finalize(()=>{setTimeout(()=>{l()},1e3)}))})}}locateResource(t,i){return Array.isArray(t)?dt(()=>o.combineLatest(t.map(n=>this.locate(n,i??{})))):this.locate(t,i??{})}}const Yt=(e,t,i)=>{const n=i.spineItems.findIndex(s=>s.href===e);return t.reduce((s,r)=>{const a=r.href.indexOf("#"),c=a>0?r.href.substr(0,a):r.href,u=c.substring(0,c.lastIndexOf("/")),l=e.substring(0,e.lastIndexOf("/")),d=e.endsWith(c),g=l!==""&&l.endsWith(u);if(d||g){const h=i.spineItems.findIndex(S=>S.href===r.href);if(n<h)return s;const f={title:r.title,path:r.path},w=Yt(e,r.contents,i);return w?{...f,subChapter:w}:f}return s},void 0)},Li=(e,t)=>{var n;const{href:i}=t;return Yt(i,((n=e.nav)==null?void 0:n.toc)??[],e)},Ci=e=>{const t=e.context.manifest,i=e.spineItemsManager.items;return t?i.reduce((n,{item:s})=>(n[s.id]=Li(t,s),n),{}):{}},Ri=e=>e.spineItemsManager.items$.pipe(o.startWith([]),o.map(()=>Ci(e))),Ai=(e,t,i)=>e+t*i,Oi=(e,t,i)=>{const n=e.context,{height:s,width:r}=i.layout.layoutInfo,{top:a,left:c}=e.spine.spineLayout.getSpineItemSpineLayoutInfo(i);return e.settings.values.computedPageTurnDirection==="vertical"?Math.max(0,Math.min(1,(t.y-a+n.state.visibleAreaRect.height)/s)):Math.max(0,Math.min(1,(t.x-c+n.state.visibleAreaRect.width)/r))},Di=(e,t,i,n,s)=>s.isReady$.pipe(o.first(),o.withLatestFrom(e.layoutInfo$),o.map(([r,a])=>{var S,y,I,v,$,F,L;const c=e.context,u=((S=c.manifest)==null?void 0:S.renditionLayout)==="pre-paginated",l=((y=c.manifest)==null?void 0:y.spineItems.length)||0,d=((I=c.manifest)==null?void 0:I.spineItems.slice(0,t).reduce((R,D)=>R+(D.progressionWeight??0),0))||0,g=e.spineItemsManager.getSpineItemIndex(s)??0,p=((v=e.context.manifest)==null?void 0:v.spineItems.length)??0,h=a.pages.filter(R=>R.itemIndex===g).length??0,m=((F=($=c.manifest)==null?void 0:$.spineItems[t])==null?void 0:F.progressionWeight)??(g+1)/p;let f=(i+1)*(m/h);!u&&s.renditionLayout==="reflowable"&&!r&&(f=0);let w=d+f;return((L=c.manifest)==null?void 0:L.renditionFlow)==="scrolled-continuous"&&(r?f=Oi(e,n,s):f=0,w=Ai(d,m,f)),t===l-1&&i===h-1&&w>.99?1:w})),Ni=e=>e.spine.spineLayout.layout$.pipe(o.debounceTime(10,o.animationFrameScheduler),o.withLatestFrom(e.pagination.state$),o.map(()=>({numberOfPagesPerItems:e.spineItemsManager.items.reduce((i,n)=>[...i,n.numberOfPages],[]),numberOfTotalPages:e.spine.spineLayout.numberOfPages})),o.distinctUntilChanged(M.isShallowEqual),o.startWith({numberOfPagesPerItems:[],numberOfTotalPages:0})),Vi=(e,t,i,n)=>{const s=e.context,r=t.beginSpineItemIndex!==void 0?e.spineItemsManager.get(t.beginSpineItemIndex):void 0,a=t.endSpineItemIndex!==void 0?e.spineItemsManager.get(t.endSpineItemIndex):void 0;return o.of({...t,beginChapterInfo:r?i[r.item.id]:void 0,beginSpineItemReadingDirection:r==null?void 0:r.readingDirection,endChapterInfo:a?i[a.item.id]:void 0,endSpineItemReadingDirection:a==null?void 0:a.readingDirection,percentageEstimateOfBook:n,isUsingSpread:s.state.isUsingSpreadMode??!1})},ki=e=>o.combineLatest([e.pagination.state$,e.layout$]).pipe(o.switchMap(([t])=>{const i=t.endSpineItemIndex!==void 0?e.spineItemsManager.get(t.endSpineItemIndex):void 0;return i?Di(e,t.endSpineItemIndex??0,t.endPageIndexInSpineItem||0,e.navigation.getNavigation().position,i):o.of(0)})),Ui=e=>{const t=Ri(e),i=Ni(e),n=new o.BehaviorSubject({...e.pagination.state,beginChapterInfo:void 0,beginCfi:void 0,beginPageIndexInSpineItem:void 0,isUsingSpread:!1,beginAbsolutePageIndex:0,endAbsolutePageIndex:0,numberOfTotalPages:0,beginSpineItemReadingDirection:void 0,beginSpineItemIndex:void 0,endCfi:void 0,endChapterInfo:void 0,endSpineItemReadingDirection:void 0,percentageEstimateOfBook:0}),s=o.combineLatest([e.pagination.state$,t,ki(e)]).pipe(o.switchMap(([a,c,u])=>Vi(e,a,c,u).pipe(o.map(l=>({...a,...l})))),o.distinctUntilChanged(M.isShallowEqual));return{paginationInfo$:o.combineLatest([s,i]).pipe(o.map(([a,c])=>({...a,...c,beginAbsolutePageIndex:c.numberOfPagesPerItems.slice(0,a.beginSpineItemIndex).reduce((u,l)=>u+l,a.beginPageIndexInSpineItem??0),endAbsolutePageIndex:c.numberOfPagesPerItems.slice(0,a.endSpineItemIndex).reduce((u,l)=>u+l,a.endPageIndexInSpineItem??0)})),o.tap(a=>{n.next(a)}),o.shareReplay(1)),getPaginationInfo:()=>n.value}},Ei=e=>t=>{const i=e(t),{paginationInfo$:n,getPaginationInfo:s}=Ui(i);n.pipe(o.takeUntil(i.$.destroy$)).subscribe();const r=new Fi(i);return{...i,locateResource:r.locateResource.bind(r),pagination:{...i.pagination,get state(){return s()},get state$(){return n}}}},Wi=e=>({put:(r,a)=>new Promise((c,u)=>{const l=e.transaction(["store"],"readwrite");l.onerror=p=>{u(p)},l.oncomplete=()=>{c()};const g=l.objectStore("store").put(a,r);g.onsuccess=()=>{c()},g.onerror=p=>{u(p)}}),keys:()=>new Promise((r,a)=>{const c=e.transaction(["store"],"readonly");c.onerror=g=>{a(g)};const l=c.objectStore("store").openKeyCursor(),d=[];l.onsuccess=()=>{const g=l.result;if(!g){r(d);return}d.push(g.key),g.continue()},l.onerror=()=>{a(l.error)}}),get:r=>new Promise((a,c)=>{const u=e.transaction(["store"],"readwrite"),d=u.objectStore("store").get(r);d.onsuccess=()=>{let g=d.result;g===void 0&&(g=null),a(g)},u.onerror=()=>{c(d.error)}}),remove:r=>new Promise((a,c)=>{const u=e.transaction(["store"],"readwrite"),d=u.objectStore("store").delete(r);u.onerror=()=>{c(d.error)},u.oncomplete=()=>{a()},u.onabort=()=>{var p;const g=d.error?d.error:(p=d.transaction)==null?void 0:p.error;c(g)}})}),St=async e=>new Promise((t,i)=>{const n=window.indexedDB.open(e);n.onerror=s=>{i(s)},n.onsuccess=()=>{t(Wi(n.result))},n.onupgradeneeded=()=>{n.result.createObjectStore("store")}}),zi=e=>{let t=Date.now().toString();const i=new o.Subject,n=u=>{var l,d;if(typeof u=="string"||typeof u=="object"){const g=typeof u=="object"?u.id:u;return(l=e.manifest)==null?void 0:l.spineItems.find(p=>p.id===g)}return(d=e.manifest)==null?void 0:d.spineItems[u]},s=async(u,l)=>{const d=n(u);if(!d)return new Response("Item not found",{status:404});const p=await(await St("prose-reader")).get(`${t}_${d.id}`);if(p)return new Response(p,{status:200});const h=l&&await l(d)||await fetch(d.href);return r(d,h.clone()),h},r=(u,l)=>{i.next({id:u,data:l})};i.asObservable().pipe(b.mergeMap(({id:u,data:l})=>{const d=n(u);return d?o.from(o.forkJoin([St("prose-reader"),o.from(l.blob())])).pipe(b.switchMap(([g,p])=>o.from(g.put(`${t}_${d.id}`,p))),b.catchError(g=>(A.error(g),o.EMPTY))):o.EMPTY}),b.takeUntil(e.destroy$)).subscribe();const a=e.manifest$.pipe(b.tap(()=>{t=Date.now().toString()}));return o.merge(a).pipe(b.switchMap(()=>(A.debug("Cleanup up old cache..."),o.from(St("prose-reader")).pipe(b.switchMap(u=>o.from(u.keys()).pipe(b.map(l=>l.filter(d=>!d.toString().startsWith(t))),b.switchMap(l=>{const d=l.map(g=>u.remove(g));return o.from(Promise.all(d))}))),b.catchError(u=>(A.error(u),o.EMPTY))))),b.takeUntil(e.destroy$)).subscribe(),{get:s,destroy:()=>{i.complete()}}},Hi=e=>t=>{const i=e(t),n=zi(i.context);return{...i,destroy:()=>{n.destroy(),i.destroy()}}},Bi=(e,t)=>{var s;const i=(s=e.node.ownerDocument)==null?void 0:s.createRange(),n=e.node.compareDocumentPosition(t.node);if(i){try{if(n&Node.DOCUMENT_POSITION_PRECEDING)i.setStart(t.node,t.offset||0),i.setEnd(e.node,e.offset||0);else if(n&Node.DOCUMENT_POSITION_FOLLOWING)i.setStart(e.node,e.offset||0),i.setEnd(t.node,t.offset||0);else{const r=Math.min(e.offset||0,t.offset||0),a=Math.max(e.offset||0,t.offset||0);i.setStart(e.node,r),i.setEnd(e.node,a)}}catch(r){A.warn("Failed to create range from selection",r,{anchor:e,focus:t})}return i}},_i=({selection:e,spineItem:t})=>{const{anchorNode:i,anchorOffset:n,focusNode:s,focusOffset:r}=e;if(!(!i||!s))try{return Bi({node:i,offset:n},{node:s,offset:r})}catch(a){A.warn("Failed to create range from selection",a,{selection:e,spineItem:t});return}};class qi extends O{constructor(t){var n;super();const i=t.contentDocument||((n=t.contentWindow)==null?void 0:n.document);if(!i)this.selectionChange$=o.NEVER,this.selectionOver$=o.NEVER;else{const s=gt(i.body,{childList:!0,subtree:!0}).pipe(o.filter(a=>!!a.find(c=>c.type==="childList"&&c.removedNodes.length))),r=t.parentElement?gt(t.parentElement,{childList:!0}).pipe(o.filter(a=>!!a.find(c=>Array.from(c.removedNodes).includes(t)))):o.of(null);this.selectionChange$=o.merge(o.fromEvent(i,"selectionchange"),s).pipe(o.map(()=>i.getSelection()),o.takeUntil(o.merge(r,this.destroy$)),o.endWith(null)),this.selectionOver$=o.fromEvent(i,"pointerdown").pipe(o.switchMap(()=>o.merge(o.fromEvent(i,"pointerup"),o.fromEvent(i,"pointercancel"),o.fromEvent(i,"contextmenu")).pipe(o.first(),o.delay(0),o.map(a=>{const c=i.getSelection();return c&&!c.isCollapsed?[a,c]:void 0}),o.filter(W))),o.takeUntil(o.merge(r,this.destroy$)))}}}const xi=e=>e.loaded$.pipe(o.switchMap(()=>{var s;const t=e.renderer.getDocumentFrame(),i=(t==null?void 0:t.contentDocument)||((s=t==null?void 0:t.contentWindow)==null?void 0:s.document);if(!t||!i)return o.NEVER;const n=new qi(t);return o.merge(n.selectionChange$.pipe(o.map(r=>{if(r!=null&&r.toString())return{type:"change",selection:r}})),n.selectionOver$.pipe(o.map(([r,a])=>({type:"over",event:r,selection:a})))).pipe(o.takeUntil(e.unloaded$),o.endWith(void 0),o.finalize(()=>{n.destroy()}))}),o.distinctUntilChanged()),Yi=e=>t=>{const i=e(t);let n;const s=i.spineItemsManager.items$.pipe(o.switchMap(d=>{const g=d.map(p=>{const h=i.spineItemsManager.getSpineItemIndex(p)??0;return xi(p).pipe(o.map(m=>{if(m)return{...m,itemIndex:h}}))});return o.merge(...g)}),o.startWith(void 0),o.distinctUntilChanged(),o.tap(d=>{n=d}),o.shareReplay({refCount:!0,bufferSize:1})),r=s,a=s.pipe(o.map(d=>!!d),o.distinctUntilChanged(),o.filter(d=>d),o.share()),c=a.pipe(o.switchMap(()=>r),o.distinctUntilChanged(),o.filter(d=>!d),o.share()),u=s.pipe(o.filter(d=>(d==null?void 0:d.type)==="over"),o.share()),l=i.context.containerElement$.pipe(o.switchMap(d=>o.fromEvent(d,"pointerdown")),o.withLatestFrom(r),o.map(([,d])=>d),o.startWith(void 0),o.shareReplay({refCount:!0,bufferSize:1}));return o.merge(r,l).pipe(o.takeUntil(i.$.destroy$)).subscribe(),{...i,selection:{selection$:r,selectionStart$:a,selectionEnd$:c,selectionOver$:u,lastSelectionOnPointerdown$:l,getSelection:()=>n,createOrderedRangeFromSelection:_i}}},Xt=[{name:"bright",backgroundColor:"white"},{name:"sepia",backgroundColor:"#eaddc7",foregroundColor:"black"},{name:"night",backgroundColor:"#191717",foregroundColor:"#f1ebeb"}],Xi=e=>t=>{const i=e(t),n=new o.BehaviorSubject(t.theme??"bright"),s=()=>{const c=Xt.find(u=>u.name===n.value);return`
247
+ `,e.appendChild(i),e.appendChild(n),e},ui=e=>t=>{const{loadingElementCreate:i=ci}=t,n=e(t),s=u=>o.of(u.reduce((l,{item:d,element:g})=>{g.style.zIndex="0";const p=g.querySelector(`.${Ht}`);if(p instanceof HTMLElement)return l[d.id]=p,l;const h=i({container:ai(g,n.context),item:d});return g.appendChild(h),l[d.id]=h,l},{})),r=u=>o.combineLatest([n.spine.spineLayout.layout$,n.theme.$.theme$]).pipe(b.map(([,l])=>({width:n.context.state.visibleAreaRect.width,theme:l})),b.distinctUntilChanged(M.isShallowEqual),b.tap(({width:l,theme:d})=>{Object.values(u).forEach(g=>{g.style.setProperty("max-width",`${l}px`),g.style.setProperty("color",d==="sepia"?"#939393":"rgb(202, 202, 202)")})})),a=u=>n.spineItemsObserver.itemIsReady$.pipe(b.tap(({item:l,isReady:d})=>{var g,p;(g=u[l.item.id])==null||g.style.setProperty("visibility",d?"hidden":"visible"),(p=u[l.item.id])==null||p.style.setProperty("z-index",d?"0":"1")})),c=n.spineItemsManager.items$.pipe(b.switchMap(u=>s(u)),b.shareReplay(1),b.takeUntil(n.context.destroy$));return c.pipe(b.switchMap(u=>o.merge(r(u),a(u))),b.takeUntil(n.$.destroy$)).subscribe(),{...n,loading:{$:{items$:c}}}};class li extends z{getImageElement(){const t=this.documentContainer;if(t instanceof HTMLImageElement)return t}onCreateDocument(){const t=this.containerElement.ownerDocument.createElement("img");return o.from(this.resourcesHandler.getResource()).pipe(o.switchMap(i=>{if(this.setDocumentContainer(t),t.style.objectFit="contain",t.style.userSelect="none",i instanceof URL)return o.of(i.href);if(i instanceof Response)return o.from(i.blob()).pipe(o.map(n=>URL.createObjectURL(n)));throw new Error("Invalid resource")}),o.map(i=>{const n=this.getImageElement();return n&&(n.src=i),t}))}onLoadDocument(){const t=this.getImageElement();if(!t)throw new Error("invalid element");return this.attach(),o.fromEvent(t,"load")}onUnload(){const t=this.getImageElement();return t&&URL.revokeObjectURL(t.src),this.detach(),o.EMPTY}onLayout({spreadPosition:t}){const i=this.getImageElement(),{height:n,width:s}=this.context.getPageSize();let r=n;const a=s;if(!i)return o.of(void 0);const c=i.naturalWidth||1,u=i.naturalHeight||1,l=c/u;return this.settings.values.computedPageTurnDirection==="vertical"&&this.settings.values.computedPageTurnMode==="scrollable"&&!this.context.state.isUsingSpreadMode&&(r=Math.ceil(s/l)),i.style.height=`${r}px`,i.style.width=`${a}px`,i.style.objectPosition=t==="left"?"right":t==="right"?"left":"center",o.of({width:a,height:r})}onRenderHeadless(){return o.EMPTY}getDocumentFrame(){}}const di=e=>t=>{const i=e({...t,getRenderer(a){var d;const c=(d=t.getRenderer)==null?void 0:d.call(t,a),u=a.mediaType??M.detectMimeTypeFromName(a.href),l=!!(u!=null&&u.startsWith("image/"));return!c&&l?g=>new li(g):c}}),n=new IntersectionObserver(a=>{a.forEach(c=>{var d;const u=c.target,l=Array.from(((d=u.contentDocument)==null?void 0:d.body.getElementsByTagName("audio"))||[]);c.isIntersecting?l.forEach(g=>{g.hasAttribute("autoplay")&&g.paused&&g.readyState>=2&&g.play().catch(console.error)}):l.forEach(g=>{g.pause(),g.currentTime=0})})},{threshold:.01}),s=new IntersectionObserver(a=>{a.forEach(c=>{if(c.target.tagName==="video"){const u=c.target;c.isIntersecting?u.hasAttribute("autoplay")&&u.paused&&u.readyState>=2&&u.play().catch(console.error):(u.pause(),u.currentTime=0)}})},{threshold:.5});return i.hookManager.register("item.onDocumentLoad",({destroy:a,itemId:c})=>{var g,p;const u=(g=i.spineItemsManager.get(c))==null?void 0:g.renderer.getDocumentFrame();if(!u)return;n.observe(u);const l=(p=u.contentDocument)==null?void 0:p.body.getElementsByTagName("video"),d=Array.from(l||[]).map(h=>(s.observe(h),()=>s.unobserve(h)));a(()=>{n.unobserve(u),d.forEach(h=>h())})}),{...i,destroy:()=>{n.disconnect(),s.disconnect(),i.destroy()}}},gi=(e,t)=>e.links$.pipe(o.tap(i=>{var a;if(!Ot(i.target,"a")||i.type!=="click")return;const n=new URL(i.target.href),s=`${n.origin}${n.pathname}`;((a=e.context.manifest)==null?void 0:a.spineItems.some(c=>c.href===s))&&t.goToUrl(n)})),U=A.namespace("navigation");class C{constructor(t){this.__symbol=Symbol("SpineItemPosition"),this.x=t.x,this.y=t.y}}const pi=({position:e,spineItem:t,pageHeight:i,pageWidth:n,spineItemLocator:s})=>{let r=new C({x:e.x-n,y:e.y});return t.isUsingVerticalWriting()&&(r=new C({x:e.x,y:e.y+i})),s.getSpineItemClosestPositionFromUnsafePosition(r,t)},Bt=({position:e,navigationResolver:t,computedPageTurnDirection:i,spineItemsManager:n,spineLocator:s,context:r})=>{const a=i,c=s.getSpineItemFromPosition(e)||n.get(0),u=e;if(!c)return u;const l=s.getSpineItemPositionFromSpinePosition(e,c),d=pi({position:l,spineItem:c,pageHeight:r.getPageSize().height,pageWidth:r.getPageSize().width,spineItemLocator:s.spineItemLocator});return t.arePositionsDifferent(d,l)?s.getSpinePositionFromSpineItemPosition({spineItemPosition:d,spineItem:c}):t.getAdjustedPositionWithSafeEdge(a==="horizontal"?new T({x:e.x-r.getPageSize().width,y:0}):new T({y:e.y-r.getPageSize().height,x:0}))},hi=({position:e,spineItem:t,context:i,navigationResolver:n,spineItemsManager:s,spineLocator:r,computedPageTurnDirection:a})=>{const c=Bt({position:e,context:i,navigationResolver:n,computedPageTurnDirection:a,spineItemsManager:s,spineLocator:r});if(t!=null&&t.isUsingVerticalWriting()&&e.x===c.x)return n.getAdjustedPositionForSpread(c);if(i.state.isUsingSpreadMode){if(t!=null&&t.isUsingVerticalWriting()&&e.x!==c.x)return n.getAdjustedPositionForSpread(n.getAdjustedPositionWithSafeEdge(i.isRTL()?{...c,x:c.x+i.getPageSize().width}:{...c,x:c.x-i.getPageSize().width}));if(a==="vertical"&&e.y!==c.y)return n.getAdjustedPositionForSpread(c);const u=Bt({position:c,context:i,navigationResolver:n,computedPageTurnDirection:a,spineItemsManager:s,spineLocator:r});return n.getAdjustedPositionForSpread(u)}return n.getAdjustedPositionForSpread(c)},mi=({position:e,spineItem:t,pageHeight:i,pageWidth:n,spineItemLocator:s})=>{let r=new C({x:e.x+n,y:e.y});return t.isUsingVerticalWriting()&&(r=new C({x:e.x,y:e.y-i})),s.getSpineItemClosestPositionFromUnsafePosition(r,t)},_t=({position:e,navigationResolver:t,computedPageTurnDirection:i,spineItemsManager:n,spineLocator:s,context:r})=>{const a=i,c=s.getSpineItemFromPosition(e)||n.get(0),u=e;if(!c)return u;const l=s.getSpineItemPositionFromSpinePosition(e,c),d=mi({position:l,spineItem:c,pageHeight:r.getPageSize().height,pageWidth:r.getPageSize().width,spineItemLocator:s.spineItemLocator});return t.arePositionsDifferent(d,l)?s.getSpinePositionFromSpineItemPosition({spineItemPosition:d,spineItem:c}):t.getAdjustedPositionWithSafeEdge(a==="horizontal"?new T({x:e.x+r.getPageSize().width,y:0}):new T({y:e.y+r.getPageSize().height,x:0}))},fi=({position:e,spineItem:t,context:i,navigationResolver:n,spineItemsManager:s,spineLocator:r,computedPageTurnDirection:a})=>{const c=_t({position:e,context:i,navigationResolver:n,computedPageTurnDirection:a,spineItemsManager:s,spineLocator:r});if(t!=null&&t.isUsingVerticalWriting()&&e.x===c.x)return n.getAdjustedPositionForSpread(c);if(i.state.isUsingSpreadMode){if(t!=null&&t.isUsingVerticalWriting()&&e.x!==c.x)return n.getAdjustedPositionForSpread(n.getAdjustedPositionWithSafeEdge(i.isRTL()?{...c,x:c.x-i.getPageSize().width}:{...c,x:c.x+i.getPageSize().width}));if(a==="vertical"&&e.y!==c.y)return n.getAdjustedPositionForSpread(c);const u=_t({position:c,context:i,navigationResolver:n,computedPageTurnDirection:a,spineItemsManager:s,spineLocator:r});return n.getAdjustedPositionForSpread(u)}return n.getAdjustedPositionForSpread(c)};class Si{constructor(t){this.reader=t,this.movingLastDelta={x:0,y:0},this.movingLastPosition=new T({x:0,y:0}),this.unlock=void 0}turnRight(){return this.turnRightOrBottom()}turnLeft(){return this.turnLeftOrTop()}turnTop(){return this.turnLeftOrTop()}turnBottom(){return this.turnRightOrBottom()}turnRightOrBottom(){const t=this.reader.navigation.getNavigation(),i=this.reader.spineItemsManager.get(t.spineItem);if(!i)return;const n=fi({context:this.reader.context,navigationResolver:this.reader.navigation.navigationResolver,position:t.position,computedPageTurnDirection:this.reader.settings.values.computedPageTurnDirection,spineItem:i,spineItemsManager:this.reader.spineItemsManager,spineLocator:this.reader.spine.locator});return this.reader.navigation.navigate({position:n})}turnLeftOrTop(){const t=this.reader.navigation.getNavigation(),i=this.reader.spineItemsManager.get(t.spineItem);if(!i)return;const n=hi({context:this.reader.context,navigationResolver:this.reader.navigation.navigationResolver,position:t.position,computedPageTurnDirection:this.reader.settings.values.computedPageTurnDirection,spineItem:i,spineItemsManager:this.reader.spineItemsManager,spineLocator:this.reader.spine.locator});return this.reader.navigation.navigate({position:n})}goToCfi(t,i={animate:!0}){return this.reader.navigation.navigate({animation:i.animate?"turn":!1,cfi:t})}goToSpineItem({indexOrId:t,...i}){const n=this.reader.spineItemsManager.get(t);if(n===void 0){U.warn("goToSpineItem",`Ignore navigation to ${t} since the item does not exist`);return}this.reader.navigation.navigate({spineItem:n.index,...i})}goToNextSpineItem(){const{endIndex:t=0}=this.reader.spine.locator.getVisibleSpineItemsFromPosition({position:this.reader.navigation.getNavigation().position,threshold:.5})||{};this.goToSpineItem({indexOrId:t+1})}goToPreviousSpineItem(){const{beginIndex:t=0}=this.reader.spine.locator.getVisibleSpineItemsFromPosition({position:this.reader.navigation.getNavigation().position,threshold:.5})??{};this.goToSpineItem({indexOrId:t-1})}goToUrl(t){this.reader.navigation.navigate({url:t,animation:!1})}goToRightSpineItem(){if(this.reader.settings.values.computedPageTurnDirection==="vertical"){U.warn("You cannot call this navigation method on vertical direction");return}return this.reader.context.isRTL()?this.goToPreviousSpineItem():this.goToNextSpineItem()}goToRightOrBottomSpineItem(){return this.reader.settings.values.computedPageTurnDirection==="vertical"?this.goToBottomSpineItem():this.goToRightSpineItem()}goToLeftOrTopSpineItem(){return this.reader.settings.values.computedPageTurnDirection==="vertical"?this.goToTopSpineItem():this.goToLeftSpineItem()}goToLeftSpineItem(){if(this.reader.settings.values.computedPageTurnDirection==="vertical"){U.warn("You cannot call this navigation method on vertical direction");return}return this.reader.context.isRTL()?this.goToNextSpineItem():this.goToPreviousSpineItem()}goToTopSpineItem(){if(this.reader.settings.values.computedPageTurnDirection==="horizontal"){U.warn("You cannot call this navigation method on horizontal direction");return}return this.goToPreviousSpineItem()}goToBottomSpineItem(){if(this.reader.settings.values.computedPageTurnDirection==="horizontal"){U.warn("You cannot call this navigation method on horizontal direction");return}return this.goToNextSpineItem()}goToPageOfSpineItem({pageIndex:t,spineItemId:i,...n}){const s=this.reader.navigation.navigationResolver.getNavigationForSpineItemPage({pageIndex:t,spineItemId:i});U.debug(".goToPageOfSpineItem()",{pageIndex:t,spineItemId:i,...n,position:s}),this.reader.navigation.navigate({position:s,...n})}goToAbsolutePageIndex({absolutePageIndex:t,...i}){const n=this.reader.spine.locator.getSpineInfoFromAbsolutePageIndex({absolutePageIndex:t});if(U.debug(".goToAbsolutePageIndex()",{absolutePageIndex:t,...i,foundInfo:n}),n){const s=this.reader.navigation.navigationResolver.getNavigationForSpineItemPage({pageIndex:n.pageIndex,spineItemId:n.itemIndex});return this.reader.navigation.navigate({position:s,...i})}}}class bi{constructor(t){this.reader=t,this.lastDelta={x:0,y:0},this.lastPosition=new T({x:0,y:0}),this.lastStartPosition=new T({x:0,y:0}),this.unlock=void 0}moveTo(t,{final:i,start:n}={}){var a,c,u,l;if(this.reader.settings.values.computedPageTurnMode==="scrollable"){U.warn("pan control is not available on free page turn mode");return}const s=this.reader.settings.values.computedPageTurnDirection;n&&((a=this.unlock)==null||a.call(this),this.unlock=this.reader.navigation.lock(),this.lastDelta={x:0,y:0},this.lastStartPosition=this.reader.navigation.controlledNavigationController.viewportPosition,this.lastPosition=this.lastStartPosition);let r=this.reader.navigation.getNavigation().position;if(t){const d=this.reader.viewport.absoluteViewport.width/this.reader.viewport.relativeViewport.width,g=Math.floor(t.x)-(((c=this.lastDelta)==null?void 0:c.x)||0),p=Math.floor(t.y)-(((u=this.lastDelta)==null?void 0:u.y)||0),h=Math.floor(s==="horizontal"?this.lastPosition.x-g/d:0),m=Math.floor(s==="horizontal"?0:this.lastPosition.y-p/d);r=new T({x:h,y:m}),this.lastDelta=t}else r=this.lastPosition;if(this.lastPosition=r,i){this.lastDelta={x:0,y:0},this.reader.navigation.navigate({position:r,animation:!1}),(l=this.unlock)==null||l.call(this);return}this.reader.navigation.navigate({position:r,animation:!1})}}const yi=e=>e.pagination.state$.pipe(o.withLatestFrom(e.context.manifest$,e.settings.values$),o.map(([t,{spineItems:i,readingDirection:n},{computedPageTurnDirection:s}])=>{const r=i.length??0,a=t.beginSpineItemIndex===0,c=t.endSpineItemIndex===Math.max(r-1,0),u=t.endSpineItemIndex===Math.max(r-1,0),l=t.beginSpineItemIndex===0,d=t.beginPageIndexInSpineItem===0,g=t.endPageIndexInSpineItem===t.endNumberOfPagesInSpineItem-1;return{canTurnLeft:s==="vertical"?!1:!d,canTurnRight:s==="vertical"?!1:!g,canGoTopSpineItem:s==="vertical"&&!a,canGoBottomSpineItem:s==="vertical"&&!c,canGoLeftSpineItem:s!=="vertical"&&(n==="ltr"&&!a||n==="rtl"&&!u),canGoRightSpineItem:s!=="vertical"&&(n==="ltr"&&!c||n==="rtl"&&!l)}}),o.distinctUntilChanged(M.isShallowEqual)),wi=({reader:e,duration:t})=>i=>{let n;const s=()=>{n==null||n(),n=void 0};return i.pipe(o.tap(()=>{n||(n=e==null?void 0:e.navigation.lock())}),o.throttleTime(t,o.animationFrameScheduler,{trailing:!0,leading:!0}),o.tap(s),o.finalize(s))},Ii=e=>t=>{const i=e(t),n=yi(i),s=new Si(i),r=new bi(i),a=u=>{const{cfi:l,...d}=u;i.load(d),l&&s.goToCfi(l,{animate:!1})};return gi(i,s).pipe(o.takeUntil(i.$.destroy$)).subscribe(),{...i,load:a,navigation:{...i.navigation,state$:n,throttleLock:({duration:u,trigger:l})=>l.pipe(wi({duration:u,reader:i})),moveTo:r.moveTo.bind(r),turnBottom:s.turnBottom.bind(s),turnTop:s.turnTop.bind(s),turnLeftOrTop:s.turnLeftOrTop.bind(s),turnRightOrBottom:s.turnRightOrBottom.bind(s),turnLeft:s.turnLeft.bind(s),turnRight:s.turnRight.bind(s),goToCfi:s.goToCfi.bind(s),goToUrl:s.goToUrl.bind(s),goToSpineItem:s.goToSpineItem.bind(s),goToNextSpineItem:s.goToNextSpineItem.bind(s),goToPreviousSpineItem:s.goToPreviousSpineItem.bind(s),goToLeftOrTopSpineItem:s.goToLeftOrTopSpineItem.bind(s),goToRightOrBottomSpineItem:s.goToRightOrBottomSpineItem.bind(s),goToTopSpineItem:s.goToTopSpineItem.bind(s),goToBottomSpineItem:s.goToBottomSpineItem.bind(s),goToLeftSpineItem:s.goToLeftSpineItem.bind(s),goToRightSpineItem:s.goToRightSpineItem.bind(s),goToPageOfSpineItem:s.goToPageOfSpineItem.bind(s),goToAbsolutePageIndex:s.goToAbsolutePageIndex.bind(s)}}},q=e=>E.generate({spineIndex:e.index,spineId:e.id}),x=(e,t,i)=>{var n;return!e.ownerDocument||!((n=e.ownerDocument)!=null&&n.documentElement)||e===e.ownerDocument?q(i):E.generate({node:e,offset:t,spineIndex:i.index,spineId:i.id})},ht=({pageIndex:e,spineItem:t,spineItemLocator:i})=>{var r;const n=i.getFirstNodeOrRangeAtPage(e,t),s=t.renderer.getDocumentFrame();return n&&s instanceof HTMLIFrameElement&&((r=s.contentWindow)!=null&&r.document)?x(n.node,n.offset,t.item).trim():q(t.item)},Pi=(e,t)=>{const i=x(e.startContainer,e.startOffset,t),n=x(e.endContainer,e.endOffset,t);return{start:i,end:n}},vi=({item:e,selection:t})=>{const i=t.anchorNode?x(t.anchorNode,t.anchorOffset,e):void 0,n=t.focusNode?x(t.focusNode,t.focusOffset,e):void 0;return{anchorCfi:i,focusCfi:n}},qt=e=>{var t;return((t=e[0])==null?void 0:t.index)===6&&e.length>1},mt=e=>{const t=E.parse(e);return E.isIndirectionOnly(t)},$i=e=>Array.isArray(e)?e[0]&&qt(e[0])?e[0]:void 0:e.parent[0]&&qt(e.parent[0])?e.parent[0]:void 0,ft=e=>{var c,u,l,d;const t=E.parse(e),n=($i(t)??[])[1],r=((n==null?void 0:n.index)??2)/2-1,a=E.isParsedCfiRange(t)?(u=(c=t.end.at(-1))==null?void 0:c.at(-1))==null?void 0:u.offset:(d=(l=t.at(-1))==null?void 0:l.at(-1))==null?void 0:d.offset;return{cleanedCfi:e,itemIndex:r,offset:a??0}},xt=({cfi:e,spineItemsManager:t})=>{var r,a;if(!e)return;const{itemIndex:i}=ft(e),n=t.get(i);if(!n)return;const s=n.renderer.getDocumentFrame();if(s instanceof HTMLIFrameElement){console.log("FIII",e);const c=(r=s.contentWindow)==null?void 0:r.document;if(c)try{const u=E.resolve(e,c,{throwOnError:!0});return{node:u.isRange?(a=u.node)==null?void 0:a.startContainer:u.node,offset:Array.isArray(u.offset)?u.offset.at(-1):u.offset,spineItem:n}}catch(u){return A.error(u),{spineItem:n}}}return{spineItem:n}},Mi=(e,t)=>{if("cfi"in t)return t;const i=e.spineItemsManager.get(t);if(!i)throw new Error("Spine item not found");return{cfi:q(i.item)}},Ti=(e,t)=>{let i=e==null?void 0:e.itemPageIndex;const{itemIndex:n}=t.cfi.parseCfi(e.cfi),s=t.spineItemsManager.get(n);return s?lt().pipe(o.withLatestFrom(s.isReady$),o.map(([,r])=>{var g;let a;const{node:c,offset:u}=t.cfi.resolveCfi({cfi:e.cfi})??{};if(s.renditionLayout!=="pre-paginated"&&c&&(i=t.spine.locator.spineItemLocator.getSpineItemPageIndexFromNode(c,u??0,s)??i),c&&e.endCfi){const{node:p,offset:h}=t.cfi.resolveCfi({cfi:e.cfi})??{};p&&r&&(a=(g=c==null?void 0:c.ownerDocument)==null?void 0:g.createRange(),a==null||a.setStart(c,u??0),a==null||a.setEnd(p,h??0))}let d=e==null?void 0:e.absolutePageIndex;return i===void 0&&(i=0),d=t.spine.locator.getAbsolutePageIndexFromPageIndex({pageIndex:i,spineItemOrId:s})??(e==null?void 0:e.absolutePageIndex),{...e,range:a,itemIndex:n,startNode:c??void 0,startOffset:u,absolutePageIndex:d,itemPageIndex:i}})):o.of({...e,itemIndex:n})};class Fi{constructor(t){this.reader=t,this.locate=(i,n)=>{const s={resource:i,meta:Mi(this.reader,i)};return dt(()=>{var d;const r=this.reader.spine.spineLayout.layout$.pipe(o.debounceTime(10),o.startWith(s),o.switchScan(g=>Ti(g.meta,this.reader).pipe(o.map(p=>({...g,meta:p}))),s)),a=(d=this.reader.cfi.parseCfi(s.meta.cfi))==null?void 0:d.itemIndex,c=this.reader.spineItemsManager.get(a),u=(c==null?void 0:c.renditionLayout)==="reflowable",l=n.mode==="shallow"||!u||!c?()=>{}:this.reader.spine.spineItemsLoader.forceOpen([c.index]);return r.pipe(o.finalize(()=>{setTimeout(()=>{l()},1e3)}))})}}locateResource(t,i){return Array.isArray(t)?dt(()=>o.combineLatest(t.map(n=>this.locate(n,i??{}))).pipe(o.defaultIfEmpty([]))):this.locate(t,i??{})}}const Yt=(e,t,i)=>{const n=i.spineItems.findIndex(s=>s.href===e);return t.reduce((s,r)=>{const a=r.href.indexOf("#"),c=a>0?r.href.substr(0,a):r.href,u=c.substring(0,c.lastIndexOf("/")),l=e.substring(0,e.lastIndexOf("/")),d=e.endsWith(c),g=l!==""&&l.endsWith(u);if(d||g){const h=i.spineItems.findIndex(S=>S.href===r.href);if(n<h)return s;const f={title:r.title,path:r.path},w=Yt(e,r.contents,i);return w?{...f,subChapter:w}:f}return s},void 0)},Li=(e,t)=>{var n;const{href:i}=t;return Yt(i,((n=e.nav)==null?void 0:n.toc)??[],e)},Ci=e=>{const t=e.context.manifest,i=e.spineItemsManager.items;return t?i.reduce((n,{item:s})=>(n[s.id]=Li(t,s),n),{}):{}},Ri=e=>e.spineItemsManager.items$.pipe(o.startWith([]),o.map(()=>Ci(e))),Ai=(e,t,i)=>e+t*i,Oi=(e,t,i)=>{const n=e.context,{height:s,width:r}=i.layout.layoutInfo,{top:a,left:c}=e.spine.spineLayout.getSpineItemSpineLayoutInfo(i);return e.settings.values.computedPageTurnDirection==="vertical"?Math.max(0,Math.min(1,(t.y-a+n.state.visibleAreaRect.height)/s)):Math.max(0,Math.min(1,(t.x-c+n.state.visibleAreaRect.width)/r))},Di=(e,t,i,n,s)=>s.isReady$.pipe(o.first(),o.withLatestFrom(e.layoutInfo$),o.map(([r,a])=>{var S,y,I,v,$,F,L;const c=e.context,u=((S=c.manifest)==null?void 0:S.renditionLayout)==="pre-paginated",l=((y=c.manifest)==null?void 0:y.spineItems.length)||0,d=((I=c.manifest)==null?void 0:I.spineItems.slice(0,t).reduce((R,D)=>R+(D.progressionWeight??0),0))||0,g=e.spineItemsManager.getSpineItemIndex(s)??0,p=((v=e.context.manifest)==null?void 0:v.spineItems.length)??0,h=a.pages.filter(R=>R.itemIndex===g).length??0,m=((F=($=c.manifest)==null?void 0:$.spineItems[t])==null?void 0:F.progressionWeight)??(g+1)/p;let f=(i+1)*(m/h);!u&&s.renditionLayout==="reflowable"&&!r&&(f=0);let w=d+f;return((L=c.manifest)==null?void 0:L.renditionFlow)==="scrolled-continuous"&&(r?f=Oi(e,n,s):f=0,w=Ai(d,m,f)),t===l-1&&i===h-1&&w>.99?1:w})),Ni=e=>e.spine.spineLayout.layout$.pipe(o.debounceTime(10,o.animationFrameScheduler),o.withLatestFrom(e.pagination.state$),o.map(()=>({numberOfPagesPerItems:e.spineItemsManager.items.reduce((i,n)=>[...i,n.numberOfPages],[]),numberOfTotalPages:e.spine.spineLayout.numberOfPages})),o.distinctUntilChanged(M.isShallowEqual),o.startWith({numberOfPagesPerItems:[],numberOfTotalPages:0})),Vi=(e,t,i,n)=>{const s=e.context,r=t.beginSpineItemIndex!==void 0?e.spineItemsManager.get(t.beginSpineItemIndex):void 0,a=t.endSpineItemIndex!==void 0?e.spineItemsManager.get(t.endSpineItemIndex):void 0;return o.of({...t,beginChapterInfo:r?i[r.item.id]:void 0,beginSpineItemReadingDirection:r==null?void 0:r.readingDirection,endChapterInfo:a?i[a.item.id]:void 0,endSpineItemReadingDirection:a==null?void 0:a.readingDirection,percentageEstimateOfBook:n,isUsingSpread:s.state.isUsingSpreadMode??!1})},ki=e=>o.combineLatest([e.pagination.state$,e.layout$]).pipe(o.switchMap(([t])=>{const i=t.endSpineItemIndex!==void 0?e.spineItemsManager.get(t.endSpineItemIndex):void 0;return i?Di(e,t.endSpineItemIndex??0,t.endPageIndexInSpineItem||0,e.navigation.getNavigation().position,i):o.of(0)})),Ui=e=>{const t=Ri(e),i=Ni(e),n=new o.BehaviorSubject({...e.pagination.state,beginChapterInfo:void 0,beginCfi:void 0,beginPageIndexInSpineItem:void 0,isUsingSpread:!1,beginAbsolutePageIndex:0,endAbsolutePageIndex:0,numberOfTotalPages:0,beginSpineItemReadingDirection:void 0,beginSpineItemIndex:void 0,endCfi:void 0,endChapterInfo:void 0,endSpineItemReadingDirection:void 0,percentageEstimateOfBook:0}),s=o.combineLatest([e.pagination.state$,t,ki(e)]).pipe(o.switchMap(([a,c,u])=>Vi(e,a,c,u).pipe(o.map(l=>({...a,...l})))),o.distinctUntilChanged(M.isShallowEqual));return{paginationInfo$:o.combineLatest([s,i]).pipe(o.map(([a,c])=>({...a,...c,beginAbsolutePageIndex:c.numberOfPagesPerItems.slice(0,a.beginSpineItemIndex).reduce((u,l)=>u+l,a.beginPageIndexInSpineItem??0),endAbsolutePageIndex:c.numberOfPagesPerItems.slice(0,a.endSpineItemIndex).reduce((u,l)=>u+l,a.endPageIndexInSpineItem??0)})),o.tap(a=>{n.next(a)}),o.shareReplay(1)),getPaginationInfo:()=>n.value}},Ei=e=>t=>{const i=e(t),{paginationInfo$:n,getPaginationInfo:s}=Ui(i);n.pipe(o.takeUntil(i.$.destroy$)).subscribe();const r=new Fi(i);return{...i,locateResource:r.locateResource.bind(r),pagination:{...i.pagination,get state(){return s()},get state$(){return n}}}},Wi=e=>({put:(r,a)=>new Promise((c,u)=>{const l=e.transaction(["store"],"readwrite");l.onerror=p=>{u(p)},l.oncomplete=()=>{c()};const g=l.objectStore("store").put(a,r);g.onsuccess=()=>{c()},g.onerror=p=>{u(p)}}),keys:()=>new Promise((r,a)=>{const c=e.transaction(["store"],"readonly");c.onerror=g=>{a(g)};const l=c.objectStore("store").openKeyCursor(),d=[];l.onsuccess=()=>{const g=l.result;if(!g){r(d);return}d.push(g.key),g.continue()},l.onerror=()=>{a(l.error)}}),get:r=>new Promise((a,c)=>{const u=e.transaction(["store"],"readwrite"),d=u.objectStore("store").get(r);d.onsuccess=()=>{let g=d.result;g===void 0&&(g=null),a(g)},u.onerror=()=>{c(d.error)}}),remove:r=>new Promise((a,c)=>{const u=e.transaction(["store"],"readwrite"),d=u.objectStore("store").delete(r);u.onerror=()=>{c(d.error)},u.oncomplete=()=>{a()},u.onabort=()=>{var p;const g=d.error?d.error:(p=d.transaction)==null?void 0:p.error;c(g)}})}),St=async e=>new Promise((t,i)=>{const n=window.indexedDB.open(e);n.onerror=s=>{i(s)},n.onsuccess=()=>{t(Wi(n.result))},n.onupgradeneeded=()=>{n.result.createObjectStore("store")}}),zi=e=>{let t=Date.now().toString();const i=new o.Subject,n=u=>{var l,d;if(typeof u=="string"||typeof u=="object"){const g=typeof u=="object"?u.id:u;return(l=e.manifest)==null?void 0:l.spineItems.find(p=>p.id===g)}return(d=e.manifest)==null?void 0:d.spineItems[u]},s=async(u,l)=>{const d=n(u);if(!d)return new Response("Item not found",{status:404});const p=await(await St("prose-reader")).get(`${t}_${d.id}`);if(p)return new Response(p,{status:200});const h=l&&await l(d)||await fetch(d.href);return r(d,h.clone()),h},r=(u,l)=>{i.next({id:u,data:l})};i.asObservable().pipe(b.mergeMap(({id:u,data:l})=>{const d=n(u);return d?o.from(o.forkJoin([St("prose-reader"),o.from(l.blob())])).pipe(b.switchMap(([g,p])=>o.from(g.put(`${t}_${d.id}`,p))),b.catchError(g=>(A.error(g),o.EMPTY))):o.EMPTY}),b.takeUntil(e.destroy$)).subscribe();const a=e.manifest$.pipe(b.tap(()=>{t=Date.now().toString()}));return o.merge(a).pipe(b.switchMap(()=>(A.debug("Cleanup up old cache..."),o.from(St("prose-reader")).pipe(b.switchMap(u=>o.from(u.keys()).pipe(b.map(l=>l.filter(d=>!d.toString().startsWith(t))),b.switchMap(l=>{const d=l.map(g=>u.remove(g));return o.from(Promise.all(d))}))),b.catchError(u=>(A.error(u),o.EMPTY))))),b.takeUntil(e.destroy$)).subscribe(),{get:s,destroy:()=>{i.complete()}}},Hi=e=>t=>{const i=e(t),n=zi(i.context);return{...i,destroy:()=>{n.destroy(),i.destroy()}}},Bi=(e,t)=>{var s;const i=(s=e.node.ownerDocument)==null?void 0:s.createRange(),n=e.node.compareDocumentPosition(t.node);if(i){try{if(n&Node.DOCUMENT_POSITION_PRECEDING)i.setStart(t.node,t.offset||0),i.setEnd(e.node,e.offset||0);else if(n&Node.DOCUMENT_POSITION_FOLLOWING)i.setStart(e.node,e.offset||0),i.setEnd(t.node,t.offset||0);else{const r=Math.min(e.offset||0,t.offset||0),a=Math.max(e.offset||0,t.offset||0);i.setStart(e.node,r),i.setEnd(e.node,a)}}catch(r){A.warn("Failed to create range from selection",r,{anchor:e,focus:t})}return i}},_i=({selection:e,spineItem:t})=>{const{anchorNode:i,anchorOffset:n,focusNode:s,focusOffset:r}=e;if(!(!i||!s))try{return Bi({node:i,offset:n},{node:s,offset:r})}catch(a){A.warn("Failed to create range from selection",a,{selection:e,spineItem:t});return}};class qi extends O{constructor(t){var n;super();const i=t.contentDocument||((n=t.contentWindow)==null?void 0:n.document);if(!i)this.selectionChange$=o.NEVER,this.selectionOver$=o.NEVER;else{const s=gt(i.body,{childList:!0,subtree:!0}).pipe(o.filter(a=>!!a.find(c=>c.type==="childList"&&c.removedNodes.length))),r=t.parentElement?gt(t.parentElement,{childList:!0}).pipe(o.filter(a=>!!a.find(c=>Array.from(c.removedNodes).includes(t)))):o.of(null);this.selectionChange$=o.merge(o.fromEvent(i,"selectionchange"),s).pipe(o.map(()=>i.getSelection()),o.takeUntil(o.merge(r,this.destroy$)),o.endWith(null)),this.selectionOver$=o.fromEvent(i,"pointerdown").pipe(o.switchMap(()=>o.merge(o.fromEvent(i,"pointerup"),o.fromEvent(i,"pointercancel"),o.fromEvent(i,"contextmenu")).pipe(o.first(),o.delay(0),o.map(a=>{const c=i.getSelection();return c&&!c.isCollapsed?[a,c]:void 0}),o.filter(W))),o.takeUntil(o.merge(r,this.destroy$)))}}}const xi=e=>e.loaded$.pipe(o.switchMap(()=>{var s;const t=e.renderer.getDocumentFrame(),i=(t==null?void 0:t.contentDocument)||((s=t==null?void 0:t.contentWindow)==null?void 0:s.document);if(!t||!i)return o.NEVER;const n=new qi(t);return o.merge(n.selectionChange$.pipe(o.map(r=>{if(r!=null&&r.toString())return{type:"change",selection:r}})),n.selectionOver$.pipe(o.map(([r,a])=>({type:"over",event:r,selection:a})))).pipe(o.takeUntil(e.unloaded$),o.endWith(void 0),o.finalize(()=>{n.destroy()}))}),o.distinctUntilChanged()),Yi=e=>t=>{const i=e(t);let n;const s=i.spineItemsManager.items$.pipe(o.switchMap(d=>{const g=d.map(p=>{const h=i.spineItemsManager.getSpineItemIndex(p)??0;return xi(p).pipe(o.map(m=>{if(m)return{...m,itemIndex:h}}))});return o.merge(...g)}),o.startWith(void 0),o.distinctUntilChanged(),o.tap(d=>{n=d}),o.shareReplay({refCount:!0,bufferSize:1})),r=s,a=s.pipe(o.map(d=>!!d),o.distinctUntilChanged(),o.filter(d=>d),o.share()),c=a.pipe(o.switchMap(()=>r),o.distinctUntilChanged(),o.filter(d=>!d),o.share()),u=s.pipe(o.filter(d=>(d==null?void 0:d.type)==="over"),o.share()),l=i.context.containerElement$.pipe(o.switchMap(d=>o.fromEvent(d,"pointerdown")),o.withLatestFrom(r),o.map(([,d])=>d),o.startWith(void 0),o.shareReplay({refCount:!0,bufferSize:1}));return o.merge(r,l).pipe(o.takeUntil(i.$.destroy$)).subscribe(),{...i,selection:{selection$:r,selectionStart$:a,selectionEnd$:c,selectionOver$:u,lastSelectionOnPointerdown$:l,getSelection:()=>n,createOrderedRangeFromSelection:_i}}},Xt=[{name:"bright",backgroundColor:"white"},{name:"sepia",backgroundColor:"#eaddc7",foregroundColor:"black"},{name:"night",backgroundColor:"#191717",foregroundColor:"#f1ebeb"}],Xi=e=>t=>{const i=e(t),n=new o.BehaviorSubject(t.theme??"bright"),s=()=>{const c=Xt.find(u=>u.name===n.value);return`
248
248
  body {
249
249
  ${c!==void 0?`background-color: ${c.backgroundColor} !important;`:""}
250
250
  }