保存对话框的默认文件名收进下载助手,复用既有取文件名工具
- xhsMinitoolDownload 自己从 relativePath 取最后一段当默认文件名(取不到才用调用方给的兜底名),参数名随之改为 fallbackFileName;/ 与 \ 两种分隔符统一交给既有的 resourceFileNameFromPath,不再在面板里写第二份取文件名实现。 - ArtifactsPane 只传兜底名,不再自己拼默认文件名,视图层也就不必跨到 features/resource-canvas 去 import。
This commit is contained in:
+7
-3
@@ -7,14 +7,15 @@
|
|||||||
import { save as saveNativeFileDialog } from '@tauri-apps/plugin-dialog';
|
import { save as saveNativeFileDialog } from '@tauri-apps/plugin-dialog';
|
||||||
|
|
||||||
import { resolveTauriInvoke } from '../../../../app/tauri';
|
import { resolveTauriInvoke } from '../../../../app/tauri';
|
||||||
|
import { resourceFileNameFromPath } from '../../../../features/resource-canvas/resourceCanvasAssetTransferModel';
|
||||||
|
|
||||||
/** 返回给用户看的一句话;用户取消对话框时返回空串(不打扰)。 */
|
/** 返回给用户看的一句话;用户取消对话框时返回空串(不打扰)。 */
|
||||||
export async function saveXhsMinitoolFileToDisk(input: {
|
export async function saveXhsMinitoolFileToDisk(input: {
|
||||||
projectPath: string;
|
projectPath: string;
|
||||||
/** 项目内相对路径,交给宿主的既有门禁复核。 */
|
/** 项目内相对路径,交给宿主的既有门禁复核。 */
|
||||||
relativePath: string;
|
relativePath: string;
|
||||||
/** 对话框里的默认文件名。 */
|
/** 路径里取不出文件名时用的兜底名。 */
|
||||||
defaultFileName: string;
|
fallbackFileName: string;
|
||||||
title: string;
|
title: string;
|
||||||
}): Promise<string> {
|
}): Promise<string> {
|
||||||
const invoke = resolveTauriInvoke();
|
const invoke = resolveTauriInvoke();
|
||||||
@@ -22,7 +23,10 @@ export async function saveXhsMinitoolFileToDisk(input: {
|
|||||||
let destination: string | null = null;
|
let destination: string | null = null;
|
||||||
try {
|
try {
|
||||||
destination = await saveNativeFileDialog({
|
destination = await saveNativeFileDialog({
|
||||||
defaultPath: input.defaultFileName,
|
// 默认文件名取路径最后一段(`/` 与 `\` 都认),取不到就用调用方给的兜底名。
|
||||||
|
defaultPath:
|
||||||
|
resourceFileNameFromPath(input.relativePath.trim()) ||
|
||||||
|
input.fallbackFileName,
|
||||||
title: input.title,
|
title: input.title,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
+2
-10
@@ -34,7 +34,7 @@ export function ArtifactsPane({
|
|||||||
void saveXhsMinitoolFileToDisk({
|
void saveXhsMinitoolFileToDisk({
|
||||||
projectPath,
|
projectPath,
|
||||||
relativePath: form.iconPath.trim(),
|
relativePath: form.iconPath.trim(),
|
||||||
defaultFileName: fileNameOf(form.iconPath, 'icon.png'),
|
fallbackFileName: 'icon.png',
|
||||||
title: '保存 icon 文件',
|
title: '保存 icon 文件',
|
||||||
}).then(setDownloadNotice);
|
}).then(setDownloadNotice);
|
||||||
};
|
};
|
||||||
@@ -44,10 +44,7 @@ export function ArtifactsPane({
|
|||||||
void saveXhsMinitoolFileToDisk({
|
void saveXhsMinitoolFileToDisk({
|
||||||
projectPath,
|
projectPath,
|
||||||
relativePath: XHS_MINITOOL_ARTIFACT_RELATIVE_PATH,
|
relativePath: XHS_MINITOOL_ARTIFACT_RELATIVE_PATH,
|
||||||
defaultFileName: fileNameOf(
|
fallbackFileName: 'xhs-minitool.zip',
|
||||||
XHS_MINITOOL_ARTIFACT_RELATIVE_PATH,
|
|
||||||
'xhs-minitool.zip',
|
|
||||||
),
|
|
||||||
title: '保存代码 zip',
|
title: '保存代码 zip',
|
||||||
}).then(setDownloadNotice);
|
}).then(setDownloadNotice);
|
||||||
};
|
};
|
||||||
@@ -162,8 +159,3 @@ function saveHint(minitool: XhsMinitoolExportState): string {
|
|||||||
if (minitool.dirty) return '改动会在停手后自动保存';
|
if (minitool.dirty) return '改动会在停手后自动保存';
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
function fileNameOf(relativePath: string, fallback: string): string {
|
|
||||||
const name = relativePath.trim().split('/').pop();
|
|
||||||
return name ? name : fallback;
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user