Skip to content

Commit a5163d6

Browse files
committed
feat: add message passing examples
1 parent b38e7e4 commit a5163d6

File tree

5 files changed

+83
-26
lines changed

5 files changed

+83
-26
lines changed

ext-src/extension.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,30 @@ class WebPanel {
3636
return WebPanel.currentPanel;
3737
}
3838

39+
public static updateTitle(extensionPath: string) {
40+
const column = vscode.window.activeTextEditor
41+
? vscode.window.activeTextEditor.viewColumn
42+
: undefined;
43+
44+
// If we already have a panel, show it.
45+
// Otherwise, create angular panel.
46+
if (WebPanel.currentPanel) {
47+
WebPanel.currentPanel.panel.reveal(column);
48+
} else {
49+
WebPanel.currentPanel = new WebPanel(
50+
extensionPath,
51+
column || vscode.ViewColumn.One,
52+
);
53+
}
54+
55+
WebPanel.currentPanel.panel.webview.postMessage({
56+
command: 'update-title',
57+
payload: {
58+
title: `Hello from ${extensionPath} - ${Date().toString()}`,
59+
},
60+
});
61+
}
62+
3963
private constructor(extensionPath: string, column: vscode.ViewColumn) {
4064
this.extensionPath = extensionPath;
4165
this.builtAppFolder = 'dist';
@@ -65,9 +89,10 @@ class WebPanel {
6589

6690
// Handle messages from the webview
6791
this.panel.webview.onDidReceiveMessage(
68-
(message: any) => {
92+
(message) => {
6993
switch (message.command) {
70-
case 'alert':
94+
case 'notification':
95+
vscode.window.showInformationMessage(message.data.text);
7196
vscode.window.showErrorMessage(message.text);
7297
return;
7398
}
@@ -128,4 +153,10 @@ export function activate(context: vscode.ExtensionContext) {
128153
WebPanel.createOrShow(context.extensionPath);
129154
}),
130155
);
156+
157+
context.subscriptions.push(
158+
vscode.commands.registerCommand('angular-webview.update-title', () => {
159+
WebPanel.updateTitle(context.extensionPath);
160+
}),
161+
);
131162
}

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
"command": "angular-webview.start",
1414
"title": "Open webview",
1515
"category": "Angular"
16+
},
17+
{
18+
"command": "angular-webview.update-title",
19+
"title": "Update title",
20+
"category": "Angular"
1621
}
1722
]
1823
},

screenshot.png

456 KB
Loading

src/app/app.component.html

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,14 @@ <h1>Welcome to {{ title }}!</h1>
77
src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNTAgMjUwIj4KICAgIDxwYXRoIGZpbGw9IiNERDAwMzEiIGQ9Ik0xMjUgMzBMMzEuOSA2My4ybDE0LjIgMTIzLjFMMTI1IDIzMGw3OC45LTQzLjcgMTQuMi0xMjMuMXoiIC8+CiAgICA8cGF0aCBmaWxsPSIjQzMwMDJGIiBkPSJNMTI1IDMwdjIyLjItLjFWMjMwbDc4LjktNDMuNyAxNC4yLTEyMy4xTDEyNSAzMHoiIC8+CiAgICA8cGF0aCAgZmlsbD0iI0ZGRkZGRiIgZD0iTTEyNSA1Mi4xTDY2LjggMTgyLjZoMjEuN2wxMS43LTI5LjJoNDkuNGwxMS43IDI5LjJIMTgzTDEyNSA1Mi4xem0xNyA4My4zaC0zNGwxNy00MC45IDE3IDQwLjl6IiAvPgogIDwvc3ZnPg=="
88
/>
99
</div>
10-
<h2>Here are some links to help you start:</h2>
11-
<ul>
12-
<li>
13-
<h2>
14-
<a target="_blank" rel="noopener" href="https://angular.io/tutorial"
15-
>Tour of Heroes</a
16-
>
17-
</h2>
18-
</li>
19-
<li>
20-
<h2>
21-
<a target="_blank" rel="noopener" href="https://angular.io/cli"
22-
>CLI Documentation</a
23-
>
24-
</h2>
25-
</li>
26-
<li>
27-
<h2>
28-
<a target="_blank" rel="noopener" href="https://blog.angular.io/"
29-
>Angular blog</a
30-
>
31-
</h2>
32-
</li>
33-
</ul>
10+
11+
<h2>Send message from Webview to Extension</h2>
12+
<textarea #text>Hello world notification!</textarea><br />
13+
<button (click)="postToExtension(text.value)">Send message to extension</button>
14+
<p>Should produce a notification from the extension process.</p>
15+
16+
<h2>Send message from Extension to Webview</h2>
17+
<p>
18+
Open Command Palette (Cmd+Shift) and select "Angular: Update title" to update
19+
the title in Webview from the extension process.
20+
</p>

src/app/app.component.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,44 @@
11
import { Component } from '@angular/core';
22

3+
declare function acquireVsCodeApi(): {
4+
postMessage(options: { command: string; data: unknown }): void;
5+
};
6+
37
@Component({
48
selector: 'app-root',
59
templateUrl: './app.component.html',
610
standalone: true,
11+
styles: `button {
12+
background: rgb(108, 108, 255);
13+
color: white;
14+
padding: 5px 20px;
15+
border-radius: 4px;
16+
border: none;
17+
}`,
718
})
819
export class AppComponent {
920
title = 'VSCode Webview Angular';
21+
vscode = acquireVsCodeApi();
22+
23+
constructor() {
24+
window.addEventListener(
25+
'message',
26+
(
27+
event: MessageEvent<{ command: string; payload: { title: string } }>,
28+
) => {
29+
if (event.data.command === 'update-title') {
30+
this.title = event.data.payload.title;
31+
}
32+
},
33+
);
34+
}
35+
36+
postToExtension(text: string) {
37+
this.vscode.postMessage({
38+
command: 'notification',
39+
data: {
40+
text,
41+
},
42+
});
43+
}
1044
}

0 commit comments

Comments
 (0)