-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.jsx
56 lines (53 loc) · 2.25 KB
/
index.jsx
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
import attachImmutableEntitiesToYouTube from './modifiers/attachImmutableEntitiesToYouTube';
import {EditorState} from 'draft-js';
import YouTube from './YouTube';
import youtubeStrategy from './youtubeStrategy';
const createYouTubePlugin = () => {
const callbacks = {
keyBindingFn: undefined,
handleKeyCommand: undefined,
onDownArrow: undefined,
onUpArrow: undefined,
onTab: undefined,
onEscape: undefined,
handleReturn: undefined,
onChange: undefined,
};
const store = {
getEditorState: undefined,
setEditorState: undefined,
};
return {
decorators: [
{
strategy: youtubeStrategy,
component: YouTube
}
],
initialize: ({ getEditorState, setEditorState }) => {
store.getEditorState = getEditorState;
store.setEditorState = setEditorState;
},
onDownArrow: keyboardEvent => callbacks.onDownArrow && callbacks.onDownArrow(keyboardEvent),
onTab: keyboardEvent => callbacks.onTab && callbacks.onTab(keyboardEvent),
onUpArrow: keyboardEvent => callbacks.onUpArrow && callbacks.onUpArrow(keyboardEvent),
onEscape: keyboardEvent => callbacks.onEscape && callbacks.onEscape(keyboardEvent),
handleReturn: keyboardEvent => callbacks.handleReturn && callbacks.handleReturn(keyboardEvent),
onChange: editorState => {
let newEditorState = attachImmutableEntitiesToYouTube(editorState);
if (!newEditorState.getCurrentContent().equals(editorState.getCurrentContent())) {
// Forcing the current selection ensures that it will be at it's right place.
// This solves the issue where inserting an Emoji on OSX with Apple's Emoji
// selector led to the right selection the data, but wrong position in
// the contenteditable.
newEditorState = EditorState.forceSelection(
newEditorState,
newEditorState.getSelection()
);
}
if (callbacks.onChange) return callbacks.onChange(newEditorState);
return newEditorState;
}
}
};
export default createYouTubePlugin