From 4e68e61b674359995b74924d9fbb2727ee4b97ea Mon Sep 17 00:00:00 2001 From: howard Date: Fri, 23 Apr 2021 00:01:07 -0700 Subject: [PATCH] test --- extlib/fs/fs-access/file-save.mjs | 1 + src/react/fileHelpers.js | 65 +++++++++++++++++++++++++------ 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/extlib/fs/fs-access/file-save.mjs b/extlib/fs/fs-access/file-save.mjs index 16b8b2c..7083cc2 100644 --- a/extlib/fs/fs-access/file-save.mjs +++ b/extlib/fs/fs-access/file-save.mjs @@ -26,6 +26,7 @@ export default async ( throwIfExistingHandleNotGood = false ) => { options.fileName = options.fileName || 'Untitled'; + console.log(options.fileName,"ddddddddddd") const accept = {}; if (options.mimeTypes) { options.mimeTypes.push(blob.type); diff --git a/src/react/fileHelpers.js b/src/react/fileHelpers.js index 0651e44..64a3ce6 100644 --- a/src/react/fileHelpers.js +++ b/src/react/fileHelpers.js @@ -1,14 +1,14 @@ -// import { -// fileOpen, -// fileSave, -// } from '../../extlib/fs/index'; - import { fileOpen, fileSave, -} from 'browser-fs-access'; +} from '../../extlib/fs/index'; + +// import { +// fileOpen, +// fileSave, +// } from 'browser-fs-access'; // https://web.dev/file-system-access/ @@ -46,14 +46,56 @@ export async function saveFile(fileHandle, file, dispatch) { } }; -export async function saveFileAs(file, dispatch) { +// export async function saveFileAs(file, dispatch) { +// try { +// console.log('heeeeeeeeeeeeeer') +// const fileHandle = await fileSave(new Blob([file], { type: 'application/json' }), { +// fileName: 'unamed', +// extensions: ['.json'], +// }) + +// 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 saveFileAs(file, dispatch) { + let fileHandle; + try { + + const opts = { + suggestedName: 'test', + types: [{ + description: 'Text file', + accept: { 'application/json': ['.json'] }, + }], + }; + fileHandle = await showSaveFilePicker(opts) + + + } catch (ex) { + if (ex.name === 'AbortError') { + console.log('aborted') + return; + } + const msg = 'An error occured trying to open the file.'; + console.error(msg, ex); + alert(msg); + return; + } + try { - const fileHandle = await fileSave(new Blob([file], { type: 'application/json' }), { - // fileName: 'unamed.json', - extensions: ['.json'], - }) + const writable = await fileHandle.createWritable(); + await writable.write(file); + await writable.close() dispatch({ type: 'set-file-handle', fileHandle, modified: false }) @@ -67,7 +109,6 @@ export async function saveFileAs(file, dispatch) { }; - export async function openFile(dispatch) { let file