Skip to content

Commit

Permalink
fix: slide patching (#1327)
Browse files Browse the repository at this point in the history
  • Loading branch information
KermanX authored Feb 22, 2024
1 parent ab1b35b commit e05a716
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
8 changes: 4 additions & 4 deletions packages/client/logic/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import type { MaybeRef } from '@vueuse/core'
import { useFetch } from '@vueuse/core'
import type { Ref } from 'vue'
import { computed, ref, unref } from 'vue'
import type { SlideInfo } from '@slidev/types'
import type { SlideInfo, SlidePatch } from '@slidev/types'

export interface UseSlideInfo {
info: Ref<SlideInfo | undefined>
update: (data: Partial<SlideInfo>) => Promise<SlideInfo | void>
update: (data: SlidePatch) => Promise<SlideInfo | void>
}

export function useSlideInfo(id: number | undefined): UseSlideInfo {
Expand All @@ -21,7 +21,7 @@ export function useSlideInfo(id: number | undefined): UseSlideInfo {

execute()

const update = async (data: Partial<SlideInfo>) => {
const update = async (data: SlidePatch) => {
return await fetch(
url,
{
Expand Down Expand Up @@ -64,7 +64,7 @@ export function useDynamicSlideInfo(id: MaybeRef<number | undefined>) {

return {
info: computed(() => get(unref(id)).info.value),
update: async (data: Partial<SlideInfo>, newId?: number) => {
update: async (data: SlidePatch, newId?: number) => {
const info = get(newId ?? unref(id))
const newData = await info.update(data)
if (newData)
Expand Down
8 changes: 3 additions & 5 deletions packages/slidev/node/plugins/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { bold, gray, red, yellow } from 'kolorist'

// @ts-expect-error missing types
import mila from 'markdown-it-link-attributes'
import type { SlideInfo } from '@slidev/types'
import type { SlideInfo, SlidePatch } from '@slidev/types'
import * as parser from '@slidev/parser/fs'
import equal from 'fast-deep-equal'

Expand Down Expand Up @@ -106,12 +106,10 @@ export function createSlidesLoader(
return res.end()
}
if (type === 'json' && req.method === 'POST') {
const body = await getBodyJson(req)
const body: SlidePatch = await getBodyJson(req)
const slide = data.slides[idx]

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

Object.assign(slide.source, body)
Expand Down
5 changes: 5 additions & 0 deletions packages/types/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export interface SlideInfo extends SlideInfoBase {
noteHTML?: string
}

/**
* Editable fields for a slide
*/
export type SlidePatch = Partial<Pick<SlideInfoBase, 'content' | 'note'>>

/**
* Metadata for "slidev" field in themes' package.json
*/
Expand Down

0 comments on commit e05a716

Please sign in to comment.