@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.
@@ -2774,16 +2774,40 @@ class HocuspocusProvider extends EventEmitter {
2774
2774
  }
2775
2775
  }
2776
2776
 
2777
+ /* eslint-env browser */
2778
+ const getRandomValues = crypto.getRandomValues.bind(crypto);
2779
+
2780
+ /**
2781
+ * Isomorphic module for true random numbers / buffers / uuids.
2782
+ *
2783
+ * Attention: falls back to Math.random if the browser does not support crypto.
2784
+ *
2785
+ * @module random
2786
+ */
2787
+
2788
+ const uint32 = () => getRandomValues(new Uint32Array(1))[0];
2789
+
2790
+ // @ts-ignore
2791
+ const uuidv4Template = [1e7] + -1e3 + -4e3 + -8e3 + -1e11;
2792
+
2793
+ /**
2794
+ * @return {string}
2795
+ */
2796
+ const uuidv4 = () => uuidv4Template.replace(/[018]/g, /** @param {number} c */ c =>
2797
+ (c ^ uint32() & 15 >> c / 4).toString(16)
2798
+ );
2799
+
2777
2800
  class TiptapCollabProviderWebsocket extends HocuspocusProviderWebsocket {
2778
2801
  constructor(configuration) {
2779
- super({ ...configuration, url: `wss://${configuration.appId}.collab.tiptap.cloud` });
2802
+ var _a;
2803
+ super({ ...configuration, url: (_a = configuration.baseUrl) !== null && _a !== void 0 ? _a : `wss://${configuration.appId}.collab.tiptap.cloud` });
2780
2804
  }
2781
2805
  }
2782
2806
 
2783
2807
  class TiptapCollabProvider extends HocuspocusProvider {
2784
2808
  constructor(configuration) {
2785
2809
  if (!configuration.websocketProvider) {
2786
- configuration.websocketProvider = new TiptapCollabProviderWebsocket({ appId: configuration.appId });
2810
+ configuration.websocketProvider = new TiptapCollabProviderWebsocket({ appId: configuration.appId, baseUrl: configuration.baseUrl });
2787
2811
  }
2788
2812
  if (!configuration.token) {
2789
2813
  configuration.token = 'notoken'; // need to send a token anyway (which will be ignored)
@@ -2832,6 +2856,132 @@ class TiptapCollabProvider extends HocuspocusProvider {
2832
2856
  disableAutoVersioning() {
2833
2857
  return this.configuration.document.getMap(`${this.tiptapCollabConfigurationPrefix}config`).set('autoVersioning', 0);
2834
2858
  }
2859
+ getYThreads() {
2860
+ return this.configuration.document.getArray(`${this.tiptapCollabConfigurationPrefix}threads`);
2861
+ }
2862
+ getThreads() {
2863
+ return this.getYThreads().toJSON();
2864
+ }
2865
+ getThreadIndex(id) {
2866
+ let index = null;
2867
+ let i = 0;
2868
+ // eslint-disable-next-line no-restricted-syntax
2869
+ for (const thread of this.getThreads()) {
2870
+ if (thread.id === id) {
2871
+ index = i;
2872
+ break;
2873
+ }
2874
+ i += 1;
2875
+ }
2876
+ return index;
2877
+ }
2878
+ getThread(id) {
2879
+ const index = this.getThreadIndex(id);
2880
+ if (index === null) {
2881
+ return null;
2882
+ }
2883
+ return this.getYThreads().get(index).toJSON();
2884
+ }
2885
+ getYThread(id) {
2886
+ const index = this.getThreadIndex(id);
2887
+ if (index === null) {
2888
+ return null;
2889
+ }
2890
+ return this.getYThreads().get(index);
2891
+ }
2892
+ createThread(data) {
2893
+ const thread = new Y.Map();
2894
+ thread.set('id', uuidv4());
2895
+ thread.set('createdAt', (new Date()).toISOString());
2896
+ thread.set('comments', new Y.Array());
2897
+ this.getYThreads().push([thread]);
2898
+ return this.updateThread(String(thread.get('id')), data);
2899
+ }
2900
+ updateThread(id, data) {
2901
+ const thread = this.getYThread(id);
2902
+ if (thread === null) {
2903
+ return null;
2904
+ }
2905
+ thread.set('updatedAt', (new Date()).toISOString());
2906
+ thread.set('data', data.data);
2907
+ return thread.toJSON();
2908
+ }
2909
+ deleteThread(id) {
2910
+ const index = this.getThreadIndex(id);
2911
+ if (index === null) {
2912
+ return;
2913
+ }
2914
+ this.getYThreads().delete(index, 1);
2915
+ }
2916
+ getThreadComments(threadId) {
2917
+ var _a, _b;
2918
+ const index = this.getThreadIndex(threadId);
2919
+ if (index === null) {
2920
+ return null;
2921
+ }
2922
+ return (_b = (_a = this.getThread(threadId)) === null || _a === void 0 ? void 0 : _a.comments) !== null && _b !== void 0 ? _b : [];
2923
+ }
2924
+ getThreadComment(threadId, commentId) {
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.find(comment => comment.id === commentId)) !== null && _b !== void 0 ? _b : null;
2931
+ }
2932
+ addComment(threadId, data) {
2933
+ const thread = this.getYThread(threadId);
2934
+ if (thread === null)
2935
+ return null;
2936
+ const commentMap = new Y.Map();
2937
+ commentMap.set('id', uuidv4());
2938
+ commentMap.set('createdAt', (new Date()).toISOString());
2939
+ thread.get('comments').push([commentMap]);
2940
+ this.updateComment(threadId, String(commentMap.get('id')), data);
2941
+ return thread.toJSON();
2942
+ }
2943
+ updateComment(threadId, commentId, data) {
2944
+ const thread = this.getYThread(threadId);
2945
+ if (thread === null)
2946
+ return null;
2947
+ let comment = null;
2948
+ // eslint-disable-next-line no-restricted-syntax
2949
+ for (const c of thread.get('comments')) {
2950
+ if (c.get('id') === commentId) {
2951
+ comment = c;
2952
+ break;
2953
+ }
2954
+ }
2955
+ if (comment === null)
2956
+ return null;
2957
+ comment.set('updatedAt', (new Date()).toISOString());
2958
+ comment.set('data', data.data);
2959
+ comment.set('content', data.content);
2960
+ return thread.toJSON();
2961
+ }
2962
+ deleteComment(threadId, commentId) {
2963
+ const thread = this.getYThread(threadId);
2964
+ if (thread === null)
2965
+ return null;
2966
+ let commentIndex = 0;
2967
+ // eslint-disable-next-line no-restricted-syntax
2968
+ for (const c of thread.get('comments')) {
2969
+ if (c.get('id') === commentId) {
2970
+ break;
2971
+ }
2972
+ commentIndex += 1;
2973
+ }
2974
+ if (commentIndex >= 0) {
2975
+ thread.get('comments').delete(commentIndex);
2976
+ }
2977
+ return thread.toJSON();
2978
+ }
2979
+ watchThreads(callback) {
2980
+ this.getYThreads().observeDeep(callback);
2981
+ }
2982
+ unwatchThreads(callback) {
2983
+ this.getYThreads().unobserveDeep(callback);
2984
+ }
2835
2985
  }
2836
2986
 
2837
2987
  export { AwarenessError, HocuspocusProvider, HocuspocusProviderWebsocket, MessageType, TiptapCollabProvider, TiptapCollabProviderWebsocket, WebSocketStatus };