From c70884fbbcb430444ffe98a9910d78b008efffb5 Mon Sep 17 00:00:00 2001 From: Lex Li Date: Sun, 14 Jan 2024 03:29:50 -0500 Subject: [PATCH] Added basic code. --- .gitignore | 1 + code/package.json | 11 +++++++++++ code/src/common/constants.ts | 1 + code/src/node/preview.ts | 30 +++++++++++++++++++++++++++++- 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 63322724b..06a0bd094 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ CHANGELOG.md # Demo tutorial is populated during the build. lib/esbonio-extensions/esbonio/tutorial_demo +code/.python-version diff --git a/code/package.json b/code/package.json index 3f0efa593..86194852f 100644 --- a/code/package.json +++ b/code/package.json @@ -75,6 +75,12 @@ "icon": "$(open-preview)", "category": "Esbonio" }, + { + "command": "esbonio.preview.showSource", + "title": "Show Source", + "icon": "$(go-to-file)", + "category": "Esbonio" + }, { "command": "esbonio.server.restart", "title": "Restart Language Server", @@ -327,6 +333,11 @@ "alt": "esbonio.preview.open", "group": "navigation", "when": "resourceLangId == restructuredtext" + }, + { + "command": "esbonio.preview.showSource", + "group": "navigation", + "when": "restructuredtextPreviewFocus" } ] }, diff --git a/code/src/common/constants.ts b/code/src/common/constants.ts index abcc3b29f..8ffa0730b 100644 --- a/code/src/common/constants.ts +++ b/code/src/common/constants.ts @@ -5,6 +5,7 @@ export namespace Server { export namespace Commands { export const OPEN_PREVIEW = "esbonio.preview.open" export const OPEN_PREVIEW_TO_SIDE = "esbonio.preview.openSide" + export const SHOW_SOURCE = "esbonio.preview.showSource" export const PREVIEW_FILE = "esbonio.server.previewFile" export const RESTART_SERVER = "esbonio.server.restart" diff --git a/code/src/node/preview.ts b/code/src/node/preview.ts index 1f9f7fb50..006ca923c 100644 --- a/code/src/node/preview.ts +++ b/code/src/node/preview.ts @@ -1,7 +1,7 @@ import * as vscode from 'vscode' import { OutputChannelLogger } from '../common/log' import { EsbonioClient } from './client' -import { Commands, Events, Notifications, Server } from '../common/constants' +import { Commands, Events, Notifications } from '../common/constants' interface PreviewFileParams { uri: string @@ -14,6 +14,8 @@ interface PreviewFileResult { export class PreviewManager { + private static readonly rstPreviewActiveContextKey = + 'restructuredtextPreviewFocus'; private panel?: vscode.WebviewPanel // The uri of the document currently shown in the preview pane @@ -30,6 +32,9 @@ export class PreviewManager { context.subscriptions.push( vscode.commands.registerTextEditorCommand(Commands.OPEN_PREVIEW_TO_SIDE, this.openPreviewToSide, this) ) + context.subscriptions.push( + vscode.commands.registerTextEditorCommand(Commands.SHOW_SOURCE, this.showSource, this) + ) context.subscriptions.push( vscode.window.onDidChangeActiveTextEditor(this.onDidChangeEditor, this) ) @@ -74,6 +79,19 @@ export class PreviewManager { return await this.previewEditor(editor, vscode.ViewColumn.Beside) } + async showSource() { + if (!this.currentUri) { + return + } + + let editor = findEditorFor(this.currentUri) + if (!editor) { + return + } + + await vscode.window.showTextDocument(editor.document, { preview: false }) + } + private scrollEditor(params: { line: number }) { let editor = findEditorFor(this.currentUri) if (!editor) { @@ -133,6 +151,7 @@ export class PreviewManager { this.currentUri = editor.document.uri panel.webview.postMessage({ 'show': result.uri }) + this.setPreviewActiveContext(true) } private getPanel(placement: vscode.ViewColumn): vscode.WebviewPanel { @@ -267,10 +286,19 @@ export class PreviewManager { this.panel.onDidDispose(() => { this.panel = undefined this.currentUri = undefined + this.setPreviewActiveContext(false) }) return this.panel } + + private setPreviewActiveContext(value: boolean) { + vscode.commands.executeCommand( + 'setContext', + PreviewManager.rstPreviewActiveContextKey, + value + ) + } }