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
@@ -45,9 +45,14 @@
|
||||
* @module
|
||||
*/
|
||||
|
||||
import { invoke } from "@tauri-apps/api/tauri";
|
||||
import { BaseDirectory } from "@tauri-apps/api/path";
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
__TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
|
||||
}
|
||||
}
|
||||
|
||||
interface Permissions {
|
||||
/**
|
||||
* `true` if these permissions describe a readonly (unwritable) file.
|
||||
@@ -226,7 +231,7 @@ async function readTextFile(
|
||||
filePath: string,
|
||||
options: FsOptions = {}
|
||||
): Promise<string> {
|
||||
return await invoke("plugin:fs|read_text_file", {
|
||||
return await window.__TAURI_INVOKE__("plugin:fs|read_text_file", {
|
||||
path: filePath,
|
||||
options,
|
||||
});
|
||||
@@ -247,7 +252,7 @@ async function readBinaryFile(
|
||||
filePath: string,
|
||||
options: FsOptions = {}
|
||||
): Promise<Uint8Array> {
|
||||
const arr = await invoke<number[]>("plugin:fs|read_file", {
|
||||
const arr = await window.__TAURI_INVOKE__<number[]>("plugin:fs|read_file", {
|
||||
path: filePath,
|
||||
options,
|
||||
});
|
||||
@@ -323,7 +328,7 @@ async function writeTextFile(
|
||||
fileOptions = contents;
|
||||
}
|
||||
|
||||
return await invoke("plugin:fs|write_file", {
|
||||
return await window.__TAURI_INVOKE__("plugin:fs|write_file", {
|
||||
path: file.path,
|
||||
contents: Array.from(new TextEncoder().encode(file.contents)),
|
||||
options: fileOptions,
|
||||
@@ -405,7 +410,7 @@ async function writeBinaryFile(
|
||||
file.contents = contents ?? [];
|
||||
}
|
||||
|
||||
return await invoke("plugin:fs|write_binary_file", {
|
||||
return await window.__TAURI_INVOKE__("plugin:fs|write_binary_file", {
|
||||
path: file.path,
|
||||
contents: Array.from(
|
||||
file.contents instanceof ArrayBuffer
|
||||
@@ -440,7 +445,7 @@ async function readDir(
|
||||
dir: string,
|
||||
options: FsDirOptions = {}
|
||||
): Promise<FileEntry[]> {
|
||||
return await invoke("plugin:fs|read_dir", {
|
||||
return await window.__TAURI_INVOKE__("plugin:fs|read_dir", {
|
||||
path: dir,
|
||||
options,
|
||||
});
|
||||
@@ -465,7 +470,7 @@ async function createDir(
|
||||
dir: string,
|
||||
options: FsDirOptions = {}
|
||||
): Promise<void> {
|
||||
return await invoke("plugin:fs|create_dir", {
|
||||
return await window.__TAURI_INVOKE__("plugin:fs|create_dir", {
|
||||
path: dir,
|
||||
options,
|
||||
});
|
||||
@@ -489,7 +494,7 @@ async function removeDir(
|
||||
dir: string,
|
||||
options: FsDirOptions = {}
|
||||
): Promise<void> {
|
||||
return await invoke("plugin:fs|remove_dir", {
|
||||
return await window.__TAURI_INVOKE__("plugin:fs|remove_dir", {
|
||||
path: dir,
|
||||
options,
|
||||
});
|
||||
@@ -513,7 +518,7 @@ async function copyFile(
|
||||
destination: string,
|
||||
options: FsOptions = {}
|
||||
): Promise<void> {
|
||||
return await invoke("plugin:fs|copy_file", {
|
||||
return await window.__TAURI_INVOKE__("plugin:fs|copy_file", {
|
||||
source,
|
||||
destination,
|
||||
options,
|
||||
@@ -537,7 +542,7 @@ async function removeFile(
|
||||
file: string,
|
||||
options: FsOptions = {}
|
||||
): Promise<void> {
|
||||
return await invoke("plugin:fs|remove_file", {
|
||||
return await window.__TAURI_INVOKE__("plugin:fs|remove_file", {
|
||||
path: file,
|
||||
options,
|
||||
});
|
||||
@@ -561,7 +566,7 @@ async function renameFile(
|
||||
newPath: string,
|
||||
options: FsOptions = {}
|
||||
): Promise<void> {
|
||||
return await invoke("plugin:fs|rename_file", {
|
||||
return await window.__TAURI_INVOKE__("plugin:fs|rename_file", {
|
||||
oldPath,
|
||||
newPath,
|
||||
options,
|
||||
@@ -580,7 +585,7 @@ async function renameFile(
|
||||
* @since 1.0.0
|
||||
*/
|
||||
async function exists(path: string): Promise<boolean> {
|
||||
return await invoke("plugin:fs|exists", { path });
|
||||
return await window.__TAURI_INVOKE__("plugin:fs|exists", { path });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -589,17 +594,19 @@ async function exists(path: string): Promise<boolean> {
|
||||
* @since 1.0.0
|
||||
*/
|
||||
async function metadata(path: string): Promise<Metadata> {
|
||||
return await invoke<BackendMetadata>("plugin:fs|metadata", {
|
||||
path,
|
||||
}).then((metadata) => {
|
||||
const { accessedAtMs, createdAtMs, modifiedAtMs, ...data } = metadata;
|
||||
return {
|
||||
accessedAt: new Date(accessedAtMs),
|
||||
createdAt: new Date(createdAtMs),
|
||||
modifiedAt: new Date(modifiedAtMs),
|
||||
...data,
|
||||
};
|
||||
});
|
||||
return await window
|
||||
.__TAURI_INVOKE__<BackendMetadata>("plugin:fs|metadata", {
|
||||
path,
|
||||
})
|
||||
.then((metadata) => {
|
||||
const { accessedAtMs, createdAtMs, modifiedAtMs, ...data } = metadata;
|
||||
return {
|
||||
accessedAt: new Date(accessedAtMs),
|
||||
createdAt: new Date(createdAtMs),
|
||||
modifiedAt: new Date(modifiedAtMs),
|
||||
...data,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export type {
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -38,6 +38,7 @@ impl<R: Runtime, T: Manager<R>> FsExt<R> for T {
|
||||
|
||||
pub fn init<R: Runtime>() -> TauriPlugin<R, Option<Config>> {
|
||||
PluginBuilder::<R, Option<Config>>::new("fs")
|
||||
.js_init_script(include_str!("api-iife.js").to_string())
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
commands::read_file,
|
||||
commands::read_text_file,
|
||||
|
||||
Reference in New Issue
Block a user