返回 slidev
error.ts
根目录 / packages / vscode / src / html / error.ts
1 import { activeProject, projects } from '../projects'
2 import { getSlidesTitle } from '../utils/getSlidesTitle'
3
4 export function generateErrorHtml(message: string) {
5 const project = activeProject.value
6 const info = project
7 ? `Slidev server for <i>${getSlidesTitle(project.data)}</i> ${project.port.value
8 ? `not found on <code>http://localhost:${project.port.value}</code>`
9 : `not started`
10 }.`
11 : projects.size
12 ? `No active project`
13 : `No projects found`
14 const errorMessage = message ? `(${message})` : ``
15 const instruction = project
16 ? project.port.value
17 ? `Please check the server status.`
18 : `Please start the server first.`
19 : projects.size
20 ? `Please choose one first.`
21 : `Please add one first.`
22 const action = project
23 ? project.port.value
24 ? ``
25 : `<button onclick="sendCommand('start-dev')"> Start Dev Server </button>`
26 : projects.size
27 ? `<button onclick="sendCommand('choose-entry')"> Choose active project </button>`
28 : `<button onclick="sendCommand('add-entry')"> Add markdown file </button>`
29 return `
30 <head>
31 <script>
32 const vscode = acquireVsCodeApi()
33 window.sendCommand = (command) => void vscode.postMessage({ type: 'command', command })
34 </script>
35 <style>
36 button {
37 background: var(--vscode-button-secondaryBackground);
38 color: var(--vscode-button-secondaryForeground);
39 border: none;
40 padding: 8px 12px;
41 flex-grow: 1;
42 }
43 button:hover {
44 background: var(--vscode-button-secondaryHoverBackground);
45 }
46 code {
47 font-size: 0.9em;
48 font-family: var(--vscode-editor-font-family);
49 background: var(--vscode-textBlockQuote-border);
50 border-radius: 4px;
51 padding: 3px 5px;
52 text-wrap: nowrap;
53 }
54 .action-container {
55 display: flex;
56 flex-wrap: wrap;
57 gap: 4px;
58 justify-content: center;
59 max-width: 180px;
60 margin: 0 auto;
61 }
62 </style>
63 </head>
64 <body>
65 <div style="text-align: center">
66 <p> ${info} </p>
67 <p> ${errorMessage} </p>
68 <p> ${instruction} </p>
69 <div class="action-container">
70 ${action}
71 <button onclick="sendCommand('config-port')"> Configure Port </button>
72 </div>
73 </div>
74 </body>
75 `
76 }
77
77 lines TYPESCRIPT