dialog.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import { invokeTauriCommand } from './helpers/tauri.js';
  2. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  3. // SPDX-License-Identifier: Apache-2.0
  4. // SPDX-License-Identifier: MIT
  5. /**
  6. * Native system dialogs for opening and saving files.
  7. *
  8. * 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`.
  9. *
  10. * The APIs must be added to [`tauri.allowlist.dialog`](https://tauri.app/v1/api/config/#allowlistconfig.dialog) in `tauri.conf.json`:
  11. * ```json
  12. * {
  13. * "tauri": {
  14. * "allowlist": {
  15. * "dialog": {
  16. * "all": true, // enable all dialog APIs
  17. * "ask": true, // enable dialog ask API
  18. * "confirm": true, // enable dialog confirm API
  19. * "message": true, // enable dialog message API
  20. * "open": true, // enable file open API
  21. * "save": true // enable file save API
  22. * }
  23. * }
  24. * }
  25. * }
  26. * ```
  27. * It is recommended to allowlist only the APIs you use for optimal bundle size and security.
  28. * @module
  29. */
  30. /**
  31. * Open a file/directory selection dialog.
  32. *
  33. * The selected paths are added to the filesystem and asset protocol allowlist scopes.
  34. * When security is more important than the easy of use of this API,
  35. * prefer writing a dedicated command instead.
  36. *
  37. * Note that the allowlist scope change is not persisted, so the values are cleared when the application is restarted.
  38. * You can save it to the filesystem using [tauri-plugin-persisted-scope](https://github.com/tauri-apps/plugins-workspace/tree/v1/plugins/persisted-scope).
  39. * @example
  40. * ```typescript
  41. * import { open } from '@tauri-apps/api/dialog';
  42. * // Open a selection dialog for image files
  43. * const selected = await open({
  44. * multiple: true,
  45. * filters: [{
  46. * name: 'Image',
  47. * extensions: ['png', 'jpeg']
  48. * }]
  49. * });
  50. * if (Array.isArray(selected)) {
  51. * // user selected multiple files
  52. * } else if (selected === null) {
  53. * // user cancelled the selection
  54. * } else {
  55. * // user selected a single file
  56. * }
  57. * ```
  58. *
  59. * @example
  60. * ```typescript
  61. * import { open } from '@tauri-apps/api/dialog';
  62. * import { appDir } from '@tauri-apps/api/path';
  63. * // Open a selection dialog for directories
  64. * const selected = await open({
  65. * directory: true,
  66. * multiple: true,
  67. * defaultPath: await appDir(),
  68. * });
  69. * if (Array.isArray(selected)) {
  70. * // user selected multiple directories
  71. * } else if (selected === null) {
  72. * // user cancelled the selection
  73. * } else {
  74. * // user selected a single directory
  75. * }
  76. * ```
  77. *
  78. * @returns A promise resolving to the selected path(s)
  79. *
  80. * @since 1.0.0
  81. */
  82. async function open(options = {}) {
  83. if (typeof options === 'object') {
  84. Object.freeze(options);
  85. }
  86. return invokeTauriCommand({
  87. __tauriModule: 'Dialog',
  88. message: {
  89. cmd: 'openDialog',
  90. options
  91. }
  92. });
  93. }
  94. /**
  95. * Open a file/directory save dialog.
  96. *
  97. * The selected path is added to the filesystem and asset protocol allowlist scopes.
  98. * When security is more important than the easy of use of this API,
  99. * prefer writing a dedicated command instead.
  100. *
  101. * Note that the allowlist scope change is not persisted, so the values are cleared when the application is restarted.
  102. * You can save it to the filesystem using [tauri-plugin-persisted-scope](https://github.com/tauri-apps/plugins-workspace/tree/v1/plugins/persisted-scope).
  103. * @example
  104. * ```typescript
  105. * import { save } from '@tauri-apps/api/dialog';
  106. * const filePath = await save({
  107. * filters: [{
  108. * name: 'Image',
  109. * extensions: ['png', 'jpeg']
  110. * }]
  111. * });
  112. * ```
  113. *
  114. * @returns A promise resolving to the selected path.
  115. *
  116. * @since 1.0.0
  117. */
  118. async function save(options = {}) {
  119. if (typeof options === 'object') {
  120. Object.freeze(options);
  121. }
  122. return invokeTauriCommand({
  123. __tauriModule: 'Dialog',
  124. message: {
  125. cmd: 'saveDialog',
  126. options
  127. }
  128. });
  129. }
  130. /**
  131. * Shows a message dialog with an `Ok` button.
  132. * @example
  133. * ```typescript
  134. * import { message } from '@tauri-apps/api/dialog';
  135. * await message('Tauri is awesome', 'Tauri');
  136. * await message('File not found', { title: 'Tauri', type: 'error' });
  137. * ```
  138. *
  139. * @param message The message to show.
  140. * @param options The dialog's options. If a string, it represents the dialog title.
  141. *
  142. * @returns A promise indicating the success or failure of the operation.
  143. *
  144. * @since 1.0.0
  145. *
  146. */
  147. async function message(message, options) {
  148. var _a, _b;
  149. const opts = typeof options === 'string' ? { title: options } : options;
  150. return invokeTauriCommand({
  151. __tauriModule: 'Dialog',
  152. message: {
  153. cmd: 'messageDialog',
  154. message: message.toString(),
  155. title: (_a = opts === null || opts === void 0 ? void 0 : opts.title) === null || _a === void 0 ? void 0 : _a.toString(),
  156. type: opts === null || opts === void 0 ? void 0 : opts.type,
  157. buttonLabel: (_b = opts === null || opts === void 0 ? void 0 : opts.okLabel) === null || _b === void 0 ? void 0 : _b.toString()
  158. }
  159. });
  160. }
  161. /**
  162. * Shows a question dialog with `Yes` and `No` buttons.
  163. * @example
  164. * ```typescript
  165. * import { ask } from '@tauri-apps/api/dialog';
  166. * const yes = await ask('Are you sure?', 'Tauri');
  167. * const yes2 = await ask('This action cannot be reverted. Are you sure?', { title: 'Tauri', type: 'warning' });
  168. * ```
  169. *
  170. * @param message The message to show.
  171. * @param options The dialog's options. If a string, it represents the dialog title.
  172. *
  173. * @returns A promise resolving to a boolean indicating whether `Yes` was clicked or not.
  174. *
  175. * @since 1.0.0
  176. */
  177. async function ask(message, options) {
  178. var _a, _b, _c, _d, _e;
  179. const opts = typeof options === 'string' ? { title: options } : options;
  180. return invokeTauriCommand({
  181. __tauriModule: 'Dialog',
  182. message: {
  183. cmd: 'askDialog',
  184. message: message.toString(),
  185. title: (_a = opts === null || opts === void 0 ? void 0 : opts.title) === null || _a === void 0 ? void 0 : _a.toString(),
  186. type: opts === null || opts === void 0 ? void 0 : opts.type,
  187. buttonLabels: [
  188. (_c = (_b = opts === null || opts === void 0 ? void 0 : opts.okLabel) === null || _b === void 0 ? void 0 : _b.toString()) !== null && _c !== void 0 ? _c : 'Yes',
  189. (_e = (_d = opts === null || opts === void 0 ? void 0 : opts.cancelLabel) === null || _d === void 0 ? void 0 : _d.toString()) !== null && _e !== void 0 ? _e : 'No'
  190. ]
  191. }
  192. });
  193. }
  194. /**
  195. * Shows a question dialog with `Ok` and `Cancel` buttons.
  196. * @example
  197. * ```typescript
  198. * import { confirm } from '@tauri-apps/api/dialog';
  199. * const confirmed = await confirm('Are you sure?', 'Tauri');
  200. * const confirmed2 = await confirm('This action cannot be reverted. Are you sure?', { title: 'Tauri', type: 'warning' });
  201. * ```
  202. *
  203. * @param message The message to show.
  204. * @param options The dialog's options. If a string, it represents the dialog title.
  205. *
  206. * @returns A promise resolving to a boolean indicating whether `Ok` was clicked or not.
  207. *
  208. * @since 1.0.0
  209. */
  210. async function confirm(message, options) {
  211. var _a, _b, _c, _d, _e;
  212. const opts = typeof options === 'string' ? { title: options } : options;
  213. return invokeTauriCommand({
  214. __tauriModule: 'Dialog',
  215. message: {
  216. cmd: 'confirmDialog',
  217. message: message.toString(),
  218. title: (_a = opts === null || opts === void 0 ? void 0 : opts.title) === null || _a === void 0 ? void 0 : _a.toString(),
  219. type: opts === null || opts === void 0 ? void 0 : opts.type,
  220. buttonLabels: [
  221. (_c = (_b = opts === null || opts === void 0 ? void 0 : opts.okLabel) === null || _b === void 0 ? void 0 : _b.toString()) !== null && _c !== void 0 ? _c : 'Ok',
  222. (_e = (_d = opts === null || opts === void 0 ? void 0 : opts.cancelLabel) === null || _d === void 0 ? void 0 : _d.toString()) !== null && _e !== void 0 ? _e : 'Cancel'
  223. ]
  224. }
  225. });
  226. }
  227. export { ask, confirm, message, open, save };