返回 slidev
commands.ts
根目录 / packages / vscode / src / commands.ts
1 import { slash } from '@antfu/utils'
2 import { relative } from 'pathe'
3 import { useCommand } from 'reactive-vscode'
4 import { ConfigurationTarget, window, workspace } from 'vscode'
5 import { useDevServer } from './composables/useDevServer'
6 import { useFocusedSlide } from './composables/useFocusedSlide'
7 import { config } from './configs'
8 import { activeEntry, activeProject, addProject, projects, rescanProjects } from './projects'
9 import { findPossibleEntries } from './utils/findPossibleEntries'
10 import { getSlidesTitle } from './utils/getSlidesTitle'
11 import { usePreviewWebview } from './views/previewWebview'
12
13 const RE_DIGITS = /^\d+$/
14
15 export function useCommands() {
16 useCommand('slidev.enable-extension', () => config.update('force-enabled', true, ConfigurationTarget.Workspace))
17 useCommand('slidev.disable-extension', () => config.update('force-enabled', false, ConfigurationTarget.Workspace))
18
19 useCommand('slidev.rescan-projects', rescanProjects)
20
21 useCommand('slidev.choose-entry', async () => {
22 while (true) {
23 const addNewEntry = '$(add) Add new slides entry...'
24 const entry = await window.showQuickPick(
25 [
26 ...projects.keys(),
27 addNewEntry,
28 ],
29 {
30 title: 'Choose active slides entry.',
31 },
32 )
33 if (entry === addNewEntry) {
34 if (!await addEntry()) {
35 break
36 }
37 }
38 else if (entry) {
39 activeEntry.value = entry
40 break
41 }
42 else {
43 break
44 }
45 }
46 })
47
48 useCommand('slidev.add-entry', addEntry)
49
50 async function addEntry() {
51 const files = await findPossibleEntries()
52 const selected = await window.showQuickPick(files, {
53 title: 'Choose Markdown files to add as slides entries.',
54 canPickMany: true,
55 })
56 if (selected) {
57 for (const entry of selected)
58 await addProject(entry)
59 if (workspace.workspaceFolders) {
60 const workspaceRoot = workspace.workspaceFolders[0].uri.fsPath
61 const relatives = selected.map(s => slash(relative(workspaceRoot, s)))
62 // write back to settings.json
63 await config.update('include', [
64 ...config.include,
65 ...relatives,
66 ])
67 }
68 }
69 return !!selected
70 }
71
72 useCommand('slidev.remove-entry', async (node: any) => {
73 const entry = slash(node.treeItem.resourceUri.fsPath)
74 if (activeEntry.value === entry)
75 activeEntry.value = null
76 projects.delete(entry)
77 })
78
79 useCommand('slidev.set-as-active', async (node: any) => {
80 const entry = slash(node.treeItem.resourceUri.fsPath)
81 activeEntry.value = entry
82 })
83
84 useCommand('slidev.goto', (filepath: string, index: number) => {
85 const { gotoSlide } = useFocusedSlide()
86 gotoSlide(filepath, index)
87 })
88 useCommand('slidev.next', () => {
89 const { focusedMarkdown, focusedSourceSlide, gotoSlide } = useFocusedSlide()
90 if (!focusedMarkdown.value || focusedSourceSlide.value == null)
91 return
92 gotoSlide(focusedMarkdown.value.filepath, focusedSourceSlide.value.index + 1)
93 })
94 useCommand('slidev.prev', () => {
95 const { focusedMarkdown, focusedSourceSlide, gotoSlide } = useFocusedSlide()
96 if (!focusedMarkdown.value || focusedSourceSlide.value == null)
97 return
98 gotoSlide(focusedMarkdown.value.filepath, focusedSourceSlide.value.index - 1)
99 })
100
101 useCommand('slidev.refresh-preview', async () => {
102 const { refresh } = usePreviewWebview()
103 await refresh()
104 })
105
106 useCommand('slidev.config-port', async () => {
107 if (!activeProject.value) {
108 window.showErrorMessage('No active project to configure port.')
109 return
110 }
111 const port = await window.showInputBox({
112 prompt: `Slidev Preview Port for ${getSlidesTitle(activeProject.value.data)}`,
113 value: config.port.toString(),
114 validateInput: (v) => {
115 if (!RE_DIGITS.test(v))
116 return 'Port should be a number'
117 if (+v < 1024 || +v > 65535)
118 return 'Port should be between 1024 and 65535'
119 return null
120 },
121 })
122 if (port && activeProject.value) {
123 activeProject.value.port.value = +port
124 }
125 })
126
127 useCommand('slidev.start-dev', async () => {
128 const project = activeProject.value
129 if (!project) {
130 window.showErrorMessage('Cannot start dev server: No active slides project.')
131 return
132 }
133
134 const { start, terminal } = useDevServer(project)
135 start()
136 terminal.value?.show()
137 })
138
139 useCommand('slidev.open-in-browser', () => usePreviewWebview().openExternal())
140
141 useCommand('slidev.preview-prev-click', () => usePreviewWebview().prevClick())
142 useCommand('slidev.preview-next-click', () => usePreviewWebview().nextClick())
143 useCommand('slidev.preview-prev-slide', () => usePreviewWebview().prevSlide())
144 useCommand('slidev.preview-next-slide', () => usePreviewWebview().nextSlide())
145 useCommand('slidev.show-slide-preview', () => usePreviewWebview().setPreviewMode('slide'))
146 useCommand('slidev.show-overview-preview', () => usePreviewWebview().setPreviewMode('overview'))
147
148 useCommand('slidev.enable-preview-sync', () => (config.update('preview-sync', true, ConfigurationTarget.Global)))
149 useCommand('slidev.disable-preview-sync', () => (config.update('preview-sync', false, ConfigurationTarget.Global)))
150 }
151
151 lines TYPESCRIPT