Blocks to code

This page describes core/code.js file, which provides the object with Blockly.Workspace with uses Google Blockly to create blocks and generate code.

For documentation about Blockly, see Google’s Blockly documentation.

Blockly workspace

The blockly workspace is stored inside the Code object.

class Code()

Create a namespace for the application.

Code.LANG

type: string

User’s language (e.g. “en”).

Code.LANGUAGE_NAME

Lookup for names of supported languages. Keys should be in ISO 639 format.

Code.LANGUAGE_RTL

List of RTL languages.

Code.TABS_

List of tab names.

Code.auto_mode

If the code should be autogenerated.

Code.workspace

type: Blockly.WorkspaceSvg

Blockly’s main workspace.

Code.bindClick(el, func)

(DEPRECATED) Bind a function to a button’s click event. On touch enabled browsers, ontouchend is treated as equivalent to onclick.

Arguments
  • el (Element|string()) – Button element or ID thereof.

  • func (function()) – Event handler to bind.

Code.changeLanguage()

Save the blocks and reload with a different language.

Code.checkAllGeneratorFunctionsDefined(generator)

Check whether all blocks in use have generator functions.

Arguments
  • generator (Blockly.Generator()) – The generator to use.

Code.discard()

Discard all blocks from the workspace.

Code.generateCode(generate)

Blockly code generator watcher, if auto_mode is true, will generate code when called by the setInterval, if directly called (this != Window), will generate code.

Arguments
  • generate (Blockly.generator()) – If should generate code when called, defaults to Blockly.Python

Returns

string – The generated code

Code.generateXML(workspace)

Generate XML Blockly with BIPES extra information, store in Code.xmlCode

Arguments
  • workspace (Blockly.workspace()) – If should generate code when called, defaults to Code.workspace

Returns

string – The generated XML

Code.getLang()

Get the language of this user from the URL.

Returns

string – User’s language.

Code.getStringParamFromUrl(name, defaultValue)

Extracts a parameter from the URL. If the parameter is absent default_value is returned.

Arguments
  • name (string()) – The name of the parameter.

  • defaultValue (string()) – Value to return if parameter not found.

Returns

string – The parameter value or the default value if not found.

Code.importPrettify()

Load the Prettify CSS and JavaScript.

Code.init()

Initialize Blockly. Called on page load.

Code.initLanguage()

Initialize the page language.

Code.isRtl()

Is the current language (Code.LANG) an RTL language?

Returns

boolean – True if RTL, false if LTR.

Code.loadBlocks(defaultXml)

Load blocks saved on App Engine Storage or in session/local storage.

Arguments
  • defaultXml (string()) – Text representation of default blocks.

Code.renderContent()

Populate the currently selected pane with content generated from the blocks.

Code.tabClick(clickedName)

Switch the visible pane when a tab is clicked.

Arguments
  • clickedName (string()) – Name of tab clicked.

Code.toDOM(code, prettyPrintType, domTarget)

Attempt to generate the code and display it in the UI, pretty printed.

Arguments
  • code (string()) – The code generated by Blockly.

  • prettyPrintType (string()) – The file type key for the pretty printer.

  • domTarget (string()) – The id for the dom element to render the code.