@hocuspocus/provider 2.11.0 → 2.11.2

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.
@@ -45,14 +45,16 @@ export declare class TiptapCollabProvider extends HocuspocusProvider {
45
45
  private getThreadIndex;
46
46
  getThread<Data, CommentData>(id: string): TCollabThread<Data, CommentData> | null;
47
47
  private getYThread;
48
- createThread(data: Omit<TCollabThread, 'id' | 'createdAt' | 'updatedAt' | 'comments'>): TCollabThread | null;
49
- updateThread(id: TCollabThread['id'], data: Partial<Pick<TCollabThread, 'data' | 'resolvedAt'>>): TCollabThread | null;
48
+ createThread(data: Omit<TCollabThread, 'id' | 'createdAt' | 'updatedAt' | 'comments'>): TCollabThread;
49
+ updateThread(id: TCollabThread['id'], data: Partial<Pick<TCollabThread, 'data'> & {
50
+ resolvedAt: TCollabThread['resolvedAt'] | null;
51
+ }>): TCollabThread;
50
52
  deleteThread(id: TCollabThread['id']): void;
51
53
  getThreadComments(threadId: TCollabThread['id']): TCollabComment[] | null;
52
54
  getThreadComment(threadId: TCollabThread['id'], commentId: TCollabComment['id']): TCollabComment | null;
53
- addComment(threadId: TCollabThread['id'], data: Omit<TCollabComment, 'id' | 'updatedAt' | 'createdAt'>): TCollabThread | null;
54
- updateComment(threadId: TCollabThread['id'], commentId: TCollabComment['id'], data: Partial<Pick<TCollabComment, 'data' | 'content'>>): TCollabThread | null;
55
- deleteComment(threadId: TCollabThread['id'], commentId: TCollabComment['id']): TCollabThread | null;
55
+ addComment(threadId: TCollabThread['id'], data: Omit<TCollabComment, 'id' | 'updatedAt' | 'createdAt'>): TCollabThread;
56
+ updateComment(threadId: TCollabThread['id'], commentId: TCollabComment['id'], data: Partial<Pick<TCollabComment, 'data' | 'content'>>): TCollabThread;
57
+ deleteComment(threadId: TCollabThread['id'], commentId: TCollabComment['id']): TCollabThread | null | undefined;
56
58
  watchThreads(callback: () => void): void;
57
59
  unwatchThreads(callback: () => void): void;
58
60
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hocuspocus/provider",
3
- "version": "2.11.0",
3
+ "version": "2.11.2",
4
4
  "description": "hocuspocus provider",
5
5
  "homepage": "https://hocuspocus.dev",
6
6
  "keywords": [
@@ -29,7 +29,7 @@
29
29
  "dist"
30
30
  ],
31
31
  "dependencies": {
32
- "@hocuspocus/common": "^2.11.0",
32
+ "@hocuspocus/common": "^2.11.2",
33
33
  "@lifeomic/attempt": "^3.0.2",
34
34
  "lib0": "^0.2.87",
35
35
  "ws": "^8.14.2"
@@ -142,33 +142,47 @@ export class TiptapCollabProvider extends HocuspocusProvider {
142
142
  }
143
143
 
144
144
  createThread(data: Omit<TCollabThread, 'id' | 'createdAt' | 'updatedAt' | 'comments'>) {
145
- const thread = new Y.Map()
146
- thread.set('id', uuidv4())
147
- thread.set('createdAt', (new Date()).toISOString())
148
- thread.set('comments', new Y.Array())
145
+ let createdThread: TCollabThread = {} as TCollabThread
149
146
 
150
- this.getYThreads().push([thread])
151
- return this.updateThread(String(thread.get('id')), data)
147
+ this.document.transact(() => {
148
+ const thread = new Y.Map()
149
+ thread.set('id', uuidv4())
150
+ thread.set('createdAt', (new Date()).toISOString())
151
+ thread.set('comments', new Y.Array())
152
+
153
+ this.getYThreads().push([thread])
154
+ createdThread = this.updateThread(String(thread.get('id')), data)
155
+ })
156
+
157
+ return createdThread
152
158
  }
153
159
 
154
- updateThread(id: TCollabThread['id'], data: Partial<Pick<TCollabThread, 'data' | 'resolvedAt'>>) {
155
- const thread = this.getYThread(id)
160
+ updateThread(id: TCollabThread['id'], data: Partial<Pick<TCollabThread, 'data'> & {
161
+ resolvedAt: TCollabThread['resolvedAt'] | null
162
+ }>) {
163
+ let updatedThread: TCollabThread = {} as TCollabThread
156
164
 
157
- if (thread === null) {
158
- return null
159
- }
165
+ this.document.transact(() => {
166
+ const thread = this.getYThread(id)
160
167
 
161
- thread.set('updatedAt', (new Date()).toISOString())
168
+ if (thread === null) {
169
+ return null
170
+ }
162
171
 
163
- if (data.data) {
164
- thread.set('data', data.data)
165
- }
172
+ thread.set('updatedAt', (new Date()).toISOString())
166
173
 
167
- if (data.resolvedAt || data.resolvedAt === null) {
168
- thread.set('resolvedAt', data.resolvedAt)
169
- }
174
+ if (data.data) {
175
+ thread.set('data', data.data)
176
+ }
170
177
 
171
- return thread.toJSON() as TCollabThread
178
+ if (data.resolvedAt || data.resolvedAt === null) {
179
+ thread.set('resolvedAt', data.resolvedAt)
180
+ }
181
+
182
+ updatedThread = thread.toJSON() as TCollabThread
183
+ })
184
+
185
+ return updatedThread
172
186
  }
173
187
 
174
188
  deleteThread(id: TCollabThread['id']) {
@@ -202,47 +216,59 @@ export class TiptapCollabProvider extends HocuspocusProvider {
202
216
  }
203
217
 
204
218
  addComment(threadId: TCollabThread['id'], data: Omit<TCollabComment, 'id' | 'updatedAt' | 'createdAt'>) {
205
- const thread = this.getYThread(threadId)
219
+ let updatedThread: TCollabThread = {} as TCollabThread
206
220
 
207
- if (thread === null) return null
221
+ this.document.transact(() => {
222
+ const thread = this.getYThread(threadId)
208
223
 
209
- const commentMap = new Y.Map()
210
- commentMap.set('id', uuidv4())
211
- commentMap.set('createdAt', (new Date()).toISOString())
212
- thread.get('comments').push([commentMap])
224
+ if (thread === null) return null
213
225
 
214
- this.updateComment(threadId, String(commentMap.get('id')), data)
226
+ const commentMap = new Y.Map()
227
+ commentMap.set('id', uuidv4())
228
+ commentMap.set('createdAt', (new Date()).toISOString())
229
+ thread.get('comments').push([commentMap])
215
230
 
216
- return thread.toJSON() as TCollabThread
231
+ this.updateComment(threadId, String(commentMap.get('id')), data)
232
+
233
+ updatedThread = thread.toJSON() as TCollabThread
234
+ })
235
+
236
+ return updatedThread
217
237
  }
218
238
 
219
239
  updateComment(threadId: TCollabThread['id'], commentId: TCollabComment['id'], data: Partial<Pick<TCollabComment, 'data' | 'content'>>) {
220
- const thread = this.getYThread(threadId)
240
+ let updatedThread: TCollabThread = {} as TCollabThread
221
241
 
222
- if (thread === null) return null
242
+ this.document.transact(() => {
243
+ const thread = this.getYThread(threadId)
223
244
 
224
- let comment = null
225
- // eslint-disable-next-line no-restricted-syntax
226
- for (const c of thread.get('comments')) {
227
- if (c.get('id') === commentId) {
228
- comment = c
229
- break
245
+ if (thread === null) return null
246
+
247
+ let comment = null
248
+ // eslint-disable-next-line no-restricted-syntax
249
+ for (const c of thread.get('comments')) {
250
+ if (c.get('id') === commentId) {
251
+ comment = c
252
+ break
253
+ }
230
254
  }
231
- }
232
255
 
233
- if (comment === null) return null
256
+ if (comment === null) return null
234
257
 
235
- comment.set('updatedAt', (new Date()).toISOString())
258
+ comment.set('updatedAt', (new Date()).toISOString())
236
259
 
237
- if (data.data) {
238
- comment.set('data', data.data)
239
- }
260
+ if (data.data) {
261
+ comment.set('data', data.data)
262
+ }
240
263
 
241
- if (data.content) {
242
- comment.set('content', data.content)
243
- }
264
+ if (data.content) {
265
+ comment.set('content', data.content)
266
+ }
244
267
 
245
- return thread.toJSON() as TCollabThread
268
+ updatedThread = thread.toJSON() as TCollabThread
269
+ })
270
+
271
+ return updatedThread
246
272
  }
247
273
 
248
274
  deleteComment(threadId: TCollabThread['id'], commentId: TCollabComment['id']) {
@@ -259,7 +285,14 @@ export class TiptapCollabProvider extends HocuspocusProvider {
259
285
  commentIndex += 1
260
286
  }
261
287
 
262
- if (commentIndex >= 0) {
288
+ // if the first comment of a thread is deleted we also
289
+ // delete the thread itself as the source comment is gone
290
+ if (commentIndex === 0) {
291
+ this.deleteThread(threadId)
292
+ return
293
+ }
294
+
295
+ if (commentIndex > 0) {
263
296
  thread.get('comments').delete(commentIndex)
264
297
  }
265
298