slidev-theme-one-purple-unicorn-pro 3.0.0 → 3.0.4
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.
- package/CHANGELOG.md +74 -0
- package/README.md +5 -3
- package/components/Countdown.vue +4 -3
- package/components/PresenterNote.vue +4 -5
- package/components/StageProgress.vue +39 -7
- package/components/internal/MediaPanel.vue +11 -3
- package/components/internal/ScaledIframe.vue +8 -2
- package/global-bottom.vue +2 -2
- package/layouts/iframe.vue +3 -2
- package/layouts/image.vue +2 -1
- package/package.json +1 -1
- package/setup/cuda-transformer.ts +16 -11
- package/setup/main.ts +5 -2
- package/styles/animations.css +35 -24
- package/styles/code-theme.css +8 -8
- package/styles/main.css +5 -3
- package/utils/border-beam.ts +6 -5
- package/utils/pages.ts +5 -3
- package/utils/print-canvas.ts +187 -0
- package/utils/print-effects.ts +231 -0
- package/utils/print-export.ts +139 -0
- package/utils/print-gradient-text.ts +125 -0
- package/utils/print-mode.ts +4 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,79 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 3.0.4
|
|
4
|
+
|
|
5
|
+
### Fixes
|
|
6
|
+
|
|
7
|
+
- **PDF export memory and size.** Shadows were drawn on a slide-sized canvas at 4× per
|
|
8
|
+
slide (about 2 GB of canvas for the 78-slide example); each shadow now gets a canvas
|
|
9
|
+
just big enough for it at 2×, and gradient text a canvas the size of its text. About
|
|
10
|
+
9× less memory; PDFs are smaller than before 3.0.3 (example 11.9 → 8.1 MB).
|
|
11
|
+
- **Export safety.** A failure while redrawing one effect leaves that effect to the CSS
|
|
12
|
+
and the rest carries on, and the export never waits more than 15 s (a font that never
|
|
13
|
+
loads used to stall `slidev export` until it failed). Canvases that would be too big
|
|
14
|
+
get a lower resolution instead of coming out blank, and browsers without the canvas
|
|
15
|
+
features used (Chromium 99, Safari 16.4, Firefox 112) keep the CSS effects.
|
|
16
|
+
|
|
17
|
+
### Internal
|
|
18
|
+
|
|
19
|
+
- Each element's styles are read once per pass; text shadows only measure the element's
|
|
20
|
+
own text; one transparency check.
|
|
21
|
+
|
|
22
|
+
## 3.0.3
|
|
23
|
+
|
|
24
|
+
### Fixes
|
|
25
|
+
|
|
26
|
+
- **PDF export: border light, glow and shadows.** The `anim-border` light filled the whole
|
|
27
|
+
card in most PDF viewers (its mask was dropped), and macOS Preview showed every blurred
|
|
28
|
+
shadow (code blocks, tables, cards, `anim-glow`, the progress bar, title text shadows)
|
|
29
|
+
as a translucent rectangle. In exports they are now drawn as images, like gradient text
|
|
30
|
+
in 3.0.2.
|
|
31
|
+
- **Export look didn't apply to `slidev export`.** The still-picture rules (no shimmer
|
|
32
|
+
band, finished entrance animations, hidden `Countdown` / `PresenterNote`) were
|
|
33
|
+
`@media print`, which `slidev export` doesn't use. They now hang off `html.print-mode`,
|
|
34
|
+
set for `slidev export`, the browser export page and browser printing. `anim-glow`
|
|
35
|
+
keeps a steady glow instead of none.
|
|
36
|
+
|
|
37
|
+
### Internal
|
|
38
|
+
|
|
39
|
+
- One `isPrintMode` for the border light and the export code; the export redraw scans
|
|
40
|
+
only newly added slides after the first pass and measures one-line text as a whole.
|
|
41
|
+
|
|
42
|
+
## 3.0.2
|
|
43
|
+
|
|
44
|
+
### Fixes
|
|
45
|
+
|
|
46
|
+
- **PDF export: gradient text.** Titles, title-slide subtitles, `.gradient-text*`,
|
|
47
|
+
`.anim-gradient-text*` and the quote marks are CSS gradient text, which PDF viewers draw
|
|
48
|
+
differently: macOS Preview showed a solid gradient bar (or black text) instead of the
|
|
49
|
+
title, other viewers a thin box around it. In exports (`slidev export`, the browser
|
|
50
|
+
export page) the theme now draws that text as a high-resolution image of the same
|
|
51
|
+
gradient over the original, which stays invisible in place, so the text can still be
|
|
52
|
+
searched and copied. The presentation itself is unchanged.
|
|
53
|
+
|
|
54
|
+
### Example
|
|
55
|
+
|
|
56
|
+
- `npm run export` in `example/` (adds `playwright-chromium`).
|
|
57
|
+
|
|
58
|
+
## 3.0.1
|
|
59
|
+
|
|
60
|
+
### Fixes
|
|
61
|
+
|
|
62
|
+
- Slidev's web editor: the caret no longer drifts away from the text. Theme code-block
|
|
63
|
+
styles (padding, border, line height) applied to the editor too; they now apply to
|
|
64
|
+
slides only. Slides look the same.
|
|
65
|
+
- Stage indicator steps are buttons: reachable with Tab, activated with Enter / Space,
|
|
66
|
+
current step announced to screen readers. Mouse clicks look and behave as before.
|
|
67
|
+
- `iframe` `scale`: zero, negative or non-numeric values fall back to 1 instead of
|
|
68
|
+
making the page disappear.
|
|
69
|
+
- `progressBarSkip`: pages below 1 or past the last page are ignored, so they no longer
|
|
70
|
+
skew `progressBarSkipMode: exclude`, and huge ranges stay cheap.
|
|
71
|
+
- `image` layout image URLs with spaces or parentheses load correctly.
|
|
72
|
+
|
|
73
|
+
### Added
|
|
74
|
+
|
|
75
|
+
- `image` layout `alt` and `iframe` layout `title` for screen readers.
|
|
76
|
+
|
|
3
77
|
## 3.0.0
|
|
4
78
|
|
|
5
79
|
Includes everything from 2.2.0 (tagged, not published to npm).
|
package/README.md
CHANGED
|
@@ -69,8 +69,8 @@ The quote mark uses a serif and the macOS button styles use the system UI font b
|
|
|
69
69
|
| `default` | Content slide (title pinned at the top) | |
|
|
70
70
|
| `center` | Centered content | `padding`, `contentMaxWidth` |
|
|
71
71
|
| `two-cols`, `three-cols` | Columns with a header (default slot) and a `bottom` slot | `leftWidth`, `centerWidth`, `rightWidth`, `divider`, `leftLabel`, `centerLabel`, `rightLabel`, `leftLabelColor`, `centerLabelColor`, `rightLabelColor` |
|
|
72
|
-
| `image` | Image beside content | `image`, `side` (`left`/`right`), `backgroundSize` |
|
|
73
|
-
| `iframe` | Embedded page, full or beside content | `url`, `side` (`full`/`left`/`right`), `scale` |
|
|
72
|
+
| `image` | Image beside content | `image`, `side` (`left`/`right`), `backgroundSize`, `alt` (screen-reader text; empty = decorative) |
|
|
73
|
+
| `iframe` | Embedded page, full or beside content | `url`, `side` (`full`/`left`/`right`), `scale` (> 0, default 1), `title` (screen-reader name) |
|
|
74
74
|
| `quote`, `statement`, `fact` | Emphasis slides | `padding`, `contentMaxWidth` |
|
|
75
75
|
| `full` | No padding, build your own | |
|
|
76
76
|
| `full-text` | Full-width text, no pinned title | |
|
|
@@ -241,7 +241,9 @@ Every value below defaults to the theme's look.
|
|
|
241
241
|
| `hover-lift` / `-scale` / `-tilt` / `-glow` / `-border` / `-gradient` / `-shine` | `--hover-duration`, `--hover-lift`, `--hover-scale`, `--hover-tilt-x`, `--hover-tilt-y`, `--hover-glow`, `--hover-border-c1` / `-c2`, `--hover-gradient-c1` / `-c2`, `--hover-shine-color`, `--hover-shine-width` | `hover-glow-blue`, `hover-glow-green` |
|
|
242
242
|
| Slide transitions (`transition:` frontmatter) | `--slide-transition-duration` | `slide-left` `slide-right` `slide-up` `fade` `scale-fade` `blur-fade` |
|
|
243
243
|
|
|
244
|
-
The animated border light runs at a constant speed along the border (a small script)
|
|
244
|
+
The animated border light runs at a constant speed along the border (a small script). All animations respect `prefers-reduced-motion`.
|
|
245
|
+
|
|
246
|
+
**In PDF / PNG exports** each effect is a still picture: a static gradient ring for `anim-border`, a steady glow for `anim-glow`, no shimmer band, entrance animations finished; `Countdown` and `PresenterNote` are left out. Gradient text, border rings and shadows are drawn as images there, because PDF viewers (macOS Preview in particular) draw the CSS versions wrongly; the text stays searchable. To style exports yourself, use the `html.print-mode` class (set for `slidev export`, the export page and browser printing), not `@media print` — `slidev export` renders with screen media.
|
|
245
247
|
|
|
246
248
|
## Colors
|
|
247
249
|
|
package/components/Countdown.vue
CHANGED
|
@@ -70,8 +70,9 @@ defineExpose({ start, stop, remaining })
|
|
|
70
70
|
.countdown-sm { --countdown-size: 1rem; }
|
|
71
71
|
.countdown-lg { --countdown-size: 3rem; }
|
|
72
72
|
.countdown-xl { --countdown-size: 5rem; }
|
|
73
|
+
</style>
|
|
73
74
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
75
|
+
<style>
|
|
76
|
+
/* Not in PDF/PNG exports or printouts (html.print-mode, utils/print-export.ts) */
|
|
77
|
+
.print-mode .countdown { display: none !important; }
|
|
77
78
|
</style>
|
|
@@ -35,10 +35,9 @@
|
|
|
35
35
|
font-size: 0.75rem;
|
|
36
36
|
margin: 0;
|
|
37
37
|
}
|
|
38
|
+
</style>
|
|
38
39
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
}
|
|
40
|
+
<style>
|
|
41
|
+
/* Not in PDF/PNG exports or printouts (html.print-mode, utils/print-export.ts) */
|
|
42
|
+
.print-mode .presenter-note { display: none !important; }
|
|
44
43
|
</style>
|
|
@@ -1,25 +1,29 @@
|
|
|
1
1
|
<template>
|
|
2
|
-
<
|
|
2
|
+
<nav
|
|
3
3
|
v-if="!isHidden && stages.length > 0"
|
|
4
4
|
class="stage-progress"
|
|
5
5
|
:class="[`stage-pos-${position}`, { 'chrome-on-dark': onDark }]"
|
|
6
|
+
aria-label="Stages"
|
|
6
7
|
>
|
|
7
|
-
<
|
|
8
|
+
<button
|
|
8
9
|
v-for="(stage, i) in stages"
|
|
9
10
|
:key="`stage-${stage}-${i}`"
|
|
11
|
+
type="button"
|
|
10
12
|
class="stage-item stage-clickable"
|
|
11
13
|
:class="{
|
|
12
14
|
'stage-done': i < currentIndex,
|
|
13
15
|
'stage-active': i === currentIndex,
|
|
14
16
|
'stage-upcoming': i > currentIndex,
|
|
15
17
|
}"
|
|
16
|
-
|
|
18
|
+
:aria-current="i === currentIndex ? 'step' : undefined"
|
|
19
|
+
@click="onStageClick($event, i)"
|
|
20
|
+
@keydown.space.stop
|
|
17
21
|
>
|
|
18
|
-
<
|
|
22
|
+
<span class="stage-dot" />
|
|
19
23
|
<span class="stage-label">{{ stage }}</span>
|
|
20
|
-
<
|
|
21
|
-
</
|
|
22
|
-
</
|
|
24
|
+
<span v-if="i < stages.length - 1" class="stage-connector" />
|
|
25
|
+
</button>
|
|
26
|
+
</nav>
|
|
23
27
|
</template>
|
|
24
28
|
|
|
25
29
|
<script setup lang="ts">
|
|
@@ -50,6 +54,14 @@ const position = computed(() =>
|
|
|
50
54
|
// Pre-compute stages key for comparison (avoids JSON.stringify per click)
|
|
51
55
|
const stagesKey = computed(() => stages.value.join('|||'))
|
|
52
56
|
|
|
57
|
+
// A mouse click leaves no focus behind, so Space / arrows keep driving the
|
|
58
|
+
// slides; keyboard activation (Enter / Space) keeps focus on the button.
|
|
59
|
+
// Space is stopped here so it activates the button instead of advancing.
|
|
60
|
+
function onStageClick(event: MouseEvent, index: number) {
|
|
61
|
+
if (event.detail > 0) (event.currentTarget as HTMLElement).blur()
|
|
62
|
+
goToStage(index)
|
|
63
|
+
}
|
|
64
|
+
|
|
53
65
|
function goToStage(index: number) {
|
|
54
66
|
// Explicit stageMap takes priority
|
|
55
67
|
const map = frontmatter.value.stageMap as number[] | undefined
|
|
@@ -170,6 +182,24 @@ function goToStage(index: number) {
|
|
|
170
182
|
.stage-clickable {
|
|
171
183
|
cursor: pointer;
|
|
172
184
|
pointer-events: auto;
|
|
185
|
+
/* Reset button chrome */
|
|
186
|
+
background: none;
|
|
187
|
+
border: 0;
|
|
188
|
+
padding: 0;
|
|
189
|
+
margin: 0;
|
|
190
|
+
font: inherit;
|
|
191
|
+
color: inherit;
|
|
192
|
+
text-align: inherit;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
.stage-clickable:focus-visible {
|
|
196
|
+
outline: 1px solid var(--stage-active-color, var(--primary-400, #c678dd));
|
|
197
|
+
outline-offset: 2px;
|
|
198
|
+
border-radius: 3px;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
.stage-clickable:focus-visible .stage-label {
|
|
202
|
+
opacity: 1 !important;
|
|
173
203
|
}
|
|
174
204
|
|
|
175
205
|
.stage-clickable:hover .stage-dot {
|
|
@@ -182,6 +212,7 @@ function goToStage(index: number) {
|
|
|
182
212
|
|
|
183
213
|
/* Dot */
|
|
184
214
|
.stage-dot {
|
|
215
|
+
display: block;
|
|
185
216
|
width: var(--stage-dot-size, 6px);
|
|
186
217
|
height: var(--stage-dot-size, 6px);
|
|
187
218
|
border-radius: 50%;
|
|
@@ -197,6 +228,7 @@ function goToStage(index: number) {
|
|
|
197
228
|
}
|
|
198
229
|
|
|
199
230
|
.stage-connector {
|
|
231
|
+
display: block;
|
|
200
232
|
width: 1.5rem;
|
|
201
233
|
height: 1px;
|
|
202
234
|
margin: 0 0.25rem;
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
<template>
|
|
2
2
|
<div class="media-panel" :class="`media-${side}`">
|
|
3
|
-
<div
|
|
4
|
-
|
|
3
|
+
<div
|
|
4
|
+
class="media-container"
|
|
5
|
+
:style="mediaStyle"
|
|
6
|
+
:role="type === 'image' ? (alt ? 'img' : 'presentation') : undefined"
|
|
7
|
+
:aria-label="type === 'image' && alt ? alt : undefined"
|
|
8
|
+
>
|
|
9
|
+
<ScaledIframe v-if="type === 'iframe'" :src="src" :scale="scale" :title="title" />
|
|
5
10
|
</div>
|
|
6
11
|
<div class="media-content">
|
|
7
12
|
<slot />
|
|
@@ -19,12 +24,15 @@ const props = defineProps({
|
|
|
19
24
|
side: { type: String, default: 'left' },
|
|
20
25
|
backgroundSize: { type: String, default: 'cover' },
|
|
21
26
|
scale: { type: [Number, String], default: 1 },
|
|
27
|
+
// Text for screen readers: alt for an image (empty = decorative), title for an iframe
|
|
28
|
+
alt: { type: String, default: '' },
|
|
29
|
+
title: { type: String, default: undefined },
|
|
22
30
|
})
|
|
23
31
|
|
|
24
32
|
const mediaStyle = computed(() => {
|
|
25
33
|
if (props.type === 'image') {
|
|
26
34
|
return {
|
|
27
|
-
backgroundImage: `url(${props.src})`,
|
|
35
|
+
backgroundImage: `url(${JSON.stringify(props.src)})`,
|
|
28
36
|
backgroundSize: props.backgroundSize,
|
|
29
37
|
backgroundPosition: 'center',
|
|
30
38
|
backgroundRepeat: 'no-repeat',
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
<iframe
|
|
3
3
|
class="scaled-iframe"
|
|
4
4
|
:src="src"
|
|
5
|
+
:title="title"
|
|
5
6
|
:style="scaleStyle"
|
|
6
7
|
frameborder="0"
|
|
7
8
|
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
|
|
@@ -15,11 +16,16 @@ import { computed } from 'vue'
|
|
|
15
16
|
const props = defineProps({
|
|
16
17
|
src: { type: String, required: true },
|
|
17
18
|
scale: { type: [Number, String], default: 1 },
|
|
19
|
+
title: { type: String, default: undefined },
|
|
18
20
|
})
|
|
19
21
|
|
|
20
|
-
// Render at 1/scale size, then scale back down to fill the container
|
|
22
|
+
// Render at 1/scale size, then scale back down to fill the container.
|
|
23
|
+
// A missing, zero or non-numeric scale falls back to 1; tiny values are
|
|
24
|
+
// clamped so the iframe never renders at an enormous size.
|
|
25
|
+
const MIN_SCALE = 0.05
|
|
21
26
|
const scaleStyle = computed(() => {
|
|
22
|
-
const
|
|
27
|
+
const n = Number(props.scale)
|
|
28
|
+
const s = Number.isFinite(n) && n > 0 ? Math.max(n, MIN_SCALE) : 1
|
|
23
29
|
return {
|
|
24
30
|
width: `${100 / s}%`,
|
|
25
31
|
height: `${100 / s}%`,
|
package/global-bottom.vue
CHANGED
|
@@ -41,7 +41,7 @@ const progressMode = computed(() => {
|
|
|
41
41
|
// progressBarSkipMode:
|
|
42
42
|
// "hide" (default) → only hide the bar on those pages
|
|
43
43
|
// "exclude" → also leave them out of the progress calculation
|
|
44
|
-
const skipPages = computed(() => parsePageList(headmatter.value.progressBarSkip))
|
|
44
|
+
const skipPages = computed(() => parsePageList(headmatter.value.progressBarSkip, total.value))
|
|
45
45
|
const excludeSkipped = computed(() => headmatter.value.progressBarSkipMode === 'exclude')
|
|
46
46
|
|
|
47
47
|
// Export / print (PDF, PNG): progressBarInExport / pageNumberInExport (headmatter)
|
|
@@ -68,7 +68,7 @@ const progress = computed(() => {
|
|
|
68
68
|
let page = currentPage.value
|
|
69
69
|
let count = total.value
|
|
70
70
|
if (excludeSkipped.value) {
|
|
71
|
-
const skipped = [...skipPages.value]
|
|
71
|
+
const skipped = [...skipPages.value]
|
|
72
72
|
page -= skipped.filter(n => n < page).length
|
|
73
73
|
count -= skipped.length
|
|
74
74
|
}
|
package/layouts/iframe.vue
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
<template>
|
|
2
2
|
<div class="slidev-layout iframe-layout slide-bare">
|
|
3
3
|
<div v-if="side === 'full'" class="iframe-full">
|
|
4
|
-
<ScaledIframe :src="url" :scale="scale" />
|
|
4
|
+
<ScaledIframe :src="url" :scale="scale" :title="title" />
|
|
5
5
|
</div>
|
|
6
|
-
<MediaPanel v-else :src="url" type="iframe" :side="side" :scale="scale">
|
|
6
|
+
<MediaPanel v-else :src="url" type="iframe" :side="side" :scale="scale" :title="title">
|
|
7
7
|
<slot />
|
|
8
8
|
</MediaPanel>
|
|
9
9
|
</div>
|
|
@@ -17,6 +17,7 @@ defineProps({
|
|
|
17
17
|
url: { type: String, required: true },
|
|
18
18
|
side: { type: String, default: 'full' },
|
|
19
19
|
scale: { type: [Number, String], default: 1 },
|
|
20
|
+
title: { type: String, default: undefined },
|
|
20
21
|
})
|
|
21
22
|
</script>
|
|
22
23
|
|
package/layouts/image.vue
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<template>
|
|
2
2
|
<div class="slidev-layout image-layout slide-bare">
|
|
3
|
-
<MediaPanel :src="image" type="image" :side="side" :background-size="backgroundSize">
|
|
3
|
+
<MediaPanel :src="image" type="image" :side="side" :background-size="backgroundSize" :alt="alt">
|
|
4
4
|
<slot />
|
|
5
5
|
</MediaPanel>
|
|
6
6
|
</div>
|
|
@@ -13,5 +13,6 @@ defineProps({
|
|
|
13
13
|
image: { type: String, required: true },
|
|
14
14
|
side: { type: String, default: 'left' },
|
|
15
15
|
backgroundSize: { type: String, default: 'cover' },
|
|
16
|
+
alt: { type: String, default: '' },
|
|
16
17
|
})
|
|
17
18
|
</script>
|
package/package.json
CHANGED
|
@@ -4,6 +4,12 @@
|
|
|
4
4
|
|
|
5
5
|
import type { ShikiTransformer } from 'shiki'
|
|
6
6
|
|
|
7
|
+
// HAST node types, taken from Shiki's own hook signatures (no extra dependency)
|
|
8
|
+
type Element = Parameters<NonNullable<ShikiTransformer['span']>>[0]
|
|
9
|
+
type Content = Element['children'][number]
|
|
10
|
+
type Root = Parameters<NonNullable<ShikiTransformer['root']>>[0]
|
|
11
|
+
type Node = Root | Root['children'][number]
|
|
12
|
+
|
|
7
13
|
interface TokenDef {
|
|
8
14
|
pattern: RegExp
|
|
9
15
|
color: string
|
|
@@ -93,11 +99,10 @@ const cudaTokens: TokenDef[] = [
|
|
|
93
99
|
},
|
|
94
100
|
]
|
|
95
101
|
|
|
96
|
-
function processSpan(span:
|
|
97
|
-
if (span.
|
|
98
|
-
if (!span.children) return
|
|
102
|
+
function processSpan(span: Element) {
|
|
103
|
+
if (span.tagName !== 'span' || !span.children) return
|
|
99
104
|
|
|
100
|
-
const newChildren:
|
|
105
|
+
const newChildren: Content[] = []
|
|
101
106
|
|
|
102
107
|
for (const child of span.children) {
|
|
103
108
|
if (child.type !== 'text') {
|
|
@@ -158,18 +163,18 @@ function processSpan(span: any) {
|
|
|
158
163
|
}
|
|
159
164
|
|
|
160
165
|
// Extract text from a HAST node
|
|
161
|
-
function getNodeText(node:
|
|
166
|
+
function getNodeText(node: Node): string {
|
|
162
167
|
if (node.type === 'text') return node.value || ''
|
|
163
|
-
if (node
|
|
168
|
+
if ('children' in node) return node.children.map(getNodeText).join('')
|
|
164
169
|
return ''
|
|
165
170
|
}
|
|
166
171
|
|
|
167
172
|
// Collect all span elements in a node tree
|
|
168
|
-
function collectSpans(node:
|
|
173
|
+
function collectSpans(node: Node, result: Element[] = []): Element[] {
|
|
169
174
|
if (node.type === 'element' && node.tagName === 'span') {
|
|
170
175
|
result.push(node)
|
|
171
176
|
}
|
|
172
|
-
if (node
|
|
177
|
+
if ('children' in node) {
|
|
173
178
|
for (const child of node.children) {
|
|
174
179
|
collectSpans(child, result)
|
|
175
180
|
}
|
|
@@ -178,8 +183,8 @@ function collectSpans(node: any, result: any[] = []): any[] {
|
|
|
178
183
|
}
|
|
179
184
|
|
|
180
185
|
// Cross-span: mark kernel function names and <<< >>> launch syntax
|
|
181
|
-
function markKernelLaunch(codeNode:
|
|
182
|
-
const lines = codeNode.children
|
|
186
|
+
function markKernelLaunch(codeNode: Element) {
|
|
187
|
+
const lines = codeNode.children.filter((c): c is Element => c.type === 'element')
|
|
183
188
|
|
|
184
189
|
for (const line of lines) {
|
|
185
190
|
const spans = collectSpans(line)
|
|
@@ -212,7 +217,7 @@ export function cudaTransformer(): ShikiTransformer {
|
|
|
212
217
|
return {
|
|
213
218
|
name: 'cuda-highlighter',
|
|
214
219
|
pre(node) {
|
|
215
|
-
const code = node.children
|
|
220
|
+
const code = node.children.find((c): c is Element => c.type === 'element' && c.tagName === 'code')
|
|
216
221
|
if (code) {
|
|
217
222
|
markKernelLaunch(code)
|
|
218
223
|
}
|
package/setup/main.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { defineAppSetup } from '@slidev/types'
|
|
2
2
|
import { setupBorderBeams } from '../utils/border-beam'
|
|
3
|
+
import { setupPrintExport } from '../utils/print-export'
|
|
3
4
|
|
|
4
|
-
export default defineAppSetup(() => {
|
|
5
|
-
if (typeof window
|
|
5
|
+
export default defineAppSetup(({ router }) => {
|
|
6
|
+
if (typeof window === 'undefined') return
|
|
7
|
+
setupBorderBeams()
|
|
8
|
+
setupPrintExport(router)
|
|
6
9
|
})
|
package/styles/animations.css
CHANGED
|
@@ -533,28 +533,39 @@
|
|
|
533
533
|
}
|
|
534
534
|
}
|
|
535
535
|
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
536
|
+
/* PDF/PNG export and printing: html.print-mode (utils/print-export.ts). A
|
|
537
|
+
still picture of each effect; `slidev export` uses screen media, so this
|
|
538
|
+
can't be @media print. */
|
|
539
|
+
.print-mode .anim-border::before,
|
|
540
|
+
.print-mode .anim-cursor,
|
|
541
|
+
.print-mode .anim-gradient-text,
|
|
542
|
+
.print-mode [class*="anim-gradient-text-"] {
|
|
543
|
+
animation: none !important;
|
|
544
|
+
}
|
|
545
|
+
/* A light band frozen mid-sweep looks like a smudge */
|
|
546
|
+
.print-mode .anim-shimmer::after,
|
|
547
|
+
.print-mode .hover-shine::after {
|
|
548
|
+
display: none !important;
|
|
549
|
+
}
|
|
550
|
+
/* Glow: the pulse's bright end, held */
|
|
551
|
+
.print-mode .anim-glow {
|
|
552
|
+
animation: none !important;
|
|
553
|
+
box-shadow:
|
|
554
|
+
0 0 16px color-mix(in srgb, var(--_glow) calc(var(--_glow-i) * 100%), transparent),
|
|
555
|
+
0 0 36px color-mix(in srgb, var(--_glow) calc(var(--_glow-i) * 20%), transparent);
|
|
556
|
+
}
|
|
557
|
+
.print-mode .anim-float,
|
|
558
|
+
.print-mode .anim-pop,
|
|
559
|
+
.print-mode .anim-fade-in,
|
|
560
|
+
.print-mode .anim-fade-up,
|
|
561
|
+
.print-mode .anim-fade-left,
|
|
562
|
+
.print-mode .anim-fade-right {
|
|
563
|
+
animation: none !important;
|
|
564
|
+
opacity: 1 !important;
|
|
565
|
+
transform: none !important;
|
|
566
|
+
}
|
|
567
|
+
/* Pseudo-elements redrawn as images in an export */
|
|
568
|
+
.print-no-before::before,
|
|
569
|
+
.print-no-after::after {
|
|
570
|
+
content: none !important;
|
|
560
571
|
}
|
package/styles/code-theme.css
CHANGED
|
@@ -3,15 +3,15 @@
|
|
|
3
3
|
This file forces dark background on code blocks
|
|
4
4
|
even when Slidev tries to override it. */
|
|
5
5
|
|
|
6
|
-
/* Force dark background on
|
|
7
|
-
pre,
|
|
8
|
-
.shiki,
|
|
9
|
-
.slidev-code {
|
|
6
|
+
/* Force dark background on code blocks in slides (not Slidev's editor) */
|
|
7
|
+
:where(.slidev-layout) pre,
|
|
8
|
+
:where(.slidev-layout) .shiki,
|
|
9
|
+
:where(.slidev-layout) .slidev-code {
|
|
10
10
|
background: var(--bg-code, #21252b) !important;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
/* Scrollbar styling for code blocks */
|
|
14
|
-
.shiki::-webkit-scrollbar { width: 8px; height: 8px; }
|
|
15
|
-
.shiki::-webkit-scrollbar-track { background: var(--bg-tertiary, #3e4452); border-radius: 4px; }
|
|
16
|
-
.shiki::-webkit-scrollbar-thumb { background: var(--text-muted, #5c6370); border-radius: 4px; }
|
|
17
|
-
.shiki::-webkit-scrollbar-thumb:hover { background: var(--text-secondary, #939aa3); }
|
|
14
|
+
:where(.slidev-layout) .shiki::-webkit-scrollbar { width: 8px; height: 8px; }
|
|
15
|
+
:where(.slidev-layout) .shiki::-webkit-scrollbar-track { background: var(--bg-tertiary, #3e4452); border-radius: 4px; }
|
|
16
|
+
:where(.slidev-layout) .shiki::-webkit-scrollbar-thumb { background: var(--text-muted, #5c6370); border-radius: 4px; }
|
|
17
|
+
:where(.slidev-layout) .shiki::-webkit-scrollbar-thumb:hover { background: var(--text-secondary, #939aa3); }
|
package/styles/main.css
CHANGED
|
@@ -83,8 +83,10 @@ mark {
|
|
|
83
83
|
|
|
84
84
|
/* ============================================
|
|
85
85
|
Code Blocks
|
|
86
|
+
Scoped to slides: Slidev's own UI (the web editor overlays a textarea on a
|
|
87
|
+
highlighted <pre>) must not get slide code styles, or the caret drifts.
|
|
86
88
|
============================================ */
|
|
87
|
-
pre {
|
|
89
|
+
:where(.slidev-layout) pre {
|
|
88
90
|
background: var(--bg-code);
|
|
89
91
|
border: 1px solid var(--border-default);
|
|
90
92
|
border-radius: 0.75rem;
|
|
@@ -114,14 +116,14 @@ pre {
|
|
|
114
116
|
}
|
|
115
117
|
|
|
116
118
|
|
|
117
|
-
code {
|
|
119
|
+
:where(.slidev-layout) code {
|
|
118
120
|
font-family: var(--font-mono);
|
|
119
121
|
font-size: 0.8rem; /* Reduced from 0.9rem */
|
|
120
122
|
font-variant-ligatures: none;
|
|
121
123
|
line-height: 1.2; /* Tight line height for code */
|
|
122
124
|
}
|
|
123
125
|
|
|
124
|
-
:not(pre) > code {
|
|
126
|
+
:where(.slidev-layout) :not(pre) > code {
|
|
125
127
|
/* When a chip wraps, give each line its own padding and corners */
|
|
126
128
|
-webkit-box-decoration-break: clone;
|
|
127
129
|
box-decoration-break: clone;
|
package/utils/border-beam.ts
CHANGED
|
@@ -5,12 +5,16 @@
|
|
|
5
5
|
// Without this script (or without pseudo-element animation support) the
|
|
6
6
|
// CSS conic rotation stays as the fallback.
|
|
7
7
|
|
|
8
|
+
import { isPrintMode } from './print-mode'
|
|
9
|
+
|
|
8
10
|
const REF_PERIMETER = 600 // px; a box this size takes --ab-duration per lap
|
|
9
11
|
|
|
10
12
|
const running = new WeakMap<Element, Animation>()
|
|
11
13
|
|
|
12
14
|
function animate(el: HTMLElement) {
|
|
13
15
|
running.get(el)?.cancel()
|
|
16
|
+
// Reached the export page from the presentation: keep the static ring there
|
|
17
|
+
if (isPrintMode()) { el.classList.remove('ab-beam'); return }
|
|
14
18
|
// offsetWidth/Height: layout size, unaffected by the slide's CSS scaling
|
|
15
19
|
const bw = el.offsetWidth
|
|
16
20
|
const bh = el.offsetHeight
|
|
@@ -44,14 +48,11 @@ function animate(el: HTMLElement) {
|
|
|
44
48
|
}
|
|
45
49
|
}
|
|
46
50
|
|
|
47
|
-
// PDF/PNG export (?print or /export): keep the static CSS ring instead of a
|
|
48
|
-
// light frozen at some point of its path
|
|
49
|
-
const isPrintMode = () =>
|
|
50
|
-
new URLSearchParams(location.search).has('print') || /\/export\b/.test(location.pathname)
|
|
51
|
-
|
|
52
51
|
export function setupBorderBeams() {
|
|
53
52
|
if (typeof window === 'undefined' || !('animate' in Element.prototype)) return
|
|
54
53
|
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return
|
|
54
|
+
// PDF/PNG export: keep the static CSS ring instead of a light frozen at some
|
|
55
|
+
// point of its path
|
|
55
56
|
if (isPrintMode()) return
|
|
56
57
|
|
|
57
58
|
const resize = new ResizeObserver(entries => entries.forEach(e => animate(e.target as HTMLElement)))
|
package/utils/pages.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
// Parse a page list like "1, 5, 10-12" or [1, 5, "10-12"] into a set of page numbers
|
|
2
|
-
|
|
1
|
+
// Parse a page list like "1, 5, 10-12" or [1, 5, "10-12"] into a set of page numbers.
|
|
2
|
+
// Pages are 1-based; anything below 1 or above `max` (the deck's page count) is ignored.
|
|
3
|
+
export function parsePageList(value: unknown, max = Number.POSITIVE_INFINITY): Set<number> {
|
|
3
4
|
const pages = new Set<number>()
|
|
4
5
|
const items = Array.isArray(value) ? value : String(value ?? '').split(',')
|
|
5
6
|
for (const item of items) {
|
|
6
7
|
const [start, end = start] = String(item).split('-').map(s => Number.parseInt(s.trim(), 10))
|
|
7
8
|
if (Number.isNaN(start) || Number.isNaN(end)) continue
|
|
8
|
-
|
|
9
|
+
const last = Math.min(Math.max(start, end), max)
|
|
10
|
+
for (let n = Math.max(Math.min(start, end), 1); n <= last; n++) pages.add(n)
|
|
9
11
|
}
|
|
10
12
|
return pages
|
|
11
13
|
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
// Shared pieces for redrawing slide effects as images in PDF export
|
|
2
|
+
// (see print-export.ts): canvas setup, CSS value parsing, text measurement.
|
|
3
|
+
|
|
4
|
+
export const RESOLUTION = 4 // canvas pixels per CSS pixel (text and rings)
|
|
5
|
+
|
|
6
|
+
// "transparent", rgba(…, 0) or color(… / 0)
|
|
7
|
+
export const isTransparent = (color: string) =>
|
|
8
|
+
color === 'transparent' || /[,/]\s*0\)$/.test(color)
|
|
9
|
+
|
|
10
|
+
// Split a CSS list at top-level commas: "a, f(b, c), d" → ["a", "f(b, c)", "d"]
|
|
11
|
+
export function splitList(value: string) {
|
|
12
|
+
const out: string[] = []
|
|
13
|
+
let depth = 0
|
|
14
|
+
let cur = ''
|
|
15
|
+
for (const ch of value) {
|
|
16
|
+
if (ch === '(') depth++
|
|
17
|
+
if (ch === ')') depth--
|
|
18
|
+
if (ch === ',' && depth === 0) { out.push(cur.trim()); cur = '' }
|
|
19
|
+
else cur += ch
|
|
20
|
+
}
|
|
21
|
+
if (cur.trim()) out.push(cur.trim())
|
|
22
|
+
return out
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Arguments of a CSS function: "linear-gradient(90deg, red, blue)" → ["90deg", "red", "blue"]
|
|
26
|
+
export const functionArgs = (value: string) =>
|
|
27
|
+
splitList(value.slice(value.indexOf('(') + 1, value.lastIndexOf(')')))
|
|
28
|
+
|
|
29
|
+
// Color stops "color [pos%]" as [offset 0..1, color]; missing offsets are
|
|
30
|
+
// spread evenly between their neighbours, as CSS does
|
|
31
|
+
export function colorStops(args: string[], length = 1) {
|
|
32
|
+
const stops = args.map((a) => {
|
|
33
|
+
const m = a.match(/^(.*?)(?:\s+(-?[\d.]+)(%|px|deg))?$/)!
|
|
34
|
+
const pos = m[2] === undefined
|
|
35
|
+
? null
|
|
36
|
+
: m[3] === '%' ? Number(m[2]) / 100 : m[3] === 'deg' ? Number(m[2]) / 360 : Number(m[2]) / length
|
|
37
|
+
return { color: m[1], pos }
|
|
38
|
+
})
|
|
39
|
+
if (stops[0].pos === null) stops[0].pos = 0
|
|
40
|
+
if (stops.at(-1)!.pos === null) stops.at(-1)!.pos = 1
|
|
41
|
+
for (let i = 1; i < stops.length; i++) {
|
|
42
|
+
if (stops[i].pos !== null) continue
|
|
43
|
+
let j = i
|
|
44
|
+
while (stops[j].pos === null) j++
|
|
45
|
+
const a = stops[i - 1].pos!
|
|
46
|
+
const b = stops[j].pos!
|
|
47
|
+
for (let k = i; k < j; k++) stops[k].pos = a + (b - a) * (k - i + 1) / (j - i + 1)
|
|
48
|
+
}
|
|
49
|
+
return stops.map(s => [Math.min(1, Math.max(0, s.pos!)), s.color] as const)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// The padding box of `el` in viewport coordinates, and the slide's scale
|
|
53
|
+
// (slides are scaled with a transform, layout sizes are not)
|
|
54
|
+
export function paddingBox(el: HTMLElement) {
|
|
55
|
+
const r = el.getBoundingClientRect()
|
|
56
|
+
const zoom = el.offsetWidth ? r.width / el.offsetWidth : 1
|
|
57
|
+
return {
|
|
58
|
+
left: r.left + el.clientLeft * zoom,
|
|
59
|
+
top: r.top + el.clientTop * zoom,
|
|
60
|
+
width: el.clientWidth,
|
|
61
|
+
height: el.clientHeight,
|
|
62
|
+
zoom,
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// The canvas features the redraw uses (Chromium 99+, Safari 16.4+, Firefox
|
|
67
|
+
// 112+); without them effects are left to the CSS
|
|
68
|
+
export const canRedraw = () =>
|
|
69
|
+
typeof CanvasRenderingContext2D !== 'undefined'
|
|
70
|
+
&& 'roundRect' in CanvasRenderingContext2D.prototype
|
|
71
|
+
&& 'createConicGradient' in CanvasRenderingContext2D.prototype
|
|
72
|
+
|
|
73
|
+
// Browsers refuse larger canvases (and then draw nothing), so big areas get a
|
|
74
|
+
// lower resolution
|
|
75
|
+
const MAX_SIDE = 8192
|
|
76
|
+
const MAX_AREA = 32 * 1024 * 1024
|
|
77
|
+
|
|
78
|
+
// An absolutely placed canvas covering w×h CSS px at (x, y), drawn at
|
|
79
|
+
// `resolution` canvas pixels per CSS pixel (less if it would be too big).
|
|
80
|
+
// null when no canvas can be made; the caller then leaves the CSS as it is.
|
|
81
|
+
export function overlayCanvas(className: string, w: number, h: number, x = 0, y = 0, resolution = RESOLUTION) {
|
|
82
|
+
if (!(w > 0 && h > 0)) return null
|
|
83
|
+
const res = Math.min(resolution, MAX_SIDE / w, MAX_SIDE / h, Math.sqrt(MAX_AREA / (w * h)))
|
|
84
|
+
const canvas = document.createElement('canvas')
|
|
85
|
+
canvas.className = className
|
|
86
|
+
canvas.setAttribute('aria-hidden', 'true')
|
|
87
|
+
canvas.width = Math.max(1, Math.ceil(w * res))
|
|
88
|
+
canvas.height = Math.max(1, Math.ceil(h * res))
|
|
89
|
+
Object.assign(canvas.style, {
|
|
90
|
+
position: 'absolute', left: `${x}px`, top: `${y}px`, width: `${w}px`, height: `${h}px`,
|
|
91
|
+
pointerEvents: 'none',
|
|
92
|
+
})
|
|
93
|
+
const ctx = canvas.getContext('2d')
|
|
94
|
+
if (!ctx) return null
|
|
95
|
+
ctx.scale(res, res)
|
|
96
|
+
return { canvas, ctx, res }
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Corner radii [top-left, top-right, bottom-right, bottom-left] in px
|
|
100
|
+
export const cornerRadii = (s: CSSStyleDeclaration) =>
|
|
101
|
+
[s.borderTopLeftRadius, s.borderTopRightRadius, s.borderBottomRightRadius, s.borderBottomLeftRadius]
|
|
102
|
+
.map(v => Number.parseFloat(v) || 0)
|
|
103
|
+
|
|
104
|
+
export function roundRect(ctx: CanvasRenderingContext2D, x: number, y: number, w: number, h: number, radii: number[]) {
|
|
105
|
+
const max = Math.min(w, h) / 2
|
|
106
|
+
ctx.roundRect(x, y, w, h, radii.map(r => Math.max(0, Math.min(r, max))))
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export interface TextRun { text: string, parent: Element, left: number, top: number, right: number, bottom: number }
|
|
110
|
+
|
|
111
|
+
let range: Range // created on first use (the module also loads outside the browser)
|
|
112
|
+
function charRect(node: Text, i: number) {
|
|
113
|
+
range.setStart(node, i)
|
|
114
|
+
range.setEnd(node, i + 1)
|
|
115
|
+
const r = range.getClientRects()[0]
|
|
116
|
+
return r && r.width > 0 ? r : null
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Text inside `el` as laid out, one run per line, for the text nodes whose
|
|
120
|
+
// parent passes `accept` (only `el`'s own text nodes when `own`). A text node
|
|
121
|
+
// on one line (the usual case) is measured as a whole; only a node that wraps
|
|
122
|
+
// is measured per character to find its line breaks.
|
|
123
|
+
export function textRuns(el: Element, accept: (parent: CSSStyleDeclaration) => boolean, own = false) {
|
|
124
|
+
const runs: TextRun[] = []
|
|
125
|
+
range ??= document.createRange()
|
|
126
|
+
const nodes: Text[] = []
|
|
127
|
+
if (own) {
|
|
128
|
+
el.childNodes.forEach(n => n.nodeType === Node.TEXT_NODE && nodes.push(n as Text))
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
const walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT)
|
|
132
|
+
for (let n = walker.nextNode(); n; n = walker.nextNode()) nodes.push(n as Text)
|
|
133
|
+
}
|
|
134
|
+
for (const node of nodes) {
|
|
135
|
+
const parent = node.parentElement
|
|
136
|
+
if (!parent) continue
|
|
137
|
+
const ps = getComputedStyle(parent)
|
|
138
|
+
if (!accept(ps)) continue
|
|
139
|
+
|
|
140
|
+
range.selectNodeContents(node)
|
|
141
|
+
const lines = [...range.getClientRects()].filter(r => r.width > 0)
|
|
142
|
+
if (lines.length === 0) continue
|
|
143
|
+
|
|
144
|
+
if (lines.length === 1) {
|
|
145
|
+
// Collapse white space as the browser did; keep an edge space only if
|
|
146
|
+
// it was rendered (it is when the text continues after inline content)
|
|
147
|
+
let text = node.data
|
|
148
|
+
if (!ps.whiteSpace.startsWith('pre') && ps.whiteSpace !== 'break-spaces') {
|
|
149
|
+
const end = node.data.trimEnd().length
|
|
150
|
+
text = text.replace(/[\t\n\f\r ]+/g, ' ')
|
|
151
|
+
if (text.startsWith(' ') && !charRect(node, 0)) text = text.slice(1)
|
|
152
|
+
if (text.endsWith(' ') && (end === 0 || !charRect(node, end))) text = text.slice(0, -1)
|
|
153
|
+
}
|
|
154
|
+
const r = lines[0]
|
|
155
|
+
runs.push({ text, parent, left: r.left, top: r.top, right: r.right, bottom: r.bottom })
|
|
156
|
+
continue
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
let run: TextRun | null = null
|
|
160
|
+
for (let i = 0; i < node.data.length; i++) {
|
|
161
|
+
const r = charRect(node, i)
|
|
162
|
+
if (!r) continue // collapsed white space
|
|
163
|
+
if (!run || Math.abs(r.top - run.top) > 1) {
|
|
164
|
+
run = { text: '', parent, left: r.left, top: r.top, right: r.right, bottom: r.bottom }
|
|
165
|
+
runs.push(run)
|
|
166
|
+
}
|
|
167
|
+
run.text += node.data[i]
|
|
168
|
+
run.right = r.right
|
|
169
|
+
run.bottom = Math.max(run.bottom, r.bottom)
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return runs
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Draw a text run with its parent's font at the laid-out position, stretched
|
|
176
|
+
// to the laid-out width. (x0, y0, zoom) map viewport to canvas coordinates.
|
|
177
|
+
export function fillRun(ctx: CanvasRenderingContext2D, run: TextRun, x0: number, y0: number, zoom: number) {
|
|
178
|
+
const ps = getComputedStyle(run.parent)
|
|
179
|
+
ctx.font = `${ps.fontStyle} ${ps.fontWeight} ${ps.fontSize} ${ps.fontFamily}`
|
|
180
|
+
ctx.letterSpacing = ps.letterSpacing === 'normal' ? '0px' : ps.letterSpacing
|
|
181
|
+
const m = ctx.measureText(run.text)
|
|
182
|
+
ctx.save()
|
|
183
|
+
ctx.translate((run.left - x0) / zoom, (run.top - y0) / zoom + m.fontBoundingBoxAscent)
|
|
184
|
+
if (m.width > 0) ctx.scale((run.right - run.left) / zoom / m.width, 1)
|
|
185
|
+
ctx.fillText(run.text, 0, 0)
|
|
186
|
+
ctx.restore()
|
|
187
|
+
}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
// Border rings and shadows in PDF export.
|
|
2
|
+
//
|
|
3
|
+
// Two kinds of CSS that PDF viewers draw wrongly when Chromium writes them:
|
|
4
|
+
// - Masked rings (.anim-border's light, `mask-composite: exclude`): the mask is
|
|
5
|
+
// dropped, so the whole box is filled with the gradient.
|
|
6
|
+
// - Blurred shadows (box-shadow, text-shadow): macOS Preview / PDFKit drops
|
|
7
|
+
// them and paints a translucent rectangle instead.
|
|
8
|
+
// Both are drawn on canvases instead, which every viewer shows the same.
|
|
9
|
+
|
|
10
|
+
import { colorStops, cornerRadii, fillRun, functionArgs, isTransparent, overlayCanvas, paddingBox, roundRect, splitList, textRuns } from './print-canvas'
|
|
11
|
+
|
|
12
|
+
export const RING_DONE = 'data-print-ring'
|
|
13
|
+
export const BOX_SHADOW_DONE = 'data-print-box-shadow'
|
|
14
|
+
export const TEXT_SHADOW_DONE = 'data-print-text-shadow'
|
|
15
|
+
|
|
16
|
+
// --- Rings ----------------------------------------------------------------
|
|
17
|
+
|
|
18
|
+
// A pseudo-element ring: padding = ring width, a conic gradient background and
|
|
19
|
+
// a mask that keeps only the padding area
|
|
20
|
+
function isMaskedRing(ps: CSSStyleDeclaration) {
|
|
21
|
+
const composite = `${ps.maskComposite} ${ps.webkitMaskComposite}`
|
|
22
|
+
return ps.content !== 'none' && ps.opacity !== '0'
|
|
23
|
+
&& /exclude|xor/.test(composite) && ps.backgroundImage.startsWith('conic-gradient')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function conicGradient(ctx: CanvasRenderingContext2D, image: string, w: number, h: number) {
|
|
27
|
+
const args = functionArgs(image)
|
|
28
|
+
let from = 0
|
|
29
|
+
let cx = w / 2
|
|
30
|
+
let cy = h / 2
|
|
31
|
+
const head = args[0].match(/^(?:from\s+(-?[\d.]+)deg)?\s*(?:at\s+(-?[\d.]+)(%|px)\s+(-?[\d.]+)(%|px))?$/)
|
|
32
|
+
if (head && (head[1] || head[2])) {
|
|
33
|
+
args.shift()
|
|
34
|
+
if (head[1]) from = Number(head[1])
|
|
35
|
+
if (head[2]) {
|
|
36
|
+
cx = head[3] === '%' ? w * Number(head[2]) / 100 : Number(head[2])
|
|
37
|
+
cy = head[5] === '%' ? h * Number(head[4]) / 100 : Number(head[4])
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
// CSS 0deg points up, canvas 0 points right
|
|
41
|
+
const g = ctx.createConicGradient((from - 90) * Math.PI / 180, cx, cy)
|
|
42
|
+
for (const [offset, color] of colorStops(args)) g.addColorStop(offset, color)
|
|
43
|
+
return g
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function redrawRing(el: HTMLElement, which: '::before' | '::after', ps: CSSStyleDeclaration) {
|
|
47
|
+
if (!isMaskedRing(ps)) return
|
|
48
|
+
el.setAttribute(RING_DONE, '')
|
|
49
|
+
const { width: w, height: h } = paddingBox(el)
|
|
50
|
+
if (!w || !h) return
|
|
51
|
+
const width = Number.parseFloat(ps.paddingTop) || 0
|
|
52
|
+
const radii = cornerRadii(ps)
|
|
53
|
+
const made = overlayCanvas('print-ring', w, h)
|
|
54
|
+
if (!made) return
|
|
55
|
+
const { canvas, ctx } = made
|
|
56
|
+
ctx.beginPath()
|
|
57
|
+
roundRect(ctx, 0, 0, w, h, radii)
|
|
58
|
+
roundRect(ctx, width, width, w - 2 * width, h - 2 * width, radii.map(r => r - width))
|
|
59
|
+
ctx.fillStyle = conicGradient(ctx, ps.backgroundImage, w, h)
|
|
60
|
+
ctx.fill('evenodd')
|
|
61
|
+
canvas.style.zIndex = ps.zIndex
|
|
62
|
+
canvas.style.opacity = ps.opacity
|
|
63
|
+
el.classList.add(which === '::before' ? 'print-no-before' : 'print-no-after')
|
|
64
|
+
el.prepend(canvas)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// --- Shadows --------------------------------------------------------------
|
|
68
|
+
|
|
69
|
+
interface Shadow { color: string, x: number, y: number, blur: number, spread: number }
|
|
70
|
+
|
|
71
|
+
// Computed box-shadow / text-shadow: "rgb(…) 0px 10px 25px 0px, …"
|
|
72
|
+
function parseShadows(value: string, withSpread: boolean): Shadow[] {
|
|
73
|
+
if (!value || value === 'none') return []
|
|
74
|
+
return splitList(value).flatMap((item) => {
|
|
75
|
+
if (/\binset\b/.test(item)) return [] // inner shadows are left to the browser
|
|
76
|
+
const m = item.match(/^(.*?)\s+(-?[\d.]+)px\s+(-?[\d.]+)px(?:\s+(-?[\d.]+)px)?(?:\s+(-?[\d.]+)px)?$/)
|
|
77
|
+
if (!m) return []
|
|
78
|
+
return [{ color: m[1], x: Number(m[2]), y: Number(m[3]), blur: Number(m[4] ?? 0), spread: withSpread ? Number(m[5] ?? 0) : 0 }]
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Each shadow gets its own canvas, just big enough for it, under the content
|
|
83
|
+
// of its host: the nearest positioned ancestor with a background (a card, the
|
|
84
|
+
// progress bar), else the slide. A shadow under an opaque card would be hidden
|
|
85
|
+
// there anyway, as in CSS. Blur is soft, so a lower resolution is enough.
|
|
86
|
+
const SHADOW_RESOLUTION = 2
|
|
87
|
+
|
|
88
|
+
// How far a shadow reaches past its shape: a CSS blur radius B is a Gaussian
|
|
89
|
+
// with σ = B/2, which fades out by 3σ
|
|
90
|
+
const reach = (sh: Shadow) => 1.5 * sh.blur + Math.max(0, sh.spread) + 1
|
|
91
|
+
|
|
92
|
+
const prepared = new WeakSet<HTMLElement>()
|
|
93
|
+
|
|
94
|
+
const hasBackground = (s: CSSStyleDeclaration) =>
|
|
95
|
+
s.backgroundImage !== 'none' || !isTransparent(s.backgroundColor)
|
|
96
|
+
|
|
97
|
+
function hostFor(el: HTMLElement) {
|
|
98
|
+
let host = el.parentElement
|
|
99
|
+
while (host && !host.classList.contains('slidev-layout')) {
|
|
100
|
+
const s = getComputedStyle(host)
|
|
101
|
+
if (s.position !== 'static' && hasBackground(s)) break
|
|
102
|
+
host = host.parentElement
|
|
103
|
+
}
|
|
104
|
+
if (!host) return null
|
|
105
|
+
if (!prepared.has(host)) {
|
|
106
|
+
host.style.isolation = 'isolate' // keep the shadows above the host's background
|
|
107
|
+
if (getComputedStyle(host).position === 'static') host.style.position = 'relative'
|
|
108
|
+
prepared.add(host)
|
|
109
|
+
}
|
|
110
|
+
// Measured each time: the export page scrolls between passes
|
|
111
|
+
return { host, box: paddingBox(host) }
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// A canvas for under the host's content covering the host-local rectangle
|
|
115
|
+
// [x0, x1] × [y0, y1]; drawing uses host-local CSS px. The caller adds it to
|
|
116
|
+
// the host once it is drawn.
|
|
117
|
+
function shadowCanvas(x0: number, y0: number, x1: number, y1: number) {
|
|
118
|
+
const made = overlayCanvas('print-shadows', x1 - x0, y1 - y0, x0, y0, SHADOW_RESOLUTION)
|
|
119
|
+
if (!made) return null
|
|
120
|
+
made.canvas.style.zIndex = '-1'
|
|
121
|
+
made.ctx.translate(-x0, -y0)
|
|
122
|
+
return made
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Canvas shadows ignore the transform, so they are given in canvas pixels. The
|
|
126
|
+
// shape is drawn far outside the canvas and only its shadow lands in place.
|
|
127
|
+
const FAR = 100000
|
|
128
|
+
function withShadow(ctx: CanvasRenderingContext2D, res: number, sh: Shadow, draw: () => void) {
|
|
129
|
+
ctx.save()
|
|
130
|
+
ctx.shadowColor = sh.color
|
|
131
|
+
ctx.shadowBlur = sh.blur * res
|
|
132
|
+
ctx.shadowOffsetX = (FAR + sh.x) * res
|
|
133
|
+
ctx.shadowOffsetY = sh.y * res
|
|
134
|
+
ctx.translate(-FAR, 0)
|
|
135
|
+
ctx.fillStyle = '#000'
|
|
136
|
+
draw()
|
|
137
|
+
ctx.restore()
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// The host-local area a set of shadows of a w×h shape at (x, y) can reach
|
|
141
|
+
function shadowBounds(shadows: Shadow[], x: number, y: number, w: number, h: number) {
|
|
142
|
+
return [
|
|
143
|
+
Math.min(...shadows.map(sh => x + sh.x - reach(sh))),
|
|
144
|
+
Math.min(...shadows.map(sh => y + sh.y - reach(sh))),
|
|
145
|
+
Math.max(...shadows.map(sh => x + w + sh.x + reach(sh))),
|
|
146
|
+
Math.max(...shadows.map(sh => y + h + sh.y + reach(sh))),
|
|
147
|
+
] as const
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function redrawBoxShadow(el: HTMLElement, s: CSSStyleDeclaration) {
|
|
151
|
+
if (el.hasAttribute(BOX_SHADOW_DONE)) return
|
|
152
|
+
const shadows = parseShadows(s.boxShadow, true)
|
|
153
|
+
if (!shadows.length || s.visibility === 'hidden' || s.opacity === '0') return
|
|
154
|
+
const r = el.getBoundingClientRect()
|
|
155
|
+
if (!r.width || !r.height) return
|
|
156
|
+
const found = hostFor(el)
|
|
157
|
+
if (!found) return
|
|
158
|
+
el.setAttribute(BOX_SHADOW_DONE, '')
|
|
159
|
+
const { host, box } = found
|
|
160
|
+
const x = (r.left - box.left) / box.zoom
|
|
161
|
+
const y = (r.top - box.top) / box.zoom
|
|
162
|
+
const w = r.width / box.zoom
|
|
163
|
+
const h = r.height / box.zoom
|
|
164
|
+
const radii = cornerRadii(s)
|
|
165
|
+
const made = shadowCanvas(...shadowBounds(shadows, x, y, w, h))
|
|
166
|
+
if (!made) return
|
|
167
|
+
const { canvas, ctx, res } = made
|
|
168
|
+
// The first shadow is on top, so draw the list backwards
|
|
169
|
+
for (const sh of [...shadows].reverse()) {
|
|
170
|
+
withShadow(ctx, res, sh, () => {
|
|
171
|
+
ctx.beginPath()
|
|
172
|
+
roundRect(ctx, x - sh.spread, y - sh.spread, w + 2 * sh.spread, h + 2 * sh.spread, radii.map(c => c + sh.spread))
|
|
173
|
+
ctx.fill()
|
|
174
|
+
})
|
|
175
|
+
}
|
|
176
|
+
// An outer shadow is not painted under its own box
|
|
177
|
+
ctx.globalCompositeOperation = 'destination-out'
|
|
178
|
+
ctx.beginPath()
|
|
179
|
+
roundRect(ctx, x, y, w, h, radii)
|
|
180
|
+
ctx.fill()
|
|
181
|
+
host.prepend(canvas)
|
|
182
|
+
el.style.setProperty('box-shadow', 'none', 'important')
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// text-shadow is inherited: each element whose own text has a shadow gets it
|
|
186
|
+
// drawn, and all values are read before any is turned off (turning one off
|
|
187
|
+
// changes what its children inherit)
|
|
188
|
+
export interface TextShadowJob { el: HTMLElement, shadows: Shadow[] }
|
|
189
|
+
|
|
190
|
+
export function textShadowJob(el: HTMLElement, s: CSSStyleDeclaration): TextShadowJob | null {
|
|
191
|
+
if (el.hasAttribute(TEXT_SHADOW_DONE)) return null
|
|
192
|
+
const shadows = parseShadows(s.textShadow, false)
|
|
193
|
+
if (!shadows.length || s.visibility === 'hidden' || s.opacity === '0') return null
|
|
194
|
+
return { el, shadows }
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export function redrawTextShadows(jobs: TextShadowJob[], onError: (e: unknown) => void) {
|
|
198
|
+
const drawn = jobs.filter(({ el, shadows }) => {
|
|
199
|
+
try {
|
|
200
|
+
return drawTextShadow(el, shadows)
|
|
201
|
+
}
|
|
202
|
+
catch (e) {
|
|
203
|
+
onError(e)
|
|
204
|
+
return false
|
|
205
|
+
}
|
|
206
|
+
})
|
|
207
|
+
for (const { el } of drawn) {
|
|
208
|
+
el.setAttribute(TEXT_SHADOW_DONE, '')
|
|
209
|
+
el.style.setProperty('text-shadow', 'none', 'important')
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function drawTextShadow(el: HTMLElement, shadows: Shadow[]) {
|
|
214
|
+
const runs = textRuns(el, () => true, true)
|
|
215
|
+
const found = runs.length ? hostFor(el) : null
|
|
216
|
+
if (!found) return false
|
|
217
|
+
const { host, box } = found
|
|
218
|
+
const local = (vx: number, vy: number) => [(vx - box.left) / box.zoom, (vy - box.top) / box.zoom]
|
|
219
|
+
const [x, y] = local(Math.min(...runs.map(r => r.left)), Math.min(...runs.map(r => r.top)))
|
|
220
|
+
const [x2, y2] = local(Math.max(...runs.map(r => r.right)), Math.max(...runs.map(r => r.bottom)))
|
|
221
|
+
const made = shadowCanvas(...shadowBounds(shadows, x, y, x2 - x, y2 - y))
|
|
222
|
+
if (!made) return false
|
|
223
|
+
const { canvas, ctx, res } = made
|
|
224
|
+
for (const sh of [...shadows].reverse()) {
|
|
225
|
+
withShadow(ctx, res, sh, () => {
|
|
226
|
+
for (const run of runs) fillRun(ctx, run, box.left, box.top, box.zoom)
|
|
227
|
+
})
|
|
228
|
+
}
|
|
229
|
+
host.prepend(canvas)
|
|
230
|
+
return true
|
|
231
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// PDF/PNG export.
|
|
2
|
+
//
|
|
3
|
+
// html.print-mode marks an export (`slidev export`, the browser export page) and
|
|
4
|
+
// browser printing; the CSS print rules (static animations, hidden timers…)
|
|
5
|
+
// hang off it. `slidev export` renders with screen media, so `@media print`
|
|
6
|
+
// alone would miss it.
|
|
7
|
+
//
|
|
8
|
+
// In an export, effects that PDF viewers draw wrongly are redrawn as images:
|
|
9
|
+
// gradient text (print-gradient-text.ts), masked border rings and blurred
|
|
10
|
+
// shadows (print-effects.ts).
|
|
11
|
+
|
|
12
|
+
import { GRADIENT_DONE, isGradientText, materializePseudo, PSEUDO, redrawGradientText } from './print-gradient-text'
|
|
13
|
+
import type { TextShadowJob } from './print-effects'
|
|
14
|
+
import { redrawBoxShadow, redrawRing, redrawTextShadows, RING_DONE, textShadowJob } from './print-effects'
|
|
15
|
+
import { canRedraw } from './print-canvas'
|
|
16
|
+
import { isPrintMode } from './print-mode'
|
|
17
|
+
|
|
18
|
+
const HOLD_MS = 15000
|
|
19
|
+
const OWN = 'canvas.print-gradient-text, canvas.print-ring, canvas.print-shadows'
|
|
20
|
+
// Slide content and the theme's slide chrome (global-top / global-bottom)
|
|
21
|
+
const SLIDE = '.slidev-layout, .progress-bar, .slide-indicator, .stage-progress'
|
|
22
|
+
|
|
23
|
+
// Slide elements in `root` (the root itself included)
|
|
24
|
+
function slideElements(root: Element | Document) {
|
|
25
|
+
const all = root instanceof Element ? [root, ...root.querySelectorAll('*')] : [...root.querySelectorAll('*')]
|
|
26
|
+
return all.filter(el => el.closest(SLIDE) && !el.matches(OWN)) as HTMLElement[]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Every redraw step only replaces an effect once its image is complete, so a
|
|
30
|
+
// step that fails leaves that effect to the CSS; the others carry on
|
|
31
|
+
let failures = 0
|
|
32
|
+
function attempt(step: () => void) {
|
|
33
|
+
try {
|
|
34
|
+
step()
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
report(e)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function report(e: unknown) {
|
|
41
|
+
if (failures++ < 3) console.warn('[theme] PDF export: an effect is left as CSS:', e)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function redraw(root: Element | Document) {
|
|
45
|
+
// Pseudo-elements: gradient ones become real elements, masked rings images
|
|
46
|
+
for (const el of slideElements(root)) {
|
|
47
|
+
if (el.hasAttribute(PSEUDO) || el.closest(`[${GRADIENT_DONE}]`)) continue
|
|
48
|
+
for (const which of ['::before', '::after'] as const) {
|
|
49
|
+
if (el.classList.contains(which === '::before' ? 'print-no-before' : 'print-no-after')) continue
|
|
50
|
+
const ps = getComputedStyle(el, which)
|
|
51
|
+
if (ps.content === 'none') continue
|
|
52
|
+
attempt(() => materializePseudo(el, which, ps))
|
|
53
|
+
if (!el.hasAttribute(RING_DONE)) attempt(() => redrawRing(el, which, ps))
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// Then each element once: its box shadow, its gradient text, and its text
|
|
57
|
+
// shadow, which is drawn after all are read (see redrawTextShadows)
|
|
58
|
+
const textShadows: TextShadowJob[] = []
|
|
59
|
+
for (const el of slideElements(root)) {
|
|
60
|
+
const s = getComputedStyle(el)
|
|
61
|
+
attempt(() => redrawBoxShadow(el, s))
|
|
62
|
+
const job = textShadowJob(el, s)
|
|
63
|
+
if (job) textShadows.push(job)
|
|
64
|
+
// Document order: an outer gradient element is redrawn with its
|
|
65
|
+
// children's text before the children come up, so they are skipped
|
|
66
|
+
if (isGradientText(s) && !el.closest(`[${GRADIENT_DONE}]`)) attempt(() => redrawGradientText(el, s))
|
|
67
|
+
}
|
|
68
|
+
redrawTextShadows(textShadows, report)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
let started = false
|
|
72
|
+
|
|
73
|
+
function start() {
|
|
74
|
+
started = true
|
|
75
|
+
// `slidev export` waits for .slidev-slide-loading to be removed before it
|
|
76
|
+
// captures, so hold the capture until the slides have settled and been redrawn
|
|
77
|
+
// (at most HOLD_MS: a font that never loads must not stall the export)
|
|
78
|
+
const hold = document.createElement('div')
|
|
79
|
+
hold.className = 'slidev-slide-loading'
|
|
80
|
+
hold.style.display = 'none'
|
|
81
|
+
document.body.appendChild(hold)
|
|
82
|
+
setTimeout(() => hold.remove(), HOLD_MS)
|
|
83
|
+
|
|
84
|
+
// A full pass once the slides have settled, then only what is added later
|
|
85
|
+
// (e.g. another range on the browser export page)
|
|
86
|
+
let pending: Set<Element> | null = null
|
|
87
|
+
let timer: number | undefined
|
|
88
|
+
const schedule = () => {
|
|
89
|
+
clearTimeout(timer)
|
|
90
|
+
timer = window.setTimeout(async () => {
|
|
91
|
+
try {
|
|
92
|
+
await document.fonts.ready
|
|
93
|
+
if (!isPrintMode() || !canRedraw()) return
|
|
94
|
+
if (pending === null) {
|
|
95
|
+
pending = new Set()
|
|
96
|
+
redraw(document)
|
|
97
|
+
return
|
|
98
|
+
}
|
|
99
|
+
const roots = [...pending].filter(r => r.isConnected)
|
|
100
|
+
pending.clear()
|
|
101
|
+
for (const r of roots) {
|
|
102
|
+
if (!roots.some(o => o !== r && o.contains(r))) redraw(r)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
catch (e) {
|
|
106
|
+
report(e)
|
|
107
|
+
}
|
|
108
|
+
finally {
|
|
109
|
+
hold.remove()
|
|
110
|
+
}
|
|
111
|
+
}, 300)
|
|
112
|
+
}
|
|
113
|
+
new MutationObserver((mutations) => {
|
|
114
|
+
let added = false
|
|
115
|
+
for (const m of mutations) {
|
|
116
|
+
m.addedNodes.forEach((n) => {
|
|
117
|
+
if (!(n instanceof Element) || n.matches(OWN) || n.hasAttribute(PSEUDO)) return
|
|
118
|
+
pending?.add(n)
|
|
119
|
+
added = true
|
|
120
|
+
})
|
|
121
|
+
}
|
|
122
|
+
if (added) schedule()
|
|
123
|
+
}).observe(document.body, { childList: true, subtree: true })
|
|
124
|
+
schedule()
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function setupPrintExport(router?: { afterEach: (hook: () => void) => unknown }) {
|
|
128
|
+
const sync = () => {
|
|
129
|
+
const on = isPrintMode()
|
|
130
|
+
document.documentElement.classList.toggle('print-mode', on)
|
|
131
|
+
if (on && !started) start()
|
|
132
|
+
}
|
|
133
|
+
sync()
|
|
134
|
+
router?.afterEach(() => setTimeout(sync))
|
|
135
|
+
|
|
136
|
+
// Browser printing of the presentation itself: the CSS print rules only
|
|
137
|
+
window.addEventListener('beforeprint', () => document.documentElement.classList.add('print-mode'))
|
|
138
|
+
window.addEventListener('afterprint', sync)
|
|
139
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// Gradient text (CSS `background-clip: text`) in PDF export.
|
|
2
|
+
//
|
|
3
|
+
// Chromium writes it into a PDF as text used as a clip, and PDF viewers
|
|
4
|
+
// disagree on that: macOS Preview / PDFKit paint the whole gradient box or
|
|
5
|
+
// black text, poppler adds hairline box edges; SVG gradient text fails in
|
|
6
|
+
// other viewers. Each gradient-text element instead gets a canvas with its text
|
|
7
|
+
// painted in its gradient, laid over the original, which stays in place
|
|
8
|
+
// (invisible) for layout and text search.
|
|
9
|
+
|
|
10
|
+
import { colorStops, fillRun, functionArgs, isTransparent, overlayCanvas, paddingBox, textRuns } from './print-canvas'
|
|
11
|
+
|
|
12
|
+
export const GRADIENT_DONE = 'data-print-gradient'
|
|
13
|
+
export const PSEUDO = 'data-print-pseudo'
|
|
14
|
+
|
|
15
|
+
export const isGradientText = (s: CSSStyleDeclaration) =>
|
|
16
|
+
s.backgroundClip === 'text' && s.backgroundImage.startsWith('linear-gradient')
|
|
17
|
+
&& isTransparent(s.webkitTextFillColor)
|
|
18
|
+
|
|
19
|
+
// A gradient ::before / ::after (e.g. the quote layout's closing mark) can't be
|
|
20
|
+
// measured or covered, so it is replaced by a real span with the same computed
|
|
21
|
+
// style and text, which is then redrawn like any other element
|
|
22
|
+
export function materializePseudo(el: Element, which: '::before' | '::after', ps: CSSStyleDeclaration) {
|
|
23
|
+
const m = ps.content.match(/^"(.*)"$/s)
|
|
24
|
+
if (!m || !isGradientText(ps)) return
|
|
25
|
+
const span = document.createElement('span')
|
|
26
|
+
for (const prop of ps) {
|
|
27
|
+
if (prop !== 'content') span.style.setProperty(prop, ps.getPropertyValue(prop))
|
|
28
|
+
}
|
|
29
|
+
span.textContent = m[1].replace(/\\(.)/g, '$1')
|
|
30
|
+
span.setAttribute('aria-hidden', 'true')
|
|
31
|
+
span.setAttribute(PSEUDO, '')
|
|
32
|
+
if (which === '::before') el.prepend(span)
|
|
33
|
+
else el.append(span)
|
|
34
|
+
el.classList.add(which === '::before' ? 'print-no-before' : 'print-no-after')
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const SIDES: Record<string, number> = {
|
|
38
|
+
'to top': 0, 'to right': 90, 'to bottom': 180, 'to left': 270,
|
|
39
|
+
'to top right': 45, 'to right top': 45, 'to bottom right': 135, 'to right bottom': 135,
|
|
40
|
+
'to bottom left': 225, 'to left bottom': 225, 'to top left': 315, 'to left top': 315,
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// A canvas gradient matching CSS linear-gradient(...) over a w×h box at (x, 0)
|
|
44
|
+
// (CSS gradient-line geometry: the line passes through the box center and its
|
|
45
|
+
// length makes the corners hit 0% and 100%)
|
|
46
|
+
function linearGradient(ctx: CanvasRenderingContext2D, image: string, x: number, w: number, h: number) {
|
|
47
|
+
const args = functionArgs(image)
|
|
48
|
+
let angle = 180
|
|
49
|
+
if (/^-?[\d.]+(deg|turn|rad)$/.test(args[0])) {
|
|
50
|
+
const v = Number.parseFloat(args[0])
|
|
51
|
+
angle = args[0].endsWith('turn') ? v * 360 : args[0].endsWith('rad') ? v * 180 / Math.PI : v
|
|
52
|
+
args.shift()
|
|
53
|
+
}
|
|
54
|
+
else if (args[0] in SIDES) {
|
|
55
|
+
angle = SIDES[args.shift()!]
|
|
56
|
+
}
|
|
57
|
+
const rad = angle * Math.PI / 180
|
|
58
|
+
const dx = Math.sin(rad)
|
|
59
|
+
const dy = -Math.cos(rad)
|
|
60
|
+
const len = Math.abs(w * dx) + Math.abs(h * dy)
|
|
61
|
+
const cx = x + w / 2
|
|
62
|
+
const cy = h / 2
|
|
63
|
+
const g = ctx.createLinearGradient(cx - dx * len / 2, cy - dy * len / 2, cx + dx * len / 2, cy + dy * len / 2)
|
|
64
|
+
for (const [offset, color] of colorStops(args, len)) g.addColorStop(offset, color)
|
|
65
|
+
return g
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// One scratch canvas for the gradient fill, reused by every element
|
|
69
|
+
let paint: HTMLCanvasElement | null = null
|
|
70
|
+
|
|
71
|
+
// Room around the text boxes for glyph overhang (italics, swashes)
|
|
72
|
+
const OVERHANG = 6
|
|
73
|
+
|
|
74
|
+
export function redrawGradientText(el: HTMLElement, s: CSSStyleDeclaration) {
|
|
75
|
+
el.setAttribute(GRADIENT_DONE, '')
|
|
76
|
+
const box = paddingBox(el)
|
|
77
|
+
const { width: w, height: h, zoom } = box
|
|
78
|
+
const runs = textRuns(el, ps => isTransparent(ps.webkitTextFillColor))
|
|
79
|
+
if (!w || !h || !runs.length) return
|
|
80
|
+
|
|
81
|
+
// The canvas covers only the text (in the element's coordinates); the
|
|
82
|
+
// gradient is still laid out over the whole element
|
|
83
|
+
const x = Math.max(-OVERHANG, Math.min(...runs.map(r => (r.left - box.left) / zoom)) - OVERHANG)
|
|
84
|
+
const y = Math.max(-OVERHANG, Math.min(...runs.map(r => (r.top - box.top) / zoom)) - OVERHANG)
|
|
85
|
+
const cw = Math.max(...runs.map(r => (r.right - box.left) / zoom)) + OVERHANG - x
|
|
86
|
+
const ch = Math.max(...runs.map(r => (r.bottom - box.top) / zoom)) + OVERHANG - y
|
|
87
|
+
const made = overlayCanvas('print-gradient-text', cw, ch, x, y)
|
|
88
|
+
if (!made) return
|
|
89
|
+
const { canvas, ctx, res } = made
|
|
90
|
+
ctx.translate(-x, -y)
|
|
91
|
+
|
|
92
|
+
// 1. The text, in any solid color, at the positions the browser laid it out
|
|
93
|
+
for (const run of runs) fillRun(ctx, run, box.left, box.top, zoom)
|
|
94
|
+
|
|
95
|
+
// 2. The gradient (tiled like the background), kept only where the text is.
|
|
96
|
+
// Painted on the scratch canvas first: with 'source-in' every fill would
|
|
97
|
+
// clear what the previous tile left.
|
|
98
|
+
paint ??= document.createElement('canvas')
|
|
99
|
+
paint.width = canvas.width // also clears it
|
|
100
|
+
paint.height = canvas.height
|
|
101
|
+
const pctx = paint.getContext('2d')
|
|
102
|
+
if (!pctx) return
|
|
103
|
+
pctx.scale(res, res)
|
|
104
|
+
pctx.translate(-x, -y)
|
|
105
|
+
const size = s.backgroundSize.split(' ')[0]
|
|
106
|
+
const tile = size.endsWith('px') ? Number.parseFloat(size) : w
|
|
107
|
+
const posX = s.backgroundPositionX
|
|
108
|
+
const pos = posX.endsWith('%') ? (w - tile) * Number.parseFloat(posX) / 100 : Number.parseFloat(posX) || 0
|
|
109
|
+
const offset = pos % tile
|
|
110
|
+
for (let tx = offset > 0 ? offset - tile : offset; tx < w; tx += tile) {
|
|
111
|
+
if (tx + tile < x || tx > x + cw) continue // outside the canvas
|
|
112
|
+
pctx.fillStyle = linearGradient(pctx, s.backgroundImage, tx, tile, h)
|
|
113
|
+
pctx.fillRect(tx, 0, tile, h)
|
|
114
|
+
}
|
|
115
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0)
|
|
116
|
+
ctx.globalCompositeOperation = 'source-in'
|
|
117
|
+
ctx.drawImage(paint, 0, 0)
|
|
118
|
+
|
|
119
|
+
// Only now that the image is complete: keep the original for layout and
|
|
120
|
+
// text selection, hide its paint
|
|
121
|
+
if (s.position === 'static') el.style.position = 'relative'
|
|
122
|
+
el.style.setProperty('background', 'none', 'important')
|
|
123
|
+
el.style.setProperty('-webkit-text-fill-color', 'transparent', 'important')
|
|
124
|
+
el.appendChild(canvas)
|
|
125
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
// PDF/PNG export: `slidev export` loads slides with ?print, the browser export
|
|
2
|
+
// page is /export. (useNav().isPrintMode is the same test, for components.)
|
|
3
|
+
export const isPrintMode = () =>
|
|
4
|
+
new URLSearchParams(location.search).has('print') || /\/export\b/.test(location.pathname)
|