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