three.cad/src/react/fileHelpers.js

142 lines
3.1 KiB
JavaScript
Raw Normal View History

2021-04-23 13:55:26 +08:00
2021-04-26 14:04:24 +08:00
// import {
// fileOpen,
// fileSave,
2021-04-26 14:51:07 +08:00
// } from '../../extlib/fs/index';
import {
fileOpen,
fileSave,
} from 'browser-fs-access';
2021-04-23 13:55:26 +08:00
2021-04-26 15:25:47 +08:00
// import { sce } from './app'
2021-04-24 04:13:49 +08:00
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-24 04:13:49 +08:00
const result = STLexp.parse(sce.selected[0], { binary: true });
2021-04-19 15:30:29 +08:00
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-26 14:04:24 +08:00
export async function saveFile(fileHandle, file, dispatch, suggestedName) {
2021-04-19 03:14:01 +08:00
try {
if (!fileHandle) {
2021-04-26 14:04:24 +08:00
return await saveFileAs(file, dispatch, suggestedName);
2021-04-19 03:14:01 +08:00
}
2021-04-23 10:31:30 +08:00
2021-04-26 14:04:24 +08:00
const blob = new Blob([file], { type: 'application/json' })
await fileSave(blob, 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);
}
};
2021-04-26 14:04:24 +08:00
const options = {
mimeTypes: ['application/json'],
extensions: ['.json'],
multiple: false,
description: 'Part files',
};
export async function saveFileAs(file, dispatch, suggestedName) {
2021-04-19 03:14:01 +08:00
try {
2021-04-26 14:04:24 +08:00
const blob = new Blob([file], { type: 'application/json' })
options.fileName = suggestedName + options.extensions[0]
const fileHandle = await fileSave(blob, options)
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;
}
};
2021-04-19 03:14:01 +08:00
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
2021-04-26 14:04:24 +08:00
2021-04-24 07:59:03 +08:00
file = await fileOpen(options);
2021-04-23 13:55:26 +08:00
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-26 14:04:24 +08:00
const text = await file.text();
console.log(file, file.handle)
2021-04-19 11:53:02 +08:00
2021-04-26 14:04:24 +08:00
dispatch({ type: 'restore-state', state: sce.loadState(text), fileName: file.name })
if (file.handle) {
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;
}