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