优化保存方式
This commit is contained in:
parent
4370189e81
commit
5985971041
3
TODO.md
3
TODO.md
@ -17,6 +17,5 @@
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
- [ ] 使用Reducer 和 Immer重构代码
|
- [ ] 整理、重构代码
|
||||||
- [ ] 添加测试用例
|
- [ ] 添加测试用例
|
||||||
- [ ] 体验调优:生成配置完成后弹出预览窗口可以下载或者复制
|
|
||||||
|
|||||||
48
src/App.tsx
48
src/App.tsx
@ -168,12 +168,48 @@ function FlowContent(): ReactNode {
|
|||||||
|
|
||||||
const json = instanceToPlain(saveConfig);
|
const json = instanceToPlain(saveConfig);
|
||||||
const blob = new Blob([JSON.stringify(json, null, 2)], { type: 'application/json' });
|
const blob = new Blob([JSON.stringify(json, null, 2)], { type: 'application/json' });
|
||||||
const url = URL.createObjectURL(blob);
|
const lastNameKey = 'wg-last-filename';
|
||||||
const a = document.createElement('a');
|
const suggestedName = (localStorage.getItem(lastNameKey) || 'wg-config.json');
|
||||||
a.href = url;
|
|
||||||
a.download = 'wg-config.json';
|
// Use File System Access API when available to show save file picker
|
||||||
a.click();
|
if ((window as any).showSaveFilePicker) {
|
||||||
URL.revokeObjectURL(url);
|
try {
|
||||||
|
const handle = await (window as any).showSaveFilePicker({
|
||||||
|
suggestedName: suggestedName,
|
||||||
|
types: [{
|
||||||
|
description: 'WireGuard Config',
|
||||||
|
accept: { 'application/json': ['.json'] }
|
||||||
|
}]
|
||||||
|
});
|
||||||
|
const writable = await handle.createWritable();
|
||||||
|
await writable.write(blob);
|
||||||
|
await writable.close();
|
||||||
|
// remember chosen name if available
|
||||||
|
try {
|
||||||
|
const name = handle.name || suggestedName;
|
||||||
|
localStorage.setItem(lastNameKey, name);
|
||||||
|
} catch (e) {
|
||||||
|
localStorage.setItem(lastNameKey, suggestedName);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// If user cancels or error, fallback to anchor download
|
||||||
|
const url = URL.createObjectURL(blob);
|
||||||
|
const a = document.createElement('a');
|
||||||
|
a.href = url;
|
||||||
|
a.download = suggestedName;
|
||||||
|
a.click();
|
||||||
|
URL.revokeObjectURL(url);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Fallback: create an anchor and trigger download, using last used filename
|
||||||
|
const url = URL.createObjectURL(blob);
|
||||||
|
const a = document.createElement('a');
|
||||||
|
a.href = url;
|
||||||
|
a.download = suggestedName;
|
||||||
|
a.click();
|
||||||
|
URL.revokeObjectURL(url);
|
||||||
|
localStorage.setItem(lastNameKey, suggestedName);
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
toast.error('保存失败: ' + e);
|
toast.error('保存失败: ' + e);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user