Fix invoke calls in dialog & shell init scripts (#675)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
Mo
2023-10-22 16:39:40 +03:30
committed by GitHub
parent 5c137365c6
commit beb6b139eb
14 changed files with 112 additions and 66 deletions
@@ -2,13 +2,16 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
window.alert = function (message) {
import { invoke } from "@tauri-apps/api/primitives";
window.alert = function (message: string) {
invoke("plugin:dialog|message", {
message: message.toString(),
});
};
window.confirm = function (message) {
// @ts-expect-error tauri does not have sync IPC :(
window.confirm = function (message: string) {
return invoke("plugin:dialog|confirm", {
message: message.toString(),
});
+24 -1
View File
@@ -2,10 +2,33 @@ import { readFileSync } from "fs";
import { createConfig } from "../../shared/rollup.config.mjs";
export default createConfig({
import typescript from "@rollup/plugin-typescript";
import resolve from "@rollup/plugin-node-resolve";
import terser from "@rollup/plugin-terser";
const config = createConfig({
input: "guest-js/index.ts",
pkg: JSON.parse(
readFileSync(new URL("./package.json", import.meta.url), "utf8"),
),
external: [/^@tauri-apps\/api/],
});
config.push({
input: "guest-js/init.ts",
output: {
file: "src/init-iife.js",
format: "iife",
},
plugins: [
resolve(),
typescript({
sourceMap: false,
declaration: false,
declarationDir: undefined,
}),
terser(),
],
});
export default config;
+1
View File
@@ -0,0 +1 @@
!function(){"use strict";var e=Object.defineProperty,n=(e,n,t)=>{if(!n.has(e))throw TypeError("Cannot "+t)},t=(e,t,r)=>(n(e,t,"read from private field"),r?r.call(e):t.get(e));function r(e,n=!1){return window.__TAURI_INTERNALS__.transformCallback(e,n)}((n,t)=>{for(var r in t)e(n,r,{get:t[r],enumerable:!0})})({},{Channel:()=>s,PluginListener:()=>a,addPluginListener:()=>o,convertFileSrc:()=>c,invoke:()=>l,transformCallback:()=>r});var i,s=class{constructor(){this.__TAURI_CHANNEL_MARKER__=!0,((e,n,t)=>{if(n.has(e))throw TypeError("Cannot add the same private member more than once");n instanceof WeakSet?n.add(e):n.set(e,t)})(this,i,(()=>{})),this.id=r((e=>{t(this,i).call(this,e)}))}set onmessage(e){var t,r,s,a;s=e,n(t=this,r=i,"write to private field"),a?a.call(t,s):r.set(t,s)}get onmessage(){return t(this,i)}toJSON(){return`__CHANNEL__:${this.id}`}};i=new WeakMap;var a=class{constructor(e,n,t){this.plugin=e,this.event=n,this.channelId=t}async unregister(){return l(`plugin:${this.plugin}|remove_listener`,{event:this.event,channelId:this.channelId})}};async function o(e,n,t){let r=new s;return r.onmessage=t,l(`plugin:${e}|register_listener`,{event:n,handler:r}).then((()=>new a(e,n,r.id)))}async function l(e,n={},t){return window.__TAURI_INTERNALS__.invoke(e,n,t)}function c(e,n="asset"){return window.__TAURI_INTERNALS__.convertFileSrc(e,n)}window.alert=function(e){l("plugin:dialog|message",{message:e.toString()})},window.confirm=function(e){return l("plugin:dialog|confirm",{message:e.toString()})}}();
+1 -1
View File
@@ -84,7 +84,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
// Dialogs are implemented natively on Android
#[cfg(not(target_os = "android"))]
{
let mut init_script = include_str!("init.js").to_string();
let mut init_script = include_str!("init-iife.js").to_string();
init_script.push_str(include_str!("api-iife.js"));
builder = builder.js_init_script(init_script);
}