@hocuspocus/provider 2.9.1-rc.0 → 2.10.0

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.
@@ -1751,9 +1751,10 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1751
1751
  this.receivedOnStatusPayload = data;
1752
1752
  }
1753
1753
  attach(provider) {
1754
+ let connectPromise;
1754
1755
  this.configuration.providerMap.set(provider.configuration.name, provider);
1755
1756
  if (this.status === WebSocketStatus.Disconnected && this.shouldConnect) {
1756
- this.connect();
1757
+ connectPromise = this.connect();
1757
1758
  }
1758
1759
  if (this.receivedOnOpenPayload) {
1759
1760
  provider.onOpen(this.receivedOnOpenPayload);
@@ -1761,6 +1762,7 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1761
1762
  if (this.receivedOnStatusPayload) {
1762
1763
  provider.onStatus(this.receivedOnStatusPayload);
1763
1764
  }
1765
+ return connectPromise;
1764
1766
  }
1765
1767
  detach(provider) {
1766
1768
  this.configuration.providerMap.delete(provider.configuration.name);
@@ -2594,7 +2596,8 @@ class HocuspocusProvider extends EventEmitter {
2594
2596
  if (this.configuration.broadcast) {
2595
2597
  this.subscribeToBroadcastChannel();
2596
2598
  }
2597
- return this.configuration.websocketProvider.connect();
2599
+ this.configuration.websocketProvider.shouldConnect = true;
2600
+ return this.configuration.websocketProvider.attach(this);
2598
2601
  }
2599
2602
  disconnect() {
2600
2603
  this.disconnectBroadcastChannel();
@@ -2774,6 +2777,29 @@ class HocuspocusProvider extends EventEmitter {
2774
2777
  }
2775
2778
  }
2776
2779
 
2780
+ /* eslint-env browser */
2781
+ const getRandomValues = crypto.getRandomValues.bind(crypto);
2782
+
2783
+ /**
2784
+ * Isomorphic module for true random numbers / buffers / uuids.
2785
+ *
2786
+ * Attention: falls back to Math.random if the browser does not support crypto.
2787
+ *
2788
+ * @module random
2789
+ */
2790
+
2791
+ const uint32 = () => getRandomValues(new Uint32Array(1))[0];
2792
+
2793
+ // @ts-ignore
2794
+ const uuidv4Template = [1e7] + -1e3 + -4e3 + -8e3 + -1e11;
2795
+
2796
+ /**
2797
+ * @return {string}
2798
+ */
2799
+ const uuidv4 = () => uuidv4Template.replace(/[018]/g, /** @param {number} c */ c =>
2800
+ (c ^ uint32() & 15 >> c / 4).toString(16)
2801
+ );
2802
+
2777
2803
  class TiptapCollabProviderWebsocket extends HocuspocusProviderWebsocket {
2778
2804
  constructor(configuration) {
2779
2805
  var _a;
@@ -2833,6 +2859,141 @@ class TiptapCollabProvider extends HocuspocusProvider {
2833
2859
  disableAutoVersioning() {
2834
2860
  return this.configuration.document.getMap(`${this.tiptapCollabConfigurationPrefix}config`).set('autoVersioning', 0);
2835
2861
  }
2862
+ getYThreads() {
2863
+ return this.configuration.document.getArray(`${this.tiptapCollabConfigurationPrefix}threads`);
2864
+ }
2865
+ getThreads() {
2866
+ return this.getYThreads().toJSON();
2867
+ }
2868
+ getThreadIndex(id) {
2869
+ let index = null;
2870
+ let i = 0;
2871
+ // eslint-disable-next-line no-restricted-syntax
2872
+ for (const thread of this.getThreads()) {
2873
+ if (thread.id === id) {
2874
+ index = i;
2875
+ break;
2876
+ }
2877
+ i += 1;
2878
+ }
2879
+ return index;
2880
+ }
2881
+ getThread(id) {
2882
+ const index = this.getThreadIndex(id);
2883
+ if (index === null) {
2884
+ return null;
2885
+ }
2886
+ return this.getYThreads().get(index).toJSON();
2887
+ }
2888
+ getYThread(id) {
2889
+ const index = this.getThreadIndex(id);
2890
+ if (index === null) {
2891
+ return null;
2892
+ }
2893
+ return this.getYThreads().get(index);
2894
+ }
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);
2902
+ }
2903
+ 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();
2916
+ }
2917
+ deleteThread(id) {
2918
+ const index = this.getThreadIndex(id);
2919
+ if (index === null) {
2920
+ return;
2921
+ }
2922
+ this.getYThreads().delete(index, 1);
2923
+ }
2924
+ getThreadComments(threadId) {
2925
+ var _a, _b;
2926
+ const index = this.getThreadIndex(threadId);
2927
+ if (index === null) {
2928
+ return null;
2929
+ }
2930
+ return (_b = (_a = this.getThread(threadId)) === null || _a === void 0 ? void 0 : _a.comments) !== null && _b !== void 0 ? _b : [];
2931
+ }
2932
+ getThreadComment(threadId, commentId) {
2933
+ var _a, _b;
2934
+ const index = this.getThreadIndex(threadId);
2935
+ if (index === null) {
2936
+ return null;
2937
+ }
2938
+ 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
+ }
2940
+ 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();
2950
+ }
2951
+ 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;
2961
+ }
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();
2973
+ }
2974
+ deleteComment(threadId, commentId) {
2975
+ const thread = this.getYThread(threadId);
2976
+ if (thread === null)
2977
+ return null;
2978
+ let commentIndex = 0;
2979
+ // eslint-disable-next-line no-restricted-syntax
2980
+ for (const c of thread.get('comments')) {
2981
+ if (c.get('id') === commentId) {
2982
+ break;
2983
+ }
2984
+ commentIndex += 1;
2985
+ }
2986
+ if (commentIndex >= 0) {
2987
+ thread.get('comments').delete(commentIndex);
2988
+ }
2989
+ return thread.toJSON();
2990
+ }
2991
+ watchThreads(callback) {
2992
+ this.getYThreads().observeDeep(callback);
2993
+ }
2994
+ unwatchThreads(callback) {
2995
+ this.getYThreads().unobserveDeep(callback);
2996
+ }
2836
2997
  }
2837
2998
 
2838
2999
  export { AwarenessError, HocuspocusProvider, HocuspocusProviderWebsocket, MessageType, TiptapCollabProvider, TiptapCollabProviderWebsocket, WebSocketStatus };