-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEditor.tsx
42 lines (35 loc) · 1.24 KB
/
Editor.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
import "draft-js/dist/Draft.css";
import { DraftEditorCommand, DraftHandleValue, EditorState, RichUtils } from "draft-js";
import PluginEditor from "draft-js-plugins-editor";
import React, { ReactElement, useState } from "react";
import createListPlugin from "../../src/index";
const listPlugin = createListPlugin();
const plugins = [listPlugin];
export default function Editor(): ReactElement {
const [editorState, setEditorState] = useState(EditorState.createEmpty());
const handleKeyCommand = (
command: DraftEditorCommand,
oldState: EditorState,
): DraftHandleValue => {
const newState = RichUtils.handleKeyCommand(oldState, command);
if (newState) {
setEditorState(newState);
return "handled";
}
return "not-handled";
};
// Determine whether placeholder should be displayed (to avoid overlap with lists)
const blockType = RichUtils.getCurrentBlockType(editorState);
const isOl = blockType === "ordered-list-item";
const isUl = blockType === "unordered-list-item";
const placeholderIsVisible = !isOl && !isUl;
return (
<PluginEditor
editorState={editorState}
handleKeyCommand={handleKeyCommand}
onChange={setEditorState}
placeholder={placeholderIsVisible ? "Start typing…" : ""}
plugins={plugins}
/>
);
}