返回 slidev
utils.ts
根目录 / packages / client / logic / utils.ts
1 import { parseRangeString } from '@slidev/parser/core'
2
3 export function makeId(length = 5) {
4 const result = []
5 const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
6 const charactersLength = characters.length
7 for (let i = 0; i < length; i++)
8 result.push(characters.charAt(Math.floor(Math.random() * charactersLength)))
9 return result.join('')
10 }
11
12 export function updateCodeHighlightRange(
13 rangeStr: string,
14 linesCount: number,
15 startLine: number,
16 getTokenOfLine: (line: number) => Element[],
17 ) {
18 const highlights: number[] = parseRangeString(linesCount + startLine - 1, rangeStr)
19 for (let line = 0; line < linesCount; line++) {
20 const tokens = getTokenOfLine(line)
21 const isHighlighted = highlights.includes(line + startLine)
22 for (const token of tokens) {
23 // token.classList.toggle(CLASS_VCLICK_TARGET, true)
24 token.classList.toggle('slidev-code-highlighted', isHighlighted)
25 token.classList.toggle('slidev-code-dishonored', !isHighlighted)
26
27 // for backward compatibility
28 token.classList.toggle('highlighted', isHighlighted)
29 token.classList.toggle('dishonored', !isHighlighted)
30 }
31 }
32 }
33
34 /**
35 * Google Slides' "Present" keys: Cmd+Enter (macOS) and Ctrl+F5 (Windows/Linux).
36 * Checks the modifier flag on the event itself, because clickers (e.g. Logitech Spotlight)
37 * send it without a separate modifier keydown, which `useMagicKeys` combos miss.
38 */
39 export function isPresentShortcut(e: Pick<KeyboardEvent, 'key' | 'metaKey' | 'ctrlKey'>) {
40 return (e.key === 'Enter' && e.metaKey) || (e.key === 'F5' && e.ctrlKey)
41 }
42
42 lines TYPESCRIPT