Fix alignment, progress bar, grid safety, and strict typing issues
- Load layout name before reading it in progress bar creation - Load shape dimensions before aligning and align to slide edges/center - Clamp grid spacing/opacity and guard zero spacing before deleting a grid - Use hasText and text-capable shape checks with null-safe property matching - Add info status style and remove dead imports - Enable strict mode and skipLibCheck; add typecheck script
This commit is contained in:
1 parent
16491c00ce
commit
791cbc7b6c
13 files changed
+271
-183
No files matched your search
@@ -20,26 +20,19 @@ import {
|
||||
} from "@fluentui/react-icons";
|
||||
import { useStatusContext } from "./App";
|
||||
import { useCommonStyles } from "./commonStyles";
|
||||
import { getErrorMessage } from "../types/office-types";
|
||||
|
||||
/**
|
||||
* Configuration constants for alignment positions
|
||||
* Slide dimensions in points (16:9). Shapes are aligned to the slide edges
|
||||
* and center, matching PowerPoint's "Align to Slide" behavior.
|
||||
*/
|
||||
const ALIGNMENT_CONFIG = {
|
||||
// Horizontal alignment positions
|
||||
HORIZONTAL: {
|
||||
LEFT: 81.1,
|
||||
CENTER: 480, // Center point of slide
|
||||
RIGHT: 879.75
|
||||
},
|
||||
// Vertical alignment positions
|
||||
VERTICAL: {
|
||||
TOP: 136.75,
|
||||
MIDDLE: 321, // Middle point of slide
|
||||
BOTTOM: 505.25
|
||||
},
|
||||
// Warning message when no shapes are selected
|
||||
NO_SELECTION_WARNING: "No shapes are selected. Please select shapes first."
|
||||
};
|
||||
const SLIDE_WIDTH = 960;
|
||||
const SLIDE_HEIGHT = 540;
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -89,11 +82,10 @@ export const AlignmentButtons: React.FC = () => {
|
||||
* @param error - The error object
|
||||
* @param operation - The operation being performed (for logging)
|
||||
*/
|
||||
const handleError = (error: any, operation: string): void => {
|
||||
setStatusMessage(`Error: ${error.message || "Unknown error occurred"}`);
|
||||
const handleError = (error: unknown, operation: string): void => {
|
||||
setStatusMessage(`Error: ${getErrorMessage(error)}`);
|
||||
setStatusType("error");
|
||||
console.error(`${operation} error:`, error);
|
||||
setIsProcessing(false);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -112,33 +104,39 @@ export const AlignmentButtons: React.FC = () => {
|
||||
|
||||
// Check if shapes are selected
|
||||
if (shapes.items.length === 0) {
|
||||
setStatusMessage(ALIGNMENT_CONFIG.NO_SELECTION_WARNING);
|
||||
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 => {
|
||||
shapes.items.forEach((shape) => {
|
||||
switch (alignmentType) {
|
||||
case 'left':
|
||||
shape.left = ALIGNMENT_CONFIG.HORIZONTAL.LEFT;
|
||||
case "left":
|
||||
shape.left = 0;
|
||||
break;
|
||||
case 'center':
|
||||
shape.left = ALIGNMENT_CONFIG.HORIZONTAL.CENTER - (shape.width / 2);
|
||||
case "center":
|
||||
shape.left = Math.max(0, (SLIDE_WIDTH - shape.width) / 2);
|
||||
break;
|
||||
case 'right':
|
||||
shape.left = ALIGNMENT_CONFIG.HORIZONTAL.RIGHT - shape.width;
|
||||
case "right":
|
||||
shape.left = Math.max(0, SLIDE_WIDTH - shape.width);
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// Single sync after all updates
|
||||
await context.sync();
|
||||
|
||||
|
||||
setStatusMessage(`Objects aligned to ${alignmentType}.`);
|
||||
setStatusType("success");
|
||||
});
|
||||
} catch (error) {
|
||||
} catch (error: unknown) {
|
||||
handleError(error, `Align ${alignmentType}`);
|
||||
} finally {
|
||||
setIsProcessing(false);
|
||||
@@ -161,33 +159,39 @@ export const AlignmentButtons: React.FC = () => {
|
||||
|
||||
// Check if shapes are selected
|
||||
if (shapes.items.length === 0) {
|
||||
setStatusMessage(ALIGNMENT_CONFIG.NO_SELECTION_WARNING);
|
||||
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 => {
|
||||
shapes.items.forEach((shape) => {
|
||||
switch (alignmentType) {
|
||||
case 'top':
|
||||
shape.top = ALIGNMENT_CONFIG.VERTICAL.TOP;
|
||||
case "top":
|
||||
shape.top = 0;
|
||||
break;
|
||||
case 'middle':
|
||||
shape.top = ALIGNMENT_CONFIG.VERTICAL.MIDDLE - (shape.height / 2);
|
||||
case "middle":
|
||||
shape.top = Math.max(0, (SLIDE_HEIGHT - shape.height) / 2);
|
||||
break;
|
||||
case 'bottom':
|
||||
shape.top = ALIGNMENT_CONFIG.VERTICAL.BOTTOM - shape.height;
|
||||
case "bottom":
|
||||
shape.top = Math.max(0, SLIDE_HEIGHT - shape.height);
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// Single sync after all updates
|
||||
await context.sync();
|
||||
|
||||
|
||||
setStatusMessage(`Objects aligned to ${alignmentType}.`);
|
||||
setStatusType("success");
|
||||
});
|
||||
} catch (error) {
|
||||
} catch (error: unknown) {
|
||||
handleError(error, `Align ${alignmentType}`);
|
||||
} finally {
|
||||
setIsProcessing(false);
|
||||
|
||||
Reference in new issue
Block a user