import * as vscode from "vscode" ; import { DrawioChrome } from "./DrawioChrome" ; import * as fs from "fs" ; import * as path from "path" ; const mdChrome = { } as { [ k ] : DrawioChrome ; } ; vscode . workspace . onDidCloseTextDocument ( async ( doc ) => { const docPath = doc . uri . fsPath ; const chrome = mdChrome [ docPath ] ; if ( chrome ) { await chrome . close ( ) ; delete mdChrome [ docPath ] ; } } ) ; async function _getCursorDrawioSvgName ( ) { if ( ! vscode . window . activeTextEditor ) return ; const sel = vscode . window . activeTextEditor . selections ; if ( ! sel || sel . length <= 0 ) return ; const assetsDirName = vscode . workspace . getConfiguration ( "drawio" ) . get ( "assetsDirName" ) ; const line = vscode . window . activeTextEditor . document . lineAt ( sel [ 0 ] . anchor . line ) ; const m = line . text . match ( /\!\[.*\]\((.*)\)$/ ) ; console . log ( "line:" , line . lineNumber , line . text , m ) ; if ( ! m || m . length !== 2 ) return ; const svgPath = m [ 1 ] . trim ( ) ; if ( ! svgPath . startsWith ( assetsDirName ) ) { console . log ( `svg file not in ${ assetsDirName } dir` ) ; } const name = path . parse ( svgPath ) . base ; console . log ( "using name:" , name ) ; return name ; } export const cmdDrawio = vscode . commands . registerCommand ( "extension.drawio" , async ( ) => { if ( ! vscode . window . activeTextEditor || vscode . window . activeTextEditor . document . languageId !== "markdown" ) { vscode . window . showInformationMessage ( `please open markdown file` ) ; return ; } const mdfile = vscode . window . activeTextEditor . document . uri . fsPath ; const mdPath = path . parse ( mdfile ) . dir ; try { if ( ! mdChrome [ mdPath ] || mdChrome [ mdPath ] . isClosed ( ) ) { mdChrome [ mdPath ] = new DrawioChrome ( mdPath ) ; await mdChrome [ mdPath ] . init ( ) ; } const chrome = mdChrome [ mdPath ] ; const svgName = await _getCursorDrawioSvgName ( ) ; await chrome . active ( svgName ) ; } catch ( e ) { console . error ( "open chrome error:" , e ) ; } } ) ;