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:
1 parent
791cbc7b6c
commit
e57258afe9
8 files changed
+616
-711
No files matched your search
@@ -17,6 +17,7 @@ import {
|
||||
import { useStatusContext } from "./App";
|
||||
import { useCommonStyles } from "./commonStyles";
|
||||
import { getErrorMessage } from "../types/office-types";
|
||||
import ConfirmButton from "./ConfirmButton";
|
||||
|
||||
/**
|
||||
* Configuration constants for draft watermarks
|
||||
@@ -183,62 +184,51 @@ export const DraftButtons: React.FC = () => {
|
||||
affectedCount: 0
|
||||
};
|
||||
|
||||
// Process each slide master (usually there's just one)
|
||||
for (let i = 0; i < masters.length; i++) {
|
||||
try {
|
||||
const master = masters[i];
|
||||
|
||||
// Create the textbox on the master slide
|
||||
const textBox = master.shapes.addTextBox("");
|
||||
|
||||
// Set position and size in a single batch
|
||||
textBox.left = DRAFT_CONFIG.TEXT_BOX.LEFT;
|
||||
textBox.top = DRAFT_CONFIG.TEXT_BOX.TOP;
|
||||
textBox.width = DRAFT_CONFIG.TEXT_BOX.WIDTH;
|
||||
textBox.height = DRAFT_CONFIG.TEXT_BOX.HEIGHT;
|
||||
textBox.name = DRAFT_CONFIG.SHAPE_NAME;
|
||||
|
||||
// Load textFrame to set text properties
|
||||
textBox.load("textFrame");
|
||||
await context.sync();
|
||||
|
||||
if (textBox.textFrame) {
|
||||
// Load textRange to set text and properties
|
||||
textBox.textFrame.load("textRange");
|
||||
await context.sync();
|
||||
|
||||
// Set text and load font properties in a single batch
|
||||
textBox.textFrame.textRange.text = DRAFT_CONFIG.TEXT;
|
||||
textBox.textFrame.textRange.load("font,paragraphFormat");
|
||||
await context.sync();
|
||||
|
||||
try {
|
||||
// Apply all font properties in a single batch
|
||||
const font = textBox.textFrame.textRange.font;
|
||||
font.name = DRAFT_CONFIG.FONT.NAME;
|
||||
font.size = DRAFT_CONFIG.FONT.SIZE;
|
||||
font.bold = true;
|
||||
font.color = DRAFT_CONFIG.FONT.COLOR;
|
||||
|
||||
// Set alignment
|
||||
textBox.textFrame.verticalAlignment = "MiddleCentered";
|
||||
|
||||
await context.sync();
|
||||
result.affectedCount++;
|
||||
} catch (fontError) {
|
||||
console.error("Error setting font properties:", fontError);
|
||||
// Continue with default font if custom font fails
|
||||
}
|
||||
}
|
||||
|
||||
result.processedMasters++;
|
||||
} catch (masterError) {
|
||||
console.error(`Error processing master ${i+1}:`, masterError);
|
||||
result.errorMasters++;
|
||||
// Continue to the next master
|
||||
const { LEFT, TOP, WIDTH, HEIGHT } = DRAFT_CONFIG.TEXT_BOX;
|
||||
|
||||
// Load the shapes and names for every master in two batches instead of three syncs per master
|
||||
for (const master of masters) {
|
||||
master.shapes.load("items");
|
||||
}
|
||||
await context.sync();
|
||||
|
||||
const shapesByMaster: PowerPoint.Shape[][] = [];
|
||||
for (const master of masters) {
|
||||
shapesByMaster.push(master.shapes.items);
|
||||
for (const shape of master.shapes.items) {
|
||||
shape.load("name");
|
||||
}
|
||||
}
|
||||
|
||||
await context.sync();
|
||||
|
||||
// Replace any existing watermark so repeated clicks do not duplicate it
|
||||
for (const shapes of shapesByMaster) {
|
||||
for (const shape of shapes) {
|
||||
if (shape.name === DRAFT_CONFIG.SHAPE_NAME) {
|
||||
shape.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Create one watermark per master in a single batch
|
||||
for (const master of masters) {
|
||||
const textBox = master.shapes.addTextBox(DRAFT_CONFIG.TEXT, {
|
||||
left: LEFT,
|
||||
top: TOP,
|
||||
width: WIDTH,
|
||||
height: HEIGHT
|
||||
});
|
||||
textBox.name = DRAFT_CONFIG.SHAPE_NAME;
|
||||
textBox.textFrame.textRange.font.name = DRAFT_CONFIG.FONT.NAME;
|
||||
textBox.textFrame.textRange.font.size = DRAFT_CONFIG.FONT.SIZE;
|
||||
textBox.textFrame.textRange.font.bold = true;
|
||||
textBox.textFrame.textRange.font.color = DRAFT_CONFIG.FONT.COLOR;
|
||||
textBox.textFrame.verticalAlignment = "MiddleCentered";
|
||||
result.processedMasters++;
|
||||
}
|
||||
|
||||
await context.sync();
|
||||
result.affectedCount = masters.length;
|
||||
return result;
|
||||
};
|
||||
|
||||
@@ -287,48 +277,31 @@ export const DraftButtons: React.FC = () => {
|
||||
affectedCount: 0
|
||||
};
|
||||
|
||||
// Process each master slide
|
||||
for (let i = 0; i < masters.length; i++) {
|
||||
try {
|
||||
const master = masters[i];
|
||||
|
||||
// Load all shapes on the master slide
|
||||
master.shapes.load("items");
|
||||
await context.sync();
|
||||
|
||||
// Collect shapes to delete
|
||||
const shapesToDelete: PowerPoint.Shape[] = [];
|
||||
|
||||
// Load all shape names in a single batch
|
||||
for (let j = 0; j < master.shapes.items.length; j++) {
|
||||
const shape = master.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 < master.shapes.items.length; j++) {
|
||||
const shape = master.shapes.items[j];
|
||||
if (shape.name === DRAFT_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.processedMasters++;
|
||||
} catch (masterError) {
|
||||
console.error(`Error processing master slide ${i+1}:`, masterError);
|
||||
result.errorMasters++;
|
||||
// Continue to the next master slide
|
||||
// Load the shapes and names for every master in two batches
|
||||
for (const master of masters) {
|
||||
master.shapes.load("items");
|
||||
}
|
||||
await context.sync();
|
||||
|
||||
const allShapes: PowerPoint.Shape[] = [];
|
||||
for (const master of masters) {
|
||||
for (const shape of master.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 === DRAFT_CONFIG.SHAPE_NAME) {
|
||||
shape.delete();
|
||||
result.affectedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
await context.sync();
|
||||
result.processedMasters = masters.length;
|
||||
return result;
|
||||
};
|
||||
|
||||
@@ -345,16 +318,19 @@ export const DraftButtons: React.FC = () => {
|
||||
>
|
||||
Add
|
||||
</Button>
|
||||
<Button
|
||||
appearance="primary"
|
||||
<ConfirmButton
|
||||
appearance="primary"
|
||||
className={styles.draftButton}
|
||||
onClick={removeDraftWatermark}
|
||||
onConfirm={removeDraftWatermark}
|
||||
icon={<DismissRegular />}
|
||||
disabled={isProcessing}
|
||||
title="Remove Draft"
|
||||
confirmTitle="Remove draft watermarks?"
|
||||
confirmMessage="This removes the DRAFT watermark from every slide master."
|
||||
confirmLabel="Remove"
|
||||
>
|
||||
Remove
|
||||
</Button>
|
||||
</ConfirmButton>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user