-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-system-api.js
98 lines (77 loc) · 2.5 KB
/
file-system-api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
FILE SYSTEM ACCESS API
Interfaces
FileSystemHandle
FileSystemFileHandle
FileSystemDirectoryHandle
FileSystemWritableFileStream
Methods
window.showOpenFilePicker()
window.showSaveFilePicker()
window.showDirectoryPicker()
DataTransferItem.getAsFileSystemHandle()
Don't use "open()" as a function, already in use
https://developer.mozilla.org/en-US/docs/Web/API/Window/open
(other MIME) accept: 'text/plain': ['.txt'],
const options = {
startIn: 'pictures', //how is this captured, custom address
suggestedName: 'Untitled Text.txt',
types: [
{
description: 'Images',
accept: {
'image/*': ['.png', '.gif', '.jpeg', '.jpg']
}
},
],
excludeAcceptAllOption: true,
multiple: false
};
const options = {
startIn: 'desktop', //how is this captured, custom address
suggestedName: 'untitled.txt', // fileHandle.name
};
*/
let fileHandle;
let filePath = location.pathname.split("/")
let fileDirectory = "desktop"//filePath[filePath.length - 2].toLowerCase()//startIn option will not work with Uppercase letters
let fileLocation = filePath[filePath.length - 1]
//fix this for codemirror
function init(){
async function openFile(){
// Destructure the one-element array.
[fileHandle] = await window.showOpenFilePicker(); //options
// Do something with the file handle.
const file = await fileHandle.getFile();
const contents = await file.text();
//editor.value = contents;
};
openFile()
}
function save(){
async function writeFile(fileHandle, contents) {
// Create a FileSystemWritableFileStream to write to.
const writable = await fileHandle.createWritable();
// Write the contents of the file to the stream.
await writable.write(contents);
// Close the file and write the contents to disk.
await writable.close();
}
writeFile(fileHandle, editor.state.doc.toString())
}
function saveAs(){
async function getNewFileHandle() {
const options = {
startIn: fileDirectory, //how is this captured, custom address, plit location.pathname
suggestedName: fileLocation, // split location.pathname
};
fileHandle = await window.showSaveFilePicker(options);
// Create a FileSystemWritableFileStream to write to.
const writable = await fileHandle.createWritable();
// Write the contents of the file to the stream.
await writable.write(editor.state.doc.toString());
// Close the file and write the contents to disk.
await writable.close();
}
getNewFileHandle()
}