File size: 4,419 Bytes
89ce340 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
import type { Ref } from 'vue'
import { useSlidesStore } from '@/store'
import type { PPTElement, PPTShapeElement } from '@/types/slides'
import useHistorySnapshot from '@/hooks/useHistorySnapshot'
import { SHAPE_PATH_FORMULAS } from '@/configs/shapes'
interface ShapePathData {
baseSize: number,
originPos: number,
min: number,
max: number,
relative: string,
}
export default (
elementList: Ref<PPTElement[]>,
canvasScale: Ref<number>,
) => {
const slidesStore = useSlidesStore()
const { addHistorySnapshot } = useHistorySnapshot()
const moveShapeKeypoint = (e: MouseEvent | TouchEvent, element: PPTShapeElement, index = 0) => {
const isTouchEvent = !(e instanceof MouseEvent)
if (isTouchEvent && (!e.changedTouches || !e.changedTouches[0])) return
let isMouseDown = true
const startPageX = isTouchEvent ? e.changedTouches[0].pageX : e.pageX
const startPageY = isTouchEvent ? e.changedTouches[0].pageY : e.pageY
const originKeypoints = element.keypoints!
const pathFormula = SHAPE_PATH_FORMULAS[element.pathFormula!]
let shapePathData: ShapePathData | null = null
if ('editable' in pathFormula && pathFormula.editable) {
const getBaseSize = pathFormula.getBaseSize![index]
const range = pathFormula.range![index]
const relative = pathFormula.relative![index]
const keypoint = originKeypoints[index]
const baseSize = getBaseSize(element.width, element.height)
const originPos = baseSize * keypoint
const [min, max] = range
shapePathData = { baseSize, originPos, min, max, relative }
}
const handleMousemove = (e: MouseEvent | TouchEvent) => {
if (!isMouseDown) return
const currentPageX = e instanceof MouseEvent ? e.pageX : e.changedTouches[0].pageX
const currentPageY = e instanceof MouseEvent ? e.pageY : e.changedTouches[0].pageY
const moveX = (currentPageX - startPageX) / canvasScale.value
const moveY = (currentPageY - startPageY) / canvasScale.value
elementList.value = elementList.value.map(el => {
if (el.id === element.id && shapePathData) {
const { baseSize, originPos, min, max, relative } = shapePathData
const shapeElement = el as PPTShapeElement
let keypoint = 0
if (relative === 'center') keypoint = (originPos - moveX * 2) / baseSize
else if (relative === 'left') keypoint = (originPos + moveX) / baseSize
else if (relative === 'right') keypoint = (originPos - moveX) / baseSize
else if (relative === 'top') keypoint = (originPos + moveY) / baseSize
else if (relative === 'bottom') keypoint = (originPos - moveY) / baseSize
else if (relative === 'left_bottom') keypoint = (originPos + moveX) / baseSize
else if (relative === 'right_bottom') keypoint = (originPos - moveX) / baseSize
else if (relative === 'top_right') keypoint = (originPos + moveY) / baseSize
else if (relative === 'bottom_right') keypoint = (originPos - moveY) / baseSize
if (keypoint < min) keypoint = min
if (keypoint > max) keypoint = max
let keypoints: number[] = []
if (Array.isArray(originKeypoints)) {
keypoints = [...originKeypoints]
keypoints[index] = keypoint
}
else keypoints = [keypoint]
return {
...el,
keypoints,
path: pathFormula.formula(shapeElement.width, shapeElement.height, keypoints),
}
}
return el
})
}
const handleMouseup = (e: MouseEvent | TouchEvent) => {
isMouseDown = false
document.ontouchmove = null
document.ontouchend = null
document.onmousemove = null
document.onmouseup = null
const currentPageX = e instanceof MouseEvent ? e.pageX : e.changedTouches[0].pageX
const currentPageY = e instanceof MouseEvent ? e.pageY : e.changedTouches[0].pageY
if (startPageX === currentPageX && startPageY === currentPageY) return
slidesStore.updateSlide({ elements: elementList.value })
addHistorySnapshot()
}
if (isTouchEvent) {
document.ontouchmove = handleMousemove
document.ontouchend = handleMouseup
}
else {
document.onmousemove = handleMousemove
document.onmouseup = handleMouseup
}
}
return {
moveShapeKeypoint,
}
} |