@@ -3006,6 +3006,8 @@ function RemoteFunctions(config = {}) {
3006
3006
// Make the element editable
3007
3007
element . setAttribute ( "contenteditable" , "true" ) ;
3008
3008
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 ;
3009
3011
3010
3012
// Move cursor to end if no existing selection
3011
3013
const selection = window . getSelection ( ) ;
@@ -3015,16 +3017,35 @@ function RemoteFunctions(config = {}) {
3015
3017
3016
3018
dismissUIAndCleanupState ( ) ;
3017
3019
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
+
3018
3023
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 ) ;
3020
3039
}
3021
3040
3022
3041
function onKeyDown ( event ) {
3023
3042
if ( event . key === "Escape" ) {
3043
+ isEscapePressed = true ;
3024
3044
// Cancel editing
3025
3045
event . preventDefault ( ) ;
3026
3046
finishEditing ( element , false ) ; // false means that the edit operation was cancelled
3027
3047
} else if ( event . key === "Enter" && ! event . shiftKey ) {
3048
+ isEscapePressed = false ;
3028
3049
// Finish editing on Enter (unless Shift is held)
3029
3050
event . preventDefault ( ) ;
3030
3051
finishEditing ( element ) ;
@@ -3044,9 +3065,7 @@ function RemoteFunctions(config = {}) {
3044
3065
} ;
3045
3066
}
3046
3067
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 ) {
3050
3069
if ( ! isElementEditable ( element ) || ! element . hasAttribute ( "contenteditable" ) ) {
3051
3070
return ;
3052
3071
}
@@ -3061,6 +3080,12 @@ function RemoteFunctions(config = {}) {
3061
3080
element . removeEventListener ( "keydown" , element . _editListeners . keydown ) ;
3062
3081
delete element . _editListeners ;
3063
3082
}
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 ) ;
3064
3089
3065
3090
const tagId = element . getAttribute ( "data-brackets-id" ) ;
3066
3091
window . _Brackets_MessageBroker . send ( {
0 commit comments