Skip to content

Commit 4cdb8b1

Browse files
committed
fix: add event listeners to focus parent comment
1 parent 6bc398b commit 4cdb8b1

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

core/comments/comment_editor.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

core/comments/comment_view.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,13 @@ export class CommentView implements IRenderedElement {
230230
* Creates the text area where users can type. Registers event listeners.
231231
*/
232232
private createTextArea() {
233-
const commentEditor = new CommentEditor(this.workspace, this.commentId);
233+
// When the user is done editing comment, focus the entire comment.
234+
const onFinishEditing = () => this.svgRoot.focus();
235+
const commentEditor = new CommentEditor(
236+
this.workspace,
237+
this.commentId,
238+
onFinishEditing,
239+
);
234240

235241
this.svgRoot.appendChild(commentEditor.getDom());
236242

0 commit comments

Comments
 (0)