forked from github/cinny
* add commands hook * add commands in editor * add command auto complete menu * add commands in room input * remove old reply code from room input * fix video component css * do not auto focus input on android or ios * fix crash on enable block after selection * fix circular deps in editor * fix autocomplete return focus move editor cursor * remove unwanted keydown from room input * fix emoji alignment in editor * test ipad user agent * refactor isAndroidOrIOS to mobileOrTablet * update slate & slate-react * downgrade slate-react to 0.98.4 0.99.0 has breaking changes with ReactEditor.focus * add sql to readable ext mimetype * fix empty editor formatting gets saved as draft * add option to use enter for newline * remove empty msg draft from atom family * prevent msg ctx menu from open on text selection
168 lines
4.4 KiB
TypeScript
168 lines
4.4 KiB
TypeScript
/* eslint-disable no-param-reassign */
|
|
import React, {
|
|
ClipboardEventHandler,
|
|
KeyboardEventHandler,
|
|
ReactNode,
|
|
forwardRef,
|
|
useCallback,
|
|
useState,
|
|
} from 'react';
|
|
import { Box, Scroll, Text } from 'folds';
|
|
import { Descendant, Editor, createEditor } from 'slate';
|
|
import {
|
|
Slate,
|
|
Editable,
|
|
withReact,
|
|
RenderLeafProps,
|
|
RenderElementProps,
|
|
RenderPlaceholderProps,
|
|
} from 'slate-react';
|
|
import { withHistory } from 'slate-history';
|
|
import { BlockType } from './types';
|
|
import { RenderElement, RenderLeaf } from './Elements';
|
|
import { CustomElement } from './slate';
|
|
import * as css from './Editor.css';
|
|
import { toggleKeyboardShortcut } from './keyboard';
|
|
|
|
const initialValue: CustomElement[] = [
|
|
{
|
|
type: BlockType.Paragraph,
|
|
children: [{ text: '' }],
|
|
},
|
|
];
|
|
|
|
const withInline = (editor: Editor): Editor => {
|
|
const { isInline } = editor;
|
|
|
|
editor.isInline = (element) =>
|
|
[BlockType.Mention, BlockType.Emoticon, BlockType.Link, BlockType.Command].includes(
|
|
element.type
|
|
) || isInline(element);
|
|
|
|
return editor;
|
|
};
|
|
|
|
const withVoid = (editor: Editor): Editor => {
|
|
const { isVoid } = editor;
|
|
|
|
editor.isVoid = (element) =>
|
|
[BlockType.Mention, BlockType.Emoticon, BlockType.Command].includes(element.type) ||
|
|
isVoid(element);
|
|
|
|
return editor;
|
|
};
|
|
|
|
export const useEditor = (): Editor => {
|
|
const [editor] = useState(() => withInline(withVoid(withReact(withHistory(createEditor())))));
|
|
return editor;
|
|
};
|
|
|
|
export type EditorChangeHandler = (value: Descendant[]) => void;
|
|
type CustomEditorProps = {
|
|
editableName?: string;
|
|
top?: ReactNode;
|
|
bottom?: ReactNode;
|
|
before?: ReactNode;
|
|
after?: ReactNode;
|
|
maxHeight?: string;
|
|
editor: Editor;
|
|
placeholder?: string;
|
|
onKeyDown?: KeyboardEventHandler;
|
|
onKeyUp?: KeyboardEventHandler;
|
|
onChange?: EditorChangeHandler;
|
|
onPaste?: ClipboardEventHandler;
|
|
};
|
|
export const CustomEditor = forwardRef<HTMLDivElement, CustomEditorProps>(
|
|
(
|
|
{
|
|
editableName,
|
|
top,
|
|
bottom,
|
|
before,
|
|
after,
|
|
maxHeight = '50vh',
|
|
editor,
|
|
placeholder,
|
|
onKeyDown,
|
|
onKeyUp,
|
|
onChange,
|
|
onPaste,
|
|
},
|
|
ref
|
|
) => {
|
|
const renderElement = useCallback(
|
|
(props: RenderElementProps) => <RenderElement {...props} />,
|
|
[]
|
|
);
|
|
|
|
const renderLeaf = useCallback((props: RenderLeafProps) => <RenderLeaf {...props} />, []);
|
|
|
|
const handleKeydown: KeyboardEventHandler = useCallback(
|
|
(evt) => {
|
|
onKeyDown?.(evt);
|
|
const shortcutToggled = toggleKeyboardShortcut(editor, evt);
|
|
if (shortcutToggled) evt.preventDefault();
|
|
},
|
|
[editor, onKeyDown]
|
|
);
|
|
|
|
const renderPlaceholder = useCallback(({ attributes, children }: RenderPlaceholderProps) => {
|
|
// drop style attribute as we use our custom placeholder css.
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
const { style, ...props } = attributes;
|
|
return (
|
|
<Text
|
|
as="span"
|
|
{...props}
|
|
className={css.EditorPlaceholder}
|
|
contentEditable={false}
|
|
truncate
|
|
>
|
|
{children}
|
|
</Text>
|
|
);
|
|
}, []);
|
|
|
|
return (
|
|
<div className={css.Editor} ref={ref}>
|
|
<Slate editor={editor} initialValue={initialValue} onChange={onChange}>
|
|
{top}
|
|
<Box alignItems="Start">
|
|
{before && (
|
|
<Box className={css.EditorOptions} alignItems="Center" gap="100" shrink="No">
|
|
{before}
|
|
</Box>
|
|
)}
|
|
<Scroll
|
|
className={css.EditorTextareaScroll}
|
|
variant="SurfaceVariant"
|
|
style={{ maxHeight }}
|
|
size="300"
|
|
visibility="Hover"
|
|
hideTrack
|
|
>
|
|
<Editable
|
|
data-editable-name={editableName}
|
|
className={css.EditorTextarea}
|
|
placeholder={placeholder}
|
|
renderPlaceholder={renderPlaceholder}
|
|
renderElement={renderElement}
|
|
renderLeaf={renderLeaf}
|
|
onKeyDown={handleKeydown}
|
|
onKeyUp={onKeyUp}
|
|
onPaste={onPaste}
|
|
/>
|
|
</Scroll>
|
|
{after && (
|
|
<Box className={css.EditorOptions} alignItems="Center" gap="100" shrink="No">
|
|
{after}
|
|
</Box>
|
|
)}
|
|
</Box>
|
|
{bottom}
|
|
</Slate>
|
|
</div>
|
|
);
|
|
}
|
|
);
|