Repository navigation
Expand file tree
/
Copy pathfile-picker.ts
More file actions
50 lines (43 loc) · 1.46 KB
/
Copy pathfile-picker.ts
File metadata and controls
50 lines (43 loc) · 1.46 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
/*
Copyright (c) 2025 Xiang Yang.
Published under the standard MIT License.
Full text available at: https://opensource.org/licenses/MIT
*/
import { LocalFileSystem } from "../../kicanvas/services/vfs";
export class FilePicker {
/**
* Open file picker and call callback with the selected files.
*/
static async pick(callback: (vfs: LocalFileSystem) => Promise<void>) {
const files = await FilePicker.open_picker();
if (files.length > 0) {
await callback(new LocalFileSystem(files));
}
}
/**
* Open the file picker
*/
private static open_picker(): Promise<File[]> {
return new Promise((resolve) => {
// because the Window:showOpenFilePicker() method is experimental
// so we use the traditional technique
const input = document.createElement("input");
input.type = "file";
input.style.display = "none";
input.multiple = true;
input.accept = ".kicad_pcb,.kicad_pro,.kicad_sch";
input.onchange = (event) => {
const files = (event.target as HTMLInputElement).files;
if (files && files.length > 0) {
resolve(Array.from(files));
} else {
resolve([]);
}
};
input.oncancel = () => {
resolve([]);
};
input.click();
});
}
}