Files
vscode-extension-samples/webview-sample/media/main.js

42 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

2018-04-24 15:07:32 -07:00
// This script will be run within the webview itself
// It cannot access the main VS Code APIs directly.
2021-06-05 22:46:15 -07:00
(function () {
const vscode = acquireVsCodeApi();
2018-04-24 15:07:32 -07:00
2021-06-05 22:46:15 -07:00
const oldState = /** @type {{ count: number} | undefined} */ (vscode.getState());
2018-06-28 16:38:49 -07:00
2021-06-05 22:46:15 -07:00
const counter = /** @type {HTMLElement} */ (document.getElementById('lines-of-code-counter'));
2021-05-27 15:53:59 -07:00
console.log('Initial state', oldState);
2018-06-28 16:38:49 -07:00
let currentCount = (oldState && oldState.count) || 0;
2021-06-05 22:46:15 -07:00
counter.textContent = `${currentCount}`;
2018-04-24 15:07:32 -07:00
setInterval(() => {
2021-06-05 22:46:15 -07:00
counter.textContent = `${currentCount++} `;
2018-04-24 15:07:32 -07:00
2018-06-28 16:38:49 -07:00
// Update state
2018-07-18 18:56:40 -07:00
vscode.setState({ count: currentCount });
2018-06-28 16:38:49 -07:00
// Alert the extension when the cat introduces a bug
if (Math.random() < Math.min(0.001 * currentCount, 0.05)) {
// Send a message back to the extension
vscode.postMessage({
command: 'alert',
text: '🐛 on line ' + currentCount
});
}
}, 100);
2018-04-24 15:07:32 -07:00
2018-04-26 14:00:51 -07:00
// Handle messages sent from the extension to the webview
window.addEventListener('message', event => {
2018-04-26 14:00:51 -07:00
const message = event.data; // The json data that the extension sent
switch (message.command) {
case 'refactor':
currentCount = Math.ceil(currentCount * 0.5);
2021-06-05 22:46:15 -07:00
counter.textContent = `${currentCount}`;
break;
}
});
2021-10-01 21:30:20 -04:00
}());