mirror of
https://github.com/google/blockly.git
synced 2026-06-17 00:25:14 +02:00
Migrate core/events/events.js to goog.module syntax (#5302)
* Migrate core/events/events.js to ES6 const/let * Migrate core/events/events.js to goog.module * Migrate core/events/events.js to named requires * clang-format core/events/events.js * Migrate Blockly core to use getRecordUndo/setRecordUndo * Update core/events.js to reflect latest guidance around exporting mutable fields
This commit is contained in:
+4
-4
@@ -224,14 +224,14 @@ const Block = function(workspace, prototypeName, opt_id) {
|
||||
if (!existingGroup) {
|
||||
Events.setGroup(true);
|
||||
}
|
||||
const initialUndoFlag = Events.recordUndo;
|
||||
const initialUndoFlag = Events.getRecordUndo();
|
||||
|
||||
try {
|
||||
// Call an initialization function, if it exists.
|
||||
if (typeof this.init == 'function') {
|
||||
Events.recordUndo = false;
|
||||
Events.setRecordUndo(false);
|
||||
this.init();
|
||||
Events.recordUndo = initialUndoFlag;
|
||||
Events.setRecordUndo(initialUndoFlag);
|
||||
}
|
||||
|
||||
// Fire a create event.
|
||||
@@ -244,7 +244,7 @@ const Block = function(workspace, prototypeName, opt_id) {
|
||||
Events.setGroup(false);
|
||||
}
|
||||
// In case init threw, recordUndo flag should still be reset.
|
||||
Events.recordUndo = initialUndoFlag;
|
||||
Events.setRecordUndo(initialUndoFlag);
|
||||
}
|
||||
|
||||
// Record initial inline state.
|
||||
|
||||
+224
-128
@@ -14,190 +14,274 @@
|
||||
* Events fired as a result of actions in Blockly's editor.
|
||||
* @namespace Blockly.Events
|
||||
*/
|
||||
goog.provide('Blockly.Events');
|
||||
goog.module('Blockly.Events');
|
||||
goog.module.declareLegacyNamespace();
|
||||
|
||||
goog.require('Blockly.registry');
|
||||
goog.require('Blockly.utils');
|
||||
|
||||
goog.requireType('Blockly.Block');
|
||||
goog.requireType('Blockly.Events.Abstract');
|
||||
goog.requireType('Blockly.Workspace');
|
||||
/* eslint-disable-next-line no-unused-vars */
|
||||
const Abstract = goog.requireType('Blockly.Events.Abstract');
|
||||
/* eslint-disable-next-line no-unused-vars */
|
||||
const Block = goog.requireType('Blockly.Block');
|
||||
/* eslint-disable-next-line no-unused-vars */
|
||||
const BlockCreate = goog.requireType('Blockly.Events.BlockCreate');
|
||||
/* eslint-disable-next-line no-unused-vars */
|
||||
const BlockMove = goog.requireType('Blockly.Events.BlockMove');
|
||||
/* eslint-disable-next-line no-unused-vars */
|
||||
const CommentCreate = goog.requireType('Blockly.Events.CommentCreate');
|
||||
/* eslint-disable-next-line no-unused-vars */
|
||||
const CommentMove = goog.requireType('Blockly.Events.CommentMove');
|
||||
/* eslint-disable-next-line no-unused-vars */
|
||||
const Workspace = goog.requireType('Blockly.Workspace');
|
||||
const deprecation = goog.require('Blockly.utils.deprecation');
|
||||
const registry = goog.require('Blockly.registry');
|
||||
const utils = goog.require('Blockly.utils');
|
||||
|
||||
|
||||
/**
|
||||
* Group ID for new events. Grouped events are indivisible.
|
||||
* @type {string}
|
||||
* @private
|
||||
*/
|
||||
Blockly.Events.group_ = '';
|
||||
let group = '';
|
||||
|
||||
/**
|
||||
* Sets whether the next event should be added to the undo stack.
|
||||
* @type {boolean}
|
||||
*/
|
||||
Blockly.Events.recordUndo = true;
|
||||
let recordUndo = true;
|
||||
/** @deprecated September 2021 */
|
||||
exports.recordUndo = recordUndo;
|
||||
|
||||
/**
|
||||
* Sets whether events should be added to the undo stack.
|
||||
* @param {boolean} newValue True if events should be added to the undo stack.
|
||||
*/
|
||||
const setRecordUndo = function(newValue) {
|
||||
recordUndo = newValue;
|
||||
};
|
||||
exports.setRecordUndo = setRecordUndo;
|
||||
|
||||
/**
|
||||
* Returns whether or not events will be added to the undo stack.
|
||||
* @returns {boolean} True if events will be added to the undo stack.
|
||||
*/
|
||||
const getRecordUndo = function() {
|
||||
return recordUndo;
|
||||
};
|
||||
exports.getRecordUndo = getRecordUndo;
|
||||
|
||||
Object.defineProperties(exports, {
|
||||
recordUndo: {
|
||||
get: function() {
|
||||
deprecation.warn(
|
||||
'Blockly.Events.recordUndo', 'September 2021', 'September 2022',
|
||||
'Blockly.Events.getRecordUndo()');
|
||||
return getRecordUndo();
|
||||
},
|
||||
set: function(record) {
|
||||
deprecation.warn(
|
||||
'Blockly.Events.recordUndo', 'September 2021', 'September 2022',
|
||||
'Blockly.Events.setRecordUndo()');
|
||||
setRecordUndo(record);
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Allow change events to be created and fired.
|
||||
* @type {number}
|
||||
* @private
|
||||
*/
|
||||
Blockly.Events.disabled_ = 0;
|
||||
let disabled = 0;
|
||||
/** @private */
|
||||
exports.disabled_ = disabled;
|
||||
|
||||
|
||||
Object.defineProperties(exports, {
|
||||
disabled_: {
|
||||
set: function(newValue) {
|
||||
disabled = newValue;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* Name of event that creates a block. Will be deprecated for BLOCK_CREATE.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.CREATE = 'create';
|
||||
const CREATE = 'create';
|
||||
exports.CREATE = CREATE;
|
||||
|
||||
/**
|
||||
* Name of event that creates a block.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.BLOCK_CREATE = Blockly.Events.CREATE;
|
||||
const BLOCK_CREATE = CREATE;
|
||||
exports.BLOCK_CREATE = BLOCK_CREATE;
|
||||
|
||||
/**
|
||||
* Name of event that deletes a block. Will be deprecated for BLOCK_DELETE.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.DELETE = 'delete';
|
||||
const DELETE = 'delete';
|
||||
exports.DELETE = DELETE;
|
||||
|
||||
/**
|
||||
* Name of event that deletes a block.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.BLOCK_DELETE = Blockly.Events.DELETE;
|
||||
const BLOCK_DELETE = DELETE;
|
||||
exports.BLOCK_DELETE = BLOCK_DELETE;
|
||||
|
||||
/**
|
||||
* Name of event that changes a block. Will be deprecated for BLOCK_CHANGE.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.CHANGE = 'change';
|
||||
const CHANGE = 'change';
|
||||
exports.CHANGE = CHANGE;
|
||||
|
||||
/**
|
||||
* Name of event that changes a block.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.BLOCK_CHANGE = Blockly.Events.CHANGE;
|
||||
const BLOCK_CHANGE = CHANGE;
|
||||
exports.BLOCK_CHANGE = BLOCK_CHANGE;
|
||||
|
||||
/**
|
||||
* Name of event that moves a block. Will be deprecated for BLOCK_MOVE.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.MOVE = 'move';
|
||||
const MOVE = 'move';
|
||||
exports.MOVE = MOVE;
|
||||
|
||||
/**
|
||||
* Name of event that moves a block.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.BLOCK_MOVE = Blockly.Events.MOVE;
|
||||
const BLOCK_MOVE = MOVE;
|
||||
exports.BLOCK_MOVE = BLOCK_MOVE;
|
||||
|
||||
/**
|
||||
* Name of event that creates a variable.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.VAR_CREATE = 'var_create';
|
||||
const VAR_CREATE = 'var_create';
|
||||
exports.VAR_CREATE = VAR_CREATE;
|
||||
|
||||
/**
|
||||
* Name of event that deletes a variable.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.VAR_DELETE = 'var_delete';
|
||||
const VAR_DELETE = 'var_delete';
|
||||
exports.VAR_DELETE = VAR_DELETE;
|
||||
|
||||
/**
|
||||
* Name of event that renames a variable.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.VAR_RENAME = 'var_rename';
|
||||
const VAR_RENAME = 'var_rename';
|
||||
exports.VAR_RENAME = VAR_RENAME;
|
||||
|
||||
/**
|
||||
* Name of generic event that records a UI change.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.UI = 'ui';
|
||||
const UI = 'ui';
|
||||
exports.UI = UI;
|
||||
|
||||
/**
|
||||
* Name of event that record a block drags a block.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.BLOCK_DRAG = 'drag';
|
||||
const BLOCK_DRAG = 'drag';
|
||||
exports.BLOCK_DRAG = BLOCK_DRAG;
|
||||
|
||||
/**
|
||||
* Name of event that records a change in selected element.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.SELECTED = 'selected';
|
||||
const SELECTED = 'selected';
|
||||
exports.SELECTED = SELECTED;
|
||||
|
||||
/**
|
||||
* Name of event that records a click.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.CLICK = 'click';
|
||||
const CLICK = 'click';
|
||||
exports.CLICK = CLICK;
|
||||
|
||||
/**
|
||||
* Name of event that records a marker move.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.MARKER_MOVE = 'marker_move';
|
||||
const MARKER_MOVE = 'marker_move';
|
||||
exports.MARKER_MOVE = MARKER_MOVE;
|
||||
|
||||
/**
|
||||
* Name of event that records a bubble open.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.BUBBLE_OPEN = 'bubble_open';
|
||||
const BUBBLE_OPEN = 'bubble_open';
|
||||
exports.BUBBLE_OPEN = BUBBLE_OPEN;
|
||||
|
||||
/**
|
||||
* Name of event that records a trashcan open.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.TRASHCAN_OPEN = 'trashcan_open';
|
||||
const TRASHCAN_OPEN = 'trashcan_open';
|
||||
exports.TRASHCAN_OPEN = TRASHCAN_OPEN;
|
||||
|
||||
/**
|
||||
* Name of event that records a toolbox item select.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.TOOLBOX_ITEM_SELECT = 'toolbox_item_select';
|
||||
const TOOLBOX_ITEM_SELECT = 'toolbox_item_select';
|
||||
exports.TOOLBOX_ITEM_SELECT = TOOLBOX_ITEM_SELECT;
|
||||
|
||||
/**
|
||||
* Name of event that records a theme change.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.THEME_CHANGE = 'theme_change';
|
||||
const THEME_CHANGE = 'theme_change';
|
||||
exports.THEME_CHANGE = THEME_CHANGE;
|
||||
|
||||
/**
|
||||
* Name of event that records a viewport change.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.VIEWPORT_CHANGE = 'viewport_change';
|
||||
const VIEWPORT_CHANGE = 'viewport_change';
|
||||
exports.VIEWPORT_CHANGE = VIEWPORT_CHANGE;
|
||||
|
||||
/**
|
||||
* Name of event that creates a comment.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.COMMENT_CREATE = 'comment_create';
|
||||
const COMMENT_CREATE = 'comment_create';
|
||||
exports.COMMENT_CREATE = COMMENT_CREATE;
|
||||
|
||||
/**
|
||||
* Name of event that deletes a comment.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.COMMENT_DELETE = 'comment_delete';
|
||||
const COMMENT_DELETE = 'comment_delete';
|
||||
exports.COMMENT_DELETE = COMMENT_DELETE;
|
||||
|
||||
/**
|
||||
* Name of event that changes a comment.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.COMMENT_CHANGE = 'comment_change';
|
||||
const COMMENT_CHANGE = 'comment_change';
|
||||
exports.COMMENT_CHANGE = COMMENT_CHANGE;
|
||||
|
||||
/**
|
||||
* Name of event that moves a comment.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.COMMENT_MOVE = 'comment_move';
|
||||
const COMMENT_MOVE = 'comment_move';
|
||||
exports.COMMENT_MOVE = COMMENT_MOVE;
|
||||
|
||||
/**
|
||||
* Name of event that records a workspace load.
|
||||
*/
|
||||
Blockly.Events.FINISHED_LOADING = 'finished_loading';
|
||||
const FINISHED_LOADING = 'finished_loading';
|
||||
exports.FINISHED_LOADING = FINISHED_LOADING;
|
||||
|
||||
/**
|
||||
* Type of events that cause objects to be bumped back into the visible
|
||||
@@ -205,10 +289,11 @@ Blockly.Events.FINISHED_LOADING = 'finished_loading';
|
||||
*
|
||||
* Not to be confused with bumping so that disconnected connections do not
|
||||
* appear connected.
|
||||
* @typedef {!Blockly.Events.BlockCreate|!Blockly.Events.BlockMove|
|
||||
* !Blockly.Events.CommentCreate|!Blockly.Events.CommentMove}
|
||||
* @typedef {!BlockCreate|!BlockMove|
|
||||
* !CommentCreate|!CommentMove}
|
||||
*/
|
||||
Blockly.Events.BumpEvent;
|
||||
let BumpEvent;
|
||||
exports.BumpEvent = BumpEvent;
|
||||
|
||||
/**
|
||||
* List of events that cause objects to be bumped back into the visible
|
||||
@@ -218,101 +303,99 @@ Blockly.Events.BumpEvent;
|
||||
* appear connected.
|
||||
* @const
|
||||
*/
|
||||
Blockly.Events.BUMP_EVENTS = [
|
||||
Blockly.Events.BLOCK_CREATE,
|
||||
Blockly.Events.BLOCK_MOVE,
|
||||
Blockly.Events.COMMENT_CREATE,
|
||||
Blockly.Events.COMMENT_MOVE
|
||||
];
|
||||
const BUMP_EVENTS = [BLOCK_CREATE, BLOCK_MOVE, COMMENT_CREATE, COMMENT_MOVE];
|
||||
exports.BUMP_EVENTS = BUMP_EVENTS;
|
||||
|
||||
/**
|
||||
* List of events queued for firing.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Events.FIRE_QUEUE_ = [];
|
||||
const FIRE_QUEUE = [];
|
||||
/** @private */
|
||||
exports.FIRE_QUEUE_ = FIRE_QUEUE;
|
||||
|
||||
/**
|
||||
* Create a custom event and fire it.
|
||||
* @param {!Blockly.Events.Abstract} event Custom data for event.
|
||||
* @param {!Abstract} event Custom data for event.
|
||||
*/
|
||||
Blockly.Events.fire = function(event) {
|
||||
if (!Blockly.Events.isEnabled()) {
|
||||
const fire = function(event) {
|
||||
if (!isEnabled()) {
|
||||
return;
|
||||
}
|
||||
if (!Blockly.Events.FIRE_QUEUE_.length) {
|
||||
if (!FIRE_QUEUE.length) {
|
||||
// First event added; schedule a firing of the event queue.
|
||||
setTimeout(Blockly.Events.fireNow_, 0);
|
||||
setTimeout(fireNow, 0);
|
||||
}
|
||||
Blockly.Events.FIRE_QUEUE_.push(event);
|
||||
FIRE_QUEUE.push(event);
|
||||
};
|
||||
exports.fire = fire;
|
||||
|
||||
/**
|
||||
* Fire all queued events.
|
||||
* @private
|
||||
*/
|
||||
Blockly.Events.fireNow_ = function() {
|
||||
var queue = Blockly.Events.filter(Blockly.Events.FIRE_QUEUE_, true);
|
||||
Blockly.Events.FIRE_QUEUE_.length = 0;
|
||||
for (var i = 0, event; (event = queue[i]); i++) {
|
||||
const fireNow = function() {
|
||||
const queue = filter(FIRE_QUEUE, true);
|
||||
FIRE_QUEUE.length = 0;
|
||||
for (let i = 0, event; (event = queue[i]); i++) {
|
||||
if (!event.workspaceId) {
|
||||
continue;
|
||||
}
|
||||
var workspace = Blockly.Workspace.getById(event.workspaceId);
|
||||
if (workspace) {
|
||||
workspace.fireChangeListener(event);
|
||||
const Workspace = goog.module.get('Blockly.Workspace');
|
||||
const eventWorkspace = Workspace.getById(event.workspaceId);
|
||||
if (eventWorkspace) {
|
||||
eventWorkspace.fireChangeListener(event);
|
||||
}
|
||||
}
|
||||
};
|
||||
/** @private */
|
||||
exports.fireNow_ = fireNow;
|
||||
|
||||
/**
|
||||
* Filter the queued events and merge duplicates.
|
||||
* @param {!Array<!Blockly.Events.Abstract>} queueIn Array of events.
|
||||
* @param {!Array<!Abstract>} queueIn Array of events.
|
||||
* @param {boolean} forward True if forward (redo), false if backward (undo).
|
||||
* @return {!Array<!Blockly.Events.Abstract>} Array of filtered events.
|
||||
* @return {!Array<!Abstract>} Array of filtered events.
|
||||
*/
|
||||
Blockly.Events.filter = function(queueIn, forward) {
|
||||
var queue = queueIn.slice(); // Shallow copy of queue.
|
||||
const filter = function(queueIn, forward) {
|
||||
let queue = queueIn.slice(); // Shallow copy of queue.
|
||||
if (!forward) {
|
||||
// Undo is merged in reverse order.
|
||||
queue.reverse();
|
||||
}
|
||||
var mergedQueue = [];
|
||||
var hash = Object.create(null);
|
||||
const mergedQueue = [];
|
||||
const hash = Object.create(null);
|
||||
// Merge duplicates.
|
||||
for (var i = 0, event; (event = queue[i]); i++) {
|
||||
for (let i = 0, event; (event = queue[i]); i++) {
|
||||
if (!event.isNull()) {
|
||||
// Treat all UI events as the same type in hash table.
|
||||
var eventType = event.isUiEvent ? Blockly.Events.UI : event.type;
|
||||
var key = [eventType, event.blockId, event.workspaceId].join(' ');
|
||||
const eventType = event.isUiEvent ? UI : event.type;
|
||||
const key = [eventType, event.blockId, event.workspaceId].join(' ');
|
||||
|
||||
var lastEntry = hash[key];
|
||||
var lastEvent = lastEntry ? lastEntry.event : null;
|
||||
const lastEntry = hash[key];
|
||||
const lastEvent = lastEntry ? lastEntry.event : null;
|
||||
if (!lastEntry) {
|
||||
// Each item in the hash table has the event and the index of that event
|
||||
// in the input array. This lets us make sure we only merge adjacent
|
||||
// move events.
|
||||
hash[key] = { event: event, index: i};
|
||||
hash[key] = {event: event, index: i};
|
||||
mergedQueue.push(event);
|
||||
} else if (event.type == Blockly.Events.MOVE &&
|
||||
lastEntry.index == i - 1) {
|
||||
} else if (event.type == MOVE && lastEntry.index == i - 1) {
|
||||
// Merge move events.
|
||||
lastEvent.newParentId = event.newParentId;
|
||||
lastEvent.newInputName = event.newInputName;
|
||||
lastEvent.newCoordinate = event.newCoordinate;
|
||||
lastEntry.index = i;
|
||||
} else if (event.type == Blockly.Events.CHANGE &&
|
||||
event.element == lastEvent.element &&
|
||||
} else if (
|
||||
event.type == CHANGE && event.element == lastEvent.element &&
|
||||
event.name == lastEvent.name) {
|
||||
// Merge change events.
|
||||
lastEvent.newValue = event.newValue;
|
||||
} else if (event.type == Blockly.Events.VIEWPORT_CHANGE) {
|
||||
} else if (event.type == VIEWPORT_CHANGE) {
|
||||
// Merge viewport change events.
|
||||
lastEvent.viewTop = event.viewTop;
|
||||
lastEvent.viewLeft = event.viewLeft;
|
||||
lastEvent.scale = event.scale;
|
||||
lastEvent.oldScale = event.oldScale;
|
||||
} else if (event.type == Blockly.Events.CLICK &&
|
||||
lastEvent.type == Blockly.Events.BUBBLE_OPEN) {
|
||||
} else if (event.type == CLICK && lastEvent.type == BUBBLE_OPEN) {
|
||||
// Drop click events caused by opening/closing bubbles.
|
||||
} else {
|
||||
// Collision: newer events should merge into this event to maintain
|
||||
@@ -323,155 +406,168 @@ Blockly.Events.filter = function(queueIn, forward) {
|
||||
}
|
||||
}
|
||||
// Filter out any events that have become null due to merging.
|
||||
queue = mergedQueue.filter(function(e) { return !e.isNull(); });
|
||||
queue = mergedQueue.filter(function(e) {
|
||||
return !e.isNull();
|
||||
});
|
||||
if (!forward) {
|
||||
// Restore undo order.
|
||||
queue.reverse();
|
||||
}
|
||||
// Move mutation events to the top of the queue.
|
||||
// Intentionally skip first event.
|
||||
for (var i = 1, event; (event = queue[i]); i++) {
|
||||
if (event.type == Blockly.Events.CHANGE &&
|
||||
event.element == 'mutation') {
|
||||
for (let i = 1, event; (event = queue[i]); i++) {
|
||||
if (event.type == CHANGE && event.element == 'mutation') {
|
||||
queue.unshift(queue.splice(i, 1)[0]);
|
||||
}
|
||||
}
|
||||
return queue;
|
||||
};
|
||||
exports.filter = filter;
|
||||
|
||||
/**
|
||||
* Modify pending undo events so that when they are fired they don't land
|
||||
* in the undo stack. Called by Blockly.Workspace.clearUndo.
|
||||
* in the undo stack. Called by Workspace.clearUndo.
|
||||
*/
|
||||
Blockly.Events.clearPendingUndo = function() {
|
||||
for (var i = 0, event; (event = Blockly.Events.FIRE_QUEUE_[i]); i++) {
|
||||
const clearPendingUndo = function() {
|
||||
for (let i = 0, event; (event = FIRE_QUEUE[i]); i++) {
|
||||
event.recordUndo = false;
|
||||
}
|
||||
};
|
||||
exports.clearPendingUndo = clearPendingUndo;
|
||||
|
||||
/**
|
||||
* Stop sending events. Every call to this function MUST also call enable.
|
||||
*/
|
||||
Blockly.Events.disable = function() {
|
||||
Blockly.Events.disabled_++;
|
||||
const disable = function() {
|
||||
disabled++;
|
||||
};
|
||||
exports.disable = disable;
|
||||
|
||||
/**
|
||||
* Start sending events. Unless events were already disabled when the
|
||||
* corresponding call to disable was made.
|
||||
*/
|
||||
Blockly.Events.enable = function() {
|
||||
Blockly.Events.disabled_--;
|
||||
const enable = function() {
|
||||
disabled--;
|
||||
};
|
||||
exports.enable = enable;
|
||||
|
||||
/**
|
||||
* Returns whether events may be fired or not.
|
||||
* @return {boolean} True if enabled.
|
||||
*/
|
||||
Blockly.Events.isEnabled = function() {
|
||||
return Blockly.Events.disabled_ == 0;
|
||||
const isEnabled = function() {
|
||||
return disabled == 0;
|
||||
};
|
||||
exports.isEnabled = isEnabled;
|
||||
|
||||
/**
|
||||
* Current group.
|
||||
* @return {string} ID string.
|
||||
*/
|
||||
Blockly.Events.getGroup = function() {
|
||||
return Blockly.Events.group_;
|
||||
const getGroup = function() {
|
||||
return group;
|
||||
};
|
||||
exports.getGroup = getGroup;
|
||||
|
||||
/**
|
||||
* Start or stop a group.
|
||||
* @param {boolean|string} state True to start new group, false to end group.
|
||||
* String to set group explicitly.
|
||||
*/
|
||||
Blockly.Events.setGroup = function(state) {
|
||||
const setGroup = function(state) {
|
||||
if (typeof state == 'boolean') {
|
||||
Blockly.Events.group_ = state ? Blockly.utils.genUid() : '';
|
||||
group = state ? utils.genUid() : '';
|
||||
} else {
|
||||
Blockly.Events.group_ = state;
|
||||
group = state;
|
||||
}
|
||||
};
|
||||
exports.setGroup = setGroup;
|
||||
|
||||
/**
|
||||
* Compute a list of the IDs of the specified block and all its descendants.
|
||||
* @param {!Blockly.Block} block The root block.
|
||||
* @param {!Block} block The root block.
|
||||
* @return {!Array<string>} List of block IDs.
|
||||
* @package
|
||||
*/
|
||||
Blockly.Events.getDescendantIds = function(block) {
|
||||
var ids = [];
|
||||
var descendants = block.getDescendants(false);
|
||||
for (var i = 0, descendant; (descendant = descendants[i]); i++) {
|
||||
const getDescendantIds = function(block) {
|
||||
const ids = [];
|
||||
const descendants = block.getDescendants(false);
|
||||
for (let i = 0, descendant; (descendant = descendants[i]); i++) {
|
||||
ids[i] = descendant.id;
|
||||
}
|
||||
return ids;
|
||||
};
|
||||
/** @package */
|
||||
exports.getDescendantIds = getDescendantIds;
|
||||
|
||||
/**
|
||||
* Decode the JSON into an event.
|
||||
* @param {!Object} json JSON representation.
|
||||
* @param {!Blockly.Workspace} workspace Target workspace for event.
|
||||
* @return {!Blockly.Events.Abstract} The event represented by the JSON.
|
||||
* @param {!Workspace} workspace Target workspace for event.
|
||||
* @return {!Abstract} The event represented by the JSON.
|
||||
* @throws {Error} if an event type is not found in the registry.
|
||||
*/
|
||||
Blockly.Events.fromJson = function(json, workspace) {
|
||||
var eventClass = Blockly.Events.get(json.type);
|
||||
const fromJson = function(json, workspace) {
|
||||
const eventClass = get(json.type);
|
||||
if (!eventClass) {
|
||||
throw Error('Unknown event type.');
|
||||
}
|
||||
var event = new eventClass();
|
||||
const event = new eventClass();
|
||||
event.fromJson(json);
|
||||
event.workspaceId = workspace.id;
|
||||
return event;
|
||||
};
|
||||
exports.fromJson = fromJson;
|
||||
|
||||
/**
|
||||
* Gets the class for a specific event type from the registry.
|
||||
* @param {string} eventType The type of the event to get.
|
||||
* @return {?function(new:Blockly.Events.Abstract, ...?)} The event class with
|
||||
* @return {?function(new:Abstract, ...?)} The event class with
|
||||
* the given type or null if none exists.
|
||||
*/
|
||||
Blockly.Events.get = function(eventType) {
|
||||
return Blockly.registry.getClass(Blockly.registry.Type.EVENT, eventType);
|
||||
const get = function(eventType) {
|
||||
return registry.getClass(registry.Type.EVENT, eventType);
|
||||
};
|
||||
exports.get = get;
|
||||
|
||||
/**
|
||||
* Enable/disable a block depending on whether it is properly connected.
|
||||
* Use this on applications where all blocks should be connected to a top block.
|
||||
* Recommend setting the 'disable' option to 'false' in the config so that
|
||||
* users don't try to re-enable disabled orphan blocks.
|
||||
* @param {!Blockly.Events.Abstract} event Custom data for event.
|
||||
* @param {!Abstract} event Custom data for event.
|
||||
*/
|
||||
Blockly.Events.disableOrphans = function(event) {
|
||||
if (event.type == Blockly.Events.MOVE ||
|
||||
event.type == Blockly.Events.CREATE) {
|
||||
const disableOrphans = function(event) {
|
||||
if (event.type == MOVE || event.type == CREATE) {
|
||||
if (!event.workspaceId) {
|
||||
return;
|
||||
}
|
||||
var workspace = Blockly.Workspace.getById(event.workspaceId);
|
||||
var block = workspace.getBlockById(event.blockId);
|
||||
const Workspace = goog.module.get('Blockly.Workspace');
|
||||
const eventWorkspace = Workspace.getById(event.workspaceId);
|
||||
let block = eventWorkspace.getBlockById(event.blockId);
|
||||
if (block) {
|
||||
// Changing blocks as part of this event shouldn't be undoable.
|
||||
var initialUndoFlag = Blockly.Events.recordUndo;
|
||||
const initialUndoFlag = recordUndo;
|
||||
try {
|
||||
Blockly.Events.recordUndo = false;
|
||||
var parent = block.getParent();
|
||||
recordUndo = false;
|
||||
const parent = block.getParent();
|
||||
if (parent && parent.isEnabled()) {
|
||||
var children = block.getDescendants(false);
|
||||
for (var i = 0, child; (child = children[i]); i++) {
|
||||
const children = block.getDescendants(false);
|
||||
for (let i = 0, child; (child = children[i]); i++) {
|
||||
child.setEnabled(true);
|
||||
}
|
||||
} else if ((block.outputConnection || block.previousConnection) &&
|
||||
!workspace.isDragging()) {
|
||||
} else if (
|
||||
(block.outputConnection || block.previousConnection) &&
|
||||
!eventWorkspace.isDragging()) {
|
||||
do {
|
||||
block.setEnabled(false);
|
||||
block = block.getNextBlock();
|
||||
} while (block);
|
||||
}
|
||||
} finally {
|
||||
Blockly.Events.recordUndo = initialUndoFlag;
|
||||
recordUndo = initialUndoFlag;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
exports.disableOrphans = disableOrphans;
|
||||
|
||||
@@ -48,7 +48,7 @@ const Abstract = function() {
|
||||
* Sets whether the event should be added to the undo stack.
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.recordUndo = Events.recordUndo;
|
||||
this.recordUndo = Events.getRecordUndo();
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -60,7 +60,7 @@ const CommentBase = function(opt_comment) {
|
||||
* Sets whether the event should be added to the undo stack.
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.recordUndo = Events.recordUndo;
|
||||
this.recordUndo = Events.getRecordUndo();
|
||||
};
|
||||
object.inherits(CommentBase, AbstractEvents);
|
||||
|
||||
|
||||
+3
-3
@@ -362,7 +362,7 @@ exports.getCallers = getCallers;
|
||||
* @param {!Block} defBlock Procedure definition block.
|
||||
*/
|
||||
const mutateCallers = function(defBlock) {
|
||||
const oldRecordUndo = Events.recordUndo;
|
||||
const oldRecordUndo = Events.getRecordUndo();
|
||||
const procedureBlock = /** @type {!ProcedureBlock} */ (defBlock);
|
||||
const name = procedureBlock.getProcedureDef()[0];
|
||||
const xmlElement = defBlock.mutationToDom(true);
|
||||
@@ -377,10 +377,10 @@ const mutateCallers = function(defBlock) {
|
||||
// Fire a mutation on every caller block. But don't record this as an
|
||||
// undo action since it is deterministically tied to the procedure's
|
||||
// definition mutation.
|
||||
Events.recordUndo = false;
|
||||
Events.setRecordUndo(false);
|
||||
Events.fire(new (Events.get(Events.BLOCK_CHANGE))(
|
||||
caller, 'mutation', null, oldMutation, newMutation));
|
||||
Events.recordUndo = oldRecordUndo;
|
||||
Events.setRecordUndo(oldRecordUndo);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -458,7 +458,7 @@ RenderedConnection.prototype.isConnectionAllowed = function(
|
||||
*/
|
||||
RenderedConnection.prototype.onFailedConnect = function(otherConnection) {
|
||||
const block = this.getSourceBlock();
|
||||
if (Events.recordUndo) {
|
||||
if (Events.getRecordUndo()) {
|
||||
const group = Events.getGroup();
|
||||
setTimeout(function() {
|
||||
if (!block.isDisposed() && !block.getParent()) {
|
||||
|
||||
+2
-2
@@ -633,14 +633,14 @@ Workspace.prototype.undo = function(redo) {
|
||||
outputStack.push(event);
|
||||
}
|
||||
events = Events.filter(events, redo);
|
||||
Events.recordUndo = false;
|
||||
Events.setRecordUndo(false);
|
||||
try {
|
||||
for (let i = 0; i < events.length; i++) {
|
||||
const event = events[i];
|
||||
event.run(redo);
|
||||
}
|
||||
} finally {
|
||||
Events.recordUndo = true;
|
||||
Events.setRecordUndo(true);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
+5
-4
@@ -25,6 +25,8 @@ const Size = goog.require('Blockly.utils.Size');
|
||||
const VariableModel = goog.requireType('Blockly.VariableModel');
|
||||
/* eslint-disable-next-line no-unused-vars */
|
||||
const Workspace = goog.requireType('Blockly.Workspace');
|
||||
/* eslint-disable-next-line no-unused-vars */
|
||||
const WorkspaceSvg = goog.requireType('Blockly.WorkspaceSvg');
|
||||
const dom = goog.require('Blockly.utils.dom');
|
||||
const inputTypes = goog.require('Blockly.inputTypes');
|
||||
const utilsXml = goog.require('Blockly.utils.xml');
|
||||
@@ -433,11 +435,10 @@ const domToWorkspace = function(xml, workspace) {
|
||||
}
|
||||
let variablesFirst = true;
|
||||
try {
|
||||
for (let i = 0; i < xml.childNodes.length; i++) {
|
||||
const xmlChild = xml.childNodes[i];
|
||||
for (let i = 0, xmlChild; (xmlChild = xml.childNodes[i]); i++) {
|
||||
const name = xmlChild.nodeName.toLowerCase();
|
||||
const xmlChildElement = /** @type {!Element} */ (xmlChild);
|
||||
if (name == 'block' || (name == 'shadow' && !Events.recordUndo)) {
|
||||
if (name == 'block' || (name == 'shadow' && !Events.getRecordUndo())) {
|
||||
// Allow top-level shadow blocks if recordUndo is disabled since
|
||||
// that means an undo is in progress. Such a block is expected
|
||||
// to be moved to a nested destination in the next operation.
|
||||
@@ -466,7 +467,7 @@ const domToWorkspace = function(xml, workspace) {
|
||||
} else {
|
||||
WorkspaceCommentSvg.fromXml(
|
||||
xmlChildElement,
|
||||
/** @type {!Blockly.WorkspaceSvg} */ (workspace), width);
|
||||
/** @type {!WorkspaceSvg} */ (workspace), width);
|
||||
}
|
||||
} else {
|
||||
const WorkspaceComment = goog.module.get('Blockly.WorkspaceComment');
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ goog.addDependency('../../core/css.js', ['Blockly.Css'], [], {'lang': 'es6', 'mo
|
||||
goog.addDependency('../../core/delete_area.js', ['Blockly.DeleteArea'], ['Blockly.BlockSvg', 'Blockly.DragTarget', 'Blockly.utils.object'], {'lang': 'es6', 'module': 'goog'});
|
||||
goog.addDependency('../../core/drag_target.js', ['Blockly.DragTarget'], [], {'lang': 'es6', 'module': 'goog'});
|
||||
goog.addDependency('../../core/dropdowndiv.js', ['Blockly.DropDownDiv'], ['Blockly.common', 'Blockly.utils.Rect', 'Blockly.utils.dom', 'Blockly.utils.math', 'Blockly.utils.style'], {'lang': 'es6', 'module': 'goog'});
|
||||
goog.addDependency('../../core/events/events.js', ['Blockly.Events'], ['Blockly.registry', 'Blockly.utils']);
|
||||
goog.addDependency('../../core/events/events.js', ['Blockly.Events'], ['Blockly.registry', 'Blockly.utils', 'Blockly.utils.deprecation'], {'lang': 'es6', 'module': 'goog'});
|
||||
goog.addDependency('../../core/events/events_abstract.js', ['Blockly.Events.Abstract'], ['Blockly.Events'], {'lang': 'es6', 'module': 'goog'});
|
||||
goog.addDependency('../../core/events/events_block_base.js', ['Blockly.Events.BlockBase'], ['Blockly.Events.Abstract', 'Blockly.utils.object'], {'lang': 'es6', 'module': 'goog'});
|
||||
goog.addDependency('../../core/events/events_block_change.js', ['Blockly.Events.BlockChange'], ['Blockly.Events', 'Blockly.Xml', 'Blockly.registry', 'Blockly.utils.object'], {'lang': 'es6', 'module': 'goog'});
|
||||
|
||||
Reference in New Issue
Block a user