mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-06-06 13:53:54 +02:00
2cf8faa3e1
* chore(deps): update to tauri alpha.20, @tauri-apps/api alpha.13 * fix lockfile
57 lines
1.3 KiB
TypeScript
57 lines
1.3 KiB
TypeScript
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
/**
|
|
* Read and write to the system clipboard.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import { invoke } from "@tauri-apps/api/core";
|
|
|
|
type ClipResponse = Record<"plainText", { text: string }>;
|
|
|
|
/**
|
|
* Writes plain text to the clipboard.
|
|
* @example
|
|
* ```typescript
|
|
* import { writeText, readText } from '@tauri-apps/plugin-clipboard-manager';
|
|
* await writeText('Tauri is awesome!');
|
|
* assert(await readText(), 'Tauri is awesome!');
|
|
* ```
|
|
*
|
|
* @returns A promise indicating the success or failure of the operation.
|
|
*
|
|
* @since 2.0.0
|
|
*/
|
|
async function writeText(
|
|
text: string,
|
|
opts?: { label?: string },
|
|
): Promise<void> {
|
|
return invoke("plugin:clipboard|write", {
|
|
data: {
|
|
plainText: {
|
|
label: opts?.label,
|
|
text,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Gets the clipboard content as plain text.
|
|
* @example
|
|
* ```typescript
|
|
* import { readText } from '@tauri-apps/plugin-clipboard-manager';
|
|
* const clipboardText = await readText();
|
|
* ```
|
|
* @since 2.0.0
|
|
*/
|
|
async function readText(): Promise<string> {
|
|
const kind: ClipResponse = await invoke("plugin:clipboard|read");
|
|
return kind.plainText.text;
|
|
}
|
|
|
|
export { writeText, readText };
|