| 1 | <script setup lang="ts"> |
| 2 | import { throttledWatch, useEventListener } from '@vueuse/core' |
| 3 | import { computed, ref, watch } from 'vue' |
| 4 | import { useNav } from '../composables/useNav' |
| 5 | import { useDynamicSlideInfo } from '../composables/useSlideInfo' |
| 6 | import { parseSideEditorContent } from '../logic/sideEditor' |
| 7 | import { activeElement, editorHeight, editorWidth, isInputting, showEditor, isEditorVertical as vertical } from '../state' |
| 8 | import IconButton from './IconButton.vue' |
| 9 | import ShikiEditor from './ShikiEditor.vue' |
| 10 | |
| 11 | const props = defineProps<{ |
| 12 | resize?: boolean |
| 13 | }>() |
| 14 | |
| 15 | const { currentSlideNo, openInEditor } = useNav() |
| 16 | |
| 17 | const tab = ref<'content' | 'note'>('content') |
| 18 | const content = ref('') |
| 19 | const note = ref('') |
| 20 | const dirty = ref(false) |
| 21 | |
| 22 | const { info, update } = useDynamicSlideInfo(currentSlideNo) |
| 23 | |
| 24 | watch( |
| 25 | info, |
| 26 | (v) => { |
| 27 | if (!isInputting.value) { |
| 28 | note.value = (v?.note || '').trim() |
| 29 | const frontmatterPart = v?.frontmatterRaw?.trim() |
| 30 | ? `---\n${v.frontmatterRaw.trim()}\n---\n\n` |
| 31 | : '' |
| 32 | content.value = frontmatterPart + (v?.source.contentRaw || '').trim() |
| 33 | dirty.value = false |
| 34 | } |
| 35 | }, |
| 36 | { immediate: true }, |
| 37 | ) |
| 38 | |
| 39 | async function save() { |
| 40 | dirty.value = false |
| 41 | |
| 42 | const { content: contentOnly, frontmatterRaw } = parseSideEditorContent(content.value) |
| 43 | |
| 44 | await update({ |
| 45 | note: note.value || undefined, |
| 46 | content: contentOnly, |
| 47 | frontmatterRaw, |
| 48 | }) |
| 49 | } |
| 50 | |
| 51 | function close() { |
| 52 | showEditor.value = false |
| 53 | } |
| 54 | |
| 55 | useEventListener('keydown', (e) => { |
| 56 | if (activeElement.value?.tagName === 'TEXTAREA' && e.code === 'KeyS' && (e.ctrlKey || e.metaKey)) { |
| 57 | save() |
| 58 | e.preventDefault() |
| 59 | } |
| 60 | }) |
| 61 | |
| 62 | const contentRef = computed({ |
| 63 | get() { return content.value }, |
| 64 | set(v) { |
| 65 | if (content.value.trim() !== v.trim()) |
| 66 | dirty.value = true |
| 67 | content.value = v |
| 68 | }, |
| 69 | }) |
| 70 | |
| 71 | const noteRef = computed({ |
| 72 | get() { return note.value }, |
| 73 | set(v) { |
| 74 | note.value = v |
| 75 | dirty.value = true |
| 76 | }, |
| 77 | }) |
| 78 | |
| 79 | const handlerDown = ref(false) |
| 80 | function onHandlerDown() { |
| 81 | handlerDown.value = true |
| 82 | } |
| 83 | function updateSize(v?: number) { |
| 84 | if (vertical.value) |
| 85 | editorHeight.value = Math.min(Math.max(300, v ?? editorHeight.value), window.innerHeight - 200) |
| 86 | else |
| 87 | editorWidth.value = Math.min(Math.max(318, v ?? editorWidth.value), window.innerWidth - 200) |
| 88 | } |
| 89 | function switchTab(newTab: typeof tab.value) { |
| 90 | tab.value = newTab |
| 91 | // @ts-expect-error force cast |
| 92 | document.activeElement?.blur?.() |
| 93 | } |
| 94 | |
| 95 | if (props.resize) { |
| 96 | useEventListener('pointermove', (e) => { |
| 97 | if (handlerDown.value) { |
| 98 | updateSize(vertical.value |
| 99 | ? window.innerHeight - e.pageY |
| 100 | : window.innerWidth - e.pageX) |
| 101 | } |
| 102 | }, { passive: true }) |
| 103 | useEventListener('pointerup', () => { |
| 104 | handlerDown.value = false |
| 105 | }) |
| 106 | useEventListener('resize', () => { |
| 107 | updateSize() |
| 108 | }) |
| 109 | } |
| 110 | |
| 111 | throttledWatch( |
| 112 | [content, note], |
| 113 | () => { |
| 114 | if (dirty.value) |
| 115 | save() |
| 116 | }, |
| 117 | { throttle: 500 }, |
| 118 | ) |
| 119 | </script> |
| 120 | |
| 121 | <template> |
| 122 | <div |
| 123 | v-if="resize" class="fixed bg-gray-400 select-none opacity-0 hover:opacity-10 z-dragging" |
| 124 | :class="vertical ? 'left-0 right-0 w-full h-10px' : 'top-0 bottom-0 w-10px h-full'" :style="{ |
| 125 | opacity: handlerDown ? '0.3' : undefined, |
| 126 | bottom: vertical ? `${editorHeight - 5}px` : undefined, |
| 127 | right: !vertical ? `${editorWidth - 5}px` : undefined, |
| 128 | cursor: vertical ? 'row-resize' : 'col-resize', |
| 129 | }" @pointerdown="onHandlerDown" |
| 130 | /> |
| 131 | <div |
| 132 | class="shadow bg-main p-2 pt-4 grid grid-rows-[max-content_1fr] h-full overflow-hidden" |
| 133 | :class="resize ? 'border-l border-gray-400 border-opacity-20' : ''" |
| 134 | :style="resize ? { |
| 135 | height: vertical ? `${editorHeight}px` : undefined, |
| 136 | width: !vertical ? `${editorWidth}px` : undefined, |
| 137 | } : {}" |
| 138 | > |
| 139 | <div class="flex pb-2 text-xl -mt-1"> |
| 140 | <div class="mr-4 rounded flex"> |
| 141 | <IconButton |
| 142 | title="Switch to content tab" :class="tab === 'content' ? 'text-primary' : ''" |
| 143 | @click="switchTab('content')" |
| 144 | > |
| 145 | <div class="i-carbon:account" /> |
| 146 | </IconButton> |
| 147 | <IconButton |
| 148 | title="Switch to notes tab" :class="tab === 'note' ? 'text-primary' : ''" |
| 149 | @click="switchTab('note')" |
| 150 | > |
| 151 | <div class="i-carbon:align-box-bottom-right" /> |
| 152 | </IconButton> |
| 153 | </div> |
| 154 | <span class="text-2xl pt-1"> |
| 155 | {{ tab === 'content' ? 'Slide' : 'Notes' }} |
| 156 | </span> |
| 157 | <div class="flex-auto" /> |
| 158 | <template v-if="resize"> |
| 159 | <IconButton v-if="vertical" title="Dock to right" @click="vertical = false"> |
| 160 | <div class="i-carbon:open-panel-right" /> |
| 161 | </IconButton> |
| 162 | <IconButton v-else title="Dock to bottom" @click="vertical = true"> |
| 163 | <div class="i-carbon:open-panel-bottom" /> |
| 164 | </IconButton> |
| 165 | </template> |
| 166 | <IconButton title="Open in editor" @click="openInEditor()"> |
| 167 | <div class="i-carbon:launch" /> |
| 168 | </IconButton> |
| 169 | <IconButton title="Close" @click="close"> |
| 170 | <div class="i-carbon:close" /> |
| 171 | </IconButton> |
| 172 | </div> |
| 173 | <div class="relative overflow-hidden rounded" style="background-color: var(--slidev-code-background)"> |
| 174 | <ShikiEditor v-show="tab === 'content'" v-model="contentRef" placeholder="Create slide content..." /> |
| 175 | <ShikiEditor v-show="tab === 'note'" v-model="noteRef" placeholder="Write some notes..." /> |
| 176 | </div> |
| 177 | </div> |
| 178 | </template> |
| 179 |