Registry Mirror
Skip to content

Undeletable blocks that are children of deletable blocks get deleted from the workspace #2158

Open

Description

@phoenixchinar

Problem statement

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.

  <variables>
    <variable type="" id="iN-Y8-{m3,;cH?My|q0L">abc</variable>
  </variables>
  <block type="controls_repeat_ext" id="ImpkNv4%FbaaC67hXv0$" x="113" y="63">
    <value name="TIMES">
      <shadow type="math_number" id="vaA4a63b7^N]i`Jly;m(">
        <field name="NUM">1</field>
      </shadow>
    </value>
    <statement name="DO">
      <block type="variables_set" id="E_l9?u6=v-hPZ]+LFiU_" deletable="false">
        <field name="VAR" id="iN-Y8-{m3,;cH?My|q0L" variabletype="">abc</field>
        <value name="VALUE">
          <block type="math_number" id="m{w]XMW[f;8-s!Bh{Tmy">
            <field name="NUM">123</field>
          </block>
        </value>
      </block>
    </statement>
  </block>
</xml>

Activity

  1. added
    help wanted
    on Jan 9, 2019
  2. BeksOmega commented on Feb 7, 2019

    @BeksOmega
    Contributor

    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.

    undeletabledeletable

    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!

  3. added this to the Bug Bash Backlog milestone on Jan 25, 2021
  4. removed
    help wanted
    on Nov 1, 2023
  5. added theissue type on Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    • Status
      Todo

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions