-
Notifications
You must be signed in to change notification settings - Fork 0
/
sand-download.js
179 lines (136 loc) · 4.46 KB
/
sand-download.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//LOAD AND SAVE START
function loadfile(input){
var reader = new FileReader();
reader.onload = function(e) {
if ( editor.somethingSelected() ) {
editor.replaceSelection( e.target.result );
return;
}
editor.replaceRange(e.target.result, editor.getCursor());};
reader.readAsText(input.files[0]);
}
let nameMe = document.getElementById("nameMe");
let extendMe = document.getElementById("extendMe");
function saveTextAsFile()
{
var textToWrite = editor.getValue();
var textToWrite = textToWrite.replace(/\n/g, "\r\n");
var textFileAsBlob = new Blob([textToWrite], {type:'text/plain'});
var fileNameToSaveAs = `${nameMe.value}.${extendMe.value}`;
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = nameMe;//<--custom change, find out meaning
window.URL = window.URL || window.webkitURL;
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
}
function destroyClickedElement(event)
{
document.body.removeChild(event.target);
}
//LOAD AND SAVE END
/*
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 = "downloads"//filePath[filePath.length - 2].toLowerCase()//startIn option will not work with Uppercase letters
let fileLocation = filePath[filePath.length - 1]
/**/
function init(){
async function openFile(){
const options = {
startIn: fileDirectory, //how is this captured, custom address, plit location.pathname
};
// Destructure the one-element array.
[fileHandle] = await window.showOpenFilePicker(options); //options
// Do something with the file handle.
const file = await fileHandle.getFile();
const contents = await file.text();
editor.setValue(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.getValue())
}
function saveAs(){
async function getNewFileHandle() {
const options = {
startIn: fileDirectory, //how is this captured, custom address, plit location.pathname
suggestedName: "PPPP", // 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.getValue());
// Close the file and write the contents to disk.
await writable.close();
}
getNewFileHandle()
}
window.addEventListener('keydown', (e) => {
// console.log('key', e.code, e.ctrlKey, e.metaKey, e.shiftKey, e.key);
// Save As
if ((e.ctrlKey || e.metaKey) && e.shiftKey && e.code === 'KeyS') {
e.preventDefault();
saveAs();
return;
}
// Save
if ((e.ctrlKey === true || e.metaKey === true) && e.key === 's') {
e.preventDefault();
save();
return;
}
// Open
if ((e.ctrlKey === true || e.metaKey === true) && e.key === 'o') {
e.preventDefault();
init();
return;
}
/**/
})