From d6c63476d2298c767810b06b14ec7bbc1cd03bfe Mon Sep 17 00:00:00 2001 From: aqz236 <2367814400@qq.com> Date: Mon, 7 Jul 2025 10:19:21 +0800 Subject: [PATCH 1/2] feat: add custom editor provider to make markdown editor default for *.md files - Added customEditors configuration in package.json - Implemented MarkdownEditorProvider class - Registered custom editor provider in extension activation - Updated activation events to include custom editor - Made EditorPanel constructor public for external access This allows the markdown editor to be the default editor for .md files while still maintaining compatibility with existing functionality. --- package.json | 15 ++++++++++++++- src/extension.ts | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 732203e..04efbf8 100755 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "activationEvents": [ "onCommand:markdown-editor.openEditor", "onWebviewPanel:markdown-editor", - "onLanguage:markdown" + "onLanguage:markdown", + "onCustomEditor:markdown-editor.editor" ], "repository": { "type": "git", @@ -81,6 +82,18 @@ "mac": "cmd+shift+alt+m", "when": "editorTextFocus && editorLangId == markdown" } + ], + "customEditors": [ + { + "viewType": "markdown-editor.editor", + "displayName": "Markdown Editor", + "selector": [ + { + "filenamePattern": "*.md" + } + ], + "priority": "default" + } ] }, "scripts": { diff --git a/src/extension.ts b/src/extension.ts index f1941b4..dd2c754 100755 --- a/src/extension.ts +++ b/src/extension.ts @@ -10,6 +10,25 @@ function showError(msg: string) { vscode.window.showErrorMessage(`[markdown-editor] ${msg}`) } +/** + * 自定义编辑器提供者 + */ +class MarkdownEditorProvider implements vscode.CustomTextEditorProvider { + constructor(private readonly context: vscode.ExtensionContext) {} + + public async resolveCustomTextEditor( + document: vscode.TextDocument, + webviewPanel: vscode.WebviewPanel, + _token: vscode.CancellationToken + ): Promise { + // 设置webview选项 + webviewPanel.webview.options = EditorPanel.getWebviewOptions(document.uri) + + // 创建EditorPanel实例来处理编辑器逻辑 + new EditorPanel(this.context, webviewPanel, this.context.extensionUri, document, document.uri) + } +} + export function activate(context: vscode.ExtensionContext) { context.subscriptions.push( vscode.commands.registerCommand( @@ -21,6 +40,20 @@ export function activate(context: vscode.ExtensionContext) { ) ) + // 注册自定义编辑器提供者 + context.subscriptions.push( + vscode.window.registerCustomEditorProvider( + 'markdown-editor.editor', + new MarkdownEditorProvider(context), + { + webviewOptions: { + retainContextWhenHidden: true, + }, + supportsMultipleEditorsPerDocument: false, + } + ) + ) + context.globalState.setKeysForSync([KeyVditorOptions]) } @@ -123,7 +156,7 @@ class EditorPanel { return vscode.workspace.getConfiguration('markdown-editor') } - private constructor( + public constructor( private readonly _context: vscode.ExtensionContext, private readonly _panel: vscode.WebviewPanel, private readonly _extensionUri: vscode.Uri, From cbda845895512441ffe9e8bc4a4f073592fc7950 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=E5=85=BD?= <54022108+aqz236@users.noreply.github.com> Date: Thu, 14 Aug 2025 07:56:24 +0800 Subject: [PATCH 2/2] Update extension.ts --- src/extension.ts | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index dd2c754..fe88737 100755 --- a/src/extension.ts +++ b/src/extension.ts @@ -10,9 +10,7 @@ function showError(msg: string) { vscode.window.showErrorMessage(`[markdown-editor] ${msg}`) } -/** - * 自定义编辑器提供者 - */ + class MarkdownEditorProvider implements vscode.CustomTextEditorProvider { constructor(private readonly context: vscode.ExtensionContext) {} @@ -21,10 +19,9 @@ class MarkdownEditorProvider implements vscode.CustomTextEditorProvider { webviewPanel: vscode.WebviewPanel, _token: vscode.CancellationToken ): Promise { - // 设置webview选项 webviewPanel.webview.options = EditorPanel.getWebviewOptions(document.uri) - // 创建EditorPanel实例来处理编辑器逻辑 + // Create an instance of EditorPanel to handle editor logic new EditorPanel(this.context, webviewPanel, this.context.extensionUri, document, document.uri) } } @@ -40,7 +37,7 @@ export function activate(context: vscode.ExtensionContext) { ) ) - // 注册自定义编辑器提供者 + // Register custom editor provider context.subscriptions.push( vscode.window.registerCustomEditorProvider( 'markdown-editor.editor', @@ -91,9 +88,9 @@ class EditorPanel { return } let doc: undefined | vscode.TextDocument - // from context menu : 从当前打开的 textEditor 中寻找 是否有当前 markdown 的 editor, 有的话则绑定 document + // From the context menu: Check if there is a current Markdown editor in the currently open text editor, and if so, bind the document. if (uri) { - // 从右键打开文件,先打开文档然后开启自动同步,不然没法保存文件和同步到已经打开的document + // To open a file from the right-click menu, first open the document and then enable auto-sync; otherwise, you won't be able to save the file or sync it to the already opened document. doc = await vscode.workspace.openTextDocument(uri) } else { doc = vscode.window.activeTextEditor?.document @@ -160,8 +157,8 @@ class EditorPanel { private readonly _context: vscode.ExtensionContext, private readonly _panel: vscode.WebviewPanel, private readonly _extensionUri: vscode.Uri, - public _document: vscode.TextDocument, // 当前有 markdown 编辑器 - public _uri = _document.uri // 从资源管理器打开,只有 uri 没有 _document + public _document: vscode.TextDocument, + public _uri = _document.uri ) { // Set the webview's initial html content @@ -182,7 +179,6 @@ class EditorPanel { if (e.document.fileName !== this._document.fileName) { return } - // 当 webview panel 激活时不将由 webview编辑导致的 vsc 编辑器更新同步回 webview // don't change webview panel when webview panel is focus if (this._panel.active) { return @@ -241,7 +237,7 @@ class EditorPanel { showError(message.content) break case 'edit': { - // 只有当 webview 处于编辑状态时才同步到 vsc 编辑器,避免重复刷新 + // Sync to the VSC editor only when the webview is in edit mode to avoid repeated refreshing. if (this._panel.active) { await syncToEditor() this._updateEditTitle()