noteloom 0.6.0 → 0.7.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.
Files changed (62) hide show
  1. package/dist/canvas.cjs +1 -1
  2. package/dist/canvas.js +1 -1
  3. package/dist/comments.cjs +1 -1
  4. package/dist/comments.js +3 -3
  5. package/dist/index.cjs +2 -1
  6. package/dist/index.cjs.map +1 -1
  7. package/dist/index.d.ts +6 -0
  8. package/dist/index.js +1529 -1488
  9. package/dist/index.js.map +1 -1
  10. package/dist/shared/{CommentThreadCard-DcCHq2Dy.cjs → CommentThreadCard-CIB6nZEA.cjs} +2 -2
  11. package/dist/shared/{CommentThreadCard-DcCHq2Dy.cjs.map → CommentThreadCard-CIB6nZEA.cjs.map} +1 -1
  12. package/dist/shared/{CommentThreadCard-DwtE85Xx.js → CommentThreadCard-lW_lbI74.js} +2 -2
  13. package/dist/shared/{CommentThreadCard-DwtE85Xx.js.map → CommentThreadCard-lW_lbI74.js.map} +1 -1
  14. package/dist/shared/{CommentsPanel-BRgACUu_.cjs → CommentsPanel-CRxb3RWi.cjs} +2 -2
  15. package/dist/shared/{CommentsPanel-BRgACUu_.cjs.map → CommentsPanel-CRxb3RWi.cjs.map} +1 -1
  16. package/dist/shared/{CommentsPanel-D4eP-K_f.js → CommentsPanel-Dg0k_XNU.js} +3 -3
  17. package/dist/shared/{CommentsPanel-D4eP-K_f.js.map → CommentsPanel-Dg0k_XNU.js.map} +1 -1
  18. package/dist/shared/{VersionHistory-BRBYQgj5.cjs → VersionHistory-BsSwOq_k.cjs} +2 -2
  19. package/dist/shared/{VersionHistory-BRBYQgj5.cjs.map → VersionHistory-BsSwOq_k.cjs.map} +1 -1
  20. package/dist/shared/{VersionHistory-D9sIIAYN.js → VersionHistory-BzMnJpbc.js} +3 -3
  21. package/dist/shared/{VersionHistory-D9sIIAYN.js.map → VersionHistory-BzMnJpbc.js.map} +1 -1
  22. package/dist/shared/{VoiceListeningIndicator-Dw0fggpE.cjs → VoiceListeningIndicator-B_7-6Mq4.cjs} +2 -2
  23. package/dist/shared/{VoiceListeningIndicator-Dw0fggpE.cjs.map → VoiceListeningIndicator-B_7-6Mq4.cjs.map} +1 -1
  24. package/dist/shared/{VoiceListeningIndicator-DgLMmGSP.js → VoiceListeningIndicator-DeA3hb33.js} +4 -4
  25. package/dist/shared/{VoiceListeningIndicator-DgLMmGSP.js.map → VoiceListeningIndicator-DeA3hb33.js.map} +1 -1
  26. package/dist/shared/{icons-Bdptdx1e.js → icons-CmfXmn13.js} +307 -259
  27. package/dist/shared/icons-CmfXmn13.js.map +1 -0
  28. package/dist/shared/{icons-DabR_JDW.cjs → icons-DQ7j_-ZK.cjs} +44 -3
  29. package/dist/shared/icons-DQ7j_-ZK.cjs.map +1 -0
  30. package/dist/shared/{index-DOIEccLM.js → index-DUigqWQn.js} +3 -3
  31. package/dist/shared/{index-DOIEccLM.js.map → index-DUigqWQn.js.map} +1 -1
  32. package/dist/shared/{index-B7_F6NA9.cjs → index-vWtS1iyt.cjs} +2 -2
  33. package/dist/shared/{index-B7_F6NA9.cjs.map → index-vWtS1iyt.cjs.map} +1 -1
  34. package/dist/shared/{leafBlockFactory-D3SCDvzc.cjs → leafBlockFactory-6GFBveaC.cjs} +2 -2
  35. package/dist/shared/{leafBlockFactory-D3SCDvzc.cjs.map → leafBlockFactory-6GFBveaC.cjs.map} +1 -1
  36. package/dist/shared/{leafBlockFactory-ChlzpKIn.js → leafBlockFactory-Dm6cd2NY.js} +5 -5
  37. package/dist/shared/{leafBlockFactory-ChlzpKIn.js.map → leafBlockFactory-Dm6cd2NY.js.map} +1 -1
  38. package/dist/shared/{serialize-C1rjLsIz.js → serialize-R4V4gmHs.js} +2 -2
  39. package/dist/shared/{serialize-C1rjLsIz.js.map → serialize-R4V4gmHs.js.map} +1 -1
  40. package/dist/shared/{serialize-k0zXqwEM.cjs → serialize-vLXz0cze.cjs} +2 -2
  41. package/dist/shared/{serialize-k0zXqwEM.cjs.map → serialize-vLXz0cze.cjs.map} +1 -1
  42. package/dist/shared/starter-kit-BQUaaql0.cjs +29 -0
  43. package/dist/shared/starter-kit-BQUaaql0.cjs.map +1 -0
  44. package/dist/shared/{starter-kit-CJzWMyF8.js → starter-kit-Bp30PiE4.js} +1246 -1188
  45. package/dist/shared/starter-kit-Bp30PiE4.js.map +1 -0
  46. package/dist/shared/{usePopoverEdgeClamp-gRlLQeVG.cjs → usePopoverEdgeClamp-BJLbbANB.cjs} +2 -2
  47. package/dist/shared/{usePopoverEdgeClamp-gRlLQeVG.cjs.map → usePopoverEdgeClamp-BJLbbANB.cjs.map} +1 -1
  48. package/dist/shared/{usePopoverEdgeClamp-B9KNv9SE.js → usePopoverEdgeClamp-DSpz-6xR.js} +2 -2
  49. package/dist/shared/{usePopoverEdgeClamp-B9KNv9SE.js.map → usePopoverEdgeClamp-DSpz-6xR.js.map} +1 -1
  50. package/dist/starter-kit.cjs +1 -1
  51. package/dist/starter-kit.js +2 -2
  52. package/dist/style.css +41 -0
  53. package/dist/versions.cjs +1 -1
  54. package/dist/versions.js +1 -1
  55. package/dist/voice.cjs +1 -1
  56. package/dist/voice.js +1 -1
  57. package/package.json +1 -1
  58. package/dist/shared/icons-Bdptdx1e.js.map +0 -1
  59. package/dist/shared/icons-DabR_JDW.cjs.map +0 -1
  60. package/dist/shared/starter-kit-CJzWMyF8.js.map +0 -1
  61. package/dist/shared/starter-kit-Ch2LEpzF.cjs +0 -27
  62. package/dist/shared/starter-kit-Ch2LEpzF.cjs.map +0 -1
@@ -1,4 +1,4 @@
1
- "use strict";const h=require("react/jsx-runtime"),_=require("react"),Q=require("react-dom"),a=require("./icons-DabR_JDW.cjs"),P=require("./indexedDbPersistence-BWJ1h5Vp.cjs"),v=require("./serialize-k0zXqwEM.cjs"),S=require("./fieldRegistry-BoYyW3GH.cjs"),j=require("./CommentAvatar-V457IgoQ.cjs");function Z(d,t){const i=Array.from(d??""),e=Array.from(t??"");let s=0;const n=Math.min(i.length,e.length);for(;s<n&&i[s]===e[s];)s+=1;let r=i.length,o=e.length;for(;r>s&&o>s&&i[r-1]===e[o-1];)r-=1,o-=1;return{start:s,oldEnd:r,newEnd:o,oldChars:i,newChars:e}}const B="$fieldTypes",x="$comments",C="$people",H="$title";class L{constructor(t){this.blocks=new Map(((t==null?void 0:t.blocks)??[]).map(i=>[i.id,i])),this.runs=new Map(((t==null?void 0:t.runs)??[]).map(i=>[i.id,i])),this.rootId=(t==null?void 0:t.rootId)??null,this.fieldTypes=new Map(((t==null?void 0:t.fieldTypes)??[]).map(i=>[i.id,i])),this._fieldTypesSnapshot=null,this.comments=new Map(((t==null?void 0:t.comments)??[]).map(i=>[i.id,i])),this._commentsSnapshot=null,this.people=new Map(((t==null?void 0:t.people)??[]).map(i=>[i.id,i])),this._peopleSnapshot=null,this.title=(t==null?void 0:t.title)??"",this._listeners=new Map,this._globalListeners=new Set,this._peerId=S.genPeerId(),this._clock=new S.HLC(this._peerId),this._fieldClocks=new S.FieldClockRegistry,this._orders=new Map,this._runOrders=new Map,this._runChars=new Map,this._runValueSnapshots=new Map}_getOrder(t){let i=this._orders.get(t);if(!i){const e=this.blocks.get(t);i=S.ListCrdtState.fromArray((e==null?void 0:e.contentIds)??[],{peerId:"legacy"}),this._orders.set(t,i)}return i}_getRunOrder(t){let i=this._runOrders.get(t);if(!i){const e=this.runs.get(t),s=new Map;i=new S.ListCrdtState;let n=null,r=0;for(const o of Array.from((e==null?void 0:e.value)??"")){const c=`${t}:legacyChar:${r}`;s.set(c,o),i.insert(c,n,{wallTime:0,counter:0,peerId:"legacy"},"legacy"),n=c,r+=1}this._runOrders.set(t,i),this._runChars.set(t,s)}return i}_materializeRunValue(t){let i=this._runValueSnapshots.get(t);if(i===void 0){const e=this._getRunOrder(t),s=this._runChars.get(t);i=e.toArray().map(n=>s.get(n)).join(""),this._runValueSnapshots.set(t,i)}return i}_editRunText(t,i){const e=this._materializeRunValue(t),{start:s,oldEnd:n,newEnd:r,newChars:o}=Z(e,i),c=this._getRunOrder(t),l=this._runChars.get(t),u=c.toArray(),m=this._clock.tick(),f=[];for(let T=s;T<n;T+=1){const g=u[T];c.delete(g,m),f.push({id:g,clock:m})}const k=[];let w=s>0?u[s-1]:null;for(let T=s;T<r;T+=1){const g=a.genId(),I=o[T];l.set(g,I),c.insert(g,w,m,this._peerId),k.push({id:g,char:I,afterId:w,clock:m}),w=g}return this._runValueSnapshots.delete(t),{oldValue:e,insertedChars:k,deletedCharIds:f,clock:m}}_replayRunTextEdit(t,i){const e=this._getRunOrder(t),s=this._runChars.get(t);for(const n of i.insertedChars)e.has(n.id)?e.restore(n.id):(s.set(n.id,n.char),e.insert(n.id,n.afterId,n.clock,this._peerId));for(const n of i.deletedCharIds)e.delete(n.id,n.clock);this._runValueSnapshots.delete(t)}getBlock(t){return this.blocks.get(t)}getRun(t){return this.runs.get(t)}getFieldTypes(){return this._fieldTypesSnapshot||(this._fieldTypesSnapshot=[...this.fieldTypes.values()]),this._fieldTypesSnapshot}getFieldType(t){return this.fieldTypes.get(t)}getComments(){return this._commentsSnapshot||(this._commentsSnapshot=[...this.comments.values()]),this._commentsSnapshot}getComment(t){return this.comments.get(t)}getPeople(){return this._peopleSnapshot||(this._peopleSnapshot=[...this.people.values()]),this._peopleSnapshot}getPerson(t){return this.people.get(t)}getTitle(){return this.title}getAllRunIds(){return[...this.runs.keys()]}getRootId(){return this.rootId}subscribe(t,i){let e=this._listeners.get(t);return e||(e=new Set,this._listeners.set(t,e)),e.add(i),()=>{e.delete(i),e.size===0&&this._listeners.delete(t)}}subscribeAll(t){return this._globalListeners.add(t),()=>this._globalListeners.delete(t)}_notify(t){for(const i of t){const e=this._listeners.get(i);if(e)for(const s of[...e])s()}if(this._globalListeners.size>0)for(const i of[...this._globalListeners])i()}_captureSubtree(t){const i=[],e=[],s=n=>{var o;if(this.runs.has(n)){e.push(this.runs.get(n));return}const r=this.blocks.get(n);if(r){i.push(r);for(const c of r.contentIds)s(c);for(const c of((o=r.props)==null?void 0:o.titleRunIds)??[])s(c)}};return s(t),{blocks:i,runs:e}}_deleteSubtree(t){const i=e=>{var n;if(this.runs.has(e)){this.runs.delete(e);return}const s=this.blocks.get(e);if(s){for(const r of s.contentIds)i(r);for(const r of((n=s.props)==null?void 0:n.titleRunIds)??[])i(r);this.blocks.delete(e)}};i(t)}applyOperation(t){var i;switch(t.type){case a.OP.UPDATE_RUN:{const e=this.runs.get(t.id),s=Object.keys(t.patch);if(s.length===1&&s[0]==="value"){let o=t._charEdit;if(o)this._replayRunTextEdit(t.id,o);else{const{oldValue:l,insertedChars:u,deletedCharIds:m,clock:f}=this._editRunText(t.id,t.patch.value);o={oldValue:l,insertedChars:u,deletedCharIds:m,clock:f},t._charEdit=o}const c=this._materializeRunValue(t.id);return this.runs.set(t.id,{...e,value:c}),this._lastEnvelope={kind:"runTextEdit",runId:t.id,insertedChars:o.insertedChars,deletedCharIds:o.deletedCharIds,clock:o.clock},this._notify([t.id]),a.editRunChars(t.id,o.insertedChars.map(l=>l.id),o.deletedCharIds.map(l=>l.id),o.oldValue)}const n={},r={};for(const o of s)n[o]=e[o],r[o]=this._clock.tick(),this._fieldClocks.recordLocal(t.id,o,r[o]);return this.runs.set(t.id,{...e,...t.patch}),this._lastEnvelope={kind:"fieldWrite",target:"run",id:t.id,patch:t.patch,clocks:r},this._notify([t.id]),a.updateRun(t.id,n)}case a.OP.EDIT_RUN_CHARS:{const e=this._getRunOrder(t.id),s=this._clock.tick();for(const o of t.tombstoneIds)e.delete(o,s);for(const o of t.restoreIds)e.restore(o);this._runValueSnapshots.delete(t.id);const n=this.runs.get(t.id),r=this._materializeRunValue(t.id);return this.runs.set(t.id,{...n,value:r}),this._lastEnvelope={kind:"runCharEdit",runId:t.id,tombstoneIds:t.tombstoneIds,restoreIds:t.restoreIds,clock:s},this._notify([t.id]),a.editRunChars(t.id,t.restoreIds,t.tombstoneIds)}case a.OP.UPDATE_BLOCK_PROPS:{const e=this.blocks.get(t.id),s={},n={};for(const r of Object.keys(t.patch))s[r]=e.props[r],n[r]=this._clock.tick(),this._fieldClocks.recordLocal(t.id,`props.${r}`,n[r]);return this.blocks.set(t.id,{...e,props:{...e.props,...t.patch}}),this._lastEnvelope={kind:"fieldWrite",target:"blockProps",id:t.id,patch:t.patch,clocks:n},this._notify([t.id]),a.updateBlockProps(t.id,s)}case a.OP.CHANGE_BLOCK_TYPE:{const e=this.blocks.get(t.id),s=e.type,n=e.props,r=this._clock.tick();return this._fieldClocks.recordLocal(t.id,"type",r),this.blocks.set(t.id,{...e,type:t.blockType,props:t.props}),this._lastEnvelope={kind:"fieldWrite",target:"blockType",id:t.id,blockType:t.blockType,props:t.props,clock:r},this._notify([t.id]),a.changeBlockType(t.id,s,n)}case a.OP.INSERT_BLOCK:{if(t.subtree){for(const n of t.subtree.blocks)this.blocks.set(n.id,n);for(const n of t.subtree.runs)this.runs.set(n.id,n)}else this.blocks.set(t.block.id,t.block);const e=this.blocks.get(t.parentId),s=this._getOrder(t.parentId);if(s.has(t.block.id))s.restore(t.block.id);else{const n=s.toArray(),r=t.index??n.length,o=r>0?n[r-1]:null;s.insert(t.block.id,o,this._clock.tick(),this._peerId)}return this.blocks.set(t.parentId,{...e,contentIds:s.toArray()}),this._lastEnvelope={kind:"insertSlot",parentId:t.parentId,blockId:t.block.id,slot:s.getSlot(t.block.id),subtree:t.subtree??{blocks:[t.block],runs:[]}},this._notify([t.block.id,t.parentId]),a.removeBlock(t.block.id)}case a.OP.REMOVE_BLOCK:{const e=this.blocks.get(t.id),s=e.parentId,n=this.blocks.get(s),r=this._getOrder(s),o=r.toArray().indexOf(t.id),c=this._captureSubtree(t.id);this._deleteSubtree(t.id);const l=this._clock.tick();return r.delete(t.id,l),this.blocks.set(s,{...n,contentIds:r.toArray()}),this._lastEnvelope={kind:"deleteSlot",parentId:s,blockId:t.id,clock:l},this._notify([s]),{type:a.OP.INSERT_BLOCK,block:e,parentId:s,index:o,subtree:c}}case a.OP.MOVE_BLOCK:{const s=this.blocks.get(t.id).parentId,n=this._getOrder(s),r=n.toArray().indexOf(t.id);n.delete(t.id,this._clock.tick()),this.blocks.set(s,{...this.blocks.get(s),contentIds:n.toArray()});const o=this._getOrder(t.toParentId),c=o.toArray(),l=t.toIndex??c.length,u=l>0?c[l-1]:null,m=this._clock.tick();o.has(t.id)?o.move(t.id,u,m,this._peerId):o.insert(t.id,u,m,this._peerId),this.blocks.set(t.toParentId,{...this.blocks.get(t.toParentId),contentIds:o.toArray()}),this.blocks.set(t.id,{...this.blocks.get(t.id),parentId:t.toParentId}),this._lastEnvelope={kind:"moveSlot",fromParentId:s,toParentId:t.toParentId,blockId:t.id,slot:o.getSlot(t.id)};const f=new Set([t.id,s,t.toParentId]);return this._notify(f),a.moveBlock(t.id,s,r)}case a.OP.SET_BLOCK_CONTENT_IDS:{const e=this.blocks.get(t.blockId),s=e.contentIds;return this.blocks.set(t.blockId,{...e,contentIds:t.contentIds}),this._orders.set(t.blockId,S.ListCrdtState.fromArray(t.contentIds,{peerId:"legacy"})),this._lastEnvelope=null,this._notify([t.blockId]),a.setBlockContentIds(t.blockId,s)}case a.OP.REPLACE_RUN_SPAN:{const e=this.blocks.get(t.blockId),s=e.contentIds.indexOf(t.oldRunIds[0])!==-1,n=s?e.contentIds:((i=e.props)==null?void 0:i.titleRunIds)??[],r=n.indexOf(t.oldRunIds[0]),o=t.oldRunIds.map(l=>this.runs.get(l));for(const l of t.oldRunIds)this.runs.delete(l);for(const l of t.newRuns)this.runs.set(l.id,l);const c=[...n];return c.splice(r,t.oldRunIds.length,...t.newRuns.map(l=>l.id)),s?this.blocks.set(t.blockId,{...e,contentIds:c}):this.blocks.set(t.blockId,{...e,props:{...e.props,titleRunIds:c}}),this._lastEnvelope=null,this._notify([t.blockId,...t.oldRunIds,...t.newRuns.map(l=>l.id)]),a.replaceRunSpan(t.blockId,t.newRuns.map(l=>l.id),o)}case a.OP.SET_BLOCK_RUNS:{const e=this.blocks.get(t.blockId),s=e.props&&"titleRunIds"in e.props,n=s?e.props.titleRunIds:e.contentIds,r=n.map(c=>this.runs.get(c)).filter(Boolean);for(const c of n)this.runs.delete(c);for(const c of t.runs)this.runs.set(c.id,c);const o=t.runs.map(c=>c.id);return s?this.blocks.set(t.blockId,{...e,props:{...e.props,titleRunIds:o}}):this.blocks.set(t.blockId,{...e,contentIds:o}),this._lastEnvelope=null,this._notify([t.blockId,...n,...o]),a.setBlockRuns(t.blockId,r)}case a.OP.ADD_FIELD_TYPE:return this.fieldTypes.set(t.fieldType.id,t.fieldType),this._fieldTypesSnapshot=null,this._notify([B]),a.removeFieldType(t.fieldType.id);case a.OP.UPDATE_FIELD_TYPE:{const e=this.fieldTypes.get(t.id),s={};for(const n of Object.keys(t.patch))s[n]=e[n];return this.fieldTypes.set(t.id,{...e,...t.patch}),this._fieldTypesSnapshot=null,this._notify([B]),a.updateFieldType(t.id,s)}case a.OP.REMOVE_FIELD_TYPE:{const e=this.fieldTypes.get(t.id);return this.fieldTypes.delete(t.id),this._fieldTypesSnapshot=null,this._notify([B]),a.addFieldType(e)}case a.OP.ADD_COMMENT_THREAD:return this.comments.set(t.thread.id,t.thread),this._commentsSnapshot=null,this._lastEnvelope={kind:"commentWrite",op:"addThread",thread:t.thread},this._notify([x]),a.removeCommentThread(t.thread.id);case a.OP.REMOVE_COMMENT_THREAD:{const e=this.comments.get(t.commentId);return this.comments.delete(t.commentId),this._commentsSnapshot=null,this._lastEnvelope={kind:"commentWrite",op:"removeThread",commentId:t.commentId},this._notify([x]),a.addCommentThread(e)}case a.OP.ADD_COMMENT_REPLY:{const e=this.comments.get(t.commentId);return this.comments.set(t.commentId,{...e,messages:[...e.messages,t.message]}),this._commentsSnapshot=null,this._lastEnvelope={kind:"commentWrite",op:"addReply",commentId:t.commentId,message:t.message},this._notify([x]),a.removeCommentReply(t.commentId,t.message.id)}case a.OP.REMOVE_COMMENT_REPLY:{const e=this.comments.get(t.commentId),s=e.messages.find(n=>n.id===t.messageId);return this.comments.set(t.commentId,{...e,messages:e.messages.filter(n=>n.id!==t.messageId)}),this._commentsSnapshot=null,this._lastEnvelope={kind:"commentWrite",op:"removeReply",commentId:t.commentId,messageId:t.messageId},this._notify([x]),a.addCommentReply(t.commentId,s)}case a.OP.RESOLVE_COMMENT:{const e=this.comments.get(t.commentId),s=e.resolved;return this.comments.set(t.commentId,{...e,resolved:t.resolved}),this._commentsSnapshot=null,this._lastEnvelope={kind:"commentWrite",op:"resolve",commentId:t.commentId,resolved:t.resolved},this._notify([x]),a.resolveComment(t.commentId,s)}case a.OP.ADD_PERSON:return this.people.set(t.person.id,t.person),this._peopleSnapshot=null,this._lastEnvelope={kind:"peopleWrite",op:"add",person:t.person},this._notify([C]),a.removePerson(t.person.id);case a.OP.UPDATE_PERSON:{const e=this.people.get(t.id),s={};for(const n of Object.keys(t.patch))s[n]=e[n];return this.people.set(t.id,{...e,...t.patch}),this._peopleSnapshot=null,this._lastEnvelope={kind:"peopleWrite",op:"update",id:t.id,patch:t.patch},this._notify([C]),a.updatePerson(t.id,s)}case a.OP.REMOVE_PERSON:{const e=this.people.get(t.id);return this.people.delete(t.id),this._peopleSnapshot=null,this._lastEnvelope={kind:"peopleWrite",op:"remove",id:t.id},this._notify([C]),a.addPerson(e)}case a.OP.SET_TITLE:{const e=this.title,s=this._clock.tick();return this._fieldClocks.recordLocal("$doc","title",s),this.title=t.title,this._lastEnvelope={kind:"fieldWrite",target:"docTitle",title:t.title,clock:s},this._notify([H]),a.setTitle(e)}default:throw new Error(`Unknown operation type: ${t.type}`)}}applyOperations(t){return t.map(i=>this.applyOperation(i))}getLastEnvelope(){return this._lastEnvelope}applyRemoteOperation(t){var i,e;if(t)switch(t.kind){case"insertSlot":{for(const r of t.subtree.blocks)this.blocks.has(r.id)||this.blocks.set(r.id,r);for(const r of t.subtree.runs)this.runs.has(r.id)||this.runs.set(r.id,r);const s=this._getOrder(t.parentId);s.merge([t.slot]);const n=this.blocks.get(t.parentId);this.blocks.set(t.parentId,{...n,contentIds:s.toArray()}),this._clock.receive(t.slot.clock),this._notify([t.blockId,t.parentId]);return}case"deleteSlot":{const s=this._getOrder(t.parentId);this.blocks.has(t.blockId)&&this._deleteSubtree(t.blockId),s.delete(t.blockId,t.clock),t.clock&&this._clock.receive(t.clock);const n=this.blocks.get(t.parentId);this.blocks.set(t.parentId,{...n,contentIds:s.toArray()}),this._notify([t.parentId]);return}case"moveSlot":{const s=this._getOrder(t.fromParentId);s.delete(t.blockId,(i=t.slot)==null?void 0:i.clock),this.blocks.set(t.fromParentId,{...this.blocks.get(t.fromParentId),contentIds:s.toArray()});const n=this._getOrder(t.toParentId),r=n.has(t.blockId);n.merge([t.slot]),r&&n.move(t.blockId,t.slot.originId,t.slot.clock,t.slot.peerId),this.blocks.set(t.toParentId,{...this.blocks.get(t.toParentId),contentIds:n.toArray()}),this.blocks.has(t.blockId)&&this.blocks.set(t.blockId,{...this.blocks.get(t.blockId),parentId:t.toParentId}),this._clock.receive(t.slot.clock),this._notify([t.blockId,t.fromParentId,t.toParentId]);return}case"fieldWrite":{if(t.target==="run"){const s=this.runs.get(t.id);if(s){const n={};for(const r of Object.keys(t.patch)){const o=t.clocks[r];this._fieldClocks.shouldApplyRemote(t.id,r,o)&&(n[r]=t.patch[r],this._fieldClocks.recordRemote(t.id,r,o))}Object.keys(n).length>0&&(this.runs.set(t.id,{...s,...n}),this._notify([t.id]))}}else if(t.target==="blockProps"){const s=this.blocks.get(t.id);if(s){const n={};for(const r of Object.keys(t.patch)){const o=t.clocks[r];this._fieldClocks.shouldApplyRemote(t.id,`props.${r}`,o)&&(n[r]=t.patch[r],this._fieldClocks.recordRemote(t.id,`props.${r}`,o))}Object.keys(n).length>0&&(this.blocks.set(t.id,{...s,props:{...s.props,...n}}),this._notify([t.id]))}}else if(t.target==="blockType"){const s=this.blocks.get(t.id);s&&this._fieldClocks.shouldApplyRemote(t.id,"type",t.clock)&&(this._fieldClocks.recordRemote(t.id,"type",t.clock),this.blocks.set(t.id,{...s,type:t.blockType,props:t.props}),this._notify([t.id]))}else t.target==="docTitle"&&this._fieldClocks.shouldApplyRemote("$doc","title",t.clock)&&(this._fieldClocks.recordRemote("$doc","title",t.clock),this.title=t.title,this._notify([H]));for(const s of Object.values(t.clocks??{}))this._clock.receive(s);t.clock&&this._clock.receive(t.clock);return}case"runTextEdit":{const s=this._getRunOrder(t.runId),n=this._runChars.get(t.runId);for(const o of t.insertedChars)s.has(o.id)||n.set(o.id,o.char),s.insert(o.id,o.afterId,o.clock,(e=o.clock)==null?void 0:e.peerId);for(const o of t.deletedCharIds)s.delete(o.id,o.clock);this._runValueSnapshots.delete(t.runId);const r=this.runs.get(t.runId);r&&(this.runs.set(t.runId,{...r,value:this._materializeRunValue(t.runId)}),this._notify([t.runId])),this._clock.receive(t.clock);return}case"runCharEdit":{const s=this._getRunOrder(t.runId);for(const r of t.tombstoneIds)s.delete(r,t.clock);for(const r of t.restoreIds)s.restore(r);this._runValueSnapshots.delete(t.runId);const n=this.runs.get(t.runId);n&&(this.runs.set(t.runId,{...n,value:this._materializeRunValue(t.runId)}),this._notify([t.runId])),this._clock.receive(t.clock);return}case"commentWrite":{if(t.op==="addThread")this.comments.has(t.thread.id)||(this.comments.set(t.thread.id,t.thread),this._commentsSnapshot=null,this._notify([x]));else if(t.op==="removeThread")this.comments.delete(t.commentId)&&(this._commentsSnapshot=null,this._notify([x]));else if(t.op==="addReply"){const s=this.comments.get(t.commentId);s&&!s.messages.some(n=>n.id===t.message.id)&&(this.comments.set(t.commentId,{...s,messages:[...s.messages,t.message]}),this._commentsSnapshot=null,this._notify([x]))}else if(t.op==="removeReply"){const s=this.comments.get(t.commentId);s&&s.messages.some(n=>n.id===t.messageId)&&(this.comments.set(t.commentId,{...s,messages:s.messages.filter(n=>n.id!==t.messageId)}),this._commentsSnapshot=null,this._notify([x]))}else if(t.op==="resolve"){const s=this.comments.get(t.commentId);s&&(this.comments.set(t.commentId,{...s,resolved:t.resolved}),this._commentsSnapshot=null,this._notify([x]))}return}case"peopleWrite":{if(t.op==="add")this.people.has(t.person.id)||(this.people.set(t.person.id,t.person),this._peopleSnapshot=null,this._notify([C]));else if(t.op==="update"){const s=this.people.get(t.id);s&&(this.people.set(t.id,{...s,...t.patch}),this._peopleSnapshot=null,this._notify([C]))}else t.op==="remove"&&this.people.delete(t.id)&&(this._peopleSnapshot=null,this._notify([C]));return}default:throw new Error(`Unknown remote envelope kind: ${t.kind}`)}}getTombstoneCount(){let t=0;for(const i of this._orders.values())t+=i.tombstoneCount();for(const i of this._runOrders.values())t+=i.tombstoneCount();return t}pruneTombstones({maxAgeMs:t=24*60*60*1e3,now:i=Date.now()}={}){const e={wallTime:i-t,counter:0,peerId:""};let s=0;for(const n of this._orders.values())s+=n.pruneTombstones(e);for(const n of this._runOrders.values())s+=n.pruneTombstones(e);return s}toJSON(){return{blocks:[...this.blocks.values()],runs:[...this.runs.values()],rootId:this.rootId,fieldTypes:[...this.fieldTypes.values()],comments:[...this.comments.values()],people:[...this.people.values()],title:this.title}}static fromJSON(t){return new L(t)}}function tt(d,t){if(typeof d.performBatch=="function")d.performBatch(t);else for(const i of t)d.applyOperation(i)}function et(d,t){return{roots:t.map(i=>v.captureSubtree(d,i))}}function W(d,t,{parentId:i,index:e}){const s=[];let n=e;for(const r of t.roots){const{block:o,runs:c,subtreeBlocks:l}=v.remapSubtreeIds(r);o.parentId=i,s.push(a.insertBlock(o,i,n,{blocks:[o,...l],runs:c})),n+=1}tt(d,s)}function U(d,t){const i=d.store??d;i.blocks=new Map((t.blocks??[]).map(e=>[e.id,e])),i.runs=new Map((t.runs??[]).map(e=>[e.id,e])),i.rootId=t.rootId??null,i.fieldTypes=new Map((t.fieldTypes??[]).map(e=>[e.id,e])),i._fieldTypesSnapshot=null,i.comments=new Map((t.comments??[]).map(e=>[e.id,e])),i._commentsSnapshot=null,i._orders=new Map,i._notify([...i.blocks.keys(),...i.runs.keys(),"$fieldTypes","$comments"])}function st(d,t){for(const i of t)d.register(`__blockTemplate:${i.id}`,{component:()=>null,isLeaf:!0,slashCommand:{label:i.label,icon:i.icon,keywords:i.keywords??[i.label],run(e,{blockId:s,runId:n,sliceStart:r,sliceEnd:o}){const c=e.getRun(n),l=(c==null?void 0:c.value)??"";e.applyOperation(a.updateRun(n,{value:l.slice(0,r)+l.slice(o)}));const u=e.getBlock(s),f=e.getBlock(u.parentId).contentIds.indexOf(s)+1;W(e,i,{parentId:u.parentId,index:f})}}})}function Y(d,{pretty:t=!0}={}){const i=d.getRootId(),{blocks:e,runs:s}=v.captureSubtree(d,i);return JSON.stringify({version:1,rootId:i,blocks:e,runs:s},null,t?2:0)}function V(d,t,i){const e=d.getBlock(d.getRootId());return e?v.serializeBlockRange(d,t,e.contentIds,i).html:""}function nt(d,t,i){const e=d.getBlock(d.getRootId());return e?v.serializeBlockRange(d,t,e.contentIds,i).text:""}function it(d,t,i){return`<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">
1
+ "use strict";const h=require("react/jsx-runtime"),_=require("react"),Q=require("react-dom"),a=require("./icons-DQ7j_-ZK.cjs"),P=require("./indexedDbPersistence-BWJ1h5Vp.cjs"),v=require("./serialize-vLXz0cze.cjs"),S=require("./fieldRegistry-BoYyW3GH.cjs"),j=require("./CommentAvatar-V457IgoQ.cjs");function Z(d,t){const i=Array.from(d??""),e=Array.from(t??"");let s=0;const n=Math.min(i.length,e.length);for(;s<n&&i[s]===e[s];)s+=1;let r=i.length,o=e.length;for(;r>s&&o>s&&i[r-1]===e[o-1];)r-=1,o-=1;return{start:s,oldEnd:r,newEnd:o,oldChars:i,newChars:e}}const B="$fieldTypes",x="$comments",C="$people",H="$title";class L{constructor(t){this.blocks=new Map(((t==null?void 0:t.blocks)??[]).map(i=>[i.id,i])),this.runs=new Map(((t==null?void 0:t.runs)??[]).map(i=>[i.id,i])),this.rootId=(t==null?void 0:t.rootId)??null,this.fieldTypes=new Map(((t==null?void 0:t.fieldTypes)??[]).map(i=>[i.id,i])),this._fieldTypesSnapshot=null,this.comments=new Map(((t==null?void 0:t.comments)??[]).map(i=>[i.id,i])),this._commentsSnapshot=null,this.people=new Map(((t==null?void 0:t.people)??[]).map(i=>[i.id,i])),this._peopleSnapshot=null,this.title=(t==null?void 0:t.title)??"",this._listeners=new Map,this._globalListeners=new Set,this._peerId=S.genPeerId(),this._clock=new S.HLC(this._peerId),this._fieldClocks=new S.FieldClockRegistry,this._orders=new Map,this._runOrders=new Map,this._runChars=new Map,this._runValueSnapshots=new Map}_getOrder(t){let i=this._orders.get(t);if(!i){const e=this.blocks.get(t);i=S.ListCrdtState.fromArray((e==null?void 0:e.contentIds)??[],{peerId:"legacy"}),this._orders.set(t,i)}return i}_getRunOrder(t){let i=this._runOrders.get(t);if(!i){const e=this.runs.get(t),s=new Map;i=new S.ListCrdtState;let n=null,r=0;for(const o of Array.from((e==null?void 0:e.value)??"")){const c=`${t}:legacyChar:${r}`;s.set(c,o),i.insert(c,n,{wallTime:0,counter:0,peerId:"legacy"},"legacy"),n=c,r+=1}this._runOrders.set(t,i),this._runChars.set(t,s)}return i}_materializeRunValue(t){let i=this._runValueSnapshots.get(t);if(i===void 0){const e=this._getRunOrder(t),s=this._runChars.get(t);i=e.toArray().map(n=>s.get(n)).join(""),this._runValueSnapshots.set(t,i)}return i}_editRunText(t,i){const e=this._materializeRunValue(t),{start:s,oldEnd:n,newEnd:r,newChars:o}=Z(e,i),c=this._getRunOrder(t),l=this._runChars.get(t),u=c.toArray(),m=this._clock.tick(),f=[];for(let T=s;T<n;T+=1){const g=u[T];c.delete(g,m),f.push({id:g,clock:m})}const k=[];let w=s>0?u[s-1]:null;for(let T=s;T<r;T+=1){const g=a.genId(),I=o[T];l.set(g,I),c.insert(g,w,m,this._peerId),k.push({id:g,char:I,afterId:w,clock:m}),w=g}return this._runValueSnapshots.delete(t),{oldValue:e,insertedChars:k,deletedCharIds:f,clock:m}}_replayRunTextEdit(t,i){const e=this._getRunOrder(t),s=this._runChars.get(t);for(const n of i.insertedChars)e.has(n.id)?e.restore(n.id):(s.set(n.id,n.char),e.insert(n.id,n.afterId,n.clock,this._peerId));for(const n of i.deletedCharIds)e.delete(n.id,n.clock);this._runValueSnapshots.delete(t)}getBlock(t){return this.blocks.get(t)}getRun(t){return this.runs.get(t)}getFieldTypes(){return this._fieldTypesSnapshot||(this._fieldTypesSnapshot=[...this.fieldTypes.values()]),this._fieldTypesSnapshot}getFieldType(t){return this.fieldTypes.get(t)}getComments(){return this._commentsSnapshot||(this._commentsSnapshot=[...this.comments.values()]),this._commentsSnapshot}getComment(t){return this.comments.get(t)}getPeople(){return this._peopleSnapshot||(this._peopleSnapshot=[...this.people.values()]),this._peopleSnapshot}getPerson(t){return this.people.get(t)}getTitle(){return this.title}getAllRunIds(){return[...this.runs.keys()]}getRootId(){return this.rootId}subscribe(t,i){let e=this._listeners.get(t);return e||(e=new Set,this._listeners.set(t,e)),e.add(i),()=>{e.delete(i),e.size===0&&this._listeners.delete(t)}}subscribeAll(t){return this._globalListeners.add(t),()=>this._globalListeners.delete(t)}_notify(t){for(const i of t){const e=this._listeners.get(i);if(e)for(const s of[...e])s()}if(this._globalListeners.size>0)for(const i of[...this._globalListeners])i()}_captureSubtree(t){const i=[],e=[],s=n=>{var o;if(this.runs.has(n)){e.push(this.runs.get(n));return}const r=this.blocks.get(n);if(r){i.push(r);for(const c of r.contentIds)s(c);for(const c of((o=r.props)==null?void 0:o.titleRunIds)??[])s(c)}};return s(t),{blocks:i,runs:e}}_deleteSubtree(t){const i=e=>{var n;if(this.runs.has(e)){this.runs.delete(e);return}const s=this.blocks.get(e);if(s){for(const r of s.contentIds)i(r);for(const r of((n=s.props)==null?void 0:n.titleRunIds)??[])i(r);this.blocks.delete(e)}};i(t)}applyOperation(t){var i;switch(t.type){case a.OP.UPDATE_RUN:{const e=this.runs.get(t.id),s=Object.keys(t.patch);if(s.length===1&&s[0]==="value"){let o=t._charEdit;if(o)this._replayRunTextEdit(t.id,o);else{const{oldValue:l,insertedChars:u,deletedCharIds:m,clock:f}=this._editRunText(t.id,t.patch.value);o={oldValue:l,insertedChars:u,deletedCharIds:m,clock:f},t._charEdit=o}const c=this._materializeRunValue(t.id);return this.runs.set(t.id,{...e,value:c}),this._lastEnvelope={kind:"runTextEdit",runId:t.id,insertedChars:o.insertedChars,deletedCharIds:o.deletedCharIds,clock:o.clock},this._notify([t.id]),a.editRunChars(t.id,o.insertedChars.map(l=>l.id),o.deletedCharIds.map(l=>l.id),o.oldValue)}const n={},r={};for(const o of s)n[o]=e[o],r[o]=this._clock.tick(),this._fieldClocks.recordLocal(t.id,o,r[o]);return this.runs.set(t.id,{...e,...t.patch}),this._lastEnvelope={kind:"fieldWrite",target:"run",id:t.id,patch:t.patch,clocks:r},this._notify([t.id]),a.updateRun(t.id,n)}case a.OP.EDIT_RUN_CHARS:{const e=this._getRunOrder(t.id),s=this._clock.tick();for(const o of t.tombstoneIds)e.delete(o,s);for(const o of t.restoreIds)e.restore(o);this._runValueSnapshots.delete(t.id);const n=this.runs.get(t.id),r=this._materializeRunValue(t.id);return this.runs.set(t.id,{...n,value:r}),this._lastEnvelope={kind:"runCharEdit",runId:t.id,tombstoneIds:t.tombstoneIds,restoreIds:t.restoreIds,clock:s},this._notify([t.id]),a.editRunChars(t.id,t.restoreIds,t.tombstoneIds)}case a.OP.UPDATE_BLOCK_PROPS:{const e=this.blocks.get(t.id),s={},n={};for(const r of Object.keys(t.patch))s[r]=e.props[r],n[r]=this._clock.tick(),this._fieldClocks.recordLocal(t.id,`props.${r}`,n[r]);return this.blocks.set(t.id,{...e,props:{...e.props,...t.patch}}),this._lastEnvelope={kind:"fieldWrite",target:"blockProps",id:t.id,patch:t.patch,clocks:n},this._notify([t.id]),a.updateBlockProps(t.id,s)}case a.OP.CHANGE_BLOCK_TYPE:{const e=this.blocks.get(t.id),s=e.type,n=e.props,r=this._clock.tick();return this._fieldClocks.recordLocal(t.id,"type",r),this.blocks.set(t.id,{...e,type:t.blockType,props:t.props}),this._lastEnvelope={kind:"fieldWrite",target:"blockType",id:t.id,blockType:t.blockType,props:t.props,clock:r},this._notify([t.id]),a.changeBlockType(t.id,s,n)}case a.OP.INSERT_BLOCK:{if(t.subtree){for(const n of t.subtree.blocks)this.blocks.set(n.id,n);for(const n of t.subtree.runs)this.runs.set(n.id,n)}else this.blocks.set(t.block.id,t.block);const e=this.blocks.get(t.parentId),s=this._getOrder(t.parentId);if(s.has(t.block.id))s.restore(t.block.id);else{const n=s.toArray(),r=t.index??n.length,o=r>0?n[r-1]:null;s.insert(t.block.id,o,this._clock.tick(),this._peerId)}return this.blocks.set(t.parentId,{...e,contentIds:s.toArray()}),this._lastEnvelope={kind:"insertSlot",parentId:t.parentId,blockId:t.block.id,slot:s.getSlot(t.block.id),subtree:t.subtree??{blocks:[t.block],runs:[]}},this._notify([t.block.id,t.parentId]),a.removeBlock(t.block.id)}case a.OP.REMOVE_BLOCK:{const e=this.blocks.get(t.id),s=e.parentId,n=this.blocks.get(s),r=this._getOrder(s),o=r.toArray().indexOf(t.id),c=this._captureSubtree(t.id);this._deleteSubtree(t.id);const l=this._clock.tick();return r.delete(t.id,l),this.blocks.set(s,{...n,contentIds:r.toArray()}),this._lastEnvelope={kind:"deleteSlot",parentId:s,blockId:t.id,clock:l},this._notify([s]),{type:a.OP.INSERT_BLOCK,block:e,parentId:s,index:o,subtree:c}}case a.OP.MOVE_BLOCK:{const s=this.blocks.get(t.id).parentId,n=this._getOrder(s),r=n.toArray().indexOf(t.id);n.delete(t.id,this._clock.tick()),this.blocks.set(s,{...this.blocks.get(s),contentIds:n.toArray()});const o=this._getOrder(t.toParentId),c=o.toArray(),l=t.toIndex??c.length,u=l>0?c[l-1]:null,m=this._clock.tick();o.has(t.id)?o.move(t.id,u,m,this._peerId):o.insert(t.id,u,m,this._peerId),this.blocks.set(t.toParentId,{...this.blocks.get(t.toParentId),contentIds:o.toArray()}),this.blocks.set(t.id,{...this.blocks.get(t.id),parentId:t.toParentId}),this._lastEnvelope={kind:"moveSlot",fromParentId:s,toParentId:t.toParentId,blockId:t.id,slot:o.getSlot(t.id)};const f=new Set([t.id,s,t.toParentId]);return this._notify(f),a.moveBlock(t.id,s,r)}case a.OP.SET_BLOCK_CONTENT_IDS:{const e=this.blocks.get(t.blockId),s=e.contentIds;return this.blocks.set(t.blockId,{...e,contentIds:t.contentIds}),this._orders.set(t.blockId,S.ListCrdtState.fromArray(t.contentIds,{peerId:"legacy"})),this._lastEnvelope=null,this._notify([t.blockId]),a.setBlockContentIds(t.blockId,s)}case a.OP.REPLACE_RUN_SPAN:{const e=this.blocks.get(t.blockId),s=e.contentIds.indexOf(t.oldRunIds[0])!==-1,n=s?e.contentIds:((i=e.props)==null?void 0:i.titleRunIds)??[],r=n.indexOf(t.oldRunIds[0]),o=t.oldRunIds.map(l=>this.runs.get(l));for(const l of t.oldRunIds)this.runs.delete(l);for(const l of t.newRuns)this.runs.set(l.id,l);const c=[...n];return c.splice(r,t.oldRunIds.length,...t.newRuns.map(l=>l.id)),s?this.blocks.set(t.blockId,{...e,contentIds:c}):this.blocks.set(t.blockId,{...e,props:{...e.props,titleRunIds:c}}),this._lastEnvelope=null,this._notify([t.blockId,...t.oldRunIds,...t.newRuns.map(l=>l.id)]),a.replaceRunSpan(t.blockId,t.newRuns.map(l=>l.id),o)}case a.OP.SET_BLOCK_RUNS:{const e=this.blocks.get(t.blockId),s=e.props&&"titleRunIds"in e.props,n=s?e.props.titleRunIds:e.contentIds,r=n.map(c=>this.runs.get(c)).filter(Boolean);for(const c of n)this.runs.delete(c);for(const c of t.runs)this.runs.set(c.id,c);const o=t.runs.map(c=>c.id);return s?this.blocks.set(t.blockId,{...e,props:{...e.props,titleRunIds:o}}):this.blocks.set(t.blockId,{...e,contentIds:o}),this._lastEnvelope=null,this._notify([t.blockId,...n,...o]),a.setBlockRuns(t.blockId,r)}case a.OP.ADD_FIELD_TYPE:return this.fieldTypes.set(t.fieldType.id,t.fieldType),this._fieldTypesSnapshot=null,this._notify([B]),a.removeFieldType(t.fieldType.id);case a.OP.UPDATE_FIELD_TYPE:{const e=this.fieldTypes.get(t.id),s={};for(const n of Object.keys(t.patch))s[n]=e[n];return this.fieldTypes.set(t.id,{...e,...t.patch}),this._fieldTypesSnapshot=null,this._notify([B]),a.updateFieldType(t.id,s)}case a.OP.REMOVE_FIELD_TYPE:{const e=this.fieldTypes.get(t.id);return this.fieldTypes.delete(t.id),this._fieldTypesSnapshot=null,this._notify([B]),a.addFieldType(e)}case a.OP.ADD_COMMENT_THREAD:return this.comments.set(t.thread.id,t.thread),this._commentsSnapshot=null,this._lastEnvelope={kind:"commentWrite",op:"addThread",thread:t.thread},this._notify([x]),a.removeCommentThread(t.thread.id);case a.OP.REMOVE_COMMENT_THREAD:{const e=this.comments.get(t.commentId);return this.comments.delete(t.commentId),this._commentsSnapshot=null,this._lastEnvelope={kind:"commentWrite",op:"removeThread",commentId:t.commentId},this._notify([x]),a.addCommentThread(e)}case a.OP.ADD_COMMENT_REPLY:{const e=this.comments.get(t.commentId);return this.comments.set(t.commentId,{...e,messages:[...e.messages,t.message]}),this._commentsSnapshot=null,this._lastEnvelope={kind:"commentWrite",op:"addReply",commentId:t.commentId,message:t.message},this._notify([x]),a.removeCommentReply(t.commentId,t.message.id)}case a.OP.REMOVE_COMMENT_REPLY:{const e=this.comments.get(t.commentId),s=e.messages.find(n=>n.id===t.messageId);return this.comments.set(t.commentId,{...e,messages:e.messages.filter(n=>n.id!==t.messageId)}),this._commentsSnapshot=null,this._lastEnvelope={kind:"commentWrite",op:"removeReply",commentId:t.commentId,messageId:t.messageId},this._notify([x]),a.addCommentReply(t.commentId,s)}case a.OP.RESOLVE_COMMENT:{const e=this.comments.get(t.commentId),s=e.resolved;return this.comments.set(t.commentId,{...e,resolved:t.resolved}),this._commentsSnapshot=null,this._lastEnvelope={kind:"commentWrite",op:"resolve",commentId:t.commentId,resolved:t.resolved},this._notify([x]),a.resolveComment(t.commentId,s)}case a.OP.ADD_PERSON:return this.people.set(t.person.id,t.person),this._peopleSnapshot=null,this._lastEnvelope={kind:"peopleWrite",op:"add",person:t.person},this._notify([C]),a.removePerson(t.person.id);case a.OP.UPDATE_PERSON:{const e=this.people.get(t.id),s={};for(const n of Object.keys(t.patch))s[n]=e[n];return this.people.set(t.id,{...e,...t.patch}),this._peopleSnapshot=null,this._lastEnvelope={kind:"peopleWrite",op:"update",id:t.id,patch:t.patch},this._notify([C]),a.updatePerson(t.id,s)}case a.OP.REMOVE_PERSON:{const e=this.people.get(t.id);return this.people.delete(t.id),this._peopleSnapshot=null,this._lastEnvelope={kind:"peopleWrite",op:"remove",id:t.id},this._notify([C]),a.addPerson(e)}case a.OP.SET_TITLE:{const e=this.title,s=this._clock.tick();return this._fieldClocks.recordLocal("$doc","title",s),this.title=t.title,this._lastEnvelope={kind:"fieldWrite",target:"docTitle",title:t.title,clock:s},this._notify([H]),a.setTitle(e)}default:throw new Error(`Unknown operation type: ${t.type}`)}}applyOperations(t){return t.map(i=>this.applyOperation(i))}getLastEnvelope(){return this._lastEnvelope}applyRemoteOperation(t){var i,e;if(t)switch(t.kind){case"insertSlot":{for(const r of t.subtree.blocks)this.blocks.has(r.id)||this.blocks.set(r.id,r);for(const r of t.subtree.runs)this.runs.has(r.id)||this.runs.set(r.id,r);const s=this._getOrder(t.parentId);s.merge([t.slot]);const n=this.blocks.get(t.parentId);this.blocks.set(t.parentId,{...n,contentIds:s.toArray()}),this._clock.receive(t.slot.clock),this._notify([t.blockId,t.parentId]);return}case"deleteSlot":{const s=this._getOrder(t.parentId);this.blocks.has(t.blockId)&&this._deleteSubtree(t.blockId),s.delete(t.blockId,t.clock),t.clock&&this._clock.receive(t.clock);const n=this.blocks.get(t.parentId);this.blocks.set(t.parentId,{...n,contentIds:s.toArray()}),this._notify([t.parentId]);return}case"moveSlot":{const s=this._getOrder(t.fromParentId);s.delete(t.blockId,(i=t.slot)==null?void 0:i.clock),this.blocks.set(t.fromParentId,{...this.blocks.get(t.fromParentId),contentIds:s.toArray()});const n=this._getOrder(t.toParentId),r=n.has(t.blockId);n.merge([t.slot]),r&&n.move(t.blockId,t.slot.originId,t.slot.clock,t.slot.peerId),this.blocks.set(t.toParentId,{...this.blocks.get(t.toParentId),contentIds:n.toArray()}),this.blocks.has(t.blockId)&&this.blocks.set(t.blockId,{...this.blocks.get(t.blockId),parentId:t.toParentId}),this._clock.receive(t.slot.clock),this._notify([t.blockId,t.fromParentId,t.toParentId]);return}case"fieldWrite":{if(t.target==="run"){const s=this.runs.get(t.id);if(s){const n={};for(const r of Object.keys(t.patch)){const o=t.clocks[r];this._fieldClocks.shouldApplyRemote(t.id,r,o)&&(n[r]=t.patch[r],this._fieldClocks.recordRemote(t.id,r,o))}Object.keys(n).length>0&&(this.runs.set(t.id,{...s,...n}),this._notify([t.id]))}}else if(t.target==="blockProps"){const s=this.blocks.get(t.id);if(s){const n={};for(const r of Object.keys(t.patch)){const o=t.clocks[r];this._fieldClocks.shouldApplyRemote(t.id,`props.${r}`,o)&&(n[r]=t.patch[r],this._fieldClocks.recordRemote(t.id,`props.${r}`,o))}Object.keys(n).length>0&&(this.blocks.set(t.id,{...s,props:{...s.props,...n}}),this._notify([t.id]))}}else if(t.target==="blockType"){const s=this.blocks.get(t.id);s&&this._fieldClocks.shouldApplyRemote(t.id,"type",t.clock)&&(this._fieldClocks.recordRemote(t.id,"type",t.clock),this.blocks.set(t.id,{...s,type:t.blockType,props:t.props}),this._notify([t.id]))}else t.target==="docTitle"&&this._fieldClocks.shouldApplyRemote("$doc","title",t.clock)&&(this._fieldClocks.recordRemote("$doc","title",t.clock),this.title=t.title,this._notify([H]));for(const s of Object.values(t.clocks??{}))this._clock.receive(s);t.clock&&this._clock.receive(t.clock);return}case"runTextEdit":{const s=this._getRunOrder(t.runId),n=this._runChars.get(t.runId);for(const o of t.insertedChars)s.has(o.id)||n.set(o.id,o.char),s.insert(o.id,o.afterId,o.clock,(e=o.clock)==null?void 0:e.peerId);for(const o of t.deletedCharIds)s.delete(o.id,o.clock);this._runValueSnapshots.delete(t.runId);const r=this.runs.get(t.runId);r&&(this.runs.set(t.runId,{...r,value:this._materializeRunValue(t.runId)}),this._notify([t.runId])),this._clock.receive(t.clock);return}case"runCharEdit":{const s=this._getRunOrder(t.runId);for(const r of t.tombstoneIds)s.delete(r,t.clock);for(const r of t.restoreIds)s.restore(r);this._runValueSnapshots.delete(t.runId);const n=this.runs.get(t.runId);n&&(this.runs.set(t.runId,{...n,value:this._materializeRunValue(t.runId)}),this._notify([t.runId])),this._clock.receive(t.clock);return}case"commentWrite":{if(t.op==="addThread")this.comments.has(t.thread.id)||(this.comments.set(t.thread.id,t.thread),this._commentsSnapshot=null,this._notify([x]));else if(t.op==="removeThread")this.comments.delete(t.commentId)&&(this._commentsSnapshot=null,this._notify([x]));else if(t.op==="addReply"){const s=this.comments.get(t.commentId);s&&!s.messages.some(n=>n.id===t.message.id)&&(this.comments.set(t.commentId,{...s,messages:[...s.messages,t.message]}),this._commentsSnapshot=null,this._notify([x]))}else if(t.op==="removeReply"){const s=this.comments.get(t.commentId);s&&s.messages.some(n=>n.id===t.messageId)&&(this.comments.set(t.commentId,{...s,messages:s.messages.filter(n=>n.id!==t.messageId)}),this._commentsSnapshot=null,this._notify([x]))}else if(t.op==="resolve"){const s=this.comments.get(t.commentId);s&&(this.comments.set(t.commentId,{...s,resolved:t.resolved}),this._commentsSnapshot=null,this._notify([x]))}return}case"peopleWrite":{if(t.op==="add")this.people.has(t.person.id)||(this.people.set(t.person.id,t.person),this._peopleSnapshot=null,this._notify([C]));else if(t.op==="update"){const s=this.people.get(t.id);s&&(this.people.set(t.id,{...s,...t.patch}),this._peopleSnapshot=null,this._notify([C]))}else t.op==="remove"&&this.people.delete(t.id)&&(this._peopleSnapshot=null,this._notify([C]));return}default:throw new Error(`Unknown remote envelope kind: ${t.kind}`)}}getTombstoneCount(){let t=0;for(const i of this._orders.values())t+=i.tombstoneCount();for(const i of this._runOrders.values())t+=i.tombstoneCount();return t}pruneTombstones({maxAgeMs:t=24*60*60*1e3,now:i=Date.now()}={}){const e={wallTime:i-t,counter:0,peerId:""};let s=0;for(const n of this._orders.values())s+=n.pruneTombstones(e);for(const n of this._runOrders.values())s+=n.pruneTombstones(e);return s}toJSON(){return{blocks:[...this.blocks.values()],runs:[...this.runs.values()],rootId:this.rootId,fieldTypes:[...this.fieldTypes.values()],comments:[...this.comments.values()],people:[...this.people.values()],title:this.title}}static fromJSON(t){return new L(t)}}function tt(d,t){if(typeof d.performBatch=="function")d.performBatch(t);else for(const i of t)d.applyOperation(i)}function et(d,t){return{roots:t.map(i=>v.captureSubtree(d,i))}}function W(d,t,{parentId:i,index:e}){const s=[];let n=e;for(const r of t.roots){const{block:o,runs:c,subtreeBlocks:l}=v.remapSubtreeIds(r);o.parentId=i,s.push(a.insertBlock(o,i,n,{blocks:[o,...l],runs:c})),n+=1}tt(d,s)}function U(d,t){const i=d.store??d;i.blocks=new Map((t.blocks??[]).map(e=>[e.id,e])),i.runs=new Map((t.runs??[]).map(e=>[e.id,e])),i.rootId=t.rootId??null,i.fieldTypes=new Map((t.fieldTypes??[]).map(e=>[e.id,e])),i._fieldTypesSnapshot=null,i.comments=new Map((t.comments??[]).map(e=>[e.id,e])),i._commentsSnapshot=null,i._orders=new Map,i._notify([...i.blocks.keys(),...i.runs.keys(),"$fieldTypes","$comments"])}function st(d,t){for(const i of t)d.register(`__blockTemplate:${i.id}`,{component:()=>null,isLeaf:!0,slashCommand:{label:i.label,icon:i.icon,keywords:i.keywords??[i.label],run(e,{blockId:s,runId:n,sliceStart:r,sliceEnd:o}){const c=e.getRun(n),l=(c==null?void 0:c.value)??"";e.applyOperation(a.updateRun(n,{value:l.slice(0,r)+l.slice(o)}));const u=e.getBlock(s),f=e.getBlock(u.parentId).contentIds.indexOf(s)+1;W(e,i,{parentId:u.parentId,index:f})}}})}function Y(d,{pretty:t=!0}={}){const i=d.getRootId(),{blocks:e,runs:s}=v.captureSubtree(d,i);return JSON.stringify({version:1,rootId:i,blocks:e,runs:s},null,t?2:0)}function V(d,t,i){const e=d.getBlock(d.getRootId());return e?v.serializeBlockRange(d,t,e.contentIds,i).html:""}function nt(d,t,i){const e=d.getBlock(d.getRootId());return e?v.serializeBlockRange(d,t,e.contentIds,i).text:""}function it(d,t,i){return`<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">
2
2
  <head>
3
3
  <meta charset="utf-8">
4
4
  <title>Document</title>
@@ -9,4 +9,4 @@
9
9
  `:`
10
10
 
11
11
  `}r+=m,o=l}return r}const ot=5*60*1e3,ct=200;function K({store:d,docId:t,idleMs:i=ot,maxVersions:e=ct,onSnapshot:s,onError:n}){let r=null,o=d.getHistoryLog().length,c=!1;function l(){r&&(clearTimeout(r),r=null)}async function u(){if(l(),!c)return;c=!1;const f=d.getHistoryLog(),k=f.slice(o);if(o=f.length,k.length===0)return;const w=new Set(k.map(I=>I.id).filter(Boolean)),T=[...new Set(k.map(I=>I.actorId).filter(Boolean))],g=k[k.length-1].actorId??null;try{const I=JSON.parse(Y(d)),E={id:crypto.randomUUID(),docId:t,timestamp:Date.now(),authorId:g,authorIds:T,summary:`${w.size} block${w.size===1?"":"s"} changed`,doc:I};await P.saveDocumentVersion(E),s==null||s(E);const b=await P.listDocumentVersions(t);if(b.length>e){const O=b.slice(e);await Promise.all(O.map(N=>P.deleteDocumentVersion(N.id)))}}catch(I){n==null||n(I)}}const m=d.subscribeToHistory(()=>{c=!0,l(),r=setTimeout(u,i)});return{stop(){l(),m()},flush:u}}function lt(d,t){const i=$(d),e=$(t),s=i.length,n=e.length,r=Array.from({length:s+1},()=>new Uint32Array(n+1));for(let m=s-1;m>=0;m-=1)for(let f=n-1;f>=0;f-=1)r[m][f]=i[m]===e[f]?r[m+1][f+1]+1:Math.max(r[m+1][f],r[m][f+1]);const o=[],c=(m,f)=>{const k=o[o.length-1];k&&k.type===m?k.text+=f:o.push({type:m,text:f})};let l=0,u=0;for(;l<s&&u<n;)i[l]===e[u]?(c("equal",i[l]),l+=1,u+=1):r[l+1][u]>=r[l][u+1]?(c("removed",i[l]),l+=1):(c("added",e[u]),u+=1);for(;l<s;)c("removed",i[l]),l+=1;for(;u<n;)c("added",e[u]),u+=1;return o}function $(d){return(d??"").match(/\s+|[^\s]+/g)??[]}const dt={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"};function A(d){return d.replace(/[&<>"']/g,t=>dt[t])}function F(d){const t=new Map(d.blocks.map(r=>[r.id,r])),i=new Map(d.runs.map(r=>[r.id,r])),e=[],s=r=>r.map(o=>{var c;return((c=i.get(o))==null?void 0:c.value)??""}).join(""),n=r=>{var l;const o=t.get(r);if(!o)return;const c=(l=o.props)==null?void 0:l.titleRunIds;Array.isArray(c)&&c.length>0?e.push({id:r,text:s(c)}):o.contentIds.length>0&&o.contentIds.every(u=>i.has(u))&&e.push({id:r,text:s(o.contentIds)});for(const u of o.contentIds)i.has(u)||n(u)};return n(d.rootId),e}const z={rootId:"root",blocks:[{id:"root",type:"page",parentId:null,contentIds:[],props:{}}],runs:[]};function q(d,t){const i=F(d??z),e=F(t??z),s=new Map(i.map(o=>[o.id,o])),n=new Set(e.map(o=>o.id)),r=[];for(const o of e){const c=s.get(o.id);if(!c){const m=A(o.text)||"&nbsp;";r.push(`<p class="be-version-diff-block"><span class="be-version-diff-added">${m}</span></p>`);continue}if(c.text===o.text){r.push(`<p class="be-version-diff-block">${A(o.text)||"&nbsp;"}</p>`);continue}const u=lt(c.text,o.text).map(m=>{const f=A(m.text);return m.type==="added"?`<span class="be-version-diff-added">${f}</span>`:m.type==="removed"?`<span class="be-version-diff-removed">${f}</span>`:f}).join("");r.push(`<p class="be-version-diff-block">${u}</p>`)}for(const o of i)if(!n.has(o.id)){const c=A(o.text)||"&nbsp;";r.push(`<p class="be-version-diff-block"><span class="be-version-diff-removed">${c}</span></p>`)}return r.length===0?'<p class="be-version-diff-empty">No text changes.</p>':r.join("")}function J(d){const[t,i]=_.useState([]),[e,s]=_.useState(!1),n=_.useCallback(()=>d?(s(!1),P.listDocumentVersions(d).then(r=>{i(r),s(!0)})):Promise.resolve(),[d]);return _.useEffect(()=>{if(!d){i([]),s(!0);return}let r=!1;return s(!1),P.listDocumentVersions(d).then(o=>{r||(i(o),s(!0))}),()=>{r=!0}},[d]),{versions:t,isLoaded:e,refresh:n}}const at='button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])';function ht(d){const t=new Date(d),i=new Date,e=n=>new Date(n.getFullYear(),n.getMonth(),n.getDate()).getTime(),s=Math.round((e(i)-e(t))/(24*60*60*1e3));return s===0?"Today":s===1?"Yesterday":t.toLocaleDateString(void 0,{month:"long",day:"numeric",year:t.getFullYear()!==i.getFullYear()?"numeric":void 0})}function ut({docId:d,idleMs:t,maxVersions:i}){const e=a.useEditorStore(),s=a.useBlockRegistry(),n=a.useInlineRegistry(),{versions:r,isLoaded:o,refresh:c}=J(d),[l,u]=_.useState(!1),[m,f]=_.useState(null),[k,w]=_.useState("changes"),T=_.useRef(null),g=_.useRef(null);_.useEffect(()=>{if(typeof e.getHistoryLog!="function")return;const y=K({store:e,docId:d,idleMs:t,maxVersions:i,onSnapshot:c});return()=>y.stop()},[e,d]);function I(){u(!1),f(null),w("changes")}_.useEffect(()=>{if(!l)return;const y=p=>{p.key==="Escape"&&I()};return document.addEventListener("keydown",y),()=>document.removeEventListener("keydown",y)},[l]),_.useEffect(()=>{var y,p,R;if(l){g.current=document.activeElement;const M=T.current;(y=(M==null?void 0:M.querySelector(at))??M)==null||y.focus();return}(R=(p=g.current)==null?void 0:p.focus)==null||R.call(p),g.current=null},[l]);const E=r.findIndex(y=>y.id===m),b=E>=0?r[E]:null,O=E>=0?r[E+1]??null:null,N=_.useMemo(()=>{if(!b)return"";const y=new L(b.doc);return V(y,s,n)},[b,s,n]),X=_.useMemo(()=>b?q((O==null?void 0:O.doc)??null,b.doc):"",[b,O]);function G(){b&&window.confirm("Restore this version? This replaces the current document.")&&(U(e,b.doc),I())}const D=[];for(const y of r){const p=ht(y.timestamp);let R=D[D.length-1];(!R||R.label!==p)&&(R={label:p,versions:[]},D.push(R)),R.versions.push(y)}return h.jsxs(h.Fragment,{children:[h.jsxs("button",{type:"button",className:"be-version-history-trigger",onClick:()=>u(!0),title:"Version history",children:[h.jsx(a.ClockHistoryIcon,{size:15}),"Version history"]}),l&&Q.createPortal(h.jsxs(h.Fragment,{children:[h.jsx("div",{className:"be-version-history-backdrop",onClick:I}),h.jsxs("div",{ref:T,className:"be-version-history-drawer",role:"dialog","aria-modal":"true","aria-label":"Version history",tabIndex:-1,children:[h.jsxs("div",{className:"be-version-history-header",children:[h.jsx("span",{children:b?"Preview":"Version history"}),h.jsx("button",{type:"button",className:"be-version-history-close",onClick:I,"aria-label":"Close",children:h.jsx(a.XIcon,{size:14})})]}),b?h.jsxs("div",{className:"be-version-history-preview",children:[h.jsxs("div",{className:"be-version-history-preview-meta",children:[h.jsx(j.CommentAvatar,{authorId:b.authorId,size:24}),h.jsxs("div",{children:[h.jsx("div",{className:"be-version-history-preview-author",children:b.authorId??"Unknown"}),h.jsxs("div",{className:"be-version-history-preview-time",children:[new Date(b.timestamp).toLocaleString()," · ",b.summary]})]})]}),h.jsxs("div",{className:"be-version-history-view-toggle",role:"tablist","aria-label":"View mode",children:[h.jsx("button",{type:"button",role:"tab","aria-selected":k==="changes",className:k==="changes"?"is-active":"",onClick:()=>w("changes"),children:"Changes"}),h.jsx("button",{type:"button",role:"tab","aria-selected":k==="preview",className:k==="preview"?"is-active":"",onClick:()=>w("preview"),children:"Preview"})]}),k==="changes"&&h.jsxs(h.Fragment,{children:[h.jsxs("div",{className:"be-version-history-diff-legend",children:[h.jsx("span",{className:"be-version-diff-added",children:"Added"}),h.jsx("span",{className:"be-version-diff-removed",children:"Removed"}),!O&&h.jsx("span",{className:"be-version-history-diff-legend-note",children:"First saved version — everything shown is new."})]}),h.jsx("div",{className:"be-version-history-preview-body be-version-history-diff-body",dangerouslySetInnerHTML:{__html:X}})]}),k==="preview"&&h.jsx("div",{className:"be-version-history-preview-body",dangerouslySetInnerHTML:{__html:N}}),h.jsxs("div",{className:"be-version-history-preview-actions",children:[h.jsx("button",{type:"button",onClick:()=>{f(null),w("changes")},children:"Back"}),h.jsxs("button",{type:"button",className:"be-version-history-restore",onClick:G,children:[h.jsx(a.RestoreIcon,{size:13}),"Restore this version"]})]})]}):h.jsxs("div",{className:"be-version-history-list",children:[!o&&h.jsx("p",{className:"be-version-history-empty",children:"Loading…"}),o&&r.length===0&&h.jsx("p",{className:"be-version-history-empty",children:"No versions yet — one is saved automatically after you make some edits and pause."}),D.map(y=>h.jsxs("div",{className:"be-version-history-group",children:[h.jsx("div",{className:"be-version-history-group-label",children:y.label}),y.versions.map(p=>h.jsxs("button",{type:"button",className:"be-version-history-item",onClick:()=>f(p.id),children:[h.jsx(j.CommentAvatar,{authorId:p.authorId,size:24}),h.jsxs("div",{className:"be-version-history-item-body",children:[h.jsx("div",{className:"be-version-history-item-author",children:p.authorId??"Unknown"}),h.jsxs("div",{className:"be-version-history-item-meta",children:[j.formatRelativeTime(p.timestamp)," · ",p.summary]})]})]},p.id))]},y.label))]})]})]}),document.body)]})}exports.EditorStore=L;exports.VersionHistory=ut;exports.applyDocumentTemplate=U;exports.captureBlockTemplate=et;exports.createAutoVersionHistory=K;exports.diffDocumentsHTML=q;exports.exportDocumentHTML=V;exports.exportDocumentJSON=Y;exports.exportDocumentMarkdown=rt;exports.exportDocumentText=nt;exports.exportDocumentWordHTML=it;exports.insertBlockTemplate=W;exports.registerBlockTemplates=st;exports.useDocumentVersions=J;
12
- //# sourceMappingURL=VersionHistory-BRBYQgj5.cjs.map
12
+ //# sourceMappingURL=VersionHistory-BsSwOq_k.cjs.map