mirror of
https://github.com/google/blockly.git
synced 2026-01-08 17:40:09 +01:00
* Remove cargo-culted bloat from CSS The `goog-menuitem-icon` and `goog-menuitem-noicon` classes are not present in Blockly. Blockly doesn’t support the CSS compiler, so #noflip has no effect. Shorten uncompressible warning string. Also remove the “Copied from Closure” notes. These were intended so that the CSS could be easily updated as the Closure Library evolved. We are no longer linked to the Closure Library. * Fix bug (in prod) where menu highlighting is lost Previously, open playground. Right-click on workspace. Mouse-over “Add comment” (it highlights). Mouse over “Download screenshot” (disabled option). Mouse over “Add comment” (highlighting is lost). Also remove `canHighlightItem` helper function. In theory this helps abstract the concept of non-highlightable options. But in practice it was only called in one of the several places that it should have been. This was a false abstraction. * Add support for Space/PgUp/PgDn/Home/End to menus * Eliminate calls to clearHighlighted The JSDoc for `setHighlightedIndex` specifically states, “If another item was previously highlighted, it is un-highlighted.” This is not what was implemented, but it should be. This commit adds the un-highlighting, and removes all the calls previously required to correct this bug. * Stop wrapping at top or bottom of menu. Real OS menus don’t wrap when one cursors off the top or bottom. Also, replace the overly complicated helper function with a simple 1/-1 step value. * Remove unused menu code * Simplify menu roles Remove unneeded sets to RTL on Menu (only MenuItem cares). * Fix lack of disposal for context menus. Context menus only disposed properly when an option was clicked. If they were dismissed by clicking outside the menu there was no disposal. This might result in a memory leak. Also un-extract (inject?) several now trivial functions. * Remove Component dependency from Menu & MenuItem Component is now only used by the category tree. * Remove unused functions in Component These were used by Menu/MenuItem. * Fix dependencies. * Record highlighted menu item by object, not index Less code, simpler. * Rename CSS classes goog-menu* to blocklyMenu* Old classes remain in DOM and are deprecated so that any custom CSS will continue to function. * Remove unused focus tracker in tree. * Add support for space/enter to toggle tree cats * Delete unsettable .isUserCollapsible_ from tree * Change visibility tags throughout menus. The previous tags were inherited from Closure and don’t reflect current usage in the Blockly codebase. The core/components/tree files are non-compliant in this regard, but I’m not going to update them since they need to be replaced and there’s no need to create an interim API change. * Remove property on DOM element linking to JS obj Performance is slower (O(n) rather than (O(1)), but ’n’ is the number of entries on the menu, so shouldn’t be more than a dozen or so. * Fixes a compile error (node != element) Usually we avoid parentElement in Blockly. That’s because it has very spotty behaviour with SVG. But in this case we are in pure HTML.
299 lines
7.6 KiB
JavaScript
299 lines
7.6 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2016 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview Class for a button in the flyout.
|
|
* @author fenichel@google.com (Rachel Fenichel)
|
|
*/
|
|
'use strict';
|
|
|
|
goog.provide('Blockly.FlyoutButton');
|
|
|
|
goog.require('Blockly.Css');
|
|
goog.require('Blockly.utils');
|
|
goog.require('Blockly.utils.Coordinate');
|
|
goog.require('Blockly.utils.dom');
|
|
|
|
|
|
/**
|
|
* Class for a button in the flyout.
|
|
* @param {!Blockly.WorkspaceSvg} workspace The workspace in which to place this
|
|
* button.
|
|
* @param {!Blockly.WorkspaceSvg} targetWorkspace The flyout's target workspace.
|
|
* @param {!Element} xml The XML specifying the label/button.
|
|
* @param {boolean} isLabel Whether this button should be styled as a label.
|
|
* @constructor
|
|
*/
|
|
Blockly.FlyoutButton = function(workspace, targetWorkspace, xml, isLabel) {
|
|
// Labels behave the same as buttons, but are styled differently.
|
|
|
|
/**
|
|
* @type {!Blockly.WorkspaceSvg}
|
|
* @private
|
|
*/
|
|
this.workspace_ = workspace;
|
|
|
|
/**
|
|
* @type {!Blockly.WorkspaceSvg}
|
|
* @private
|
|
*/
|
|
this.targetWorkspace_ = targetWorkspace;
|
|
|
|
/**
|
|
* @type {string}
|
|
* @private
|
|
*/
|
|
this.text_ = xml.getAttribute('text');
|
|
|
|
/**
|
|
* @type {!Blockly.utils.Coordinate}
|
|
* @private
|
|
*/
|
|
this.position_ = new Blockly.utils.Coordinate(0, 0);
|
|
|
|
/**
|
|
* Whether this button should be styled as a label.
|
|
* @type {boolean}
|
|
* @private
|
|
*/
|
|
this.isLabel_ = isLabel;
|
|
|
|
/**
|
|
* The key to the function called when this button is clicked.
|
|
* @type {string}
|
|
* @private
|
|
*/
|
|
this.callbackKey_ = xml.getAttribute('callbackKey') ||
|
|
/* Check the lower case version too to satisfy IE */
|
|
xml.getAttribute('callbackkey');
|
|
|
|
/**
|
|
* If specified, a CSS class to add to this button.
|
|
* @type {?string}
|
|
* @private
|
|
*/
|
|
this.cssClass_ = xml.getAttribute('web-class') || null;
|
|
|
|
/**
|
|
* Mouse up event data.
|
|
* @type {?Blockly.EventData}
|
|
* @private
|
|
*/
|
|
this.onMouseUpWrapper_ = null;
|
|
};
|
|
|
|
/**
|
|
* The horizontal margin around the text in the button.
|
|
*/
|
|
Blockly.FlyoutButton.MARGIN_X = 5;
|
|
|
|
/**
|
|
* The vertical margin around the text in the button.
|
|
*/
|
|
Blockly.FlyoutButton.MARGIN_Y = 2;
|
|
|
|
/**
|
|
* The width of the button's rect.
|
|
* @type {number}
|
|
*/
|
|
Blockly.FlyoutButton.prototype.width = 0;
|
|
|
|
/**
|
|
* The height of the button's rect.
|
|
* @type {number}
|
|
*/
|
|
Blockly.FlyoutButton.prototype.height = 0;
|
|
|
|
/**
|
|
* Create the button elements.
|
|
* @return {!SVGElement} The button's SVG group.
|
|
*/
|
|
Blockly.FlyoutButton.prototype.createDom = function() {
|
|
var cssClass = this.isLabel_ ? 'blocklyFlyoutLabel' : 'blocklyFlyoutButton';
|
|
if (this.cssClass_) {
|
|
cssClass += ' ' + this.cssClass_;
|
|
}
|
|
|
|
this.svgGroup_ = Blockly.utils.dom.createSvgElement('g', {'class': cssClass},
|
|
this.workspace_.getCanvas());
|
|
|
|
if (!this.isLabel_) {
|
|
// Shadow rectangle (light source does not mirror in RTL).
|
|
var shadow = Blockly.utils.dom.createSvgElement('rect',
|
|
{
|
|
'class': 'blocklyFlyoutButtonShadow',
|
|
'rx': 4, 'ry': 4, 'x': 1, 'y': 1
|
|
},
|
|
this.svgGroup_);
|
|
}
|
|
// Background rectangle.
|
|
var rect = Blockly.utils.dom.createSvgElement('rect',
|
|
{
|
|
'class': this.isLabel_ ?
|
|
'blocklyFlyoutLabelBackground' : 'blocklyFlyoutButtonBackground',
|
|
'rx': 4, 'ry': 4
|
|
},
|
|
this.svgGroup_);
|
|
|
|
var svgText = Blockly.utils.dom.createSvgElement('text',
|
|
{
|
|
'class': this.isLabel_ ? 'blocklyFlyoutLabelText' : 'blocklyText',
|
|
'x': 0,
|
|
'y': 0,
|
|
'text-anchor': 'middle'
|
|
},
|
|
this.svgGroup_);
|
|
var text = Blockly.utils.replaceMessageReferences(this.text_);
|
|
if (this.workspace_.RTL) {
|
|
// Force text to be RTL by adding an RLM.
|
|
text += '\u200F';
|
|
}
|
|
svgText.textContent = text;
|
|
if (this.isLabel_) {
|
|
this.svgText_ = svgText;
|
|
this.workspace_.getThemeManager().subscribe(this.svgText_,
|
|
'flyoutForegroundColour', 'fill');
|
|
}
|
|
|
|
var fontSize = Blockly.utils.style.getComputedStyle(svgText, 'fontSize');
|
|
var fontWeight = Blockly.utils.style.getComputedStyle(svgText, 'fontWeight');
|
|
var fontFamily = Blockly.utils.style.getComputedStyle(svgText, 'fontFamily');
|
|
this.width = Blockly.utils.dom.getFastTextWidthWithSizeString(svgText,
|
|
fontSize, fontWeight, fontFamily);
|
|
var fontMetrics = Blockly.utils.dom.measureFontMetrics(text, fontSize,
|
|
fontWeight, fontFamily);
|
|
this.height = fontMetrics.height;
|
|
|
|
if (!this.isLabel_) {
|
|
this.width += 2 * Blockly.FlyoutButton.MARGIN_X;
|
|
this.height += 2 * Blockly.FlyoutButton.MARGIN_Y;
|
|
shadow.setAttribute('width', this.width);
|
|
shadow.setAttribute('height', this.height);
|
|
}
|
|
rect.setAttribute('width', this.width);
|
|
rect.setAttribute('height', this.height);
|
|
|
|
svgText.setAttribute('x', this.width / 2);
|
|
svgText.setAttribute('y', this.height / 2 - fontMetrics.height / 2 +
|
|
fontMetrics.baseline);
|
|
|
|
this.updateTransform_();
|
|
|
|
this.onMouseUpWrapper_ = Blockly.bindEventWithChecks_(
|
|
this.svgGroup_, 'mouseup', this, this.onMouseUp_);
|
|
return this.svgGroup_;
|
|
};
|
|
|
|
/**
|
|
* Correctly position the flyout button and make it visible.
|
|
*/
|
|
Blockly.FlyoutButton.prototype.show = function() {
|
|
this.updateTransform_();
|
|
this.svgGroup_.setAttribute('display', 'block');
|
|
};
|
|
|
|
/**
|
|
* Update SVG attributes to match internal state.
|
|
* @private
|
|
*/
|
|
Blockly.FlyoutButton.prototype.updateTransform_ = function() {
|
|
this.svgGroup_.setAttribute('transform',
|
|
'translate(' + this.position_.x + ',' + this.position_.y + ')');
|
|
};
|
|
|
|
/**
|
|
* Move the button to the given x, y coordinates.
|
|
* @param {number} x The new x coordinate.
|
|
* @param {number} y The new y coordinate.
|
|
*/
|
|
Blockly.FlyoutButton.prototype.moveTo = function(x, y) {
|
|
this.position_.x = x;
|
|
this.position_.y = y;
|
|
this.updateTransform_();
|
|
};
|
|
|
|
/**
|
|
* Location of the button.
|
|
* @return {!Blockly.utils.Coordinate} x, y coordinates.
|
|
* @package
|
|
*/
|
|
Blockly.FlyoutButton.prototype.getPosition = function() {
|
|
return this.position_;
|
|
};
|
|
|
|
/**
|
|
* Get the button's target workspace.
|
|
* @return {!Blockly.WorkspaceSvg} The target workspace of the flyout where this
|
|
* button resides.
|
|
*/
|
|
Blockly.FlyoutButton.prototype.getTargetWorkspace = function() {
|
|
return this.targetWorkspace_;
|
|
};
|
|
|
|
/**
|
|
* Dispose of this button.
|
|
*/
|
|
Blockly.FlyoutButton.prototype.dispose = function() {
|
|
if (this.onMouseUpWrapper_) {
|
|
Blockly.unbindEvent_(this.onMouseUpWrapper_);
|
|
}
|
|
if (this.svgGroup_) {
|
|
Blockly.utils.dom.removeNode(this.svgGroup_);
|
|
}
|
|
if (this.svgText_) {
|
|
this.workspace_.getThemeManager().unsubscribe(this.svgText_);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Do something when the button is clicked.
|
|
* @param {!Event} e Mouse up event.
|
|
* @private
|
|
*/
|
|
Blockly.FlyoutButton.prototype.onMouseUp_ = function(e) {
|
|
var gesture = this.targetWorkspace_.getGesture(e);
|
|
if (gesture) {
|
|
gesture.cancel();
|
|
}
|
|
|
|
if (this.isLabel_ && this.callbackKey_) {
|
|
console.warn('Labels should not have callbacks. Label text: ' + this.text_);
|
|
} else if (!this.isLabel_ && !(this.callbackKey_ &&
|
|
this.targetWorkspace_.getButtonCallback(this.callbackKey_))) {
|
|
console.warn('Buttons should have callbacks. Button text: ' + this.text_);
|
|
} else if (!this.isLabel_) {
|
|
this.targetWorkspace_.getButtonCallback(this.callbackKey_)(this);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* CSS for buttons and labels. See css.js for use.
|
|
*/
|
|
Blockly.Css.register([
|
|
/* eslint-disable indent */
|
|
'.blocklyFlyoutButton {',
|
|
'fill: #888;',
|
|
'cursor: default;',
|
|
'}',
|
|
|
|
'.blocklyFlyoutButtonShadow {',
|
|
'fill: #666;',
|
|
'}',
|
|
|
|
'.blocklyFlyoutButton:hover {',
|
|
'fill: #aaa;',
|
|
'}',
|
|
|
|
'.blocklyFlyoutLabel {',
|
|
'cursor: default;',
|
|
'}',
|
|
|
|
'.blocklyFlyoutLabelBackground {',
|
|
'opacity: 0;',
|
|
'}',
|
|
/* eslint-enable indent */
|
|
]);
|