Files
powerpoint-toolbox/src/taskpane/components/MatchSizes.tsx
T

178 lines
4.7 KiB
TypeScript

import * as React from "react";
import {
Button,
makeStyles,
Text,
Tooltip,
InfoLabel,
Spinner,
tokens,
Card
} from "@fluentui/react-components";
import {
ArrowFitInRegular,
ArrowSortDownRegular,
ArrowSortUpRegular,
SquareRegular,
ShapeUnionRegular,
InfoRegular
} from "@fluentui/react-icons";
const useStyles = makeStyles({
container: {
display: "flex",
flexDirection: "column",
width: "100%",
},
buttonGroup: {
display: "flex",
flexDirection: "column",
gap: "8px",
marginBottom: "2px",
},
actionButton: {
justifyContent: "flex-start",
transitionProperty: "all",
transitionDuration: "200ms",
transitionTimingFunction: "cubic-bezier(0.33, 0, 0.67, 1)",
},
statusContainer: {
marginTop: "4px",
padding: "8px 12px",
fontSize: "13px",
borderRadius: "6px",
backgroundColor: "#f3f2f1", // Light gray background
transition: "all 0.3s ease",
},
successStatus: {
backgroundColor: "#DFF6DD", // Light green background
color: "#107C10", // Green text
},
warningStatus: {
backgroundColor: "#FFF4CE", // Light yellow background
color: "#797673", // Dark gray text
},
errorStatus: {
backgroundColor: "#FDE7E9", // Light red background
color: "#A80000", // Red text
},
statusIcon: {
marginRight: "8px",
},
statusText: {
display: "flex",
alignItems: "center",
},
});
export const MatchSizes: React.FC = () => {
const styles = useStyles();
const [statusMessage, setStatusMessage] = React.useState("");
const [statusType, setStatusType] = React.useState<"info" | "success" | "warning" | "error">("info");
const [isProcessing, setIsProcessing] = React.useState(false);
const matchSizeToFirstSelected = async () => {
setIsProcessing(true);
try {
await PowerPoint.run(async (context) => {
// Get the selected shapes
const shapes = context.presentation.getSelectedShapes();
shapes.load("items");
await context.sync();
// Check if shapes are selected
if (shapes.items.length === 0) {
setStatusMessage("No shapes are selected. Please select shapes first.");
setStatusType("warning");
return;
}
// Check if there is more than one shape selected
if (shapes.items.length === 1) {
setStatusMessage("Please select multiple shapes to resize.");
setStatusType("warning");
return;
}
// Get the first shape's dimensions
const firstShape = shapes.items[0];
firstShape.load("width,height");
await context.sync();
// Loop through the remaining shapes and resize them
for (let i = 1; i < shapes.items.length; i++) {
shapes.items[i].width = firstShape.width;
shapes.items[i].height = firstShape.height;
}
await context.sync();
setStatusMessage(`Resized ${shapes.items.length - 1} shapes to match the first selected shape.`);
setStatusType("success");
// Auto-clear success message after 5 seconds
setTimeout(() => {
if (statusType === "success") {
setStatusMessage("");
}
}, 5000);
});
} catch (error) {
setStatusMessage(`Error: ${error.message}`);
setStatusType("error");
console.error(error);
} finally {
setIsProcessing(false);
}
};
const getStatusIcon = () => {
switch (statusType) {
case "success":
return <ShapeUnionRegular className={styles.statusIcon} />;
case "warning":
return <SquareRegular className={styles.statusIcon} />;
case "error":
return <InfoRegular className={styles.statusIcon} />;
default:
return null;
}
};
return (
<div className={styles.container}>
<div className={styles.buttonGroup}>
<Button
appearance="primary"
className={styles.actionButton}
onClick={matchSizeToFirstSelected}
icon={<ArrowFitInRegular />}
disabled={isProcessing}
>
Match Sizes
</Button>
</div>
{isProcessing && (
<div className={styles.statusContainer}>
<div className={styles.statusText}>
<Spinner size="tiny" style={{ marginRight: "8px" }} />
Resizing shapes...
</div>
</div>
)}
{!isProcessing && statusMessage && (
<div className={`${styles.statusContainer} ${styles[`${statusType}Status`]}`}>
<div className={styles.statusText}>
{getStatusIcon()}
{statusMessage}
</div>
</div>
)}
</div>
);
};
export default MatchSizes;