react-code-dataset / react-page /docs /examples /slate-plugin-src /components /VoidEditableElement.tsx
Download react-page/docs/examples/slate-plugin-src/components/VoidEditableElement.tsx from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.62 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-page/docs/examples/slate-plugin-src/components/VoidEditableElement.tsx
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-page/docs/examples/slate-plugin-src/components/VoidEditableElement.tsx
-
curl -L -o VoidEditableElement.tsx https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-page/docs/examples/slate-plugin-src/components/VoidEditableElement.tsx
1.62 kB
| 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; | |