@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.
@@ -1775,9 +1775,10 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1775
1775
  this.receivedOnStatusPayload = data;
1776
1776
  }
1777
1777
  attach(provider) {
1778
+ let connectPromise;
1778
1779
  this.configuration.providerMap.set(provider.configuration.name, provider);
1779
1780
  if (this.status === exports.WebSocketStatus.Disconnected && this.shouldConnect) {
1780
- this.connect();
1781
+ connectPromise = this.connect();
1781
1782
  }
1782
1783
  if (this.receivedOnOpenPayload) {
1783
1784
  provider.onOpen(this.receivedOnOpenPayload);
@@ -1785,6 +1786,7 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1785
1786
  if (this.receivedOnStatusPayload) {
1786
1787
  provider.onStatus(this.receivedOnStatusPayload);
1787
1788
  }
1789
+ return connectPromise;
1788
1790
  }
1789
1791
  detach(provider) {
1790
1792
  this.configuration.providerMap.delete(provider.configuration.name);
@@ -2618,7 +2620,8 @@ class HocuspocusProvider extends EventEmitter {
2618
2620
  if (this.configuration.broadcast) {
2619
2621
  this.subscribeToBroadcastChannel();
2620
2622
  }
2621
- return this.configuration.websocketProvider.connect();
2623
+ this.configuration.websocketProvider.shouldConnect = true;
2624
+ return this.configuration.websocketProvider.attach(this);
2622
2625
  }
2623
2626
  disconnect() {
2624
2627
  this.disconnectBroadcastChannel();
@@ -2798,6 +2801,29 @@ class HocuspocusProvider extends EventEmitter {
2798
2801
  }
2799
2802
  }
2800
2803
 
2804
+ /* eslint-env browser */
2805
+ const getRandomValues = crypto.getRandomValues.bind(crypto);
2806
+
2807
+ /**
2808
+ * Isomorphic module for true random numbers / buffers / uuids.
2809
+ *
2810
+ * Attention: falls back to Math.random if the browser does not support crypto.
2811
+ *
2812
+ * @module random
2813
+ */
2814
+
2815
+ const uint32 = () => getRandomValues(new Uint32Array(1))[0];
2816
+
2817
+ // @ts-ignore
2818
+ const uuidv4Template = [1e7] + -1e3 + -4e3 + -8e3 + -1e11;
2819
+
2820
+ /**
2821
+ * @return {string}
2822
+ */
2823
+ const uuidv4 = () => uuidv4Template.replace(/[018]/g, /** @param {number} c */ c =>
2824
+ (c ^ uint32() & 15 >> c / 4).toString(16)
2825
+ );
2826
+
2801
2827
  class TiptapCollabProviderWebsocket extends HocuspocusProviderWebsocket {
2802
2828
  constructor(configuration) {
2803
2829
  var _a;
@@ -2857,6 +2883,141 @@ class TiptapCollabProvider extends HocuspocusProvider {
2857
2883
  disableAutoVersioning() {
2858
2884
  return this.configuration.document.getMap(`${this.tiptapCollabConfigurationPrefix}config`).set('autoVersioning', 0);
2859
2885
  }
2886
+ getYThreads() {
2887
+ return this.configuration.document.getArray(`${this.tiptapCollabConfigurationPrefix}threads`);
2888
+ }
2889
+ getThreads() {
2890
+ return this.getYThreads().toJSON();
2891
+ }
2892
+ getThreadIndex(id) {
2893
+ let index = null;
2894
+ let i = 0;
2895
+ // eslint-disable-next-line no-restricted-syntax
2896
+ for (const thread of this.getThreads()) {
2897
+ if (thread.id === id) {
2898
+ index = i;
2899
+ break;
2900
+ }
2901
+ i += 1;
2902
+ }
2903
+ return index;
2904
+ }
2905
+ getThread(id) {
2906
+ const index = this.getThreadIndex(id);
2907
+ if (index === null) {
2908
+ return null;
2909
+ }
2910
+ return this.getYThreads().get(index).toJSON();
2911
+ }
2912
+ getYThread(id) {
2913
+ const index = this.getThreadIndex(id);
2914
+ if (index === null) {
2915
+ return null;
2916
+ }
2917
+ return this.getYThreads().get(index);
2918
+ }
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);
2926
+ }
2927
+ 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();
2940
+ }
2941
+ deleteThread(id) {
2942
+ const index = this.getThreadIndex(id);
2943
+ if (index === null) {
2944
+ return;
2945
+ }
2946
+ this.getYThreads().delete(index, 1);
2947
+ }
2948
+ getThreadComments(threadId) {
2949
+ var _a, _b;
2950
+ const index = this.getThreadIndex(threadId);
2951
+ if (index === null) {
2952
+ return null;
2953
+ }
2954
+ return (_b = (_a = this.getThread(threadId)) === null || _a === void 0 ? void 0 : _a.comments) !== null && _b !== void 0 ? _b : [];
2955
+ }
2956
+ getThreadComment(threadId, commentId) {
2957
+ var _a, _b;
2958
+ const index = this.getThreadIndex(threadId);
2959
+ if (index === null) {
2960
+ return null;
2961
+ }
2962
+ 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
+ }
2964
+ 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();
2974
+ }
2975
+ 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;
2985
+ }
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();
2997
+ }
2998
+ deleteComment(threadId, commentId) {
2999
+ const thread = this.getYThread(threadId);
3000
+ if (thread === null)
3001
+ return null;
3002
+ let commentIndex = 0;
3003
+ // eslint-disable-next-line no-restricted-syntax
3004
+ for (const c of thread.get('comments')) {
3005
+ if (c.get('id') === commentId) {
3006
+ break;
3007
+ }
3008
+ commentIndex += 1;
3009
+ }
3010
+ if (commentIndex >= 0) {
3011
+ thread.get('comments').delete(commentIndex);
3012
+ }
3013
+ return thread.toJSON();
3014
+ }
3015
+ watchThreads(callback) {
3016
+ this.getYThreads().observeDeep(callback);
3017
+ }
3018
+ unwatchThreads(callback) {
3019
+ this.getYThreads().unobserveDeep(callback);
3020
+ }
2860
3021
  }
2861
3022
 
2862
3023
  exports.AwarenessError = AwarenessError;