@@ -3006,6 +3006,8 @@ function RemoteFunctions(config = {}) {
30063006 // Make the element editable
30073007 element . setAttribute ( "contenteditable" , "true" ) ;
30083008 element . focus ( ) ;
3009+ // to compare with the new text content, if same we don't make any changes in the editor area
3010+ const oldContent = element . textContent ;
30093011
30103012 // Move cursor to end if no existing selection
30113013 const selection = window . getSelection ( ) ;
@@ -3015,16 +3017,35 @@ function RemoteFunctions(config = {}) {
30153017
30163018 dismissUIAndCleanupState ( ) ;
30173019
3020+ // flag to check if escape is pressed, if pressed we prevent onBlur from handling it as keydown already handles
3021+ let isEscapePressed = false ;
3022+
30183023 function onBlur ( ) {
3019- finishEditing ( element ) ;
3024+ // Small delay so that keydown can handle things first
3025+ setTimeout ( ( ) => {
3026+ if ( isEscapePressed ) {
3027+ isEscapePressed = false ;
3028+ finishEditingCleanup ( element ) ;
3029+ return ;
3030+ }
3031+
3032+ const newContent = element . textContent ;
3033+ if ( oldContent !== newContent ) {
3034+ finishEditing ( element ) ;
3035+ } else { // if same content, we just cleanup things
3036+ finishEditingCleanup ( element ) ;
3037+ }
3038+ } , 10 ) ;
30203039 }
30213040
30223041 function onKeyDown ( event ) {
30233042 if ( event . key === "Escape" ) {
3043+ isEscapePressed = true ;
30243044 // Cancel editing
30253045 event . preventDefault ( ) ;
30263046 finishEditing ( element , false ) ; // false means that the edit operation was cancelled
30273047 } else if ( event . key === "Enter" && ! event . shiftKey ) {
3048+ isEscapePressed = false ;
30283049 // Finish editing on Enter (unless Shift is held)
30293050 event . preventDefault ( ) ;
30303051 finishEditing ( element ) ;
@@ -3044,9 +3065,7 @@ function RemoteFunctions(config = {}) {
30443065 } ;
30453066 }
30463067
3047- // Function to finish editing and apply changes
3048- // isEditSuccessful: this is a boolean value, defaults to true. false only when the edit operation is cancelled
3049- function finishEditing ( element , isEditSuccessful = true ) {
3068+ function finishEditingCleanup ( element ) {
30503069 if ( ! isElementEditable ( element ) || ! element . hasAttribute ( "contenteditable" ) ) {
30513070 return ;
30523071 }
@@ -3061,6 +3080,12 @@ function RemoteFunctions(config = {}) {
30613080 element . removeEventListener ( "keydown" , element . _editListeners . keydown ) ;
30623081 delete element . _editListeners ;
30633082 }
3083+ }
3084+
3085+ // Function to finish editing and apply changes
3086+ // isEditSuccessful: this is a boolean value, defaults to true. false only when the edit operation is cancelled
3087+ function finishEditing ( element , isEditSuccessful = true ) {
3088+ finishEditingCleanup ( element ) ;
30643089
30653090 const tagId = element . getAttribute ( "data-brackets-id" ) ;
30663091 window . _Brackets_MessageBroker . send ( {
0 commit comments