Skip to content

Commit

Permalink
Migrate core/events/block_events.js to goog.module
Browse files Browse the repository at this point in the history
  • Loading branch information
kozbial committed Aug 6, 2021
1 parent 17b081d commit b94e6bd
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 64 deletions.
21 changes: 11 additions & 10 deletions core/events/events_block_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
*/
'use strict';

goog.provide('Blockly.Events.BlockBase');
goog.module('Blockly.Events.BlockBase');
goog.module.declareLegacyNamespace();

goog.require('Blockly.Events');
goog.require('Blockly.Events.Abstract');
goog.require('Blockly.Events.BlockBase');
goog.require('Blockly.utils.object');

goog.requireType('Blockly.Block');
Expand All @@ -27,8 +26,8 @@ goog.requireType('Blockly.Block');
* @extends {Blockly.Events.Abstract}
* @constructor
*/
Blockly.Events.BlockBase = function(opt_block) {
Blockly.Events.BlockBase.superClass_.constructor.call(this);
const BlockBase = function(opt_block) {
BlockBase.superClass_.constructor.call(this);
this.isBlank = typeof opt_block == 'undefined';

/**
Expand All @@ -43,15 +42,15 @@ Blockly.Events.BlockBase = function(opt_block) {
*/
this.workspaceId = this.isBlank ? '' : opt_block.workspace.id;
};
Blockly.utils.object.inherits(Blockly.Events.BlockBase,
Blockly.utils.object.inherits(BlockBase,
Blockly.Events.Abstract);

/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
Blockly.Events.BlockBase.prototype.toJson = function() {
const json = Blockly.Events.BlockBase.superClass_.toJson.call(this);
BlockBase.prototype.toJson = function() {
const json = BlockBase.superClass_.toJson.call(this);
json['blockId'] = this.blockId;
return json;
};
Expand All @@ -60,7 +59,9 @@ Blockly.Events.BlockBase.prototype.toJson = function() {
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
Blockly.Events.BlockBase.prototype.fromJson = function(json) {
Blockly.Events.BlockBase.superClass_.fromJson.call(this, json);
BlockBase.prototype.fromJson = function(json) {
BlockBase.superClass_.fromJson.call(this, json);
this.blockId = json['blockId'];
};

exports = BlockBase;
30 changes: 17 additions & 13 deletions core/events/events_block_change.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
*/
'use strict';

goog.provide('Blockly.Events.BlockChange');
goog.module('Blockly.Events.BlockChange');
goog.module.declareLegacyNamespace();

goog.require('Blockly.Events');
goog.require('Blockly.registry');
goog.require('Blockly.utils.object');
goog.require('Blockly.Xml');

Expand All @@ -30,9 +32,9 @@ goog.requireType('Blockly.Block');
* @extends {Blockly.Events.BlockBase}
* @constructor
*/
Blockly.Events.BlockChange = function(opt_block, opt_element, opt_name, opt_oldValue,
const BlockChange = function(opt_block, opt_element, opt_name, opt_oldValue,
opt_newValue) {
Blockly.Events.BlockChange.superClass_.constructor.call(this, opt_block);
BlockChange.superClass_.constructor.call(this, opt_block);
if (!opt_block) {
return; // Blank event to be populated by fromJson.
}
Expand All @@ -41,20 +43,20 @@ Blockly.Events.BlockChange = function(opt_block, opt_element, opt_name, opt_oldV
this.oldValue = typeof opt_oldValue == 'undefined' ? '' : opt_oldValue;
this.newValue = typeof opt_newValue == 'undefined' ? '' : opt_newValue;
};
Blockly.utils.object.inherits(Blockly.Events.BlockChange, Blockly.Events.BlockBase);
Blockly.utils.object.inherits(BlockChange, Blockly.Events.BlockBase);

/**
* Type of this event.
* @type {string}
*/
Blockly.Events.BlockChange.prototype.type = Blockly.Events.BLOCK_CHANGE;
BlockChange.prototype.type = Blockly.Events.BLOCK_CHANGE;

/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
Blockly.Events.BlockChange.prototype.toJson = function() {
const json = Blockly.Events.BlockChange.superClass_.toJson.call(this);
BlockChange.prototype.toJson = function() {
const json = BlockChange.superClass_.toJson.call(this);
json['element'] = this.element;
if (this.name) {
json['name'] = this.name;
Expand All @@ -68,8 +70,8 @@ Blockly.Events.BlockChange.prototype.toJson = function() {
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
Blockly.Events.BlockChange.prototype.fromJson = function(json) {
Blockly.Events.BlockChange.superClass_.fromJson.call(this, json);
BlockChange.prototype.fromJson = function(json) {
BlockChange.superClass_.fromJson.call(this, json);
this.element = json['element'];
this.name = json['name'];
this.oldValue = json['oldValue'];
Expand All @@ -80,15 +82,15 @@ Blockly.Events.BlockChange.prototype.fromJson = function(json) {
* Does this event record any change of state?
* @return {boolean} False if something changed.
*/
Blockly.Events.BlockChange.prototype.isNull = function() {
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).
*/
Blockly.Events.BlockChange.prototype.run = function(forward) {
BlockChange.prototype.run = function(forward) {
const workspace = this.getEventWorkspace_();
const block = workspace.getBlockById(this.blockId);
if (!block) {
Expand Down Expand Up @@ -133,7 +135,7 @@ Blockly.Events.BlockChange.prototype.run = function(forward) {
(value) || '<mutation/>');
block.domToMutation(dom);
}
Blockly.Events.fire(new Blockly.Events.BlockChange(
Blockly.Events.fire(new BlockChange(
block, 'mutation', null, oldMutation, value));
break;
}
Expand All @@ -143,4 +145,6 @@ Blockly.Events.BlockChange.prototype.run = function(forward) {
};

Blockly.registry.register(Blockly.registry.Type.EVENT, Blockly.Events.CHANGE,
Blockly.Events.BlockChange);
BlockChange);

exports = BlockChange;
26 changes: 15 additions & 11 deletions core/events/events_block_create.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
*/
'use strict';

goog.provide('Blockly.Events.BlockCreate');
goog.module('Blockly.Events.BlockCreate');
goog.module.declareLegacyNamespace();

goog.require('Blockly.Events');
goog.require('Blockly.Events.BlockBase');
goog.require('Blockly.registry');
goog.require('Blockly.utils.object');
goog.require('Blockly.utils.xml');
goog.require('Blockly.Xml');
Expand All @@ -28,8 +30,8 @@ goog.requireType('Blockly.Block');
* @extends {Blockly.Events.BlockBase}
* @constructor
*/
Blockly.Events.BlockCreate = function(opt_block) {
Blockly.Events.BlockCreate.superClass_.constructor.call(this, opt_block);
const BlockCreate = function(opt_block) {
BlockCreate.superClass_.constructor.call(this, opt_block);
if (!opt_block) {
return; // Blank event to be populated by fromJson.
}
Expand All @@ -45,20 +47,20 @@ Blockly.Events.BlockCreate = function(opt_block) {
}
this.ids = Blockly.Events.getDescendantIds(opt_block);
};
Blockly.utils.object.inherits(Blockly.Events.BlockCreate, Blockly.Events.BlockBase);
Blockly.utils.object.inherits(BlockCreate, Blockly.Events.BlockBase);

/**
* Type of this event.
* @type {string}
*/
Blockly.Events.BlockCreate.prototype.type = Blockly.Events.BLOCK_CREATE;
BlockCreate.prototype.type = Blockly.Events.BLOCK_CREATE;

/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
Blockly.Events.BlockCreate.prototype.toJson = function() {
const json = Blockly.Events.BlockCreate.superClass_.toJson.call(this);
BlockCreate.prototype.toJson = function() {
const json = BlockCreate.superClass_.toJson.call(this);
json['xml'] = Blockly.Xml.domToText(this.xml);
json['ids'] = this.ids;
if (!this.recordUndo) {
Expand All @@ -71,8 +73,8 @@ Blockly.Events.BlockCreate.prototype.toJson = function() {
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
Blockly.Events.BlockCreate.prototype.fromJson = function(json) {
Blockly.Events.BlockCreate.superClass_.fromJson.call(this, json);
BlockCreate.prototype.fromJson = function(json) {
BlockCreate.superClass_.fromJson.call(this, json);
this.xml = Blockly.Xml.textToDom(json['xml']);
this.ids = json['ids'];
if (json['recordUndo'] !== undefined) {
Expand All @@ -84,7 +86,7 @@ Blockly.Events.BlockCreate.prototype.fromJson = function(json) {
* Run a creation event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
Blockly.Events.BlockCreate.prototype.run = function(forward) {
BlockCreate.prototype.run = function(forward) {
const workspace = this.getEventWorkspace_();
if (forward) {
const xml = Blockly.utils.xml.createElement('xml');
Expand All @@ -105,4 +107,6 @@ Blockly.Events.BlockCreate.prototype.run = function(forward) {
};

Blockly.registry.register(Blockly.registry.Type.EVENT, Blockly.Events.CREATE,
Blockly.Events.BlockCreate);
BlockCreate);

exports = BlockCreate;
25 changes: 14 additions & 11 deletions core/events/events_block_delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
*/
'use strict';

goog.provide('Blockly.Events.BlockDelete');
goog.module('Blockly.Events.BlockDelete');
goog.module.declareLegacyNamespace();

goog.require('Blockly.Events');
goog.require('Blockly.Events.BlockBase');
Expand All @@ -29,8 +30,8 @@ goog.requireType('Blockly.Block');
* @extends {Blockly.Events.BlockBase}
* @constructor
*/
Blockly.Events.BlockDelete = function(opt_block) {
Blockly.Events.BlockDelete.superClass_.constructor.call(this, opt_block);
const BlockDelete = function(opt_block) {
BlockDelete.superClass_.constructor.call(this, opt_block);
if (!opt_block) {
return; // Blank event to be populated by fromJson.
}
Expand All @@ -49,20 +50,20 @@ Blockly.Events.BlockDelete = function(opt_block) {
}
this.ids = Blockly.Events.getDescendantIds(opt_block);
};
Blockly.utils.object.inherits(Blockly.Events.BlockDelete, Blockly.Events.BlockBase);
Blockly.utils.object.inherits(BlockDelete, Blockly.Events.BlockBase);

/**
* Type of this event.
* @type {string}
*/
Blockly.Events.BlockDelete.prototype.type = Blockly.Events.BLOCK_DELETE;
BlockDelete.prototype.type = Blockly.Events.BLOCK_DELETE;

/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
Blockly.Events.BlockDelete.prototype.toJson = function() {
const json = Blockly.Events.BlockDelete.superClass_.toJson.call(this);
BlockDelete.prototype.toJson = function() {
const json = BlockDelete.superClass_.toJson.call(this);
json['oldXml'] = Blockly.Xml.domToText(this.oldXml);
json['ids'] = this.ids;
if (!this.recordUndo) {
Expand All @@ -75,8 +76,8 @@ Blockly.Events.BlockDelete.prototype.toJson = function() {
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
Blockly.Events.BlockDelete.prototype.fromJson = function(json) {
Blockly.Events.BlockDelete.superClass_.fromJson.call(this, json);
BlockDelete.prototype.fromJson = function(json) {
BlockDelete.superClass_.fromJson.call(this, json);
this.oldXml = Blockly.Xml.textToDom(json['oldXml']);
this.ids = json['ids'];
if (json['recordUndo'] !== undefined) {
Expand All @@ -88,7 +89,7 @@ Blockly.Events.BlockDelete.prototype.fromJson = function(json) {
* Run a deletion event.
* @param {boolean} forward True if run forward, false if run backward (undo).
*/
Blockly.Events.BlockDelete.prototype.run = function(forward) {
BlockDelete.prototype.run = function(forward) {
const workspace = this.getEventWorkspace_();
if (forward) {
for (let i = 0; i < this.ids.length; i++) {
Expand All @@ -109,4 +110,6 @@ Blockly.Events.BlockDelete.prototype.run = function(forward) {
};

Blockly.registry.register(Blockly.registry.Type.EVENT, Blockly.Events.DELETE,
Blockly.Events.BlockDelete);
BlockDelete);

exports = BlockDelete;
Loading

0 comments on commit b94e6bd

Please sign in to comment.