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

View File

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