@@ -16,7 +16,48 @@ import leaveList from './modifiers/leaveList';
1616import insertText from './modifiers/insertText' ;
1717import createLinkDecorator from './decorators/link' ;
1818import createImageDecorator from './decorators/image' ;
19+ import { addText , addEmptyBlock } from './utils' ;
1920
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+ }
2061
2162const createMarkdownShortcutsPlugin = ( config = { } ) => {
2263 const store = { } ;
@@ -36,34 +77,6 @@ const createMarkdownShortcutsPlugin = (config = {}) => {
3677 store . setEditorState = setEditorState ;
3778 store . getEditorState = getEditorState ;
3879 } ,
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- } ,
6780 blockStyleFn ( block ) {
6881 switch ( block . getType ( ) ) {
6982 case CHECKABLE_LIST_ITEM :
@@ -100,21 +113,49 @@ const createMarkdownShortcutsPlugin = (config = {}) => {
100113 }
101114 return 'not-handled' ;
102115 } ,
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+ } ,
103125 handleBeforeInput ( character , { getEditorState, setEditorState } ) {
104126 if ( character !== ' ' ) {
105127 return 'not-handled' ;
106128 }
107129 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' ;
114134 }
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+ }
117157 }
158+
118159 if ( editorState !== newEditorState ) {
119160 setEditorState ( newEditorState ) ;
120161 return 'handled' ;
0 commit comments