File size: 1,617 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
import type { FC, MouseEvent } from 'react';
import React, { useCallback, useState } from 'react';

import type { Element } from 'slate';
import { Transforms } from 'slate';
import { ReactEditor, useSlateStatic } from 'slate-react';
import type { SlatePluginDefinition } from '../types/slatePluginDefinitions';
import PluginControls from './PluginControls';

import { useSelected } from 'slate-react';

const VoidEditableElement: FC<{
  plugin: SlatePluginDefinition;
  element: Element;
  component: React.ReactNode;
}> = ({ plugin, element, component }) => {
  const [showVoidDialog, setShowVoidDialog] = useState(false);
  const editor = useSlateStatic();
  const onVoidClick = useCallback(
    (e: MouseEvent) => {
      e.stopPropagation();

      const path = ReactEditor.findPath(editor, element);

      setShowVoidDialog(true);
      Transforms.select(editor, path);
    },
    [editor, element]
  );
  const closeVoidDialog = useCallback(() => setShowVoidDialog(false), []);
  const Element = plugin.object === 'inline' ? 'span' : 'div';
  const selected = useSelected();

  return (
    <>
      {showVoidDialog ? (
        <PluginControls
          open={showVoidDialog}
          close={closeVoidDialog}
          plugin={plugin}
          // TODO: translations
        />
      ) : null}

      <Element
        onClick={onVoidClick}
        style={{
          cursor: 'pointer',
          outline: selected ? '1px dotted grey' : undefined,
        }}
      >
        <Element style={{ pointerEvents: 'none' }}>{component}</Element>
      </Element>
    </>
  );
};

export default VoidEditableElement;