- 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
352 lines
10 KiB
TypeScript
352 lines
10 KiB
TypeScript
/**
|
||
* @file ConfidentialButtons.tsx
|
||
* @description Component that provides functionality to add and remove confidential markings
|
||
* to PowerPoint slides. The markings appear as text at the bottom of each slide.
|
||
*/
|
||
|
||
import * as React from "react";
|
||
import {
|
||
Button,
|
||
makeStyles
|
||
} from "@fluentui/react-components";
|
||
import {
|
||
ShieldLockRegular,
|
||
DismissRegular
|
||
} from "@fluentui/react-icons";
|
||
import { useStatusContext } from "./App";
|
||
import { useCommonStyles } from "./commonStyles";
|
||
import { getErrorMessage } from "../types/office-types";
|
||
|
||
/**
|
||
* Configuration constants for confidential markings
|
||
*/
|
||
const CONFIDENTIAL_CONFIG = {
|
||
// Marking text that appears on slides
|
||
TEXT: "– Confidential –",
|
||
// Shape name used to identify confidential markings
|
||
SHAPE_NAME: "ConfidentialMarking",
|
||
// Font configuration
|
||
FONT: {
|
||
NAME: "Inter",
|
||
SIZE: 8,
|
||
COLOR: "#DA1335" // RGB(218, 19, 53)
|
||
},
|
||
// Default slide dimensions in points (if we can't get actual dimensions)
|
||
SLIDE: {
|
||
WIDTH: 960, // 10 inches * 72 points
|
||
HEIGHT: 540, // 7.5 inches * 72 points
|
||
},
|
||
// Text box configuration
|
||
TEXT_BOX: {
|
||
HEIGHT: 25,
|
||
POSITION_FROM_BOTTOM: 23
|
||
}
|
||
};
|
||
|
||
/**
|
||
* Component-specific styles
|
||
*/
|
||
const useStyles = makeStyles({
|
||
buttonGrid: {
|
||
display: "grid",
|
||
gridTemplateColumns: "repeat(2, 1fr)",
|
||
gap: "8px",
|
||
width: "100%"
|
||
},
|
||
confidentialButton: {
|
||
width: "100%",
|
||
minWidth: 0,
|
||
padding: "8px 4px",
|
||
"& span": {
|
||
justifyContent: "center"
|
||
}
|
||
}
|
||
});
|
||
|
||
/**
|
||
* Interface for slide processing results
|
||
*/
|
||
interface ProcessingResult {
|
||
processedSlides: number;
|
||
errorSlides: number;
|
||
affectedCount: number; // Either added or removed markings
|
||
}
|
||
|
||
/**
|
||
* ConfidentialButtons component provides UI and functionality to add or remove
|
||
* confidential markings on PowerPoint slides.
|
||
*
|
||
* @returns React component
|
||
*/
|
||
export const ConfidentialButtons: React.FC = () => {
|
||
const styles = useStyles();
|
||
const commonStyles = useCommonStyles();
|
||
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);
|
||
};
|
||
|
||
/**
|
||
* Updates the status message based on processing results
|
||
*
|
||
* @param result - The processing result object
|
||
* @param isAddOperation - Whether this was an add operation (true) or remove operation (false)
|
||
*/
|
||
const updateStatusFromResult = (result: ProcessingResult, isAddOperation: boolean): void => {
|
||
const { errorSlides, affectedCount } = result;
|
||
|
||
if (affectedCount > 0) {
|
||
const action = isAddOperation ? "Added" : "Removed";
|
||
setStatusMessage(`${action} confidential marking to ${affectedCount} slides.`);
|
||
setStatusType("success");
|
||
} else if (errorSlides > 0) {
|
||
setStatusMessage(`Failed to ${isAddOperation ? "add" : "remove"} markings. Errors on ${errorSlides} slides.`);
|
||
setStatusType("error");
|
||
} else {
|
||
if (isAddOperation) {
|
||
setStatusMessage("No slides found to add confidential marking.");
|
||
} else {
|
||
setStatusMessage("No confidential markings found to remove.");
|
||
}
|
||
setStatusType(isAddOperation ? "warning" : "info");
|
||
}
|
||
};
|
||
|
||
/**
|
||
* Adds confidential markings to all slides in the presentation
|
||
*/
|
||
const addConfidentialMarking = async (): Promise<void> => {
|
||
setIsProcessing(true);
|
||
try {
|
||
await PowerPoint.run(async (context) => {
|
||
// Get all slides in the presentation
|
||
const slides = context.presentation.slides;
|
||
slides.load("items");
|
||
await context.sync();
|
||
|
||
if (slides.items.length === 0) {
|
||
setStatusMessage("No slides found in the presentation.");
|
||
setStatusType("warning");
|
||
return;
|
||
}
|
||
|
||
const result = await processAddMarkings(context, slides.items);
|
||
updateStatusFromResult(result, true);
|
||
});
|
||
} catch (error) {
|
||
handleError(error, "Add confidential");
|
||
} finally {
|
||
setIsProcessing(false);
|
||
}
|
||
};
|
||
|
||
/**
|
||
* Processes all slides to add confidential markings
|
||
*
|
||
* @param context - The PowerPoint API context
|
||
* @param slides - Array of slides to process
|
||
* @returns Processing result with counts of processed and error slides
|
||
*/
|
||
const processAddMarkings = async (
|
||
context: PowerPoint.RequestContext,
|
||
slides: PowerPoint.Slide[]
|
||
): Promise<ProcessingResult> => {
|
||
const result: ProcessingResult = {
|
||
processedSlides: 0,
|
||
errorSlides: 0,
|
||
affectedCount: 0
|
||
};
|
||
|
||
const { WIDTH, HEIGHT } = CONFIDENTIAL_CONFIG.SLIDE;
|
||
const slideWidth = WIDTH;
|
||
const slideHeight = HEIGHT;
|
||
|
||
// Process each slide
|
||
for (let i = 0; i < slides.length; i++) {
|
||
try {
|
||
const slide = slides[i];
|
||
const positionFromTop = slideHeight - CONFIDENTIAL_CONFIG.TEXT_BOX.POSITION_FROM_BOTTOM;
|
||
|
||
// Create the textbox spanning the full width of the slide
|
||
const textBox = slide.shapes.addTextBox("");
|
||
textBox.left = 0; // Start from left edge
|
||
textBox.top = positionFromTop;
|
||
textBox.width = slideWidth;
|
||
textBox.height = CONFIDENTIAL_CONFIG.TEXT_BOX.HEIGHT;
|
||
textBox.name = CONFIDENTIAL_CONFIG.SHAPE_NAME;
|
||
|
||
// Load textFrame and its properties in a single batch
|
||
textBox.load("textFrame");
|
||
await context.sync();
|
||
|
||
if (textBox.textFrame) {
|
||
// Load textRange and its properties
|
||
textBox.textFrame.load("textRange");
|
||
await context.sync();
|
||
|
||
// Set text and formatting in a single batch
|
||
textBox.textFrame.textRange.text = CONFIDENTIAL_CONFIG.TEXT;
|
||
textBox.textFrame.textRange.load("font,paragraphFormat");
|
||
await context.sync();
|
||
|
||
try {
|
||
// Apply font properties in a single batch
|
||
const font = textBox.textFrame.textRange.font;
|
||
font.name = CONFIDENTIAL_CONFIG.FONT.NAME;
|
||
font.size = CONFIDENTIAL_CONFIG.FONT.SIZE;
|
||
font.color = CONFIDENTIAL_CONFIG.FONT.COLOR;
|
||
textBox.textFrame.textRange.paragraphFormat.horizontalAlignment = "Center";
|
||
|
||
await context.sync();
|
||
result.affectedCount++;
|
||
} catch (fontError) {
|
||
console.error("Error setting font properties:", fontError);
|
||
// Continue with default font if custom font fails
|
||
}
|
||
}
|
||
|
||
result.processedSlides++;
|
||
} catch (slideError) {
|
||
console.error(`Error processing slide ${i+1}:`, slideError);
|
||
result.errorSlides++;
|
||
// Continue to the next slide
|
||
}
|
||
}
|
||
|
||
return result;
|
||
};
|
||
|
||
/**
|
||
* Removes confidential markings from all slides in the presentation
|
||
*/
|
||
const removeConfidentialMarking = async (): Promise<void> => {
|
||
setIsProcessing(true);
|
||
try {
|
||
await PowerPoint.run(async (context) => {
|
||
// Get all slides in the presentation
|
||
const slides = context.presentation.slides;
|
||
slides.load("items");
|
||
await context.sync();
|
||
|
||
if (slides.items.length === 0) {
|
||
setStatusMessage("No slides found in the presentation.");
|
||
setStatusType("warning");
|
||
return;
|
||
}
|
||
|
||
const result = await processRemoveMarkings(context, slides.items);
|
||
updateStatusFromResult(result, false);
|
||
});
|
||
} catch (error) {
|
||
handleError(error, "Remove confidential");
|
||
} finally {
|
||
setIsProcessing(false);
|
||
}
|
||
};
|
||
|
||
/**
|
||
* Processes all slides to remove confidential markings
|
||
*
|
||
* @param context - The PowerPoint API context
|
||
* @param slides - Array of slides to process
|
||
* @returns Processing result with counts of processed and affected slides
|
||
*/
|
||
const processRemoveMarkings = async (
|
||
context: PowerPoint.RequestContext,
|
||
slides: PowerPoint.Slide[]
|
||
): Promise<ProcessingResult> => {
|
||
const result: ProcessingResult = {
|
||
processedSlides: 0,
|
||
errorSlides: 0,
|
||
affectedCount: 0
|
||
};
|
||
|
||
// Process each slide
|
||
for (let i = 0; i < slides.length; i++) {
|
||
try {
|
||
const slide = slides[i];
|
||
|
||
// Load all shapes on the slide
|
||
slide.shapes.load("items");
|
||
await context.sync();
|
||
|
||
// Collect shapes to delete
|
||
const shapesToDelete: PowerPoint.Shape[] = [];
|
||
|
||
// Find shapes with the confidential marking name
|
||
for (let j = 0; j < slide.shapes.items.length; j++) {
|
||
const shape = slide.shapes.items[j];
|
||
shape.load("name");
|
||
}
|
||
|
||
// Wait for all shape names to load
|
||
await context.sync();
|
||
|
||
// Now check names and collect shapes to delete
|
||
for (let j = 0; j < slide.shapes.items.length; j++) {
|
||
const shape = slide.shapes.items[j];
|
||
if (shape.name === CONFIDENTIAL_CONFIG.SHAPE_NAME) {
|
||
shapesToDelete.push(shape);
|
||
}
|
||
}
|
||
|
||
// Delete all matching shapes in one batch
|
||
shapesToDelete.forEach(shape => shape.delete());
|
||
result.affectedCount += shapesToDelete.length;
|
||
|
||
await context.sync();
|
||
result.processedSlides++;
|
||
} catch (slideError) {
|
||
console.error(`Error processing slide ${i+1}:`, slideError);
|
||
result.errorSlides++;
|
||
// Continue to the next slide
|
||
}
|
||
}
|
||
|
||
return result;
|
||
};
|
||
|
||
return (
|
||
<div className={commonStyles.container}>
|
||
<div className={styles.buttonGrid}>
|
||
<Button
|
||
appearance="primary"
|
||
className={styles.confidentialButton}
|
||
onClick={addConfidentialMarking}
|
||
icon={<ShieldLockRegular />}
|
||
disabled={isProcessing}
|
||
title="Add Confidential"
|
||
>
|
||
Add
|
||
</Button>
|
||
<Button
|
||
appearance="primary"
|
||
className={styles.confidentialButton}
|
||
onClick={removeConfidentialMarking}
|
||
icon={<DismissRegular />}
|
||
disabled={isProcessing}
|
||
title="Remove Confidential"
|
||
>
|
||
Remove
|
||
</Button>
|
||
</div>
|
||
</div>
|
||
);
|
||
};
|
||
|
||
export default ConfidentialButtons;
|