goview_vue/src/views/chart/hooks/useContextMenu.hook.ts

182 lines
4.2 KiB
TypeScript
Raw Normal View History

2022-02-07 20:55:40 +08:00
import { reactive, nextTick } from 'vue'
import { useChartEditStoreStore } from '@/store/modules/chartEditStore/chartEditStore'
2022-02-01 01:20:00 +08:00
import { CreateComponentType } from '@/packages/index.d'
2022-02-01 17:12:16 +08:00
import { renderIcon, loadingError } from '@/utils'
import { icon } from '@/plugins'
2022-02-04 13:22:00 +08:00
const {
CopyIcon,
CutIcon,
ClipboardOutlineIcon,
TrashIcon,
ChevronDownIcon,
ChevronUpIcon
} = icon.ionicons5
const { UpToTopIcon, DownToBottomIcon, PaintBrushIcon } = icon.carbon
2022-01-27 23:16:51 +08:00
const chartEditStore = useChartEditStoreStore()
2022-01-27 22:30:35 +08:00
2022-02-04 12:17:50 +08:00
export enum MenuEnum {
2022-02-01 17:12:16 +08:00
DELETE = 'delete',
COPY = 'copy',
2022-02-04 12:17:50 +08:00
CUT = 'cut',
2022-02-03 22:54:31 +08:00
PARSE = 'parse',
2022-02-01 17:12:16 +08:00
TOP = 'top',
BOTTOM = 'bottom',
UP = 'up',
2022-02-04 13:22:00 +08:00
DOWN = 'down',
CLEAR = 'clear'
2022-01-27 22:30:35 +08:00
}
2022-02-01 00:31:28 +08:00
export interface MenuOptionsItemType {
2022-02-01 17:12:16 +08:00
type?: string
label?: string
key: MenuEnum | string
icon?: Function
fnHandle?: Function
2022-02-04 13:22:00 +08:00
disabled?: boolean
hidden?: boolean
2022-02-01 00:31:28 +08:00
}
2022-02-01 17:12:16 +08:00
// * 默认选项
const defaultOptions: MenuOptionsItemType[] = [
{
label: '复制',
key: MenuEnum.COPY,
icon: renderIcon(CopyIcon),
2022-02-04 13:22:00 +08:00
fnHandle: chartEditStore.setCopy,
2022-02-03 22:54:31 +08:00
},
2022-02-04 12:17:50 +08:00
{
label: '剪切',
key: MenuEnum.CUT,
icon: renderIcon(CutIcon),
2022-02-04 13:22:00 +08:00
fnHandle: chartEditStore.setCut,
2022-02-04 12:17:50 +08:00
},
2022-02-03 22:54:31 +08:00
{
label: '粘贴',
key: MenuEnum.PARSE,
icon: renderIcon(ClipboardOutlineIcon),
2022-02-04 13:22:00 +08:00
fnHandle: chartEditStore.setParse,
2022-02-01 17:12:16 +08:00
},
{
type: 'divider',
key: 'd1'
},
{
label: '置顶',
key: MenuEnum.TOP,
icon: renderIcon(UpToTopIcon),
2022-02-04 13:22:00 +08:00
fnHandle: chartEditStore.setTop,
2022-02-01 17:12:16 +08:00
},
{
label: '置底',
key: MenuEnum.BOTTOM,
icon: renderIcon(DownToBottomIcon),
2022-02-04 13:22:00 +08:00
fnHandle: chartEditStore.setBottom,
2022-02-01 17:12:16 +08:00
},
{
label: '上移一层',
key: MenuEnum.UP,
icon: renderIcon(ChevronUpIcon),
2022-02-04 13:22:00 +08:00
fnHandle: chartEditStore.setUp,
2022-02-01 17:12:16 +08:00
},
{
label: '下移一层',
key: MenuEnum.DOWN,
icon: renderIcon(ChevronDownIcon),
2022-02-04 13:22:00 +08:00
fnHandle: chartEditStore.setDown,
2022-02-01 20:57:54 +08:00
},
{
type: 'divider',
key: 'd2'
},
2022-02-04 13:22:00 +08:00
{
label: '清空剪贴板',
key: MenuEnum.CLEAR,
icon: renderIcon(PaintBrushIcon),
fnHandle: chartEditStore.setRecordChart,
},
2022-02-01 20:57:54 +08:00
{
label: '删除',
key: MenuEnum.DELETE,
icon: renderIcon(TrashIcon),
2022-02-04 13:22:00 +08:00
fnHandle: chartEditStore.removeComponentList,
2022-02-01 17:12:16 +08:00
}
]
2022-02-07 20:55:40 +08:00
// * 去除隐藏选项
const clearHideOption = (options: MenuOptionsItemType[], hideList?: MenuEnum[]) => {
if(!hideList) return options
const a = options.filter((op: MenuOptionsItemType) => {
return hideList.findIndex((e: MenuEnum) => e !== op.key)
})
}
// * 右键处理
2022-02-28 11:00:33 +08:00
const handleContextMenu = (e: MouseEvent, item?: CreateComponentType) => {
2022-02-07 20:55:40 +08:00
e.stopPropagation()
e.preventDefault()
let target = e.target
while (target instanceof SVGElement) {
target = target.parentNode
}
chartEditStore.setRightMenuShow(false)
nextTick().then(() => {
chartEditStore.setMousePosition(e.clientX, e.clientY)
chartEditStore.setRightMenuShow(true)
})
}
2022-02-01 00:31:28 +08:00
/**
* * hook
2022-02-07 20:55:40 +08:00
* @param menuConfig
2022-02-01 00:31:28 +08:00
* @returns
*/
2022-02-08 19:39:57 +08:00
export const useContextMenu = (menuConfig?: {
2022-02-01 00:31:28 +08:00
// 自定义右键配置
2022-02-07 20:55:40 +08:00
selfOptions?: MenuOptionsItemType[]
2022-02-01 00:31:28 +08:00
// 前置处理函数
2022-02-07 20:55:40 +08:00
optionsHandle?: Function
// 隐藏列表
hideOptionsList?: MenuEnum[]
2022-02-01 00:31:28 +08:00
}) => {
2022-02-07 20:55:40 +08:00
const selfOptions = menuConfig?.selfOptions
const optionsHandle = menuConfig?.optionsHandle
const hideOptionsList = menuConfig?.hideOptionsList
2022-02-01 00:31:28 +08:00
// * 右键选项
const menuOptions: MenuOptionsItemType[] = selfOptions || defaultOptions
2022-01-27 22:30:35 +08:00
// * 失焦
2022-02-01 17:12:16 +08:00
const onClickoutside = () => {
chartEditStore.setRightMenuShow(false)
2022-01-27 22:30:35 +08:00
}
// * 事件处理
const handleMenuSelect = (key: string) => {
chartEditStore.setRightMenuShow(false)
2022-02-01 00:31:28 +08:00
const targetItem: MenuOptionsItemType[] = menuOptions.filter(
(e: MenuOptionsItemType) => e.key === key
)
2022-02-01 17:12:16 +08:00
menuOptions.forEach((e: MenuOptionsItemType) => {
if (e.key === key) {
if (e.fnHandle) {
e.fnHandle()
return
}
if (!targetItem) loadingError()
}
})
2022-01-27 22:30:35 +08:00
}
return {
2022-02-07 20:55:40 +08:00
// todo 每次都重新计算的功能
// menuOptions: clearHideOption ? clearHideOption(menuOptions, hideOptionsList) : menuOptions,
menuOptions: menuOptions,
2022-01-27 22:30:35 +08:00
handleContextMenu,
onClickoutside,
handleMenuSelect,
mousePosition: chartEditStore.getMousePosition
}
}