From 395b5a608c0f135cb72aada431529e610671ae21 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Thu, 21 Sep 2023 15:18:05 +0000 Subject: [PATCH 01/10] chore: delete dead code --- core/block_svg.ts | 40 ------------------------------------- core/rendered_connection.ts | 22 -------------------- 2 files changed, 62 deletions(-) diff --git a/core/block_svg.ts b/core/block_svg.ts index b419122ba58..a332f6400cb 100644 --- a/core/block_svg.ts +++ b/core/block_svg.ts @@ -1628,46 +1628,6 @@ export class BlockSvg } } - /** - * Update all of the connections on this block with the new locations - * calculated during rendering. Also move all of the connected blocks based - * on the new connection locations. - * - * @internal - */ - private updateConnectionAndIconLocations() { - const blockTL = this.getRelativeToSurfaceXY(); - // Don't tighten previous or output connections because they are inferior - // connections. - if (this.previousConnection) { - this.previousConnection.moveToOffset(blockTL); - } - if (this.outputConnection) { - this.outputConnection.moveToOffset(blockTL); - } - - for (let i = 0; i < this.inputList.length; i++) { - const conn = this.inputList[i].connection as RenderedConnection; - if (conn) { - conn.moveToOffset(blockTL); - if (conn.isConnected()) { - conn.tighten(); - } - } - } - - if (this.nextConnection) { - this.nextConnection.moveToOffset(blockTL); - if (this.nextConnection.isConnected()) { - this.nextConnection.tighten(); - } - } - - for (const icon of this.getIcons()) { - icon.onLocationChange(blockTL); - } - } - /** * Add the cursor SVG to this block's SVG group. * diff --git a/core/rendered_connection.ts b/core/rendered_connection.ts index 3a1c3238514..08c0471a2e6 100644 --- a/core/rendered_connection.ts +++ b/core/rendered_connection.ts @@ -24,7 +24,6 @@ import * as internalConstants from './internal_constants.js'; import {Coordinate} from './utils/coordinate.js'; import * as dom from './utils/dom.js'; import {Svg} from './utils/svg.js'; -import * as svgMath from './utils/svg_math.js'; import * as svgPaths from './utils/svg_paths.js'; /** A shape that has a pathDown property. */ @@ -270,27 +269,6 @@ export class RenderedConnection extends Connection { return this.offsetInBlock; } - /** - * Move the blocks on either side of this connection right next to each other. - * - * @internal - */ - tighten() { - const dx = this.targetConnection!.x - this.x; - const dy = this.targetConnection!.y - this.y; - if (dx !== 0 || dy !== 0) { - const block = this.targetBlock(); - const svgRoot = block!.getSvgRoot(); - if (!svgRoot) { - throw Error('block is not rendered.'); - } - // Workspace coordinates. - const xy = svgMath.getRelativeXY(svgRoot); - block!.translate(xy.x - dx, xy.y - dy); - block!.moveConnections(-dx, -dy); - } - } - /** * Moves the blocks on either side of this connection right next to * each other, based on their local offsets, not global positions. From 8e02cb72ddd49dd2dcd32cdd27b363a2f5bd2610 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Thu, 21 Sep 2023 15:40:21 +0000 Subject: [PATCH 02/10] chore: moves location updating into the block --- core/block_svg.ts | 48 +++++++++++++++++++++++---------------- core/render_management.ts | 45 +----------------------------------- 2 files changed, 30 insertions(+), 63 deletions(-) diff --git a/core/block_svg.ts b/core/block_svg.ts index a332f6400cb..e2330f54e47 100644 --- a/core/block_svg.ts +++ b/core/block_svg.ts @@ -384,9 +384,13 @@ export class BlockSvg event = new (eventUtils.get(eventUtils.BLOCK_MOVE)!)(this) as BlockMove; reason && event.setReason(reason); } - const xy = this.getRelativeToSurfaceXY(); - this.translate(xy.x + dx, xy.y + dy); - this.moveConnections(dx, dy); + + const delta = new Coordinate(dx, dy); + const currLoc = this.getRelativeToSurfaceXY(); + const newLoc = Coordinate.sum(currLoc, delta); + this.translate(newLoc.x, newLoc.y); + this.updateComponentLocations(newLoc); + if (eventsEnabled && event) { event!.recordNew(); eventUtils.fire(event); @@ -649,32 +653,38 @@ export class BlockSvg } /** - * Move the connections for this block and all blocks attached under it. - * Also update any attached bubbles. - * - * @param dx Horizontal offset from current location, in workspace units. - * @param dy Vertical offset from current location, in workspace units. + * Updates the locations of any parts of the block that need to know where + * they are (e.g. connections, icons). + * + * @param blockOrigin The top-left of this block in workspace coordinates. * @internal */ - moveConnections(dx: number, dy: number) { + updateComponentLocations(blockOrigin: Coordinate) { if (!this.rendered) { // Rendering is required to lay out the blocks. // This is probably an invisible block attached to a collapsed block. return; } - const myConnections = this.getConnections_(false); - for (let i = 0; i < myConnections.length; i++) { - myConnections[i].moveBy(dx, dy); + + this.updateConnectionLocations(blockOrigin); + this.updateIconLocations(blockOrigin); + + for (const child of this.getChildren(false)) { + child.updateComponentLocations( + Coordinate.sum(blockOrigin, child.relativeCoords), + ); } - const icons = this.getIcons(); - const pos = this.getRelativeToSurfaceXY(); - for (const icon of icons) { - icon.onLocationChange(pos); + } + + private updateConnectionLocations(blockOrigin: Coordinate) { + for (const conn of this.getConnections_(false)) { + conn.moveToOffset(blockOrigin); } + } - // Recurse through all blocks attached under this one. - for (let i = 0; i < this.childBlocks_.length; i++) { - (this.childBlocks_[i] as BlockSvg).moveConnections(dx, dy); + private updateIconLocations(blockOrigin: Coordinate) { + for (const icon of this.getIcons()) { + icon.onLocationChange(blockOrigin); } } diff --git a/core/render_management.ts b/core/render_management.ts index 8c088eacfe0..f0b7aab137a 100644 --- a/core/render_management.ts +++ b/core/render_management.ts @@ -5,7 +5,6 @@ */ import {BlockSvg} from './block_svg.js'; -import {Coordinate} from './utils/coordinate.js'; import * as userAgent from './utils/useragent.js'; /** The set of all blocks in need of rendering which don't have parents. */ @@ -124,8 +123,7 @@ function doRenders() { renderBlock(block); const blockOrigin = block.getRelativeToSurfaceXY(); - updateConnectionLocations(block, blockOrigin); - updateIconLocations(block, blockOrigin); + block.updateComponentLocations(blockOrigin); } for (const workspace of workspaces) { workspace.resizeContents(); @@ -149,44 +147,3 @@ function renderBlock(block: BlockSvg) { } block.renderEfficiently(); } - -/** - * Updates the connection database with the new locations of all of the - * connections that are children of the given block. - * - * @param block The block to update the connection locations of. - * @param blockOrigin The top left of the given block in workspace coordinates. - */ -function updateConnectionLocations(block: BlockSvg, blockOrigin: Coordinate) { - for (const conn of block.getConnections_(false)) { - const moved = conn.moveToOffset(blockOrigin); - const target = conn.targetBlock(); - if (!conn.isSuperior()) continue; - if (!target) continue; - if (moved || dirtyBlocks.has(target)) { - updateConnectionLocations( - target, - Coordinate.sum(blockOrigin, target.relativeCoords), - ); - } - } -} - -/** - * Updates all icons that are children of the given block with their new - * locations. - * - * @param block The block to update the icon locations of. - */ -function updateIconLocations(block: BlockSvg, blockOrigin: Coordinate) { - if (!block.getIcons) return; - for (const icon of block.getIcons()) { - icon.onLocationChange(blockOrigin); - } - for (const child of block.getChildren(false)) { - updateIconLocations( - child, - Coordinate.sum(blockOrigin, child.relativeCoords), - ); - } -} From ff4901ba9ca5c4b1e4c388f4838d44dcc006c600 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Thu, 21 Sep 2023 16:48:46 +0000 Subject: [PATCH 03/10] chore: change dragging to use update component locations --- core/block_dragger.ts | 63 ++++++++----------------------------------- core/block_svg.ts | 7 ++++- 2 files changed, 17 insertions(+), 53 deletions(-) diff --git a/core/block_dragger.ts b/core/block_dragger.ts index 2d72d55de19..f9eb53d802f 100644 --- a/core/block_dragger.ts +++ b/core/block_dragger.ts @@ -28,7 +28,7 @@ import * as registry from './registry.js'; import {Coordinate} from './utils/coordinate.js'; import * as dom from './utils/dom.js'; import type {WorkspaceSvg} from './workspace_svg.js'; -import {hasBubble} from './interfaces/i_has_bubble.js'; +import * as deprecation from './utils/deprecation.js'; /** * Class for a block dragger. It moves blocks around the workspace when they @@ -48,7 +48,12 @@ export class BlockDragger implements IBlockDragger { /** Whether the block would be deleted if dropped immediately. */ protected wouldDeleteBlock_ = false; protected startXY_: Coordinate; - protected dragIconData_: IconPositionData[]; + + /** + * @deprecated To be removed in v11. Updating icons is now handled by the + * block's `moveDuringDrag` method. + */ + protected dragIconData_: IconPositionData[] = []; /** * @param block The block to drag. @@ -69,13 +74,6 @@ export class BlockDragger implements IBlockDragger { * beginning of the drag in workspace coordinates. */ this.startXY_ = this.draggingBlock_.getRelativeToSurfaceXY(); - - /** - * A list of all of the icons (comment, warning, and mutator) that are - * on this block and its descendants. Moving an icon moves the bubble that - * extends from it if that bubble is open. - */ - this.dragIconData_ = initIconData(block, this.startXY_); } /** @@ -84,8 +82,6 @@ export class BlockDragger implements IBlockDragger { * @internal */ dispose() { - this.dragIconData_.length = 0; - if (this.draggedConnectionManager_) { this.draggedConnectionManager_.dispose(); } @@ -178,7 +174,6 @@ export class BlockDragger implements IBlockDragger { const delta = this.pixelsToWorkspaceUnits_(currentDragDeltaXY); const newLoc = Coordinate.sum(this.startXY_, delta); this.draggingBlock_.moveDuringDrag(newLoc); - this.dragIcons_(delta); const oldDragTarget = this.dragTarget_; this.dragTarget_ = this.workspace_.getDragTarget(e); @@ -210,7 +205,6 @@ export class BlockDragger implements IBlockDragger { endDrag(e: PointerEvent, currentDragDeltaXY: Coordinate) { // Make sure internal state is fresh. this.drag(e, currentDragDeltaXY); - this.dragIconData_ = []; this.fireDragEndEvent_(); dom.stopTextWidthCache(); @@ -400,12 +394,11 @@ export class BlockDragger implements IBlockDragger { * * @param dxy How far to move the icons from their original positions, in * workspace units. + * @deprecated To be removed in v11. This is now handled by the block's + * `moveDuringDrag` method. */ - protected dragIcons_(dxy: Coordinate) { - // Moving icons moves their associated bubbles. - for (const data of this.dragIconData_) { - data.icon.onLocationChange(Coordinate.sum(data.location, dxy)); - } + protected dragIcons_() { + deprecation.warn('Blockly.BlockDragger.prototype.dragIcons_', 'v10', 'v11'); } /** @@ -432,38 +425,4 @@ export interface IconPositionData { icon: Icon; } -/** - * Make a list of all of the icons (comment, warning, and mutator) that are - * on this block and its descendants. Moving an icon moves the bubble that - * extends from it if that bubble is open. - * - * @param block The root block that is being dragged. - * @param blockOrigin The top left of the given block in workspace coordinates. - * @returns The list of all icons and their locations. - */ -function initIconData( - block: BlockSvg, - blockOrigin: Coordinate, -): IconPositionData[] { - // Build a list of icons that need to be moved and where they started. - const dragIconData = []; - - for (const icon of block.getIcons()) { - // Only bother to track icons whose bubble is visible. - if (hasBubble(icon) && !icon.bubbleIsVisible()) continue; - - dragIconData.push({location: blockOrigin, icon: icon}); - icon.onLocationChange(blockOrigin); - } - - for (const child of block.getChildren(false)) { - dragIconData.push( - ...initIconData(child, Coordinate.sum(blockOrigin, child.relativeCoords)), - ); - } - // AnyDuringMigration because: Type '{ location: Coordinate | null; icon: - // Icon; }[]' is not assignable to type 'IconPositionData[]'. - return dragIconData as AnyDuringMigration; -} - registry.register(registry.Type.BLOCK_DRAGGER, registry.DEFAULT, BlockDragger); diff --git a/core/block_svg.ts b/core/block_svg.ts index e2330f54e47..4dcb43a3cc4 100644 --- a/core/block_svg.ts +++ b/core/block_svg.ts @@ -154,6 +154,9 @@ export class BlockSvg */ private bumpNeighboursPid = 0; + /** Whether this block is currently being dragged. */ + private dragging = false; + /** * The location of the top left of this block (in workspace coordinates) * relative to either its parent block, or the workspace origin if it has no @@ -441,6 +444,7 @@ export class BlockSvg moveDuringDrag(newLoc: Coordinate) { this.translate(newLoc.x, newLoc.y); this.getSvgRoot().setAttribute('transform', this.getTranslation()); + this.updateComponentLocations(newLoc); } /** Snap this block to the nearest grid point. */ @@ -666,7 +670,7 @@ export class BlockSvg return; } - this.updateConnectionLocations(blockOrigin); + if (!this.dragging) this.updateConnectionLocations(blockOrigin); this.updateIconLocations(blockOrigin); for (const child of this.getChildren(false)) { @@ -696,6 +700,7 @@ export class BlockSvg * @internal */ setDragging(adding: boolean) { + this.dragging = adding; if (adding) { this.translation = ''; common.draggingConnections.push(...this.getConnections_(true)); From 07e61eec1750b2be2ca6f4d5e4c157bce578b3f6 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Thu, 21 Sep 2023 16:58:29 +0000 Subject: [PATCH 04/10] fix: field widgets not being moved when blocks are editted --- core/block_svg.ts | 9 +++++++++ core/field.ts | 8 ++++++++ core/field_input.ts | 8 ++++++++ 3 files changed, 25 insertions(+) diff --git a/core/block_svg.ts b/core/block_svg.ts index 4dcb43a3cc4..d1c1e19624d 100644 --- a/core/block_svg.ts +++ b/core/block_svg.ts @@ -672,6 +672,7 @@ export class BlockSvg if (!this.dragging) this.updateConnectionLocations(blockOrigin); this.updateIconLocations(blockOrigin); + this.updateFieldLocations(blockOrigin); for (const child of this.getChildren(false)) { child.updateComponentLocations( @@ -692,6 +693,14 @@ export class BlockSvg } } + private updateFieldLocations(blockOrigin: Coordinate) { + for (const input of this.inputList) { + for (const field of input.fieldRow) { + field.onLocationChange(blockOrigin); + } + } + } + /** * Recursively adds or removes the dragging class to this node and its * children. diff --git a/core/field.ts b/core/field.ts index 529c5aaef14..cfae1429bbc 100644 --- a/core/field.ts +++ b/core/field.ts @@ -960,6 +960,14 @@ export abstract class Field return new Rect(xy.y, xy.y + scaledHeight, xy.x, xy.x + scaledWidth); } + /** + * Notifies the field that it has changed locations. + * + * @param _ The location of this field's block's top-start corner + * in workspace coordinates. + */ + onLocationChange(_: Coordinate) {} + /** * Get the text from this field to display on the block. May differ from * `getText` due to ellipsis, and other formatting. diff --git a/core/field_input.ts b/core/field_input.ts index 22c6765b49c..bf37fa17efc 100644 --- a/core/field_input.ts +++ b/core/field_input.ts @@ -251,6 +251,14 @@ export abstract class FieldInput extends Field< return super.getSize(); } + /** + * Notifies the field that it has changed locations. Moves the widget div to + * be in the correct place if it is open. + */ + onLocationChange(): void { + if (this.isBeingEdited_) this.resizeEditor_(); + } + /** * Updates the colour of the htmlInput given the current validity of the * field's value. From 8abf8792fde08800b716d18f5c51d3bf609ca8bf Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Thu, 21 Sep 2023 17:31:00 +0000 Subject: [PATCH 05/10] chore: remove unnecessary resizeEditor_ calls --- core/field_input.ts | 14 ++++---------- core/render_management.ts | 29 +++++++++++++++++++---------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/core/field_input.ts b/core/field_input.ts index bf37fa17efc..3da18a30dc1 100644 --- a/core/field_input.ts +++ b/core/field_input.ts @@ -33,7 +33,6 @@ import {Coordinate} from './utils/coordinate.js'; import * as userAgent from './utils/useragent.js'; import * as WidgetDiv from './widgetdiv.js'; import type {WorkspaceSvg} from './workspace_svg.js'; -import * as renderManagement from './render_management.js'; import {Size} from './utils/size.js'; /** @@ -271,7 +270,6 @@ export abstract class FieldInput extends Field< // This logic is done in render_ rather than doValueInvalid_ or // doValueUpdate_ so that the code is more centralized. if (this.isBeingEdited_) { - this.resizeEditor_(); const htmlInput = this.htmlInput_ as HTMLElement; if (!this.isTextValid_) { dom.addClass(htmlInput, 'blocklyInvalidInput'); @@ -584,11 +582,6 @@ export abstract class FieldInput extends Field< ), ); } - - // Resize the widget div after the block has finished rendering. - renderManagement.finishQueuedRenders().then(() => { - this.resizeEditor_(); - }); } /** @@ -624,6 +617,7 @@ export abstract class FieldInput extends Field< } const div = WidgetDiv.getDiv(); const bBox = this.getScaledBBox(); + console.log(bBox); div!.style.width = bBox.right - bBox.left + 'px'; div!.style.height = bBox.bottom - bBox.top + 'px'; @@ -639,7 +633,7 @@ export abstract class FieldInput extends Field< /** * Handles repositioning the WidgetDiv used for input fields when the * workspace is resized. Will bump the block into the viewport and update the - * position of the field if necessary. + * position of the text input if necessary. * * @returns True for rendered workspaces, as we never want to hide the widget * div. @@ -650,13 +644,13 @@ export abstract class FieldInput extends Field< // rendered blocks. if (!(block instanceof BlockSvg)) return false; - bumpObjects.bumpIntoBounds( + const bumped = bumpObjects.bumpIntoBounds( this.workspace_!, this.workspace_!.getMetricsManager().getViewMetrics(true), block, ); - this.resizeEditor_(); + if (!bumped) this.resizeEditor_(); return true; } diff --git a/core/render_management.ts b/core/render_management.ts index f0b7aab137a..000d6fdd59b 100644 --- a/core/render_management.ts +++ b/core/render_management.ts @@ -113,27 +113,36 @@ function queueBlock(block: BlockSvg) { */ function doRenders() { const workspaces = new Set([...rootBlocks].map((block) => block.workspace)); - for (const block of rootBlocks) { - // No need to render a dead block. - if (block.isDisposed()) continue; - // A render for this block may have been queued, and then the block was - // connected to a parent, so it is no longer a root block. - // Rendering will be triggered through the real root block. - if (block.getParent()) continue; - + const blocks = [...rootBlocks].filter(shouldRenderRootBlock); + for (const block of blocks) { renderBlock(block); - const blockOrigin = block.getRelativeToSurfaceXY(); - block.updateComponentLocations(blockOrigin); } for (const workspace of workspaces) { workspace.resizeContents(); } + for (const block of rootBlocks) { + const blockOrigin = block.getRelativeToSurfaceXY(); + block.updateComponentLocations(blockOrigin); + } rootBlocks.clear(); dirtyBlocks = new Set(); afterRendersPromise = null; } +/** + * Returns true if the block should be rendered. + * + * No need to render dead blocks. + * + * No need to render blocks with parents. A render for the block may have been + * queued, and the the block was connected to a parent, so it is no longer a + * root block. Rendering will be triggered through the real root block. + */ +function shouldRenderRootBlock(block: BlockSvg): boolean { + return !block.isDisposed() && !block.getParent(); +} + /** * Recursively renders all of the dirty children of the given block, and * then renders the block. From 10be76e42d30c344e46571cc3efa31cdedd0d865 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Thu, 21 Sep 2023 17:36:05 +0000 Subject: [PATCH 06/10] chore: format --- core/block_svg.ts | 2 +- core/render_management.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/block_svg.ts b/core/block_svg.ts index d1c1e19624d..e175db56798 100644 --- a/core/block_svg.ts +++ b/core/block_svg.ts @@ -659,7 +659,7 @@ export class BlockSvg /** * Updates the locations of any parts of the block that need to know where * they are (e.g. connections, icons). - * + * * @param blockOrigin The top-left of this block in workspace coordinates. * @internal */ diff --git a/core/render_management.ts b/core/render_management.ts index 000d6fdd59b..a8dec30ac3f 100644 --- a/core/render_management.ts +++ b/core/render_management.ts @@ -132,9 +132,9 @@ function doRenders() { /** * Returns true if the block should be rendered. - * + * * No need to render dead blocks. - * + * * No need to render blocks with parents. A render for the block may have been * queued, and the the block was connected to a parent, so it is no longer a * root block. Rendering will be triggered through the real root block. From 1f8605a5c53ae1081d8bb96f53a947a90771d9f8 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Thu, 21 Sep 2023 19:34:33 +0000 Subject: [PATCH 07/10] chore: fix build --- core/block_dragger.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/block_dragger.ts b/core/block_dragger.ts index f9eb53d802f..f50262ae0c7 100644 --- a/core/block_dragger.ts +++ b/core/block_dragger.ts @@ -392,8 +392,6 @@ export class BlockDragger implements IBlockDragger { /** * Move all of the icons connected to this drag. * - * @param dxy How far to move the icons from their original positions, in - * workspace units. * @deprecated To be removed in v11. This is now handled by the block's * `moveDuringDrag` method. */ From 6b71ae2943fa105cafbc6cb88738f365fee8818c Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Thu, 21 Sep 2023 19:40:53 +0000 Subject: [PATCH 08/10] fix: tests --- core/render_management.ts | 2 +- tests/mocha/render_management_test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/render_management.ts b/core/render_management.ts index a8dec30ac3f..541459860ee 100644 --- a/core/render_management.ts +++ b/core/render_management.ts @@ -120,7 +120,7 @@ function doRenders() { for (const workspace of workspaces) { workspace.resizeContents(); } - for (const block of rootBlocks) { + for (const block of blocks) { const blockOrigin = block.getRelativeToSurfaceXY(); block.updateComponentLocations(blockOrigin); } diff --git a/tests/mocha/render_management_test.js b/tests/mocha/render_management_test.js index 240a9dd156e..d5d957df458 100644 --- a/tests/mocha/render_management_test.js +++ b/tests/mocha/render_management_test.js @@ -30,8 +30,8 @@ suite('Render Management', function () { getParent: () => null, getChildren: () => [], isDisposed: () => false, - getConnections_: () => [], getRelativeToSurfaceXY: () => ({x: 0, y: 0}), + updateComponentLocations: () => {}, workspace: { resizeContents: () => {}, }, From 2fd39c43e8d8c6b53bb50aff1cf87cd9ce3f2e69 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Thu, 26 Oct 2023 16:35:17 +0000 Subject: [PATCH 09/10] chore: PR comments --- core/block_dragger.ts | 39 +++++++++++++++++++++++++++++++++++++++ core/field_input.ts | 1 - 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/core/block_dragger.ts b/core/block_dragger.ts index f50262ae0c7..af83740ed04 100644 --- a/core/block_dragger.ts +++ b/core/block_dragger.ts @@ -28,6 +28,7 @@ import * as registry from './registry.js'; import {Coordinate} from './utils/coordinate.js'; import * as dom from './utils/dom.js'; import type {WorkspaceSvg} from './workspace_svg.js'; +import {hasBubble} from './interfaces/i_has_bubble.js'; import * as deprecation from './utils/deprecation.js'; /** @@ -74,6 +75,8 @@ export class BlockDragger implements IBlockDragger { * beginning of the drag in workspace coordinates. */ this.startXY_ = this.draggingBlock_.getRelativeToSurfaceXY(); + + this.dragIconData_ = initIconData(block, this.startXY_); } /** @@ -82,6 +85,7 @@ export class BlockDragger implements IBlockDragger { * @internal */ dispose() { + this.dragIconData_.length = 0; if (this.draggedConnectionManager_) { this.draggedConnectionManager_.dispose(); } @@ -423,4 +427,39 @@ export interface IconPositionData { icon: Icon; } +/** + * Make a list of all of the icons (comment, warning, and mutator) that are + * on this block and its descendants. Moving an icon moves the bubble that + * extends from it if that bubble is open. + * + * @param block The root block that is being dragged. + * @param blockOrigin The top left of the given block in workspace coordinates. + * @returns The list of all icons and their locations. + */ +function initIconData( + block: BlockSvg, + blockOrigin: Coordinate, +): IconPositionData[] { + // Build a list of icons that need to be moved and where they started. + const dragIconData = []; + + for (const icon of block.getIcons()) { + // Only bother to track icons whose bubble is visible. + if (hasBubble(icon) && !icon.bubbleIsVisible()) continue; + + dragIconData.push({location: blockOrigin, icon: icon}); + icon.onLocationChange(blockOrigin); + } + + for (const child of block.getChildren(false)) { + dragIconData.push( + ...initIconData(child, Coordinate.sum(blockOrigin, child.relativeCoords)), + ); + } + // AnyDuringMigration because: Type '{ location: Coordinate | null; icon: + // Icon; }[]' is not assignable to type 'IconPositionData[]'. + return dragIconData as AnyDuringMigration; +} + + registry.register(registry.Type.BLOCK_DRAGGER, registry.DEFAULT, BlockDragger); diff --git a/core/field_input.ts b/core/field_input.ts index 3da18a30dc1..2cd4015b093 100644 --- a/core/field_input.ts +++ b/core/field_input.ts @@ -617,7 +617,6 @@ export abstract class FieldInput extends Field< } const div = WidgetDiv.getDiv(); const bBox = this.getScaledBBox(); - console.log(bBox); div!.style.width = bBox.right - bBox.left + 'px'; div!.style.height = bBox.bottom - bBox.top + 'px'; From 8f82f0200f12ef660c5777cfb2c731bda0219325 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Thu, 26 Oct 2023 16:37:39 +0000 Subject: [PATCH 10/10] chore: format --- core/block_dragger.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/block_dragger.ts b/core/block_dragger.ts index af83740ed04..cd9a1e48d44 100644 --- a/core/block_dragger.ts +++ b/core/block_dragger.ts @@ -437,7 +437,7 @@ export interface IconPositionData { * @returns The list of all icons and their locations. */ function initIconData( - block: BlockSvg, + block: BlockSvg, blockOrigin: Coordinate, ): IconPositionData[] { // Build a list of icons that need to be moved and where they started. @@ -461,5 +461,4 @@ function initIconData( return dragIconData as AnyDuringMigration; } - registry.register(registry.Type.BLOCK_DRAGGER, registry.DEFAULT, BlockDragger);