@@ -16,7 +16,48 @@ import leaveList from './modifiers/leaveList';
16
16
import insertText from './modifiers/insertText' ;
17
17
import createLinkDecorator from './decorators/link' ;
18
18
import createImageDecorator from './decorators/image' ;
19
+ import { addText , addEmptyBlock } from './utils' ;
19
20
21
+ const INLINE_STYLE_CHARACTERS = [ ' ' , '*' , '_' ] ;
22
+
23
+ function checkCharacterForState ( editorState , character ) {
24
+ let newEditorState = handleBlockType ( editorState , character ) ;
25
+ if ( editorState === newEditorState ) {
26
+ newEditorState = handleImage ( editorState , character ) ;
27
+ }
28
+ if ( editorState === newEditorState ) {
29
+ newEditorState = handleLink ( editorState , character ) ;
30
+ }
31
+ if ( editorState === newEditorState ) {
32
+ newEditorState = handleInlineStyle ( editorState , character ) ;
33
+ }
34
+ return newEditorState ;
35
+ }
36
+
37
+ function checkReturnForState ( editorState , ev ) {
38
+ let newEditorState = editorState ;
39
+ const contentState = editorState . getCurrentContent ( ) ;
40
+ const selection = editorState . getSelection ( ) ;
41
+ const key = selection . getStartKey ( ) ;
42
+ const currentBlock = contentState . getBlockForKey ( key ) ;
43
+ const type = currentBlock . getType ( ) ;
44
+ const text = currentBlock . getText ( ) ;
45
+ if ( / - l i s t - i t e m $ / . test ( type ) && text === '' ) {
46
+ newEditorState = leaveList ( editorState ) ;
47
+ }
48
+ if ( newEditorState === editorState &&
49
+ ( ev . ctrlKey || ev . shiftKey || ev . metaKey || ev . altKey || / ^ h e a d e r - / . test ( type ) ) ) {
50
+ newEditorState = insertEmptyBlock ( editorState ) ;
51
+ }
52
+ if ( newEditorState === editorState && type === 'code-block' ) {
53
+ newEditorState = insertText ( editorState , '\n' ) ;
54
+ }
55
+ if ( newEditorState === editorState ) {
56
+ newEditorState = handleNewCodeBlock ( editorState ) ;
57
+ }
58
+
59
+ return newEditorState ;
60
+ }
20
61
21
62
const createMarkdownShortcutsPlugin = ( config = { } ) => {
22
63
const store = { } ;
@@ -36,34 +77,6 @@ const createMarkdownShortcutsPlugin = (config = {}) => {
36
77
store . setEditorState = setEditorState ;
37
78
store . getEditorState = getEditorState ;
38
79
} ,
39
- handleReturn ( ev , { setEditorState, getEditorState } ) {
40
- const editorState = getEditorState ( ) ;
41
- let newEditorState = editorState ;
42
- const contentState = editorState . getCurrentContent ( ) ;
43
- const selection = editorState . getSelection ( ) ;
44
- const key = selection . getStartKey ( ) ;
45
- const currentBlock = contentState . getBlockForKey ( key ) ;
46
- const type = currentBlock . getType ( ) ;
47
- const text = currentBlock . getText ( ) ;
48
- if ( / - l i s t - i t e m $ / . test ( type ) && text === '' ) {
49
- newEditorState = leaveList ( editorState ) ;
50
- }
51
- if ( newEditorState === editorState &&
52
- ( ev . ctrlKey || ev . shiftKey || ev . metaKey || ev . altKey || / ^ h e a d e r - / . test ( type ) ) ) {
53
- newEditorState = insertEmptyBlock ( editorState ) ;
54
- }
55
- if ( newEditorState === editorState && type === 'code-block' ) {
56
- newEditorState = insertText ( editorState , '\n' ) ;
57
- }
58
- if ( newEditorState === editorState ) {
59
- newEditorState = handleNewCodeBlock ( editorState ) ;
60
- }
61
- if ( editorState !== newEditorState ) {
62
- setEditorState ( newEditorState ) ;
63
- return 'handled' ;
64
- }
65
- return 'not-handled' ;
66
- } ,
67
80
blockStyleFn ( block ) {
68
81
switch ( block . getType ( ) ) {
69
82
case CHECKABLE_LIST_ITEM :
@@ -100,21 +113,49 @@ const createMarkdownShortcutsPlugin = (config = {}) => {
100
113
}
101
114
return 'not-handled' ;
102
115
} ,
116
+ handleReturn ( ev , { setEditorState, getEditorState } ) {
117
+ const editorState = getEditorState ( ) ;
118
+ const newEditorState = checkReturnForState ( editorState , ev ) ;
119
+ if ( editorState !== newEditorState ) {
120
+ setEditorState ( newEditorState ) ;
121
+ return 'handled' ;
122
+ }
123
+ return 'not-handled' ;
124
+ } ,
103
125
handleBeforeInput ( character , { getEditorState, setEditorState } ) {
104
126
if ( character !== ' ' ) {
105
127
return 'not-handled' ;
106
128
}
107
129
const editorState = getEditorState ( ) ;
108
- let newEditorState = handleBlockType ( editorState , character ) ;
109
- if ( editorState === newEditorState ) {
110
- newEditorState = handleImage ( editorState , character ) ;
111
- }
112
- if ( editorState === newEditorState ) {
113
- newEditorState = handleLink ( editorState , character ) ;
130
+ const newEditorState = checkCharacterForState ( editorState , character ) ;
131
+ if ( editorState !== newEditorState ) {
132
+ setEditorState ( newEditorState ) ;
133
+ return 'handled' ;
114
134
}
115
- if ( editorState === newEditorState ) {
116
- newEditorState = handleInlineStyle ( editorState , character ) ;
135
+ return 'not-handled' ;
136
+ } ,
137
+ handlePastedText ( text , html , { getEditorState, setEditorState } ) {
138
+ const editorState = getEditorState ( ) ;
139
+ let newEditorState = editorState ;
140
+ let buffer = [ ] ;
141
+ for ( let i = 0 ; i < text . length ; i ++ ) { // eslint-disable-line no-plusplus
142
+ if ( INLINE_STYLE_CHARACTERS . indexOf ( text [ i ] ) >= 0 ) {
143
+ newEditorState = addText ( newEditorState , buffer . join ( '' ) + text [ i ] ) ;
144
+ newEditorState = checkCharacterForState ( newEditorState , text [ i ] ) ;
145
+ buffer = [ ] ;
146
+ } else if ( text [ i ] . charCodeAt ( 0 ) === 10 ) {
147
+ newEditorState = addText ( newEditorState , buffer . join ( '' ) ) ;
148
+ newEditorState = addEmptyBlock ( newEditorState ) ;
149
+ newEditorState = checkReturnForState ( newEditorState , { } ) ;
150
+ buffer = [ ] ;
151
+ } else if ( i === text . length - 1 ) {
152
+ newEditorState = addText ( newEditorState , buffer . join ( '' ) + text [ i ] ) ;
153
+ buffer = [ ] ;
154
+ } else {
155
+ buffer . push ( text [ i ] ) ;
156
+ }
117
157
}
158
+
118
159
if ( editorState !== newEditorState ) {
119
160
setEditorState ( newEditorState ) ;
120
161
return 'handled' ;
0 commit comments