Skip to content

Commit 917b9d1

Browse files
committed
fix: mp/menu菜单拖动后激活菜单
1 parent ebacbbb commit 917b9d1

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

src/views/mp/menu/components/MenuPreviewer.vue

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
item-key="id"
55
ghost-class="draggable-ghost"
66
:animation="400"
7-
@end="onDragEnd"
7+
@end="onParentDragEnd"
88
>
99
<template #item="{ element: parent, index: x }">
1010
<div class="menu_bottom">
@@ -23,6 +23,7 @@
2323
item-key="id"
2424
ghost-class="draggable-ghost"
2525
:animation="400"
26+
@end="onChildDragEnd"
2627
>
2728
<template #item="{ element: child, index: y }">
2829
<div class="subtitle menu_bottom">
@@ -118,42 +119,49 @@ const subMenuClicked = (child: Menu, x: number, y: number) => {
118119
}
119120
120121
/**
121-
* 处理一级菜单展开后被拖动
122+
* 处理一级菜单展开后被拖动,激活(展开)原来活动的一级菜单
122123
*
123124
* @param oldIndex: 一级菜单拖动前的位置
124125
* @param newIndex: 一级菜单拖动后的位置
125126
*/
126-
const onDragEnd = ({ oldIndex, newIndex }) => {
127+
const onParentDragEnd = ({ oldIndex, newIndex }) => {
127128
// 二级菜单没有展开,直接返回
128129
if (props.activeIndex === '__MENU_NOT_SELECTED__') {
129130
return
130131
}
131132
132-
let newParent = props.parentIndex
133-
if (props.parentIndex === oldIndex) {
134-
newParent = newIndex
135-
} else if (props.parentIndex === newIndex) {
136-
newParent = oldIndex
137-
} else {
138-
// 如果展开的二级菜单下标`props.parentIndex`不是被移动的菜单的前后下标。
139-
// 那么使用一个辅助素组来模拟菜单移动,然后找到展开的二级菜单的新下标`newParent`
140-
let positions = new Array<boolean>(menuList.value.length).fill(false)
141-
positions[props.parentIndex] = true
142-
positions.splice(oldIndex, 1)
143-
positions.splice(newIndex, 0, true)
144-
newParent = positions.indexOf(true)
145-
}
133+
// 使用一个辅助数组来模拟菜单移动,然后找到展开的二级菜单的新下标`newParent`
134+
let positions = new Array<boolean>(menuList.value.length).fill(false)
135+
positions[props.parentIndex] = true
136+
const [out] = positions.splice(oldIndex, 1) // 移出菜单,保存到变量out
137+
positions.splice(newIndex, 0, out) // 把out变量插入被移出的菜单
138+
const newParentIndex = positions.indexOf(true)
146139
147140
// 找到菜单元素,触发一级菜单点击
148-
const parent = menuList.value[newParent]
149-
emit('menu-clicked', parent, newParent)
141+
const parent = menuList.value[newParentIndex]
142+
emit('menu-clicked', parent, newParentIndex)
143+
}
144+
145+
/**
146+
* 处理二级菜单展开后被拖动,激活被拖动的菜单
147+
*
148+
* @param newIndex 二级菜单拖动后的位置
149+
*/
150+
const onChildDragEnd = ({ newIndex }) => {
151+
const x = props.parentIndex
152+
const y = newIndex
153+
const children = menuList.value[x]?.children
154+
if (children && children?.length > 0) {
155+
const child = children[y]
156+
emit('submenu-clicked', child, x, y)
157+
}
150158
}
151159
</script>
152160

153161
<style lang="scss" scoped>
154162
.menu_bottom {
155163
position: relative;
156-
display: inline-block;
164+
display: block;
157165
float: left;
158166
width: 85.5px;
159167
text-align: center;

0 commit comments

Comments
 (0)