forked from tauri-apps/plugins-workspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
151 lines (142 loc) · 3.7 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// 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'
import { Image, transformImage } from '@tauri-apps/api/image'
/**
* 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> {
await invoke('plugin:clipboard-manager|write_text', {
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> {
return await invoke('plugin:clipboard-manager|read_text')
}
/**
* Writes image buffer to the clipboard.
*
* #### Platform-specific
*
* - **Android / iOS:** Not supported.
*
* @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> {
await invoke('plugin:clipboard-manager|write_image', {
image: transformImage(image)
})
}
/**
* Gets the clipboard content as Uint8Array image.
*
* #### Platform-specific
*
* - **Android / iOS:** Not supported.
*
* @example
* ```typescript
* import { readImage } from '@tauri-apps/plugin-clipboard-manager';
*
* const clipboardImage = await readImage();
* const blob = new Blob([await clipboardImage.rbga()], { 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 HTML or fallbacks to write provided plain text to the clipboard.
*
* #### Platform-specific
*
* - **Android / iOS:** Not supported.
*
* @example
* ```typescript
* import { writeHtml } from '@tauri-apps/plugin-clipboard-manager';
* await writeHtml('<h1>Tauri is awesome!</h1>', 'plaintext');
* // The following will write "<h1>Tauri is awesome</h1>" as plain text
* await writeHtml('<h1>Tauri is awesome!</h1>', '<h1>Tauri is awesome</h1>');
* // we can read html data only as a string so there's just readText(), no readHtml()
* assert(await readText(), '<h1>Tauri is awesome!</h1>');
* ```
*
* @returns A promise indicating the success or failure of the operation.
*
* @since 2.0.0
*/
async function writeHtml(html: string, altText?: string): Promise<void> {
await invoke('plugin:clipboard-manager|write_html', {
html,
altText
})
}
/**
* Clears the clipboard.
*
* #### Platform-specific
*
* - **Android:** Only supported on SDK 28+. For older releases we write an empty string to the clipboard instead.
*
* @example
* ```typescript
* import { clear } from '@tauri-apps/plugin-clipboard-manager';
* await clear();
* ```
* @since 2.0.0
*/
async function clear(): Promise<void> {
await invoke('plugin:clipboard-manager|clear')
}
export { writeText, readText, writeHtml, clear, readImage, writeImage }