useLockElement.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { useStore } from 'vuex'
  2. import { computed } from 'vue'
  3. import { State, MutationTypes } from '@/store'
  4. import { PPTElement, Slide } from '@/types/slides'
  5. import useHistorySnapshot from '@/hooks/useHistorySnapshot'
  6. export default () => {
  7. const store = useStore<State>()
  8. const activeElementIdList = computed(() => store.state.activeElementIdList)
  9. const currentSlide = computed<Slide>(() => store.getters.currentSlide)
  10. const { addHistorySnapshot } = useHistorySnapshot()
  11. const lockElement = () => {
  12. const newElementList: PPTElement[] = JSON.parse(JSON.stringify(currentSlide.value.elements))
  13. for(const element of newElementList) {
  14. if(activeElementIdList.value.includes(element.id)) element.lock = true
  15. }
  16. store.commit(MutationTypes.UPDATE_SLIDE, { elements: newElementList })
  17. store.commit(MutationTypes.SET_ACTIVE_ELEMENT_ID_LIST, [])
  18. addHistorySnapshot()
  19. }
  20. const unlockElement = (handleElement: PPTElement) => {
  21. const newElementList: PPTElement[] = JSON.parse(JSON.stringify(currentSlide.value.elements))
  22. if(handleElement.groupId) {
  23. for(const element of newElementList) {
  24. if(element.groupId === handleElement.groupId) element.lock = false
  25. }
  26. return newElementList
  27. }
  28. for(const element of newElementList) {
  29. if(element.id === handleElement.id) {
  30. element.lock = false
  31. break
  32. }
  33. }
  34. store.commit(MutationTypes.UPDATE_SLIDE, { elements: newElementList })
  35. store.commit(MutationTypes.SET_ACTIVE_ELEMENT_ID_LIST, [handleElement.id])
  36. addHistorySnapshot()
  37. }
  38. return {
  39. lockElement,
  40. unlockElement,
  41. }
  42. }