Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate core/events/block_events.js to goog.module syntax #5320

Merged
merged 6 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
573 changes: 0 additions & 573 deletions core/events/block_events.js

This file was deleted.

66 changes: 66 additions & 0 deletions core/events/events_block_base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* @license
* Copyright 2018 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @fileoverview Base class for all types of block events.
* @author fenichel@google.com (Rachel Fenichel)
*/
'use strict';

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

const Abstract = goog.require('Blockly.Events.Abstract');
/* eslint-disable-next-line no-unused-vars */
const Block = goog.requireType('Blockly.Block');
const object = goog.require('Blockly.utils.object');


/**
* Abstract class for a block event.
* @param {!Block=} opt_block The block this event corresponds to.
* Undefined for a blank event.
* @extends {Abstract}
* @constructor
*/
const BlockBase = function(opt_block) {
BlockBase.superClass_.constructor.call(this);
this.isBlank = typeof opt_block == 'undefined';

/**
* The block ID for the block this event pertains to
* @type {string}
*/
this.blockId = this.isBlank ? '' : opt_block.id;

/**
* The workspace identifier for this event.
* @type {string}
*/
this.workspaceId = this.isBlank ? '' : opt_block.workspace.id;
};
object.inherits(BlockBase, Abstract);

/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
BlockBase.prototype.toJson = function() {
const json = BlockBase.superClass_.toJson.call(this);
json['blockId'] = this.blockId;
return json;
};

/**
* Decode the JSON event.
* @param {!Object} json JSON representation.
*/
BlockBase.prototype.fromJson = function(json) {
BlockBase.superClass_.fromJson.call(this, json);
this.blockId = json['blockId'];
};

exports = BlockBase;
148 changes: 148 additions & 0 deletions core/events/events_block_change.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/**
* @license
* Copyright 2018 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @fileoverview Class for a block change event.
* @author fenichel@google.com (Rachel Fenichel)
*/
'use strict';

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

/* eslint-disable-next-line no-unused-vars */
const Block = goog.requireType('Blockly.Block');
const Events = goog.require('Blockly.Events');
const Xml = goog.require('Blockly.Xml');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');


/**
* 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 {Events.BlockBase}
* @constructor
*/
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, Events.BlockBase);

/**
* Type of this event.
* @type {string}
*/
BlockChange.prototype.type = Events.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;
}
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': {
let oldMutation = '';
if (block.mutationToDom) {
const oldMutationDom = block.mutationToDom();
oldMutation = oldMutationDom && Xml.domToText(oldMutationDom);
}
if (block.domToMutation) {
const dom = Xml.textToDom(/** @type {string} */
(value) || '<mutation/>');
block.domToMutation(dom);
}
Events.fire(new BlockChange(block, 'mutation', null, oldMutation, value));
break;
}
default:
console.warn('Unknown change type: ' + this.element);
}
};

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

exports = BlockChange;
111 changes: 111 additions & 0 deletions core/events/events_block_create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* @license
* Copyright 2018 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @fileoverview Class for a block creation event.
* @author fenichel@google.com (Rachel Fenichel)
*/
'use strict';

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

/* eslint-disable-next-line no-unused-vars */
const Block = goog.requireType('Blockly.Block');
const BlockBase = goog.require('Blockly.Events.BlockBase');
const Events = goog.require('Blockly.Events');
const Xml = goog.require('Blockly.Xml');
const object = goog.require('Blockly.utils.object');
const registry = goog.require('Blockly.registry');
const xml = goog.require('Blockly.utils.xml');


/**
* Class for a block creation event.
* @param {!Block=} opt_block The created block. Undefined for a blank
* event.
* @extends {BlockBase}
* @constructor
*/
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;
}

if (opt_block.workspace.rendered) {
this.xml = Xml.blockToDomWithXY(opt_block);
} else {
this.xml = Xml.blockToDom(opt_block);
}
this.ids = Events.getDescendantIds(opt_block);
};
object.inherits(BlockCreate, BlockBase);

/**
* Type of this event.
* @type {string}
*/
BlockCreate.prototype.type = Events.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;
if (!this.recordUndo) {
json['recordUndo'] = this.recordUndo;
}
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'];
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) {
const xmlEl = xml.createElement('xml');
xmlEl.appendChild(this.xml);
Xml.domToWorkspace(xmlEl, 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, Events.CREATE, BlockCreate);

exports = BlockCreate;
Loading