Skip to content

Commit

Permalink
Added basic code.
Browse files Browse the repository at this point in the history
  • Loading branch information
lextm committed Jan 14, 2024
1 parent 0f5ab50 commit c70884f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ CHANGELOG.md

# Demo tutorial is populated during the build.
lib/esbonio-extensions/esbonio/tutorial_demo
code/.python-version
11 changes: 11 additions & 0 deletions code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -327,6 +333,11 @@
"alt": "esbonio.preview.open",
"group": "navigation",
"when": "resourceLangId == restructuredtext"
},
{
"command": "esbonio.preview.showSource",
"group": "navigation",
"when": "restructuredtextPreviewFocus"
}
]
},
Expand Down
1 change: 1 addition & 0 deletions code/src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
30 changes: 29 additions & 1 deletion code/src/node/preview.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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)
)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
)
}
}


Expand Down

0 comments on commit c70884f

Please sign in to comment.