refactor: 重构编辑器右键菜单动作分发
使用 switch 按动作类型分发右键菜单操作 在各动作分支内校验目标类型和媒体类型 移除结果状态更新中的嵌套三元表达式
This commit is contained in:
@@ -293,38 +293,52 @@ export function useRightClickMenu({
|
||||
}
|
||||
: current,
|
||||
);
|
||||
const succeeded =
|
||||
action === EditorAgentRightClickAction.CopyText &&
|
||||
target.kind === 'text'
|
||||
? await copyTextToClipboard(target.text)
|
||||
: action === EditorAgentRightClickAction.CopyImage &&
|
||||
target.kind === 'asset' &&
|
||||
target.asset.mediaType === 'image'
|
||||
? await copyAssetImage(target.asset)
|
||||
: action === EditorAgentRightClickAction.ReferenceImage &&
|
||||
target.kind === 'asset' &&
|
||||
target.asset.mediaType === 'image' &&
|
||||
contextAssetMediaSrc(target.asset).trim()
|
||||
? (onReferenceImage?.(target.asset) ?? false)
|
||||
: action === EditorAgentRightClickAction.FocusCanvas &&
|
||||
target.kind === 'asset'
|
||||
? focusAssetOnCanvas(target.asset, onFocusResource)
|
||||
: action === EditorAgentRightClickAction.DownloadAsset &&
|
||||
target.kind === 'asset'
|
||||
? await downloadAsset(target.asset)
|
||||
: false;
|
||||
setRightClickMenu((current) =>
|
||||
current?.target === target && current.pendingAction === action
|
||||
? succeeded
|
||||
? null
|
||||
: {
|
||||
...current,
|
||||
pendingAction: null,
|
||||
resultAction: action,
|
||||
result: 'error',
|
||||
}
|
||||
: current,
|
||||
);
|
||||
let succeeded = false;
|
||||
switch (action) {
|
||||
case EditorAgentRightClickAction.CopyText:
|
||||
if (target.kind === 'text') {
|
||||
succeeded = await copyTextToClipboard(target.text);
|
||||
}
|
||||
break;
|
||||
case EditorAgentRightClickAction.CopyImage:
|
||||
if (target.kind === 'asset' && target.asset.mediaType === 'image') {
|
||||
succeeded = await copyAssetImage(target.asset);
|
||||
}
|
||||
break;
|
||||
case EditorAgentRightClickAction.ReferenceImage:
|
||||
if (
|
||||
target.kind === 'asset' &&
|
||||
target.asset.mediaType === 'image' &&
|
||||
contextAssetMediaSrc(target.asset).trim()
|
||||
) {
|
||||
succeeded = onReferenceImage?.(target.asset) ?? false;
|
||||
}
|
||||
break;
|
||||
case EditorAgentRightClickAction.FocusCanvas:
|
||||
if (target.kind === 'asset') {
|
||||
succeeded = focusAssetOnCanvas(target.asset, onFocusResource);
|
||||
}
|
||||
break;
|
||||
case EditorAgentRightClickAction.DownloadAsset:
|
||||
if (target.kind === 'asset') {
|
||||
succeeded = await downloadAsset(target.asset);
|
||||
}
|
||||
break;
|
||||
}
|
||||
setRightClickMenu((current) => {
|
||||
if (current?.target !== target || current.pendingAction !== action) {
|
||||
return current;
|
||||
}
|
||||
if (succeeded) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
...current,
|
||||
pendingAction: null,
|
||||
resultAction: action,
|
||||
result: 'error',
|
||||
};
|
||||
});
|
||||
},
|
||||
[onFocusResource, onReferenceImage, rightClickMenu],
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user