-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.tsx
46 lines (38 loc) · 1.15 KB
/
index.tsx
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
import React from 'react';
import AutoReplace from 'slate-auto-replace';
import { RenderBlockProps, Editor } from 'slate-react';
import BlockButton, { BlockButtonProps } from '../../ui/BlockButton';
import EditorHandler from '../../../helpers/EditorHandler';
import Icon from './icon.svg';
export const TYPE = 'blockquote';
const Button: React.FunctionComponent<BlockButtonProps> = (props) => (
<BlockButton blockType={TYPE} {...props}>
<Icon />
</BlockButton>
)
const Block: React.FunctionComponent<RenderBlockProps> = ({ isSelected, isFocused, ...props }) => (
<blockquote {...props} />
)
const Plugin = [
AutoReplace({
trigger: 'space', before: /^([\>])$/, change: (editor: Editor) => editor.setBlocks({ type: TYPE }),
}),
{
onKeyDown(event: KeyboardEvent, editor: Editor, next: () => void) {
if (!EditorHandler.hasBlock(editor.value, TYPE)) {
return next();
}
if (event.key === 'Enter' && EditorHandler.isEmpty(editor.value)) {
event.preventDefault();
return editor.setBlocks(EditorHandler.DEFAULT_NODE);
}
return next();
},
}
]
export default {
TYPE,
Button,
Block,
Plugin
};