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