MCPcopy Create free account
hub / github.com/coderoad/coderoad-vscode / createReactWebView

Function createReactWebView

src/services/webview/create.ts:20–100  ·  view source on GitHub ↗
({ extensionPath, channel }: ReactWebViewProps)

Source from the content-addressed store, hash-verified

18const state = { loaded: false }
19
20const createReactWebView = ({ extensionPath, channel }: ReactWebViewProps): Output => {
21 // throttle "already open" popup
22 let lastWebviewOpenedAt = new Date()
23
24 // TODO add disposables
25 const disposables: vscode.Disposable[] = []
26
27 function createWebViewPanel(): vscode.WebviewPanel {
28 const viewType = 'CodeRoad'
29 const title = 'CodeRoad'
30 const config = {
31 // Enable javascript in the webview
32 enableScripts: true,
33 // And restrict the webview to only loading content from our extension's `media` directory.
34 localResourceRoots: [vscode.Uri.file(path.join(extensionPath, 'build'))],
35 // prevents destroying the window when it is in the background
36 retainContextWhenHidden: true,
37 // allows scripts to load external resources (eg. markdown images, fonts)
38 enableCommandUris: true,
39 }
40 state.loaded = true
41 return vscode.window.createWebviewPanel(viewType, title, vscode.ViewColumn.Two, config)
42 }
43
44 let panel: vscode.WebviewPanel = createWebViewPanel()
45
46 // Listen for when the panel is disposed
47 // This happens when the user closes the panel or when the panel is closed programmatically
48 panel.onDidDispose(
49 () => {
50 panel.dispose()
51 state.loaded = false
52 },
53 null,
54 disposables,
55 )
56
57 // Handle messages from the webview
58 const receive = channel.receive
59 const send = (action: T.Action) =>
60 panel.webview.postMessage({
61 ...action,
62 source: 'coderoad', // filter events on client by source. origin is not reliable
63 })
64
65 panel.webview.onDidReceiveMessage(receive, null, disposables)
66
67 // panel.onDidDispose(() => {
68 // // Clean up our resources
69 // loaded = false
70 // panel.dispose()
71 // Promise.all(disposables.map((x) => x.dispose()))
72 // })
73
74 const rootPath = path.join(extensionPath, 'build')
75 render(panel, rootPath)
76
77 return {

Callers

nothing calls this directly

Calls 2

createWebViewPanelFunction · 0.85
renderFunction · 0.85

Tested by

no test coverage detected