@hocuspocus/provider 2.10.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.
@@ -2917,26 +2917,34 @@ class TiptapCollabProvider extends HocuspocusProvider {
2917
2917
  return this.getYThreads().get(index);
2918
2918
  }
2919
2919
  createThread(data) {
2920
- const thread = new Y__namespace.Map();
2921
- thread.set('id', uuidv4());
2922
- thread.set('createdAt', (new Date()).toISOString());
2923
- thread.set('comments', new Y__namespace.Array());
2924
- this.getYThreads().push([thread]);
2925
- return this.updateThread(String(thread.get('id')), data);
2920
+ let createdThread = {};
2921
+ this.document.transact(() => {
2922
+ const thread = new Y__namespace.Map();
2923
+ thread.set('id', uuidv4());
2924
+ thread.set('createdAt', (new Date()).toISOString());
2925
+ thread.set('comments', new Y__namespace.Array());
2926
+ this.getYThreads().push([thread]);
2927
+ createdThread = this.updateThread(String(thread.get('id')), data);
2928
+ });
2929
+ return createdThread;
2926
2930
  }
2927
2931
  updateThread(id, data) {
2928
- const thread = this.getYThread(id);
2929
- if (thread === null) {
2930
- return null;
2931
- }
2932
- thread.set('updatedAt', (new Date()).toISOString());
2933
- if (data.data) {
2934
- thread.set('data', data.data);
2935
- }
2936
- if (data.resolvedAt || data.resolvedAt === null) {
2937
- thread.set('resolvedAt', data.resolvedAt);
2938
- }
2939
- return thread.toJSON();
2932
+ let updatedThread = {};
2933
+ this.document.transact(() => {
2934
+ const thread = this.getYThread(id);
2935
+ if (thread === null) {
2936
+ return null;
2937
+ }
2938
+ thread.set('updatedAt', (new Date()).toISOString());
2939
+ if (data.data) {
2940
+ thread.set('data', data.data);
2941
+ }
2942
+ if (data.resolvedAt || data.resolvedAt === null) {
2943
+ thread.set('resolvedAt', data.resolvedAt);
2944
+ }
2945
+ updatedThread = thread.toJSON();
2946
+ });
2947
+ return updatedThread;
2940
2948
  }
2941
2949
  deleteThread(id) {
2942
2950
  const index = this.getThreadIndex(id);
@@ -2962,38 +2970,46 @@ class TiptapCollabProvider extends HocuspocusProvider {
2962
2970
  return (_b = (_a = this.getThread(threadId)) === null || _a === void 0 ? void 0 : _a.comments.find(comment => comment.id === commentId)) !== null && _b !== void 0 ? _b : null;
2963
2971
  }
2964
2972
  addComment(threadId, data) {
2965
- const thread = this.getYThread(threadId);
2966
- if (thread === null)
2967
- return null;
2968
- const commentMap = new Y__namespace.Map();
2969
- commentMap.set('id', uuidv4());
2970
- commentMap.set('createdAt', (new Date()).toISOString());
2971
- thread.get('comments').push([commentMap]);
2972
- this.updateComment(threadId, String(commentMap.get('id')), data);
2973
- return thread.toJSON();
2973
+ let updatedThread = {};
2974
+ this.document.transact(() => {
2975
+ const thread = this.getYThread(threadId);
2976
+ if (thread === null)
2977
+ return null;
2978
+ const commentMap = new Y__namespace.Map();
2979
+ commentMap.set('id', uuidv4());
2980
+ commentMap.set('createdAt', (new Date()).toISOString());
2981
+ thread.get('comments').push([commentMap]);
2982
+ this.updateComment(threadId, String(commentMap.get('id')), data);
2983
+ updatedThread = thread.toJSON();
2984
+ });
2985
+ return updatedThread;
2974
2986
  }
2975
2987
  updateComment(threadId, commentId, data) {
2976
- const thread = this.getYThread(threadId);
2977
- if (thread === null)
2978
- return null;
2979
- let comment = null;
2980
- // eslint-disable-next-line no-restricted-syntax
2981
- for (const c of thread.get('comments')) {
2982
- if (c.get('id') === commentId) {
2983
- comment = c;
2984
- break;
2988
+ let updatedThread = {};
2989
+ this.document.transact(() => {
2990
+ const thread = this.getYThread(threadId);
2991
+ if (thread === null)
2992
+ return null;
2993
+ let comment = null;
2994
+ // eslint-disable-next-line no-restricted-syntax
2995
+ for (const c of thread.get('comments')) {
2996
+ if (c.get('id') === commentId) {
2997
+ comment = c;
2998
+ break;
2999
+ }
2985
3000
  }
2986
- }
2987
- if (comment === null)
2988
- return null;
2989
- comment.set('updatedAt', (new Date()).toISOString());
2990
- if (data.data) {
2991
- comment.set('data', data.data);
2992
- }
2993
- if (data.content) {
2994
- comment.set('content', data.content);
2995
- }
2996
- return thread.toJSON();
3001
+ if (comment === null)
3002
+ return null;
3003
+ comment.set('updatedAt', (new Date()).toISOString());
3004
+ if (data.data) {
3005
+ comment.set('data', data.data);
3006
+ }
3007
+ if (data.content) {
3008
+ comment.set('content', data.content);
3009
+ }
3010
+ updatedThread = thread.toJSON();
3011
+ });
3012
+ return updatedThread;
2997
3013
  }
2998
3014
  deleteComment(threadId, commentId) {
2999
3015
  const thread = this.getYThread(threadId);
@@ -3007,7 +3023,13 @@ class TiptapCollabProvider extends HocuspocusProvider {
3007
3023
  }
3008
3024
  commentIndex += 1;
3009
3025
  }
3010
- if (commentIndex >= 0) {
3026
+ // if the first comment of a thread is deleted we also
3027
+ // delete the thread itself as the source comment is gone
3028
+ if (commentIndex === 0) {
3029
+ this.deleteThread(threadId);
3030
+ return;
3031
+ }
3032
+ if (commentIndex > 0) {
3011
3033
  thread.get('comments').delete(commentIndex);
3012
3034
  }
3013
3035
  return thread.toJSON();