Undo
photopea_undoReverse one or more recent editing steps in the active document. Use after applying filters, adjustments, or fills to revert changes.
Instructions
Undo one or more recent actions in the active document. Each step reverses one operation from the history. Use after destructive operations (apply_filter, apply_adjustment, fill_selection) to revert changes.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| steps | No | Number of history steps to undo (default 1) |
Implementation Reference
- src/tools/export.ts:108-114 (handler)The async handler for photopea_undo tool. It calls buildUndo(params.steps) to generate the script, sends activity, executes the script via the bridge, and returns the result.
}, async (params) => { const script = buildUndo(params.steps); bridge.sendActivity({ type: "activity", id: "", tool: "undo", summary: `Undo ${params.steps} step(s)` }); const result = await bridge.executeScript(script); if (!result.success) return { isError: true, content: [{ type: "text" as const, text: result.error || "Failed to undo" }] }; return { content: [{ type: "text" as const, text: `Undid ${params.steps} step(s)` }] }; }); - src/tools/export.ts:104-106 (schema)Input schema for photopea_undo: a single 'steps' parameter (z.number().int().positive().default(1)) describing how many history steps to undo.
inputSchema: { steps: z.number().int().positive().default(1).describe("Number of history steps to undo (default 1)"), }, - src/tools/export.ts:101-114 (registration)Registration of photopea_undo tool via server.registerTool with title 'Undo' and description explaining its purpose.
server.registerTool("photopea_undo", { title: "Undo", description: "Undo one or more recent actions in the active document. Each step reverses one operation from the history. Use after destructive operations (apply_filter, apply_adjustment, fill_selection) to revert changes.", inputSchema: { steps: z.number().int().positive().default(1).describe("Number of history steps to undo (default 1)"), }, annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: false, openWorldHint: false }, }, async (params) => { const script = buildUndo(params.steps); bridge.sendActivity({ type: "activity", id: "", tool: "undo", summary: `Undo ${params.steps} step(s)` }); const result = await bridge.executeScript(script); if (!result.success) return { isError: true, content: [{ type: "text" as const, text: result.error || "Failed to undo" }] }; return { content: [{ type: "text" as const, text: `Undid ${params.steps} step(s)` }] }; }); - src/bridge/script-builder.ts:827-834 (helper)The buildUndo function generates a Photoshop/Photopea script that accesses history states and sets the active history state to undo the specified number of steps.
export function buildUndo(steps: number = 1): string { const lines: string[] = []; lines.push(`var _hs = app.activeDocument.historyStates;`); lines.push(`var _target = Math.max(0, _hs.length - 1 - ${steps});`); lines.push(`app.activeDocument.activeHistoryState = _hs[_target];`); lines.push(`app.echoToOE('ok');`); return lines.join("\n"); }