All files / views/webviews webview-router.ts

100% Statements 5/5
100% Branches 3/3
100% Functions 1/1
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45                                                                  2x   2x 2x   2x 2x          
import * as vscode from 'vscode';
import { container } from 'tsyringe';
import { ServiceToken } from '@src/services/tokens';
import { SettingsSectionId } from './views/settings/sections';
import { SettingsPanelController } from './views/settings/settings-panel-controller';
import { SparqlConnection } from '@src/languages/sparql/services/sparql-connection';
 
/**
 * A view target identifies a destination the user can be navigated to.
 * Adding a new view type means extending this union and handling it in {@link WebviewRouter}.
 */
export type WebviewTarget =
	| { kind: 'settings'; section?: SettingsSectionId; params?: { connection?: SparqlConnection } };
 
/**
 * Generic navigation API between webview panels. Commands and other code should depend on
 * {@link IWebviewRouter} rather than resolving concrete controllers, so the wiring between a
 * "view target" and the controller that hosts it lives in exactly one place.
 */
export interface IWebviewRouter {
	/**
	 * Opens a webview panel for the given target.
	 * @param target The target view to open.
	 * @param viewColumn The column in which to open the view.
	 */
	open(target: WebviewTarget, viewColumn?: vscode.ViewColumn): Promise<void>;
}
 
/**
 * Default implementation of {@link IWebviewRouter}. Routes view targets to the appropriate controllers.
 */
export class WebviewRouter implements IWebviewRouter {
	async open(target: WebviewTarget, viewColumn?: vscode.ViewColumn): Promise<void> {
		switch (target.kind) {
			case 'settings': {
				const controller = container.resolve<SettingsPanelController>(ServiceToken.SettingsPanelController);
				const params = target.params?.connection ? { connection: target.params.connection } : undefined;
 
				await controller.openSection(target.section, params, viewColumn);
				return;
			}
		}
	}
}