Batch slide operations, make adds idempotent, and add confirmations

- Replace per-slide sync loops in confidential, draft, progress bar, and grid operations
- Use addTextBox options and batch loads; remove the nested PowerPoint.run in grid creation
- Replace O(n^2) progress segments with two rectangles per slide and apply to all slides
- Rewrite Insert Titles with batched loads and a pure title ranking function
- Delete existing marks before adding so repeated clicks replace instead of duplicate
- Report a warning when there is nothing to remove
- Add ConfirmButton and confirmation support to ActionButton
This commit is contained in:
schihei committed 2026-11-25 11:51:04 +01:00
1 parent 791cbc7b6c
commit e57258afe9
8 files changed
+616 -711

No files matched your search

+72 -96
View File
@@ -16,6 +16,7 @@ import {
import { useStatusContext } from "./App";
import { useCommonStyles } from "./commonStyles";
import { getErrorMessage } from "../types/office-types";
import ConfirmButton from "./ConfirmButton";
/**
* Configuration constants for confidential markings
@@ -172,61 +173,50 @@ export const ConfidentialButtons: React.FC = () => {
};
const { WIDTH, HEIGHT } = CONFIDENTIAL_CONFIG.SLIDE;
const slideWidth = WIDTH;
const slideHeight = HEIGHT;
const positionFromTop = HEIGHT - CONFIDENTIAL_CONFIG.TEXT_BOX.POSITION_FROM_BOTTOM;
// 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
// Load the shapes and names for every slide in two batches instead of two syncs per slide
for (const slide of slides) {
slide.shapes.load("items");
}
await context.sync();
const shapesBySlide: PowerPoint.Shape[][] = [];
for (const slide of slides) {
shapesBySlide.push(slide.shapes.items);
for (const shape of slide.shapes.items) {
shape.load("name");
}
}
await context.sync();
// Replace any existing marking so repeated clicks do not duplicate it
for (const shapes of shapesBySlide) {
for (const shape of shapes) {
if (shape.name === CONFIDENTIAL_CONFIG.SHAPE_NAME) {
shape.delete();
}
}
}
// Create one marking per slide in a single batch
for (const slide of slides) {
const textBox = slide.shapes.addTextBox(CONFIDENTIAL_CONFIG.TEXT, {
left: 0,
top: positionFromTop,
width: WIDTH,
height: CONFIDENTIAL_CONFIG.TEXT_BOX.HEIGHT
});
textBox.name = CONFIDENTIAL_CONFIG.SHAPE_NAME;
textBox.textFrame.textRange.font.name = CONFIDENTIAL_CONFIG.FONT.NAME;
textBox.textFrame.textRange.font.size = CONFIDENTIAL_CONFIG.FONT.SIZE;
textBox.textFrame.textRange.font.color = CONFIDENTIAL_CONFIG.FONT.COLOR;
textBox.textFrame.textRange.paragraphFormat.horizontalAlignment = "Center";
result.processedSlides++;
}
await context.sync();
result.affectedCount = slides.length;
return result;
};
@@ -275,48 +265,31 @@ export const ConfidentialButtons: React.FC = () => {
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
// Load the shapes and names for every slide in two batches
for (const slide of slides) {
slide.shapes.load("items");
}
await context.sync();
const allShapes: PowerPoint.Shape[] = [];
for (const slide of slides) {
for (const shape of slide.shapes.items) {
shape.load("name");
allShapes.push(shape);
}
}
await context.sync();
// Delete all matching shapes in one batch
for (const shape of allShapes) {
if (shape.name === CONFIDENTIAL_CONFIG.SHAPE_NAME) {
shape.delete();
result.affectedCount++;
}
}
await context.sync();
result.processedSlides = slides.length;
return result;
};
@@ -333,16 +306,19 @@ export const ConfidentialButtons: React.FC = () => {
>
Add
</Button>
<Button
appearance="primary"
<ConfirmButton
appearance="primary"
className={styles.confidentialButton}
onClick={removeConfidentialMarking}
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
</Button>
</ConfirmButton>
</div>
</div>
);