File size: 3,688 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { DataTType, JsonSchema } from '@react-page/editor';
import React, {
  useCallback,
  useEffect,
  useMemo,
  useRef,
  useState,
} from 'react';
import type { BaseRange } from 'slate';
import { Range, Transforms } from 'slate';
import { useSlate } from 'slate-react';
import useAddPlugin from '../hooks/useAddPlugin';
import { getCurrentNodeDataWithPlugin } from '../hooks/useCurrentNodeDataWithPlugin';
import usePluginIsActive from '../hooks/usePluginIsActive';
import useRemovePlugin from '../hooks/useRemovePlugin';
import UniformsControls from '../pluginFactories/components/UniformsControls';

import type {
  PluginButtonProps,
  SlatePluginControls,
  SlatePluginDefinition,
} from '../types/slatePluginDefinitions';
import { useSetDialogIsVisible } from './DialogVisibleProvider';

type Props = {
  plugin: SlatePluginDefinition;
} & PluginButtonProps;

function PluginControls(
  props: Props & {
    open: boolean;
    close: () => void;
  }
) {
  const { plugin } = props;

  const storedPropsRef = useRef<{
    selection: Range;
    isActive: boolean;
    data: DataTType;
  }>();

  const isVoid =
    plugin.pluginType === 'component' &&
    (plugin.object === 'inline' || plugin.object === 'block') &&
    plugin.isVoid;
  const shouldInsertWithText =
    !isVoid &&
    (!storedPropsRef?.current?.selection ||
      Range.isCollapsed(storedPropsRef?.current?.selection)) &&
    !storedPropsRef?.current?.isActive;

  const addPlugin = useAddPlugin(plugin);
  const removePlugin = useRemovePlugin(plugin);
  const editor = useSlate();
  const setIsVisible = useSetDialogIsVisible();
  const [_open, _setOpen] = useState(false);
  const isActive = usePluginIsActive(plugin);

  useEffect(() => {
    // this is to indicate that any dialog is visible
    setIsVisible?.(props.open);
    _setOpen(props.open);
    if (props.open) {
      // we need to store the current state, when the dialog will open (but before it actually does)
      // this is also why we have a "delayed" _setOpen
      storedPropsRef.current = {
        selection: editor.selection as BaseRange,
        isActive,
        data: getCurrentNodeDataWithPlugin(editor, plugin),
      };
    }
    return () => {
      setIsVisible?.(false);
    };
  }, [props.open, setIsVisible, _setOpen]);

  const { controls } = plugin;
  const Controls = useMemo(() => {
    return controls
      ? controls.type === 'autoform'
        ? (props: SlatePluginControls<any>) => (
            <UniformsControls
              {...props}
              schema={controls?.schema as JsonSchema<any>}
            />
          )
        : controls.Component
      : UniformsControls;
  }, [controls]);

  const add = useCallback(
    (p: any) => {
      if (storedPropsRef?.current?.selection) {
        // restore selection before adding
        Transforms.select(editor, storedPropsRef?.current?.selection);
      }
      addPlugin(p);
    },
    [addPlugin]
  );
  const remove = useCallback(() => {
    // see https://github.com/ianstormtaylor/slate/issues/4240
    setTimeout(() => {
      if (storedPropsRef?.current?.selection) {
        // restore selection before removing
        Transforms.select(editor, storedPropsRef?.current?.selection);
      }
      removePlugin();
    }, 100);
  }, [removePlugin]);

  return props.open ? (
    <Controls
      pluginConfig={plugin}
      add={add}
      remove={remove}
      isActive={storedPropsRef?.current?.isActive ?? false}
      shouldInsertWithText={shouldInsertWithText}
      data={storedPropsRef?.current?.data}
      {...props}
    />
  ) : null;
}

export default React.memo(PluginControls);