mirror of
https://github.com/google/blockly.git
synced 2026-01-10 02:17:09 +01:00
192 lines
5.7 KiB
JavaScript
192 lines
5.7 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 change events fired as a result of actions in Blockly's
|
|
* editor.
|
|
* @author fraser@google.com (Neil Fraser)
|
|
*/
|
|
'use strict';
|
|
|
|
goog.provide('Blockly.Events.BlockMove');
|
|
goog.provide('Blockly.Events.Move'); // Deprecated.
|
|
|
|
goog.require('Blockly.Events');
|
|
goog.require('Blockly.Events.BlockBase');
|
|
goog.require('goog.array');
|
|
goog.require('goog.math.Coordinate');
|
|
|
|
/**
|
|
* Class for a block move event. Created before the move.
|
|
* @param {Blockly.Block} block The moved block. Null for a blank event.
|
|
* @extends {Blockly.Events.BlockBase}
|
|
* @constructor
|
|
*/
|
|
Blockly.Events.Move = function(block) {
|
|
if (!block) {
|
|
return; // Blank event to be populated by fromJson.
|
|
}
|
|
Blockly.Events.Move.superClass_.constructor.call(this, block);
|
|
var location = this.currentLocation_();
|
|
this.oldParentId = location.parentId;
|
|
this.oldInputName = location.inputName;
|
|
this.oldCoordinate = location.coordinate;
|
|
};
|
|
goog.inherits(Blockly.Events.Move, Blockly.Events.BlockBase);
|
|
|
|
|
|
/**
|
|
* Class for a block move event. Created before the move.
|
|
* @param {Blockly.Block} block The moved block. Null for a blank event.
|
|
* @extends {Blockly.Events.BlockBase}
|
|
* @constructor
|
|
*/
|
|
Blockly.Events.BlockMove = Blockly.Events.Move;
|
|
|
|
/**
|
|
* Type of this event.
|
|
* @type {string}
|
|
*/
|
|
Blockly.Events.Move.prototype.type = Blockly.Events.MOVE;
|
|
|
|
/**
|
|
* Encode the event as JSON.
|
|
* @return {!Object} JSON representation.
|
|
*/
|
|
Blockly.Events.Move.prototype.toJson = function() {
|
|
var json = Blockly.Events.Move.superClass_.toJson.call(this);
|
|
if (this.newParentId) {
|
|
json['newParentId'] = this.newParentId;
|
|
}
|
|
if (this.newInputName) {
|
|
json['newInputName'] = this.newInputName;
|
|
}
|
|
if (this.newCoordinate) {
|
|
json['newCoordinate'] = Math.round(this.newCoordinate.x) + ',' +
|
|
Math.round(this.newCoordinate.y);
|
|
}
|
|
return json;
|
|
};
|
|
|
|
/**
|
|
* Decode the JSON event.
|
|
* @param {!Object} json JSON representation.
|
|
*/
|
|
Blockly.Events.Move.prototype.fromJson = function(json) {
|
|
Blockly.Events.Move.superClass_.fromJson.call(this, json);
|
|
this.newParentId = json['newParentId'];
|
|
this.newInputName = json['newInputName'];
|
|
if (json['newCoordinate']) {
|
|
var xy = json['newCoordinate'].split(',');
|
|
this.newCoordinate =
|
|
new goog.math.Coordinate(parseFloat(xy[0]), parseFloat(xy[1]));
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Record the block's new location. Called after the move.
|
|
*/
|
|
Blockly.Events.Move.prototype.recordNew = function() {
|
|
var location = this.currentLocation_();
|
|
this.newParentId = location.parentId;
|
|
this.newInputName = location.inputName;
|
|
this.newCoordinate = location.coordinate;
|
|
};
|
|
|
|
/**
|
|
* Returns the parentId and input if the block is connected,
|
|
* or the XY location if disconnected.
|
|
* @return {!Object} Collection of location info.
|
|
* @private
|
|
*/
|
|
Blockly.Events.Move.prototype.currentLocation_ = function() {
|
|
var workspace = Blockly.Workspace.getById(this.workspaceId);
|
|
var block = workspace.getBlockById(this.blockId);
|
|
var location = {};
|
|
var parent = block.getParent();
|
|
if (parent) {
|
|
location.parentId = parent.id;
|
|
var input = parent.getInputWithBlock(block);
|
|
if (input) {
|
|
location.inputName = input.name;
|
|
}
|
|
} else {
|
|
location.coordinate = block.getRelativeToSurfaceXY();
|
|
}
|
|
return location;
|
|
};
|
|
|
|
/**
|
|
* Does this event record any change of state?
|
|
* @return {boolean} True if something changed.
|
|
*/
|
|
Blockly.Events.Move.prototype.isNull = function() {
|
|
return this.oldParentId == this.newParentId &&
|
|
this.oldInputName == this.newInputName &&
|
|
goog.math.Coordinate.equals(this.oldCoordinate, this.newCoordinate);
|
|
};
|
|
|
|
/**
|
|
* Run a move event.
|
|
* @param {boolean} forward True if run forward, false if run backward (undo).
|
|
*/
|
|
Blockly.Events.Move.prototype.run = function(forward) {
|
|
var workspace = this.getEventWorkspace_();
|
|
var block = workspace.getBlockById(this.blockId);
|
|
if (!block) {
|
|
console.warn("Can't move non-existent block: " + this.blockId);
|
|
return;
|
|
}
|
|
var parentId = forward ? this.newParentId : this.oldParentId;
|
|
var inputName = forward ? this.newInputName : this.oldInputName;
|
|
var coordinate = forward ? this.newCoordinate : this.oldCoordinate;
|
|
var parentBlock = null;
|
|
if (parentId) {
|
|
parentBlock = workspace.getBlockById(parentId);
|
|
if (!parentBlock) {
|
|
console.warn("Can't connect to non-existent block: " + parentId);
|
|
return;
|
|
}
|
|
}
|
|
if (block.getParent()) {
|
|
block.unplug();
|
|
}
|
|
if (coordinate) {
|
|
var xy = block.getRelativeToSurfaceXY();
|
|
block.moveBy(coordinate.x - xy.x, coordinate.y - xy.y);
|
|
} else {
|
|
var blockConnection = block.outputConnection || block.previousConnection;
|
|
var parentConnection;
|
|
if (inputName) {
|
|
var input = parentBlock.getInput(inputName);
|
|
if (input) {
|
|
parentConnection = input.connection;
|
|
}
|
|
} else if (blockConnection.type == Blockly.PREVIOUS_STATEMENT) {
|
|
parentConnection = parentBlock.nextConnection;
|
|
}
|
|
if (parentConnection) {
|
|
blockConnection.connect(parentConnection);
|
|
} else {
|
|
console.warn("Can't connect to non-existent input: " + inputName);
|
|
}
|
|
}
|
|
};
|