forked from slidevjs/slidev
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support snapshoting overview (slidevjs#1843)
Co-authored-by: _Kerman <[email protected]>
- Loading branch information
Showing
19 changed files
with
305 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,4 +81,5 @@ export const HEADMATTER_FIELDS = [ | |
'mdc', | ||
'contextMenu', | ||
'wakeLock', | ||
'overviewSnapshots', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { snapshotState } from '../state/snapshot' | ||
import { getSlide } from './slides' | ||
|
||
export class SlideSnapshotManager { | ||
private _capturePromises = new Map<number, Promise<void>>() | ||
|
||
getSnapshot(slideNo: number) { | ||
const data = snapshotState.state[slideNo] | ||
if (!data) { | ||
return | ||
} | ||
const slide = getSlide(slideNo) | ||
if (!slide) { | ||
return | ||
} | ||
if (data?.revision === slide?.meta.slide.revision) { | ||
return data.image | ||
} | ||
} | ||
|
||
async captureSnapshot(slideNo: number, el: HTMLElement, delay = 1000) { | ||
if (!__DEV__) | ||
return | ||
if (this.getSnapshot(slideNo)) { | ||
return | ||
} | ||
if (this._capturePromises.has(slideNo)) { | ||
await this._capturePromises.get(slideNo) | ||
} | ||
const promise = this._captureSnapshot(slideNo, el, delay) | ||
.finally(() => { | ||
this._capturePromises.delete(slideNo) | ||
}) | ||
this._capturePromises.set(slideNo, promise) | ||
await promise | ||
} | ||
|
||
private async _captureSnapshot(slideNo: number, el: HTMLElement, delay: number) { | ||
if (!__DEV__) | ||
return | ||
const slide = getSlide(slideNo) | ||
if (!slide) | ||
return | ||
|
||
const revision = slide.meta.slide.revision | ||
|
||
// Retry until the slide is loaded | ||
let retries = 100 | ||
while (retries-- > 0) { | ||
if (!el.querySelector('.slidev-slide-loading')) | ||
break | ||
await new Promise(r => setTimeout(r, 100)) | ||
} | ||
|
||
// Artificial delay for the content to be loaded | ||
await new Promise(r => setTimeout(r, delay)) | ||
|
||
// Capture the snapshot | ||
const toImage = await import('html-to-image') | ||
try { | ||
const dataUrl = await toImage.toPng(el, { | ||
width: el.offsetWidth, | ||
height: el.offsetHeight, | ||
skipFonts: true, | ||
cacheBust: true, | ||
pixelRatio: 1.5, | ||
}) | ||
if (revision !== slide.meta.slide.revision) { | ||
// eslint-disable-next-line no-console | ||
console.info('[Slidev] Slide', slideNo, 'changed, discarding the snapshot') | ||
return | ||
} | ||
snapshotState.patch(slideNo, { | ||
revision, | ||
image: dataUrl, | ||
}) | ||
// eslint-disable-next-line no-console | ||
console.info('[Slidev] Snapshot captured for slide', slideNo) | ||
} | ||
catch (e) { | ||
console.error('[Slidev] Failed to capture snapshot for slide', slideNo, e) | ||
} | ||
} | ||
} | ||
|
||
export const snapshotManager = new SlideSnapshotManager() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import serverSnapshotState from 'server-reactive:snapshots?diff' | ||
import { createSyncState } from './syncState' | ||
|
||
export type SnapshotState = Record<number, { | ||
revision: string | ||
image: string | ||
}> | ||
|
||
export const snapshotState = createSyncState<SnapshotState>( | ||
serverSnapshotState, | ||
serverSnapshotState, | ||
true, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { dirname, join, resolve } from 'node:path' | ||
import fs from 'fs-extra' | ||
import type { ResolvedSlidevOptions } from '@slidev/types' | ||
|
||
function resolveSnapshotsDir(options: ResolvedSlidevOptions): string { | ||
return resolve(dirname(options.entry), '.slidev/snapshots') | ||
} | ||
|
||
export async function loadSnapshots(options: ResolvedSlidevOptions) { | ||
const dir = resolveSnapshotsDir(options) | ||
const file = join(dir, 'snapshots.json') | ||
if (!dir || !fs.existsSync(file)) | ||
return {} | ||
|
||
return JSON.parse(await fs.readFile(file, 'utf8')) | ||
} | ||
|
||
export async function writeSnapshots(options: ResolvedSlidevOptions, data: Record<string, any>) { | ||
const dir = resolveSnapshotsDir(options) | ||
if (!dir) | ||
return | ||
|
||
await fs.ensureDir(dir) | ||
// TODO: write as each image file | ||
await fs.writeFile(join(dir, 'snapshots.json'), JSON.stringify(data, null, 2), 'utf-8') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.