From ec3c1eec582bd7d1eba2c475637a17f9930f8f55 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Tue, 9 Jul 2024 17:15:20 +0200 Subject: [PATCH] feat: Add URI handler for command links from documentation #821 --- CHANGELOG.md | 1 + src/commands/Taxonomy.ts | 33 +++++++++++++++++++++++++++++++++ src/commands/index.ts | 2 ++ src/constants/Extension.ts | 4 ++-- src/extension.ts | 35 ++++++++++++----------------------- src/providers/UriHandler.ts | 35 +++++++++++++++++++++++++++++++++++ 6 files changed, 85 insertions(+), 25 deletions(-) create mode 100644 src/commands/Taxonomy.ts create mode 100644 src/providers/UriHandler.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e340356..8b226b5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - [#467](https://github.com/estruyf/vscode-front-matter/issues/467): New `fmContentType` metadata field to link content type (fallback to the `type` field) - [#819](https://github.com/estruyf/vscode-front-matter/issues/819): Added new extensibility support for media scripts +- [#821](https://github.com/estruyf/vscode-front-matter/issues/821): Added URI handler to support command links from the documentation - [#822](https://github.com/estruyf/vscode-front-matter/issues/822): Added docs to the panel & dashboard views - [#829](https://github.com/estruyf/vscode-front-matter/issues/829): UI extensibility is now generally available diff --git a/src/commands/Taxonomy.ts b/src/commands/Taxonomy.ts new file mode 100644 index 00000000..c51f132f --- /dev/null +++ b/src/commands/Taxonomy.ts @@ -0,0 +1,33 @@ +import { commands } from 'vscode'; +import { COMMAND_NAME } from '../constants'; +import { TagType } from '../panelWebView/TagType'; +import { PanelProvider } from '../panelWebView/PanelProvider'; + +export class Taxonomy { + /** + * Registers the commands for the Article class. + * + * @param subscriptions - The array of subscriptions to register the commands with. + */ + public static async registerCommands(subscriptions: unknown[]) { + const explorerSidebar = PanelProvider.getInstance(); + + if (explorerSidebar) { + subscriptions.push( + commands.registerCommand(COMMAND_NAME.insertTags, async () => { + await commands.executeCommand('workbench.view.extension.frontmatter-explorer'); + await commands.executeCommand('workbench.action.focusSideBar'); + explorerSidebar.triggerInputFocus(TagType.tags); + }) + ); + + subscriptions.push( + commands.registerCommand(COMMAND_NAME.insertCategories, async () => { + await commands.executeCommand('workbench.view.extension.frontmatter-explorer'); + await commands.executeCommand('workbench.action.focusSideBar'); + explorerSidebar.triggerInputFocus(TagType.categories); + }) + ); + } + } +} diff --git a/src/commands/index.ts b/src/commands/index.ts index 0d3b53d0..0c29b985 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -10,5 +10,7 @@ export * from './Preview'; export * from './Project'; export * from './Settings'; export * from './StatusListener'; +export * from './Taxonomy'; export * from './Template'; export * from './Wysiwyg'; +export * from './i18n'; diff --git a/src/constants/Extension.ts b/src/constants/Extension.ts index 91fdf8fd..4c1e01cc 100644 --- a/src/constants/Extension.ts +++ b/src/constants/Extension.ts @@ -1,10 +1,10 @@ -const extensionName = 'frontMatter'; +export const EXTENSION_COMMAND_PREFIX = 'frontMatter'; export const EXTENSION_ID = 'eliostruyf.vscode-front-matter'; export const EXTENSION_BETA_ID = 'eliostruyf.vscode-front-matter-beta'; export const getCommandName = (command: string) => { - return `${extensionName}.${command}`; + return `${EXTENSION_COMMAND_PREFIX}.${command}`; }; export const COMMAND_NAME = { diff --git a/src/extension.ts b/src/extension.ts index 55e20871..c122324b 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -2,7 +2,6 @@ import { GitListener } from './listeners/general/GitListener'; import * as vscode from 'vscode'; import { COMMAND_NAME, CONTEXT, EXTENSION_NAME, TelemetryEvent } from './constants'; import { MarkdownFoldingProvider } from './providers/MarkdownFoldingProvider'; -import { TagType } from './panelWebView/TagType'; import { PanelProvider } from './panelWebView/PanelProvider'; import { DashboardSettings, @@ -31,11 +30,13 @@ import { Article, Settings, StatusListener, - Chatbot + Chatbot, + Taxonomy } from './commands'; import { join } from 'path'; import { Terminal } from './services'; import { i18n } from './commands/i18n'; +import { UriHandler } from './providers/UriHandler'; let pageUpdateDebouncer: { (fnc: any, time: number): void }; let editDebounce: { (fnc: any, time: number): void }; @@ -54,6 +55,7 @@ export async function activate(context: vscode.ExtensionContext) { vscode.commands.executeCommand('setContext', CONTEXT.isDevelopment, true); } + // Sponsor check Backers.init(context).then(() => {}); // Make sure the EN language file is loaded @@ -118,32 +120,16 @@ export async function activate(context: vscode.ExtensionContext) { // Folding the front matter of markdown files MarkdownFoldingProvider.register(); - const insertTags = vscode.commands.registerCommand(COMMAND_NAME.insertTags, async () => { - await vscode.commands.executeCommand('workbench.view.extension.frontmatter-explorer'); - await vscode.commands.executeCommand('workbench.action.focusSideBar'); - explorerSidebar.triggerInputFocus(TagType.tags); - }); - - const insertCategories = vscode.commands.registerCommand( - COMMAND_NAME.insertCategories, - async () => { - await vscode.commands.executeCommand('workbench.view.extension.frontmatter-explorer'); - await vscode.commands.executeCommand('workbench.action.focusSideBar'); - explorerSidebar.triggerInputFocus(TagType.categories); - } - ); + // Register the taxonomy commands + Taxonomy.registerCommands(subscriptions); // Register all the article commands Article.registerCommands(subscriptions); - /** - * Template creation - */ + // Template creation Template.registerCommands(); - /** - * Content creation - */ + // Content creation ContentType.registerCommands(); Content.registerCommands(); Folders.registerCommands(); @@ -251,8 +237,11 @@ export async function activate(context: vscode.ExtensionContext) { // Cache commands Cache.registerCommands(); + // Register the URI handler + UriHandler.register(); + // Subscribe all commands - subscriptions.push(insertTags, PanelView, insertCategories, collapseAll, fmStatusBarItem); + subscriptions.push(PanelView, collapseAll, fmStatusBarItem); console.log(`π–₯π—‹π—ˆπ—‡π— 𝖬𝖺𝗍𝗍𝖾𝗋 𝖒𝖬𝖲 𝖺𝖼𝗍𝗂𝗏𝖺𝗍𝖾𝖽! 𝖱𝖾𝖺𝖽𝗒 π—π—ˆ π—Œπ—π–Ίπ—‹π— 𝗐𝗋𝗂𝗍𝗂𝗇𝗀... πŸ‘©β€πŸ’»πŸ§‘β€πŸ’»πŸ‘¨β€πŸ’»`); } diff --git a/src/providers/UriHandler.ts b/src/providers/UriHandler.ts new file mode 100644 index 00000000..fbafa7f0 --- /dev/null +++ b/src/providers/UriHandler.ts @@ -0,0 +1,35 @@ +import { commands, Uri, window } from 'vscode'; +import { EXTENSION_COMMAND_PREFIX } from '../constants'; + +export class UriHandler { + /** + * Register the URI handler + */ + public static register() { + window.registerUriHandler({ + handleUri(uri: Uri) { + const queryParams = new URLSearchParams(uri.query); + if (!queryParams.has('command')) { + return; + } + + const command = queryParams.get('command'); + let args = queryParams.get('args'); + + if (!command || !command.startsWith(EXTENSION_COMMAND_PREFIX)) { + return; + } + + if (args) { + try { + args = JSON.parse(args); + } catch (error) { + // Ignore error + } + } + + commands.executeCommand(command, args); + } + }); + } +}