Files
powerpoint-toolbox/src/taskpane/components/ConfidentialButtons.tsx
T
schihei 89989c04e9
CI / build (push) Canceled after 0s
Modernize platform, structure, and UX
Phase 3:
- Declare PowerPointApi 1.5 and replace template manifest values
- Remove the dead command pipeline and the tracked Babel/polyfill setup
- Extract constants, status/settings contexts, PowerPoint services, and an error boundary
- Build with ES2020 and strict TypeScript; hash production assets and drop prod source maps
- Add Vitest tests, CI, LICENSE, Node 20 pinning, and prune unused dependencies
- Correct the README, user guide, developer guide, and audit notes

Phase 4:
- Add a persisted 16:9/4:3 slide size setting used by all layout tools
- Add dark mode with Office/system theme detection
- Make sections collapsible and move to accessible Fluent controls
- Announce status through aria-live and add semantic status icons
- Add a progress counter with cooperative cancellation and a Stop button
- Update the tracked dependency tree for the pruned packages
2026-11-25 12:01:40 +01:00

298 lines
8.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @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 "../context/StatusContext";
import { useSettings } from "../context/SettingsContext";
import { useCommonStyles } from "./commonStyles";
import { getErrorMessage } from "../types/office-types";
import { MARKER_NAMES } from "../constants";
import { addMarkerToCollections, removeMarkerFromCollections } from "../services/markers";
import { isCancellationRequested, resetCancellation } from "../services/cancellation";
import ConfirmButton from "./ConfirmButton";
/**
* Configuration constants for confidential markings
*/
const CONFIDENTIAL_CONFIG = {
// Marking text that appears on slides
TEXT: "– Confidential –",
// Font configuration
FONT: {
NAME: "Inter",
SIZE: 8,
COLOR: "#DA1335" // RGB(218, 19, 53)
},
// 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 { slideSize } = useSettings();
const {
setStatusMessage,
setStatusType,
isProcessing,
setIsProcessing,
setProgress
} = 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> => {
resetCancellation();
setProgress(null);
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);
if (isCancellationRequested()) {
setStatusMessage("Operation cancelled.");
setStatusType("warning");
return;
}
updateStatusFromResult(result, true);
});
} catch (error: unknown) {
handleError(error, "Add confidential");
} finally {
setIsProcessing(false);
setProgress(null);
}
};
/**
* 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 positionFromTop = slideSize.height - CONFIDENTIAL_CONFIG.TEXT_BOX.POSITION_FROM_BOTTOM;
const result = await addMarkerToCollections(
context,
slides.map((slide) => slide.shapes),
{
name: MARKER_NAMES.confidential,
text: CONFIDENTIAL_CONFIG.TEXT,
font: {
name: CONFIDENTIAL_CONFIG.FONT.NAME,
size: CONFIDENTIAL_CONFIG.FONT.SIZE,
color: CONFIDENTIAL_CONFIG.FONT.COLOR
},
box: {
left: 0,
top: positionFromTop,
width: slideSize.width,
height: CONFIDENTIAL_CONFIG.TEXT_BOX.HEIGHT
},
horizontalAlignment: PowerPoint.ParagraphHorizontalAlignment.center
},
(current, total) => setProgress({ current, total })
);
return {
processedSlides: slides.length,
errorSlides: 0,
affectedCount: result.affected
};
};
/**
* Removes confidential markings from all slides in the presentation
*/
const removeConfidentialMarking = async (): Promise<void> => {
resetCancellation();
setProgress(null);
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);
if (isCancellationRequested()) {
setStatusMessage("Operation cancelled.");
setStatusType("warning");
return;
}
updateStatusFromResult(result, false);
});
} catch (error: unknown) {
handleError(error, "Remove confidential");
} finally {
setIsProcessing(false);
setProgress(null);
}
};
/**
* 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 = await removeMarkerFromCollections(
context,
slides.map((slide) => slide.shapes),
MARKER_NAMES.confidential
);
return {
processedSlides: slides.length,
errorSlides: 0,
affectedCount: result.affected
};
};
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>
<ConfirmButton
appearance="primary"
className={styles.confidentialButton}
onConfirm={removeConfidentialMarking}
icon={<DismissRegular />}
disabled={isProcessing}
title="Remove Confidential"
confirmTitle="Remove confidential markings?"
confirmMessage="This removes the confidential marking from every slide in the presentation."
confirmLabel="Remove"
>
Remove
</ConfirmButton>
</div>
</div>
);
};
export default ConfidentialButtons;