Mutation with default value input block

180 views
Skip to first unread message

Omid Abdollahi

unread,
Apr 21, 2021, 10:11:32 AM4/21/21
to Blockly
pic 1 is default behavior of create text block
but I want a text block is created as soon as the user dragged the item in container. (pic 2)

blockly_mutation.png

I tried to change the original source code (please see extra section )
the problem is now every time i reposition items, it duplicates text block!
It seems on every compose method call every thing should be deleted and created again. anyway I couldn't fix it. Is there anything else I can check of read? Thanks in advance.


Blockly.Constants.Text.TEXT_JOIN_MUTATOR_MIXIN = {
  /**
   * Modify this block to have the correct number of inputs.
   * @private
   * @this {Blockly.Block}
   */
  updateShape_: function() {
    if (this.itemCount_ && this.getInput('EMPTY')) {
      this.removeInput('EMPTY');
    } else if (!this.itemCount_ && !this.getInput('EMPTY')) {
      this.appendDummyInput('EMPTY')
          .appendField(this.newQuote_(true))
          .appendField(this.newQuote_(false));
    }
    // Add new inputs.
    for (var i = 0; i < this.itemCount_; i++) {
      if (!this.getInput('ADD' + i)) {
        var input = this.appendValueInput('ADD' + i).setAlign(Blockly.ALIGN_RIGHT);
//__________ extra section start ___________
var parentConnection = input.connection;
if (!parentConnection.targetBlock()) {
  var child = this.workspace.newBlock("text");
  parentConnection.connect(child.outputConnection);
  child.initSvg();
  child.render();
}
//__________ extra section end ___________
        if (i == 0) {
          input.appendField(Blockly.Msg['TEXT_JOIN_TITLE_CREATEWITH']);
        }
      }
    }
    // Remove deleted inputs.
    while (this.getInput('ADD' + i)) {
      this.removeInput('ADD' + i);
      i++;
    }
  }
};

Beka Westberg

unread,
Apr 21, 2021, 9:01:13 PM4/21/21
to blo...@googlegroups.com
Hello,

I think that the general recommendation in these situations it to expect all of state of your block to be lost, and to be prepared to rebuild it. It sounds like what's happening is when updateShape_ is called, any text the user might have entered into your shadows is getting lost? If that is the case, you'll want to save the values of the shadow blocks' fields into some sort of collection, and then restore them in updateShape_ or compose. The best place to do the saving is probably in the saveConnections function. If you follow the same pattern of associating values with the blocks in the mutator, that should allow the values to be easily rearranged =)

I hope that helps! If you have any further questions please reply!
--Beka

--
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 on the web visit https://groups.google.com/d/msgid/blockly/4a28168e-7372-419c-8111-38b886c527a3n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages