返回 slidev
DragControl.vue
根目录 / packages / client / internals / DragControl.vue
1 <script setup lang="ts">
2 import type { Pausable } from '@vueuse/core'
3 import type { DragElementState } from '../composables/useDragElements'
4 import { clamp } from '@antfu/utils'
5 import { useIntervalFn } from '@vueuse/core'
6 import { computed, inject, ref, watchEffect } from 'vue'
7 import { useSlideBounds } from '../composables/useSlideBounds'
8 import { injectionSlideScale } from '../constants'
9 import { slideHeight, slideWidth } from '../env'
10 import { magicKeys } from '../state'
11
12 const { data } = defineProps<{ data: DragElementState }>()
13 const { dragId, zoom, autoHeight, x0, y0, width, height, rotate, isArrow } = data
14
15 const slideScale = inject(injectionSlideScale, ref(1))
16 const scale = computed(() => slideScale.value * zoom.value)
17 const { left: slideLeft, top: slideTop } = useSlideBounds()
18
19 const ctrlSize = 10
20 const minSize = isArrow ? Number.NEGATIVE_INFINITY : 40
21 const minRemain = 10
22
23 const rotateRad = computed(() => rotate.value * Math.PI / 180)
24 const rotateSin = computed(() => Math.sin(rotateRad.value))
25 const rotateCos = computed(() => Math.cos(rotateRad.value))
26
27 const boundingWidth = computed(() => width.value * rotateCos.value + height.value * rotateSin.value)
28 const boundingHeight = computed(() => width.value * rotateSin.value + height.value * rotateCos.value)
29
30 const boundingLeft = computed(() => x0.value - boundingWidth.value / 2)
31 const boundingTop = computed(() => y0.value - boundingHeight.value / 2)
32 const boundingRight = computed(() => x0.value + boundingWidth.value / 2)
33 const boundingBottom = computed(() => y0.value + boundingHeight.value / 2)
34
35 const arrowRevX = computed(() => isArrow && width.value < 0)
36 const arrowRevY = computed(() => isArrow && height.value < 0)
37
38 let currentDrag: {
39 x0: number
40 y0: number
41 width: number
42 height: number
43 rotate: number
44 dx0: number
45 dy0: number
46 ltx: number
47 lty: number
48 rtx: number
49 rty: number
50 lbx: number
51 lby: number
52 rbx: number
53 rby: number
54 } | null = null
55
56 function onPointerdown(ev: PointerEvent) {
57 if (ev.buttons !== 1)
58 return
59
60 ev.preventDefault()
61 ev.stopPropagation()
62 const el = ev.target as HTMLElement
63 const elBounds = el.getBoundingClientRect()
64
65 const cross1x = width.value * rotateCos.value - height.value * rotateSin.value
66 const cross1y = width.value * rotateSin.value + height.value * rotateCos.value
67 const cross2x = width.value * rotateCos.value + height.value * rotateSin.value
68 const cross2y = -width.value * rotateSin.value + height.value * rotateCos.value
69
70 currentDrag = {
71 x0: x0.value,
72 y0: y0.value,
73 width: width.value,
74 height: height.value,
75 rotate: rotate.value,
76 dx0: ev.clientX - (elBounds.left + elBounds.right) / 2,
77 dy0: ev.clientY - (elBounds.top + elBounds.bottom) / 2,
78 ltx: x0.value - cross1x / 2,
79 lty: y0.value - cross1y / 2,
80 rtx: x0.value + cross2x / 2,
81 rty: y0.value - cross2y / 2,
82 lbx: x0.value - cross2x / 2,
83 lby: y0.value + cross2y / 2,
84 rbx: x0.value + cross1x / 2,
85 rby: y0.value + cross1y / 2,
86 };
87
88 (ev.currentTarget as HTMLElement).setPointerCapture(ev.pointerId)
89 }
90
91 function onPointermove(ev: PointerEvent) {
92 if (!currentDrag || ev.buttons !== 1)
93 return
94
95 ev.preventDefault()
96 ev.stopPropagation()
97
98 const x = (ev.clientX - slideLeft.value - currentDrag.dx0) / scale.value
99 const y = (ev.clientY - slideTop.value - currentDrag.dy0) / scale.value
100
101 x0.value = clamp(x, -boundingWidth.value / 2 + minRemain, slideWidth.value + boundingWidth.value / 2 - minRemain)
102 y0.value = clamp(y, -boundingHeight.value / 2 + minRemain, slideHeight.value + boundingHeight.value / 2 - minRemain)
103 }
104
105 function onPointerup(ev: PointerEvent) {
106 if (!currentDrag)
107 return
108
109 ev.preventDefault()
110 ev.stopPropagation()
111
112 currentDrag = null
113 }
114
115 const ctrlClasses = `absolute border border-gray bg-gray dark:border-gray-500 dark:bg-gray-800 bg-opacity-30 `
116
117 function getCornerProps(isLeft: boolean, isTop: boolean) {
118 return {
119 onPointerdown,
120 onPointermove: (ev: PointerEvent) => {
121 if (!currentDrag || ev.buttons !== 1)
122 return
123
124 ev.preventDefault()
125 ev.stopPropagation()
126
127 let x = (ev.clientX - slideLeft.value) / scale.value
128 let y = (ev.clientY - slideTop.value) / scale.value
129
130 const { ltx, lty, rtx, rty, lbx, lby, rbx, rby } = currentDrag
131
132 const ratio = currentDrag.width / currentDrag.height
133 const wMin = Math.max(minSize, minSize * ratio)
134 function getSize(w1: number, h1: number) {
135 if (ev.shiftKey) {
136 const w = Math.max(w1, h1 * ratio, wMin)
137 const h = w / ratio
138 return { w, h }
139 }
140 else {
141 return {
142 w: Math.max(w1, minSize),
143 h: Math.max(h1, minSize),
144 }
145 }
146 }
147
148 if (isLeft) {
149 if (isTop) {
150 const w1 = (rbx - x) * rotateCos.value + (rby - y) * rotateSin.value
151 const h1 = -(rbx - x) * rotateSin.value + (rby - y) * rotateCos.value
152 const { w, h } = getSize(w1, h1)
153 x = rbx - w * rotateCos.value + h * rotateSin.value
154 y = rby - w * rotateSin.value - h * rotateCos.value
155 }
156 else {
157 const w1 = (rtx - x) * rotateCos.value - (y - rty) * rotateSin.value
158 const h1 = (rtx - x) * rotateSin.value + (y - rty) * rotateCos.value
159 const { w, h } = getSize(w1, h1)
160 x = rtx - w * rotateCos.value - h * rotateSin.value
161 y = rty - w * rotateSin.value + h * rotateCos.value
162 }
163 }
164 else {
165 if (isTop) {
166 const w1 = (x - lbx) * rotateCos.value - (lby - y) * rotateSin.value
167 const h1 = (x - lbx) * rotateSin.value + (lby - y) * rotateCos.value
168 const { w, h } = getSize(w1, h1)
169 x = lbx + w * rotateCos.value + h * rotateSin.value
170 y = lby + w * rotateSin.value - h * rotateCos.value
171 }
172 else {
173 const w1 = (x - ltx) * rotateCos.value + (y - lty) * rotateSin.value
174 const h1 = -(x - ltx) * rotateSin.value + (y - lty) * rotateCos.value
175 const { w, h } = getSize(w1, h1)
176 x = ltx + w * rotateCos.value - h * rotateSin.value
177 y = lty + w * rotateSin.value + h * rotateCos.value
178 }
179 }
180
181 if (isLeft) {
182 if (isTop) {
183 x0.value = (x + rbx) / 2
184 y0.value = (y + rby) / 2
185 width.value = (rbx - x) * rotateCos.value + (rby - y) * rotateSin.value
186 height.value = -(rbx - x) * rotateSin.value + (rby - y) * rotateCos.value
187 }
188 else {
189 x0.value = (x + rtx) / 2
190 y0.value = (y + rty) / 2
191 width.value = (rtx - x) * rotateCos.value - (y - rty) * rotateSin.value
192 height.value = (rtx - x) * rotateSin.value + (y - rty) * rotateCos.value
193 }
194 }
195 else {
196 if (isTop) {
197 x0.value = (x + lbx) / 2
198 y0.value = (y + lby) / 2
199 width.value = (x - lbx) * rotateCos.value - (lby - y) * rotateSin.value
200 height.value = (x - lbx) * rotateSin.value + (lby - y) * rotateCos.value
201 }
202 else {
203 x0.value = (x + ltx) / 2
204 y0.value = (y + lty) / 2
205 width.value = (x - ltx) * rotateCos.value + (y - lty) * rotateSin.value
206 height.value = -(x - ltx) * rotateSin.value + (y - lty) * rotateCos.value
207 }
208 }
209 },
210 onPointerup,
211 style: {
212 width: `${ctrlSize}px`,
213 height: `${ctrlSize}px`,
214 margin: `-${ctrlSize / 2}px`,
215 left: isLeft !== arrowRevX.value
216 ? '0'
217 : undefined,
218 right: isLeft !== arrowRevX.value
219 ? undefined
220 : '0',
221 top: isTop !== arrowRevY.value
222 ? '0'
223 : undefined,
224 bottom: isTop !== arrowRevY.value
225 ? undefined
226 : '0',
227 cursor: isArrow
228 ? 'move'
229 : +isLeft + +isTop === 1
230 ? 'nesw-resize'
231 : 'nwse-resize',
232 borderRadius: isArrow ? '50%' : undefined,
233 },
234 class: ctrlClasses,
235 }
236 }
237
238 function getBorderProps(dir: 'l' | 'r' | 't' | 'b') {
239 return {
240 onPointerdown,
241 onPointermove: (ev: PointerEvent) => {
242 if (!currentDrag || ev.buttons !== 1)
243 return
244
245 ev.preventDefault()
246 ev.stopPropagation()
247
248 const x = (ev.clientX - slideLeft.value) / scale.value
249 const y = (ev.clientY - slideTop.value) / scale.value
250
251 const { ltx, lty, rtx, rty, lbx, lby, rbx, rby } = currentDrag
252
253 if (dir === 'l') {
254 const rx = (rtx + rbx) / 2
255 const ry = (rty + rby) / 2
256 width.value = Math.max((rx - x) * rotateCos.value + (ry - y) * rotateSin.value, minSize)
257 x0.value = rx - width.value * rotateCos.value / 2
258 y0.value = ry - width.value * rotateSin.value / 2
259 }
260 else if (dir === 'r') {
261 const lx = (ltx + lbx) / 2
262 const ly = (lty + lby) / 2
263 width.value = Math.max((x - lx) * rotateCos.value + (y - ly) * rotateSin.value, minSize)
264 x0.value = lx + width.value * rotateCos.value / 2
265 y0.value = ly + width.value * rotateSin.value / 2
266 }
267 else if (dir === 't') {
268 const bx = (lbx + rbx) / 2
269 const by = (lby + rby) / 2
270 height.value = Math.max((by - y) * rotateCos.value - (bx - x) * rotateSin.value, minSize)
271 x0.value = bx + height.value * rotateSin.value / 2
272 y0.value = by - height.value * rotateCos.value / 2
273 }
274 else if (dir === 'b') {
275 const tx = (ltx + rtx) / 2
276 const ty = (lty + rty) / 2
277 height.value = Math.max((y - ty) * rotateCos.value - (x - tx) * rotateSin.value, minSize)
278 x0.value = tx - height.value * rotateSin.value / 2
279 y0.value = ty + height.value * rotateCos.value / 2
280 }
281 },
282 onPointerup,
283 style: {
284 width: `${ctrlSize}px`,
285 height: `${ctrlSize}px`,
286 margin: `-${ctrlSize / 2}px`,
287 left: dir === 'l'
288 ? '0'
289 : dir === 'r'
290 ? `100%`
291 : `50%`,
292 top: dir === 't'
293 ? '0'
294 : dir === 'b'
295 ? `100%`
296 : `50%`,
297 cursor: 'lr'.includes(dir)
298 ? 'ew-resize'
299 : 'ns-resize',
300 borderRadius: '50%',
301 },
302 class: ctrlClasses,
303 }
304 }
305
306 function getRotateProps() {
307 return {
308 onPointerdown,
309 onPointermove: (ev: PointerEvent) => {
310 if (!currentDrag || ev.buttons !== 1)
311 return
312
313 ev.preventDefault()
314 ev.stopPropagation()
315
316 const x = (ev.clientX - slideLeft.value - currentDrag.dx0) / scale.value - ctrlSize / 4
317 const y = (ev.clientY - slideTop.value - currentDrag.dy0) / scale.value - ctrlSize / 4
318
319 let angle = Math.atan2(y - y0.value, x - x0.value) * 180 / Math.PI + 90
320
321 const commonAngles = [0, 90, 180, 270, 360]
322 for (const a of commonAngles) {
323 if (Math.abs(angle - a) < 5) {
324 angle = a % 360
325 break
326 }
327 }
328
329 rotate.value = angle
330 },
331 onPointerup,
332 style: {
333 width: `${ctrlSize}px`,
334 height: `${ctrlSize}px`,
335 margin: `-${ctrlSize / 2}px`,
336 left: '50%',
337 top: '-20px',
338 cursor: 'grab',
339 borderRadius: '50%',
340 },
341 class: ctrlClasses,
342 }
343 }
344
345 const moveInterval = 20
346 const intervalFnOptions = {
347 immediate: false,
348 immediateCallback: false,
349 }
350 const moveLeft = useIntervalFn(() => {
351 if (boundingRight.value <= minRemain)
352 return
353 x0.value--
354 }, moveInterval, intervalFnOptions)
355 const moveRight = useIntervalFn(() => {
356 if (boundingLeft.value >= slideWidth.value - minRemain)
357 return
358 x0.value++
359 }, moveInterval, intervalFnOptions)
360 const moveUp = useIntervalFn(() => {
361 if (boundingBottom.value <= minRemain)
362 return
363 y0.value--
364 }, moveInterval, intervalFnOptions)
365 const moveDown = useIntervalFn(() => {
366 if (boundingTop.value >= slideHeight.value - minRemain)
367 return
368 y0.value++
369 }, moveInterval, intervalFnOptions)
370
371 watchEffect(() => {
372 function shortcut(key: string, fn: Pausable) {
373 if (magicKeys[key].value)
374 fn.resume()
375 else fn.pause()
376 }
377 shortcut('left', moveLeft)
378 shortcut('right', moveRight)
379 shortcut('up', moveUp)
380 shortcut('down', moveDown)
381 })
382 </script>
383
384 <template>
385 <div
386 v-if="Number.isFinite(x0)"
387 id="drag-control-container"
388 :data-drag-id="dragId"
389 :style="{
390 position: 'absolute',
391 zIndex: 100,
392 left: `${zoom * (x0 - Math.abs(width) / 2)}px`,
393 top: `${zoom * (y0 - Math.abs(height) / 2)}px`,
394 width: `${zoom * Math.abs(width)}px`,
395 height: `${zoom * Math.abs(height)}px`,
396 transformOrigin: 'center center',
397 transform: `rotate(${rotate}deg)`,
398 }"
399 @pointerdown="onPointerdown"
400 @pointermove="onPointermove"
401 @pointerup="onPointerup"
402 >
403 <div class="absolute inset-0 z-nav dark:b-gray-400" :class="isArrow ? '' : 'b b-dark'">
404 <template v-if="!autoHeight">
405 <div v-bind="getCornerProps(true, true)" />
406 <div v-bind="getCornerProps(false, false)" />
407 <template v-if="!isArrow">
408 <div v-bind="getCornerProps(true, false)" />
409 <div v-bind="getCornerProps(false, true)" />
410 </template>
411 </template>
412 <template v-if="!isArrow">
413 <div v-bind="getBorderProps('l')" />
414 <div v-bind="getBorderProps('r')" />
415 <template v-if="!autoHeight">
416 <div v-bind="getBorderProps('t')" />
417 <div v-bind="getBorderProps('b')" />
418 </template>
419 <div v-bind="getRotateProps()" />
420 <div
421 class="absolute -top-15px w-0 b b-dashed b-dark dark:b-gray-400"
422 :style="{
423 left: 'calc(50% - 1px)',
424 height: autoHeight ? '14px' : '10px',
425 }"
426 />
427 </template>
428 </div>
429 </div>
430 </template>
431
431 lines Plain Text