index.vue
9.73 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<template>
<div class="go-edit-align-line">
<div
class="line"
v-for="item in line.lineArr"
:key="item"
:class="[item.includes('row') ? 'row' : 'col', line['select'].has(item) && 'visible']"
:style="useComponentStyle(line['select'].get(item))"
></div>
</div>
</template>
<script setup lang="ts">
import { reactive, computed, watch } from 'vue'
import { useDesignStore } from '@/store/modules/designStore/designStore'
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
import { EditCanvasTypeEnum } from '@/store/modules/chartEditStore/chartEditStore.d'
import { useSettingStore } from '@/store/modules/settingStore/settingStore'
import { CreateComponentType, CreateComponentGroupType } from '@/packages/index.d'
import { setComponentPosition } from '@/utils'
import throttle from 'lodash/throttle'
import cloneDeep from 'lodash/cloneDeep'
// 全局颜色
const designStore = useDesignStore()
const chartEditStore = useChartEditStore()
const settingStore = useSettingStore()
// * 线条集合
const line = reactive({
// 行横向row(上中下),列竖线col(左中右)
lineArr: ['rowt', 'rowc', 'rowb', 'coll', 'colc', 'colr'],
// 展示线
select: new Map(),
// 已经吸附
sorptioned: {
x: false,
y: false
}
})
// * 位置计算
const useComponentStyle = (attr?: Partial<{ x: number; y: number }>) => {
if (!attr) return {}
const componentStyle = {
left: `${attr.x ? attr.x : 0}px`,
top: `${attr.y ? attr.y : 0}px`
}
return componentStyle
}
// 颜色
const themeColor = computed(() => {
return designStore.getAppTheme
})
// * 吸附距离
const minDistance = computed(() => {
return settingStore.getChartAlignRange
})
// * 是否开始计算
const isComputedLine = computed(() => {
// IS_DRAG 移动时为 true,Drag Hook里设置
const isDrag = chartEditStore.getEditCanvas[EditCanvasTypeEnum.IS_DRAG]
return isDrag
})
// * 吸附判定
const isSorption = (selectValue: number, componentValue: number) => {
const isSorption = Math.abs(selectValue - componentValue) <= minDistance.value
return isSorption
}
// * 当前目标
const selectId = computed(() => chartEditStore.getTargetChart.selectId)
const selectTarget = computed(() => chartEditStore.getComponentList[chartEditStore.fetchTargetIndex()])
const selectAttr = computed(() => selectTarget.value?.attr || {})
// * 画布坐标
const canvasPositionList = computed(() => {
return {
id: '0',
attr: {
w: cloneDeep(chartEditStore.getEditCanvasConfig.width),
h: cloneDeep(chartEditStore.getEditCanvasConfig.height),
x: 0,
y: 0,
offsetX: 0,
offsetY: 0,
zIndex: 0
}
}
})
// * 监听鼠标移动
watch(
() => chartEditStore.getMousePosition,
throttle(() => {
try {
if (!isComputedLine.value || selectId.value.length !== 1) return
// 获取目标组件数据
const selectW = selectAttr.value.w
const selectH = selectAttr.value.h
// 距离左侧
const selectLeftX = selectAttr.value.x
const selectHalfX = selectLeftX + selectW / 2
const selectRightX = selectLeftX + selectW
const seletX = [selectLeftX, selectHalfX, selectRightX]
// 距离顶部
const selectTopY = selectAttr.value.y
const selectHalfY = selectTopY + selectH / 2
const selectBottomY = selectTopY + selectH
const selectY = [selectTopY, selectHalfY, selectBottomY]
line.select.clear()
line.sorptioned.y = false
// 循环查询所有组件数据
const componentList = chartEditStore.getComponentList.map((e: CreateComponentType | CreateComponentGroupType) => {
return {
id: e.id,
attr: e.attr
}
})
componentList.push(canvasPositionList.value)
// 传入画布数据
line.lineArr.forEach(lineItem => {
componentList.forEach((component: typeof canvasPositionList.value) => {
// 排除自身
if (selectId.value[0] === component.id) return
const componentW = component.attr.w
const componentH = component.attr.h
// 距离左侧
const componentLeftX = component.attr.x
const componentHalfX = componentLeftX + componentW / 2
const componentRightX = componentLeftX + componentW
const componentX = [componentLeftX, componentHalfX, componentRightX]
// 距离顶部
const componentTopY = component.attr.y
const componentHalfY = componentTopY + componentH / 2
const componentBottomY = componentTopY + componentH
const componentY = [componentTopY, componentHalfY, componentBottomY]
// 横线对比的是 Y
if (lineItem.includes('rowt')) {
// 顶部
if (isSorption(selectTopY, componentTopY)) {
line.select.set(lineItem, { y: componentTopY })
setComponentPosition(selectTarget.value, selectLeftX, componentTopY)
}
if (isSorption(selectTopY, componentHalfY)) {
line.select.set(lineItem, { y: componentHalfY })
setComponentPosition(selectTarget.value, selectLeftX, componentHalfY)
}
if (isSorption(selectTopY, componentBottomY)) {
line.select.set(lineItem, { y: componentBottomY })
setComponentPosition(selectTarget.value, selectLeftX, componentBottomY)
}
}
if (lineItem.includes('rowc')) {
// 顶部
if (isSorption(selectHalfY, componentTopY)) {
line.select.set(lineItem, { y: componentTopY })
setComponentPosition(selectTarget.value, selectLeftX, componentTopY - selectH / 2)
}
if (isSorption(selectHalfY, componentHalfY)) {
line.select.set(lineItem, { y: componentHalfY })
setComponentPosition(selectTarget.value, selectLeftX, componentHalfY - selectH / 2)
}
if (isSorption(selectHalfY, componentBottomY)) {
line.select.set(lineItem, { y: componentBottomY })
setComponentPosition(selectTarget.value, selectLeftX, componentBottomY - selectH / 2)
}
}
if (lineItem.includes('rowb')) {
// 顶部
if (isSorption(selectBottomY, componentTopY)) {
line.select.set(lineItem, { y: componentTopY })
setComponentPosition(selectTarget.value, selectLeftX, componentTopY - selectH)
}
if (isSorption(selectBottomY, componentHalfY)) {
line.select.set(lineItem, { y: componentHalfY })
setComponentPosition(selectTarget.value, selectLeftX, componentHalfY - selectH)
}
if (isSorption(selectBottomY, componentBottomY)) {
line.select.set(lineItem, { y: componentBottomY })
setComponentPosition(selectTarget.value, selectLeftX, componentBottomY - selectH)
}
}
// 纵线对比的是 X
if (lineItem.includes('coll')) {
if (isSorption(selectLeftX, componentLeftX)) {
line.select.set(lineItem, { x: componentLeftX })
setComponentPosition(selectTarget.value, componentLeftX, selectTopY)
}
if (isSorption(selectLeftX, componentHalfX)) {
line.select.set(lineItem, { x: componentHalfX })
setComponentPosition(selectTarget.value, componentHalfX, selectTopY)
}
if (isSorption(selectLeftX, componentRightX)) {
line.select.set(lineItem, { x: componentRightX })
setComponentPosition(selectTarget.value, componentRightX, selectTopY)
}
}
if (lineItem.includes('colc')) {
if (isSorption(selectHalfX, componentLeftX)) {
line.select.set(lineItem, { x: componentLeftX })
setComponentPosition(selectTarget.value, componentLeftX - selectW / 2, selectTopY)
}
if (isSorption(selectHalfX, componentHalfX)) {
line.select.set(lineItem, { x: componentHalfX })
setComponentPosition(selectTarget.value, componentHalfX - selectW / 2, selectTopY)
}
if (isSorption(selectHalfX, componentRightX)) {
line.select.set(lineItem, { x: componentRightX })
setComponentPosition(selectTarget.value, componentRightX - selectW / 2, selectTopY)
}
}
if (lineItem.includes('colr')) {
if (isSorption(selectRightX, componentLeftX)) {
line.select.set(lineItem, { x: componentLeftX })
setComponentPosition(selectTarget.value, componentLeftX - selectW, selectTopY)
}
if (isSorption(selectRightX, componentHalfX)) {
line.select.set(lineItem, { x: componentHalfX })
setComponentPosition(selectTarget.value, componentHalfX - selectW, selectTopY)
}
if (isSorption(selectRightX, componentRightX)) {
line.select.set(lineItem, { x: componentRightX })
setComponentPosition(selectTarget.value, componentRightX - selectW, selectTopY)
}
}
})
})
} catch (err) {
console.log(err)
}
}, 200),
{
deep: true
}
)
// * 取消对齐线
watch(
() => isComputedLine.value,
(val: boolean) => {
if (!val) {
line.select.clear()
line.sorptioned.y = false
}
}
)
</script>
<style lang="scss" scoped>
@include go(edit-align-line) {
.line {
z-index: 1;
position: absolute;
top: 0;
left: 0;
display: none;
border-width: 1px;
border-style: solid;
border-color: v-bind('themeColor');
opacity: 0.7;
&.visible {
display: block;
}
}
.row {
width: 100%;
}
.col {
height: 100%;
}
}
</style>