-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
NoteEditor.vue
122 lines (110 loc) · 2.53 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<script setup lang="ts">
import type { PropType } from 'vue'
import { nextTick, ref, watch, watchEffect } from 'vue'
import { ignorableWatch, onClickOutside, useVModel } from '@vueuse/core'
import type { ClicksContext } from '@slidev/types'
import { useDynamicSlideInfo } from '../logic/note'
import NoteDisplay from './NoteDisplay.vue'
const props = defineProps({
no: {
type: Number,
},
class: {
default: '',
},
editing: {
default: false,
},
style: {
default: () => ({}),
},
placeholder: {
default: 'No notes for this slide',
},
clicksContext: {
type: Object as PropType<ClicksContext>,
},
autoHeight: {
default: false,
},
})
const emit = defineEmits([
'update:editing',
])
const editing = useVModel(props, 'editing', emit, { passive: true })
const { info, update } = useDynamicSlideInfo(props.no)
const note = ref('')
let timer: any
const { ignoreUpdates } = ignorableWatch(
note,
(v) => {
const id = props.no
clearTimeout(timer)
timer = setTimeout(() => {
update({ note: v }, id)
}, 500)
},
)
watch(
info,
(v) => {
if (editing.value)
return
clearTimeout(timer)
ignoreUpdates(() => {
note.value = v?.note || ''
})
},
{ immediate: true, flush: 'sync' },
)
const input = ref<HTMLTextAreaElement>()
watchEffect(() => {
if (editing.value)
input.value?.focus()
})
onClickOutside(input, () => {
editing.value = false
})
function calculateHeight() {
if (!props.autoHeight || !input.value || !editing.value)
return
if (input.value.scrollHeight > input.value.clientHeight)
input.value.style.height = `${input.value.scrollHeight}px`
}
const inputHeight = ref<number | null>()
watchEffect(() => {
calculateHeight()
})
watch(
note,
() => {
nextTick(() => {
calculateHeight()
})
},
{ flush: 'post' },
)
</script>
<template>
<NoteDisplay
v-if="!editing"
class="my--4 border-transparent border-2"
:class="[props.class, note ? '' : 'opacity-25 italic select-none']"
:style="props.style"
:note="note || placeholder"
:note-html="info?.noteHTML"
:clicks-context="clicksContext"
/>
<textarea
v-else
ref="input"
v-model="note"
class="prose resize-none overflow-auto outline-none bg-transparent block border-primary border-2"
style="line-height: 1.75;"
:style="[props.style, inputHeight != null ? { height: `${inputHeight}px` } : {}]"
:class="props.class"
:placeholder="placeholder"
@keydown.esc=" editing = false"
@focus="editing = true"
/>
</template>