index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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/_common/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. icon: 'icon-scissor',
  88. handler: cutSlide,
  89. },
  90. {
  91. text: '复制',
  92. subText: 'Ctrl + C',
  93. icon: 'icon-copy',
  94. handler: copySlide,
  95. },
  96. {
  97. text: '粘贴',
  98. subText: 'Ctrl + V',
  99. icon: 'icon-paste',
  100. handler: pasteSlide,
  101. },
  102. { divider: true },
  103. {
  104. text: '新建页面',
  105. subText: 'Enter',
  106. icon: 'icon-add-page',
  107. handler: createSlide,
  108. },
  109. {
  110. text: '复制页面',
  111. icon: 'icon-copy',
  112. handler: copyAndPasteSlide,
  113. },
  114. {
  115. text: '删除页面',
  116. subText: 'Delete',
  117. icon: 'icon-delete',
  118. handler: deleteSlide,
  119. },
  120. ]
  121. }
  122. return {
  123. setThumbnailsFocus,
  124. slides,
  125. slideIndex,
  126. createSlide,
  127. changSlideIndex,
  128. contextmenus,
  129. fillDigit,
  130. handleDragEnd,
  131. }
  132. },
  133. })
  134. </script>
  135. <style lang="scss" scoped>
  136. .thumbnails {
  137. border-right: solid 1px #eee;
  138. background-color: #fff;
  139. display: flex;
  140. flex-direction: column;
  141. overflow: auto;
  142. user-select: none;
  143. }
  144. .add-slide {
  145. height: 40px;
  146. font-size: 12px;
  147. display: flex;
  148. justify-content: center;
  149. align-items: center;
  150. span {
  151. cursor: pointer;
  152. padding: 5px;
  153. &:hover {
  154. border: 1px solid #eee;
  155. }
  156. }
  157. }
  158. .thumbnail-list {
  159. padding: 5px 0;
  160. }
  161. .thumbnail-wrapper {
  162. display: flex;
  163. justify-content: center;
  164. align-items: center;
  165. padding: 5px 0;
  166. .thumbnail {
  167. outline: 2px solid rgba($color: $themeColor, $alpha: .1);
  168. }
  169. &.active {
  170. .slide-index {
  171. color: $themeColor;
  172. }
  173. .thumbnail {
  174. outline-color: $themeColor;
  175. }
  176. }
  177. }
  178. .thumbnail {
  179. width: 120px;
  180. height: 67.5px;
  181. }
  182. .slide-index {
  183. font-size: 12px;
  184. color: #999;
  185. width: 20px;
  186. }
  187. </style>