返回 slidev
useFocusedSlide.ts
根目录 / packages / vscode / src / composables / useFocusedSlide.ts
1 import type { SlidevProject } from '../projects'
2 import { computed, defineService, useActiveTextEditor, useTextEditorSelection, useTextEditorVisibleRanges, useVscodeContext } from 'reactive-vscode'
3 import { Position, Range, TextEditorRevealType, Uri, window, workspace } from 'vscode'
4 import { activeData } from '../projects'
5 import { getFirstDisplayedChild } from '../utils/getFirstDisplayedChild'
6 import { useDebouncedComputed } from './useDebouncedComputed'
7 import { useProjectFromDoc } from './useProjectFromDoc'
8
9 interface OverviewAnchor {
10 line: number
11 no: number
12 }
13
14 export const useFocusedSlide = defineService(() => {
15 const editor = useActiveTextEditor()
16 const selection = useTextEditorSelection(editor)
17 const visibleRanges = useTextEditorVisibleRanges(editor)
18 const debouncedEditor = useDebouncedComputed(() => editor.value, val => val ? null : 150)
19 const projectInfo = useProjectFromDoc(() => debouncedEditor.value?.document)
20 const focusedMarkdown = computed(() => projectInfo.value?.md)
21 useVscodeContext('slidev:editing-markdown', () => !!focusedMarkdown.value)
22
23 const focusedSourceSlide = useDebouncedComputed(
24 () => {
25 const md = focusedMarkdown.value
26 if (!md || !debouncedEditor.value || !selection.value) {
27 return null
28 }
29 const line = selection.value.active.line + 1
30 const slide = md.slides.find(s => line <= s.end)
31 return slide || md.slides.at(-1)!
32 },
33 (newVal, oldVal) => newVal?.filepath === oldVal?.filepath
34 ? 30
35 : 150,
36 )
37
38 const displayedSourceSlide = computed(() => {
39 if (!focusedSourceSlide.value)
40 return null
41 return getFirstDisplayedChild(focusedSourceSlide.value)
42 })
43 const focusedSlideNo = computed(() => {
44 const slides = projectInfo.value?.project.data.slides
45 if (!slides || !displayedSourceSlide.value)
46 return null
47 const index = slides.findIndex(s => s.source === displayedSourceSlide.value)
48 return index < 0
49 ? null
50 : index + 1
51 })
52 const overviewAnchors = computed(() => {
53 const info = projectInfo.value
54 if (!info)
55 return []
56 const slides = info.project.data.slides
57 return info.md.slides
58 .map((source): OverviewAnchor | null => {
59 const displayedSource = getFirstDisplayedChild(source)
60 const index = slides.findIndex(s => s.source === displayedSource)
61 if (index < 0)
62 return null
63 return {
64 line: source.contentStart,
65 no: index + 1,
66 }
67 })
68 .filter((anchor): anchor is OverviewAnchor => anchor != null)
69 .sort((a, b) => a.no - b.no)
70 })
71 const viewportSlideNo = computed(() => {
72 const range = visibleRanges.value[0]
73 if (!range)
74 return null
75 const start = getFractionalVisibleLine(range.start)
76 const end = getFractionalVisibleLine(range.end)
77 const visibleLines = Math.max(1, end - start)
78 return interpolate(overviewAnchors.value.map(anchor => ({
79 x: (anchor.line - start) / visibleLines,
80 y: anchor.no,
81 })), 0.5)
82 })
83
84 function getFractionalVisibleLine(position: Position) {
85 const document = editor.value?.document
86 const line = position.line
87 if (!document || line < 0 || line >= document.lineCount)
88 return line
89 return line + position.character / (document.lineAt(line).text.length + 2)
90 }
91
92 function interpolate(points: { x: number, y: number }[], x: number) {
93 if (points.length === 0)
94 return null
95 if (points.length === 1)
96 return points[0].y
97 const sorted = [...points].sort((a, b) => a.x - b.x)
98 const minY = Math.min(...sorted.map(point => point.y))
99 const maxY = Math.max(...sorted.map(point => point.y))
100
101 let previous = sorted[0]
102 let next = sorted[1]
103 if (x > sorted.at(-1)!.x) {
104 previous = sorted.at(-2)!
105 next = sorted.at(-1)!
106 }
107 else {
108 for (let i = 1; i < sorted.length; i++) {
109 if (sorted[i].x >= x) {
110 previous = sorted[i - 1]
111 next = sorted[i]
112 break
113 }
114 }
115 }
116
117 const span = next.x - previous.x
118 const y = span === 0
119 ? previous.y
120 : previous.y + (x - previous.x) / span * (next.y - previous.y)
121 return Math.min(Math.max(y, minY), maxY)
122 }
123
124 async function gotoSlide(filepath: string, index: number) {
125 if (focusedMarkdown.value?.filepath === filepath && focusedSourceSlide.value?.index === index)
126 return
127 const slide = activeData.value?.markdownFiles[filepath]?.slides[index]
128 if (!slide)
129 return
130 const document = await workspace.openTextDocument(Uri.file(filepath))
131 const cursorPos = new Position(slide.contentStart, 0)
132 await window.showTextDocument(document, {
133 selection: new Range(cursorPos, cursorPos),
134 })
135 }
136
137 async function focusSlide(project: SlidevProject, no: number) {
138 const source = project.data.slides[no - 1]?.source
139 if (!source || displayedSourceSlide.value === source)
140 return
141 await gotoSlide(source.filepath, source.index)
142 }
143
144 function revealViewportSlide(no: number) {
145 const textEditor = editor.value
146 if (!textEditor)
147 return
148 const line = interpolate(overviewAnchors.value.map(anchor => ({
149 x: anchor.no,
150 y: anchor.line,
151 })), no)
152 if (line == null)
153 return
154 const position = new Position(Math.round(line), 0)
155 textEditor.revealRange(new Range(position, position), TextEditorRevealType.InCenter)
156 }
157
158 return {
159 focusedMarkdown,
160 focusedSourceSlide,
161 focusedSlideNo,
162 viewportSlideNo,
163 gotoSlide,
164 focusSlide,
165 revealViewportSlide,
166 }
167 })
168
168 lines TYPESCRIPT