mirror of
https://github.com/google/blockly.git
synced 2026-05-12 15:10:11 +02:00
29e1f0cb03
* fix: relative path for deprecation utils * fix: checking if properties exist in svg_math * fix: set all timeout PIDs to AnyDuringMigration * fix: make nullability errors explicity in block drag surface * fix: make null check in events_block_change explicit * fix: make getEventWorkspace_ internal so we can access it from CommentCreateDeleteHelper * fix: rename DIV -> containerDiv in tooltip * fix: ignore backwards compat check in category * fix: set block styles to AnyDuringMigration * fix: type typo in KeyboardShortcut * fix: constants name in row measurables * fix: typecast in mutator * fix: populateProcedures type of flattened array * fix: ignore errors related to workspace comment deserialization * chore: format files * fix: renaming imports missing file extensions * fix: remove check for sound.play * fix: temporarily remove bad requireType. All `export type` statements are stripped when tsc is run. This means that when we attempt to require BlockDefinition from the block files, we get an error because it does not exist. We decided to temporarily remove the require, because this will no longer be a problem when we conver the blocks to typescript, and everything gets compiled together. * fix: bad jsdoc in array * fix: silence missing property errors Closure was complaining about inexistant properties, but they actually do exist, they're just not being transpiled by tsc in a way that closure understands. I.E. if things are initialized in a function called by the constructor, rather than in a class field or in the custructor itself, closure would error. It would also error on enums, because they are transpiled to a weird IIFE. * fix: context menu action handler not knowing the type of this. this: TypeX information gets stripped when tsc is run, so closure could not know that this was not global. Fixed this by reorganizing to use the option object directly instead of passing it to onAction to be bound to this. * fix: readd getDeveloperVars checks (should not be part of migration) This was found because ALL_DEVELOPER_VARS_WARNINGS_BY_BLOCK_TYPE was no longer being accessed. * fix: silence closure errors about overriding supertype props We propertly define the overrides in typescript, but these get removed from the compiled output, so closure doesn't know they exist. * fix: silence globalThis errors this: TypeX annotations get stripped from the compiled output, so closure can't know that we're accessing the correct things. However, typescript makes sure that this always has the correct properties, so silencing this should be fine. * fix: bad jsdoc name * chore: attempt compiling with blockly.js * fix: attempt moving the import statement above the namespace line * chore: add todo comments to block def files * chore: remove todo from context menu * chore: add comments abotu disabled errors
295 lines
8.2 KiB
TypeScript
295 lines
8.2 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2016 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
/**
|
|
* @fileoverview Class for a button in the flyout.
|
|
*/
|
|
|
|
/**
|
|
* Class for a button in the flyout.
|
|
* @class
|
|
*/
|
|
import * as goog from '../closure/goog/goog.js';
|
|
goog.declareModuleId('Blockly.FlyoutButton');
|
|
|
|
import * as browserEvents from './browser_events.js';
|
|
import * as Css from './css.js';
|
|
import {Coordinate} from './utils/coordinate.js';
|
|
import * as dom from './utils/dom.js';
|
|
import * as parsing from './utils/parsing.js';
|
|
import * as style from './utils/style.js';
|
|
import {Svg} from './utils/svg.js';
|
|
/* eslint-disable-next-line no-unused-vars */
|
|
import * as toolbox from './utils/toolbox.js';
|
|
/* eslint-disable-next-line no-unused-vars */
|
|
import {WorkspaceSvg} from './workspace_svg.js';
|
|
|
|
|
|
/**
|
|
* Class for a button or label in the flyout.
|
|
* @alias Blockly.FlyoutButton
|
|
*/
|
|
export class FlyoutButton {
|
|
/** The horizontal margin around the text in the button. */
|
|
static TEXT_MARGIN_X = 5;
|
|
|
|
/** The vertical margin around the text in the button. */
|
|
static TEXT_MARGIN_Y = 2;
|
|
private readonly text_: string;
|
|
private readonly position_: Coordinate;
|
|
private readonly callbackKey_: string;
|
|
private readonly cssClass_: string|null;
|
|
|
|
/** Mouse up event data. */
|
|
private onMouseUpWrapper_: browserEvents.Data|null = null;
|
|
info: toolbox.ButtonOrLabelInfo;
|
|
|
|
/** The width of the button's rect. */
|
|
width = 0;
|
|
|
|
/** The height of the button's rect. */
|
|
height = 0;
|
|
|
|
/** The root SVG group for the button or label. */
|
|
private svgGroup_: SVGGElement|null = null;
|
|
|
|
/** The SVG element with the text of the label or button. */
|
|
private svgText_: SVGTextElement|null = null;
|
|
|
|
/**
|
|
* @param workspace The workspace in which to place this button.
|
|
* @param targetWorkspace The flyout's target workspace.
|
|
* @param json The JSON specifying the label/button.
|
|
* @param isLabel_ Whether this button should be styled as a label.
|
|
* @internal
|
|
*/
|
|
constructor(
|
|
private readonly workspace: WorkspaceSvg,
|
|
private readonly targetWorkspace: WorkspaceSvg,
|
|
json: toolbox.ButtonOrLabelInfo, private readonly isLabel_: boolean) {
|
|
this.text_ = json['text'];
|
|
|
|
this.position_ = new Coordinate(0, 0);
|
|
|
|
/** The key to the function called when this button is clicked. */
|
|
this.callbackKey_ =
|
|
(json as
|
|
AnyDuringMigration)['callbackKey'] || /* Check the lower case version
|
|
too to satisfy IE */
|
|
(json as AnyDuringMigration)['callbackkey'];
|
|
|
|
/** If specified, a CSS class to add to this button. */
|
|
this.cssClass_ = (json as AnyDuringMigration)['web-class'] || null;
|
|
|
|
/** The JSON specifying the label / button. */
|
|
this.info = json;
|
|
}
|
|
|
|
/**
|
|
* Create the button elements.
|
|
* @return The button's SVG group.
|
|
*/
|
|
createDom(): SVGElement {
|
|
let cssClass = this.isLabel_ ? 'blocklyFlyoutLabel' : 'blocklyFlyoutButton';
|
|
if (this.cssClass_) {
|
|
cssClass += ' ' + this.cssClass_;
|
|
}
|
|
|
|
this.svgGroup_ = dom.createSvgElement(
|
|
Svg.G, {'class': cssClass}, this.workspace.getCanvas());
|
|
|
|
let shadow;
|
|
if (!this.isLabel_) {
|
|
// Shadow rectangle (light source does not mirror in RTL).
|
|
shadow = dom.createSvgElement(
|
|
Svg.RECT, {
|
|
'class': 'blocklyFlyoutButtonShadow',
|
|
'rx': 4,
|
|
'ry': 4,
|
|
'x': 1,
|
|
'y': 1,
|
|
},
|
|
this.svgGroup_!);
|
|
}
|
|
// Background rectangle.
|
|
const rect = dom.createSvgElement(
|
|
Svg.RECT, {
|
|
'class': this.isLabel_ ? 'blocklyFlyoutLabelBackground' :
|
|
'blocklyFlyoutButtonBackground',
|
|
'rx': 4,
|
|
'ry': 4,
|
|
},
|
|
this.svgGroup_!);
|
|
|
|
const svgText = dom.createSvgElement(
|
|
Svg.TEXT, {
|
|
'class': this.isLabel_ ? 'blocklyFlyoutLabelText' : 'blocklyText',
|
|
'x': 0,
|
|
'y': 0,
|
|
'text-anchor': 'middle',
|
|
},
|
|
this.svgGroup_!);
|
|
let text = parsing.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');
|
|
}
|
|
|
|
const fontSize = style.getComputedStyle(svgText, 'fontSize');
|
|
const fontWeight = style.getComputedStyle(svgText, 'fontWeight');
|
|
const fontFamily = style.getComputedStyle(svgText, 'fontFamily');
|
|
this.width = dom.getFastTextWidthWithSizeString(
|
|
svgText, fontSize, fontWeight, fontFamily);
|
|
const fontMetrics =
|
|
dom.measureFontMetrics(text, fontSize, fontWeight, fontFamily);
|
|
this.height = fontMetrics.height;
|
|
|
|
if (!this.isLabel_) {
|
|
this.width += 2 * FlyoutButton.TEXT_MARGIN_X;
|
|
this.height += 2 * FlyoutButton.TEXT_MARGIN_Y;
|
|
shadow?.setAttribute('width', this.width.toString());
|
|
shadow?.setAttribute('height', this.height.toString());
|
|
}
|
|
rect.setAttribute('width', this.width.toString());
|
|
rect.setAttribute('height', this.height.toString());
|
|
|
|
svgText.setAttribute('x', (this.width / 2).toString());
|
|
svgText.setAttribute(
|
|
'y',
|
|
(this.height / 2 - fontMetrics.height / 2 + fontMetrics.baseline)
|
|
.toString());
|
|
|
|
this.updateTransform_();
|
|
|
|
// AnyDuringMigration because: Argument of type 'SVGGElement | null' is not
|
|
// assignable to parameter of type 'EventTarget'.
|
|
this.onMouseUpWrapper_ = browserEvents.conditionalBind(
|
|
this.svgGroup_ as AnyDuringMigration, 'mouseup', this, this.onMouseUp_);
|
|
return this.svgGroup_!;
|
|
}
|
|
|
|
/** Correctly position the flyout button and make it visible. */
|
|
show() {
|
|
this.updateTransform_();
|
|
this.svgGroup_!.setAttribute('display', 'block');
|
|
}
|
|
|
|
/** Update SVG attributes to match internal state. */
|
|
private updateTransform_() {
|
|
this.svgGroup_!.setAttribute(
|
|
'transform',
|
|
'translate(' + this.position_.x + ',' + this.position_.y + ')');
|
|
}
|
|
|
|
/**
|
|
* Move the button to the given x, y coordinates.
|
|
* @param x The new x coordinate.
|
|
* @param y The new y coordinate.
|
|
*/
|
|
moveTo(x: number, y: number) {
|
|
this.position_.x = x;
|
|
this.position_.y = y;
|
|
this.updateTransform_();
|
|
}
|
|
|
|
/** @return Whether or not the button is a label. */
|
|
isLabel(): boolean {
|
|
return this.isLabel_;
|
|
}
|
|
|
|
/**
|
|
* Location of the button.
|
|
* @return x, y coordinates.
|
|
* @internal
|
|
*/
|
|
getPosition(): Coordinate {
|
|
return this.position_;
|
|
}
|
|
|
|
/** @return Text of the button. */
|
|
getButtonText(): string {
|
|
return this.text_;
|
|
}
|
|
|
|
/**
|
|
* Get the button's target workspace.
|
|
* @return The target workspace of the flyout where this button resides.
|
|
*/
|
|
getTargetWorkspace(): WorkspaceSvg {
|
|
return this.targetWorkspace;
|
|
}
|
|
|
|
/** Dispose of this button. */
|
|
dispose() {
|
|
if (this.onMouseUpWrapper_) {
|
|
browserEvents.unbind(this.onMouseUpWrapper_);
|
|
}
|
|
if (this.svgGroup_) {
|
|
dom.removeNode(this.svgGroup_);
|
|
}
|
|
if (this.svgText_) {
|
|
this.workspace.getThemeManager().unsubscribe(this.svgText_);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Do something when the button is clicked.
|
|
* @param e Mouse up event.
|
|
*/
|
|
private onMouseUp_(e: Event) {
|
|
const 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_) {
|
|
const callback =
|
|
this.targetWorkspace.getButtonCallback(this.callbackKey_);
|
|
if (callback) {
|
|
callback(this);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/** CSS for buttons and labels. See css.js for use. */
|
|
Css.register(`
|
|
.blocklyFlyoutButton {
|
|
fill: #888;
|
|
cursor: default;
|
|
}
|
|
|
|
.blocklyFlyoutButtonShadow {
|
|
fill: #666;
|
|
}
|
|
|
|
.blocklyFlyoutButton:hover {
|
|
fill: #aaa;
|
|
}
|
|
|
|
.blocklyFlyoutLabel {
|
|
cursor: default;
|
|
}
|
|
|
|
.blocklyFlyoutLabelBackground {
|
|
opacity: 0;
|
|
}
|
|
`);
|