Blockly: Headless Workspaces Require a Theme to Be Defined

Created on 13 Jun 2019  ·  3Comments  ·  Source: google/blockly

Problem statement


Headless workspaces require a theme to be defined, even though they are not rendered.

Expected Behavior


I can create themed blocks on a headless workspace.

Actual Behavior


I recieve an error.

Steps to Reproduce

  1. Disable the changeTheme function of the playground.
  2. Remove all of the code from the start function and replace it with the below:
  var altWorkspace = new Blockly.Workspace();
  // JSON block def.
  var block = new Blockly.Block(altWorkspace, 'controls_if');
  // JS block def.
  var block = new Blockly.Block(altWorkspace, 'text_getSubstring');

Stack Traces

block.js:1520 Block "controls_if": Style does not exist:  logic_blocks
  Blockly.Block.jsonInitStyle_ @ block.js:1520
  Blockly.Block.jsonInit @ block.js:1426
  (anonymous) @ blockly.js:414
  Blockly.Block @ block.js:181
  start @ playground.html:146
  onload @ playground.html:429

block.js:975 Uncaught Error: Trying to set block style to text_blocks before theme was defined via Blockly.setTheme().
    at Blockly.Block.setStyle (block.js:975)
    at Blockly.Block.init (text.js:224)
    at new Blockly.Block (block.js:181)
    at start (playground.html:147)
    at onload (playground.html:429)

Additional Information


Just noticed this when I was writing some unit tests, and thought I should send it in.

Oh I also noticed that colour functions (some of them my own additions) are defined on the block rather than the rendered block, not sure if this is intensional.

themes bug

Most helpful comment

The following seems to work:

var Blockly = require('node-blockly/browser');
Blockly.setTheme(Blockly.Themes.Classic);

All 3 comments

I think this and #2540 are related

I have the same problem. Is there a workaround?

code:

var Blockly = require('node-blockly/browser');
var xml = Blockly.Xml.textToDom(xml_text);
var workspace = new Blockly.Workspace();
Blockly.Xml.domToWorkspace(xml, workspace);
var code = Blockly.JavaScript.workspaceToCode(workspace);

error:

Error: Trying to set block style to procedure_blocks before theme was defined via Blockly.setTheme().
    at module.exports.Blockly.Block.setStyle (blockly_compressed_browser.js:1460)
    at module.exports.Blockly.Block.init (blocks_compressed_browser.js:106)
    at new module.exports.Blockly.Block (blockly_compressed_browser.js:1440)
    at module.exports.Blockly.Workspace.newBlock (blockly_compressed_browser.js:1029)
    at Object.module.exports.Blockly.Xml.domToBlockHeadless_ (blockly_compressed_browser.js:993)
    at Object.module.exports.Blockly.Xml.domToBlock (blockly_compressed_browser.js:990)
    at Object.module.exports.Blockly.Xml.domToWorkspace (blockly_compressed_browser.js:986)

The following seems to work:

var Blockly = require('node-blockly/browser');
Blockly.setTheme(Blockly.Themes.Classic);
Was this page helpful?
0 / 5 - 0 ratings