VS Code extension that previews Archify diagrams from their JSON sources (live, as you type) and opens rendered Archify HTML in a viewer tab. Bundles the Archify 3.0.1 renderer and runs it on VS Code's Node runtime. Adds validation diagnostics, JSON schema help, source-link navigation, export saving, render-to-file and open-in-browser commands. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
101 lines
3.8 KiB
TypeScript
101 lines
3.8 KiB
TypeScript
import path from 'node:path';
|
|
import * as vscode from 'vscode';
|
|
import { log, repoRootFor } from './runtime';
|
|
|
|
export type WebviewMessage =
|
|
| { type: 'hash'; hash: string }
|
|
| { type: 'save'; filename: string; base64: string }
|
|
| { type: 'openExternal'; href: string }
|
|
| { type: 'openRelative'; href: string }
|
|
| { type: 'openSource'; path: string; line?: number; endLine?: number; href?: string }
|
|
| { type: 'error'; message: string };
|
|
|
|
/** Column for opening files next to a webview without covering it. */
|
|
function besideColumn(panel: vscode.WebviewPanel): vscode.ViewColumn {
|
|
return panel.viewColumn === vscode.ViewColumn.One ? vscode.ViewColumn.Two : vscode.ViewColumn.One;
|
|
}
|
|
|
|
async function exists(uri: vscode.Uri): Promise<boolean> {
|
|
try {
|
|
await vscode.workspace.fs.stat(uri);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Opens a diagram's source evidence in the editor. The path is relative to
|
|
* the repository root; the diagram's checkout and the workspace folders are
|
|
* tried in turn before falling back to the web link.
|
|
*/
|
|
async function openSource(
|
|
message: Extract<WebviewMessage, { type: 'openSource' }>,
|
|
context: vscode.Uri,
|
|
panel: vscode.WebviewPanel,
|
|
): Promise<void> {
|
|
const relative = message.path.replace(/^\/+/, '');
|
|
const roots = new Set<string>();
|
|
const repoRoot = await repoRootFor(context);
|
|
if (repoRoot) roots.add(repoRoot);
|
|
for (const folder of vscode.workspace.workspaceFolders ?? []) roots.add(folder.uri.fsPath);
|
|
|
|
for (const root of roots) {
|
|
const candidate = vscode.Uri.file(path.join(root, relative));
|
|
if (relative && (await exists(candidate))) {
|
|
const start = Math.max((message.line ?? 1) - 1, 0);
|
|
const end = Math.max((message.endLine ?? message.line ?? 1) - 1, start);
|
|
const selection = new vscode.Range(start, 0, end, Number.MAX_SAFE_INTEGER);
|
|
await vscode.window.showTextDocument(candidate, { viewColumn: besideColumn(panel), selection });
|
|
return;
|
|
}
|
|
}
|
|
if (message.href && /^https?:/i.test(message.href)) {
|
|
await vscode.env.openExternal(vscode.Uri.parse(message.href));
|
|
return;
|
|
}
|
|
vscode.window.showWarningMessage(`Archify: source file "${message.path}" was not found in the workspace.`);
|
|
}
|
|
|
|
export async function handleWebviewMessage(
|
|
message: WebviewMessage,
|
|
context: vscode.Uri,
|
|
panel: vscode.WebviewPanel,
|
|
): Promise<void> {
|
|
try {
|
|
switch (message.type) {
|
|
case 'openExternal':
|
|
await vscode.env.openExternal(vscode.Uri.parse(message.href));
|
|
break;
|
|
case 'openRelative': {
|
|
const [file] = message.href.split('#');
|
|
if (!file) break;
|
|
const target = vscode.Uri.joinPath(context, '..', decodeURIComponent(file));
|
|
await vscode.commands.executeCommand('vscode.open', target, besideColumn(panel));
|
|
break;
|
|
}
|
|
case 'openSource':
|
|
await openSource(message, context, panel);
|
|
break;
|
|
case 'save': {
|
|
const name = path.basename(message.filename || 'archify-export');
|
|
const defaultUri = vscode.Uri.joinPath(context, '..', name);
|
|
const target = await vscode.window.showSaveDialog({ defaultUri, saveLabel: 'Export' });
|
|
if (!target) break;
|
|
await vscode.workspace.fs.writeFile(target, Buffer.from(message.base64, 'base64'));
|
|
const choice = await vscode.window.showInformationMessage(`Exported ${path.basename(target.fsPath)}`, 'Reveal');
|
|
if (choice === 'Reveal') await vscode.commands.executeCommand('revealFileInOS', target);
|
|
break;
|
|
}
|
|
case 'error':
|
|
vscode.window.showErrorMessage(`Archify: ${message.message}`);
|
|
break;
|
|
case 'hash':
|
|
break;
|
|
}
|
|
} catch (error) {
|
|
log().error(error instanceof Error ? error : String(error));
|
|
vscode.window.showErrorMessage(`Archify: ${error instanceof Error ? error.message : String(error)}`);
|
|
}
|
|
}
|