Add raster tool with slide, layout, and master scopes
CI / build (push) Canceled after 0s

- 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:
schihei committed 2026-11-25 13:03:43 +01:00
1 parent e223dc61c8
commit 9889ce1025
10 files changed
+1106 -206

No files matched your search

+275
View File
@@ -0,0 +1,275 @@
/* global PowerPoint */
/**
* @file raster.ts
* @description Wireframe cell-grid ("raster") geometry and PowerPoint operations.
*
* A raster is a columns × rows grid of rectangles that fills the current slide.
* It is used as a temporary placement aid, similar to grids and guidelines.
*/
import { RASTER_MAX_CELLS, SHAPE_NAME_PREFIXES, RasterScope } from "../constants";
import { deleteShapesByPrefix } from "../types/office-types";
import { isCancellationRequested } from "./cancellation";
export const RASTER_PREFIX = SHAPE_NAME_PREFIXES.raster;
/** Smallest allowed cell edge, in points. */
const MIN_CELL_SIZE = 1;
export interface RasterLayoutOptions {
columns: number;
rows: number;
/** Space between cells, in points. */
gutter: number;
/** Slide width in points. */
slideWidth: number;
/** Slide height in points. */
slideHeight: number;
}
export interface RasterCell {
column: number;
row: number;
left: number;
top: number;
width: number;
height: number;
}
export interface RasterLayout {
columns: number;
rows: number;
cellWidth: number;
cellHeight: number;
total: number;
cells: RasterCell[];
}
export type RasterLayoutResult = { ok: true; layout: RasterLayout } | { ok: false; error: string };
/**
* Converts points to millimetres for display.
*/
export const formatMillimeters = (points: number): string => ((points * 25.4) / 72).toFixed(2);
/**
* Computes the rectangles for a raster that fills the slide.
*
* Cells are evenly sized; the leftover space is distributed into the gutters.
*/
export function computeRasterLayout(options: RasterLayoutOptions): RasterLayoutResult {
const { columns, rows, gutter, slideWidth, slideHeight } = options;
if (!Number.isInteger(columns) || columns < 1) {
return { ok: false, error: "Columns must be at least 1." };
}
if (!Number.isInteger(rows) || rows < 1) {
return { ok: false, error: "Rows must be at least 1." };
}
if (!Number.isFinite(gutter) || gutter < 0) {
return { ok: false, error: "Gutter must be 0 or greater." };
}
if (
!Number.isFinite(slideWidth) ||
!Number.isFinite(slideHeight) ||
slideWidth <= 0 ||
slideHeight <= 0
) {
return { ok: false, error: "Slide size is not available." };
}
const total = columns * rows;
if (total > RASTER_MAX_CELLS) {
return {
ok: false,
error: `A ${columns} × ${rows} raster creates ${total} cells. The maximum is ${RASTER_MAX_CELLS}; reduce columns or rows.`,
};
}
const cellWidth = (slideWidth - (columns - 1) * gutter) / columns;
const cellHeight = (slideHeight - (rows - 1) * gutter) / rows;
if (cellWidth < MIN_CELL_SIZE || cellHeight < MIN_CELL_SIZE) {
return {
ok: false,
error: "The gutter is too large for this number of columns or rows.",
};
}
const cells: RasterCell[] = [];
for (let row = 0; row < rows; row++) {
for (let column = 0; column < columns; column++) {
cells.push({
column,
row,
left: column * (cellWidth + gutter),
top: row * (cellHeight + gutter),
width: cellWidth,
height: cellHeight,
});
}
}
return {
ok: true,
layout: { columns, rows, cellWidth, cellHeight, total, cells },
};
}
export interface AddRasterResult {
created: number;
cancelled: boolean;
}
/**
* Adds the raster cells to any shape collection (slide, layout, or master).
* Existing raster shapes should be removed by the caller before this runs.
*/
export async function addRasterCells(
context: PowerPoint.RequestContext,
shapes: PowerPoint.ShapeCollection,
layout: RasterLayout,
color: string,
opacity: number,
onProgress: (current: number, total: number) => void
): Promise<AddRasterResult> {
const clampedOpacity = Math.min(100, Math.max(0, opacity));
const transparency = (100 - clampedOpacity) / 100;
const created: PowerPoint.Shape[] = [];
let cancelled = false;
try {
for (const cell of layout.cells) {
if (isCancellationRequested()) {
cancelled = true;
break;
}
const shape = shapes.addGeometricShape("Rectangle", {
left: cell.left,
top: cell.top,
width: cell.width,
height: cell.height,
});
shape.name = `${RASTER_PREFIX}c${cell.column}_r${cell.row}`;
shape.fill.setSolidColor(color);
shape.fill.transparency = transparency;
shape.lineFormat.visible = false;
created.push(shape);
onProgress(created.length, layout.total);
}
} catch (error: unknown) {
// Do not leave a partial raster behind if the host rejects a shape.
for (const shape of created) {
shape.delete();
}
await context.sync();
throw error;
}
if (cancelled) {
// Leave no partial raster behind.
for (const shape of created) {
shape.delete();
}
await context.sync();
return { created: 0, cancelled: true };
}
await context.sync();
return { created: created.length, cancelled: false };
}
/**
* Resolves the shape collection for the requested raster scope.
*/
export async function getRasterTarget(
context: PowerPoint.RequestContext,
slide: PowerPoint.Slide,
scope: RasterScope
): Promise<PowerPoint.ShapeCollection> {
if (scope === "layout") {
slide.load("layout");
await context.sync();
return slide.layout.shapes;
}
if (scope === "master") {
slide.load("slideMaster");
await context.sync();
return slide.slideMaster.shapes;
}
return slide.shapes;
}
/**
* Loads every shape collection that can contain a raster: all slides, all
* layouts, and all slide masters. Names are loaded so callers can filter.
*/
async function loadRasterCollections(
context: PowerPoint.RequestContext
): Promise<PowerPoint.ShapeCollection[]> {
const slides = context.presentation.slides;
const masters = context.presentation.slideMasters;
slides.load("items");
masters.load("items");
await context.sync();
const collections: PowerPoint.ShapeCollection[] = [];
for (const slide of slides.items) {
collections.push(slide.shapes);
}
for (const master of masters.items) {
master.layouts.load("items");
collections.push(master.shapes);
}
await context.sync();
for (const master of masters.items) {
for (const layout of master.layouts.items) {
collections.push(layout.shapes);
}
}
for (const collection of collections) {
collection.load("items");
}
await context.sync();
for (const collection of collections) {
for (const shape of collection.items) {
shape.load("name");
}
}
await context.sync();
return collections;
}
/**
* Removes raster cells from every slide, layout, and slide master.
*
* The raster tool keeps one raster in the presentation, so cleanup is global
* regardless of the selected scope.
*/
export async function removeAllRasters(
context: PowerPoint.RequestContext
): Promise<{ removed: number }> {
const collections = await loadRasterCollections(context);
let removed = 0;
for (const collection of collections) {
removed += deleteShapesByPrefix(collection.items, RASTER_PREFIX);
}
await context.sync();
return { removed };
}