File size: 2,148 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 |
import {
lazyLoad,
useAllFocusedNodeIds,
useUiTranslator,
} from '@react-page/editor';
import React from 'react';
import { Editable, useFocused, useSelected } from 'slate-react';
import type { SlateProps } from '../types/component';
import type { SlatePlugin } from '../types/SlatePlugin';
import { useDialogIsVisible } from './DialogVisibleProvider';
import { useOnKeyDown } from './hotkeyHooks';
import { useRenderElement, useRenderLeave } from './renderHooks';
const HoverButtons = lazyLoad(() => import('./HoverButtons'));
const SlateEditable = React.memo(
(props: {
plugins: SlatePlugin[];
defaultPluginType: string;
readOnly: boolean;
placeholder: string;
}) => {
const { plugins, defaultPluginType, readOnly, placeholder } = props;
const injections = {
useSelected,
useFocused,
readOnly,
};
const renderElement = useRenderElement(
{ plugins, defaultPluginType, injections },
[]
);
const renderLeaf = useRenderLeave({ plugins, injections }, []);
const onKeyDown = useOnKeyDown({ plugins }, []);
// this is required so that dialogs & controls don't mess with slate's selection
const dialogVisible = useDialogIsVisible();
const multipleNodesSelected = useAllFocusedNodeIds().length > 1;
return (
<Editable
placeholder={readOnly ? undefined : placeholder}
readOnly={dialogVisible || readOnly || multipleNodesSelected}
renderElement={renderElement}
renderLeaf={renderLeaf}
onKeyDown={readOnly ? undefined : onKeyDown}
/>
);
}
);
const SlateEditor = (props: SlateProps) => {
const { plugins, focused, readOnly } = props;
const { t } = useUiTranslator();
return (
<>
{!readOnly && focused && (
<HoverButtons
plugins={props.plugins}
translations={props.translations}
/>
)}
<SlateEditable
placeholder={t(props.translations?.placeholder) ?? ''}
readOnly={readOnly}
plugins={plugins}
defaultPluginType={props.defaultPluginType}
/>
</>
);
};
export default React.memo(SlateEditor);
|