diff --git a/src/dataEditor/dataEditorClient.ts b/src/dataEditor/dataEditorClient.ts index 3ccbc9361..51874f11b 100644 --- a/src/dataEditor/dataEditorClient.ts +++ b/src/dataEditor/dataEditorClient.ts @@ -61,8 +61,8 @@ import XDGAppPaths from 'xdg-app-paths' import assert from 'assert' import { SvelteWebviewInitializer } from './svelteWebviewInitializer' import { - MessageCommands, - RobertMessageCommand, + MessageCommand, + MessageLevel, } from '../svelte/src/utilities/message' import { EditByteModes, @@ -305,26 +305,26 @@ export class DataEditorClient implements vscode.Disposable { } private async sendHeartbeat() { + + let heartBeatMsg: MessageCommand['HeartBeat'] = { + latency: heartbeatInfo.latency, + omegaEditPort: heartbeatInfo.omegaEditPort, + serverCpuLoadAverage: heartbeatInfo.serverCpuLoadAverage, + serverUptime: heartbeatInfo.serverUptime, + serverUsedMemory: heartbeatInfo.serverUsedMemory, + sessionCount: heartbeatInfo.sessionCount, + omegaEditPort: heartbeatInfo.omegaEditPort, + serverVersion: heartbeatInfo.serverInfo.serverVersion, + serverHostname: heartbeatInfo.serverInfo.serverHostname, + serverProcessId: heartbeatInfo.serverInfo.serverProcessId, + jvmVersion: heartbeatInfo.serverInfo.jvmVersion, + jvmVendor: heartbeatInfo.serverInfo.jvmVendor, + jvmPath: heartbeatInfo.serverInfo.jvmPath, + availableProcessors: heartbeatInfo.serverInfo.availableProcessors, + } await this.panel.webview.postMessage({ - command: MessageCommand.heartbeat, - data: { - latency: heartbeatInfo.latency, - omegaEditPort: heartbeatInfo.omegaEditPort, - serverCpuLoadAverage: heartbeatInfo.serverCpuLoadAverage, - serverUptime: heartbeatInfo.serverUptime, - serverUsedMemory: heartbeatInfo.serverUsedMemory, - sessionCount: heartbeatInfo.sessionCount, - serverInfo: { - omegaEditPort: heartbeatInfo.omegaEditPort, - serverVersion: heartbeatInfo.serverInfo.serverVersion, - serverHostname: heartbeatInfo.serverInfo.serverHostname, - serverProcessId: heartbeatInfo.serverInfo.serverProcessId, - jvmVersion: heartbeatInfo.serverInfo.jvmVersion, - jvmVendor: heartbeatInfo.serverInfo.jvmVendor, - jvmPath: heartbeatInfo.serverInfo.jvmPath, - availableProcessors: heartbeatInfo.serverInfo.availableProcessors, - }, - }, + command: 'Heartbeat', + data: heartBeatMsg, }) } @@ -357,18 +357,29 @@ export class DataEditorClient implements vscode.Disposable { } }) + let fileInfoMsg: MessageCommand['FileInfo'] = { + computedFileSize: data.computedFileSize, + fileName: data.fileName, + changeCount: data.changeCount, + undoCount: data.undoCount + } + // send the accumulated counts to the webview await this.panel.webview.postMessage({ - command: MessageCommand.fileInfo, - data: data, + command: 'FileInfo', + data: fileInfoMsg, }) } // handle messages from the webview - private async messageReceiver(message: IMessage) { - switch (message.command) { - case MessageCommand.showMessage: - switch (message.data.messageLevel as MessageLevel) { + private async messageReceiver() { + switch (MessageCommand[]) { + case 'Show': + let showMessage: MessageCommand['Show'] = { + messageLevel: , + message: , + } + switch (showMessage.messageLevel as MessageLevel) { case MessageLevel.Error: vscode.window.showErrorMessage(message.data.message) break @@ -381,7 +392,7 @@ export class DataEditorClient implements vscode.Disposable { } break - case MessageCommand.scrollViewport: + case 'ScrollViewport': await this.scrollViewport( this.panel, this.currentViewportId, @@ -390,22 +401,27 @@ export class DataEditorClient implements vscode.Disposable { ) break - case MessageCommand.editorOnChange: + case 'Edit': { - this.displayState.editorEncoding = message.data.encoding + let editMsg: MessageCommand['Edit'] = { + encoding: , + editMode: , + selectionData: , + } + this.displayState.editorEncoding = editMsg.encoding const encodeDataAs = - message.data.editMode === EditByteModes.Single + editMsg.editMode === EditByteModes.Single ? 'hex' : this.displayState.editorEncoding if ( - message.data.selectionData && - message.data.selectionData.length > 0 + editMsg.selectionData && + editMsg.selectionData.length > 0 ) { await this.panel.webview.postMessage({ command: MessageCommand.editorOnChange, display: dataToEncodedStr( - Buffer.from(message.data.selectionData), + Buffer.from(editMsg.selectionData), encodeDataAs ), }) @@ -415,52 +431,46 @@ export class DataEditorClient implements vscode.Disposable { // case MessageCommand.applyChanges: case 'apply': - let incomingMsg: RobertMessageCommand['Apply'] = message.data + let incomingMsg: MessageCommand['Apply'] = message.data incomingMsg.editedSegmentData incomingMsg.offset incomingMsg.originalSegmentData await edit( this.omegaSessionId, - message.data.offset, - message.data.originalSegment, - message.data.editedSegment + incomingMsg.offset, + incomingMsg.originalSegmentData, + incomingMsg.editedSegmentData ) await this.sendChangesInfo() break - case MessageCommand.undoChange: + case 'Undo' + let undoMsg: MessageCommand[] await undo(this.omegaSessionId) await this.sendChangesInfo() break - case MessageCommand.redoChange: + case 'Redo' await redo(this.omegaSessionId) await this.sendChangesInfo() break - case MessageCommand.profile: + case 'Profile': { - const startOffset: number = message.data.startOffset - const length: number = message.data.length - const byteProfile: number[] = await profileSession( - this.omegaSessionId, - startOffset, - length - ) + let profileMsg: MessageCommand['Profile'] = message.data + profileMsg.startOffset + profileMsg.length + profileMsg.byteProfile + await this.panel.webview.postMessage({ - command: MessageCommand.profile, - data: { - startOffset: startOffset, - length: length, - byteProfile: byteProfile, - numAscii: numAscii(byteProfile), - }, + command: 'Profile', + data: profileMsg, }) } break - case MessageCommand.clearChanges: + case 'Clear': if ( (await vscode.window.showInformationMessage( 'Are you sure you want to revert all changes?', @@ -474,11 +484,11 @@ export class DataEditorClient implements vscode.Disposable { } break - case MessageCommand.save: + case 'Save': await this.saveFile(this.fileToEdit) break - case MessageCommand.saveAs: + case 'SaveAs': { const uri = await vscode.window.showSaveDialog({ title: 'Save Session', @@ -490,29 +500,27 @@ export class DataEditorClient implements vscode.Disposable { } break - case MessageCommand.requestEditedData: + case 'RequestEditedData': { + let requestEditedDataMsg: MessageCommand['RequestEditedData'] = message.data const [selectionData, selectionDisplay] = fillRequestData(message) - await this.panel.webview.postMessage({ - command: MessageCommand.requestEditedData, - data: { - data: Uint8Array.from(selectionData), - dataDisplay: selectionDisplay, - }, + command: 'RequestEditedData', + data: requestEditedDataMsg, }) } break - case MessageCommand.replace: + case 'ReplaceResults': { + let replaceResultsMsg: MessageCommand['ReplaceResults'] = message.data const searchDataBytes = encodedStrToData( message.data.searchData, message.data.encoding ) const replaceDataBytes = encodedStrToData( message.data.replaceData, - message.data.encoding + message.data.encoding, ) const nextOffset = await replaceOneSession( this.omegaSessionId, @@ -530,49 +538,44 @@ export class DataEditorClient implements vscode.Disposable { await this.sendChangesInfo() } await this.panel.webview.postMessage({ - command: MessageCommand.replaceResults, - data: { - replacementsCount: nextOffset === -1 ? 0 : 1, - nextOffset: nextOffset, - searchDataBytesLength: searchDataBytes.length, - replaceDataBytesLength: replaceDataBytes.length, - }, + command: 'ReplaceResults', + data: replaceResultsMsg }) } break - case MessageCommand.search: + case 'Search': { + let searchMsg: MessageCommand['Search'] = message.data + const searchDataBytes = encodedStrToData( - message.data.searchData, - message.data.encoding + searchMsg.searchData, + searchMsg.encoding ) + + let searchResultsMsg: MessageCommand['SearchResults'] = message.data const searchResults = await searchSession( this.omegaSessionId, searchDataBytes, - message.data.caseInsensitive, - message.data.isReverse, - message.data.searchOffset, - message.data.searchLength, - message.data.limit + 1 + searchResultsMsg.caseInsensitive, + searchResultsMsg.isReverse, + searchResultsMsg.searchOffset, + searchResultsMsg.searchLength, + searchResultsMsg.limit + 1 ) if (searchResults.length === 0) { vscode.window.showInformationMessage( - `No more matches found for '${message.data.searchData}'` + `No more matches found for '${searchMsg.searchData}'` ) } let overflow = false - if (searchResults.length > message.data.limit) { + if (searchResults.length > searchResultsMsg.limit) { overflow = true searchResults.pop() } await this.panel.webview.postMessage({ - command: MessageCommand.searchResults, - data: { - searchResults: searchResults, - searchDataBytesLength: searchDataBytes.length, - overflow: overflow, - }, + command: 'SearchResults', + data: searchResultsMsg }) } break @@ -615,14 +618,11 @@ export class DataEditorClient implements vscode.Disposable { if (saved) { this.fileToEdit = fileToSave - this.fileSize = await getComputedFileSize(this.omegaSessionId) + this.fileSize = await getComputedFileSize(this.omegaSessionId) + let fileInfoMsg: MessageCommand['FileInfo'] = message.data await this.panel.webview.postMessage({ - command: MessageCommand.fileInfo, - data: { - computedFileSize: this.fileSize, - diskFileSize: this.fileSize, - fileName: this.fileToEdit, - }, + command: 'FileInfo', + data: fileInfoMsg, }) vscode.window.showInformationMessage(`Saved: ${this.fileToEdit}`) } else if (cancelled) { @@ -827,16 +827,17 @@ async function sendViewportRefresh( panel: vscode.WebviewPanel, viewportDataResponse: ViewportDataResponse ): Promise { + let viewportRefreshMsg: MessageCommand['ViewPortRefresh'] = { + viewportId: viewportDataResponse.getViewportId(), + viewportOffset: viewportDataResponse.getOffset(), + viewportLength: viewportDataResponse.getLength(), + viewportFollowingByteCount: viewportDataResponse.getFollowingByteCount(), + viewportData: viewportDataResponse.getData_asU8(), + viewportCapacity: VIEWPORT_CAPACITY_MAX, + } await panel.webview.postMessage({ - command: MessageCommand.viewportRefresh, - data: { - viewportId: viewportDataResponse.getViewportId(), - viewportOffset: viewportDataResponse.getOffset(), - viewportLength: viewportDataResponse.getLength(), - viewportFollowingByteCount: viewportDataResponse.getFollowingByteCount(), - viewportData: viewportDataResponse.getData_asU8(), - viewportCapacity: VIEWPORT_CAPACITY_MAX, - }, + command: 'ViewportRefresh', + data: viewportRefreshMsg, }) } @@ -891,9 +892,12 @@ class DisplayState { } private sendUIThemeUpdate() { - return this.panel.webview.postMessage({ - command: MessageCommand.setUITheme, + let setUIThemeMsg: MessageCommand['SetUITheme'] = { theme: this.colorThemeKind, + } + return this.panel.webview.postMessage({ + command: 'SetUITheme', + theme: setUIThemeMsg, }) } } diff --git a/src/svelte/src/components/dataEditor.svelte b/src/svelte/src/components/dataEditor.svelte index 0d513286b..4385450d3 100644 --- a/src/svelte/src/components/dataEditor.svelte +++ b/src/svelte/src/components/dataEditor.svelte @@ -42,7 +42,7 @@ limitations under the License. UIThemeCSSClass, darkUITheme, } from '../utilities/colorScheme' - import { type RobertMessageCommand } from '../utilities/message' + import { type MessageCommand } from '../utilities/message' import { vscode } from '../utilities/vscode' import Header from './Header/Header.svelte' import Main from './Main.svelte' @@ -56,9 +56,9 @@ limitations under the License. elementKeypressEventMap, key_is_mappable, } from '../utilities/elementKeypressEvents' - import type { - EditEvent, - ViewportData_t, + import { + ViewportData_t, + type EditEvent, } from './DataDisplays/CustomByteDisplay/BinaryData' import { byte_count_divisible_offset } from '../utilities/display' import { clearSearchResultsHighlights } from '../utilities/highlights' @@ -66,6 +66,9 @@ limitations under the License. $: $UIThemeCSSClass = $darkUITheme ? CSSThemeClass.Dark : CSSThemeClass.Light function requestEditedData() { + let requestEditedDataMsg: MessageCommand['RequestEditedData'] = { + data: + } if ($requestable) { vscode.postMessage({ command: MessageCommand.requestEditedData, @@ -140,13 +143,15 @@ limitations under the License. fetchOffset ) + let scrollViewportMsg: MessageCommand['ScrollViewport'] = { + scrollOffset: fetchOffset, + bytesPerRow : $bytesPerRow, + numLinesDisplayed: $viewportNumLinesDisplayed + } + vscode.postMessage({ - command: MessageCommand.scrollViewport, - data: { - scrollOffset: fetchOffset, - bytesPerRow: $bytesPerRow, - numLinesDisplayed: $viewportNumLinesDisplayed, - }, + command: 'ScrollViewport', + data: scrollViewportMsg, }) clearDataDisplays() } @@ -159,12 +164,15 @@ limitations under the License. const navigationData = navigationEvent.detail $dataFeedAwaitRefresh = true + let scrollViewportMsg: MessageCommand['ScrollViewport'] = { + scrollOffset: navigationData.nextViewportOffset, + bytesPerRow : $bytesPerRow, + numLinesDisplayed: $viewportNumLinesDisplayed + } + vscode.postMessage({ - command: MessageCommand.scrollViewport, - data: { - scrollOffset: navigationData.nextViewportOffset, - bytesPerRow: $bytesPerRow, - }, + command: 'ScrollViewport', + data: scrollViewportMsg, }) $dataFeedLineTop = navigationData.lineTopOnRefresh @@ -211,7 +219,7 @@ limitations under the License. break } - let applyMsg: RobertMessageCommand['Apply'] = { + let applyMsg: MessageCommand['Apply']= { originalSegmentData: originalData as Uint8Array, editedSegmentData: editedData, offset: editedOffset @@ -226,20 +234,30 @@ limitations under the License. } function undo() { + let undoMsg: MessageCommand['Undo'] = { + //TODO add undo + } vscode.postMessage({ - command: MessageCommand.undoChange, + command: undoMsg, }) } function redo() { + let redoMsg: MessageCommand['Redo'] = { + //TODO add redo + } vscode.postMessage({ - command: MessageCommand.redoChange, + command: redoMsg }) } function clearChangeStack() { + let clearMsg: MessageCommand['Clear'] = { + //TODO add clear + } + vscode.postMessage({ - command: MessageCommand.clearChanges, + command: clear, }) } @@ -267,16 +285,17 @@ limitations under the License. return } } - + let msg: MessageCommand + window.addEventListener('message', (msg) => { - switch (msg.data.command) { - case MessageCommand.editorOnChange: + switch (msg) { + case 'Edit': const data = msg.data.data if ($editMode === EditByteModes.Multiple) $editorSelection = msg.data.display break - case MessageCommand.requestEditedData: + case 'RequestEditedData': $editorSelection = msg.data.data.dataDisplay if ($editMode === EditByteModes.Multiple) { $editedDataSegment = new Uint8Array(msg.data.data.data) @@ -287,10 +306,10 @@ limitations under the License. $selectionDataStore.startOffset + $editedDataSegment.byteLength - 1 break - case MessageCommand.setUITheme: + case 'SetUITheme': $darkUITheme = msg.data.theme === 2 break - case MessageCommand.viewportRefresh: + case 'ViewportRefresh': // the viewport has been refreshed, so the editor views need to be updated $viewport = { data: msg.data.data.viewportData, diff --git a/src/svelte/src/utilities/message.ts b/src/svelte/src/utilities/message.ts index 25d592fb8..0aef4ebda 100644 --- a/src/svelte/src/utilities/message.ts +++ b/src/svelte/src/utilities/message.ts @@ -22,59 +22,31 @@ import type { AvailableStrEncodings } from '../stores/configuration' * limitations under the License. */ -export enum MessageCommands { - applyChanges = 'apply', - clearChanges = 'clear', - editorOnChange = 'edit', - fileInfo = 'fileinfo', - heartbeat = 'heartbeat', - profile = 'profile', - redoChange = 'Redo', - replaceResults = 'replaceResults', - requestEditedData = 'requestEdit', - save = 'save', - saveAs = 'saveas', - scrollViewport = 'scrollViewport', - search = 'search', - replace = 'replace', - searchResults = 'searchResults', - setUITheme = 'setUITheme', - showMessage = 'showMessage', - undoChange = 'undoChange', - updateLogicalDisplay = 'updateLogicalDisplay', - viewportRefresh = 'viewPortRefresh', +export enum MessageLevel { + Error, + Info, + Warn, } -export interface BaseMessage { - data: any -} - -export interface ApplyMessage extends BaseMessage { - data: { - bytes: Uint8Array - start: number - length: number +export interface MessageCommand { + Apply: { + originalSegmentData: Uint8Array + editedSegmentData: Uint8Array + offset: number } -} - -export interface EditMessage extends BaseMessage { - data: { - encoding: BufferEncoding + Clear:{} + Edit: { + encoding: AvailableStrEncodings editMode: EditByteModes - selectionData: any + selectionData: string } -} - -export interface FileInfoMessage extends BaseMessage { - data: { + FileInfo: { computedFileSize: number - diskFileSize: number fileName: string + changeCount: number + undoCount: number } -} - -export interface HeartBeatMessage extends BaseMessage { - data: { + HeartBeat: { latency: number omegaEditPort: number serverCpuLoadAverage: number @@ -89,72 +61,51 @@ export interface HeartBeatMessage extends BaseMessage { jvmPath: string availableProcessors: number } -} - -export interface ProfileMessage extends BaseMessage { - data: { + Profile: { startOffset: number length: number byteProfile: number } -} - -export interface ReplaceResultsMessage extends BaseMessage { - data: { + Redo:{} + ReplaceResults: { replacementsCount: number nextOffset: number searchDataBytesLength: number replaceDataBytesLength: number } -} - -export interface RequestEditedData extends BaseMessage { - data: { + RequestEditedData: { data: Uint8Array dataDisplay: string } -} - -export interface ScrollViewport extends BaseMessage { - data: { + Save:{} + SaveAs:{} + ScrollViewport: { scrollOffset: any bytesPerRow: any + numLinesDisplayed: any } -} - -export interface Search extends BaseMessage { - data: { + Search: { searchData: any encoding: any } -} - -export interface Replace extends BaseMessage { - data: { + Replace: { replaceData: any encoding: any } -} - -export interface SearchResults extends BaseMessage { - data: { + SearchResults:{ caseInsensitive: any isReverse: any searchOffset: any searchLength: any limit: any } -} - -export interface ShowMessage extends BaseMessage { - data: { + SetUITheme:{} + Show: { messageLevel: any message: any } -} - -export interface ViewPortRefresh extends BaseMessage { - data: { + Undo:{} + ViewPortRefresh: { viewportId: string viewportOffset: number viewportLength: number @@ -163,101 +114,3 @@ export interface ViewPortRefresh extends BaseMessage { viewportCapacity: number } } - -export function buildMessage(data: T['data']): T { - return { data: data } as T -} - -const applyMsg = buildMessage({ - bytes: new Uint8Array(100), - start: 0, - length: 100, -}) - -const editMsg = buildMessage({ - encoding: 'utf-8', - editMode: EditByteModes.Single, - selectionData: '', -}) - -const fileInfoMsg = buildMessage({ - computedFileSize: 0, - diskFileSize: 0, - fileName: '', -}) - -const HeartBeatMsg = buildMessage({ - latency: 0, - omegaEditPort: 0, - serverCpuLoadAverage: 0, - serverUptime: 0, - serverUsedMemory: 0, - sessionCount: 0, - serverVersion: '', - serverHostname: '', - serverProcessId: 0, - jvmVersion: '', - jvmVendor: '', - jvmPath: '', - availableProcessors: 0, -}) - -const ProfileMsg = buildMessage({ - startOffset: 0, - length: 0, - byteProfile: 0, -}) - -const ReplaceResultsMsg = buildMessage({ - replacementsCount: 0, - nextOffset: 0, - searchDataBytesLength: 0, - replaceDataBytesLength: 0, -}) - -const RequestEditedDataMsg = buildMessage({ - data: new Uint8Array(100), - dataDisplay: '', -}) - -const ScrollViewportMsg = buildMessage({ - scrollOffset: '', - bytesPerRow: '', -}) - -const SearchMsg = buildMessage({ searchData: '', encoding: '' }) - -const SearchResultsMsg = buildMessage({ - caseInsensitive: '', - isReverse: '', - searchOffset: '', - searchLength: '', - limit: '', -}) - -const ShowMessageMsg = buildMessage({ - messageLevel: '', - message: '', -}) - -const ViewPortRefreshMsg = buildMessage({ - viewportId: '', - viewportOffset: 0, - viewportLength: 0, - viewportFollowingByteCount: 0, - viewportData: new Uint8Array(100), - viewportCapacity: 0, -}) - -export interface RobertMessageCommand { - Apply: { - originalSegmentData: Uint8Array - editedSegmentData: Uint8Array - offset: number - } - Edit: { - encoding: AvailableStrEncodings - editMode: EditByteModes - selectionData: string - } -}