Way to select and copy blocks from a read-only instance of Blockly (v13)?

26 views
Skip to first unread message

Alan Smith

unread,
Jul 16, 2026, 5:34:08 PM (3 days ago) Jul 16
to Blockly
I have a samples which allows you to view a set of samples.  I would like to allow users to copy from this (to the clipboard) and then paste into their workspace.

However, when it is read only you can't select a block (necessary for then copying it)

Is there a simple way to allow this selection yet keep everything read-only?

Thanks in advance,

Alan

Alan Smith

unread,
Jul 16, 2026, 5:46:22 PM (3 days ago) Jul 16
to Blockly
I ended up getting it to work by capturing pointerDown on the container and then walking the DOM.   This works but it doesn't feel like a very "blockly" way to solve the problem so I am wondering if there is a better way.

Thanks in advance,

Alan

Zoë Spriggs

unread,
Jul 17, 2026, 8:16:09 PM (2 days ago) Jul 17
to Blockly

Hi Alan,

In your case, you probably want to avoid readonly mode, since it’s intended that you can’t select blocks. Instead, you can set the blocks to be non-movable and/or non-editable. 


The easiest way to handle this is to only set the blocks to be non-editable. Copyability is tied to movability, so if the blocks are still movable then they’ll be copyable. In this scenario, copy/paste would work. The caveat is that the blocks would still paste as non-editable, so you’d have to adjust the state of blocks as they’re coming in. But you could make that adjustment via a changeListener that looks for BLOCK_CREATE events. 


If you want the blocks to be non-editable *and* non-movable, you would have to make more adjustments. You can re-register the copy shortcut via the ShortcutRegistry and add your own copy logic. If you want to be able to right click and copy, you can add a context menu item with the same copy logic. For your use case, when you’re writing the copy logic, you’ll likely want to make it so that the block you copy gets serialized as movable and editable. That way, when your users paste into the regular workspace, the block will be usable.


Let me know if that works or if you have other questions!


Best,

Zoë

Alan Smith

unread,
Jul 18, 2026, 9:00:21 AM (yesterday) Jul 18
to blo...@googlegroups.com
Zoë,
That is an interesting approach. I don't want the blocks movable either. (Samples should be static).

Is there any way to apply that to the workspace or would I need to make sure each block in the sample was marked as non-movable and non-editable?

Thanks in advance,

Alan

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/blockly/94b9c512-836b-4897-84a1-1112db399f13n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages