More WebView message types

feature/search
Kelvin Schoofs 6 years ago
parent 879bde065d
commit 5e73396f8b

@ -3,7 +3,8 @@ import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import * as request from 'request'; import * as request from 'request';
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import { loadConfigs } from './config'; import { deleteConfig, loadConfigsRaw, updateConfig } from './config';
import { getLocations } from './fileSystemConfig';
import { Message } from './webviewMessages'; import { Message } from './webviewMessages';
let webviewPanel: vscode.WebviewPanel | undefined; let webviewPanel: vscode.WebviewPanel | undefined;
@ -39,9 +40,44 @@ async function handleMessage(message: Message): Promise<any> {
console.log('Got message:', message); console.log('Got message:', message);
switch (message.type) { switch (message.type) {
case 'requestData': { case 'requestData': {
const configs = await loadConfigsRaw();
const locations = getLocations(configs);
return postMessage({ return postMessage({
configs, locations,
type: 'responseData', type: 'responseData',
configs: await loadConfigs(), });
}
case 'saveConfig': {
const { uniqueId, config, name, remove } = message;
let error: string | undefined;
try {
if (remove) {
await deleteConfig(config);
} else {
await updateConfig(config, name);
}
} catch (e) {
error = e.message;
}
return postMessage({
uniqueId, config, error,
type: 'saveConfigResult',
});
}
case 'promptPath': {
const { uniqueId } = message;
let uri: vscode.Uri | undefined;
let error: string | undefined;
try {
const uris = await vscode.window.showOpenDialog({});
if (uris) [uri] = uris;
} catch (e) {
error = e.message;
}
return postMessage({
uniqueId,
path: uri && uri.fsPath,
type: 'promptPathResult',
}); });
} }
} }

@ -1,4 +1,4 @@
import { FileSystemConfig } from './fileSystemConfig'; import { ConfigLocation, FileSystemConfig } from './fileSystemConfig';
export interface RequestDataMessage { export interface RequestDataMessage {
type: 'requestData'; type: 'requestData';
@ -6,11 +6,41 @@ export interface RequestDataMessage {
export interface ResponseDataMessage { export interface ResponseDataMessage {
type: 'responseData'; type: 'responseData';
configs: FileSystemConfig[]; configs: FileSystemConfig[];
locations: ConfigLocation[];
}
export interface SaveConfigMessage {
type: 'saveConfig';
config: FileSystemConfig;
name?: string;
uniqueId?: string;
remove?: boolean;
}
export interface SaveConfigResultMessage {
type: 'saveConfigResult';
error?: string;
config: FileSystemConfig;
uniqueId?: string;
}
export interface PromptPathMessage {
type: 'promptPath';
uniqueId?: string;
}
export interface PromptPathResultMessage {
type: 'promptPathResult';
error?: string;
path?: string;
uniqueId?: string;
} }
export interface MessageTypes { export interface MessageTypes {
requestData: RequestDataMessage; requestData: RequestDataMessage;
responseData: ResponseDataMessage; responseData: ResponseDataMessage;
saveConfig: SaveConfigMessage;
saveConfigResult: SaveConfigResultMessage;
promptPath: PromptPathMessage;
promptPathResult: PromptPathResultMessage;
} }
export type Message = MessageTypes[keyof MessageTypes]; export type Message = MessageTypes[keyof MessageTypes];

Loading…
Cancel
Save