Skip to content

Commit

Permalink
fix: improve note editor
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Sep 26, 2021
1 parent e7d33d8 commit 184290c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
25 changes: 17 additions & 8 deletions packages/client/internals/NoteEditor.vue
Expand Up @@ -59,15 +59,24 @@ onClickOutside(input, () => {
</script>

<template>
<div
v-show="!editing && note"
class="prose overflow-auto outline-none"
:class="props.class"
@click="switchNoteEdit"
v-html="info?.notesHTML"
/>
<template v-if="!editing && note">
<div
v-if="info?.notesHTML"
class="prose overflow-auto outline-none"
:class="props.class"
@click="switchNoteEdit"
v-html="info?.notesHTML"
></div>
<div
v-else
class="prose overflow-auto outline-none"
:class="props.class"
@click="switchNoteEdit"
v-text="note"
></div>
</template>
<textarea
v-show="editing || !note"
v-else
ref="input"
v-model="note"
class="prose resize-none overflow-auto outline-none bg-transparent block"
Expand Down
20 changes: 13 additions & 7 deletions packages/client/logic/note.ts
Expand Up @@ -4,7 +4,7 @@ import type { SlideInfo, SlideInfoExtended } from '@slidev/types'

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

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

const update = async(data: Partial<SlideInfo>) => {
await fetch(
return await fetch(
url,
{
method: 'POST',
Expand All @@ -30,12 +30,12 @@ export function useSlideInfo(id: number | undefined): UseSlideInfo {
},
body: JSON.stringify(data),
},
)
).then(r => r.json())
}

import.meta.hot?.on('slidev-update', (playload) => {
if (playload.id === id)
info.value = playload.data
import.meta.hot?.on('slidev-update', (payload) => {
if (payload.id === id)
info.value = payload.data
})

return {
Expand All @@ -56,6 +56,12 @@ export function useDynamicSlideInfo(id: MaybeRef<number | undefined>) {

return {
info: computed(() => get(unref(id)).info.value),
update: (data: Partial<SlideInfo>, newId?: number) => get(newId ?? unref(id)).update(data),
update: async(data: Partial<SlideInfo>, newId?: number) => {
const info = get(newId ?? unref(id))
const newData = await info.update(data)
if (newData)
info.info.value = newData
return newData
},
}
}
10 changes: 6 additions & 4 deletions packages/slidev/node/plugins/loaders.ts
Expand Up @@ -112,6 +112,7 @@ export function createSlidesLoader(
}

res.statusCode = 200
res.write(JSON.stringify(prepareSlideInfo(slide)))
return res.end()
}

Expand Down Expand Up @@ -149,13 +150,14 @@ export function createSlidesLoader(
const length = Math.max(data.slides.length, newData.slides.length)

for (let i = 0; i < length; i++) {
if (hmrPages.has(i))
continue

const a = data.slides[i]
const b = newData.slides[i]

if (a?.content.trim() === b?.content.trim() && equal(a.frontmatter, b.frontmatter))
if (
a?.content.trim() === b?.content.trim()
&& a?.note === b?.note
&& equal(a.frontmatter, b.frontmatter)
)
continue

ctx.server.ws.send({
Expand Down

0 comments on commit 184290c

Please sign in to comment.