|
import type { Ref } from 'vue' |
|
import { uniq } from 'lodash' |
|
import { storeToRefs } from 'pinia' |
|
import { useMainStore, useKeyboardStore } from '@/store' |
|
import type { PPTElement } from '@/types/slides' |
|
|
|
export default ( |
|
elementList: Ref<PPTElement[]>, |
|
moveElement: (e: MouseEvent | TouchEvent, element: PPTElement) => void, |
|
) => { |
|
const mainStore = useMainStore() |
|
const { activeElementIdList, activeGroupElementId, handleElementId, editorAreaFocus } = storeToRefs(mainStore) |
|
const { ctrlOrShiftKeyActive } = storeToRefs(useKeyboardStore()) |
|
|
|
|
|
|
|
const selectElement = (e: MouseEvent | TouchEvent, element: PPTElement, startMove = true) => { |
|
if (!editorAreaFocus.value) mainStore.setEditorareaFocus(true) |
|
|
|
|
|
|
|
|
|
if (!activeElementIdList.value.includes(element.id)) { |
|
let newActiveIdList: string[] = [] |
|
|
|
if (ctrlOrShiftKeyActive.value) { |
|
newActiveIdList = [...activeElementIdList.value, element.id] |
|
} |
|
else newActiveIdList = [element.id] |
|
|
|
if (element.groupId) { |
|
const groupMembersId: string[] = [] |
|
elementList.value.forEach((el: PPTElement) => { |
|
if (el.groupId === element.groupId) groupMembersId.push(el.id) |
|
}) |
|
newActiveIdList = [...newActiveIdList, ...groupMembersId] |
|
} |
|
|
|
mainStore.setActiveElementIdList(uniq(newActiveIdList)) |
|
mainStore.setHandleElementId(element.id) |
|
} |
|
|
|
|
|
|
|
|
|
else if (ctrlOrShiftKeyActive.value) { |
|
let newActiveIdList: string[] = [] |
|
|
|
if (element.groupId) { |
|
const groupMembersId: string[] = [] |
|
elementList.value.forEach((el: PPTElement) => { |
|
if (el.groupId === element.groupId) groupMembersId.push(el.id) |
|
}) |
|
newActiveIdList = activeElementIdList.value.filter(id => !groupMembersId.includes(id)) |
|
} |
|
else { |
|
newActiveIdList = activeElementIdList.value.filter(id => id !== element.id) |
|
} |
|
|
|
if (newActiveIdList.length > 0) { |
|
mainStore.setActiveElementIdList(newActiveIdList) |
|
} |
|
} |
|
|
|
|
|
else if (handleElementId.value !== element.id) { |
|
mainStore.setHandleElementId(element.id) |
|
} |
|
|
|
|
|
else if (activeGroupElementId.value !== element.id) { |
|
const startPageX = e instanceof MouseEvent ? e.pageX : e.changedTouches[0].pageX |
|
const startPageY = e instanceof MouseEvent ? e.pageY : e.changedTouches[0].pageY |
|
|
|
;(e.target as HTMLElement).onmouseup = (e: MouseEvent) => { |
|
const currentPageX = e.pageX |
|
const currentPageY = e.pageY |
|
|
|
if (startPageX === currentPageX && startPageY === currentPageY) { |
|
mainStore.setActiveGroupElementId(element.id) |
|
;(e.target as HTMLElement).onmouseup = null |
|
} |
|
} |
|
} |
|
|
|
if (startMove) moveElement(e, element) |
|
} |
|
|
|
return { |
|
selectElement, |
|
} |
|
} |
|
|