Files
blockly/core/renderers/common/path_object.ts
T
Beka Westberg 29e1f0cb03 fix: tsc errors picked up from develop (#6224)
* 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
2022-06-27 09:25:56 -07:00

277 lines
7.7 KiB
TypeScript

/**
* @license
* Copyright 2019 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview An object that owns a block's rendering SVG elements.
*/
/**
* An object that owns a block's rendering SVG elements.
* @class
*/
import * as goog from '../../../closure/goog/goog.js';
goog.declareModuleId('Blockly.blockRendering.PathObject');
/* eslint-disable-next-line no-unused-vars */
// Unused import preserved for side-effects. Remove if unneeded.
import '../../theme.js';
/* eslint-disable-next-line no-unused-vars */
import {BlockSvg} from '../../block_svg.js';
/* eslint-disable-next-line no-unused-vars */
import {Connection} from '../../connection.js';
import {BlockStyle} from '../../theme.js';
import * as dom from '../../utils/dom.js';
import {Svg} from '../../utils/svg.js';
/* eslint-disable-next-line no-unused-vars */
import {ConstantProvider} from './constants.js';
/* eslint-disable-next-line no-unused-vars */
import {IPathObject} from './i_path_object.js';
/**
* An object that handles creating and setting each of the SVG elements
* used by the renderer.
* @alias Blockly.blockRendering.PathObject
*/
export class PathObject implements IPathObject {
svgRoot: AnyDuringMigration;
/** @internal */
svgPath: SVGElement;
/**
* Holds the cursors svg element when the cursor is attached to the block.
* This is null if there is no cursor on the block.
* @internal
*/
// AnyDuringMigration because: Type 'null' is not assignable to type
// 'SVGElement'.
cursorSvg: SVGElement = null as AnyDuringMigration;
/**
* Holds the markers svg element when the marker is attached to the block.
* This is null if there is no marker on the block.
* @internal
*/
// AnyDuringMigration because: Type 'null' is not assignable to type
// 'SVGElement'.
markerSvg: SVGElement = null as AnyDuringMigration;
/** @internal */
constants: ConstantProvider;
/** @internal */
style: BlockStyle;
/**
* @param root The root SVG element.
* @param style The style object to use for colouring.
* @param constants The renderer's constants.
* @internal
*/
constructor(
root: SVGElement, style: BlockStyle, constants: ConstantProvider) {
this.constants = constants;
this.style = style;
this.svgRoot = root;
/** The primary path of the block. */
this.svgPath =
dom.createSvgElement(Svg.PATH, {'class': 'blocklyPath'}, this.svgRoot);
}
/**
* Set the path generated by the renderer onto the respective SVG element.
* @param pathString The path.
* @internal
*/
setPath(pathString: string) {
this.svgPath.setAttribute('d', pathString);
}
/**
* Flip the SVG paths in RTL.
* @internal
*/
flipRTL() {
// Mirror the block's path.
this.svgPath.setAttribute('transform', 'scale(-1 1)');
}
/**
* Add the cursor SVG to this block's SVG group.
* @param cursorSvg The SVG root of the cursor to be added to the block SVG
* group.
* @internal
*/
setCursorSvg(cursorSvg: SVGElement) {
if (!cursorSvg) {
// AnyDuringMigration because: Type 'null' is not assignable to type
// 'SVGElement'.
this.cursorSvg = null as AnyDuringMigration;
return;
}
this.svgRoot.appendChild(cursorSvg);
this.cursorSvg = cursorSvg;
}
/**
* Add the marker SVG to this block's SVG group.
* @param markerSvg The SVG root of the marker to be added to the block SVG
* group.
* @internal
*/
setMarkerSvg(markerSvg: SVGElement) {
if (!markerSvg) {
// AnyDuringMigration because: Type 'null' is not assignable to type
// 'SVGElement'.
this.markerSvg = null as AnyDuringMigration;
return;
}
if (this.cursorSvg) {
this.svgRoot.insertBefore(markerSvg, this.cursorSvg);
} else {
this.svgRoot.appendChild(markerSvg);
}
this.markerSvg = markerSvg;
}
/**
* Apply the stored colours to the block's path, taking into account whether
* the paths belong to a shadow block.
* @param block The source block.
* @internal
*/
applyColour(block: BlockSvg) {
this.svgPath.setAttribute('stroke', this.style.colourTertiary);
this.svgPath.setAttribute('fill', this.style.colourPrimary);
this.updateShadow_(block.isShadow());
this.updateDisabled_(!block.isEnabled() || block.getInheritedDisabled());
}
/**
* Set the style.
* @param blockStyle The block style to use.
* @internal
*/
setStyle(blockStyle: BlockStyle) {
this.style = blockStyle;
}
/**
* Add or remove the given CSS class on the path object's root SVG element.
* @param className The name of the class to add or remove
* @param add True if the class should be added. False if it should be
* removed.
*/
protected setClass_(className: string, add: boolean) {
if (add) {
dom.addClass(this.svgRoot as Element, className);
} else {
dom.removeClass(this.svgRoot as Element, className);
}
}
/**
* Set whether the block shows a highlight or not. Block highlighting is
* often used to visually mark blocks currently being executed.
* @param enable True if highlighted.
* @internal
*/
updateHighlighted(enable: boolean) {
if (enable) {
this.svgPath.setAttribute(
'filter', 'url(#' + this.constants.embossFilterId + ')');
} else {
this.svgPath.setAttribute('filter', 'none');
}
}
/**
* Updates the look of the block to reflect a shadow state.
* @param shadow True if the block is a shadow block.
*/
protected updateShadow_(shadow: boolean) {
if (shadow) {
this.svgPath.setAttribute('stroke', 'none');
this.svgPath.setAttribute('fill', this.style.colourSecondary);
}
}
/**
* Updates the look of the block to reflect a disabled state.
* @param disabled True if disabled.
*/
protected updateDisabled_(disabled: boolean) {
this.setClass_('blocklyDisabled', disabled);
if (disabled) {
this.svgPath.setAttribute(
'fill', 'url(#' + this.constants.disabledPatternId + ')');
}
}
/**
* Add or remove styling showing that a block is selected.
* @param enable True if selection is enabled, false otherwise.
* @internal
*/
updateSelected(enable: boolean) {
this.setClass_('blocklySelected', enable);
}
/**
* Add or remove styling showing that a block is dragged over a delete area.
* @param enable True if the block is being dragged over a delete area, false
* otherwise.
* @internal
*/
updateDraggingDelete(enable: boolean) {
this.setClass_('blocklyDraggingDelete', enable);
}
/**
* Add or remove styling showing that a block is an insertion marker.
* @param enable True if the block is an insertion marker, false otherwise.
* @internal
*/
updateInsertionMarker(enable: boolean) {
this.setClass_('blocklyInsertionMarker', enable);
}
/**
* Add or remove styling showing that a block is movable.
* @param enable True if the block is movable, false otherwise.
* @internal
*/
updateMovable(enable: boolean) {
this.setClass_('blocklyDraggable', enable);
}
/**
* Add or remove styling that shows that if the dragging block is dropped,
* this block will be replaced. If a shadow block, it will disappear.
* Otherwise it will bump.
* @param enable True if styling should be added.
* @internal
*/
updateReplacementFade(enable: boolean) {
this.setClass_('blocklyReplaceable', enable);
}
/**
* Add or remove styling that shows that if the dragging block is dropped,
* this block will be connected to the input.
* @param _conn The connection on the input to highlight.
* @param _enable True if styling should be added.
* @internal
*/
updateShapeForInputHighlight(_conn: Connection, _enable: boolean) {}
}
// NOP