Skip to content

Commit

Permalink
refactor: make Tooltip a class that is accessed via a singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
maribethb committed Feb 23, 2022
1 parent fd6e74a commit b3d543c
Show file tree
Hide file tree
Showing 11 changed files with 426 additions and 466 deletions.
9 changes: 5 additions & 4 deletions core/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
goog.module('Blockly.Block');

const Extensions = goog.require('Blockly.Extensions');
const Tooltip = goog.require('Blockly.Tooltip');
const arrayUtils = goog.require('Blockly.utils.array');
const common = goog.require('Blockly.common');
const constants = goog.require('Blockly.constants');
Expand Down Expand Up @@ -51,6 +50,8 @@ const {VariableModel} = goog.requireType('Blockly.VariableModel');
/* eslint-disable-next-line no-unused-vars */
const {Workspace} = goog.requireType('Blockly.Workspace');
const {inputTypes} = goog.require('Blockly.inputTypes');
/* eslint-disable-next-line no-unused-vars */
const {tooltipManager, TipInfo} = goog.require('Blockly.Tooltip');
/** @suppress {extraRequire} */
goog.require('Blockly.Events.BlockChange');
/** @suppress {extraRequire} */
Expand Down Expand Up @@ -191,7 +192,7 @@ const Block = function(workspace, prototypeName, opt_id) {
* @private
*/
this.disabled = false;
/** @type {!Tooltip.TipInfo} */
/** @type {!TipInfo} */
this.tooltip = '';
/** @type {boolean} */
this.contextMenu = true;
Expand Down Expand Up @@ -990,7 +991,7 @@ Block.prototype.setHelpUrl = function(url) {

/**
* Sets the tooltip for this block.
* @param {!Tooltip.TipInfo} newTip The text for the tooltip, a function
* @param {!TipInfo} newTip The text for the tooltip, a function
* that returns the text for the tooltip, or a parent object whose tooltip
* will be used. To not display a tooltip pass the empty string.
*/
Expand All @@ -1003,7 +1004,7 @@ Block.prototype.setTooltip = function(newTip) {
* @return {!string} The tooltip text for this block.
*/
Block.prototype.getTooltip = function() {
return Tooltip.getTooltipOfObject(this);
return tooltipManager.getTooltipOfObject(this);
};

/**
Expand Down
8 changes: 4 additions & 4 deletions core/block_svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
goog.module('Blockly.BlockSvg');

const ContextMenu = goog.require('Blockly.ContextMenu');
const Tooltip = goog.require('Blockly.Tooltip');
const blockAnimations = goog.require('Blockly.blockAnimations');
const blocks = goog.require('Blockly.serialization.blocks');
const browserEvents = goog.require('Blockly.browserEvents');
Expand Down Expand Up @@ -73,6 +72,7 @@ const {Theme} = goog.requireType('Blockly.Theme');
const {Warning} = goog.requireType('Blockly.Warning');
/* eslint-disable-next-line no-unused-vars */
const {WorkspaceSvg} = goog.requireType('Blockly.WorkspaceSvg');
const {tooltipManager} = goog.require('Blockly.Tooltip');
/** @suppress {extraRequire} */
goog.require('Blockly.Events.BlockMove');
/** @suppress {extraRequire} */
Expand Down Expand Up @@ -240,7 +240,7 @@ const BlockSvg = function(workspace, prototypeName, opt_id) {

const svgPath = this.pathObject.svgPath;
svgPath.tooltip = this;
Tooltip.bindMouseEvents(svgPath);
tooltipManager.bindMouseEvents(svgPath);
BlockSvg.superClass_.constructor.call(this, workspace, prototypeName, opt_id);

// Expose this block's ID on its top-level SVG group.
Expand Down Expand Up @@ -903,8 +903,8 @@ BlockSvg.prototype.dispose = function(healStack, animate) {
// The block has already been deleted.
return;
}
Tooltip.dispose();
Tooltip.unbindMouseEvents(this.pathObject.svgPath);
tooltipManager.dispose();
tooltipManager.unbindMouseEvents(this.pathObject.svgPath);
dom.startTextWidthCache();
// Save the block's workspace temporarily so we can resize the
// contents once the block is disposed.
Expand Down
3 changes: 2 additions & 1 deletion core/blockly.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const Extensions = goog.require('Blockly.Extensions');
const Procedures = goog.require('Blockly.Procedures');
const ShortcutItems = goog.require('Blockly.ShortcutItems');
const Themes = goog.require('Blockly.Themes');
const Tooltip = goog.require('Blockly.Tooltip');
const Touch = goog.require('Blockly.Touch');
const Variables = goog.require('Blockly.Variables');
const VariablesDynamic = goog.require('Blockly.VariablesDynamic');
Expand Down Expand Up @@ -151,6 +150,7 @@ const {ToolboxCategory} = goog.require('Blockly.ToolboxCategory');
const {ToolboxItem} = goog.require('Blockly.ToolboxItem');
const {ToolboxSeparator} = goog.require('Blockly.ToolboxSeparator');
const {Toolbox} = goog.require('Blockly.Toolbox');
const {Tooltip, tooltipManager} = goog.require('Blockly.Tooltip');
const {TouchGesture} = goog.require('Blockly.TouchGesture');
const {Trashcan} = goog.require('Blockly.Trashcan');
const {VariableMap} = goog.require('Blockly.VariableMap');
Expand Down Expand Up @@ -838,6 +838,7 @@ exports.serialization = {
ISerializer: ISerializer,
};
exports.thrasos = thrasos;
exports.tooltipManager = tooltipManager;
exports.uiPosition = uiPosition;
exports.utils = utils;
exports.zelos = zelos;
Expand Down
15 changes: 8 additions & 7 deletions core/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
goog.module('Blockly.Field');

const Tooltip = goog.require('Blockly.Tooltip');
const WidgetDiv = goog.require('Blockly.WidgetDiv');
const Xml = goog.require('Blockly.Xml');
const browserEvents = goog.require('Blockly.browserEvents');
Expand Down Expand Up @@ -55,6 +54,8 @@ const {ShortcutRegistry} = goog.requireType('Blockly.ShortcutRegistry');
const {Size} = goog.require('Blockly.utils.Size');
const {Svg} = goog.require('Blockly.utils.Svg');
/* eslint-disable-next-line no-unused-vars */
const {TipInfo, tooltipManager} = goog.require('Blockly.Tooltip');
/* eslint-disable-next-line no-unused-vars */
const {WorkspaceSvg} = goog.requireType('Blockly.WorkspaceSvg');
/** @suppress {extraRequire} */
goog.require('Blockly.Events.BlockChange');
Expand Down Expand Up @@ -98,7 +99,7 @@ const Field = function(value, opt_validator, opt_config) {
/**
* Used to cache the field's tooltip value if setTooltip is called when the
* field is not yet initialized. Is *not* guaranteed to be accurate.
* @type {?Tooltip.TipInfo}
* @type {?TipInfo}
* @private
*/
this.tooltip_ = null;
Expand Down Expand Up @@ -411,7 +412,7 @@ Field.prototype.createTextElement_ = function() {
* @protected
*/
Field.prototype.bindEvents_ = function() {
Tooltip.bindMouseEvents(this.getClickTarget_());
tooltipManager.bindMouseEvents(this.getClickTarget_());
this.mouseDownWrapper_ = browserEvents.conditionalBind(
this.getClickTarget_(), 'mousedown', this, this.onMouseDown_);
};
Expand Down Expand Up @@ -519,7 +520,7 @@ Field.prototype.loadLegacyState = function(callingClass, state) {
Field.prototype.dispose = function() {
DropDownDiv.hideIfOwner(this);
WidgetDiv.hideIfOwner(this);
Tooltip.unbindMouseEvents(this.getClickTarget_());
tooltipManager.unbindMouseEvents(this.getClickTarget_());

if (this.mouseDownWrapper_) {
browserEvents.unbind(this.mouseDownWrapper_);
Expand Down Expand Up @@ -1052,7 +1053,7 @@ Field.prototype.onMouseDown_ = function(e) {

/**
* Sets the tooltip for this field.
* @param {?Tooltip.TipInfo} newTip The
* @param {?TipInfo} newTip The
* text for the tooltip, a function that returns the text for the tooltip, a
* parent object whose tooltip will be used, or null to display the tooltip
* of the parent block. To not display a tooltip pass the empty string.
Expand All @@ -1077,10 +1078,10 @@ Field.prototype.setTooltip = function(newTip) {
Field.prototype.getTooltip = function() {
const clickTarget = this.getClickTarget_();
if (clickTarget) {
return Tooltip.getTooltipOfObject(clickTarget);
return tooltipManager.getTooltipOfObject(clickTarget);
}
// Field has not been initialized yet. Return stashed this.tooltip_ value.
return Tooltip.getTooltipOfObject({tooltip: this.tooltip_});
return tooltipManager.getTooltipOfObject({tooltip: this.tooltip_});
};

/**
Expand Down
6 changes: 3 additions & 3 deletions core/flyout_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
goog.module('Blockly.Flyout');

const Tooltip = goog.require('Blockly.Tooltip');
const Variables = goog.require('Blockly.Variables');
const Xml = goog.require('Blockly.Xml');
const blocks = goog.require('Blockly.serialization.blocks');
Expand Down Expand Up @@ -44,6 +43,7 @@ const {Rect} = goog.require('Blockly.utils.Rect');
const {ScrollbarPair} = goog.require('Blockly.ScrollbarPair');
const {Svg} = goog.require('Blockly.utils.Svg');
const {WorkspaceSvg} = goog.require('Blockly.WorkspaceSvg');
const {tooltipManager} = goog.require('Blockly.Tooltip');
/** @suppress {extraRequire} */
goog.require('Blockly.Events.BlockCreate');
/** @suppress {extraRequire} */
Expand Down Expand Up @@ -836,7 +836,7 @@ class Flyout extends DeleteArea {
for (let j = 0; j < this.mats_.length; j++) {
const rect = this.mats_[j];
if (rect) {
Tooltip.unbindMouseEvents(rect);
tooltipManager.unbindMouseEvents(rect);
dom.removeNode(rect);
}
}
Expand Down Expand Up @@ -1041,7 +1041,7 @@ class Flyout extends DeleteArea {
},
null);
rect.tooltip = block;
Tooltip.bindMouseEvents(rect);
tooltipManager.bindMouseEvents(rect);
// Add the rectangles under the blocks, so that the blocks' tooltips work.
this.workspace_.getCanvas().insertBefore(rect, block.getSvgRoot());

Expand Down
6 changes: 3 additions & 3 deletions core/gesture.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
goog.module('Blockly.Gesture');

const Tooltip = goog.require('Blockly.Tooltip');
const Touch = goog.require('Blockly.Touch');
const blockAnimations = goog.require('Blockly.blockAnimations');
const browserEvents = goog.require('Blockly.browserEvents');
Expand All @@ -42,6 +41,7 @@ const {WorkspaceDragger} = goog.require('Blockly.WorkspaceDragger');
/* eslint-disable-next-line no-unused-vars */
const {WorkspaceSvg} = goog.requireType('Blockly.WorkspaceSvg');
const {Workspace} = goog.require('Blockly.Workspace');
const {tooltipManager} = goog.require('Blockly.Tooltip');
/** @suppress {extraRequire} */
goog.require('Blockly.BlockDragger');
/** @suppress {extraRequire} */
Expand Down Expand Up @@ -253,7 +253,7 @@ class Gesture {
*/
dispose() {
Touch.clearTouchIdentifier();
Tooltip.unblock();
tooltipManager.unblock();
// Clear the owner's reference to this gesture.
this.creatorWorkspace_.clearGesture();

Expand Down Expand Up @@ -508,7 +508,7 @@ class Gesture {
this.startWorkspace_.markFocused();
this.mostRecentEvent_ = e;

Tooltip.block();
tooltipManager.block();

if (this.targetBlock_) {
this.targetBlock_.select();
Expand Down
4 changes: 2 additions & 2 deletions core/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
goog.module('Blockly.inject');

const Css = goog.require('Blockly.Css');
const Tooltip = goog.require('Blockly.Tooltip');
const Touch = goog.require('Blockly.Touch');
const WidgetDiv = goog.require('Blockly.WidgetDiv');
const aria = goog.require('Blockly.utils.aria');
Expand All @@ -38,6 +37,7 @@ const {Svg} = goog.require('Blockly.utils.Svg');
const {WorkspaceDragSurfaceSvg} = goog.require('Blockly.WorkspaceDragSurfaceSvg');
const {WorkspaceSvg} = goog.require('Blockly.WorkspaceSvg');
const {Workspace} = goog.require('Blockly.Workspace');
const {tooltipManager} = goog.require('Blockly.Tooltip');


/**
Expand Down Expand Up @@ -193,7 +193,7 @@ const createMainWorkspace = function(
common.svgResize(mainWorkspace);
WidgetDiv.createDom();
DropDownDiv.createDom();
Tooltip.createDom();
tooltipManager.createDom();
return mainWorkspace;
};

Expand Down
Loading

0 comments on commit b3d543c

Please sign in to comment.