@hocuspocus/provider 2.14.0 → 2.15.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.
@@ -2824,6 +2824,13 @@ const defaultDeleteCommentOptions = {
2824
2824
  deleteContent: false,
2825
2825
  deleteThread: false,
2826
2826
  };
2827
+ const defaultGetThreadsOptions = {
2828
+ types: ['unarchived'],
2829
+ };
2830
+ const defaultDeleteThreadOptions = {
2831
+ deleteComments: false,
2832
+ force: false,
2833
+ };
2827
2834
  class TiptapCollabProvider extends HocuspocusProvider {
2828
2835
  constructor(configuration) {
2829
2836
  if (!configuration.websocketProvider) {
@@ -2889,10 +2896,24 @@ class TiptapCollabProvider extends HocuspocusProvider {
2889
2896
  }
2890
2897
  /**
2891
2898
  * Finds all threads in the document and returns them as JSON objects
2899
+ * @options Options to control the output of the threads (e.g. include deleted threads)
2892
2900
  * @returns An array of threads as JSON objects
2893
2901
  */
2894
- getThreads() {
2895
- return this.getYThreads().toJSON();
2902
+ getThreads(options) {
2903
+ const { types } = { ...defaultGetThreadsOptions, ...options };
2904
+ const threads = this.getYThreads().toJSON();
2905
+ if ((types === null || types === void 0 ? void 0 : types.includes('archived')) && (types === null || types === void 0 ? void 0 : types.includes('unarchived'))) {
2906
+ return threads;
2907
+ }
2908
+ return threads.filter(currentThead => {
2909
+ if ((types === null || types === void 0 ? void 0 : types.includes('archived')) && currentThead.deletedAt) {
2910
+ return true;
2911
+ }
2912
+ if ((types === null || types === void 0 ? void 0 : types.includes('unarchived')) && !currentThead.deletedAt) {
2913
+ return true;
2914
+ }
2915
+ return false;
2916
+ });
2896
2917
  }
2897
2918
  /**
2898
2919
  * Find the index of a thread by its id
@@ -2903,7 +2924,7 @@ class TiptapCollabProvider extends HocuspocusProvider {
2903
2924
  let index = null;
2904
2925
  let i = 0;
2905
2926
  // eslint-disable-next-line no-restricted-syntax
2906
- for (const thread of this.getThreads()) {
2927
+ for (const thread of this.getThreads({ types: ['archived', 'unarchived'] })) {
2907
2928
  if (thread.id === id) {
2908
2929
  index = i;
2909
2930
  break;
@@ -2949,6 +2970,7 @@ class TiptapCollabProvider extends HocuspocusProvider {
2949
2970
  thread.set('createdAt', (new Date()).toISOString());
2950
2971
  thread.set('comments', new Y.Array());
2951
2972
  thread.set('deletedComments', new Y.Array());
2973
+ thread.set('deletedAt', null);
2952
2974
  this.getYThreads().push([thread]);
2953
2975
  createdThread = this.updateThread(String(thread.get('id')), data);
2954
2976
  });
@@ -2979,16 +3001,45 @@ class TiptapCollabProvider extends HocuspocusProvider {
2979
3001
  return updatedThread;
2980
3002
  }
2981
3003
  /**
2982
- * Delete a specific thread and all its comments
3004
+ * Handle the deletion of a thread. By default, the thread and it's comments are not deleted, but marked as deleted
3005
+ * via the `deletedAt` property. Forceful deletion can be enabled by setting the `force` option to `true`.
3006
+ *
3007
+ * If you only want to delete the comments of a thread, you can set the `deleteComments` option to `true`.
2983
3008
  * @param id The thread id
2984
- * @returns void
3009
+ * @param options A set of options that control how the thread is deleted
3010
+ * @returns The deleted thread or null if the thread is not found
2985
3011
  */
2986
- deleteThread(id) {
3012
+ deleteThread(id, options) {
3013
+ const { deleteComments, force } = { ...defaultDeleteThreadOptions, ...options };
2987
3014
  const index = this.getThreadIndex(id);
2988
3015
  if (index === null) {
3016
+ return null;
3017
+ }
3018
+ if (force) {
3019
+ this.getYThreads().delete(index, 1);
2989
3020
  return;
2990
3021
  }
2991
- this.getYThreads().delete(index, 1);
3022
+ const thread = this.getYThreads().get(index);
3023
+ thread.set('deletedAt', (new Date()).toISOString());
3024
+ if (deleteComments) {
3025
+ thread.set('comments', new Y.Array());
3026
+ thread.set('deletedComments', new Y.Array());
3027
+ }
3028
+ return thread.toJSON();
3029
+ }
3030
+ /**
3031
+ * Tries to restore a deleted thread
3032
+ * @param id The thread id
3033
+ * @returns The restored thread or null if the thread is not found
3034
+ */
3035
+ restoreThread(id) {
3036
+ const index = this.getThreadIndex(id);
3037
+ if (index === null) {
3038
+ return null;
3039
+ }
3040
+ const thread = this.getYThreads().get(index);
3041
+ thread.set('deletedAt', null);
3042
+ return thread.toJSON();
2992
3043
  }
2993
3044
  /**
2994
3045
  * Returns comments from a thread, either deleted or not