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

276 lines
6.4 KiB
TypeScript
Raw Normal View History

2022-08-09 19:50:03 +08:00
import { ref, nextTick, toRaw } from 'vue'
2022-03-04 20:57:36 +08:00
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
import { CreateComponentType, CreateComponentGroupType } from '@/packages/index.d'
2022-02-01 17:12:16 +08:00
import { renderIcon, loadingError } from '@/utils'
import { icon } from '@/plugins'
import { MenuOptionsItemType } from './useContextMenu.hook.d'
import { MenuEnum } from '@/enums/editPageEnum'
import cloneDeep from 'lodash/cloneDeep'
2022-02-01 17:12:16 +08:00
const {
CopyIcon,
CutIcon,
ClipboardOutlineIcon,
TrashIcon,
ChevronDownIcon,
ChevronUpIcon,
LockOpenOutlineIcon,
LockClosedOutlineIcon,
EyeOutlineIcon,
EyeOffOutlineIcon
} = icon.ionicons5
const { UpToTopIcon, DownToBottomIcon, PaintBrushIcon, Carbon3DSoftwareIcon, Carbon3DCursorIcon } = icon.carbon
2022-01-27 23:16:51 +08:00
2022-03-04 20:57:36 +08:00
const chartEditStore = useChartEditStore()
2022-01-27 22:30:35 +08:00
2022-08-14 01:04:03 +08:00
/**
* 线
* @param {number} n > 2
* @returns
*/
export const divider = (n: number = 3) => {
2022-08-14 01:04:03 +08:00
return {
2022-08-09 21:08:25 +08:00
type: 'divider',
2022-08-14 01:04:03 +08:00
key: `d${n}`
2022-08-09 21:08:25 +08:00
}
2022-08-14 01:04:03 +08:00
}
2022-08-09 21:08:25 +08:00
// * 默认单组件选项
2022-08-09 19:50:03 +08:00
export const defaultOptions: MenuOptionsItemType[] = [
{
label: '锁定',
key: MenuEnum.LOCK,
icon: renderIcon(LockClosedOutlineIcon),
fnHandle: chartEditStore.setLock
},
{
label: '解除锁定',
key: MenuEnum.UNLOCK,
icon: renderIcon(LockOpenOutlineIcon),
fnHandle: chartEditStore.setUnLock
},
{
label: '隐藏',
key: MenuEnum.HIDE,
icon: renderIcon(EyeOffOutlineIcon),
fnHandle: chartEditStore.setHide
},
{
label: '显示',
key: MenuEnum.SHOW,
icon: renderIcon(EyeOutlineIcon),
fnHandle: chartEditStore.setShow
},
{
type: 'divider',
key: 'd0'
},
2022-02-01 17:12:16 +08:00
{
label: '复制',
key: MenuEnum.COPY,
icon: renderIcon(CopyIcon),
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),
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),
fnHandle: chartEditStore.setParse
2022-02-01 17:12:16 +08:00
},
{
type: 'divider',
key: 'd1'
2022-02-01 17:12:16 +08:00
},
{
label: '置顶',
key: MenuEnum.TOP,
icon: renderIcon(UpToTopIcon),
fnHandle: chartEditStore.setTop
2022-02-01 17:12:16 +08:00
},
{
label: '置底',
key: MenuEnum.BOTTOM,
icon: renderIcon(DownToBottomIcon),
fnHandle: chartEditStore.setBottom
2022-02-01 17:12:16 +08:00
},
{
label: '上移一层',
key: MenuEnum.UP,
icon: renderIcon(ChevronUpIcon),
fnHandle: chartEditStore.setUp
2022-02-01 17:12:16 +08:00
},
{
label: '下移一层',
key: MenuEnum.DOWN,
icon: renderIcon(ChevronDownIcon),
fnHandle: chartEditStore.setDown
2022-02-01 20:57:54 +08:00
},
{
type: 'divider',
key: 'd2'
2022-02-01 20:57:54 +08:00
},
2022-02-04 13:22:00 +08:00
{
label: '清空剪贴板',
key: MenuEnum.CLEAR,
icon: renderIcon(PaintBrushIcon),
fnHandle: chartEditStore.setRecordChart
2022-02-04 13:22:00 +08:00
},
2022-02-01 20:57:54 +08:00
{
label: '删除',
key: MenuEnum.DELETE,
icon: renderIcon(TrashIcon),
fnHandle: chartEditStore.removeComponentList
}
]
// * 默认多选组件选项
2022-08-09 19:50:03 +08:00
export const defaultMultiSelectOptions: MenuOptionsItemType[] = [
{
label: '创建分组',
key: MenuEnum.GROUP,
icon: renderIcon(Carbon3DSoftwareIcon),
fnHandle: chartEditStore.setGroup
},
{
label: '解除分组',
key: MenuEnum.UN_GROUP,
icon: renderIcon(Carbon3DCursorIcon),
fnHandle: chartEditStore.setUnGroup
2022-08-09 19:50:03 +08:00
}
2022-02-01 17:12:16 +08:00
]
2022-03-28 17:17:44 +08:00
// * 无数据传递拥有的选项
const defaultNoItemKeys = [MenuEnum.PARSE, MenuEnum.CLEAR]
/**
* *
* @param options
* @param pickList
* @returns
*/
const pickOption = (options: MenuOptionsItemType[], pickList?: MenuEnum[]) => {
if (!pickList) return options
2022-08-09 19:50:03 +08:00
const list: MenuOptionsItemType[] = []
pickList.forEach(e => {
list.push(...options.filter(op => op.key === e))
2022-02-07 20:55:40 +08:00
})
2022-08-09 19:50:03 +08:00
return list
2022-02-07 20:55:40 +08:00
}
2022-03-28 17:17:44 +08:00
/**
* *
* @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[]>([])
2022-02-07 20:55:40 +08:00
// * 右键处理
2022-03-28 17:17:44 +08:00
const handleContextMenu = (
e: MouseEvent,
// 右键对象
2022-08-14 01:04:03 +08:00
targetInstance?: CreateComponentType | CreateComponentGroupType,
2022-03-28 17:17:44 +08:00
// 判断函数
optionsHandle?: Function,
// 隐藏选项列表
hideOptionsList?: MenuEnum[],
// 挑选选项列表
pickOptionsList?: MenuEnum[]
2022-03-28 17:17:44 +08:00
) => {
2022-02-07 20:55:40 +08:00
e.stopPropagation()
e.preventDefault()
2022-02-07 20:55:40 +08:00
let target = e.target
while (target instanceof SVGElement) {
target = target.parentNode
}
// 展示列表
2022-02-07 20:55:40 +08:00
chartEditStore.setRightMenuShow(false)
2022-03-28 17:19:50 +08:00
// * 多选默认选项
if (chartEditStore.getTargetChart.selectId.length > 1) {
menuOptions.value = defaultMultiSelectOptions
} else {
// * 单选默认选项
menuOptions.value = defaultOptions
}
2022-03-28 17:32:49 +08:00
2022-08-14 01:04:03 +08:00
if (!targetInstance) {
menuOptions.value = pickOption(toRaw(menuOptions.value), defaultNoItemKeys)
2022-03-28 17:17:44 +08:00
}
if (hideOptionsList) {
2022-08-14 01:04:03 +08:00
menuOptions.value = hideOption([...defaultMultiSelectOptions, divider(), ...defaultOptions], hideOptionsList)
2022-03-28 17:17:44 +08:00
}
if (pickOptionsList) {
2022-08-14 01:04:03 +08:00
menuOptions.value = pickOption([...defaultMultiSelectOptions, divider(), ...defaultOptions], pickOptionsList)
2022-03-28 17:17:44 +08:00
}
if (optionsHandle) {
// 自定义函数能够拿到当前选项和所有选项
2022-08-09 19:50:03 +08:00
menuOptions.value = optionsHandle(
cloneDeep(toRaw(menuOptions.value)),
[...defaultMultiSelectOptions, ...defaultOptions],
2022-08-14 01:04:03 +08:00
targetInstance
2022-08-09 19:50:03 +08:00
)
2022-03-28 17:17:44 +08:00
}
2022-02-07 20:55:40 +08:00
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-03-28 17:17:44 +08:00
export const useContextMenu = () => {
2022-03-28 17:24:56 +08:00
// 设置默认项
menuOptions.value = defaultOptions
2022-01-27 22:30:35 +08:00
// * 失焦
2022-03-28 17:24:56 +08:00
const onClickOutSide = () => {
chartEditStore.setRightMenuShow(false)
2022-01-27 22:30:35 +08:00
}
// * 事件处理
const handleMenuSelect = (key: string) => {
chartEditStore.setRightMenuShow(false)
const targetItem: MenuOptionsItemType[] = menuOptions.value.filter((e: MenuOptionsItemType) => e.key === key)
2022-02-01 17:12:16 +08:00
2022-03-28 17:17:44 +08:00
menuOptions.value.forEach((e: MenuOptionsItemType) => {
2022-02-01 17:12:16 +08:00
if (e.key === key) {
if (e.fnHandle) {
e.fnHandle()
return
}
if (!targetItem) loadingError()
}
})
2022-01-27 22:30:35 +08:00
}
2022-01-27 22:30:35 +08:00
return {
2022-03-28 17:17:44 +08:00
menuOptions,
defaultOptions,
defaultMultiSelectOptions,
2022-01-27 22:30:35 +08:00
handleContextMenu,
2022-03-28 17:24:56 +08:00
onClickOutSide,
2022-01-27 22:30:35 +08:00
handleMenuSelect,
mousePosition: chartEditStore.getMousePosition
2022-01-27 22:30:35 +08:00
}
}