mirror of
https://github.com/google/blockly.git
synced 2026-01-14 12:27:10 +01:00
167 lines
5.0 KiB
JavaScript
167 lines
5.0 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.BlockChange');
|
|
goog.provide('Blockly.Events.Change'); // Deprecated.
|
|
|
|
goog.require('Blockly.Events');
|
|
goog.require('Blockly.Events.Abstract');
|
|
goog.require('goog.array');
|
|
goog.require('goog.math.Coordinate');
|
|
|
|
/**
|
|
* Class for a block change event.
|
|
* @param {Blockly.Block} block The changed block. Null for a blank event.
|
|
* @param {string} element One of 'field', 'comment', 'disabled', etc.
|
|
* @param {?string} name Name of input or field affected, or null.
|
|
* @param {*} oldValue Previous value of element.
|
|
* @param {*} newValue New value of element.
|
|
* @extends {Blockly.Events.Abstract}
|
|
* @constructor
|
|
*/
|
|
Blockly.Events.Change = function(block, element, name, oldValue, newValue) {
|
|
if (!block) {
|
|
return; // Blank event to be populated by fromJson.
|
|
}
|
|
Blockly.Events.Change.superClass_.constructor.call(this, block);
|
|
this.element = element;
|
|
this.name = name;
|
|
this.oldValue = oldValue;
|
|
this.newValue = newValue;
|
|
};
|
|
goog.inherits(Blockly.Events.Change, Blockly.Events.Abstract);
|
|
|
|
/**
|
|
* Class for a block change event.
|
|
* @param {Blockly.Block} block The changed block. Null for a blank event.
|
|
* @param {string} element One of 'field', 'comment', 'disabled', etc.
|
|
* @param {?string} name Name of input or field affected, or null.
|
|
* @param {*} oldValue Previous value of element.
|
|
* @param {*} newValue New value of element.
|
|
* @extends {Blockly.Events.Abstract}
|
|
* @constructor
|
|
*/
|
|
Blockly.Events.BlockChange = Blockly.Events.Change;
|
|
|
|
/**
|
|
* Type of this event.
|
|
* @type {string}
|
|
*/
|
|
Blockly.Events.Change.prototype.type = Blockly.Events.CHANGE;
|
|
|
|
/**
|
|
* Encode the event as JSON.
|
|
* @return {!Object} JSON representation.
|
|
*/
|
|
Blockly.Events.Change.prototype.toJson = function() {
|
|
var json = Blockly.Events.Change.superClass_.toJson.call(this);
|
|
json['element'] = this.element;
|
|
if (this.name) {
|
|
json['name'] = this.name;
|
|
}
|
|
json['newValue'] = this.newValue;
|
|
return json;
|
|
};
|
|
|
|
/**
|
|
* Decode the JSON event.
|
|
* @param {!Object} json JSON representation.
|
|
*/
|
|
Blockly.Events.Change.prototype.fromJson = function(json) {
|
|
Blockly.Events.Change.superClass_.fromJson.call(this, json);
|
|
this.element = json['element'];
|
|
this.name = json['name'];
|
|
this.newValue = json['newValue'];
|
|
};
|
|
|
|
/**
|
|
* Does this event record any change of state?
|
|
* @return {boolean} True if something changed.
|
|
*/
|
|
Blockly.Events.Change.prototype.isNull = function() {
|
|
return this.oldValue == this.newValue;
|
|
};
|
|
|
|
/**
|
|
* 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 = this.getEventWorkspace_();
|
|
var block = workspace.getBlockById(this.blockId);
|
|
if (!block) {
|
|
console.warn("Can't change non-existent 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.
|
|
// The validator's opinion on validity is ignored.
|
|
field.callValidator(value);
|
|
field.setValue(value);
|
|
} else {
|
|
console.warn("Can't set non-existent field: " + this.name);
|
|
}
|
|
break;
|
|
case 'comment':
|
|
block.setCommentText(value || null);
|
|
break;
|
|
case 'collapsed':
|
|
block.setCollapsed(value);
|
|
break;
|
|
case 'disabled':
|
|
block.setDisabled(value);
|
|
break;
|
|
case 'inline':
|
|
block.setInputsInline(value);
|
|
break;
|
|
case 'mutation':
|
|
var oldMutation = '';
|
|
if (block.mutationToDom) {
|
|
var oldMutationDom = block.mutationToDom();
|
|
oldMutation = oldMutationDom && Blockly.Xml.domToText(oldMutationDom);
|
|
}
|
|
if (block.domToMutation) {
|
|
value = value || '<mutation></mutation>';
|
|
var dom = Blockly.Xml.textToDom('<xml>' + value + '</xml>');
|
|
block.domToMutation(dom.firstChild);
|
|
}
|
|
Blockly.Events.fire(new Blockly.Events.Change(
|
|
block, 'mutation', null, oldMutation, value));
|
|
break;
|
|
default:
|
|
console.warn('Unknown change type: ' + this.element);
|
|
}
|
|
};
|