@ -4,10 +4,12 @@ import * as path from 'path';
import * as vscode from 'vscode' ;
import * as vscode from 'vscode' ;
import { deleteConfig , loadConfigsRaw , updateConfig } from './config' ;
import { deleteConfig , loadConfigsRaw , updateConfig } from './config' ;
import { getLocations } from './fileSystemConfig' ;
import { getLocations } from './fileSystemConfig' ;
import { DEBUG , Logging , LOGGING_NO_STACKTRACE } from './logging' ;
import { DEBUG , Logging as _Logging , LOGGING_NO_STACKTRACE } from './logging' ;
import { toPromise } from './toPromise' ;
import { toPromise } from './toPromise' ;
import type { Message , Navigation } from './webviewMessages' ;
import type { Message , Navigation } from './webviewMessages' ;
const Logging = _Logging . scope ( 'WebView' ) ;
let webviewPanel : vscode.WebviewPanel | undefined ;
let webviewPanel : vscode.WebviewPanel | undefined ;
let pendingNavigation : Navigation | undefined ;
let pendingNavigation : Navigation | undefined ;
@ -31,20 +33,21 @@ async function getDebugContent(): Promise<string | false> {
body = body . replace ( /\$WEBVIEW_CSPEXTRA/g , ` connect-src ${ URL } ${ URL . replace ( 'http://' , 'ws://' ) } ; ` ) ;
body = body . replace ( /\$WEBVIEW_CSPEXTRA/g , ` connect-src ${ URL } ${ URL . replace ( 'http://' , 'ws://' ) } ; ` ) ;
cb ( null , body ) ;
cb ( null , body ) ;
} ) . on ( 'error' , err = > {
} ) . on ( 'error' , err = > {
console. warn ( 'Error connecting to React dev server:' , err ) ;
Logging. warning ( ` Error connecting to React dev server: ${ err } ` ) ;
cb ( new Error ( 'Could not connect to React dev server. Not running?' ) ) ;
cb ( new Error ( 'Could not connect to React dev server. Not running?' ) ) ;
} ) ) ;
} ) ) ;
}
}
export async function open() {
export async function open() {
if ( ! webviewPanel ) {
if ( ! webviewPanel ) {
webviewPanel = vscode . window . createWebviewPanel ( 'sshfs-settings' , 'SSH-FS Settings' , vscode . ViewColumn . One , { enableFindWidget : true , enableScripts : true } ) ;
const extensionPath = getExtensionPath ( ) ;
webviewPanel = vscode . window . createWebviewPanel ( 'sshfs-settings' , 'SSH-FS' , vscode . ViewColumn . One , { enableFindWidget : true , enableScripts : true } ) ;
webviewPanel . onDidDispose ( ( ) = > webviewPanel = undefined ) ;
webviewPanel . onDidDispose ( ( ) = > webviewPanel = undefined ) ;
if ( extensionPath ) webviewPanel . iconPath = vscode . Uri . file ( path . join ( extensionPath , 'resources/icon.svg' ) ) ;
const { webview } = webviewPanel ;
const { webview } = webviewPanel ;
webview . onDidReceiveMessage ( handleMessage ) ;
webview . onDidReceiveMessage ( handleMessage ) ;
let content = await getDebugContent ( ) . catch ( ( e : Error ) = > ( vscode . window . showErrorMessage ( e . message ) , null ) ) ;
let content = await getDebugContent ( ) . catch ( ( e : Error ) = > ( vscode . window . showErrorMessage ( e . message ) , null ) ) ;
if ( ! content ) {
if ( ! content ) {
const extensionPath = getExtensionPath ( ) ;
if ( ! extensionPath ) throw new Error ( 'Could not get extensionPath' ) ;
if ( ! extensionPath ) throw new Error ( 'Could not get extensionPath' ) ;
// If we got here, we're either not in debug mode, or something went wrong (and an error message is shown)
// If we got here, we're either not in debug mode, or something went wrong (and an error message is shown)
content = fs . readFileSync ( path . resolve ( extensionPath , 'webview/build/index.html' ) ) . toString ( ) ;
content = fs . readFileSync ( path . resolve ( extensionPath , 'webview/build/index.html' ) ) . toString ( ) ;
@ -63,19 +66,19 @@ export async function open() {
}
}
export async function navigate ( navigation : Navigation ) {
export async function navigate ( navigation : Navigation ) {
Logging . debug ( ` Navigation requested: ${ JSON . stringify ( navigation , null , 4 ) } ` ) ;
pendingNavigation = navigation ;
pendingNavigation = navigation ;
postMessage ( { navigation , type : 'navigate' } ) ;
postMessage ( { navigation , type : 'navigate' } ) ;
return open ( ) ;
return open ( ) ;
}
}
function postMessage < T extends Message > ( message : T ) {
function postMessage < T extends Message > ( message : T ) {
if ( ! webviewPanel ) return ;
webviewPanel ? . webview . postMessage ( message ) ;
webviewPanel . webview . postMessage ( message ) ;
}
}
async function handleMessage ( message : Message ) : Promise < any > {
async function handleMessage ( message : Message ) : Promise < any > {
console . log ( 'Got message:' , message ) ;
if ( ! webviewPanel ) return Logging . warning ( ` Got message without webviewPanel: ${ JSON . stringify ( message , null , 4 ) } ` ) ;
if ( message . type === 'navigated' ) pendingNavigation = undefined ;
Logging . debug ( ` Got message: ${ JSON . stringify ( message , null , 4 ) } ` ) ;
if ( pendingNavigation ) {
if ( pendingNavigation ) {
postMessage ( {
postMessage ( {
type : 'navigate' ,
type : 'navigate' ,
@ -131,5 +134,22 @@ async function handleMessage(message: Message): Promise<any> {
type : 'promptPathResult' ,
type : 'promptPathResult' ,
} ) ;
} ) ;
}
}
case 'navigated' : {
const { view } = message ;
type View = 'startscreen' | 'newconfig' | 'configeditor' | 'configlocator' ;
let title : string | undefined ;
switch ( view as View ) {
case 'configeditor' :
title = 'SSH FS - Edit config' ;
break ;
case 'configlocator' :
title = 'SSH FS - Locate config' ;
break ;
case 'newconfig' :
title = 'SSH FS - New config' ;
break ;
}
webviewPanel . title = title || 'SSH FS' ;
}
}
}
}
}