File size: 3,055 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { useCallback } from 'react';
import type { BaseElement, Editor } from 'slate';
import { Transforms } from 'slate';

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

import { getCurrentNodeWithPlugin } from './useCurrentNodeWithPlugin';
import { removePlugin } from './useRemovePlugin';
import getCurrentData from '../utils/getCurrentData';
import type { Data } from '../types';

export const addPlugin = <T extends Data>(
  editor: Editor,
  plugin: SlatePluginDefinition<T>,
  props?: { data?: T; text?: string | null }
) => {
  const { data: passedData, text } = props || {};
  const currentNodeEntry = getCurrentNodeWithPlugin(editor, plugin);
  if (text) {
    const withExtraSpace =
      plugin.pluginType === 'component' &&
      plugin.object === 'inline' &&
      plugin.addExtraSpace;
    const textToInsert = withExtraSpace ? text + ' ' : text;
    editor.insertText(textToInsert);
    if (editor.selection) {
      Transforms.select(editor, {
        anchor: editor.selection.anchor,
        focus: {
          ...editor.selection.focus,
          offset: editor.selection.focus.offset - textToInsert.length,
        },
      });
    }
  }

  const data =
    passedData || (plugin.getInitialData ? plugin.getInitialData() : null);

  if (currentNodeEntry) {
    Transforms.select(editor, currentNodeEntry[1]);
    removePlugin(editor, plugin);
  }
  // add new
  if (plugin.customAdd) {
    plugin.customAdd(editor);
  } else if (plugin.pluginType === 'component') {
    if (plugin.object === 'mark') {
      editor.addMark(plugin.type, data || true);
    } else if (plugin.isVoid) {
      Transforms.insertNodes(editor, {
        type: plugin.type,
        data,
        children: [{ text: '' }],
      });
    } else {
      if (plugin.object === 'block' && plugin.replaceWithDefaultOnRemove) {
        Transforms.setNodes(editor, { type: plugin.type, data });
      } else {
        Transforms.wrapNodes(
          editor,
          {
            type: plugin.type,
            children: [],
            data,
          },
          { split: true }
        );
        // workaround for inline problems in slate
        if (
          plugin.object === 'inline' &&
          plugin.addExtraSpace &&
          !text &&
          editor.selection
        ) {
          const focus = { ...editor.selection.focus };
          Transforms.insertText(editor, ' ', {
            at: editor.selection.focus,
          });
          Transforms.select(editor, focus);
        }
      }
    }
  } else if (plugin.pluginType === 'data') {
    const existingData = getCurrentData(editor) ?? {};
    Transforms.setNodes(editor, {
      data: {
        ...existingData,
        ...((data ?? {}) as Record<string, unknown>),
      },
    });
  }
};

export default <T extends Data>(plugin: SlatePluginDefinition<T>) => {
  const editor = useSlate();
  return useCallback(
    (props?: { data?: T; text?: string | null }) =>
      addPlugin(editor, plugin, props),
    []
  );
};