修改自定义点击上下左右时的操作

v1.0.0
于肖磊 2024-09-06 16:02:52 +08:00
parent 5fa51457ee
commit a97a8b09ca
1 changed files with 78 additions and 1 deletions

View File

@ -299,7 +299,7 @@ const resizingHandle = (new_location: any, key: string, index: number) => {
com_data.com_height = h;
com_data.staging_height = h;
if (key == 'img') {
const { img_width, img_height } = handleImg(com_data, w, h);
const { img_width, img_height } = handleImg(com_data, w, h);
com_data.img_width = img_width;
com_data.img_height = img_height;
} else if (key == 'auxiliary-line') {
@ -368,6 +368,10 @@ const end_drag = (event: MouseEvent) => {
init_drag_style.value = ``;
if (rect_end.value.width > 16 && rect_end.value.height > 16) {
hot_list.data = [{ name: '热区' + (hot_list.data.length + 1), link: {}, drag_start: cloneDeep(rect_start.value), drag_end: cloneDeep(rect_end.value) }];
diy_data.value.forEach((item: any) => {
item.show_tabs = '0';
});
emits('rightUpdate', {});
}
rect_start.value = { x: 0, y: 0, width: 0, height: 0 };
rect_end.value = { x: 0, y: 0, width: 0, height: 0 };
@ -565,6 +569,79 @@ const rect_style = computed(() => {
});
//#endregion
//#region
const handleKeyUp = (e: KeyboardEvent) => {
let key_code = ['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'];
let x = 0;
let y = 0;
//
if (e.key === 'ArrowUp') { //
y = -1;
} else if (e.key === 'ArrowDown') { //
y = 1;
} else if (e.key === 'ArrowLeft') { //
x = -1;
} else if (e.key === 'ArrowRight') { //
x = 1;
}
e.preventDefault();
//
if (key_code.includes(e.key)) {
data_handling(x, y);
}
};
const data_handling = (x: number, y: number) => {
// ,
if (hot_list.data.length > 0) {
//
const { drag_start, drag_end } = hot_list.data[0];
if (isWithinBounds(drag_start.x + x, drag_end.width, 390)) {
hot_list.data[0].drag_start.x += x;
}
if (isWithinBounds(drag_start.y + y, drag_end.height, center_height.value)) {
hot_list.data[0].drag_start.y += y;
}
// ,
const rect1 = { x: drag_start.x, y: drag_start.y, width: drag_end.width, height: drag_end.height }
diy_data.value.forEach(item => {
const rect2 = { x: item.location.x, y: item.location.y, width: item.com_data.com_width, height: item.com_data.com_height };
// 10
if (isRectangleIntersecting(rect1, rect2) == '1') {
// x 0
if (isWithinBounds(item.location.x + x, item.com_data.com_width, 390)) {
item.location.x += x;
}
// Y0
if (isWithinBounds(item.location.y + y, item.com_data.com_height, center_height.value)) {
item.location.y += y;
}
}
});
} else {
diy_data.value.forEach(item => {
//
if (item.show_tabs == '1') {
// x 0
if (isWithinBounds(item.location.x + x, item.com_data.com_width, 390)) {
item.location.x += x;
}
// Y0
if (isWithinBounds(item.location.y + y, item.com_data.com_height, center_height.value)) {
item.location.y += y;
}
}
});
}
};
// coordinate current_size () max_size
const isWithinBounds = (coordinate:number, current_size: number, max_size: number) => coordinate >= 0 && coordinate + current_size <= max_size;
onMounted(() => {
//
document.addEventListener('keyup', handleKeyUp);
});
onUnmounted(() => {
//
document.removeEventListener('keyup', handleKeyUp);
});
//#endregion
defineExpose({
diy_data,