Error "Property 'registerButtonCallback' does not exist on type 'Workspace'"

104 views
Skip to first unread message

Keino TT

unread,
Nov 12, 2019, 1:12:18 AM11/12/19
to Blockly
Hey. I am trying to make typed variables. In the documentation I found an example with the type Panda. I want to do the same. Several buttons for different types. But this error occured: 
Error "Property 'registerButtonCallback' does not exist on type 'Workspace'" 
    function myUpdateFunction(event) {
      var code = (Blockly as any).JavaScript.workspaceToCode(workspace);
      console.log(code);
    }
    workspace.addChangeListener(myUpdateFunction);
    workspace.registerButtonCallback('myFirstButtonPressed', function(button) {
      Blockly.Variables.createVariable(button.getTargetWorkspace(), null, 'panda');
    });



Screenshot_8.png

Please help. 

Keino TT

unread,
Nov 12, 2019, 1:30:51 AM11/12/19
to Blockly
if I write 
import './blocks/var';
...
    (workspace as any).registerButtonCallback('myFirstButtonPressed', function(button) {
      Blockly.Variables.createVariable(button.getTargetWorkspace(), null, 'panda');
    });

Screenshot_9.png


var.ts 
import * as Blockly from 'blockly';
(Blockly as any).Blocks.variables_get_panda = {
    init(){
        this.jsonInit({
            message0: ' %1', 
            args0: [
                {
                  type: "field_variable",
                  name: "VAR",
                  variable: "%{BKY_VARIABLES_DEFAULT_NAME}",
                  variableTypes: ["Panda"]    // Specifies what types to put in the dropdown
                }
              ],
              output: "Panda",    // Returns a value of "Panda"
              tooltip: "",
              helpUrl: ""
        })
    }
},
(Blockly as any).Blocks.variables_set_panda = {
    init(){
        this.jsonInit({
            message0: '%{BKY_VARIABLES_SET}', 
            args0: [
                {
                  type: "field_variable",
                  name: "VAR",
                  variable: "%{BKY_VARIABLES_DEFAULT_NAME}",
                  variableTypes: ["Panda"]    // Specifies what types to put in the dropdown
                },
                {
                    type: "input_value",
                    name: "VALUE",
                    check: "Panda"    // Checks that the input value is of type "Panda"
                  }
              ],
              previousStatement: null,
              nextStatement: null,
              tooltip: "",
              helpUrl: ""
        })
    }
};

 The variable is created but not displayed:

Screenshot_10.png







Beka Westberg

unread,
Nov 12, 2019, 9:43:55 AM11/12/19
to Blockly
Hello,

So I think to fix the problem with `.registerButtonCallback` you need to cast the workspace var to a WorkspaceSvg. Right now it probably things you're working with the headless version (Workspace) which doesn't have the `.registerButtonCallback` method.

As for your second problem, could you post a stack trace for the error? I'm not sure why it would be throwing that unless you were indeed creating a duplicate variable.

I hope that helps!
--Beka
Message has been deleted

Keino TT

unread,
Nov 13, 2019, 12:24:58 AM11/13/19
to Blockly

How I can cast my workspace to a WorkspaceSvg? 

Screenshot_11.png



вторник, 12 ноября 2019 г., 17:43:55 UTC+3 пользователь Beka Westberg написал:

Beka Westberg

unread,
Nov 13, 2019, 10:18:00 AM11/13/19
to Blockly
Hello,

Honestly I don't know much about Javascript or Typescript, I'm just pretty familiar with Blockly. I think you'd need to do something like
let workspace = <Blockly.WorkspaceSvg> Blockly.inject(blocklyDiv, { /* myOptions */ });

but I'm not sure :/ Someone else here might be able to give you a more definitive answer.

--Beka

Sam El-Husseini

unread,
Nov 13, 2019, 10:48:40 AM11/13/19
to Blockly
Hey Keino, 

Casting is the correct fix for the time being, you can do so like this:

    const workspace = (Blockly.inject(blocklyDiv, {

    }) as Blockly.WorkspaceSvg);
workspace.registerToolboxCategoryCallback(...);

or 

const workspace = Blockly.inject(blocklyDiv, {

    });
const workspaceSvg = (workspace as Blockly.WorkspaceSvg);
workspaceSvg.registerToolboxCategoryCallback(...);

The next release will have this fixed as Blockly.inject should return the type of Blockly.WorkspaceSvg by default.

Cheers,
Sam

Yilin Xia

unread,
Nov 18, 2019, 9:24:59 PM11/18/19
to Blockly
Hi Keino,

May I ask have you ever figured out how to customized blocks and add code generators?  If possible, can we contact through email? 

Best,
Yilin
Reply all
Reply to author
Forward
0 new messages