@@ -11,7 +11,15 @@ import SelectionManager from 'mobiledoc-kit/editor/selection-manager';
1111import Browser from 'mobiledoc-kit/utils/browser' ;
1212
1313const ELEMENT_EVENT_TYPES = [
14- 'keydown' , 'keyup' , 'cut' , 'copy' , 'paste' , 'keypress' , 'drop'
14+ 'keydown' ,
15+ 'keyup' ,
16+ 'cut' ,
17+ 'copy' ,
18+ 'paste' ,
19+ 'keypress' ,
20+ 'drop' ,
21+ 'compositionstart' ,
22+ 'compositionend' ,
1523] ;
1624
1725export default class EventManager {
@@ -146,6 +154,13 @@ export default class EventManager {
146154 event . preventDefault ( ) ;
147155 }
148156
157+ // Handle carriage returns
158+ if ( ! key . isEnter ( ) && key . keyCode === 13 ) {
159+ _textInputHandler . handleNewLine ( ) ;
160+ editor . handleNewline ( event ) ;
161+ return ;
162+ }
163+
149164 _textInputHandler . handle ( key . toString ( ) ) ;
150165 }
151166
@@ -166,6 +181,10 @@ export default class EventManager {
166181 let range = editor . range ;
167182
168183 switch ( true ) {
184+ // Ignore keydown events when using an IME
185+ case key . isIME ( ) : {
186+ break ;
187+ }
169188 // FIXME This should be restricted to only card/atom boundaries
170189 case key . isHorizontalArrowWithoutModifiersOtherThanShift ( ) : {
171190 let newRange ;
@@ -210,6 +229,59 @@ export default class EventManager {
210229 this . _updateModifiersFromKey ( key , { isDown :false } ) ;
211230 }
212231
232+ // The mutation handler interferes with IMEs when composing
233+ // on a blank line. These two event handlers are for suppressing
234+ // mutation handling in this scenario.
235+ compositionstart ( event ) { // eslint-disable-line
236+ let { editor } = this ;
237+ // Ignore compositionstart if not on a blank line
238+ if ( editor . range . headMarker ) {
239+ return ;
240+ }
241+ this . _isComposingOnBlankLine = true ;
242+
243+ if ( editor . post . isBlank ) {
244+ editor . _insertEmptyMarkupSectionAtCursor ( ) ;
245+ }
246+
247+ // Stop listening for mutations on Chrome browsers and suppress
248+ // mutations by prepending a character for other browsers.
249+ // The reason why we treat these separately is because
250+ // of the way each browser processes IME inputs.
251+ if ( Browser . isChrome ( ) ) {
252+ editor . setPlaceholder ( '' ) ;
253+ editor . _mutationHandler . stopObserving ( ) ;
254+ } else {
255+ this . _textInputHandler . handle ( ' ' ) ;
256+ }
257+ }
258+
259+ compositionend ( event ) {
260+ let { editor } = this ;
261+
262+ // Ignore compositionend if not composing on blank line
263+ if ( ! this . _isComposingOnBlankLine ) {
264+ return ;
265+ }
266+ this . _isComposingOnBlankLine = false ;
267+
268+ // Start listening for mutations on Chrome browsers and
269+ // delete the prepended character introduced by compositionstart
270+ // for other browsers.
271+ if ( Browser . isChrome ( ) ) {
272+ editor . insertText ( event . data ) ;
273+ editor . setPlaceholder ( editor . placeholder ) ;
274+ editor . _mutationHandler . startObserving ( ) ;
275+ } else {
276+ let startOfCompositionLine = editor . range . headSection . toPosition ( 0 ) ;
277+ let endOfCompositionLine = editor . range . headSection . toPosition ( event . data . length ) ;
278+ editor . run ( postEditor => {
279+ postEditor . deleteAtPosition ( startOfCompositionLine , 1 , { unit : 'char' } ) ;
280+ postEditor . setRange ( endOfCompositionLine ) ;
281+ } ) ;
282+ }
283+ }
284+
213285 cut ( event ) {
214286 event . preventDefault ( ) ;
215287
0 commit comments