feat(plugins): inject API on window.__TAURI__ (#383)

This commit is contained in:
Lucas Fernandes Nogueira
2023-05-23 10:20:14 -07:00
committed by GitHub
parent 3c8577bc9a
commit b131bc8f7c
78 changed files with 754 additions and 337 deletions
+10 -6
View File
@@ -2,7 +2,11 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
import { invoke } from "@tauri-apps/api/tauri";
declare global {
interface Window {
__TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
}
}
interface FileResponse {
base64Data?: string;
@@ -169,7 +173,7 @@ async function open(
Object.freeze(options);
}
return invoke("plugin:dialog|open", { options });
return window.__TAURI_INVOKE__("plugin:dialog|open", { options });
}
/**
@@ -201,7 +205,7 @@ async function save(options: SaveDialogOptions = {}): Promise<string | null> {
Object.freeze(options);
}
return invoke("plugin:dialog|save", { options });
return window.__TAURI_INVOKE__("plugin:dialog|save", { options });
}
/**
@@ -226,7 +230,7 @@ async function message(
options?: string | MessageDialogOptions
): Promise<void> {
const opts = typeof options === "string" ? { title: options } : options;
return invoke("plugin:dialog|message", {
return window.__TAURI_INVOKE__("plugin:dialog|message", {
message: message.toString(),
title: opts?.title?.toString(),
type_: opts?.type,
@@ -255,7 +259,7 @@ async function ask(
options?: string | ConfirmDialogOptions
): Promise<boolean> {
const opts = typeof options === "string" ? { title: options } : options;
return invoke("plugin:dialog|ask", {
return window.__TAURI_INVOKE__("plugin:dialog|ask", {
message: message.toString(),
title: opts?.title?.toString(),
type_: opts?.type,
@@ -285,7 +289,7 @@ async function confirm(
options?: string | ConfirmDialogOptions
): Promise<boolean> {
const opts = typeof options === "string" ? { title: options } : options;
return invoke("plugin:dialog|confirm", {
return window.__TAURI_INVOKE__("plugin:dialog|confirm", {
message: message.toString(),
title: opts?.title?.toString(),
type_: opts?.type,
+1
View File
@@ -0,0 +1 @@
if("__TAURI__"in window){var __TAURI_DIALOG__=function(n){"use strict";return n.ask=async function(n,o){var t,i,l,e,u;const _="string"==typeof o?{title:o}:o;return window.__TAURI_INVOKE__("plugin:dialog|ask",{message:n.toString(),title:null===(t=null==_?void 0:_.title)||void 0===t?void 0:t.toString(),type_:null==_?void 0:_.type,okButtonLabel:null!==(l=null===(i=null==_?void 0:_.okLabel)||void 0===i?void 0:i.toString())&&void 0!==l?l:"Yes",cancelButtonLabel:null!==(u=null===(e=null==_?void 0:_.cancelLabel)||void 0===e?void 0:e.toString())&&void 0!==u?u:"No"})},n.confirm=async function(n,o){var t,i,l,e,u;const _="string"==typeof o?{title:o}:o;return window.__TAURI_INVOKE__("plugin:dialog|confirm",{message:n.toString(),title:null===(t=null==_?void 0:_.title)||void 0===t?void 0:t.toString(),type_:null==_?void 0:_.type,okButtonLabel:null!==(l=null===(i=null==_?void 0:_.okLabel)||void 0===i?void 0:i.toString())&&void 0!==l?l:"Ok",cancelButtonLabel:null!==(u=null===(e=null==_?void 0:_.cancelLabel)||void 0===e?void 0:e.toString())&&void 0!==u?u:"Cancel"})},n.message=async function(n,o){var t,i;const l="string"==typeof o?{title:o}:o;return window.__TAURI_INVOKE__("plugin:dialog|message",{message:n.toString(),title:null===(t=null==l?void 0:l.title)||void 0===t?void 0:t.toString(),type_:null==l?void 0:l.type,okButtonLabel:null===(i=null==l?void 0:l.okLabel)||void 0===i?void 0:i.toString()})},n.open=async function(n={}){return"object"==typeof n&&Object.freeze(n),window.__TAURI_INVOKE__("plugin:dialog|open",{options:n})},n.save=async function(n={}){return"object"==typeof n&&Object.freeze(n),window.__TAURI_INVOKE__("plugin:dialog|save",{options:n})},n}({});Object.defineProperty(window.__TAURI__,"dialog",{value:__TAURI_DIALOG__})}
+7 -1
View File
@@ -75,7 +75,13 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
// Dialogs are implemented natively on Android
#[cfg(not(target_os = "android"))]
{
builder = builder.js_init_script(include_str!("init.js").to_string());
let mut init_script = include_str!("init.js").to_string();
init_script.push_str(include_str!("api-iife.js"));
builder = builder.js_init_script(init_script);
}
#[cfg(target_os = "android")]
{
builder = builder.js_init_script(include_str!("api-iife.js").to_string());
}
builder