@blocknote/core 0.41.1 → 0.42.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 (81) hide show
  1. package/dist/BlockNoteSchema-Bi-eeHal.js +3288 -0
  2. package/dist/BlockNoteSchema-Bi-eeHal.js.map +1 -0
  3. package/dist/BlockNoteSchema-DjDaA2C3.cjs +6 -0
  4. package/dist/BlockNoteSchema-DjDaA2C3.cjs.map +1 -0
  5. package/dist/blockToNode-DIfPWLH8.js +1140 -0
  6. package/dist/blockToNode-DIfPWLH8.js.map +1 -0
  7. package/dist/blockToNode-w7H99R6p.cjs +7 -0
  8. package/dist/blockToNode-w7H99R6p.cjs.map +1 -0
  9. package/dist/blocknote.cjs +4 -4
  10. package/dist/blocknote.cjs.map +1 -1
  11. package/dist/blocknote.js +1413 -1366
  12. package/dist/blocknote.js.map +1 -1
  13. package/dist/blocks.cjs +1 -1
  14. package/dist/blocks.js +1 -1
  15. package/dist/comments.cjs +1 -1
  16. package/dist/comments.cjs.map +1 -1
  17. package/dist/comments.js +49 -49
  18. package/dist/comments.js.map +1 -1
  19. package/dist/en-Cl87Uuyf.cjs.map +1 -1
  20. package/dist/en-njEqD7AG.js.map +1 -1
  21. package/dist/locales.cjs.map +1 -1
  22. package/dist/locales.js.map +1 -1
  23. package/dist/tsconfig.tsbuildinfo +1 -0
  24. package/dist/webpack-stats.json +1 -1
  25. package/dist/yjs.cjs +2 -0
  26. package/dist/yjs.cjs.map +1 -0
  27. package/dist/yjs.js +44 -0
  28. package/dist/yjs.js.map +1 -0
  29. package/package.json +30 -25
  30. package/src/api/blockManipulation/commands/replaceBlocks/replaceBlocks.ts +19 -6
  31. package/src/api/blockManipulation/commands/replaceBlocks/util/fixColumnList.ts +173 -0
  32. package/src/api/nodeConversions/nodeToBlock.ts +17 -14
  33. package/src/blocks/Code/block.ts +1 -0
  34. package/src/blocks/ListItem/NumberedListItem/IndexingPlugin.ts +2 -1
  35. package/src/blocks/defaultBlockTypeGuards.ts +7 -18
  36. package/src/comments/threadstore/yjs/YjsThreadStoreBase.ts +3 -1
  37. package/src/editor/BlockNoteEditor.test.ts +70 -1
  38. package/src/editor/BlockNoteEditor.ts +55 -4
  39. package/src/editor/managers/ExportManager.ts +1 -1
  40. package/src/extensions/Collaboration/__snapshots__/fork-yjs-snap-editor-forked.json +2 -2
  41. package/src/extensions/Collaboration/__snapshots__/fork-yjs-snap-editor.json +2 -2
  42. package/src/extensions/Collaboration/__snapshots__/fork-yjs-snap-forked.html +1 -1
  43. package/src/extensions/Collaboration/__snapshots__/fork-yjs-snap.html +1 -1
  44. package/src/extensions/Collaboration/schemaMigration/SchemaMigrationPlugin.ts +10 -3
  45. package/src/extensions/Collaboration/schemaMigration/migrationRules/moveColorAttributes.test.ts +130 -0
  46. package/src/extensions/Collaboration/schemaMigration/migrationRules/moveColorAttributes.ts +34 -21
  47. package/src/extensions/Comments/CommentsPlugin.ts +37 -7
  48. package/src/extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts +30 -128
  49. package/src/extensions/SideMenu/SideMenuPlugin.ts +2 -0
  50. package/src/index.ts +1 -0
  51. package/src/schema/inlineContent/createSpec.ts +3 -0
  52. package/src/schema/inlineContent/types.ts +1 -0
  53. package/src/schema/schema.ts +49 -6
  54. package/src/schema/styles/createSpec.ts +6 -0
  55. package/src/schema/styles/types.ts +1 -0
  56. package/src/yjs/index.ts +1 -0
  57. package/src/yjs/utils.test.ts +1023 -0
  58. package/src/yjs/utils.ts +150 -0
  59. package/types/src/api/blockManipulation/commands/replaceBlocks/replaceBlocks.d.ts +1 -1
  60. package/types/src/api/blockManipulation/commands/replaceBlocks/util/fixColumnList.d.ts +32 -0
  61. package/types/src/api/nodeConversions/nodeToBlock.d.ts +1 -1
  62. package/types/src/editor/BlockNoteEditor.d.ts +6 -1
  63. package/types/src/editor/managers/ExportManager.d.ts +1 -1
  64. package/types/src/extensions/Collaboration/schemaMigration/migrationRules/moveColorAttributes.test.d.ts +1 -0
  65. package/types/src/extensions/Comments/CommentsPlugin.d.ts +1 -1
  66. package/types/src/index.d.ts +1 -0
  67. package/types/src/schema/inlineContent/createSpec.d.ts +1 -0
  68. package/types/src/schema/inlineContent/types.d.ts +1 -0
  69. package/types/src/schema/styles/createSpec.d.ts +1 -0
  70. package/types/src/schema/styles/types.d.ts +1 -0
  71. package/types/src/yjs/index.d.ts +1 -0
  72. package/types/src/yjs/utils.d.ts +55 -0
  73. package/types/src/yjs/utils.test.d.ts +1 -0
  74. package/dist/BlockNoteSchema-COA0fsXW.cjs +0 -11
  75. package/dist/BlockNoteSchema-COA0fsXW.cjs.map +0 -1
  76. package/dist/BlockNoteSchema-CYRHak18.js +0 -4375
  77. package/dist/BlockNoteSchema-CYRHak18.js.map +0 -1
  78. package/src/extensions/BackgroundColor/BackgroundColorMark.ts +0 -46
  79. package/src/extensions/TextColor/TextColorMark.ts +0 -38
  80. package/types/src/extensions/BackgroundColor/BackgroundColorMark.d.ts +0 -10
  81. package/types/src/extensions/TextColor/TextColorMark.d.ts +0 -10
package/dist/blocks.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./BlockNoteSchema-COA0fsXW.cjs");exports.BlockNoteSchema=e.BlockNoteSchema;exports.EMPTY_CELL_HEIGHT=e.EMPTY_CELL_HEIGHT;exports.EMPTY_CELL_WIDTH=e.EMPTY_CELL_WIDTH;exports.FILE_AUDIO_ICON_SVG=e.FILE_AUDIO_ICON_SVG;exports.FILE_IMAGE_ICON_SVG=e.FILE_IMAGE_ICON_SVG;exports.FILE_VIDEO_ICON_SVG=e.FILE_VIDEO_ICON_SVG;exports.addDefaultPropsExternalHTML=e.addDefaultPropsExternalHTML;exports.audioParse=e.audioParse;exports.audioRender=e.audioRender;exports.audioToExternalHTML=e.audioToExternalHTML;exports.blockHasType=e.blockHasType;exports.checkPageBreakBlocksInSchema=e.checkPageBreakBlocksInSchema;exports.createAudioBlockConfig=e.createAudioBlockConfig;exports.createAudioBlockSpec=e.createAudioBlockSpec;exports.createBulletListItemBlockConfig=e.createBulletListItemBlockConfig;exports.createBulletListItemBlockSpec=e.createBulletListItemBlockSpec;exports.createCheckListItemBlockSpec=e.createCheckListItemBlockSpec;exports.createCheckListItemConfig=e.createCheckListItemConfig;exports.createCodeBlockConfig=e.createCodeBlockConfig;exports.createCodeBlockSpec=e.createCodeBlockSpec;exports.createDefaultBlockDOMOutputSpec=e.createDefaultBlockDOMOutputSpec;exports.createDividerBlockConfig=e.createDividerBlockConfig;exports.createDividerBlockSpec=e.createDividerBlockSpec;exports.createFileBlockConfig=e.createFileBlockConfig;exports.createFileBlockSpec=e.createFileBlockSpec;exports.createHeadingBlockConfig=e.createHeadingBlockConfig;exports.createHeadingBlockSpec=e.createHeadingBlockSpec;exports.createImageBlockConfig=e.createImageBlockConfig;exports.createImageBlockSpec=e.createImageBlockSpec;exports.createNumberedListItemBlockConfig=e.createNumberedListItemBlockConfig;exports.createNumberedListItemBlockSpec=e.createNumberedListItemBlockSpec;exports.createPageBreakBlockConfig=e.createPageBreakBlockConfig;exports.createPageBreakBlockSpec=e.createPageBreakBlockSpec;exports.createParagraphBlockConfig=e.createParagraphBlockConfig;exports.createParagraphBlockSpec=e.createParagraphBlockSpec;exports.createQuoteBlockConfig=e.createQuoteBlockConfig;exports.createQuoteBlockSpec=e.createQuoteBlockSpec;exports.createTableBlockSpec=e.createTableBlockSpec;exports.createToggleListItemBlockConfig=e.createToggleListItemBlockConfig;exports.createToggleListItemBlockSpec=e.createToggleListItemBlockSpec;exports.createToggleWrapper=e.createToggleWrapper;exports.createVideoBlockConfig=e.createVideoBlockConfig;exports.createVideoBlockSpec=e.createVideoBlockSpec;exports.defaultBlockSpecs=e.defaultBlockSpecs;exports.defaultBlockToHTML=e.defaultBlockToHTML;exports.defaultInlineContentSchema=e.defaultInlineContentSchema;exports.defaultInlineContentSpecs=e.defaultInlineContentSpecs;exports.defaultProps=e.defaultProps;exports.defaultStyleSchema=e.defaultStyleSchema;exports.defaultStyleSpecs=e.defaultStyleSpecs;exports.defaultToggledState=e.defaultToggledState;exports.editorHasBlockWithType=e.editorHasBlockWithType;exports.fileParse=e.fileParse;exports.getBackgroundColorAttribute=e.getBackgroundColorAttribute;exports.getLanguageId=e.getLanguageId;exports.getPageBreakSlashMenuItems=e.getPageBreakSlashMenuItems;exports.getTextAlignmentAttribute=e.getTextAlignmentAttribute;exports.getTextColorAttribute=e.getTextColorAttribute;exports.imageParse=e.imageParse;exports.imageRender=e.imageRender;exports.imageToExternalHTML=e.imageToExternalHTML;exports.isTableCellSelection=e.isTableCellSelection;exports.mergeParagraphs=e.mergeParagraphs;exports.parseAudioElement=e.parseAudioElement;exports.parseDefaultProps=e.parseDefaultProps;exports.tablePropSchema=e.tablePropSchema;exports.uploadToTmpFilesDotOrg_DEV_ONLY=e.uploadToTmpFilesDotOrg_DEV_ONLY;exports.videoParse=e.videoParse;exports.withPageBreak=e.withPageBreak;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./BlockNoteSchema-DjDaA2C3.cjs");exports.BlockNoteSchema=e.BlockNoteSchema;exports.EMPTY_CELL_HEIGHT=e.EMPTY_CELL_HEIGHT;exports.EMPTY_CELL_WIDTH=e.EMPTY_CELL_WIDTH;exports.FILE_AUDIO_ICON_SVG=e.FILE_AUDIO_ICON_SVG;exports.FILE_IMAGE_ICON_SVG=e.FILE_IMAGE_ICON_SVG;exports.FILE_VIDEO_ICON_SVG=e.FILE_VIDEO_ICON_SVG;exports.addDefaultPropsExternalHTML=e.addDefaultPropsExternalHTML;exports.audioParse=e.audioParse;exports.audioRender=e.audioRender;exports.audioToExternalHTML=e.audioToExternalHTML;exports.blockHasType=e.blockHasType;exports.checkPageBreakBlocksInSchema=e.checkPageBreakBlocksInSchema;exports.createAudioBlockConfig=e.createAudioBlockConfig;exports.createAudioBlockSpec=e.createAudioBlockSpec;exports.createBulletListItemBlockConfig=e.createBulletListItemBlockConfig;exports.createBulletListItemBlockSpec=e.createBulletListItemBlockSpec;exports.createCheckListItemBlockSpec=e.createCheckListItemBlockSpec;exports.createCheckListItemConfig=e.createCheckListItemConfig;exports.createCodeBlockConfig=e.createCodeBlockConfig;exports.createCodeBlockSpec=e.createCodeBlockSpec;exports.createDefaultBlockDOMOutputSpec=e.createDefaultBlockDOMOutputSpec;exports.createDividerBlockConfig=e.createDividerBlockConfig;exports.createDividerBlockSpec=e.createDividerBlockSpec;exports.createFileBlockConfig=e.createFileBlockConfig;exports.createFileBlockSpec=e.createFileBlockSpec;exports.createHeadingBlockConfig=e.createHeadingBlockConfig;exports.createHeadingBlockSpec=e.createHeadingBlockSpec;exports.createImageBlockConfig=e.createImageBlockConfig;exports.createImageBlockSpec=e.createImageBlockSpec;exports.createNumberedListItemBlockConfig=e.createNumberedListItemBlockConfig;exports.createNumberedListItemBlockSpec=e.createNumberedListItemBlockSpec;exports.createPageBreakBlockConfig=e.createPageBreakBlockConfig;exports.createPageBreakBlockSpec=e.createPageBreakBlockSpec;exports.createParagraphBlockConfig=e.createParagraphBlockConfig;exports.createParagraphBlockSpec=e.createParagraphBlockSpec;exports.createQuoteBlockConfig=e.createQuoteBlockConfig;exports.createQuoteBlockSpec=e.createQuoteBlockSpec;exports.createTableBlockSpec=e.createTableBlockSpec;exports.createToggleListItemBlockConfig=e.createToggleListItemBlockConfig;exports.createToggleListItemBlockSpec=e.createToggleListItemBlockSpec;exports.createToggleWrapper=e.createToggleWrapper;exports.createVideoBlockConfig=e.createVideoBlockConfig;exports.createVideoBlockSpec=e.createVideoBlockSpec;exports.defaultBlockSpecs=e.defaultBlockSpecs;exports.defaultBlockToHTML=e.defaultBlockToHTML;exports.defaultInlineContentSchema=e.defaultInlineContentSchema;exports.defaultInlineContentSpecs=e.defaultInlineContentSpecs;exports.defaultProps=e.defaultProps;exports.defaultStyleSchema=e.defaultStyleSchema;exports.defaultStyleSpecs=e.defaultStyleSpecs;exports.defaultToggledState=e.defaultToggledState;exports.editorHasBlockWithType=e.editorHasBlockWithType;exports.fileParse=e.fileParse;exports.getBackgroundColorAttribute=e.getBackgroundColorAttribute;exports.getLanguageId=e.getLanguageId;exports.getPageBreakSlashMenuItems=e.getPageBreakSlashMenuItems;exports.getTextAlignmentAttribute=e.getTextAlignmentAttribute;exports.getTextColorAttribute=e.getTextColorAttribute;exports.imageParse=e.imageParse;exports.imageRender=e.imageRender;exports.imageToExternalHTML=e.imageToExternalHTML;exports.isTableCellSelection=e.isTableCellSelection;exports.mergeParagraphs=e.mergeParagraphs;exports.parseAudioElement=e.parseAudioElement;exports.parseDefaultProps=e.parseDefaultProps;exports.tablePropSchema=e.tablePropSchema;exports.uploadToTmpFilesDotOrg_DEV_ONLY=e.uploadToTmpFilesDotOrg_DEV_ONLY;exports.videoParse=e.videoParse;exports.withPageBreak=e.withPageBreak;
2
2
  //# sourceMappingURL=blocks.cjs.map
package/dist/blocks.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Z as t, E as c, a as s, F as o, r as l, Q as r, ab as i, b as g, d as k, e as B, a7 as n, X as p, c as d, f as u, x as S, y as C, A as f, z as m, g as I, h as T, _ as L, j as P, k as E, l as _, n as h, o as D, q as H, s as b, w as A, B as M, C as O, H as N, I as V, K as x, L as F, M as G, N as y, P as v, D as W, G as Y, V as Q, R, T as w, a1 as U, $ as j, a5 as q, a4 as z, a9 as J, a3 as K, a2 as X, U as Z, a6 as $, m as ee, ac as ae, i as te, Y as ce, ae as se, ad as oe, t as le, u as re, v as ie, a8 as ge, a0 as ke, p as Be, aa as ne, O as pe, W as de, S as ue, J as Se } from "./BlockNoteSchema-CYRHak18.js";
1
+ import { Z as t, E as c, a as s, F as o, r as l, Q as r, ab as i, b as g, d as k, e as B, a7 as n, X as p, c as d, f as u, x as S, y as C, A as f, z as m, g as I, h as T, _ as L, j as P, k as E, l as _, n as h, o as D, q as H, s as b, w as A, B as M, C as O, H as N, I as V, K as x, L as F, M as G, N as y, P as v, D as W, G as Y, V as Q, R, T as w, a1 as U, $ as j, a5 as q, a4 as z, a9 as J, a3 as K, a2 as X, U as Z, a6 as $, m as ee, ac as ae, i as te, Y as ce, ae as se, ad as oe, t as le, u as re, v as ie, a8 as ge, a0 as ke, p as Be, aa as ne, O as pe, W as de, S as ue, J as Se } from "./BlockNoteSchema-Bi-eeHal.js";
2
2
  export {
3
3
  t as BlockNoteSchema,
4
4
  c as EMPTY_CELL_HEIGHT,
package/dist/comments.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var A=Object.defineProperty;var y=(r,d,e)=>d in r?A(r,d,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[d]=e;var o=(r,d,e)=>y(r,typeof d!="symbol"?d+"":d,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const v=require("yjs"),g=require("uuid");function C(r){if(r&&typeof r=="object"&&"default"in r)return r;const d=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(r){for(const e in r)if(e!=="default"){const t=Object.getOwnPropertyDescriptor(r,e);Object.defineProperty(d,e,t.get?t:{enumerable:!0,get:()=>r[e]})}}return d.default=r,Object.freeze(d)}const T=C(v);class f{}class D extends f{constructor(d,e){super(),this.userId=d,this.role=e}canCreateThread(){return!0}canAddComment(d){return!0}canUpdateComment(d){return d.userId===this.userId}canDeleteComment(d){return d.userId===this.userId||this.role==="editor"}canDeleteThread(d){return this.role==="editor"}canResolveThread(d){return!0}canUnresolveThread(d){return!0}canAddReaction(d,e){return e?!d.reactions.some(t=>t.emoji===e&&t.userIds.includes(this.userId)):!0}canDeleteReaction(d,e){return e?d.reactions.some(t=>t.emoji===e&&t.userIds.includes(this.userId)):!0}}class I{constructor(d){o(this,"auth");this.auth=d}}class E extends I{constructor(e,t,a){super(a);o(this,"addThreadToDocument");this.userId=e,this.provider=t}async createThread(e){let t=this.provider.createThread({data:e.metadata});return t=this.provider.addComment(t.id,{content:e.initialComment.body,data:{metadata:e.initialComment.metadata,userId:this.userId}}),this.tiptapThreadToThreadData(t)}async addComment(e){const t=this.provider.addComment(e.threadId,{content:e.comment.body,data:{metadata:e.comment.metadata,userId:this.userId}});return this.tiptapCommentToCommentData(t.comments[t.comments.length-1])}async updateComment(e){const t=this.provider.getThreadComment(e.threadId,e.commentId,!0);if(!t)throw new Error("Comment not found");this.provider.updateComment(e.threadId,e.commentId,{content:e.comment.body,data:{...t.data,metadata:e.comment.metadata}})}tiptapCommentToCommentData(e){var a,s,n;const t=[];for(const h of((a=e.data)==null?void 0:a.reactions)||[]){const i=t.find(m=>m.emoji===h.emoji);i?(i.userIds.push(h.userId),i.createdAt=new Date(Math.min(i.createdAt.getTime(),h.createdAt))):t.push({emoji:h.emoji,createdAt:new Date(h.createdAt),userIds:[h.userId]})}return{type:"comment",id:e.id,body:e.content,metadata:(s=e.data)==null?void 0:s.metadata,userId:(n=e.data)==null?void 0:n.userId,createdAt:new Date(e.createdAt),updatedAt:new Date(e.updatedAt),reactions:t}}tiptapThreadToThreadData(e){var t;return{type:"thread",id:e.id,comments:e.comments.map(a=>this.tiptapCommentToCommentData(a)),resolved:!!e.resolvedAt,metadata:(t=e.data)==null?void 0:t.metadata,createdAt:new Date(e.createdAt),updatedAt:new Date(e.updatedAt)}}async deleteComment(e){this.provider.deleteComment(e.threadId,e.commentId)}async deleteThread(e){this.provider.deleteThread(e.threadId)}async resolveThread(e){this.provider.updateThread(e.threadId,{resolvedAt:new Date().toISOString()})}async unresolveThread(e){this.provider.updateThread(e.threadId,{resolvedAt:null})}async addReaction(e){var a;const t=this.provider.getThreadComment(e.threadId,e.commentId,!0);if(!t)throw new Error("Comment not found");this.provider.updateComment(e.threadId,e.commentId,{data:{...t.data,reactions:[...((a=t.data)==null?void 0:a.reactions)||[],{emoji:e.emoji,createdAt:Date.now(),userId:this.userId}]}})}async deleteReaction(e){var a;const t=this.provider.getThreadComment(e.threadId,e.commentId,!0);if(!t)throw new Error("Comment not found");this.provider.updateComment(e.threadId,e.commentId,{data:{...t.data,reactions:(((a=t.data)==null?void 0:a.reactions)||[]).filter(s=>s.emoji!==e.emoji&&s.userId!==this.userId)}})}getThread(e){const t=this.provider.getThread(e);if(!t)throw new Error("Thread not found");return this.tiptapThreadToThreadData(t)}getThreads(){return new Map(this.provider.getThreads().map(e=>[e.id,this.tiptapThreadToThreadData(e)]))}subscribe(e){const t=()=>{e(this.getThreads())};return this.provider.watchThreads(t),()=>{this.provider.unwatchThreads(t)}}}function p(r){const d=new T.Map;if(d.set("id",r.id),d.set("userId",r.userId),d.set("createdAt",r.createdAt.getTime()),d.set("updatedAt",r.updatedAt.getTime()),r.deletedAt?(d.set("deletedAt",r.deletedAt.getTime()),d.set("body",void 0)):d.set("body",r.body),r.reactions.length>0)throw new Error("Reactions should be empty in commentToYMap");return d.set("reactionsByUser",new T.Map),d.set("metadata",r.metadata),d}function b(r){var t;const d=new T.Map;d.set("id",r.id),d.set("createdAt",r.createdAt.getTime()),d.set("updatedAt",r.updatedAt.getTime());const e=new T.Array;return e.push(r.comments.map(a=>p(a))),d.set("comments",e),d.set("resolved",r.resolved),d.set("resolvedUpdatedAt",(t=r.resolvedUpdatedAt)==null?void 0:t.getTime()),d.set("resolvedBy",r.resolvedBy),d.set("metadata",r.metadata),d}function j(r){return{emoji:r.get("emoji"),createdAt:new Date(r.get("createdAt")),userId:r.get("userId")}}function R(r){return[...r.values()].map(e=>j(e)).reduce((e,t)=>{const a=e.find(s=>s.emoji===t.emoji);return a?(a.userIds.push(t.userId),a.createdAt=new Date(Math.min(a.createdAt.getTime(),t.createdAt.getTime()))):e.push({emoji:t.emoji,createdAt:t.createdAt,userIds:[t.userId]}),e},[])}function u(r){return{type:"comment",id:r.get("id"),userId:r.get("userId"),createdAt:new Date(r.get("createdAt")),updatedAt:new Date(r.get("updatedAt")),deletedAt:r.get("deletedAt")?new Date(r.get("deletedAt")):void 0,reactions:R(r.get("reactionsByUser")),metadata:r.get("metadata"),body:r.get("body")}}function c(r){return{type:"thread",id:r.get("id"),createdAt:new Date(r.get("createdAt")),updatedAt:new Date(r.get("updatedAt")),comments:(r.get("comments")||[]).map(d=>u(d)),resolved:r.get("resolved"),resolvedUpdatedAt:new Date(r.get("resolvedUpdatedAt")),resolvedBy:r.get("resolvedBy"),metadata:r.get("metadata")}}class w extends I{constructor(d,e){super(e),this.threadsYMap=d}getThread(d){const e=this.threadsYMap.get(d);if(!e)throw new Error("Thread not found");return c(e)}getThreads(){const d=new Map;return this.threadsYMap.forEach((e,t)=>{d.set(t,c(e))}),d}subscribe(d){const e=()=>{d(this.getThreads())};return this.threadsYMap.observeDeep(e),()=>{this.threadsYMap.unobserveDeep(e)}}}class S extends w{constructor(e,t,a,s){super(a,s);o(this,"doRequest",async(e,t,a)=>{const s=await fetch(`${this.BASE_URL}${e}`,{method:t,body:JSON.stringify(a),headers:{"Content-Type":"application/json",...this.headers}});if(!s.ok)throw new Error(`Failed to ${t} ${e}: ${s.statusText}`);return s.json()});o(this,"addThreadToDocument",async e=>{const{threadId:t,...a}=e;return this.doRequest(`/${t}/addToDocument`,"POST",a)});o(this,"createThread",async e=>this.doRequest("","POST",e));o(this,"addComment",e=>{const{threadId:t,...a}=e;return this.doRequest(`/${t}/comments`,"POST",a)});o(this,"updateComment",e=>{const{threadId:t,commentId:a,...s}=e;return this.doRequest(`/${t}/comments/${a}`,"PUT",s)});o(this,"deleteComment",e=>{const{threadId:t,commentId:a,...s}=e;return this.doRequest(`/${t}/comments/${a}?soft=${!!s.softDelete}`,"DELETE")});o(this,"deleteThread",e=>this.doRequest(`/${e.threadId}`,"DELETE"));o(this,"resolveThread",e=>this.doRequest(`/${e.threadId}/resolve`,"POST"));o(this,"unresolveThread",e=>this.doRequest(`/${e.threadId}/unresolve`,"POST"));o(this,"addReaction",e=>{const{threadId:t,commentId:a,...s}=e;return this.doRequest(`/${t}/comments/${a}/reactions`,"POST",s)});o(this,"deleteReaction",e=>this.doRequest(`/${e.threadId}/comments/${e.commentId}/reactions/${e.emoji}`,"DELETE"));this.BASE_URL=e,this.headers=t}}class Y extends w{constructor(e,t,a){super(t,a);o(this,"transact",e=>async t=>this.threadsYMap.doc.transact(()=>e(t)));o(this,"createThread",this.transact(e=>{if(!this.auth.canCreateThread())throw new Error("Not authorized");const t=new Date,a={type:"comment",id:g.v4(),userId:this.userId,createdAt:t,updatedAt:t,reactions:[],metadata:e.initialComment.metadata,body:e.initialComment.body},s={type:"thread",id:g.v4(),createdAt:t,updatedAt:t,comments:[a],resolved:!1,metadata:e.metadata};return this.threadsYMap.set(s.id,b(s)),s}));o(this,"addThreadToDocument");o(this,"addComment",this.transact(e=>{const t=this.threadsYMap.get(e.threadId);if(!t)throw new Error("Thread not found");if(!this.auth.canAddComment(c(t)))throw new Error("Not authorized");const a=new Date,s={type:"comment",id:g.v4(),userId:this.userId,createdAt:a,updatedAt:a,deletedAt:void 0,reactions:[],metadata:e.comment.metadata,body:e.comment.body};return t.get("comments").push([p(s)]),t.set("updatedAt",new Date().getTime()),s}));o(this,"updateComment",this.transact(e=>{const t=this.threadsYMap.get(e.threadId);if(!t)throw new Error("Thread not found");const a=l(t.get("comments"),n=>n.get("id")===e.commentId);if(a===-1)throw new Error("Comment not found");const s=t.get("comments").get(a);if(!this.auth.canUpdateComment(u(s)))throw new Error("Not authorized");s.set("body",e.comment.body),s.set("updatedAt",new Date().getTime()),s.set("metadata",e.comment.metadata)}));o(this,"deleteComment",this.transact(e=>{const t=this.threadsYMap.get(e.threadId);if(!t)throw new Error("Thread not found");const a=l(t.get("comments"),n=>n.get("id")===e.commentId);if(a===-1)throw new Error("Comment not found");const s=t.get("comments").get(a);if(!this.auth.canDeleteComment(u(s)))throw new Error("Not authorized");if(s.get("deletedAt"))throw new Error("Comment already deleted");e.softDelete?(s.set("deletedAt",new Date().getTime()),s.set("body",void 0)):t.get("comments").delete(a),t.get("comments").toArray().every(n=>n.get("deletedAt"))&&(e.softDelete?t.set("deletedAt",new Date().getTime()):this.threadsYMap.delete(e.threadId)),t.set("updatedAt",new Date().getTime())}));o(this,"deleteThread",this.transact(e=>{if(!this.auth.canDeleteThread(c(this.threadsYMap.get(e.threadId))))throw new Error("Not authorized");this.threadsYMap.delete(e.threadId)}));o(this,"resolveThread",this.transact(e=>{const t=this.threadsYMap.get(e.threadId);if(!t)throw new Error("Thread not found");if(!this.auth.canResolveThread(c(t)))throw new Error("Not authorized");t.set("resolved",!0),t.set("resolvedUpdatedAt",new Date().getTime()),t.set("resolvedBy",this.userId)}));o(this,"unresolveThread",this.transact(e=>{const t=this.threadsYMap.get(e.threadId);if(!t)throw new Error("Thread not found");if(!this.auth.canUnresolveThread(c(t)))throw new Error("Not authorized");t.set("resolved",!1),t.set("resolvedUpdatedAt",new Date().getTime())}));o(this,"addReaction",this.transact(e=>{const t=this.threadsYMap.get(e.threadId);if(!t)throw new Error("Thread not found");const a=l(t.get("comments"),m=>m.get("id")===e.commentId);if(a===-1)throw new Error("Comment not found");const s=t.get("comments").get(a);if(!this.auth.canAddReaction(u(s),e.emoji))throw new Error("Not authorized");const n=new Date,h=`${this.userId}-${e.emoji}`,i=s.get("reactionsByUser");if(!i.has(h)){const m=new T.Map;m.set("emoji",e.emoji),m.set("createdAt",n.getTime()),m.set("userId",this.userId),i.set(h,m)}}));o(this,"deleteReaction",this.transact(e=>{const t=this.threadsYMap.get(e.threadId);if(!t)throw new Error("Thread not found");const a=l(t.get("comments"),i=>i.get("id")===e.commentId);if(a===-1)throw new Error("Comment not found");const s=t.get("comments").get(a);if(!this.auth.canDeleteReaction(u(s),e.emoji))throw new Error("Not authorized");const n=`${this.userId}-${e.emoji}`;s.get("reactionsByUser").delete(n)}));this.userId=e}}function l(r,d){for(let e=0;e<r.length;e++)if(d(r.get(e)))return e;return-1}exports.DefaultThreadStoreAuth=D;exports.RESTYjsThreadStore=S;exports.ThreadStore=I;exports.ThreadStoreAuth=f;exports.TiptapThreadStore=E;exports.YjsThreadStore=Y;exports.YjsThreadStoreBase=w;
1
+ "use strict";var A=Object.defineProperty;var y=(r,d,e)=>d in r?A(r,d,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[d]=e;var o=(r,d,e)=>y(r,typeof d!="symbol"?d+"":d,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const v=require("yjs"),g=require("uuid");function C(r){if(r&&typeof r=="object"&&"default"in r)return r;const d=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(r){for(const e in r)if(e!=="default"){const t=Object.getOwnPropertyDescriptor(r,e);Object.defineProperty(d,e,t.get?t:{enumerable:!0,get:()=>r[e]})}}return d.default=r,Object.freeze(d)}const u=C(v);class w{}class D extends w{constructor(d,e){super(),this.userId=d,this.role=e}canCreateThread(){return!0}canAddComment(d){return!0}canUpdateComment(d){return d.userId===this.userId}canDeleteComment(d){return d.userId===this.userId||this.role==="editor"}canDeleteThread(d){return this.role==="editor"}canResolveThread(d){return!0}canUnresolveThread(d){return!0}canAddReaction(d,e){return e?!d.reactions.some(t=>t.emoji===e&&t.userIds.includes(this.userId)):!0}canDeleteReaction(d,e){return e?d.reactions.some(t=>t.emoji===e&&t.userIds.includes(this.userId)):!0}}class I{constructor(d){o(this,"auth");this.auth=d}}class E extends I{constructor(e,t,a){super(a);o(this,"addThreadToDocument");this.userId=e,this.provider=t}async createThread(e){let t=this.provider.createThread({data:e.metadata});return t=this.provider.addComment(t.id,{content:e.initialComment.body,data:{metadata:e.initialComment.metadata,userId:this.userId}}),this.tiptapThreadToThreadData(t)}async addComment(e){const t=this.provider.addComment(e.threadId,{content:e.comment.body,data:{metadata:e.comment.metadata,userId:this.userId}});return this.tiptapCommentToCommentData(t.comments[t.comments.length-1])}async updateComment(e){const t=this.provider.getThreadComment(e.threadId,e.commentId,!0);if(!t)throw new Error("Comment not found");this.provider.updateComment(e.threadId,e.commentId,{content:e.comment.body,data:{...t.data,metadata:e.comment.metadata}})}tiptapCommentToCommentData(e){var a,s,n;const t=[];for(const h of((a=e.data)==null?void 0:a.reactions)||[]){const i=t.find(m=>m.emoji===h.emoji);i?(i.userIds.push(h.userId),i.createdAt=new Date(Math.min(i.createdAt.getTime(),h.createdAt))):t.push({emoji:h.emoji,createdAt:new Date(h.createdAt),userIds:[h.userId]})}return{type:"comment",id:e.id,body:e.content,metadata:(s=e.data)==null?void 0:s.metadata,userId:(n=e.data)==null?void 0:n.userId,createdAt:new Date(e.createdAt),updatedAt:new Date(e.updatedAt),reactions:t}}tiptapThreadToThreadData(e){var t;return{type:"thread",id:e.id,comments:e.comments.map(a=>this.tiptapCommentToCommentData(a)),resolved:!!e.resolvedAt,metadata:(t=e.data)==null?void 0:t.metadata,createdAt:new Date(e.createdAt),updatedAt:new Date(e.updatedAt)}}async deleteComment(e){this.provider.deleteComment(e.threadId,e.commentId)}async deleteThread(e){this.provider.deleteThread(e.threadId)}async resolveThread(e){this.provider.updateThread(e.threadId,{resolvedAt:new Date().toISOString()})}async unresolveThread(e){this.provider.updateThread(e.threadId,{resolvedAt:null})}async addReaction(e){var a;const t=this.provider.getThreadComment(e.threadId,e.commentId,!0);if(!t)throw new Error("Comment not found");this.provider.updateComment(e.threadId,e.commentId,{data:{...t.data,reactions:[...((a=t.data)==null?void 0:a.reactions)||[],{emoji:e.emoji,createdAt:Date.now(),userId:this.userId}]}})}async deleteReaction(e){var a;const t=this.provider.getThreadComment(e.threadId,e.commentId,!0);if(!t)throw new Error("Comment not found");this.provider.updateComment(e.threadId,e.commentId,{data:{...t.data,reactions:(((a=t.data)==null?void 0:a.reactions)||[]).filter(s=>s.emoji!==e.emoji&&s.userId!==this.userId)}})}getThread(e){const t=this.provider.getThread(e);if(!t)throw new Error("Thread not found");return this.tiptapThreadToThreadData(t)}getThreads(){return new Map(this.provider.getThreads().map(e=>[e.id,this.tiptapThreadToThreadData(e)]))}subscribe(e){const t=()=>{e(this.getThreads())};return this.provider.watchThreads(t),()=>{this.provider.unwatchThreads(t)}}}function p(r){const d=new u.Map;if(d.set("id",r.id),d.set("userId",r.userId),d.set("createdAt",r.createdAt.getTime()),d.set("updatedAt",r.updatedAt.getTime()),r.deletedAt?(d.set("deletedAt",r.deletedAt.getTime()),d.set("body",void 0)):d.set("body",r.body),r.reactions.length>0)throw new Error("Reactions should be empty in commentToYMap");return d.set("reactionsByUser",new u.Map),d.set("metadata",r.metadata),d}function b(r){var t;const d=new u.Map;d.set("id",r.id),d.set("createdAt",r.createdAt.getTime()),d.set("updatedAt",r.updatedAt.getTime());const e=new u.Array;return e.push(r.comments.map(a=>p(a))),d.set("comments",e),d.set("resolved",r.resolved),d.set("resolvedUpdatedAt",(t=r.resolvedUpdatedAt)==null?void 0:t.getTime()),d.set("resolvedBy",r.resolvedBy),d.set("metadata",r.metadata),d}function j(r){return{emoji:r.get("emoji"),createdAt:new Date(r.get("createdAt")),userId:r.get("userId")}}function R(r){return[...r.values()].map(e=>j(e)).reduce((e,t)=>{const a=e.find(s=>s.emoji===t.emoji);return a?(a.userIds.push(t.userId),a.createdAt=new Date(Math.min(a.createdAt.getTime(),t.createdAt.getTime()))):e.push({emoji:t.emoji,createdAt:t.createdAt,userIds:[t.userId]}),e},[])}function T(r){return{type:"comment",id:r.get("id"),userId:r.get("userId"),createdAt:new Date(r.get("createdAt")),updatedAt:new Date(r.get("updatedAt")),deletedAt:r.get("deletedAt")?new Date(r.get("deletedAt")):void 0,reactions:R(r.get("reactionsByUser")),metadata:r.get("metadata"),body:r.get("body")}}function c(r){return{type:"thread",id:r.get("id"),createdAt:new Date(r.get("createdAt")),updatedAt:new Date(r.get("updatedAt")),comments:(r.get("comments")||[]).map(d=>T(d)),resolved:r.get("resolved"),resolvedUpdatedAt:new Date(r.get("resolvedUpdatedAt")),resolvedBy:r.get("resolvedBy"),metadata:r.get("metadata")}}class f extends I{constructor(d,e){super(e),this.threadsYMap=d}getThread(d){const e=this.threadsYMap.get(d);if(!e)throw new Error("Thread not found");return c(e)}getThreads(){const d=new Map;return this.threadsYMap.forEach((e,t)=>{e instanceof u.Map&&d.set(t,c(e))}),d}subscribe(d){const e=()=>{d(this.getThreads())};return this.threadsYMap.observeDeep(e),()=>{this.threadsYMap.unobserveDeep(e)}}}class S extends f{constructor(e,t,a,s){super(a,s);o(this,"doRequest",async(e,t,a)=>{const s=await fetch(`${this.BASE_URL}${e}`,{method:t,body:JSON.stringify(a),headers:{"Content-Type":"application/json",...this.headers}});if(!s.ok)throw new Error(`Failed to ${t} ${e}: ${s.statusText}`);return s.json()});o(this,"addThreadToDocument",async e=>{const{threadId:t,...a}=e;return this.doRequest(`/${t}/addToDocument`,"POST",a)});o(this,"createThread",async e=>this.doRequest("","POST",e));o(this,"addComment",e=>{const{threadId:t,...a}=e;return this.doRequest(`/${t}/comments`,"POST",a)});o(this,"updateComment",e=>{const{threadId:t,commentId:a,...s}=e;return this.doRequest(`/${t}/comments/${a}`,"PUT",s)});o(this,"deleteComment",e=>{const{threadId:t,commentId:a,...s}=e;return this.doRequest(`/${t}/comments/${a}?soft=${!!s.softDelete}`,"DELETE")});o(this,"deleteThread",e=>this.doRequest(`/${e.threadId}`,"DELETE"));o(this,"resolveThread",e=>this.doRequest(`/${e.threadId}/resolve`,"POST"));o(this,"unresolveThread",e=>this.doRequest(`/${e.threadId}/unresolve`,"POST"));o(this,"addReaction",e=>{const{threadId:t,commentId:a,...s}=e;return this.doRequest(`/${t}/comments/${a}/reactions`,"POST",s)});o(this,"deleteReaction",e=>this.doRequest(`/${e.threadId}/comments/${e.commentId}/reactions/${e.emoji}`,"DELETE"));this.BASE_URL=e,this.headers=t}}class Y extends f{constructor(e,t,a){super(t,a);o(this,"transact",e=>async t=>this.threadsYMap.doc.transact(()=>e(t)));o(this,"createThread",this.transact(e=>{if(!this.auth.canCreateThread())throw new Error("Not authorized");const t=new Date,a={type:"comment",id:g.v4(),userId:this.userId,createdAt:t,updatedAt:t,reactions:[],metadata:e.initialComment.metadata,body:e.initialComment.body},s={type:"thread",id:g.v4(),createdAt:t,updatedAt:t,comments:[a],resolved:!1,metadata:e.metadata};return this.threadsYMap.set(s.id,b(s)),s}));o(this,"addThreadToDocument");o(this,"addComment",this.transact(e=>{const t=this.threadsYMap.get(e.threadId);if(!t)throw new Error("Thread not found");if(!this.auth.canAddComment(c(t)))throw new Error("Not authorized");const a=new Date,s={type:"comment",id:g.v4(),userId:this.userId,createdAt:a,updatedAt:a,deletedAt:void 0,reactions:[],metadata:e.comment.metadata,body:e.comment.body};return t.get("comments").push([p(s)]),t.set("updatedAt",new Date().getTime()),s}));o(this,"updateComment",this.transact(e=>{const t=this.threadsYMap.get(e.threadId);if(!t)throw new Error("Thread not found");const a=l(t.get("comments"),n=>n.get("id")===e.commentId);if(a===-1)throw new Error("Comment not found");const s=t.get("comments").get(a);if(!this.auth.canUpdateComment(T(s)))throw new Error("Not authorized");s.set("body",e.comment.body),s.set("updatedAt",new Date().getTime()),s.set("metadata",e.comment.metadata)}));o(this,"deleteComment",this.transact(e=>{const t=this.threadsYMap.get(e.threadId);if(!t)throw new Error("Thread not found");const a=l(t.get("comments"),n=>n.get("id")===e.commentId);if(a===-1)throw new Error("Comment not found");const s=t.get("comments").get(a);if(!this.auth.canDeleteComment(T(s)))throw new Error("Not authorized");if(s.get("deletedAt"))throw new Error("Comment already deleted");e.softDelete?(s.set("deletedAt",new Date().getTime()),s.set("body",void 0)):t.get("comments").delete(a),t.get("comments").toArray().every(n=>n.get("deletedAt"))&&(e.softDelete?t.set("deletedAt",new Date().getTime()):this.threadsYMap.delete(e.threadId)),t.set("updatedAt",new Date().getTime())}));o(this,"deleteThread",this.transact(e=>{if(!this.auth.canDeleteThread(c(this.threadsYMap.get(e.threadId))))throw new Error("Not authorized");this.threadsYMap.delete(e.threadId)}));o(this,"resolveThread",this.transact(e=>{const t=this.threadsYMap.get(e.threadId);if(!t)throw new Error("Thread not found");if(!this.auth.canResolveThread(c(t)))throw new Error("Not authorized");t.set("resolved",!0),t.set("resolvedUpdatedAt",new Date().getTime()),t.set("resolvedBy",this.userId)}));o(this,"unresolveThread",this.transact(e=>{const t=this.threadsYMap.get(e.threadId);if(!t)throw new Error("Thread not found");if(!this.auth.canUnresolveThread(c(t)))throw new Error("Not authorized");t.set("resolved",!1),t.set("resolvedUpdatedAt",new Date().getTime())}));o(this,"addReaction",this.transact(e=>{const t=this.threadsYMap.get(e.threadId);if(!t)throw new Error("Thread not found");const a=l(t.get("comments"),m=>m.get("id")===e.commentId);if(a===-1)throw new Error("Comment not found");const s=t.get("comments").get(a);if(!this.auth.canAddReaction(T(s),e.emoji))throw new Error("Not authorized");const n=new Date,h=`${this.userId}-${e.emoji}`,i=s.get("reactionsByUser");if(!i.has(h)){const m=new u.Map;m.set("emoji",e.emoji),m.set("createdAt",n.getTime()),m.set("userId",this.userId),i.set(h,m)}}));o(this,"deleteReaction",this.transact(e=>{const t=this.threadsYMap.get(e.threadId);if(!t)throw new Error("Thread not found");const a=l(t.get("comments"),i=>i.get("id")===e.commentId);if(a===-1)throw new Error("Comment not found");const s=t.get("comments").get(a);if(!this.auth.canDeleteReaction(T(s),e.emoji))throw new Error("Not authorized");const n=`${this.userId}-${e.emoji}`;s.get("reactionsByUser").delete(n)}));this.userId=e}}function l(r,d){for(let e=0;e<r.length;e++)if(d(r.get(e)))return e;return-1}exports.DefaultThreadStoreAuth=D;exports.RESTYjsThreadStore=S;exports.ThreadStore=I;exports.ThreadStoreAuth=w;exports.TiptapThreadStore=E;exports.YjsThreadStore=Y;exports.YjsThreadStoreBase=f;
2
2
  //# sourceMappingURL=comments.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"comments.cjs","sources":["../src/comments/threadstore/ThreadStoreAuth.ts","../src/comments/threadstore/DefaultThreadStoreAuth.ts","../src/comments/threadstore/ThreadStore.ts","../src/comments/threadstore/TipTapThreadStore.ts","../src/comments/threadstore/yjs/yjsHelpers.ts","../src/comments/threadstore/yjs/YjsThreadStoreBase.ts","../src/comments/threadstore/yjs/RESTYjsThreadStore.ts","../src/comments/threadstore/yjs/YjsThreadStore.ts"],"sourcesContent":["import { CommentData, ThreadData } from \"../types.js\";\n\nexport abstract class ThreadStoreAuth {\n abstract canCreateThread(): boolean;\n abstract canAddComment(thread: ThreadData): boolean;\n abstract canUpdateComment(comment: CommentData): boolean;\n abstract canDeleteComment(comment: CommentData): boolean;\n abstract canDeleteThread(thread: ThreadData): boolean;\n abstract canResolveThread(thread: ThreadData): boolean;\n abstract canUnresolveThread(thread: ThreadData): boolean;\n abstract canAddReaction(comment: CommentData, emoji?: string): boolean;\n abstract canDeleteReaction(comment: CommentData, emoji?: string): boolean;\n}\n","import { CommentData, ThreadData } from \"../types.js\";\nimport { ThreadStoreAuth } from \"./ThreadStoreAuth.js\";\n\n/*\n * The DefaultThreadStoreAuth class defines the authorization rules for interacting with comments.\n * We take a role (\"comment\" or \"editor\") and implement the rules.\n *\n * This class is then used in the UI to show / hide specific interactions.\n *\n * Rules:\n * - View-only users should not be able to see any comments\n * - Comment-only users and editors can:\n * - - create new comments / replies / reactions\n * - - edit / delete their own comments / reactions\n * - - resolve / unresolve threads\n * - Editors can also delete any comment or thread\n */\nexport class DefaultThreadStoreAuth extends ThreadStoreAuth {\n constructor(\n private readonly userId: string,\n private readonly role: \"comment\" | \"editor\",\n ) {\n super();\n }\n\n /**\n * Auth: should be possible by anyone with comment access\n */\n canCreateThread(): boolean {\n return true;\n }\n\n /**\n * Auth: should be possible by anyone with comment access\n */\n canAddComment(_thread: ThreadData): boolean {\n return true;\n }\n\n /**\n * Auth: should only be possible by the comment author\n */\n canUpdateComment(comment: CommentData): boolean {\n return comment.userId === this.userId;\n }\n\n /**\n * Auth: should be possible by the comment author OR an editor of the document\n */\n canDeleteComment(comment: CommentData): boolean {\n return comment.userId === this.userId || this.role === \"editor\";\n }\n\n /**\n * Auth: should only be possible by an editor of the document\n */\n canDeleteThread(_thread: ThreadData): boolean {\n return this.role === \"editor\";\n }\n\n /**\n * Auth: should be possible by anyone with comment access\n */\n canResolveThread(_thread: ThreadData): boolean {\n return true;\n }\n\n /**\n * Auth: should be possible by anyone with comment access\n */\n canUnresolveThread(_thread: ThreadData): boolean {\n return true;\n }\n\n /**\n * Auth: should be possible by anyone with comment access\n *\n * Note: will also check if the user has already reacted with the same emoji. TBD: is that a nice design or should this responsibility be outside of auth?\n */\n canAddReaction(comment: CommentData, emoji?: string): boolean {\n if (!emoji) {\n return true;\n }\n\n return !comment.reactions.some(\n (reaction) =>\n reaction.emoji === emoji && reaction.userIds.includes(this.userId),\n );\n }\n\n /**\n * Auth: should be possible by anyone with comment access\n *\n * Note: will also check if the user has already reacted with the same emoji. TBD: is that a nice design or should this responsibility be outside of auth?\n */\n canDeleteReaction(comment: CommentData, emoji?: string): boolean {\n if (!emoji) {\n return true;\n }\n\n return comment.reactions.some(\n (reaction) =>\n reaction.emoji === emoji && reaction.userIds.includes(this.userId),\n );\n }\n}\n","import { CommentBody, CommentData, ThreadData } from \"../types.js\";\nimport { ThreadStoreAuth } from \"./ThreadStoreAuth.js\";\n\n/**\n * ThreadStore is an abstract class that defines the interface\n * to read / add / update / delete threads and comments.\n */\nexport abstract class ThreadStore {\n public readonly auth: ThreadStoreAuth;\n\n constructor(auth: ThreadStoreAuth) {\n this.auth = auth;\n }\n\n /**\n * A \"thread\" in the ThreadStore only contains information about the content\n * of the thread / comments. It does not contain information about the position.\n *\n * This function can be implemented to store the thread in the document (by creating a mark)\n * If not implemented, default behavior will apply (creating the mark via TipTap)\n * See CommentsPlugin.ts for more details.\n */\n abstract addThreadToDocument?(options: {\n threadId: string;\n selection: {\n prosemirror: {\n head: number;\n anchor: number;\n };\n yjs?: {\n head: any;\n anchor: any;\n };\n };\n }): Promise<void>;\n\n /**\n * Creates a new thread with an initial comment.\n */\n abstract createThread(options: {\n initialComment: {\n body: CommentBody;\n metadata?: any;\n };\n metadata?: any;\n }): Promise<ThreadData>;\n\n /**\n * Adds a comment to a thread.\n */\n abstract addComment(options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n }): Promise<CommentData>;\n\n /**\n * Updates a comment in a thread.\n */\n abstract updateComment(options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n commentId: string;\n }): Promise<void>;\n\n /**\n * Deletes a comment from a thread.\n */\n abstract deleteComment(options: {\n threadId: string;\n commentId: string;\n }): Promise<void>;\n\n /**\n * Deletes a thread.\n */\n abstract deleteThread(options: { threadId: string }): Promise<void>;\n\n /**\n * Marks a thread as resolved.\n */\n abstract resolveThread(options: { threadId: string }): Promise<void>;\n\n /**\n * Marks a thread as unresolved.\n */\n abstract unresolveThread(options: { threadId: string }): Promise<void>;\n\n /**\n * Adds a reaction to a comment.\n *\n * Auth: should be possible by anyone with comment access\n */\n abstract addReaction(options: {\n threadId: string;\n commentId: string;\n emoji: string;\n }): Promise<void>;\n\n /**\n * Deletes a reaction from a comment.\n *\n * Auth: should be possible by the reaction author\n */\n abstract deleteReaction(options: {\n threadId: string;\n commentId: string;\n emoji: string;\n }): Promise<void>;\n\n /**\n * Retrieve data for a specific thread.\n */\n abstract getThread(threadId: string): ThreadData;\n\n /**\n * Retrieve all threads.\n */\n abstract getThreads(): Map<string, ThreadData>;\n\n /**\n * Subscribe to changes in the thread store.\n *\n * @returns a function to unsubscribe from the thread store\n */\n abstract subscribe(\n cb: (threads: Map<string, ThreadData>) => void,\n ): () => void;\n}\n","import type {\n TCollabComment,\n TCollabThread,\n TiptapCollabProvider,\n} from \"@hocuspocus/provider\";\nimport {\n CommentBody,\n CommentData,\n CommentReactionData,\n ThreadData,\n} from \"../types.js\";\nimport { ThreadStore } from \"./ThreadStore.js\";\nimport { ThreadStoreAuth } from \"./ThreadStoreAuth.js\";\n\ntype ReactionAsTiptapData = {\n emoji: string;\n createdAt: number;\n userId: string;\n};\n\n/**\n * The `TiptapThreadStore` integrates with Tiptap's collaboration provider for comment management.\n * You can pass a `TiptapCollabProvider` to the constructor which takes care of storing the comments.\n *\n * Under the hood, this actually works similarly to the `YjsThreadStore` implementation. (comments are stored in the Yjs document)\n */\nexport class TiptapThreadStore extends ThreadStore {\n constructor(\n private readonly userId: string,\n private readonly provider: TiptapCollabProvider,\n auth: ThreadStoreAuth, // TODO: use?\n ) {\n super(auth);\n }\n\n /**\n * Creates a new thread with an initial comment.\n */\n public async createThread(options: {\n initialComment: {\n body: CommentBody;\n metadata?: any;\n };\n metadata?: any;\n }): Promise<ThreadData> {\n let thread = this.provider.createThread({\n data: options.metadata,\n });\n\n thread = this.provider.addComment(thread.id, {\n content: options.initialComment.body,\n data: {\n metadata: options.initialComment.metadata,\n userId: this.userId,\n },\n });\n\n return this.tiptapThreadToThreadData(thread);\n }\n\n // TipTapThreadStore does not support addThreadToDocument\n public addThreadToDocument = undefined;\n\n /**\n * Adds a comment to a thread.\n */\n public async addComment(options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n }): Promise<CommentBody> {\n const thread = this.provider.addComment(options.threadId, {\n content: options.comment.body,\n data: {\n metadata: options.comment.metadata,\n userId: this.userId,\n },\n });\n\n return this.tiptapCommentToCommentData(\n thread.comments[thread.comments.length - 1],\n );\n }\n\n /**\n * Updates a comment in a thread.\n */\n public async updateComment(options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n commentId: string;\n }) {\n const comment = this.provider.getThreadComment(\n options.threadId,\n options.commentId,\n true,\n );\n\n if (!comment) {\n throw new Error(\"Comment not found\");\n }\n\n this.provider.updateComment(options.threadId, options.commentId, {\n content: options.comment.body,\n data: {\n ...comment.data,\n metadata: options.comment.metadata,\n },\n });\n }\n\n private tiptapCommentToCommentData(comment: TCollabComment): CommentData {\n const reactions: CommentReactionData[] = [];\n\n for (const reaction of (comment.data?.reactions ||\n []) as ReactionAsTiptapData[]) {\n const existingReaction = reactions.find(\n (r) => r.emoji === reaction.emoji,\n );\n if (existingReaction) {\n existingReaction.userIds.push(reaction.userId);\n existingReaction.createdAt = new Date(\n Math.min(existingReaction.createdAt.getTime(), reaction.createdAt),\n );\n } else {\n reactions.push({\n emoji: reaction.emoji,\n createdAt: new Date(reaction.createdAt),\n userIds: [reaction.userId],\n });\n }\n }\n\n return {\n type: \"comment\",\n id: comment.id,\n body: comment.content,\n metadata: comment.data?.metadata,\n userId: comment.data?.userId,\n createdAt: new Date(comment.createdAt),\n updatedAt: new Date(comment.updatedAt),\n reactions,\n };\n }\n\n private tiptapThreadToThreadData(thread: TCollabThread): ThreadData {\n return {\n type: \"thread\",\n id: thread.id,\n comments: thread.comments.map((comment) =>\n this.tiptapCommentToCommentData(comment),\n ),\n resolved: !!thread.resolvedAt,\n metadata: thread.data?.metadata,\n createdAt: new Date(thread.createdAt),\n updatedAt: new Date(thread.updatedAt),\n };\n }\n\n /**\n * Deletes a comment from a thread.\n */\n public async deleteComment(options: { threadId: string; commentId: string }) {\n this.provider.deleteComment(options.threadId, options.commentId);\n }\n\n /**\n * Deletes a thread.\n */\n public async deleteThread(options: { threadId: string }) {\n this.provider.deleteThread(options.threadId);\n }\n\n /**\n * Marks a thread as resolved.\n */\n public async resolveThread(options: { threadId: string }) {\n this.provider.updateThread(options.threadId, {\n resolvedAt: new Date().toISOString(),\n });\n }\n\n /**\n * Marks a thread as unresolved.\n */\n public async unresolveThread(options: { threadId: string }) {\n this.provider.updateThread(options.threadId, {\n resolvedAt: null,\n });\n }\n\n /**\n * Adds a reaction to a comment.\n *\n * Auth: should be possible by anyone with comment access\n */\n public async addReaction(options: {\n threadId: string;\n commentId: string;\n emoji: string;\n }) {\n const comment = this.provider.getThreadComment(\n options.threadId,\n options.commentId,\n true,\n );\n\n if (!comment) {\n throw new Error(\"Comment not found\");\n }\n\n this.provider.updateComment(options.threadId, options.commentId, {\n data: {\n ...comment.data,\n reactions: [\n ...((comment.data?.reactions || []) as ReactionAsTiptapData[]),\n {\n emoji: options.emoji,\n createdAt: Date.now(),\n userId: this.userId,\n },\n ],\n },\n });\n }\n\n /**\n * Deletes a reaction from a comment.\n *\n * Auth: should be possible by the reaction author\n */\n public async deleteReaction(options: {\n threadId: string;\n commentId: string;\n emoji: string;\n }) {\n const comment = this.provider.getThreadComment(\n options.threadId,\n options.commentId,\n true,\n );\n\n if (!comment) {\n throw new Error(\"Comment not found\");\n }\n\n this.provider.updateComment(options.threadId, options.commentId, {\n data: {\n ...comment.data,\n reactions: (\n (comment.data?.reactions || []) as ReactionAsTiptapData[]\n ).filter(\n (reaction) =>\n reaction.emoji !== options.emoji && reaction.userId !== this.userId,\n ),\n },\n });\n }\n\n public getThread(threadId: string): ThreadData {\n const thread = this.provider.getThread(threadId);\n\n if (!thread) {\n throw new Error(\"Thread not found\");\n }\n\n return this.tiptapThreadToThreadData(thread);\n }\n\n public getThreads(): Map<string, ThreadData> {\n return new Map(\n this.provider\n .getThreads()\n .map((thread) => [thread.id, this.tiptapThreadToThreadData(thread)]),\n );\n }\n\n public subscribe(cb: (threads: Map<string, ThreadData>) => void): () => void {\n const newCb = () => {\n cb(this.getThreads());\n };\n this.provider.watchThreads(newCb);\n return () => {\n this.provider.unwatchThreads(newCb);\n };\n }\n}\n","import * as Y from \"yjs\";\nimport { CommentData, CommentReactionData, ThreadData } from \"../../types.js\";\n\nexport function commentToYMap(comment: CommentData) {\n const yMap = new Y.Map<any>();\n yMap.set(\"id\", comment.id);\n yMap.set(\"userId\", comment.userId);\n yMap.set(\"createdAt\", comment.createdAt.getTime());\n yMap.set(\"updatedAt\", comment.updatedAt.getTime());\n if (comment.deletedAt) {\n yMap.set(\"deletedAt\", comment.deletedAt.getTime());\n yMap.set(\"body\", undefined);\n } else {\n yMap.set(\"body\", comment.body);\n }\n if (comment.reactions.length > 0) {\n throw new Error(\"Reactions should be empty in commentToYMap\");\n }\n\n /**\n * Reactions are stored in a map keyed by {userId-emoji},\n * this makes it easy to add / remove reactions and in a way that works local-first.\n * The cost is that \"reading\" the reactions is a bit more complex (see yMapToReactions).\n */\n yMap.set(\"reactionsByUser\", new Y.Map());\n yMap.set(\"metadata\", comment.metadata);\n\n return yMap;\n}\n\nexport function threadToYMap(thread: ThreadData) {\n const yMap = new Y.Map();\n yMap.set(\"id\", thread.id);\n yMap.set(\"createdAt\", thread.createdAt.getTime());\n yMap.set(\"updatedAt\", thread.updatedAt.getTime());\n const commentsArray = new Y.Array<Y.Map<any>>();\n\n commentsArray.push(thread.comments.map((comment) => commentToYMap(comment)));\n\n yMap.set(\"comments\", commentsArray);\n yMap.set(\"resolved\", thread.resolved);\n yMap.set(\"resolvedUpdatedAt\", thread.resolvedUpdatedAt?.getTime());\n yMap.set(\"resolvedBy\", thread.resolvedBy);\n yMap.set(\"metadata\", thread.metadata);\n return yMap;\n}\n\ntype SingleUserCommentReactionData = {\n emoji: string;\n createdAt: Date;\n userId: string;\n};\n\nexport function yMapToReaction(\n yMap: Y.Map<any>,\n): SingleUserCommentReactionData {\n return {\n emoji: yMap.get(\"emoji\"),\n createdAt: new Date(yMap.get(\"createdAt\")),\n userId: yMap.get(\"userId\"),\n };\n}\n\nfunction yMapToReactions(yMap: Y.Map<any>): CommentReactionData[] {\n const flatReactions = [...yMap.values()].map((reaction: Y.Map<any>) =>\n yMapToReaction(reaction),\n );\n // combine reactions by the same emoji\n return flatReactions.reduce(\n (acc: CommentReactionData[], reaction: SingleUserCommentReactionData) => {\n const existingReaction = acc.find((r) => r.emoji === reaction.emoji);\n if (existingReaction) {\n existingReaction.userIds.push(reaction.userId);\n existingReaction.createdAt = new Date(\n Math.min(\n existingReaction.createdAt.getTime(),\n reaction.createdAt.getTime(),\n ),\n );\n } else {\n acc.push({\n emoji: reaction.emoji,\n createdAt: reaction.createdAt,\n userIds: [reaction.userId],\n });\n }\n return acc;\n },\n [] as CommentReactionData[],\n );\n}\n\nexport function yMapToComment(yMap: Y.Map<any>): CommentData {\n return {\n type: \"comment\",\n id: yMap.get(\"id\"),\n userId: yMap.get(\"userId\"),\n createdAt: new Date(yMap.get(\"createdAt\")),\n updatedAt: new Date(yMap.get(\"updatedAt\")),\n deletedAt: yMap.get(\"deletedAt\")\n ? new Date(yMap.get(\"deletedAt\"))\n : undefined,\n reactions: yMapToReactions(yMap.get(\"reactionsByUser\")),\n metadata: yMap.get(\"metadata\"),\n body: yMap.get(\"body\"),\n };\n}\n\nexport function yMapToThread(yMap: Y.Map<any>): ThreadData {\n return {\n type: \"thread\",\n id: yMap.get(\"id\"),\n createdAt: new Date(yMap.get(\"createdAt\")),\n updatedAt: new Date(yMap.get(\"updatedAt\")),\n comments: ((yMap.get(\"comments\") as Y.Array<Y.Map<any>>) || []).map(\n (comment) => yMapToComment(comment),\n ),\n resolved: yMap.get(\"resolved\"),\n resolvedUpdatedAt: new Date(yMap.get(\"resolvedUpdatedAt\")),\n resolvedBy: yMap.get(\"resolvedBy\"),\n metadata: yMap.get(\"metadata\"),\n };\n}\n","import * as Y from \"yjs\";\nimport { ThreadData } from \"../../types.js\";\nimport { ThreadStore } from \"../ThreadStore.js\";\nimport { ThreadStoreAuth } from \"../ThreadStoreAuth.js\";\nimport { yMapToThread } from \"./yjsHelpers.js\";\n\n/**\n * This is an abstract class that only implements the READ methods required by the ThreadStore interface.\n * The data is read from a Yjs Map.\n */\nexport abstract class YjsThreadStoreBase extends ThreadStore {\n constructor(\n protected readonly threadsYMap: Y.Map<any>,\n auth: ThreadStoreAuth,\n ) {\n super(auth);\n }\n\n // TODO: async / reactive interface?\n public getThread(threadId: string) {\n const yThread = this.threadsYMap.get(threadId);\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n const thread = yMapToThread(yThread);\n return thread;\n }\n\n public getThreads(): Map<string, ThreadData> {\n const threadMap = new Map<string, ThreadData>();\n this.threadsYMap.forEach((yThread, id) => {\n threadMap.set(id, yMapToThread(yThread));\n });\n return threadMap;\n }\n\n public subscribe(cb: (threads: Map<string, ThreadData>) => void) {\n const observer = () => {\n cb(this.getThreads());\n };\n\n this.threadsYMap.observeDeep(observer);\n\n return () => {\n this.threadsYMap.unobserveDeep(observer);\n };\n }\n}\n","import * as Y from \"yjs\";\nimport { CommentBody } from \"../../types.js\";\nimport { ThreadStoreAuth } from \"../ThreadStoreAuth.js\";\nimport { YjsThreadStoreBase } from \"./YjsThreadStoreBase.js\";\n\n/**\n * This is a REST-based implementation of the YjsThreadStoreBase.\n * It Reads data directly from the underlying document (same as YjsThreadStore),\n * but for Writes, it sends data to a REST API that should:\n * - check the user has the correct permissions to make the desired changes\n * - apply the updates to the underlying Yjs document\n *\n * (see https://github.com/TypeCellOS/BlockNote-demo-nextjs-hocuspocus)\n *\n * The reason we still use the Yjs document as underlying storage is that it makes it easy to\n * sync updates in real-time to other collaborators.\n * (but technically, you could also implement a different storage altogether\n * and not store the thread related data in the Yjs document)\n */\nexport class RESTYjsThreadStore extends YjsThreadStoreBase {\n constructor(\n private readonly BASE_URL: string,\n private readonly headers: Record<string, string>,\n threadsYMap: Y.Map<any>,\n auth: ThreadStoreAuth,\n ) {\n super(threadsYMap, auth);\n }\n\n private doRequest = async (path: string, method: string, body?: any) => {\n const response = await fetch(`${this.BASE_URL}${path}`, {\n method,\n body: JSON.stringify(body),\n headers: {\n \"Content-Type\": \"application/json\",\n ...this.headers,\n },\n });\n\n if (!response.ok) {\n throw new Error(`Failed to ${method} ${path}: ${response.statusText}`);\n }\n\n return response.json();\n };\n\n public addThreadToDocument = async (options: {\n threadId: string;\n selection: {\n prosemirror: {\n head: number;\n anchor: number;\n };\n yjs: {\n head: any;\n anchor: any;\n };\n };\n }) => {\n const { threadId, ...rest } = options;\n return this.doRequest(`/${threadId}/addToDocument`, \"POST\", rest);\n };\n\n public createThread = async (options: {\n initialComment: {\n body: CommentBody;\n metadata?: any;\n };\n metadata?: any;\n }) => {\n return this.doRequest(\"\", \"POST\", options);\n };\n\n public addComment = (options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n }) => {\n const { threadId, ...rest } = options;\n return this.doRequest(`/${threadId}/comments`, \"POST\", rest);\n };\n\n public updateComment = (options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n commentId: string;\n }) => {\n const { threadId, commentId, ...rest } = options;\n return this.doRequest(`/${threadId}/comments/${commentId}`, \"PUT\", rest);\n };\n\n public deleteComment = (options: {\n threadId: string;\n commentId: string;\n softDelete?: boolean;\n }) => {\n const { threadId, commentId, ...rest } = options;\n return this.doRequest(\n `/${threadId}/comments/${commentId}?soft=${!!rest.softDelete}`,\n \"DELETE\",\n );\n };\n\n public deleteThread = (options: { threadId: string }) => {\n return this.doRequest(`/${options.threadId}`, \"DELETE\");\n };\n\n public resolveThread = (options: { threadId: string }) => {\n return this.doRequest(`/${options.threadId}/resolve`, \"POST\");\n };\n\n public unresolveThread = (options: { threadId: string }) => {\n return this.doRequest(`/${options.threadId}/unresolve`, \"POST\");\n };\n\n public addReaction = (options: {\n threadId: string;\n commentId: string;\n emoji: string;\n }) => {\n const { threadId, commentId, ...rest } = options;\n return this.doRequest(\n `/${threadId}/comments/${commentId}/reactions`,\n \"POST\",\n rest,\n );\n };\n\n public deleteReaction = (options: {\n threadId: string;\n commentId: string;\n emoji: string;\n }) => {\n return this.doRequest(\n `/${options.threadId}/comments/${options.commentId}/reactions/${options.emoji}`,\n \"DELETE\",\n );\n };\n}\n","import { v4 } from \"uuid\";\nimport * as Y from \"yjs\";\nimport { CommentBody, CommentData, ThreadData } from \"../../types.js\";\nimport { ThreadStoreAuth } from \"../ThreadStoreAuth.js\";\nimport { YjsThreadStoreBase } from \"./YjsThreadStoreBase.js\";\nimport {\n commentToYMap,\n threadToYMap,\n yMapToComment,\n yMapToThread,\n} from \"./yjsHelpers.js\";\n\n/**\n * This is a Yjs-based implementation of the ThreadStore interface.\n *\n * It reads and writes thread / comments information directly to the underlying Yjs Document.\n *\n * @important While this is the easiest to add to your app, there are two challenges:\n * - The user needs to be able to write to the Yjs document to store the information.\n * So a user without write access to the Yjs document cannot leave any comments.\n * - Even with write access, the operations are not secure. Unless your Yjs server\n * guards against malicious operations, it's technically possible for one user to make changes to another user's comments, etc.\n * (even though these options are not visible in the UI, a malicious user can make unauthorized changes to the underlying Yjs document)\n */\nexport class YjsThreadStore extends YjsThreadStoreBase {\n constructor(\n private readonly userId: string,\n threadsYMap: Y.Map<any>,\n auth: ThreadStoreAuth,\n ) {\n super(threadsYMap, auth);\n }\n\n private transact = <T, R>(\n fn: (options: T) => R,\n ): ((options: T) => Promise<R>) => {\n return async (options: T) => {\n return this.threadsYMap.doc!.transact(() => {\n return fn(options);\n });\n };\n };\n\n public createThread = this.transact(\n (options: {\n initialComment: {\n body: CommentBody;\n metadata?: any;\n };\n metadata?: any;\n }) => {\n if (!this.auth.canCreateThread()) {\n throw new Error(\"Not authorized\");\n }\n\n const date = new Date();\n\n const comment: CommentData = {\n type: \"comment\",\n id: v4(),\n userId: this.userId,\n createdAt: date,\n updatedAt: date,\n reactions: [],\n metadata: options.initialComment.metadata,\n body: options.initialComment.body,\n };\n\n const thread: ThreadData = {\n type: \"thread\",\n id: v4(),\n createdAt: date,\n updatedAt: date,\n comments: [comment],\n resolved: false,\n metadata: options.metadata,\n };\n\n this.threadsYMap.set(thread.id, threadToYMap(thread));\n\n return thread;\n },\n );\n\n // YjsThreadStore does not support addThreadToDocument\n public addThreadToDocument = undefined;\n\n public addComment = this.transact(\n (options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n }) => {\n const yThread = this.threadsYMap.get(options.threadId);\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n if (!this.auth.canAddComment(yMapToThread(yThread))) {\n throw new Error(\"Not authorized\");\n }\n\n const date = new Date();\n const comment: CommentData = {\n type: \"comment\",\n id: v4(),\n userId: this.userId,\n createdAt: date,\n updatedAt: date,\n deletedAt: undefined,\n reactions: [],\n metadata: options.comment.metadata,\n body: options.comment.body,\n };\n\n (yThread.get(\"comments\") as Y.Array<Y.Map<any>>).push([\n commentToYMap(comment),\n ]);\n\n yThread.set(\"updatedAt\", new Date().getTime());\n return comment;\n },\n );\n\n public updateComment = this.transact(\n (options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n commentId: string;\n }) => {\n const yThread = this.threadsYMap.get(options.threadId);\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n const yCommentIndex = yArrayFindIndex(\n yThread.get(\"comments\"),\n (comment) => comment.get(\"id\") === options.commentId,\n );\n\n if (yCommentIndex === -1) {\n throw new Error(\"Comment not found\");\n }\n\n const yComment = yThread.get(\"comments\").get(yCommentIndex);\n\n if (!this.auth.canUpdateComment(yMapToComment(yComment))) {\n throw new Error(\"Not authorized\");\n }\n\n yComment.set(\"body\", options.comment.body);\n yComment.set(\"updatedAt\", new Date().getTime());\n yComment.set(\"metadata\", options.comment.metadata);\n },\n );\n\n public deleteComment = this.transact(\n (options: {\n threadId: string;\n commentId: string;\n softDelete?: boolean;\n }) => {\n const yThread = this.threadsYMap.get(options.threadId);\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n const yCommentIndex = yArrayFindIndex(\n yThread.get(\"comments\"),\n (comment) => comment.get(\"id\") === options.commentId,\n );\n\n if (yCommentIndex === -1) {\n throw new Error(\"Comment not found\");\n }\n\n const yComment = yThread.get(\"comments\").get(yCommentIndex);\n\n if (!this.auth.canDeleteComment(yMapToComment(yComment))) {\n throw new Error(\"Not authorized\");\n }\n\n if (yComment.get(\"deletedAt\")) {\n throw new Error(\"Comment already deleted\");\n }\n\n if (options.softDelete) {\n yComment.set(\"deletedAt\", new Date().getTime());\n yComment.set(\"body\", undefined);\n } else {\n yThread.get(\"comments\").delete(yCommentIndex);\n }\n\n if (\n (yThread.get(\"comments\") as Y.Array<any>)\n .toArray()\n .every((comment) => comment.get(\"deletedAt\"))\n ) {\n // all comments deleted\n if (options.softDelete) {\n yThread.set(\"deletedAt\", new Date().getTime());\n } else {\n this.threadsYMap.delete(options.threadId);\n }\n }\n\n yThread.set(\"updatedAt\", new Date().getTime());\n },\n );\n\n public deleteThread = this.transact((options: { threadId: string }) => {\n if (\n !this.auth.canDeleteThread(\n yMapToThread(this.threadsYMap.get(options.threadId)),\n )\n ) {\n throw new Error(\"Not authorized\");\n }\n\n this.threadsYMap.delete(options.threadId);\n });\n\n public resolveThread = this.transact((options: { threadId: string }) => {\n const yThread = this.threadsYMap.get(options.threadId);\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n if (!this.auth.canResolveThread(yMapToThread(yThread))) {\n throw new Error(\"Not authorized\");\n }\n\n yThread.set(\"resolved\", true);\n yThread.set(\"resolvedUpdatedAt\", new Date().getTime());\n yThread.set(\"resolvedBy\", this.userId);\n });\n\n public unresolveThread = this.transact((options: { threadId: string }) => {\n const yThread = this.threadsYMap.get(options.threadId);\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n if (!this.auth.canUnresolveThread(yMapToThread(yThread))) {\n throw new Error(\"Not authorized\");\n }\n\n yThread.set(\"resolved\", false);\n yThread.set(\"resolvedUpdatedAt\", new Date().getTime());\n });\n\n public addReaction = this.transact(\n (options: { threadId: string; commentId: string; emoji: string }) => {\n const yThread = this.threadsYMap.get(options.threadId);\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n const yCommentIndex = yArrayFindIndex(\n yThread.get(\"comments\"),\n (comment) => comment.get(\"id\") === options.commentId,\n );\n\n if (yCommentIndex === -1) {\n throw new Error(\"Comment not found\");\n }\n\n const yComment = yThread.get(\"comments\").get(yCommentIndex);\n\n if (!this.auth.canAddReaction(yMapToComment(yComment), options.emoji)) {\n throw new Error(\"Not authorized\");\n }\n\n const date = new Date();\n\n const key = `${this.userId}-${options.emoji}`;\n\n const reactionsByUser = yComment.get(\"reactionsByUser\");\n\n if (reactionsByUser.has(key)) {\n // already exists\n return;\n } else {\n const reaction = new Y.Map();\n reaction.set(\"emoji\", options.emoji);\n reaction.set(\"createdAt\", date.getTime());\n reaction.set(\"userId\", this.userId);\n reactionsByUser.set(key, reaction);\n }\n },\n );\n\n public deleteReaction = this.transact(\n (options: { threadId: string; commentId: string; emoji: string }) => {\n const yThread = this.threadsYMap.get(options.threadId);\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n const yCommentIndex = yArrayFindIndex(\n yThread.get(\"comments\"),\n (comment) => comment.get(\"id\") === options.commentId,\n );\n\n if (yCommentIndex === -1) {\n throw new Error(\"Comment not found\");\n }\n\n const yComment = yThread.get(\"comments\").get(yCommentIndex);\n\n if (\n !this.auth.canDeleteReaction(yMapToComment(yComment), options.emoji)\n ) {\n throw new Error(\"Not authorized\");\n }\n\n const key = `${this.userId}-${options.emoji}`;\n\n const reactionsByUser = yComment.get(\"reactionsByUser\");\n\n reactionsByUser.delete(key);\n },\n );\n}\n\nfunction yArrayFindIndex(\n yArray: Y.Array<any>,\n predicate: (item: any) => boolean,\n) {\n for (let i = 0; i < yArray.length; i++) {\n if (predicate(yArray.get(i))) {\n return i;\n }\n }\n return -1;\n}\n"],"names":["ThreadStoreAuth","DefaultThreadStoreAuth","userId","role","_thread","comment","emoji","reaction","ThreadStore","auth","__publicField","TiptapThreadStore","provider","options","thread","reactions","_a","existingReaction","r","_b","_c","threadId","cb","newCb","commentToYMap","yMap","Y","threadToYMap","commentsArray","yMapToReaction","yMapToReactions","acc","yMapToComment","yMapToThread","YjsThreadStoreBase","threadsYMap","yThread","threadMap","id","observer","RESTYjsThreadStore","BASE_URL","headers","path","method","body","response","rest","commentId","YjsThreadStore","fn","date","v4","yCommentIndex","yArrayFindIndex","yComment","key","reactionsByUser","yArray","predicate","i"],"mappings":"wmBAEO,MAAeA,CAAgB,CAUtC,CCKO,MAAMC,UAA+BD,CAAgB,CAC1D,YACmBE,EACAC,EACjB,CACM,MAAA,EAHW,KAAA,OAAAD,EACA,KAAA,KAAAC,CAAA,CAQnB,iBAA2B,CAClB,MAAA,EAAA,CAMT,cAAcC,EAA8B,CACnC,MAAA,EAAA,CAMT,iBAAiBC,EAA+B,CACvC,OAAAA,EAAQ,SAAW,KAAK,MAAA,CAMjC,iBAAiBA,EAA+B,CAC9C,OAAOA,EAAQ,SAAW,KAAK,QAAU,KAAK,OAAS,QAAA,CAMzD,gBAAgBD,EAA8B,CAC5C,OAAO,KAAK,OAAS,QAAA,CAMvB,iBAAiBA,EAA8B,CACtC,MAAA,EAAA,CAMT,mBAAmBA,EAA8B,CACxC,MAAA,EAAA,CAQT,eAAeC,EAAsBC,EAAyB,CAC5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/B5C,EAAA,IAAI,SAAU,KAAK,MAAM,EAClBkD,EAAA,IAAID,EAAKjD,CAAQ,CAAA,CACnC,CAEJ,GAEOG,EAAA,sBAAiB,KAAK,SAC1BG,GAAoE,CACnE,MAAMuB,EAAU,KAAK,YAAY,IAAIvB,EAAQ,QAAQ,EACrD,GAAI,CAACuB,EACG,MAAA,IAAI,MAAM,kBAAkB,EAGpC,MAAMiB,EAAgBC,EACpBlB,EAAQ,IAAI,UAAU,EACrB/B,GAAYA,EAAQ,IAAI,IAAI,IAAMQ,EAAQ,SAC7C,EAEA,GAAIwC,IAAkB,GACd,MAAA,IAAI,MAAM,mBAAmB,EAGrC,MAAME,EAAWnB,EAAQ,IAAI,UAAU,EAAE,IAAIiB,CAAa,EAGxD,GAAA,CAAC,KAAK,KAAK,kBAAkBrB,EAAcuB,CAAQ,EAAG1C,EAAQ,KAAK,EAE7D,MAAA,IAAI,MAAM,gBAAgB,EAGlC,MAAM2C,EAAM,GAAG,KAAK,MAAM,IAAI3C,EAAQ,KAAK,GAEnB0C,EAAS,IAAI,iBAAiB,EAEtC,OAAOC,CAAG,CAAA,CAE9B,GA7SmB,KAAA,OAAAtD,CAAA,CA8SrB,CAEA,SAASoD,EACPI,EACAC,EACA,CACA,QAASC,EAAI,EAAGA,EAAIF,EAAO,OAAQE,IACjC,GAAID,EAAUD,EAAO,IAAIE,CAAC,CAAC,EAClB,OAAAA,EAGJ,MAAA,EACT"}
1
+ {"version":3,"file":"comments.cjs","sources":["../src/comments/threadstore/ThreadStoreAuth.ts","../src/comments/threadstore/DefaultThreadStoreAuth.ts","../src/comments/threadstore/ThreadStore.ts","../src/comments/threadstore/TipTapThreadStore.ts","../src/comments/threadstore/yjs/yjsHelpers.ts","../src/comments/threadstore/yjs/YjsThreadStoreBase.ts","../src/comments/threadstore/yjs/RESTYjsThreadStore.ts","../src/comments/threadstore/yjs/YjsThreadStore.ts"],"sourcesContent":["import { CommentData, ThreadData } from \"../types.js\";\n\nexport abstract class ThreadStoreAuth {\n abstract canCreateThread(): boolean;\n abstract canAddComment(thread: ThreadData): boolean;\n abstract canUpdateComment(comment: CommentData): boolean;\n abstract canDeleteComment(comment: CommentData): boolean;\n abstract canDeleteThread(thread: ThreadData): boolean;\n abstract canResolveThread(thread: ThreadData): boolean;\n abstract canUnresolveThread(thread: ThreadData): boolean;\n abstract canAddReaction(comment: CommentData, emoji?: string): boolean;\n abstract canDeleteReaction(comment: CommentData, emoji?: string): boolean;\n}\n","import { CommentData, ThreadData } from \"../types.js\";\nimport { ThreadStoreAuth } from \"./ThreadStoreAuth.js\";\n\n/*\n * The DefaultThreadStoreAuth class defines the authorization rules for interacting with comments.\n * We take a role (\"comment\" or \"editor\") and implement the rules.\n *\n * This class is then used in the UI to show / hide specific interactions.\n *\n * Rules:\n * - View-only users should not be able to see any comments\n * - Comment-only users and editors can:\n * - - create new comments / replies / reactions\n * - - edit / delete their own comments / reactions\n * - - resolve / unresolve threads\n * - Editors can also delete any comment or thread\n */\nexport class DefaultThreadStoreAuth extends ThreadStoreAuth {\n constructor(\n private readonly userId: string,\n private readonly role: \"comment\" | \"editor\",\n ) {\n super();\n }\n\n /**\n * Auth: should be possible by anyone with comment access\n */\n canCreateThread(): boolean {\n return true;\n }\n\n /**\n * Auth: should be possible by anyone with comment access\n */\n canAddComment(_thread: ThreadData): boolean {\n return true;\n }\n\n /**\n * Auth: should only be possible by the comment author\n */\n canUpdateComment(comment: CommentData): boolean {\n return comment.userId === this.userId;\n }\n\n /**\n * Auth: should be possible by the comment author OR an editor of the document\n */\n canDeleteComment(comment: CommentData): boolean {\n return comment.userId === this.userId || this.role === \"editor\";\n }\n\n /**\n * Auth: should only be possible by an editor of the document\n */\n canDeleteThread(_thread: ThreadData): boolean {\n return this.role === \"editor\";\n }\n\n /**\n * Auth: should be possible by anyone with comment access\n */\n canResolveThread(_thread: ThreadData): boolean {\n return true;\n }\n\n /**\n * Auth: should be possible by anyone with comment access\n */\n canUnresolveThread(_thread: ThreadData): boolean {\n return true;\n }\n\n /**\n * Auth: should be possible by anyone with comment access\n *\n * Note: will also check if the user has already reacted with the same emoji. TBD: is that a nice design or should this responsibility be outside of auth?\n */\n canAddReaction(comment: CommentData, emoji?: string): boolean {\n if (!emoji) {\n return true;\n }\n\n return !comment.reactions.some(\n (reaction) =>\n reaction.emoji === emoji && reaction.userIds.includes(this.userId),\n );\n }\n\n /**\n * Auth: should be possible by anyone with comment access\n *\n * Note: will also check if the user has already reacted with the same emoji. TBD: is that a nice design or should this responsibility be outside of auth?\n */\n canDeleteReaction(comment: CommentData, emoji?: string): boolean {\n if (!emoji) {\n return true;\n }\n\n return comment.reactions.some(\n (reaction) =>\n reaction.emoji === emoji && reaction.userIds.includes(this.userId),\n );\n }\n}\n","import { CommentBody, CommentData, ThreadData } from \"../types.js\";\nimport { ThreadStoreAuth } from \"./ThreadStoreAuth.js\";\n\n/**\n * ThreadStore is an abstract class that defines the interface\n * to read / add / update / delete threads and comments.\n */\nexport abstract class ThreadStore {\n public readonly auth: ThreadStoreAuth;\n\n constructor(auth: ThreadStoreAuth) {\n this.auth = auth;\n }\n\n /**\n * A \"thread\" in the ThreadStore only contains information about the content\n * of the thread / comments. It does not contain information about the position.\n *\n * This function can be implemented to store the thread in the document (by creating a mark)\n * If not implemented, default behavior will apply (creating the mark via TipTap)\n * See CommentsPlugin.ts for more details.\n */\n abstract addThreadToDocument?(options: {\n threadId: string;\n selection: {\n prosemirror: {\n head: number;\n anchor: number;\n };\n yjs?: {\n head: any;\n anchor: any;\n };\n };\n }): Promise<void>;\n\n /**\n * Creates a new thread with an initial comment.\n */\n abstract createThread(options: {\n initialComment: {\n body: CommentBody;\n metadata?: any;\n };\n metadata?: any;\n }): Promise<ThreadData>;\n\n /**\n * Adds a comment to a thread.\n */\n abstract addComment(options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n }): Promise<CommentData>;\n\n /**\n * Updates a comment in a thread.\n */\n abstract updateComment(options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n commentId: string;\n }): Promise<void>;\n\n /**\n * Deletes a comment from a thread.\n */\n abstract deleteComment(options: {\n threadId: string;\n commentId: string;\n }): Promise<void>;\n\n /**\n * Deletes a thread.\n */\n abstract deleteThread(options: { threadId: string }): Promise<void>;\n\n /**\n * Marks a thread as resolved.\n */\n abstract resolveThread(options: { threadId: string }): Promise<void>;\n\n /**\n * Marks a thread as unresolved.\n */\n abstract unresolveThread(options: { threadId: string }): Promise<void>;\n\n /**\n * Adds a reaction to a comment.\n *\n * Auth: should be possible by anyone with comment access\n */\n abstract addReaction(options: {\n threadId: string;\n commentId: string;\n emoji: string;\n }): Promise<void>;\n\n /**\n * Deletes a reaction from a comment.\n *\n * Auth: should be possible by the reaction author\n */\n abstract deleteReaction(options: {\n threadId: string;\n commentId: string;\n emoji: string;\n }): Promise<void>;\n\n /**\n * Retrieve data for a specific thread.\n */\n abstract getThread(threadId: string): ThreadData;\n\n /**\n * Retrieve all threads.\n */\n abstract getThreads(): Map<string, ThreadData>;\n\n /**\n * Subscribe to changes in the thread store.\n *\n * @returns a function to unsubscribe from the thread store\n */\n abstract subscribe(\n cb: (threads: Map<string, ThreadData>) => void,\n ): () => void;\n}\n","import type {\n TCollabComment,\n TCollabThread,\n TiptapCollabProvider,\n} from \"@hocuspocus/provider\";\nimport {\n CommentBody,\n CommentData,\n CommentReactionData,\n ThreadData,\n} from \"../types.js\";\nimport { ThreadStore } from \"./ThreadStore.js\";\nimport { ThreadStoreAuth } from \"./ThreadStoreAuth.js\";\n\ntype ReactionAsTiptapData = {\n emoji: string;\n createdAt: number;\n userId: string;\n};\n\n/**\n * The `TiptapThreadStore` integrates with Tiptap's collaboration provider for comment management.\n * You can pass a `TiptapCollabProvider` to the constructor which takes care of storing the comments.\n *\n * Under the hood, this actually works similarly to the `YjsThreadStore` implementation. (comments are stored in the Yjs document)\n */\nexport class TiptapThreadStore extends ThreadStore {\n constructor(\n private readonly userId: string,\n private readonly provider: TiptapCollabProvider,\n auth: ThreadStoreAuth, // TODO: use?\n ) {\n super(auth);\n }\n\n /**\n * Creates a new thread with an initial comment.\n */\n public async createThread(options: {\n initialComment: {\n body: CommentBody;\n metadata?: any;\n };\n metadata?: any;\n }): Promise<ThreadData> {\n let thread = this.provider.createThread({\n data: options.metadata,\n });\n\n thread = this.provider.addComment(thread.id, {\n content: options.initialComment.body,\n data: {\n metadata: options.initialComment.metadata,\n userId: this.userId,\n },\n });\n\n return this.tiptapThreadToThreadData(thread);\n }\n\n // TipTapThreadStore does not support addThreadToDocument\n public addThreadToDocument = undefined;\n\n /**\n * Adds a comment to a thread.\n */\n public async addComment(options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n }): Promise<CommentBody> {\n const thread = this.provider.addComment(options.threadId, {\n content: options.comment.body,\n data: {\n metadata: options.comment.metadata,\n userId: this.userId,\n },\n });\n\n return this.tiptapCommentToCommentData(\n thread.comments[thread.comments.length - 1],\n );\n }\n\n /**\n * Updates a comment in a thread.\n */\n public async updateComment(options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n commentId: string;\n }) {\n const comment = this.provider.getThreadComment(\n options.threadId,\n options.commentId,\n true,\n );\n\n if (!comment) {\n throw new Error(\"Comment not found\");\n }\n\n this.provider.updateComment(options.threadId, options.commentId, {\n content: options.comment.body,\n data: {\n ...comment.data,\n metadata: options.comment.metadata,\n },\n });\n }\n\n private tiptapCommentToCommentData(comment: TCollabComment): CommentData {\n const reactions: CommentReactionData[] = [];\n\n for (const reaction of (comment.data?.reactions ||\n []) as ReactionAsTiptapData[]) {\n const existingReaction = reactions.find(\n (r) => r.emoji === reaction.emoji,\n );\n if (existingReaction) {\n existingReaction.userIds.push(reaction.userId);\n existingReaction.createdAt = new Date(\n Math.min(existingReaction.createdAt.getTime(), reaction.createdAt),\n );\n } else {\n reactions.push({\n emoji: reaction.emoji,\n createdAt: new Date(reaction.createdAt),\n userIds: [reaction.userId],\n });\n }\n }\n\n return {\n type: \"comment\",\n id: comment.id,\n body: comment.content,\n metadata: comment.data?.metadata,\n userId: comment.data?.userId,\n createdAt: new Date(comment.createdAt),\n updatedAt: new Date(comment.updatedAt),\n reactions,\n };\n }\n\n private tiptapThreadToThreadData(thread: TCollabThread): ThreadData {\n return {\n type: \"thread\",\n id: thread.id,\n comments: thread.comments.map((comment) =>\n this.tiptapCommentToCommentData(comment),\n ),\n resolved: !!thread.resolvedAt,\n metadata: thread.data?.metadata,\n createdAt: new Date(thread.createdAt),\n updatedAt: new Date(thread.updatedAt),\n };\n }\n\n /**\n * Deletes a comment from a thread.\n */\n public async deleteComment(options: { threadId: string; commentId: string }) {\n this.provider.deleteComment(options.threadId, options.commentId);\n }\n\n /**\n * Deletes a thread.\n */\n public async deleteThread(options: { threadId: string }) {\n this.provider.deleteThread(options.threadId);\n }\n\n /**\n * Marks a thread as resolved.\n */\n public async resolveThread(options: { threadId: string }) {\n this.provider.updateThread(options.threadId, {\n resolvedAt: new Date().toISOString(),\n });\n }\n\n /**\n * Marks a thread as unresolved.\n */\n public async unresolveThread(options: { threadId: string }) {\n this.provider.updateThread(options.threadId, {\n resolvedAt: null,\n });\n }\n\n /**\n * Adds a reaction to a comment.\n *\n * Auth: should be possible by anyone with comment access\n */\n public async addReaction(options: {\n threadId: string;\n commentId: string;\n emoji: string;\n }) {\n const comment = this.provider.getThreadComment(\n options.threadId,\n options.commentId,\n true,\n );\n\n if (!comment) {\n throw new Error(\"Comment not found\");\n }\n\n this.provider.updateComment(options.threadId, options.commentId, {\n data: {\n ...comment.data,\n reactions: [\n ...((comment.data?.reactions || []) as ReactionAsTiptapData[]),\n {\n emoji: options.emoji,\n createdAt: Date.now(),\n userId: this.userId,\n },\n ],\n },\n });\n }\n\n /**\n * Deletes a reaction from a comment.\n *\n * Auth: should be possible by the reaction author\n */\n public async deleteReaction(options: {\n threadId: string;\n commentId: string;\n emoji: string;\n }) {\n const comment = this.provider.getThreadComment(\n options.threadId,\n options.commentId,\n true,\n );\n\n if (!comment) {\n throw new Error(\"Comment not found\");\n }\n\n this.provider.updateComment(options.threadId, options.commentId, {\n data: {\n ...comment.data,\n reactions: (\n (comment.data?.reactions || []) as ReactionAsTiptapData[]\n ).filter(\n (reaction) =>\n reaction.emoji !== options.emoji && reaction.userId !== this.userId,\n ),\n },\n });\n }\n\n public getThread(threadId: string): ThreadData {\n const thread = this.provider.getThread(threadId);\n\n if (!thread) {\n throw new Error(\"Thread not found\");\n }\n\n return this.tiptapThreadToThreadData(thread);\n }\n\n public getThreads(): Map<string, ThreadData> {\n return new Map(\n this.provider\n .getThreads()\n .map((thread) => [thread.id, this.tiptapThreadToThreadData(thread)]),\n );\n }\n\n public subscribe(cb: (threads: Map<string, ThreadData>) => void): () => void {\n const newCb = () => {\n cb(this.getThreads());\n };\n this.provider.watchThreads(newCb);\n return () => {\n this.provider.unwatchThreads(newCb);\n };\n }\n}\n","import * as Y from \"yjs\";\nimport { CommentData, CommentReactionData, ThreadData } from \"../../types.js\";\n\nexport function commentToYMap(comment: CommentData) {\n const yMap = new Y.Map<any>();\n yMap.set(\"id\", comment.id);\n yMap.set(\"userId\", comment.userId);\n yMap.set(\"createdAt\", comment.createdAt.getTime());\n yMap.set(\"updatedAt\", comment.updatedAt.getTime());\n if (comment.deletedAt) {\n yMap.set(\"deletedAt\", comment.deletedAt.getTime());\n yMap.set(\"body\", undefined);\n } else {\n yMap.set(\"body\", comment.body);\n }\n if (comment.reactions.length > 0) {\n throw new Error(\"Reactions should be empty in commentToYMap\");\n }\n\n /**\n * Reactions are stored in a map keyed by {userId-emoji},\n * this makes it easy to add / remove reactions and in a way that works local-first.\n * The cost is that \"reading\" the reactions is a bit more complex (see yMapToReactions).\n */\n yMap.set(\"reactionsByUser\", new Y.Map());\n yMap.set(\"metadata\", comment.metadata);\n\n return yMap;\n}\n\nexport function threadToYMap(thread: ThreadData) {\n const yMap = new Y.Map();\n yMap.set(\"id\", thread.id);\n yMap.set(\"createdAt\", thread.createdAt.getTime());\n yMap.set(\"updatedAt\", thread.updatedAt.getTime());\n const commentsArray = new Y.Array<Y.Map<any>>();\n\n commentsArray.push(thread.comments.map((comment) => commentToYMap(comment)));\n\n yMap.set(\"comments\", commentsArray);\n yMap.set(\"resolved\", thread.resolved);\n yMap.set(\"resolvedUpdatedAt\", thread.resolvedUpdatedAt?.getTime());\n yMap.set(\"resolvedBy\", thread.resolvedBy);\n yMap.set(\"metadata\", thread.metadata);\n return yMap;\n}\n\ntype SingleUserCommentReactionData = {\n emoji: string;\n createdAt: Date;\n userId: string;\n};\n\nexport function yMapToReaction(\n yMap: Y.Map<any>,\n): SingleUserCommentReactionData {\n return {\n emoji: yMap.get(\"emoji\"),\n createdAt: new Date(yMap.get(\"createdAt\")),\n userId: yMap.get(\"userId\"),\n };\n}\n\nfunction yMapToReactions(yMap: Y.Map<any>): CommentReactionData[] {\n const flatReactions = [...yMap.values()].map((reaction: Y.Map<any>) =>\n yMapToReaction(reaction),\n );\n // combine reactions by the same emoji\n return flatReactions.reduce(\n (acc: CommentReactionData[], reaction: SingleUserCommentReactionData) => {\n const existingReaction = acc.find((r) => r.emoji === reaction.emoji);\n if (existingReaction) {\n existingReaction.userIds.push(reaction.userId);\n existingReaction.createdAt = new Date(\n Math.min(\n existingReaction.createdAt.getTime(),\n reaction.createdAt.getTime(),\n ),\n );\n } else {\n acc.push({\n emoji: reaction.emoji,\n createdAt: reaction.createdAt,\n userIds: [reaction.userId],\n });\n }\n return acc;\n },\n [] as CommentReactionData[],\n );\n}\n\nexport function yMapToComment(yMap: Y.Map<any>): CommentData {\n return {\n type: \"comment\",\n id: yMap.get(\"id\"),\n userId: yMap.get(\"userId\"),\n createdAt: new Date(yMap.get(\"createdAt\")),\n updatedAt: new Date(yMap.get(\"updatedAt\")),\n deletedAt: yMap.get(\"deletedAt\")\n ? new Date(yMap.get(\"deletedAt\"))\n : undefined,\n reactions: yMapToReactions(yMap.get(\"reactionsByUser\")),\n metadata: yMap.get(\"metadata\"),\n body: yMap.get(\"body\"),\n };\n}\n\nexport function yMapToThread(yMap: Y.Map<any>): ThreadData {\n return {\n type: \"thread\",\n id: yMap.get(\"id\"),\n createdAt: new Date(yMap.get(\"createdAt\")),\n updatedAt: new Date(yMap.get(\"updatedAt\")),\n comments: ((yMap.get(\"comments\") as Y.Array<Y.Map<any>>) || []).map(\n (comment) => yMapToComment(comment),\n ),\n resolved: yMap.get(\"resolved\"),\n resolvedUpdatedAt: new Date(yMap.get(\"resolvedUpdatedAt\")),\n resolvedBy: yMap.get(\"resolvedBy\"),\n metadata: yMap.get(\"metadata\"),\n };\n}\n","import * as Y from \"yjs\";\nimport { ThreadData } from \"../../types.js\";\nimport { ThreadStore } from \"../ThreadStore.js\";\nimport { ThreadStoreAuth } from \"../ThreadStoreAuth.js\";\nimport { yMapToThread } from \"./yjsHelpers.js\";\n\n/**\n * This is an abstract class that only implements the READ methods required by the ThreadStore interface.\n * The data is read from a Yjs Map.\n */\nexport abstract class YjsThreadStoreBase extends ThreadStore {\n constructor(\n protected readonly threadsYMap: Y.Map<any>,\n auth: ThreadStoreAuth,\n ) {\n super(auth);\n }\n\n // TODO: async / reactive interface?\n public getThread(threadId: string) {\n const yThread = this.threadsYMap.get(threadId);\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n const thread = yMapToThread(yThread);\n return thread;\n }\n\n public getThreads(): Map<string, ThreadData> {\n const threadMap = new Map<string, ThreadData>();\n this.threadsYMap.forEach((yThread, id) => {\n if (yThread instanceof Y.Map) {\n threadMap.set(id, yMapToThread(yThread));\n }\n });\n return threadMap;\n }\n\n public subscribe(cb: (threads: Map<string, ThreadData>) => void) {\n const observer = () => {\n cb(this.getThreads());\n };\n\n this.threadsYMap.observeDeep(observer);\n\n return () => {\n this.threadsYMap.unobserveDeep(observer);\n };\n }\n}\n","import * as Y from \"yjs\";\nimport { CommentBody } from \"../../types.js\";\nimport { ThreadStoreAuth } from \"../ThreadStoreAuth.js\";\nimport { YjsThreadStoreBase } from \"./YjsThreadStoreBase.js\";\n\n/**\n * This is a REST-based implementation of the YjsThreadStoreBase.\n * It Reads data directly from the underlying document (same as YjsThreadStore),\n * but for Writes, it sends data to a REST API that should:\n * - check the user has the correct permissions to make the desired changes\n * - apply the updates to the underlying Yjs document\n *\n * (see https://github.com/TypeCellOS/BlockNote-demo-nextjs-hocuspocus)\n *\n * The reason we still use the Yjs document as underlying storage is that it makes it easy to\n * sync updates in real-time to other collaborators.\n * (but technically, you could also implement a different storage altogether\n * and not store the thread related data in the Yjs document)\n */\nexport class RESTYjsThreadStore extends YjsThreadStoreBase {\n constructor(\n private readonly BASE_URL: string,\n private readonly headers: Record<string, string>,\n threadsYMap: Y.Map<any>,\n auth: ThreadStoreAuth,\n ) {\n super(threadsYMap, auth);\n }\n\n private doRequest = async (path: string, method: string, body?: any) => {\n const response = await fetch(`${this.BASE_URL}${path}`, {\n method,\n body: JSON.stringify(body),\n headers: {\n \"Content-Type\": \"application/json\",\n ...this.headers,\n },\n });\n\n if (!response.ok) {\n throw new Error(`Failed to ${method} ${path}: ${response.statusText}`);\n }\n\n return response.json();\n };\n\n public addThreadToDocument = async (options: {\n threadId: string;\n selection: {\n prosemirror: {\n head: number;\n anchor: number;\n };\n yjs: {\n head: any;\n anchor: any;\n };\n };\n }) => {\n const { threadId, ...rest } = options;\n return this.doRequest(`/${threadId}/addToDocument`, \"POST\", rest);\n };\n\n public createThread = async (options: {\n initialComment: {\n body: CommentBody;\n metadata?: any;\n };\n metadata?: any;\n }) => {\n return this.doRequest(\"\", \"POST\", options);\n };\n\n public addComment = (options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n }) => {\n const { threadId, ...rest } = options;\n return this.doRequest(`/${threadId}/comments`, \"POST\", rest);\n };\n\n public updateComment = (options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n commentId: string;\n }) => {\n const { threadId, commentId, ...rest } = options;\n return this.doRequest(`/${threadId}/comments/${commentId}`, \"PUT\", rest);\n };\n\n public deleteComment = (options: {\n threadId: string;\n commentId: string;\n softDelete?: boolean;\n }) => {\n const { threadId, commentId, ...rest } = options;\n return this.doRequest(\n `/${threadId}/comments/${commentId}?soft=${!!rest.softDelete}`,\n \"DELETE\",\n );\n };\n\n public deleteThread = (options: { threadId: string }) => {\n return this.doRequest(`/${options.threadId}`, \"DELETE\");\n };\n\n public resolveThread = (options: { threadId: string }) => {\n return this.doRequest(`/${options.threadId}/resolve`, \"POST\");\n };\n\n public unresolveThread = (options: { threadId: string }) => {\n return this.doRequest(`/${options.threadId}/unresolve`, \"POST\");\n };\n\n public addReaction = (options: {\n threadId: string;\n commentId: string;\n emoji: string;\n }) => {\n const { threadId, commentId, ...rest } = options;\n return this.doRequest(\n `/${threadId}/comments/${commentId}/reactions`,\n \"POST\",\n rest,\n );\n };\n\n public deleteReaction = (options: {\n threadId: string;\n commentId: string;\n emoji: string;\n }) => {\n return this.doRequest(\n `/${options.threadId}/comments/${options.commentId}/reactions/${options.emoji}`,\n \"DELETE\",\n );\n };\n}\n","import { v4 } from \"uuid\";\nimport * as Y from \"yjs\";\nimport { CommentBody, CommentData, ThreadData } from \"../../types.js\";\nimport { ThreadStoreAuth } from \"../ThreadStoreAuth.js\";\nimport { YjsThreadStoreBase } from \"./YjsThreadStoreBase.js\";\nimport {\n commentToYMap,\n threadToYMap,\n yMapToComment,\n yMapToThread,\n} from \"./yjsHelpers.js\";\n\n/**\n * This is a Yjs-based implementation of the ThreadStore interface.\n *\n * It reads and writes thread / comments information directly to the underlying Yjs Document.\n *\n * @important While this is the easiest to add to your app, there are two challenges:\n * - The user needs to be able to write to the Yjs document to store the information.\n * So a user without write access to the Yjs document cannot leave any comments.\n * - Even with write access, the operations are not secure. Unless your Yjs server\n * guards against malicious operations, it's technically possible for one user to make changes to another user's comments, etc.\n * (even though these options are not visible in the UI, a malicious user can make unauthorized changes to the underlying Yjs document)\n */\nexport class YjsThreadStore extends YjsThreadStoreBase {\n constructor(\n private readonly userId: string,\n threadsYMap: Y.Map<any>,\n auth: ThreadStoreAuth,\n ) {\n super(threadsYMap, auth);\n }\n\n private transact = <T, R>(\n fn: (options: T) => R,\n ): ((options: T) => Promise<R>) => {\n return async (options: T) => {\n return this.threadsYMap.doc!.transact(() => {\n return fn(options);\n });\n };\n };\n\n public createThread = this.transact(\n (options: {\n initialComment: {\n body: CommentBody;\n metadata?: any;\n };\n metadata?: any;\n }) => {\n if (!this.auth.canCreateThread()) {\n throw new Error(\"Not authorized\");\n }\n\n const date = new Date();\n\n const comment: CommentData = {\n type: \"comment\",\n id: v4(),\n userId: this.userId,\n createdAt: date,\n updatedAt: date,\n reactions: [],\n metadata: options.initialComment.metadata,\n body: options.initialComment.body,\n };\n\n const thread: ThreadData = {\n type: \"thread\",\n id: v4(),\n createdAt: date,\n updatedAt: date,\n comments: [comment],\n resolved: false,\n metadata: options.metadata,\n };\n\n this.threadsYMap.set(thread.id, threadToYMap(thread));\n\n return thread;\n },\n );\n\n // YjsThreadStore does not support addThreadToDocument\n public addThreadToDocument = undefined;\n\n public addComment = this.transact(\n (options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n }) => {\n const yThread = this.threadsYMap.get(options.threadId);\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n if (!this.auth.canAddComment(yMapToThread(yThread))) {\n throw new Error(\"Not authorized\");\n }\n\n const date = new Date();\n const comment: CommentData = {\n type: \"comment\",\n id: v4(),\n userId: this.userId,\n createdAt: date,\n updatedAt: date,\n deletedAt: undefined,\n reactions: [],\n metadata: options.comment.metadata,\n body: options.comment.body,\n };\n\n (yThread.get(\"comments\") as Y.Array<Y.Map<any>>).push([\n commentToYMap(comment),\n ]);\n\n yThread.set(\"updatedAt\", new Date().getTime());\n return comment;\n },\n );\n\n public updateComment = this.transact(\n (options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n commentId: string;\n }) => {\n const yThread = this.threadsYMap.get(options.threadId);\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n const yCommentIndex = yArrayFindIndex(\n yThread.get(\"comments\"),\n (comment) => comment.get(\"id\") === options.commentId,\n );\n\n if (yCommentIndex === -1) {\n throw new Error(\"Comment not found\");\n }\n\n const yComment = yThread.get(\"comments\").get(yCommentIndex);\n\n if (!this.auth.canUpdateComment(yMapToComment(yComment))) {\n throw new Error(\"Not authorized\");\n }\n\n yComment.set(\"body\", options.comment.body);\n yComment.set(\"updatedAt\", new Date().getTime());\n yComment.set(\"metadata\", options.comment.metadata);\n },\n );\n\n public deleteComment = this.transact(\n (options: {\n threadId: string;\n commentId: string;\n softDelete?: boolean;\n }) => {\n const yThread = this.threadsYMap.get(options.threadId);\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n const yCommentIndex = yArrayFindIndex(\n yThread.get(\"comments\"),\n (comment) => comment.get(\"id\") === options.commentId,\n );\n\n if (yCommentIndex === -1) {\n throw new Error(\"Comment not found\");\n }\n\n const yComment = yThread.get(\"comments\").get(yCommentIndex);\n\n if (!this.auth.canDeleteComment(yMapToComment(yComment))) {\n throw new Error(\"Not authorized\");\n }\n\n if (yComment.get(\"deletedAt\")) {\n throw new Error(\"Comment already deleted\");\n }\n\n if (options.softDelete) {\n yComment.set(\"deletedAt\", new Date().getTime());\n yComment.set(\"body\", undefined);\n } else {\n yThread.get(\"comments\").delete(yCommentIndex);\n }\n\n if (\n (yThread.get(\"comments\") as Y.Array<any>)\n .toArray()\n .every((comment) => comment.get(\"deletedAt\"))\n ) {\n // all comments deleted\n if (options.softDelete) {\n yThread.set(\"deletedAt\", new Date().getTime());\n } else {\n this.threadsYMap.delete(options.threadId);\n }\n }\n\n yThread.set(\"updatedAt\", new Date().getTime());\n },\n );\n\n public deleteThread = this.transact((options: { threadId: string }) => {\n if (\n !this.auth.canDeleteThread(\n yMapToThread(this.threadsYMap.get(options.threadId)),\n )\n ) {\n throw new Error(\"Not authorized\");\n }\n\n this.threadsYMap.delete(options.threadId);\n });\n\n public resolveThread = this.transact((options: { threadId: string }) => {\n const yThread = this.threadsYMap.get(options.threadId);\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n if (!this.auth.canResolveThread(yMapToThread(yThread))) {\n throw new Error(\"Not authorized\");\n }\n\n yThread.set(\"resolved\", true);\n yThread.set(\"resolvedUpdatedAt\", new Date().getTime());\n yThread.set(\"resolvedBy\", this.userId);\n });\n\n public unresolveThread = this.transact((options: { threadId: string }) => {\n const yThread = this.threadsYMap.get(options.threadId);\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n if (!this.auth.canUnresolveThread(yMapToThread(yThread))) {\n throw new Error(\"Not authorized\");\n }\n\n yThread.set(\"resolved\", false);\n yThread.set(\"resolvedUpdatedAt\", new Date().getTime());\n });\n\n public addReaction = this.transact(\n (options: { threadId: string; commentId: string; emoji: string }) => {\n const yThread = this.threadsYMap.get(options.threadId);\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n const yCommentIndex = yArrayFindIndex(\n yThread.get(\"comments\"),\n (comment) => comment.get(\"id\") === options.commentId,\n );\n\n if (yCommentIndex === -1) {\n throw new Error(\"Comment not found\");\n }\n\n const yComment = yThread.get(\"comments\").get(yCommentIndex);\n\n if (!this.auth.canAddReaction(yMapToComment(yComment), options.emoji)) {\n throw new Error(\"Not authorized\");\n }\n\n const date = new Date();\n\n const key = `${this.userId}-${options.emoji}`;\n\n const reactionsByUser = yComment.get(\"reactionsByUser\");\n\n if (reactionsByUser.has(key)) {\n // already exists\n return;\n } else {\n const reaction = new Y.Map();\n reaction.set(\"emoji\", options.emoji);\n reaction.set(\"createdAt\", date.getTime());\n reaction.set(\"userId\", this.userId);\n reactionsByUser.set(key, reaction);\n }\n },\n );\n\n public deleteReaction = this.transact(\n (options: { threadId: string; commentId: string; emoji: string }) => {\n const yThread = this.threadsYMap.get(options.threadId);\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n const yCommentIndex = yArrayFindIndex(\n yThread.get(\"comments\"),\n (comment) => comment.get(\"id\") === options.commentId,\n );\n\n if (yCommentIndex === -1) {\n throw new Error(\"Comment not found\");\n }\n\n const yComment = yThread.get(\"comments\").get(yCommentIndex);\n\n if (\n !this.auth.canDeleteReaction(yMapToComment(yComment), options.emoji)\n ) {\n throw new Error(\"Not authorized\");\n }\n\n const key = `${this.userId}-${options.emoji}`;\n\n const reactionsByUser = yComment.get(\"reactionsByUser\");\n\n reactionsByUser.delete(key);\n },\n );\n}\n\nfunction yArrayFindIndex(\n yArray: Y.Array<any>,\n predicate: (item: any) => boolean,\n) {\n for (let i = 0; i < yArray.length; i++) {\n if (predicate(yArray.get(i))) {\n return i;\n }\n }\n return -1;\n}\n"],"names":["ThreadStoreAuth","DefaultThreadStoreAuth","userId","role","_thread","comment","emoji","reaction","ThreadStore","auth","__publicField","TiptapThreadStore","provider","options","thread","reactions","_a","existingReaction","r","_b","_c","threadId","cb","newCb","commentToYMap","yMap","Y","threadToYMap","commentsArray","yMapToReaction","yMapToReactions","acc","yMapToComment","yMapToThread","YjsThreadStoreBase","threadsYMap","yThread","threadMap","id","observer","RESTYjsThreadStore","BASE_URL","headers","path","method","body","response","rest","commentId","YjsThreadStore","fn","date","v4","yCommentIndex","yArrayFindIndex","yComment","key","reactionsByUser","yArray","predicate","i"],"mappings":"wmBAEO,MAAeA,CAAgB,CAUtC,CCKO,MAAMC,UAA+BD,CAAgB,CAC1D,YACmBE,EACAC,EACjB,CACA,MAAA,EAHiB,KAAA,OAAAD,EACA,KAAA,KAAAC,CAGnB,CAKA,iBAA2B,CACzB,MAAO,EACT,CAKA,cAAcC,EAA8B,CAC1C,MAAO,EACT,CAKA,iBAAiBC,EAA+B,CAC9C,OAAOA,EAAQ,SAAW,KAAK,MACjC,CAKA,iBAAiBA,EAA+B,CAC9C,OAAOA,EAAQ,SAAW,KAAK,QAAU,KAAK,OAAS,QACzD,CAKA,gBAAgBD,EAA8B,CAC5C,OAAO,KAAK,OAAS,QACvB,CAKA,iBAAiBA,EAA8B,CAC7C,MAAO,EACT,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IAAImB,EAAE,IACvBnB,EAAS,IAAI,QAASM,EAAQ,KAAK,EACnCN,EAAS,IAAI,YAAa4C,EAAK,QAAA,CAAS,EACxC5C,EAAS,IAAI,SAAU,KAAK,MAAM,EAClCkD,EAAgB,IAAID,EAAKjD,CAAQ,CACnC,CACF,CAAA,GAGKG,EAAA,sBAAiB,KAAK,SAC1BG,GAAoE,CACnE,MAAMuB,EAAU,KAAK,YAAY,IAAIvB,EAAQ,QAAQ,EACrD,GAAI,CAACuB,EACH,MAAM,IAAI,MAAM,kBAAkB,EAGpC,MAAMiB,EAAgBC,EACpBlB,EAAQ,IAAI,UAAU,EACrB/B,GAAYA,EAAQ,IAAI,IAAI,IAAMQ,EAAQ,SAAA,EAG7C,GAAIwC,IAAkB,GACpB,MAAM,IAAI,MAAM,mBAAmB,EAGrC,MAAME,EAAWnB,EAAQ,IAAI,UAAU,EAAE,IAAIiB,CAAa,EAE1D,GACE,CAAC,KAAK,KAAK,kBAAkBrB,EAAcuB,CAAQ,EAAG1C,EAAQ,KAAK,EAEnE,MAAM,IAAI,MAAM,gBAAgB,EAGlC,MAAM2C,EAAM,GAAG,KAAK,MAAM,IAAI3C,EAAQ,KAAK,GAEnB0C,EAAS,IAAI,iBAAiB,EAEtC,OAAOC,CAAG,CAC5B,CAAA,GA5SiB,KAAA,OAAAtD,CAKnB,CAySF,CAEA,SAASoD,EACPI,EACAC,EACA,CACA,QAASC,EAAI,EAAGA,EAAIF,EAAO,OAAQE,IACjC,GAAID,EAAUD,EAAO,IAAIE,CAAC,CAAC,EACzB,OAAOA,EAGX,MAAO,EACT"}
package/dist/comments.js CHANGED
@@ -1,7 +1,7 @@
1
- var A = Object.defineProperty;
2
- var p = (d, r, e) => r in d ? A(d, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : d[r] = e;
3
- var o = (d, r, e) => p(d, typeof r != "symbol" ? r + "" : r, e);
4
- import * as T from "yjs";
1
+ var p = Object.defineProperty;
2
+ var A = (d, r, e) => r in d ? p(d, r, { enumerable: !0, configurable: !0, writable: !0, value: e }) : d[r] = e;
3
+ var n = (d, r, e) => A(d, typeof r != "symbol" ? r + "" : r, e);
4
+ import * as u from "yjs";
5
5
  import { v4 as I } from "uuid";
6
6
  class v {
7
7
  }
@@ -74,7 +74,7 @@ class b extends v {
74
74
  }
75
75
  class w {
76
76
  constructor(r) {
77
- o(this, "auth");
77
+ n(this, "auth");
78
78
  this.auth = r;
79
79
  }
80
80
  }
@@ -82,7 +82,7 @@ class j extends w {
82
82
  constructor(e, t, a) {
83
83
  super(a);
84
84
  // TipTapThreadStore does not support addThreadToDocument
85
- o(this, "addThreadToDocument");
85
+ n(this, "addThreadToDocument");
86
86
  this.userId = e, this.provider = t;
87
87
  }
88
88
  /**
@@ -135,7 +135,7 @@ class j extends w {
135
135
  });
136
136
  }
137
137
  tiptapCommentToCommentData(e) {
138
- var a, s, n;
138
+ var a, s, o;
139
139
  const t = [];
140
140
  for (const h of ((a = e.data) == null ? void 0 : a.reactions) || []) {
141
141
  const m = t.find(
@@ -154,7 +154,7 @@ class j extends w {
154
154
  id: e.id,
155
155
  body: e.content,
156
156
  metadata: (s = e.data) == null ? void 0 : s.metadata,
157
- userId: (n = e.data) == null ? void 0 : n.userId,
157
+ userId: (o = e.data) == null ? void 0 : o.userId,
158
158
  createdAt: new Date(e.createdAt),
159
159
  updatedAt: new Date(e.updatedAt),
160
160
  reactions: t
@@ -274,16 +274,16 @@ class j extends w {
274
274
  }
275
275
  }
276
276
  function g(d) {
277
- const r = new T.Map();
277
+ const r = new u.Map();
278
278
  if (r.set("id", d.id), r.set("userId", d.userId), r.set("createdAt", d.createdAt.getTime()), r.set("updatedAt", d.updatedAt.getTime()), d.deletedAt ? (r.set("deletedAt", d.deletedAt.getTime()), r.set("body", void 0)) : r.set("body", d.body), d.reactions.length > 0)
279
279
  throw new Error("Reactions should be empty in commentToYMap");
280
- return r.set("reactionsByUser", new T.Map()), r.set("metadata", d.metadata), r;
280
+ return r.set("reactionsByUser", new u.Map()), r.set("metadata", d.metadata), r;
281
281
  }
282
282
  function y(d) {
283
283
  var t;
284
- const r = new T.Map();
284
+ const r = new u.Map();
285
285
  r.set("id", d.id), r.set("createdAt", d.createdAt.getTime()), r.set("updatedAt", d.updatedAt.getTime());
286
- const e = new T.Array();
286
+ const e = new u.Array();
287
287
  return e.push(d.comments.map((a) => g(a))), r.set("comments", e), r.set("resolved", d.resolved), r.set("resolvedUpdatedAt", (t = d.resolvedUpdatedAt) == null ? void 0 : t.getTime()), r.set("resolvedBy", d.resolvedBy), r.set("metadata", d.metadata), r;
288
288
  }
289
289
  function C(d) {
@@ -313,7 +313,7 @@ function D(d) {
313
313
  []
314
314
  );
315
315
  }
316
- function u(d) {
316
+ function T(d) {
317
317
  return {
318
318
  type: "comment",
319
319
  id: d.get("id"),
@@ -333,7 +333,7 @@ function c(d) {
333
333
  createdAt: new Date(d.get("createdAt")),
334
334
  updatedAt: new Date(d.get("updatedAt")),
335
335
  comments: (d.get("comments") || []).map(
336
- (r) => u(r)
336
+ (r) => T(r)
337
337
  ),
338
338
  resolved: d.get("resolved"),
339
339
  resolvedUpdatedAt: new Date(d.get("resolvedUpdatedAt")),
@@ -355,7 +355,7 @@ class f extends w {
355
355
  getThreads() {
356
356
  const r = /* @__PURE__ */ new Map();
357
357
  return this.threadsYMap.forEach((e, t) => {
358
- r.set(t, c(e));
358
+ e instanceof u.Map && r.set(t, c(e));
359
359
  }), r;
360
360
  }
361
361
  subscribe(r) {
@@ -370,7 +370,7 @@ class f extends w {
370
370
  class Y extends f {
371
371
  constructor(e, t, a, s) {
372
372
  super(a, s);
373
- o(this, "doRequest", async (e, t, a) => {
373
+ n(this, "doRequest", async (e, t, a) => {
374
374
  const s = await fetch(`${this.BASE_URL}${e}`, {
375
375
  method: t,
376
376
  body: JSON.stringify(a),
@@ -383,30 +383,30 @@ class Y extends f {
383
383
  throw new Error(`Failed to ${t} ${e}: ${s.statusText}`);
384
384
  return s.json();
385
385
  });
386
- o(this, "addThreadToDocument", async (e) => {
386
+ n(this, "addThreadToDocument", async (e) => {
387
387
  const { threadId: t, ...a } = e;
388
388
  return this.doRequest(`/${t}/addToDocument`, "POST", a);
389
389
  });
390
- o(this, "createThread", async (e) => this.doRequest("", "POST", e));
391
- o(this, "addComment", (e) => {
390
+ n(this, "createThread", async (e) => this.doRequest("", "POST", e));
391
+ n(this, "addComment", (e) => {
392
392
  const { threadId: t, ...a } = e;
393
393
  return this.doRequest(`/${t}/comments`, "POST", a);
394
394
  });
395
- o(this, "updateComment", (e) => {
395
+ n(this, "updateComment", (e) => {
396
396
  const { threadId: t, commentId: a, ...s } = e;
397
397
  return this.doRequest(`/${t}/comments/${a}`, "PUT", s);
398
398
  });
399
- o(this, "deleteComment", (e) => {
399
+ n(this, "deleteComment", (e) => {
400
400
  const { threadId: t, commentId: a, ...s } = e;
401
401
  return this.doRequest(
402
402
  `/${t}/comments/${a}?soft=${!!s.softDelete}`,
403
403
  "DELETE"
404
404
  );
405
405
  });
406
- o(this, "deleteThread", (e) => this.doRequest(`/${e.threadId}`, "DELETE"));
407
- o(this, "resolveThread", (e) => this.doRequest(`/${e.threadId}/resolve`, "POST"));
408
- o(this, "unresolveThread", (e) => this.doRequest(`/${e.threadId}/unresolve`, "POST"));
409
- o(this, "addReaction", (e) => {
406
+ n(this, "deleteThread", (e) => this.doRequest(`/${e.threadId}`, "DELETE"));
407
+ n(this, "resolveThread", (e) => this.doRequest(`/${e.threadId}/resolve`, "POST"));
408
+ n(this, "unresolveThread", (e) => this.doRequest(`/${e.threadId}/unresolve`, "POST"));
409
+ n(this, "addReaction", (e) => {
410
410
  const { threadId: t, commentId: a, ...s } = e;
411
411
  return this.doRequest(
412
412
  `/${t}/comments/${a}/reactions`,
@@ -414,7 +414,7 @@ class Y extends f {
414
414
  s
415
415
  );
416
416
  });
417
- o(this, "deleteReaction", (e) => this.doRequest(
417
+ n(this, "deleteReaction", (e) => this.doRequest(
418
418
  `/${e.threadId}/comments/${e.commentId}/reactions/${e.emoji}`,
419
419
  "DELETE"
420
420
  ));
@@ -424,8 +424,8 @@ class Y extends f {
424
424
  class $ extends f {
425
425
  constructor(e, t, a) {
426
426
  super(t, a);
427
- o(this, "transact", (e) => async (t) => this.threadsYMap.doc.transact(() => e(t)));
428
- o(this, "createThread", this.transact(
427
+ n(this, "transact", (e) => async (t) => this.threadsYMap.doc.transact(() => e(t)));
428
+ n(this, "createThread", this.transact(
429
429
  (e) => {
430
430
  if (!this.auth.canCreateThread())
431
431
  throw new Error("Not authorized");
@@ -451,8 +451,8 @@ class $ extends f {
451
451
  }
452
452
  ));
453
453
  // YjsThreadStore does not support addThreadToDocument
454
- o(this, "addThreadToDocument");
455
- o(this, "addComment", this.transact(
454
+ n(this, "addThreadToDocument");
455
+ n(this, "addComment", this.transact(
456
456
  (e) => {
457
457
  const t = this.threadsYMap.get(e.threadId);
458
458
  if (!t)
@@ -475,50 +475,50 @@ class $ extends f {
475
475
  ]), t.set("updatedAt", (/* @__PURE__ */ new Date()).getTime()), s;
476
476
  }
477
477
  ));
478
- o(this, "updateComment", this.transact(
478
+ n(this, "updateComment", this.transact(
479
479
  (e) => {
480
480
  const t = this.threadsYMap.get(e.threadId);
481
481
  if (!t)
482
482
  throw new Error("Thread not found");
483
483
  const a = l(
484
484
  t.get("comments"),
485
- (n) => n.get("id") === e.commentId
485
+ (o) => o.get("id") === e.commentId
486
486
  );
487
487
  if (a === -1)
488
488
  throw new Error("Comment not found");
489
489
  const s = t.get("comments").get(a);
490
- if (!this.auth.canUpdateComment(u(s)))
490
+ if (!this.auth.canUpdateComment(T(s)))
491
491
  throw new Error("Not authorized");
492
492
  s.set("body", e.comment.body), s.set("updatedAt", (/* @__PURE__ */ new Date()).getTime()), s.set("metadata", e.comment.metadata);
493
493
  }
494
494
  ));
495
- o(this, "deleteComment", this.transact(
495
+ n(this, "deleteComment", this.transact(
496
496
  (e) => {
497
497
  const t = this.threadsYMap.get(e.threadId);
498
498
  if (!t)
499
499
  throw new Error("Thread not found");
500
500
  const a = l(
501
501
  t.get("comments"),
502
- (n) => n.get("id") === e.commentId
502
+ (o) => o.get("id") === e.commentId
503
503
  );
504
504
  if (a === -1)
505
505
  throw new Error("Comment not found");
506
506
  const s = t.get("comments").get(a);
507
- if (!this.auth.canDeleteComment(u(s)))
507
+ if (!this.auth.canDeleteComment(T(s)))
508
508
  throw new Error("Not authorized");
509
509
  if (s.get("deletedAt"))
510
510
  throw new Error("Comment already deleted");
511
- e.softDelete ? (s.set("deletedAt", (/* @__PURE__ */ new Date()).getTime()), s.set("body", void 0)) : t.get("comments").delete(a), t.get("comments").toArray().every((n) => n.get("deletedAt")) && (e.softDelete ? t.set("deletedAt", (/* @__PURE__ */ new Date()).getTime()) : this.threadsYMap.delete(e.threadId)), t.set("updatedAt", (/* @__PURE__ */ new Date()).getTime());
511
+ e.softDelete ? (s.set("deletedAt", (/* @__PURE__ */ new Date()).getTime()), s.set("body", void 0)) : t.get("comments").delete(a), t.get("comments").toArray().every((o) => o.get("deletedAt")) && (e.softDelete ? t.set("deletedAt", (/* @__PURE__ */ new Date()).getTime()) : this.threadsYMap.delete(e.threadId)), t.set("updatedAt", (/* @__PURE__ */ new Date()).getTime());
512
512
  }
513
513
  ));
514
- o(this, "deleteThread", this.transact((e) => {
514
+ n(this, "deleteThread", this.transact((e) => {
515
515
  if (!this.auth.canDeleteThread(
516
516
  c(this.threadsYMap.get(e.threadId))
517
517
  ))
518
518
  throw new Error("Not authorized");
519
519
  this.threadsYMap.delete(e.threadId);
520
520
  }));
521
- o(this, "resolveThread", this.transact((e) => {
521
+ n(this, "resolveThread", this.transact((e) => {
522
522
  const t = this.threadsYMap.get(e.threadId);
523
523
  if (!t)
524
524
  throw new Error("Thread not found");
@@ -526,7 +526,7 @@ class $ extends f {
526
526
  throw new Error("Not authorized");
527
527
  t.set("resolved", !0), t.set("resolvedUpdatedAt", (/* @__PURE__ */ new Date()).getTime()), t.set("resolvedBy", this.userId);
528
528
  }));
529
- o(this, "unresolveThread", this.transact((e) => {
529
+ n(this, "unresolveThread", this.transact((e) => {
530
530
  const t = this.threadsYMap.get(e.threadId);
531
531
  if (!t)
532
532
  throw new Error("Thread not found");
@@ -534,7 +534,7 @@ class $ extends f {
534
534
  throw new Error("Not authorized");
535
535
  t.set("resolved", !1), t.set("resolvedUpdatedAt", (/* @__PURE__ */ new Date()).getTime());
536
536
  }));
537
- o(this, "addReaction", this.transact(
537
+ n(this, "addReaction", this.transact(
538
538
  (e) => {
539
539
  const t = this.threadsYMap.get(e.threadId);
540
540
  if (!t)
@@ -546,16 +546,16 @@ class $ extends f {
546
546
  if (a === -1)
547
547
  throw new Error("Comment not found");
548
548
  const s = t.get("comments").get(a);
549
- if (!this.auth.canAddReaction(u(s), e.emoji))
549
+ if (!this.auth.canAddReaction(T(s), e.emoji))
550
550
  throw new Error("Not authorized");
551
- const n = /* @__PURE__ */ new Date(), h = `${this.userId}-${e.emoji}`, m = s.get("reactionsByUser");
551
+ const o = /* @__PURE__ */ new Date(), h = `${this.userId}-${e.emoji}`, m = s.get("reactionsByUser");
552
552
  if (!m.has(h)) {
553
- const i = new T.Map();
554
- i.set("emoji", e.emoji), i.set("createdAt", n.getTime()), i.set("userId", this.userId), m.set(h, i);
553
+ const i = new u.Map();
554
+ i.set("emoji", e.emoji), i.set("createdAt", o.getTime()), i.set("userId", this.userId), m.set(h, i);
555
555
  }
556
556
  }
557
557
  ));
558
- o(this, "deleteReaction", this.transact(
558
+ n(this, "deleteReaction", this.transact(
559
559
  (e) => {
560
560
  const t = this.threadsYMap.get(e.threadId);
561
561
  if (!t)
@@ -567,10 +567,10 @@ class $ extends f {
567
567
  if (a === -1)
568
568
  throw new Error("Comment not found");
569
569
  const s = t.get("comments").get(a);
570
- if (!this.auth.canDeleteReaction(u(s), e.emoji))
570
+ if (!this.auth.canDeleteReaction(T(s), e.emoji))
571
571
  throw new Error("Not authorized");
572
- const n = `${this.userId}-${e.emoji}`;
573
- s.get("reactionsByUser").delete(n);
572
+ const o = `${this.userId}-${e.emoji}`;
573
+ s.get("reactionsByUser").delete(o);
574
574
  }
575
575
  ));
576
576
  this.userId = e;