dialog.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. /**
  5. * Native system dialogs for opening and saving files.
  6. *
  7. * This package is also accessible with `window.__TAURI__.dialog` when `tauri.conf.json > build > withGlobalTauri` is set to true.
  8. *
  9. * The APIs must be allowlisted on `tauri.conf.json`:
  10. * ```json
  11. * {
  12. * "tauri": {
  13. * "allowlist": {
  14. * "dialog": {
  15. * "all": true, // enable all dialog APIs
  16. * "open": true, // enable file open API
  17. * "save": true // enable file save API
  18. * }
  19. * }
  20. * }
  21. * }
  22. * ```
  23. * It is recommended to allowlist only the APIs you use for optimal bundle size and security.
  24. * @module
  25. */
  26. import { invokeTauriCommand } from './helpers/tauri'
  27. /** Extension filters for the file dialog. */
  28. interface DialogFilter {
  29. /** Filter name. */
  30. name: string
  31. /**
  32. * Extensions to filter, without a `.` prefix.
  33. * @example
  34. * ```typescript
  35. * extensions: ['svg', 'png']
  36. * ```
  37. */
  38. extensions: string[]
  39. }
  40. /** Options for the open dialog. */
  41. interface OpenDialogOptions {
  42. /** The filters of the dialog. */
  43. filters?: DialogFilter[]
  44. /** Initial directory or file path. It must exist. */
  45. defaultPath?: string
  46. /** Whether the dialog allows multiple selection or not. */
  47. multiple?: boolean
  48. /** Whether the dialog is a directory selection or not. */
  49. directory?: boolean
  50. }
  51. /** Options for the save dialog. */
  52. interface SaveDialogOptions {
  53. /** The filters of the dialog. */
  54. filters?: DialogFilter[]
  55. /**
  56. * Initial directory or file path.
  57. * If it's a directory path, the dialog interface will change to that folder.
  58. * 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.
  59. */
  60. defaultPath?: string
  61. }
  62. /**
  63. * Open a file/directory selection dialog
  64. *
  65. * @returns A promise resolving to the selected path(s)
  66. */
  67. async function open(
  68. options: OpenDialogOptions = {}
  69. ): Promise<string | string[]> {
  70. if (typeof options === 'object') {
  71. Object.freeze(options)
  72. }
  73. return invokeTauriCommand({
  74. __tauriModule: 'Dialog',
  75. message: {
  76. cmd: 'openDialog',
  77. options
  78. }
  79. })
  80. }
  81. /**
  82. * Open a file/directory save dialog.
  83. *
  84. * @returns A promise resolving to the selected path.
  85. */
  86. async function save(options: SaveDialogOptions = {}): Promise<string> {
  87. if (typeof options === 'object') {
  88. Object.freeze(options)
  89. }
  90. return invokeTauriCommand({
  91. __tauriModule: 'Dialog',
  92. message: {
  93. cmd: 'saveDialog',
  94. options
  95. }
  96. })
  97. }
  98. /**
  99. * Shows a message dialog with an `Ok` button.
  100. *
  101. * @param {string} message The message to show.
  102. *
  103. * @return {Promise<void>} A promise indicating the success or failure of the operation.
  104. */
  105. async function message(message: string): Promise<void> {
  106. return invokeTauriCommand({
  107. __tauriModule: 'Dialog',
  108. message: {
  109. cmd: 'messageDialog',
  110. message
  111. }
  112. })
  113. }
  114. /**
  115. * Shows a question dialog with `Yes` and `No` buttons.
  116. *
  117. * @param {string} message The message to show.
  118. * @param {string|undefined} title The dialog's title. Defaults to the application name.
  119. *
  120. * @return {Promise<void>} A promise resolving to a boolean indicating whether `Yes` was clicked or not.
  121. */
  122. async function ask(message: string, title?: string): Promise<boolean> {
  123. return invokeTauriCommand({
  124. __tauriModule: 'Dialog',
  125. message: {
  126. cmd: 'askDialog',
  127. title,
  128. message
  129. }
  130. })
  131. }
  132. /**
  133. * Shows a question dialog with `Ok` and `Cancel` buttons.
  134. *
  135. * @param {string} message The message to show.
  136. * @param {string|undefined} title The dialog's title. Defaults to the application name.
  137. *
  138. * @return {Promise<void>} A promise resolving to a boolean indicating whether `Ok` was clicked or not.
  139. */
  140. async function confirm(message: string, title?: string): Promise<boolean> {
  141. return invokeTauriCommand({
  142. __tauriModule: 'Dialog',
  143. message: {
  144. cmd: 'confirmDialog',
  145. title,
  146. message
  147. }
  148. })
  149. }
  150. export type { DialogFilter, OpenDialogOptions, SaveDialogOptions }
  151. export { open, save, message, ask, confirm }