/** * @file AlignmentButtons.tsx * @description Component that provides functionality to align selected shapes * in PowerPoint presentations. Supports horizontal alignment (left, center, right) * and vertical alignment (top, middle, bottom). */ import * as React from "react"; import { makeStyles, Text, tokens, Toolbar, ToolbarButton, ToolbarDivider, Tooltip } from "@fluentui/react-components"; import { TextAlignLeftRegular, TextAlignCenterRegular, TextAlignRightRegular, TextboxAlignTopRegular, TextboxAlignMiddleRegular, TextboxAlignBottomRegular } from "@fluentui/react-icons"; import { useStatusContext } from "../context/StatusContext"; import { useSettings } from "../context/SettingsContext"; import { useCommonStyles } from "./commonStyles"; import { getErrorMessage } from "../types/office-types"; import { resetCancellation } from "../services/cancellation"; /** * Warning message when no shapes are selected */ const NO_SELECTION_WARNING = "No shapes are selected. Please select shapes first."; /** * Alignment types for horizontal and vertical alignment */ type HorizontalAlignmentType = 'left' | 'center' | 'right'; type VerticalAlignmentType = 'top' | 'middle' | 'bottom'; /** * Component-specific styles */ const useStyles = makeStyles({ toolbar: { width: "100%", flexWrap: "wrap", gap: tokens.spacingHorizontalXS, padding: 0, backgroundColor: "transparent" } }); interface AlignButtonProps { label: string; icon: React.ReactElement; disabled: boolean; onClick: () => void; } /** * Icon-only alignment button with a tooltip that doubles as its accessible name. */ const AlignButton: React.FC = ({ label, icon, disabled, onClick }) => ( ); /** * AlignmentButtons component provides UI and functionality to align * selected shapes in PowerPoint presentations. * * @returns React component */ export const AlignmentButtons: React.FC = () => { const styles = useStyles(); const commonStyles = useCommonStyles(); const { slideSize } = useSettings(); const { setStatusMessage, setStatusType, isProcessing, setIsProcessing } = useStatusContext(); /** * Handles errors and updates the status message * * @param error - The error object * @param operation - The operation being performed (for logging) */ const handleError = (error: unknown, operation: string): void => { setStatusMessage(`Error: ${getErrorMessage(error)}`); setStatusType("error"); console.error(`${operation} error:`, error); }; /** * Generic function to handle horizontal alignment of shapes * * @param alignmentType - The type of horizontal alignment to apply */ const handleHorizontalAlignment = async (alignmentType: HorizontalAlignmentType): Promise => { resetCancellation(); 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_SELECTION_WARNING); setStatusType("warning"); return; } // Shape widths are required to center shapes or align their right edges. if (alignmentType !== "left") { shapes.items.forEach((shape) => shape.load("width")); await context.sync(); } // Set alignment for all shapes at once based on alignment type shapes.items.forEach((shape) => { switch (alignmentType) { case "left": shape.left = 0; break; case "center": shape.left = Math.max(0, (slideSize.width - shape.width) / 2); break; case "right": shape.left = Math.max(0, slideSize.width - shape.width); break; } }); // Single sync after all updates await context.sync(); setStatusMessage(`Objects aligned to ${alignmentType}.`); setStatusType("success"); }); } catch (error: unknown) { handleError(error, `Align ${alignmentType}`); } finally { setIsProcessing(false); } }; /** * Generic function to handle vertical alignment of shapes * * @param alignmentType - The type of vertical alignment to apply */ const handleVerticalAlignment = async (alignmentType: VerticalAlignmentType): Promise => { resetCancellation(); 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_SELECTION_WARNING); setStatusType("warning"); return; } // Shape heights are required to center shapes or align their bottom edges. if (alignmentType !== "top") { shapes.items.forEach((shape) => shape.load("height")); await context.sync(); } // Set alignment for all shapes at once based on alignment type shapes.items.forEach((shape) => { switch (alignmentType) { case "top": shape.top = 0; break; case "middle": shape.top = Math.max(0, (slideSize.height - shape.height) / 2); break; case "bottom": shape.top = Math.max(0, slideSize.height - shape.height); break; } }); // Single sync after all updates await context.sync(); setStatusMessage(`Objects aligned to ${alignmentType}.`); setStatusType("success"); }); } catch (error: unknown) { handleError(error, `Align ${alignmentType}`); } finally { setIsProcessing(false); } }; /** * Aligns selected shapes to the left edge of the slide */ const alignLeft = async (): Promise => { await handleHorizontalAlignment('left'); }; /** * Aligns selected shapes to the horizontal center of the slide */ const alignCenter = async (): Promise => { await handleHorizontalAlignment('center'); }; /** * Aligns selected shapes to the right edge of the slide */ const alignRight = async (): Promise => { await handleHorizontalAlignment('right'); }; /** * Aligns selected shapes to the top edge of the slide */ const alignTop = async (): Promise => { await handleVerticalAlignment('top'); }; /** * Aligns selected shapes to the vertical middle of the slide */ const alignMiddle = async (): Promise => { await handleVerticalAlignment('middle'); }; /** * Aligns selected shapes to the bottom edge of the slide */ const alignBottom = async (): Promise => { await handleVerticalAlignment('bottom'); }; return (
Select objects on the slide, then align them to the slide edges or center. } disabled={isProcessing} onClick={alignLeft} /> } disabled={isProcessing} onClick={alignCenter} /> } disabled={isProcessing} onClick={alignRight} /> } disabled={isProcessing} onClick={alignTop} /> } disabled={isProcessing} onClick={alignMiddle} /> } disabled={isProcessing} onClick={alignBottom} />
); }; export default AlignmentButtons;