feat(clipboard): support readImage & writeImage (#845)

* feat(clipboard): support `read_image` & `write_image`

* fix plugin name

* platform specific bahavior

* remove unnecessary BufWriter

* improvement

* update example

* update example

* format

* header, fix change file

* use image from tauri

* fix ci

* update tauri, fix read

* image crate only on desktop [skip ci]

* Update plugins/authenticator/src/u2f_crate/protocol.rs [skip ci]

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>

* Update plugins/authenticator/src/u2f_crate/protocol.rs [skip ci]

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>

* update deps, address code review

* fix mobile [skip ci]

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Co-authored-by: Lucas Nogueira <lucas@tauri.app>
Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
This commit is contained in:
authored and GitHub committed 2024-03-19 09:35:37 -03:00
1 parent 7b9fa6607b
commit 9dec9605ed
91 files changed
+1331 -795

No files matched your search

+53 -4
View File
@@ -9,6 +9,7 @@
*/
import { invoke } from "@tauri-apps/api/core";
import { Image, transformImage } from "@tauri-apps/api/image";
type ClipResponse = Record<"plainText", { text: string }>;
@@ -29,7 +30,7 @@ async function writeText(
text: string,
opts?: { label?: string },
): Promise<void> {
return invoke("plugin:clipboard-manager|write", {
return invoke("plugin:clipboard-manager|write_text", {
data: {
plainText: {
label: opts?.label,
@@ -49,12 +50,60 @@ async function writeText(
* @since 2.0.0
*/
async function readText(): Promise<string> {
const kind: ClipResponse = await invoke("plugin:clipboard-manager|read");
const kind: ClipResponse = await invoke("plugin:clipboard-manager|read_text");
return kind.plainText.text;
}
/**
* Writes HTML or fallbacks to write provided plain text to the clipboard.
* Gets the clipboard content as Uint8Array image.
* @example
* ```typescript
* import { readImage } from '@tauri-apps/plugin-clipboard-manager';
*
* const clipboardImage = await readImage();
* const blob = new Blob([clipboardImage.bytes], { type: 'image' })
* const url = URL.createObjectURL(blob)
* ```
* @since 2.0.0
*/
async function readImage(): Promise<Image> {
return await invoke<number>("plugin:clipboard-manager|read_image").then(
(rid) => new Image(rid),
);
}
/**
* Writes image buffer to the clipboard.
* @example
* ```typescript
* import { writeImage } from '@tauri-apps/plugin-clipboard-manager';
* const buffer = [
* // A red pixel
* 255, 0, 0, 255,
*
* // A green pixel
* 0, 255, 0, 255,
* ];
* await writeImage(buffer);
*
* @returns A promise indicating the success or failure of the operation.
*
* @since 2.0.0
*/
async function writeImage(
image: string | Image | Uint8Array | ArrayBuffer | number[],
): Promise<void> {
return invoke("plugin:clipboard-manager|write_image", {
data: {
image: {
image: transformImage(image),
},
},
});
}
/**
* * Writes HTML or fallbacks to write provided plain text to the clipboard.
* @example
* ```typescript
* import { writeHtml, readHtml } from '@tauri-apps/plugin-clipboard-manager';
@@ -92,4 +141,4 @@ async function clear(): Promise<void> {
return;
}
export { writeText, readText, writeHtml, clear };
export { writeText, readText, writeHtml, clear, readImage, writeImage };