From e9f4a0e4673c9bb2f666ef46bbf0bee1ff488430 Mon Sep 17 00:00:00 2001 From: MarsLuay <70299537+MarsLuay@users.noreply.github.com> Date: Tue, 25 Aug 2026 09:25:02 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20simplify=20deeply=20nested=20sha?= =?UTF-8?q?pe=20reordering=20logic=20in=20PresentationEngine?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/PresentationEngine.ts | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/PresentationEngine.ts b/src/PresentationEngine.ts index 7028e65..a99f742 100644 --- a/src/PresentationEngine.ts +++ b/src/PresentationEngine.ts @@ -191,6 +191,26 @@ const RESETTABLE_IMAGE_EFFECTS = new Set([ 'tint', ]); +function shiftShapeAdjacent( + shapeTree: Element, + element: Element, + selected: Set, + direction: 1 | -1, + intersectingOnly: boolean +): boolean { + const target = intersectingOnly + ? adjacentIntersectingUnselectedShape(element, selected, direction) + : adjacentUnselectedShape(element, selected, direction); + if (!target) return false; + + if (direction === 1) { + shapeTree.insertBefore(element, target.nextSibling); + } else { + shapeTree.insertBefore(element, target); + } + return true; +} + function getImageResetDetails(shape: Element): { state: ImageResetState; srcRects: Element[]; @@ -2694,19 +2714,15 @@ export class PresentationEngine { for (let index = ordered.length - 1; index >= 0; index--) { const element = ordered[index]; if (!element) continue; - const next = options.intersectingOnly - ? adjacentIntersectingUnselectedShape(element, selected, 1) - : adjacentUnselectedShape(element, selected, 1); - if (options.intersectingOnly && next) intersectionTargetCount += 1; - if (next) shapeTree.insertBefore(element, next.nextSibling); + if (shiftShapeAdjacent(shapeTree, element, selected, 1, Boolean(options.intersectingOnly)) && options.intersectingOnly) { + intersectionTargetCount += 1; + } } } else { for (const element of ordered) { - const previous = options.intersectingOnly - ? adjacentIntersectingUnselectedShape(element, selected, -1) - : adjacentUnselectedShape(element, selected, -1); - if (options.intersectingOnly && previous) intersectionTargetCount += 1; - if (previous) shapeTree.insertBefore(element, previous); + if (shiftShapeAdjacent(shapeTree, element, selected, -1, Boolean(options.intersectingOnly)) && options.intersectingOnly) { + intersectionTargetCount += 1; + } } }