index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <div
  3. class="thumbnails"
  4. @mousedown="() => setThumbnailsFocus(true)"
  5. v-click-outside="() => setThumbnailsFocus(false)"
  6. >
  7. <div class="add-slide">
  8. <span @click="createSlide()">+ 添加幻灯片</span>
  9. </div>
  10. <draggable
  11. class="thumbnail-list"
  12. :modelValue="slides"
  13. :animation="300"
  14. :scroll="true"
  15. :scrollSensitivity="50"
  16. @end="handleDragEnd"
  17. itemKey="id"
  18. >
  19. <template #item="{ element, index }">
  20. <div
  21. class="thumbnail-wrapper"
  22. :class="{ 'active': slideIndex === index }"
  23. @mousedown="changSlideIndex(index)"
  24. v-contextmenu="contextmenus"
  25. >
  26. <div class="slide-index">{{ fillDigit(index + 1, 2) }}</div>
  27. <div class="thumbnail">
  28. <ThumbnailSlide :slide="element" :size="120" />
  29. </div>
  30. </div>
  31. </template>
  32. </draggable>
  33. </div>
  34. </template>
  35. <script lang="ts">
  36. import { computed, defineComponent } from 'vue'
  37. import draggable from 'vuedraggable'
  38. import { useStore } from 'vuex'
  39. import { State, MutationTypes } from '@/store'
  40. import { fillDigit } from '@/utils/common'
  41. import { ContextmenuItem } from '@/components/Contextmenu/types'
  42. import useSlideHandler from '@/hooks/useSlideHandler'
  43. import ThumbnailSlide from '@/views/ThumbnailSlide.vue'
  44. export default defineComponent({
  45. name: 'thumbnails',
  46. components: {
  47. draggable,
  48. ThumbnailSlide,
  49. },
  50. setup() {
  51. const store = useStore<State>()
  52. const slides = computed(() => store.state.slides)
  53. const slideIndex = computed(() => store.state.slideIndex)
  54. const {
  55. copySlide,
  56. pasteSlide,
  57. createSlide,
  58. copyAndPasteSlide,
  59. deleteSlide,
  60. cutSlide,
  61. } = useSlideHandler()
  62. const changSlideIndex = (index: number) => {
  63. store.commit(MutationTypes.SET_ACTIVE_ELEMENT_ID_LIST, [])
  64. if(slideIndex.value === index) return
  65. store.commit(MutationTypes.UPDATE_SLIDE_INDEX, index)
  66. }
  67. const thumbnailsFocus = computed(() => store.state.thumbnailsFocus)
  68. const setThumbnailsFocus = (focus: boolean) => {
  69. if(thumbnailsFocus.value === focus) return
  70. store.commit(MutationTypes.SET_THUMBNAILS_FOCUS, focus)
  71. }
  72. const handleDragEnd = (eventData: { newIndex: number; oldIndex: number }) => {
  73. const { newIndex, oldIndex } = eventData
  74. if(oldIndex === newIndex) return
  75. const _slides = JSON.parse(JSON.stringify(slides.value))
  76. const _slide = _slides[oldIndex]
  77. _slides.splice(oldIndex, 1)
  78. _slides.splice(newIndex, 0, _slide)
  79. store.commit(MutationTypes.SET_SLIDES, _slides)
  80. store.commit(MutationTypes.UPDATE_SLIDE_INDEX, newIndex)
  81. }
  82. const contextmenus = (): ContextmenuItem[] => {
  83. return [
  84. {
  85. text: '剪切',
  86. subText: 'Ctrl + X',
  87. handler: cutSlide,
  88. },
  89. {
  90. text: '复制',
  91. subText: 'Ctrl + C',
  92. handler: copySlide,
  93. },
  94. {
  95. text: '粘贴',
  96. subText: 'Ctrl + V',
  97. handler: pasteSlide,
  98. },
  99. { divider: true },
  100. {
  101. text: '新建页面',
  102. subText: 'Enter',
  103. handler: createSlide,
  104. },
  105. {
  106. text: '复制页面',
  107. handler: copyAndPasteSlide,
  108. },
  109. {
  110. text: '删除页面',
  111. subText: 'Delete',
  112. handler: deleteSlide,
  113. },
  114. ]
  115. }
  116. return {
  117. setThumbnailsFocus,
  118. slides,
  119. slideIndex,
  120. createSlide,
  121. changSlideIndex,
  122. contextmenus,
  123. fillDigit,
  124. handleDragEnd,
  125. }
  126. },
  127. })
  128. </script>
  129. <style lang="scss" scoped>
  130. .thumbnails {
  131. border-right: solid 1px #eee;
  132. background-color: #fff;
  133. display: flex;
  134. flex-direction: column;
  135. overflow: auto;
  136. user-select: none;
  137. }
  138. .add-slide {
  139. height: 40px;
  140. font-size: 12px;
  141. display: flex;
  142. justify-content: center;
  143. align-items: center;
  144. span {
  145. cursor: pointer;
  146. padding: 5px;
  147. &:hover {
  148. border: 1px solid #eee;
  149. }
  150. }
  151. }
  152. .thumbnail-list {
  153. padding: 5px 0;
  154. }
  155. .thumbnail-wrapper {
  156. display: flex;
  157. justify-content: center;
  158. align-items: center;
  159. padding: 5px 0;
  160. .thumbnail {
  161. outline: 2px solid rgba($color: $themeColor, $alpha: .1);
  162. }
  163. &.active {
  164. .slide-index {
  165. color: $themeColor;
  166. }
  167. .thumbnail {
  168. outline-color: $themeColor;
  169. }
  170. }
  171. }
  172. .thumbnail {
  173. width: 120px;
  174. height: 67.5px;
  175. }
  176. .slide-index {
  177. font-size: 12px;
  178. color: #999;
  179. width: 20px;
  180. }
  181. </style>