Repository navigation
Expand file tree
/
Copy pathio.ts
More file actions
62 lines (49 loc) · 1.42 KB
/
Copy pathio.ts
File metadata and controls
62 lines (49 loc) · 1.42 KB
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
import fileSaver = require('file-saver');
function getIo() {
return getWebIo();
}
function getWebIo() {
function openFile() {
return new Promise<string>((resolve, reject) => {
const fileInput = document.createElement("input");
fileInput.type = "file";
fileInput.accept = ".xml";
fileInput.addEventListener("change", readSingleFile, false);
fileInput.click();
function readSingleFile(e: Event) {
const file = (e.target as any).files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (e) => {
const contents = (e.target as any).result;
resolve(contents);
};
reader.onerror = (err) => {
reject(new Error(err.message));
};
reader.readAsText(file);
}
});
}
/**
* Save a file to the user's computer
* @param text Data to save
* @param ext Suggested file name extension, i.e. 'xml'
*/
function saveFile(data: string | Uint8Array, ext: string, type: string) {
let fileName = "main";
let disableAutoBOM = true;
if (!fileName) return Promise.resolve(void 0);
if (ext && fileName.slice(-4) !== `.${ext}`) fileName = `${fileName}.${ext}`;
const blob = new Blob([data], { type });
fileSaver.saveAs(blob, fileName, disableAutoBOM);
return Promise.resolve(void 0);
}
return {
openFile,
saveFile,
};
}
export {
getIo,
}