slidev-theme-kotlin 0.13.2 → 0.14.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 (40) hide show
  1. package/CHANGELOG.md +38 -1
  2. package/README.md +396 -633
  3. package/assets/gutter/suspendCall@14x14.svg +6 -0
  4. package/assets/gutter/suspendCall@14x14_dark.svg +6 -0
  5. package/bin/present.mjs +296 -0
  6. package/components/DrawnAnnotation.vue +57 -9
  7. package/components/DrawnAnnotationEditorToolbar.vue +1 -1
  8. package/components/InlineCompilerError.vue +46 -15
  9. package/components/InlineValue.vue +3 -3
  10. package/components/MagicMoveBetween.vue +6 -4
  11. package/components/README.md +60 -2
  12. package/components/SmartCast.vue +3 -3
  13. package/components/SuspendGutter.vue +168 -0
  14. package/components/TypeHint.vue +3 -3
  15. package/components/Warning.vue +3 -3
  16. package/components/carried-layer.ts +29 -0
  17. package/components/code-highlight.ts +119 -5
  18. package/components/transition-ink.ts +27 -6
  19. package/docs/annotation-editor.png +0 -0
  20. package/docs/code-marks.md +76 -0
  21. package/docs/phone-remote.png +0 -0
  22. package/docs/presenting.md +122 -0
  23. package/docs/publishing.md +111 -0
  24. package/docs/snippets.md +84 -0
  25. package/global-top.vue +3 -8
  26. package/package.json +11 -4
  27. package/remote/RemoteControl.vue +278 -0
  28. package/remote/connection.ts +19 -0
  29. package/setup/code-imports.ts +3 -2
  30. package/setup/code-marks.ts +562 -0
  31. package/setup/kotlin-snippets.ts +4 -2
  32. package/setup/magic-move-between.ts +10 -2
  33. package/setup/main.ts +5 -1
  34. package/setup/remote-sync.ts +226 -0
  35. package/setup/routes.ts +21 -0
  36. package/setup/static-content.ts +39 -5
  37. package/setup/transformers.ts +16 -1
  38. package/styles/layout.css +17 -0
  39. package/assets/edit-3-svgrepo-com.svg +0 -5
  40. package/components/DrawnAnnotationEditorLauncher.vue +0 -59
@@ -19,14 +19,14 @@ const highlightProps = reactive({
19
19
  ...toRefs(props),
20
20
  message: computed(() => props.value),
21
21
  })
22
- const { message, messageShown, messageStyle } = useCodeHighlight(highlightProps, container, { component: 'InlineValue' })
22
+ const { message, messageShown, messageStyle, messageCarried } = useCodeHighlight(highlightProps, container, { component: 'InlineValue' })
23
23
  </script>
24
24
 
25
25
  <template>
26
26
  <div ref="container" class="inline-value-host">
27
27
  <slot />
28
- <Transition name="inline-value">
29
- <span v-if="messageShown" class="inline-value" :style="messageStyle">{{ message }}</span>
28
+ <Transition :name="messageCarried ? 'carried' : 'inline-value'">
29
+ <span v-if="messageShown" class="inline-value" :class="{ 'magic-move-carried': messageCarried }" :style="messageStyle">{{ message }}</span>
30
30
  </Transition>
31
31
  </div>
32
32
  </template>
@@ -311,8 +311,10 @@ function copyCode() {
311
311
  animation. Keep the root of a morphing slide opaque and fade the content
312
312
  around the window instead: every element that neither is nor contains a
313
313
  morphing window, at the topmost level where that holds, so nested opacities
314
- never compound. The root keeps Slidev's own `transition` declaration, which
315
- is where Vue reads the transition's duration from. These rules target the
314
+ never compound. An annotation carried over from the neighbouring slide
315
+ (`.magic-move-carried`) is on screen already and stays out of the fade the
316
+ same way. The root keeps Slidev's own `transition` declaration, which is
317
+ where Vue reads the transition's duration from. These rules target the
316
318
  slide root, an ancestor of this component, so they cannot be scoped. */
317
319
  .slidev-page.fade-enter-from:has(.magic-move-between--arriving),
318
320
  .slidev-page.fade-leave-to:has(.magic-move-between--departing) {
@@ -320,14 +322,14 @@ function copyCode() {
320
322
  }
321
323
 
322
324
  :is(.slidev-page.fade-enter-active, .slidev-page.fade-enter-active *):has(.magic-move-between--arriving)
323
- > :not(.magic-move-between--arriving, :has(.magic-move-between--arriving)),
325
+ > :not(.magic-move-between--arriving, :has(.magic-move-between--arriving), .magic-move-carried),
324
326
  :is(.slidev-page.fade-leave-active, .slidev-page.fade-leave-active *):has(.magic-move-between--departing)
325
327
  > :not(.magic-move-between--departing, :has(.magic-move-between--departing)) {
326
328
  transition: opacity var(--slidev-transition-duration) ease;
327
329
  }
328
330
 
329
331
  :is(.slidev-page.fade-enter-from, .slidev-page.fade-enter-from *):has(.magic-move-between--arriving)
330
- > :not(.magic-move-between--arriving, :has(.magic-move-between--arriving)),
332
+ > :not(.magic-move-between--arriving, :has(.magic-move-between--arriving), .magic-move-carried),
331
333
  :is(.slidev-page.fade-leave-to, .slidev-page.fade-leave-to *):has(.magic-move-between--departing)
332
334
  > :not(.magic-move-between--departing, :has(.magic-move-between--departing)) {
333
335
  opacity: 0;
@@ -299,8 +299,8 @@ No annotation ID or generated stylesheet is used. Enable
299
299
  serving it injects a transient source locator and the editor rewrites only this
300
300
  opening tag's `:geometry` binding and its `:connect` binding. The
301
301
  locator is not part of authored Markdown
302
- or production output. With the writer configured, press **Alt+Shift+A** (or use
303
- the global **Edit annotations** toolbar) in a development deck.
302
+ or production output. With the writer configured, press **Alt+Shift+A** in a
303
+ development deck.
304
304
 
305
305
  Editing is a deliberate frozen session. Entering edit mode locks the deck on
306
306
  the current slide and click state: Slidev's keyboard shortcuts are suspended
@@ -438,6 +438,12 @@ browser console, once, prefixed `[DrawnAnnotation]`. The warnings wait until
438
438
  the annotation's click has been reached and the slide has stopped moving, so a
439
439
  Magic Move step that legitimately does not contain the text never causes one.
440
440
 
441
+ In `slidev dev` the same problem also puts a red chip in the top-left corner
442
+ of the slide; hover it for the full message. `InlineCompilerError`,
443
+ `SmartCast`, `Warning`, `TypeHint`, and `InlineValue` report a target they
444
+ cannot find the same way. The chip disappears when the target turns up, and
445
+ builds, exports, the presenter view, and print mode never show it.
446
+
441
447
  - **Text … was not found in the slot** — `text` has to match the rendered text
442
448
  exactly, spaces included. Inside a Magic Move block it also has to be part of
443
449
  the step the annotation is drawn on.
@@ -460,6 +466,14 @@ error and anchors its message beside the relevant code. Wrap exactly one fenced
460
466
  code block; the component supports ordinary Shiki fences and `magic-move`
461
467
  blocks, and remains hidden when its target is not part of the current step.
462
468
 
469
+ The wrapper can also be written as a code mark inside the fence,
470
+ `// [!error userName Unresolved reference: userName]` on the line itself; the
471
+ same goes for `SmartCast` (`[!smartcast …]`), `Warning` (`[!warning …]`),
472
+ `TypeHint` (`[!this …]`, `[!context …]`, `[!type …]`) and `InlineValue`
473
+ (`[!value …]`). The theme turns the comment into the wrapper below with the
474
+ line computed, so a mark follows its line through a Magic Move chain. See
475
+ "Code marks" in the top-level README.
476
+
463
477
  ````md
464
478
  <InlineCompilerError text="userName" message="Unresolved reference: userName" :on="1">
465
479
 
@@ -793,3 +807,47 @@ Exactly as for `SmartCast`: `at`, `until`, and `on`.
793
807
 
794
808
  The value uses the code font. Read where they are used, so any scope can
795
809
  redefine them — a single slide's class as well as `:root`.
810
+
811
+ # `SuspendGutter`
812
+
813
+ IntelliJ marks a call that may suspend with a small icon in the editor gutter.
814
+ `SuspendGutter` draws that icon beside the listed lines of one fence:
815
+
816
+ ````md
817
+ <SuspendGutter :lines="[2, 3]">
818
+
819
+ ```kotlin
820
+ suspend fun program() {
821
+ delay(100)
822
+ yield()
823
+ }
824
+ ```
825
+
826
+ </SuspendGutter>
827
+ ````
828
+
829
+ The lines are one-based source lines of the fence, blank lines included and
830
+ regardless of a displayed `startLine`; duplicates, blank, and missing lines are
831
+ ignored. The component does not analyse Kotlin: the author names the
832
+ suspension points worth a glance. From inside the fence the same slide reads
833
+ `delay(100) // [!suspend]`, and the theme collects the marks of a fence into
834
+ one wrapper.
835
+
836
+ The icon sits outside the code window's left border, so the window and its
837
+ padding stay where they are. It follows the current Magic Move step, hides
838
+ while tokens are moving, and swaps between the light and dark JetBrains
839
+ variants with the colour scheme.
840
+
841
+ ## Props
842
+
843
+ | prop | required | meaning |
844
+ | --- | --- | --- |
845
+ | `lines` | yes | one-based source lines of the fence to mark |
846
+ | `label` | no | accessible label and tooltip, `Suspension point` by default |
847
+
848
+ ## Styling
849
+
850
+ | CSS variable | default | meaning |
851
+ | --- | --- | --- |
852
+ | `--suspend-gutter-size` | `1em` of the code font | icon width and height |
853
+ | `--suspend-gutter-gap` | `0.35em` | space between the icon and the window border |
@@ -7,14 +7,14 @@ const props = defineProps<CodeHighlightProps>()
7
7
  const container = useTemplateRef<HTMLElement>('container')
8
8
 
9
9
  // Everything but the colours is shared with `Warning`; see code-highlight.ts.
10
- const { messageHtml, messageShown, messageStyle } = useCodeHighlight(props, container, { component: 'SmartCast', highlightClass: 'smart-cast-token' })
10
+ const { messageHtml, messageShown, messageStyle, messageCarried } = useCodeHighlight(props, container, { component: 'SmartCast', highlightClass: 'smart-cast-token' })
11
11
  </script>
12
12
 
13
13
  <template>
14
14
  <div ref="container" class="smart-cast-host">
15
15
  <slot />
16
- <Transition name="smart-cast-message">
17
- <span v-if="messageShown" class="smart-cast-message" :style="messageStyle"><i>●</i><span class="annotation-label-content" v-html="messageHtml" /></span>
16
+ <Transition :name="messageCarried ? 'carried' : 'smart-cast-message'">
17
+ <span v-if="messageShown" class="smart-cast-message" :class="{ 'magic-move-carried': messageCarried }" :style="messageStyle"><i>●</i><span class="annotation-label-content" v-html="messageHtml" /></span>
18
18
  </Transition>
19
19
  </div>
20
20
  </template>
@@ -0,0 +1,168 @@
1
+ <script setup lang="ts">
2
+ import { nextTick, onBeforeUnmount, onMounted, ref, useTemplateRef, watch } from 'vue'
3
+ import { useSlideContext } from '@slidev/client'
4
+ import { codeTextSegments, trimmedLineRange } from './code-text-match'
5
+ import suspendCall from '../assets/gutter/suspendCall@14x14.svg'
6
+ import suspendCallDark from '../assets/gutter/suspendCall@14x14_dark.svg'
7
+
8
+ const props = withDefaults(defineProps<{
9
+ /** One-based source lines, including blank lines; wrap one code fence. */
10
+ lines: number[]
11
+ label?: string
12
+ }>(), { label: 'Suspension point' })
13
+
14
+ const host = useTemplateRef<HTMLElement>('host')
15
+ const markers = ref<{ line: number, top: string, left: string, fontSize: string }[]>([])
16
+ const { $scale, $zoom, $clicks } = useSlideContext()
17
+ const moving = '.shiki-magic-move-enter-active, .shiki-magic-move-leave-active, .shiki-magic-move-move, .shiki-magic-move-container-resize, .shiki-magic-move-container-restyle'
18
+ const entering = '.shiki-magic-move-enter-from, .shiki-magic-move-enter-active'
19
+ // True while Magic Move is moving the code; see `sync`.
20
+ let wasMoving = false
21
+ let frame: number | undefined
22
+ let mutations: MutationObserver | undefined
23
+ let resize: ResizeObserver | undefined
24
+ let disposed = false
25
+
26
+ function sync() {
27
+ const root = host.value
28
+ if (!root)
29
+ return
30
+ const pre = root.querySelector<HTMLElement>('pre.slidev-code')
31
+ const magic = pre?.classList.contains('shiki-magic-move-container') ?? false
32
+ const code = magic ? pre : pre?.querySelector<HTMLElement>('code')
33
+ if (!pre || !code) {
34
+ markers.value = []
35
+ return
36
+ }
37
+ // A marker follows its line through a Magic Move step, measured again every
38
+ // frame: the tokens move by CSS transform, which no observer reports. Taking
39
+ // the markers away until the code has settled made every one of them blink,
40
+ // including those of lines that never moved.
41
+ wasMoving = !!root.querySelector(moving)
42
+ if (wasMoving)
43
+ schedule()
44
+ const scale = $scale.value * $zoom.value || 1
45
+ const rootBox = root.getBoundingClientRect()
46
+ const preBox = pre.getBoundingClientRect()
47
+ const style = getComputedStyle(pre)
48
+ const segments = codeTextSegments(code, magic)
49
+ // Anchor to the outside of the editor border, without changing code padding.
50
+ const left = `${(preBox.left - rootBox.left) / scale}px`
51
+ const next: typeof markers.value = []
52
+ for (const line of new Set(props.lines)) {
53
+ if (!Number.isInteger(line) || line < 1)
54
+ continue
55
+ const { range } = trimmedLineRange(segments, line)
56
+ const box = Array.from(range?.getClientRects() ?? []).find(rect => rect.width && rect.height)
57
+ // Blank and missing lines have no suspension point to annotate.
58
+ if (!range || !box)
59
+ continue
60
+ // A line Magic Move is still writing gets its marker once it is there.
61
+ if (wasMoving && range.startContainer.parentElement?.closest(entering))
62
+ continue
63
+ const center = box.top + box.height / 2
64
+ // Respect scrollable code windows as well as the editor's own bounds.
65
+ let visible = true
66
+ for (let parent: HTMLElement | null = pre; parent && parent !== root; parent = parent.parentElement) {
67
+ if (/(auto|scroll|hidden|clip)/.test(getComputedStyle(parent).overflowY)) {
68
+ const bounds = parent.getBoundingClientRect()
69
+ if (center < bounds.top || center > bounds.bottom)
70
+ visible = false
71
+ }
72
+ }
73
+ if (visible)
74
+ next.push({ line, left, top: `${(center - rootBox.top) / scale}px`, fontSize: style.fontSize })
75
+ }
76
+ markers.value = next
77
+ }
78
+
79
+ function schedule() {
80
+ if (!disposed)
81
+ frame ??= requestAnimationFrame(() => { frame = undefined; sync() })
82
+ }
83
+
84
+ watch(() => [props.lines, $scale.value, $zoom.value, $clicks.value], schedule, { deep: true, flush: 'post' })
85
+ onMounted(async () => {
86
+ await nextTick()
87
+ if (disposed || !host.value)
88
+ return
89
+ mutations = new MutationObserver((records) => {
90
+ // Only code mutations matter; updating our own overlay must not loop.
91
+ if (!records.some(record => {
92
+ const target = record.target instanceof Element ? record.target : record.target.parentElement
93
+ return !target?.closest('.suspend-gutter-markers')
94
+ }))
95
+ return
96
+ // The frame in which the code sets off is measured before it is painted:
97
+ // the markers are then where the lines start from, not where they end up.
98
+ if (!wasMoving && host.value?.querySelector(moving))
99
+ sync()
100
+ else
101
+ schedule()
102
+ })
103
+ mutations.observe(host.value, { subtree: true, childList: true, characterData: true, attributes: true, attributeFilter: ['class', 'style'] })
104
+ resize = new ResizeObserver(schedule)
105
+ resize.observe(host.value)
106
+ host.value.addEventListener('scroll', schedule, true)
107
+ document.fonts?.ready.then(schedule)
108
+ schedule()
109
+ })
110
+
111
+ onBeforeUnmount(() => {
112
+ disposed = true
113
+ mutations?.disconnect()
114
+ resize?.disconnect()
115
+ host.value?.removeEventListener('scroll', schedule, true)
116
+ if (frame !== undefined)
117
+ cancelAnimationFrame(frame)
118
+ })
119
+ </script>
120
+
121
+ <template>
122
+ <div ref="host" class="suspend-gutter">
123
+ <slot />
124
+ <div class="suspend-gutter-markers magic-move-carried">
125
+ <span
126
+ v-for="marker in markers"
127
+ :key="marker.line"
128
+ class="suspend-gutter-marker"
129
+ :data-line="marker.line"
130
+ :style="{ top: marker.top, left: marker.left, fontSize: marker.fontSize }"
131
+ role="img"
132
+ :aria-label="`${label} on line ${marker.line}`"
133
+ :title="`${label} on line ${marker.line}`"
134
+ >
135
+ <img class="suspend-gutter-light" :src="suspendCall" alt="" aria-hidden="true" />
136
+ <img class="suspend-gutter-dark" :src="suspendCallDark" alt="" aria-hidden="true" />
137
+ </span>
138
+ </div>
139
+ </div>
140
+ </template>
141
+
142
+ <style scoped>
143
+ .suspend-gutter { position: relative; }
144
+ .suspend-gutter-markers { pointer-events: none; }
145
+ /* The neighbouring slide of a cross-slide Magic Move chain is morphing this
146
+ window's code and draws the markers itself (they keep out of its fade, as
147
+ `.magic-move-carried`), so the ones left here would only ghost beneath. */
148
+ .suspend-gutter:has(.magic-move-between--departing) .suspend-gutter-markers { visibility: hidden; }
149
+ .suspend-gutter-marker {
150
+ position: absolute;
151
+ z-index: 1;
152
+ display: flex;
153
+ align-items: center;
154
+ justify-content: center;
155
+ width: var(--suspend-gutter-size, 1em);
156
+ height: var(--suspend-gutter-size, 1em);
157
+ margin-left: calc(-1 * var(--suspend-gutter-gap, 0.35em));
158
+ transform: translate(-100%, -50%);
159
+ pointer-events: auto;
160
+ }
161
+ .suspend-gutter-marker img {
162
+ width: 100%;
163
+ height: 100%;
164
+ }
165
+ .suspend-gutter-dark { display: none; }
166
+ :global(html.dark .suspend-gutter-light) { display: none; }
167
+ :global(html.dark .suspend-gutter-dark) { display: block; }
168
+ </style>
@@ -37,7 +37,7 @@ const highlightProps = reactive({
37
37
  text: computed(() => props.text ?? (props.type ? undefined : '{')),
38
38
  message: computed(() => props.type ? undefined : props.receiver ? `this: ${props.receiver}` : props.context ? `context(${props.context})` : undefined),
39
39
  })
40
- const { message, messageShown, messageStyle, inlaid } = useCodeHighlight(highlightProps, container, {
40
+ const { message, messageShown, messageStyle, messageCarried, inlaid } = useCodeHighlight(highlightProps, container, {
41
41
  component: 'TypeHint',
42
42
  highlightClass: props.type ? 'type-hint-inlay' : 'type-hint-lambda',
43
43
  inlay: () => props.type ? `: ${props.type}` : highlightProps.message,
@@ -47,8 +47,8 @@ const { message, messageShown, messageStyle, inlaid } = useCodeHighlight(highlig
47
47
  <template>
48
48
  <div ref="container" class="type-hint-host">
49
49
  <slot />
50
- <Transition name="type-hint">
51
- <span v-if="messageShown && !inlaid" class="type-hint" :style="messageStyle">{{ message }}</span>
50
+ <Transition :name="messageCarried ? 'carried' : 'type-hint'">
51
+ <span v-if="messageShown && !inlaid" class="type-hint" :class="{ 'magic-move-carried': messageCarried }" :style="messageStyle">{{ message }}</span>
52
52
  </Transition>
53
53
  </div>
54
54
  </template>
@@ -7,14 +7,14 @@ const props = defineProps<CodeHighlightProps>()
7
7
  const container = useTemplateRef<HTMLElement>('container')
8
8
 
9
9
  // Everything but the colours is shared with `SmartCast`; see code-highlight.ts.
10
- const { messageHtml, messageShown, messageStyle } = useCodeHighlight(props, container, { component: 'Warning', highlightClass: 'warning-token' })
10
+ const { messageHtml, messageShown, messageStyle, messageCarried } = useCodeHighlight(props, container, { component: 'Warning', highlightClass: 'warning-token' })
11
11
  </script>
12
12
 
13
13
  <template>
14
14
  <div ref="container" class="warning-host">
15
15
  <slot />
16
- <Transition name="warning-message">
17
- <span v-if="messageShown" class="warning-message" :style="messageStyle"><i>●</i><span class="annotation-label-content" v-html="messageHtml" /></span>
16
+ <Transition :name="messageCarried ? 'carried' : 'warning-message'">
17
+ <span v-if="messageShown" class="warning-message" :class="{ 'magic-move-carried': messageCarried }" :style="messageStyle"><i>●</i><span class="annotation-label-content" v-html="messageHtml" /></span>
18
18
  </Transition>
19
19
  </div>
20
20
  </template>
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Lifts an annotation that was carried over from the neighbouring slide of a
3
+ * cross-slide Magic Move chain above the code window while a view transition
4
+ * is running.
5
+ *
6
+ * With `transition: view-transition` the browser paints the page as a stack
7
+ * of snapshots: the root first, then every element that has a
8
+ * `view-transition-name`, and `MagicMoveBetween` names its code window so it
9
+ * morphs live instead of cross-fading with the page. Everything an annotation
10
+ * draws on top of the window (a squiggle, an inline message, hand-drawn ink)
11
+ * belongs to the root snapshot and would spend the transition hidden
12
+ * underneath the window. A carried annotation therefore becomes a snapshot of
13
+ * its own for as long as it is carried, which the browser stacks above the
14
+ * window; `styles/layout.css` shows it complete from the first frame.
15
+ *
16
+ * The name has to be unique in the document, or the browser skips the whole
17
+ * transition, so each caller gets its own.
18
+ */
19
+ import type { CSSProperties, Ref } from 'vue'
20
+ import { computed } from 'vue'
21
+
22
+ let layers = 0
23
+
24
+ export function useCarriedLayer(carried: Ref<boolean>) {
25
+ const name = `magic-move-carried-${++layers}`
26
+ return computed<CSSProperties | undefined>(() => carried.value
27
+ ? { 'view-transition-name': name, 'view-transition-class': 'magic-move-carried' } as CSSProperties
28
+ : undefined)
29
+ }
@@ -17,6 +17,9 @@ import { codeTextSegments, findTextInSegments, mergeVisualLines, trimmedLineRang
17
17
  import { normalizeAnnotationLineBreaks } from './annotation-line-breaks'
18
18
  import { renderAnnotationLabel } from './annotation-label-markdown'
19
19
  import { clearAnnotationAuthoringProblems, reportAnnotationAuthoringProblem } from './drawn-annotation/editor-store'
20
+ import { useCarriedLayer } from './carried-layer'
21
+ import { handOverTransitionInk, takeTransitionInk } from './transition-ink'
22
+ import type { TransitionInkBox } from './transition-ink'
20
23
 
21
24
  // Slidev's build renderer can leave `import.meta.env.DEV` true. Authoring
22
25
  // warnings belong to Vite's explicit serve mode only, the same boundary
@@ -72,8 +75,8 @@ export interface CodeHighlightOptions {
72
75
  }
73
76
 
74
77
  // Classes Shiki Magic Move keeps on its elements only while a step transition
75
- // is playing; the message waits for the tokens to settle instead of chasing
76
- // them frame by frame, since the highlight itself already travels with them.
78
+ // is playing. A message that is not on screen yet waits for the tokens to
79
+ // settle; one that is stays, and follows its line frame by frame.
77
80
  const MAGIC_MOVE_ANIMATING = [
78
81
  '.shiki-magic-move-enter-active',
79
82
  '.shiki-magic-move-leave-active',
@@ -204,7 +207,30 @@ export function useCodeHighlight(props: CodeHighlightProps, container: Ref<HTMLE
204
207
  return true
205
208
  return fromStart ? $clicks.value < info.end : info.isActive.value
206
209
  })
207
- const messageShown = computed(() => visible.value && hasTarget.value && !animating.value && !!message.value)
210
+ // A new message must not appear on top of code that is still moving. Once
211
+ // it is shown, however, it stays through later Magic Move steps: taking it
212
+ // away for every step makes the message of a line that never moved blink.
213
+ const revealed = ref(false)
214
+ // The message was on screen on the neighbouring Magic Move slide: it is
215
+ // shown complete from the first frame, without its entrance fade.
216
+ const carried = ref(false)
217
+ const messageShown = computed(() => revealed.value && !!message.value)
218
+ // Each slide of a cross-slide Magic Move chain has its own instance of this
219
+ // component. A message on screen when its slide is left is handed to the
220
+ // equivalent instance on the neighbouring slide (see transition-ink.ts).
221
+ const transitionInkIdentity = computed(() => JSON.stringify([component, props.line, text.value, props.occurrence, message.value]))
222
+ let lastTransitionBox: TransitionInkBox | undefined
223
+ // Set on arrival from a neighbouring slide, until the message is revealed.
224
+ let carryPending = false
225
+ // The window of a chain slide starts its move a frame after the slide is
226
+ // entered. Until it had the time to, only a carried message is shown: one
227
+ // that is new here waits for the move, like on any other step.
228
+ let arrivalHold: ReturnType<typeof setTimeout> | undefined
229
+ // Only the entrance is skipped: a message that goes fades out as usual.
230
+ watch(revealed, (isRevealed) => {
231
+ if (!isRevealed)
232
+ carried.value = false
233
+ }, { flush: 'sync' })
208
234
 
209
235
  // The wrapped code block renders in one of two shapes, discovered lazily
210
236
  // because Magic Move only renders after mount. The <pre>/<code> element
@@ -282,6 +308,14 @@ export function useCodeHighlight(props: CodeHighlightProps, container: Ref<HTMLE
282
308
  const targetElement = range.startContainer instanceof HTMLElement
283
309
  ? range.startContainer
284
310
  : range.startContainer.parentElement
311
+ lastTransitionBox = slideBox.width && slideBox.height
312
+ ? {
313
+ left: (first.left - slideBox.left) / slideBox.width,
314
+ top: (first.top - slideBox.top) / slideBox.height,
315
+ right: (messageRight - slideBox.left) / slideBox.width,
316
+ bottom: (first.bottom - slideBox.top) / slideBox.height,
317
+ }
318
+ : undefined
285
319
  messageStyle.value = {
286
320
  left: `${(messageRight - hostBox.left) / scale + 10}px`,
287
321
  top: `${(first.top - hostBox.top + (first.bottom - first.top) / 2) / scale}px`,
@@ -385,6 +419,7 @@ export function useCodeHighlight(props: CodeHighlightProps, container: Ref<HTMLE
385
419
  return
386
420
  if (!visible.value) {
387
421
  clear()
422
+ revealed.value = false
388
423
  return
389
424
  }
390
425
  const target = findTarget(host)
@@ -393,8 +428,46 @@ export function useCodeHighlight(props: CodeHighlightProps, container: Ref<HTMLE
393
428
  paint(target.range)
394
429
  // Magic Move ends a transition by removing its animation classes, which
395
430
  // the class observer below turns into one more sync: the settled edge.
431
+ const wasAnimating = animating.value
396
432
  animating.value = !!host.querySelector(MAGIC_MOVE_ANIMATING)
397
433
  placeMessage(host, target)
434
+ // Carried only for the move it arrived with; see carried-layer.ts.
435
+ if (wasAnimating && !animating.value)
436
+ carried.value = false
437
+ if (!message.value)
438
+ return
439
+ if (!matched.value) {
440
+ // Magic Move can leave a retained target briefly unmatched while it
441
+ // swaps its DOM; only a settled step without the target hides it.
442
+ if (!animating.value)
443
+ revealed.value = false
444
+ }
445
+ else if (!revealed.value) {
446
+ if (carryPending && lastTransitionBox && takeTransitionInk(transitionInkIdentity.value, slideContext.$page.value, lastTransitionBox))
447
+ carried.value = true
448
+ if (carried.value || (!animating.value && arrivalHold === undefined))
449
+ revealed.value = true
450
+ }
451
+ if (revealed.value)
452
+ carryPending = false
453
+ trackMessage()
454
+ }
455
+
456
+ // CSS transforms move the tokens without a mutation to observe, so a
457
+ // message on screen is placed again every frame of a Magic Move step.
458
+ let trackFrame: number | undefined
459
+ function trackMessage() {
460
+ if (!animating.value || !revealed.value || trackFrame !== undefined)
461
+ return
462
+ trackFrame = requestAnimationFrame(() => {
463
+ trackFrame = undefined
464
+ sync()
465
+ })
466
+ }
467
+
468
+ function isCrossSlideMagicMove() {
469
+ const host = container.value
470
+ return !!host && !!(host.closest('.slidev-layout') ?? host).querySelector('.magic-move-between')
398
471
  }
399
472
 
400
473
  // Observer bursts are coalesced into one sync per frame: a Magic Move step
@@ -411,7 +484,18 @@ export function useCodeHighlight(props: CodeHighlightProps, container: Ref<HTMLE
411
484
  // The code lines do not exist until Shiki Magic Move renders its first step,
412
485
  // and are replaced for every subsequent step; re-painting on every mutation
413
486
  // keeps the highlight on the current step's tokens instead of stale ones.
414
- useMutationObserver(container, scheduleSync, { childList: true, subtree: true, attributes: true, attributeFilter: ['class'] })
487
+ // Two changes cannot wait for the next frame, or the frame in between is
488
+ // painted without the highlight: Magic Move resetting the text of a token
489
+ // this component had split (which drops the painted wrapper), and a step
490
+ // starting to move, which is when a message on screen has to follow.
491
+ useMutationObserver(container, () => {
492
+ const host = container.value
493
+ if (host && visible.value
494
+ && (targets.value.some(el => !el.isConnected) || (!animating.value && host.querySelector(MAGIC_MOVE_ANIMATING))))
495
+ sync()
496
+ else
497
+ scheduleSync()
498
+ }, { childList: true, subtree: true, attributes: true, attributeFilter: ['class'] })
415
499
  // A resize moves the tokens, never the text: repaint is a no-op, the message moves.
416
500
  useResizeObserver(container, scheduleSync)
417
501
 
@@ -426,13 +510,41 @@ export function useCodeHighlight(props: CodeHighlightProps, container: Ref<HTMLE
426
510
  $clicksContext?.unregister(id)
427
511
  if (pendingSync !== undefined)
428
512
  cancelAnimationFrame(pendingSync)
513
+ if (trackFrame !== undefined)
514
+ cancelAnimationFrame(trackFrame)
429
515
  clearTimeout(missingTargetWarning)
516
+ clearTimeout(arrivalHold)
430
517
  if (isDevelopment)
431
518
  clearAnnotationAuthoringProblems(id)
432
519
  })
433
520
  watch([() => props.line, text, () => props.occurrence, message, () => inlay?.({ endsLine: matchEndsLine.value })], sync)
434
521
  watch(visible, sync)
435
522
  watch(isCurrentSlide, sync)
523
+ // The hand-off between the slides of a chain. Synchronous, so the slide
524
+ // being left has recorded its message before the `sync` above, which runs
525
+ // with the next render, lets the slide being entered look for it.
526
+ watch(isCurrentSlide, (current) => {
527
+ const host = container.value
528
+ if (current) {
529
+ carried.value = false
530
+ carryPending = isCrossSlideMagicMove()
531
+ host?.removeAttribute('data-magic-move-handed-over')
532
+ clearTimeout(arrivalHold)
533
+ arrivalHold = carryPending
534
+ ? setTimeout(() => {
535
+ arrivalHold = undefined
536
+ sync()
537
+ }, 100)
538
+ : undefined
539
+ }
540
+ else if (revealed.value && lastTransitionBox && isCrossSlideMagicMove()) {
541
+ // Once the neighbouring slide shows the message, the copy left here
542
+ // goes at once instead of fading out beneath it (see layout.css).
543
+ handOverTransitionInk(transitionInkIdentity.value, slideContext.$page.value, lastTransitionBox, () => {
544
+ host?.setAttribute('data-magic-move-handed-over', '')
545
+ })
546
+ }
547
+ }, { flush: 'sync' })
436
548
 
437
549
  // A target that is absent from the current code step has nothing to paint, so
438
550
  // the highlight stays off. Silent in production, but tell the slide author
@@ -462,5 +574,7 @@ export function useCodeHighlight(props: CodeHighlightProps, container: Ref<HTMLE
462
574
  }
463
575
  }, { immediate: true })
464
576
 
465
- return { message, messageHtml, messageShown, messageStyle, inlaid }
577
+ const carriedLayer = useCarriedLayer(carried)
578
+ const placedMessageStyle = computed(() => ({ ...messageStyle.value, ...carriedLayer.value }))
579
+ return { message, messageHtml, messageShown, messageStyle: placedMessageStyle, messageCarried: carried, inlaid }
466
580
  }
@@ -21,17 +21,37 @@ export interface TransitionInkBox {
21
21
 
22
22
  interface TransitionInk extends TransitionInkBox {
23
23
  page: number
24
+ /** Left for the neighbouring page rather than for a remount of this one. */
25
+ handedOver: boolean
26
+ onTaken?: () => void
24
27
  }
25
28
 
26
29
  const inkByIdentity = new Map<string, TransitionInk[]>()
27
30
 
28
- /** Record ink that was visible when its page was left. */
31
+ /**
32
+ * Record ink that was visible when its instance was unmounted, for the
33
+ * instance that replaces it on the same page.
34
+ */
29
35
  export function rememberTransitionInk(identity: string, page: number, box: TransitionInkBox) {
36
+ store(identity, { page, ...box, handedOver: false })
37
+ }
38
+
39
+ /**
40
+ * Record ink that was visible when its page was left, for the equivalent
41
+ * annotation on the neighbouring page. Never taken back by the page that left
42
+ * it: an annotation its neighbour does not have is simply new on the way back.
43
+ * `onTaken` runs when the neighbour takes the ink over, so the instance that
44
+ * left it can drop its own copy at once instead of fading out underneath.
45
+ */
46
+ export function handOverTransitionInk(identity: string, page: number, box: TransitionInkBox, onTaken?: () => void) {
47
+ store(identity, { page, ...box, handedOver: true, onTaken })
48
+ }
49
+
50
+ function store(identity: string, record: TransitionInk) {
30
51
  const records = inkByIdentity.get(identity) ?? []
31
52
  // Repeated active/inactive updates for the same annotation replace its old
32
53
  // hand-off instead of accumulating stale geometry.
33
- const existing = records.findIndex(record => record.page === page)
34
- const record = { page, ...box }
54
+ const existing = records.findIndex(other => other.page === record.page)
35
55
  if (existing >= 0)
36
56
  records[existing] = record
37
57
  else
@@ -40,8 +60,8 @@ export function rememberTransitionInk(identity: string, page: number, box: Trans
40
60
  }
41
61
 
42
62
  /**
43
- * Consume ink from the same page (left by an instance that was unmounted) or
44
- * the immediately adjacent one when it occupies the same final position.
63
+ * Consume ink left by an unmounted instance of the same page, or handed over
64
+ * by the immediately adjacent one, when it occupies the same final position.
45
65
  * Coordinates are fractions of the concrete slide, so this survives presenter
46
66
  * scaling and a differently sized browser window.
47
67
  */
@@ -52,7 +72,7 @@ export function takeTransitionInk(identity: string, page: number, box: Transitio
52
72
 
53
73
  const adjacent = records
54
74
  .map((record, index) => ({ record, index }))
55
- .filter(({ record }) => Math.abs(record.page - page) <= 1)
75
+ .filter(({ record }) => Math.abs(record.page - page) === (record.handedOver ? 1 : 0))
56
76
  if (!adjacent.length)
57
77
  return false
58
78
 
@@ -77,5 +97,6 @@ export function takeTransitionInk(identity: string, page: number, box: Transitio
77
97
  inkByIdentity.set(identity, records)
78
98
  else
79
99
  inkByIdentity.delete(identity)
100
+ closest.record.onTaken?.()
80
101
  return true
81
102
  }
Binary file