import * as vscode from 'vscode' ; import TelemetryReporter from 'vscode-extension-telemetry' ; import { BrowserPreview } from './editorPreview/browserPreview' ; import { Manager } from './manager' ; import { EXTENSION_ID } from './utils/constants' ; import { PathUtil } from './utils/pathUtil' ; import { Settings , SETTINGS_SECTION_ID , SettingUtil , } from './utils/settingsUtil' ; let reporter ; let manager ; export function activate ( context ) { const extPackageJSON = context . extension . packageJSON ; reporter = new TelemetryReporter ( EXTENSION_ID , extPackageJSON . version , extPackageJSON . aiKey ) ; manager = new Manager ( context . extensionUri , reporter , PathUtil . GetUserDataDirFromStorageUri ( context . storageUri ?. fsPath ) ) ; reporter . sendTelemetryEvent ( 'extension.startUp' , { } , { numWorkspaceFolders : vscode . workspace . workspaceFolders ?. length ?? 0 } ) ; const openPreview = ( internal , file , isRelative , debug = false ) => { if ( internal ) { manager . createOrShowEmbeddedPreview ( undefined , file , isRelative ) ; } else { manager . showPreviewInBrowser ( file , isRelative , debug ) ; } } ; const handleOpenFile = ( internal , file , fileStringRelative = true , debug = false ) => { if ( typeof file == 'string' ) { openPreview ( internal , file , fileStringRelative , debug ) ; return ; } else if ( file instanceof vscode . Uri ) { const filePath = file ?. fsPath ; if ( filePath ) { openPreview ( internal , filePath , false , debug ) ; return ; } else { const activeFilePath = vscode . window . activeTextEditor ?. document . fileName ; if ( activeFilePath ) { openPreview ( internal , activeFilePath , false , debug ) ; return ; } } } else { const activeFilePath = vscode . window . activeTextEditor ?. document . fileName ; if ( activeFilePath ) { openPreview ( internal , activeFilePath , false , debug ) ; return ; } } vscode . window . showErrorMessage ( 'This file is not a part of the workspace where the server has started. Cannot preview.' ) ; return ; } ; context . subscriptions . push ( reporter ) ; context . subscriptions . push ( vscode . commands . registerCommand ( `${ SETTINGS_SECTION_ID }.start` , ( ) => { const filePath = SettingUtil . GetConfig ( context . extensionUri ) . defaultPreviewPath ; if ( filePath == '' ) { if ( manager . workspace ) { vscode . commands . executeCommand ( `${ SETTINGS_SECTION_ID }.start.preview.atFile` , '/' , true ) ; } else { manager . openServer ( ) ; } } else { vscode . commands . executeCommand ( `${ SETTINGS_SECTION_ID }.start.preview.atFile` , filePath , manager . pathExistsRelativeToWorkspace ( filePath ) ) ; } } ) ) ; context . subscriptions . push ( vscode . commands . registerCommand ( `${ SETTINGS_SECTION_ID }.start.preview.atFile` , ( file ? , relativeFileString = true ) => { const previewType = SettingUtil . GetPreviewType ( context . extensionUri ) ; vscode . commands . executeCommand ( `${ SETTINGS_SECTION_ID }.start.${ previewType }.atFile` , file , relativeFileString ) ; } ) ) ; context . subscriptions . push ( vscode . commands . registerCommand ( `${ SETTINGS_SECTION_ID }.start.debugPreview.atFile` , ( file ? , relativeFileString = true ) => { vscode . commands . executeCommand ( `${ SETTINGS_SECTION_ID }.start.externalDebugPreview.atFile` , file , relativeFileString ) ; } ) ) ; context . subscriptions . push ( vscode . commands . registerCommand ( `${ SETTINGS_SECTION_ID }.start.externalPreview.atFile` , ( file ? , relativeFileString = true ) => { reporter . sendTelemetryEvent ( 'preview' , { type : 'external' , location : 'atFile' , debug : 'false' , } ) ; handleOpenFile ( false , file , relativeFileString ) ; } ) ) ; context . subscriptions . push ( vscode . commands . registerCommand ( `${ SETTINGS_SECTION_ID }.start.internalPreview.atFile` , ( file ? , relativeFileString = true ) => { reporter . sendTelemetryEvent ( 'preview' , { type : 'internal' , location : 'atFile' , } ) ; handleOpenFile ( true , file , relativeFileString ) ; } ) ) ; context . subscriptions . push ( vscode . commands . registerCommand ( `${ SETTINGS_SECTION_ID }.start.externalDebugPreview.atFile` , ( file ? , relativeFileString = true ) => { reporter . sendTelemetryEvent ( 'preview' , { type : 'external' , location : 'atFile' , debug : 'true' , } ) ; handleOpenFile ( false , file , relativeFileString , true ) ; } ) ) ; context . subscriptions . push ( vscode . commands . registerCommand ( `${ SETTINGS_SECTION_ID }.end` , ( ) => { if ( ! manager . closeServer ( ) ) { reporter . sendTelemetryEvent ( 'server.forceClose' ) ; vscode . window . showErrorMessage ( 'Server already off.' ) ; } } ) ) ; context . subscriptions . push ( vscode . commands . registerCommand ( `${ SETTINGS_SECTION_ID }.setDefaultOpenFile` , ( file ) => { if ( manager . absPathInDefaultWorkspace ( file . fsPath ) ) { const fileRelativeToWorkspace = manager . getFileRelativeToDefaultWorkspace ( file . fsPath ) ?? '' ; SettingUtil . UpdateSettings ( Settings . defaultPreviewPath , fileRelativeToWorkspace , false ) ; } else { SettingUtil . UpdateSettings ( Settings . defaultPreviewPath , file . fsPath , false ) ; } } ) ) ; if ( vscode . window . registerWebviewPanelSerializer ) { vscode . window . registerWebviewPanelSerializer ( BrowserPreview . viewType , { async deserializeWebviewPanel ( webviewPanel , state ) { let relative = true ; let file = unescape ( state . currentAddress ) ?? '/' ; if ( ! manager . pathExistsRelativeToWorkspace ( file ) ) { const absFile = manager . decodeEndpoint ( file ) ; file = absFile ?? '/' ; relative = false ; } if ( file == '/' && ! manager . workspace ) { webviewPanel . dispose ( ) ; return ; } webviewPanel . webview . options = manager . getWebviewOptions ( ) ; manager . createOrShowEmbeddedPreview ( webviewPanel , file , relative ) ; } , } ) ; } } export function deactivate ( ) { reporter . dispose ( ) ; manager . dispose ( ) ; }