Skip to content

Commit 21db02a

Browse files
committed
feat: files selector
1 parent b4904ac commit 21db02a

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

electron/mapi/file/main.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
11
import {dialog, ipcMain, shell} from "electron";
22
import fileIndex from "./index";
33

4-
ipcMain.handle("file:openFile", async (_, options): Promise<string | null> => {
4+
ipcMain.handle("file:openFile", async (
5+
event,
6+
options: {
7+
filters?: {
8+
name: string;
9+
extensions: string[];
10+
}[],
11+
properties?: ("multiSelections" | "openFile")[]
12+
} = {}): Promise<string | string[] | null> => {
13+
options = Object.assign({
14+
filters: [],
15+
properties: [],
16+
}, options);
17+
if (!options.properties.includes("openFile")) {
18+
options.properties.push("openFile");
19+
}
520
const res = await dialog
621
.showOpenDialog({
7-
properties: ["openFile"],
822
...options,
923
})
1024
.catch(e => {
1125
});
1226
if (!res || res.canceled) {
1327
return null;
1428
}
29+
if (res.filePaths.length > 1) {
30+
return res.filePaths;
31+
}
1532
return res.filePaths?.[0] || null;
1633
});
1734

src/components/common/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export const doOpenFile = async (
4848
}]
4949
}
5050
if (options.multiple) {
51-
opt.properties = ['openFile', 'multiSelections'];
51+
opt.properties = ['multiSelections'];
5252
}
5353
const result = await window.$mapi.file.openFile(opt);
5454
if (result) {

src/declarations/type.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,13 @@ type DefsMapi = {
205205
progress?: (percent: number, total: number) => void;
206206
}
207207
) => Promise<string>;
208-
openFile: (options: {} = {}) => Promise<string | null>;
208+
openFile: (options: {
209+
filters?: {
210+
name: string;
211+
extensions: string[];
212+
}[],
213+
properties?: ("multiSelections")[]
214+
} = {}) => Promise<string | null>;
209215
openDirectory: (options: {} = {}) => Promise<string | null>;
210216
openSave: (options: {} = {}) => Promise<string | null>;
211217
ext: (path: string) => Promise<string>;

0 commit comments

Comments
 (0)