clipboard.cjs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. * Read and write to the system clipboard.
  8. *
  9. * This package is also accessible with `window.__TAURI__.clipboard` 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.clipboard`](https://tauri.app/v1/api/config/#allowlistconfig.clipboard) in `tauri.conf.json`:
  12. * ```json
  13. * {
  14. * "tauri": {
  15. * "allowlist": {
  16. * "clipboard": {
  17. * "all": true, // enable all Clipboard APIs
  18. * "writeText": true,
  19. * "readText": true
  20. * }
  21. * }
  22. * }
  23. * }
  24. * ```
  25. * It is recommended to allowlist only the APIs you use for optimal bundle size and security.
  26. *
  27. * @module
  28. */
  29. /**
  30. * Writes plain text to the clipboard.
  31. * @example
  32. * ```typescript
  33. * import { writeText, readText } from '@tauri-apps/api/clipboard';
  34. * await writeText('Tauri is awesome!');
  35. * assert(await readText(), 'Tauri is awesome!');
  36. * ```
  37. *
  38. * @returns A promise indicating the success or failure of the operation.
  39. *
  40. * @since 1.0.0.
  41. */
  42. async function writeText(text) {
  43. return tauri.invokeTauriCommand({
  44. __tauriModule: 'Clipboard',
  45. message: {
  46. cmd: 'writeText',
  47. data: text
  48. }
  49. });
  50. }
  51. /**
  52. * Gets the clipboard content as plain text.
  53. * @example
  54. * ```typescript
  55. * import { readText } from '@tauri-apps/api/clipboard';
  56. * const clipboardText = await readText();
  57. * ```
  58. * @since 1.0.0.
  59. */
  60. async function readText() {
  61. return tauri.invokeTauriCommand({
  62. __tauriModule: 'Clipboard',
  63. message: {
  64. cmd: 'readText',
  65. // if data is not set, `serde` will ignore the custom deserializer
  66. // that is set when the API is not allowlisted
  67. data: null
  68. }
  69. });
  70. }
  71. exports.readText = readText;
  72. exports.writeText = writeText;