@@ -63,6 +63,7 @@ const CALLBACK_QUEUES = {
6363 DID_RENDER : 'didRender' ,
6464 WILL_DELETE : 'willDelete' ,
6565 DID_DELETE : 'didDelete' ,
66+ WILL_HANDLE_NEWLINE : 'willHandleNewline' ,
6667 CURSOR_DID_CHANGE : 'cursorDidChange' ,
6768 DID_REPARSE : 'didReparse' ,
6869 POST_DID_CHANGE : 'postDidChange' ,
@@ -339,6 +340,13 @@ class Editor {
339340 return ;
340341 }
341342 }
343+
344+ // Above logic might delete redundant range, so callback must run after it.
345+ let defaultPrevented = false ;
346+ const event = { preventDefault ( ) { defaultPrevented = true ; } } ;
347+ this . runCallbacks ( CALLBACK_QUEUES . WILL_HANDLE_NEWLINE , [ event ] ) ;
348+ if ( defaultPrevented ) { return ; }
349+
342350 cursorSection = postEditor . splitSection ( range . head ) [ 1 ] ;
343351 postEditor . setRange ( cursorSection . headPosition ( ) ) ;
344352 } ) ;
@@ -782,6 +790,14 @@ class Editor {
782790 this . addCallback ( CALLBACK_QUEUES . DID_DELETE , callback ) ;
783791 }
784792
793+ /**
794+ * @param {Function } callback This callback will be called before handling new line.
795+ * @public
796+ */
797+ willHandleNewline ( callback ) {
798+ this . addCallback ( CALLBACK_QUEUES . WILL_HANDLE_NEWLINE , callback ) ;
799+ }
800+
785801 /**
786802 * @param {Function } callback This callback will be called every time the cursor
787803 * position (or selection) changes.
0 commit comments