返回 slidev
v-mark.ts
根目录 / packages / client / modules / v-mark.ts
1 import type { RoughAnnotationConfig } from '@slidev/rough-notation'
2 import type { RawAtValue } from '@slidev/types'
3 import type { App } from 'vue'
4 import { annotate } from '@slidev/rough-notation'
5 import { computed, nextTick, watch, watchEffect } from 'vue'
6 import { useNav } from '../composables/useNav'
7 import { injectionCurrentPage } from '../constants'
8 import { directiveInject } from '../utils'
9 import { resolveClick } from './v-click'
10
11 export interface RoughDirectiveOptions extends Partial<RoughAnnotationConfig> {
12 at: RawAtValue
13 }
14
15 export type RoughDirectiveValue = RawAtValue | RoughDirectiveOptions
16
17 function addClass(options: RoughDirectiveOptions, cls: string) {
18 options.class = [options.class, cls].filter(Boolean).join(' ')
19 return options
20 }
21
22 // Definitions of supported modifiers
23 const vMarkModifiers: Record<string, (options: RoughDirectiveOptions, value?: any) => RoughDirectiveOptions> = {
24 // Types
25 'box': options => Object.assign(options, { type: 'box' }),
26 'circle': options => Object.assign(options, { type: 'circle' }),
27 'underline': options => Object.assign(options, { type: 'underline' }),
28 'highlight': options => Object.assign(options, { type: 'highlight' }),
29 'strike-through': options => Object.assign(options, { type: 'strike-through' }),
30 'crossed-off': options => Object.assign(options, { type: 'crossed-off' }),
31 'bracket': options => Object.assign(options, { type: 'bracket' }),
32
33 // Type Aliases
34 'strike': options => Object.assign(options, { type: 'strike-through' }),
35 'cross': options => Object.assign(options, { type: 'crossed-off' }),
36 'crossed': options => Object.assign(options, { type: 'crossed-off' }),
37 'linethrough': options => Object.assign(options, { type: 'strike-through' }),
38 'line-through': options => Object.assign(options, { type: 'strike-through' }),
39
40 // Colors
41 // @unocss-include
42 'black': options => addClass(options, 'text-black'),
43 'blue': options => addClass(options, 'text-blue'),
44 'cyan': options => addClass(options, 'text-cyan'),
45 'gray': options => addClass(options, 'text-gray'),
46 'green': options => addClass(options, 'text-green'),
47 'indigo': options => addClass(options, 'text-indigo'),
48 'lime': options => addClass(options, 'text-lime'),
49 'orange': options => addClass(options, 'text-orange'),
50 'pink': options => addClass(options, 'text-pink'),
51 'purple': options => addClass(options, 'text-purple'),
52 'red': options => addClass(options, 'text-red'),
53 'teal': options => addClass(options, 'text-teal'),
54 'white': options => addClass(options, 'text-white'),
55 'yellow': options => addClass(options, 'text-yellow'),
56 }
57
58 const vMarkModifiersDynamic: [RegExp, (match: RegExpMatchArray, options: RoughDirectiveOptions, value?: any) => RoughDirectiveOptions][] = [
59 // Support setting delay like `v-mark.delay300="1"`
60 [
61 /^delay-?(\d+)?$/,
62 (match, options, value) => {
63 const ms = (match[1]
64 ? Number.parseInt(match[1])
65 : value) || 300
66 options.delay = ms
67 return options
68 },
69 ],
70 // Support setting opacity like `v-mark.op50="1"`
71 [
72 /^(?:op|opacity)-?(\d+)?$/,
73 (match, options, value) => {
74 const opacity = (match[1]
75 ? Number.parseInt(match[1])
76 : value) || 100
77 options.opacity = opacity / 100
78 return options
79 },
80 ],
81 ]
82
83 /**
84 * This supports v-mark directive to add notations to elements, powered by `rough-notation`.
85 */
86 export function createVMarkDirective() {
87 return {
88 install(app: App) {
89 app.directive<HTMLElement, RoughDirectiveValue>('mark', {
90 // @ts-expect-error extra prop
91 name: 'v-mark',
92
93 mounted: (el, binding) => {
94 const { currentPage, isPrintMode } = useNav()
95 const thisPage = directiveInject(binding, injectionCurrentPage)
96
97 const options = computed(() => {
98 const bindingOptions = (typeof binding.value === 'object' && !Array.isArray(binding.value))
99 ? { ...binding.value }
100 : { at: binding.value }
101
102 let modifierOptions: RoughDirectiveOptions = { at: bindingOptions.at }
103 const unknownModifiers = Object.entries(binding.modifiers)
104 .filter(([k, v]) => {
105 if (vMarkModifiers[k]) {
106 modifierOptions = vMarkModifiers[k](modifierOptions, v)
107 return false
108 }
109 for (const [re, fn] of vMarkModifiersDynamic) {
110 const match = k.match(re)
111 if (match) {
112 modifierOptions = fn(match, modifierOptions, v)
113 return false
114 }
115 }
116 return true
117 })
118
119 if (unknownModifiers.length)
120 console.warn('[Slidev] Invalid modifiers for v-mark:', unknownModifiers)
121
122 const options = {
123 ...modifierOptions,
124 ...bindingOptions,
125 }
126 options.type ||= 'underline'
127
128 if (isPrintMode.value)
129 options.animationDuration = 1 /* millisecond */
130
131 return options
132 })
133
134 const annotation = annotate(el, options.value as RoughAnnotationConfig)
135
136 // Preloaded slides are hidden, so marks shown there are drawn with zero size.
137 // Redraw them once the slide becomes visible, after `v-show` has been applied.
138 // @ts-expect-error extra prop
139 el.redrawStopHandle = watch(
140 () => thisPage?.value === currentPage.value,
141 async (isCurrent) => {
142 if (!isCurrent)
143 return
144 await nextTick()
145 if (annotation.isShowing())
146 annotation.show()
147 },
148 )
149
150 const resolvedClick = resolveClick(el, binding, options.value.at)
151 if (!resolvedClick) {
152 // No click animation, just show the mark
153 annotation.show()
154 return
155 }
156
157 // @ts-expect-error extra prop
158 el.watchStopHandle = watchEffect(() => {
159 let shouldShow: boolean | undefined
160
161 if (options.value.class)
162 annotation.class = options.value.class
163 if (options.value.color)
164 annotation.color = options.value.color
165
166 const at = options.value.at
167
168 if (at === true)
169 shouldShow = true
170 else if (at === false)
171 shouldShow = false
172 else
173 shouldShow = resolvedClick.isActive.value
174
175 if (shouldShow == null)
176 return
177
178 if (shouldShow)
179 annotation.show()
180 else
181 annotation.hide()
182 })
183 },
184
185 unmounted: (el) => {
186 // @ts-expect-error extra prop
187 el.watchStopHandle?.()
188 // @ts-expect-error extra prop
189 el.redrawStopHandle?.()
190 },
191 })
192 },
193 }
194 }
195
195 lines TYPESCRIPT