返回 slidev
NoteDisplay.vue
根目录 / packages / client / internals / NoteDisplay.vue
1 <script setup lang="ts">
2 import type { ClicksContext } from '@slidev/types'
3 import { computed, nextTick, onMounted, ref, watch, watchEffect } from 'vue'
4 import { CLICKS_MAX } from '../constants'
5
6 const props = withDefaults(
7 defineProps<{
8 class?: string | string[]
9 noteHtml?: string
10 note?: string
11 highlight?: boolean
12 placeholder?: string
13 clicksContext?: ClicksContext
14 autoScroll?: boolean
15 }>(),
16 {
17 highlight: true,
18 },
19 )
20
21 const emit = defineEmits<{
22 (type: 'markerDblclick', e: MouseEvent, clicks: number): void
23 (type: 'markerClick', e: MouseEvent, clicks: number): void
24 }>()
25
26 const CLASS_FADE = 'slidev-note-fade'
27 const CLASS_MARKER = 'slidev-note-click-mark'
28
29 const withClicks = computed(() => props.clicksContext != null && props.noteHtml?.includes(CLASS_MARKER))
30 const noteDisplay = ref<HTMLElement | null>(null)
31
32 function processNote() {
33 if (!noteDisplay.value || !withClicks.value)
34 return
35
36 const markers = Array.from(noteDisplay.value.querySelectorAll(`.${CLASS_MARKER}`)) as HTMLElement[]
37
38 const markersMap = new Map<HTMLElement, number>()
39 const parentsMap = new Map<HTMLElement, [divider: HTMLElement | null, dividerClicks: number][]>()
40 let lastClicks = 0
41 for (const marker of markers) {
42 const clicks = Number(marker.dataset!.clicks)
43 markersMap.set(marker, clicks)
44
45 // Set parent clicks map
46 let n = marker
47 let p = marker.parentElement
48 while (p && n !== noteDisplay.value) {
49 if (!parentsMap.has(p)) {
50 parentsMap.set(p, [
51 [null, lastClicks],
52 ])
53 }
54 parentsMap.get(p)!.push([n, clicks])
55 n = p
56 p = p.parentElement
57 }
58
59 lastClicks = clicks
60 }
61
62 const siblingsMap = new Map<HTMLElement, number>()
63 for (const [parent, dividers] of parentsMap) {
64 let hasPrefix = false
65 let dividerIdx = 0
66 for (const sibling of Array.from(parent.childNodes)) {
67 let skip = false
68 while (sibling === dividers[dividerIdx + 1]?.[0]) {
69 skip = true
70 dividerIdx++
71 }
72 if (skip)
73 continue
74
75 // Convert sibling text nodes to spans
76 let siblingEl = sibling as HTMLElement
77 if (sibling.nodeType === 3 /* text node */) {
78 if (!sibling.textContent?.trim())
79 continue
80 siblingEl = document.createElement('span')
81 siblingEl.textContent = sibling.textContent
82 parent.insertBefore(siblingEl, sibling)
83 sibling.remove()
84 }
85
86 hasPrefix ||= dividerIdx === 0
87 siblingsMap.set(siblingEl, dividers[dividerIdx][1])
88 }
89 if (!hasPrefix)
90 dividers[0][1] = -1
91 }
92
93 // Apply
94 return (current: number) => {
95 const enabled = props.highlight
96 for (const [parent, clicks] of parentsMap)
97 parent.classList.toggle(CLASS_FADE, enabled && !clicks.some(([_, c]) => c === current))
98 for (const [parent, clicks] of siblingsMap)
99 parent.classList.toggle(CLASS_FADE, enabled && clicks !== current)
100 for (const [marker, clicks] of markersMap) {
101 marker.classList.remove(CLASS_FADE)
102 marker.classList.toggle(`${CLASS_MARKER}-past`, enabled && clicks < current)
103 marker.classList.toggle(`${CLASS_MARKER}-active`, enabled && clicks === current)
104 marker.classList.toggle(`${CLASS_MARKER}-next`, enabled && clicks === current + 1)
105 marker.classList.toggle(`${CLASS_MARKER}-future`, enabled && clicks > current + 1)
106 marker.ondblclick = (e) => {
107 if (!enabled)
108 return
109 emit('markerDblclick', e, clicks)
110 if (e.defaultPrevented)
111 return
112 props.clicksContext!.current = clicks
113 e.stopPropagation()
114 e.stopImmediatePropagation()
115 }
116 marker.onclick = (e) => {
117 if (enabled) {
118 emit('markerClick', e, clicks)
119 }
120 }
121
122 if (enabled && props.autoScroll && clicks === current)
123 marker.scrollIntoView({ block: 'center', behavior: 'smooth' })
124 }
125 }
126 }
127
128 const applyHighlight = ref<ReturnType<typeof processNote>>()
129
130 watch(
131 () => [props.noteHtml, props.highlight],
132 () => {
133 nextTick(() => {
134 applyHighlight.value = processNote()
135 })
136 },
137 { immediate: true },
138 )
139
140 onMounted(() => {
141 processNote()
142 })
143
144 watchEffect(() => {
145 const current = props.clicksContext?.current ?? CLICKS_MAX
146 applyHighlight.value?.(current)
147 })
148 </script>
149
150 <template>
151 <div
152 v-if="noteHtml"
153 ref="noteDisplay"
154 class="prose dark:prose-invert overflow-auto outline-none slidev-note"
155 :class="[props.class, withClicks ? 'slidev-note-with-clicks' : '']"
156 v-html="noteHtml"
157 />
158 <div
159 v-else-if="note"
160 class="prose dark:prose-invert overflow-auto outline-none slidev-note"
161 :class="props.class"
162 >
163 <p v-text="note" />
164 </div>
165 <div
166 v-else
167 class="prose dark:prose-invert overflow-auto outline-none opacity-50 italic select-none slidev-note"
168 :class="props.class"
169 >
170 <p v-text="props.placeholder || 'No notes.'" />
171 </div>
172 </template>
173
174 <style>
175 .slidev-note :first-child {
176 margin-top: 0;
177 }
178
179 .slidev-note {
180 overflow-wrap: anywhere;
181 word-break: break-word;
182 }
183 </style>
184
184 lines Plain Text