From e6bb06583d5d3761f94f2724b585975aae7c7632 Mon Sep 17 00:00:00 2001 From: Yiyi Wang Date: Mon, 18 Mar 2019 21:48:55 +0800 Subject: [PATCH] 0.3.13 (#203) * Try to fix isseu https://github.com/shd101wyy/vscode-markdown-preview-enhanced/issues/202 * Try to support https://github.com/shd101wyy/vscode-markdown-preview-enhanced/issues/198 * Add mathRenderingOnlineService setting * Add/Change "openPreview" and "openPreviewToTheSide" commands. * Upgrade mume to 0.3.8 --- README.md | 3 ++- package.json | 27 +++++++++++++++++----- src/config.ts | 6 ++--- src/extension.ts | 31 +++++++++++++++++++++++-- src/preview-content-provider.ts | 41 +++++++++++++++++++++++++-------- 5 files changed, 87 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index ff9ebd6..473d0d8 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,8 @@ Contact me if you are willing to help translate the documentation :) | Shortcuts | Functionality | | ------------------------------------------- | -------------------------- | -| cmd-k v | Open preview | +| cmd-k v | Open preview to the Side | +| ctrl-shift-v | Open preview | | ctrl-shift-s | Sync preview / Sync source | | shift-enter | Run Code Chunk | | ctrl-shift-enter | Run all Code Chunks | diff --git a/package.json b/package.json index 26399b1..90a2bf0 100644 --- a/package.json +++ b/package.json @@ -20,11 +20,20 @@ ], "activationEvents": [ "onLanguage:markdown", - "onCommand:markdown-preview-enhanced.openPreview" + "onCommand:markdown-preview-enhanced.openPreviewToTheSide" ], "main": "./out/src/extension", "contributes": { "commands": [ + { + "command": "markdown-preview-enhanced.openPreviewToTheSide", + "title": "Markdown Preview Enhanced: Open Preview to the Side", + "category": "Markdown", + "icon": { + "light": "./media/PreviewOnRightPane_16x.svg", + "dark": "./media/PreviewOnRightPane_16x_dark.svg" + } + }, { "command": "markdown-preview-enhanced.openPreview", "title": "Markdown Preview Enhanced: Open Preview", @@ -105,11 +114,17 @@ ], "keybindings": [ { - "command": "markdown-preview-enhanced.openPreview", + "command": "markdown-preview-enhanced.openPreviewToTheSide", "key": "ctrl+k v", "mac": "cmd+k v", "when": "editorLangId == markdown" }, + { + "command": "markdown-preview-enhanced.openPreview", + "key": "ctrl+shift+v", + "mac": "cmd+shift+v", + "when": "editorLangId == markdown" + }, { "command": "markdown-preview-enhanced.runAllCodeChunks", "key": "ctrl+shift+enter", @@ -129,14 +144,14 @@ "menus": { "editor/context": [ { - "command": "markdown-preview-enhanced.openPreview", + "command": "markdown-preview-enhanced.openPreviewToTheSide", "when": "editorLangId == markdown", "group": "markdown-preview-enhanced" } ], "editor/title": [ { - "command": "markdown-preview-enhanced.openPreview", + "command": "markdown-preview-enhanced.openPreviewToTheSide", "when": "editorLangId == markdown", "group": "navigation" } @@ -219,7 +234,7 @@ ], "type": "array" }, - "markdown-preview-enhanced.saveAsMarkdown.mathRenderingOnLineService": { + "markdown-preview-enhanced.mathRenderingOnlineService": { "description": "Choose the Math expression rendering method option for GFM markdown export (Save as Markdown).", "default": "https://latex.codecogs.com/gif.latex", "type": "string", @@ -439,7 +454,7 @@ "test": "npm run compile && node ./node_modules/vscode/bin/test" }, "dependencies": { - "@shd101wyy/mume": "^0.3.7", + "@shd101wyy/mume": "^0.3.8", "@types/vfile": "^3.0.2" }, "devDependencies": { diff --git a/src/config.ts b/src/config.ts index b378b88..0b2886b 100644 --- a/src/config.ts +++ b/src/config.ts @@ -20,7 +20,7 @@ export class MarkdownPreviewEnhancedConfig implements MarkdownEngineConfig { public readonly mathRenderingOption: MathRenderingOption; public readonly mathInlineDelimiters: string[][]; public readonly mathBlockDelimiters: string[][]; - public readonly mathRenderingOnLineService: string; + public readonly mathRenderingOnlineService: string; public readonly codeBlockTheme: string; public readonly mermaidTheme: string; public readonly previewTheme: string; @@ -69,8 +69,8 @@ export class MarkdownPreviewEnhancedConfig implements MarkdownEngineConfig { ) as MathRenderingOption; this.mathInlineDelimiters = config.get("mathInlineDelimiters"); this.mathBlockDelimiters = config.get("mathBlockDelimiters"); - this.mathRenderingOnLineService = config.get( - "saveAsMarkdown.mathRenderingOnLineService", + this.mathRenderingOnlineService = config.get( + "mathRenderingOnlineService", ); this.codeBlockTheme = config.get("codeBlockTheme"); this.previewTheme = config.get("previewTheme"); diff --git a/src/extension.ts b/src/extension.ts index 0b28163..7ac2a18 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -20,6 +20,20 @@ export function activate(context: vscode.ExtensionContext) { // assume only one preview supported. const contentProvider = new MarkdownPreviewEnhancedView(context); + function openPreviewToTheSide(uri?: vscode.Uri) { + let resource = uri; + if (!(resource instanceof vscode.Uri)) { + if (vscode.window.activeTextEditor) { + // we are relaxed and don't check for markdown files + resource = vscode.window.activeTextEditor.document.uri; + } + } + contentProvider.initPreview(resource, vscode.window.activeTextEditor, { + viewColumn: vscode.ViewColumn.Two, + preserveFocus: true, + }); + } + function openPreview(uri?: vscode.Uri) { let resource = uri; if (!(resource instanceof vscode.Uri)) { @@ -28,7 +42,10 @@ export function activate(context: vscode.ExtensionContext) { resource = vscode.window.activeTextEditor.document.uri; } } - contentProvider.initPreview(resource, vscode.window.activeTextEditor); + contentProvider.initPreview(resource, vscode.window.activeTextEditor, { + viewColumn: vscode.ViewColumn.One, + preserveFocus: false, + }); } function toggleScrollSync() { @@ -500,7 +517,10 @@ export function activate(context: vscode.ExtensionContext) { isUsingSinglePreview && !contentProvider.previewHasTheSameSingleSourceUri(sourceUri) ) { - contentProvider.initPreview(sourceUri, textEditor); + contentProvider.initPreview(sourceUri, textEditor, { + viewColumn: contentProvider.getPreview(sourceUri).viewColumn, + preserveFocus: true, + }); } else if ( !isUsingSinglePreview && automaticallyShowPreviewOfMarkdownBeingEdited @@ -528,6 +548,13 @@ export function activate(context: vscode.ExtensionContext) { })) */ + context.subscriptions.push( + vscode.commands.registerCommand( + "markdown-preview-enhanced.openPreviewToTheSide", + openPreviewToTheSide, + ), + ); + context.subscriptions.push( vscode.commands.registerCommand( "markdown-preview-enhanced.openPreview", diff --git a/src/preview-content-provider.ts b/src/preview-content-provider.ts index 21701c0..fd179ac 100644 --- a/src/preview-content-provider.ts +++ b/src/preview-content-provider.ts @@ -233,7 +233,8 @@ export class MarkdownPreviewEnhancedView { if (useSinglePreview()) { this.singlePreviewPanel = null; this.singlePreviewPanelSourceUriTarget = null; - return (this.previewMaps = {}); + this.preview2EditorMap = new Map(); + this.previewMaps = {}; } else { const previewPanel = this.getPreview(sourceUri); if (previewPanel) { @@ -323,12 +324,31 @@ export class MarkdownPreviewEnhancedView { return engine; } - public async initPreview(sourceUri: vscode.Uri, editor: vscode.TextEditor) { + public async initPreview( + sourceUri: vscode.Uri, + editor: vscode.TextEditor, + viewOptions: { viewColumn: vscode.ViewColumn; preserveFocus?: boolean }, + ) { const isUsingSinglePreview = useSinglePreview(); let previewPanel: vscode.WebviewPanel; if (isUsingSinglePreview && this.singlePreviewPanel) { - previewPanel = this.singlePreviewPanel; - this.singlePreviewPanelSourceUriTarget = sourceUri; + const oldResourceRoot = + this.getProjectDirectoryPath( + this.singlePreviewPanelSourceUriTarget, + vscode.workspace.workspaceFolders, + ) || path.dirname(this.singlePreviewPanelSourceUriTarget.fsPath); + const newResourceRoot = + this.getProjectDirectoryPath( + sourceUri, + vscode.workspace.workspaceFolders, + ) || path.dirname(sourceUri.fsPath); + if (oldResourceRoot !== newResourceRoot) { + this.singlePreviewPanel.dispose(); + return this.initPreview(sourceUri, editor, viewOptions); + } else { + previewPanel = this.singlePreviewPanel; + this.singlePreviewPanelSourceUriTarget = sourceUri; + } } else if (this.previewMaps[sourceUri.fsPath]) { previewPanel = this.previewMaps[sourceUri.fsPath]; } else { @@ -341,14 +361,14 @@ export class MarkdownPreviewEnhancedView { this.getProjectDirectoryPath( sourceUri, vscode.workspace.workspaceFolders, - ), + ) || path.dirname(sourceUri.fsPath), ), ]; previewPanel = vscode.window.createWebviewPanel( "markdown-preview-enhanced", `Preview ${path.basename(sourceUri.fsPath)}`, - { viewColumn: vscode.ViewColumn.Two, preserveFocus: true }, + viewOptions, { enableFindWidget: true, localResourceRoots, @@ -487,7 +507,7 @@ export class MarkdownPreviewEnhancedView { // not presentation mode vscode.workspace.openTextDocument(sourceUri).then((document) => { const text = document.getText(); - previewPanel.webview.postMessage({ + this.previewPostMessage(sourceUri, { command: "startParsingMarkdown", }); @@ -510,7 +530,7 @@ export class MarkdownPreviewEnhancedView { // restart iframe this.refreshPreview(sourceUri); } else { - previewPanel.webview.postMessage({ + this.previewPostMessage(sourceUri, { command: "updateHTML", html, tocHTML, @@ -534,7 +554,10 @@ export class MarkdownPreviewEnhancedView { editor.document.uri && editor.document.uri.fsPath === sourceUri.fsPath ) { - this.initPreview(sourceUri, editor); + this.initPreview(sourceUri, editor, { + viewColumn: previewPanel.viewColumn, + preserveFocus: true, + }); } }); }