@@ -33,6 +33,7 @@ export class CommentEditor implements IFocusableNode {
3333 constructor (
3434 public workspace : WorkspaceSvg ,
3535 commentId ?: string ,
36+ private onFinishEditing ?: ( ) => void ,
3637 ) {
3738 this . foreignObject = dom . createSvgElement ( Svg . FOREIGNOBJECT , {
3839 'class' : 'blocklyCommentForeignObject' ,
@@ -75,6 +76,14 @@ export class CommentEditor implements IFocusableNode {
7576 getFocusManager ( ) . focusNode ( this ) ;
7677 } ,
7778 ) ;
79+
80+ // Register listener for keydown events that would finish editing.
81+ browserEvents . conditionalBind (
82+ this . textArea ,
83+ 'keydown' ,
84+ this ,
85+ this . handleKeyDown ,
86+ ) ;
7887 }
7988
8089 /** Gets the dom structure for this comment editor. */
@@ -86,6 +95,7 @@ export class CommentEditor implements IFocusableNode {
8695 getText ( ) : string {
8796 return this . text ;
8897 }
98+
8999 /** Sets the current text of the comment and fires change listeners. */
90100 setText ( text : string ) {
91101 this . textArea . value = text ;
@@ -105,6 +115,18 @@ export class CommentEditor implements IFocusableNode {
105115 }
106116 }
107117
118+ /**
119+ * Do something when the user indicates they've finished editing.
120+ *
121+ * @param e Keyboard event.
122+ */
123+ private handleKeyDown ( e : KeyboardEvent ) {
124+ if ( e . key === 'Escape' || ( e . key === 'Enter' && ( e . ctrlKey || e . metaKey ) ) ) {
125+ if ( this . onFinishEditing ) this . onFinishEditing ( ) ;
126+ e . stopPropagation ( ) ;
127+ }
128+ }
129+
108130 /** Registers a callback that listens for text changes. */
109131 addTextChangeListener ( listener : ( oldText : string , newText : string ) => void ) {
110132 this . textChangeListeners . push ( listener ) ;
0 commit comments