-
Notifications
You must be signed in to change notification settings - Fork 11
/
api.ts
427 lines (389 loc) · 13.8 KB
/
api.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import { z } from "zod";
import { invoke } from "@tauri-apps/api/tauri";
import { emit, listen, UnlistenFn } from "@tauri-apps/api/event";
export const START_MONITOR_COMMAND = "plugin:clipboard|start_monitor";
export const STOP_MONITOR_COMMAND = "plugin:clipboard|stop_monitor";
export const SOMETHING_CHANGED = "plugin:clipboard://something-changed";
export const TEXT_CHANGED = "plugin:clipboard://text-changed";
export const HTML_CHANGED = "plugin:clipboard://html-changed";
export const RTF_CHANGED = "plugin:clipboard://rtf-changed";
export const FILES_CHANGED = "plugin:clipboard://files-changed";
export const IMAGE_CHANGED = "plugin:clipboard://image-changed";
export const IS_MONITOR_RUNNING_COMMAND = "plugin:clipboard|is_monitor_running";
export const HAS_TEXT_COMMAND = "plugin:clipboard|has_text";
export const HAS_IMAGE_COMMAND = "plugin:clipboard|has_image";
export const HAS_HTML_COMMAND = "plugin:clipboard|has_html";
export const HAS_RTF_COMMAND = "plugin:clipboard|has_rtf";
export const HAS_FILES_COMMAND = "plugin:clipboard|has_files";
export const WRITE_TEXT_COMMAND = "plugin:clipboard|write_text";
export const WRITE_HTML_COMMAND = "plugin:clipboard|write_html";
export const WRITE_HTML_AND_TEXT_COMMAND =
"plugin:clipboard|write_html_and_text";
export const WRITE_RTF_COMMAND = "plugin:clipboard|write_rtf";
export const WRITE_FILES_URIS_COMMAND = "plugin:clipboard|write_files_uris";
export const WRITE_FILES_COMMAND = "plugin:clipboard|write_files";
export const CLEAR_COMMAND = "plugin:clipboard|clear";
export const READ_TEXT_COMMAND = "plugin:clipboard|read_text";
export const READ_HTML_COMMAND = "plugin:clipboard|read_html";
export const READ_RTF_COMMAND = "plugin:clipboard|read_rtf";
export const READ_FILES_COMMAND = "plugin:clipboard|read_files";
export const READ_FILES_URIS_COMMAND = "plugin:clipboard|read_files_uris";
export const READ_IMAGE_BINARY_COMMAND = "plugin:clipboard|read_image_binary";
export const READ_IMAGE_BASE64_COMMAND = "plugin:clipboard|read_image_base64";
export const WRITE_IMAGE_BINARY_COMMAND = "plugin:clipboard|write_image_binary";
export const WRITE_IMAGE_BASE64_COMMAND = "plugin:clipboard|write_image_base64";
export const CLIPBOARD_MONITOR_STATUS_UPDATE_EVENT =
"plugin:clipboard://clipboard-monitor/status";
export const MONITOR_UPDATE_EVENT =
"plugin:clipboard://clipboard-monitor/update";
export const ClipboardChangedPayloadSchema = z.object({ value: z.string() });
export const ClipboardChangedFilesPayloadSchema = z.object({
value: z.string().array(),
});
export type ClipboardChangedPayload = z.infer<
typeof ClipboardChangedPayloadSchema
>;
export function hasText() {
return invoke<boolean>(HAS_TEXT_COMMAND);
}
export function hasHTML() {
return invoke<boolean>(HAS_HTML_COMMAND);
}
export function hasRTF() {
return invoke<boolean>(HAS_RTF_COMMAND);
}
export function hasImage() {
return invoke<boolean>(HAS_IMAGE_COMMAND);
}
export function hasFiles() {
return invoke<boolean>(HAS_FILES_COMMAND);
}
export function writeText(text: string) {
return invoke<void>(WRITE_TEXT_COMMAND, { text });
}
export function writeHtml(html: string) {
return invoke<void>(WRITE_HTML_COMMAND, { html });
}
/**
* Write html and text to clipboard.
* writeHtml API only writes html, readText will return nothing.
* This API writes both html and text, so readText will return the text.
*/
export function writeHtmlAndText(html: string, text: string) {
return invoke<void>(WRITE_HTML_AND_TEXT_COMMAND, { html, text });
}
export function writeRtf(rtf: string) {
return invoke<void>(WRITE_RTF_COMMAND, { rtf });
}
/**
* Write files uris to clipboard. The files should be in uri format: `file:///path/to/file` on Mac and Linux. File path is absolute path.
* On Windows, the path should be in the format `C:\\path\\to\\file`.
* @param filesUris
* @returns
*/
export function writeFilesURIs(filesUris: string[]) {
return invoke<void>(WRITE_FILES_URIS_COMMAND, { filesUris });
}
export function writeFiles(filesPaths: string[]) {
return invoke<void>(WRITE_FILES_COMMAND, { filesPaths });
}
export function clear() {
return invoke<void>(CLEAR_COMMAND);
}
export function readText() {
return invoke<string>(READ_TEXT_COMMAND);
}
export function readHtml() {
return invoke<string>(READ_HTML_COMMAND);
}
export function readRtf() {
return invoke<string>(READ_RTF_COMMAND);
}
export function readFiles() {
return invoke<string[]>(READ_FILES_COMMAND);
}
export function readFilesURIs() {
return invoke<string[]>(READ_FILES_URIS_COMMAND);
}
/**
* read clipboard image
* @returns image in base64 string
*/
export function readImageBase64() {
return invoke<string>(READ_IMAGE_BASE64_COMMAND);
}
// export const readImageBase64 = readImage;
/**
* Read clipboard image, get the data in binary format
* int_array (Array<number>) is received from Tauri core, Uint8Array and Blob are transformed from int_array
* @param format data type of returned value, "int_array" is the fastest
* @returns
*/
export function readImageBinary(format: "int_array" | "Uint8Array" | "Blob") {
return (
invoke<number[] | Uint8Array | Blob>(READ_IMAGE_BINARY_COMMAND) as Promise<
number[]
>
).then((img_arr: number[]) => {
switch (format) {
case "int_array":
return img_arr;
case "Uint8Array":
return new Uint8Array(img_arr);
case "Blob":
return new Blob([new Uint8Array(img_arr)]);
default:
return img_arr;
}
});
}
/**
* Here is the transformation flow,
* read clipboard image as Array<number> (int_array) -> int_array -> Uint8Array -> Blob -> ObjectURL
* There are many layers which could make this function slow for large images.
* @returns ObjectURL for clipboard image
*/
export function readImageObjectURL(): Promise<string> {
return readImageBinary("Blob").then((blob) => {
return URL.createObjectURL(blob as Blob);
});
}
/**
* write image to clipboard
* @param data image data in base64 encoded string
* @returns Promise<void>
*/
export function writeImageBase64(base64: string) {
return invoke<void>(WRITE_IMAGE_BASE64_COMMAND, { base64Image: base64 });
}
export function writeImageBinary(bytes: number[]) {
return invoke<void>(WRITE_IMAGE_BINARY_COMMAND, { bytes: bytes });
}
/**
* @deprecated since version v0.5.x
* Brute force listen to clipboard text update.
* Detect update by comparing current value with previous value every delay ms.
* When there is a update, "plugin:clipboard://text-changed" is emitted.
* You still need to listen to the event.
*
* @param delay check interval delay
* @returns a stop running function that can be called when component unmounts
*/
export function startBruteForceTextMonitor(delay: number = 500) {
let prevText: string = "";
let active: boolean = true; // whether the listener should be running
setTimeout(async function x() {
try {
const text = await readText();
if (prevText !== text) {
await emit(TEXT_CHANGED, { value: text });
}
prevText = text;
} catch (error) {}
if (active) setTimeout(x, delay);
}, delay);
return function () {
active = false;
};
}
/**
* @deprecated since version v0.5.x
* Brute force monitor clipboard image update by comparing current value with previous value.
* When there is a update, "plugin:clipboard://image-changed" is emitted.
* You still need to listen to the event.
*
* @param delay check interval delay
* @returns stop running function that can be called to stop the monitor
*/
export function startBruteForceImageMonitor(delay: number = 1000) {
let prevImg: string = "";
let active: boolean = true; // whether the listener should be running
setTimeout(async function x() {
try {
const img = await readImageBase64();
if (prevImg !== img) {
await emit(IMAGE_CHANGED, { value: img });
}
prevImg = img;
} catch (error) {
// ! when there is no image in clipboard, there may be error thrown, we ignore the error
}
if (active) setTimeout(x, delay);
}, delay);
return function () {
active = false;
};
}
type UpdatedTypes = {
text: boolean;
html: boolean;
rtf: boolean;
image: boolean;
files: boolean;
};
/**
* Listen to "plugin:clipboard://clipboard-monitor/update" from Tauri core.
* The corresponding clipboard type event will be emitted when there is clipboard update.
* @returns unlisten function
*/
export function listenToClipboard(): Promise<UnlistenFn> {
return listen(MONITOR_UPDATE_EVENT, async (e) => {
if (e.payload === "clipboard update") {
const flags: UpdatedTypes = {
files: await hasFiles(),
image: await hasImage(),
html: await hasHTML(),
rtf: await hasRTF(),
text: await hasText(),
};
await emit(SOMETHING_CHANGED, flags);
if (flags.files) {
const files = await readFiles();
if (files && files.length > 0) {
await emit(FILES_CHANGED, { value: files });
}
flags.files = true;
return; // ! this return is necessary, copying files also update clipboard text, but we don't want text update to be triggered
}
if (flags.image) {
const img = await readImageBase64();
if (img) await emit(IMAGE_CHANGED, { value: img });
flags.image = true;
}
if (flags.html) {
await emit(HTML_CHANGED, { value: await readHtml() });
flags.html = true;
}
if (flags.rtf) {
await emit(RTF_CHANGED, { value: await readRtf() });
flags.rtf = true;
}
if (flags.text) {
await emit(TEXT_CHANGED, { value: await readText() });
flags.text = true;
}
// when clear() is called, this error is thrown, let ignore it
// if (!success) {
// throw new Error("Unexpected Error: No proper clipboard type");
// }
}
});
}
/**
* This listen to clipboard monitor update event, and trigger the callback function.
* However from this event we don't know whether it's text or image, no real data is returned.
* Use with listenToClipboard function.
* @param cb callback
* @returns unlisten function
*/
export function onClipboardUpdate(cb: () => void) {
return listen(MONITOR_UPDATE_EVENT, cb);
}
export async function onTextUpdate(
cb: (text: string) => void
): Promise<UnlistenFn> {
return await listen(TEXT_CHANGED, (event) => {
const text = ClipboardChangedPayloadSchema.parse(event.payload).value;
cb(text);
});
}
/**
* Listen to clipboard update event and get the updated types in a callback.
* This listener tells you what types of data are updated.
* This relies on `listenToClipboard()` who emits events this function listens to.
* You can run `listenToClipboard()` or `startListening()` before calling this function.
* When HTML is copied, this will be passed to callback: {files: false, image: false, html: true, rtf: false, text: true}
* @param cb
* @returns
*/
export async function onSomethingUpdate(
cb: (updatedTypes: UpdatedTypes) => void
) {
return await listen(SOMETHING_CHANGED, (event) => {
cb(event.payload as UpdatedTypes);
});
}
export async function onHTMLUpdate(
cb: (text: string) => void
): Promise<UnlistenFn> {
return await listen(HTML_CHANGED, (event) => {
const text = ClipboardChangedPayloadSchema.parse(event.payload).value;
cb(text);
});
}
export async function onRTFUpdate(
cb: (text: string) => void
): Promise<UnlistenFn> {
return await listen(RTF_CHANGED, (event) => {
const text = ClipboardChangedPayloadSchema.parse(event.payload).value;
cb(text);
});
}
export async function onFilesUpdate(
cb: (files: string[]) => void
): Promise<UnlistenFn> {
return await listen(FILES_CHANGED, (event) => {
const files = ClipboardChangedFilesPayloadSchema.parse(event.payload).value;
cb(files);
});
}
export async function onImageUpdate(
cb: (base64ImageStr: string) => void
): Promise<UnlistenFn> {
return await listen(IMAGE_CHANGED, (event) => {
const base64ImageStr = ClipboardChangedPayloadSchema.parse(
event.payload
).value;
cb(base64ImageStr);
});
}
/**
* Used to check the status of clipboard monitor
* @returns Whether the monitor is running
*/
export function isMonitorRunning() {
return invoke<boolean>(IS_MONITOR_RUNNING_COMMAND).then((res: unknown) =>
z.boolean().parse(res)
);
}
/**
* Start running mointor thread in Tauri core. This feature is added in v0.5.x.
* Before v0.5.x, the monitor is started during setup when app starts.
* After v0.5.x, this function must be called first to start monitor.
* After monitor is started, events "plugin:clipboard://clipboard-monitor/update" will be emitted when there is clipboard update.
* "plugin:clipboard://clipboard-monitor/status" event is also emitted when monitor status updates
* Still have to listen to these events.
*/
export function startMonitor() {
return invoke<void>(START_MONITOR_COMMAND);
}
/**
* Stop clipboard monitor thread.
*/
export function stopMonitor() {
return invoke<void>(STOP_MONITOR_COMMAND);
}
/**
* Listen to monitor status update. Instead of calling isMonitorRunning to get status of monitor,
* "plugin:clipboard://clipboard-monitor/status" event is emitted from Tauri core when monitor status updates.
* @param cb callback to be called when there is monitor status update
*/
export async function listenToMonitorStatusUpdate(
cb: (running: boolean) => void
): Promise<UnlistenFn> {
return await listen(CLIPBOARD_MONITOR_STATUS_UPDATE_EVENT, (event) => {
const newStatus = z.boolean().parse(event.payload);
cb(newStatus);
});
}
export function startListening(): Promise<() => Promise<void>> {
return startMonitor()
.then(() => listenToClipboard())
.then((unlistenClipboard: UnlistenFn) => {
// return an unlisten function that stop listening to clipboard update and stop the monitor
return async () => {
unlistenClipboard();
await stopMonitor();
};
});
}