2021-04-11 01:09:09 +03:00
|
|
|
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2021-05-05 20:36:40 +03:00
|
|
|
/**
|
|
|
|
* Native system dialogs for opening and saving files.
|
2021-05-18 04:33:09 +03:00
|
|
|
*
|
2021-05-17 23:56:14 +03:00
|
|
|
* This package is also accessible with `window.__TAURI__.dialog` when `tauri.conf.json > build > withGlobalTauri` is set to true.
|
2021-05-18 04:33:09 +03:00
|
|
|
*
|
|
|
|
* The APIs must be allowlisted on `tauri.conf.json`:
|
|
|
|
* ```json
|
|
|
|
* {
|
|
|
|
* "tauri": {
|
|
|
|
* "allowlist": {
|
|
|
|
* "dialog": {
|
|
|
|
* "all": true, // enable all dialog APIs
|
|
|
|
* "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.
|
2021-05-05 20:36:40 +03:00
|
|
|
* @packageDocumentation
|
|
|
|
*/
|
|
|
|
|
2021-03-07 05:19:12 +03:00
|
|
|
import { invokeTauriCommand } from './helpers/tauri'
|
2020-06-27 18:20:00 +03:00
|
|
|
|
2021-05-05 20:36:40 +03:00
|
|
|
/** Extension filters for the file dialog. */
|
2021-05-17 18:54:56 +03:00
|
|
|
interface DialogFilter {
|
2021-05-05 20:36:40 +03:00
|
|
|
/** Filter name. */
|
2021-02-18 17:43:41 +03:00
|
|
|
name: string
|
2021-05-05 20:36:40 +03:00
|
|
|
/**
|
|
|
|
* Extensions to filter, without a `.` prefix.
|
|
|
|
* @example
|
|
|
|
* ```typescript
|
|
|
|
* extensions: ['svg', 'png']
|
|
|
|
* ```
|
|
|
|
*/
|
2021-02-18 17:43:41 +03:00
|
|
|
extensions: string[]
|
|
|
|
}
|
|
|
|
|
2021-05-05 20:36:40 +03:00
|
|
|
/** Options for the open dialog. */
|
2021-05-17 18:54:56 +03:00
|
|
|
interface OpenDialogOptions {
|
2021-05-05 20:36:40 +03:00
|
|
|
/** The filters of the dialog. */
|
2021-02-18 17:43:41 +03:00
|
|
|
filters?: DialogFilter[]
|
2021-05-05 20:36:40 +03:00
|
|
|
/** Initial directory or file path. It must exist. */
|
2021-02-09 21:22:04 +03:00
|
|
|
defaultPath?: string
|
2021-05-05 20:36:40 +03:00
|
|
|
/** Whether the dialog allows multiple selection or not. */
|
2021-02-09 21:22:04 +03:00
|
|
|
multiple?: boolean
|
2021-05-05 20:36:40 +03:00
|
|
|
/** Whether the dialog is a directory selection or not. */
|
2021-02-09 21:22:04 +03:00
|
|
|
directory?: boolean
|
2020-07-13 01:34:44 +03:00
|
|
|
}
|
|
|
|
|
2021-05-05 20:36:40 +03:00
|
|
|
/** Options for the save dialog. */
|
2021-05-17 18:54:56 +03:00
|
|
|
interface SaveDialogOptions {
|
2021-05-05 20:36:40 +03:00
|
|
|
/** The filters of the dialog. */
|
2021-02-18 17:43:41 +03:00
|
|
|
filters?: DialogFilter[]
|
2021-05-05 20:36:40 +03:00
|
|
|
/** Initial directory or file path. It must exist. */
|
2021-02-18 17:43:41 +03:00
|
|
|
defaultPath?: string
|
|
|
|
}
|
2020-07-13 01:34:44 +03:00
|
|
|
|
2020-06-27 18:20:00 +03:00
|
|
|
/**
|
2021-04-13 04:44:50 +03:00
|
|
|
* Open a file/directory selection dialog
|
|
|
|
*
|
|
|
|
* @returns A promise resolving to the selected path(s)
|
2020-06-27 18:20:00 +03:00
|
|
|
*/
|
2020-08-08 03:54:17 +03:00
|
|
|
async function open(
|
|
|
|
options: OpenDialogOptions = {}
|
|
|
|
): Promise<string | string[]> {
|
2021-02-09 21:22:04 +03:00
|
|
|
if (typeof options === 'object') {
|
|
|
|
Object.freeze(options)
|
2020-06-27 18:20:00 +03:00
|
|
|
}
|
|
|
|
|
2021-03-07 05:19:12 +03:00
|
|
|
return invokeTauriCommand<string | string[]>({
|
2021-02-16 07:23:15 +03:00
|
|
|
__tauriModule: 'Dialog',
|
2021-02-12 08:42:40 +03:00
|
|
|
message: {
|
|
|
|
cmd: 'openDialog',
|
|
|
|
options
|
|
|
|
}
|
2021-02-09 21:22:04 +03:00
|
|
|
})
|
2020-06-27 18:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-13 04:44:50 +03:00
|
|
|
* Open a file/directory save dialog.
|
|
|
|
*
|
|
|
|
* @returns A promise resolving to the selected path.
|
2020-06-27 18:20:00 +03:00
|
|
|
*/
|
2020-08-08 03:54:17 +03:00
|
|
|
async function save(options: SaveDialogOptions = {}): Promise<string> {
|
2021-02-09 21:22:04 +03:00
|
|
|
if (typeof options === 'object') {
|
|
|
|
Object.freeze(options)
|
2020-06-27 18:20:00 +03:00
|
|
|
}
|
|
|
|
|
2021-03-07 05:19:12 +03:00
|
|
|
return invokeTauriCommand<string>({
|
2021-02-16 07:23:15 +03:00
|
|
|
__tauriModule: 'Dialog',
|
2021-02-12 08:42:40 +03:00
|
|
|
message: {
|
|
|
|
cmd: 'saveDialog',
|
|
|
|
options
|
|
|
|
}
|
2021-02-09 21:22:04 +03:00
|
|
|
})
|
2020-06-27 18:20:00 +03:00
|
|
|
}
|
|
|
|
|
2021-05-17 18:54:56 +03:00
|
|
|
export type { DialogFilter, OpenDialogOptions, SaveDialogOptions }
|
|
|
|
|
2021-02-09 21:22:04 +03:00
|
|
|
export { open, save }
|