How to open a block's comment programmaticly

103 views
Skip to first unread message

C Ben

unread,
Sep 1, 2022, 10:40:41 AM9/1/22
to Blockly
Hi,
In order to foster comments use in my users' blockly programs I'd like to automaticly open a comment on the Hat Block of program. This should engage users to fillin short  comments on how they use blocks to help user to reuse their programs.

I try to implement as follows:
function showCommentOnHatBlock(workspace) {
var startingBlock = workspace.getBlocksByType("hat")[0];
    startingBlock.setCommentText("Put your comments there"); // works well : the block is automaticly updated with the "?" icon
 startingBlock.commentModel.pinned = true; // has no effect and the comment bubble is not shown
}

Any idea to force comment display ?
Thanks
Chris

ewpa...@gmail.com

unread,
Sep 1, 2022, 12:17:58 PM9/1/22
to Blockly
Have you tried:

startingBlock.comment.setVisible(true);

Regards,
Evan

C Ben

unread,
Sep 1, 2022, 1:43:32 PM9/1/22
to Blockly
That works. Thanks a lot Evan !
Chris

C Ben

unread,
Sep 1, 2022, 4:18:49 PM9/1/22
to Blockly
I'd also like to open the comment bubble in the toolbox (but it seems to be not possible) or at least when the block is just added into the workspace. Is there a specific means to do that ?
Thanks
Chris

ewpa...@gmail.com

unread,
Sep 2, 2022, 4:24:49 PM9/2/22
to Blockly
I don't think it's possible to open it in the toolbox, but you might be able to do it after it's added to the workspace by adding a workspace change listener and then scheduling a call to set block's comment visible in response to a BLOCK_CREATE event.

Regards,
Evan

C Ben

unread,
Sep 16, 2022, 6:34:32 AM9/16/22
to Blockly
Thanks Evan, it works well.
I always display the comment on the block named "hat" and if no comment exists on it I create a default one:
My piece of code that works:

if ( event.type == Blockly.Events.BLOCK_CREATE && event.json.type == "hat") {
   if (workspace.getBlockById(event.json.id))
   {
       var myHatBlock = workspace.getBlockById(event.json.id);
       if (!myHatBlock.getCommentText()) {
           myHatBlock.setCommentText(Blockly.Msg["HAT_DEFAULT_COMMENT"]);
           myHatBlock.comment.setBubbleSize(300, 100);
       }
      startingBlock.comment.setVisible(true);
   }
}

Chris
Reply all
Reply to author
Forward
0 new messages