index.vue 7.0 KB

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