Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/scratch_block_paster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import * as Blockly from "blockly/core";
import type { ScratchCommentIcon } from "./scratch_comment_icon";

/**
* Class responsible for handling the pasting of copied blocks.
Expand All @@ -31,6 +32,15 @@ class ScratchBlockPaster extends Blockly.clipboard.BlockPaster {
block.setDragStrategy(new Blockly.dragging.BlockDragStrategy(block));
}

// Deserialization of blocks suppresses events, so even though this gets
// fired for blocks with comments, the VM will never receive it, causing its
// state to get out of sync. Manually fire it here (after suppression has
// been turned off) if needed.
const commentIcon = block.getIcon(Blockly.icons.IconType.COMMENT);
if (commentIcon) {
(commentIcon as ScratchCommentIcon).fireCreateEvent();
}

return block;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/scratch_comment_bubble.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ScratchCommentBubble extends Blockly.comments.CommentView {
super(sourceBlock.workspace);
this.sourceBlock = sourceBlock;
this.disposing = false;
this.id = Blockly.utils.idGenerator.genUid();
this.id = `${sourceBlock.id}_comment`;
this.setPlaceholderText(Blockly.Msg.WORKSPACE_COMMENT_DEFAULT_TEXT);
this.getSvgRoot().setAttribute(
"style",
Expand Down
15 changes: 11 additions & 4 deletions src/scratch_comment_icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface CommentState {
/**
* Custom comment icon that draws no icon indicator, used for block comments.
*/
class ScratchCommentIcon
export class ScratchCommentIcon
extends Blockly.icons.Icon
implements Blockly.ISerializable, Blockly.IHasBubble
{
Expand All @@ -34,9 +34,7 @@ class ScratchCommentIcon
constructor(protected sourceBlock: Blockly.BlockSvg) {
super(sourceBlock);
this.commentBubble = new ScratchCommentBubble(this.sourceBlock);
Blockly.Events.fire(
new (Blockly.Events.get("block_comment_create"))(this.commentBubble)
);
this.fireCreateEvent();
this.onTextChangedListener = this.onTextChanged.bind(this);
this.onSizeChangedListener = this.onSizeChanged.bind(this);
this.onCollapseListener = this.onCollapsed.bind(this);
Expand Down Expand Up @@ -202,6 +200,15 @@ class ScratchCommentIcon
this.commentBubble.dispose();
super.dispose();
}

/**
* Fires a block comment create event corresponding to this icon's comment.
*/
fireCreateEvent() {
Blockly.Events.fire(
new (Blockly.Events.get("block_comment_create"))(this.commentBubble)
);
}
}

Blockly.registry.register(
Expand Down