Skip to content

Commit

Permalink
feat: improve preview webview UX
Browse files Browse the repository at this point in the history
  • Loading branch information
kermanx committed May 9, 2024
1 parent 731e4af commit 7305de3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
4 changes: 2 additions & 2 deletions packages/vscode/src/composables/usePreviewState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export const usePreviewState = createSingletonComposable(() => {
const port = computed(() => activeServer.value?.port.value)
const compatMode = useVscodeContext('slidev:preview:compat', () => !!activeServer.value?.state.compatMode)
const ready = useVscodeContext('slidev:preview:ready', () => !!activeServer.value?.state.ready)
const message = computed(() => activeServer.value?.state.message ?? 'No active project')
const message = computed(() => activeServer.value?.state.message ?? '')
const html = computed(() => ready.value
? generateReadyHtml(port.value!)
: generateErrorHtml(activeProject.value, message.value),
: generateErrorHtml(message.value),
)

function refresh() {
Expand Down
40 changes: 25 additions & 15 deletions packages/vscode/src/html/error.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
import type { SlidevProject } from '../projects'
import { activeProject, projects } from '../projects'
import { getSlidesTitle } from '../utils/getSlidesTitle'

export function generateErrorHtml(project: SlidevProject | undefined, message: string) {
const infoText = project
export function generateErrorHtml(message: string) {
const project = activeProject.value
const info = project
? `Slidev server for <i>${getSlidesTitle(project.data)}</i> ${project.port
? `not found on <code>http://localhost:${project.port}</code>`
: `not started`
}.`
: `No active project`
: projects.size
? `No active project`
: `No projects found`
const errorMessage = message ? `(${message})` : ``
const instruction = project
? `Please start the server first.`
: projects.size
? `Please choose one first.`
: `Please add one first.`
const action = project
? project.port
? ``
: `<button onclick="sendCommand('start-dev')"> Start Dev Server </button>`
: projects.size
? `<button onclick="sendCommand('choose-entry')"> Choose active project </button>`
: `<button onclick="sendCommand('add-entry')"> Add markdown file </button>`
return `
<head>
<script>
const vscode = acquireVsCodeApi()
window.sendCommand = (command) => void vscode.postMessage({ command })
window.sendCommand = (command) => void vscode.postMessage({ type: 'command', command })
</script>
<style>
button {
Expand Down Expand Up @@ -45,17 +61,11 @@ export function generateErrorHtml(project: SlidevProject | undefined, message: s
</head>
<body>
<div style="text-align: center">
<p>
${infoText}
</p>
<p>
${message ? `(${message})` : ''}
</p>
<p>
please start the server first.
</p>
<p> ${info} </p>
<p> ${errorMessage} </p>
<p> ${instruction} </p>
<div class="action-container">
${project?.port ? `<button onclick="sendCommand('start-dev')"> Start Dev Server </button>` : ''}
${action}
<button onclick="sendCommand('config-port')"> Configure Port </button>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode/src/html/ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function generateReadyHtml(port: number) {
else
vscode.postMessage({
...data,
command: 'update-state',
type: 'update-state',
})
}
})
Expand Down
9 changes: 3 additions & 6 deletions packages/vscode/src/views/previewWebview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,10 @@ export const usePreviewWebview = createSingletonComposable(() => {
localResourceRoots: [extCtx.value.extensionUri],
}
view.value.webview.onDidReceiveMessage(async (data) => {
if (data.command === 'start-dev') {
commands.executeCommand('slidev.start-dev')
if (data.type === 'command') {
commands.executeCommand(`slidev.${data.command}`)
}
else if (data.command === 'config-port') {
commands.executeCommand('slidev.config-port')
}
else if (data.command === 'update-state') {
else if (data.type === 'update-state') {
Object.assign(previewNavState, data.navState)
if (initializedClientId.value !== data.clientId) {
initializedClientId.value = data.clientId
Expand Down

0 comments on commit 7305de3

Please sign in to comment.