Multiple instances of fixed-size Blockly?

500 views
Skip to first unread message

Austin Bart

unread,
Mar 8, 2015, 11:46:09 AM3/8/15
to blo...@googlegroups.com
So the documentation is very clear that fixed-size Blockly (i.e., Blockly injected into a div) does not allow for more than one instance per page. I can tell from experimentation that multiple Blockly injections "clobber" each other - last one wins and gets proper event interactions. Why is this the case? Is there an interesting reason why Blockly is designed around this sort of Singleton behavior? Or is it just a development cruft that is relatively unimportant (or perhaps completely unimportant), and so near the bottom of the todo list? If the latter, is there a rough idea of what work would need to be done to isolate Blockly instances?

~Cory

Neil Fraser

unread,
Mar 8, 2015, 9:05:05 PM3/8/15
to blo...@googlegroups.com
On 8 March 2015 at 08:46, Austin Bart <acb...@vt.edu> wrote:
So the documentation is very clear that fixed-size Blockly (i.e., Blockly injected into a div) does not allow for more than one instance per page. I can tell from experimentation that multiple Blockly injections "clobber" each other - last one wins and gets proper event interactions. Why is this the case? Is there an interesting reason why Blockly is designed around this sort of Singleton behavior? Or is it just a development cruft that is relatively unimportant (or perhaps completely unimportant), and so near the bottom of the todo list? If the latter, is there a rough idea of what work would need to be done to isolate Blockly instances?

The reason for this singleton behaviour is a lack of imagination on my part in the first 60 seconds of Blockly's development.  It did not occur to us three years ago that one might want to have multiple instances on a page.  After all, at that time Blockly was intended to be just a new front-end for App Inventor (to replace the Java-based Open Blocks).

Since then it has been recognized that being a singleton was a mistake.  Over the past couple of years most of the singletons (e.g. Trashcan and Toolbox) have been refactored to be instances.  Several singletons remain (e.g. Tooltip and Xml) which are fine to share across multiple Blockly instances.  I think all that remains is to de-singleton Blockly.

The catch is that we cannot break existing Blockly instances.  This code must continue to work:
Blockly.inject(document.getElementById('blocklyDiv'),
     
{toolbox: document.getElementById('toolbox')});
But it would be nice to also allow this to work:
var b1 = new Blockly();
b1
.inject(document.getElementById('blocklyDiv1'),
   
{toolbox: document.getElementById('toolbox1')});
var b2 = new Blockly();
b2
.inject(document.getElementById('blocklyDiv2'),
   
{toolbox: document.getElementById('toolbox2')});
I haven't yet looked at exactly how this can be done.

Spencer Frazier

unread,
Apr 6, 2015, 9:11:44 AM4/6/15
to blo...@googlegroups.com, ro...@neil.fraser.name
Neil,

Any idea where someone enterprising enough to try to change this on their own might begin?

We’re using Blockly in the Virtual World Framework (can be seen here: https://github.com/virtual-world-framework/vwf) with the specific injection code here at line 101: https://github.com/virtual-world-framework/vwf/blob/master/support/client/lib/vwf/view/blockly.js

We use our notion of nodes to allow switching between Blockly toolboxes but we are trying to implement Blockly in a game where a person is able to program multiple mars rovers at the same time to allow them to collaborate.

-Spencer

Neil Fraser

unread,
Apr 7, 2015, 2:05:47 PM4/7/15
to blo...@googlegroups.com
Well, since it is a Fraser (or isotope thereof) asking, I guess this is now a priority.  ;)

A new branch has been created:
https://github.com/google/blockly/tree/multi
This will take a few days.  Success is not guaranteed.

In contrast with the proposed API earlier in this thread, I believe that a more feasible API is:
var w1 = Blockly.inject(document.getElementById('blocklyDiv1'),

   
{toolbox: document.getElementById('toolbox1')});
var w2 = Blockly.inject(document.getElementById('blocklyDiv2'),

   
{toolbox: document.getElementById('toolbox2')});
Where the return value of Blockly.inject is the main workspace of each instance.


--
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.
For more options, visit https://groups.google.com/d/optout.

Spencer Frazier

unread,
Apr 8, 2015, 4:43:03 PM4/8/15
to blo...@googlegroups.com, ro...@neil.fraser.name
Great news :) Thanks! Will keep an eye on the branch

Neil Fraser

unread,
Apr 17, 2015, 9:40:57 PM4/17/15
to blo...@googlegroups.com
Making significant progress on multi-instance Blockly. We are down to
20 instances of Blockly.mainWorkspace and 9 instances of Blockly.svg.
I think these are the last two singletons left.

Not exactly sure what to do with paste using the keyboard (Ctrl-V).
Should Blockly paste into the instance that was last clicked on? Or
should Blockly paste into the instance that the copy originated from?
The advantage of the former is that one can copy and paste between
instances. The advantage of the latter is that there aren't hideous
errors if a block from one Blockly instance does not belong in another
instance. Trade-offs...

Blake

unread,
Apr 17, 2015, 9:44:18 PM4/17/15
to Blockly
Neil,

Can the paste functionality be a setting which has a default and is configurable at Inject time?

Blake

Carlos Pereira

unread,
Apr 18, 2015, 4:31:51 PM4/18/15
to blo...@googlegroups.com
Hi Neil,

While a setting to choose would be ideal, if we could only choose one option I would personally prefer to maintain the copy and paste separated as I would use this feature to have two different kinds of blocks on their own workspace.

Carlos

Neil Fraser

unread,
Apr 18, 2015, 8:49:39 PM4/18/15
to blo...@googlegroups.com
On 18 April 2015 at 13:31, Carlos Pereira <carlos...@gmail.com> wrote:
> While a setting to choose would be ideal, if we could only choose one option
> I would personally prefer to maintain the copy and paste separated as I
> would use this feature to have two different kinds of blocks on their own
> workspace.

Separate instances is definitely safer. I think that's the best
approach to start with. Options can always be added later.

Neil Fraser

unread,
Apr 24, 2015, 11:43:45 PM4/24/15
to blo...@googlegroups.com
Multi-instance Blockly is complete; I'm not aware of any remaining
bugs. However I'm currently visiting Massachusetts, so I'll leave it
in the 'multi' branch until I get back to California on Monday and can
update the documentation.

One immediate advantage of multi-instance Blockly is that the Block
Factory no longer needs an iframe, which fixes the problem of it not
working in Chrome when run off a file:// URL.

Carlos Galarza

unread,
Apr 25, 2015, 12:18:49 PM4/25/15
to blo...@googlegroups.com, ro...@neil.fraser.name
Awesome Neil!. I am very happy with the possibilities this allows , thanks.
Reply all
Reply to author
Forward
0 new messages