File size: 2,237 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
import type { SlatePlugin } from '../types/SlatePlugin';

type Definition = {
  iconIncrease: JSX.Element;
  iconDecrease: JSX.Element;
  listItemType: string;
  labelIncrease?: string;
  labelDecrease?: string;
};

const ceateSlatePlugin = (def: Definition): SlatePlugin[] => {
  return [
    {
      pluginType: 'custom',
      addToolbarButton: true,
      addHoverButton: false,
      icon: def.iconIncrease,
      label: def.labelIncrease,
      customAdd: async (editor) => {
        const { increaseListIndention } = await import('./utils/listUtils');
        increaseListIndention(editor, {
          listItemType: def.listItemType,
        });
      },
      customRemove: async (editor) => {
        const { decreaseListIndention } = await import('./utils/listUtils');
        decreaseListIndention(editor, {
          listItemType: def.listItemType,
        });
      },
      isDisabled: async (editor) => {
        const { getPreviousListItem } = await import('./utils/listUtils');
        const previous = getPreviousListItem(editor, def.listItemType);
        return !previous;
      },
    },
    {
      pluginType: 'custom',
      addToolbarButton: true,
      addHoverButton: false,
      icon: def.iconDecrease,
      label: def.labelDecrease,
      customAdd: async (editor) => {
        const { decreaseListIndention } = await import('./utils/listUtils');
        decreaseListIndention(editor, {
          listItemType: def.listItemType,
        });
      },
      customRemove: async (editor) => {
        const { increaseListIndention } = await import('./utils/listUtils');
        increaseListIndention(editor, {
          listItemType: def.listItemType,
        });
      },
      isDisabled: async (editor) => {
        const { getActiveListType } = await import('./utils/listUtils');
        return !getActiveListType(editor);
      },
    },
  ];
};

function createListIndentionPlugin(def: Definition) {
  const customizablePlugin = function (
    customize: (def2: Definition) => Definition
  ) {
    return createListIndentionPlugin(customize(def));
  };
  customizablePlugin.toPlugin = (): SlatePlugin[] => ceateSlatePlugin(def);
  return customizablePlugin;
}

export default createListIndentionPlugin;