How to use blockly - 10 common examples

To help you get started, we’ve selected a few blockly examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github miguel76 / SparqlBlocks / src / shims / shadowMorph.js View on Github external
* Set whether this block is a shadow block or not.
 * @param {boolean} shadow True if a shadow.
 */
Blockly.Block.prototype.setShadow = function(shadow) {
  if (this.isShadow_ != shadow) {
    Blockly.Events.fire(new Blockly.Events.Change(
      this, 'shadow', null, this.isShadow_, shadow));
    this.isShadow_ = shadow;
  }
};

/**
 * Run a change event.
 * @param {boolean} forward True if run forward, false if run backward (undo).
 */
Blockly.Events.Change.prototype.run = function(forward) {
  var workspace = Blockly.Workspace.getById(this.workspaceId);
  var block = workspace.getBlockById(this.blockId);
  if (!block) {
    console.warn("Can't change non-existant block: " + this.blockId);
    return;
  }
  if (block.mutator) {
    // Close the mutator (if open) since we don't want to update it.
    block.mutator.setVisible(false);
  }
  var value = forward ? this.newValue : this.oldValue;
  switch (this.element) {
    case 'field':
      var field = block.getField(this.name);
      if (field) {
        // Run the validator for any side-effects it may have.
github binary-com / binary-bot / src / botPage / view / view.js View on Github external
var run = function run() {
	// Generate JavaScript code and run it.
	try {
		window.LoopTrap = 1000;
		blockly.JavaScript.INFINITE_LOOP_TRAP =
			'if (--window.LoopTrap == 0) throw "Infinite loop.";\n';
		var code = blockly.JavaScript.workspaceToCode(blockly.mainWorkspace);
		blockly.JavaScript.INFINITE_LOOP_TRAP = null;
		var EVAL_BLOCKLY_CODE = eval;
		EVAL_BLOCKLY_CODE(code);
		$('#summaryPanel')
			.show();
		$('#stopButton')
			.bind('click', stop);
	} catch (e) {
		botUtils.showError(e);
	}
};
github binary-com / binary-bot / src / newdesign / botPage / view / blockly / code / finish / read_details.js View on Github external
var blockly = require('blockly');
blockly.JavaScript.read_details = function(block) {
  var detail_index = block.getFieldValue('DETAIL_INDEX');
  // TODO: Assemble JavaScript into code variable.
  var code = '((details instanceof Array && details.length === Bot.config.lists.DETAILS.length) ? details[' + ( parseInt(detail_index.trim()) - 1 ) + '] : \'\' )';
  return [code, blockly.JavaScript.ORDER_ATOMIC];
};
github binary-com / binary-bot / src / ui.js View on Github external
var run = function run() {
	// Generate JavaScript code and run it.
	try {
		window.LoopTrap = 1000;
		blockly.JavaScript.INFINITE_LOOP_TRAP =
			'if (--window.LoopTrap == 0) throw "Infinite loop.";\n';
		var code = blockly.JavaScript.workspaceToCode(blockly.mainWorkspace);
		blockly.JavaScript.INFINITE_LOOP_TRAP = null;
		var EVAL_BLOCKLY_CODE = eval;
		EVAL_BLOCKLY_CODE(code);
		$('#stopButton')
			.text('Stop');
		$('#runButton')
			.text('Restart');
		$('#summaryPanel')
			.show();
		$('#stopButton')
			.unbind('click', reset);
		$('#stopButton')
			.bind('click', stop);
	} catch (e) {
		utils.showError(e);
	}
github Paol-imi / blockly-gamepad / src / gamepad.js View on Github external
}
        Blockly.JavaScript.INFINITE_LOOP_TRAP && (c = Blockly.JavaScript.INFINITE_LOOP_TRAP.replace(/%1/g, "'" + a.id + "'") +
            c);
        (d = Blockly.JavaScript.valueToCode(a, "RETURN", Blockly.JavaScript.ORDER_NONE) || "") && (d = Blockly.JavaScript.INDENT + "return " + d + ";\n");
        for (var e = [], f = 0; f < a.arguments_.length; f++) e[f] = Blockly.JavaScript.variableDB_.getName(a.arguments_[f], Blockly.Variables.NAME_TYPE);

        c = "async function " + b + "(" + e.join(", ") + ") {\n" +
            c + d + "\n" +
            "}";
        c = Blockly.JavaScript.scrub_(a, c);
        Blockly.JavaScript.definitions_["%" + b] = c;

        return null;
    }
    Blockly.JavaScript.procedures_defnoreturn = Blockly.JavaScript.procedures_defreturn;
    Blockly.JavaScript.procedures_callreturn = function (a) {
        for (var b = Blockly.JavaScript.variableDB_.getName(a.getFieldValue("NAME"), Blockly.Procedures.NAME_TYPE), c = [], d = 0; d < a.arguments_.length; d++) c[d] = Blockly.JavaScript.valueToCode(a, "ARG" + d, Blockly.JavaScript.ORDER_COMMA) || "null";
        return ["await " + b + "(" + c.join(", ") + ")", Blockly.JavaScript.ORDER_FUNCTION_CALL]
    }
    Blockly.JavaScript.procedures_callnoreturn = function (a) {
        for (var b = Blockly.JavaScript.variableDB_.getName(a.getFieldValue("NAME"), Blockly.Procedures.NAME_TYPE), c = [], d = 0; d < a.arguments_.length; d++) c[d] = Blockly.JavaScript.valueToCode(a, "ARG" + d, Blockly.JavaScript.ORDER_COMMA) || "null";
        return "await " + b + "(" + c.join(", ") + ");\n"
    }

    Blockly.JavaScript[Gamepad['BLOCKS']['START']] = function () {
        return '';
    }

    // set reserved words
    Blockly.JavaScript.addReservedWords('Blockly,CONTEXT,worker,code,reject,resolve,f');

    // define the start block
github miguel76 / SparqlBlocks / src / core / field_table.js View on Github external
return function(e) {
    if (Blockly.isRightButton(e)) {
      // Right-click.  Don't create a block, let the context menu show.
      return;
    }
    if (originBlock.disabled) {
      // Beyond capacity.
      return;
    }
    Blockly.Events.disable();
    // Create the new block by cloning the block in the flyout (via XML).
    var xml = Blockly.Xml.blockToDom(originBlock);
    var block = Blockly.Xml.domToBlock(xml, workspace);
    block.setEditable(true);
    block.setCollapsed(false);
    // Place it in the same spot as the flyout copy.
    var svgRootOld = originBlock.getSvgRoot();
    if (!svgRootOld) {
      throw 'originBlock is not rendered.';
    }

    var sourceXY = sourceBlock.getRelativeToSurfaceXY();
    var fieldRelativeXY = Blockly.getRelativeXY_(svgRoot);
    var originBlockXY = originBlock.getRelativeToSurfaceXY();
    block.moveBy(
        originBlockXY.x + sourceXY.x + fieldRelativeXY.x + FieldTable.translateX_,
        originBlockXY.y + sourceXY.y + fieldRelativeXY.y + FieldTable.translateY_);
    Blockly.Events.enable();
    if (Blockly.Events.isEnabled()) {
github miguel76 / SparqlBlocks / src / core / field_table.js View on Github external
return function(e) {
    if (Blockly.isRightButton(e)) {
      // Right-click.  Don't create a block, let the context menu show.
      return;
    }
    if (originBlock.disabled) {
      // Beyond capacity.
      return;
    }
    Blockly.Events.disable();
    // Create the new block by cloning the block in the flyout (via XML).
    var xml = Blockly.Xml.blockToDom(originBlock);
    var block = Blockly.Xml.domToBlock(xml, workspace);
    block.setEditable(true);
    block.setCollapsed(false);
    // Place it in the same spot as the flyout copy.
    var svgRootOld = originBlock.getSvgRoot();
    if (!svgRootOld) {
      throw 'originBlock is not rendered.';
    }

    var sourceXY = sourceBlock.getRelativeToSurfaceXY();
    var fieldRelativeXY = Blockly.getRelativeXY_(svgRoot);
    var originBlockXY = originBlock.getRelativeToSurfaceXY();
    block.moveBy(
        originBlockXY.x + sourceXY.x + fieldRelativeXY.x + FieldTable.translateX_,
        originBlockXY.y + sourceXY.y + fieldRelativeXY.y + FieldTable.translateY_);
    Blockly.Events.enable();
github miguel76 / SparqlBlocks / src / core / jsonToBlocks.js View on Github external
valueBlock.showTooltip = function() {
      var xmlBlock = Blockly.Xml.blockToDom_(valueBlock);
      xmlBlock.removeAttribute("editable");
      xmlBlock.removeAttribute("movable");
      xmlBlock.removeAttribute("deletable");
      duplicateBlock = Blockly.Xml.domToBlock(xmlBlock, workspace);
      duplicateBlock.isInFlyout = true;
      duplicateBlock.hideTooltip = function() {
        if (duplicateBlock) {
          duplicateBlock.dispose();
        }
      };
      Blockly.Tooltip.poisonedElement_ = Blockly.Tooltip.element_ = duplicateBlock;
      var mousedownEvent = Blockly.bindEvent_(
          duplicateBlock.getSvgRoot(),
          'mousedown', null,
          function(e) {
            if (Blockly.isRightButton(e)) {
              // Right-click.  Don't create a block, let the context menu show.
              return;
            }
            if (duplicateBlock.disabled) {
github Paol-imi / blockly-gamepad / src / gamepad.js View on Github external
this.workspace.addChangeListener((event) => {
            if (event.type == Blockly.Events.BLOCK_MOVE) {
                // update codinge state and remove block highlight
                if (!this.isCoding) this.removeHighlight();
                this.isCoding = true;
            }
        });
github miguel76 / SparqlBlocks / src / core / field_table.js View on Github external
return function(e) {
    if (Blockly.isRightButton(e)) {
      // Right-click.  Don't create a block, let the context menu show.
      return;
    }
    if (originBlock.disabled) {
      // Beyond capacity.
      return;
    }
    Blockly.Events.disable();
    // Create the new block by cloning the block in the flyout (via XML).
    var xml = Blockly.Xml.blockToDom(originBlock);
    var block = Blockly.Xml.domToBlock(xml, workspace);
    block.setEditable(true);
    block.setCollapsed(false);
    // Place it in the same spot as the flyout copy.
    var svgRootOld = originBlock.getSvgRoot();
    if (!svgRootOld) {
      throw 'originBlock is not rendered.';
    }

    var sourceXY = sourceBlock.getRelativeToSurfaceXY();
    var fieldRelativeXY = Blockly.getRelativeXY_(svgRoot);
    var originBlockXY = originBlock.getRelativeToSurfaceXY();
    block.moveBy(
        originBlockXY.x + sourceXY.x + fieldRelativeXY.x + FieldTable.translateX_,