You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Undeletable blocks that are children of deletable blocks get deleted from the workspace #2158
I am working on UI where the user needs to build logic using blockly to set's few system defined variables. I generate the set blocks when the workspace is loaded, and set the deletable as false. When the user saves, now all I have to check is if there is only a single top level block, and all inputs are filled for me to have the correct logic. However if the user puts the set block inside an if or other enclosing block, the deletion of the enclosing block ends up deleting the set block too.
Expected behaviour:
Undeletable blocks should stay on the workspace even if the parent gets deleted.
Observed behaviour:
Delete of parent of undeletable block ends up deleting the undeletable block as well.
Example:
Deleting the repeat block ends up deleting the set variable block.
So I gave this my very best shot. Getting it to skip undeleteable blocks was not too too difficult, but when you skip them you run into some hidden troubles. (Skip to Trouble 4 if you're short on time)
Trouble 1 Mutators
The top block in a mutator is not deletable (for obvious reasons) but when you close the mutator, you want these blocks to be disposed. Solution: Add an opt_forceDelete parameter to the block.js and block_svg.js dispose functions.
Trouble 2 Undo Part 1
A delete event's oldXML is created by the top block. This means that when you undo the delete event, you get an extra undeletable block. Solution: Add xml filtering to remove any undeletable blocks from the oldXML before we send it off to Blockly.Xml.
Trouble 3 Undo Part 2
When you undo an action, you expect the workspace to look like it had before you did the action. This was not happening. This is because when you hit undo, the blocks would be recreated, but the undeletable block would not be reconnected because no move event had been fired to "save" the connecting/disconnecting. Solution: Fire a move event. This was a bit tricky though, since we could be deep in a stack of Event.disable() calls when we come to this undeletable block that needs to be disconnected, so I had to add a function that would let me "break through" the layers of disabled-ness to fire an event anyway.
Trouble 4 Redo
When the top block has dispose called on it creates a delete event, and saves it. When we get to the undeletable block it creates a move event, and fires it. Then once we're done with the tree of dispose calls the top block fires its delete event. So our undo stack looks like so:
Newer: Delete
Older: Move
When we hit undo it un-deletes the blocks first, and then reconnects the blocks (Yay). Now our redo stack looks like so:
Newer: Move
Older: Delete
So when we hit redo it re-moves the blocks, disconnecting them, and then triggers the dispose on the top block of the Delete event. This means that any next blocks that were connected to the un-deletable block don't get deleted, even though they should be.
And this is where I got stuck. I could create a sort of move event that only works in on undo, but that seemed even more hacky and single purpose than thing I added for Trouble #3 (to get the move event to be fired in the first place).
You can check out all my changes for this here (beware, it's still a bit dirty with logs and things).
If you have any recommendations I would still like to fix this!
Activity
BeksOmega commentedon Feb 7, 2019
Hello!
So I gave this my very best shot. Getting it to skip undeleteable blocks was not too too difficult, but when you skip them you run into some hidden troubles. (Skip to Trouble 4 if you're short on time)
Trouble 1 Mutators
The top block in a mutator is not deletable (for obvious reasons) but when you close the mutator, you want these blocks to be disposed. Solution: Add an opt_forceDelete parameter to the block.js and block_svg.js dispose functions.
Trouble 2 Undo Part 1
A delete event's oldXML is created by the top block. This means that when you undo the delete event, you get an extra undeletable block. Solution: Add xml filtering to remove any undeletable blocks from the oldXML before we send it off to Blockly.Xml.
Trouble 3 Undo Part 2
When you undo an action, you expect the workspace to look like it had before you did the action. This was not happening. This is because when you hit undo, the blocks would be recreated, but the undeletable block would not be reconnected because no move event had been fired to "save" the connecting/disconnecting. Solution: Fire a move event. This was a bit tricky though, since we could be deep in a stack of Event.disable() calls when we come to this undeletable block that needs to be disconnected, so I had to add a function that would let me "break through" the layers of disabled-ness to fire an event anyway.
Trouble 4 Redo
When the top block has dispose called on it creates a delete event, and saves it. When we get to the undeletable block it creates a move event, and fires it. Then once we're done with the tree of dispose calls the top block fires its delete event. So our undo stack looks like so:
Newer: Delete
Older: Move
When we hit undo it un-deletes the blocks first, and then reconnects the blocks (Yay). Now our redo stack looks like so:
Newer: Move
Older: Delete
So when we hit redo it re-moves the blocks, disconnecting them, and then triggers the dispose on the top block of the Delete event. This means that any next blocks that were connected to the un-deletable block don't get deleted, even though they should be.
And this is where I got stuck. I could create a sort of move event that only works in on undo, but that seemed even more hacky and single purpose than thing I added for Trouble #3 (to get the move event to be fired in the first place).
You can check out all my changes for this here (beware, it's still a bit dirty with logs and things).
If you have any recommendations I would still like to fix this!