mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-07-10 16:27:05 +02:00
feat(plugins): inject API on window.__TAURI__ (#383)
This commit is contained in:
committed by
GitHub
parent
3c8577bc9a
commit
b131bc8f7c
@@ -2,9 +2,14 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import { invoke } from "@tauri-apps/api/tauri";
|
||||
import { listen, UnlistenFn } from "@tauri-apps/api/event";
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
__TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
|
||||
}
|
||||
}
|
||||
|
||||
interface ChangePayload<T> {
|
||||
path: string;
|
||||
key: string;
|
||||
@@ -28,7 +33,7 @@ export class Store {
|
||||
* @returns
|
||||
*/
|
||||
async set(key: string, value: unknown): Promise<void> {
|
||||
return await invoke("plugin:store|set", {
|
||||
return await window.__TAURI_INVOKE__("plugin:store|set", {
|
||||
path: this.path,
|
||||
key,
|
||||
value,
|
||||
@@ -42,7 +47,7 @@ export class Store {
|
||||
* @returns
|
||||
*/
|
||||
async get<T>(key: string): Promise<T | null> {
|
||||
return await invoke("plugin:store|get", {
|
||||
return await window.__TAURI_INVOKE__("plugin:store|get", {
|
||||
path: this.path,
|
||||
key,
|
||||
});
|
||||
@@ -55,7 +60,7 @@ export class Store {
|
||||
* @returns
|
||||
*/
|
||||
async has(key: string): Promise<boolean> {
|
||||
return await invoke("plugin:store|has", {
|
||||
return await window.__TAURI_INVOKE__("plugin:store|has", {
|
||||
path: this.path,
|
||||
key,
|
||||
});
|
||||
@@ -68,7 +73,7 @@ export class Store {
|
||||
* @returns
|
||||
*/
|
||||
async delete(key: string): Promise<boolean> {
|
||||
return await invoke("plugin:store|delete", {
|
||||
return await window.__TAURI_INVOKE__("plugin:store|delete", {
|
||||
path: this.path,
|
||||
key,
|
||||
});
|
||||
@@ -81,7 +86,7 @@ export class Store {
|
||||
* @returns
|
||||
*/
|
||||
async clear(): Promise<void> {
|
||||
return await invoke("plugin:store|clear", {
|
||||
return await window.__TAURI_INVOKE__("plugin:store|clear", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
@@ -93,7 +98,7 @@ export class Store {
|
||||
* @returns
|
||||
*/
|
||||
async reset(): Promise<void> {
|
||||
return await invoke("plugin:store|reset", {
|
||||
return await window.__TAURI_INVOKE__("plugin:store|reset", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
@@ -104,7 +109,7 @@ export class Store {
|
||||
* @returns
|
||||
*/
|
||||
async keys(): Promise<string[]> {
|
||||
return await invoke("plugin:store|keys", {
|
||||
return await window.__TAURI_INVOKE__("plugin:store|keys", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
@@ -115,7 +120,7 @@ export class Store {
|
||||
* @returns
|
||||
*/
|
||||
async values<T>(): Promise<T[]> {
|
||||
return await invoke("plugin:store|values", {
|
||||
return await window.__TAURI_INVOKE__("plugin:store|values", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
@@ -126,7 +131,7 @@ export class Store {
|
||||
* @returns
|
||||
*/
|
||||
async entries<T>(): Promise<Array<[key: string, value: T]>> {
|
||||
return await invoke("plugin:store|entries", {
|
||||
return await window.__TAURI_INVOKE__("plugin:store|entries", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
@@ -137,7 +142,7 @@ export class Store {
|
||||
* @returns
|
||||
*/
|
||||
async length(): Promise<number> {
|
||||
return await invoke("plugin:store|length", {
|
||||
return await window.__TAURI_INVOKE__("plugin:store|length", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
@@ -151,7 +156,7 @@ export class Store {
|
||||
* @returns
|
||||
*/
|
||||
async load(): Promise<void> {
|
||||
return await invoke("plugin:store|load", {
|
||||
return await window.__TAURI_INVOKE__("plugin:store|load", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
@@ -164,7 +169,7 @@ export class Store {
|
||||
* @returns
|
||||
*/
|
||||
async save(): Promise<void> {
|
||||
return await invoke("plugin:store|save", {
|
||||
return await window.__TAURI_INVOKE__("plugin:store|save", {
|
||||
path: this.path,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
if("__TAURI__"in window){var __TAURI_STORE__=function(e){"use strict";var t=Object.defineProperty,n=(e,n)=>{for(var a in n)t(e,a,{get:n[a],enumerable:!0})},a=(e,t,n)=>{if(!t.has(e))throw TypeError("Cannot "+n)},r=(e,t,n)=>(a(e,t,"read from private field"),n?n.call(e):t.get(e)),i=(e,t,n,r)=>(a(e,t,"write to private field"),r?r.call(e,n):t.set(e,n),n);function s(e,t=!1){let n=window.crypto.getRandomValues(new Uint32Array(1))[0],a=`_${n}`;return Object.defineProperty(window,a,{value:n=>(t&&Reflect.deleteProperty(window,a),e?.(n)),writable:!1,configurable:!0}),n}n({},{Channel:()=>o,PluginListener:()=>l,addPluginListener:()=>u,convertFileSrc:()=>h,invoke:()=>c,transformCallback:()=>s});var _,o=class{constructor(){this.__TAURI_CHANNEL_MARKER__=!0,((e,t,n)=>{if(t.has(e))throw TypeError("Cannot add the same private member more than once");t instanceof WeakSet?t.add(e):t.set(e,n)})(this,_,(()=>{})),this.id=s((e=>{r(this,_).call(this,e)}))}set onmessage(e){i(this,_,e)}get onmessage(){return r(this,_)}toJSON(){return`__CHANNEL__:${this.id}`}};_=new WeakMap;var l=class{constructor(e,t,n){this.plugin=e,this.event=t,this.channelId=n}async unregister(){return c(`plugin:${this.plugin}|remove_listener`,{event:this.event,channelId:this.channelId})}};async function u(e,t,n){let a=new o;return a.onmessage=n,c(`plugin:${e}|register_listener`,{event:t,handler:a}).then((()=>new l(e,t,a.id)))}async function c(e,t={}){return new Promise(((n,a)=>{let r=s((e=>{n(e),Reflect.deleteProperty(window,`_${i}`)}),!0),i=s((e=>{a(e),Reflect.deleteProperty(window,`_${r}`)}),!0);window.__TAURI_IPC__({cmd:e,callback:r,error:i,...t})}))}function h(e,t="asset"){let n=encodeURIComponent(e);return navigator.userAgent.includes("Windows")?`https://${t}.localhost/${n}`:`${t}://localhost/${n}`}async function d(e,t){await c("plugin:event|unlisten",{event:e,eventId:t})}async function p(e,t,n){return c("plugin:event|listen",{event:e,windowLabel:t,handler:s(n)}).then((t=>async()=>d(e,t)))}n({},{TauriEvent:()=>y,emit:()=>E,listen:()=>I,once:()=>g});var w,y=((w=y||{}).WINDOW_RESIZED="tauri://resize",w.WINDOW_MOVED="tauri://move",w.WINDOW_CLOSE_REQUESTED="tauri://close-requested",w.WINDOW_CREATED="tauri://window-created",w.WINDOW_DESTROYED="tauri://destroyed",w.WINDOW_FOCUS="tauri://focus",w.WINDOW_BLUR="tauri://blur",w.WINDOW_SCALE_FACTOR_CHANGED="tauri://scale-change",w.WINDOW_THEME_CHANGED="tauri://theme-changed",w.WINDOW_FILE_DROP="tauri://file-drop",w.WINDOW_FILE_DROP_HOVER="tauri://file-drop-hover",w.WINDOW_FILE_DROP_CANCELLED="tauri://file-drop-cancelled",w.MENU="tauri://menu",w);async function I(e,t){return p(e,null,t)}async function g(e,t){return async function(e,t,n){return p(e,t,(t=>{n(t),d(e,t.id).catch((()=>{}))}))}(e,null,t)}async function E(e,t){return async function(e,t,n){await c("plugin:event|emit",{event:e,windowLabel:t,payload:n})}(e,void 0,t)}return e.Store=class{constructor(e){this.path=e}async set(e,t){return await window.__TAURI_INVOKE__("plugin:store|set",{path:this.path,key:e,value:t})}async get(e){return await window.__TAURI_INVOKE__("plugin:store|get",{path:this.path,key:e})}async has(e){return await window.__TAURI_INVOKE__("plugin:store|has",{path:this.path,key:e})}async delete(e){return await window.__TAURI_INVOKE__("plugin:store|delete",{path:this.path,key:e})}async clear(){return await window.__TAURI_INVOKE__("plugin:store|clear",{path:this.path})}async reset(){return await window.__TAURI_INVOKE__("plugin:store|reset",{path:this.path})}async keys(){return await window.__TAURI_INVOKE__("plugin:store|keys",{path:this.path})}async values(){return await window.__TAURI_INVOKE__("plugin:store|values",{path:this.path})}async entries(){return await window.__TAURI_INVOKE__("plugin:store|entries",{path:this.path})}async length(){return await window.__TAURI_INVOKE__("plugin:store|length",{path:this.path})}async load(){return await window.__TAURI_INVOKE__("plugin:store|load",{path:this.path})}async save(){return await window.__TAURI_INVOKE__("plugin:store|save",{path:this.path})}async onKeyChange(e,t){return await I("store://change",(n=>{n.payload.path===this.path&&n.payload.key===e&&t(n.payload.value)}))}async onChange(e){return await I("store://change",(t=>{t.payload.path===this.path&&e(t.payload.key,t.payload.value)}))}},e}({});Object.defineProperty(window.__TAURI__,"store",{value:__TAURI_STORE__})}
|
||||
@@ -278,6 +278,7 @@ impl<R: Runtime> Builder<R> {
|
||||
/// ```
|
||||
pub fn build(mut self) -> TauriPlugin<R> {
|
||||
plugin::Builder::new("store")
|
||||
.js_init_script(include_str!("api-iife.js").to_string())
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
set, get, has, delete, clear, reset, keys, values, length, entries, load, save
|
||||
])
|
||||
|
||||
Reference in New Issue
Block a user