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; } /**