Get Block ID of clicked block in readOnly mode

43 views
Skip to first unread message

Laurence Stant

unread,
Aug 11, 2025, 11:26:40 AMAug 11
to Blockly
I have a blockly workspace which is just used as a front end to a python execution engine running on a backend. In run mode I display the workspace in read-only mode and highlight the currently executed block over SSE. I'd like to allow the user, when in a stepping mode, to select a block to jump to (for help when debugging). However, it appears in read-only that the click event fires with an ID of undefined. Is there an easy mod for this, or another way around it?

Thanks,
Laurence

Laurence Stant

unread,
Aug 11, 2025, 6:45:46 PMAug 11
to Blockly
Having had a further look, I guess I would have to patch core/block_svg where the pointer down handler immeditely returns if the workspace is readonly and bypasses the gesture creation.
I suppose I can emit a Click event instead of returning, to catch with the regular custom event handlers registered on the workspace. Am I barking up the right tree? Thanks

ewpa...@gmail.com

unread,
Aug 12, 2025, 1:11:29 PMAug 12
to Blockly
This seems to work in Firefox using Blockly v12. YMMV in other browsers and versions:

document.body.addEventListener('pointerdown', function(e) {
  let target = e.target;
  while (target && target != document.body) {
    if (target.classList.contains('blocklyBlock')) {
      console.log(target.dataset['id']);  // Handle your actual logic here.
      return;
    }
    target = target.parentElement;
  }
}, true);

Cheers,
Evan

Laurence Stant

unread,
Aug 12, 2025, 2:56:46 PMAug 12
to blo...@googlegroups.com
Many thanks Evan, I didn't know the dataset trick!

Regards,
Laurence 

--
You received this message because you are subscribed to a topic in the Google Groups "Blockly" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/blockly/GGDmfd3rqKg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to blockly+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/blockly/baca4630-e0c2-47ee-91cd-e14cfe5295dcn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages