fix: 新增右键和删除功能

This commit is contained in:
MTrun 2022-01-27 22:30:35 +08:00
parent f44d119f16
commit f57fa702d9
4 changed files with 91 additions and 4 deletions

View File

@ -52,7 +52,11 @@ export const useChartEditStoreStore = defineStore({
this.componentList.push(chartData)
},
// * 删除组件列表
removeComponentList<T extends { key: string }>(chartData: T): void {
removeComponentList<T extends { key: string }>(chartData: T | number): void {
if(typeof chartData === 'number') {
this.componentList.splice(chartData, 1)
return
}
const i = this.componentList.findIndex(e => e.key === chartData.key)
if (i !== -1) {
this.componentList.splice(i, 1)

View File

@ -0,0 +1,60 @@
import { reactive, ref, nextTick } from 'vue'
import { getChartEditStore } from './useStore.hook'
const chartEditStore = getChartEditStore()
enum MenuEnum {
DELETE = 'delete'
}
export const useContextMenu = () => {
const showDropdownRef = ref(false)
const targetIndex = ref<number>(0)
// * 右键选项
const menuOptions = reactive([
{
label: '删除',
key: MenuEnum.DELETE
}
])
// * 右键处理
const handleContextMenu = (e: MouseEvent, index: number) => {
e.stopPropagation()
e.preventDefault()
targetIndex.value = index
let target = e.target
while (target instanceof SVGElement) {
target = target.parentNode
}
showDropdownRef.value = false
nextTick().then(() => {
chartEditStore.setMousePosition(e.clientX, e.clientY)
showDropdownRef.value = true
})
}
// * 失焦
const onClickoutside = (e: MouseEvent) => {
showDropdownRef.value = false
}
// * 事件处理
const handleMenuSelect = (key: string) => {
showDropdownRef.value = false
switch (key) {
case MenuEnum.DELETE:
chartEditStore.removeComponentList(targetIndex.value)
break
}
}
return {
showDropdownRef,
menuOptions,
handleContextMenu,
onClickoutside,
handleMenuSelect,
mousePosition: chartEditStore.getMousePosition
}
}

View File

@ -1,9 +1,10 @@
import { onUnmounted, onMounted } from 'vue'
import { onUnmounted, onMounted, ref, nextTick } from 'vue'
import { getChartEditStore } from './useStore.hook'
import { EditCanvasTypeEnum } from '@/store/modules/chartEditStore/chartEditStore.d'
const chartEditStore = getChartEditStore()
// 布局处理
export const useLayout = () => {
onMounted(() => {
// 设置 Dom 值(ref 不生效先用 document)
@ -26,4 +27,4 @@ export const useLayout = () => {
removeScale()
})
})
}
}

View File

@ -12,11 +12,24 @@
<div id="go-chart-edit-content">
<!-- 展示 -->
<EditRange>
<!-- 右键 -->
<n-dropdown
placement="bottom-start"
trigger="manual"
size="small"
:x="mousePosition.x"
:y="mousePosition.y"
:options="menuOptions"
:show="showDropdownRef"
:on-clickoutside="onClickoutside"
@select="handleMenuSelect"
/>
<ShapeBox
v-for="(item, index) in chartEditStore.getComponentList"
:key="item.id"
:index="index"
:style="useComponentStyle(item.attr, index)"
@contextmenu="handleContextMenu($event, index)"
>
<component
class="edit-content-chart"
@ -40,13 +53,22 @@ import { ContentBox } from '../ContentBox/index'
import { EditRange } from './components/EditRange'
import { EditBottom } from './components/EditBottom'
import { ShapeBox } from './components/ShapeBox/index'
import { useLayout } from './hooks/useLayout.hook'
import { handleDrop, handleDragOver } from './hooks/useDrop.hook'
import { useContextMenu } from './hooks/useContextMenu.hook'
import { getChartEditStore } from './hooks/useStore.hook'
import { useComponentStyle, useSizeStyle } from './hooks/useStyle.hook'
const chartEditStore = getChartEditStore()
const {
showDropdownRef,
menuOptions,
onClickoutside,
mousePosition,
handleContextMenu,
handleMenuSelect,
} = useContextMenu()
//
useLayout()
</script>