-
-
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
base: dev
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -22,21 +22,17 @@ import type { Color, DragDropEvent } from './webview' | |
*/ | ||
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 }) | ||
} | ||
|
||
/** | ||
* Gets a list of instances of `Webview` for all available webview windows. | ||
* | ||
* @since 2.0.0 | ||
* Gets a list of instances of `Webview` for all available webviews. | ||
*/ | ||
async function getAllWebviewWindows(): Promise<WebviewWindow[]> { | ||
return invoke<string[]>('plugin:window|get_all_windows').then((windows) => | ||
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 | ||
}) | ||
) | ||
|
@@ -52,7 +48,6 @@ class WebviewWindow { | |
/** Local event listeners. */ | ||
// 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 | ||
|
@@ -75,14 +70,12 @@ class WebviewWindow { | |
constructor( | ||
label: WebviewLabel, | ||
options: Omit<WebviewOptions, 'x' | 'y' | 'width' | 'height'> | ||
& WindowOptions = {} | ||
& WindowOptions & { skip?: boolean } = {} | ||
) { | ||
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) { | ||
if (!(options && 'skip' in options && options.skip)) { | ||
invoke('plugin:webview|create_webview_window', { | ||
options: { | ||
...options, | ||
|
@@ -109,11 +102,11 @@ class WebviewWindow { | |
* @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 null | ||
|
@@ -126,13 +119,9 @@ class 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. | ||
* | ||
|
@@ -152,15 +141,16 @@ class WebviewWindow { | |
* @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, { | ||
|
@@ -187,22 +177,22 @@ class WebviewWindow { | |
* @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, { | ||
target: { kind: 'WebviewWindow', label: this.label } | ||
}) | ||
} | ||
|
||
/** | ||
* Set the window and webview background color. | ||
* | ||
|
@@ -220,12 +210,16 @@ class WebviewWindow { | |
* @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', { cause: err }) | ||
} | ||
} | ||
} | ||
|
||
|
||
// 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]) | ||
|
||
|
@@ -243,12 +237,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) | ||
) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert this