Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

field_label modified in dynamic category resets when dragged onto workspace #1954

Closed
Marclev78 opened this issue Jun 28, 2018 · 2 comments
Closed
Labels

Comments

@Marclev78
Copy link

Marclev78 commented Jun 28, 2018

If I modify a label in a dynamic catetory, it resets when I drag it onto the workspace.

Given a block with a label like this:

  blocks["event_rule"] = {
    init: function() {
      this.jsonInit({
        message0: "%1 events",
        message1: "Rules: %1",
        args0: [
          {
            type: "field_label",
            name: "eventName"
          }
        ],
       .....

And a dynamic category with a block like:

const blocks = [
  `
    <block type="event_rule">
      <field name="eventName">${activeEvent}</field>
    </block>
  `
];

return blocks.map(block => `<xml>${block}</xml>`).map(block => Blockly.Xml.textToDom(block).firstChild);

The flyout correctly shows the dynamically set label value.

But the moment I drag the block from the flyout onto the workspace, it resets to undefined.

If I change the block type to field_input, it works fine. This is undesirable though, as I don't want it to appear as an input field, it's a dynamic part of the block description that I'm trying to change whenever the flyout is opened.

The only workaround I've found is to use a change listener to override the field value.

@AnmAtAnm
Copy link
Contributor

AnmAtAnm commented Jun 28, 2018

Which block reverts to "undefined"? The one in the flyout or the copy that has been dragged out? I'm guessing you mean the latter.

Field labels are not serialized, so they value you assign is not preserved during save/load, including the copy process during dragging from the toolbox.

If you want to preserve/serialize this data, you will need to add mutator functions mutationToDom and domToMutation, where the latter will update the FieldLabel.

Web Blockly mutators are covered here.

@rachel-fenichel
Copy link
Collaborator

rachel-fenichel commented Jun 28, 2018

Right, fields have an EDITABLE property that is false on labels. It controls whether they are serialized.

In scratch-blocks we added a new type of field called field_label_serializable that does what you want, but that also required changes in the base field class. In the short term, domToMutation and mutationToDom are probably easier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants