tauri/tooling/api/src/dialog.ts

107 lines
2.5 KiB
TypeScript
Raw Normal View History

feat(license): SPDX Headers (#1449) * chore(licenses): api Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(licenses): scripts Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): cli/core Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): cli/tauri-bundler Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): workflows Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): require license_template in rust Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): core/tauri Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): core/tauri-api Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): core/tauri-build Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): core/tauri-codegen Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): core/tauri-macros Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): core/tauri-updater Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): core/tauri-utils Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): examples Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): cli/tauri.js Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): changefile Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): place both licenses in root Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): package.json SPDX Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): SPDX everywhere Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * fix(tauri.js): tests more time for ubuntu Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): commons conservancy language Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): add spdx file Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * fix(license): clippy Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com> * chore(license): language Signed-off-by: Daniel Thompson-Yvetot <denjell@mailscript.com>
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
/**
* Native system dialogs for opening and saving files.
*
* This package is also accessible with `window.__TAURI__.dialog` when `tauri.conf.json > build > withGlobalTauri` is set to true.
*
* 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.
* @packageDocumentation
*/
import { invokeTauriCommand } from './helpers/tauri'
/** Extension filters for the file dialog. */
interface DialogFilter {
/** Filter name. */
name: string
/**
* Extensions to filter, without a `.` prefix.
* @example
* ```typescript
* extensions: ['svg', 'png']
* ```
*/
extensions: string[]
}
/** Options for the open dialog. */
interface OpenDialogOptions {
/** The filters of the dialog. */
filters?: DialogFilter[]
/** Initial directory or file path. It must exist. */
defaultPath?: string
/** Whether the dialog allows multiple selection or not. */
multiple?: boolean
/** Whether the dialog is a directory selection or not. */
directory?: boolean
}
/** Options for the save dialog. */
interface SaveDialogOptions {
/** The filters of the dialog. */
filters?: DialogFilter[]
/** Initial directory or file path. It must exist. */
defaultPath?: string
}
/**
* Open a file/directory selection dialog
*
* @returns A promise resolving to the selected path(s)
*/
async function open(
options: OpenDialogOptions = {}
): Promise<string | string[]> {
if (typeof options === 'object') {
Object.freeze(options)
}
return invokeTauriCommand<string | string[]>({
__tauriModule: 'Dialog',
message: {
cmd: 'openDialog',
options
}
})
}
/**
* Open a file/directory save dialog.
*
* @returns A promise resolving to the selected path.
*/
async function save(options: SaveDialogOptions = {}): Promise<string> {
if (typeof options === 'object') {
Object.freeze(options)
}
return invokeTauriCommand<string>({
__tauriModule: 'Dialog',
message: {
cmd: 'saveDialog',
options
}
})
}
export type { DialogFilter, OpenDialogOptions, SaveDialogOptions }
export { open, save }