Re: is there a way to only detect change on a workspace from the user, but not from javascript?

122 views
Skip to first unread message

Andrew n marshall

unread,
Nov 6, 2018, 12:22:53 PM11/6/18
to blo...@googlegroups.com

You can wrap your domToWorkspace() call between a Blockly.Events.disable() and .enable() call.

It is also common to .clearPendingUndo() after loading a new workspace, since the prior undo stack no longer applies.




On Tue, Nov 6, 2018 at 7:55 AM Gregoire Fessard <ent...@gmail.com> wrote:
Hello,

I am making a website using Blockly, i need to log event, which i am doing with : 
addChangeListener(mirrorEvent);
However I sometime need to change the xml of the workspace, with :
Blockly.Xml.domToWorkspace(Blockly.Xml.textToDom(xml),workspace);

When i change the xml of the workspace from javascript, it get detected by the listener and i haven't found a way to differentiate event from the user(moving a block,ect) and event from javascript( using Blockly.Xml.domToWorkspace(...) ).
I tried to add a removeChangeListener(mirrorEvent) right before modifying the workspace with javascript but it didn't work.

Is there a way to only detect the change when the user is modifying the workspace, and not when it is modified from javascript? Thanks !

--
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.

Neil Fraser

unread,
Nov 6, 2018, 1:39:09 PM11/6/18
to blo...@googlegroups.com
I did some digging to see if this was a bug, however it isn't.  The issue with your approach is that events are fired asynchronously.

To recreate on the console of the playground, create any block from the toolbox, then execute:
  function listen(e) {console.log(e);}
  var xml = Blockly.Xml.workspaceToDom(workspace);
Delete the block from the workspace, then execute (one line):
  workspace.addChangeListener(listen); Blockly.Xml.domToWorkspace(xml, workspace);
A create and a move event will be logged, as expected.  Now delete the block from the workspace and execute (one line):
  workspace.removeChangeListener(listen); Blockly.Xml.domToWorkspace(xml, workspace);
No events are logged, as expected.  Now delete the block from the workspace and execute (one line):
  workspace.removeChangeListener(listen); Blockly.Xml.domToWorkspace(xml, workspace); workspace.addChangeListener(listen);
Two events are logged, which is rather unexpected.

Andrew's approach is the correct one.

On Tue, 6 Nov 2018 at 07:55, Gregoire Fessard <ent...@gmail.com> wrote:
Hello,

I am making a website using Blockly, i need to log event, which i am doing with : 
addChangeListener(mirrorEvent);
However I sometime need to change the xml of the workspace, with :
Blockly.Xml.domToWorkspace(Blockly.Xml.textToDom(xml),workspace);

When i change the xml of the workspace from javascript, it get detected by the listener and i haven't found a way to differentiate event from the user(moving a block,ect) and event from javascript( using Blockly.Xml.domToWorkspace(...) ).
I tried to add a removeChangeListener(mirrorEvent) right before modifying the workspace with javascript but it didn't work.

Is there a way to only detect the change when the user is modifying the workspace, and not when it is modified from javascript? Thanks !

--
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.


Reply all
Reply to author
Forward
0 new messages