mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-06-06 13:53:54 +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>;
|
||||
}
|
||||
}
|
||||
|
||||
type Platform =
|
||||
| "linux"
|
||||
@@ -63,7 +67,7 @@ const EOL = isWindows() ? "\r\n" : "\n";
|
||||
*
|
||||
*/
|
||||
async function platform(): Promise<Platform> {
|
||||
return invoke("plugin:os|platform");
|
||||
return window.__TAURI_INVOKE__("plugin:os|platform");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,7 +81,7 @@ async function platform(): Promise<Platform> {
|
||||
* @since 1.0.0
|
||||
*/
|
||||
async function version(): Promise<string> {
|
||||
return invoke("plugin:os|version");
|
||||
return window.__TAURI_INVOKE__("plugin:os|version");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,7 +95,7 @@ async function version(): Promise<string> {
|
||||
* @since 1.0.0
|
||||
*/
|
||||
async function type(): Promise<OsType> {
|
||||
return invoke("plugin:os|kind");
|
||||
return window.__TAURI_INVOKE__("plugin:os|kind");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -106,7 +110,7 @@ async function type(): Promise<OsType> {
|
||||
* @since 1.0.0
|
||||
*/
|
||||
async function arch(): Promise<Arch> {
|
||||
return invoke("plugin:os|arch");
|
||||
return window.__TAURI_INVOKE__("plugin:os|arch");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -120,7 +124,7 @@ async function arch(): Promise<Arch> {
|
||||
* @since 1.0.0
|
||||
*/
|
||||
async function tempdir(): Promise<string> {
|
||||
return invoke("plugin:os|tempdir");
|
||||
return window.__TAURI_INVOKE__("plugin:os|tempdir");
|
||||
}
|
||||
|
||||
export { EOL, platform, version, type, arch, tempdir };
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
if("__TAURI__"in window){var __TAURI_OS__=function(n){"use strict";const _=navigator.appVersion.includes("Win")?"\r\n":"\n";return n.EOL=_,n.arch=async function(){return window.__TAURI_INVOKE__("plugin:os|arch")},n.platform=async function(){return window.__TAURI_INVOKE__("plugin:os|platform")},n.tempdir=async function(){return window.__TAURI_INVOKE__("plugin:os|tempdir")},n.type=async function(){return window.__TAURI_INVOKE__("plugin:os|kind")},n.version=async function(){return window.__TAURI_INVOKE__("plugin:os|version")},n}({});Object.defineProperty(window.__TAURI__,"os",{value:__TAURI_OS__})}
|
||||
@@ -70,6 +70,7 @@ pub fn tempdir() -> PathBuf {
|
||||
|
||||
pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
||||
Builder::new("os")
|
||||
.js_init_script(include_str!("api-iife.js").to_string())
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
commands::platform,
|
||||
commands::version,
|
||||
|
||||
Reference in New Issue
Block a user