-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
NoteEditor.vue
87 lines (77 loc) · 1.77 KB
/
NoteEditor.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<script setup lang="ts">
import { ignorableWatch, onClickOutside } from '@vueuse/core'
import { nextTick, ref, watch } from 'vue'
import { currentSlideId } from '../logic/nav'
import { useDynamicSlideInfo } from '../logic/note'
const props = defineProps({
class: {
default: '',
},
placeholder: {
default: 'No notes for this slide',
},
})
const { info, update } = useDynamicSlideInfo(currentSlideId)
const note = ref('')
let timer: any
const { ignoreUpdates } = ignorableWatch(
note,
(v) => {
const id = currentSlideId.value
clearTimeout(timer)
timer = setTimeout(() => {
update({ raw: null!, note: v }, id)
}, 500)
},
)
watch(
info,
(v) => {
clearTimeout(timer)
ignoreUpdates(() => {
note.value = v?.note || ''
})
},
{ immediate: true, flush: 'sync' },
)
const input = ref<HTMLTextAreaElement>()
const editing = ref(false)
async function switchNoteEdit(e: MouseEvent) {
if ((e?.target as HTMLElement)?.tagName === 'A')
return
editing.value = true
input.value?.focus()
await nextTick()
input.value?.focus()
}
onClickOutside(input, () => {
editing.value = false
})
</script>
<template>
<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-else
ref="input"
v-model="note"
class="prose resize-none overflow-auto outline-none bg-transparent block"
:class="props.class"
:placeholder="placeholder"
@focus="editing = true"
/>
</template>