From 920c66abf7e9c86ff4fef91a741f39e4fdb38666 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Sun, 25 Feb 2024 18:52:15 +0100 Subject: [PATCH] chore: improve `[click]` usage to make it easier to understand --- demo/starter/slides.md | 10 +++--- packages/client/internals/NoteDisplay.vue | 42 ++++++++++++++++++++--- packages/client/styles/index.css | 6 ++-- 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/demo/starter/slides.md b/demo/starter/slides.md index ddff683fb8..d0ab0f694a 100644 --- a/demo/starter/slides.md +++ b/demo/starter/slides.md @@ -167,15 +167,13 @@ doubled.value = 2 --- diff --git a/packages/client/internals/NoteDisplay.vue b/packages/client/internals/NoteDisplay.vue index 1e85f12ace..a1cc0c18f1 100644 --- a/packages/client/internals/NoteDisplay.vue +++ b/packages/client/internals/NoteDisplay.vue @@ -19,14 +19,40 @@ function highlightNote() { if (!noteDisplay.value || !withClicks.value || props.clicks == null) return - const children = Array.from(noteDisplay.value.querySelectorAll('*')) - const disabled = +props.clicks < 0 || +props.clicks >= CLICKS_MAX if (disabled) { - children.forEach(el => el.classList.remove('slidev-note-fade')) + Array.from(noteDisplay.value.querySelectorAll('*')) + .forEach(el => el.classList.remove('slidev-note-fade')) return } + const nodeToIgnores = new Set() + function ignoreParent(node: Element) { + if (!node || node === noteDisplay.value) + return + nodeToIgnores.add(node) + if (node.parentElement) + ignoreParent(node.parentElement) + } + + const markers = Array.from(noteDisplay.value.querySelectorAll('.slidev-note-click-mark')) + // Convert all sibling text nodes to spans, so we attach classes to them + for (const marker of markers) { + const parent = marker.parentElement! + // Ignore the parents of the marker, so the class only applies to the children + ignoreParent(parent) + Array.from(parent!.childNodes) + .forEach((node) => { + if (node.nodeType === 3) { // text node + const span = document.createElement('span') + span.textContent = node.textContent + parent.insertBefore(span, node) + node.remove() + } + }) + } + const children = Array.from(noteDisplay.value.querySelectorAll('*')) + let count = 0 const groups = new Map() @@ -40,8 +66,14 @@ function highlightNote() { count = Number((child as HTMLElement).dataset.clicks) || (count + 1) } - for (const [count, els] of groups) - els.forEach(el => el.classList.toggle('slidev-note-fade', +count !== +props.clicks!)) + for (const [count, els] of groups) { + els.forEach(el => el.classList.toggle( + 'slidev-note-fade', + nodeToIgnores.has(el) + ? false + : +count !== +props.clicks!, + )) + } } watch( diff --git a/packages/client/styles/index.css b/packages/client/styles/index.css index 4f6c52bc05..5ec410b1cb 100644 --- a/packages/client/styles/index.css +++ b/packages/client/styles/index.css @@ -70,9 +70,9 @@ html { } .slidev-note-click-mark { - font-size: 0.8em; - --uno: text-violet bg-violet/10 mx1 px1 font-mono rounded flex flex-inline - items-center align-middle; + font-size: 0.7em; + display: inline-flex; + --uno: text-violet bg-violet/10 px1 font-mono rounded items-center; } .slidev-note-click-mark::before {