@loidolt/theme-tauri 0.0.0-stage → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +521 -2
- package/dist/deep-links.d.ts +23 -0
- package/dist/deep-links.js +62 -0
- package/dist/file-drop.svelte.d.ts +62 -0
- package/dist/file-drop.svelte.js +100 -0
- package/dist/files.d.ts +130 -0
- package/dist/files.js +241 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +10 -0
- package/dist/menu-bar.svelte.d.ts +129 -0
- package/dist/menu-bar.svelte.js +439 -0
- package/dist/notifications.svelte.d.ts +67 -0
- package/dist/notifications.svelte.js +100 -0
- package/dist/platform.d.ts +37 -0
- package/dist/platform.js +102 -0
- package/dist/setup.d.ts +135 -0
- package/dist/setup.js +91 -0
- package/dist/single-instance.d.ts +34 -0
- package/dist/single-instance.js +69 -0
- package/dist/styles.css +35 -0
- package/dist/tray.d.ts +68 -0
- package/dist/tray.js +118 -0
- package/dist/types.d.ts +203 -0
- package/dist/types.js +8 -0
- package/dist/window.svelte.d.ts +102 -0
- package/dist/window.svelte.js +158 -0
- package/package.json +92 -4
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { type DesktopFile } from './files.js';
|
|
2
|
+
import type { FsPlugin, WebviewPlugin } from './types.js';
|
|
3
|
+
/** Anything that takes a batch of files: a `Dropzone` instance (`bind:this`) fits. */
|
|
4
|
+
export interface FileDropTarget {
|
|
5
|
+
addFiles(files: File[]): unknown;
|
|
6
|
+
}
|
|
7
|
+
export interface FileDropOptions {
|
|
8
|
+
/**
|
|
9
|
+
* The web view: `getCurrentWebview` from `@tauri-apps/api/webview`, or the web view it returns.
|
|
10
|
+
* Pass the function itself to stay browser-safe; it is only called inside a Tauri window.
|
|
11
|
+
*/
|
|
12
|
+
webview: WebviewPlugin | (() => WebviewPlugin);
|
|
13
|
+
/**
|
|
14
|
+
* `@tauri-apps/plugin-fs`, to read the dropped files into `File`s. Without it only `onPaths`
|
|
15
|
+
* hears about a drop.
|
|
16
|
+
*/
|
|
17
|
+
fs?: Pick<FsPlugin, 'readFile'>;
|
|
18
|
+
/**
|
|
19
|
+
* Where the dropped files go: a `Dropzone` (`bind:this`), whose `addFiles()` filters them by its
|
|
20
|
+
* `accept`, `maxSize` and `maxFiles` as for a drop of its own. A function is read at drop time,
|
|
21
|
+
* so it can return a component that mounts later.
|
|
22
|
+
*/
|
|
23
|
+
target?: FileDropTarget | null | (() => FileDropTarget | null | undefined);
|
|
24
|
+
/** Called with the dropped files once they are read, before `target` gets them. */
|
|
25
|
+
onFiles?: (files: DesktopFile[]) => void;
|
|
26
|
+
/** Called with the dropped paths as they arrive, before anything is read. */
|
|
27
|
+
onPaths?: (paths: string[]) => void;
|
|
28
|
+
/** A dropped path that could not be read, such as a folder. The other files still arrive. */
|
|
29
|
+
onError?: (error: unknown, path: string) => void;
|
|
30
|
+
}
|
|
31
|
+
export interface FileDrop {
|
|
32
|
+
/** Reactive: files are being dragged over the window. Feed it to `Dropzone`'s `externalDragging`. */
|
|
33
|
+
readonly dragging: boolean;
|
|
34
|
+
/** Reactive: the paths being dragged, while `dragging`. */
|
|
35
|
+
readonly paths: readonly string[];
|
|
36
|
+
/** Stops listening. Safe to call twice; automatic when created during component initialisation. */
|
|
37
|
+
destroy(): void;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Files dropped on the window, delivered as `File`s. A Tauri window takes OS file drops before
|
|
41
|
+
* the page sees them (`dragDropEnabled`, on by default), so HTML drag and drop events never fire
|
|
42
|
+
* for them; this listens to the web view's own drag and drop event instead, reads the dropped
|
|
43
|
+
* paths with `@tauri-apps/plugin-fs`, and hands the files to `onFiles` and to `target`.
|
|
44
|
+
*
|
|
45
|
+
* ```svelte
|
|
46
|
+
* <script lang="ts">
|
|
47
|
+
* import { getCurrentWebview } from '@tauri-apps/api/webview';
|
|
48
|
+
* import * as fs from '@tauri-apps/plugin-fs';
|
|
49
|
+
* import { Dropzone } from '@loidolt/theme-svelte';
|
|
50
|
+
* import { createFileDrop } from '@loidolt/theme-tauri';
|
|
51
|
+
*
|
|
52
|
+
* let dropzone = $state<Dropzone>();
|
|
53
|
+
* const drop = createFileDrop({ webview: getCurrentWebview, fs, target: () => dropzone });
|
|
54
|
+
* </script>
|
|
55
|
+
*
|
|
56
|
+
* <Dropzone bind:this={dropzone} window externalDragging={drop.dragging} onFiles={upload} />
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* In a browser (and on the server) it never fires and `dragging` stays `false`; the `Dropzone`'s
|
|
60
|
+
* own `window` drop handling covers that case.
|
|
61
|
+
*/
|
|
62
|
+
export declare function createFileDrop(options: FileDropOptions): FileDrop;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { onDestroy } from 'svelte';
|
|
2
|
+
import { readDesktopFiles } from './files.js';
|
|
3
|
+
import { isTauri, listen, noop } from './platform.js';
|
|
4
|
+
const resolveTarget = (target) => typeof target === 'function' ? target() : target;
|
|
5
|
+
/**
|
|
6
|
+
* Files dropped on the window, delivered as `File`s. A Tauri window takes OS file drops before
|
|
7
|
+
* the page sees them (`dragDropEnabled`, on by default), so HTML drag and drop events never fire
|
|
8
|
+
* for them; this listens to the web view's own drag and drop event instead, reads the dropped
|
|
9
|
+
* paths with `@tauri-apps/plugin-fs`, and hands the files to `onFiles` and to `target`.
|
|
10
|
+
*
|
|
11
|
+
* ```svelte
|
|
12
|
+
* <script lang="ts">
|
|
13
|
+
* import { getCurrentWebview } from '@tauri-apps/api/webview';
|
|
14
|
+
* import * as fs from '@tauri-apps/plugin-fs';
|
|
15
|
+
* import { Dropzone } from '@loidolt/theme-svelte';
|
|
16
|
+
* import { createFileDrop } from '@loidolt/theme-tauri';
|
|
17
|
+
*
|
|
18
|
+
* let dropzone = $state<Dropzone>();
|
|
19
|
+
* const drop = createFileDrop({ webview: getCurrentWebview, fs, target: () => dropzone });
|
|
20
|
+
* </script>
|
|
21
|
+
*
|
|
22
|
+
* <Dropzone bind:this={dropzone} window externalDragging={drop.dragging} onFiles={upload} />
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* In a browser (and on the server) it never fires and `dragging` stays `false`; the `Dropzone`'s
|
|
26
|
+
* own `window` drop handling covers that case.
|
|
27
|
+
*/
|
|
28
|
+
export function createFileDrop(options) {
|
|
29
|
+
const { webview, fs, target, onFiles, onPaths, onError } = options;
|
|
30
|
+
let dragging = $state(false);
|
|
31
|
+
let paths = $state([]);
|
|
32
|
+
let stopped = false;
|
|
33
|
+
const reset = () => {
|
|
34
|
+
dragging = false;
|
|
35
|
+
paths = [];
|
|
36
|
+
};
|
|
37
|
+
const deliver = async (dropped) => {
|
|
38
|
+
onPaths?.(dropped);
|
|
39
|
+
if (!fs || dropped.length === 0)
|
|
40
|
+
return;
|
|
41
|
+
const files = await readDesktopFiles(fs, dropped, onError);
|
|
42
|
+
if (stopped || files.length === 0)
|
|
43
|
+
return;
|
|
44
|
+
onFiles?.(files);
|
|
45
|
+
resolveTarget(target)?.addFiles(files);
|
|
46
|
+
};
|
|
47
|
+
const handle = (payload) => {
|
|
48
|
+
if (stopped)
|
|
49
|
+
return;
|
|
50
|
+
switch (payload.type) {
|
|
51
|
+
case 'enter':
|
|
52
|
+
dragging = true;
|
|
53
|
+
paths = Array.isArray(payload.paths) ? payload.paths : [];
|
|
54
|
+
break;
|
|
55
|
+
case 'over':
|
|
56
|
+
dragging = true;
|
|
57
|
+
break;
|
|
58
|
+
case 'leave':
|
|
59
|
+
reset();
|
|
60
|
+
break;
|
|
61
|
+
case 'drop': {
|
|
62
|
+
reset();
|
|
63
|
+
void deliver(Array.isArray(payload.paths) ? payload.paths : []);
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
let unlisten = noop;
|
|
69
|
+
if (isTauri()) {
|
|
70
|
+
try {
|
|
71
|
+
const view = typeof webview === 'function' ? webview() : webview;
|
|
72
|
+
unlisten = listen(view.onDragDropEvent(({ payload }) => handle(payload)));
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
/* not a Tauri 2 web view after all */
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const destroy = () => {
|
|
79
|
+
if (stopped)
|
|
80
|
+
return;
|
|
81
|
+
stopped = true;
|
|
82
|
+
unlisten();
|
|
83
|
+
reset();
|
|
84
|
+
};
|
|
85
|
+
try {
|
|
86
|
+
onDestroy(destroy);
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
/* module scope: the caller owns `destroy()` */
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
get dragging() {
|
|
93
|
+
return dragging;
|
|
94
|
+
},
|
|
95
|
+
get paths() {
|
|
96
|
+
return paths;
|
|
97
|
+
},
|
|
98
|
+
destroy,
|
|
99
|
+
};
|
|
100
|
+
}
|
package/dist/files.d.ts
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import type { DialogFilter, DialogPlugin, FsPlugin } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* A file read from disk: a `File` like any a `Dropzone` or `FileInput` hands you, plus the
|
|
4
|
+
* `path` it was read from. `path` is absent for a file picked through the browser's own picker.
|
|
5
|
+
*/
|
|
6
|
+
export type DesktopFile = File & {
|
|
7
|
+
readonly path?: string;
|
|
8
|
+
};
|
|
9
|
+
export interface PickOptions {
|
|
10
|
+
/**
|
|
11
|
+
* What may be picked, written as for `<input accept>`: extensions (`.pdf`), MIME types
|
|
12
|
+
* (`application/pdf`) and wildcards (`image/*`), comma-separated. Becomes the dialog's
|
|
13
|
+
* `filters`. Like the attribute it narrows the dialog, not what comes back.
|
|
14
|
+
*/
|
|
15
|
+
accept?: string;
|
|
16
|
+
/** Allow more than one file. Defaults to `false`. */
|
|
17
|
+
multiple?: boolean;
|
|
18
|
+
/** The dialog's title (not shown by every OS). */
|
|
19
|
+
title?: string;
|
|
20
|
+
/** The folder the dialog opens in, or a file in it to preselect. */
|
|
21
|
+
defaultPath?: string;
|
|
22
|
+
/** The dialog's filters as Tauri takes them, instead of the ones made from `accept`. */
|
|
23
|
+
filters?: DialogFilter[];
|
|
24
|
+
}
|
|
25
|
+
export interface PickPathsOptions extends PickOptions {
|
|
26
|
+
/** Pick folders instead of files. `accept` and `filters` are ignored then. */
|
|
27
|
+
directory?: boolean;
|
|
28
|
+
}
|
|
29
|
+
/** A file to save: a `File`, or a Blob with the name it should be saved under. */
|
|
30
|
+
export type SaveableFile = File | {
|
|
31
|
+
blob: Blob;
|
|
32
|
+
name: string;
|
|
33
|
+
};
|
|
34
|
+
export interface SaveOptions {
|
|
35
|
+
/**
|
|
36
|
+
* Where the dialog starts: a folder, or a full path to suggest. Defaults to the file's name, so
|
|
37
|
+
* the dialog opens in the OS's usual folder with the name filled in.
|
|
38
|
+
*/
|
|
39
|
+
defaultPath?: string;
|
|
40
|
+
/**
|
|
41
|
+
* The dialog's filters. Defaults to the file's own extension, which on macOS also keeps the
|
|
42
|
+
* extension on whatever name the user types.
|
|
43
|
+
*/
|
|
44
|
+
filters?: DialogFilter[];
|
|
45
|
+
/** The dialog's title (not shown by every OS). */
|
|
46
|
+
title?: string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Where a saved file went: written to the path the user chose in the save dialog, handed to the
|
|
50
|
+
* browser as a download, nowhere because the user cancelled the dialog, or nowhere at all (on
|
|
51
|
+
* the server). The same shape as `createFiles` in `@loidolt/theme-capacitor`, plus `cancelled`.
|
|
52
|
+
*/
|
|
53
|
+
export type SaveOutcome = {
|
|
54
|
+
method: 'filesystem';
|
|
55
|
+
path: string;
|
|
56
|
+
} | {
|
|
57
|
+
method: 'download';
|
|
58
|
+
name: string;
|
|
59
|
+
} | {
|
|
60
|
+
method: 'cancelled';
|
|
61
|
+
} | {
|
|
62
|
+
method: 'none';
|
|
63
|
+
};
|
|
64
|
+
export interface DesktopFilesOptions {
|
|
65
|
+
/** `@tauri-apps/plugin-dialog`, for the native open and save dialogs. */
|
|
66
|
+
dialog?: DialogPlugin;
|
|
67
|
+
/** `@tauri-apps/plugin-fs`, for reading what was picked and writing what is saved. */
|
|
68
|
+
fs?: FsPlugin;
|
|
69
|
+
}
|
|
70
|
+
export interface DesktopFiles {
|
|
71
|
+
/**
|
|
72
|
+
* Lets the user pick files — the native open dialog in a Tauri window, the browser's picker
|
|
73
|
+
* elsewhere — and resolves them as `File`s with their `path`, so one upload path serves both.
|
|
74
|
+
* Resolves `[]` when the dialog is dismissed. Each file is read whole into memory; for large
|
|
75
|
+
* files use {@link pickPaths} and read them in Rust. In a browser, call it from a click or key
|
|
76
|
+
* handler, as for any file input.
|
|
77
|
+
*/
|
|
78
|
+
pick(options?: PickOptions): Promise<DesktopFile[]>;
|
|
79
|
+
/**
|
|
80
|
+
* The native open dialog's choice as paths, unread — files, or folders with `directory`.
|
|
81
|
+
* Resolves `[]` when dismissed, and outside a Tauri window (a browser has no paths to give).
|
|
82
|
+
*/
|
|
83
|
+
pickPaths(options?: PickPathsOptions): Promise<string[]>;
|
|
84
|
+
/**
|
|
85
|
+
* Asks where to save with the native save dialog and writes the file there, or downloads it in
|
|
86
|
+
* a browser. Resolves `{ method: 'cancelled' }` when the dialog is dismissed.
|
|
87
|
+
*/
|
|
88
|
+
save(file: SaveableFile, options?: SaveOptions): Promise<SaveOutcome>;
|
|
89
|
+
}
|
|
90
|
+
/** Common extensions and their MIME types, for `File.type` and for turning `accept` into filters. */
|
|
91
|
+
export declare const EXTENSION_TYPES: Readonly<Record<string, string>>;
|
|
92
|
+
/** The last segment of a path, with either separator. */
|
|
93
|
+
export declare function baseName(path: string): string;
|
|
94
|
+
/** The MIME type for a file name or path by its extension, or `''` when it is not a known one. */
|
|
95
|
+
export declare function mimeTypeOf(name: string): string;
|
|
96
|
+
/**
|
|
97
|
+
* An `accept` string as the dialog filters Tauri takes, which list extensions: `.pdf` stays
|
|
98
|
+
* `pdf`, a MIME type becomes its known extensions, `image/*` every known image extension.
|
|
99
|
+
* `undefined` means "anything": no `accept`, `*` or a MIME type with no known extension — rather
|
|
100
|
+
* than make those files unpickable. The filter is named after its extensions (`PDF, PNG`), which
|
|
101
|
+
* needs no translation; `name` replaces that.
|
|
102
|
+
*/
|
|
103
|
+
export declare function acceptToFilters(accept: string | undefined, name?: string): DialogFilter[] | undefined;
|
|
104
|
+
/** A file read from `path`, typed by its extension. Rejects when it cannot be read. */
|
|
105
|
+
export declare function readDesktopFile(fs: Pick<FsPlugin, 'readFile'>, path: string): Promise<DesktopFile>;
|
|
106
|
+
/**
|
|
107
|
+
* The files at `paths`, read whole into memory, in order. A path that cannot be read (a folder,
|
|
108
|
+
* one outside the fs scope, a file gone since) is skipped and handed to `onError`, so one bad
|
|
109
|
+
* path does not lose the rest.
|
|
110
|
+
*/
|
|
111
|
+
export declare function readDesktopFiles(fs: Pick<FsPlugin, 'readFile'>, paths: readonly string[], onError?: (error: unknown, path: string) => void): Promise<DesktopFile[]>;
|
|
112
|
+
/**
|
|
113
|
+
* Opening and saving files the same way in a Tauri window and in a browser. In a window, picking
|
|
114
|
+
* goes through the native open dialog and reads the chosen files with `@tauri-apps/plugin-fs`,
|
|
115
|
+
* and saving asks with the native save dialog and writes there; in a browser, a file input and a
|
|
116
|
+
* download. Either way `pick` resolves `File`s, so they go down the same upload path as a
|
|
117
|
+
* `Dropzone`'s.
|
|
118
|
+
*
|
|
119
|
+
* ```ts
|
|
120
|
+
* import * as dialog from '@tauri-apps/plugin-dialog';
|
|
121
|
+
* import * as fs from '@tauri-apps/plugin-fs';
|
|
122
|
+
*
|
|
123
|
+
* const files = createDesktopFiles({ dialog, fs });
|
|
124
|
+
* const [pdf] = await files.pick({ accept: '.pdf' });
|
|
125
|
+
* await files.save(new File([bytes], 'export.pdf'));
|
|
126
|
+
* ```
|
|
127
|
+
*
|
|
128
|
+
* On the server `pick` resolves `[]` and `save` resolves `{ method: 'none' }`.
|
|
129
|
+
*/
|
|
130
|
+
export declare function createDesktopFiles(options?: DesktopFilesOptions): DesktopFiles;
|
package/dist/files.js
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { isTauri } from './platform.js';
|
|
2
|
+
/** Common extensions and their MIME types, for `File.type` and for turning `accept` into filters. */
|
|
3
|
+
export const EXTENSION_TYPES = {
|
|
4
|
+
avif: 'image/avif',
|
|
5
|
+
csv: 'text/csv',
|
|
6
|
+
doc: 'application/msword',
|
|
7
|
+
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
8
|
+
epub: 'application/epub+zip',
|
|
9
|
+
gif: 'image/gif',
|
|
10
|
+
heic: 'image/heic',
|
|
11
|
+
htm: 'text/html',
|
|
12
|
+
html: 'text/html',
|
|
13
|
+
jpeg: 'image/jpeg',
|
|
14
|
+
jpg: 'image/jpeg',
|
|
15
|
+
json: 'application/json',
|
|
16
|
+
m4a: 'audio/mp4',
|
|
17
|
+
md: 'text/markdown',
|
|
18
|
+
mov: 'video/quicktime',
|
|
19
|
+
mp3: 'audio/mpeg',
|
|
20
|
+
mp4: 'video/mp4',
|
|
21
|
+
odt: 'application/vnd.oasis.opendocument.text',
|
|
22
|
+
pdf: 'application/pdf',
|
|
23
|
+
png: 'image/png',
|
|
24
|
+
ppt: 'application/vnd.ms-powerpoint',
|
|
25
|
+
pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
26
|
+
rtf: 'application/rtf',
|
|
27
|
+
svg: 'image/svg+xml',
|
|
28
|
+
txt: 'text/plain',
|
|
29
|
+
wav: 'audio/wav',
|
|
30
|
+
webp: 'image/webp',
|
|
31
|
+
xls: 'application/vnd.ms-excel',
|
|
32
|
+
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
33
|
+
xml: 'application/xml',
|
|
34
|
+
zip: 'application/zip',
|
|
35
|
+
};
|
|
36
|
+
/** The last segment of a path, with either separator. */
|
|
37
|
+
export function baseName(path) {
|
|
38
|
+
return path.split(/[\\/]/).filter(Boolean).pop() ?? path;
|
|
39
|
+
}
|
|
40
|
+
/** The lowercase extension of a name or path, without the dot; `''` for none. */
|
|
41
|
+
function extensionOf(name) {
|
|
42
|
+
const base = baseName(name);
|
|
43
|
+
const dot = base.lastIndexOf('.');
|
|
44
|
+
return dot > 0 ? base.slice(dot + 1).toLowerCase() : '';
|
|
45
|
+
}
|
|
46
|
+
/** The MIME type for a file name or path by its extension, or `''` when it is not a known one. */
|
|
47
|
+
export function mimeTypeOf(name) {
|
|
48
|
+
return EXTENSION_TYPES[extensionOf(name)] ?? '';
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* An `accept` string as the dialog filters Tauri takes, which list extensions: `.pdf` stays
|
|
52
|
+
* `pdf`, a MIME type becomes its known extensions, `image/*` every known image extension.
|
|
53
|
+
* `undefined` means "anything": no `accept`, `*` or a MIME type with no known extension — rather
|
|
54
|
+
* than make those files unpickable. The filter is named after its extensions (`PDF, PNG`), which
|
|
55
|
+
* needs no translation; `name` replaces that.
|
|
56
|
+
*/
|
|
57
|
+
export function acceptToFilters(accept, name) {
|
|
58
|
+
const tokens = (accept ?? '')
|
|
59
|
+
.split(',')
|
|
60
|
+
.map((token) => token.trim().toLowerCase())
|
|
61
|
+
.filter(Boolean);
|
|
62
|
+
if (tokens.length === 0)
|
|
63
|
+
return undefined;
|
|
64
|
+
const extensions = new Set();
|
|
65
|
+
for (const token of tokens) {
|
|
66
|
+
if (token === '*' || token === '*/*')
|
|
67
|
+
return undefined;
|
|
68
|
+
if (token.startsWith('.')) {
|
|
69
|
+
if (token.length > 1)
|
|
70
|
+
extensions.add(token.slice(1));
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
const wildcard = token.endsWith('/*') ? token.slice(0, -1) : null;
|
|
74
|
+
const known = Object.keys(EXTENSION_TYPES).filter((extension) => wildcard
|
|
75
|
+
? EXTENSION_TYPES[extension].startsWith(wildcard)
|
|
76
|
+
: EXTENSION_TYPES[extension] === token);
|
|
77
|
+
if (known.length === 0)
|
|
78
|
+
return undefined;
|
|
79
|
+
for (const extension of known)
|
|
80
|
+
extensions.add(extension);
|
|
81
|
+
}
|
|
82
|
+
if (extensions.size === 0)
|
|
83
|
+
return undefined;
|
|
84
|
+
const list = [...extensions];
|
|
85
|
+
return [
|
|
86
|
+
{ name: name ?? list.map((extension) => extension.toUpperCase()).join(', '), extensions: list },
|
|
87
|
+
];
|
|
88
|
+
}
|
|
89
|
+
/** A file read from `path`, typed by its extension. Rejects when it cannot be read. */
|
|
90
|
+
export async function readDesktopFile(fs, path) {
|
|
91
|
+
const bytes = await fs.readFile(path);
|
|
92
|
+
const name = baseName(path);
|
|
93
|
+
const file = new File([bytes], name, { type: mimeTypeOf(name) });
|
|
94
|
+
Object.defineProperty(file, 'path', { value: path, enumerable: true });
|
|
95
|
+
return file;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The files at `paths`, read whole into memory, in order. A path that cannot be read (a folder,
|
|
99
|
+
* one outside the fs scope, a file gone since) is skipped and handed to `onError`, so one bad
|
|
100
|
+
* path does not lose the rest.
|
|
101
|
+
*/
|
|
102
|
+
export async function readDesktopFiles(fs, paths, onError) {
|
|
103
|
+
const read = await Promise.all(paths.map((path) => readDesktopFile(fs, path).catch((error) => {
|
|
104
|
+
onError?.(error, path);
|
|
105
|
+
return null;
|
|
106
|
+
})));
|
|
107
|
+
return read.filter((file) => file !== null);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* A file name that is safe as the last segment of a path: no separators, no leading dots, no
|
|
111
|
+
* control characters. Falls back to `fallback` when nothing is left.
|
|
112
|
+
*/
|
|
113
|
+
function safeFileName(name, fallback = 'file') {
|
|
114
|
+
const cleaned = name
|
|
115
|
+
// eslint-disable-next-line no-control-regex
|
|
116
|
+
.replace(/[\u0000-\u001f\u007f/\\:*?"<>|]+/g, '-')
|
|
117
|
+
.replace(/^[.\s]+/, '')
|
|
118
|
+
.trim();
|
|
119
|
+
return cleaned || fallback;
|
|
120
|
+
}
|
|
121
|
+
/** How long a download's object URL is kept: long enough for every browser to start reading it. */
|
|
122
|
+
const REVOKE_AFTER = 40_000;
|
|
123
|
+
/** Starts a browser download of `blob` as `name`, through a temporary `<a download>`. */
|
|
124
|
+
function downloadBlob(blob, name) {
|
|
125
|
+
const href = URL.createObjectURL(blob);
|
|
126
|
+
const anchor = document.createElement('a');
|
|
127
|
+
anchor.href = href;
|
|
128
|
+
anchor.download = name;
|
|
129
|
+
anchor.rel = 'noopener';
|
|
130
|
+
anchor.hidden = true;
|
|
131
|
+
document.body.append(anchor);
|
|
132
|
+
try {
|
|
133
|
+
anchor.click();
|
|
134
|
+
}
|
|
135
|
+
finally {
|
|
136
|
+
anchor.remove();
|
|
137
|
+
setTimeout(() => URL.revokeObjectURL(href), REVOKE_AFTER);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/** The browser's own picker: a hidden file input, opened and removed again. */
|
|
141
|
+
function pickInBrowser({ accept, multiple = false }) {
|
|
142
|
+
return new Promise((resolve) => {
|
|
143
|
+
const input = document.createElement('input');
|
|
144
|
+
input.type = 'file';
|
|
145
|
+
input.hidden = true;
|
|
146
|
+
input.multiple = multiple;
|
|
147
|
+
if (accept)
|
|
148
|
+
input.accept = accept;
|
|
149
|
+
const done = (files) => {
|
|
150
|
+
input.remove();
|
|
151
|
+
resolve(files);
|
|
152
|
+
};
|
|
153
|
+
input.addEventListener('change', () => done(Array.from(input.files ?? [])), { once: true });
|
|
154
|
+
// Every current browser fires `cancel` when the picker is dismissed.
|
|
155
|
+
input.addEventListener('cancel', () => done([]), { once: true });
|
|
156
|
+
document.body.append(input);
|
|
157
|
+
input.click();
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
/** The Blob's bytes, read whole into memory. */
|
|
161
|
+
function bytesOf(blob) {
|
|
162
|
+
return new Promise((resolve, reject) => {
|
|
163
|
+
const reader = new FileReader();
|
|
164
|
+
reader.onload = () => resolve(new Uint8Array(reader.result));
|
|
165
|
+
reader.onerror = () => reject(reader.error ?? new Error('Could not read the file'));
|
|
166
|
+
reader.readAsArrayBuffer(blob);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
const asPaths = (result) => result == null ? [] : Array.isArray(result) ? result : [result];
|
|
170
|
+
/**
|
|
171
|
+
* Opening and saving files the same way in a Tauri window and in a browser. In a window, picking
|
|
172
|
+
* goes through the native open dialog and reads the chosen files with `@tauri-apps/plugin-fs`,
|
|
173
|
+
* and saving asks with the native save dialog and writes there; in a browser, a file input and a
|
|
174
|
+
* download. Either way `pick` resolves `File`s, so they go down the same upload path as a
|
|
175
|
+
* `Dropzone`'s.
|
|
176
|
+
*
|
|
177
|
+
* ```ts
|
|
178
|
+
* import * as dialog from '@tauri-apps/plugin-dialog';
|
|
179
|
+
* import * as fs from '@tauri-apps/plugin-fs';
|
|
180
|
+
*
|
|
181
|
+
* const files = createDesktopFiles({ dialog, fs });
|
|
182
|
+
* const [pdf] = await files.pick({ accept: '.pdf' });
|
|
183
|
+
* await files.save(new File([bytes], 'export.pdf'));
|
|
184
|
+
* ```
|
|
185
|
+
*
|
|
186
|
+
* On the server `pick` resolves `[]` and `save` resolves `{ method: 'none' }`.
|
|
187
|
+
*/
|
|
188
|
+
export function createDesktopFiles(options = {}) {
|
|
189
|
+
const { dialog, fs } = options;
|
|
190
|
+
async function pickPaths(pickOptions = {}) {
|
|
191
|
+
if (!dialog || !isTauri())
|
|
192
|
+
return [];
|
|
193
|
+
const { accept, multiple = false, directory = false, title, defaultPath, filters, } = pickOptions;
|
|
194
|
+
const result = await dialog.open({
|
|
195
|
+
title,
|
|
196
|
+
defaultPath,
|
|
197
|
+
multiple,
|
|
198
|
+
directory,
|
|
199
|
+
filters: directory ? undefined : (filters ?? acceptToFilters(accept)),
|
|
200
|
+
});
|
|
201
|
+
return asPaths(result);
|
|
202
|
+
}
|
|
203
|
+
return {
|
|
204
|
+
pickPaths,
|
|
205
|
+
async pick(pickOptions = {}) {
|
|
206
|
+
if (typeof document === 'undefined')
|
|
207
|
+
return [];
|
|
208
|
+
// A web view's own file input works too, for a window without the plugins.
|
|
209
|
+
if (!dialog || !fs || !isTauri())
|
|
210
|
+
return pickInBrowser(pickOptions);
|
|
211
|
+
const { accept, multiple, title, defaultPath, filters } = pickOptions;
|
|
212
|
+
const paths = await pickPaths({ accept, multiple, title, defaultPath, filters });
|
|
213
|
+
return Promise.all(paths.map((path) => readDesktopFile(fs, path)));
|
|
214
|
+
},
|
|
215
|
+
async save(file, saveOptions = {}) {
|
|
216
|
+
if (typeof document === 'undefined')
|
|
217
|
+
return { method: 'none' };
|
|
218
|
+
const name = safeFileName(file.name);
|
|
219
|
+
const blob = file instanceof Blob ? file : file.blob;
|
|
220
|
+
if (!isTauri()) {
|
|
221
|
+
downloadBlob(blob, name);
|
|
222
|
+
return { method: 'download', name };
|
|
223
|
+
}
|
|
224
|
+
if (!dialog || !fs) {
|
|
225
|
+
// A web view has no download manager of its own; a download would go nowhere.
|
|
226
|
+
throw new Error('Saving a file in a Tauri window needs dialog from @tauri-apps/plugin-dialog and fs from @tauri-apps/plugin-fs');
|
|
227
|
+
}
|
|
228
|
+
const extension = extensionOf(name);
|
|
229
|
+
const path = await dialog.save({
|
|
230
|
+
title: saveOptions.title,
|
|
231
|
+
defaultPath: saveOptions.defaultPath ?? name,
|
|
232
|
+
filters: saveOptions.filters ??
|
|
233
|
+
(extension ? [{ name: extension.toUpperCase(), extensions: [extension] }] : undefined),
|
|
234
|
+
});
|
|
235
|
+
if (path == null)
|
|
236
|
+
return { method: 'cancelled' };
|
|
237
|
+
await fs.writeFile(path, await bytesOf(blob));
|
|
238
|
+
return { method: 'filesystem', path };
|
|
239
|
+
},
|
|
240
|
+
};
|
|
241
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export { isTauri, markTauriPlatform, tauriPlatform } from './platform.js';
|
|
2
|
+
export type { TauriPlatform } from './platform.js';
|
|
3
|
+
export { createNotifications } from './notifications.svelte.js';
|
|
4
|
+
export type { NotificationBackend, NotificationPermissionState, Notifications, NotificationsOptions, NotificationToaster, NotifyOptions, } from './notifications.svelte.js';
|
|
5
|
+
export { deepLinkPath, handleDeepLinks } from './deep-links.js';
|
|
6
|
+
export type { DeepLinkOptions } from './deep-links.js';
|
|
7
|
+
export { filesFromArgs, handleSecondInstance } from './single-instance.js';
|
|
8
|
+
export type { SecondInstance, SingleInstanceOptions } from './single-instance.js';
|
|
9
|
+
export { createTray } from './tray.js';
|
|
10
|
+
export type { Tray, TrayContent, TrayMenuItem, TrayOptions } from './tray.js';
|
|
11
|
+
export { acceptToFilters, baseName, createDesktopFiles, EXTENSION_TYPES, mimeTypeOf, readDesktopFile, readDesktopFiles, } from './files.js';
|
|
12
|
+
export type { DesktopFile, DesktopFiles, DesktopFilesOptions, PickOptions, PickPathsOptions, SaveableFile, SaveOptions, SaveOutcome, } from './files.js';
|
|
13
|
+
export { createFileDrop } from './file-drop.svelte.js';
|
|
14
|
+
export type { FileDrop, FileDropOptions, FileDropTarget } from './file-drop.svelte.js';
|
|
15
|
+
export { createMenuBar, isMenuBarModule, menuAccelerator } from './menu-bar.svelte.js';
|
|
16
|
+
export type { MenuBar, MenuBarEntry, MenuBarLabels, MenuBarLayoutContext, MenuBarMenu, MenuBarOptions, MenuBarPlatform, } from './menu-bar.svelte.js';
|
|
17
|
+
export { setupTauri } from './setup.js';
|
|
18
|
+
export type { SetupTauriOptions, TauriPlugins, TauriSetup } from './setup.js';
|
|
19
|
+
export { insetWindowChrome, showWindow, syncWindowAppearance, windowBackground, } from './window.svelte.js';
|
|
20
|
+
export type { ShowWindowOptions, WindowAppearance, WindowAppearanceOptions, WindowChromeOptions, } from './window.svelte.js';
|
|
21
|
+
export type { DialogFilter, DialogPlugin, DragDropPayload, FsPlugin, PhysicalPoint, WebviewPlugin, } from './types.js';
|
|
22
|
+
export type { AppMenuHandle, MenuBarModule, MenuItemHandle, PredefinedMenuItemName, SubmenuHandle, } from './types.js';
|
|
23
|
+
export type { DeepLinkPlugin, EventModule, MenuModule, NotificationPlugin, OsPlugin, TauriResource, Teardown, TrayIconHandle, TrayImage, TrayMenuEntry, TrayModule, UnlistenFn, WindowPlugin, WindowTheme, } from './types.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { isTauri, markTauriPlatform, tauriPlatform } from './platform.js';
|
|
2
|
+
export { createNotifications } from './notifications.svelte.js';
|
|
3
|
+
export { deepLinkPath, handleDeepLinks } from './deep-links.js';
|
|
4
|
+
export { filesFromArgs, handleSecondInstance } from './single-instance.js';
|
|
5
|
+
export { createTray } from './tray.js';
|
|
6
|
+
export { acceptToFilters, baseName, createDesktopFiles, EXTENSION_TYPES, mimeTypeOf, readDesktopFile, readDesktopFiles, } from './files.js';
|
|
7
|
+
export { createFileDrop } from './file-drop.svelte.js';
|
|
8
|
+
export { createMenuBar, isMenuBarModule, menuAccelerator } from './menu-bar.svelte.js';
|
|
9
|
+
export { setupTauri } from './setup.js';
|
|
10
|
+
export { insetWindowChrome, showWindow, syncWindowAppearance, windowBackground, } from './window.svelte.js';
|