From 6b023b3a32725437ff3cf0a8e7c01db33de100ad Mon Sep 17 00:00:00 2001 From: Trey Pisano <126501514+treypisano@users.noreply.github.com> Date: Mon, 2 Oct 2023 16:14:12 -0400 Subject: [PATCH] fix: comment change event fires block change event (#7505) * add fire event to comment change * change variable names to make readable * add oldtext field to commenticon * remove debugger * remove oldtext field * add firing block change event to setText * add fire to setText * fix: setCommentText fire block change when icon removed * remove whitespace * NOT PASSING TESTS: fix block test fails * fix: remove duplicate events so tests pass * fix: remove whitespace * fix: run format * refactor: add early returns to onTextChange for readability * remove console.log * disable events temporarily when setText called in setCommentText * refactor: move fire event to original position * fix: run format * fix: move fire event so it happpens after events are enabled and disabled --- core/block.ts | 22 ++++++++++++---------- core/icons/comment_icon.ts | 28 +++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/core/block.ts b/core/block.ts index 927d74b0c..578015d3a 100644 --- a/core/block.ts +++ b/core/block.ts @@ -2208,6 +2208,18 @@ export class Block implements IASTNodeLocation, IDeletable { const comment = this.getIcon(CommentIcon.TYPE) as CommentIcon | null; const oldText = comment?.getText() ?? null; if (oldText === text) return; + if (text !== null) { + let comment = this.getIcon(CommentIcon.TYPE) as CommentIcon | undefined; + if (!comment) { + comment = this.addIcon(new CommentIcon(this)); + } + eventUtils.disable(); + comment.setText(text); + eventUtils.enable(); + } else { + this.removeIcon(CommentIcon.TYPE); + } + eventUtils.fire( new (eventUtils.get(eventUtils.BLOCK_CHANGE))( this, @@ -2217,16 +2229,6 @@ export class Block implements IASTNodeLocation, IDeletable { text, ), ); - - if (text !== null) { - let comment = this.getIcon(CommentIcon.TYPE) as CommentIcon | undefined; - if (!comment) { - comment = this.addIcon(new CommentIcon(this)); - } - comment.setText(text); - } else { - this.removeIcon(CommentIcon.TYPE); - } } /** diff --git a/core/icons/comment_icon.ts b/core/icons/comment_icon.ts index d52782437..d99588fe6 100644 --- a/core/icons/comment_icon.ts +++ b/core/icons/comment_icon.ts @@ -155,6 +155,16 @@ export class CommentIcon extends Icon implements IHasBubble, ISerializable { /** Sets the text of this comment. Updates any bubbles if they are visible. */ setText(text: string) { + const oldText = this.text; + eventUtils.fire( + new (eventUtils.get(eventUtils.BLOCK_CHANGE))( + this.sourceBlock, + 'comment', + null, + oldText, + text, + ), + ); this.text = text; this.textInputBubble?.setText(this.text); this.textBubble?.setText(this.text); @@ -217,9 +227,21 @@ export class CommentIcon extends Icon implements IHasBubble, ISerializable { * the input bubble. */ onTextChange(): void { - if (this.textInputBubble) { - this.text = this.textInputBubble.getText(); - } + if (!this.textInputBubble) return; + + const newText = this.textInputBubble.getText(); + if (this.text === newText) return; + + eventUtils.fire( + new (eventUtils.get(eventUtils.BLOCK_CHANGE))( + this.sourceBlock, + 'comment', + null, + this.text, + newText, + ), + ); + this.text = newText; } /**