-
-
Notifications
You must be signed in to change notification settings - Fork 209
/
ResizeControl.vue
234 lines (186 loc) 路 7.07 KB
/
ResizeControl.vue
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<script lang="ts" setup>
import type { NodeChange, NodeDimensionChange, NodePositionChange } from '@vue-flow/core'
import { NodeIdInjection, useGetPointerPosition, useVueFlow } from '@vue-flow/core'
import { select } from 'd3-selection'
import { drag } from 'd3-drag'
import type { OnResize, OnResizeStart, ResizeControlProps, ResizeDragEvent } from './types'
import { ResizeControlVariant } from './types'
import { getDirection } from './utils'
const props = withDefaults(defineProps<ResizeControlProps>(), {
variant: 'handle' as ResizeControlVariant,
minWidth: 10,
minHeight: 10,
})
const emits = defineEmits<{
(event: 'resizeStart', resizeEvent: OnResizeStart): void
(event: 'resize', resizeEvent: OnResize): void
(event: 'resizeEnd', resizeEvent: OnResizeStart): void
}>()
const initPrevValues = { width: 0, height: 0, x: 0, y: 0 }
const initStartValues = {
...initPrevValues,
pointerX: 0,
pointerY: 0,
}
const contextNodeId = inject(NodeIdInjection, null)
const resizeControlRef = ref<HTMLDivElement>()
const id = computed(() => (typeof props.nodeId === 'string' ? props.nodeId : contextNodeId))
const { findNode, emits: triggerEmits } = useVueFlow()
const startValues = ref<typeof initStartValues>(initStartValues)
const prevValues = ref<typeof initPrevValues>(initPrevValues)
const aspectRatio = computed(() => {
if (props.keepAspectRatio === true && startValues.value.width && startValues.value.height) {
return startValues.value.width / startValues.value.height
}
if (typeof props.keepAspectRatio == 'number') {
return props.keepAspectRatio
}
return undefined
})
const getPointerPosition = useGetPointerPosition()
const defaultPosition = computed(() => (props.variant === ResizeControlVariant.Line ? 'right' : 'bottom-right'))
const controlPosition = computed(() => props.position ?? defaultPosition.value)
watchEffect((onCleanup) => {
if (!resizeControlRef.value || !id.value) {
return
}
const selection = select(resizeControlRef.value)
const dragHandler = drag<HTMLDivElement, unknown>()
.on('start', (event: ResizeDragEvent) => {
const node = findNode(id.value!)
const { xSnapped, ySnapped } = getPointerPosition(event)
prevValues.value = {
width: node?.dimensions.width ?? 0,
height: node?.dimensions.height ?? 0,
x: node?.position.x ?? 0,
y: node?.position.y ?? 0,
}
startValues.value = {
...prevValues.value,
pointerX: xSnapped,
pointerY: ySnapped,
}
emits('resizeStart', { event, params: prevValues.value })
})
.on('drag', (event: ResizeDragEvent) => {
const { xSnapped, ySnapped } = getPointerPosition(event)
const node = findNode(id.value!)
const enableX = controlPosition.value.includes('right') || controlPosition.value.includes('left')
const enableY = controlPosition.value.includes('bottom') || controlPosition.value.includes('top')
const invertX = controlPosition.value.includes('left')
const invertY = controlPosition.value.includes('top')
if (node) {
const changes: NodeChange[] = []
const {
pointerX: startX,
pointerY: startY,
width: startWidth,
height: startHeight,
x: startNodeX,
y: startNodeY,
} = startValues.value
const { x: prevX, y: prevY, width: prevWidth, height: prevHeight } = prevValues.value
const distX = Math.floor(enableX ? xSnapped - startX : 0)
const distY = Math.floor(enableY ? ySnapped - startY : 0)
let width = Math.max(startWidth + (invertX ? -distX : distX), props.minWidth)
let height = Math.max(startHeight + (invertY ? -distY : distY), props.minHeight)
if (aspectRatio.value) {
const currentAspectRatio = width / height
if (currentAspectRatio !== aspectRatio.value) {
const newWidth = height * aspectRatio.value
const newHeight = width / aspectRatio.value
if (distX > distY) {
height = Math.max(newHeight, props.minHeight)
} else {
width = Math.max(newWidth, props.minWidth)
}
}
}
const isWidthChange = width !== prevWidth
const isHeightChange = height !== prevHeight
if (invertX || invertY) {
const x = invertX ? startNodeX - (width - startWidth) : startNodeX
const y = invertY ? startNodeY - (height - startHeight) : startNodeY
// only transform the node if the width or height changes
const isXPosChange = x !== prevX && isWidthChange
const isYPosChange = y !== prevY && isHeightChange
if (isXPosChange || isYPosChange) {
const positionChange: NodePositionChange = {
id: node.id,
type: 'position',
from: node.position,
position: {
x: isXPosChange ? x : prevX,
y: isYPosChange ? y : prevY,
},
}
changes.push(positionChange)
prevValues.value.x = positionChange.position!.x
prevValues.value.y = positionChange.position!.y
}
}
if (isWidthChange || isHeightChange) {
const dimensionChange: NodeDimensionChange = {
id: id.value!,
type: 'dimensions',
updateStyle: true,
resizing: true,
dimensions: {
width,
height,
},
}
changes.push(dimensionChange)
prevValues.value.width = width
prevValues.value.height = height
}
if (changes.length === 0) return
const direction = getDirection({
width: prevValues.value.width,
prevWidth,
height: prevValues.value.height,
prevHeight,
invertX,
invertY,
})
const nextValues = { ...prevValues.value, direction }
const callResize = props.shouldResize?.(event, nextValues)
if (callResize === false) return
emits('resize', { event, params: nextValues })
triggerEmits.nodesChange(changes)
}
})
.on('end', (event: ResizeDragEvent) => {
const dimensionChange: NodeDimensionChange = {
id: id.value!,
type: 'dimensions',
resizing: false,
}
emits('resizeEnd', { event, params: prevValues.value })
triggerEmits.nodesChange([dimensionChange])
})
selection.call(dragHandler)
onCleanup(() => {
selection.on('.drag', null)
})
})
const positionClassNames = computed(() => controlPosition.value.split('-'))
const colorStyleProp = computed(() => (props.variant === ResizeControlVariant.Line ? 'borderColor' : 'backgroundColor'))
const controlStyle = computed(() => (props.color ? { [colorStyleProp.value]: props.color } : {}))
</script>
<script lang="ts">
export default {
name: 'ResizeControl',
compatConfig: { MODE: 3 },
}
</script>
<template>
<div
ref="resizeControlRef"
class="vue-flow__resize-control nodrag"
:class="[...positionClassNames, variant]"
:style="controlStyle"
>
<slot />
</div>
</template>