Skip to content

Commit

Permalink
fix: slide note sync (#1157) (#1287)
Browse files Browse the repository at this point in the history
  • Loading branch information
KermanX committed Feb 12, 2024
1 parent 1fe6120 commit 6bf284c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/client/logic/note.ts
Expand Up @@ -40,6 +40,10 @@ export function useSlideInfo(id: number | undefined): UseSlideInfo {
if (payload.id === id)
info.value = payload.data
})
import.meta.hot?.on('slidev-update-note', (payload) => {
if (payload.id === id && info.value.note.trim() !== payload.note.trim())
info.value = { ...info.value, ...payload }
})
}

return {
Expand Down
21 changes: 18 additions & 3 deletions packages/slidev/node/plugins/loaders.ts
Expand Up @@ -115,7 +115,11 @@ export function createSlidesLoader(
if (type === 'json' && req.method === 'POST') {
const body = await getBodyJson(req)
const slide = data.slides[idx]
hmrPages.add(idx)

const onlyNoteChanged = Object.keys(body).length === 2
&& 'note' in body && body.raw === null
if (!onlyNoteChanged)
hmrPages.add(idx)

if (slide.source) {
Object.assign(slide.source, body)
Expand Down Expand Up @@ -173,7 +177,6 @@ export function createSlidesLoader(
if (
a?.content.trim() === b?.content.trim()
&& a?.title?.trim() === b?.title?.trim()
&& a?.note === b?.note
&& equal(a.frontmatter, b.frontmatter)
&& Object.entries(a.snippetsUsed ?? {}).every(([file, oldContent]) => {
try {
Expand All @@ -184,8 +187,20 @@ export function createSlidesLoader(
return false
}
})
)
) {
if (a?.note !== b?.note) {
ctx.server.ws.send({
type: 'custom',
event: 'slidev-update-note',
data: {
id: i,
note: b!.note || '',
noteHTML: md.render(b!.note || ''),
},
})
}
continue
}

ctx.server.ws.send({
type: 'custom',
Expand Down

0 comments on commit 6bf284c

Please sign in to comment.