@hocuspocus/provider 2.9.0 → 2.9.2-rc.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.
@@ -2798,16 +2798,40 @@ class HocuspocusProvider extends EventEmitter {
2798
2798
  }
2799
2799
  }
2800
2800
 
2801
+ /* eslint-env browser */
2802
+ const getRandomValues = crypto.getRandomValues.bind(crypto);
2803
+
2804
+ /**
2805
+ * Isomorphic module for true random numbers / buffers / uuids.
2806
+ *
2807
+ * Attention: falls back to Math.random if the browser does not support crypto.
2808
+ *
2809
+ * @module random
2810
+ */
2811
+
2812
+ const uint32 = () => getRandomValues(new Uint32Array(1))[0];
2813
+
2814
+ // @ts-ignore
2815
+ const uuidv4Template = [1e7] + -1e3 + -4e3 + -8e3 + -1e11;
2816
+
2817
+ /**
2818
+ * @return {string}
2819
+ */
2820
+ const uuidv4 = () => uuidv4Template.replace(/[018]/g, /** @param {number} c */ c =>
2821
+ (c ^ uint32() & 15 >> c / 4).toString(16)
2822
+ );
2823
+
2801
2824
  class TiptapCollabProviderWebsocket extends HocuspocusProviderWebsocket {
2802
2825
  constructor(configuration) {
2803
- super({ ...configuration, url: `wss://${configuration.appId}.collab.tiptap.cloud` });
2826
+ var _a;
2827
+ super({ ...configuration, url: (_a = configuration.baseUrl) !== null && _a !== void 0 ? _a : `wss://${configuration.appId}.collab.tiptap.cloud` });
2804
2828
  }
2805
2829
  }
2806
2830
 
2807
2831
  class TiptapCollabProvider extends HocuspocusProvider {
2808
2832
  constructor(configuration) {
2809
2833
  if (!configuration.websocketProvider) {
2810
- configuration.websocketProvider = new TiptapCollabProviderWebsocket({ appId: configuration.appId });
2834
+ configuration.websocketProvider = new TiptapCollabProviderWebsocket({ appId: configuration.appId, baseUrl: configuration.baseUrl });
2811
2835
  }
2812
2836
  if (!configuration.token) {
2813
2837
  configuration.token = 'notoken'; // need to send a token anyway (which will be ignored)
@@ -2856,6 +2880,132 @@ class TiptapCollabProvider extends HocuspocusProvider {
2856
2880
  disableAutoVersioning() {
2857
2881
  return this.configuration.document.getMap(`${this.tiptapCollabConfigurationPrefix}config`).set('autoVersioning', 0);
2858
2882
  }
2883
+ getYThreads() {
2884
+ return this.configuration.document.getArray(`${this.tiptapCollabConfigurationPrefix}threads`);
2885
+ }
2886
+ getThreads() {
2887
+ return this.getYThreads().toJSON();
2888
+ }
2889
+ getThreadIndex(id) {
2890
+ let index = null;
2891
+ let i = 0;
2892
+ // eslint-disable-next-line no-restricted-syntax
2893
+ for (const thread of this.getThreads()) {
2894
+ if (thread.id === id) {
2895
+ index = i;
2896
+ break;
2897
+ }
2898
+ i += 1;
2899
+ }
2900
+ return index;
2901
+ }
2902
+ getThread(id) {
2903
+ const index = this.getThreadIndex(id);
2904
+ if (index === null) {
2905
+ return null;
2906
+ }
2907
+ return this.getYThreads().get(index).toJSON();
2908
+ }
2909
+ getYThread(id) {
2910
+ const index = this.getThreadIndex(id);
2911
+ if (index === null) {
2912
+ return null;
2913
+ }
2914
+ return this.getYThreads().get(index);
2915
+ }
2916
+ createThread(data) {
2917
+ const thread = new Y__namespace.Map();
2918
+ thread.set('id', uuidv4());
2919
+ thread.set('createdAt', (new Date()).toISOString());
2920
+ thread.set('comments', new Y__namespace.Array());
2921
+ this.getYThreads().push([thread]);
2922
+ return this.updateThread(String(thread.get('id')), data);
2923
+ }
2924
+ updateThread(id, data) {
2925
+ const thread = this.getYThread(id);
2926
+ if (thread === null) {
2927
+ return null;
2928
+ }
2929
+ thread.set('updatedAt', (new Date()).toISOString());
2930
+ thread.set('data', data.data);
2931
+ return thread.toJSON();
2932
+ }
2933
+ deleteThread(id) {
2934
+ const index = this.getThreadIndex(id);
2935
+ if (index === null) {
2936
+ return;
2937
+ }
2938
+ this.getYThreads().delete(index, 1);
2939
+ }
2940
+ getThreadComments(threadId) {
2941
+ var _a, _b;
2942
+ const index = this.getThreadIndex(threadId);
2943
+ if (index === null) {
2944
+ return null;
2945
+ }
2946
+ return (_b = (_a = this.getThread(threadId)) === null || _a === void 0 ? void 0 : _a.comments) !== null && _b !== void 0 ? _b : [];
2947
+ }
2948
+ getThreadComment(threadId, commentId) {
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.find(comment => comment.id === commentId)) !== null && _b !== void 0 ? _b : null;
2955
+ }
2956
+ addComment(threadId, data) {
2957
+ const thread = this.getYThread(threadId);
2958
+ if (thread === null)
2959
+ return null;
2960
+ const commentMap = new Y__namespace.Map();
2961
+ commentMap.set('id', uuidv4());
2962
+ commentMap.set('createdAt', (new Date()).toISOString());
2963
+ thread.get('comments').push([commentMap]);
2964
+ this.updateComment(threadId, String(commentMap.get('id')), data);
2965
+ return thread.toJSON();
2966
+ }
2967
+ updateComment(threadId, commentId, data) {
2968
+ const thread = this.getYThread(threadId);
2969
+ if (thread === null)
2970
+ return null;
2971
+ let comment = null;
2972
+ // eslint-disable-next-line no-restricted-syntax
2973
+ for (const c of thread.get('comments')) {
2974
+ if (c.get('id') === commentId) {
2975
+ comment = c;
2976
+ break;
2977
+ }
2978
+ }
2979
+ if (comment === null)
2980
+ return null;
2981
+ comment.set('updatedAt', (new Date()).toISOString());
2982
+ comment.set('data', data.data);
2983
+ comment.set('content', data.content);
2984
+ return thread.toJSON();
2985
+ }
2986
+ deleteComment(threadId, commentId) {
2987
+ const thread = this.getYThread(threadId);
2988
+ if (thread === null)
2989
+ return null;
2990
+ let commentIndex = 0;
2991
+ // eslint-disable-next-line no-restricted-syntax
2992
+ for (const c of thread.get('comments')) {
2993
+ if (c.get('id') === commentId) {
2994
+ break;
2995
+ }
2996
+ commentIndex += 1;
2997
+ }
2998
+ if (commentIndex >= 0) {
2999
+ thread.get('comments').delete(commentIndex);
3000
+ }
3001
+ return thread.toJSON();
3002
+ }
3003
+ watchThreads(callback) {
3004
+ this.getYThreads().observeDeep(callback);
3005
+ }
3006
+ unwatchThreads(callback) {
3007
+ this.getYThreads().unobserveDeep(callback);
3008
+ }
2859
3009
  }
2860
3010
 
2861
3011
  exports.AwarenessError = AwarenessError;