/
listener.svelte
145 lines (136 loc) · 3.48 KB
/
listener.svelte
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
<script lang="ts">
import type { UnlistenFn } from '@tauri-apps/api/event';
import { onDestroy, onMount } from 'svelte';
import {
onClipboardUpdate,
onImageUpdate,
onTextUpdate,
onHTMLUpdate,
onRTFUpdate,
onFilesUpdate,
startListening,
listenToMonitorStatusUpdate,
isMonitorRunning,
hasHTML,
hasImage,
hasText,
hasRTF,
hasFiles
} from 'tauri-plugin-clipboard-api';
let text = '';
let files: string[] = [];
let base64Image = '';
let htmlMonitorContent = '';
let monitorRunning = false;
let rtf = '';
let unlistenTextUpdate: UnlistenFn;
let unlistenImageUpdate: UnlistenFn;
let unlistenHtmlUpdate: UnlistenFn;
let unlistenRTF: UnlistenFn;
let unlistenClipboard: () => Promise<void>;
let unlistenFiles: UnlistenFn;
const has = {
hasHTML: false,
hasImage: false,
hasText: false,
hasRTF: false,
hasFiles: false
};
function clear() {
text = '';
files = [];
base64Image = '';
htmlMonitorContent = '';
rtf = '';
}
onMount(async () => {
unlistenTextUpdate = await onTextUpdate((newText) => {
text = newText;
});
unlistenHtmlUpdate = await onHTMLUpdate((newHtml) => {
htmlMonitorContent = newHtml;
});
unlistenImageUpdate = await onImageUpdate((b64Str) => {
base64Image = b64Str;
});
unlistenFiles = await onFilesUpdate((newFiles) => {
files = newFiles;
});
unlistenRTF = await onRTFUpdate((newRTF) => {
rtf = newRTF;
});
unlistenClipboard = await startListening();
onClipboardUpdate(async () => {
clear();
has.hasHTML = await hasHTML();
has.hasImage = await hasImage();
has.hasText = await hasText();
has.hasRTF = await hasRTF();
has.hasFiles = await hasFiles();
console.log('plugin:clipboard://clipboard-monitor/update event received');
});
// setInterval(async () => {
// console.log("Running:", await isMonitorRunning());
// }, 1000);
});
listenToMonitorStatusUpdate((running) => {
monitorRunning = running;
});
onDestroy(() => {
if (unlistenTextUpdate) unlistenTextUpdate();
if (unlistenImageUpdate) unlistenImageUpdate();
if (unlistenHtmlUpdate) unlistenHtmlUpdate();
if (unlistenFiles) unlistenFiles();
if (unlistenClipboard) unlistenClipboard();
});
</script>
<p><strong>Is Monitor Running:</strong> {monitorRunning}</p>
{#if monitorRunning}
<button
class="btn variant-filled-error"
on:click={async () => {
await unlistenClipboard();
}}>Stop Listening</button
>
{:else}
<button
class="btn variant-filled-success"
on:click={async () => {
unlistenClipboard = await startListening();
}}>Start Listening</button
>
{/if}
<div>
<div><strong>hasHTML:</strong> {has.hasHTML}</div>
<div><strong>hasImage:</strong> {has.hasImage}</div>
<div><strong>hasText:</strong> {has.hasText}</div>
<div><strong>hasRTF:</strong> {has.hasRTF}</div>
<div><strong>hasFiles:</strong> {has.hasFiles}</div>
</div>
<div>
<h2>Clipboard Text</h2>
{#if text}
<pre class="border p-2 rounded-lg">{text}</pre>
{/if}
<h2>Clipboard Files</h2>
{#if files}
<ol class="list-decimal pl-6">
{#each files as file}
<li><code>{file}</code></li>
{/each}
</ol>
{/if}
<h2>Clipboard HTML</h2>
{#if htmlMonitorContent}
<div class="border p-2 rounded-lg">{@html htmlMonitorContent}</div>
{/if}
<h2>Clipboard RTF</h2>
{#if rtf}
<textarea class="w-full h-32 text-black" value={rtf} />
<!-- <pre class="border p-2 rounded-lg text-xs">{rtf}</pre> -->
{/if}
<h2>Clipboard Image</h2>
{#if base64Image}
<img width="300" src={`data:image/png;base64, ${base64Image}`} alt="" />
{/if}
</div>