-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(webview): restructure and re-organize code, add jsdocs
Signed-off-by: Konstantinos Maninakis <maninak@protonmail.com>
- Loading branch information
Showing
11 changed files
with
128 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,22 @@ | ||
import type { Webview } from 'vscode' | ||
import { getVscodeRef } from '../src/webviews/src/utils/getVscodeRef' | ||
|
||
let vscode: ReturnType<typeof acquireVsCodeApi> | undefined | ||
|
||
export function getVscodeRef() { | ||
if (vscode) { | ||
return vscode | ||
} | ||
|
||
return (vscode = acquireVsCodeApi()) | ||
interface Message<Command extends string, Payload extends object | undefined = undefined> { | ||
command: Command | ||
payload: Payload | ||
} | ||
|
||
export interface MessageToWebview { | ||
command: 'resetCount' | ||
} | ||
type MessageToWebview = Message<'resetCount'> | ||
|
||
export function postMessageToWebview(message: MessageToWebview, webview: Webview): void { | ||
webview.postMessage(message) | ||
} | ||
type MessageToExtension = Message<'showInfoNotification', { text: string }> | ||
// append more message types with `| Message<'...', SomeType>` | ||
|
||
export interface MessageToExtension { | ||
command: 'showInfoNotification' | ||
text: string | ||
/** Sends a message, usually from the host window, to the provided webview. */ | ||
export function notifyWebview(message: MessageToWebview, webview: Webview): void { | ||
webview.postMessage(message) | ||
} | ||
|
||
export function postMessageToExtension(message: MessageToExtension): void { | ||
/** Sends a message from within a webview to the VS Code extension hosting it. */ | ||
export function notifyExtension(message: MessageToExtension): void { | ||
getVscodeRef().postMessage(message) | ||
} | ||
|
||
// Source: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/vscode-webview/index.d.ts | ||
/** | ||
* API exposed to webviews. | ||
* | ||
* @template StateType Type of the persisted state stored for the webview. | ||
*/ | ||
export interface WebviewApi<StateType> { | ||
/** | ||
* Post a message to the owner of the webview. | ||
* | ||
* @param message Data to post. Must be JSON serializable. | ||
*/ | ||
postMessage(message: unknown): void | ||
|
||
/** | ||
* Get the persistent state stored for this webview. | ||
* | ||
* @return The current state or `undefined` if no state has been set. | ||
*/ | ||
getState(): StateType | undefined | ||
|
||
/** | ||
* Set the persistent state stored for this webview. | ||
* | ||
* @param newState New persisted state. This must be a JSON serializable object. Can be retrieved | ||
* using {@link getState}. | ||
* | ||
* @return The new state. | ||
*/ | ||
setState<T extends StateType | undefined>(newState: T): T | ||
} | ||
|
||
declare global { | ||
/** | ||
* Acquire an instance of the webview API. | ||
* | ||
* This may only be called once in a webview's context. Attempting to call `acquireVsCodeApi` after it has already | ||
* been called will throw an exception. | ||
* | ||
* @template StateType Type of the persisted state stored for the webview. | ||
*/ | ||
function acquireVsCodeApi<StateType = unknown>(): WebviewApi<StateType> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/webviews/src/composables/registerWebviewMessageHandlers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
import { useEventListener } from '@vueuse/core' | ||
import { useCounterStore } from '@/stores/counter' | ||
import type { notifyWebview } from 'lib/webview-messaging' | ||
import { assertUnreachable } from 'utils/assertUnreachable' | ||
|
||
/** | ||
* Registers a handler for each possible message the extension can post to the webview. | ||
*/ | ||
export function registerWebviewMessageHandlers(){ | ||
useEventListener(window, 'message', (event: MessageEvent<Parameters<typeof notifyWebview>['0']>) => { | ||
const message = event.data | ||
|
||
switch (message.command) { | ||
case 'resetCount': | ||
useCounterStore().reset() | ||
break | ||
default: | ||
assertUnreachable(message.command) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
let vscode: ReturnType<typeof acquireVsCodeApi> | undefined | ||
|
||
/** Resolves a reference to the VS Code context auto-injected in a webview. */ | ||
export function getVscodeRef() { | ||
if (vscode) { | ||
return vscode | ||
} | ||
|
||
return (vscode = acquireVsCodeApi()) | ||
} | ||
|
||
// Typings copied from | ||
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/vscode-webview/index.d.ts | ||
|
||
/** | ||
* API exposed to webviews. | ||
* | ||
* @template StateType Type of the persisted state stored for the webview. | ||
*/ | ||
export interface WebviewApi<StateType> { | ||
/** | ||
* Post a message to the owner of the webview. | ||
* | ||
* @param message Data to post. Must be JSON serializable. | ||
*/ | ||
postMessage(message: unknown): void | ||
|
||
/** | ||
* Get the persistent state stored for this webview. | ||
* | ||
* @return The current state or `undefined` if no state has been set. | ||
*/ | ||
getState(): StateType | undefined | ||
|
||
/** | ||
* Set the persistent state stored for this webview. | ||
* | ||
* @param newState New persisted state. This must be a JSON serializable object. Can be retrieved | ||
* using {@link getState}. | ||
* | ||
* @return The new state. | ||
*/ | ||
setState<T extends StateType | undefined>(newState: T): T | ||
} | ||
|
||
declare global { | ||
/** | ||
* Acquire an instance of the webview API. | ||
* | ||
* This may only be called once in a webview's context. Attempting to call `acquireVsCodeApi` after it has already | ||
* been called will throw an exception. | ||
* | ||
* @template StateType Type of the persisted state stored for the webview. | ||
*/ | ||
function acquireVsCodeApi<StateType = unknown>(): WebviewApi<StateType> | ||
} |