- Add a header, a shared accordion, a docked status region, and token-based styles - Use the blue primary style for every action button and collapse all sections by default - Improve labels, selection hints, destructive confirmations, grid semantics, and narrow-pane layout - Add docs/UI_GUIDELINES.md and update the README, user guide, and developer guide
310 lines
8.6 KiB
TypeScript
310 lines
8.6 KiB
TypeScript
/**
|
|
* @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<AlignButtonProps> = ({ label, icon, disabled, onClick }) => (
|
|
<Tooltip content={label} relationship="description">
|
|
<ToolbarButton
|
|
appearance="primary"
|
|
aria-label={label}
|
|
icon={icon}
|
|
disabled={disabled}
|
|
onClick={onClick}
|
|
/>
|
|
</Tooltip>
|
|
);
|
|
|
|
/**
|
|
* 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<void> => {
|
|
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<void> => {
|
|
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<void> => {
|
|
await handleHorizontalAlignment('left');
|
|
};
|
|
|
|
/**
|
|
* Aligns selected shapes to the horizontal center of the slide
|
|
*/
|
|
const alignCenter = async (): Promise<void> => {
|
|
await handleHorizontalAlignment('center');
|
|
};
|
|
|
|
/**
|
|
* Aligns selected shapes to the right edge of the slide
|
|
*/
|
|
const alignRight = async (): Promise<void> => {
|
|
await handleHorizontalAlignment('right');
|
|
};
|
|
|
|
/**
|
|
* Aligns selected shapes to the top edge of the slide
|
|
*/
|
|
const alignTop = async (): Promise<void> => {
|
|
await handleVerticalAlignment('top');
|
|
};
|
|
|
|
/**
|
|
* Aligns selected shapes to the vertical middle of the slide
|
|
*/
|
|
const alignMiddle = async (): Promise<void> => {
|
|
await handleVerticalAlignment('middle');
|
|
};
|
|
|
|
/**
|
|
* Aligns selected shapes to the bottom edge of the slide
|
|
*/
|
|
const alignBottom = async (): Promise<void> => {
|
|
await handleVerticalAlignment('bottom');
|
|
};
|
|
|
|
return (
|
|
<div className={commonStyles.container}>
|
|
<Text size={200} className={commonStyles.hint}>
|
|
Select objects on the slide, then align them to the slide edges or center.
|
|
</Text>
|
|
<Toolbar aria-label="Align selected objects" className={styles.toolbar}>
|
|
<AlignButton
|
|
label="Align left"
|
|
icon={<TextAlignLeftRegular />}
|
|
disabled={isProcessing}
|
|
onClick={alignLeft}
|
|
/>
|
|
<AlignButton
|
|
label="Align center"
|
|
icon={<TextAlignCenterRegular />}
|
|
disabled={isProcessing}
|
|
onClick={alignCenter}
|
|
/>
|
|
<AlignButton
|
|
label="Align right"
|
|
icon={<TextAlignRightRegular />}
|
|
disabled={isProcessing}
|
|
onClick={alignRight}
|
|
/>
|
|
<ToolbarDivider />
|
|
<AlignButton
|
|
label="Align top"
|
|
icon={<TextboxAlignTopRegular />}
|
|
disabled={isProcessing}
|
|
onClick={alignTop}
|
|
/>
|
|
<AlignButton
|
|
label="Align middle"
|
|
icon={<TextboxAlignMiddleRegular />}
|
|
disabled={isProcessing}
|
|
onClick={alignMiddle}
|
|
/>
|
|
<AlignButton
|
|
label="Align bottom"
|
|
icon={<TextboxAlignBottomRegular />}
|
|
disabled={isProcessing}
|
|
onClick={alignBottom}
|
|
/>
|
|
</Toolbar>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default AlignmentButtons;
|