This repository was archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
52 lines (44 loc) · 1.59 KB
/
index.js
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
// @flow
import CodeUtils from 'draft-js-code';
import { RichUtils } from 'draft-js';
type Options = {};
type EditorState = Object;
type PluginFunctions = {
setEditorState: Function,
getEditorState: Function,
};
type Command = string;
const createCodeEditorPlugin = (options?: Options) => {
return {
handleKeyCommand(command: Command, editorState: EditorState, _: any, { setEditorState }: PluginFunctions) {
let newState;
if (CodeUtils.hasSelectionInBlock(editorState)) {
newState = CodeUtils.handleKeyCommand(editorState, command);
} else {
newState = RichUtils.handleKeyCommand(editorState, command);
}
if (newState) {
setEditorState(newState);
return 'handled';
}
return 'not-handled';
},
keyBindingFn(evt: Event, { getEditorState, setEditorState }: PluginFunctions) {
const editorState = getEditorState();
if (!CodeUtils.hasSelectionInBlock(editorState)) return;
return CodeUtils.getKeyBinding(evt);
},
handleReturn(evt: Event, editorState: EditorState, { setEditorState }: PluginFunctions) {
if (!CodeUtils.hasSelectionInBlock(editorState)) return 'not-handled';
setEditorState(CodeUtils.handleReturn(evt, editorState));
return 'handled';
},
onTab(evt: Event, { getEditorState, setEditorState }: PluginFunctions) {
const editorState = getEditorState()
if (!CodeUtils.hasSelectionInBlock(editorState)) return 'not-handled';
setEditorState(CodeUtils.onTab(evt, editorState));
return 'handled';
}
}
}
export default createCodeEditorPlugin;