@slidevideo/cli 0.2.2 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +2 -2
  3. package/addons/slidevideo/geometry.ts +334 -1
  4. package/addons/slidevideo/global-top.vue +311 -24
  5. package/addons/slidevideo/preview.ts +242 -25
  6. package/assets/fonts/OFL-LICENSES.txt +95 -0
  7. package/assets/fonts/instrument-serif-latin-2.woff2 +0 -0
  8. package/assets/fonts/instrument-serif-latin-ext-1.woff2 +0 -0
  9. package/assets/fonts/jetbrains-mono-latin-4.woff2 +0 -0
  10. package/assets/fonts/jetbrains-mono-latin-ext-3.woff2 +0 -0
  11. package/assets/fonts/plus-jakarta-sans-latin-6.woff2 +0 -0
  12. package/assets/fonts/plus-jakarta-sans-latin-ext-5.woff2 +0 -0
  13. package/dist/adapters/browser-runtime.d.ts +13 -1
  14. package/dist/adapters/capture-compression.d.ts +15 -0
  15. package/dist/adapters/capture-compression.js +69 -0
  16. package/dist/adapters/powerpoint-preview.d.ts +1 -1
  17. package/dist/adapters/powerpoint-preview.js +13 -2
  18. package/dist/adapters/powerpoint.js +108 -16
  19. package/dist/adapters/slidev-inspection.js +7 -2
  20. package/dist/adapters/slidev.js +362 -3
  21. package/dist/annotation-paint.d.ts +195 -5
  22. package/dist/annotation-paint.js +1188 -2
  23. package/dist/annotations.d.ts +26 -2
  24. package/dist/annotations.js +1117 -29
  25. package/dist/captions.d.ts +6 -0
  26. package/dist/captions.js +320 -0
  27. package/dist/cli.js +40 -17
  28. package/dist/compositor.js +51 -3
  29. package/dist/contracts.d.ts +253 -14
  30. package/dist/pipeline.d.ts +6 -1
  31. package/dist/pipeline.js +66 -19
  32. package/dist/preview/contracts.d.ts +14 -0
  33. package/dist/preview/player.js +523 -33
  34. package/dist/preview/server.d.ts +1 -1
  35. package/dist/preview/server.js +124 -3
  36. package/dist/preview/view.d.ts +1 -1
  37. package/dist/preview/view.js +26 -7
  38. package/dist/run.d.ts +1 -0
  39. package/dist/run.js +20 -0
  40. package/dist/speech.d.ts +10 -1
  41. package/dist/speech.js +18 -2
  42. package/dist/stamp-fonts.d.ts +17 -0
  43. package/dist/stamp-fonts.js +202 -0
  44. package/dist/storyboard.js +60 -3
  45. package/dist/timing.d.ts +2 -0
  46. package/dist/timing.js +277 -11
  47. package/package.json +14 -1
  48. package/schemas/annotation-geometry.v1.json +111 -0
  49. package/schemas/schedule.v1.json +1672 -84
  50. package/schemas/storyboard.v1.json +3725 -85
  51. package/skills/slidevideo/SKILL.md +5 -5
  52. package/skills/slidevideo/references/decks.md +6 -4
  53. package/skills/slidevideo/references/production.md +12 -11
  54. package/skills/slidevideo/references/storyboard.md +268 -5
  55. package/tools/powerpoint/powerpoint.ps1 +278 -40
  56. package/tools/powerpoint/preview.ps1 +69 -1
package/CHANGELOG.md CHANGED
@@ -2,6 +2,24 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.3.1
6
+
7
+ - Export speech-aligned `final.vtt` and `final.srt` captions by default on narrated `build` and `assemble` runs, with `--captions <vtt|srt|both|none>` to configure or export during `resolve`.
8
+ - Add storyboard-controlled `stamp` annotations with bundled reproducible fonts, vector symbols (`check`, `cross`, `warning`, `star`, `info`, `arrow-right`), target/point anchoring, multiline layout and overflow checks, optional rotation, and `fade` or `pop` motion for Slidev and PowerPoint.
9
+ - Make `--run` optional for `build` (without `--resume`) and `tts`, defaulting to a timestamped directory under `runs/` and reporting `run_dir` in CLI output.
10
+ - Default speech caching in `build`, `tts`, and `play` to `.slidevideo/speech` beside the storyboard, with `--cache-dir` to override and `--no-cache` to disable.
11
+ - Add storyboard-controlled `laser` pointer annotations with normalized or target-anchored paths, linear or smooth Catmull-Rom interpolation, waypoint dwell, glow, decaying trails, and fade transitions for Slidev and PowerPoint.
12
+ - Add storyboard-controlled `zoom` annotations with target fitting, padding, scale limits, smooth ease-in/hold/ease-out transitions, locked slide-attached annotations, and frame-fixed overlays for Slidev and PowerPoint.
13
+ - Add storyboard-controlled `spotlight` annotations with rounded-rectangle or elliptical apertures, optional edge softness and fades, and unified multi-spotlight compositing for Slidev and PowerPoint.
14
+ - Add declarative text-range targets (`type: text`) and narration-aligned word-following annotations (`follow`) for Slidev and PowerPoint.
15
+ - Add timeline scrubbing, scene jumping, and cached narration audio playback with mute/unmute to `slidevideo play`.
16
+
17
+ ## 0.3.0
18
+
19
+ - Speed up normal Slidev and PowerPoint recording by compressing static slide holds while keeping dynamic slides and `--verify-timing` at `1.0x`.
20
+ - Fix native PowerPoint inspection timeouts on larger decks, slideshow startup races, and sequential click-reveal playback.
21
+ - Make speech caches portable across directories by storing relative audio paths in cache metadata.
22
+
5
23
  ## 0.2.2
6
24
 
7
25
  - Support finite native PowerPoint slide transitions, broader finite `MainSequence` animations (including `Wipe`, `Split`, `Fly In`, emphasis, exit, motion paths, and automatic pre-click entry), static `.svg` graphics, cropped pictures, grouped shape targets, dynamic full-resolution window capture with output letterboxing, and storyboard video trims (`trim_start_ms` / `trim_end_ms`).
package/README.md CHANGED
@@ -13,10 +13,10 @@ Setup prepares shared tools, checks both speech providers, and detects Slidev an
13
13
 
14
14
  ```sh
15
15
  slidevideo validate --storyboard slidevideo.yaml
16
- slidevideo build --storyboard slidevideo.yaml --run runs/hello-001
16
+ slidevideo build --storyboard slidevideo.yaml
17
17
  ```
18
18
 
19
- A completed build writes `runs/hello-001/final.mp4` at `1920x1080` and `30 fps` by default. Pass `--width` and `--height` together and optional `--fps` to select another resolution or frame rate. Use `slidevideo setup --license` to activate a key. The installed version checks its bundled revocation list offline. New exports using a revoked key include the watermark.
19
+ A completed build writes `final.mp4` (plus `final.vtt` and `final.srt` captions for narrated decks) inside a fresh `runs/<timestamp>` directory (or the path passed to `--run`) at `1920x1080` and `30 fps` by default, and caches generated speech in `.slidevideo/speech` beside the storyboard for reuse across runs (override with `--cache-dir` or disable with `--no-cache`). Pass `--width` and `--height` together and optional `--fps` to select another resolution or frame rate, and `--captions <vtt|srt|both|none>` to control subtitle export. Use `slidevideo setup --license` to activate a key. The installed version checks its bundled revocation list offline. New exports using a revoked key include the watermark.
20
20
 
21
21
  To draft or edit a storyboard with a coding agent, run `slidevideo skill --install` to link the bundled skill into `~/.agents/skills/slidevideo` and `~/.claude/skills/slidevideo` when Claude Code is detected, or ask the agent to run `slidevideo skill`. For non-interactive setup, run `slidevideo setup --install --json` and see `slidevideo setup --help` and `slidevideo skill --help` for provider and project flags. Use `slidevideo inspect --deck <file> --json` to list slide names, click counts, media IDs, and highlight targets before creating `slidevideo.yaml`. The CLI includes the skill and its storyboard, deck-compatibility, and recording references.
22
22
 
@@ -1,8 +1,30 @@
1
- import type { AnnotationGeometry, GeometryRequest, Rectangle } from '../../dist/contracts.js'
1
+ import type { AnnotationGeometry, GeometryRequest, Rectangle, ResolvedTextTarget } from '../../dist/contracts.js'
2
2
 
3
3
  type PlaybackPosition = { slide: string; visit: string; click: number; index: number }
4
4
  type Measurement = { fragments: Rectangle[]; content: Rectangle; slide: Rectangle; viewport: { width: number; height: number }; elements: Element[] }
5
+ type DomCharSpan = { startNode: Text; startOffset: number; endNode: Text; endOffset: number }
6
+ const INLINE_FLOW_DISPLAY = new Set([
7
+ 'inline',
8
+ 'contents',
9
+ 'ruby',
10
+ 'ruby-base',
11
+ 'ruby-text',
12
+ 'ruby-base-container',
13
+ 'ruby-text-container',
14
+ ])
15
+ const ATOMIC_INLINE_DISPLAY = new Set([
16
+ 'inline-block',
17
+ 'inline-flex',
18
+ 'inline-grid',
19
+ 'inline-table',
20
+ ])
5
21
  const rectangle = (rect: DOMRect): Rectangle => ({ x: rect.x, y: rect.y, width: rect.width, height: rect.height })
22
+ function isElementVisible(element: Element): boolean {
23
+ const style = getComputedStyle(element)
24
+ if (style.display === 'none' || style.visibility !== 'visible' || Number(style.opacity) === 0) return false
25
+ if (style.display === 'contents') return element.parentElement ? isElementVisible(element.parentElement) : true
26
+ return element.checkVisibility({ opacityProperty: true, visibilityProperty: true })
27
+ }
6
28
  function axisAligned(element: Element) {
7
29
  const style = getComputedStyle(element)
8
30
  if (style.clipPath !== 'none' || style.maskImage !== 'none' || style.clip !== 'auto') throw new Error('Clipped or masked targets are unsupported')
@@ -46,6 +68,311 @@ function visibleBounds(element: Element, content: HTMLElement): Rectangle {
46
68
  }
47
69
  return bounds
48
70
  }
71
+ function checkTextAncestors(element: Element, bounds: Rectangle, content: HTMLElement, contentRect: Rectangle) {
72
+ if (!isElementVisible(element)) throw new Error('Target is hidden')
73
+ if (!(bounds.width > 0 && bounds.height > 0)) throw new Error('Target has empty bounds')
74
+ if (!within(bounds, contentRect)) throw new Error('Target is clipped by the slide-content rectangle')
75
+ for (let ancestor: Element | null = element; ancestor; ancestor = ancestor.parentElement) {
76
+ if (ancestor instanceof SVGGElement) throw new Error('Grouped SVG targets are unsupported')
77
+ const style = getComputedStyle(ancestor)
78
+ if (style.display !== 'contents') {
79
+ axisAligned(ancestor)
80
+ if (/(hidden|clip|scroll|auto)/.test(`${style.overflowX} ${style.overflowY}`)) {
81
+ const clip = rectangle(ancestor.getBoundingClientRect())
82
+ if (ancestor instanceof HTMLElement && ancestor.offsetWidth && ancestor.offsetHeight) {
83
+ const scaleX = clip.width / ancestor.offsetWidth, scaleY = clip.height / ancestor.offsetHeight
84
+ clip.x += ancestor.clientLeft * scaleX; clip.y += ancestor.clientTop * scaleY
85
+ clip.width = ancestor.clientWidth * scaleX; clip.height = ancestor.clientHeight * scaleY
86
+ }
87
+ if (!within(bounds, clip)) throw new Error('Target is clipped by an ancestor')
88
+ }
89
+ }
90
+ if (ancestor === content) break
91
+ }
92
+ if (element instanceof SVGGraphicsElement) {
93
+ const matrix = element.getScreenCTM()
94
+ if (!matrix || Math.abs(matrix.b) > 0.00001 || Math.abs(matrix.c) > 0.00001 || matrix.a <= 0 || matrix.d <= 0) throw new Error('Rotated, skewed or reflected SVG targets are unsupported')
95
+ }
96
+ }
97
+ const SMALL_KANA_MAP: Record<string, string> = {
98
+ ぁ: 'あ', ぃ: 'い', ぅ: 'う', ぇ: 'え', ぉ: 'お', っ: 'つ', ゃ: 'や', ゅ: 'ゆ', ょ: 'よ', ゎ: 'わ', ゕ: 'か', ゖ: 'け',
99
+ ァ: 'ア', ィ: 'イ', ゥ: 'ウ', ェ: 'エ', ォ: 'オ', ッ: 'ツ', ャ: 'ヤ', ュ: 'ユ', ョ: 'ヨ', ヮ: 'ワ', ヵ: 'カ', ヶ: 'ケ',
100
+ ァ: 'ア', ィ: 'イ', ゥ: 'ウ', ェ: 'エ', ォ: 'オ', ッ: 'ツ', ャ: 'ヤ', ュ: 'ユ', ョ: 'ヨ',
101
+ }
102
+ type CaseMode = 'none' | 'uppercase' | 'lowercase' | 'capitalize'
103
+ type ParsedTextTransform = { caseMode: CaseMode; fullWidth: boolean; fullSizeKana: boolean; lang: string | undefined }
104
+ function effectiveLang(element: Element): string | undefined {
105
+ for (let el: Element | null = element; el; el = el.parentElement) {
106
+ const lang = el.getAttribute('lang') || el.getAttribute('xml:lang')
107
+ if (lang) return lang
108
+ }
109
+ return undefined
110
+ }
111
+ function parseTextTransform(textTransform: string, element: Element): ParsedTextTransform {
112
+ if (!textTransform || textTransform === 'none') {
113
+ return { caseMode: 'none', fullWidth: false, fullSizeKana: false, lang: undefined }
114
+ }
115
+ const tokens = new Set(textTransform.trim().split(/\s+/))
116
+ const caseMode: CaseMode = tokens.has('uppercase')
117
+ ? 'uppercase'
118
+ : tokens.has('lowercase')
119
+ ? 'lowercase'
120
+ : tokens.has('capitalize')
121
+ ? 'capitalize'
122
+ : 'none'
123
+ const fullWidth = tokens.has('full-width')
124
+ const fullSizeKana = tokens.has('full-size-kana')
125
+ const lang = caseMode !== 'none' ? effectiveLang(element) : undefined
126
+ return { caseMode, fullWidth, fullSizeKana, lang }
127
+ }
128
+ function caseString(value: string, mode: 'uppercase' | 'lowercase', lang: string | undefined): string {
129
+ if (mode === 'uppercase') {
130
+ try { return lang ? value.toLocaleUpperCase(lang) : value.toUpperCase() } catch { return value.toUpperCase() }
131
+ }
132
+ try { return lang ? value.toLocaleLowerCase(lang) : value.toLowerCase() } catch { return value.toLowerCase() }
133
+ }
134
+ function graphemeSegmenter(lang: string | undefined): Intl.Segmenter {
135
+ try { return new Intl.Segmenter(lang || undefined, { granularity: 'grapheme' }) }
136
+ catch { return new Intl.Segmenter(undefined, { granularity: 'grapheme' }) }
137
+ }
138
+ function wordStartOffsets(fullRaw: string, lang: string | undefined): Set<number> {
139
+ let segmenter: Intl.Segmenter
140
+ try { segmenter = new Intl.Segmenter(lang || undefined, { granularity: 'word' }) }
141
+ catch { segmenter = new Intl.Segmenter(undefined, { granularity: 'word' }) }
142
+ const starts = new Set<number>()
143
+ for (const seg of segmenter.segment(fullRaw)) {
144
+ if (seg.isWordLike) starts.add(seg.index)
145
+ }
146
+ return starts
147
+ }
148
+ function extractNormalizedText(root: Element): { text: string; map: DomCharSpan[] } {
149
+ let text = ''
150
+ const map: DomCharSpan[] = []
151
+ let lastNode: Text | undefined
152
+ let lastOffset = 0
153
+ type SourceChar = { node: Text; offset: number; style: ParsedTextTransform }
154
+ let currentRunRaw = ''
155
+ let currentRunMap: SourceChar[] = []
156
+ function flushRun() {
157
+ if (!currentRunRaw) return
158
+ const fullRaw = currentRunRaw
159
+ const sourceMap = currentRunMap
160
+ currentRunRaw = ''
161
+ currentRunMap = []
162
+ const casedCache = new Map<string, string>()
163
+ const wordStartsCache = new Map<string, Set<number>>()
164
+ const getCasedFull = (mode: 'uppercase' | 'lowercase', lang: string | undefined) => {
165
+ const key = `${mode}\0${lang ?? ''}`
166
+ let cached = casedCache.get(key)
167
+ if (cached === undefined) {
168
+ cached = caseString(fullRaw, mode, lang)
169
+ casedCache.set(key, cached)
170
+ }
171
+ return cached
172
+ }
173
+ const getWordStarts = (lang: string | undefined) => {
174
+ const key = lang ?? ''
175
+ let cached = wordStartsCache.get(key)
176
+ if (!cached) {
177
+ cached = wordStartOffsets(fullRaw, lang)
178
+ wordStartsCache.set(key, cached)
179
+ }
180
+ return cached
181
+ }
182
+ const casedBoundary = (pos: number, fullCased: string, mode: 'uppercase' | 'lowercase', lang: string | undefined) => {
183
+ if (pos <= 0) return 0
184
+ if (pos >= fullRaw.length) return fullCased.length
185
+ if (fullCased.length === fullRaw.length) return pos
186
+ const probe = fullRaw.slice(0, pos) + (/^\p{L}/u.test(fullRaw.slice(pos)) ? 'a' : ' ')
187
+ return caseString(probe, mode, lang).length - 1
188
+ }
189
+ for (const g of graphemeSegmenter(sourceMap[0].style.lang).segment(fullRaw)) {
190
+ const gStart = g.index
191
+ const gEnd = g.index + g.segment.length
192
+ const startSrc = sourceMap[gStart]
193
+ const endSrc = sourceMap[gEnd - 1]
194
+ const { caseMode, fullWidth, fullSizeKana, lang } = startSrc.style
195
+ let rendered = g.segment
196
+ if (caseMode === 'uppercase' || caseMode === 'lowercase') {
197
+ const fullCased = getCasedFull(caseMode, lang)
198
+ rendered = fullCased.slice(casedBoundary(gStart, fullCased, caseMode, lang), casedBoundary(gEnd, fullCased, caseMode, lang))
199
+ } else if (caseMode === 'capitalize' && getWordStarts(lang).has(gStart) && /^\p{L}/u.test(rendered)) {
200
+ const firstCp = rendered.codePointAt(0)!
201
+ const firstLen = firstCp > 0xFFFF ? 2 : 1
202
+ rendered = caseString(rendered.slice(0, firstLen), 'uppercase', lang) + rendered.slice(firstLen)
203
+ }
204
+ if (fullSizeKana) {
205
+ let kanaOut = ''
206
+ for (const ch of rendered) kanaOut += SMALL_KANA_MAP[ch] ?? ch
207
+ rendered = kanaOut
208
+ }
209
+ if (fullWidth) {
210
+ let fwOut = ''
211
+ for (const ch of rendered) {
212
+ const cp = ch.codePointAt(0)!
213
+ if (cp === 0x20) fwOut += '\u3000'
214
+ else if (cp >= 0x21 && cp <= 0x7E) fwOut += String.fromCodePoint(cp + 0xFEE0)
215
+ else fwOut += ch
216
+ }
217
+ rendered = fwOut
218
+ }
219
+ if (/^\s+$/.test(rendered)) {
220
+ if (text.length > 0) {
221
+ if (text[text.length - 1] !== ' ') {
222
+ text += ' '
223
+ map.push({ startNode: startSrc.node, startOffset: startSrc.offset, endNode: endSrc.node, endOffset: endSrc.offset + 1 })
224
+ } else {
225
+ const prev = map[map.length - 1]
226
+ prev.endNode = endSrc.node
227
+ prev.endOffset = endSrc.offset + 1
228
+ }
229
+ }
230
+ } else {
231
+ for (let u = 0; u < rendered.length; u++) {
232
+ text += rendered[u]
233
+ map.push({ startNode: startSrc.node, startOffset: startSrc.offset, endNode: endSrc.node, endOffset: endSrc.offset + 1 })
234
+ }
235
+ }
236
+ }
237
+ }
238
+ function pushSeparator() {
239
+ flushRun()
240
+ if (lastNode && text.length > 0 && text[text.length - 1] !== ' ') {
241
+ text += ' '
242
+ map.push({ startNode: lastNode, startOffset: lastOffset, endNode: lastNode, endOffset: lastOffset })
243
+ }
244
+ }
245
+ function walk(node: Node) {
246
+ if (node.nodeType === Node.TEXT_NODE) {
247
+ const textNode = node as Text
248
+ const data = textNode.data
249
+ if (!data) return
250
+ const parent = textNode.parentElement
251
+ if (!parent || !isElementVisible(parent)) return
252
+ const style = parseTextTransform(getComputedStyle(parent).textTransform, parent)
253
+ currentRunRaw += data
254
+ for (let i = 0; i < data.length; i++) {
255
+ currentRunMap.push({ node: textNode, offset: i, style })
256
+ }
257
+ lastNode = textNode
258
+ lastOffset = data.length
259
+ return
260
+ }
261
+ if (node.nodeType !== Node.ELEMENT_NODE) return
262
+ const el = node as Element
263
+ if (el.tagName === 'SCRIPT' || el.tagName === 'STYLE' || el.tagName === 'NOSCRIPT' || el.tagName === 'TEMPLATE') return
264
+ if (!isElementVisible(el)) return
265
+ if (el.tagName === 'BR') { pushSeparator(); return }
266
+ const style = getComputedStyle(el)
267
+ const display = style.display
268
+ const breaksFlow = !INLINE_FLOW_DISPLAY.has(display) && !ATOMIC_INLINE_DISPLAY.has(display)
269
+ const atomicPadLeft = ATOMIC_INLINE_DISPLAY.has(display) &&
270
+ (parseFloat(style.marginLeft) + parseFloat(style.borderLeftWidth) + parseFloat(style.paddingLeft) > 0.5)
271
+ const atomicPadRight = ATOMIC_INLINE_DISPLAY.has(display) &&
272
+ (parseFloat(style.marginRight) + parseFloat(style.borderRightWidth) + parseFloat(style.paddingRight) > 0.5)
273
+ if (breaksFlow || atomicPadLeft) pushSeparator()
274
+ for (let child = el.firstChild; child; child = child.nextSibling) walk(child)
275
+ if (breaksFlow || atomicPadRight) pushSeparator()
276
+ }
277
+ walk(root)
278
+ flushRun()
279
+ if (text.endsWith(' ')) {
280
+ text = text.slice(0, -1)
281
+ map.pop()
282
+ }
283
+ return { text, map }
284
+ }
285
+ function measureTextTarget(target: ResolvedTextTarget, slide: HTMLElement, content: HTMLElement, contentRect: Rectangle): { fragments: Rectangle[]; elements: Element[] } {
286
+ if (target.container?.type === 'shape') throw new Error('PowerPoint shape targets are unsupported by Slidev')
287
+ let root: Element = slide
288
+ if (target.container?.type === 'element') {
289
+ const candidates = slide.querySelectorAll(target.container.selector)
290
+ if (candidates.length !== 1) throw new Error(`Selector ${JSON.stringify(target.container.selector)} matched ${candidates.length} elements in the active audience slide`)
291
+ root = candidates[0]
292
+ if (!isElementVisible(root)) throw new Error('Target container is hidden')
293
+ }
294
+ const needle = target.text.trim().replace(/\s+/g, ' ')
295
+ if (!needle) throw new Error('Text target cannot be empty')
296
+ const { text, map } = extractNormalizedText(root)
297
+ const matches: number[] = []
298
+ let pos = text.indexOf(needle)
299
+ while (pos >= 0) {
300
+ matches.push(pos)
301
+ pos = text.indexOf(needle, pos + 1)
302
+ }
303
+ if (matches.length === 0) throw new Error(`Text ${JSON.stringify(needle)} was not found in the active audience slide`)
304
+ if (target.occurrence === undefined && matches.length > 1) throw new Error(`Text ${JSON.stringify(needle)} matched ${matches.length} times; specify container or occurrence`)
305
+ const matchPos = matches[(target.occurrence ?? 1) - 1]
306
+ if (matchPos === undefined) throw new Error(`Text ${JSON.stringify(needle)} occurrence ${target.occurrence} does not exist (matched ${matches.length})`)
307
+ let startIdx = matchPos + (target.slice?.start ?? 0)
308
+ let endIdx = matchPos + (target.slice?.end ?? needle.length)
309
+ while (startIdx < endIdx && text[startIdx] === ' ') startIdx++
310
+ while (endIdx > startIdx && text[endIdx - 1] === ' ') endIdx--
311
+ if (startIdx >= endIdx || endIdx > map.length) throw new Error(`Text slice for ${JSON.stringify(needle)} is empty or out of bounds`)
312
+ type NodeRun = { node: Text; start: number; end: number; parent: Element }
313
+ const runs: NodeRun[] = []
314
+ const addNodeSlice = (node: Text, start: number, end: number) => {
315
+ if (start >= end) return
316
+ const parent = node.parentElement
317
+ if (!parent) throw new Error('Target text node is detached')
318
+ const prev = runs.at(-1)
319
+ if (prev && prev.node === node && end >= prev.end) {
320
+ prev.end = Math.max(prev.end, end)
321
+ } else {
322
+ runs.push({ node, start, end, parent })
323
+ }
324
+ }
325
+ for (let i = startIdx; i < endIdx; i++) {
326
+ const span = map[i]
327
+ if (span.startNode === span.endNode) {
328
+ addNodeSlice(span.startNode, span.startOffset, span.endOffset)
329
+ } else if (text[i] !== ' ') {
330
+ addNodeSlice(span.startNode, span.startOffset, span.startNode.data.length)
331
+ addNodeSlice(span.endNode, 0, span.endOffset)
332
+ }
333
+ }
334
+ const rawRects: Rectangle[] = []
335
+ const elements: Element[] = []
336
+ const seen = new Set<Element>()
337
+ const subRange = document.createRange()
338
+ for (const run of runs) {
339
+ subRange.setStart(run.node, run.start)
340
+ subRange.setEnd(run.node, run.end)
341
+ const rects = [...subRange.getClientRects()].filter(r => r.width > 0.05 && r.height > 0.05).map(rectangle)
342
+ if (rects.length > 0 && !seen.has(run.parent)) {
343
+ seen.add(run.parent)
344
+ elements.push(run.parent)
345
+ }
346
+ for (const rect of rects) {
347
+ checkTextAncestors(run.parent, rect, content, contentRect)
348
+ rawRects.push(rect)
349
+ }
350
+ }
351
+ if (rawRects.length === 0) throw new Error(`Text ${JSON.stringify(needle)} has empty rendered bounds`)
352
+ const mergedLines: Rectangle[] = []
353
+ for (const rect of rawRects) {
354
+ const prev = mergedLines.at(-1)
355
+ if (prev) {
356
+ const overlapY = Math.min(prev.y + prev.height, rect.y + rect.height) - Math.max(prev.y, rect.y)
357
+ if (overlapY > 0.5 * Math.min(prev.height, rect.height)) {
358
+ const left = Math.min(prev.x, rect.x)
359
+ const top = Math.min(prev.y, rect.y)
360
+ const right = Math.max(prev.x + prev.width, rect.x + rect.width)
361
+ const bottom = Math.max(prev.y + prev.height, rect.y + rect.height)
362
+ prev.x = left; prev.y = top; prev.width = right - left; prev.height = bottom - top
363
+ continue
364
+ }
365
+ }
366
+ mergedLines.push({ ...rect })
367
+ }
368
+ const fragments = mergedLines.map(rect => ({
369
+ x: (rect.x - contentRect.x) / contentRect.width,
370
+ y: (rect.y - contentRect.y) / contentRect.height,
371
+ width: rect.width / contentRect.width,
372
+ height: rect.height / contentRect.height,
373
+ }))
374
+ return { fragments, elements }
375
+ }
49
376
  function measure(request: GeometryRequest, position: PlaybackPosition): Measurement {
50
377
  if (position.slide !== request.slide || position.visit !== request.visit || position.click !== request.click) throw new Error('Slide visit or reveal state changed')
51
378
  const { content, slide } = audience(position.index)
@@ -78,6 +405,12 @@ function measure(request: GeometryRequest, position: PlaybackPosition): Measurem
78
405
  }
79
406
  case 'shape':
80
407
  throw new Error('PowerPoint shape targets are unsupported by Slidev')
408
+ case 'text': {
409
+ const measured = measureTextTarget(request.target, slide, content, contentRect)
410
+ fragments = measured.fragments
411
+ elements = measured.elements
412
+ break
413
+ }
81
414
  }
82
415
  return { fragments, content: contentRect, slide: rectangle(slide.getBoundingClientRect()), viewport: { width: innerWidth, height: innerHeight }, elements }
83
416
  }