-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
dialog.ts
335 lines (319 loc) · 9.13 KB
/
dialog.ts
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright 2019-2022 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
/**
* Native system dialogs for opening and saving files.
*
* This package is also accessible with `window.__TAURI__.dialog` when [`build.withGlobalTauri`](https://tauri.app/v1/api/config/#buildconfig.withglobaltauri) in `tauri.conf.json` is set to `true`.
*
* The APIs must be added to [`tauri.allowlist.dialog`](https://tauri.app/v1/api/config/#allowlistconfig.dialog) in `tauri.conf.json`:
* ```json
* {
* "tauri": {
* "allowlist": {
* "dialog": {
* "all": true, // enable all dialog APIs
* "ask": true, // enable dialog ask API
* "confirm": true, // enable dialog confirm API
* "message": true, // enable dialog message API
* "open": true, // enable file open API
* "save": true // enable file save API
* }
* }
* }
* }
* ```
* It is recommended to allowlist only the APIs you use for optimal bundle size and security.
* @module
*/
import { invokeTauriCommand } from './helpers/tauri'
/**
* Extension filters for the file dialog.
*
* @since 1.0.0
*/
interface DialogFilter {
/** Filter name. */
name: string
/**
* Extensions to filter, without a `.` prefix.
* @example
* ```typescript
* extensions: ['svg', 'png']
* ```
*/
extensions: string[]
}
/**
* Options for the open dialog.
*
* @since 1.0.0
*/
interface OpenDialogOptions {
/** The title of the dialog window. */
title?: string
/** The filters of the dialog. */
filters?: DialogFilter[]
/** Initial directory or file path. */
defaultPath?: string
/** Whether the dialog allows multiple selection or not. */
multiple?: boolean
/** Whether the dialog is a directory selection or not. */
directory?: boolean
/**
* If `directory` is true, indicates that it will be read recursively later.
* Defines whether subdirectories will be allowed on the scope or not.
*/
recursive?: boolean
}
/**
* Options for the save dialog.
*
* @since 1.0.0
*/
interface SaveDialogOptions {
/** The title of the dialog window. */
title?: string
/** The filters of the dialog. */
filters?: DialogFilter[]
/**
* Initial directory or file path.
* If it's a directory path, the dialog interface will change to that folder.
* If it's not an existing directory, the file name will be set to the dialog's file name input and the dialog will be set to the parent folder.
*/
defaultPath?: string
}
/**
* @since 1.0.0
*/
interface MessageDialogOptions {
/** The title of the dialog. Defaults to the app name. */
title?: string
/** The type of the dialog. Defaults to `info`. */
type?: 'info' | 'warning' | 'error'
/** The label of the confirm button. */
okLabel?: string
}
interface ConfirmDialogOptions {
/** The title of the dialog. Defaults to the app name. */
title?: string
/** The type of the dialog. Defaults to `info`. */
type?: 'info' | 'warning' | 'error'
/** The label of the confirm button. */
okLabel?: string
/** The label of the cancel button. */
cancelLabel?: string
}
/**
* Open a file/directory selection dialog.
*
* The selected paths are added to the filesystem and asset protocol allowlist scopes.
* When security is more important than the easy of use of this API,
* prefer writing a dedicated command instead.
*
* Note that the allowlist scope change is not persisted, so the values are cleared when the application is restarted.
* You can save it to the filesystem using [tauri-plugin-persisted-scope](https://github.com/tauri-apps/tauri-plugin-persisted-scope).
* @example
* ```typescript
* import { open } from '@tauri-apps/api/dialog';
* // Open a selection dialog for image files
* const selected = await open({
* multiple: true,
* filters: [{
* name: 'Image',
* extensions: ['png', 'jpeg']
* }]
* });
* if (Array.isArray(selected)) {
* // user selected multiple files
* } else if (selected === null) {
* // user cancelled the selection
* } else {
* // user selected a single file
* }
* ```
*
* @example
* ```typescript
* import { open } from '@tauri-apps/api/dialog';
* import { appDir } from '@tauri-apps/api/path';
* // Open a selection dialog for directories
* const selected = await open({
* directory: true,
* multiple: true,
* defaultPath: await appDir(),
* });
* if (Array.isArray(selected)) {
* // user selected multiple directories
* } else if (selected === null) {
* // user cancelled the selection
* } else {
* // user selected a single directory
* }
* ```
*
* @returns A promise resolving to the selected path(s)
*
* @since 1.0.0
*/
async function open(
options: OpenDialogOptions = {}
): Promise<null | string | string[]> {
if (typeof options === 'object') {
Object.freeze(options)
}
return invokeTauriCommand({
__tauriModule: 'Dialog',
message: {
cmd: 'openDialog',
options
}
})
}
/**
* Open a file/directory save dialog.
*
* The selected path is added to the filesystem and asset protocol allowlist scopes.
* When security is more important than the easy of use of this API,
* prefer writing a dedicated command instead.
*
* Note that the allowlist scope change is not persisted, so the values are cleared when the application is restarted.
* You can save it to the filesystem using [tauri-plugin-persisted-scope](https://github.com/tauri-apps/tauri-plugin-persisted-scope).
* @example
* ```typescript
* import { save } from '@tauri-apps/api/dialog';
* const filePath = await save({
* filters: [{
* name: 'Image',
* extensions: ['png', 'jpeg']
* }]
* });
* ```
*
* @returns A promise resolving to the selected path.
*
* @since 1.0.0
*/
async function save(options: SaveDialogOptions = {}): Promise<string | null> {
if (typeof options === 'object') {
Object.freeze(options)
}
return invokeTauriCommand({
__tauriModule: 'Dialog',
message: {
cmd: 'saveDialog',
options
}
})
}
/**
* Shows a message dialog with an `Ok` button.
* @example
* ```typescript
* import { message } from '@tauri-apps/api/dialog';
* await message('Tauri is awesome', 'Tauri');
* await message('File not found', { title: 'Tauri', type: 'error' });
* ```
*
* @param message The message to show.
* @param options The dialog's options. If a string, it represents the dialog title.
*
* @returns A promise indicating the success or failure of the operation.
*
* @since 1.0.0
*
*/
async function message(
message: string,
options?: string | MessageDialogOptions
): Promise<void> {
const opts = typeof options === 'string' ? { title: options } : options
return invokeTauriCommand({
__tauriModule: 'Dialog',
message: {
cmd: 'messageDialog',
message: message.toString(),
title: opts?.title?.toString(),
type: opts?.type,
buttonLabel: opts?.okLabel?.toString()
}
})
}
/**
* Shows a question dialog with `Yes` and `No` buttons.
* @example
* ```typescript
* import { ask } from '@tauri-apps/api/dialog';
* const yes = await ask('Are you sure?', 'Tauri');
* const yes2 = await ask('This action cannot be reverted. Are you sure?', { title: 'Tauri', type: 'warning' });
* ```
*
* @param message The message to show.
* @param options The dialog's options. If a string, it represents the dialog title.
*
* @returns A promise resolving to a boolean indicating whether `Yes` was clicked or not.
*
* @since 1.0.0
*/
async function ask(
message: string,
options?: string | ConfirmDialogOptions
): Promise<boolean> {
const opts = typeof options === 'string' ? { title: options } : options
return invokeTauriCommand({
__tauriModule: 'Dialog',
message: {
cmd: 'askDialog',
message: message.toString(),
title: opts?.title?.toString(),
type: opts?.type,
buttonLabels: [
opts?.okLabel?.toString() ?? 'Yes',
opts?.cancelLabel?.toString() ?? 'No'
]
}
})
}
/**
* Shows a question dialog with `Ok` and `Cancel` buttons.
* @example
* ```typescript
* import { confirm } from '@tauri-apps/api/dialog';
* const confirmed = await confirm('Are you sure?', 'Tauri');
* const confirmed2 = await confirm('This action cannot be reverted. Are you sure?', { title: 'Tauri', type: 'warning' });
* ```
*
* @param message The message to show.
* @param options The dialog's options. If a string, it represents the dialog title.
*
* @returns A promise resolving to a boolean indicating whether `Ok` was clicked or not.
*
* @since 1.0.0
*/
async function confirm(
message: string,
options?: string | ConfirmDialogOptions
): Promise<boolean> {
const opts = typeof options === 'string' ? { title: options } : options
return invokeTauriCommand({
__tauriModule: 'Dialog',
message: {
cmd: 'confirmDialog',
message: message.toString(),
title: opts?.title?.toString(),
type: opts?.type,
buttonLabels: [
opts?.okLabel?.toString() ?? 'Ok',
opts?.cancelLabel?.toString() ?? 'Cancel'
]
}
})
}
export type {
DialogFilter,
OpenDialogOptions,
SaveDialogOptions,
MessageDialogOptions,
ConfirmDialogOptions
}
export { open, save, message, ask, confirm }