From 99098684352aa895f4c9c5ccf75acfac1efa6838 Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Mon, 21 Aug 2023 14:58:30 -0700 Subject: [PATCH] fix: Always draw dragged blocks atop others in the workspace. (#6874) * fix: Always draw dragged blocks atop others in the workspace. * chore: format --------- Co-authored-by: Beka Westberg --- core/block_dragger.ts | 11 ++++------- core/block_svg.ts | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/core/block_dragger.ts b/core/block_dragger.ts index b6f3ab7ba..9bbce0fbe 100644 --- a/core/block_dragger.ts +++ b/core/block_dragger.ts @@ -105,13 +105,10 @@ export class BlockDragger implements IBlockDragger { } this.fireDragStartEvent_(); - // Mutators don't have the same type of z-ordering as the normal workspace - // during a drag. They have to rely on the order of the blocks in the SVG. - // For performance reasons that usually happens at the end of a drag, - // but do it at the beginning for mutators. - if (this.workspace_.isMutator) { - this.draggingBlock_.bringToFront(); - } + // The z-order of blocks depends on their order in the SVG, so move the + // block being dragged to the front so that it will appear atop other blocks + // in the workspace. + this.draggingBlock_.bringToFront(true); // During a drag there may be a lot of rerenders, but not field changes. // Turn the cache on so we don't do spurious remeasures during the drag. diff --git a/core/block_svg.ts b/core/block_svg.ts index dff7fafea..6290608fe 100644 --- a/core/block_svg.ts +++ b/core/block_svg.ts @@ -328,7 +328,14 @@ export class BlockSvg } else if (oldParent) { // If we are losing a parent, we want to move our DOM element to the // root of the workspace. - this.workspace.getCanvas().appendChild(svgRoot); + const draggingBlock = this.workspace + .getCanvas() + .querySelector('.blocklyDragging'); + if (draggingBlock) { + this.workspace.getCanvas().insertBefore(svgRoot, draggingBlock); + } else { + this.workspace.getCanvas().appendChild(svgRoot); + } this.translate(oldXY.x, oldXY.y); } @@ -1146,9 +1153,11 @@ export class BlockSvg * order that they are in the DOM. By placing this block first within the * block group's , it will render on top of any other blocks. * + * @param blockOnly: True to only move this block to the front without + * adjusting its parents. * @internal */ - bringToFront() { + bringToFront(blockOnly = false) { /* eslint-disable-next-line @typescript-eslint/no-this-alias */ let block: this | null = this; do { @@ -1159,6 +1168,7 @@ export class BlockSvg if (childNodes[childNodes.length - 1] !== root) { parent!.appendChild(root); } + if (blockOnly) break; block = block.getParent(); } while (block); }