fix:修改右键逻辑

This commit is contained in:
mtrun 2022-03-28 17:17:44 +08:00
parent cd360a1833
commit 851a4d58ec
6 changed files with 92 additions and 64 deletions

View File

@ -9,7 +9,7 @@
@drop="handleDrag" @drop="handleDrag"
@dragover="handleDragOver" @dragover="handleDragOver"
> >
<div id="go-chart-edit-content"> <div id="go-chart-edit-content" @contextmenu="handleContextMenu">
<!-- 展示 --> <!-- 展示 -->
<edit-range> <edit-range>
<!-- 图表 --> <!-- 图表 -->

View File

@ -33,10 +33,8 @@ import { ChartLayoutStoreEnum } from '@/store/modules/chartLayoutStore/chartLayo
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore' import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
import { CreateComponentType } from '@/packages/index.d' import { CreateComponentType } from '@/packages/index.d'
import cloneDeep from 'lodash/cloneDeep' import cloneDeep from 'lodash/cloneDeep'
import { import { useContextMenu } from '@/views/chart/hooks/useContextMenu.hook'
useContextMenu, import { MenuEnum } from '@/views/chart/hooks/useContextMenu.hook.d'
MenuEnum
} from '@/views/chart/hooks/useContextMenu.hook'
import { LayersListItem } from './components/LayersListItem/index' import { LayersListItem } from './components/LayersListItem/index'
import { icon } from '@/plugins' import { icon } from '@/plugins'
@ -45,9 +43,7 @@ const { LayersIcon } = icon.ionicons5
const chartLayoutStore = useChartLayoutStore() const chartLayoutStore = useChartLayoutStore()
const chartEditStore = useChartEditStore() const chartEditStore = useChartEditStore()
const { handleContextMenu } = useContextMenu({ const { handleContextMenu } = useContextMenu()
hideOptionsList: [MenuEnum.CLEAR, MenuEnum.PARSE]
})
// //
const reverseList = computed(() => { const reverseList = computed(() => {

View File

@ -0,0 +1,22 @@
// 右键枚举
export enum MenuEnum {
DELETE = 'delete',
COPY = 'copy',
CUT = 'cut',
PARSE = 'parse',
TOP = 'top',
BOTTOM = 'bottom',
UP = 'up',
DOWN = 'down',
CLEAR = 'clear',
}
export interface MenuOptionsItemType {
type?: string
label?: string
key: MenuEnum | string
icon?: Function
fnHandle?: Function
disabled?: boolean
hidden?: boolean
}

View File

@ -1,8 +1,9 @@
import { reactive, nextTick } from 'vue' import { ref, nextTick } from 'vue'
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore' import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
import { CreateComponentType } from '@/packages/index.d' import { CreateComponentType } from '@/packages/index.d'
import { renderIcon, loadingError } from '@/utils' import { renderIcon, loadingError } from '@/utils'
import { icon } from '@/plugins' import { icon } from '@/plugins'
import { MenuEnum, MenuOptionsItemType } from './useContextMenu.hook.d'
const { const {
CopyIcon, CopyIcon,
@ -10,34 +11,12 @@ const {
ClipboardOutlineIcon, ClipboardOutlineIcon,
TrashIcon, TrashIcon,
ChevronDownIcon, ChevronDownIcon,
ChevronUpIcon ChevronUpIcon,
} = icon.ionicons5 } = icon.ionicons5
const { UpToTopIcon, DownToBottomIcon, PaintBrushIcon } = icon.carbon const { UpToTopIcon, DownToBottomIcon, PaintBrushIcon } = icon.carbon
const chartEditStore = useChartEditStore() const chartEditStore = useChartEditStore()
export enum MenuEnum {
DELETE = 'delete',
COPY = 'copy',
CUT = 'cut',
PARSE = 'parse',
TOP = 'top',
BOTTOM = 'bottom',
UP = 'up',
DOWN = 'down',
CLEAR = 'clear'
}
export interface MenuOptionsItemType {
type?: string
label?: string
key: MenuEnum | string
icon?: Function
fnHandle?: Function
disabled?: boolean
hidden?: boolean
}
// * 默认选项 // * 默认选项
const defaultOptions: MenuOptionsItemType[] = [ const defaultOptions: MenuOptionsItemType[] = [
{ {
@ -60,7 +39,7 @@ const defaultOptions: MenuOptionsItemType[] = [
}, },
{ {
type: 'divider', type: 'divider',
key: 'd1' key: 'd1',
}, },
{ {
label: '置顶', label: '置顶',
@ -88,7 +67,7 @@ const defaultOptions: MenuOptionsItemType[] = [
}, },
{ {
type: 'divider', type: 'divider',
key: 'd2' key: 'd2',
}, },
{ {
label: '清空剪贴板', label: '清空剪贴板',
@ -101,19 +80,53 @@ const defaultOptions: MenuOptionsItemType[] = [
key: MenuEnum.DELETE, key: MenuEnum.DELETE,
icon: renderIcon(TrashIcon), icon: renderIcon(TrashIcon),
fnHandle: chartEditStore.removeComponentList, fnHandle: chartEditStore.removeComponentList,
} },
] ]
// * 去除隐藏选项 // * 无数据传递拥有的选项
const clearHideOption = (options: MenuOptionsItemType[], hideList?: MenuEnum[]) => { const defaultNoItemKeys = [MenuEnum.PARSE, MenuEnum.CLEAR]
if(!hideList) return options
const a = options.filter((op: MenuOptionsItemType) => { /**
return hideList.findIndex((e: MenuEnum) => e !== op.key) * *
* @param options
* @param pickList
* @returns
*/
const pickOption = (options: MenuOptionsItemType[], pickList?: MenuEnum[]) => {
if (!pickList) return options
return options.filter((op: MenuOptionsItemType) => {
return pickList.findIndex((e: MenuEnum) => e === op.key) !== -1
}) })
} }
/**
* *
* @param options
* @param hideList
* @returns
*/
const hideOption = (options: MenuOptionsItemType[], hideList?: MenuEnum[]) => {
if (!hideList) return options
return options.filter((op: MenuOptionsItemType) => {
return hideList.findIndex((e: MenuEnum) => e !== op.key) !== -1
})
}
// * 右键内容
const menuOptions = ref<MenuOptionsItemType[]>([])
// * 右键处理 // * 右键处理
const handleContextMenu = (e: MouseEvent, item?: CreateComponentType) => { const handleContextMenu = (
e: MouseEvent,
// 右键对象
item?: CreateComponentType,
// 判断函数
optionsHandle?: Function,
// 隐藏选项列表
hideOptionsList?: MenuEnum[],
// 挑选选项列表
pickOptionsList?: MenuEnum[]
) => {
e.stopPropagation() e.stopPropagation()
e.preventDefault() e.preventDefault()
let target = e.target let target = e.target
@ -121,6 +134,20 @@ const handleContextMenu = (e: MouseEvent, item?: CreateComponentType) => {
target = target.parentNode target = target.parentNode
} }
chartEditStore.setRightMenuShow(false) chartEditStore.setRightMenuShow(false)
// * 设置默认选项
menuOptions.value = defaultOptions
if (!item) {
menuOptions.value = pickOption(menuOptions.value, defaultNoItemKeys)
}
if (hideOptionsList) {
menuOptions.value = hideOption(menuOptions.value, hideOptionsList)
}
if (pickOptionsList) {
menuOptions.value = hideOption(menuOptions.value, pickOptionsList)
}
if (optionsHandle) {
menuOptions.value = optionsHandle(menuOptions.value)
}
nextTick().then(() => { nextTick().then(() => {
chartEditStore.setMousePosition(e.clientX, e.clientY) chartEditStore.setMousePosition(e.clientX, e.clientY)
chartEditStore.setRightMenuShow(true) chartEditStore.setRightMenuShow(true)
@ -132,21 +159,7 @@ const handleContextMenu = (e: MouseEvent, item?: CreateComponentType) => {
* @param menuConfig * @param menuConfig
* @returns * @returns
*/ */
export const useContextMenu = (menuConfig?: { export const useContextMenu = () => {
// 自定义右键配置
selfOptions?: MenuOptionsItemType[]
// 前置处理函数
optionsHandle?: Function
// 隐藏列表
hideOptionsList?: MenuEnum[]
}) => {
const selfOptions = menuConfig?.selfOptions
const optionsHandle = menuConfig?.optionsHandle
const hideOptionsList = menuConfig?.hideOptionsList
// * 右键选项
const menuOptions: MenuOptionsItemType[] = selfOptions || defaultOptions
// * 失焦 // * 失焦
const onClickoutside = () => { const onClickoutside = () => {
chartEditStore.setRightMenuShow(false) chartEditStore.setRightMenuShow(false)
@ -155,11 +168,11 @@ export const useContextMenu = (menuConfig?: {
// * 事件处理 // * 事件处理
const handleMenuSelect = (key: string) => { const handleMenuSelect = (key: string) => {
chartEditStore.setRightMenuShow(false) chartEditStore.setRightMenuShow(false)
const targetItem: MenuOptionsItemType[] = menuOptions.filter( const targetItem: MenuOptionsItemType[] = menuOptions.value.filter(
(e: MenuOptionsItemType) => e.key === key (e: MenuOptionsItemType) => e.key === key
) )
menuOptions.forEach((e: MenuOptionsItemType) => { menuOptions.value.forEach((e: MenuOptionsItemType) => {
if (e.key === key) { if (e.key === key) {
if (e.fnHandle) { if (e.fnHandle) {
e.fnHandle() e.fnHandle()
@ -170,12 +183,10 @@ export const useContextMenu = (menuConfig?: {
}) })
} }
return { return {
// todo 每次右键都执行判断功能 menuOptions,
// menuOptions: clearHideOption ? clearHideOption(menuOptions, hideOptionsList) : menuOptions,
menuOptions: menuOptions,
handleContextMenu, handleContextMenu,
onClickoutside, onClickoutside,
handleMenuSelect, handleMenuSelect,
mousePosition: chartEditStore.getMousePosition mousePosition: chartEditStore.getMousePosition,
} }
} }

View File

@ -1,6 +1,6 @@
import { isMac, addEventListener, removeEventListener } from '@/utils' import { isMac, addEventListener, removeEventListener } from '@/utils'
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore' import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
import { MenuEnum } from '@/views/chart/hooks/useContextMenu.hook' import { MenuEnum } from '@/views/chart/hooks/useContextMenu.hook.d'
const chartEditStore = useChartEditStore() const chartEditStore = useChartEditStore()

View File

@ -69,7 +69,6 @@ const {
menuOptions, menuOptions,
onClickoutside, onClickoutside,
mousePosition, mousePosition,
handleContextMenu,
handleMenuSelect handleMenuSelect
} = useContextMenu() } = useContextMenu()
</script> </script>