From 15b7815f6b5a07f35852f2285064e220a53f1ad9 Mon Sep 17 00:00:00 2001 From: Heiko Joerg Schick Date: Sat, 8 Mar 2025 19:50:08 +0100 Subject: [PATCH] Added swap position functionality --- src/taskpane/components/App.tsx | 3 + src/taskpane/components/SwapPositions.tsx | 85 +++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/taskpane/components/SwapPositions.tsx diff --git a/src/taskpane/components/App.tsx b/src/taskpane/components/App.tsx index 3c3f450b..6ab2d950 100644 --- a/src/taskpane/components/App.tsx +++ b/src/taskpane/components/App.tsx @@ -3,6 +3,7 @@ import { useEffect, useState } from "react"; import MatchSizes from "./MatchSizes"; import MatchProperties from "./MatchProperties"; import RoundImage from "./RoundImage"; +import SwapPositions from "./SwapPositions"; import { makeStyles, Text, Subtitle1, tokens, Theme, Spinner } from "@fluentui/react-components"; import { ShapeUnionRegular, SquareRegular, InfoRegular } from "@fluentui/react-icons"; @@ -154,6 +155,8 @@ const App: React.FC = () => {
+
+
diff --git a/src/taskpane/components/SwapPositions.tsx b/src/taskpane/components/SwapPositions.tsx new file mode 100644 index 00000000..dbe412e2 --- /dev/null +++ b/src/taskpane/components/SwapPositions.tsx @@ -0,0 +1,85 @@ +import * as React from "react"; +import { + Button +} from "@fluentui/react-components"; +import { + ArrowSwapRegular +} from "@fluentui/react-icons"; +import { useStatusContext } from "./App"; +import { useCommonStyles } from "./commonStyles"; + +export const SwapPositions: React.FC = () => { + const styles = useCommonStyles(); + const { + statusMessage, setStatusMessage, + statusType, setStatusType, + isProcessing, setIsProcessing + } = useStatusContext(); + + const swapPositionsOfTwoSelectedObjects = async () => { + setIsProcessing(true); + try { + await PowerPoint.run(async (context) => { + // Get the selected shapes + const shapes = context.presentation.getSelectedShapes(); + shapes.load("items/count"); + await context.sync(); + + // Check if exactly two shapes are selected + if (shapes.items.length !== 2) { + setStatusMessage("Please select exactly two shapes to swap their positions."); + setStatusType("warning"); + return; + } + + // Get the two shapes + const shapeObj1 = shapes.items[0]; + const shapeObj2 = shapes.items[1]; + + // Load position properties + shapeObj1.load("left,top"); + shapeObj2.load("left,top"); + await context.sync(); + + // Store the position of the first shape + const tempLeft = shapeObj1.left; + const tempTop = shapeObj1.top; + + // Swap positions + shapeObj1.left = shapeObj2.left; + shapeObj1.top = shapeObj2.top; + shapeObj2.left = tempLeft; + shapeObj2.top = tempTop; + + await context.sync(); + + setStatusMessage("Positions of the two shapes have been swapped successfully."); + setStatusType("success"); + }); + } catch (error) { + setStatusMessage(`Error: ${error.message}`); + setStatusType("error"); + console.error("Swap positions error:", error); + } finally { + setIsProcessing(false); + } + }; + + return ( +
+
+ +
+
+ ); +}; + +export default SwapPositions; \ No newline at end of file