diff --git a/core/block_dragger.ts b/core/block_dragger.ts index f0200870d..3291c5c74 100644 --- a/core/block_dragger.ts +++ b/core/block_dragger.ts @@ -21,7 +21,7 @@ import * as bumpObjects from './bump_objects.js'; import * as common from './common.js'; import type {BlockMove} from './events/events_block_move.js'; import * as eventUtils from './events/utils.js'; -import type {Icon} from './icon.js'; +import type {Icon} from './icon_old.js'; import {InsertionMarkerManager} from './insertion_marker_manager.js'; import type {IBlockDragger} from './interfaces/i_block_dragger.js'; import type {IDragTarget} from './interfaces/i_drag_target.js'; diff --git a/core/block_svg.ts b/core/block_svg.ts index dd31152ad..c9db3554a 100644 --- a/core/block_svg.ts +++ b/core/block_svg.ts @@ -30,7 +30,7 @@ import type {BlockMove} from './events/events_block_move.js'; import * as eventUtils from './events/utils.js'; import type {Field} from './field.js'; import {FieldLabel} from './field_label.js'; -import type {Icon} from './icon.js'; +import type {Icon} from './icon_old.js'; import type {Input} from './inputs/input.js'; import type {IASTNodeLocationSvg} from './interfaces/i_ast_node_location_svg.js'; import type {IBoundedElement} from './interfaces/i_bounded_element.js'; diff --git a/core/blockly.ts b/core/blockly.ts index 08a5d1c99..975d64941 100644 --- a/core/blockly.ts +++ b/core/blockly.ts @@ -69,7 +69,7 @@ import {VerticalFlyout} from './flyout_vertical.js'; import {CodeGenerator} from './generator.js'; import {Gesture} from './gesture.js'; import {Grid} from './grid.js'; -import {Icon} from './icon.js'; +import {Icon} from './icon_old.js'; import {inject} from './inject.js'; import {Align, Input} from './inputs/input.js'; import {inputTypes} from './inputs/input_types.js'; diff --git a/core/comment.ts b/core/comment.ts index b60d7547e..7468383b3 100644 --- a/core/comment.ts +++ b/core/comment.ts @@ -23,7 +23,7 @@ import * as browserEvents from './browser_events.js'; import {Bubble} from './bubble.js'; import * as Css from './css.js'; import * as eventUtils from './events/utils.js'; -import {Icon} from './icon.js'; +import {Icon} from './icon_old.js'; import type {Coordinate} from './utils/coordinate.js'; import * as dom from './utils/dom.js'; import type {Size} from './utils/size.js'; diff --git a/core/icon.ts b/core/icon_old.ts similarity index 100% rename from core/icon.ts rename to core/icon_old.ts diff --git a/core/icons/icon.ts b/core/icons/icon.ts new file mode 100644 index 000000000..f85195bf4 --- /dev/null +++ b/core/icons/icon.ts @@ -0,0 +1,85 @@ +/** + * @license + * Copyright 2023 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import type {Block} from '../block.js'; +import type {BlockSvg} from '../block_svg.js'; +import * as browserEvents from '../browser_events.js'; +import type {IIcon} from '../interfaces/i_icon.js'; +import {Coordinate} from '../utils/coordinate.js'; +import * as dom from '../utils/dom.js'; +import {Size} from '../utils/size.js'; +import {Svg} from '../utils/svg.js'; + +export abstract class Icon implements IIcon { + /** + * The position of this icon relative to its blocks top-start, + * in workspace units. + */ + protected offsetInBlock: Coordinate = new Coordinate(0, 0); + + /** The position of this icon in workspace coordinates. */ + protected workspaceLocation: Coordinate = new Coordinate(0, 0); + + /** The root svg element visually representing this icon. */ + protected svgRoot: SVGGElement|null = null; + + constructor(protected sourceBlock: Block) {} + + getType(): string { + return 'abstract type'; + } + + initView(pointerdownListener: (e: PointerEvent) => void): void { + if (this.svgRoot) return; // The icon has already been initialized. + + const svgBlock = this.sourceBlock as BlockSvg; + this.svgRoot = dom.createSvgElement(Svg.G, {'class': 'blocklyIconGroup'}); + svgBlock.getSvgRoot().appendChild(this.svgRoot); + browserEvents.conditionalBind( + this.svgRoot, 'pointerdown', this, pointerdownListener); + } + + dispose(): void {} + + getWeight(): number { + return -1; + } + + getSize(): Size { + return new Size(0, 0); + } + + applyColour(): void {} + + updateEditable(): void {} + + updateCollapsed(): void { + if (!this.svgRoot) { + throw new Error( + 'Attempt to update the collapsed-ness of an icon before its ' + + 'view has been initialized.'); + } + if (this.sourceBlock.isCollapsed()) { + this.svgRoot.style.display = 'none'; + } else { + this.svgRoot.style.display = 'block'; + } + } + + isShownWhenCollapsed(): boolean { + return false; + } + + setOffsetInBlock(offset: Coordinate): void { + this.offsetInBlock = offset; + } + + onLocationChange(blockOrigin: Coordinate): void { + this.workspaceLocation = Coordinate.sum(blockOrigin, this.offsetInBlock); + } + + onClick(): void {} +} diff --git a/core/mutator.ts b/core/mutator.ts index 58ce528e9..ec7c817b1 100644 --- a/core/mutator.ts +++ b/core/mutator.ts @@ -25,7 +25,7 @@ import type {Connection} from './connection.js'; import type {Abstract} from './events/events_abstract.js'; import {BlockChange} from './events/events_block_change.js'; import * as eventUtils from './events/utils.js'; -import {Icon} from './icon.js'; +import {Icon} from './icon_old.js'; import {Options} from './options.js'; import type {Coordinate} from './utils/coordinate.js'; import * as dom from './utils/dom.js'; diff --git a/core/renderers/measurables/icon.ts b/core/renderers/measurables/icon.ts index afba526ed..a6d983878 100644 --- a/core/renderers/measurables/icon.ts +++ b/core/renderers/measurables/icon.ts @@ -8,7 +8,7 @@ import * as goog from '../../../closure/goog/goog.js'; goog.declareModuleId('Blockly.blockRendering.Icon'); /* eslint-disable-next-line no-unused-vars */ -import type {Icon as BlocklyIcon} from '../../icon.js'; +import type {Icon as BlocklyIcon} from '../../icon_old.js'; import type {ConstantProvider} from '../common/constants.js'; import {Measurable} from './base.js'; diff --git a/core/warning.ts b/core/warning.ts index 8e80f9234..71bfae85e 100644 --- a/core/warning.ts +++ b/core/warning.ts @@ -18,7 +18,7 @@ import './events/events_bubble_open.js'; import type {BlockSvg} from './block_svg.js'; import {Bubble} from './bubble.js'; import * as eventUtils from './events/utils.js'; -import {Icon} from './icon.js'; +import {Icon} from './icon_old.js'; import type {Coordinate} from './utils/coordinate.js'; import * as dom from './utils/dom.js'; import {Svg} from './utils/svg.js';