fix: stop psd replacements inheriting source masks
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled

This commit is contained in:
Your Name 2026-04-03 11:36:07 +08:00
parent 1963c84d7c
commit bcb0006a9e
3 changed files with 8 additions and 38 deletions

View File

@ -2,7 +2,7 @@ import type { AgentPsdTextColorConfig } from "@/types/agent";
import type { AgentPsdTemplateSecondaryCategoryConfig } from "@/lib/agents/psd-template-secondary-category-config";
export const AGENT_PSD_TEMPLATES_KEY = "agent_psd_templates";
export const AGENT_PSD_TEMPLATE_ASSET_VERSION = 5;
export const AGENT_PSD_TEMPLATE_ASSET_VERSION = 6;
export type AgentPsdTemplateStatus = "active" | "disabled";
export type AgentPsdTemplateParseStatus =

View File

@ -664,8 +664,7 @@ export async function preparePsdTemplateRenderAssets(params: {
if (
!isAgentPsdImageLayerSlot(targetLayer) ||
!targetLayer.isEditable ||
(!params.forceRefreshLayerAssets && targetLayer.maskUrl)
!targetLayer.isEditable
) {
const layerIndex = nextLayers.findIndex((layer) => layer.id === targetLayer.id);
if (layerIndex >= 0 && isAgentPsdImageLayerSlot(nextLayers[layerIndex])) {
@ -677,45 +676,19 @@ export async function preparePsdTemplateRenderAssets(params: {
continue;
}
const pixelCount = entry.layer.width * entry.layer.height;
const maskRgba = new Uint8ClampedArray(pixelCount * 4);
let hasVisiblePixel = false;
for (let index = 0; index < pixelCount; index += 1) {
const alpha = rgba[index * 4 + 3] || 0;
maskRgba[index * 4] = 255;
maskRgba[index * 4 + 1] = 255;
maskRgba[index * 4 + 2] = 255;
maskRgba[index * 4 + 3] = alpha;
if (alpha > 0) {
hasVisiblePixel = true;
}
}
if (!hasVisiblePixel) {
continue;
}
const maskBuffer = await rgbaToPngBuffer(maskRgba, entry.layer.width, entry.layer.height);
const uploaded = await uploadFile(
`agent-psd-mask-${params.templateId}-${sanitizeFileSegment(targetLayer.layerId, targetLayer.id)}.png`,
maskBuffer.buffer.slice(
maskBuffer.byteOffset,
maskBuffer.byteOffset + maskBuffer.byteLength
) as ArrayBuffer
);
const layerIndex = nextLayers.findIndex((layer) => layer.id === targetLayer.id);
if (layerIndex >= 0 && isAgentPsdImageLayerSlot(nextLayers[layerIndex])) {
nextLayers[layerIndex] = {
...nextLayers[layerIndex],
contentBounds: visibleContentBounds,
maskUrl: uploaded.url,
maskPath: uploaded.pathname,
// Do not auto-reuse the original PSD image alpha as a hard replacement mask.
// Otherwise newly assigned images get trapped inside the source image silhouette.
maskUrl: undefined,
maskPath: undefined,
};
}
} catch (error) {
console.warn("[PSD Template] Failed to generate layer mask:", {
console.warn("[PSD Template] Failed to prepare editable image layer assets:", {
layerId: targetLayer.layerId,
error,
});

View File

@ -22,10 +22,7 @@ function hasMissingLayerAssets(template: AgentPsdTemplate) {
return template.layers.some(
(layer) =>
!String(layer.previewUrl || "").trim() ||
!String(layer.sourceUrl || "").trim() ||
(isAgentPsdImageLayerSlot(layer) &&
layer.isEditable &&
!String(layer.maskUrl || "").trim())
!String(layer.sourceUrl || "").trim()
);
}