three.cad/src/react/fileHelpers.js

130 lines
2.8 KiB
JavaScript
Raw Normal View History

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;
2021-04-17 21:32:14 +08:00
2021-04-19 06:05:33 +08:00
export function STLExport(filename) {
2021-04-17 21:32:14 +08:00
if (sc.selected[0] && sc.selected[0].userData.type == 'mesh') {
2021-04-19 06:05:33 +08:00
2021-04-19 03:14:01 +08:00
const result = STLexp.parse(sc.selected[0], { binary: true });
2021-04-19 06:05:33 +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-19 03:14:01 +08:00
export async function saveFile(fileHandle, file, dispatch) {
try {
if (!fileHandle) {
return await saveFileAs(file, dispatch);
}
await writeFile(fileHandle, file);
dispatch({ type: 'set-modified', status: false })
} catch (ex) {
const msg = 'Unable to save file';
console.error(msg, ex);
alert(msg);
}
// app.setFocus();
};
export async function saveFileAs(file, dispatch) {
let fileHandle;
try {
const opts = {
types: [{
// description: 'Text file',
accept: { 'application/json': ['.json'] },
}],
};
fileHandle = await showSaveFilePicker(opts)
} catch (ex) {
if (ex.name === 'AbortError') {
return;
}
const msg = 'An error occured trying to open the file.';
console.error(msg, ex);
alert(msg);
return;
}
try {
const writable = await fileHandle.createWritable();
// Write the contents of the file to the stream.
await writable.write(file);
// Close the file and write the contents to disk.
await writable.close()
dispatch({ type: 'set-file-handle', fileHandle, modified: false })
} catch (ex) {
const msg = 'Unable to save file.';
console.error(msg, ex);
alert(msg);
return;
}
// app.setFocus();
};
export async function openFile(dispatch) {
// if (!app.confirmDiscard()) {
// return;
// }
let fileHandle
// If a fileHandle is provided, verify we have permission to read/write it,
// otherwise, show the file open prompt and allow the user to select the file.
try {
fileHandle = await getFileHandle();
} catch (ex) {
if (ex.name === 'AbortError') {
return;
}
const msg = 'An error occured trying to open the file.';
console.error(msg, ex);
alert(msg);
}
if (!fileHandle) {
return;
}
try {
2021-04-19 06:05:33 +08:00
const file = await fileHandle.getFile();
const text = await file.text();;
2021-04-19 03:14:01 +08:00
sc.loadState(text)
2021-04-19 06:05:33 +08:00
2021-04-19 03:14:01 +08:00
dispatch({ type: 'set-file-handle', fileHandle })
// app.setModified(false);
// app.setFocus(true);
} 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
};