@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.
@@ -2893,26 +2893,34 @@ class TiptapCollabProvider extends HocuspocusProvider {
2893
2893
  return this.getYThreads().get(index);
2894
2894
  }
2895
2895
  createThread(data) {
2896
- const thread = new Y.Map();
2897
- thread.set('id', uuidv4());
2898
- thread.set('createdAt', (new Date()).toISOString());
2899
- thread.set('comments', new Y.Array());
2900
- this.getYThreads().push([thread]);
2901
- return this.updateThread(String(thread.get('id')), data);
2896
+ let createdThread = {};
2897
+ this.document.transact(() => {
2898
+ const thread = new Y.Map();
2899
+ thread.set('id', uuidv4());
2900
+ thread.set('createdAt', (new Date()).toISOString());
2901
+ thread.set('comments', new Y.Array());
2902
+ this.getYThreads().push([thread]);
2903
+ createdThread = this.updateThread(String(thread.get('id')), data);
2904
+ });
2905
+ return createdThread;
2902
2906
  }
2903
2907
  updateThread(id, data) {
2904
- const thread = this.getYThread(id);
2905
- if (thread === null) {
2906
- return null;
2907
- }
2908
- thread.set('updatedAt', (new Date()).toISOString());
2909
- if (data.data) {
2910
- thread.set('data', data.data);
2911
- }
2912
- if (data.resolvedAt || data.resolvedAt === null) {
2913
- thread.set('resolvedAt', data.resolvedAt);
2914
- }
2915
- return thread.toJSON();
2908
+ let updatedThread = {};
2909
+ this.document.transact(() => {
2910
+ const thread = this.getYThread(id);
2911
+ if (thread === null) {
2912
+ return null;
2913
+ }
2914
+ thread.set('updatedAt', (new Date()).toISOString());
2915
+ if (data.data) {
2916
+ thread.set('data', data.data);
2917
+ }
2918
+ if (data.resolvedAt || data.resolvedAt === null) {
2919
+ thread.set('resolvedAt', data.resolvedAt);
2920
+ }
2921
+ updatedThread = thread.toJSON();
2922
+ });
2923
+ return updatedThread;
2916
2924
  }
2917
2925
  deleteThread(id) {
2918
2926
  const index = this.getThreadIndex(id);
@@ -2938,38 +2946,46 @@ class TiptapCollabProvider extends HocuspocusProvider {
2938
2946
  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;
2939
2947
  }
2940
2948
  addComment(threadId, data) {
2941
- const thread = this.getYThread(threadId);
2942
- if (thread === null)
2943
- return null;
2944
- const commentMap = new Y.Map();
2945
- commentMap.set('id', uuidv4());
2946
- commentMap.set('createdAt', (new Date()).toISOString());
2947
- thread.get('comments').push([commentMap]);
2948
- this.updateComment(threadId, String(commentMap.get('id')), data);
2949
- return thread.toJSON();
2949
+ let updatedThread = {};
2950
+ this.document.transact(() => {
2951
+ const thread = this.getYThread(threadId);
2952
+ if (thread === null)
2953
+ return null;
2954
+ const commentMap = new Y.Map();
2955
+ commentMap.set('id', uuidv4());
2956
+ commentMap.set('createdAt', (new Date()).toISOString());
2957
+ thread.get('comments').push([commentMap]);
2958
+ this.updateComment(threadId, String(commentMap.get('id')), data);
2959
+ updatedThread = thread.toJSON();
2960
+ });
2961
+ return updatedThread;
2950
2962
  }
2951
2963
  updateComment(threadId, commentId, data) {
2952
- const thread = this.getYThread(threadId);
2953
- if (thread === null)
2954
- return null;
2955
- let comment = null;
2956
- // eslint-disable-next-line no-restricted-syntax
2957
- for (const c of thread.get('comments')) {
2958
- if (c.get('id') === commentId) {
2959
- comment = c;
2960
- break;
2964
+ let updatedThread = {};
2965
+ this.document.transact(() => {
2966
+ const thread = this.getYThread(threadId);
2967
+ if (thread === null)
2968
+ return null;
2969
+ let comment = null;
2970
+ // eslint-disable-next-line no-restricted-syntax
2971
+ for (const c of thread.get('comments')) {
2972
+ if (c.get('id') === commentId) {
2973
+ comment = c;
2974
+ break;
2975
+ }
2961
2976
  }
2962
- }
2963
- if (comment === null)
2964
- return null;
2965
- comment.set('updatedAt', (new Date()).toISOString());
2966
- if (data.data) {
2967
- comment.set('data', data.data);
2968
- }
2969
- if (data.content) {
2970
- comment.set('content', data.content);
2971
- }
2972
- return thread.toJSON();
2977
+ if (comment === null)
2978
+ return null;
2979
+ comment.set('updatedAt', (new Date()).toISOString());
2980
+ if (data.data) {
2981
+ comment.set('data', data.data);
2982
+ }
2983
+ if (data.content) {
2984
+ comment.set('content', data.content);
2985
+ }
2986
+ updatedThread = thread.toJSON();
2987
+ });
2988
+ return updatedThread;
2973
2989
  }
2974
2990
  deleteComment(threadId, commentId) {
2975
2991
  const thread = this.getYThread(threadId);
@@ -2983,7 +2999,13 @@ class TiptapCollabProvider extends HocuspocusProvider {
2983
2999
  }
2984
3000
  commentIndex += 1;
2985
3001
  }
2986
- if (commentIndex >= 0) {
3002
+ // if the first comment of a thread is deleted we also
3003
+ // delete the thread itself as the source comment is gone
3004
+ if (commentIndex === 0) {
3005
+ this.deleteThread(threadId);
3006
+ return;
3007
+ }
3008
+ if (commentIndex > 0) {
2987
3009
  thread.get('comments').delete(commentIndex);
2988
3010
  }
2989
3011
  return thread.toJSON();