Skip to content

Commit 7851c5f

Browse files
committed
Update doc
1 parent f80df71 commit 7851c5f

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

README.md

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
## Installation
2020

2121
> If you are installing from npm and crate.io package registry, make sure the versions for both packages are the same, otherwise, the API may not match.
22-
>
22+
>
2323
> I will make sure the latest version is published to both npm and crates.io.
2424
2525
### Short Instructions
@@ -38,7 +38,7 @@ You need to make sure the package versions is compatible with the correct versio
3838
- Tauri 1.x: package and crate version should start with 1.x
3939
- Tauri 2.x: package and crate version should start with 2.x
4040
- Or you can install with git url and branch `v2`
41-
41+
4242
```toml
4343
tauri-plugin-clipboard = { git = "https://github.com/CrossCopy/tauri-plugin-clipboard", branch = "v2" }
4444
```
@@ -48,7 +48,6 @@ You need to make sure the package versions is compatible with the correct versio
4848
- Check crate versions at https://crates.io/crates/tauri-plugin-clipboard/versions
4949
- Check npm package verison at https://www.npmjs.com/package/tauri-plugin-clipboard-api?activeTab=versions
5050

51-
5251
<details>
5352
<summary>More Installation Options</summary>
5453

@@ -271,7 +270,7 @@ Difference between URI and no-URI is that URI starts with `files://` on Linux an
271270

272271
## Notes
273272

274-
> You don't really need to read this section if you are just using the plugin.
273+
### Monitor
275274

276275
The logic of tauri's listen API is encapsulated in `onTextUpdate`, `onFilesUpdate`, `startListening`.
277276

@@ -308,7 +307,45 @@ The returned unlisten function from `startListening` also does two things:
308307

309308
For more details read the source code from [./webview-src/api.ts](./webview-src/api.ts).
310309

311-
## Note
310+
#### Break On Type
311+
312+
`listenToClipboard` and `startListening` function takes an optional parameter `breakOnType` with type
313+
314+
```ts
315+
type BreakOnTypeInput = {
316+
text?: boolean;
317+
html?: boolean;
318+
rtf?: boolean;
319+
image?: boolean;
320+
files?: boolean;
321+
};
322+
```
323+
324+
This parameter is used to break the monitor event emission when the clipboard content type matches the type. For example, when HTML content is copied, text update event will also be emitted.
325+
326+
The order of type checking is `file -> image -> html -> rtf -> text`.
327+
328+
The default `breakOnType` is
329+
330+
```ts
331+
{
332+
text: false,
333+
html: false,
334+
rtf: false,
335+
image: false,
336+
files: true
337+
}
338+
```
339+
340+
When files are copied, text is also updated. By default, text update event will not be emitted when files are copied.
341+
342+
If you don't want to listen to text update when HTML content is copied, you can set `breakOnType` to `{ html: true }`.
343+
344+
Read more
345+
- https://crosscopy.github.io/tauri-plugin-clipboard/functions/listenToClipboard.html
346+
- https://crosscopy.github.io/tauri-plugin-clipboard/functions/startListening.html
347+
348+
### Image
312349

313350
The base64 image string can be converted to `Uint8Array` and written to file system using tauri's fs API. (We also provide a `readImageBinary` function to read image as binary data (`Uint8Array` is one of the available return type).
314351

webview-src/api.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,17 +248,20 @@ export const DefaultBreakOn: BreakOnType = {
248248

249249
/**
250250
* Listen to "plugin:clipboard://clipboard-monitor/update" from Tauri core.
251-
* But this event doesn't tell us whether text or image is updated,
252-
* so this function will detect which is changed and emit the corresponding event
253-
* Event constant variables: TEXT_CHANGED or IMAGE_CHANGED
251+
* The corresponding clipboard type event will be emitted when there is clipboard update.
252+
* Multiple types of clipboard data can be copied at the same time. e.g. When HTML is copied, text is also updated. files copy also update text.
253+
* There is an optional `breakOn` argument to control whether to break event emitting for other types.
254+
* Type checking order: files -> image -> html -> rtf -> text
255+
* If you don't want text update triggered when html is copied, pass breakOn argument `{html: true}`
256+
* By default, files is set to true. When files are copied, text event won't be triggered. If you want text event to be triggered, pass `{files: false}`
257+
*
258+
* Due to the order of checking, the text field doesn't matter as no other types are checked after text.
254259
* @returns unlisten function
255260
*/
256261
export function listenToClipboard(
257262
breakOn: BreakOnTypeInput = DefaultBreakOn
258263
): Promise<UnlistenFn> {
259264
const parseBreakOn = BreakOnType.parse(breakOn);
260-
console.log(parseBreakOn);
261-
262265
return listen(MONITOR_UPDATE_EVENT, async (e) => {
263266
if (e.payload === "clipboard update") {
264267
if (await hasFiles()) {
@@ -394,6 +397,20 @@ export async function listenToMonitorStatusUpdate(
394397
});
395398
}
396399

400+
/**
401+
* Start monitor service thread with `startMonitor()` and then run `listenToClipboard()`
402+
* The corresponding clipboard type event will be emitted when there is clipboard update.
403+
* Use `onImageUpdate()`, `onTextUpdate()`, `onHTMLUpdate()`, `onFilesUpdate()`, `onRTFUpdate()` to listen to the event after calling this function.
404+
*
405+
* Multiple types of clipboard data can be copied at the same time. e.g. When HTML is copied, text is also updated. files copy also update text.
406+
* There is an optional `breakOn` argument to control whether to break event emitting for other types.
407+
* Type checking order: files -> image -> html -> rtf -> text
408+
* If you don't want text update triggered when html is copied, pass breakOn argument `{html: true}`
409+
* By default, files is set to true. When files are copied, text event won't be triggered. If you want text event to be triggered, pass `{files: false}`
410+
*
411+
* Due to the order of checking, the text field doesn't matter as no other types are checked after text.
412+
* @returns unlisten function
413+
*/
397414
export function startListening(
398415
breakOn: BreakOnTypeInput = DefaultBreakOn
399416
): Promise<() => Promise<void>> {

0 commit comments

Comments
 (0)