Skip to content

Commit ffcd280

Browse files
committed
Improving documentation on webviews
Fixes microsoft#91656
1 parent 033a5d4 commit ffcd280

2 files changed

Lines changed: 45 additions & 12 deletions

File tree

src/vs/vscode.d.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6325,7 +6325,7 @@ declare module 'vscode' {
63256325
}
63266326

63276327
/**
6328-
* A webview displays html content, like an iframe.
6328+
* Displays html content, similarly to an iframe.
63296329
*/
63306330
export interface Webview {
63316331
/**
@@ -6334,9 +6334,29 @@ declare module 'vscode' {
63346334
options: WebviewOptions;
63356335

63366336
/**
6337-
* Contents of the webview.
6337+
* HTML contents of the webview.
63386338
*
6339-
* Should be a complete html document.
6339+
* This should be a complete, valid html document. Changing this property causes the webview to be reloaded.
6340+
*
6341+
* Webviews are sandboxed from normal extension process, so all communication with the webview must use
6342+
* message passing. To send a message from the extension to the webview, use [`postMessage`](#Webview.postMessage).
6343+
* To send message from the webview back to an extension, use the `acquireVsCodeApi` function inside the webview
6344+
* to get a handle to VS Code's api and then call `.postMessage()`:
6345+
*
6346+
* ```html
6347+
* <script>
6348+
* const vscode = acquireVsCodeApi(); // acquireVsCodeApi can only be invoked once
6349+
* vscode.postMessage({ message: 'hello!' });
6350+
* </script>
6351+
* ```
6352+
*
6353+
* To load a resources from the workspace inside a webview, use the `[asWebviewUri](#Webview.asWebviewUri)` method
6354+
* and ensure the resource's directory is listed in [`WebviewOptions.localResourceRoots`](#WebviewOptions.localResourceRoots).
6355+
*
6356+
* Keep in mind that even though webviews are sandboxed, they still allow running scripts and loading arbitrary content,
6357+
* so extensions must follow all standard web security best practices when working with webviews. This includes
6358+
* properly sanitizing all untrusted input (including content from the workspace) and
6359+
* setting a [content security policy](https://aka.ms/vscode-api-webview-csp).
63406360
*/
63416361
html: string;
63426362

@@ -6434,7 +6454,7 @@ declare module 'vscode' {
64346454
iconPath?: Uri | { light: Uri; dark: Uri };
64356455

64366456
/**
6437-
* Webview belonging to the panel.
6457+
* [`Webview`](#Webview) belonging to the panel.
64386458
*/
64396459
readonly webview: Webview;
64406460

src/vs/vscode.proposed.d.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,10 +1320,10 @@ declare module 'vscode' {
13201320
}
13211321

13221322
/**
1323-
* Represents a custom document for a custom webview editor.
1323+
* Represents a custom document used by a `CustomEditorProvider`.
13241324
*
13251325
* Custom documents are only used within a given `CustomEditorProvider`. The lifecycle of a
1326-
* `CustomDocument` is managed by VS Code. When more more references remain to a given `CustomDocument`
1326+
* `CustomDocument` is managed by VS Code. When no more references remain to a given `CustomDocument`,
13271327
* then it is disposed of.
13281328
*
13291329
* @param UserDataType Type of custom object that extensions can store on the document.
@@ -1366,6 +1366,11 @@ declare module 'vscode' {
13661366
/**
13671367
* Resolve the model for a given resource.
13681368
*
1369+
* `resolveCustomDocument` is called when the first editor for a given resource is opened, and the resolve document
1370+
* is passed to `resolveCustomEditor`. The resolved `CustomDocument` is re-used for subsequent editor opens.
1371+
* If all editors for a given resource are closed, the `CustomDocument` is disposed of. Opening an editor at
1372+
* this point will trigger another call to `resolveCustomDocument`.
1373+
*
13691374
* @param document Document to resolve.
13701375
*
13711376
* @return The capabilities of the resolved document.
@@ -1375,11 +1380,15 @@ declare module 'vscode' {
13751380
/**
13761381
* Resolve a webview editor for a given resource.
13771382
*
1383+
* This is called when a user first opens a resource for a `CustomTextEditorProvider`, or if they reopen an
1384+
* existing editor using this `CustomTextEditorProvider`.
1385+
*
13781386
* To resolve a webview editor, the provider must fill in its initial html content and hook up all
1379-
* the event listeners it is interested it. The provider should also take ownership of the passed in `WebviewPanel`.
1387+
* the event listeners it is interested it. The provider can also hold onto the `WebviewPanel` to use later,
1388+
* for example in a command. See [`WebviewPanel`](#WebviewPanel) for additional details
13801389
*
13811390
* @param document Document for the resource being resolved.
1382-
* @param webviewPanel Webview to resolve. The provider should take ownership of this webview.
1391+
* @param webviewPanel Webview to resolve.
13831392
*
13841393
* @return Thenable indicating that the webview editor has been resolved.
13851394
*/
@@ -1398,13 +1407,17 @@ declare module 'vscode' {
13981407
*/
13991408
export interface CustomTextEditorProvider {
14001409
/**
1401-
* Resolve a webview editor for a given resource.
1410+
* Resolve a webview editor for a given text resource.
1411+
*
1412+
* This is called when a user first opens a resource for a `CustomTextEditorProvider`, or if they reopen an
1413+
* existing editor using this `CustomTextEditorProvider`.
14021414
*
14031415
* To resolve a webview editor, the provider must fill in its initial html content and hook up all
1404-
* the event listeners it is interested it. The provider should also take ownership of the passed in `WebviewPanel`.
1416+
* the event listeners it is interested it. The provider can also hold onto the `WebviewPanel` to use later,
1417+
* for example in a command. See [`WebviewPanel`](#WebviewPanel) for additional details.
14051418
*
1406-
* @param document Resource being resolved.
1407-
* @param webviewPanel Webview to resolve. The provider should take ownership of this webview.
1419+
* @param document Document for the resource to resolve.
1420+
* @param webviewPanel Webview to resolve.
14081421
*
14091422
* @return Thenable indicating that the webview editor has been resolved.
14101423
*/

0 commit comments

Comments
 (0)