Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@tauri-apps/api

Package Overview
Dependencies
Maintainers
5
Versions
80
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tauri-apps/api - npm Package Compare versions

Comparing version 2.0.0-beta.0 to 2.0.0-beta.1

webviewWindow.cjs

25

CHANGELOG.md
# Changelog
## \[2.0.0-beta.1]
### New Features
- [`16e550ec`](https://www.github.com/tauri-apps/tauri/commit/16e550ec1503765158cdc3bb2a20e70ec710e981)([#8844](https://www.github.com/tauri-apps/tauri/pull/8844)) Add a new `webviewWindow` module that exports `WebviewWindow` class and related methods such as `getCurrent` and `getAll`.
- [`16e550ec`](https://www.github.com/tauri-apps/tauri/commit/16e550ec1503765158cdc3bb2a20e70ec710e981)([#8844](https://www.github.com/tauri-apps/tauri/pull/8844)) Add `Window.onFileDropEvent` method.
### Breaking Changes
- [`16e550ec`](https://www.github.com/tauri-apps/tauri/commit/16e550ec1503765158cdc3bb2a20e70ec710e981)([#8844](https://www.github.com/tauri-apps/tauri/pull/8844)) Renamed the following enum variants of `TauriEvent` enum:
- `TauriEvent.WEBVIEW_FILE_DROP` -> `TauriEvent.FILE_DROP`
- `TauriEvent.WEBVIEW_FILE_DROP_HOVER` -> `TauriEvent.FILE_DROP_HOVER`
- `TauriEvent.WEBVIEW_FILE_DROP_CANCELLED` -> `TauriEvent.FILE_DROP_CANCELLED`
- [`16e550ec`](https://www.github.com/tauri-apps/tauri/commit/16e550ec1503765158cdc3bb2a20e70ec710e981)([#8844](https://www.github.com/tauri-apps/tauri/pull/8844)) Move `WebviewWindow` class from `webview` module to a new `webviewWindow` module.
## \[2.0.0-beta.0]

@@ -26,5 +42,2 @@

### Bug Fixes
- [`1c582a94`](https://www.github.com/tauri-apps/tauri/commit/1c582a942e345a066b65620e4db9f688ec142bb9)([#8392](https://www.github.com/tauri-apps/tauri/pull/8392)) Fix a regression where typescript could not find types when using `"moduleResolution": "node"`
- [`97e33412`](https://www.github.com/tauri-apps/tauri/commit/97e334129956159bbd60e1c531b6acd3bc6139a6)([#8534](https://www.github.com/tauri-apps/tauri/pull/8534)) `mockIPC` and `mockWindows` no longer crash if `window.__TAURI_INTERNALS__` is undefined.

@@ -160,2 +173,8 @@

## \[1.5.3]
### Bug Fixes
- [`1c582a94`](https://www.github.com/tauri-apps/tauri/commit/1c582a942e345a066b65620e4db9f688ec142bb9)([#8392](https://www.github.com/tauri-apps/tauri/pull/8392)) Fix a regression where typescript could not find types when using `"moduleResolution": "node"`
## \[1.5.2]

@@ -162,0 +181,0 @@

10

event.d.ts

@@ -50,5 +50,5 @@ type EventTarget = {

WEBVIEW_CREATED = "tauri://webview-created",
WEBVIEW_FILE_DROP = "tauri://file-drop",
WEBVIEW_FILE_DROP_HOVER = "tauri://file-drop-hover",
WEBVIEW_FILE_DROP_CANCELLED = "tauri://file-drop-cancelled"
FILE_DROP = "tauri://file-drop",
FILE_DROP_HOVER = "tauri://file-drop-hover",
FILE_DROP_CANCELLED = "tauri://file-drop-cancelled"
}

@@ -125,4 +125,4 @@ /**

* ```typescript
* import { emit } from '@tauri-apps/api/event';
* await emit('frontend-loaded', { loggedIn: true, token: 'authToken' });
* import { emitTo } from '@tauri-apps/api/event';
* await emitTo('main', 'frontend-loaded', { loggedIn: true, token: 'authToken' });
* ```

@@ -129,0 +129,0 @@ *

@@ -26,5 +26,5 @@ import { invoke, transformCallback } from './core.js';

TauriEvent["WEBVIEW_CREATED"] = "tauri://webview-created";
TauriEvent["WEBVIEW_FILE_DROP"] = "tauri://file-drop";
TauriEvent["WEBVIEW_FILE_DROP_HOVER"] = "tauri://file-drop-hover";
TauriEvent["WEBVIEW_FILE_DROP_CANCELLED"] = "tauri://file-drop-cancelled";
TauriEvent["FILE_DROP"] = "tauri://file-drop";
TauriEvent["FILE_DROP_HOVER"] = "tauri://file-drop-hover";
TauriEvent["FILE_DROP_CANCELLED"] = "tauri://file-drop-cancelled";
})(TauriEvent || (TauriEvent = {}));

@@ -136,4 +136,4 @@ /**

* ```typescript
* import { emit } from '@tauri-apps/api/event';
* await emit('frontend-loaded', { loggedIn: true, token: 'authToken' });
* import { emitTo } from '@tauri-apps/api/event';
* await emitTo('main', 'frontend-loaded', { loggedIn: true, token: 'authToken' });
* ```

@@ -140,0 +140,0 @@ *

@@ -16,2 +16,3 @@ /**

import * as webview from './webview';
import * as webviewWindow from './webviewWindow';
import * as path from './path';

@@ -21,2 +22,2 @@ import * as dpi from './dpi';

import * as menu from './menu';
export { app, dpi, event, path, core, window, webview, tray, menu };
export { app, dpi, event, path, core, window, webview, webviewWindow, tray, menu };

@@ -11,2 +11,4 @@ import * as app from './app.js';

export { webview };
import * as webviewWindow from './webviewWindow.js';
export { webviewWindow };
import * as path from './path.js';

@@ -13,0 +15,0 @@ export { path };

{
"name": "@tauri-apps/api",
"version": "2.0.0-beta.0",
"version": "2.0.0-beta.1",
"description": "Tauri API definitions",

@@ -48,8 +48,8 @@ "funding": {

"@rollup/plugin-typescript": "11.1.5",
"@types/node": "20.9.0",
"@types/node": "20.10.5",
"@typescript-eslint/eslint-plugin": "6.10.0",
"@typescript-eslint/parser": "6.10.0",
"eslint": "8.53.0",
"eslint": "8.56.0",
"eslint-config-prettier": "9.0.0",
"eslint-plugin-import": "2.29.0",
"eslint-plugin-import": "2.29.1",
"eslint-plugin-n": "16.3.0",

@@ -60,6 +60,6 @@ "eslint-plugin-node": "11.1.0",

"fast-glob": "3.3.2",
"prettier": "3.0.3",
"prettier": "3.1.1",
"rollup": "4.3.0",
"tslib": "2.6.2",
"typescript": "5.2.2"
"typescript": "5.3.3"
},

@@ -66,0 +66,0 @@ "engines": {

@@ -438,3 +438,3 @@ /**

* const appDataDirPath = await appDataDir();
* const path = await normalize(appDataDirPath, '..', 'users', 'tauri', 'avatar.png');
* const path = await normalize(`${appDataDirPath}/../users/tauri/avatar.png`);
* ```

@@ -441,0 +441,0 @@ *

@@ -555,3 +555,3 @@ import { invoke } from './core.js';

* const appDataDirPath = await appDataDir();
* const path = await normalize(appDataDirPath, '..', 'users', 'tauri', 'avatar.png');
* const path = await normalize(`${appDataDirPath}/../users/tauri/avatar.png`);
* ```

@@ -558,0 +558,0 @@ *

@@ -19,3 +19,2 @@ /**

import { Window } from './window';
import type { WindowOptions } from './window';
interface FileDropPayload {

@@ -36,3 +35,3 @@ paths: string[];

*
* @since 1.0.0
* @since 2.0.0
*/

@@ -43,3 +42,3 @@ declare function getCurrent(): Webview;

*
* @since 1.0.0
* @since 2.0.0
*/

@@ -196,3 +195,3 @@ declare function getAll(): Webview[];

* import { getCurrent } from '@tauri-apps/api/webview';
* await getCurrent().emit('webview-loaded', { loggedIn: true, token: 'authToken' });
* await getCurrent().emitTo('main', 'webview-loaded', { loggedIn: true, token: 'authToken' });
* ```

@@ -303,93 +302,6 @@ *

}
interface WebviewWindow extends Webview, Window {
}
declare class WebviewWindow {
label: string;
/** Local event listeners. */
listeners: Record<string, Array<EventCallback<any>>>;
/**
* Creates a new {@link Window} hosting a {@link Webview}.
* @example
* ```typescript
* import { WebviewWindow } from '@tauri-apps/api/webview'
* const webview = new WebviewWindow('my-label', {
* url: 'https://github.com/tauri-apps/tauri'
* });
* webview.once('tauri://created', function () {
* // webview successfully created
* });
* webview.once('tauri://error', function (e) {
* // an error happened creating the webview
* });
* ```
*
* @param label The unique webview label. Must be alphanumeric: `a-zA-Z-/:_`.
* @returns The {@link WebviewWindow} instance to communicate with the window and webview.
*/
constructor(label: WebviewLabel, options?: Omit<WebviewOptions, 'x' | 'y' | 'width' | 'height'> & WindowOptions);
/**
* Gets the Webview for the webview associated with the given label.
* @example
* ```typescript
* import { Webview } from '@tauri-apps/api/webview';
* const mainWebview = Webview.getByLabel('main');
* ```
*
* @param label The webview label.
* @returns The Webview instance to communicate with the webview or null if the webview doesn't exist.
*/
static getByLabel(label: string): WebviewWindow | null;
/**
* Get an instance of `Webview` for the current webview.
*/
static getCurrent(): WebviewWindow;
/**
* Gets a list of instances of `Webview` for all available webviews.
*/
static getAll(): WebviewWindow[];
/**
* Listen to an emitted event on this webivew window.
*
* @example
* ```typescript
* import { WebviewWindow } from '@tauri-apps/api/webview';
* const unlisten = await WebviewWindow.getCurrent().listen<string>('state-changed', (event) => {
* console.log(`Got error: ${payload}`);
* });
*
* // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
* unlisten();
* ```
*
* @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
* @param handler Event handler.
* @returns A promise resolving to a function to unlisten to the event.
* Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
*/
listen<T>(event: EventName, handler: EventCallback<T>): Promise<UnlistenFn>;
/**
* Listen to an emitted event on this webivew window only once.
*
* @example
* ```typescript
* import { WebviewWindow } from '@tauri-apps/api/webview';
* const unlisten = await WebviewWindow.getCurrent().once<null>('initialized', (event) => {
* console.log(`Webview initialized!`);
* });
*
* // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
* unlisten();
* ```
*
* @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
* @param handler Event handler.
* @returns A promise resolving to a function to unlisten to the event.
* Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
*/
once<T>(event: string, handler: EventCallback<T>): Promise<UnlistenFn>;
}
/**
* Configuration for the webview to create.
*
* @since 1.0.0
* @since 2.0.0
*/

@@ -452,3 +364,3 @@ interface WebviewOptions {

}
export { Webview, WebviewWindow, getCurrent, getAll };
export type { FileDropEvent, WebviewOptions };
export { Webview, getCurrent, getAll };
export type { FileDropEvent, FileDropPayload, WebviewOptions };

@@ -25,3 +25,3 @@ import { PhysicalPosition, PhysicalSize } from './dpi.js';

*
* @since 1.0.0
* @since 2.0.0
*/

@@ -37,3 +37,3 @@ function getCurrent() {

*
* @since 1.0.0
* @since 2.0.0
*/

@@ -248,3 +248,3 @@ function getAll() {

* import { getCurrent } from '@tauri-apps/api/webview';
* await getCurrent().emit('webview-loaded', { loggedIn: true, token: 'authToken' });
* await getCurrent().emitTo('main', 'webview-loaded', { loggedIn: true, token: 'authToken' });
* ```

@@ -428,3 +428,3 @@ *

async onFileDropEvent(handler) {
const unlistenFileDrop = await this.listen(TauriEvent.WEBVIEW_FILE_DROP, (event) => {
const unlistenFileDrop = await this.listen(TauriEvent.FILE_DROP, (event) => {
handler({

@@ -439,3 +439,3 @@ ...event,

});
const unlistenFileHover = await this.listen(TauriEvent.WEBVIEW_FILE_DROP_HOVER, (event) => {
const unlistenFileHover = await this.listen(TauriEvent.FILE_DROP_HOVER, (event) => {
handler({

@@ -450,3 +450,3 @@ ...event,

});
const unlistenCancel = await this.listen(TauriEvent.WEBVIEW_FILE_DROP_CANCELLED, (event) => {
const unlistenCancel = await this.listen(TauriEvent.FILE_DROP_CANCELLED, (event) => {
handler({ ...event, payload: { type: 'cancel' } });

@@ -464,159 +464,3 @@ });

}
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
class WebviewWindow {
/**
* Creates a new {@link Window} hosting a {@link Webview}.
* @example
* ```typescript
* import { WebviewWindow } from '@tauri-apps/api/webview'
* const webview = new WebviewWindow('my-label', {
* url: 'https://github.com/tauri-apps/tauri'
* });
* webview.once('tauri://created', function () {
* // webview successfully created
* });
* webview.once('tauri://error', function (e) {
* // an error happened creating the webview
* });
* ```
*
* @param label The unique webview label. Must be alphanumeric: `a-zA-Z-/:_`.
* @returns The {@link WebviewWindow} instance to communicate with the window and webview.
*/
constructor(label, options = {}) {
this.label = label;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
this.listeners = Object.create(null);
// @ts-expect-error `skip` is not a public API so it is not defined in WebviewOptions
if (!options?.skip) {
invoke('plugin:webview|create_webview_window', {
options: {
...options,
parent: typeof options.parent === 'string'
? options.parent
: options.parent?.label,
label
}
})
.then(async () => this.emit('tauri://created'))
.catch(async (e) => this.emit('tauri://error', e));
}
}
/**
* Gets the Webview for the webview associated with the given label.
* @example
* ```typescript
* import { Webview } from '@tauri-apps/api/webview';
* const mainWebview = Webview.getByLabel('main');
* ```
*
* @param label The webview label.
* @returns The Webview instance to communicate with the webview or null if the webview doesn't exist.
*/
static getByLabel(label) {
const webview = getAll().find((w) => w.label === label) ?? null;
if (webview) {
// @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor
return new WebviewWindow(webview.label, { skip: true });
}
return null;
}
/**
* Get an instance of `Webview` for the current webview.
*/
static getCurrent() {
const webview = getCurrent();
// @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor
return new WebviewWindow(webview.label, { skip: true });
}
/**
* Gets a list of instances of `Webview` for all available webviews.
*/
static getAll() {
// @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor
return getAll().map((w) => new WebviewWindow(w.label, { skip: true }));
}
/**
* Listen to an emitted event on this webivew window.
*
* @example
* ```typescript
* import { WebviewWindow } from '@tauri-apps/api/webview';
* const unlisten = await WebviewWindow.getCurrent().listen<string>('state-changed', (event) => {
* console.log(`Got error: ${payload}`);
* });
*
* // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
* unlisten();
* ```
*
* @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
* @param handler Event handler.
* @returns A promise resolving to a function to unlisten to the event.
* Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
*/
async listen(event, handler) {
if (this._handleTauriEvent(event, handler)) {
return Promise.resolve(() => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, security/detect-object-injection
const listeners = this.listeners[event];
listeners.splice(listeners.indexOf(handler), 1);
});
}
return listen(event, handler, {
target: { kind: 'WebviewWindow', label: this.label }
});
}
/**
* Listen to an emitted event on this webivew window only once.
*
* @example
* ```typescript
* import { WebviewWindow } from '@tauri-apps/api/webview';
* const unlisten = await WebviewWindow.getCurrent().once<null>('initialized', (event) => {
* console.log(`Webview initialized!`);
* });
*
* // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
* unlisten();
* ```
*
* @param event Event name. Must include only alphanumeric characters, `-`, `/`, `:` and `_`.
* @param handler Event handler.
* @returns A promise resolving to a function to unlisten to the event.
* Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
*/
async once(event, handler) {
if (this._handleTauriEvent(event, handler)) {
return Promise.resolve(() => {
// eslint-disable-next-line security/detect-object-injection
const listeners = this.listeners[event];
listeners.splice(listeners.indexOf(handler), 1);
});
}
return once(event, handler, {
target: { kind: 'WebviewWindow', label: this.label }
});
}
}
// Order matters, we use window APIs by default
applyMixins(WebviewWindow, [Window, Webview]);
/** Extends a base class by other specifed classes, wihtout overriding existing properties */
function applyMixins(baseClass, extendedClasses) {
(Array.isArray(extendedClasses)
? extendedClasses
: [extendedClasses]).forEach((extendedClass) => {
Object.getOwnPropertyNames(extendedClass.prototype).forEach((name) => {
if (typeof baseClass.prototype === 'object' &&
baseClass.prototype &&
name in baseClass.prototype)
return;
Object.defineProperty(baseClass.prototype, name,
// eslint-disable-next-line
Object.getOwnPropertyDescriptor(extendedClass.prototype, name) ??
Object.create(null));
});
});
}
export { Webview, WebviewWindow, getAll, getCurrent };
export { Webview, getAll, getCurrent };

@@ -17,3 +17,4 @@ /**

import { type EventTarget } from './event';
import { WebviewWindow } from './webview';
import { WebviewWindow } from './webviewWindow';
import type { FileDropEvent, FileDropPayload } from './webview';
/**

@@ -275,3 +276,3 @@ * Allows you to retrieve information about a given monitor.

* import { getCurrent } from '@tauri-apps/api/window';
* await getCurrent().emit('window-loaded', { loggedIn: true, token: 'authToken' });
* await getCurrent().emit('main', 'window-loaded', { loggedIn: true, token: 'authToken' });
* ```

@@ -1065,2 +1066,28 @@ * @param target Label of the target Window/Webview/WebviewWindow or raw {@link EventTarget} object.

/**
* Listen to a file drop event.
* The listener is triggered when the user hovers the selected files on the webview,
* drops the files or cancels the operation.
*
* @example
* ```typescript
* import { getCurrent } from "@tauri-apps/api/webview";
* const unlisten = await getCurrent().onFileDropEvent((event) => {
* if (event.payload.type === 'hover') {
* console.log('User hovering', event.payload.paths);
* } else if (event.payload.type === 'drop') {
* console.log('User dropped', event.payload.paths);
* } else {
* console.log('File drop cancelled');
* }
* });
*
* // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
* unlisten();
* ```
*
* @returns A promise resolving to a function to unlisten to the event.
* Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
*/
onFileDropEvent(handler: EventCallback<FileDropEvent>): Promise<UnlistenFn>;
/**
* Listen to window focus change.

@@ -1459,2 +1486,2 @@ *

export { Window, CloseRequestedEvent, getCurrent, getAll, LogicalSize, PhysicalSize, LogicalPosition, PhysicalPosition, UserAttentionType, Effect, EffectState, currentMonitor, primaryMonitor, availableMonitors };
export type { Effects, Theme, TitleBarStyle, ScaleFactorChanged, WindowOptions, Color };
export type { Effects, Theme, TitleBarStyle, ScaleFactorChanged, WindowOptions, Color, FileDropEvent, FileDropPayload };

@@ -312,3 +312,3 @@ import { PhysicalPosition, PhysicalSize } from './dpi.js';

* import { getCurrent } from '@tauri-apps/api/window';
* await getCurrent().emit('window-loaded', { loggedIn: true, token: 'authToken' });
* await getCurrent().emit('main', 'window-loaded', { loggedIn: true, token: 'authToken' });
* ```

@@ -1468,2 +1468,57 @@ * @param target Label of the target Window/Webview/WebviewWindow or raw {@link EventTarget} object.

/**
* Listen to a file drop event.
* The listener is triggered when the user hovers the selected files on the webview,
* drops the files or cancels the operation.
*
* @example
* ```typescript
* import { getCurrent } from "@tauri-apps/api/webview";
* const unlisten = await getCurrent().onFileDropEvent((event) => {
* if (event.payload.type === 'hover') {
* console.log('User hovering', event.payload.paths);
* } else if (event.payload.type === 'drop') {
* console.log('User dropped', event.payload.paths);
* } else {
* console.log('File drop cancelled');
* }
* });
*
* // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
* unlisten();
* ```
*
* @returns A promise resolving to a function to unlisten to the event.
* Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.
*/
async onFileDropEvent(handler) {
const unlistenFileDrop = await this.listen(TauriEvent.FILE_DROP, (event) => {
handler({
...event,
payload: {
type: 'drop',
paths: event.payload.paths,
position: mapPhysicalPosition(event.payload.position)
}
});
});
const unlistenFileHover = await this.listen(TauriEvent.FILE_DROP_HOVER, (event) => {
handler({
...event,
payload: {
type: 'hover',
paths: event.payload.paths,
position: mapPhysicalPosition(event.payload.position)
}
});
});
const unlistenCancel = await this.listen(TauriEvent.FILE_DROP_CANCELLED, (event) => {
handler({ ...event, payload: { type: 'cancel' } });
});
return () => {
unlistenFileDrop();
unlistenFileHover();
unlistenCancel();
};
}
/**
* Listen to window focus change.

@@ -1470,0 +1525,0 @@ *

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc