@blocknote/core 0.47.3 → 0.48.1

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 (121) hide show
  1. package/dist/BlockNoteExtension-B6RhuQ3V.js +27 -0
  2. package/dist/BlockNoteExtension-B6RhuQ3V.js.map +1 -0
  3. package/dist/BlockNoteExtension-ab0REuob.cjs +2 -0
  4. package/dist/BlockNoteExtension-ab0REuob.cjs.map +1 -0
  5. package/dist/EventEmitter-CuPnGDYt.js +22 -0
  6. package/dist/EventEmitter-CuPnGDYt.js.map +1 -0
  7. package/dist/EventEmitter-DHdSnYCD.cjs +2 -0
  8. package/dist/EventEmitter-DHdSnYCD.cjs.map +1 -0
  9. package/dist/ShowSelection-BsHFfdMi.js +29 -0
  10. package/dist/ShowSelection-BsHFfdMi.js.map +1 -0
  11. package/dist/ShowSelection-DhXglbt8.cjs +2 -0
  12. package/dist/ShowSelection-DhXglbt8.cjs.map +1 -0
  13. package/dist/blocknote.cjs +1 -5
  14. package/dist/blocknote.js +7 -4256
  15. package/dist/blocks-BEvIOzln.cjs +11 -0
  16. package/dist/blocks-BEvIOzln.cjs.map +1 -0
  17. package/dist/blocks-DYl7UTS4.js +3313 -0
  18. package/dist/blocks-DYl7UTS4.js.map +1 -0
  19. package/dist/blocks.cjs +1 -2
  20. package/dist/blocks.js +2 -74
  21. package/dist/comments.cjs +2 -2
  22. package/dist/comments.cjs.map +1 -1
  23. package/dist/comments.js +604 -900
  24. package/dist/comments.js.map +1 -1
  25. package/dist/en-C4ykW581.cjs +2 -0
  26. package/dist/en-C4ykW581.cjs.map +1 -0
  27. package/dist/en-QsSI16s5.js +453 -0
  28. package/dist/en-QsSI16s5.js.map +1 -0
  29. package/dist/extensions-3nf9PPhA.js +1495 -0
  30. package/dist/extensions-3nf9PPhA.js.map +1 -0
  31. package/dist/extensions-D9y6evn6.cjs +2 -0
  32. package/dist/extensions-D9y6evn6.cjs.map +1 -0
  33. package/dist/extensions.cjs +1 -2
  34. package/dist/extensions.js +4 -36
  35. package/dist/locales.cjs +2 -2
  36. package/dist/locales.cjs.map +1 -1
  37. package/dist/locales.js +10049 -9111
  38. package/dist/locales.js.map +1 -1
  39. package/dist/src-BKJH7Jla.cjs +5 -0
  40. package/dist/src-BKJH7Jla.cjs.map +1 -0
  41. package/dist/src-Hjv1f5lc.js +2386 -0
  42. package/dist/src-Hjv1f5lc.js.map +1 -0
  43. package/dist/style.css +2 -1
  44. package/dist/webpack-stats.json +1 -1
  45. package/dist/yjs.cjs +2 -2
  46. package/dist/yjs.cjs.map +1 -1
  47. package/dist/yjs.js +20 -38
  48. package/dist/yjs.js.map +1 -1
  49. package/package.json +8 -8
  50. package/src/api/blockManipulation/commands/mergeBlocks/mergeBlocks.test.ts +15 -1
  51. package/src/api/blockManipulation/commands/mergeBlocks/mergeBlocks.ts +5 -0
  52. package/src/api/blockManipulation/commands/nestBlock/__snapshots__/nestBlock.test.ts.snap +1095 -0
  53. package/src/api/blockManipulation/commands/nestBlock/nestBlock.test.ts +661 -0
  54. package/src/api/blockManipulation/commands/nestBlock/nestBlock.ts +128 -11
  55. package/src/blocks/File/helpers/render/createResizableFileBlockWrapper.ts +17 -23
  56. package/src/blocks/ListItem/NumberedListItem/IndexingPlugin.test.ts +346 -0
  57. package/src/blocks/ListItem/NumberedListItem/IndexingPlugin.ts +143 -71
  58. package/src/editor/Block.css +5 -8
  59. package/src/editor/BlockNoteEditor.ts +38 -1
  60. package/src/editor/editor.css +0 -20
  61. package/src/editor/managers/ExtensionManager/extensions.ts +12 -1
  62. package/src/editor/performance.test.ts +134 -0
  63. package/src/extensions/Collaboration/ForkYDoc.test.ts +115 -103
  64. package/src/extensions/Placeholder/Placeholder.ts +1 -1
  65. package/src/extensions/PreviousBlockType/PreviousBlockType.test.ts +86 -0
  66. package/src/extensions/PreviousBlockType/PreviousBlockType.ts +33 -49
  67. package/src/extensions/SideMenu/SideMenu.ts +14 -8
  68. package/src/extensions/TableHandles/TableHandles.ts +14 -0
  69. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts +57 -17
  70. package/src/extensions/tiptap-extensions/UniqueID/UniqueID.ts +9 -8
  71. package/src/index.ts +1 -1
  72. package/src/schema/blocks/createSpec.ts +6 -2
  73. package/src/schema/inlineContent/types.ts +0 -1
  74. package/src/vite-env.d.ts +6 -0
  75. package/types/src/api/blockManipulation/commands/nestBlock/nestBlock.d.ts +14 -1
  76. package/types/src/api/blockManipulation/commands/nestBlock/nestBlock.test.d.ts +1 -0
  77. package/types/src/blocks/ListItem/NumberedListItem/IndexingPlugin.test.d.ts +1 -0
  78. package/types/src/editor/BlockNoteEditor.d.ts +13 -0
  79. package/types/src/editor/performance.test.d.ts +1 -0
  80. package/types/src/extensions/PreviousBlockType/PreviousBlockType.test.d.ts +1 -0
  81. package/types/src/extensions/SideMenu/SideMenu.d.ts +6 -0
  82. package/types/src/extensions/TableHandles/TableHandles.d.ts +6 -0
  83. package/dist/BlockNoteExtension-BWw0r8Gy.cjs +0 -2
  84. package/dist/BlockNoteExtension-BWw0r8Gy.cjs.map +0 -1
  85. package/dist/BlockNoteExtension-C2X7LW-V.js +0 -25
  86. package/dist/BlockNoteExtension-C2X7LW-V.js.map +0 -1
  87. package/dist/BlockNoteSchema-CCs_V3lo.cjs +0 -2
  88. package/dist/BlockNoteSchema-CCs_V3lo.cjs.map +0 -1
  89. package/dist/BlockNoteSchema-ooiKsd5B.js +0 -270
  90. package/dist/BlockNoteSchema-ooiKsd5B.js.map +0 -1
  91. package/dist/EventEmitter-CLwfmbqG.cjs +0 -2
  92. package/dist/EventEmitter-CLwfmbqG.cjs.map +0 -1
  93. package/dist/EventEmitter-CjSwpTbz.js +0 -27
  94. package/dist/EventEmitter-CjSwpTbz.js.map +0 -1
  95. package/dist/ShowSelection-B0ch3unP.js +0 -51
  96. package/dist/ShowSelection-B0ch3unP.js.map +0 -1
  97. package/dist/ShowSelection-BxnbRvy4.cjs +0 -2
  98. package/dist/ShowSelection-BxnbRvy4.cjs.map +0 -1
  99. package/dist/TrailingNode-GzE59m_7.js +0 -2288
  100. package/dist/TrailingNode-GzE59m_7.js.map +0 -1
  101. package/dist/TrailingNode-n0WdMPUl.cjs +0 -2
  102. package/dist/TrailingNode-n0WdMPUl.cjs.map +0 -1
  103. package/dist/blockToNode-BNoNIXU7.js +0 -1140
  104. package/dist/blockToNode-BNoNIXU7.js.map +0 -1
  105. package/dist/blockToNode-CumVjgem.cjs +0 -7
  106. package/dist/blockToNode-CumVjgem.cjs.map +0 -1
  107. package/dist/blocknote.cjs.map +0 -1
  108. package/dist/blocknote.js.map +0 -1
  109. package/dist/blocks.cjs.map +0 -1
  110. package/dist/blocks.js.map +0 -1
  111. package/dist/defaultBlocks-Dg9kQWXm.cjs +0 -6
  112. package/dist/defaultBlocks-Dg9kQWXm.cjs.map +0 -1
  113. package/dist/defaultBlocks-ZzGbYgQn.js +0 -3284
  114. package/dist/defaultBlocks-ZzGbYgQn.js.map +0 -1
  115. package/dist/en-Cl87Uuyf.cjs +0 -2
  116. package/dist/en-Cl87Uuyf.cjs.map +0 -1
  117. package/dist/en-njEqD7AG.js +0 -408
  118. package/dist/en-njEqD7AG.js.map +0 -1
  119. package/dist/extensions.cjs.map +0 -1
  120. package/dist/extensions.js.map +0 -1
  121. package/dist/tsconfig.tsbuildinfo +0 -1
package/dist/comments.js CHANGED
@@ -1,908 +1,612 @@
1
- var x = Object.defineProperty;
2
- var Y = (r, d, e) => d in r ? x(r, d, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[d] = e;
3
- var c = (r, d, e) => Y(r, typeof d != "symbol" ? d + "" : d, e);
4
- import { PluginKey as $, Plugin as k } from "prosemirror-state";
5
- import { DecorationSet as C, Decoration as z } from "prosemirror-view";
6
- import { ySyncPluginKey as B, getRelativeSelection as q } from "y-prosemirror";
7
- import { c as L, a as N } from "./BlockNoteExtension-C2X7LW-V.js";
8
- import { S } from "./ShowSelection-B0ch3unP.js";
9
- import { Mark as O, mergeAttributes as _ } from "@tiptap/core";
10
- import { E as H } from "./EventEmitter-CjSwpTbz.js";
11
- import * as g from "yjs";
12
- import { v4 as D } from "uuid";
13
- const M = O.create({
14
- name: "comment",
15
- excludes: "",
16
- inclusive: !1,
17
- keepOnSplit: !0,
18
- addAttributes() {
19
- return {
20
- // orphans are marks that currently don't have an active thread. It could be
21
- // that users have resolved the thread. Resolved threads by default are not shown in the document,
22
- // but we need to keep the mark (positioning) data so we can still "revive" it when the thread is unresolved
23
- // or we enter a "comments" view that includes resolved threads.
24
- orphan: {
25
- parseHTML: (r) => !!r.getAttribute("data-orphan"),
26
- renderHTML: (r) => r.orphan ? {
27
- "data-orphan": "true"
28
- } : {},
29
- default: !1
30
- },
31
- threadId: {
32
- parseHTML: (r) => r.getAttribute("data-bn-thread-id"),
33
- renderHTML: (r) => ({
34
- "data-bn-thread-id": r.threadId
35
- }),
36
- default: ""
37
- }
38
- };
39
- },
40
- renderHTML({ HTMLAttributes: r }) {
41
- return [
42
- "span",
43
- _(r, {
44
- class: "bn-thread-mark"
45
- })
46
- ];
47
- },
48
- parseHTML() {
49
- return [{ tag: "span.bn-thread-mark" }];
50
- },
51
- extendMarkSchema(r) {
52
- return r.name === "comment" ? {
53
- blocknoteIgnore: !0
54
- } : {};
55
- }
56
- });
57
- class F extends H {
58
- constructor(e) {
59
- super();
60
- c(this, "userCache", /* @__PURE__ */ new Map());
61
- // avoid duplicate loads
62
- c(this, "loadingUsers", /* @__PURE__ */ new Set());
63
- this.resolveUsers = e;
64
- }
65
- /**
66
- * Load information about users based on an array of user ids.
67
- */
68
- async loadUsers(e) {
69
- const t = e.filter(
70
- (a) => !this.userCache.has(a) && !this.loadingUsers.has(a)
71
- );
72
- if (t.length !== 0) {
73
- for (const a of t)
74
- this.loadingUsers.add(a);
75
- try {
76
- const a = await this.resolveUsers(t);
77
- for (const s of a)
78
- this.userCache.set(s.id, s);
79
- this.emit("update", this.userCache);
80
- } finally {
81
- for (const a of t)
82
- this.loadingUsers.delete(a);
83
- }
84
- }
85
- }
86
- /**
87
- * Retrieve information about a user based on their id, if cached.
88
- *
89
- * The user will have to be loaded via `loadUsers` first
90
- */
91
- getUser(e) {
92
- return this.userCache.get(e);
93
- }
94
- /**
95
- * Subscribe to changes in the user store.
96
- *
97
- * @param cb - The callback to call when the user store changes.
98
- * @returns A function to unsubscribe from the user store.
99
- */
100
- subscribe(e) {
101
- return this.on("update", e);
102
- }
1
+ import { n as e, t } from "./BlockNoteExtension-B6RhuQ3V.js";
2
+ import { t as n } from "./ShowSelection-BsHFfdMi.js";
3
+ import { t as r } from "./EventEmitter-CuPnGDYt.js";
4
+ import { Mark as i, mergeAttributes as a } from "@tiptap/core";
5
+ import { Plugin as o, PluginKey as s } from "prosemirror-state";
6
+ import { v4 as c } from "uuid";
7
+ import { Decoration as l, DecorationSet as u } from "prosemirror-view";
8
+ import { getRelativeSelection as d, ySyncPluginKey as f } from "y-prosemirror";
9
+ import * as p from "yjs";
10
+ //#region src/comments/mark.ts
11
+ var m = i.create({
12
+ name: "comment",
13
+ excludes: "",
14
+ inclusive: !1,
15
+ keepOnSplit: !0,
16
+ addAttributes() {
17
+ return {
18
+ orphan: {
19
+ parseHTML: (e) => !!e.getAttribute("data-orphan"),
20
+ renderHTML: (e) => e.orphan ? { "data-orphan": "true" } : {},
21
+ default: !1
22
+ },
23
+ threadId: {
24
+ parseHTML: (e) => e.getAttribute("data-bn-thread-id"),
25
+ renderHTML: (e) => ({ "data-bn-thread-id": e.threadId }),
26
+ default: ""
27
+ }
28
+ };
29
+ },
30
+ renderHTML({ HTMLAttributes: e }) {
31
+ return ["span", a(e, { class: "bn-thread-mark" })];
32
+ },
33
+ parseHTML() {
34
+ return [{ tag: "span.bn-thread-mark" }];
35
+ },
36
+ extendMarkSchema(e) {
37
+ return e.name === "comment" ? { blocknoteIgnore: !0 } : {};
38
+ }
39
+ }), h = class extends r {
40
+ userCache = /* @__PURE__ */ new Map();
41
+ loadingUsers = /* @__PURE__ */ new Set();
42
+ constructor(e) {
43
+ super(), this.resolveUsers = e;
44
+ }
45
+ async loadUsers(e) {
46
+ let t = e.filter((e) => !this.userCache.has(e) && !this.loadingUsers.has(e));
47
+ if (t.length !== 0) {
48
+ for (let e of t) this.loadingUsers.add(e);
49
+ try {
50
+ let e = await this.resolveUsers(t);
51
+ for (let t of e) this.userCache.set(t.id, t);
52
+ this.emit("update", this.userCache);
53
+ } finally {
54
+ for (let e of t) this.loadingUsers.delete(e);
55
+ }
56
+ }
57
+ }
58
+ getUser(e) {
59
+ return this.userCache.get(e);
60
+ }
61
+ subscribe(e) {
62
+ return this.on("update", e);
63
+ }
64
+ }, g = new s("blocknote-comments");
65
+ function _(e, t) {
66
+ let n = /* @__PURE__ */ new Map();
67
+ return e.descendants((e, r) => {
68
+ e.marks.forEach((i) => {
69
+ if (i.type.name === t) {
70
+ let t = i.attrs.threadId;
71
+ if (!t) return;
72
+ let a = r, o = a + e.nodeSize, s = n.get(t) ?? {
73
+ from: Infinity,
74
+ to: 0
75
+ };
76
+ n.set(t, {
77
+ from: Math.min(a, s.from),
78
+ to: Math.max(o, s.to)
79
+ });
80
+ }
81
+ });
82
+ }), n;
103
83
  }
104
- const w = new $("blocknote-comments");
105
- function K(r, d) {
106
- const e = /* @__PURE__ */ new Map();
107
- return r.descendants((t, a) => {
108
- t.marks.forEach((s) => {
109
- if (s.type.name === d) {
110
- const o = s.attrs.threadId;
111
- if (!o)
112
- return;
113
- const m = a, n = m + t.nodeSize, i = e.get(o) ?? {
114
- from: 1 / 0,
115
- to: 0
116
- };
117
- e.set(o, {
118
- from: Math.min(m, i.from),
119
- to: Math.max(n, i.to)
120
- });
121
- }
122
- });
123
- }), e;
124
- }
125
- const ne = L(
126
- ({
127
- editor: r,
128
- options: { schema: d, threadStore: e, resolveUsers: t }
129
- }) => {
130
- if (!t)
131
- throw new Error(
132
- "resolveUsers is required to be defined when using comments"
133
- );
134
- if (!e)
135
- throw new Error(
136
- "threadStore is required to be defined when using comments"
137
- );
138
- const a = M.name, s = new F(t), o = N(
139
- {
140
- pendingComment: !1,
141
- selectedThreadId: void 0,
142
- threadPositions: /* @__PURE__ */ new Map()
143
- },
144
- {
145
- onUpdate() {
146
- o.state.selectedThreadId !== o.prevState.selectedThreadId && r.transact((n) => n.setMeta(w, !0));
147
- }
148
- }
149
- ), m = (n) => {
150
- r.transact((i) => {
151
- i.doc.descendants((u, l) => {
152
- u.marks.forEach((h) => {
153
- if (h.type.name === a) {
154
- const T = h.type, f = h.attrs.threadId, A = n.get(f), v = !!(!A || A.resolved || A.deletedAt);
155
- if (v !== h.attrs.orphan) {
156
- const E = Math.max(l, 0), b = Math.min(
157
- l + u.nodeSize,
158
- i.doc.content.size - 1,
159
- i.doc.content.size - 1
160
- );
161
- i.removeMark(E, b, h), i.addMark(
162
- E,
163
- b,
164
- T.create({
165
- ...h.attrs,
166
- orphan: v
167
- })
168
- ), v && o.state.selectedThreadId === f && o.setState((P) => ({
169
- ...P,
170
- selectedThreadId: void 0
171
- }));
172
- }
173
- }
174
- });
175
- });
176
- });
177
- };
178
- return {
179
- key: "comments",
180
- store: o,
181
- prosemirrorPlugins: [
182
- new k({
183
- key: w,
184
- state: {
185
- init() {
186
- return {
187
- decorations: C.empty
188
- };
189
- },
190
- apply(n, i) {
191
- const u = n.getMeta(w);
192
- if (!n.docChanged && !u)
193
- return i;
194
- const l = n.docChanged ? K(n.doc, a) : o.state.threadPositions;
195
- (l.size > 0 || o.state.threadPositions.size > 0) && o.setState((T) => ({
196
- ...T,
197
- threadPositions: l
198
- }));
199
- const h = [];
200
- if (o.state.selectedThreadId) {
201
- const T = l.get(
202
- o.state.selectedThreadId
203
- );
204
- T && h.push(
205
- z.inline(
206
- T.from,
207
- T.to,
208
- {
209
- class: "bn-thread-mark-selected"
210
- }
211
- )
212
- );
213
- }
214
- return {
215
- decorations: C.create(n.doc, h)
216
- };
217
- }
218
- },
219
- props: {
220
- decorations(n) {
221
- var i;
222
- return ((i = w.getState(n)) == null ? void 0 : i.decorations) ?? C.empty;
223
- },
224
- handleClick: (n, i, u) => {
225
- if (u.button !== 0)
226
- return;
227
- const l = n.state.doc.nodeAt(i);
228
- if (!l) {
229
- o.setState((f) => ({
230
- ...f,
231
- selectedThreadId: void 0
232
- }));
233
- return;
234
- }
235
- const h = l.marks.find(
236
- (f) => f.type.name === a && f.attrs.orphan !== !0
237
- ), T = h == null ? void 0 : h.attrs.threadId;
238
- T !== o.state.selectedThreadId && o.setState((f) => ({
239
- ...f,
240
- selectedThreadId: T
241
- }));
242
- }
243
- }
244
- })
245
- ],
246
- threadStore: e,
247
- mount() {
248
- const n = e.subscribe(m);
249
- m(e.getThreads());
250
- const i = r.onSelectionChange(() => {
251
- o.state.pendingComment && o.setState((u) => ({
252
- ...u,
253
- pendingComment: !1
254
- }));
255
- });
256
- return () => {
257
- n(), i();
258
- };
259
- },
260
- selectThread(n, i = !0) {
261
- var u, l;
262
- if (o.state.selectedThreadId !== n && (o.setState((h) => ({
263
- ...h,
264
- pendingComment: !1,
265
- selectedThreadId: n
266
- })), n && i)) {
267
- const h = o.state.threadPositions.get(n);
268
- if (!h)
269
- return;
270
- (l = (u = r.prosemirrorView) == null ? void 0 : u.domAtPos(h.from).node) == null || l.scrollIntoView({
271
- behavior: "smooth",
272
- block: "center"
273
- });
274
- }
275
- },
276
- startPendingComment() {
277
- var n;
278
- o.setState((i) => ({
279
- ...i,
280
- selectedThreadId: void 0,
281
- pendingComment: !0
282
- })), (n = r.getExtension(S)) == null || n.showSelection(!0, "comments");
283
- },
284
- stopPendingComment() {
285
- var n;
286
- o.setState((i) => ({
287
- ...i,
288
- selectedThreadId: void 0,
289
- pendingComment: !1
290
- })), (n = r.getExtension(S)) == null || n.showSelection(!1, "comments");
291
- },
292
- async createThread(n) {
293
- const i = await e.createThread(n);
294
- if (e.addThreadToDocument) {
295
- const u = r.prosemirrorView, l = u.state.selection, h = B.getState(u.state), T = {
296
- prosemirror: {
297
- head: l.head,
298
- anchor: l.anchor
299
- },
300
- yjs: h ? q(h.binding, u.state) : void 0
301
- };
302
- await e.addThreadToDocument({
303
- threadId: i.id,
304
- selection: T
305
- });
306
- } else
307
- r._tiptapEditor.commands.setMark(a, {
308
- orphan: !1,
309
- threadId: i.id
310
- });
311
- },
312
- userStore: s,
313
- commentEditorSchema: d,
314
- tiptapExtensions: [M]
315
- };
316
- }
317
- );
318
- class V {
319
- }
320
- class oe extends V {
321
- constructor(d, e) {
322
- super(), this.userId = d, this.role = e;
323
- }
324
- /**
325
- * Auth: should be possible by anyone with comment access
326
- */
327
- canCreateThread() {
328
- return !0;
329
- }
330
- /**
331
- * Auth: should be possible by anyone with comment access
332
- */
333
- canAddComment(d) {
334
- return !0;
335
- }
336
- /**
337
- * Auth: should only be possible by the comment author
338
- */
339
- canUpdateComment(d) {
340
- return d.userId === this.userId;
341
- }
342
- /**
343
- * Auth: should be possible by the comment author OR an editor of the document
344
- */
345
- canDeleteComment(d) {
346
- return d.userId === this.userId || this.role === "editor";
347
- }
348
- /**
349
- * Auth: should only be possible by an editor of the document
350
- */
351
- canDeleteThread(d) {
352
- return this.role === "editor";
353
- }
354
- /**
355
- * Auth: should be possible by anyone with comment access
356
- */
357
- canResolveThread(d) {
358
- return !0;
359
- }
360
- /**
361
- * Auth: should be possible by anyone with comment access
362
- */
363
- canUnresolveThread(d) {
364
- return !0;
365
- }
366
- /**
367
- * Auth: should be possible by anyone with comment access
368
- *
369
- * 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?
370
- */
371
- canAddReaction(d, e) {
372
- return e ? !d.reactions.some(
373
- (t) => t.emoji === e && t.userIds.includes(this.userId)
374
- ) : !0;
375
- }
376
- /**
377
- * Auth: should be possible by anyone with comment access
378
- *
379
- * 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?
380
- */
381
- canDeleteReaction(d, e) {
382
- return e ? d.reactions.some(
383
- (t) => t.emoji === e && t.userIds.includes(this.userId)
384
- ) : !0;
385
- }
386
- }
387
- class R {
388
- constructor(d) {
389
- c(this, "auth");
390
- this.auth = d;
391
- }
392
- }
393
- class ie extends R {
394
- constructor(e, t, a) {
395
- super(a);
396
- // TipTapThreadStore does not support addThreadToDocument
397
- c(this, "addThreadToDocument");
398
- this.userId = e, this.provider = t;
399
- }
400
- /**
401
- * Creates a new thread with an initial comment.
402
- */
403
- async createThread(e) {
404
- let t = this.provider.createThread({
405
- data: e.metadata
406
- });
407
- return t = this.provider.addComment(t.id, {
408
- content: e.initialComment.body,
409
- data: {
410
- metadata: e.initialComment.metadata,
411
- userId: this.userId
412
- }
413
- }), this.tiptapThreadToThreadData(t);
414
- }
415
- /**
416
- * Adds a comment to a thread.
417
- */
418
- async addComment(e) {
419
- const t = this.provider.addComment(e.threadId, {
420
- content: e.comment.body,
421
- data: {
422
- metadata: e.comment.metadata,
423
- userId: this.userId
424
- }
425
- });
426
- return this.tiptapCommentToCommentData(
427
- t.comments[t.comments.length - 1]
428
- );
429
- }
430
- /**
431
- * Updates a comment in a thread.
432
- */
433
- async updateComment(e) {
434
- const t = this.provider.getThreadComment(
435
- e.threadId,
436
- e.commentId,
437
- !0
438
- );
439
- if (!t)
440
- throw new Error("Comment not found");
441
- this.provider.updateComment(e.threadId, e.commentId, {
442
- content: e.comment.body,
443
- data: {
444
- ...t.data,
445
- metadata: e.comment.metadata
446
- }
447
- });
448
- }
449
- tiptapCommentToCommentData(e) {
450
- var a, s, o;
451
- const t = [];
452
- for (const m of ((a = e.data) == null ? void 0 : a.reactions) || []) {
453
- const n = t.find(
454
- (i) => i.emoji === m.emoji
455
- );
456
- n ? (n.userIds.push(m.userId), n.createdAt = new Date(
457
- Math.min(n.createdAt.getTime(), m.createdAt)
458
- )) : t.push({
459
- emoji: m.emoji,
460
- createdAt: new Date(m.createdAt),
461
- userIds: [m.userId]
462
- });
463
- }
464
- return {
465
- type: "comment",
466
- id: e.id,
467
- body: e.content,
468
- metadata: (s = e.data) == null ? void 0 : s.metadata,
469
- userId: (o = e.data) == null ? void 0 : o.userId,
470
- createdAt: new Date(e.createdAt),
471
- updatedAt: new Date(e.updatedAt),
472
- reactions: t
473
- };
474
- }
475
- tiptapThreadToThreadData(e) {
476
- var t;
477
- return {
478
- type: "thread",
479
- id: e.id,
480
- comments: e.comments.map(
481
- (a) => this.tiptapCommentToCommentData(a)
482
- ),
483
- resolved: !!e.resolvedAt,
484
- metadata: (t = e.data) == null ? void 0 : t.metadata,
485
- createdAt: new Date(e.createdAt),
486
- updatedAt: new Date(e.updatedAt)
487
- };
488
- }
489
- /**
490
- * Deletes a comment from a thread.
491
- */
492
- async deleteComment(e) {
493
- this.provider.deleteComment(e.threadId, e.commentId);
494
- }
495
- /**
496
- * Deletes a thread.
497
- */
498
- async deleteThread(e) {
499
- this.provider.deleteThread(e.threadId);
500
- }
501
- /**
502
- * Marks a thread as resolved.
503
- */
504
- async resolveThread(e) {
505
- this.provider.updateThread(e.threadId, {
506
- resolvedAt: (/* @__PURE__ */ new Date()).toISOString()
507
- });
508
- }
509
- /**
510
- * Marks a thread as unresolved.
511
- */
512
- async unresolveThread(e) {
513
- this.provider.updateThread(e.threadId, {
514
- resolvedAt: null
515
- });
516
- }
517
- /**
518
- * Adds a reaction to a comment.
519
- *
520
- * Auth: should be possible by anyone with comment access
521
- */
522
- async addReaction(e) {
523
- var a;
524
- const t = this.provider.getThreadComment(
525
- e.threadId,
526
- e.commentId,
527
- !0
528
- );
529
- if (!t)
530
- throw new Error("Comment not found");
531
- this.provider.updateComment(e.threadId, e.commentId, {
532
- data: {
533
- ...t.data,
534
- reactions: [
535
- ...((a = t.data) == null ? void 0 : a.reactions) || [],
536
- {
537
- emoji: e.emoji,
538
- createdAt: Date.now(),
539
- userId: this.userId
540
- }
541
- ]
542
- }
543
- });
544
- }
545
- /**
546
- * Deletes a reaction from a comment.
547
- *
548
- * Auth: should be possible by the reaction author
549
- */
550
- async deleteReaction(e) {
551
- var a;
552
- const t = this.provider.getThreadComment(
553
- e.threadId,
554
- e.commentId,
555
- !0
556
- );
557
- if (!t)
558
- throw new Error("Comment not found");
559
- this.provider.updateComment(e.threadId, e.commentId, {
560
- data: {
561
- ...t.data,
562
- reactions: (((a = t.data) == null ? void 0 : a.reactions) || []).filter(
563
- (s) => s.emoji !== e.emoji && s.userId !== this.userId
564
- )
565
- }
566
- });
567
- }
568
- getThread(e) {
569
- const t = this.provider.getThread(e);
570
- if (!t)
571
- throw new Error("Thread not found");
572
- return this.tiptapThreadToThreadData(t);
573
- }
574
- getThreads() {
575
- return new Map(
576
- this.provider.getThreads().map((e) => [e.id, this.tiptapThreadToThreadData(e)])
577
- );
578
- }
579
- subscribe(e) {
580
- const t = () => {
581
- e(this.getThreads());
582
- };
583
- return this.provider.watchThreads(t), () => {
584
- this.provider.unwatchThreads(t);
585
- };
586
- }
587
- }
588
- function U(r) {
589
- const d = new g.Map();
590
- 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)
591
- throw new Error("Reactions should be empty in commentToYMap");
592
- return d.set("reactionsByUser", new g.Map()), d.set("metadata", r.metadata), d;
593
- }
594
- function G(r) {
595
- var t;
596
- const d = new g.Map();
597
- d.set("id", r.id), d.set("createdAt", r.createdAt.getTime()), d.set("updatedAt", r.updatedAt.getTime());
598
- const e = new g.Array();
599
- return e.push(r.comments.map((a) => U(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;
600
- }
601
- function J(r) {
602
- return {
603
- emoji: r.get("emoji"),
604
- createdAt: new Date(r.get("createdAt")),
605
- userId: r.get("userId")
606
- };
607
- }
608
- function Q(r) {
609
- return [...r.values()].map(
610
- (e) => J(e)
611
- ).reduce(
612
- (e, t) => {
613
- const a = e.find((s) => s.emoji === t.emoji);
614
- return a ? (a.userIds.push(t.userId), a.createdAt = new Date(
615
- Math.min(
616
- a.createdAt.getTime(),
617
- t.createdAt.getTime()
618
- )
619
- )) : e.push({
620
- emoji: t.emoji,
621
- createdAt: t.createdAt,
622
- userIds: [t.userId]
623
- }), e;
624
- },
625
- []
626
- );
627
- }
628
- function I(r) {
629
- return {
630
- type: "comment",
631
- id: r.get("id"),
632
- userId: r.get("userId"),
633
- createdAt: new Date(r.get("createdAt")),
634
- updatedAt: new Date(r.get("updatedAt")),
635
- deletedAt: r.get("deletedAt") ? new Date(r.get("deletedAt")) : void 0,
636
- reactions: Q(r.get("reactionsByUser")),
637
- metadata: r.get("metadata"),
638
- body: r.get("body")
639
- };
84
+ var v = t(({ editor: t, options: { schema: r, threadStore: i, resolveUsers: a } }) => {
85
+ if (!a) throw Error("resolveUsers is required to be defined when using comments");
86
+ if (!i) throw Error("threadStore is required to be defined when using comments");
87
+ let s = m.name, c = new h(a), p = e({
88
+ pendingComment: !1,
89
+ selectedThreadId: void 0,
90
+ threadPositions: /* @__PURE__ */ new Map()
91
+ }, { onUpdate() {
92
+ p.state.selectedThreadId !== p.prevState.selectedThreadId && t.transact((e) => e.setMeta(g, !0));
93
+ } }), v = (e) => {
94
+ t.transact((t) => {
95
+ t.doc.descendants((n, r) => {
96
+ n.marks.forEach((i) => {
97
+ if (i.type.name === s) {
98
+ let a = i.type, o = i.attrs.threadId, s = e.get(o), c = !!(!s || s.resolved || s.deletedAt);
99
+ if (c !== i.attrs.orphan) {
100
+ let e = Math.max(r, 0), s = Math.min(r + n.nodeSize, t.doc.content.size - 1, t.doc.content.size - 1);
101
+ t.removeMark(e, s, i), t.addMark(e, s, a.create({
102
+ ...i.attrs,
103
+ orphan: c
104
+ })), c && p.state.selectedThreadId === o && p.setState((e) => ({
105
+ ...e,
106
+ selectedThreadId: void 0
107
+ }));
108
+ }
109
+ }
110
+ });
111
+ });
112
+ });
113
+ };
114
+ return {
115
+ key: "comments",
116
+ store: p,
117
+ prosemirrorPlugins: [new o({
118
+ key: g,
119
+ state: {
120
+ init() {
121
+ return { decorations: u.empty };
122
+ },
123
+ apply(e, t) {
124
+ let n = e.getMeta(g);
125
+ if (!e.docChanged && !n) return t;
126
+ let r = e.docChanged ? _(e.doc, s) : p.state.threadPositions;
127
+ (r.size > 0 || p.state.threadPositions.size > 0) && p.setState((e) => ({
128
+ ...e,
129
+ threadPositions: r
130
+ }));
131
+ let i = [];
132
+ if (p.state.selectedThreadId) {
133
+ let e = r.get(p.state.selectedThreadId);
134
+ e && i.push(l.inline(e.from, e.to, { class: "bn-thread-mark-selected" }));
135
+ }
136
+ return { decorations: u.create(e.doc, i) };
137
+ }
138
+ },
139
+ props: {
140
+ decorations(e) {
141
+ return g.getState(e)?.decorations ?? u.empty;
142
+ },
143
+ handleClick: (e, t, n) => {
144
+ if (n.button !== 0) return;
145
+ let r = e.state.doc.nodeAt(t);
146
+ if (!r) {
147
+ p.setState((e) => ({
148
+ ...e,
149
+ selectedThreadId: void 0
150
+ }));
151
+ return;
152
+ }
153
+ let i = r.marks.find((e) => e.type.name === s && e.attrs.orphan !== !0)?.attrs.threadId;
154
+ i !== p.state.selectedThreadId && p.setState((e) => ({
155
+ ...e,
156
+ selectedThreadId: i
157
+ }));
158
+ }
159
+ }
160
+ })],
161
+ threadStore: i,
162
+ mount() {
163
+ let e = i.subscribe(v);
164
+ v(i.getThreads());
165
+ let n = t.onSelectionChange(() => {
166
+ p.state.pendingComment && p.setState((e) => ({
167
+ ...e,
168
+ pendingComment: !1
169
+ }));
170
+ });
171
+ return () => {
172
+ e(), n();
173
+ };
174
+ },
175
+ selectThread(e, n = !0) {
176
+ if (p.state.selectedThreadId !== e && (p.setState((t) => ({
177
+ ...t,
178
+ pendingComment: !1,
179
+ selectedThreadId: e
180
+ })), e && n)) {
181
+ let n = p.state.threadPositions.get(e);
182
+ if (!n) return;
183
+ (t.prosemirrorView?.domAtPos(n.from).node)?.scrollIntoView({
184
+ behavior: "smooth",
185
+ block: "center"
186
+ });
187
+ }
188
+ },
189
+ startPendingComment() {
190
+ p.setState((e) => ({
191
+ ...e,
192
+ selectedThreadId: void 0,
193
+ pendingComment: !0
194
+ })), t.getExtension(n)?.showSelection(!0, "comments");
195
+ },
196
+ stopPendingComment() {
197
+ p.setState((e) => ({
198
+ ...e,
199
+ selectedThreadId: void 0,
200
+ pendingComment: !1
201
+ })), t.getExtension(n)?.showSelection(!1, "comments");
202
+ },
203
+ async createThread(e) {
204
+ let n = await i.createThread(e);
205
+ if (i.addThreadToDocument) {
206
+ let e = t.prosemirrorView, r = e.state.selection, a = f.getState(e.state), o = {
207
+ prosemirror: {
208
+ head: r.head,
209
+ anchor: r.anchor
210
+ },
211
+ yjs: a ? d(a.binding, e.state) : void 0
212
+ };
213
+ await i.addThreadToDocument({
214
+ threadId: n.id,
215
+ selection: o
216
+ });
217
+ } else t._tiptapEditor.commands.setMark(s, {
218
+ orphan: !1,
219
+ threadId: n.id
220
+ });
221
+ },
222
+ userStore: c,
223
+ commentEditorSchema: r,
224
+ tiptapExtensions: [m]
225
+ };
226
+ }), y = class {}, b = class extends y {
227
+ constructor(e, t) {
228
+ super(), this.userId = e, this.role = t;
229
+ }
230
+ canCreateThread() {
231
+ return !0;
232
+ }
233
+ canAddComment(e) {
234
+ return !0;
235
+ }
236
+ canUpdateComment(e) {
237
+ return e.userId === this.userId;
238
+ }
239
+ canDeleteComment(e) {
240
+ return e.userId === this.userId || this.role === "editor";
241
+ }
242
+ canDeleteThread(e) {
243
+ return this.role === "editor";
244
+ }
245
+ canResolveThread(e) {
246
+ return !0;
247
+ }
248
+ canUnresolveThread(e) {
249
+ return !0;
250
+ }
251
+ canAddReaction(e, t) {
252
+ return t ? !e.reactions.some((e) => e.emoji === t && e.userIds.includes(this.userId)) : !0;
253
+ }
254
+ canDeleteReaction(e, t) {
255
+ return t ? e.reactions.some((e) => e.emoji === t && e.userIds.includes(this.userId)) : !0;
256
+ }
257
+ }, x = class {
258
+ auth;
259
+ constructor(e) {
260
+ this.auth = e;
261
+ }
262
+ }, S = class extends x {
263
+ constructor(e, t, n) {
264
+ super(n), this.userId = e, this.provider = t;
265
+ }
266
+ async createThread(e) {
267
+ let t = this.provider.createThread({ data: e.metadata });
268
+ return t = this.provider.addComment(t.id, {
269
+ content: e.initialComment.body,
270
+ data: {
271
+ metadata: e.initialComment.metadata,
272
+ userId: this.userId
273
+ }
274
+ }), this.tiptapThreadToThreadData(t);
275
+ }
276
+ addThreadToDocument = void 0;
277
+ async addComment(e) {
278
+ let t = this.provider.addComment(e.threadId, {
279
+ content: e.comment.body,
280
+ data: {
281
+ metadata: e.comment.metadata,
282
+ userId: this.userId
283
+ }
284
+ });
285
+ return this.tiptapCommentToCommentData(t.comments[t.comments.length - 1]);
286
+ }
287
+ async updateComment(e) {
288
+ let t = this.provider.getThreadComment(e.threadId, e.commentId, !0);
289
+ if (!t) throw Error("Comment not found");
290
+ this.provider.updateComment(e.threadId, e.commentId, {
291
+ content: e.comment.body,
292
+ data: {
293
+ ...t.data,
294
+ metadata: e.comment.metadata
295
+ }
296
+ });
297
+ }
298
+ tiptapCommentToCommentData(e) {
299
+ let t = [];
300
+ for (let n of e.data?.reactions || []) {
301
+ let e = t.find((e) => e.emoji === n.emoji);
302
+ e ? (e.userIds.push(n.userId), e.createdAt = new Date(Math.min(e.createdAt.getTime(), n.createdAt))) : t.push({
303
+ emoji: n.emoji,
304
+ createdAt: new Date(n.createdAt),
305
+ userIds: [n.userId]
306
+ });
307
+ }
308
+ return {
309
+ type: "comment",
310
+ id: e.id,
311
+ body: e.content,
312
+ metadata: e.data?.metadata,
313
+ userId: e.data?.userId,
314
+ createdAt: new Date(e.createdAt),
315
+ updatedAt: new Date(e.updatedAt),
316
+ reactions: t
317
+ };
318
+ }
319
+ tiptapThreadToThreadData(e) {
320
+ return {
321
+ type: "thread",
322
+ id: e.id,
323
+ comments: e.comments.map((e) => this.tiptapCommentToCommentData(e)),
324
+ resolved: !!e.resolvedAt,
325
+ metadata: e.data?.metadata,
326
+ createdAt: new Date(e.createdAt),
327
+ updatedAt: new Date(e.updatedAt)
328
+ };
329
+ }
330
+ async deleteComment(e) {
331
+ this.provider.deleteComment(e.threadId, e.commentId);
332
+ }
333
+ async deleteThread(e) {
334
+ this.provider.deleteThread(e.threadId);
335
+ }
336
+ async resolveThread(e) {
337
+ this.provider.updateThread(e.threadId, { resolvedAt: (/* @__PURE__ */ new Date()).toISOString() });
338
+ }
339
+ async unresolveThread(e) {
340
+ this.provider.updateThread(e.threadId, { resolvedAt: null });
341
+ }
342
+ async addReaction(e) {
343
+ let t = this.provider.getThreadComment(e.threadId, e.commentId, !0);
344
+ if (!t) throw Error("Comment not found");
345
+ this.provider.updateComment(e.threadId, e.commentId, { data: {
346
+ ...t.data,
347
+ reactions: [...t.data?.reactions || [], {
348
+ emoji: e.emoji,
349
+ createdAt: Date.now(),
350
+ userId: this.userId
351
+ }]
352
+ } });
353
+ }
354
+ async deleteReaction(e) {
355
+ let t = this.provider.getThreadComment(e.threadId, e.commentId, !0);
356
+ if (!t) throw Error("Comment not found");
357
+ this.provider.updateComment(e.threadId, e.commentId, { data: {
358
+ ...t.data,
359
+ reactions: (t.data?.reactions || []).filter((t) => t.emoji !== e.emoji && t.userId !== this.userId)
360
+ } });
361
+ }
362
+ getThread(e) {
363
+ let t = this.provider.getThread(e);
364
+ if (!t) throw Error("Thread not found");
365
+ return this.tiptapThreadToThreadData(t);
366
+ }
367
+ getThreads() {
368
+ return new Map(this.provider.getThreads().map((e) => [e.id, this.tiptapThreadToThreadData(e)]));
369
+ }
370
+ subscribe(e) {
371
+ let t = () => {
372
+ e(this.getThreads());
373
+ };
374
+ return this.provider.watchThreads(t), () => {
375
+ this.provider.unwatchThreads(t);
376
+ };
377
+ }
378
+ };
379
+ //#endregion
380
+ //#region src/comments/threadstore/yjs/yjsHelpers.ts
381
+ function C(e) {
382
+ let t = new p.Map();
383
+ if (t.set("id", e.id), t.set("userId", e.userId), t.set("createdAt", e.createdAt.getTime()), t.set("updatedAt", e.updatedAt.getTime()), e.deletedAt ? (t.set("deletedAt", e.deletedAt.getTime()), t.set("body", void 0)) : t.set("body", e.body), e.reactions.length > 0) throw Error("Reactions should be empty in commentToYMap");
384
+ return t.set("reactionsByUser", new p.Map()), t.set("metadata", e.metadata), t;
640
385
  }
641
- function p(r) {
642
- return {
643
- type: "thread",
644
- id: r.get("id"),
645
- createdAt: new Date(r.get("createdAt")),
646
- updatedAt: new Date(r.get("updatedAt")),
647
- comments: (r.get("comments") || []).map(
648
- (d) => I(d)
649
- ),
650
- resolved: r.get("resolved"),
651
- resolvedUpdatedAt: new Date(r.get("resolvedUpdatedAt")),
652
- resolvedBy: r.get("resolvedBy"),
653
- metadata: r.get("metadata")
654
- };
386
+ function w(e) {
387
+ let t = new p.Map();
388
+ t.set("id", e.id), t.set("createdAt", e.createdAt.getTime()), t.set("updatedAt", e.updatedAt.getTime());
389
+ let n = new p.Array();
390
+ return n.push(e.comments.map((e) => C(e))), t.set("comments", n), t.set("resolved", e.resolved), t.set("resolvedUpdatedAt", e.resolvedUpdatedAt?.getTime()), t.set("resolvedBy", e.resolvedBy), t.set("metadata", e.metadata), t;
655
391
  }
656
- class j extends R {
657
- constructor(d, e) {
658
- super(e), this.threadsYMap = d;
659
- }
660
- // TODO: async / reactive interface?
661
- getThread(d) {
662
- const e = this.threadsYMap.get(d);
663
- if (!e)
664
- throw new Error("Thread not found");
665
- return p(e);
666
- }
667
- getThreads() {
668
- const d = /* @__PURE__ */ new Map();
669
- return this.threadsYMap.forEach((e, t) => {
670
- e instanceof g.Map && d.set(t, p(e));
671
- }), d;
672
- }
673
- subscribe(d) {
674
- const e = () => {
675
- d(this.getThreads());
676
- };
677
- return this.threadsYMap.observeDeep(e), () => {
678
- this.threadsYMap.unobserveDeep(e);
679
- };
680
- }
392
+ function T(e) {
393
+ return {
394
+ emoji: e.get("emoji"),
395
+ createdAt: new Date(e.get("createdAt")),
396
+ userId: e.get("userId")
397
+ };
681
398
  }
682
- class ce extends j {
683
- constructor(e, t, a, s) {
684
- super(a, s);
685
- c(this, "doRequest", async (e, t, a) => {
686
- const s = await fetch(`${this.BASE_URL}${e}`, {
687
- method: t,
688
- body: JSON.stringify(a),
689
- headers: {
690
- "Content-Type": "application/json",
691
- ...this.headers
692
- }
693
- });
694
- if (!s.ok)
695
- throw new Error(`Failed to ${t} ${e}: ${s.statusText}`);
696
- return s.json();
697
- });
698
- c(this, "addThreadToDocument", async (e) => {
699
- const { threadId: t, ...a } = e;
700
- return this.doRequest(`/${t}/addToDocument`, "POST", a);
701
- });
702
- c(this, "createThread", async (e) => this.doRequest("", "POST", e));
703
- c(this, "addComment", (e) => {
704
- const { threadId: t, ...a } = e;
705
- return this.doRequest(`/${t}/comments`, "POST", a);
706
- });
707
- c(this, "updateComment", (e) => {
708
- const { threadId: t, commentId: a, ...s } = e;
709
- return this.doRequest(`/${t}/comments/${a}`, "PUT", s);
710
- });
711
- c(this, "deleteComment", (e) => {
712
- const { threadId: t, commentId: a, ...s } = e;
713
- return this.doRequest(
714
- `/${t}/comments/${a}?soft=${!!s.softDelete}`,
715
- "DELETE"
716
- );
717
- });
718
- c(this, "deleteThread", (e) => this.doRequest(`/${e.threadId}`, "DELETE"));
719
- c(this, "resolveThread", (e) => this.doRequest(`/${e.threadId}/resolve`, "POST"));
720
- c(this, "unresolveThread", (e) => this.doRequest(`/${e.threadId}/unresolve`, "POST"));
721
- c(this, "addReaction", (e) => {
722
- const { threadId: t, commentId: a, ...s } = e;
723
- return this.doRequest(
724
- `/${t}/comments/${a}/reactions`,
725
- "POST",
726
- s
727
- );
728
- });
729
- c(this, "deleteReaction", (e) => this.doRequest(
730
- `/${e.threadId}/comments/${e.commentId}/reactions/${e.emoji}`,
731
- "DELETE"
732
- ));
733
- this.BASE_URL = e, this.headers = t;
734
- }
399
+ function E(e) {
400
+ return [...e.values()].map((e) => T(e)).reduce((e, t) => {
401
+ let n = e.find((e) => e.emoji === t.emoji);
402
+ return n ? (n.userIds.push(t.userId), n.createdAt = new Date(Math.min(n.createdAt.getTime(), t.createdAt.getTime()))) : e.push({
403
+ emoji: t.emoji,
404
+ createdAt: t.createdAt,
405
+ userIds: [t.userId]
406
+ }), e;
407
+ }, []);
735
408
  }
736
- class he extends j {
737
- constructor(e, t, a) {
738
- super(t, a);
739
- c(this, "transact", (e) => async (t) => this.threadsYMap.doc.transact(() => e(t)));
740
- c(this, "createThread", this.transact(
741
- (e) => {
742
- if (!this.auth.canCreateThread())
743
- throw new Error("Not authorized");
744
- const t = /* @__PURE__ */ new Date(), a = {
745
- type: "comment",
746
- id: D(),
747
- userId: this.userId,
748
- createdAt: t,
749
- updatedAt: t,
750
- reactions: [],
751
- metadata: e.initialComment.metadata,
752
- body: e.initialComment.body
753
- }, s = {
754
- type: "thread",
755
- id: D(),
756
- createdAt: t,
757
- updatedAt: t,
758
- comments: [a],
759
- resolved: !1,
760
- metadata: e.metadata
761
- };
762
- return this.threadsYMap.set(s.id, G(s)), s;
763
- }
764
- ));
765
- // YjsThreadStore does not support addThreadToDocument
766
- c(this, "addThreadToDocument");
767
- c(this, "addComment", this.transact(
768
- (e) => {
769
- const t = this.threadsYMap.get(e.threadId);
770
- if (!t)
771
- throw new Error("Thread not found");
772
- if (!this.auth.canAddComment(p(t)))
773
- throw new Error("Not authorized");
774
- const a = /* @__PURE__ */ new Date(), s = {
775
- type: "comment",
776
- id: D(),
777
- userId: this.userId,
778
- createdAt: a,
779
- updatedAt: a,
780
- deletedAt: void 0,
781
- reactions: [],
782
- metadata: e.comment.metadata,
783
- body: e.comment.body
784
- };
785
- return t.get("comments").push([
786
- U(s)
787
- ]), t.set("updatedAt", (/* @__PURE__ */ new Date()).getTime()), s;
788
- }
789
- ));
790
- c(this, "updateComment", this.transact(
791
- (e) => {
792
- const t = this.threadsYMap.get(e.threadId);
793
- if (!t)
794
- throw new Error("Thread not found");
795
- const a = y(
796
- t.get("comments"),
797
- (o) => o.get("id") === e.commentId
798
- );
799
- if (a === -1)
800
- throw new Error("Comment not found");
801
- const s = t.get("comments").get(a);
802
- if (!this.auth.canUpdateComment(I(s)))
803
- throw new Error("Not authorized");
804
- s.set("body", e.comment.body), s.set("updatedAt", (/* @__PURE__ */ new Date()).getTime()), s.set("metadata", e.comment.metadata);
805
- }
806
- ));
807
- c(this, "deleteComment", this.transact(
808
- (e) => {
809
- const t = this.threadsYMap.get(e.threadId);
810
- if (!t)
811
- throw new Error("Thread not found");
812
- const a = y(
813
- t.get("comments"),
814
- (o) => o.get("id") === e.commentId
815
- );
816
- if (a === -1)
817
- throw new Error("Comment not found");
818
- const s = t.get("comments").get(a);
819
- if (!this.auth.canDeleteComment(I(s)))
820
- throw new Error("Not authorized");
821
- if (s.get("deletedAt"))
822
- throw new Error("Comment already deleted");
823
- 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());
824
- }
825
- ));
826
- c(this, "deleteThread", this.transact((e) => {
827
- if (!this.auth.canDeleteThread(
828
- p(this.threadsYMap.get(e.threadId))
829
- ))
830
- throw new Error("Not authorized");
831
- this.threadsYMap.delete(e.threadId);
832
- }));
833
- c(this, "resolveThread", this.transact((e) => {
834
- const t = this.threadsYMap.get(e.threadId);
835
- if (!t)
836
- throw new Error("Thread not found");
837
- if (!this.auth.canResolveThread(p(t)))
838
- throw new Error("Not authorized");
839
- t.set("resolved", !0), t.set("resolvedUpdatedAt", (/* @__PURE__ */ new Date()).getTime()), t.set("resolvedBy", this.userId);
840
- }));
841
- c(this, "unresolveThread", this.transact((e) => {
842
- const t = this.threadsYMap.get(e.threadId);
843
- if (!t)
844
- throw new Error("Thread not found");
845
- if (!this.auth.canUnresolveThread(p(t)))
846
- throw new Error("Not authorized");
847
- t.set("resolved", !1), t.set("resolvedUpdatedAt", (/* @__PURE__ */ new Date()).getTime());
848
- }));
849
- c(this, "addReaction", this.transact(
850
- (e) => {
851
- const t = this.threadsYMap.get(e.threadId);
852
- if (!t)
853
- throw new Error("Thread not found");
854
- const a = y(
855
- t.get("comments"),
856
- (i) => i.get("id") === e.commentId
857
- );
858
- if (a === -1)
859
- throw new Error("Comment not found");
860
- const s = t.get("comments").get(a);
861
- if (!this.auth.canAddReaction(I(s), e.emoji))
862
- throw new Error("Not authorized");
863
- const o = /* @__PURE__ */ new Date(), m = `${this.userId}-${e.emoji}`, n = s.get("reactionsByUser");
864
- if (!n.has(m)) {
865
- const i = new g.Map();
866
- i.set("emoji", e.emoji), i.set("createdAt", o.getTime()), i.set("userId", this.userId), n.set(m, i);
867
- }
868
- }
869
- ));
870
- c(this, "deleteReaction", this.transact(
871
- (e) => {
872
- const t = this.threadsYMap.get(e.threadId);
873
- if (!t)
874
- throw new Error("Thread not found");
875
- const a = y(
876
- t.get("comments"),
877
- (n) => n.get("id") === e.commentId
878
- );
879
- if (a === -1)
880
- throw new Error("Comment not found");
881
- const s = t.get("comments").get(a);
882
- if (!this.auth.canDeleteReaction(I(s), e.emoji))
883
- throw new Error("Not authorized");
884
- const o = `${this.userId}-${e.emoji}`;
885
- s.get("reactionsByUser").delete(o);
886
- }
887
- ));
888
- this.userId = e;
889
- }
409
+ function D(e) {
410
+ return {
411
+ type: "comment",
412
+ id: e.get("id"),
413
+ userId: e.get("userId"),
414
+ createdAt: new Date(e.get("createdAt")),
415
+ updatedAt: new Date(e.get("updatedAt")),
416
+ deletedAt: e.get("deletedAt") ? new Date(e.get("deletedAt")) : void 0,
417
+ reactions: E(e.get("reactionsByUser")),
418
+ metadata: e.get("metadata"),
419
+ body: e.get("body")
420
+ };
890
421
  }
891
- function y(r, d) {
892
- for (let e = 0; e < r.length; e++)
893
- if (d(r.get(e)))
894
- return e;
895
- return -1;
422
+ function O(e) {
423
+ return {
424
+ type: "thread",
425
+ id: e.get("id"),
426
+ createdAt: new Date(e.get("createdAt")),
427
+ updatedAt: new Date(e.get("updatedAt")),
428
+ comments: (e.get("comments") || []).map((e) => D(e)),
429
+ resolved: e.get("resolved"),
430
+ resolvedUpdatedAt: new Date(e.get("resolvedUpdatedAt")),
431
+ resolvedBy: e.get("resolvedBy"),
432
+ metadata: e.get("metadata")
433
+ };
896
434
  }
897
- export {
898
- M as CommentMark,
899
- ne as CommentsExtension,
900
- oe as DefaultThreadStoreAuth,
901
- ce as RESTYjsThreadStore,
902
- R as ThreadStore,
903
- V as ThreadStoreAuth,
904
- ie as TiptapThreadStore,
905
- he as YjsThreadStore,
906
- j as YjsThreadStoreBase
435
+ //#endregion
436
+ //#region src/comments/threadstore/yjs/YjsThreadStoreBase.ts
437
+ var k = class extends x {
438
+ constructor(e, t) {
439
+ super(t), this.threadsYMap = e;
440
+ }
441
+ getThread(e) {
442
+ let t = this.threadsYMap.get(e);
443
+ if (!t) throw Error("Thread not found");
444
+ return O(t);
445
+ }
446
+ getThreads() {
447
+ let e = /* @__PURE__ */ new Map();
448
+ return this.threadsYMap.forEach((t, n) => {
449
+ t instanceof p.Map && e.set(n, O(t));
450
+ }), e;
451
+ }
452
+ subscribe(e) {
453
+ let t = () => {
454
+ e(this.getThreads());
455
+ };
456
+ return this.threadsYMap.observeDeep(t), () => {
457
+ this.threadsYMap.unobserveDeep(t);
458
+ };
459
+ }
460
+ }, A = class extends k {
461
+ constructor(e, t, n, r) {
462
+ super(n, r), this.BASE_URL = e, this.headers = t;
463
+ }
464
+ doRequest = async (e, t, n) => {
465
+ let r = await fetch(`${this.BASE_URL}${e}`, {
466
+ method: t,
467
+ body: JSON.stringify(n),
468
+ headers: {
469
+ "Content-Type": "application/json",
470
+ ...this.headers
471
+ }
472
+ });
473
+ if (!r.ok) throw Error(`Failed to ${t} ${e}: ${r.statusText}`);
474
+ return r.json();
475
+ };
476
+ addThreadToDocument = async (e) => {
477
+ let { threadId: t, ...n } = e;
478
+ return this.doRequest(`/${t}/addToDocument`, "POST", n);
479
+ };
480
+ createThread = async (e) => this.doRequest("", "POST", e);
481
+ addComment = (e) => {
482
+ let { threadId: t, ...n } = e;
483
+ return this.doRequest(`/${t}/comments`, "POST", n);
484
+ };
485
+ updateComment = (e) => {
486
+ let { threadId: t, commentId: n, ...r } = e;
487
+ return this.doRequest(`/${t}/comments/${n}`, "PUT", r);
488
+ };
489
+ deleteComment = (e) => {
490
+ let { threadId: t, commentId: n, ...r } = e;
491
+ return this.doRequest(`/${t}/comments/${n}?soft=${!!r.softDelete}`, "DELETE");
492
+ };
493
+ deleteThread = (e) => this.doRequest(`/${e.threadId}`, "DELETE");
494
+ resolveThread = (e) => this.doRequest(`/${e.threadId}/resolve`, "POST");
495
+ unresolveThread = (e) => this.doRequest(`/${e.threadId}/unresolve`, "POST");
496
+ addReaction = (e) => {
497
+ let { threadId: t, commentId: n, ...r } = e;
498
+ return this.doRequest(`/${t}/comments/${n}/reactions`, "POST", r);
499
+ };
500
+ deleteReaction = (e) => this.doRequest(`/${e.threadId}/comments/${e.commentId}/reactions/${e.emoji}`, "DELETE");
501
+ }, j = class extends k {
502
+ constructor(e, t, n) {
503
+ super(t, n), this.userId = e;
504
+ }
505
+ transact = (e) => async (t) => this.threadsYMap.doc.transact(() => e(t));
506
+ createThread = this.transact((e) => {
507
+ if (!this.auth.canCreateThread()) throw Error("Not authorized");
508
+ let t = /* @__PURE__ */ new Date(), n = {
509
+ type: "comment",
510
+ id: c(),
511
+ userId: this.userId,
512
+ createdAt: t,
513
+ updatedAt: t,
514
+ reactions: [],
515
+ metadata: e.initialComment.metadata,
516
+ body: e.initialComment.body
517
+ }, r = {
518
+ type: "thread",
519
+ id: c(),
520
+ createdAt: t,
521
+ updatedAt: t,
522
+ comments: [n],
523
+ resolved: !1,
524
+ metadata: e.metadata
525
+ };
526
+ return this.threadsYMap.set(r.id, w(r)), r;
527
+ });
528
+ addThreadToDocument = void 0;
529
+ addComment = this.transact((e) => {
530
+ let t = this.threadsYMap.get(e.threadId);
531
+ if (!t) throw Error("Thread not found");
532
+ if (!this.auth.canAddComment(O(t))) throw Error("Not authorized");
533
+ let n = /* @__PURE__ */ new Date(), r = {
534
+ type: "comment",
535
+ id: c(),
536
+ userId: this.userId,
537
+ createdAt: n,
538
+ updatedAt: n,
539
+ deletedAt: void 0,
540
+ reactions: [],
541
+ metadata: e.comment.metadata,
542
+ body: e.comment.body
543
+ };
544
+ return t.get("comments").push([C(r)]), t.set("updatedAt", (/* @__PURE__ */ new Date()).getTime()), r;
545
+ });
546
+ updateComment = this.transact((e) => {
547
+ let t = this.threadsYMap.get(e.threadId);
548
+ if (!t) throw Error("Thread not found");
549
+ let n = M(t.get("comments"), (t) => t.get("id") === e.commentId);
550
+ if (n === -1) throw Error("Comment not found");
551
+ let r = t.get("comments").get(n);
552
+ if (!this.auth.canUpdateComment(D(r))) throw Error("Not authorized");
553
+ r.set("body", e.comment.body), r.set("updatedAt", (/* @__PURE__ */ new Date()).getTime()), r.set("metadata", e.comment.metadata);
554
+ });
555
+ deleteComment = this.transact((e) => {
556
+ let t = this.threadsYMap.get(e.threadId);
557
+ if (!t) throw Error("Thread not found");
558
+ let n = M(t.get("comments"), (t) => t.get("id") === e.commentId);
559
+ if (n === -1) throw Error("Comment not found");
560
+ let r = t.get("comments").get(n);
561
+ if (!this.auth.canDeleteComment(D(r))) throw Error("Not authorized");
562
+ if (r.get("deletedAt")) throw Error("Comment already deleted");
563
+ e.softDelete ? (r.set("deletedAt", (/* @__PURE__ */ new Date()).getTime()), r.set("body", void 0)) : t.get("comments").delete(n), t.get("comments").toArray().every((e) => e.get("deletedAt")) && (e.softDelete ? t.set("deletedAt", (/* @__PURE__ */ new Date()).getTime()) : this.threadsYMap.delete(e.threadId)), t.set("updatedAt", (/* @__PURE__ */ new Date()).getTime());
564
+ });
565
+ deleteThread = this.transact((e) => {
566
+ if (!this.auth.canDeleteThread(O(this.threadsYMap.get(e.threadId)))) throw Error("Not authorized");
567
+ this.threadsYMap.delete(e.threadId);
568
+ });
569
+ resolveThread = this.transact((e) => {
570
+ let t = this.threadsYMap.get(e.threadId);
571
+ if (!t) throw Error("Thread not found");
572
+ if (!this.auth.canResolveThread(O(t))) throw Error("Not authorized");
573
+ t.set("resolved", !0), t.set("resolvedUpdatedAt", (/* @__PURE__ */ new Date()).getTime()), t.set("resolvedBy", this.userId);
574
+ });
575
+ unresolveThread = this.transact((e) => {
576
+ let t = this.threadsYMap.get(e.threadId);
577
+ if (!t) throw Error("Thread not found");
578
+ if (!this.auth.canUnresolveThread(O(t))) throw Error("Not authorized");
579
+ t.set("resolved", !1), t.set("resolvedUpdatedAt", (/* @__PURE__ */ new Date()).getTime());
580
+ });
581
+ addReaction = this.transact((e) => {
582
+ let t = this.threadsYMap.get(e.threadId);
583
+ if (!t) throw Error("Thread not found");
584
+ let n = M(t.get("comments"), (t) => t.get("id") === e.commentId);
585
+ if (n === -1) throw Error("Comment not found");
586
+ let r = t.get("comments").get(n);
587
+ if (!this.auth.canAddReaction(D(r), e.emoji)) throw Error("Not authorized");
588
+ let i = /* @__PURE__ */ new Date(), a = `${this.userId}-${e.emoji}`, o = r.get("reactionsByUser");
589
+ if (!o.has(a)) {
590
+ let t = new p.Map();
591
+ t.set("emoji", e.emoji), t.set("createdAt", i.getTime()), t.set("userId", this.userId), o.set(a, t);
592
+ }
593
+ });
594
+ deleteReaction = this.transact((e) => {
595
+ let t = this.threadsYMap.get(e.threadId);
596
+ if (!t) throw Error("Thread not found");
597
+ let n = M(t.get("comments"), (t) => t.get("id") === e.commentId);
598
+ if (n === -1) throw Error("Comment not found");
599
+ let r = t.get("comments").get(n);
600
+ if (!this.auth.canDeleteReaction(D(r), e.emoji)) throw Error("Not authorized");
601
+ let i = `${this.userId}-${e.emoji}`;
602
+ r.get("reactionsByUser").delete(i);
603
+ });
907
604
  };
908
- //# sourceMappingURL=comments.js.map
605
+ function M(e, t) {
606
+ for (let n = 0; n < e.length; n++) if (t(e.get(n))) return n;
607
+ return -1;
608
+ }
609
+ //#endregion
610
+ export { m as CommentMark, v as CommentsExtension, b as DefaultThreadStoreAuth, A as RESTYjsThreadStore, x as ThreadStore, y as ThreadStoreAuth, S as TiptapThreadStore, j as YjsThreadStore, k as YjsThreadStoreBase };
611
+
612
+ //# sourceMappingURL=comments.js.map