| // Copyright 2022 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| import { WebviewApi } from 'vscode-webview'; |
| import { ExternalActionRequestEvent, LoggingView } from './components/view'; |
| import { PersistedState, State } from './src/state'; |
| |
| //@ts-check |
| |
| // This script will be run within the webview itself |
| // It cannot access the main VS Code APIs directly. |
| (function () { |
| const vscode = acquireVsCodeApi() as WebviewApi<PersistedState>; |
| const view = new LoggingView(new State(vscode), document.body); |
| |
| // TODO(fxbug.dev/109719): move this to the persisted state. |
| let playing = true; |
| |
| // Handle messages sent from the extension to the webview |
| window.addEventListener('message', event => { |
| const message = event.data; |
| switch (message.type) { |
| case 'addLog': |
| if (playing) { |
| view.addLog(message.log); |
| } |
| break; |
| case 'reset': |
| view.reset(); |
| } |
| }); |
| |
| // Forward webview messages to the extension. |
| view.addEventListener(LoggingView.externalActionRequestEvent, (e) => { |
| const event = (e as CustomEvent).detail as ExternalActionRequestEvent; |
| switch (event.type) { |
| case 'pause-log-streaming': |
| playing = false; |
| vscode.postMessage({ |
| command: 'pauseLogStreaming' |
| }); |
| break; |
| case 'resume-log-streaming': |
| playing = true; |
| vscode.postMessage({ |
| command: 'resumeLogStreaming' |
| }); |
| break; |
| } |
| }); |
| }()); |