mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-07-08 16:07:50 +02:00
feat(plugins): inject API on window.__TAURI__ (#383)
This commit is contained in:
committed by
GitHub
parent
3c8577bc9a
commit
b131bc8f7c
@@ -8,7 +8,11 @@
|
||||
* @module
|
||||
*/
|
||||
|
||||
import { invoke } from "@tauri-apps/api/tauri";
|
||||
declare global {
|
||||
interface Window {
|
||||
__TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
|
||||
}
|
||||
}
|
||||
|
||||
interface Clip<K, T> {
|
||||
kind: K;
|
||||
@@ -34,7 +38,7 @@ async function writeText(
|
||||
text: string,
|
||||
opts?: { label?: string }
|
||||
): Promise<void> {
|
||||
return invoke("plugin:clipboard|write", {
|
||||
return window.__TAURI_INVOKE__("plugin:clipboard|write", {
|
||||
data: {
|
||||
kind: "PlainText",
|
||||
options: {
|
||||
@@ -55,7 +59,9 @@ async function writeText(
|
||||
* @since 1.0.0.
|
||||
*/
|
||||
async function readText(): Promise<string> {
|
||||
const kind: ClipResponse = await invoke("plugin:clipboard|read");
|
||||
const kind: ClipResponse = await window.__TAURI_INVOKE__(
|
||||
"plugin:clipboard|read"
|
||||
);
|
||||
return kind.options;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
if("__TAURI__"in window){var __TAURI_CLIPBOARD__=function(_){"use strict";return _.readText=async function(){return(await window.__TAURI_INVOKE__("plugin:clipboard|read")).options},_.writeText=async function(_,i){return window.__TAURI_INVOKE__("plugin:clipboard|write",{data:{kind:"PlainText",options:{label:null==i?void 0:i.label,text:_}}})},_}({});Object.defineProperty(window.__TAURI__,"clipboard",{value:__TAURI_CLIPBOARD__})}
|
||||
@@ -39,6 +39,7 @@ impl<R: Runtime, T: Manager<R>> crate::ClipboardExt<R> for T {
|
||||
/// Initializes the plugin.
|
||||
pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
||||
Builder::new("clipboard")
|
||||
.js_init_script(include_str!("api-iife.js").to_string())
|
||||
.invoke_handler(tauri::generate_handler![commands::write, commands::read])
|
||||
.setup(|app, api| {
|
||||
#[cfg(mobile)]
|
||||
|
||||
Reference in New Issue
Block a user