Displaying specific blocks in mutator

16 views
Skip to first unread message

Mia

unread,
Jul 2, 2024, 11:19:52 AM (13 hours ago) Jul 2
to Blockly

In the current code, when I click the mutatorIcon, the 'selectedApp' block is displayed, but the 'app_container' block is also displayed.

I want only the 'selectedApp' block to be displayed when the mutator is opened. Is this possible?

Setting the return of the decompose method to null causes an error.

Here is the code I am working on:

Blockly.Blocks["app"] = {
    init: function () {
        this.setColour(15);
        this.appendDummyInput().appendField(
            new Blockly.FieldDropdown(function () {
                return get_app();
            }),
            "APP_LIST"
        );
        this.setPreviousStatement(true, "app_pre");
        this.setNextStatement(true);
        this.setMutator(new Blockly.icons.MutatorIcon([], this));
    },
    mutationToDom: function () {
        var container = Blockly.utils.xml.createElement("mutation");
        return container;
    },
    domToMutation: function (xmlElement) {},
    decompose: function (workspace) {
        var containerBlock = workspace.newBlock("app_container");
        containerBlock.initSvg();
        var selectedApp = this.getFieldValue("APP_LIST");
        if (selectedApp) {
            var xml = Blockly.utils.xml.textToDom(selectedApp);
            Blockly.Xml.domToWorkspace(xml, workspace);
        }
        return containerBlock;
    },
    compose: function (containerBlock) {},
};

Blockly.Blocks["app_container"] = {
    init: function () {
        this.appendDummyInput();
        this.appendStatementInput("APPS").setCheck(null);
        this.setColour(15);
    },
};

Beka Westberg

unread,
Jul 2, 2024, 11:26:56 AM (13 hours ago) Jul 2
to blo...@googlegroups.com
Hello,

You need to return some block from decompose, but it doesn't matter what it actually is. So yeah you can definitely remove the following lines:

```
var containerBlock = workspace.newBlock("app_container");
containerBlock.initSvg();
```

And just return the block deserialized in `domToWorkspace` instead.

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/376e6578-9207-4491-88b4-bb863b9dd5c1n%40googlegroups.com.

Mia

unread,
Jul 2, 2024, 7:41:56 PM (5 hours ago) Jul 2
to Blockly

Hello Beka,

Thank you for your suggestion. I removed the following lines as you advised:

var containerBlock = workspace.newBlock("app_container");
containerBlock.initSvg();

My updated decompose method looks like this:

    decompose: function (workspace) {
        var selectedApp = this.getFieldValue("APP_LIST");
        if (selectedApp) {
            var xml = Blockly.utils.xml.textToDom(selectedApp);
            var block = Blockly.Xml.domToWorkspace(xml, workspace);
            return block;
        }
    },


However, I encountered the following error:

Uncaught TypeError: this.rootBlock.getDescendants is not a function or its return value is not iterable

How should I correctly return the block to avoid this error?


2024年7月3日水曜日 0:26:56 UTC+9 bwes...@google.com:
Reply all
Reply to author
Forward
0 new messages