mirror of
https://github.com/google/blockly.git
synced 2026-01-09 10:00:09 +01:00
115 lines
3.2 KiB
JavaScript
115 lines
3.2 KiB
JavaScript
/**
|
|
* @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 deletion events fired as a result of actions in Blockly's
|
|
* editor.
|
|
* @author fraser@google.com (Neil Fraser)
|
|
*/
|
|
'use strict';
|
|
|
|
goog.provide('Blockly.Events.BlockDelete');
|
|
goog.provide('Blockly.Events.Delete'); // Deprecated.
|
|
|
|
goog.require('Blockly.Events');
|
|
goog.require('Blockly.Events.BlockBase');
|
|
goog.require('goog.array');
|
|
goog.require('goog.math.Coordinate');
|
|
|
|
/**
|
|
* Class for a block deletion event.
|
|
* @param {Blockly.Block} block The deleted block. Null for a blank event.
|
|
* @extends {Blockly.Events.BlockBase}
|
|
* @constructor
|
|
*/
|
|
Blockly.Events.Delete = function(block) {
|
|
if (!block) {
|
|
return; // Blank event to be populated by fromJson.
|
|
}
|
|
if (block.getParent()) {
|
|
throw 'Connected blocks cannot be deleted.';
|
|
}
|
|
Blockly.Events.Delete.superClass_.constructor.call(this, block);
|
|
|
|
if (block.workspace.rendered) {
|
|
this.oldXml = Blockly.Xml.blockToDomWithXY(block);
|
|
} else {
|
|
this.oldXml = Blockly.Xml.blockToDom(block);
|
|
}
|
|
this.ids = Blockly.Events.getDescendantIds_(block);
|
|
};
|
|
goog.inherits(Blockly.Events.Delete, Blockly.Events.BlockBase);
|
|
|
|
/**
|
|
* Class for a block deletion event.
|
|
* @param {Blockly.Block} block The deleted block. Null for a blank event.
|
|
* @extends {Blockly.Events.BlockBase}
|
|
* @constructor
|
|
*/
|
|
Blockly.Events.BlockDelete = Blockly.Events.Delete;
|
|
|
|
/**
|
|
* Type of this event.
|
|
* @type {string}
|
|
*/
|
|
Blockly.Events.Delete.prototype.type = Blockly.Events.DELETE;
|
|
|
|
/**
|
|
* Encode the event as JSON.
|
|
* @return {!Object} JSON representation.
|
|
*/
|
|
Blockly.Events.Delete.prototype.toJson = function() {
|
|
var json = Blockly.Events.Delete.superClass_.toJson.call(this);
|
|
json['ids'] = this.ids;
|
|
return json;
|
|
};
|
|
|
|
/**
|
|
* Decode the JSON event.
|
|
* @param {!Object} json JSON representation.
|
|
*/
|
|
Blockly.Events.Delete.prototype.fromJson = function(json) {
|
|
Blockly.Events.Delete.superClass_.fromJson.call(this, json);
|
|
this.ids = json['ids'];
|
|
};
|
|
|
|
/**
|
|
* Run a deletion event.
|
|
* @param {boolean} forward True if run forward, false if run backward (undo).
|
|
*/
|
|
Blockly.Events.Delete.prototype.run = function(forward) {
|
|
var workspace = this.getEventWorkspace_();
|
|
if (forward) {
|
|
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 delete non-existent block: " + id);
|
|
}
|
|
}
|
|
} else {
|
|
var xml = goog.dom.createDom('xml');
|
|
xml.appendChild(this.oldXml);
|
|
Blockly.Xml.domToWorkspace(xml, workspace);
|
|
}
|
|
};
|