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

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
pull/677/head
Mo 2 years ago committed by GitHub
parent 5c137365c6
commit beb6b139eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,6 @@
---
"shell": patch
"dialog": patch
---
Fix invoke usage.

@ -0,0 +1,5 @@
---
"window-state-js": patch
---
Fix usage of no longer available `__TAURI_METADATA__` API.

@ -181,17 +181,12 @@ jobs:
- uses: Swatinem/rust-cache@v2
with:
key: cache-${{ matrix.package }}
key: cache-${{ matrix.package }}-${{ matrix.platform.target }}
- name: create dummy dist
working-directory: examples/api
run: mkdir dist
- name: Downgrade crates with MSRV conflict
# The --precise flag can only be used once per invocation.
run: |
cargo update -p time@0.3.24 --precise 0.3.23
- name: install cross
if: ${{ matrix.platform.runner == 'cross' }}
run: cargo install cross --git https://github.com/cross-rs/cross

@ -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(),
});

@ -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;

@ -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()})}}();

@ -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);
}

@ -0,0 +1,40 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
import { invoke } from "@tauri-apps/api/primitives";
// open <a href="..."> links with the API
function openLinks() {
document.querySelector("body")?.addEventListener("click", function (e) {
let target = e.target as HTMLElement;
while (target != null) {
if (target.matches("a")) {
const t = target as HTMLAnchorElement;
if (
t.href &&
["http://", "https://", "mailto:", "tel:"].some((v) =>
t.href.startsWith(v),
) &&
t.target === "_blank"
) {
invoke("plugin:shell|open", {
path: t.href,
});
e.preventDefault();
}
break;
}
target = target.parentElement as HTMLElement;
}
});
}
if (
document.readyState === "complete" ||
document.readyState === "interactive"
) {
openLinks();
} else {
window.addEventListener("DOMContentLoaded", openLinks, true);
}

@ -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;

@ -0,0 +1 @@
!function(){"use strict";var e=Object.defineProperty,t=(e,t,n)=>{if(!t.has(e))throw TypeError("Cannot "+n)},n=(e,n,r)=>(t(e,n,"read from private field"),r?r.call(e):n.get(e));function r(e,t=!1){return window.__TAURI_INTERNALS__.transformCallback(e,t)}((t,n)=>{for(var r in n)e(t,r,{get:n[r],enumerable:!0})})({},{Channel:()=>i,PluginListener:()=>s,addPluginListener:()=>o,convertFileSrc:()=>c,invoke:()=>l,transformCallback:()=>r});var a,i=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,a,(()=>{})),this.id=r((e=>{n(this,a).call(this,e)}))}set onmessage(e){var n,r,i,s;i=e,t(n=this,r=a,"write to private field"),s?s.call(n,i):r.set(n,i)}get onmessage(){return n(this,a)}toJSON(){return`__CHANNEL__:${this.id}`}};a=new WeakMap;var s=class{constructor(e,t,n){this.plugin=e,this.event=t,this.channelId=n}async unregister(){return l(`plugin:${this.plugin}|remove_listener`,{event:this.event,channelId:this.channelId})}};async function o(e,t,n){let r=new i;return r.onmessage=n,l(`plugin:${e}|register_listener`,{event:t,handler:r}).then((()=>new s(e,t,r.id)))}async function l(e,t={},n){return window.__TAURI_INTERNALS__.invoke(e,t,n)}function c(e,t="asset"){return window.__TAURI_INTERNALS__.convertFileSrc(e,t)}function h(){var e;null===(e=document.querySelector("body"))||void 0===e||e.addEventListener("click",(function(e){let t=e.target;for(;null!=t;){if(t.matches("a")){const n=t;n.href&&["http://","https://","mailto:","tel:"].some((e=>n.href.startsWith(e)))&&"_blank"===n.target&&(l("plugin:shell|open",{path:n.href}),e.preventDefault());break}t=t.parentElement}}))}"complete"===document.readyState||"interactive"===document.readyState?h():window.addEventListener("DOMContentLoaded",h,!0)}();

@ -1,39 +0,0 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
(function () {
// open <a href="..."> links with the API
function openLinks() {
document.querySelector("body").addEventListener("click", function (e) {
var target = e.target;
while (target != null) {
if (target.matches("a")) {
if (
target.href &&
["http://", "https://", "mailto:", "tel:"].some((v) =>
target.href.startsWith(v),
) &&
target.target === "_blank"
) {
invoke("plugin:shell|open", {
path: target.href,
});
e.preventDefault();
}
break;
}
target = target.parentElement;
}
});
}
if (
document.readyState === "complete" ||
document.readyState === "interactive"
) {
openLinks();
} else {
window.addEventListener("DOMContentLoaded", openLinks, true);
}
})();

@ -78,7 +78,7 @@ impl<R: Runtime, T: Manager<R>> ShellExt<R> for T {
}
pub fn init<R: Runtime>() -> TauriPlugin<R, Option<Config>> {
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::<R, Option<Config>>::new("shell")

@ -3,19 +3,7 @@
// SPDX-License-Identifier: MIT
import { invoke } from "@tauri-apps/api/primitives";
interface WindowDef {
label: string;
}
declare global {
interface Window {
__TAURI_METADATA__: {
__windows: WindowDef[];
__currentWindow: WindowDef;
};
}
}
import { getCurrent } from "@tauri-apps/api/window";
export enum StateFlags {
SIZE = 1 << 0,
@ -50,7 +38,7 @@ async function restoreState(label: string, flags: StateFlags): Promise<void> {
* Restore the state for the current window from disk.
*/
async function restoreStateCurrent(flags: StateFlags): Promise<void> {
return restoreState(window.__TAURI_METADATA__.__currentWindow.label, flags);
return restoreState(getCurrent().label, flags);
}
export { restoreState, restoreStateCurrent, saveWindowState };

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save