提交 | 用户 | 时间
|
820397
|
1 |
<template> |
H |
2 |
<div class="relative"> |
|
3 |
<table class="cube-table"> |
|
4 |
<!-- 底层:魔方矩阵 --> |
|
5 |
<tbody> |
|
6 |
<tr v-for="(rowCubes, row) in cubes" :key="row"> |
|
7 |
<td |
|
8 |
v-for="(cube, col) in rowCubes" |
|
9 |
:key="col" |
|
10 |
:class="['cube', { active: cube.active }]" |
|
11 |
:style="{ |
|
12 |
width: `${cubeSize}px`, |
|
13 |
height: `${cubeSize}px` |
|
14 |
}" |
|
15 |
@click="handleCubeClick(row, col)" |
|
16 |
@mouseenter="handleCellHover(row, col)" |
|
17 |
> |
|
18 |
<Icon icon="ep-plus" /> |
|
19 |
</td> |
|
20 |
</tr> |
|
21 |
</tbody> |
|
22 |
<!-- 顶层:热区 --> |
|
23 |
<div |
|
24 |
v-for="(hotArea, index) in hotAreas" |
|
25 |
:key="index" |
|
26 |
class="hot-area" |
|
27 |
:style="{ |
|
28 |
top: `${cubeSize * hotArea.top}px`, |
|
29 |
left: `${cubeSize * hotArea.left}px`, |
|
30 |
height: `${cubeSize * hotArea.height}px`, |
|
31 |
width: `${cubeSize * hotArea.width}px` |
|
32 |
}" |
|
33 |
@click="handleHotAreaSelected(hotArea, index)" |
|
34 |
@mouseover="exitHotAreaSelectMode" |
|
35 |
> |
|
36 |
<!-- 右上角热区删除按钮 --> |
|
37 |
<div |
|
38 |
v-if="selectedHotAreaIndex === index" |
|
39 |
class="btn-delete" |
|
40 |
@click="handleDeleteHotArea(index)" |
|
41 |
> |
|
42 |
<Icon icon="ep:circle-close-filled" /> |
|
43 |
</div> |
|
44 |
{{ `${hotArea.width}×${hotArea.height}` }} |
|
45 |
</div> |
|
46 |
</table> |
|
47 |
</div> |
|
48 |
</template> |
|
49 |
<script lang="ts" setup> |
|
50 |
import { propTypes } from '@/utils/propTypes' |
|
51 |
import * as vueTypes from 'vue-types' |
|
52 |
import { Point, Rect, isContains, isOverlap, createRect } from './util' |
|
53 |
|
|
54 |
// 魔方编辑器 |
|
55 |
// 有两部分组成: |
|
56 |
// 1. 魔方矩阵:位于底层,由方块组件的二维表格,用于创建热区 |
|
57 |
// 操作方法: |
|
58 |
// 1.1 点击其中一个方块就会进入热区选择模式 |
|
59 |
// 1.2 再次点击另外一个方块时,结束热区选择模式 |
|
60 |
// 1.3 在两个方块中间的区域创建热区 |
|
61 |
// 如果两次点击的都是同一方块,就只创建一个格子的热区 |
|
62 |
// 2. 热区:位于顶层,采用绝对定位,覆盖在魔方矩阵上面。 |
|
63 |
defineOptions({ name: 'MagicCubeEditor' }) |
|
64 |
|
|
65 |
/** |
|
66 |
* 方块 |
|
67 |
* @property active 是否激活 |
|
68 |
*/ |
|
69 |
type Cube = Point & { active: boolean } |
|
70 |
|
|
71 |
// 定义属性 |
|
72 |
const props = defineProps({ |
|
73 |
// 热区列表 |
|
74 |
modelValue: vueTypes.array<any>().isRequired, |
|
75 |
// 行数,默认 4 行 |
|
76 |
rows: propTypes.number.def(4), |
|
77 |
// 列数,默认 4 列 |
|
78 |
cols: propTypes.number.def(4), |
|
79 |
// 方块大小,单位px,默认75px |
|
80 |
cubeSize: propTypes.number.def(75) |
|
81 |
}) |
|
82 |
|
|
83 |
// 魔方矩阵:所有的方块 |
|
84 |
const cubes = ref<Cube[][]>([]) |
|
85 |
// 监听行数、列数变化 |
|
86 |
watch( |
|
87 |
() => [props.rows, props.cols], |
|
88 |
() => { |
|
89 |
// 清空魔方 |
|
90 |
cubes.value = [] |
|
91 |
if (!props.rows || !props.cols) return |
|
92 |
|
|
93 |
// 初始化魔方 |
|
94 |
for (let row = 0; row < props.rows; row++) { |
|
95 |
cubes.value[row] = [] |
|
96 |
for (let col = 0; col < props.cols; col++) { |
|
97 |
cubes.value[row].push({ x: col, y: row, active: false }) |
|
98 |
} |
|
99 |
} |
|
100 |
}, |
|
101 |
{ immediate: true } |
|
102 |
) |
|
103 |
|
|
104 |
// 热区列表 |
|
105 |
const hotAreas = ref<Rect[]>([]) |
|
106 |
// 初始化热区 |
|
107 |
watch( |
|
108 |
() => props.modelValue, |
|
109 |
() => (hotAreas.value = props.modelValue || []), |
|
110 |
{ immediate: true } |
|
111 |
) |
|
112 |
|
|
113 |
// 热区起始方块 |
|
114 |
const hotAreaBeginCube = ref<Cube>() |
|
115 |
// 是否开启了热区选择模式 |
|
116 |
const isHotAreaSelectMode = () => !!hotAreaBeginCube.value |
|
117 |
/** |
|
118 |
* 处理鼠标点击方块 |
|
119 |
* |
|
120 |
* @param currentRow 当前行号 |
|
121 |
* @param currentCol 当前列号 |
|
122 |
*/ |
|
123 |
const handleCubeClick = (currentRow: number, currentCol: number) => { |
|
124 |
const currentCube = cubes.value[currentRow][currentCol] |
|
125 |
// 情况1:进入热区选择模式 |
|
126 |
if (!isHotAreaSelectMode()) { |
|
127 |
hotAreaBeginCube.value = currentCube |
|
128 |
hotAreaBeginCube.value.active = true |
|
129 |
return |
|
130 |
} |
|
131 |
|
|
132 |
// 情况2:结束热区选择模式 |
|
133 |
hotAreas.value.push(createRect(hotAreaBeginCube.value!, currentCube)) |
|
134 |
// 结束热区选择模式 |
|
135 |
exitHotAreaSelectMode() |
|
136 |
// 创建后就选中热区 |
|
137 |
let hotAreaIndex = hotAreas.value.length - 1 |
|
138 |
handleHotAreaSelected(hotAreas.value[hotAreaIndex], hotAreaIndex) |
|
139 |
// 发送热区变动通知 |
|
140 |
emitUpdateModelValue() |
|
141 |
} |
|
142 |
/** |
|
143 |
* 处理鼠标经过方块 |
|
144 |
* |
|
145 |
* @param currentRow 当前行号 |
|
146 |
* @param currentCol 当前列号 |
|
147 |
*/ |
|
148 |
const handleCellHover = (currentRow: number, currentCol: number) => { |
|
149 |
// 当前没有进入热区选择模式 |
|
150 |
if (!isHotAreaSelectMode()) return |
|
151 |
|
|
152 |
// 当前已选的区域 |
|
153 |
const currentSelectedArea = createRect( |
|
154 |
hotAreaBeginCube.value!, |
|
155 |
cubes.value[currentRow][currentCol] |
|
156 |
) |
|
157 |
// 热区不允许重叠 |
|
158 |
for (const hotArea of hotAreas.value) { |
|
159 |
// 检查是否重叠 |
|
160 |
if (isOverlap(hotArea, currentSelectedArea)) { |
|
161 |
// 结束热区选择模式 |
|
162 |
exitHotAreaSelectMode() |
|
163 |
|
|
164 |
return |
|
165 |
} |
|
166 |
} |
|
167 |
|
|
168 |
// 激活选中区域内部的方块 |
|
169 |
eachCube((_, __, cube) => { |
|
170 |
cube.active = isContains(currentSelectedArea, cube) |
|
171 |
}) |
|
172 |
} |
|
173 |
/** |
|
174 |
* 处理热区删除 |
|
175 |
* |
|
176 |
* @param index 热区索引 |
|
177 |
*/ |
|
178 |
const handleDeleteHotArea = (index: number) => { |
|
179 |
hotAreas.value.splice(index, 1) |
|
180 |
// 结束热区选择模式 |
|
181 |
exitHotAreaSelectMode() |
|
182 |
// 发送热区变动通知 |
|
183 |
emitUpdateModelValue() |
|
184 |
} |
|
185 |
|
|
186 |
// 发送模型更新 |
|
187 |
const emit = defineEmits(['update:modelValue', 'hotAreaSelected']) |
|
188 |
// 发送热区变动通知 |
|
189 |
const emitUpdateModelValue = () => emit('update:modelValue', hotAreas) |
|
190 |
|
|
191 |
// 热区选中 |
|
192 |
const selectedHotAreaIndex = ref(0) |
|
193 |
const handleHotAreaSelected = (hotArea: Rect, index: number) => { |
|
194 |
selectedHotAreaIndex.value = index |
|
195 |
emit('hotAreaSelected', hotArea, index) |
|
196 |
} |
|
197 |
|
|
198 |
/** |
|
199 |
* 结束热区选择模式 |
|
200 |
*/ |
|
201 |
function exitHotAreaSelectMode() { |
|
202 |
// 移除方块激活标记 |
|
203 |
eachCube((_, __, cube) => { |
|
204 |
if (cube.active) { |
|
205 |
cube.active = false |
|
206 |
} |
|
207 |
}) |
|
208 |
|
|
209 |
// 清除起点 |
|
210 |
hotAreaBeginCube.value = undefined |
|
211 |
} |
|
212 |
|
|
213 |
/** |
|
214 |
* 迭代魔方矩阵 |
|
215 |
* @param callback 回调 |
|
216 |
*/ |
|
217 |
const eachCube = (callback: (x: number, y: number, cube: Cube) => void) => { |
|
218 |
for (let x = 0; x < cubes.value.length; x++) { |
|
219 |
for (let y = 0; y < cubes.value[x].length; y++) { |
|
220 |
callback(x, y, cubes.value[x][y]) |
|
221 |
} |
|
222 |
} |
|
223 |
} |
|
224 |
</script> |
|
225 |
<style lang="scss" scoped> |
|
226 |
.cube-table { |
|
227 |
position: relative; |
|
228 |
border-spacing: 0; |
|
229 |
border-collapse: collapse; |
|
230 |
|
|
231 |
.cube { |
|
232 |
border: 1px solid var(--el-border-color); |
|
233 |
text-align: center; |
|
234 |
color: var(--el-text-color-secondary); |
|
235 |
cursor: pointer; |
|
236 |
box-sizing: border-box; |
|
237 |
&.active { |
|
238 |
background: var(--el-color-primary-light-9); |
|
239 |
} |
|
240 |
} |
|
241 |
|
|
242 |
.hot-area { |
|
243 |
position: absolute; |
|
244 |
display: flex; |
|
245 |
align-items: center; |
|
246 |
justify-content: center; |
|
247 |
border: 1px solid var(--el-color-primary); |
|
248 |
background: var(--el-color-primary-light-8); |
|
249 |
color: var(--el-color-primary); |
|
250 |
box-sizing: border-box; |
|
251 |
border-spacing: 0; |
|
252 |
border-collapse: collapse; |
|
253 |
cursor: pointer; |
|
254 |
|
|
255 |
.btn-delete { |
|
256 |
z-index: 1; |
|
257 |
position: absolute; |
|
258 |
top: -8px; |
|
259 |
right: -8px; |
|
260 |
height: 16px; |
|
261 |
width: 16px; |
|
262 |
display: flex; |
|
263 |
align-items: center; |
|
264 |
justify-content: center; |
|
265 |
border-radius: 50%; |
|
266 |
background-color: #fff; |
|
267 |
} |
|
268 |
} |
|
269 |
} |
|
270 |
</style> |