refactor: convert leaf event classes to es6 classes (#5870)

* chore(tests): update event assert functions to pass in types directly

* refactor: move event types from prototypes to constructors

* refactor: convert events/events_block_change.js to ES6 class

* refactor: convert events/events_block_create.js to ES6 class

* refactor: convert events/events_block_delete.js to ES6 class

* refactor: convert events/events_block_drag.js to ES6 class

* refactor: convert events/events_block_move.js to ES6 class

* refactor: convert events/events_click.js to ES6 class

* refactor: convert events/events_comment_change.js to ES6 class

* refactor: convert events/events_comment_create.js to ES6 class

* refactor: convert events/events_comment_delete.js to ES6 class

* refactor: convert events/events_comment_move.js to ES6 class

* refactor: convert events/events_marker_move.js to ES6 class

* refactor: convert events/events_selected.js to ES6 class

* refactor: convert events/events_theme_change.js to ES6 class

* refactor: convert events/events_toolbox_item_select.js to ES6 class

* refactor: convert events/events_trashcan_open.js to ES6 class

* refactor: convert events/events_ui.js to ES6 class

* refactor: convert events/events_var_create.js to ES6 class

* refactor: convert events/events_var_delete.js to ES6 class

* refactor: convert events/events_var_rename.js to ES6 class

* refactor: convert events/events_viewport.js to ES6 class

* chore: rebuild

* chore: run clang-format
This commit is contained in:
Rachel Fenichel
2022-01-12 09:29:30 -08:00
committed by GitHub
parent 1c74679d83
commit 29fb7b7893
34 changed files with 1452 additions and 1404 deletions

View File

@@ -53,13 +53,13 @@ const Abstract = function() {
* @type {boolean}
*/
this.recordUndo = eventUtils.getRecordUndo();
};
/**
* Whether or not the event is a UI event.
* @type {boolean}
*/
Abstract.prototype.isUiEvent = false;
/**
* Whether or not the event is a UI event.
* @type {boolean}
*/
this.isUiEvent = false;
};
/**
* Encode the event as JSON.

View File

@@ -17,7 +17,6 @@ goog.module('Blockly.Events.BlockChange');
const Xml = goog.require('Blockly.Xml');
const eventUtils = goog.require('Blockly.Events.utils');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
const {BlockBase} = goog.require('Blockly.Events.BlockBase');
/* eslint-disable-next-line no-unused-vars */
@@ -28,145 +27,149 @@ const {Block} = goog.requireType('Blockly.Block');
/**
* Class for a block change event.
* @param {!Block=} opt_block The changed block. Undefined for a blank
* event.
* @param {string=} opt_element One of 'field', 'comment', 'disabled', etc.
* @param {?string=} opt_name Name of input or field affected, or null.
* @param {*=} opt_oldValue Previous value of element.
* @param {*=} opt_newValue New value of element.
* @extends {BlockBase}
* @constructor
* @alias Blockly.Events.BlockChange
*/
const BlockChange = function(
opt_block, opt_element, opt_name, opt_oldValue, opt_newValue) {
BlockChange.superClass_.constructor.call(this, opt_block);
if (!opt_block) {
return; // Blank event to be populated by fromJson.
}
this.element = typeof opt_element === 'undefined' ? '' : opt_element;
this.name = typeof opt_name === 'undefined' ? '' : opt_name;
this.oldValue = typeof opt_oldValue === 'undefined' ? '' : opt_oldValue;
this.newValue = typeof opt_newValue === 'undefined' ? '' : opt_newValue;
};
object.inherits(BlockChange, BlockBase);
class BlockChange extends BlockBase {
/**
* @param {!Block=} opt_block The changed block. Undefined for a blank
* event.
* @param {string=} opt_element One of 'field', 'comment', 'disabled', etc.
* @param {?string=} opt_name Name of input or field affected, or null.
* @param {*=} opt_oldValue Previous value of element.
* @param {*=} opt_newValue New value of element.
* @alias Blockly.Events.BlockChange
*/
constructor(opt_block, opt_element, opt_name, opt_oldValue, opt_newValue) {
super(opt_block);
/**
* Type of this event.
* @type {string}
*/
BlockChange.prototype.type = eventUtils.BLOCK_CHANGE;
/**
* Type of this event.
* @type {string}
*/
this.type = eventUtils.BLOCK_CHANGE;
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
BlockChange.prototype.toJson = function() {
const json = BlockChange.superClass_.toJson.call(this);
json['element'] = this.element;
if (this.name) {
json['name'] = this.name;
}
json['oldValue'] = this.oldValue;
json['newValue'] = this.newValue;
return json;
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
BlockChange.prototype.fromJson = function(json) {
BlockChange.superClass_.fromJson.call(this, json);
this.element = json['element'];
this.name = json['name'];
this.oldValue = json['oldValue'];
this.newValue = json['newValue'];
};
/**
* Does this event record any change of state?
* @return {boolean} False if something changed.
*/
BlockChange.prototype.isNull = function() {
return this.oldValue === this.newValue;
};
/**
* Run a change event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
BlockChange.prototype.run = function(forward) {
const workspace = this.getEventWorkspace_();
const 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);
}
const value = forward ? this.newValue : this.oldValue;
switch (this.element) {
case 'field': {
const field = block.getField(this.name);
if (field) {
field.setValue(value);
} else {
console.warn('Can\'t set non-existent field: ' + this.name);
}
break;
if (!opt_block) {
return; // Blank event to be populated by fromJson.
}
case 'comment':
block.setCommentText(/** @type {string} */ (value) || null);
break;
case 'collapsed':
block.setCollapsed(!!value);
break;
case 'disabled':
block.setEnabled(!value);
break;
case 'inline':
block.setInputsInline(!!value);
break;
case 'mutation': {
const oldState = BlockChange.getExtraBlockState_(
/** @type {!BlockSvg} */ (block));
if (block.loadExtraState) {
block.loadExtraState(JSON.parse(/** @type {string} */ (value) || '{}'));
} else if (block.domToMutation) {
block.domToMutation(
Xml.textToDom(/** @type {string} */ (value) || '<mutation/>'));
}
eventUtils.fire(
new BlockChange(block, 'mutation', null, oldState, value));
break;
}
default:
console.warn('Unknown change type: ' + this.element);
this.element = typeof opt_element === 'undefined' ? '' : opt_element;
this.name = typeof opt_name === 'undefined' ? '' : opt_name;
this.oldValue = typeof opt_oldValue === 'undefined' ? '' : opt_oldValue;
this.newValue = typeof opt_newValue === 'undefined' ? '' : opt_newValue;
}
};
// TODO (#5397): Encapsulate this in the BlocklyMutationChange event when
// refactoring change events.
/**
* Returns the extra state of the given block (either as XML or a JSO, depending
* on the block's definition).
* @param {!BlockSvg} block The block to get the extra state of.
* @return {string} A stringified version of the extra state of the given block.
* @package
*/
BlockChange.getExtraBlockState_ = function(block) {
if (block.saveExtraState) {
const state = block.saveExtraState();
return state ? JSON.stringify(state) : '';
} else if (block.mutationToDom) {
const state = block.mutationToDom();
return state ? Xml.domToText(state) : '';
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
toJson() {
const json = super.toJson();
json['element'] = this.element;
if (this.name) {
json['name'] = this.name;
}
json['oldValue'] = this.oldValue;
json['newValue'] = this.newValue;
return json;
}
return '';
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
fromJson(json) {
super.fromJson(json);
this.element = json['element'];
this.name = json['name'];
this.oldValue = json['oldValue'];
this.newValue = json['newValue'];
}
/**
* Does this event record any change of state?
* @return {boolean} False if something changed.
*/
isNull() {
return this.oldValue === this.newValue;
}
/**
* Run a change event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
run(forward) {
const workspace = this.getEventWorkspace_();
const 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);
}
const value = forward ? this.newValue : this.oldValue;
switch (this.element) {
case 'field': {
const field = block.getField(this.name);
if (field) {
field.setValue(value);
} else {
console.warn('Can\'t set non-existent field: ' + this.name);
}
break;
}
case 'comment':
block.setCommentText(/** @type {string} */ (value) || null);
break;
case 'collapsed':
block.setCollapsed(!!value);
break;
case 'disabled':
block.setEnabled(!value);
break;
case 'inline':
block.setInputsInline(!!value);
break;
case 'mutation': {
const oldState = BlockChange.getExtraBlockState_(
/** @type {!BlockSvg} */ (block));
if (block.loadExtraState) {
block.loadExtraState(
JSON.parse(/** @type {string} */ (value) || '{}'));
} else if (block.domToMutation) {
block.domToMutation(
Xml.textToDom(/** @type {string} */ (value) || '<mutation/>'));
}
eventUtils.fire(
new BlockChange(block, 'mutation', null, oldState, value));
break;
}
default:
console.warn('Unknown change type: ' + this.element);
}
}
// TODO (#5397): Encapsulate this in the BlocklyMutationChange event when
// refactoring change events.
/**
* Returns the extra state of the given block (either as XML or a JSO,
* depending on the block's definition).
* @param {!BlockSvg} block The block to get the extra state of.
* @return {string} A stringified version of the extra state of the given
* block.
* @package
*/
static getExtraBlockState_(block) {
if (block.saveExtraState) {
const state = block.saveExtraState();
return state ? JSON.stringify(state) : '';
} else if (block.mutationToDom) {
const state = block.mutationToDom();
return state ? Xml.domToText(state) : '';
}
return '';
}
}
registry.register(registry.Type.EVENT, eventUtils.CHANGE, BlockChange);

View File

@@ -18,7 +18,6 @@ goog.module('Blockly.Events.BlockCreate');
const Xml = goog.require('Blockly.Xml');
const blocks = goog.require('Blockly.serialization.blocks');
const eventUtils = goog.require('Blockly.Events.utils');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
const {BlockBase} = goog.require('Blockly.Events.BlockBase');
/* eslint-disable-next-line no-unused-vars */
@@ -27,90 +26,93 @@ const {Block} = goog.requireType('Blockly.Block');
/**
* Class for a block creation event.
* @param {!Block=} opt_block The created block. Undefined for a blank
* event.
* @extends {BlockBase}
* @constructor
* @alias Blockly.Events.BlockCreate
*/
const BlockCreate = function(opt_block) {
BlockCreate.superClass_.constructor.call(this, opt_block);
if (!opt_block) {
return; // Blank event to be populated by fromJson.
}
if (opt_block.isShadow()) {
// Moving shadow blocks is handled via disconnection.
this.recordUndo = false;
}
class BlockCreate extends BlockBase {
/**
* @param {!Block=} opt_block The created block. Undefined for a blank
* event.
* @alias Blockly.Events.BlockCreate
*/
constructor(opt_block) {
super(opt_block);
this.xml = Xml.blockToDomWithXY(opt_block);
this.ids = eventUtils.getDescendantIds(opt_block);
/**
* Type of this event.
* @type {string}
*/
this.type = eventUtils.BLOCK_CREATE;
if (!opt_block) {
return; // Blank event to be populated by fromJson.
}
if (opt_block.isShadow()) {
// Moving shadow blocks is handled via disconnection.
this.recordUndo = false;
}
this.xml = Xml.blockToDomWithXY(opt_block);
this.ids = eventUtils.getDescendantIds(opt_block);
/**
* JSON representation of the block that was just created.
* @type {!blocks.State}
*/
this.json = /** @type {!blocks.State} */ (
blocks.save(opt_block, {addCoordinates: true}));
}
/**
* JSON representation of the block that was just created.
* @type {!blocks.State}
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
this.json = /** @type {!blocks.State} */ (
blocks.save(opt_block, {addCoordinates: true}));
};
object.inherits(BlockCreate, BlockBase);
/**
* Type of this event.
* @type {string}
*/
BlockCreate.prototype.type = eventUtils.BLOCK_CREATE;
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
BlockCreate.prototype.toJson = function() {
const json = BlockCreate.superClass_.toJson.call(this);
json['xml'] = Xml.domToText(this.xml);
json['ids'] = this.ids;
json['json'] = this.json;
if (!this.recordUndo) {
json['recordUndo'] = this.recordUndo;
toJson() {
const json = super.toJson();
json['xml'] = Xml.domToText(this.xml);
json['ids'] = this.ids;
json['json'] = this.json;
if (!this.recordUndo) {
json['recordUndo'] = this.recordUndo;
}
return json;
}
return json;
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
BlockCreate.prototype.fromJson = function(json) {
BlockCreate.superClass_.fromJson.call(this, json);
this.xml = Xml.textToDom(json['xml']);
this.ids = json['ids'];
this.json = /** @type {!blocks.State} */ (json['json']);
if (json['recordUndo'] !== undefined) {
this.recordUndo = json['recordUndo'];
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
fromJson(json) {
super.fromJson(json);
this.xml = Xml.textToDom(json['xml']);
this.ids = json['ids'];
this.json = /** @type {!blocks.State} */ (json['json']);
if (json['recordUndo'] !== undefined) {
this.recordUndo = json['recordUndo'];
}
}
};
/**
* Run a creation event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
BlockCreate.prototype.run = function(forward) {
const workspace = this.getEventWorkspace_();
if (forward) {
blocks.append(this.json, workspace);
} else {
for (let i = 0; i < this.ids.length; i++) {
const id = this.ids[i];
const block = workspace.getBlockById(id);
if (block) {
block.dispose(false);
} else if (id === this.blockId) {
// Only complain about root-level block.
console.warn('Can\'t uncreate non-existent block: ' + id);
/**
* Run a creation event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
run(forward) {
const workspace = this.getEventWorkspace_();
if (forward) {
blocks.append(this.json, workspace);
} else {
for (let i = 0; i < this.ids.length; i++) {
const id = this.ids[i];
const block = workspace.getBlockById(id);
if (block) {
block.dispose(false);
} else if (id === this.blockId) {
// Only complain about root-level block.
console.warn('Can\'t uncreate non-existent block: ' + id);
}
}
}
}
};
}
registry.register(registry.Type.EVENT, eventUtils.CREATE, BlockCreate);

View File

@@ -18,7 +18,6 @@ goog.module('Blockly.Events.BlockDelete');
const Xml = goog.require('Blockly.Xml');
const blocks = goog.require('Blockly.serialization.blocks');
const eventUtils = goog.require('Blockly.Events.utils');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
const {BlockBase} = goog.require('Blockly.Events.BlockBase');
/* eslint-disable-next-line no-unused-vars */
@@ -27,102 +26,105 @@ const {Block} = goog.requireType('Blockly.Block');
/**
* Class for a block deletion event.
* @param {!Block=} opt_block The deleted block. Undefined for a blank
* event.
* @extends {BlockBase}
* @constructor
* @alias Blockly.Events.BlockDelete
*/
const BlockDelete = function(opt_block) {
BlockDelete.superClass_.constructor.call(this, opt_block);
if (!opt_block) {
return; // Blank event to be populated by fromJson.
}
if (opt_block.getParent()) {
throw Error('Connected blocks cannot be deleted.');
}
if (opt_block.isShadow()) {
// Respawning shadow blocks is handled via disconnection.
this.recordUndo = false;
}
this.oldXml = Xml.blockToDomWithXY(opt_block);
this.ids = eventUtils.getDescendantIds(opt_block);
class BlockDelete extends BlockBase {
/**
* Was the block that was just deleted a shadow?
* @type {boolean}
* @param {!Block=} opt_block The deleted block. Undefined for a blank
* event.
* @alias Blockly.Events.BlockDelete
*/
this.wasShadow = opt_block.isShadow();
constructor(opt_block) {
super(opt_block);
/**
* JSON representation of the block that was just deleted.
* @type {!blocks.State}
*/
this.oldJson = /** @type {!blocks.State} */ (
blocks.save(opt_block, {addCoordinates: true}));
};
object.inherits(BlockDelete, BlockBase);
/**
* Type of this event.
* @type {string}
*/
this.type = eventUtils.BLOCK_DELETE;
/**
* Type of this event.
* @type {string}
*/
BlockDelete.prototype.type = eventUtils.BLOCK_DELETE;
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
BlockDelete.prototype.toJson = function() {
const json = BlockDelete.superClass_.toJson.call(this);
json['oldXml'] = Xml.domToText(this.oldXml);
json['ids'] = this.ids;
json['wasShadow'] = this.wasShadow;
json['oldJson'] = this.oldJson;
if (!this.recordUndo) {
json['recordUndo'] = this.recordUndo;
}
return json;
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
BlockDelete.prototype.fromJson = function(json) {
BlockDelete.superClass_.fromJson.call(this, json);
this.oldXml = Xml.textToDom(json['oldXml']);
this.ids = json['ids'];
this.wasShadow =
json['wasShadow'] || this.oldXml.tagName.toLowerCase() === 'shadow';
this.oldJson = /** @type {!blocks.State} */ (json['oldJson']);
if (json['recordUndo'] !== undefined) {
this.recordUndo = json['recordUndo'];
}
};
/**
* Run a deletion event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
BlockDelete.prototype.run = function(forward) {
const workspace = this.getEventWorkspace_();
if (forward) {
for (let i = 0; i < this.ids.length; i++) {
const id = this.ids[i];
const block = workspace.getBlockById(id);
if (block) {
block.dispose(false);
} else if (id === this.blockId) {
// Only complain about root-level block.
console.warn('Can\'t delete non-existent block: ' + id);
}
if (!opt_block) {
return; // Blank event to be populated by fromJson.
}
} else {
blocks.append(this.oldJson, workspace);
if (opt_block.getParent()) {
throw Error('Connected blocks cannot be deleted.');
}
if (opt_block.isShadow()) {
// Respawning shadow blocks is handled via disconnection.
this.recordUndo = false;
}
this.oldXml = Xml.blockToDomWithXY(opt_block);
this.ids = eventUtils.getDescendantIds(opt_block);
/**
* Was the block that was just deleted a shadow?
* @type {boolean}
*/
this.wasShadow = opt_block.isShadow();
/**
* JSON representation of the block that was just deleted.
* @type {!blocks.State}
*/
this.oldJson = /** @type {!blocks.State} */ (
blocks.save(opt_block, {addCoordinates: true}));
}
};
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
toJson() {
const json = super.toJson();
json['oldXml'] = Xml.domToText(this.oldXml);
json['ids'] = this.ids;
json['wasShadow'] = this.wasShadow;
json['oldJson'] = this.oldJson;
if (!this.recordUndo) {
json['recordUndo'] = this.recordUndo;
}
return json;
}
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
fromJson(json) {
super.fromJson(json);
this.oldXml = Xml.textToDom(json['oldXml']);
this.ids = json['ids'];
this.wasShadow =
json['wasShadow'] || this.oldXml.tagName.toLowerCase() === 'shadow';
this.oldJson = /** @type {!blocks.State} */ (json['oldJson']);
if (json['recordUndo'] !== undefined) {
this.recordUndo = json['recordUndo'];
}
}
/**
* Run a deletion event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
run(forward) {
const workspace = this.getEventWorkspace_();
if (forward) {
for (let i = 0; i < this.ids.length; i++) {
const id = this.ids[i];
const block = workspace.getBlockById(id);
if (block) {
block.dispose(false);
} else if (id === this.blockId) {
// Only complain about root-level block.
console.warn('Can\'t delete non-existent block: ' + id);
}
}
} else {
blocks.append(this.oldJson, workspace);
}
}
}
registry.register(registry.Type.EVENT, eventUtils.DELETE, BlockDelete);

View File

@@ -16,7 +16,6 @@
goog.module('Blockly.Events.BlockDrag');
const eventUtils = goog.require('Blockly.Events.utils');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
/* eslint-disable-next-line no-unused-vars */
const {Block} = goog.requireType('Blockly.Block');
@@ -25,63 +24,65 @@ const {UiBase} = goog.require('Blockly.Events.UiBase');
/**
* Class for a block drag event.
* @param {!Block=} opt_block The top block in the stack that is being
* dragged. Undefined for a blank event.
* @param {boolean=} opt_isStart Whether this is the start of a block drag.
* Undefined for a blank event.
* @param {!Array<!Block>=} opt_blocks The blocks affected by this
* drag. Undefined for a blank event.
* @extends {UiBase}
* @constructor
* @alias Blockly.Events.BlockDrag
*/
const BlockDrag = function(opt_block, opt_isStart, opt_blocks) {
const workspaceId = opt_block ? opt_block.workspace.id : undefined;
BlockDrag.superClass_.constructor.call(this, workspaceId);
this.blockId = opt_block ? opt_block.id : null;
class BlockDrag extends UiBase {
/**
* @param {!Block=} opt_block The top block in the stack that is being
* dragged. Undefined for a blank event.
* @param {boolean=} opt_isStart Whether this is the start of a block drag.
* Undefined for a blank event.
* @param {!Array<!Block>=} opt_blocks The blocks affected by this
* drag. Undefined for a blank event.
* @alias Blockly.Events.BlockDrag
*/
constructor(opt_block, opt_isStart, opt_blocks) {
const workspaceId = opt_block ? opt_block.workspace.id : undefined;
super(workspaceId);
this.blockId = opt_block ? opt_block.id : null;
/**
* Whether this is the start of a block drag.
* @type {boolean|undefined}
*/
this.isStart = opt_isStart;
/**
* The blocks affected by this drag event.
* @type {!Array<!Block>|undefined}
*/
this.blocks = opt_blocks;
/**
* Type of this event.
* @type {string}
*/
this.type = eventUtils.BLOCK_DRAG;
}
/**
* Whether this is the start of a block drag.
* @type {boolean|undefined}
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
this.isStart = opt_isStart;
toJson() {
const json = super.toJson();
json['isStart'] = this.isStart;
json['blockId'] = this.blockId;
json['blocks'] = this.blocks;
return json;
}
/**
* The blocks affected by this drag event.
* @type {!Array<!Block>|undefined}
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
this.blocks = opt_blocks;
};
object.inherits(BlockDrag, UiBase);
/**
* Type of this event.
* @type {string}
*/
BlockDrag.prototype.type = eventUtils.BLOCK_DRAG;
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
BlockDrag.prototype.toJson = function() {
const json = BlockDrag.superClass_.toJson.call(this);
json['isStart'] = this.isStart;
json['blockId'] = this.blockId;
json['blocks'] = this.blocks;
return json;
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
BlockDrag.prototype.fromJson = function(json) {
BlockDrag.superClass_.fromJson.call(this, json);
this.isStart = json['isStart'];
this.blockId = json['blockId'];
this.blocks = json['blocks'];
};
fromJson(json) {
super.fromJson(json);
this.isStart = json['isStart'];
this.blockId = json['blockId'];
this.blocks = json['blocks'];
}
}
registry.register(registry.Type.EVENT, eventUtils.BLOCK_DRAG, BlockDrag);

View File

@@ -16,7 +16,6 @@
goog.module('Blockly.Events.BlockMove');
const eventUtils = goog.require('Blockly.Events.utils');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
const {BlockBase} = goog.require('Blockly.Events.BlockBase');
/* eslint-disable-next-line no-unused-vars */
@@ -27,168 +26,176 @@ const {Coordinate} = goog.require('Blockly.utils.Coordinate');
/**
* Class for a block move event. Created before the move.
* @param {!Block=} opt_block The moved block. Undefined for a blank
* event.
* @extends {BlockBase}
* @constructor
* @alias Blockly.Events.BlockMove
*/
const BlockMove = function(opt_block) {
BlockMove.superClass_.constructor.call(this, opt_block);
if (!opt_block) {
return; // Blank event to be populated by fromJson.
}
if (opt_block.isShadow()) {
// Moving shadow blocks is handled via disconnection.
this.recordUndo = false;
}
class BlockMove extends BlockBase {
/**
* @param {!Block=} opt_block The moved block. Undefined for a blank
* event.
* @alias Blockly.Events.BlockMove
*/
constructor(opt_block) {
super(opt_block);
const location = this.currentLocation_();
this.oldParentId = location.parentId;
this.oldInputName = location.inputName;
this.oldCoordinate = location.coordinate;
};
object.inherits(BlockMove, BlockBase);
/**
* Type of this event.
* @type {string}
*/
this.type = eventUtils.BLOCK_MOVE;
/**
* Type of this event.
* @type {string}
*/
BlockMove.prototype.type = eventUtils.BLOCK_MOVE;
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
BlockMove.prototype.toJson = function() {
const json = BlockMove.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);
}
if (!this.recordUndo) {
json['recordUndo'] = this.recordUndo;
}
return json;
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
BlockMove.prototype.fromJson = function(json) {
BlockMove.superClass_.fromJson.call(this, json);
this.newParentId = json['newParentId'];
this.newInputName = json['newInputName'];
if (json['newCoordinate']) {
const xy = json['newCoordinate'].split(',');
this.newCoordinate = new Coordinate(Number(xy[0]), Number(xy[1]));
}
if (json['recordUndo'] !== undefined) {
this.recordUndo = json['recordUndo'];
}
};
/**
* Record the block's new location. Called after the move.
*/
BlockMove.prototype.recordNew = function() {
const 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
*/
BlockMove.prototype.currentLocation_ = function() {
const workspace = this.getEventWorkspace_();
const block = workspace.getBlockById(this.blockId);
const location = {};
const parent = block.getParent();
if (parent) {
location.parentId = parent.id;
const input = parent.getInputWithBlock(block);
if (input) {
location.inputName = input.name;
if (!opt_block) {
return; // Blank event to be populated by fromJson.
}
if (opt_block.isShadow()) {
// Moving shadow blocks is handled via disconnection.
this.recordUndo = false;
}
} else {
location.coordinate = block.getRelativeToSurfaceXY();
}
return location;
};
/**
* Does this event record any change of state?
* @return {boolean} False if something changed.
*/
BlockMove.prototype.isNull = function() {
return this.oldParentId === this.newParentId &&
this.oldInputName === this.newInputName &&
Coordinate.equals(this.oldCoordinate, this.newCoordinate);
};
const location = this.currentLocation_();
this.oldParentId = location.parentId;
this.oldInputName = location.inputName;
this.oldCoordinate = location.coordinate;
/**
* Run a move event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
BlockMove.prototype.run = function(forward) {
const workspace = this.getEventWorkspace_();
const block = workspace.getBlockById(this.blockId);
if (!block) {
console.warn('Can\'t move non-existent block: ' + this.blockId);
return;
this.newParentId = null;
this.newInputName = null;
this.newCoordinate = null;
}
const parentId = forward ? this.newParentId : this.oldParentId;
const inputName = forward ? this.newInputName : this.oldInputName;
const coordinate = forward ? this.newCoordinate : this.oldCoordinate;
let parentBlock;
if (parentId) {
parentBlock = workspace.getBlockById(parentId);
if (!parentBlock) {
console.warn('Can\'t connect to non-existent block: ' + parentId);
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
toJson() {
const json = super.toJson();
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);
}
if (!this.recordUndo) {
json['recordUndo'] = this.recordUndo;
}
return json;
}
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
fromJson(json) {
super.fromJson(json);
this.newParentId = json['newParentId'];
this.newInputName = json['newInputName'];
if (json['newCoordinate']) {
const xy = json['newCoordinate'].split(',');
this.newCoordinate = new Coordinate(Number(xy[0]), Number(xy[1]));
}
if (json['recordUndo'] !== undefined) {
this.recordUndo = json['recordUndo'];
}
}
/**
* Record the block's new location. Called after the move.
*/
recordNew() {
const 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
*/
currentLocation_() {
const workspace = this.getEventWorkspace_();
const block = workspace.getBlockById(this.blockId);
const location = {};
const parent = block.getParent();
if (parent) {
location.parentId = parent.id;
const 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} False if something changed.
*/
isNull() {
return this.oldParentId === this.newParentId &&
this.oldInputName === this.newInputName &&
Coordinate.equals(this.oldCoordinate, this.newCoordinate);
}
/**
* Run a move event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
run(forward) {
const workspace = this.getEventWorkspace_();
const block = workspace.getBlockById(this.blockId);
if (!block) {
console.warn('Can\'t move non-existent block: ' + this.blockId);
return;
}
}
if (block.getParent()) {
block.unplug();
}
if (coordinate) {
const xy = block.getRelativeToSurfaceXY();
block.moveBy(coordinate.x - xy.x, coordinate.y - xy.y);
} else {
let blockConnection = block.outputConnection;
if (!blockConnection ||
(block.previousConnection && block.previousConnection.isConnected())) {
blockConnection = block.previousConnection;
}
let parentConnection;
const connectionType = blockConnection.type;
if (inputName) {
const input = parentBlock.getInput(inputName);
if (input) {
parentConnection = input.connection;
const parentId = forward ? this.newParentId : this.oldParentId;
const inputName = forward ? this.newInputName : this.oldInputName;
const coordinate = forward ? this.newCoordinate : this.oldCoordinate;
let parentBlock;
if (parentId) {
parentBlock = workspace.getBlockById(parentId);
if (!parentBlock) {
console.warn('Can\'t connect to non-existent block: ' + parentId);
return;
}
} else if (connectionType === ConnectionType.PREVIOUS_STATEMENT) {
parentConnection = parentBlock.nextConnection;
}
if (parentConnection) {
blockConnection.connect(parentConnection);
if (block.getParent()) {
block.unplug();
}
if (coordinate) {
const xy = block.getRelativeToSurfaceXY();
block.moveBy(coordinate.x - xy.x, coordinate.y - xy.y);
} else {
console.warn('Can\'t connect to non-existent input: ' + inputName);
let blockConnection = block.outputConnection;
if (!blockConnection ||
(block.previousConnection &&
block.previousConnection.isConnected())) {
blockConnection = block.previousConnection;
}
let parentConnection;
const connectionType = blockConnection.type;
if (inputName) {
const input = parentBlock.getInput(inputName);
if (input) {
parentConnection = input.connection;
}
} else if (connectionType === ConnectionType.PREVIOUS_STATEMENT) {
parentConnection = parentBlock.nextConnection;
}
if (parentConnection) {
blockConnection.connect(parentConnection);
} else {
console.warn('Can\'t connect to non-existent input: ' + inputName);
}
}
}
};
}
registry.register(registry.Type.EVENT, eventUtils.MOVE, BlockMove);

View File

@@ -52,15 +52,15 @@ const BubbleOpen = function(opt_block, opt_isOpen, opt_bubbleType) {
* @type {string|undefined}
*/
this.bubbleType = opt_bubbleType;
/**
* Type of this event.
* @type {string}
*/
this.type = eventUtils.BUBBLE_OPEN;
};
object.inherits(BubbleOpen, UiBase);
/**
* Type of this event.
* @type {string}
*/
BubbleOpen.prototype.type = eventUtils.BUBBLE_OPEN;
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.

View File

@@ -16,7 +16,6 @@
goog.module('Blockly.Events.Click');
const eventUtils = goog.require('Blockly.Events.utils');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
/* eslint-disable-next-line no-unused-vars */
const {Block} = goog.requireType('Blockly.Block');
@@ -25,58 +24,63 @@ const {UiBase} = goog.require('Blockly.Events.UiBase');
/**
* Class for a click event.
* @param {?Block=} opt_block The affected block. Null for click events
* that do not have an associated block (i.e. workspace click). Undefined
* for a blank event.
* @param {?string=} opt_workspaceId The workspace identifier for this event.
* Not used if block is passed. Undefined for a blank event.
* @param {string=} opt_targetType The type of element targeted by this click
* event. Undefined for a blank event.
* @extends {UiBase}
* @constructor
* @alias Blockly.Events.Click
*/
const Click = function(opt_block, opt_workspaceId, opt_targetType) {
const workspaceId = opt_block ? opt_block.workspace.id : opt_workspaceId;
Click.superClass_.constructor.call(this, workspaceId);
this.blockId = opt_block ? opt_block.id : null;
class Click extends UiBase {
/**
* @param {?Block=} opt_block The affected block. Null for click events
* that do not have an associated block (i.e. workspace click). Undefined
* for a blank event.
* @param {?string=} opt_workspaceId The workspace identifier for this event.
* Not used if block is passed. Undefined for a blank event.
* @param {string=} opt_targetType The type of element targeted by this click
* event. Undefined for a blank event.
* @alias Blockly.Events.Click
*/
constructor(opt_block, opt_workspaceId, opt_targetType) {
let workspaceId = opt_block ? opt_block.workspace.id : opt_workspaceId;
if (workspaceId === null) {
workspaceId = undefined;
}
super(workspaceId);
this.blockId = opt_block ? opt_block.id : null;
/**
* The type of element targeted by this click event.
* @type {string|undefined}
*/
this.targetType = opt_targetType;
/**
* Type of this event.
* @type {string}
*/
this.type = eventUtils.CLICK;
}
/**
* The type of element targeted by this click event.
* @type {string|undefined}
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
this.targetType = opt_targetType;
};
object.inherits(Click, UiBase);
/**
* Type of this event.
* @type {string}
*/
Click.prototype.type = eventUtils.CLICK;
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
Click.prototype.toJson = function() {
const json = Click.superClass_.toJson.call(this);
json['targetType'] = this.targetType;
if (this.blockId) {
json['blockId'] = this.blockId;
toJson() {
const json = super.toJson();
json['targetType'] = this.targetType;
if (this.blockId) {
json['blockId'] = this.blockId;
}
return json;
}
return json;
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
Click.prototype.fromJson = function(json) {
Click.superClass_.fromJson.call(this, json);
this.targetType = json['targetType'];
this.blockId = json['blockId'];
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
fromJson(json) {
super.fromJson(json);
this.targetType = json['targetType'];
this.blockId = json['blockId'];
}
}
registry.register(registry.Type.EVENT, eventUtils.CLICK, Click);

View File

@@ -16,7 +16,6 @@
goog.module('Blockly.Events.CommentChange');
const eventUtils = goog.require('Blockly.Events.utils');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
const {CommentBase} = goog.require('Blockly.Events.CommentBase');
/* eslint-disable-next-line no-unused-vars */
@@ -25,77 +24,80 @@ const {WorkspaceComment} = goog.requireType('Blockly.WorkspaceComment');
/**
* Class for a comment change event.
* @param {!WorkspaceComment=} opt_comment The comment that is being
* changed. Undefined for a blank event.
* @param {string=} opt_oldContents Previous contents of the comment.
* @param {string=} opt_newContents New contents of the comment.
* @extends {CommentBase}
* @constructor
* @alias Blockly.Events.CommentChange
*/
const CommentChange = function(opt_comment, opt_oldContents, opt_newContents) {
CommentChange.superClass_.constructor.call(this, opt_comment);
if (!opt_comment) {
return; // Blank event to be populated by fromJson.
class CommentChange extends CommentBase {
/**
* @param {!WorkspaceComment=} opt_comment The comment that is being
* changed. Undefined for a blank event.
* @param {string=} opt_oldContents Previous contents of the comment.
* @param {string=} opt_newContents New contents of the comment.
* @alias Blockly.Events.CommentChange
*/
constructor(opt_comment, opt_oldContents, opt_newContents) {
super(opt_comment);
/**
* Type of this event.
* @type {string}
*/
this.type = eventUtils.COMMENT_CHANGE;
if (!opt_comment) {
return; // Blank event to be populated by fromJson.
}
this.oldContents_ =
typeof opt_oldContents === 'undefined' ? '' : opt_oldContents;
this.newContents_ =
typeof opt_newContents === 'undefined' ? '' : opt_newContents;
}
this.oldContents_ =
typeof opt_oldContents === 'undefined' ? '' : opt_oldContents;
this.newContents_ =
typeof opt_newContents === 'undefined' ? '' : opt_newContents;
};
object.inherits(CommentChange, CommentBase);
/**
* Type of this event.
* @type {string}
*/
CommentChange.prototype.type = eventUtils.COMMENT_CHANGE;
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
CommentChange.prototype.toJson = function() {
const json = CommentChange.superClass_.toJson.call(this);
json['oldContents'] = this.oldContents_;
json['newContents'] = this.newContents_;
return json;
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
CommentChange.prototype.fromJson = function(json) {
CommentChange.superClass_.fromJson.call(this, json);
this.oldContents_ = json['oldContents'];
this.newContents_ = json['newContents'];
};
/**
* Does this event record any change of state?
* @return {boolean} False if something changed.
*/
CommentChange.prototype.isNull = function() {
return this.oldContents_ === this.newContents_;
};
/**
* Run a change event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
CommentChange.prototype.run = function(forward) {
const workspace = this.getEventWorkspace_();
const comment = workspace.getCommentById(this.commentId);
if (!comment) {
console.warn('Can\'t change non-existent comment: ' + this.commentId);
return;
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
toJson() {
const json = super.toJson();
json['oldContents'] = this.oldContents_;
json['newContents'] = this.newContents_;
return json;
}
const contents = forward ? this.newContents_ : this.oldContents_;
comment.setContent(contents);
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
fromJson(json) {
super.fromJson(json);
this.oldContents_ = json['oldContents'];
this.newContents_ = json['newContents'];
}
/**
* Does this event record any change of state?
* @return {boolean} False if something changed.
*/
isNull() {
return this.oldContents_ === this.newContents_;
}
/**
* Run a change event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
run(forward) {
const workspace = this.getEventWorkspace_();
const comment = workspace.getCommentById(this.commentId);
if (!comment) {
console.warn('Can\'t change non-existent comment: ' + this.commentId);
return;
}
const contents = forward ? this.newContents_ : this.oldContents_;
comment.setContent(contents);
}
}
registry.register(
registry.Type.EVENT, eventUtils.COMMENT_CHANGE, CommentChange);

View File

@@ -17,7 +17,6 @@ goog.module('Blockly.Events.CommentCreate');
const Xml = goog.require('Blockly.Xml');
const eventUtils = goog.require('Blockly.Events.utils');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
const {CommentBase} = goog.require('Blockly.Events.CommentBase');
/* eslint-disable-next-line no-unused-vars */
@@ -26,55 +25,58 @@ const {WorkspaceComment} = goog.requireType('Blockly.WorkspaceComment');
/**
* Class for a comment creation event.
* @param {!WorkspaceComment=} opt_comment The created comment.
* Undefined for a blank event.
* @extends {CommentBase}
* @constructor
* @alias Blockly.Events.CommentCreate
*/
const CommentCreate = function(opt_comment) {
CommentCreate.superClass_.constructor.call(this, opt_comment);
if (!opt_comment) {
return; // Blank event to be populated by fromJson.
class CommentCreate extends CommentBase {
/**
* @param {!WorkspaceComment=} opt_comment The created comment.
* Undefined for a blank event.
* @alias Blockly.Events.CommentCreate
*/
constructor(opt_comment) {
super(opt_comment);
/**
* Type of this event.
* @type {string}
*/
this.type = eventUtils.COMMENT_CREATE;
if (!opt_comment) {
return; // Blank event to be populated by fromJson.
}
this.xml = opt_comment.toXmlWithXY();
}
this.xml = opt_comment.toXmlWithXY();
};
object.inherits(CommentCreate, CommentBase);
// TODO (#1266): "Full" and "minimal" serialization.
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
toJson() {
const json = super.toJson();
json['xml'] = Xml.domToText(this.xml);
return json;
}
/**
* Type of this event.
* @type {string}
*/
CommentCreate.prototype.type = eventUtils.COMMENT_CREATE;
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
fromJson(json) {
super.fromJson(json);
this.xml = Xml.textToDom(json['xml']);
}
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
// TODO (#1266): "Full" and "minimal" serialization.
CommentCreate.prototype.toJson = function() {
const json = CommentCreate.superClass_.toJson.call(this);
json['xml'] = Xml.domToText(this.xml);
return json;
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
CommentCreate.prototype.fromJson = function(json) {
CommentCreate.superClass_.fromJson.call(this, json);
this.xml = Xml.textToDom(json['xml']);
};
/**
* Run a creation event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
CommentCreate.prototype.run = function(forward) {
CommentBase.CommentCreateDeleteHelper(this, forward);
};
/**
* Run a creation event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
run(forward) {
CommentBase.CommentCreateDeleteHelper(this, forward);
}
}
registry.register(
registry.Type.EVENT, eventUtils.COMMENT_CREATE, CommentCreate);

View File

@@ -16,7 +16,6 @@
goog.module('Blockly.Events.CommentDelete');
const eventUtils = goog.require('Blockly.Events.utils');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
const {CommentBase} = goog.require('Blockly.Events.CommentBase');
/* eslint-disable-next-line no-unused-vars */
@@ -25,53 +24,56 @@ const {WorkspaceComment} = goog.requireType('Blockly.WorkspaceComment');
/**
* Class for a comment deletion event.
* @param {!WorkspaceComment=} opt_comment The deleted comment.
* Undefined for a blank event.
* @extends {CommentBase}
* @constructor
* @alias Blockly.Events.CommentDelete
*/
const CommentDelete = function(opt_comment) {
CommentDelete.superClass_.constructor.call(this, opt_comment);
if (!opt_comment) {
return; // Blank event to be populated by fromJson.
class CommentDelete extends CommentBase {
/**
* @param {!WorkspaceComment=} opt_comment The deleted comment.
* Undefined for a blank event.
* @alias Blockly.Events.CommentDelete
*/
constructor(opt_comment) {
super(opt_comment);
/**
* Type of this event.
* @type {string}
*/
this.type = eventUtils.COMMENT_DELETE;
if (!opt_comment) {
return; // Blank event to be populated by fromJson.
}
this.xml = opt_comment.toXmlWithXY();
}
this.xml = opt_comment.toXmlWithXY();
};
object.inherits(CommentDelete, CommentBase);
// TODO (#1266): "Full" and "minimal" serialization.
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
toJson() {
const json = super.toJson();
return json;
}
/**
* Type of this event.
* @type {string}
*/
CommentDelete.prototype.type = eventUtils.COMMENT_DELETE;
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
fromJson(json) {
super.fromJson(json);
}
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
// TODO (#1266): "Full" and "minimal" serialization.
CommentDelete.prototype.toJson = function() {
const json = CommentDelete.superClass_.toJson.call(this);
return json;
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
CommentDelete.prototype.fromJson = function(json) {
CommentDelete.superClass_.fromJson.call(this, json);
};
/**
* Run a creation event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
CommentDelete.prototype.run = function(forward) {
CommentBase.CommentCreateDeleteHelper(this, !forward);
};
/**
* Run a creation event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
run(forward) {
CommentBase.CommentCreateDeleteHelper(this, !forward);
}
}
registry.register(
registry.Type.EVENT, eventUtils.COMMENT_DELETE, CommentDelete);

View File

@@ -16,7 +16,6 @@
goog.module('Blockly.Events.CommentMove');
const eventUtils = goog.require('Blockly.Events.utils');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
const {CommentBase} = goog.require('Blockly.Events.CommentBase');
const {Coordinate} = goog.require('Blockly.utils.Coordinate');
@@ -26,129 +25,132 @@ const {WorkspaceComment} = goog.requireType('Blockly.WorkspaceComment');
/**
* Class for a comment move event. Created before the move.
* @param {!WorkspaceComment=} opt_comment The comment that is being
* moved. Undefined for a blank event.
* @extends {CommentBase}
* @constructor
* @alias Blockly.Events.CommentMove
*/
const CommentMove = function(opt_comment) {
CommentMove.superClass_.constructor.call(this, opt_comment);
if (!opt_comment) {
return; // Blank event to be populated by fromJson.
class CommentMove extends CommentBase {
/**
* @param {!WorkspaceComment=} opt_comment The comment that is being
* moved. Undefined for a blank event.
* @alias Blockly.Events.CommentMove
*/
constructor(opt_comment) {
super(opt_comment);
/**
* Type of this event.
* @type {string}
*/
this.type = eventUtils.COMMENT_MOVE;
if (!opt_comment) {
return; // Blank event to be populated by fromJson.
}
/**
* The comment that is being moved. Will be cleared after recording the new
* location.
* @type {WorkspaceComment}
*/
this.comment_ = opt_comment;
/**
* The location before the move, in workspace coordinates.
* @type {!Coordinate}
*/
this.oldCoordinate_ = opt_comment.getXY();
/**
* The location after the move, in workspace coordinates.
* @type {Coordinate}
*/
this.newCoordinate_ = null;
}
/**
* The comment that is being moved. Will be cleared after recording the new
* location.
* @type {WorkspaceComment}
* Record the comment's new location. Called after the move. Can only be
* called once.
*/
this.comment_ = opt_comment;
recordNew() {
if (!this.comment_) {
throw Error(
'Tried to record the new position of a comment on the ' +
'same event twice.');
}
this.newCoordinate_ = this.comment_.getXY();
this.comment_ = null;
}
/**
* The location before the move, in workspace coordinates.
* @type {!Coordinate}
* Override the location before the move. Use this if you don't create the
* event until the end of the move, but you know the original location.
* @param {!Coordinate} xy The location before the move,
* in workspace coordinates.
*/
this.oldCoordinate_ = opt_comment.getXY();
setOldCoordinate(xy) {
this.oldCoordinate_ = xy;
}
// TODO (#1266): "Full" and "minimal" serialization.
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
toJson() {
const json = super.toJson();
if (this.oldCoordinate_) {
json['oldCoordinate'] = Math.round(this.oldCoordinate_.x) + ',' +
Math.round(this.oldCoordinate_.y);
}
if (this.newCoordinate_) {
json['newCoordinate'] = Math.round(this.newCoordinate_.x) + ',' +
Math.round(this.newCoordinate_.y);
}
return json;
}
/**
* The location after the move, in workspace coordinates.
* @type {Coordinate}
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
this.newCoordinate_ = null;
};
object.inherits(CommentMove, CommentBase);
fromJson(json) {
super.fromJson(json);
/**
* Record the comment's new location. Called after the move. Can only be
* called once.
*/
CommentMove.prototype.recordNew = function() {
if (!this.comment_) {
throw Error(
'Tried to record the new position of a comment on the ' +
'same event twice.');
}
this.newCoordinate_ = this.comment_.getXY();
this.comment_ = null;
};
/**
* Type of this event.
* @type {string}
*/
CommentMove.prototype.type = eventUtils.COMMENT_MOVE;
/**
* Override the location before the move. Use this if you don't create the
* event until the end of the move, but you know the original location.
* @param {!Coordinate} xy The location before the move,
* in workspace coordinates.
*/
CommentMove.prototype.setOldCoordinate = function(xy) {
this.oldCoordinate_ = xy;
};
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
// TODO (#1266): "Full" and "minimal" serialization.
CommentMove.prototype.toJson = function() {
const json = CommentMove.superClass_.toJson.call(this);
if (this.oldCoordinate_) {
json['oldCoordinate'] = Math.round(this.oldCoordinate_.x) + ',' +
Math.round(this.oldCoordinate_.y);
}
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.
*/
CommentMove.prototype.fromJson = function(json) {
CommentMove.superClass_.fromJson.call(this, json);
if (json['oldCoordinate']) {
const xy = json['oldCoordinate'].split(',');
this.oldCoordinate_ = new Coordinate(Number(xy[0]), Number(xy[1]));
}
if (json['newCoordinate']) {
const xy = json['newCoordinate'].split(',');
this.newCoordinate_ = new Coordinate(Number(xy[0]), Number(xy[1]));
}
};
/**
* Does this event record any change of state?
* @return {boolean} False if something changed.
*/
CommentMove.prototype.isNull = function() {
return Coordinate.equals(this.oldCoordinate_, this.newCoordinate_);
};
/**
* Run a move event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
CommentMove.prototype.run = function(forward) {
const workspace = this.getEventWorkspace_();
const comment = workspace.getCommentById(this.commentId);
if (!comment) {
console.warn('Can\'t move non-existent comment: ' + this.commentId);
return;
if (json['oldCoordinate']) {
const xy = json['oldCoordinate'].split(',');
this.oldCoordinate_ = new Coordinate(Number(xy[0]), Number(xy[1]));
}
if (json['newCoordinate']) {
const xy = json['newCoordinate'].split(',');
this.newCoordinate_ = new Coordinate(Number(xy[0]), Number(xy[1]));
}
}
const target = forward ? this.newCoordinate_ : this.oldCoordinate_;
// TODO: Check if the comment is being dragged, and give up if so.
const current = comment.getXY();
comment.moveBy(target.x - current.x, target.y - current.y);
};
/**
* Does this event record any change of state?
* @return {boolean} False if something changed.
*/
isNull() {
return Coordinate.equals(this.oldCoordinate_, this.newCoordinate_);
}
/**
* Run a move event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
run(forward) {
const workspace = this.getEventWorkspace_();
const comment = workspace.getCommentById(this.commentId);
if (!comment) {
console.warn('Can\'t move non-existent comment: ' + this.commentId);
return;
}
const target = forward ? this.newCoordinate_ : this.oldCoordinate_;
// TODO: Check if the comment is being dragged, and give up if so.
const current = comment.getXY();
comment.moveBy(target.x - current.x, target.y - current.y);
}
}
registry.register(registry.Type.EVENT, eventUtils.COMMENT_MOVE, CommentMove);

View File

@@ -16,7 +16,6 @@
goog.module('Blockly.Events.MarkerMove');
const eventUtils = goog.require('Blockly.Events.utils');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
const {ASTNode} = goog.require('Blockly.ASTNode');
/* eslint-disable-next-line no-unused-vars */
@@ -28,81 +27,83 @@ const {Workspace} = goog.requireType('Blockly.Workspace');
/**
* Class for a marker move event.
* @param {?Block=} opt_block The affected block. Null if current node
* is of type workspace. Undefined for a blank event.
* @param {boolean=} isCursor Whether this is a cursor event. Undefined for a
* blank event.
* @param {?ASTNode=} opt_oldNode The old node the marker used to be on.
* Undefined for a blank event.
* @param {!ASTNode=} opt_newNode The new node the marker is now on.
* Undefined for a blank event.
* @extends {UiBase}
* @constructor
* @alias Blockly.Events.MarkerMove
*/
const MarkerMove = function(opt_block, isCursor, opt_oldNode, opt_newNode) {
let workspaceId = opt_block ? opt_block.workspace.id : undefined;
if (opt_newNode && opt_newNode.getType() === ASTNode.types.WORKSPACE) {
workspaceId = (/** @type {!Workspace} */ (opt_newNode.getLocation())).id;
class MarkerMove extends UiBase {
/**
* @param {?Block=} opt_block The affected block. Null if current node
* is of type workspace. Undefined for a blank event.
* @param {boolean=} isCursor Whether this is a cursor event. Undefined for a
* blank event.
* @param {?ASTNode=} opt_oldNode The old node the marker used to be on.
* Undefined for a blank event.
* @param {!ASTNode=} opt_newNode The new node the marker is now on.
* Undefined for a blank event.
* @alias Blockly.Events.MarkerMove
*/
constructor(opt_block, isCursor, opt_oldNode, opt_newNode) {
let workspaceId = opt_block ? opt_block.workspace.id : undefined;
if (opt_newNode && opt_newNode.getType() === ASTNode.types.WORKSPACE) {
workspaceId = (/** @type {!Workspace} */ (opt_newNode.getLocation())).id;
}
super(workspaceId);
/**
* The workspace identifier for this event.
* @type {?string}
*/
this.blockId = opt_block ? opt_block.id : null;
/**
* The old node the marker used to be on.
* @type {?ASTNode|undefined}
*/
this.oldNode = opt_oldNode;
/**
* The new node the marker is now on.
* @type {ASTNode|undefined}
*/
this.newNode = opt_newNode;
/**
* Whether this is a cursor event.
* @type {boolean|undefined}
*/
this.isCursor = isCursor;
/**
* Type of this event.
* @type {string}
*/
this.type = eventUtils.MARKER_MOVE;
}
MarkerMove.superClass_.constructor.call(this, workspaceId);
/**
* The workspace identifier for this event.
* @type {?string}
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
this.blockId = opt_block ? opt_block.id : null;
toJson() {
const json = super.toJson();
json['isCursor'] = this.isCursor;
json['blockId'] = this.blockId;
json['oldNode'] = this.oldNode;
json['newNode'] = this.newNode;
return json;
}
/**
* The old node the marker used to be on.
* @type {?ASTNode|undefined}
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
this.oldNode = opt_oldNode;
/**
* The new node the marker is now on.
* @type {ASTNode|undefined}
*/
this.newNode = opt_newNode;
/**
* Whether this is a cursor event.
* @type {boolean|undefined}
*/
this.isCursor = isCursor;
};
object.inherits(MarkerMove, UiBase);
/**
* Type of this event.
* @type {string}
*/
MarkerMove.prototype.type = eventUtils.MARKER_MOVE;
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
MarkerMove.prototype.toJson = function() {
const json = MarkerMove.superClass_.toJson.call(this);
json['isCursor'] = this.isCursor;
json['blockId'] = this.blockId;
json['oldNode'] = this.oldNode;
json['newNode'] = this.newNode;
return json;
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
MarkerMove.prototype.fromJson = function(json) {
MarkerMove.superClass_.fromJson.call(this, json);
this.isCursor = json['isCursor'];
this.blockId = json['blockId'];
this.oldNode = json['oldNode'];
this.newNode = json['newNode'];
};
fromJson(json) {
super.fromJson(json);
this.isCursor = json['isCursor'];
this.blockId = json['blockId'];
this.oldNode = json['oldNode'];
this.newNode = json['newNode'];
}
}
registry.register(registry.Type.EVENT, eventUtils.MARKER_MOVE, MarkerMove);

View File

@@ -16,66 +16,67 @@
goog.module('Blockly.Events.Selected');
const eventUtils = goog.require('Blockly.Events.utils');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
const {UiBase} = goog.require('Blockly.Events.UiBase');
/**
* Class for a selected event.
* @param {?string=} opt_oldElementId The ID of the previously selected
* element. Null if no element last selected. Undefined for a blank event.
* @param {?string=} opt_newElementId The ID of the selected element. Null if no
* element currently selected (deselect). Undefined for a blank event.
* @param {string=} opt_workspaceId The workspace identifier for this event.
* Null if no element previously selected. Undefined for a blank event.
* @extends {UiBase}
* @constructor
* @alias Blockly.Events.Selected
*/
const Selected = function(opt_oldElementId, opt_newElementId, opt_workspaceId) {
Selected.superClass_.constructor.call(this, opt_workspaceId);
class Selected extends UiBase {
/**
* @param {?string=} opt_oldElementId The ID of the previously selected
* element. Null if no element last selected. Undefined for a blank event.
* @param {?string=} opt_newElementId The ID of the selected element. Null if
* no element currently selected (deselect). Undefined for a blank event.
* @param {string=} opt_workspaceId The workspace identifier for this event.
* Null if no element previously selected. Undefined for a blank event.
* @alias Blockly.Events.Selected
*/
constructor(opt_oldElementId, opt_newElementId, opt_workspaceId) {
super(opt_workspaceId);
/**
* The id of the last selected element.
* @type {?string|undefined}
*/
this.oldElementId = opt_oldElementId;
/**
* The id of the selected element.
* @type {?string|undefined}
*/
this.newElementId = opt_newElementId;
/**
* Type of this event.
* @type {string}
*/
this.type = eventUtils.SELECTED;
}
/**
* The id of the last selected element.
* @type {?string|undefined}
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
this.oldElementId = opt_oldElementId;
toJson() {
const json = super.toJson();
json['oldElementId'] = this.oldElementId;
json['newElementId'] = this.newElementId;
return json;
}
/**
* The id of the selected element.
* @type {?string|undefined}
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
this.newElementId = opt_newElementId;
};
object.inherits(Selected, UiBase);
/**
* Type of this event.
* @type {string}
*/
Selected.prototype.type = eventUtils.SELECTED;
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
Selected.prototype.toJson = function() {
const json = Selected.superClass_.toJson.call(this);
json['oldElementId'] = this.oldElementId;
json['newElementId'] = this.newElementId;
return json;
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
Selected.prototype.fromJson = function(json) {
Selected.superClass_.fromJson.call(this, json);
this.oldElementId = json['oldElementId'];
this.newElementId = json['newElementId'];
};
fromJson(json) {
super.fromJson(json);
this.oldElementId = json['oldElementId'];
this.newElementId = json['newElementId'];
}
}
registry.register(registry.Type.EVENT, eventUtils.SELECTED, Selected);

View File

@@ -16,55 +16,56 @@
goog.module('Blockly.Events.ThemeChange');
const eventUtils = goog.require('Blockly.Events.utils');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
const {UiBase} = goog.require('Blockly.Events.UiBase');
/**
* Class for a theme change event.
* @param {string=} opt_themeName The theme name. Undefined for a blank event.
* @param {string=} opt_workspaceId The workspace identifier for this event.
* event. Undefined for a blank event.
* @extends {UiBase}
* @constructor
* @alias Blockly.Events.ThemeChange
*/
const ThemeChange = function(opt_themeName, opt_workspaceId) {
ThemeChange.superClass_.constructor.call(this, opt_workspaceId);
class ThemeChange extends UiBase {
/**
* @param {string=} opt_themeName The theme name. Undefined for a blank event.
* @param {string=} opt_workspaceId The workspace identifier for this event.
* event. Undefined for a blank event.
* @alias Blockly.Events.ThemeChange
*/
constructor(opt_themeName, opt_workspaceId) {
super(opt_workspaceId);
/**
* The theme name.
* @type {string|undefined}
*/
this.themeName = opt_themeName;
/**
* Type of this event.
* @type {string}
*/
this.type = eventUtils.THEME_CHANGE;
}
/**
* The theme name.
* @type {string|undefined}
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
this.themeName = opt_themeName;
};
object.inherits(ThemeChange, UiBase);
toJson() {
const json = super.toJson();
json['themeName'] = this.themeName;
return json;
}
/**
* Type of this event.
* @type {string}
*/
ThemeChange.prototype.type = eventUtils.THEME_CHANGE;
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
ThemeChange.prototype.toJson = function() {
const json = ThemeChange.superClass_.toJson.call(this);
json['themeName'] = this.themeName;
return json;
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
ThemeChange.prototype.fromJson = function(json) {
ThemeChange.superClass_.fromJson.call(this, json);
this.themeName = json['themeName'];
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
fromJson(json) {
super.fromJson(json);
this.themeName = json['themeName'];
}
}
registry.register(registry.Type.EVENT, eventUtils.THEME_CHANGE, ThemeChange);

View File

@@ -16,66 +16,67 @@
goog.module('Blockly.Events.ToolboxItemSelect');
const eventUtils = goog.require('Blockly.Events.utils');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
const {UiBase} = goog.require('Blockly.Events.UiBase');
/**
* Class for a toolbox item select event.
* @param {?string=} opt_oldItem The previously selected toolbox item. Undefined
* for a blank event.
* @param {?string=} opt_newItem The newly selected toolbox item. Undefined for
* a blank event.
* @param {string=} opt_workspaceId The workspace identifier for this event.
* Undefined for a blank event.
* @extends {UiBase}
* @constructor
* @alias Blockly.Events.ToolboxItemSelect
*/
const ToolboxItemSelect = function(opt_oldItem, opt_newItem, opt_workspaceId) {
ToolboxItemSelect.superClass_.constructor.call(this, opt_workspaceId);
class ToolboxItemSelect extends UiBase {
/**
* @param {?string=} opt_oldItem The previously selected toolbox item.
* Undefined for a blank event.
* @param {?string=} opt_newItem The newly selected toolbox item. Undefined
* for a blank event.
* @param {string=} opt_workspaceId The workspace identifier for this event.
* Undefined for a blank event.
* @alias Blockly.Events.ToolboxItemSelect
*/
constructor(opt_oldItem, opt_newItem, opt_workspaceId) {
super(opt_workspaceId);
/**
* The previously selected toolbox item.
* @type {?string|undefined}
*/
this.oldItem = opt_oldItem;
/**
* The newly selected toolbox item.
* @type {?string|undefined}
*/
this.newItem = opt_newItem;
/**
* Type of this event.
* @type {string}
*/
this.type = eventUtils.TOOLBOX_ITEM_SELECT;
}
/**
* The previously selected toolbox item.
* @type {?string|undefined}
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
this.oldItem = opt_oldItem;
toJson() {
const json = super.toJson();
json['oldItem'] = this.oldItem;
json['newItem'] = this.newItem;
return json;
}
/**
* The newly selected toolbox item.
* @type {?string|undefined}
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
this.newItem = opt_newItem;
};
object.inherits(ToolboxItemSelect, UiBase);
/**
* Type of this event.
* @type {string}
*/
ToolboxItemSelect.prototype.type = eventUtils.TOOLBOX_ITEM_SELECT;
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
ToolboxItemSelect.prototype.toJson = function() {
const json = ToolboxItemSelect.superClass_.toJson.call(this);
json['oldItem'] = this.oldItem;
json['newItem'] = this.newItem;
return json;
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
ToolboxItemSelect.prototype.fromJson = function(json) {
ToolboxItemSelect.superClass_.fromJson.call(this, json);
this.oldItem = json['oldItem'];
this.newItem = json['newItem'];
};
fromJson(json) {
super.fromJson(json);
this.oldItem = json['oldItem'];
this.newItem = json['newItem'];
}
}
registry.register(
registry.Type.EVENT, eventUtils.TOOLBOX_ITEM_SELECT, ToolboxItemSelect);

View File

@@ -16,56 +16,57 @@
goog.module('Blockly.Events.TrashcanOpen');
const eventUtils = goog.require('Blockly.Events.utils');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
const {UiBase} = goog.require('Blockly.Events.UiBase');
/**
* Class for a trashcan open event.
* @param {boolean=} opt_isOpen Whether the trashcan flyout is opening (false if
* opening). Undefined for a blank event.
* @param {string=} opt_workspaceId The workspace identifier for this event.
* Undefined for a blank event.
* @extends {UiBase}
* @constructor
* @alias Blockly.Events.TrashcanOpen
*/
const TrashcanOpen = function(opt_isOpen, opt_workspaceId) {
TrashcanOpen.superClass_.constructor.call(this, opt_workspaceId);
class TrashcanOpen extends UiBase {
/**
* @param {boolean=} opt_isOpen Whether the trashcan flyout is opening (false
* if opening). Undefined for a blank event.
* @param {string=} opt_workspaceId The workspace identifier for this event.
* Undefined for a blank event.
* @alias Blockly.Events.TrashcanOpen
*/
constructor(opt_isOpen, opt_workspaceId) {
super(opt_workspaceId);
/**
* Whether the trashcan flyout is opening (false if closing).
* @type {boolean|undefined}
*/
this.isOpen = opt_isOpen;
/**
* Type of this event.
* @type {string}
*/
this.type = eventUtils.TRASHCAN_OPEN;
}
/**
* Whether the trashcan flyout is opening (false if closing).
* @type {boolean|undefined}
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
this.isOpen = opt_isOpen;
};
object.inherits(TrashcanOpen, UiBase);
toJson() {
const json = super.toJson();
json['isOpen'] = this.isOpen;
return json;
}
/**
* Type of this event.
* @type {string}
*/
TrashcanOpen.prototype.type = eventUtils.TRASHCAN_OPEN;
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
TrashcanOpen.prototype.toJson = function() {
const json = TrashcanOpen.superClass_.toJson.call(this);
json['isOpen'] = this.isOpen;
return json;
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
TrashcanOpen.prototype.fromJson = function(json) {
TrashcanOpen.superClass_.fromJson.call(this, json);
this.isOpen = json['isOpen'];
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
fromJson(json) {
super.fromJson(json);
this.isOpen = json['isOpen'];
}
}
registry.register(registry.Type.EVENT, eventUtils.TRASHCAN_OPEN, TrashcanOpen);

View File

@@ -18,7 +18,6 @@
goog.module('Blockly.Events.Ui');
const eventUtils = goog.require('Blockly.Events.utils');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
/* eslint-disable-next-line no-unused-vars */
const {Block} = goog.requireType('Blockly.Block');
@@ -27,60 +26,62 @@ const {UiBase} = goog.require('Blockly.Events.UiBase');
/**
* Class for a UI event.
* @param {?Block=} opt_block The affected block. Null for UI events
* that do not have an associated block. Undefined for a blank event.
* @param {string=} opt_element One of 'selected', 'comment', 'mutatorOpen',
* etc.
* @param {*=} opt_oldValue Previous value of element.
* @param {*=} opt_newValue New value of element.
* @extends {UiBase}
* @deprecated December 2020. Instead use a more specific UI event.
* @constructor
* @alias Blockly.Events.Ui
*/
const Ui = function(opt_block, opt_element, opt_oldValue, opt_newValue) {
const workspaceId = opt_block ? opt_block.workspace.id : undefined;
Ui.superClass_.constructor.call(this, workspaceId);
class Ui extends UiBase {
/**
* @param {?Block=} opt_block The affected block. Null for UI events
* that do not have an associated block. Undefined for a blank event.
* @param {string=} opt_element One of 'selected', 'comment', 'mutatorOpen',
* etc.
* @param {*=} opt_oldValue Previous value of element.
* @param {*=} opt_newValue New value of element.
* @alias Blockly.Events.Ui
*/
constructor(opt_block, opt_element, opt_oldValue, opt_newValue) {
const workspaceId = opt_block ? opt_block.workspace.id : undefined;
super(workspaceId);
this.blockId = opt_block ? opt_block.id : null;
this.element = typeof opt_element === 'undefined' ? '' : opt_element;
this.oldValue = typeof opt_oldValue === 'undefined' ? '' : opt_oldValue;
this.newValue = typeof opt_newValue === 'undefined' ? '' : opt_newValue;
};
object.inherits(Ui, UiBase);
this.blockId = opt_block ? opt_block.id : null;
this.element = typeof opt_element === 'undefined' ? '' : opt_element;
this.oldValue = typeof opt_oldValue === 'undefined' ? '' : opt_oldValue;
this.newValue = typeof opt_newValue === 'undefined' ? '' : opt_newValue;
/**
* Type of this event.
* @type {string}
*/
Ui.prototype.type = eventUtils.UI;
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
Ui.prototype.toJson = function() {
const json = Ui.superClass_.toJson.call(this);
json['element'] = this.element;
if (this.newValue !== undefined) {
json['newValue'] = this.newValue;
/**
* Type of this event.
* @type {string}
*/
this.type = eventUtils.UI;
}
if (this.blockId) {
json['blockId'] = this.blockId;
}
return json;
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
Ui.prototype.fromJson = function(json) {
Ui.superClass_.fromJson.call(this, json);
this.element = json['element'];
this.newValue = json['newValue'];
this.blockId = json['blockId'];
};
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
toJson() {
const json = super.toJson();
json['element'] = this.element;
if (this.newValue !== undefined) {
json['newValue'] = this.newValue;
}
if (this.blockId) {
json['blockId'] = this.blockId;
}
return json;
}
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
fromJson(json) {
super.fromJson(json);
this.element = json['element'];
this.newValue = json['newValue'];
this.blockId = json['blockId'];
}
}
registry.register(registry.Type.EVENT, eventUtils.UI, Ui);

View File

@@ -50,13 +50,13 @@ const UiBase = function(opt_workspaceId) {
// UI events do not undo or redo.
this.recordUndo = false;
/**
* Whether or not the event is a UI event.
* @type {boolean}
*/
this.isUiEvent = true;
};
object.inherits(UiBase, Abstract);
/**
* Whether or not the event is a UI event.
* @type {boolean}
*/
UiBase.prototype.isUiEvent = true;
exports.UiBase = UiBase;

View File

@@ -16,7 +16,6 @@
goog.module('Blockly.Events.VarCreate');
const eventUtils = goog.require('Blockly.Events.utils');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
const {VarBase} = goog.require('Blockly.Events.VarBase');
/* eslint-disable-next-line no-unused-vars */
@@ -25,62 +24,65 @@ const {VariableModel} = goog.requireType('Blockly.VariableModel');
/**
* Class for a variable creation event.
* @param {!VariableModel=} opt_variable The created variable. Undefined
* for a blank event.
* @extends {VarBase}
* @constructor
* @alias Blockly.Events.VarCreate
*/
const VarCreate = function(opt_variable) {
VarCreate.superClass_.constructor.call(this, opt_variable);
if (!opt_variable) {
return; // Blank event to be populated by fromJson.
class VarCreate extends VarBase {
/**
* @param {!VariableModel=} opt_variable The created variable. Undefined
* for a blank event.
* @alias Blockly.Events.VarCreate
*/
constructor(opt_variable) {
super(opt_variable);
/**
* Type of this event.
* @type {string}
*/
this.type = eventUtils.VAR_CREATE;
if (!opt_variable) {
return; // Blank event to be populated by fromJson.
}
this.varType = opt_variable.type;
this.varName = opt_variable.name;
}
this.varType = opt_variable.type;
this.varName = opt_variable.name;
};
object.inherits(VarCreate, VarBase);
/**
* Type of this event.
* @type {string}
*/
VarCreate.prototype.type = eventUtils.VAR_CREATE;
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
VarCreate.prototype.toJson = function() {
const json = VarCreate.superClass_.toJson.call(this);
json['varType'] = this.varType;
json['varName'] = this.varName;
return json;
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
VarCreate.prototype.fromJson = function(json) {
VarCreate.superClass_.fromJson.call(this, json);
this.varType = json['varType'];
this.varName = json['varName'];
};
/**
* Run a variable creation event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
VarCreate.prototype.run = function(forward) {
const workspace = this.getEventWorkspace_();
if (forward) {
workspace.createVariable(this.varName, this.varType, this.varId);
} else {
workspace.deleteVariableById(this.varId);
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
toJson() {
const json = super.toJson();
json['varType'] = this.varType;
json['varName'] = this.varName;
return json;
}
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
fromJson(json) {
super.fromJson(json);
this.varType = json['varType'];
this.varName = json['varName'];
}
/**
* Run a variable creation event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
run(forward) {
const workspace = this.getEventWorkspace_();
if (forward) {
workspace.createVariable(this.varName, this.varType, this.varId);
} else {
workspace.deleteVariableById(this.varId);
}
}
}
registry.register(registry.Type.EVENT, eventUtils.VAR_CREATE, VarCreate);

View File

@@ -16,7 +16,6 @@
goog.module('Blockly.Events.VarDelete');
const eventUtils = goog.require('Blockly.Events.utils');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
const {VarBase} = goog.require('Blockly.Events.VarBase');
/* eslint-disable-next-line no-unused-vars */
@@ -25,62 +24,65 @@ const {VariableModel} = goog.requireType('Blockly.VariableModel');
/**
* Class for a variable deletion event.
* @param {!VariableModel=} opt_variable The deleted variable. Undefined
* for a blank event.
* @extends {VarBase}
* @constructor
* @alias Blockly.Events.VarDelete
*/
const VarDelete = function(opt_variable) {
VarDelete.superClass_.constructor.call(this, opt_variable);
if (!opt_variable) {
return; // Blank event to be populated by fromJson.
class VarDelete extends VarBase {
/**
* @param {!VariableModel=} opt_variable The deleted variable. Undefined
* for a blank event.
* @alias Blockly.Events.VarDelete
*/
constructor(opt_variable) {
super(opt_variable);
/**
* Type of this event.
* @type {string}
*/
this.type = eventUtils.VAR_DELETE;
if (!opt_variable) {
return; // Blank event to be populated by fromJson.
}
this.varType = opt_variable.type;
this.varName = opt_variable.name;
}
this.varType = opt_variable.type;
this.varName = opt_variable.name;
};
object.inherits(VarDelete, VarBase);
/**
* Type of this event.
* @type {string}
*/
VarDelete.prototype.type = eventUtils.VAR_DELETE;
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
VarDelete.prototype.toJson = function() {
const json = VarDelete.superClass_.toJson.call(this);
json['varType'] = this.varType;
json['varName'] = this.varName;
return json;
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
VarDelete.prototype.fromJson = function(json) {
VarDelete.superClass_.fromJson.call(this, json);
this.varType = json['varType'];
this.varName = json['varName'];
};
/**
* Run a variable deletion event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
VarDelete.prototype.run = function(forward) {
const workspace = this.getEventWorkspace_();
if (forward) {
workspace.deleteVariableById(this.varId);
} else {
workspace.createVariable(this.varName, this.varType, this.varId);
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
toJson() {
const json = super.toJson();
json['varType'] = this.varType;
json['varName'] = this.varName;
return json;
}
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
fromJson(json) {
super.fromJson(json);
this.varType = json['varType'];
this.varName = json['varName'];
}
/**
* Run a variable deletion event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
run(forward) {
const workspace = this.getEventWorkspace_();
if (forward) {
workspace.deleteVariableById(this.varId);
} else {
workspace.createVariable(this.varName, this.varType, this.varId);
}
}
}
registry.register(registry.Type.EVENT, eventUtils.VAR_DELETE, VarDelete);

View File

@@ -16,7 +16,6 @@
goog.module('Blockly.Events.VarRename');
const eventUtils = goog.require('Blockly.Events.utils');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
const {VarBase} = goog.require('Blockly.Events.VarBase');
/* eslint-disable-next-line no-unused-vars */
@@ -25,63 +24,66 @@ const {VariableModel} = goog.requireType('Blockly.VariableModel');
/**
* Class for a variable rename event.
* @param {!VariableModel=} opt_variable The renamed variable. Undefined
* for a blank event.
* @param {string=} newName The new name the variable will be changed to.
* @extends {VarBase}
* @constructor
* @alias Blockly.Events.VarRename
*/
const VarRename = function(opt_variable, newName) {
VarRename.superClass_.constructor.call(this, opt_variable);
if (!opt_variable) {
return; // Blank event to be populated by fromJson.
class VarRename extends VarBase {
/**
* @param {!VariableModel=} opt_variable The renamed variable. Undefined
* for a blank event.
* @param {string=} newName The new name the variable will be changed to.
* @alias Blockly.Events.VarRename
*/
constructor(opt_variable, newName) {
super(opt_variable);
/**
* Type of this event.
* @type {string}
*/
this.type = eventUtils.VAR_RENAME;
if (!opt_variable) {
return; // Blank event to be populated by fromJson.
}
this.oldName = opt_variable.name;
this.newName = typeof newName === 'undefined' ? '' : newName;
}
this.oldName = opt_variable.name;
this.newName = typeof newName === 'undefined' ? '' : newName;
};
object.inherits(VarRename, VarBase);
/**
* Type of this event.
* @type {string}
*/
VarRename.prototype.type = eventUtils.VAR_RENAME;
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
VarRename.prototype.toJson = function() {
const json = VarRename.superClass_.toJson.call(this);
json['oldName'] = this.oldName;
json['newName'] = this.newName;
return json;
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
VarRename.prototype.fromJson = function(json) {
VarRename.superClass_.fromJson.call(this, json);
this.oldName = json['oldName'];
this.newName = json['newName'];
};
/**
* Run a variable rename event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
VarRename.prototype.run = function(forward) {
const workspace = this.getEventWorkspace_();
if (forward) {
workspace.renameVariableById(this.varId, this.newName);
} else {
workspace.renameVariableById(this.varId, this.oldName);
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
toJson() {
const json = super.toJson();
json['oldName'] = this.oldName;
json['newName'] = this.newName;
return json;
}
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
fromJson(json) {
super.fromJson(json);
this.oldName = json['oldName'];
this.newName = json['newName'];
}
/**
* Run a variable rename event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
run(forward) {
const workspace = this.getEventWorkspace_();
if (forward) {
workspace.renameVariableById(this.varId, this.newName);
} else {
workspace.renameVariableById(this.varId, this.oldName);
}
}
}
registry.register(registry.Type.EVENT, eventUtils.VAR_RENAME, VarRename);

View File

@@ -16,89 +16,90 @@
goog.module('Blockly.Events.ViewportChange');
const eventUtils = goog.require('Blockly.Events.utils');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
const {UiBase} = goog.require('Blockly.Events.UiBase');
/**
* Class for a viewport change event.
* @param {number=} opt_top Top-edge of the visible portion of the workspace,
* relative to the workspace origin. Undefined for a blank event.
* @param {number=} opt_left Left-edge of the visible portion of the workspace,
* relative to the workspace origin. Undefined for a blank event.
* @param {number=} opt_scale The scale of the workspace. Undefined for a blank
* event.
* @param {string=} opt_workspaceId The workspace identifier for this event.
* Undefined for a blank event.
* @param {number=} opt_oldScale The old scale of the workspace. Undefined for a
* blank event.
* @extends {UiBase}
* @constructor
* @alias Blockly.Events.ViewportChange
*/
const ViewportChange = function(
opt_top, opt_left, opt_scale, opt_workspaceId, opt_oldScale) {
ViewportChange.superClass_.constructor.call(this, opt_workspaceId);
class ViewportChange extends UiBase {
/**
* @param {number=} opt_top Top-edge of the visible portion of the workspace,
* relative to the workspace origin. Undefined for a blank event.
* @param {number=} opt_left Left-edge of the visible portion of the
* workspace relative to the workspace origin. Undefined for a blank
* event.
* @param {number=} opt_scale The scale of the workspace. Undefined for a
* blank event.
* @param {string=} opt_workspaceId The workspace identifier for this event.
* Undefined for a blank event.
* @param {number=} opt_oldScale The old scale of the workspace. Undefined for
* a blank event.
* @alias Blockly.Events.ViewportChange
*/
constructor(opt_top, opt_left, opt_scale, opt_workspaceId, opt_oldScale) {
super(opt_workspaceId);
/**
* Top-edge of the visible portion of the workspace, relative to the
* workspace origin.
* @type {number|undefined}
*/
this.viewTop = opt_top;
/**
* Left-edge of the visible portion of the workspace, relative to the
* workspace origin.
* @type {number|undefined}
*/
this.viewLeft = opt_left;
/**
* The scale of the workspace.
* @type {number|undefined}
*/
this.scale = opt_scale;
/**
* The old scale of the workspace.
* @type {number|undefined}
*/
this.oldScale = opt_oldScale;
/**
* Type of this event.
* @type {string}
*/
this.type = eventUtils.VIEWPORT_CHANGE;
}
/**
* Top-edge of the visible portion of the workspace, relative to the workspace
* origin.
* @type {number|undefined}
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
this.viewTop = opt_top;
toJson() {
const json = super.toJson();
json['viewTop'] = this.viewTop;
json['viewLeft'] = this.viewLeft;
json['scale'] = this.scale;
json['oldScale'] = this.oldScale;
return json;
}
/**
* Left-edge of the visible portion of the workspace, relative to the
* workspace origin.
* @type {number|undefined}
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
this.viewLeft = opt_left;
/**
* The scale of the workspace.
* @type {number|undefined}
*/
this.scale = opt_scale;
/**
* The old scale of the workspace.
* @type {number|undefined}
*/
this.oldScale = opt_oldScale;
};
object.inherits(ViewportChange, UiBase);
/**
* Type of this event.
* @type {string}
*/
ViewportChange.prototype.type = eventUtils.VIEWPORT_CHANGE;
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
ViewportChange.prototype.toJson = function() {
const json = ViewportChange.superClass_.toJson.call(this);
json['viewTop'] = this.viewTop;
json['viewLeft'] = this.viewLeft;
json['scale'] = this.scale;
json['oldScale'] = this.oldScale;
return json;
};
/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
ViewportChange.prototype.fromJson = function(json) {
ViewportChange.superClass_.fromJson.call(this, json);
this.viewTop = json['viewTop'];
this.viewLeft = json['viewLeft'];
this.scale = json['scale'];
this.oldScale = json['oldScale'];
};
fromJson(json) {
super.fromJson(json);
this.viewTop = json['viewTop'];
this.viewLeft = json['viewLeft'];
this.scale = json['scale'];
this.oldScale = json['oldScale'];
}
}
registry.register(
registry.Type.EVENT, eventUtils.VIEWPORT_CHANGE, ViewportChange);