three.cad/src/react/fileHelpers.js

134 lines
2.9 KiB
JavaScript
Raw Normal View History

2021-04-23 13:55:26 +08:00
// import {
// fileOpen,
// fileSave,
// } from '../../extlib/fs/index';
import {
fileOpen,
fileSave,
} from 'browser-fs-access';
2021-04-20 09:31:32 +08:00
// https://web.dev/file-system-access/
2021-04-19 03:14:01 +08:00
const link = document.createElement('a');
2021-04-17 21:32:14 +08:00
link.style.display = 'none';
2021-04-19 03:14:01 +08:00
document.body.appendChild(link);
2021-04-17 21:32:14 +08:00
2021-04-19 06:05:33 +08:00
function saveLegacy(blob, filename) {
2021-04-17 21:32:14 +08:00
link.href = URL.createObjectURL(blob);
link.download = filename;
link.click();
}
2021-04-19 06:05:33 +08:00
var tzoffset = (new Date()).getTimezoneOffset() * 60000;
export function STLExport(filename) {
2021-04-19 15:30:29 +08:00
const result = STLexp.parse(sc.selected[0], { binary: true });
const time = (new Date(Date.now() - tzoffset)).toISOString().slice(0, -5).replace(/:/g, '-');
saveLegacy(new Blob([result], { type: 'model/stl' }), `${filename}_${time}.stl`);
2021-04-17 21:32:14 +08:00
}
2021-04-19 03:14:01 +08:00
export async function saveFile(fileHandle, file, dispatch) {
try {
if (!fileHandle) {
return await saveFileAs(file, dispatch);
}
2021-04-23 10:31:30 +08:00
2021-04-23 13:55:26 +08:00
await fileSave(new Blob([file], { type: 'application/json' }), undefined, fileHandle, true)
2021-04-19 03:14:01 +08:00
dispatch({ type: 'set-modified', status: false })
} catch (ex) {
const msg = 'Unable to save file';
console.error(msg, ex);
alert(msg);
}
};
export async function saveFileAs(file, dispatch) {
try {
2021-04-23 13:55:26 +08:00
const fileHandle = await fileSave(new Blob([file], { type: 'application/json' }), {
2021-04-23 14:05:24 +08:00
fileName: 'unamed.json',
2021-04-23 13:55:26 +08:00
extensions: ['.json'],
})
2021-04-19 03:14:01 +08:00
dispatch({ type: 'set-file-handle', fileHandle, modified: false })
} catch (ex) {
const msg = 'Unable to save file.';
console.error(msg, ex);
alert(msg);
return;
}
};
export async function openFile(dispatch) {
2021-04-23 13:55:26 +08:00
let file
2021-04-19 03:14:01 +08:00
try {
2021-04-23 13:55:26 +08:00
const options = {
mimeTypes: ['application/json'],
extensions: ['.json'],
multiple: false,
description: 'Part files',
};
file = await fileOpen(options);
2021-04-19 03:14:01 +08:00
} catch (ex) {
if (ex.name === 'AbortError') {
return;
}
const msg = 'An error occured trying to open the file.';
console.error(msg, ex);
alert(msg);
}
try {
2021-04-19 06:05:33 +08:00
const text = await file.text();;
2021-04-19 11:53:02 +08:00
dispatch({ type: 'restore-state', state: sc.loadState(text) })
2021-04-23 13:55:26 +08:00
dispatch({ type: 'set-file-handle', fileHandle:file.handle })
2021-04-19 11:53:02 +08:00
2021-04-19 03:14:01 +08:00
} catch (ex) {
const msg = `An error occured reading ${fileHandle}`;
console.error(msg, ex);
alert(msg);
}
2021-04-18 05:08:14 +08:00
2021-04-17 21:32:14 +08:00
2021-04-19 03:14:01 +08:00
};
2021-04-19 11:53:02 +08:00
export function confirmDiscard(modified) {
if (!modified) {
return true;
}
const confirmMsg = 'Discard changes? All changes will be lost.';
return confirm(confirmMsg);
};
export async function verifyPermission(fileHandle) {
const opts = {
2021-04-19 15:30:29 +08:00
mode: 'readwrite'
2021-04-19 11:53:02 +08:00
};
// Check if we already have permission, if so, return true.
if (await fileHandle.queryPermission(opts) === 'granted') {
return true;
}
// Request permission to the file, if the user grants permission, return true.
if (await fileHandle.requestPermission(opts) === 'granted') {
return true;
}
// The user did nt grant permission, return false.
return false;
}