-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
fix(api): resolve TypeScript errors in webviewWindow.ts #14140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
d2df187
f254da8
e06aafb
9a7b6ba
2639f11
012bec8
f7253e0
61044d8
11dec7e
34f594f
2a8e5cc
c46bedc
f7d4387
984da5d
8c5b7a5
677a1d0
370ccd3
53e2462
01ccff9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,15 +15,21 @@ import type { EventName, EventCallback, UnlistenFn } from './event' | |
import { invoke } from './core' | ||
import type { Color, DragDropEvent } from './webview' | ||
|
||
// Internal-only type to avoid @ts-expect-error everywhere | ||
interface InternalWebviewOptions | ||
extends Omit<WebviewOptions, 'x' | 'y' | 'width' | 'height'> { | ||
skip?: boolean | ||
parent?: Window | WebviewWindow | string | ||
} | ||
|
||
/** | ||
* Get an instance of `Webview` for the current webview window. | ||
* | ||
* @since 2.0.0 | ||
*/ | ||
function getCurrentWebviewWindow(): WebviewWindow { | ||
const webview = getCurrentWebview() | ||
// @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 new WebviewWindow(webview.label, { skip: true } as any) | ||
} | ||
|
||
/** | ||
|
@@ -36,9 +42,8 @@ async function getAllWebviewWindows(): Promise<WebviewWindow[]> { | |
windows.map( | ||
(w) => | ||
new WebviewWindow(w, { | ||
// @ts-expect-error `skip` is not defined in the public API but it is handled by the constructor | ||
skip: true | ||
}) | ||
} as any) | ||
amrbashir marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
) | ||
) | ||
} | ||
|
@@ -53,43 +58,24 @@ class WebviewWindow { | |
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
listeners: Record<string, Array<EventCallback<any>>> | ||
|
||
/** | ||
KushalMeghani1644 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Creates a new {@link Window} hosting a {@link Webview}. | ||
* @example | ||
* ```typescript | ||
* import { WebviewWindow } from '@tauri-apps/api/webviewWindow' | ||
* 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 = {} | ||
& WindowOptions & { skip?: boolean } = {} | ||
amrbashir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) { | ||
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) { | ||
const internalOptions = options as InternalWebviewOptions | ||
|
||
if (!internalOptions?.skip) { | ||
amrbashir marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
invoke('plugin:webview|create_webview_window', { | ||
options: { | ||
...options, | ||
...internalOptions, | ||
parent: | ||
typeof options.parent === 'string' | ||
? options.parent | ||
: options.parent?.label, | ||
typeof internalOptions.parent === 'string' | ||
? internalOptions.parent | ||
: internalOptions.parent?.label, | ||
label | ||
} | ||
}) | ||
|
@@ -98,104 +84,48 @@ class WebviewWindow { | |
} | ||
} | ||
|
||
/** | ||
* Gets the Webview for the webview associated with the given label. | ||
* @example | ||
* ```typescript | ||
* import { Webview } from '@tauri-apps/api/webviewWindow'; | ||
* 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 async getByLabel(label: string): Promise<WebviewWindow | null> { | ||
const webview = | ||
(await getAllWebviewWindows()).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 new WebviewWindow(webview.label, { skip: true } as any) | ||
KushalMeghani1644 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
return null | ||
} | ||
|
||
/** | ||
* Get an instance of `Webview` for the current webview. | ||
*/ | ||
static getCurrent(): WebviewWindow { | ||
return getCurrentWebviewWindow() | ||
} | ||
|
||
/** | ||
* Gets a list of instances of `Webview` for all available webviews. | ||
*/ | ||
Comment on lines
-129
to
-131
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert this |
||
static async getAll(): Promise<WebviewWindow[]> { | ||
return getAllWebviewWindows() | ||
} | ||
|
||
/** | ||
* Listen to an emitted event on this webivew window. | ||
* | ||
* @example | ||
* ```typescript | ||
* import { WebviewWindow } from '@tauri-apps/api/webviewWindow'; | ||
* 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<T>( | ||
event: EventName, | ||
handler: EventCallback<T> | ||
): Promise<UnlistenFn> { | ||
if (this._handleTauriEvent(event, handler)) { | ||
return () => { | ||
// eslint-disable-next-line security/detect-object-injection | ||
const listeners = this.listeners[event] | ||
listeners.splice(listeners.indexOf(handler), 1) | ||
const idx = listeners.indexOf(handler) | ||
if (idx >= 0) listeners.splice(idx, 1) | ||
} | ||
} | ||
return listen(event, handler, { | ||
target: { kind: 'WebviewWindow', label: this.label } | ||
}) | ||
} | ||
|
||
/** | ||
* Listen to an emitted event on this webview window only once. | ||
* | ||
* @example | ||
* ```typescript | ||
* import { WebviewWindow } from '@tauri-apps/api/webviewWindow'; | ||
* 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<T>( | ||
event: EventName, | ||
handler: EventCallback<T> | ||
): Promise<UnlistenFn> { | ||
if (this._handleTauriEvent(event, handler)) { | ||
return () => { | ||
// eslint-disable-next-line security/detect-object-injection | ||
const listeners = this.listeners[event] | ||
listeners.splice(listeners.indexOf(handler), 1) | ||
const idx = listeners.indexOf(handler) | ||
if (idx >= 0) listeners.splice(idx, 1) | ||
} | ||
} | ||
return once(event, handler, { | ||
|
@@ -206,27 +136,18 @@ class WebviewWindow { | |
/** | ||
* Set the window and webview background color. | ||
* | ||
* #### Platform-specific: | ||
* | ||
* - **Android / iOS:** Unsupported for the window layer. | ||
* - **macOS / iOS**: Not implemented for the webview layer. | ||
* - **Windows**: | ||
* - alpha channel is ignored for the window layer. | ||
* - On Windows 7, alpha channel is ignored for the webview layer. | ||
* - On Windows 8 and newer, if alpha channel is not `0`, it will be ignored. | ||
* | ||
* @returns A promise indicating the success or failure of the operation. | ||
* | ||
Comment on lines
209
to
219
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert this |
||
* @since 2.1.0 | ||
*/ | ||
async setBackgroundColor(color: Color): Promise<void> { | ||
return invoke('plugin:window|set_background_color', { color }).then(() => { | ||
return invoke('plugin:webview|set_webview_background_color', { color }) | ||
}) | ||
try { | ||
await invoke('plugin:window|set_background_color', { color }) | ||
await invoke('plugin:webview|set_webview_background_color', { color }) | ||
} catch (err) { | ||
throw new Error(`Failed to set background color: ${String(err)}`) | ||
amrbashir marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
// Order matters, we use window APIs by default | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert this comment, it is vital |
||
applyMixins(WebviewWindow, [Window, Webview]) | ||
|
||
/** Extends a base class by other specified classes, without overriding existing properties */ | ||
|
@@ -243,12 +164,12 @@ function applyMixins( | |
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) | ||
) | ||
|
Uh oh!
There was an error while loading. Please reload this page.