diff --git a/src/PresentationEngine.ts b/src/PresentationEngine.ts index 7c77b65..0d68cc9 100644 --- a/src/PresentationEngine.ts +++ b/src/PresentationEngine.ts @@ -191,6 +191,25 @@ 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 getRunFontSizePt(run: Element): number | null { const runProperties = getElementChildren(run) .find((element) => element.localName === 'rPr' && element.namespaceURI === DRAWINGML_NAMESPACE) ?? null; @@ -2746,19 +2765,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; + } } }