Skip to content

Commit 9aa2fb6

Browse files
committed
✨ add preload prop to warm up VS Code session before slide is visited
1 parent 9343551 commit 9aa2fb6

4 files changed

Lines changed: 26 additions & 7 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ Use `disableInitialFocus` to prevent VS Code from stealing keyboard focus when y
8282

8383
Focus is held on the slide for 5 seconds after VS Code loads, then released normally. The user can interact with VS Code freely after that.
8484

85+
## ⚡ Preload
86+
87+
Use `preload` to warm up the VS Code session before the user reaches the slide. Slidev mounts adjacent slides in the background, so the session can start while the user is still on the previous slide:
88+
89+
```md
90+
<Editor session="demo" preload />
91+
```
92+
93+
Combine with `disableInitialFocus` for a seamless experience — the IDE is ready and keyboard navigation still works on slide entry.
94+
8595
## 🔒 Keep the session alive across navigation
8696

8797
By default, navigating away from a slide stops the session. Use `persist` to keep it running:
@@ -124,6 +134,7 @@ Per-component props override these values.
124134
| `hideStatusBar` | `boolean` | `false` | Hide the VS Code status bar (bottom bar). |
125135
| `height` | `string` | `100%` | CSS height of the editor container. |
126136
| `persist` | `boolean` | `false` | Keep the session alive when navigating away. |
137+
| `preload` | `boolean` | `false` | Start the session while the slide is not yet active (requires Slidev preload). |
127138
| `port` | `number` | auto | Force a specific port for this session. |
128139
| `startTimeout` | `number` | `30000` | Max startup time in ms before the session is marked as failed. |
129140
| `zoom` | `number` | `1` | Scale factor for the VS Code UI (e.g. `0.8` for 80%). |

components/Editor.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const props = withDefaults(defineProps<EditorProps>(), {
1414
hideMinimap: false,
1515
hideStatusBar: false,
1616
persist: false,
17+
preload: false,
1718
zoom: 1,
1819
})
1920
@@ -37,6 +38,7 @@ const attrs = useAttrs()
3738
const isDisableInitialFocus = computed(() =>
3839
!!props.disableInitialFocus || 'disableInitialFocus' in attrs || 'disable-initial-focus' in attrs,
3940
)
41+
const isPreload = computed(() => !!props.preload || 'preload' in attrs)
4042
4143
const session = shallowRef<SessionEntry | null>(registry.get(sessionId) ?? null)
4244
@@ -94,17 +96,22 @@ async function retry(): Promise<void> {
9496
9597
const isActive = useIsSlideActive()
9698
99+
let hasBeenActive = false
100+
97101
watch(
98102
isActive,
99103
async (active) => {
100104
if (active) {
105+
hasBeenActive = true
101106
const existing = registry!.get(sessionId)
102107
if (existing) {
103108
session.value = existing
104109
return
105110
}
106111
await start()
107-
} else if (!props.persist) {
112+
} else if (isPreload.value && !hasBeenActive && !registry!.get(sessionId)) {
113+
await start()
114+
} else if (!props.persist && hasBeenActive) {
108115
requestStop(sessionId)
109116
registry!.teardown(sessionId)
110117
session.value = null

showcase/slides.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ livecode:
4848
Add `<Editor />` to any slide. Use `session` to identify it and `defaultFolder` to set the workspace.
4949

5050
<div class="flex-1 mt-2 rounded-xl overflow-hidden border border-gray-200">
51-
<Editor session="basic" />
51+
<Editor session="basic" preload disableInitialFocus />
5252
</div>
5353

5454
---
@@ -58,7 +58,7 @@ Add `<Editor />` to any slide. Use `session` to identify it and `defaultFolder`
5858
Use `persist` to keep the session alive when navigating away — state is preserved when you come back.
5959

6060
<div class="flex-1 mt-2 rounded-xl overflow-hidden border border-gray-200">
61-
<Editor session="persistent" persist />
61+
<Editor session="persistent" persist preload disableInitialFocus />
6262
</div>
6363

6464
---
@@ -68,7 +68,7 @@ Use `persist` to keep the session alive when navigating away — state is preser
6868
Use `:zoom` to scale down VS Code for a better fit. Can also be set globally in the frontmatter with `livecode.zoom`.
6969

7070
<div class="flex-1 mt-2 rounded-xl overflow-hidden border border-gray-200">
71-
<Editor session="zoomed" :zoom="0.7" />
71+
<Editor session="zoomed" :zoom="0.7" preload disableInitialFocus />
7272
</div>
7373

7474
---
@@ -78,7 +78,7 @@ Use `:zoom` to scale down VS Code for a better fit. Can also be set globally in
7878
Use `colorScheme` to force a dark or light theme. Follows Slidev's `colorSchema` automatically if not set.
7979

8080
<div class="flex-1 mt-2 rounded-xl overflow-hidden border border-gray-200">
81-
<Editor session="dark-theme" colorScheme="dark" />
81+
<Editor session="dark-theme" colorScheme="dark" preload disableInitialFocus />
8282
</div>
8383

8484
---
@@ -88,7 +88,7 @@ Use `colorScheme` to force a dark or light theme. Follows Slidev's `colorSchema`
8888
Use `fontSize`, `hideMinimap`, `hideActivityBar` and `hideStatusBar` to clean up the UI for a focused demo.
8989

9090
<div class="flex-1 mt-2 rounded-xl overflow-hidden border border-gray-200">
91-
<Editor session="presentation" :fontSize="18" hideMinimap hideActivityBar hideStatusBar />
91+
<Editor session="presentation" :fontSize="18" hideMinimap hideActivityBar hideStatusBar preload disableInitialFocus />
9292
</div>
9393

9494
---
@@ -98,5 +98,5 @@ Use `fontSize`, `hideMinimap`, `hideActivityBar` and `hideStatusBar` to clean up
9898
Use `disableInitialFocus` to prevent VS Code from stealing keyboard focus on slide entry — arrow keys keep working.
9999

100100
<div class="flex-1 mt-2 rounded-xl overflow-hidden border border-gray-200">
101-
<Editor session="focus-guard" disableInitialFocus />
101+
<Editor session="focus-guard" disableInitialFocus preload />
102102
</div>

types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface EditorProps {
1111
hideStatusBar?: boolean
1212
persist?: boolean
1313
port?: number
14+
preload?: boolean
1415
session?: string
1516
startTimeout?: number
1617
zoom?: number

0 commit comments

Comments
 (0)