Skip to content

Commit 5a5785a

Browse files
committed
Add command to open website preview URL in external browser with editor context menu integration
1 parent c882978 commit 5a5785a

3 files changed

Lines changed: 66 additions & 8 deletions

File tree

‎apps/editor/package.json‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,11 @@
352352
"title": "Remove Folder from Workspace",
353353
"category": "Code Web Chat",
354354
"icon": "$(close)"
355+
},
356+
{
357+
"command": "codeWebChat.openWebsitePreviewUrl",
358+
"title": "Open URL in Browser",
359+
"icon": "$(link-external)"
355360
}
356361
],
357362
"submenus": [
@@ -525,6 +530,10 @@
525530
{
526531
"command": "codeWebChat.removeFolderFromWorkspace",
527532
"when": "false"
533+
},
534+
{
535+
"command": "codeWebChat.openWebsitePreviewUrl",
536+
"when": "codeWebChat.websitePreviewActive"
528537
}
529538
],
530539
"view/title": [
@@ -646,6 +655,13 @@
646655
"when": "scmProvider == git"
647656
}
648657
],
658+
"editor/title": [
659+
{
660+
"command": "codeWebChat.openWebsitePreviewUrl",
661+
"when": "codeWebChat.websitePreviewActive",
662+
"group": "navigation@1"
663+
}
664+
],
649665
"view/item/context": [
650666
{
651667
"command": "codeWebChat.closeEditor",

‎apps/editor/src/extension.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
} from './commands/checkpoints-command/actions'
4343
import { CHECKPOINTS_STATE_KEY } from './constants/state-keys'
4444
import { SettingsProvider } from './views/settings/backend/settings-provider'
45+
import { get_current_preview_url } from './views/panel/backend/message-handlers/handle-open-website'
4546

4647
// Store WebSocketServer instance at module level
4748
let websocket_server_instance: WebSocketManager | null = null
@@ -154,6 +155,15 @@ export async function activate(context: vscode.ExtensionContext) {
154155
}
155156
),
156157
generate_commit_message_command(context),
158+
vscode.commands.registerCommand(
159+
'codeWebChat.openWebsitePreviewUrl',
160+
async () => {
161+
const url = get_current_preview_url()
162+
if (url) {
163+
await vscode.env.openExternal(vscode.Uri.parse(url))
164+
}
165+
}
166+
),
157167
vscode.commands.registerCommand(
158168
'codeWebChat.revealInExplorer',
159169
async (item: any) => {

‎apps/editor/src/views/panel/backend/message-handlers/handle-open-website.ts‎

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import * as fs from 'fs'
44
import { get_website_file_path } from '../utils/website-fetcher'
55
import { marked } from 'marked'
66

7+
let current_preview_url: string | undefined
8+
9+
export const get_current_preview_url = () => current_preview_url
10+
711
export const handle_open_website = async (message: OpenWebsiteMessage) => {
812
const file_path = get_website_file_path(message.url)
913

@@ -17,28 +21,56 @@ export const handle_open_website = async (message: OpenWebsiteMessage) => {
1721
vscode.ViewColumn.One
1822
)
1923

24+
current_preview_url = message.url
25+
vscode.commands.executeCommand(
26+
'setContext',
27+
'codeWebChat.websitePreviewActive',
28+
true
29+
)
30+
31+
panel.onDidChangeViewState((e) => {
32+
if (e.webviewPanel.active) {
33+
current_preview_url = message.url
34+
vscode.commands.executeCommand(
35+
'setContext',
36+
'codeWebChat.websitePreviewActive',
37+
true
38+
)
39+
} else {
40+
vscode.commands.executeCommand(
41+
'setContext',
42+
'codeWebChat.websitePreviewActive',
43+
false
44+
)
45+
}
46+
})
47+
48+
panel.onDidDispose(() => {
49+
if (current_preview_url === message.url) {
50+
current_preview_url = undefined
51+
vscode.commands.executeCommand(
52+
'setContext',
53+
'codeWebChat.websitePreviewActive',
54+
false
55+
)
56+
}
57+
})
58+
2059
panel.webview.html = `
2160
<!DOCTYPE html>
2261
<html lang="en">
2362
<head>
2463
<meta charset="UTF-8">
2564
<meta name="viewport" content="width=device-width, initial-scale=1.0">
26-
<title>Website Preview</title>
65+
<title>${message.url}</title>
2766
<style>
2867
body {
2968
line-height: 1.5;
3069
padding: 20px 40px;
3170
}
32-
hr {
33-
height: 1px;
34-
border: none;
35-
background-color: var(--vscode-editorWidget-border);
36-
}
3771
</style>
3872
</head>
3973
<body>
40-
<a href="${message.url}">${message.url}</a>
41-
<hr>
4274
${html}
4375
</body>
4476
</html>

0 commit comments

Comments
 (0)