Skip to content

Commit

Permalink
Clean up some of the example code
Browse files Browse the repository at this point in the history
  • Loading branch information
mjbvz committed Apr 24, 2018
1 parent 2f94860 commit 7a2a0ba
Showing 1 changed file with 14 additions and 24 deletions.
38 changes: 14 additions & 24 deletions docs/extensions/webview.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,28 +350,15 @@ import * as path from 'path';
export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.commands.registerCommand('catCoding.newCat', () => {
const panel = vscode.window.createWebviewPanel('catCoding', "Cat Coding", vscode.ViewColumn.One, { });
panel.webview.html = getWebviewContent(context.extensionPath);
}));
}

function getWebviewContent(extensionPath) {
// Get path to resource on disk
const onDiskPath = vscode.Uri.file(path.join(extensionPath, 'media', 'cat.gif'));
// Get path to resource on disk
const onDiskPath = vscode.Uri.file(path.join(extensionPath, 'media', 'cat.gif'));

// And get the special uri to use with the webview
const catGifSrc = onDiskPath.with({ scheme: 'vscode-resource' });
// And get the special uri to use with the webview
const catGifSrc = onDiskPath.with({ scheme: 'vscode-resource' });

return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cat Coding</title>
</head>
<body>
<img src="${catGifSrc}" width="300" />
</body>
</html>`;
panel.webview.html = getWebviewContent(catGifSrc);
}));
}
```

Expand Down Expand Up @@ -407,7 +394,10 @@ export function activate(context: vscode.ExtensionContext) {
]
});

panel.webview.html = getWebviewContent(context.extensionPath);
const onDiskPath = vscode.Uri.file(path.join(extensionPath, 'media', 'cat.gif'));
const catGifSrc = onDiskPath.with({ scheme: 'vscode-resource' });

panel.webview.html = getWebviewContent(catGifSrc);
}));
}
```
Expand Down Expand Up @@ -500,7 +490,7 @@ export function activate(context: vscode.ExtensionContext) {

// Send a message to our webview.
// You can send any JSON serializable data.
currentPanel.webview.postMessage({ type: 'refactor' });
currentPanel.webview.postMessage({ command: 'refactor' });
}));
}

Expand Down Expand Up @@ -529,7 +519,7 @@ function getWebviewContent() {
const message = event.data; // The json data our extension sent
switch (message.type) {
switch (message.command) {
case 'refactor':
count = Math.ceil(count * 0.5);
counter.textContent = count;
Expand Down Expand Up @@ -560,7 +550,7 @@ export function activate(context: vscode.ExtensionContext) {

// Handle messages from the webview
panel.webview.onDidReceiveMessage(message => {
switch (message.type) {
switch (message.command) {
case 'alert':
vscode.window.showErrorMessage(message.text);
return;
Expand Down Expand Up @@ -591,7 +581,7 @@ function getWebviewContent() {
// Alert the extension when our cat introduces a bug
if (Math.random() < 0.001 * count) {
window.parent.postMessage(
{ type: 'alert', text: '🐛 on line ' + count },
{ command: 'alert', text: '🐛 on line ' + count },
'*')
}
}, 100);
Expand Down

0 comments on commit 7a2a0ba

Please sign in to comment.