noteloom 0.4.0 → 0.5.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 (55) hide show
  1. package/README.md +162 -1026
  2. package/dist/canvas.cjs +1 -1
  3. package/dist/canvas.js +1 -1
  4. package/dist/comments.cjs +1 -1
  5. package/dist/comments.js +3 -3
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.d.ts +2 -1
  8. package/dist/index.js +19 -19
  9. package/dist/shared/{CommentThreadCard-Cxf8GwHJ.js → CommentThreadCard-CwKd6fp3.js} +2 -2
  10. package/dist/shared/{CommentThreadCard-Cxf8GwHJ.js.map → CommentThreadCard-CwKd6fp3.js.map} +1 -1
  11. package/dist/shared/{CommentThreadCard-CAJ3oPdr.cjs → CommentThreadCard-YziIa8My.cjs} +2 -2
  12. package/dist/shared/{CommentThreadCard-CAJ3oPdr.cjs.map → CommentThreadCard-YziIa8My.cjs.map} +1 -1
  13. package/dist/shared/{CommentsPanel-CES4uzye.cjs → CommentsPanel-DezejDdE.cjs} +2 -2
  14. package/dist/shared/{CommentsPanel-CES4uzye.cjs.map → CommentsPanel-DezejDdE.cjs.map} +1 -1
  15. package/dist/shared/{CommentsPanel-BwVVyMOC.js → CommentsPanel-dx7IaoqS.js} +3 -3
  16. package/dist/shared/{CommentsPanel-BwVVyMOC.js.map → CommentsPanel-dx7IaoqS.js.map} +1 -1
  17. package/dist/shared/{VersionHistory-Bg6fufP5.js → VersionHistory-DhYb8C2e.js} +3 -3
  18. package/dist/shared/{VersionHistory-Bg6fufP5.js.map → VersionHistory-DhYb8C2e.js.map} +1 -1
  19. package/dist/shared/{VersionHistory-DELa2Wbq.cjs → VersionHistory-DuSxis_7.cjs} +2 -2
  20. package/dist/shared/{VersionHistory-DELa2Wbq.cjs.map → VersionHistory-DuSxis_7.cjs.map} +1 -1
  21. package/dist/shared/{VoiceListeningIndicator-DNX8JlQM.cjs → VoiceListeningIndicator-BXByLZwO.cjs} +2 -2
  22. package/dist/shared/{VoiceListeningIndicator-DNX8JlQM.cjs.map → VoiceListeningIndicator-BXByLZwO.cjs.map} +1 -1
  23. package/dist/shared/{VoiceListeningIndicator-BOfVmLK0.js → VoiceListeningIndicator-bv285FSY.js} +4 -4
  24. package/dist/shared/{VoiceListeningIndicator-BOfVmLK0.js.map → VoiceListeningIndicator-bv285FSY.js.map} +1 -1
  25. package/dist/shared/{icons-DtCsXuU6.cjs → icons-BCseh2pt.cjs} +3 -3
  26. package/dist/shared/{icons-DtCsXuU6.cjs.map → icons-BCseh2pt.cjs.map} +1 -1
  27. package/dist/shared/{icons-C1_s0jKK.js → icons-D5Ctow8R.js} +249 -239
  28. package/dist/shared/{icons-C1_s0jKK.js.map → icons-D5Ctow8R.js.map} +1 -1
  29. package/dist/shared/{index-D9WEXpYM.cjs → index-CbIBsFmI.cjs} +2 -2
  30. package/dist/shared/{index-D9WEXpYM.cjs.map → index-CbIBsFmI.cjs.map} +1 -1
  31. package/dist/shared/{index-B9jyyL8u.js → index-D_8hJpI7.js} +3 -3
  32. package/dist/shared/{index-B9jyyL8u.js.map → index-D_8hJpI7.js.map} +1 -1
  33. package/dist/shared/{leafBlockFactory-Dp382eP8.cjs → leafBlockFactory-CaLWUcfa.cjs} +2 -2
  34. package/dist/shared/{leafBlockFactory-Dp382eP8.cjs.map → leafBlockFactory-CaLWUcfa.cjs.map} +1 -1
  35. package/dist/shared/{leafBlockFactory-A9eUhSe-.js → leafBlockFactory-DSa1XCox.js} +2 -2
  36. package/dist/shared/{leafBlockFactory-A9eUhSe-.js.map → leafBlockFactory-DSa1XCox.js.map} +1 -1
  37. package/dist/shared/{serialize-T9Odwx24.js → serialize-BqKU9EZy.js} +2 -2
  38. package/dist/shared/{serialize-T9Odwx24.js.map → serialize-BqKU9EZy.js.map} +1 -1
  39. package/dist/shared/{serialize-DWEfk0vU.cjs → serialize-CqwX55uK.cjs} +2 -2
  40. package/dist/shared/{serialize-DWEfk0vU.cjs.map → serialize-CqwX55uK.cjs.map} +1 -1
  41. package/dist/shared/{starter-kit-lPN-NpJ5.cjs → starter-kit-DyX1U84W.cjs} +2 -2
  42. package/dist/shared/{starter-kit-lPN-NpJ5.cjs.map → starter-kit-DyX1U84W.cjs.map} +1 -1
  43. package/dist/shared/{starter-kit-CY5wzYDf.js → starter-kit-mfeNxqFn.js} +7 -7
  44. package/dist/shared/{starter-kit-CY5wzYDf.js.map → starter-kit-mfeNxqFn.js.map} +1 -1
  45. package/dist/shared/{usePopoverEdgeClamp-CDG26jBM.cjs → usePopoverEdgeClamp-CPRWQtZV.cjs} +2 -2
  46. package/dist/shared/{usePopoverEdgeClamp-CDG26jBM.cjs.map → usePopoverEdgeClamp-CPRWQtZV.cjs.map} +1 -1
  47. package/dist/shared/{usePopoverEdgeClamp-gmFm0CnI.js → usePopoverEdgeClamp-D_hB5t60.js} +2 -2
  48. package/dist/shared/{usePopoverEdgeClamp-gmFm0CnI.js.map → usePopoverEdgeClamp-D_hB5t60.js.map} +1 -1
  49. package/dist/starter-kit.cjs +1 -1
  50. package/dist/starter-kit.js +2 -2
  51. package/dist/versions.cjs +1 -1
  52. package/dist/versions.js +1 -1
  53. package/dist/voice.cjs +1 -1
  54. package/dist/voice.js +1 -1
  55. package/package.json +1 -1
@@ -1,4 +1,4 @@
1
- "use strict";const h=require("react/jsx-runtime"),_=require("react"),G=require("react-dom"),a=require("./icons-DtCsXuU6.cjs"),P=require("./indexedDbPersistence-BWJ1h5Vp.cjs"),v=require("./serialize-DWEfk0vU.cjs"),E=require("./fieldRegistry-BoYyW3GH.cjs"),j=require("./CommentAvatar-V457IgoQ.cjs");function Q(l,t){const r=Array.from(l??""),e=Array.from(t??"");let s=0;const n=Math.min(r.length,e.length);for(;s<n&&r[s]===e[s];)s+=1;let i=r.length,o=e.length;for(;i>s&&o>s&&r[i-1]===e[o-1];)i-=1,o-=1;return{start:s,oldEnd:i,newEnd:o,oldChars:r,newChars:e}}const B="$fieldTypes",x="$comments",C="$people";class N{constructor(t){this.blocks=new Map(((t==null?void 0:t.blocks)??[]).map(r=>[r.id,r])),this.runs=new Map(((t==null?void 0:t.runs)??[]).map(r=>[r.id,r])),this.rootId=(t==null?void 0:t.rootId)??null,this.fieldTypes=new Map(((t==null?void 0:t.fieldTypes)??[]).map(r=>[r.id,r])),this._fieldTypesSnapshot=null,this.comments=new Map(((t==null?void 0:t.comments)??[]).map(r=>[r.id,r])),this._commentsSnapshot=null,this.people=new Map(((t==null?void 0:t.people)??[]).map(r=>[r.id,r])),this._peopleSnapshot=null,this._listeners=new Map,this._globalListeners=new Set,this._peerId=E.genPeerId(),this._clock=new E.HLC(this._peerId),this._fieldClocks=new E.FieldClockRegistry,this._orders=new Map,this._runOrders=new Map,this._runChars=new Map,this._runValueSnapshots=new Map}_getOrder(t){let r=this._orders.get(t);if(!r){const e=this.blocks.get(t);r=E.ListCrdtState.fromArray((e==null?void 0:e.contentIds)??[],{peerId:"legacy"}),this._orders.set(t,r)}return r}_getRunOrder(t){let r=this._runOrders.get(t);if(!r){const e=this.runs.get(t),s=new Map;r=new E.ListCrdtState;let n=null,i=0;for(const o of Array.from((e==null?void 0:e.value)??"")){const c=`${t}:legacyChar:${i}`;s.set(c,o),r.insert(c,n,{wallTime:0,counter:0,peerId:"legacy"},"legacy"),n=c,i+=1}this._runOrders.set(t,r),this._runChars.set(t,s)}return r}_materializeRunValue(t){let r=this._runValueSnapshots.get(t);if(r===void 0){const e=this._getRunOrder(t),s=this._runChars.get(t);r=e.toArray().map(n=>s.get(n)).join(""),this._runValueSnapshots.set(t,r)}return r}_editRunText(t,r){const e=this._materializeRunValue(t),{start:s,oldEnd:n,newEnd:i,newChars:o}=Q(e,r),c=this._getRunOrder(t),d=this._runChars.get(t),u=c.toArray(),m=this._clock.tick(),f=[];for(let w=s;w<n;w+=1){const g=u[w];c.delete(g,m),f.push({id:g,clock:m})}const k=[];let T=s>0?u[s-1]:null;for(let w=s;w<i;w+=1){const g=a.genId(),I=o[w];d.set(g,I),c.insert(g,T,m,this._peerId),k.push({id:g,char:I,afterId:T,clock:m}),T=g}return this._runValueSnapshots.delete(t),{oldValue:e,insertedChars:k,deletedCharIds:f,clock:m}}_replayRunTextEdit(t,r){const e=this._getRunOrder(t),s=this._runChars.get(t);for(const n of r.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 r.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)}getAllRunIds(){return[...this.runs.keys()]}getRootId(){return this.rootId}subscribe(t,r){let e=this._listeners.get(t);return e||(e=new Set,this._listeners.set(t,e)),e.add(r),()=>{e.delete(r),e.size===0&&this._listeners.delete(t)}}subscribeAll(t){return this._globalListeners.add(t),()=>this._globalListeners.delete(t)}_notify(t){for(const r of t){const e=this._listeners.get(r);if(e)for(const s of[...e])s()}if(this._globalListeners.size>0)for(const r of[...this._globalListeners])r()}_captureSubtree(t){const r=[],e=[],s=n=>{var o;if(this.runs.has(n)){e.push(this.runs.get(n));return}const i=this.blocks.get(n);if(i){r.push(i);for(const c of i.contentIds)s(c);for(const c of((o=i.props)==null?void 0:o.titleRunIds)??[])s(c)}};return s(t),{blocks:r,runs:e}}_deleteSubtree(t){const r=e=>{var n;if(this.runs.has(e)){this.runs.delete(e);return}const s=this.blocks.get(e);if(s){for(const i of s.contentIds)r(i);for(const i of((n=s.props)==null?void 0:n.titleRunIds)??[])r(i);this.blocks.delete(e)}};r(t)}applyOperation(t){var r;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:d,insertedChars:u,deletedCharIds:m,clock:f}=this._editRunText(t.id,t.patch.value);o={oldValue:d,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(d=>d.id),o.deletedCharIds.map(d=>d.id),o.oldValue)}const n={},i={};for(const o of s)n[o]=e[o],i[o]=this._clock.tick(),this._fieldClocks.recordLocal(t.id,o,i[o]);return this.runs.set(t.id,{...e,...t.patch}),this._lastEnvelope={kind:"fieldWrite",target:"run",id:t.id,patch:t.patch,clocks:i},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),i=this._materializeRunValue(t.id);return this.runs.set(t.id,{...n,value:i}),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 i of Object.keys(t.patch))s[i]=e.props[i],n[i]=this._clock.tick(),this._fieldClocks.recordLocal(t.id,`props.${i}`,n[i]);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,i=this._clock.tick();return this._fieldClocks.recordLocal(t.id,"type",i),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:i},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(),i=t.index??n.length,o=i>0?n[i-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),i=this._getOrder(s),o=i.toArray().indexOf(t.id),c=this._captureSubtree(t.id);this._deleteSubtree(t.id);const d=this._clock.tick();return i.delete(t.id,d),this.blocks.set(s,{...n,contentIds:i.toArray()}),this._lastEnvelope={kind:"deleteSlot",parentId:s,blockId:t.id,clock:d},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),i=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(),d=t.toIndex??c.length,u=d>0?c[d-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,i)}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,E.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:((r=e.props)==null?void 0:r.titleRunIds)??[],i=n.indexOf(t.oldRunIds[0]),o=t.oldRunIds.map(d=>this.runs.get(d));for(const d of t.oldRunIds)this.runs.delete(d);for(const d of t.newRuns)this.runs.set(d.id,d);const c=[...n];return c.splice(i,t.oldRunIds.length,...t.newRuns.map(d=>d.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(d=>d.id)]),a.replaceRunSpan(t.blockId,t.newRuns.map(d=>d.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,i=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,i)}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)}default:throw new Error(`Unknown operation type: ${t.type}`)}}applyOperations(t){return t.map(r=>this.applyOperation(r))}getLastEnvelope(){return this._lastEnvelope}applyRemoteOperation(t){var r,e;if(t)switch(t.kind){case"insertSlot":{for(const i of t.subtree.blocks)this.blocks.has(i.id)||this.blocks.set(i.id,i);for(const i of t.subtree.runs)this.runs.has(i.id)||this.runs.set(i.id,i);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,(r=t.slot)==null?void 0:r.clock),this.blocks.set(t.fromParentId,{...this.blocks.get(t.fromParentId),contentIds:s.toArray()});const n=this._getOrder(t.toParentId),i=n.has(t.blockId);n.merge([t.slot]),i&&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 i of Object.keys(t.patch)){const o=t.clocks[i];this._fieldClocks.shouldApplyRemote(t.id,i,o)&&(n[i]=t.patch[i],this._fieldClocks.recordRemote(t.id,i,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 i of Object.keys(t.patch)){const o=t.clocks[i];this._fieldClocks.shouldApplyRemote(t.id,`props.${i}`,o)&&(n[i]=t.patch[i],this._fieldClocks.recordRemote(t.id,`props.${i}`,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]))}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 i=this.runs.get(t.runId);i&&(this.runs.set(t.runId,{...i,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 i of t.tombstoneIds)s.delete(i,t.clock);for(const i of t.restoreIds)s.restore(i);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 r of this._orders.values())t+=r.tombstoneCount();for(const r of this._runOrders.values())t+=r.tombstoneCount();return t}pruneTombstones({maxAgeMs:t=24*60*60*1e3,now:r=Date.now()}={}){const e={wallTime:r-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()]}}static fromJSON(t){return new N(t)}}function Z(l,t){if(typeof l.performBatch=="function")l.performBatch(t);else for(const r of t)l.applyOperation(r)}function tt(l,t){return{roots:t.map(r=>v.captureSubtree(l,r))}}function z(l,t,{parentId:r,index:e}){const s=[];let n=e;for(const i of t.roots){const{block:o,runs:c,subtreeBlocks:d}=v.remapSubtreeIds(i);o.parentId=r,s.push(a.insertBlock(o,r,n,{blocks:[o,...d],runs:c})),n+=1}Z(l,s)}function W(l,t){const r=l.store??l;r.blocks=new Map((t.blocks??[]).map(e=>[e.id,e])),r.runs=new Map((t.runs??[]).map(e=>[e.id,e])),r.rootId=t.rootId??null,r.fieldTypes=new Map((t.fieldTypes??[]).map(e=>[e.id,e])),r._fieldTypesSnapshot=null,r.comments=new Map((t.comments??[]).map(e=>[e.id,e])),r._commentsSnapshot=null,r._orders=new Map,r._notify([...r.blocks.keys(),...r.runs.keys(),"$fieldTypes","$comments"])}function et(l,t){for(const r of t)l.register(`__blockTemplate:${r.id}`,{component:()=>null,isLeaf:!0,slashCommand:{label:r.label,icon:r.icon,keywords:r.keywords??[r.label],run(e,{blockId:s,runId:n,sliceStart:i,sliceEnd:o}){const c=e.getRun(n),d=(c==null?void 0:c.value)??"";e.applyOperation(a.updateRun(n,{value:d.slice(0,i)+d.slice(o)}));const u=e.getBlock(s),f=e.getBlock(u.parentId).contentIds.indexOf(s)+1;z(e,r,{parentId:u.parentId,index:f})}}})}function U(l,{pretty:t=!0}={}){const r=l.getRootId(),{blocks:e,runs:s}=v.captureSubtree(l,r);return JSON.stringify({version:1,rootId:r,blocks:e,runs:s},null,t?2:0)}function V(l,t,r){const e=l.getBlock(l.getRootId());return e?v.serializeBlockRange(l,t,e.contentIds,r).html:""}function st(l,t,r){const e=l.getBlock(l.getRootId());return e?v.serializeBlockRange(l,t,e.contentIds,r).text:""}function nt(l,t,r){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"),G=require("react-dom"),a=require("./icons-BCseh2pt.cjs"),P=require("./indexedDbPersistence-BWJ1h5Vp.cjs"),v=require("./serialize-CqwX55uK.cjs"),E=require("./fieldRegistry-BoYyW3GH.cjs"),j=require("./CommentAvatar-V457IgoQ.cjs");function Q(l,t){const r=Array.from(l??""),e=Array.from(t??"");let s=0;const n=Math.min(r.length,e.length);for(;s<n&&r[s]===e[s];)s+=1;let i=r.length,o=e.length;for(;i>s&&o>s&&r[i-1]===e[o-1];)i-=1,o-=1;return{start:s,oldEnd:i,newEnd:o,oldChars:r,newChars:e}}const B="$fieldTypes",x="$comments",C="$people";class N{constructor(t){this.blocks=new Map(((t==null?void 0:t.blocks)??[]).map(r=>[r.id,r])),this.runs=new Map(((t==null?void 0:t.runs)??[]).map(r=>[r.id,r])),this.rootId=(t==null?void 0:t.rootId)??null,this.fieldTypes=new Map(((t==null?void 0:t.fieldTypes)??[]).map(r=>[r.id,r])),this._fieldTypesSnapshot=null,this.comments=new Map(((t==null?void 0:t.comments)??[]).map(r=>[r.id,r])),this._commentsSnapshot=null,this.people=new Map(((t==null?void 0:t.people)??[]).map(r=>[r.id,r])),this._peopleSnapshot=null,this._listeners=new Map,this._globalListeners=new Set,this._peerId=E.genPeerId(),this._clock=new E.HLC(this._peerId),this._fieldClocks=new E.FieldClockRegistry,this._orders=new Map,this._runOrders=new Map,this._runChars=new Map,this._runValueSnapshots=new Map}_getOrder(t){let r=this._orders.get(t);if(!r){const e=this.blocks.get(t);r=E.ListCrdtState.fromArray((e==null?void 0:e.contentIds)??[],{peerId:"legacy"}),this._orders.set(t,r)}return r}_getRunOrder(t){let r=this._runOrders.get(t);if(!r){const e=this.runs.get(t),s=new Map;r=new E.ListCrdtState;let n=null,i=0;for(const o of Array.from((e==null?void 0:e.value)??"")){const c=`${t}:legacyChar:${i}`;s.set(c,o),r.insert(c,n,{wallTime:0,counter:0,peerId:"legacy"},"legacy"),n=c,i+=1}this._runOrders.set(t,r),this._runChars.set(t,s)}return r}_materializeRunValue(t){let r=this._runValueSnapshots.get(t);if(r===void 0){const e=this._getRunOrder(t),s=this._runChars.get(t);r=e.toArray().map(n=>s.get(n)).join(""),this._runValueSnapshots.set(t,r)}return r}_editRunText(t,r){const e=this._materializeRunValue(t),{start:s,oldEnd:n,newEnd:i,newChars:o}=Q(e,r),c=this._getRunOrder(t),d=this._runChars.get(t),u=c.toArray(),m=this._clock.tick(),f=[];for(let w=s;w<n;w+=1){const g=u[w];c.delete(g,m),f.push({id:g,clock:m})}const k=[];let T=s>0?u[s-1]:null;for(let w=s;w<i;w+=1){const g=a.genId(),I=o[w];d.set(g,I),c.insert(g,T,m,this._peerId),k.push({id:g,char:I,afterId:T,clock:m}),T=g}return this._runValueSnapshots.delete(t),{oldValue:e,insertedChars:k,deletedCharIds:f,clock:m}}_replayRunTextEdit(t,r){const e=this._getRunOrder(t),s=this._runChars.get(t);for(const n of r.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 r.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)}getAllRunIds(){return[...this.runs.keys()]}getRootId(){return this.rootId}subscribe(t,r){let e=this._listeners.get(t);return e||(e=new Set,this._listeners.set(t,e)),e.add(r),()=>{e.delete(r),e.size===0&&this._listeners.delete(t)}}subscribeAll(t){return this._globalListeners.add(t),()=>this._globalListeners.delete(t)}_notify(t){for(const r of t){const e=this._listeners.get(r);if(e)for(const s of[...e])s()}if(this._globalListeners.size>0)for(const r of[...this._globalListeners])r()}_captureSubtree(t){const r=[],e=[],s=n=>{var o;if(this.runs.has(n)){e.push(this.runs.get(n));return}const i=this.blocks.get(n);if(i){r.push(i);for(const c of i.contentIds)s(c);for(const c of((o=i.props)==null?void 0:o.titleRunIds)??[])s(c)}};return s(t),{blocks:r,runs:e}}_deleteSubtree(t){const r=e=>{var n;if(this.runs.has(e)){this.runs.delete(e);return}const s=this.blocks.get(e);if(s){for(const i of s.contentIds)r(i);for(const i of((n=s.props)==null?void 0:n.titleRunIds)??[])r(i);this.blocks.delete(e)}};r(t)}applyOperation(t){var r;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:d,insertedChars:u,deletedCharIds:m,clock:f}=this._editRunText(t.id,t.patch.value);o={oldValue:d,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(d=>d.id),o.deletedCharIds.map(d=>d.id),o.oldValue)}const n={},i={};for(const o of s)n[o]=e[o],i[o]=this._clock.tick(),this._fieldClocks.recordLocal(t.id,o,i[o]);return this.runs.set(t.id,{...e,...t.patch}),this._lastEnvelope={kind:"fieldWrite",target:"run",id:t.id,patch:t.patch,clocks:i},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),i=this._materializeRunValue(t.id);return this.runs.set(t.id,{...n,value:i}),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 i of Object.keys(t.patch))s[i]=e.props[i],n[i]=this._clock.tick(),this._fieldClocks.recordLocal(t.id,`props.${i}`,n[i]);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,i=this._clock.tick();return this._fieldClocks.recordLocal(t.id,"type",i),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:i},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(),i=t.index??n.length,o=i>0?n[i-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),i=this._getOrder(s),o=i.toArray().indexOf(t.id),c=this._captureSubtree(t.id);this._deleteSubtree(t.id);const d=this._clock.tick();return i.delete(t.id,d),this.blocks.set(s,{...n,contentIds:i.toArray()}),this._lastEnvelope={kind:"deleteSlot",parentId:s,blockId:t.id,clock:d},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),i=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(),d=t.toIndex??c.length,u=d>0?c[d-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,i)}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,E.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:((r=e.props)==null?void 0:r.titleRunIds)??[],i=n.indexOf(t.oldRunIds[0]),o=t.oldRunIds.map(d=>this.runs.get(d));for(const d of t.oldRunIds)this.runs.delete(d);for(const d of t.newRuns)this.runs.set(d.id,d);const c=[...n];return c.splice(i,t.oldRunIds.length,...t.newRuns.map(d=>d.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(d=>d.id)]),a.replaceRunSpan(t.blockId,t.newRuns.map(d=>d.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,i=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,i)}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)}default:throw new Error(`Unknown operation type: ${t.type}`)}}applyOperations(t){return t.map(r=>this.applyOperation(r))}getLastEnvelope(){return this._lastEnvelope}applyRemoteOperation(t){var r,e;if(t)switch(t.kind){case"insertSlot":{for(const i of t.subtree.blocks)this.blocks.has(i.id)||this.blocks.set(i.id,i);for(const i of t.subtree.runs)this.runs.has(i.id)||this.runs.set(i.id,i);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,(r=t.slot)==null?void 0:r.clock),this.blocks.set(t.fromParentId,{...this.blocks.get(t.fromParentId),contentIds:s.toArray()});const n=this._getOrder(t.toParentId),i=n.has(t.blockId);n.merge([t.slot]),i&&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 i of Object.keys(t.patch)){const o=t.clocks[i];this._fieldClocks.shouldApplyRemote(t.id,i,o)&&(n[i]=t.patch[i],this._fieldClocks.recordRemote(t.id,i,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 i of Object.keys(t.patch)){const o=t.clocks[i];this._fieldClocks.shouldApplyRemote(t.id,`props.${i}`,o)&&(n[i]=t.patch[i],this._fieldClocks.recordRemote(t.id,`props.${i}`,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]))}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 i=this.runs.get(t.runId);i&&(this.runs.set(t.runId,{...i,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 i of t.tombstoneIds)s.delete(i,t.clock);for(const i of t.restoreIds)s.restore(i);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 r of this._orders.values())t+=r.tombstoneCount();for(const r of this._runOrders.values())t+=r.tombstoneCount();return t}pruneTombstones({maxAgeMs:t=24*60*60*1e3,now:r=Date.now()}={}){const e={wallTime:r-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()]}}static fromJSON(t){return new N(t)}}function Z(l,t){if(typeof l.performBatch=="function")l.performBatch(t);else for(const r of t)l.applyOperation(r)}function tt(l,t){return{roots:t.map(r=>v.captureSubtree(l,r))}}function z(l,t,{parentId:r,index:e}){const s=[];let n=e;for(const i of t.roots){const{block:o,runs:c,subtreeBlocks:d}=v.remapSubtreeIds(i);o.parentId=r,s.push(a.insertBlock(o,r,n,{blocks:[o,...d],runs:c})),n+=1}Z(l,s)}function W(l,t){const r=l.store??l;r.blocks=new Map((t.blocks??[]).map(e=>[e.id,e])),r.runs=new Map((t.runs??[]).map(e=>[e.id,e])),r.rootId=t.rootId??null,r.fieldTypes=new Map((t.fieldTypes??[]).map(e=>[e.id,e])),r._fieldTypesSnapshot=null,r.comments=new Map((t.comments??[]).map(e=>[e.id,e])),r._commentsSnapshot=null,r._orders=new Map,r._notify([...r.blocks.keys(),...r.runs.keys(),"$fieldTypes","$comments"])}function et(l,t){for(const r of t)l.register(`__blockTemplate:${r.id}`,{component:()=>null,isLeaf:!0,slashCommand:{label:r.label,icon:r.icon,keywords:r.keywords??[r.label],run(e,{blockId:s,runId:n,sliceStart:i,sliceEnd:o}){const c=e.getRun(n),d=(c==null?void 0:c.value)??"";e.applyOperation(a.updateRun(n,{value:d.slice(0,i)+d.slice(o)}));const u=e.getBlock(s),f=e.getBlock(u.parentId).contentIds.indexOf(s)+1;z(e,r,{parentId:u.parentId,index:f})}}})}function U(l,{pretty:t=!0}={}){const r=l.getRootId(),{blocks:e,runs:s}=v.captureSubtree(l,r);return JSON.stringify({version:1,rootId:r,blocks:e,runs:s},null,t?2:0)}function V(l,t,r){const e=l.getBlock(l.getRootId());return e?v.serializeBlockRange(l,t,e.contentIds,r).html:""}function st(l,t,r){const e=l.getBlock(l.getRootId());return e?v.serializeBlockRange(l,t,e.contentIds,r).text:""}function nt(l,t,r){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
  `}i+=m,o=d}return i}const it=5*60*1e3,ot=200;function Y({store:l,docId:t,idleMs:r=it,maxVersions:e=ot,onSnapshot:s,onError:n}){let i=null,o=l.getHistoryLog().length,c=!1;function d(){i&&(clearTimeout(i),i=null)}async function u(){if(d(),!c)return;c=!1;const f=l.getHistoryLog(),k=f.slice(o);if(o=f.length,k.length===0)return;const T=new Set(k.map(I=>I.id).filter(Boolean)),w=[...new Set(k.map(I=>I.actorId).filter(Boolean))],g=k[k.length-1].actorId??null;try{const I=JSON.parse(U(l)),O={id:crypto.randomUUID(),docId:t,timestamp:Date.now(),authorId:g,authorIds:w,summary:`${T.size} block${T.size===1?"":"s"} changed`,doc:I};await P.saveDocumentVersion(O),s==null||s(O);const b=await P.listDocumentVersions(t);if(b.length>e){const S=b.slice(e);await Promise.all(S.map(L=>P.deleteDocumentVersion(L.id)))}}catch(I){n==null||n(I)}}const m=l.subscribeToHistory(()=>{c=!0,d(),i=setTimeout(u,r)});return{stop(){d(),m()},flush:u}}function ct(l,t){const r=H(l),e=H(t),s=r.length,n=e.length,i=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)i[m][f]=r[m]===e[f]?i[m+1][f+1]+1:Math.max(i[m+1][f],i[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 d=0,u=0;for(;d<s&&u<n;)r[d]===e[u]?(c("equal",r[d]),d+=1,u+=1):i[d+1][u]>=i[d][u+1]?(c("removed",r[d]),d+=1):(c("added",e[u]),u+=1);for(;d<s;)c("removed",r[d]),d+=1;for(;u<n;)c("added",e[u]),u+=1;return o}function H(l){return(l??"").match(/\s+|[^\s]+/g)??[]}const dt={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#39;"};function A(l){return l.replace(/[&<>"']/g,t=>dt[t])}function F(l){const t=new Map(l.blocks.map(i=>[i.id,i])),r=new Map(l.runs.map(i=>[i.id,i])),e=[],s=i=>i.map(o=>{var c;return((c=r.get(o))==null?void 0:c.value)??""}).join(""),n=i=>{var d;const o=t.get(i);if(!o)return;const c=(d=o.props)==null?void 0:d.titleRunIds;Array.isArray(c)&&c.length>0?e.push({id:i,text:s(c)}):o.contentIds.length>0&&o.contentIds.every(u=>r.has(u))&&e.push({id:i,text:s(o.contentIds)});for(const u of o.contentIds)r.has(u)||n(u)};return n(l.rootId),e}const $={rootId:"root",blocks:[{id:"root",type:"page",parentId:null,contentIds:[],props:{}}],runs:[]};function q(l,t){const r=F(l??$),e=F(t??$),s=new Map(r.map(o=>[o.id,o])),n=new Set(e.map(o=>o.id)),i=[];for(const o of e){const c=s.get(o.id);if(!c){const m=A(o.text)||"&nbsp;";i.push(`<p class="be-version-diff-block"><span class="be-version-diff-added">${m}</span></p>`);continue}if(c.text===o.text){i.push(`<p class="be-version-diff-block">${A(o.text)||"&nbsp;"}</p>`);continue}const u=ct(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("");i.push(`<p class="be-version-diff-block">${u}</p>`)}for(const o of r)if(!n.has(o.id)){const c=A(o.text)||"&nbsp;";i.push(`<p class="be-version-diff-block"><span class="be-version-diff-removed">${c}</span></p>`)}return i.length===0?'<p class="be-version-diff-empty">No text changes.</p>':i.join("")}function K(l){const[t,r]=_.useState([]),[e,s]=_.useState(!1),n=_.useCallback(()=>l?(s(!1),P.listDocumentVersions(l).then(i=>{r(i),s(!0)})):Promise.resolve(),[l]);return _.useEffect(()=>{if(!l){r([]),s(!0);return}let i=!1;return s(!1),P.listDocumentVersions(l).then(o=>{i||(r(o),s(!0))}),()=>{i=!0}},[l]),{versions:t,isLoaded:e,refresh:n}}const lt='button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])';function at(l){const t=new Date(l),r=new Date,e=n=>new Date(n.getFullYear(),n.getMonth(),n.getDate()).getTime(),s=Math.round((e(r)-e(t))/(24*60*60*1e3));return s===0?"Today":s===1?"Yesterday":t.toLocaleDateString(void 0,{month:"long",day:"numeric",year:t.getFullYear()!==r.getFullYear()?"numeric":void 0})}function ht({docId:l,idleMs:t,maxVersions:r}){const e=a.useEditorStore(),s=a.useBlockRegistry(),n=a.useInlineRegistry(),{versions:i,isLoaded:o,refresh:c}=K(l),[d,u]=_.useState(!1),[m,f]=_.useState(null),[k,T]=_.useState("changes"),w=_.useRef(null),g=_.useRef(null);_.useEffect(()=>{if(typeof e.getHistoryLog!="function")return;const p=Y({store:e,docId:l,idleMs:t,maxVersions:r,onSnapshot:c});return()=>p.stop()},[e,l]);function I(){u(!1),f(null),T("changes")}_.useEffect(()=>{if(!d)return;const p=y=>{y.key==="Escape"&&I()};return document.addEventListener("keydown",p),()=>document.removeEventListener("keydown",p)},[d]),_.useEffect(()=>{var p,y,R;if(d){g.current=document.activeElement;const M=w.current;(p=(M==null?void 0:M.querySelector(lt))??M)==null||p.focus();return}(R=(y=g.current)==null?void 0:y.focus)==null||R.call(y),g.current=null},[d]);const O=i.findIndex(p=>p.id===m),b=O>=0?i[O]:null,S=O>=0?i[O+1]??null:null,L=_.useMemo(()=>{if(!b)return"";const p=new N(b.doc);return V(p,s,n)},[b,s,n]),J=_.useMemo(()=>b?q((S==null?void 0:S.doc)??null,b.doc):"",[b,S]);function X(){b&&window.confirm("Restore this version? This replaces the current document.")&&(W(e,b.doc),I())}const D=[];for(const p of i){const y=at(p.timestamp);let R=D[D.length-1];(!R||R.label!==y)&&(R={label:y,versions:[]},D.push(R)),R.versions.push(p)}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"]}),d&&G.createPortal(h.jsxs(h.Fragment,{children:[h.jsx("div",{className:"be-version-history-backdrop",onClick:I}),h.jsxs("div",{ref:w,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:()=>T("changes"),children:"Changes"}),h.jsx("button",{type:"button",role:"tab","aria-selected":k==="preview",className:k==="preview"?"is-active":"",onClick:()=>T("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"}),!S&&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:J}})]}),k==="preview"&&h.jsx("div",{className:"be-version-history-preview-body",dangerouslySetInnerHTML:{__html:L}}),h.jsxs("div",{className:"be-version-history-preview-actions",children:[h.jsx("button",{type:"button",onClick:()=>{f(null),T("changes")},children:"Back"}),h.jsxs("button",{type:"button",className:"be-version-history-restore",onClick:X,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&&i.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(p=>h.jsxs("div",{className:"be-version-history-group",children:[h.jsx("div",{className:"be-version-history-group-label",children:p.label}),p.versions.map(y=>h.jsxs("button",{type:"button",className:"be-version-history-item",onClick:()=>f(y.id),children:[h.jsx(j.CommentAvatar,{authorId:y.authorId,size:24}),h.jsxs("div",{className:"be-version-history-item-body",children:[h.jsx("div",{className:"be-version-history-item-author",children:y.authorId??"Unknown"}),h.jsxs("div",{className:"be-version-history-item-meta",children:[j.formatRelativeTime(y.timestamp)," · ",y.summary]})]})]},y.id))]},p.label))]})]})]}),document.body)]})}exports.EditorStore=N;exports.VersionHistory=ht;exports.applyDocumentTemplate=W;exports.captureBlockTemplate=tt;exports.createAutoVersionHistory=Y;exports.diffDocumentsHTML=q;exports.exportDocumentHTML=V;exports.exportDocumentJSON=U;exports.exportDocumentMarkdown=rt;exports.exportDocumentText=st;exports.exportDocumentWordHTML=nt;exports.insertBlockTemplate=z;exports.registerBlockTemplates=et;exports.useDocumentVersions=K;
12
- //# sourceMappingURL=VersionHistory-DELa2Wbq.cjs.map
12
+ //# sourceMappingURL=VersionHistory-DuSxis_7.cjs.map