Skip to content

Commit beb6b13

Browse files
Fix invoke calls in dialog & shell init scripts (#675)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 5c13736 commit beb6b13

14 files changed

Lines changed: 112 additions & 66 deletions

File tree

.changes/fix-invoke-usage.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"shell": patch
3+
"dialog": patch
4+
---
5+
6+
Fix invoke usage.

.changes/fix-window-state-api.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"window-state-js": patch
3+
---
4+
5+
Fix usage of no longer available `__TAURI_METADATA__` API.

.github/workflows/test-rust.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,12 @@ jobs:
181181

182182
- uses: Swatinem/rust-cache@v2
183183
with:
184-
key: cache-${{ matrix.package }}
184+
key: cache-${{ matrix.package }}-${{ matrix.platform.target }}
185185

186186
- name: create dummy dist
187187
working-directory: examples/api
188188
run: mkdir dist
189189

190-
- name: Downgrade crates with MSRV conflict
191-
# The --precise flag can only be used once per invocation.
192-
run: |
193-
cargo update -p time@0.3.24 --precise 0.3.23
194-
195190
- name: install cross
196191
if: ${{ matrix.platform.runner == 'cross' }}
197192
run: cargo install cross --git https://github.com/cross-rs/cross
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
window.alert = function (message) {
5+
import { invoke } from "@tauri-apps/api/primitives";
6+
7+
window.alert = function (message: string) {
68
invoke("plugin:dialog|message", {
79
message: message.toString(),
810
});
911
};
1012

11-
window.confirm = function (message) {
13+
// @ts-expect-error tauri does not have sync IPC :(
14+
window.confirm = function (message: string) {
1215
return invoke("plugin:dialog|confirm", {
1316
message: message.toString(),
1417
});

plugins/dialog/rollup.config.mjs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,33 @@ import { readFileSync } from "fs";
22

33
import { createConfig } from "../../shared/rollup.config.mjs";
44

5-
export default createConfig({
5+
import typescript from "@rollup/plugin-typescript";
6+
import resolve from "@rollup/plugin-node-resolve";
7+
import terser from "@rollup/plugin-terser";
8+
9+
const config = createConfig({
610
input: "guest-js/index.ts",
711
pkg: JSON.parse(
812
readFileSync(new URL("./package.json", import.meta.url), "utf8"),
913
),
1014
external: [/^@tauri-apps\/api/],
1115
});
16+
17+
config.push({
18+
input: "guest-js/init.ts",
19+
output: {
20+
file: "src/init-iife.js",
21+
format: "iife",
22+
},
23+
plugins: [
24+
resolve(),
25+
typescript({
26+
sourceMap: false,
27+
declaration: false,
28+
declarationDir: undefined,
29+
}),
30+
terser(),
31+
],
32+
});
33+
34+
export default config;

plugins/dialog/src/init-iife.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/dialog/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
8484
// Dialogs are implemented natively on Android
8585
#[cfg(not(target_os = "android"))]
8686
{
87-
let mut init_script = include_str!("init.js").to_string();
87+
let mut init_script = include_str!("init-iife.js").to_string();
8888
init_script.push_str(include_str!("api-iife.js"));
8989
builder = builder.js_init_script(init_script);
9090
}

plugins/shell/guest-js/init.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2+
// SPDX-License-Identifier: Apache-2.0
3+
// SPDX-License-Identifier: MIT
4+
5+
import { invoke } from "@tauri-apps/api/primitives";
6+
7+
// open <a href="..."> links with the API
8+
function openLinks() {
9+
document.querySelector("body")?.addEventListener("click", function (e) {
10+
let target = e.target as HTMLElement;
11+
while (target != null) {
12+
if (target.matches("a")) {
13+
const t = target as HTMLAnchorElement;
14+
if (
15+
t.href &&
16+
["http://", "https://", "mailto:", "tel:"].some((v) =>
17+
t.href.startsWith(v),
18+
) &&
19+
t.target === "_blank"
20+
) {
21+
invoke("plugin:shell|open", {
22+
path: t.href,
23+
});
24+
e.preventDefault();
25+
}
26+
break;
27+
}
28+
target = target.parentElement as HTMLElement;
29+
}
30+
});
31+
}
32+
33+
if (
34+
document.readyState === "complete" ||
35+
document.readyState === "interactive"
36+
) {
37+
openLinks();
38+
} else {
39+
window.addEventListener("DOMContentLoaded", openLinks, true);
40+
}

plugins/shell/rollup.config.mjs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,33 @@ import { readFileSync } from "fs";
22

33
import { createConfig } from "../../shared/rollup.config.mjs";
44

5-
export default createConfig({
5+
import typescript from "@rollup/plugin-typescript";
6+
import resolve from "@rollup/plugin-node-resolve";
7+
import terser from "@rollup/plugin-terser";
8+
9+
const config = createConfig({
610
input: "guest-js/index.ts",
711
pkg: JSON.parse(
812
readFileSync(new URL("./package.json", import.meta.url), "utf8"),
913
),
1014
external: [/^@tauri-apps\/api/],
1115
});
16+
17+
config.push({
18+
input: "guest-js/init.ts",
19+
output: {
20+
file: "src/init-iife.js",
21+
format: "iife",
22+
},
23+
plugins: [
24+
resolve(),
25+
typescript({
26+
sourceMap: false,
27+
declaration: false,
28+
declarationDir: undefined,
29+
}),
30+
terser(),
31+
],
32+
});
33+
34+
export default config;

plugins/shell/src/init-iife.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)