Duplicate Layer
photopea_duplicate_layerDuplicate a layer in an active Photopea document. Specify the layer by name or index, and optionally assign a new display name to the copy. The duplicate becomes the active layer, placed above the original.
Instructions
Create a copy of a layer in the active document. The duplicate becomes the active layer and is placed above the original. Use newName to distinguish the copy from the original.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| target | Yes | Layer name (string) or index (number) | |
| newName | No | Display name for the duplicated layer (defaults to 'original name copy') |
Implementation Reference
- src/tools/layer.ts:130-145 (registration)Registration of photopea_duplicate_layer tool with input schema (target, newName) and handler that calls buildDuplicateLayer
// 12. photopea_duplicate_layer server.registerTool("photopea_duplicate_layer", { title: "Duplicate Layer", description: "Create a copy of a layer in the active document. The duplicate becomes the active layer and is placed above the original. Use newName to distinguish the copy from the original.", inputSchema: { target: layerTarget, newName: z.string().optional().describe("Display name for the duplicated layer (defaults to 'original name copy')"), }, annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: false }, }, async (params) => { const script = buildDuplicateLayer(params); bridge.sendActivity({ type: "activity", id: "", tool: "duplicate_layer", summary: `Duplicate layer: ${params.target}` }); const result = await bridge.executeScript(script); if (!result.success) return { isError: true, content: [{ type: "text" as const, text: result.error || "Failed to duplicate layer" }] }; return { content: [{ type: "text" as const, text: `Layer duplicated: ${params.target}` }] }; }); - src/tools/layer.ts:139-145 (handler)Handler function for photopea_duplicate_layer: calls buildDuplicateLayer(params), sends activity, and executes script via bridge
}, async (params) => { const script = buildDuplicateLayer(params); bridge.sendActivity({ type: "activity", id: "", tool: "duplicate_layer", summary: `Duplicate layer: ${params.target}` }); const result = await bridge.executeScript(script); if (!result.success) return { isError: true, content: [{ type: "text" as const, text: result.error || "Failed to duplicate layer" }] }; return { content: [{ type: "text" as const, text: `Layer duplicated: ${params.target}` }] }; }); - src/bridge/script-builder.ts:303-314 (helper)buildDuplicateLayer helper function that generates Photopea JS script to duplicate a layer and optionally rename it
export function buildDuplicateLayer(params: DuplicateLayerParams): string { const { target, newName } = params; const ref = layerRef(target); const lines: string[] = []; lines.push(`var _layer = ${ref};`); lines.push(`var _copy = _layer.duplicate();`); if (newName !== undefined) { lines.push(`_copy.name = '${escapeString(newName)}';`); } lines.push(`app.echoToOE('ok');`); return lines.join("\n"); } - src/bridge/types.ts:136-139 (schema)DuplicateLayerParams interface: target (string|number) and optional newName (string)
export interface DuplicateLayerParams { target: string | number; newName?: string; }