Webviews and Custom Editors
Relevant source files
The following files were used as context for generating this wiki page:- extensions/markdown-language-features/package.json
- extensions/markdown-language-features/package.nls.json
- extensions/simple-browser/package.json
- extensions/markdown-math/package.json
- extensions/github/package.json
- extensions/html-language-features/package.nls.json
- extensions/typescript-language-features/package.nls.json
Webviews and Custom Editors are powerful features that allow extensions to create fully customizable views and editors within the VS Code UI using standard web technologies (HTML, CSS, JavaScript). They are fundamental for implementing complex user interfaces, such as live previews, custom document renderers, and integrated tools that go beyond the standard text editor. Extensions like Markdown Language Features use them to provide rich document previews, while others like Simple Browser use them to embed a web browser directly into a tab.
These features are primarily declared and configured within an extension's package.json file through various contribution points. This includes defining custom editors for specific file types, registering commands to open webview panels, and specifying activation events that trigger the extension when a webview becomes active. This system allows for a highly integrated and seamless user experience.
Contribution and Activation
Extensions register Webviews and Custom Editors through the contributes section of their package.json. The primary mechanisms are the customEditors contribution point and commands that programmatically create WebviewPanel instances.
Sources: extensions/markdown-language-features/package.json, extensions/simple-browser/package.json
Activation Events
To ensure extensions are loaded only when needed, specific activation events are used. The most common ones for webviews are:
onCommand:<commandId>: Activates the extension when a specific command is executed, such as a command to open a preview.onWebviewPanel:<viewType>: Activates the extension whenever aWebviewPanelwith a matchingviewTypeis being restored.onLanguage:<languageId>: A general activation event that can be used to make commands for webviews available when a certain file type is open.
Sources: extensions/markdown-language-features/package.json:20-22, extensions/simple-browser/package.json:29-32
The following diagram illustrates the activation flow for the Markdown preview.
This diagram shows the sequence of activation events when a user opens a Markdown file and then its preview.
Sources: extensions/markdown-language-features/package.json:18-22
Custom Editors
The customEditors contribution point allows an extension to register a custom, webview-based editor for specific file types. This is a more structured approach than creating a simple webview panel and is the preferred method for document-based views.
The Markdown Language Features extension uses this to provide its preview functionality.
"customEditors": [
{
"viewType": "vscode.markdown.preview.editor",
"displayName": "Markdown Preview",
"priority": "option",
"selector": [
{
"filenamePattern": "*.md"
}
]
}
]
Sources: extensions/markdown-language-features/package.json:416-427
The structure of a custom editor contribution is as follows:
This diagram shows the key properties of a customEditors contribution object.
Sources: extensions/markdown-language-features/package.json:416-427
| Property | Description | Example |
|---|---|---|
viewType | A unique identifier for the custom editor. | vscode.markdown.preview.editor |
displayName | The human-readable name shown to the user. | Markdown Preview |
priority | Defines when the editor is used. option makes it an alternative to the default, while default would make it the primary editor. | option |
selector | An array of objects that determine which files this editor can be used for. | [{ "filenamePattern": "*.md" }] |
Sources: extensions/markdown-language-features/package.json:416-427
Commands and UI Integration
Commands are the primary way for users to interact with and open webviews. These are defined in package.json and can be surfaced in various parts of the UI, such as the command palette, editor title bars, and context menus.
Key Commands
The Markdown extension defines several commands to manage its preview.
| Command | Title | Description |
|---|---|---|
markdown.showPreview | Open Preview | Opens the Markdown preview in the current editor group. |
markdown.showPreviewToSide | Open Preview to the Side | Opens the Markdown preview in an editor group to the side. |
markdown.showSource | Show Source | Switches from the preview back to the source Markdown file. |
markdown.preview.toggleLock | Toggle Preview Locking | Toggles whether the preview scrolls along with the editor. |
Sources: extensions/markdown-language-features/package.json:141-171, extensions/markdown-language-features/package.nls.json:18-24
The following sequence diagram shows the interaction when a user opens a preview to the side.
This diagram illustrates the flow when a user triggers the command to open the Markdown preview.
Sources: extensions/markdown-language-features/package.json:147-152
Menu Contributions
To make webview-related commands easily accessible, they are added to menus.
editor/title: Adds an icon to the editor's title bar. The Markdown preview icon is a prime example.webview/context: Adds items to the context menu inside a webview. The Markdown preview uses this to add "Copy Image" and "Open Image" actions when right-clicking an image in the preview.explorer/context: Adds commands to the context menu of files in the Explorer view.
Sources: extensions/markdown-language-features/package.json:186-224
Configuration and Styling
Webviews are highly configurable and can be styled using CSS. Configuration options are defined in package.json and allow users to customize the behavior and appearance of the webview.
Preview Configuration
The Markdown extension provides a rich set of configurations for its preview.
| Setting | Type | Default | Description |
|---|---|---|---|
markdown.preview.fontFamily | string | -apple-system, ... | Controls the font family used in the preview. |
markdown.preview.fontSize | number | 14 | Controls the font size in pixels. |
markdown.preview.lineHeight | number | 1.6 | Controls the line height, relative to the font size. |
markdown.preview.scrollPreviewWithEditor | boolean | true | When the editor is scrolled, the preview view is updated. |
markdown.preview.markEditorSelection | boolean | true | Marks the current editor selection in the preview. |
markdown.styles | array | [] | A list of URLs or local paths to CSS stylesheets to use in the preview. |
Sources: extensions/markdown-language-features/package.json:299-335, extensions/markdown-language-features/package.nls.json:8-14
Extensible Styling
One of the powerful aspects of the webview system is that other extensions can contribute styles to an existing webview. This is achieved through the markdown.previewStyles contribution point.
Extensions like markdown-math and github use this to inject their own CSS into the standard Markdown preview, allowing them to render math equations (via KaTeX) and apply GitHub-specific styling, respectively.
This diagram shows how different extensions contribute styles to the central Markdown Preview webview.
Sources: extensions/markdown-math/package.json:71-74, extensions/github/package.json:215-217
This modular approach allows for a rich, extensible ecosystem built around a core webview feature.
Conclusion
Webviews and Custom Editors are a cornerstone of VS Code's extensibility model. They provide a sandboxed yet powerful way for extensions to render custom content and create unique user experiences using web technologies. Through a well-defined system of contribution points in package.json, extensions can register custom editors, define activation events, add commands and UI elements, and even allow other extensions to augment their appearance. This architecture is exemplified by the Markdown preview, which serves as a platform for other extensions like markdown-math to build upon, showcasing a collaborative and modular extension ecosystem.