/** * @license * Visual Blocks Editor * * Copyright 2018 Google Inc. * https://developers.google.com/blockly/ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @fileoverview Block creation events fired as a result of actions in Blockly's * editor. * @author fraser@google.com (Neil Fraser) */ 'use strict'; goog.provide('Blockly.Events.BlockCreate'); goog.provide('Blockly.Events.Create'); // Deprecated. goog.require('Blockly.Events'); goog.require('Blockly.Events.Abstract'); goog.require('goog.array'); goog.require('goog.math.Coordinate'); /** * Class for a block creation event. * @param {Blockly.Block} block The created block. Null for a blank event. * @extends {Blockly.Events.Abstract} * @constructor */ Blockly.Events.Create = function(block) { if (!block) { return; // Blank event to be populated by fromJson. } Blockly.Events.Create.superClass_.constructor.call(this, block); if (block.workspace.rendered) { this.xml = Blockly.Xml.blockToDomWithXY(block); } else { this.xml = Blockly.Xml.blockToDom(block); } this.ids = Blockly.Events.getDescendantIds_(block); }; goog.inherits(Blockly.Events.Create, Blockly.Events.Abstract); /** * Class for a block creation event. * @param {Blockly.Block} block The created block. Null for a blank event. * @extends {Blockly.Events.Abstract} * @constructor */ Blockly.Events.BlockCreate = Blockly.Events.Create; /** * Type of this event. * @type {string} */ Blockly.Events.Create.prototype.type = Blockly.Events.CREATE; /** * Encode the event as JSON. * @return {!Object} JSON representation. */ Blockly.Events.Create.prototype.toJson = function() { var json = Blockly.Events.Create.superClass_.toJson.call(this); json['xml'] = Blockly.Xml.domToText(this.xml); json['ids'] = this.ids; return json; }; /** * Decode the JSON event. * @param {!Object} json JSON representation. */ Blockly.Events.Create.prototype.fromJson = function(json) { Blockly.Events.Create.superClass_.fromJson.call(this, json); this.xml = Blockly.Xml.textToDom('' + json['xml'] + '').firstChild; this.ids = json['ids']; }; /** * Run a creation event. * @param {boolean} forward True if run forward, false if run backward (undo). */ Blockly.Events.Create.prototype.run = function(forward) { var workspace = this.getEventWorkspace_(); if (forward) { var xml = goog.dom.createDom('xml'); xml.appendChild(this.xml); Blockly.Xml.domToWorkspace(xml, workspace); } else { for (var i = 0, id; id = this.ids[i]; i++) { var block = workspace.getBlockById(id); if (block) { block.dispose(false, false); } else if (id == this.blockId) { // Only complain about root-level block. console.warn("Can't uncreate non-existent block: " + id); } } } };