File size: 2,133 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import type { DataTType } from '@react-page/editor';
import { useCallback } from 'react';
import type { Editor } from 'slate';
import { Transforms } from 'slate';

import { useSlate } from 'slate-react';
import type { SlatePluginDefinition } from '../types/slatePluginDefinitions';
import getCurrentData from '../utils/getCurrentData';

export const removePlugin = <T extends DataTType>(
  editor: Editor,
  plugin: SlatePluginDefinition<T>
) => {
  if (plugin.customRemove) {
    plugin.customRemove(editor);
  } else if (plugin.pluginType === 'component') {
    if (plugin.object === 'mark') {
      editor.removeMark(plugin.type);
    } else if (plugin.object === 'inline') {
      if (plugin.isVoid) {
        Transforms.removeNodes(editor, {
          match: (elem) => elem.type === plugin.type,
        });
      } else {
        Transforms.unwrapNodes(editor, {
          match: (elem) => elem.type === plugin.type,
        });
      }
      // Transforms.setNodes(editor, { type: null });
    } else if (plugin.object === 'block') {
      if (plugin.isVoid) {
        Transforms.removeNodes(editor, {
          match: (elem) => elem.type === plugin.type,
        });
      } else if (plugin.replaceWithDefaultOnRemove) {
        Transforms.setNodes(editor, {
          type: null,
        });
      } else {
        Transforms.unwrapNodes(editor, {
          match: (elem) => elem.type === plugin.type,
          split: true,
        });
      }
    }
  } else if (plugin.pluginType === 'data') {
    if (!plugin.properties) {
      // can't be removed
    } else {
      const existingData = getCurrentData(editor);

      const dataWithout = Object.keys(existingData).reduce((acc, key) => {
        if (plugin.properties?.includes(key)) {
          return acc;
        }
        return {
          ...acc,
          [key]: existingData[key],
        };
      }, {});
      Transforms.setNodes(editor, {
        data: dataWithout,
      });
    }
  }
};
export default <T extends DataTType>(plugin: SlatePluginDefinition<T>) => {
  const editor = useSlate();
  return useCallback(() => removePlugin(editor, plugin), []);
};