import * as vscode from 'vscode';
import { HTML_VIEW_TYPE, HtmlViewerProvider } from '../views/htmlViewer';
import { DiagramViewManager } from '../views/diagramView';
/** What command handlers share: the extension context and the open views. */
export interface CommandContext {
extension: vscode.ExtensionContext;
views: DiagramViewManager;
htmlViewer: HtmlViewerProvider;
}
export function isHtmlUri(uri: vscode.Uri): boolean {
return /\.html?$/i.test(uri.path);
}
/** The file a command applies to: explicit argument, focused preview or viewer, then the text editor. */
export function targetUri(ctx: CommandContext, arg?: unknown): vscode.Uri | undefined {
if (arg instanceof vscode.Uri) return arg;
return ctx.views.activePreview?.uri ?? ctx.htmlViewer.activeUri ?? vscode.window.activeTextEditor?.document.uri;
}
export function sourceUri(ctx: CommandContext, arg?: unknown): vscode.Uri | undefined {
const uri = targetUri(ctx, arg);
if (!uri) vscode.window.showWarningMessage('Archify: open a .archify diagram first.');
return uri;
}
export const openHtmlViewer = (uri: vscode.Uri, viewColumn?: vscode.ViewColumn) =>
vscode.commands.executeCommand('vscode.openWith', uri, HTML_VIEW_TYPE, viewColumn);
export async function exists(uri: vscode.Uri): Promise {
try {
await vscode.workspace.fs.stat(uri);
return true;
} catch {
return false;
}
}