Skip to content

Commit

Permalink
fix: convert the Workspace class to an ES6 class (google#5977)
Browse files Browse the repository at this point in the history
* fix: run conversion script on workspace

* fix: cleanup from conversion script

* fix: make debug build happy

* fix: tests

* fix: format

* fix: format
  • Loading branch information
BeksOmega authored Mar 16, 2022
1 parent a65b895 commit e2eaebe
Show file tree
Hide file tree
Showing 16 changed files with 743 additions and 711 deletions.
3 changes: 2 additions & 1 deletion core/blockly.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ exports.svgResize = common.svgResize;
* @alias Blockly.hideChaff
*/
const hideChaff = function(opt_onlyClosePopups) {
common.getMainWorkspace().hideChaff(opt_onlyClosePopups);
/** @type {!WorkspaceSvg} */ (common.getMainWorkspace())
.hideChaff(opt_onlyClosePopups);
};
exports.hideChaff = hideChaff;

Expand Down
6 changes: 5 additions & 1 deletion core/events/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const {CommentMove} = goog.requireType('Blockly.Events.CommentMove');
const {ViewportChange} = goog.requireType('Blockly.Events.ViewportChange');
/* eslint-disable-next-line no-unused-vars */
const {Workspace} = goog.requireType('Blockly.Workspace');
/* eslint-disable-next-line no-unused-vars */
const {WorkspaceSvg} = goog.requireType('Blockly.WorkspaceSvg');


/**
Expand Down Expand Up @@ -561,7 +563,9 @@ const disableOrphans = function(event) {
return;
}
const {Workspace} = goog.module.get('Blockly.Workspace');
const eventWorkspace = Workspace.getById(blockEvent.workspaceId);
const eventWorkspace =
/** @type {!WorkspaceSvg} */ (
Workspace.getById(blockEvent.workspaceId));
let block = eventWorkspace.getBlockById(blockEvent.blockId);
if (block) {
// Changing blocks as part of this event shouldn't be undoable.
Expand Down
11 changes: 8 additions & 3 deletions core/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,9 @@ class Field {
if (!this.constants_ && this.sourceBlock_ && this.sourceBlock_.workspace &&
this.sourceBlock_.workspace.rendered) {
this.constants_ =
this.sourceBlock_.workspace.getRenderer().getConstants();
/** @type {!WorkspaceSvg} */ (this.sourceBlock_.workspace)
.getRenderer()
.getConstants();
}
return this.constants_;
}
Expand Down Expand Up @@ -834,7 +836,8 @@ class Field {
// - Webkit / Blink: fill-box / object bounding box
// - Gecko / Triden / EdgeHTML: stroke-box
const bBox = this.sourceBlock_.getHeightWidth();
const scale = this.sourceBlock_.workspace.scale;
const scale =
/** @type {!WorkspaceSvg} */ (this.sourceBlock_.workspace).scale;
xy = this.getAbsoluteXY_();
scaledWidth = bBox.width * scale;
scaledHeight = bBox.height * scale;
Expand Down Expand Up @@ -1072,7 +1075,9 @@ class Field {
if (!this.sourceBlock_ || !this.sourceBlock_.workspace) {
return;
}
const gesture = this.sourceBlock_.workspace.getGesture(e);
const gesture =
/** @type {!WorkspaceSvg} */ (this.sourceBlock_.workspace)
.getGesture(e);
if (gesture) {
gesture.setStartField(this);
}
Expand Down
5 changes: 4 additions & 1 deletion core/gesture.js
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,10 @@ class Gesture {
static inProgress() {
const workspaces = Workspace.getAll();
for (let i = 0, workspace; (workspace = workspaces[i]); i++) {
if (workspace.currentGesture_) {
// Not actually necessarily a WorkspaceSvg, but it doesn't matter b/c
// we're just checking if the property exists. Theoretically we would
// want to use instanceof, but that causes a circular dependency.
if (/** @type {!WorkspaceSvg} */ (workspace).currentGesture_) {
return true;
}
}
Expand Down
5 changes: 3 additions & 2 deletions core/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ const init = function(mainWorkspace) {
// TODO (https://github.com/google/blockly/issues/1998) handle cases where there
// are multiple workspaces and non-main workspaces are able to accept input.
const onKeyDown = function(e) {
const mainWorkspace = common.getMainWorkspace();
const mainWorkspace =
/** @type {!WorkspaceSvg} */ (common.getMainWorkspace());
if (!mainWorkspace) {
return;
}
Expand Down Expand Up @@ -338,7 +339,7 @@ const bindDocumentEvents = function() {
/**
* Load sounds for the given workspace.
* @param {string} pathToMedia The path to the media directory.
* @param {!Workspace} workspace The workspace to load sounds for.
* @param {!WorkspaceSvg} workspace The workspace to load sounds for.
*/
const loadSounds = function(pathToMedia, workspace) {
const audioMgr = workspace.getAudioManager();
Expand Down
6 changes: 2 additions & 4 deletions core/mutator.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ const {Icon} = goog.require('Blockly.Icon');
const {Options} = goog.require('Blockly.Options');
const {Svg} = goog.require('Blockly.utils.Svg');
const {WorkspaceSvg} = goog.require('Blockly.WorkspaceSvg');
/* eslint-disable-next-line no-unused-vars */
const {Workspace} = goog.requireType('Blockly.Workspace');
/** @suppress {extraRequire} */
goog.require('Blockly.Events.BubbleOpen');

Expand Down Expand Up @@ -550,8 +548,8 @@ class Mutator extends Icon {
/**
* Get the parent workspace of a workspace that is inside a mutator, taking
* into account whether it is a flyout.
* @param {Workspace} workspace The workspace that is inside a mutator.
* @return {?Workspace} The mutator's parent workspace or null.
* @param {WorkspaceSvg} workspace The workspace that is inside a mutator.
* @return {?WorkspaceSvg} The mutator's parent workspace or null.
* @public
*/
static findParentWs(workspace) {
Expand Down
4 changes: 2 additions & 2 deletions core/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ exports.rename = rename;

/**
* Construct the blocks required by the flyout for the procedure category.
* @param {!Workspace} workspace The workspace containing procedures.
* @param {!WorkspaceSvg} workspace The workspace containing procedures.
* @return {!Array<!Element>} Array of XML block elements.
* @alias Blockly.Procedures.flyoutCategory
*/
Expand Down Expand Up @@ -297,7 +297,7 @@ exports.flyoutCategory = flyoutCategory;
/**
* Updates the procedure mutator's flyout so that the arg block is not a
* duplicate of another arg.
* @param {!Workspace} workspace The procedure mutator's workspace. This
* @param {!WorkspaceSvg} workspace The procedure mutator's workspace. This
* workspace's flyout is what is being updated.
*/
const updateMutatorFlyout = function(workspace) {
Expand Down
5 changes: 3 additions & 2 deletions core/serialization/workspaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const eventUtils = goog.require('Blockly.Events.utils');
const registry = goog.require('Blockly.registry');
// eslint-disable-next-line no-unused-vars
const {Workspace} = goog.require('Blockly.Workspace');
const {WorkspaceSvg} = goog.require('Blockly.WorkspaceSvg');


/**
Expand Down Expand Up @@ -70,7 +71,7 @@ const load = function(state, workspace, {recordUndo = false} = {}) {
}

dom.startTextWidthCache();
if (workspace.setResizesEnabled) {
if (workspace instanceof WorkspaceSvg) {
workspace.setResizesEnabled(false);
}

Expand All @@ -89,7 +90,7 @@ const load = function(state, workspace, {recordUndo = false} = {}) {
}
}

if (workspace.setResizesEnabled) {
if (workspace instanceof WorkspaceSvg) {
workspace.setResizesEnabled(true);
}
dom.stopTextWidthCache();
Expand Down
4 changes: 2 additions & 2 deletions core/theme_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class ThemeManager {
// Refresh all subscribed workspaces.
for (let i = 0, workspace; (workspace = this.subscribedWorkspaces_[i]);
i++) {
workspace.refreshTheme();
/** @type {!WorkspaceSvg} */ (workspace).refreshTheme();
}

// Refresh all registered Blockly UI components.
Expand All @@ -112,7 +112,7 @@ class ThemeManager {
}

for (const workspace of this.subscribedWorkspaces_) {
workspace.hideChaff();
/** @type {!WorkspaceSvg} */ (workspace).hideChaff();
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/toolbox/toolbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ class Toolbox extends DeleteArea {
onClick_(e) {
if (browserEvents.isRightButton(e) || e.target === this.HtmlDiv) {
// Close flyout.
common.getMainWorkspace().hideChaff(false);
/** @type {!WorkspaceSvg} */ (common.getMainWorkspace()).hideChaff(false);
} else {
const targetElement = e.target;
const itemId = targetElement.getAttribute('id');
Expand All @@ -332,7 +332,7 @@ class Toolbox extends DeleteArea {
}
}
// Just close popups.
common.getMainWorkspace().hideChaff(true);
/** @type {!WorkspaceSvg} */ (common.getMainWorkspace()).hideChaff(true);
}
Touch.clearTouchIdentifier(); // Don't block future drags.
}
Expand Down
11 changes: 9 additions & 2 deletions core/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const {Msg} = goog.require('Blockly.Msg');
const {VariableModel} = goog.require('Blockly.VariableModel');
/* eslint-disable-next-line no-unused-vars */
const {Workspace} = goog.requireType('Blockly.Workspace');
/* eslint-disable-next-line no-unused-vars */
const {WorkspaceSvg} = goog.requireType('Blockly.WorkspaceSvg');

/**
* String for use in the "custom" attribute of a category in toolbox XML.
Expand Down Expand Up @@ -118,7 +120,7 @@ exports.allDeveloperVariables = allDeveloperVariables;
/**
* Construct the elements (blocks and button) required by the flyout for the
* variable category.
* @param {!Workspace} workspace The workspace containing variables.
* @param {!WorkspaceSvg} workspace The workspace containing variables.
* @return {!Array<!Element>} Array of XML elements.
* @alias Blockly.Variables.flyoutCategory
*/
Expand Down Expand Up @@ -526,7 +528,12 @@ const createVariable = function(workspace, id, opt_name, opt_type) {
const potentialVariableMap = workspace.getPotentialVariableMap();
// Variables without names get uniquely named for this workspace.
if (!opt_name) {
const ws = workspace.isFlyout ? workspace.targetWorkspace : workspace;
const ws =
/** @type {!Workspace} */ (
workspace.isFlyout ?
/** @type {!WorkspaceSvg} */ (workspace).targetWorkspace :
workspace);
// Must call version on exports to allow for mocking in tests. See #5321
opt_name = exports.generateUniqueName(ws);
}

Expand Down
4 changes: 3 additions & 1 deletion core/variables_dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const {Msg} = goog.require('Blockly.Msg');
const {VariableModel} = goog.require('Blockly.VariableModel');
/* eslint-disable-next-line no-unused-vars */
const {Workspace} = goog.requireType('Blockly.Workspace');
/* eslint-disable-next-line no-unused-vars */
const {WorkspaceSvg} = goog.requireType('Blockly.WorkspaceSvg');


/**
Expand Down Expand Up @@ -59,7 +61,7 @@ exports.onCreateVariableButtonClick_Colour = colourButtonClickHandler;
/**
* Construct the elements (blocks and button) required by the flyout for the
* variable category.
* @param {!Workspace} workspace The workspace containing variables.
* @param {!WorkspaceSvg} workspace The workspace containing variables.
* @return {!Array<!Element>} Array of XML elements.
* @alias Blockly.VariablesDynamic.flyoutCategory
*/
Expand Down
Loading

0 comments on commit e2eaebe

Please sign in to comment.