From c7bc4860659d254f9a1ab0bdbd054f925d572b7e Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Thu, 16 May 2024 22:28:58 +0000 Subject: [PATCH 1/4] Revert "fix: dragging shadow blocks (#7992)" This reverts commit c0e6e6745f7bb07d40be5012b23f0fa6f1ec3cec. --- core/block_svg.ts | 9 ++--- core/dragging/block_drag_strategy.ts | 49 ++-------------------------- 2 files changed, 5 insertions(+), 53 deletions(-) diff --git a/core/block_svg.ts b/core/block_svg.ts index ca2ad181a82..adf432ccf3d 100644 --- a/core/block_svg.ts +++ b/core/block_svg.ts @@ -245,8 +245,9 @@ export class BlockSvg /** Selects this block. Highlights the block visually. */ select() { - if (this.isShadow()) { - this.getParent()?.select(); + if (this.isShadow() && this.getParent()) { + // Shadow blocks should not be selected. + this.getParent()!.select(); return; } this.addSelect(); @@ -254,10 +255,6 @@ export class BlockSvg /** Unselects this block. Unhighlights the blockv visually. */ unselect() { - if (this.isShadow()) { - this.getParent()?.unselect(); - return; - } this.removeSelect(); } diff --git a/core/dragging/block_drag_strategy.ts b/core/dragging/block_drag_strategy.ts index fadba28fb69..fb913f88215 100644 --- a/core/dragging/block_drag_strategy.ts +++ b/core/dragging/block_drag_strategy.ts @@ -55,24 +55,15 @@ export class BlockDragStrategy implements IDragStrategy { private dragging = false; - /** - * If this is a shadow block, the offset between this block and the parent - * block, to add to the drag location. In workspace units. - */ - private dragOffset = new Coordinate(0, 0); - constructor(private block: BlockSvg) { this.workspace = block.workspace; } /** Returns true if the block is currently movable. False otherwise. */ isMovable(): boolean { - if (this.block.isShadow()) { - return this.block.getParent()?.isMovable() ?? false; - } - return ( this.block.isOwnMovable() && + !this.block.isShadow() && !this.block.isDeadOrDying() && !this.workspace.options.readOnly && // We never drag blocks in the flyout, only create new blocks that are @@ -86,11 +77,6 @@ export class BlockDragStrategy implements IDragStrategy { * from any parent blocks. */ startDrag(e?: PointerEvent): void { - if (this.block.isShadow()) { - this.startDraggingShadow(e); - return; - } - this.dragging = true; if (!eventUtils.getGroup()) { eventUtils.setGroup(true); @@ -120,22 +106,6 @@ export class BlockDragStrategy implements IDragStrategy { this.workspace.getLayerManager()?.moveToDragLayer(this.block); } - /** Starts a drag on a shadow, recording the drag offset. */ - private startDraggingShadow(e?: PointerEvent) { - const parent = this.block.getParent(); - if (!parent) { - throw new Error( - 'Tried to drag a shadow block with no parent. ' + - 'Shadow blocks should always have parents.', - ); - } - this.dragOffset = Coordinate.difference( - parent.getRelativeToSurfaceXY(), - this.block.getRelativeToSurfaceXY(), - ); - parent.startDrag(e); - } - /** * Whether or not we should disconnect the block when a drag is started. * @@ -204,11 +174,6 @@ export class BlockDragStrategy implements IDragStrategy { /** Moves the block and updates any connection previews. */ drag(newLoc: Coordinate): void { - if (this.block.isShadow()) { - this.block.getParent()?.drag(Coordinate.sum(newLoc, this.dragOffset)); - return; - } - this.block.moveDuringDrag(newLoc); this.updateConnectionPreview( this.block, @@ -352,12 +317,7 @@ export class BlockDragStrategy implements IDragStrategy { * Cleans up any state at the end of the drag. Applies any pending * connections. */ - endDrag(e?: PointerEvent): void { - if (this.block.isShadow()) { - this.block.getParent()?.endDrag(e); - return; - } - + endDrag(): void { this.fireDragEndEvent(); this.fireMoveEvent(); @@ -413,11 +373,6 @@ export class BlockDragStrategy implements IDragStrategy { * including reconnecting connections. */ revertDrag(): void { - if (this.block.isShadow()) { - this.block.getParent()?.revertDrag(); - return; - } - this.startChildConn?.connect(this.block.nextConnection); if (this.startParentConn) { switch (this.startParentConn.type) { From 94070e26353ae9fc40f7af5e5ba08c90e8622d16 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Thu, 16 May 2024 22:39:29 +0000 Subject: [PATCH 2/4] fix: dragging by shadow not being deletable --- core/block.ts | 11 +++++++++++ core/gesture.ts | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/core/block.ts b/core/block.ts index 52191d63c3c..fa00c3ab4d3 100644 --- a/core/block.ts +++ b/core/block.ts @@ -675,6 +675,17 @@ export class Block implements IASTNodeLocation { return block; } + /** + * Returns this block if it is a shadow block, or the first non-shadow parent. + * + * @internal + */ + getFirstNonShadowBlock(): this { + if (!this.isShadow()) return this; + // We can assert the parent is non-null because shadows must have parents. + return this.getParent()!.getFirstNonShadowBlock(); + } + /** * Find all the blocks that are directly nested inside this one. * Includes value and statement inputs, as well as any following statement. diff --git a/core/gesture.ts b/core/gesture.ts index 7970ed00689..46938d24f14 100644 --- a/core/gesture.ts +++ b/core/gesture.ts @@ -1015,7 +1015,7 @@ export class Gesture { // If the gesture already went through a bubble, don't set the start block. if (!this.startBlock && !this.startBubble) { this.startBlock = block; - common.setSelected(this.startBlock); + common.setSelected(this.startBlock.getFirstNonShadowBlock()); if (block.isInFlyout && block !== block.getRootBlock()) { this.setTargetBlock(block.getRootBlock()); } else { From 90bf0f3ebebe89aa28330317926613d9bb955189 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Fri, 17 May 2024 19:53:41 +0000 Subject: [PATCH 3/4] fix: unselecting shadows --- core/block_svg.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/block_svg.ts b/core/block_svg.ts index adf432ccf3d..37cfe921ea5 100644 --- a/core/block_svg.ts +++ b/core/block_svg.ts @@ -246,7 +246,6 @@ export class BlockSvg /** Selects this block. Highlights the block visually. */ select() { if (this.isShadow() && this.getParent()) { - // Shadow blocks should not be selected. this.getParent()!.select(); return; } @@ -255,6 +254,10 @@ export class BlockSvg /** Unselects this block. Unhighlights the blockv visually. */ unselect() { + if (this.isShadow() && this.getParent()) { + this.getParent()!.unselect(); + return; + } this.removeSelect(); } From 72c79b99b56b8d6510601a982fb7bf2683d749e1 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Fri, 17 May 2024 20:11:30 +0000 Subject: [PATCH 4/4] fix: revert changes to select and unselect --- core/block_svg.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/block_svg.ts b/core/block_svg.ts index 37cfe921ea5..ca2ad181a82 100644 --- a/core/block_svg.ts +++ b/core/block_svg.ts @@ -245,8 +245,8 @@ export class BlockSvg /** Selects this block. Highlights the block visually. */ select() { - if (this.isShadow() && this.getParent()) { - this.getParent()!.select(); + if (this.isShadow()) { + this.getParent()?.select(); return; } this.addSelect(); @@ -254,8 +254,8 @@ export class BlockSvg /** Unselects this block. Unhighlights the blockv visually. */ unselect() { - if (this.isShadow() && this.getParent()) { - this.getParent()!.unselect(); + if (this.isShadow()) { + this.getParent()?.unselect(); return; } this.removeSelect();