|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import * as vscode from 'vscode' |
| 19 | +import { |
| 20 | + ChangeKind, |
| 21 | + ChangeRequest, |
| 22 | + ViewportDataRequest, |
| 23 | +} from 'omega-edit/omega_edit_pb' |
| 24 | +import { |
| 25 | + client, |
| 26 | + getVersion, |
| 27 | + newSession, |
| 28 | + newViewport, |
| 29 | + deleteSession, |
| 30 | + // deleteViewport, |
| 31 | + randomId, |
| 32 | + // uri, |
| 33 | + viewportSubscribe, |
| 34 | +} from './omegaUtils' |
| 35 | +import { startServer, stopServer } from './server' |
| 36 | + |
| 37 | +let serverRunning = false |
| 38 | + |
| 39 | +async function cleanupViewportSession( |
| 40 | + sessionId: string, |
| 41 | + viewportIds: Array<string> |
| 42 | +) { |
| 43 | + // viewportIds.forEach(async (vId) => { |
| 44 | + // await deleteViewport(vId) |
| 45 | + // }) |
| 46 | + |
| 47 | + await deleteSession(sessionId) |
| 48 | +} |
| 49 | + |
| 50 | +export function activate(ctx: vscode.ExtensionContext) { |
| 51 | + ctx.subscriptions.push( |
| 52 | + vscode.commands.registerCommand('omega.version', async () => { |
| 53 | + if (!serverRunning) { |
| 54 | + await startServer(ctx) |
| 55 | + serverRunning = true |
| 56 | + } |
| 57 | + let v = await getVersion() |
| 58 | + vscode.window.showInformationMessage(v) |
| 59 | + }) |
| 60 | + ) |
| 61 | + |
| 62 | + ctx.subscriptions.push( |
| 63 | + vscode.commands.registerCommand('omega.grpc', async () => { |
| 64 | + if (!serverRunning) { |
| 65 | + await startServer(ctx) |
| 66 | + serverRunning = true |
| 67 | + } |
| 68 | + |
| 69 | + let panel = vscode.window.createWebviewPanel( |
| 70 | + 'viewport', |
| 71 | + 'Ω Edit gRPC', |
| 72 | + vscode.ViewColumn.One, |
| 73 | + { |
| 74 | + enableScripts: true, |
| 75 | + } |
| 76 | + ) |
| 77 | + |
| 78 | + // panel.webview.html = getWebviewContent(uri) |
| 79 | + panel.webview.html = getWebviewContent() |
| 80 | + |
| 81 | + let fileToEdit = await vscode.window |
| 82 | + .showOpenDialog({ |
| 83 | + canSelectMany: false, |
| 84 | + openLabel: 'Select', |
| 85 | + canSelectFiles: true, |
| 86 | + canSelectFolders: false, |
| 87 | + }) |
| 88 | + .then((fileUri) => { |
| 89 | + if (fileUri && fileUri[0]) { |
| 90 | + return fileUri[0].fsPath |
| 91 | + } |
| 92 | + }) |
| 93 | + |
| 94 | + let s = await newSession(fileToEdit) |
| 95 | + // panel.webview.postMessage({ command: 'session', text: s }) |
| 96 | + |
| 97 | + let vpin = await newViewport(randomId().toString(), s, 0, 1000) |
| 98 | + let vp1 = await newViewport(randomId().toString(), s, 0, 64) |
| 99 | + let vp2 = await newViewport(randomId().toString(), s, 64, 64) |
| 100 | + let vp3 = await newViewport(randomId().toString(), s, 128, 64) |
| 101 | + |
| 102 | + let vpdrin = new ViewportDataRequest() |
| 103 | + vpdrin.setViewportId(vpin) |
| 104 | + client.getViewportData(vpdrin, (err, r) => { |
| 105 | + let data = r?.getData_asB64() |
| 106 | + if (data) { |
| 107 | + let txt = Buffer.from(data, 'base64').toString('binary') |
| 108 | + panel.webview.postMessage({ command: 'input', text: txt }) |
| 109 | + } |
| 110 | + }) |
| 111 | + |
| 112 | + await viewportSubscribe(panel, vp1, vp1, 'viewport1', 'hex1') |
| 113 | + await viewportSubscribe(panel, vp1, vp2, 'viewport2', null) |
| 114 | + await viewportSubscribe(panel, vp1, vp3, 'viewport3', 'hex2') |
| 115 | + |
| 116 | + panel.webview.onDidReceiveMessage( |
| 117 | + (message) => { |
| 118 | + switch (message.command) { |
| 119 | + case 'send': |
| 120 | + let b64 = Buffer.from(message.text, 'binary').toString('base64') |
| 121 | + let change = new ChangeRequest() |
| 122 | + change.setSessionId(s) |
| 123 | + change.setKind(ChangeKind.CHANGE_OVERWRITE) |
| 124 | + change.setData(b64) |
| 125 | + change.setOffset(0) |
| 126 | + change.setLength(1000) |
| 127 | + client.submitChange(change, (err, r) => { |
| 128 | + if (err) console.log(err) |
| 129 | + else console.log(r) |
| 130 | + }) |
| 131 | + } |
| 132 | + }, |
| 133 | + undefined, |
| 134 | + ctx.subscriptions |
| 135 | + ) |
| 136 | + |
| 137 | + panel.onDidDispose( |
| 138 | + async () => { |
| 139 | + await cleanupViewportSession(s, [vpin, vp1, vp2, vp3]) |
| 140 | + panel.dispose() |
| 141 | + await stopServer() |
| 142 | + }, |
| 143 | + undefined, |
| 144 | + ctx.subscriptions |
| 145 | + ) |
| 146 | + }) |
| 147 | + ) |
| 148 | +} |
| 149 | + |
| 150 | +function getWebviewContent() { |
| 151 | + return `<!DOCTYPE html> |
| 152 | +<html lang="en"> |
| 153 | +<head> |
| 154 | + <meta charset="UTF-8"> |
| 155 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 156 | + <title>Omega gRPC</title> |
| 157 | + <style> |
| 158 | + .grid-container { |
| 159 | + display: grid; |
| 160 | + grid-gap: 2px 2px; |
| 161 | + grid-template-columns: auto auto auto; |
| 162 | + background-color: #2196F3; |
| 163 | + padding: 5px; |
| 164 | + } |
| 165 | +
|
| 166 | + .grid-item { |
| 167 | + background-color: rgba(255, 255, 255, 0.8); |
| 168 | + border: 1px solid rgba(0, 0, 0, 0.8); |
| 169 | + padding: 2px; |
| 170 | + font-size: 12px; |
| 171 | + text-align: left; |
| 172 | + color: black; |
| 173 | + white-space: pre; |
| 174 | + font-family: monospace; |
| 175 | + } |
| 176 | + </style> |
| 177 | +</head> |
| 178 | +<body> |
| 179 | + <!-- <div id="server">uri</div> --> |
| 180 | + <!-- <div id="session">?</div> --> |
| 181 | + <div class="grid-container"> |
| 182 | + <div class="grid-item" id="viewport1">empty</div> |
| 183 | + <div class="grid-item" id="viewport2">empty</div> |
| 184 | + <div class="grid-item" id="viewport3">empty</div> |
| 185 | + <div class="grid-item" id="hex1"></div> |
| 186 | + <div class="grid-item"><textarea id="input" rows="10" cols="50" oninput="sendit(this.value)"></textarea></div> |
| 187 | + <div class="grid-item" id="hex2"></div> |
| 188 | + </div> |
| 189 | + <script> |
| 190 | + const vscode = acquireVsCodeApi(); |
| 191 | + function sendit(value) { |
| 192 | + vscode.postMessage({ |
| 193 | + command: 'send', |
| 194 | + text: value |
| 195 | + }) |
| 196 | + } |
| 197 | + window.addEventListener('message', event => { |
| 198 | + const message = event.data; |
| 199 | + document.getElementById(message.command).innerHTML = message.text |
| 200 | + }); |
| 201 | + </script> |
| 202 | +</body> |
| 203 | +</html>` |
| 204 | +} |
0 commit comments