mirror of
https://github.com/wanderer-industries/wanderer
synced 2025-12-13 03:06:15 +00:00
fix(Map): Unified settings. Second part: Import/Export
This commit is contained in:
36
assets/js/hooks/Mapper/utils/saveToFile.ts
Normal file
36
assets/js/hooks/Mapper/utils/saveToFile.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
export function saveTextFile(filename: string, content: string) {
|
||||
const blob = new Blob([content], { type: 'text/plain;charset=utf-8' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
|
||||
// эмулируем клик
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
|
||||
// освобождаем URL
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
export async function saveTextFileInteractive(filename: string, content: string) {
|
||||
if (!('showSaveFilePicker' in window)) {
|
||||
throw new Error('File System Access API is not supported in this browser.');
|
||||
}
|
||||
|
||||
const handle = await (window as any).showSaveFilePicker({
|
||||
suggestedName: filename,
|
||||
types: [
|
||||
{
|
||||
description: 'Text Files',
|
||||
accept: { 'text/plain': ['.txt', '.json'] },
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const writable = await handle.createWritable();
|
||||
await writable.write(content);
|
||||
await writable.close();
|
||||
}
|
||||
Reference in New Issue
Block a user