- Add 12 × 8 and 24 × 16 wireframe presets plus custom columns, rows, gutter, opacity, and colour - Place the raster on the current slide, its layout, or the slide master so background rasters sit behind content - Keep one raster per presentation: add clears every scope and remove cleans the whole presentation - Share the colour swatch control across grids, guidelines, and rasters, including grey - Add raster geometry tests and document scope, background, and export caveats
This commit is contained in:
1 parent
e223dc61c8
commit
9889ce1025
10 files changed
+1106
-206
No files matched your search
@@ -17,6 +17,7 @@ import DraftButtons from "./DraftButtons";
|
||||
import ProgressBarButtons from "./ProgressBarButtons";
|
||||
import AlignmentButtons from "./AlignmentButtons";
|
||||
import GridGuidelineManager from "./GridGuidelineManager";
|
||||
import RasterManager from "./RasterManager";
|
||||
import Section from "./Section";
|
||||
import {
|
||||
Accordion,
|
||||
@@ -231,6 +232,10 @@ const App: React.FC = () => {
|
||||
<GridGuidelineManager />
|
||||
</Section>
|
||||
|
||||
<Section title="Raster" value="raster">
|
||||
<RasterManager />
|
||||
</Section>
|
||||
|
||||
<Section title="Settings" value="settings">
|
||||
<Field
|
||||
label="Slide size"
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* @file ColorSwatches.tsx
|
||||
* @description Shared colour picker for grids, guidelines, and rasters.
|
||||
*/
|
||||
|
||||
import * as React from "react";
|
||||
import { Button, makeStyles, Text, tokens } from "@fluentui/react-components";
|
||||
import { CheckmarkRegular } from "@fluentui/react-icons";
|
||||
|
||||
export type SwatchColor = "blue" | "red" | "yellow" | "green" | "gray";
|
||||
|
||||
/** Colours written to the slide. */
|
||||
export const SWATCH_VALUES: Record<SwatchColor, string> = {
|
||||
blue: "#4472C4",
|
||||
red: "#C00000",
|
||||
yellow: "#FFC000",
|
||||
green: "#70AD47",
|
||||
gray: "#D9D9D9",
|
||||
};
|
||||
|
||||
const SWATCH_LABELS: Record<SwatchColor, string> = {
|
||||
blue: "Blue",
|
||||
red: "Red",
|
||||
yellow: "Yellow",
|
||||
green: "Green",
|
||||
gray: "Gray",
|
||||
};
|
||||
|
||||
const SWATCH_ORDER: readonly SwatchColor[] = ["blue", "red", "yellow", "green", "gray"];
|
||||
|
||||
/** Choose a check colour that stays readable on every swatch. */
|
||||
const getSwatchForeground = (color: SwatchColor): string =>
|
||||
color === "yellow" || color === "green" || color === "gray" ? "#000000" : "#ffffff";
|
||||
|
||||
const useStyles = makeStyles({
|
||||
row: {
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
gap: tokens.spacingHorizontalS,
|
||||
},
|
||||
label: {
|
||||
minWidth: "48px",
|
||||
color: tokens.colorNeutralForeground2,
|
||||
},
|
||||
group: {
|
||||
display: "flex",
|
||||
gap: tokens.spacingHorizontalXS,
|
||||
},
|
||||
button: {
|
||||
width: "32px",
|
||||
height: "32px",
|
||||
minWidth: "32px",
|
||||
padding: 0,
|
||||
border: `1px solid ${tokens.colorNeutralStroke1}`,
|
||||
borderRadius: tokens.borderRadiusMedium,
|
||||
},
|
||||
selected: {
|
||||
border: `2px solid ${tokens.colorBrandForeground1}`,
|
||||
},
|
||||
});
|
||||
|
||||
export interface ColorSwatchesProps {
|
||||
/** Visible group label, e.g. "Color" */
|
||||
label: string;
|
||||
value: SwatchColor;
|
||||
onChange: (color: SwatchColor) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* A labelled group of colour swatches with a check mark on the selected value.
|
||||
*/
|
||||
export const ColorSwatches: React.FC<ColorSwatchesProps> = ({ label, value, onChange }) => {
|
||||
const styles = useStyles();
|
||||
const labelId = React.useId();
|
||||
|
||||
return (
|
||||
<div className={styles.row}>
|
||||
<Text id={labelId} size={200} className={styles.label}>
|
||||
{label}
|
||||
</Text>
|
||||
<div className={styles.group} role="group" aria-labelledby={labelId}>
|
||||
{SWATCH_ORDER.map((color) => {
|
||||
const selected = color === value;
|
||||
return (
|
||||
<Button
|
||||
key={color}
|
||||
appearance="subtle"
|
||||
className={`${styles.button} ${selected ? styles.selected : ""}`}
|
||||
style={{
|
||||
backgroundColor: SWATCH_VALUES[color],
|
||||
color: getSwatchForeground(color),
|
||||
width: "32px",
|
||||
minWidth: "32px",
|
||||
height: "32px",
|
||||
padding: 0,
|
||||
}}
|
||||
onClick={() => onChange(color)}
|
||||
title={`${SWATCH_LABELS[color]} color`}
|
||||
aria-label={`${SWATCH_LABELS[color]} color`}
|
||||
aria-pressed={selected}
|
||||
icon={selected ? <CheckmarkRegular /> : undefined}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default ColorSwatches;
|
||||
@@ -1,31 +1,30 @@
|
||||
/**
|
||||
* GridGuidelineManager Component
|
||||
*
|
||||
*
|
||||
* This component provides functionality to create and manage grids and guidelines
|
||||
* on PowerPoint slides. It allows users to add customizable grids with adjustable
|
||||
* spacing, opacity, and color, as well as add individual guidelines with custom
|
||||
* positioning and color.
|
||||
*
|
||||
*
|
||||
* @module GridGuidelineManager
|
||||
*/
|
||||
|
||||
import * as React from "react";
|
||||
import {
|
||||
import {
|
||||
Button,
|
||||
Field,
|
||||
Input,
|
||||
makeStyles,
|
||||
Text,
|
||||
ToggleButton,
|
||||
tokens
|
||||
tokens,
|
||||
} from "@fluentui/react-components";
|
||||
import {
|
||||
CheckmarkRegular,
|
||||
import {
|
||||
DismissRegular,
|
||||
AddRegular,
|
||||
LineHorizontal3Regular,
|
||||
SplitVerticalRegular,
|
||||
GridDotsRegular
|
||||
GridDotsRegular,
|
||||
} from "@fluentui/react-icons";
|
||||
import { useStatusContext } from "../context/StatusContext";
|
||||
import { useSettings } from "../context/SettingsContext";
|
||||
@@ -33,6 +32,7 @@ import { deleteShapesByPrefix, getErrorMessage } from "../types/office-types";
|
||||
import { SHAPE_NAME_PREFIXES } from "../constants";
|
||||
import { isCancellationRequested, resetCancellation } from "../services/cancellation";
|
||||
import ConfirmButton from "./ConfirmButton";
|
||||
import { ColorSwatches, SwatchColor, SWATCH_VALUES } from "./ColorSwatches";
|
||||
|
||||
/**
|
||||
* Safety limits for user-provided grid and guideline values.
|
||||
@@ -52,73 +52,35 @@ const GUIDE_PREFIX = SHAPE_NAME_PREFIXES.guide;
|
||||
const clamp = (value: number, min: number, max: number): number =>
|
||||
Math.min(max, Math.max(min, value));
|
||||
|
||||
/**
|
||||
* Available colors for grids and guidelines
|
||||
*/
|
||||
type GridColor = "blue" | "red" | "yellow" | "green";
|
||||
|
||||
/**
|
||||
* Direction options for guidelines
|
||||
*/
|
||||
type GuideDirection = "horizontal" | "vertical";
|
||||
|
||||
/**
|
||||
* Color values mapping for the available grid and guideline colors
|
||||
*/
|
||||
const colorValues = {
|
||||
blue: "#4472C4",
|
||||
red: "#C00000",
|
||||
yellow: "#FFC000",
|
||||
green: "#70AD47"
|
||||
};
|
||||
|
||||
/**
|
||||
* Props for the ColorButton component
|
||||
*/
|
||||
interface ColorButtonProps {
|
||||
/** The color this button represents */
|
||||
color: GridColor;
|
||||
/** The currently selected color */
|
||||
selectedColor: GridColor;
|
||||
/** Callback function when the color is clicked */
|
||||
onClick: (color: GridColor) => void;
|
||||
}
|
||||
|
||||
const useStyles = makeStyles({
|
||||
root: {
|
||||
display: "flex",
|
||||
flexDirection: "column",
|
||||
gap: tokens.spacingVerticalM
|
||||
gap: tokens.spacingVerticalM,
|
||||
},
|
||||
hint: {
|
||||
color: tokens.colorNeutralForeground3
|
||||
color: tokens.colorNeutralForeground3,
|
||||
},
|
||||
field: {
|
||||
maxWidth: "180px"
|
||||
maxWidth: "180px",
|
||||
},
|
||||
controlRow: {
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
gap: tokens.spacingHorizontalS
|
||||
gap: tokens.spacingHorizontalS,
|
||||
},
|
||||
controlLabel: {
|
||||
minWidth: "48px",
|
||||
color: tokens.colorNeutralForeground2
|
||||
color: tokens.colorNeutralForeground2,
|
||||
},
|
||||
colorGroup: {
|
||||
inlineGroup: {
|
||||
display: "flex",
|
||||
gap: tokens.spacingHorizontalXS
|
||||
},
|
||||
colorButton: {
|
||||
width: "32px",
|
||||
height: "32px",
|
||||
minWidth: "32px",
|
||||
padding: 0,
|
||||
border: `1px solid ${tokens.colorNeutralStroke1}`,
|
||||
borderRadius: tokens.borderRadiusMedium
|
||||
},
|
||||
colorButtonSelected: {
|
||||
border: `2px solid ${tokens.colorBrandForeground1}`
|
||||
gap: tokens.spacingHorizontalXS,
|
||||
},
|
||||
buttonGrid: {
|
||||
display: "grid",
|
||||
@@ -126,12 +88,12 @@ const useStyles = makeStyles({
|
||||
gap: tokens.spacingHorizontalS,
|
||||
width: "100%",
|
||||
"@media (max-width: 360px)": {
|
||||
gridTemplateColumns: "1fr"
|
||||
}
|
||||
gridTemplateColumns: "1fr",
|
||||
},
|
||||
},
|
||||
gridButton: {
|
||||
width: "100%",
|
||||
minWidth: 0
|
||||
minWidth: 0,
|
||||
},
|
||||
guidesContainer: {
|
||||
display: "flex",
|
||||
@@ -140,58 +102,20 @@ const useStyles = makeStyles({
|
||||
paddingTop: tokens.spacingVerticalM,
|
||||
borderTopWidth: "1px",
|
||||
borderTopStyle: "solid",
|
||||
borderTopColor: tokens.colorNeutralStroke3
|
||||
}
|
||||
borderTopColor: tokens.colorNeutralStroke3,
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* Returns a check colour with sufficient contrast for each swatch.
|
||||
*/
|
||||
const getSwatchForeground = (color: GridColor): string =>
|
||||
color === "yellow" || color === "green" ? "#000000" : "#ffffff";
|
||||
|
||||
/**
|
||||
* ColorButton component - Renders a color selection button
|
||||
*
|
||||
* @param props - Component props
|
||||
* @returns React component
|
||||
*/
|
||||
const ColorButton: React.FC<ColorButtonProps> = ({ color, selectedColor, onClick }) => {
|
||||
const styles = useStyles();
|
||||
const label = color.charAt(0).toUpperCase() + color.slice(1);
|
||||
const selected = color === selectedColor;
|
||||
|
||||
return (
|
||||
<Button
|
||||
appearance="subtle"
|
||||
className={`${styles.colorButton} ${selected ? styles.colorButtonSelected : ''}`}
|
||||
style={{
|
||||
backgroundColor: colorValues[color],
|
||||
color: getSwatchForeground(color),
|
||||
width: "32px",
|
||||
minWidth: "32px",
|
||||
height: "32px",
|
||||
padding: 0
|
||||
}}
|
||||
onClick={() => onClick(color)}
|
||||
title={`${label} color`}
|
||||
aria-label={`${label} color`}
|
||||
aria-pressed={selected}
|
||||
icon={selected ? <CheckmarkRegular /> : undefined}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Custom hook for handling numeric input fields
|
||||
*
|
||||
*
|
||||
* @param initialValue - The initial value for the input
|
||||
* @returns A tuple containing the current value and a change handler function
|
||||
*/
|
||||
type NumericInputHandler = [
|
||||
number,
|
||||
(e: React.ChangeEvent<HTMLInputElement>) => void,
|
||||
(value: number) => void
|
||||
(value: number) => void,
|
||||
];
|
||||
|
||||
const useNumericInputHandler = (initialValue: number): NumericInputHandler => {
|
||||
@@ -214,33 +138,28 @@ const useNumericInputHandler = (initialValue: number): NumericInputHandler => {
|
||||
/**
|
||||
* GridGuidelineManager component - Provides UI and functionality for creating
|
||||
* and managing grids and guidelines on PowerPoint slides
|
||||
*
|
||||
*
|
||||
* @returns React component
|
||||
*/
|
||||
export const GridGuidelineManager: React.FC = () => {
|
||||
const styles = useStyles();
|
||||
const { slideSize } = useSettings();
|
||||
const {
|
||||
setStatusMessage,
|
||||
setStatusType,
|
||||
isProcessing,
|
||||
setIsProcessing,
|
||||
setProgress
|
||||
} = useStatusContext();
|
||||
const { setStatusMessage, setStatusType, isProcessing, setIsProcessing, setProgress } =
|
||||
useStatusContext();
|
||||
|
||||
// Grid state
|
||||
const [gridSpacing, handleGridSpacingChange, setGridSpacing] = useNumericInputHandler(50);
|
||||
const [gridColor, setGridColor] = React.useState<GridColor>("blue");
|
||||
const [gridColor, setGridColor] = React.useState<SwatchColor>("blue");
|
||||
const [gridOpacity, handleGridOpacityChange, setGridOpacity] = useNumericInputHandler(30);
|
||||
|
||||
|
||||
// Guidelines state
|
||||
const [guidePosition, handleGuidePositionChange, setGuidePosition] = useNumericInputHandler(240);
|
||||
const [guideDirection, setGuideDirection] = React.useState<GuideDirection>("horizontal");
|
||||
const [guideColor, setGuideColor] = React.useState<GridColor>("red");
|
||||
const [guideColor, setGuideColor] = React.useState<SwatchColor>("red");
|
||||
|
||||
/**
|
||||
* Creates a line shape on the slide using a thin rectangle
|
||||
*
|
||||
*
|
||||
* @param slide - The PowerPoint slide to add the shape to
|
||||
* @param startX - Starting X coordinate
|
||||
* @param startY - Starting Y coordinate
|
||||
@@ -248,32 +167,29 @@ export const GridGuidelineManager: React.FC = () => {
|
||||
* @param endY - Ending Y coordinate
|
||||
* @returns The created shape object
|
||||
*/
|
||||
const createLineShape = React.useCallback((
|
||||
slide: PowerPoint.Slide,
|
||||
startX: number,
|
||||
startY: number,
|
||||
endX: number,
|
||||
endY: number
|
||||
) => {
|
||||
// Create a line using alternative approach with a rectangle shape
|
||||
const line = slide.shapes.addGeometricShape("Rectangle");
|
||||
|
||||
if (startX === endX) {
|
||||
// Vertical line
|
||||
line.left = startX;
|
||||
line.top = startY;
|
||||
line.height = endY - startY;
|
||||
line.width = 1; // Make it 1pt wide
|
||||
} else {
|
||||
// Horizontal line
|
||||
line.left = startX;
|
||||
line.top = startY;
|
||||
line.width = endX - startX;
|
||||
line.height = 1; // Make it 1pt tall
|
||||
}
|
||||
|
||||
return line;
|
||||
}, []);
|
||||
const createLineShape = React.useCallback(
|
||||
(slide: PowerPoint.Slide, startX: number, startY: number, endX: number, endY: number) => {
|
||||
// Create a line using alternative approach with a rectangle shape
|
||||
const line = slide.shapes.addGeometricShape("Rectangle");
|
||||
|
||||
if (startX === endX) {
|
||||
// Vertical line
|
||||
line.left = startX;
|
||||
line.top = startY;
|
||||
line.height = endY - startY;
|
||||
line.width = 1; // Make it 1pt wide
|
||||
} else {
|
||||
// Horizontal line
|
||||
line.left = startX;
|
||||
line.top = startY;
|
||||
line.width = endX - startX;
|
||||
line.height = 1; // Make it 1pt tall
|
||||
}
|
||||
|
||||
return line;
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
/**
|
||||
* Creates a grid on the current slide based on the configured settings
|
||||
@@ -320,11 +236,11 @@ export const GridGuidelineManager: React.FC = () => {
|
||||
const selectedSlides = context.presentation.getSelectedSlides();
|
||||
selectedSlides.load("items");
|
||||
await context.sync();
|
||||
|
||||
|
||||
if (selectedSlides.items.length === 0) {
|
||||
throw new Error("No slide selected");
|
||||
}
|
||||
|
||||
|
||||
const currentSlide = selectedSlides.items[0];
|
||||
|
||||
// Remove any existing grid on this slide inside the same request context
|
||||
@@ -346,50 +262,50 @@ export const GridGuidelineManager: React.FC = () => {
|
||||
|
||||
const totalLines = numHorizontalLines + numVerticalLines;
|
||||
let queuedLines = 0;
|
||||
|
||||
|
||||
// Create horizontal grid lines
|
||||
for (let i = 1; i <= numHorizontalLines; i++) {
|
||||
if (isCancellationRequested()) break;
|
||||
|
||||
const yPosition = i * spacing;
|
||||
|
||||
|
||||
// Skip if we're at the edge of the slide
|
||||
if (yPosition >= slideSize.height) continue;
|
||||
|
||||
|
||||
// Create horizontal line
|
||||
const line = createLineShape(currentSlide, 0, yPosition, slideSize.width, yPosition);
|
||||
line.name = `${GRID_PREFIX}h_${i}`;
|
||||
|
||||
|
||||
// Set line properties
|
||||
line.fill.setSolidColor(colorValues[gridColor]);
|
||||
line.fill.setSolidColor(SWATCH_VALUES[gridColor]);
|
||||
line.lineFormat.weight = 0.75; // Thin line
|
||||
line.lineFormat.visible = false; // No outline, just fill
|
||||
|
||||
|
||||
// Set transparency (opacity is inverse of transparency)
|
||||
line.fill.transparency = (100 - opacity) / 100;
|
||||
|
||||
queuedLines++;
|
||||
setProgress({ current: queuedLines, total: totalLines });
|
||||
}
|
||||
|
||||
|
||||
// Create vertical grid lines
|
||||
for (let i = 1; i <= numVerticalLines; i++) {
|
||||
if (isCancellationRequested()) break;
|
||||
|
||||
const xPosition = i * spacing;
|
||||
|
||||
|
||||
// Skip if we're at the edge of the slide
|
||||
if (xPosition >= slideSize.width) continue;
|
||||
|
||||
|
||||
// Create vertical line
|
||||
const line = createLineShape(currentSlide, xPosition, 0, xPosition, slideSize.height);
|
||||
line.name = `${GRID_PREFIX}v_${i}`;
|
||||
|
||||
|
||||
// Set line properties
|
||||
line.fill.setSolidColor(colorValues[gridColor]);
|
||||
line.fill.setSolidColor(SWATCH_VALUES[gridColor]);
|
||||
line.lineFormat.weight = 0.75; // Thin line
|
||||
line.lineFormat.visible = false; // No outline, just fill
|
||||
|
||||
|
||||
// Set transparency (opacity is inverse of transparency)
|
||||
line.fill.transparency = (100 - opacity) / 100;
|
||||
|
||||
@@ -413,8 +329,20 @@ export const GridGuidelineManager: React.FC = () => {
|
||||
setIsProcessing(false);
|
||||
setProgress(null);
|
||||
}
|
||||
}, [createLineShape, gridColor, gridOpacity, gridSpacing, setGridOpacity, setGridSpacing, setIsProcessing, setProgress, setStatusMessage, setStatusType, slideSize]);
|
||||
|
||||
}, [
|
||||
createLineShape,
|
||||
gridColor,
|
||||
gridOpacity,
|
||||
gridSpacing,
|
||||
setGridOpacity,
|
||||
setGridSpacing,
|
||||
setIsProcessing,
|
||||
setProgress,
|
||||
setStatusMessage,
|
||||
setStatusType,
|
||||
slideSize,
|
||||
]);
|
||||
|
||||
/**
|
||||
* Removes the grid from the current slide
|
||||
*/
|
||||
@@ -422,34 +350,34 @@ export const GridGuidelineManager: React.FC = () => {
|
||||
resetCancellation();
|
||||
setProgress(null);
|
||||
setIsProcessing(true);
|
||||
|
||||
|
||||
try {
|
||||
await PowerPoint.run(async (context) => {
|
||||
// Get the current slide using the selected slides collection
|
||||
const selectedSlides = context.presentation.getSelectedSlides();
|
||||
selectedSlides.load("items");
|
||||
await context.sync();
|
||||
|
||||
|
||||
if (selectedSlides.items.length === 0) {
|
||||
throw new Error("No slide selected");
|
||||
}
|
||||
|
||||
|
||||
const currentSlide = selectedSlides.items[0];
|
||||
|
||||
|
||||
// Load all shapes on this slide
|
||||
currentSlide.shapes.load("items");
|
||||
await context.sync();
|
||||
|
||||
|
||||
// Load all shape names at once
|
||||
for (const shape of currentSlide.shapes.items) {
|
||||
shape.load("name");
|
||||
}
|
||||
await context.sync();
|
||||
|
||||
|
||||
// Delete all grid shapes and commit the changes
|
||||
const removedCount = deleteShapesByPrefix(currentSlide.shapes.items, GRID_PREFIX);
|
||||
await context.sync();
|
||||
|
||||
|
||||
if (removedCount > 0) {
|
||||
setStatusMessage(`Removed grid from slide`);
|
||||
setStatusType("success");
|
||||
@@ -493,18 +421,17 @@ export const GridGuidelineManager: React.FC = () => {
|
||||
const selectedSlides = context.presentation.getSelectedSlides();
|
||||
selectedSlides.load("items");
|
||||
await context.sync();
|
||||
|
||||
|
||||
if (selectedSlides.items.length === 0) {
|
||||
throw new Error("No slide selected");
|
||||
}
|
||||
|
||||
|
||||
const currentSlide = selectedSlides.items[0];
|
||||
|
||||
|
||||
|
||||
// Create a unique identifier for this guide
|
||||
const timestamp = new Date().getTime();
|
||||
const guideId = `${GUIDE_PREFIX}${guideDirection}_${timestamp}`;
|
||||
|
||||
|
||||
// Add the guide line
|
||||
let line: PowerPoint.Shape;
|
||||
if (guideDirection === "horizontal") {
|
||||
@@ -514,16 +441,16 @@ export const GridGuidelineManager: React.FC = () => {
|
||||
// Create vertical guideline
|
||||
line = createLineShape(currentSlide, position, 0, position, slideSize.height);
|
||||
}
|
||||
|
||||
|
||||
line.name = guideId;
|
||||
|
||||
|
||||
// Set line properties
|
||||
line.fill.setSolidColor(colorValues[guideColor]);
|
||||
line.fill.setSolidColor(SWATCH_VALUES[guideColor]);
|
||||
line.lineFormat.visible = false; // No outline, just fill
|
||||
|
||||
|
||||
// We want solid lines for guidelines
|
||||
line.fill.transparency = 0;
|
||||
|
||||
|
||||
setStatusMessage(`Added ${guideDirection} guideline`);
|
||||
setStatusType("success");
|
||||
});
|
||||
@@ -535,8 +462,19 @@ export const GridGuidelineManager: React.FC = () => {
|
||||
setIsProcessing(false);
|
||||
setProgress(null);
|
||||
}
|
||||
}, [createLineShape, guideColor, guideDirection, guidePosition, setGuidePosition, setIsProcessing, setProgress, setStatusMessage, setStatusType, slideSize]);
|
||||
|
||||
}, [
|
||||
createLineShape,
|
||||
guideColor,
|
||||
guideDirection,
|
||||
guidePosition,
|
||||
setGuidePosition,
|
||||
setIsProcessing,
|
||||
setProgress,
|
||||
setStatusMessage,
|
||||
setStatusType,
|
||||
slideSize,
|
||||
]);
|
||||
|
||||
/**
|
||||
* Removes all guidelines from the current slide
|
||||
*/
|
||||
@@ -550,27 +488,27 @@ export const GridGuidelineManager: React.FC = () => {
|
||||
const selectedSlides = context.presentation.getSelectedSlides();
|
||||
selectedSlides.load("items");
|
||||
await context.sync();
|
||||
|
||||
|
||||
if (selectedSlides.items.length === 0) {
|
||||
throw new Error("No slide selected");
|
||||
}
|
||||
|
||||
|
||||
const currentSlide = selectedSlides.items[0];
|
||||
|
||||
|
||||
// Load all shapes on this slide
|
||||
currentSlide.shapes.load("items");
|
||||
await context.sync();
|
||||
|
||||
|
||||
// Load all shape names at once
|
||||
for (const shape of currentSlide.shapes.items) {
|
||||
shape.load("name");
|
||||
}
|
||||
await context.sync();
|
||||
|
||||
|
||||
// Delete all guideline shapes and commit the changes
|
||||
const removedCount = deleteShapesByPrefix(currentSlide.shapes.items, GUIDE_PREFIX);
|
||||
await context.sync();
|
||||
|
||||
|
||||
if (removedCount > 0) {
|
||||
setStatusMessage(`Removed ${removedCount} guidelines`);
|
||||
setStatusType("success");
|
||||
@@ -591,7 +529,9 @@ export const GridGuidelineManager: React.FC = () => {
|
||||
|
||||
return (
|
||||
<div className={styles.root}>
|
||||
<Text size={400} weight="semibold">Grid</Text>
|
||||
<Text size={400} weight="semibold">
|
||||
Grid
|
||||
</Text>
|
||||
<Text size={200} className={styles.hint}>
|
||||
Grid lines are slide shapes and appear in slideshows and exports.
|
||||
</Text>
|
||||
@@ -618,21 +558,13 @@ export const GridGuidelineManager: React.FC = () => {
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<div className={styles.controlRow}>
|
||||
<Text id="grid-color-label" size={200} className={styles.controlLabel}>Color</Text>
|
||||
<div className={styles.colorGroup} role="group" aria-labelledby="grid-color-label">
|
||||
<ColorButton color="blue" selectedColor={gridColor} onClick={setGridColor} />
|
||||
<ColorButton color="red" selectedColor={gridColor} onClick={setGridColor} />
|
||||
<ColorButton color="yellow" selectedColor={gridColor} onClick={setGridColor} />
|
||||
<ColorButton color="green" selectedColor={gridColor} onClick={setGridColor} />
|
||||
</div>
|
||||
</div>
|
||||
<ColorSwatches label="Color" value={gridColor} onChange={setGridColor} />
|
||||
|
||||
<div className={styles.buttonGrid}>
|
||||
<Button
|
||||
appearance="primary"
|
||||
<Button
|
||||
appearance="primary"
|
||||
className={styles.gridButton}
|
||||
onClick={createGrid}
|
||||
onClick={createGrid}
|
||||
icon={<GridDotsRegular />}
|
||||
disabled={isProcessing}
|
||||
title="Add grid"
|
||||
@@ -656,7 +588,9 @@ export const GridGuidelineManager: React.FC = () => {
|
||||
|
||||
{/* Guidelines Section */}
|
||||
<div className={styles.guidesContainer}>
|
||||
<Text size={400} weight="semibold">Guidelines</Text>
|
||||
<Text size={400} weight="semibold">
|
||||
Guidelines
|
||||
</Text>
|
||||
|
||||
<Field label="Position (pt)" className={styles.field}>
|
||||
<Input
|
||||
@@ -670,8 +604,10 @@ export const GridGuidelineManager: React.FC = () => {
|
||||
</Field>
|
||||
|
||||
<div className={styles.controlRow}>
|
||||
<Text id="guide-direction-label" size={200} className={styles.controlLabel}>Type</Text>
|
||||
<div className={styles.colorGroup} role="group" aria-labelledby="guide-direction-label">
|
||||
<Text id="guide-direction-label" size={200} className={styles.controlLabel}>
|
||||
Type
|
||||
</Text>
|
||||
<div className={styles.inlineGroup} role="group" aria-labelledby="guide-direction-label">
|
||||
<ToggleButton
|
||||
size="small"
|
||||
appearance="outline"
|
||||
@@ -693,21 +629,13 @@ export const GridGuidelineManager: React.FC = () => {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className={styles.controlRow}>
|
||||
<Text id="guide-color-label" size={200} className={styles.controlLabel}>Color</Text>
|
||||
<div className={styles.colorGroup} role="group" aria-labelledby="guide-color-label">
|
||||
<ColorButton color="blue" selectedColor={guideColor} onClick={setGuideColor} />
|
||||
<ColorButton color="red" selectedColor={guideColor} onClick={setGuideColor} />
|
||||
<ColorButton color="yellow" selectedColor={guideColor} onClick={setGuideColor} />
|
||||
<ColorButton color="green" selectedColor={guideColor} onClick={setGuideColor} />
|
||||
</div>
|
||||
</div>
|
||||
<ColorSwatches label="Color" value={guideColor} onChange={setGuideColor} />
|
||||
|
||||
<div className={styles.buttonGrid}>
|
||||
<Button
|
||||
appearance="primary"
|
||||
<Button
|
||||
appearance="primary"
|
||||
className={styles.gridButton}
|
||||
onClick={addGuideline}
|
||||
onClick={addGuideline}
|
||||
icon={<AddRegular />}
|
||||
disabled={isProcessing}
|
||||
title="Add guideline"
|
||||
|
||||
@@ -0,0 +1,361 @@
|
||||
/**
|
||||
* @file RasterManager.tsx
|
||||
* @description Adds and removes a wireframe cell raster on the current slide.
|
||||
*/
|
||||
|
||||
import * as React from "react";
|
||||
import {
|
||||
Button,
|
||||
Dropdown,
|
||||
Field,
|
||||
Input,
|
||||
makeStyles,
|
||||
Option,
|
||||
Text,
|
||||
tokens,
|
||||
} from "@fluentui/react-components";
|
||||
import { DismissRegular, GridDotsRegular } 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 {
|
||||
RASTER_DEFAULT_GUTTER_MM,
|
||||
RASTER_DEFAULT_OPACITY,
|
||||
RASTER_PRESETS,
|
||||
RASTER_SCOPE_LABELS,
|
||||
RasterPresetId,
|
||||
RasterScope,
|
||||
} from "../constants";
|
||||
import {
|
||||
addRasterCells,
|
||||
computeRasterLayout,
|
||||
formatMillimeters,
|
||||
getRasterTarget,
|
||||
removeAllRasters,
|
||||
} from "../services/raster";
|
||||
import { isCancellationRequested, resetCancellation } from "../services/cancellation";
|
||||
import { ColorSwatches, SwatchColor, SWATCH_VALUES } from "./ColorSwatches";
|
||||
import ConfirmButton from "./ConfirmButton";
|
||||
|
||||
const useStyles = makeStyles({
|
||||
root: {
|
||||
display: "flex",
|
||||
flexDirection: "column",
|
||||
gap: tokens.spacingVerticalM,
|
||||
},
|
||||
control: {
|
||||
width: "100%",
|
||||
},
|
||||
twoColumn: {
|
||||
display: "grid",
|
||||
gridTemplateColumns: "repeat(2, minmax(0, 1fr))",
|
||||
gap: tokens.spacingHorizontalS,
|
||||
"@media (max-width: 360px)": {
|
||||
gridTemplateColumns: "1fr",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const parseInteger = (value: string): number => Number.parseInt(value, 10);
|
||||
const parseDecimal = (value: string): number => Number.parseFloat(value);
|
||||
|
||||
/** PowerPoint shape coordinates use points. */
|
||||
const MILLIMETERS_TO_POINTS = 72 / 25.4;
|
||||
|
||||
export const RasterManager: React.FC = () => {
|
||||
const styles = useStyles();
|
||||
const commonStyles = useCommonStyles();
|
||||
const { slideSize } = useSettings();
|
||||
const { setStatusMessage, setStatusType, isProcessing, setIsProcessing, setProgress } =
|
||||
useStatusContext();
|
||||
|
||||
const [preset, setPreset] = React.useState<RasterPresetId>("coarse");
|
||||
const [scope, setScope] = React.useState<RasterScope>("slide");
|
||||
const [columnsInput, setColumnsInput] = React.useState("12");
|
||||
const [rowsInput, setRowsInput] = React.useState("8");
|
||||
const [gutterInput, setGutterInput] = React.useState(String(RASTER_DEFAULT_GUTTER_MM));
|
||||
const [opacityInput, setOpacityInput] = React.useState(String(RASTER_DEFAULT_OPACITY));
|
||||
const [color, setColor] = React.useState<SwatchColor>("gray");
|
||||
|
||||
const layoutResult = React.useMemo(
|
||||
() =>
|
||||
computeRasterLayout({
|
||||
columns: parseInteger(columnsInput),
|
||||
rows: parseInteger(rowsInput),
|
||||
gutter: parseDecimal(gutterInput) * MILLIMETERS_TO_POINTS,
|
||||
slideWidth: slideSize.width,
|
||||
slideHeight: slideSize.height,
|
||||
}),
|
||||
[columnsInput, rowsInput, gutterInput, slideSize.height, slideSize.width]
|
||||
);
|
||||
|
||||
const opacity = parseDecimal(opacityInput);
|
||||
const opacityValid = Number.isFinite(opacity) && opacity >= 0 && opacity <= 100;
|
||||
const layout = layoutResult.ok ? layoutResult.layout : null;
|
||||
const canAdd = layout !== null && opacityValid;
|
||||
|
||||
const presetLabel =
|
||||
preset === "custom"
|
||||
? "Custom"
|
||||
: (RASTER_PRESETS.find((item) => item.id === preset)?.label ?? "Custom");
|
||||
|
||||
const scopeHint =
|
||||
scope === "slide"
|
||||
? "Sits on top of the current slide's content."
|
||||
: scope === "layout"
|
||||
? "Background: appears on every slide using this layout."
|
||||
: "Background: appears on every slide in the presentation.";
|
||||
|
||||
const scopeTarget =
|
||||
scope === "slide"
|
||||
? "the current slide"
|
||||
: scope === "layout"
|
||||
? "this slide's layout"
|
||||
: "the slide master";
|
||||
|
||||
const hint = !layoutResult.ok
|
||||
? layoutResult.error
|
||||
: !opacityValid
|
||||
? "Opacity must be between 0 and 100."
|
||||
: `Cell size: ${formatMillimeters(layoutResult.layout.cellWidth)} × ${formatMillimeters(layoutResult.layout.cellHeight)} mm · ${layoutResult.layout.total} cells`;
|
||||
|
||||
const handlePreset = (nextPreset: RasterPresetId): void => {
|
||||
setPreset(nextPreset);
|
||||
if (nextPreset === "custom") return;
|
||||
|
||||
const selected = RASTER_PRESETS.find((item) => item.id === nextPreset);
|
||||
if (!selected) return;
|
||||
|
||||
setColumnsInput(String(selected.columns));
|
||||
setRowsInput(String(selected.rows));
|
||||
setGutterInput(String(RASTER_DEFAULT_GUTTER_MM));
|
||||
};
|
||||
|
||||
const updateGeometryNumber =
|
||||
(setter: React.Dispatch<React.SetStateAction<string>>) =>
|
||||
(event: React.ChangeEvent<HTMLInputElement>): void => {
|
||||
setter(event.target.value);
|
||||
setPreset("custom");
|
||||
};
|
||||
|
||||
const updateOpacity = (event: React.ChangeEvent<HTMLInputElement>): void => {
|
||||
setOpacityInput(event.target.value);
|
||||
};
|
||||
|
||||
const addRasterToSlide = async (): Promise<void> => {
|
||||
if (!layoutResult.ok) {
|
||||
setStatusMessage(layoutResult.error);
|
||||
setStatusType("warning");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!opacityValid) {
|
||||
setStatusMessage("Opacity must be between 0 and 100.");
|
||||
setStatusType("warning");
|
||||
return;
|
||||
}
|
||||
|
||||
resetCancellation();
|
||||
setProgress(null);
|
||||
setIsProcessing(true);
|
||||
|
||||
try {
|
||||
await PowerPoint.run(async (context) => {
|
||||
const selectedSlides = context.presentation.getSelectedSlides();
|
||||
selectedSlides.load("items");
|
||||
await context.sync();
|
||||
|
||||
if (selectedSlides.items.length === 0) {
|
||||
setStatusMessage("No slide selected.");
|
||||
setStatusType("warning");
|
||||
return;
|
||||
}
|
||||
|
||||
const slide = selectedSlides.items[0];
|
||||
const target = await getRasterTarget(context, slide, scope);
|
||||
|
||||
// Keep one raster in the presentation: clear the current slide, all
|
||||
// layouts, and the slide master before adding the new one.
|
||||
await removeAllRasters(context);
|
||||
|
||||
const result = await addRasterCells(
|
||||
context,
|
||||
target,
|
||||
layoutResult.layout,
|
||||
SWATCH_VALUES[color],
|
||||
opacity,
|
||||
(current, total) => setProgress({ current, total })
|
||||
);
|
||||
|
||||
if (isCancellationRequested() || result.cancelled) {
|
||||
setStatusMessage("Operation cancelled.");
|
||||
setStatusType("warning");
|
||||
return;
|
||||
}
|
||||
|
||||
setStatusMessage(
|
||||
`Added ${layoutResult.layout.columns} × ${layoutResult.layout.rows} raster (${result.created} cells) to ${scopeTarget}.`
|
||||
);
|
||||
setStatusType("success");
|
||||
});
|
||||
} catch (error: unknown) {
|
||||
setStatusMessage(`Error: ${getErrorMessage(error)}`);
|
||||
setStatusType("error");
|
||||
console.error("Add raster error:", error);
|
||||
} finally {
|
||||
setIsProcessing(false);
|
||||
setProgress(null);
|
||||
}
|
||||
};
|
||||
|
||||
const removeRasterFromSlide = async (): Promise<void> => {
|
||||
resetCancellation();
|
||||
setProgress(null);
|
||||
setIsProcessing(true);
|
||||
|
||||
try {
|
||||
await PowerPoint.run(async (context) => {
|
||||
const result = await removeAllRasters(context);
|
||||
|
||||
if (result.removed > 0) {
|
||||
setStatusMessage(`Removed ${result.removed} raster cells from the presentation.`);
|
||||
setStatusType("success");
|
||||
} else {
|
||||
setStatusMessage("No raster found in the presentation.");
|
||||
setStatusType("warning");
|
||||
}
|
||||
});
|
||||
} catch (error: unknown) {
|
||||
setStatusMessage(`Error: ${getErrorMessage(error)}`);
|
||||
setStatusType("error");
|
||||
console.error("Remove raster error:", error);
|
||||
} finally {
|
||||
setIsProcessing(false);
|
||||
setProgress(null);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={styles.root}>
|
||||
<Text size={200} className={commonStyles.hint}>
|
||||
Adds a wireframe cell raster for object placement. Remove it before presenting.
|
||||
</Text>
|
||||
|
||||
<Field label="Place on" hint={scopeHint}>
|
||||
<Dropdown
|
||||
className={styles.control}
|
||||
value={RASTER_SCOPE_LABELS[scope]}
|
||||
selectedOptions={[scope]}
|
||||
onOptionSelect={(_, data) => {
|
||||
if (data.optionValue) {
|
||||
setScope(data.optionValue as RasterScope);
|
||||
}
|
||||
}}
|
||||
>
|
||||
<Option value="slide">{RASTER_SCOPE_LABELS.slide}</Option>
|
||||
<Option value="layout">{RASTER_SCOPE_LABELS.layout}</Option>
|
||||
<Option value="master">{RASTER_SCOPE_LABELS.master}</Option>
|
||||
</Dropdown>
|
||||
</Field>
|
||||
|
||||
<Field label="Preset">
|
||||
<Dropdown
|
||||
className={styles.control}
|
||||
value={presetLabel}
|
||||
selectedOptions={[preset]}
|
||||
onOptionSelect={(_, data) => {
|
||||
if (data.optionValue) {
|
||||
handlePreset(data.optionValue as RasterPresetId);
|
||||
}
|
||||
}}
|
||||
>
|
||||
{RASTER_PRESETS.map((item) => (
|
||||
<Option key={item.id} value={item.id}>
|
||||
{item.label}
|
||||
</Option>
|
||||
))}
|
||||
<Option value="custom">Custom</Option>
|
||||
</Dropdown>
|
||||
</Field>
|
||||
|
||||
<div className={styles.twoColumn}>
|
||||
<Field label="Columns">
|
||||
<Input
|
||||
type="number"
|
||||
min={1}
|
||||
max={48}
|
||||
value={columnsInput}
|
||||
onChange={updateGeometryNumber(setColumnsInput)}
|
||||
title="Number of raster columns"
|
||||
/>
|
||||
</Field>
|
||||
<Field label="Rows">
|
||||
<Input
|
||||
type="number"
|
||||
min={1}
|
||||
max={32}
|
||||
value={rowsInput}
|
||||
onChange={updateGeometryNumber(setRowsInput)}
|
||||
title="Number of raster rows"
|
||||
/>
|
||||
</Field>
|
||||
</div>
|
||||
|
||||
<div className={styles.twoColumn}>
|
||||
<Field label="Gutter (mm)">
|
||||
<Input
|
||||
type="number"
|
||||
min={0}
|
||||
step="0.1"
|
||||
value={gutterInput}
|
||||
onChange={updateGeometryNumber(setGutterInput)}
|
||||
title="Space between raster cells in millimetres"
|
||||
/>
|
||||
</Field>
|
||||
<Field label="Opacity (%)">
|
||||
<Input
|
||||
type="number"
|
||||
min={0}
|
||||
max={100}
|
||||
value={opacityInput}
|
||||
onChange={updateOpacity}
|
||||
title="Raster cell opacity percentage"
|
||||
/>
|
||||
</Field>
|
||||
</div>
|
||||
|
||||
<ColorSwatches label="Color" value={color} onChange={setColor} />
|
||||
|
||||
<Text size={200} className={commonStyles.hint}>
|
||||
{hint}
|
||||
</Text>
|
||||
|
||||
<div className={commonStyles.buttonGroup}>
|
||||
<Button
|
||||
appearance="primary"
|
||||
icon={<GridDotsRegular />}
|
||||
onClick={addRasterToSlide}
|
||||
disabled={isProcessing || !canAdd}
|
||||
title="Add raster"
|
||||
>
|
||||
Add raster
|
||||
</Button>
|
||||
<ConfirmButton
|
||||
appearance="primary"
|
||||
icon={<DismissRegular />}
|
||||
onConfirm={removeRasterFromSlide}
|
||||
disabled={isProcessing}
|
||||
title="Remove raster"
|
||||
confirmTitle="Remove raster?"
|
||||
confirmMessage="This removes the raster from every slide, layout, and slide master in the presentation."
|
||||
confirmLabel="Remove"
|
||||
>
|
||||
Remove raster
|
||||
</ConfirmButton>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default RasterManager;
|
||||
Reference in new issue
Block a user