feat:【mall】diy editor 的 magic-cube-editor 优化

pull/251/head
YunaiV 2025-10-29 13:56:10 +08:00
parent fa383159ea
commit cb9fc7ad3f
2 changed files with 43 additions and 53 deletions

View File

@ -7,16 +7,16 @@ import { IconifyIcon } from '@vben/icons';
import { createRect, isContains, isOverlap } from './util'; import { createRect, isContains, isOverlap } from './util';
// TODO @AI: /**
// * 魔方编辑器有两部分组成
// * 1. 魔方矩阵位于底层由方块组件的二维表格用于创建热区
// 1. * 操作方法
// * 1.1 点击其中一个方块就会进入热区选择模式
// 1.1 * 1.2 再次点击另外一个方块时结束热区选择模
// 1.2 * 1.3 在两个方块中间的区域创建热区
// 1.3 * 如果两次点击的都是同一方块就只创建一个格子的热
// * 2. 热区位于顶层采用绝对定位覆盖在魔方矩阵上面
// 2. */
defineOptions({ name: 'MagicCubeEditor' }); defineOptions({ name: 'MagicCubeEditor' });
/** 定义属性 */ /** 定义属性 */
@ -29,12 +29,10 @@ const props = defineProps({
type: Number, type: Number,
default: 4, default: 4,
}, // 4 }, // 4
cols: { cols: {
type: Number, type: Number,
default: 4, default: 4,
}, // 4 }, // 4
cubeSize: { cubeSize: {
type: Number, type: Number,
default: 75, default: 75,
@ -70,6 +68,7 @@ watch(
); );
const hotAreas = ref<Rect[]>([]); // const hotAreas = ref<Rect[]>([]); //
/** 初始化热区 */ /** 初始化热区 */
watch( watch(
() => props.modelValue, () => props.modelValue,
@ -86,20 +85,20 @@ const isHotAreaSelectMode = () => !!hotAreaBeginCube.value; // 是否开启了
* @param currentRow 当前行号 * @param currentRow 当前行号
* @param currentCol 当前列号 * @param currentCol 当前列号
*/ */
const handleCubeClick = (currentRow: number, currentCol: number) => { function handleCubeClick(currentRow: number, currentCol: number) {
const currentCube = cubes.value[currentRow]?.[currentCol]; const currentCube = cubes.value[currentRow]?.[currentCol];
if (!currentCube) { if (!currentCube) {
return; return;
} }
// 1 // 1
if (!isHotAreaSelectMode()) { if (!isHotAreaSelectMode()) {
hotAreaBeginCube.value = currentCube; hotAreaBeginCube.value = currentCube;
hotAreaBeginCube.value!.active = true; hotAreaBeginCube.value!.active = true;
return; return;
} }
// 2 // 2
hotAreas.value.push(createRect(hotAreaBeginCube.value!, currentCube)); hotAreas.value.push(createRect(hotAreaBeginCube.value!, currentCube));
// //
exitHotAreaSelectMode(); exitHotAreaSelectMode();
@ -111,7 +110,7 @@ const handleCubeClick = (currentRow: number, currentCol: number) => {
} }
// //
emitUpdateModelValue(); emitUpdateModelValue();
}; }
/** /**
* 处理鼠标经过方块 * 处理鼠标经过方块
@ -119,7 +118,7 @@ const handleCubeClick = (currentRow: number, currentCol: number) => {
* @param currentRow 当前行号 * @param currentRow 当前行号
* @param currentCol 当前列号 * @param currentCol 当前列号
*/ */
const handleCellHover = (currentRow: number, currentCol: number) => { function handleCellHover(currentRow: number, currentCol: number) {
// //
if (!isHotAreaSelectMode()) { if (!isHotAreaSelectMode()) {
return; return;
@ -138,7 +137,6 @@ const handleCellHover = (currentRow: number, currentCol: number) => {
if (isOverlap(hotArea, currentSelectedArea)) { if (isOverlap(hotArea, currentSelectedArea)) {
// //
exitHotAreaSelectMode(); exitHotAreaSelectMode();
return; return;
} }
} }
@ -147,13 +145,9 @@ const handleCellHover = (currentRow: number, currentCol: number) => {
eachCube((_, __, cube) => { eachCube((_, __, cube) => {
cube.active = isContains(currentSelectedArea, cube); cube.active = isContains(currentSelectedArea, cube);
}); });
}; }
/** /** 处理热区删除 */
* 处理热区删除
*
* @param index 热区索引
*/
function handleDeleteHotArea(index: number) { function handleDeleteHotArea(index: number) {
hotAreas.value.splice(index, 1); hotAreas.value.splice(index, 1);
// //
@ -165,14 +159,14 @@ function handleDeleteHotArea(index: number) {
const emitUpdateModelValue = () => emit('update:modelValue', hotAreas.value); // const emitUpdateModelValue = () => emit('update:modelValue', hotAreas.value); //
const selectedHotAreaIndex = ref(0); // const selectedHotAreaIndex = ref(0); //
const handleHotAreaSelected = (hotArea: Rect, index: number) => {
/** 处理热区选中 */
function handleHotAreaSelected(hotArea: Rect, index: number) {
selectedHotAreaIndex.value = index; selectedHotAreaIndex.value = index;
emit('hotAreaSelected', hotArea, index); emit('hotAreaSelected', hotArea, index);
}; }
/** /** 结束热区选择模式 */
* 结束热区选择模式
*/
function exitHotAreaSelectMode() { function exitHotAreaSelectMode() {
// //
eachCube((_, __, cube) => { eachCube((_, __, cube) => {
@ -246,9 +240,9 @@ const eachCube = (callback: (x: number, y: number, cube: Cube) => void) => {
> >
<IconifyIcon icon="ep:circle-close-filled" /> <IconifyIcon icon="ep:circle-close-filled" />
</div> </div>
<span v-if="hotArea.width">{{ <span v-if="hotArea.width">
`${hotArea.width}×${hotArea.height}` {{ `${hotArea.width}×${hotArea.height}`}}
}}</span> </span>
</div> </div>
</table> </table>
</div> </div>

View File

@ -1,52 +1,49 @@
// 坐标点 /** 坐标点 */
export interface Point { export interface Point {
x: number; x: number;
y: number; y: number;
} }
// 矩形 /** 矩形 */
export interface Rect { export interface Rect {
// 左上角 X 轴坐标 left: number; // 左上角 X 轴坐标
left: number; top: number; // 左上角 Y 轴坐标
// 左上角 Y 轴坐标 right: number; // 右下角 X 轴坐标
top: number; bottom: number; // 右下角 Y 轴坐标
// 右下角 X 轴坐标 width: number; // 矩形宽度
right: number; height: number; // 矩形高度
// 右下角 Y 轴坐标
bottom: number;
// 矩形宽度
width: number;
// 矩形高度
height: number;
} }
/** /**
* *
*
* @param a A * @param a A
* @param b B * @param b B
*/ */
export const isOverlap = (a: Rect, b: Rect): boolean => { export function isOverlap(a: Rect, b: Rect): boolean {
return ( return (
a.left < b.left + b.width && a.left < b.left + b.width &&
a.left + a.width > b.left && a.left + a.width > b.left &&
a.top < b.top + b.height && a.top < b.top + b.height &&
a.height + a.top > b.top a.height + a.top > b.top
); );
}; }
/** /**
* *
* @param hotArea * @param hotArea
* @param point * @param point
*/ */
export const isContains = (hotArea: Rect, point: Point): boolean => { export function isContains(hotArea: Rect, point: Point): boolean {
return ( return (
point.x >= hotArea.left && point.x >= hotArea.left &&
point.x < hotArea.right && point.x < hotArea.right &&
point.y >= hotArea.top && point.y >= hotArea.top &&
point.y < hotArea.bottom point.y < hotArea.bottom
); );
}; }
// TODO @AIlinter 修复
/** /**
* *
* *
@ -59,7 +56,7 @@ export const isContains = (hotArea: Rect, point: Point): boolean => {
* @param a * @param a
* @param b * @param b
*/ */
export const createRect = (a: Point, b: Point): Rect => { export function createRect(a: Point, b: Point): Rect {
// 计算矩形的范围 // 计算矩形的范围
const [left, left2] = [a.x, b.x].sort(); const [left, left2] = [a.x, b.x].sort();
const [top, top2] = [a.y, b.y].sort(); const [top, top2] = [a.y, b.y].sort();
@ -67,6 +64,5 @@ export const createRect = (a: Point, b: Point): Rect => {
const bottom = top2 + 1; const bottom = top2 + 1;
const height = bottom - top; const height = bottom - top;
const width = right - left; const width = right - left;
return { left, right, top, bottom, height, width }; return { left, right, top, bottom, height, width };
}; }