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
This commit is contained in:
Trey Pisano
2023-10-02 16:14:12 -04:00
committed by GitHub
parent c455e0c1e5
commit 6b023b3a32
2 changed files with 37 additions and 13 deletions

View File

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

View File

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