NotesPanel.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <template>
  2. <MoveablePanel
  3. class="notes-panel"
  4. :width="300"
  5. :height="560"
  6. :title="lang.ssSlideNote.replace(/\*/g, String(slideIndex + 1))"
  7. :left="-270"
  8. :top="90"
  9. :minWidth="300"
  10. :minHeight="400"
  11. :maxWidth="480"
  12. :maxHeight="780"
  13. resizeable
  14. @close="close()"
  15. >
  16. <div class="container">
  17. <div class="notes" ref="notesRef">
  18. <div class="note" :class="{ 'active': activeNoteId === note.id }" v-for="note in notes" :key="note.id" @click="handleClickNote(note)">
  19. <div class="header note-header">
  20. <div class="user">
  21. <div class="avatar"><IconUser /></div>
  22. <div class="user-info">
  23. <div class="username">{{ note.user }}</div>
  24. <div class="time">{{ new Date(note.time).toLocaleString() }}</div>
  25. </div>
  26. </div>
  27. <div class="btns">
  28. <div class="btn reply" @click="replyNoteId = note.id">{{ lang.ssReply }}</div>
  29. <div class="btn delete" @click.stop="deleteNote(note.id)">{{ lang.ssDelete }}</div>
  30. </div>
  31. </div>
  32. <div class="content">{{ note.content }}</div>
  33. <div class="replies" v-if="note.replies?.length">
  34. <div class="reply-item" v-for="reply in note.replies" :key="reply.id">
  35. <div class="header reply-header">
  36. <div class="user">
  37. <div class="avatar"><IconUser /></div>
  38. <div class="user-info">
  39. <div class="username">{{ reply.user }}</div>
  40. <div class="time">{{ new Date(reply.time).toLocaleString() }}</div>
  41. </div>
  42. </div>
  43. <div class="btns">
  44. <div class="btn delete" @click.stop="deleteReply(note.id, reply.id)">{{ lang.ssDelete }}</div>
  45. </div>
  46. </div>
  47. <div class="content">{{ reply.content }}</div>
  48. </div>
  49. </div>
  50. <div class="note-reply" v-if="replyNoteId === note.id">
  51. <TextArea :padding="6" v-model:value="replyContent" :placeholder="lang.ssReplyInput" :rows="1" @enter.prevent="createNoteReply()" />
  52. <div class="reply-btns">
  53. <Button class="btn" size="small" @click="replyNoteId = ''">{{ lang.ssCancel }}</Button>
  54. <Button class="btn" size="small" type="primary" @click="createNoteReply()">{{ lang.ssReply }}</Button>
  55. </div>
  56. </div>
  57. </div>
  58. <div class="empty" v-if="!notes.length">{{ lang.ssNoNotes }}</div>
  59. </div>
  60. <div class="send">
  61. <TextArea
  62. ref="textAreaRef"
  63. v-model:value="content"
  64. :padding="6"
  65. :placeholder="notePlaceholder"
  66. :rows="2"
  67. @focus="replyNoteId = ''; activeNoteId = ''"
  68. @enter.prevent="createNote()"
  69. />
  70. <div class="footer">
  71. <IconDelete class="btn icon" v-tooltip="lang.ssClearNotes" style="flex: 1" @click="clear()" />
  72. <Button type="primary" class="btn" style="flex: 12" @click="createNote()">{{ lang.ssAddNote }}</Button>
  73. </div>
  74. </div>
  75. </div>
  76. </MoveablePanel>
  77. </template>
  78. <script lang="ts" setup>
  79. import { ref, computed, watch, nextTick, useTemplateRef } from 'vue'
  80. import { storeToRefs } from 'pinia'
  81. import { nanoid } from 'nanoid'
  82. import { useMainStore, useSlidesStore } from '@/store'
  83. import type { Note } from '@/types/slides'
  84. import MoveablePanel from '@/components/MoveablePanel.vue'
  85. import TextArea from '@/components/TextArea.vue'
  86. import Button from '@/components/Button.vue'
  87. import { lang } from '@/main'
  88. const slidesStore = useSlidesStore()
  89. const mainStore = useMainStore()
  90. const { slideIndex, currentSlide } = storeToRefs(slidesStore)
  91. const { handleElementId } = storeToRefs(mainStore)
  92. const content = ref('')
  93. const replyContent = ref('')
  94. const notes = computed(() => currentSlide.value?.notes || [])
  95. const activeNoteId = ref('')
  96. const replyNoteId = ref('')
  97. const textAreaRef = useTemplateRef<InstanceType<typeof TextArea>>('textAreaRef')
  98. const notesRef = useTemplateRef<HTMLElement>('notesRef')
  99. const notePlaceholder = computed(() => {
  100. const target = handleElementId.value ? lang.ssSelEl : lang.ssCurSlide
  101. return lang.lang == 'en' ? 'Add a note for this slide...' :lang.ssNoteInput.replace(/\*/g, target)
  102. })
  103. watch(slideIndex, () => {
  104. activeNoteId.value = ''
  105. replyNoteId.value = ''
  106. })
  107. const scrollToBottom = () => {
  108. if (notesRef.value) {
  109. notesRef.value.scrollTop = notesRef.value.scrollHeight
  110. }
  111. }
  112. const createNote = () => {
  113. if (!content.value) {
  114. if (textAreaRef.value) textAreaRef.value.focus()
  115. return
  116. }
  117. const newNote: Note = {
  118. id: nanoid(),
  119. content: content.value,
  120. time: new Date().getTime(),
  121. user: lang.ssTestUser,
  122. }
  123. if (handleElementId.value) newNote.elId = handleElementId.value
  124. const newNotes = [
  125. ...notes.value,
  126. newNote,
  127. ]
  128. slidesStore.updateSlide({ notes: newNotes })
  129. content.value = ''
  130. nextTick(scrollToBottom)
  131. }
  132. const deleteNote = (id: string) => {
  133. const newNotes = notes.value.filter(note => note.id !== id)
  134. slidesStore.updateSlide({ notes: newNotes })
  135. }
  136. const createNoteReply = () => {
  137. if (!replyContent.value) return
  138. const currentNote = notes.value.find(note => note.id === replyNoteId.value)
  139. if (!currentNote) return
  140. const newReplies = [
  141. ...currentNote.replies || [],
  142. {
  143. id: nanoid(),
  144. content: replyContent.value,
  145. time: new Date().getTime(),
  146. user: lang.ssTestUser,
  147. },
  148. ]
  149. const newNote: Note = {
  150. ...currentNote,
  151. replies: newReplies,
  152. }
  153. const newNotes = notes.value.map(note => note.id === replyNoteId.value ? newNote : note)
  154. slidesStore.updateSlide({ notes: newNotes })
  155. replyContent.value = ''
  156. replyNoteId.value = ''
  157. nextTick(scrollToBottom)
  158. }
  159. const deleteReply = (noteId: string, replyId: string) => {
  160. const currentNote = notes.value.find(note => note.id === noteId)
  161. if (!currentNote || !currentNote.replies) return
  162. const newReplies = currentNote.replies.filter(reply => reply.id !== replyId)
  163. const newNote: Note = {
  164. ...currentNote,
  165. replies: newReplies,
  166. }
  167. const newNotes = notes.value.map(note => note.id === noteId ? newNote : note)
  168. slidesStore.updateSlide({ notes: newNotes })
  169. }
  170. const handleClickNote = (note: Note) => {
  171. activeNoteId.value = note.id
  172. if (note.elId) {
  173. const elIds = currentSlide.value.elements.map(item => item.id)
  174. if (elIds.includes(note.elId)) {
  175. mainStore.setActiveElementIdList([note.elId])
  176. }
  177. else mainStore.setActiveElementIdList([])
  178. }
  179. else mainStore.setActiveElementIdList([])
  180. }
  181. const clear = () => {
  182. slidesStore.updateSlide({ notes: [] })
  183. }
  184. const close = () => {
  185. mainStore.setNotesPanelState(false)
  186. }
  187. </script>
  188. <style lang="scss" scoped>
  189. .notes-panel {
  190. height: 100%;
  191. font-size: 12px;
  192. user-select: none;
  193. }
  194. .container {
  195. height: 100%;
  196. display: flex;
  197. flex-direction: column;
  198. }
  199. .notes {
  200. flex: 1;
  201. overflow: auto;
  202. margin: 0 -10px;
  203. padding: 2px 12px;
  204. }
  205. .empty {
  206. width: 100%;
  207. height: 100%;
  208. color: #999;
  209. font-style: italic;
  210. display: flex;
  211. justify-content: center;
  212. align-items: center;
  213. }
  214. .note {
  215. border: 1px solid #eee;
  216. border-radius: 4px;
  217. padding: 10px;
  218. & + .note {
  219. margin-top: 10px;
  220. }
  221. &.active {
  222. background-color: #f7f7f7;
  223. }
  224. .header {
  225. display: flex;
  226. justify-content: space-between;
  227. align-items: flex-start;
  228. margin-bottom: 8px;
  229. &:hover {
  230. .btns {
  231. opacity: 1;
  232. }
  233. }
  234. }
  235. .user {
  236. display: flex;
  237. align-items: center;
  238. .avatar {
  239. width: 30px;
  240. height: 30px;
  241. border-radius: 50%;
  242. background-color: #42ba97;
  243. color: #fff;
  244. font-size: 18px;
  245. display: flex;
  246. justify-content: center;
  247. align-items: center;
  248. margin-right: 10px;
  249. }
  250. .username {
  251. font-size: 14px;
  252. }
  253. .time {
  254. font-size: 12px;
  255. color: #aaa;
  256. }
  257. }
  258. .btns {
  259. display: flex;
  260. align-items: center;
  261. opacity: 0;
  262. .btn {
  263. margin-left: 5px;
  264. cursor: pointer;
  265. &:hover {
  266. text-decoration: underline;
  267. color: $themeColor;
  268. }
  269. }
  270. }
  271. .replies {
  272. margin-left: 20px;
  273. margin-top: 15px;
  274. .reply-item {
  275. margin-top: 10px;
  276. .content {
  277. margin-top: 5px;
  278. }
  279. }
  280. }
  281. }
  282. .note-reply {
  283. margin-top: 15px;
  284. }
  285. .reply-btns {
  286. margin-top: 5px;
  287. text-align: right;
  288. .btn {
  289. margin-left: 8px;
  290. }
  291. }
  292. .send {
  293. height: 120px;
  294. flex-shrink: 0;
  295. text-align: right;
  296. display: flex;
  297. flex-direction: column;
  298. justify-content: flex-end;
  299. .footer {
  300. margin-top: 10px;
  301. display: flex;
  302. .btn {
  303. display: flex;
  304. justify-content: center;
  305. align-items: center;
  306. &.icon {
  307. font-size: 18px;
  308. color: #666;
  309. cursor: pointer;
  310. }
  311. }
  312. .btn + .btn {
  313. margin-left: 8px;
  314. }
  315. }
  316. }
  317. </style>