feat: 新增置顶和置底功能

This commit is contained in:
MTrun 2022-02-01 17:12:16 +08:00
parent b9501cee37
commit 7ec0af42b8
5 changed files with 150 additions and 34 deletions

View File

@ -37,7 +37,9 @@ import {
Flash as FlashIcon, Flash as FlashIcon,
SettingsSharp as SettingsSharpIcon, SettingsSharp as SettingsSharpIcon,
Home as HomeIcon, Home as HomeIcon,
Card as CardIcon Card as CardIcon,
ChevronUp as ChevronUpIcon,
ChevronDown as ChevronDownIcon
} from '@vicons/ionicons5' } from '@vicons/ionicons5'
import { import {
@ -48,7 +50,9 @@ import {
Store as StoreIcon, Store as StoreIcon,
Devices as DevicesIcon, Devices as DevicesIcon,
ObjectStorage as ObjectStorageIcon, ObjectStorage as ObjectStorageIcon,
DicomOverlay as DicomOverlayIcon DicomOverlay as DicomOverlayIcon,
UpToTop as UpToTopIcon,
DownToBottom as DownToBottomIcon,
} from '@vicons/carbon' } from '@vicons/carbon'
const ionicons5 = { const ionicons5 = {
@ -128,7 +132,11 @@ const ionicons5 = {
// 回退 // 回退
HomeIcon, HomeIcon,
// 控件(卡片) // 控件(卡片)
CardIcon CardIcon,
// 上移
ChevronUpIcon,
// 下移
ChevronDownIcon,
} }
const carbon = { const carbon = {
@ -147,7 +155,11 @@ const carbon = {
// 我的模板 // 我的模板
ObjectStorageIcon, ObjectStorageIcon,
// 键盘 // 键盘
DicomOverlayIcon DicomOverlayIcon,
// 置顶
UpToTopIcon,
// 置底
DownToBottomIcon
} }
// https://www.xicons.org/#/ 还有很多 // https://www.xicons.org/#/ 还有很多

View File

@ -82,25 +82,70 @@ export const useChartEditStoreStore = defineStore({
setTargetSelectChart(selectId?:TargetChartType["selectId"]) { setTargetSelectChart(selectId?:TargetChartType["selectId"]) {
this.targetChart.selectId = selectId this.targetChart.selectId = selectId
}, },
// * 找到目标 id 数据下标位置
fetchTargetIndex(): number {
const index = this.componentList.findIndex(e => e.id === this.getTargetChart.selectId)
if (index === -1) {
window['$message'].success(`操作失败,无法找到此元素`)
loadingError()
}
return index
},
// * 新增组件列表 // * 新增组件列表
addComponentList<T>(chartData: T): void { addComponentList<T>(chartData: T, isEnd = false): void {
if(isEnd) {
this.componentList.unshift(chartData)
return
}
this.componentList.push(chartData) this.componentList.push(chartData)
}, },
// * 删除组件列表 // * 删除组件列表
removeComponentList(): void { removeComponentList(): void {
loadingStart()
try { try {
const i = this.componentList.findIndex(e => e.id === this.getTargetChart.selectId) loadingStart()
if (i !== -1) { const index = this.fetchTargetIndex()
this.componentList.splice(i, 1) if (index !== -1) {
this.componentList.splice(index, 1)
loadingFinish() loadingFinish()
return return
} }
window['$message'].success(`图表删除失败,无法找到此元素`)
} catch(value) { } catch(value) {
loadingError() loadingError()
} }
}, },
// * 移动数组位置到两端
setBothEnds(isEnd = false): void {
try {
loadingStart()
const length = this.getComponentList.length
if(length < 2) return
const index = this.fetchTargetIndex()
if (index !== -1) {
// 置底排除最底层, 置顶排除最顶层
if((isEnd && index === 0) || (!isEnd && index === length - 1 )) {
loadingFinish()
return
}
// 插入两端
this.addComponentList(this.getComponentList[index], isEnd)
this.getComponentList.splice(isEnd ? index + 1: index, 1)
loadingFinish()
return
}
} catch(value) {
loadingError()
}
},
// * 置顶
setTop(): void {
this.setBothEnds()
},
// * 置底
setBottom(): void {
this.setBothEnds(true)
},
// * 设置页面样式属性 // * 设置页面样式属性
setPageStyle<T extends keyof CSSStyleDeclaration>( setPageStyle<T extends keyof CSSStyleDeclaration>(
key: T, key: T,

View File

@ -6,6 +6,9 @@ import { ConfigType } from '@/packages/index.d'
import { loadingStart, loadingFinish, loadingError } from '@/utils' import { loadingStart, loadingFinish, loadingError } from '@/utils'
import { CreateComponentType } from '@/packages/index.d' import { CreateComponentType } from '@/packages/index.d'
import throttle from 'lodash/throttle' import throttle from 'lodash/throttle'
import { useContextMenu } from '@/views/chart/hooks/useContextMenu.hook'
const { onClickoutside } = useContextMenu()
const chartEditStore = getChartEditStore() const chartEditStore = getChartEditStore()
const { scale } = toRefs(chartEditStore.getEditCanvas) const { scale } = toRefs(chartEditStore.getEditCanvas)
@ -43,7 +46,7 @@ export const handleDragOver = (e: DragEvent) => {
} }
// 不拦截默认行为点击 // * 不拦截默认行为点击
export const mousedownHandleUnStop = ( export const mousedownHandleUnStop = (
e: MouseEvent, e: MouseEvent,
item?: CreateComponentType item?: CreateComponentType
@ -55,13 +58,14 @@ export const mousedownHandleUnStop = (
chartEditStore.setTargetSelectChart(undefined) chartEditStore.setTargetSelectChart(undefined)
} }
// 移动图表 // * 移动图表
export const useMouseHandle = () => { export const useMouseHandle = () => {
// 点击事件(包含移动事件) // 点击事件(包含移动事件)
const mousedownHandle = (e: MouseEvent, item: CreateComponentType) => { const mousedownHandle = (e: MouseEvent, item: CreateComponentType) => {
e.preventDefault() e.preventDefault()
e.stopPropagation() e.stopPropagation()
onClickoutside()
chartEditStore.setTargetSelectChart(item.id) chartEditStore.setTargetSelectChart(item.id)
const scale = chartEditStore.getEditCanvas.scale const scale = chartEditStore.getEditCanvas.scale
const width = chartEditStore.getEditCanvas.width const width = chartEditStore.getEditCanvas.width
@ -82,8 +86,8 @@ export const useMouseHandle = () => {
// 计算偏移量(处理 scale 比例问题) // 计算偏移量(处理 scale 比例问题)
const mousemove = throttle((moveEvent: MouseEvent) => { const mousemove = throttle((moveEvent: MouseEvent) => {
let currX = itemAttrX + (moveEvent.screenX - startX) / scale let currX = Math.round(itemAttrX + (moveEvent.screenX - startX) / scale)
let currY = itemAttrY + (moveEvent.screenY - startY) / scale let currY = Math.round(itemAttrY + (moveEvent.screenY - startY) / scale)
// 要预留的距离 // 要预留的距离
const distance = 50 const distance = 50
@ -108,14 +112,14 @@ export const useMouseHandle = () => {
editcontentDom!.addEventListener('mouseup', mouseup) editcontentDom!.addEventListener('mouseup', mouseup)
} }
// 进入事件 // * 进入事件
const mouseenterHandle = (e: MouseEvent, item: CreateComponentType) => { const mouseenterHandle = (e: MouseEvent, item: CreateComponentType) => {
e.preventDefault() e.preventDefault()
e.stopPropagation() e.stopPropagation()
chartEditStore.setTargetHoverChart(item.id) chartEditStore.setTargetHoverChart(item.id)
} }
// 移出事件 // * 移出事件
const mouseleaveHandle = (e: MouseEvent, item: CreateComponentType) => { const mouseleaveHandle = (e: MouseEvent, item: CreateComponentType) => {
e.preventDefault() e.preventDefault()
e.stopPropagation() e.stopPropagation()

View File

@ -9,7 +9,7 @@ export const useComponentStyle = (attr: AttrType, index: number) => {
const componentStyle = { const componentStyle = {
zIndex: index + 1, zIndex: index + 1,
left: `${attr.x}px`, left: `${attr.x}px`,
top: `${attr.y}px`, top: `${attr.y}px`
} }
return componentStyle return componentStyle
} }

View File

@ -1,19 +1,75 @@
import { ref, nextTick } from 'vue' import { ref, nextTick } from 'vue'
import { useChartEditStoreStore } from '@/store/modules/chartEditStore/chartEditStore' import { useChartEditStoreStore } from '@/store/modules/chartEditStore/chartEditStore'
import { CreateComponentType } from '@/packages/index.d' import { CreateComponentType } from '@/packages/index.d'
import { loadingError } from '@/utils' import { renderIcon, loadingError } from '@/utils'
import { icon } from '@/plugins'
const { CopyIcon, TrashIcon, ChevronDownIcon, ChevronUpIcon } = icon.ionicons5
const { UpToTopIcon, DownToBottomIcon } = icon.carbon
const chartEditStore = useChartEditStoreStore() const chartEditStore = useChartEditStoreStore()
enum MenuEnum { enum MenuEnum {
DELETE = 'delete' DELETE = 'delete',
COPY = 'copy',
TOP = 'top',
BOTTOM = 'bottom',
UP = 'up',
DOWN = 'down'
} }
export interface MenuOptionsItemType { export interface MenuOptionsItemType {
label: string type?: string
key: MenuEnum label?: string
fnHandle: Function key: MenuEnum | string
icon?: Function
fnHandle?: Function
} }
// * 默认选项
const defaultOptions: MenuOptionsItemType[] = [
{
label: '删除',
key: MenuEnum.DELETE,
icon: renderIcon(TrashIcon),
fnHandle: chartEditStore.removeComponentList
},
{
label: '复制',
key: MenuEnum.COPY,
icon: renderIcon(CopyIcon),
fnHandle: () => {}
},
{
type: 'divider',
key: 'd1'
},
{
label: '置顶',
key: MenuEnum.TOP,
icon: renderIcon(UpToTopIcon),
fnHandle: chartEditStore.setTop
},
{
label: '置底',
key: MenuEnum.BOTTOM,
icon: renderIcon(DownToBottomIcon),
fnHandle: chartEditStore.setBottom
},
{
label: '上移一层',
key: MenuEnum.UP,
icon: renderIcon(ChevronUpIcon),
fnHandle: () => {}
},
{
label: '下移一层',
key: MenuEnum.DOWN,
icon: renderIcon(ChevronDownIcon),
fnHandle: () => {}
}
]
/** /**
* * hook * * hook
* @param menuOption * @param menuOption
@ -28,15 +84,6 @@ export const useContextMenu = (menuOption?: {
const selfOptions = menuOption?.selfOptions const selfOptions = menuOption?.selfOptions
const optionsHandle = menuOption?.optionsHandle const optionsHandle = menuOption?.optionsHandle
// * 默认选项
const defaultOptions: MenuOptionsItemType[] = [
{
label: '删除',
key: MenuEnum.DELETE,
fnHandle: chartEditStore.removeComponentList
}
]
// * 右键选项 // * 右键选项
const menuOptions: MenuOptionsItemType[] = selfOptions || defaultOptions const menuOptions: MenuOptionsItemType[] = selfOptions || defaultOptions
@ -56,7 +103,7 @@ export const useContextMenu = (menuOption?: {
} }
// * 失焦 // * 失焦
const onClickoutside = (e: MouseEvent) => { const onClickoutside = () => {
chartEditStore.setRightMenuShow(false) chartEditStore.setRightMenuShow(false)
} }
@ -66,8 +113,16 @@ export const useContextMenu = (menuOption?: {
const targetItem: MenuOptionsItemType[] = menuOptions.filter( const targetItem: MenuOptionsItemType[] = menuOptions.filter(
(e: MenuOptionsItemType) => e.key === key (e: MenuOptionsItemType) => e.key === key
) )
menuOptions.forEach((e: MenuOptionsItemType) => {
if (e.key === key) {
if (e.fnHandle) {
e.fnHandle()
return
}
if (!targetItem) loadingError() if (!targetItem) loadingError()
if (targetItem.length) targetItem.pop()?.fnHandle() }
})
} }
return { return {