dialog.cjs 8.2 KB

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