-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
dialog.js
56 lines (54 loc) · 1.9 KB
/
dialog.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
var defaultPathInput = document.getElementById("dialog-default-path");
var filterInput = document.getElementById("dialog-filter");
var multipleInput = document.getElementById("dialog-multiple");
var directoryInput = document.getElementById("dialog-directory");
document.getElementById("open-dialog").addEventListener("click", function () {
window.__TAURI__.dialog
.open({
defaultPath: defaultPathInput.value || null,
filters: filterInput.value ? [{
name: 'Tauri Example',
extensions: filterInput.value.split(',').map(f => f.trim())
}] : [],
multiple: multipleInput.checked,
directory: directoryInput.checked,
})
.then(function (res) {
if (Array.isArray(res)) {
registerResponse(res);
} else {
var pathToRead = res;
var isFile = pathToRead.match(/\S+\.\S+$/g);
window.__TAURI__.fs
.readBinaryFile(pathToRead)
.then(function (response) {
if (isFile) {
if (pathToRead.includes(".png") || pathToRead.includes(".jpg")) {
arrayBufferToBase64(new Uint8Array(response), function (base64) {
var src = "data:image/png;base64," + base64;
registerResponse('<img src="' + src + '"></img>');
});
} else {
registerResponse(res);
}
} else {
registerResponse(res);
}
})
.catch(registerResponse(res));
}
})
.catch(registerResponse);
});
document.getElementById("save-dialog").addEventListener("click", function () {
window.__TAURI__.dialog
.save({
defaultPath: defaultPathInput.value || null,
filters: filterInput.value ? [{
name: 'Tauri Example',
extensions: filterInput.value.split(',').map(f => f.trim())
}] : [],
})
.then(registerResponse)
.catch(registerResponse);
});