This commit is contained in:
奔跑的面条 2022-08-30 19:14:03 +08:00
commit 4eca4c607a
8 changed files with 77 additions and 9 deletions

View File

@ -12,6 +12,7 @@
"@types/color": "^3.0.3",
"@types/crypto-js": "^4.1.1",
"@types/keymaster": "^1.6.30",
"@types/lodash": "^4.14.184",
"animate.css": "^4.1.1",
"axios": "^0.27.2",
"color": "^4.2.3",

View File

@ -63,7 +63,7 @@ export enum FilterEnum {
export interface PublicConfigType {
id: string
isGroup: boolean
attr: { x: number; y: number; w: number; h: number; zIndex: number }
attr: { x: number; y: number; w: number; h: number; zIndex: number; offsetX: number; offsetY: number; }
styles: {
[FilterEnum.OPACITY]: number
[FilterEnum.SATURATE]: number

View File

@ -104,5 +104,5 @@ export class PublicGroupConfigClass extends publicConfig implements CreateCompon
// 标识
public id = getUUID()
// 基本信息
public attr = { w: 0, h: 0, x: 0, y: 0, zIndex: -1 }
public attr = { w: 0, h: 0, x: 0, y: 0, offsetX: 0, offsetY: 0, zIndex: -1 }
}

View File

@ -25,7 +25,10 @@ export const chartInitConfig = {
x: 50,
y: 50,
w: 500,
h: 300
h: 300,
// 不建议动 offset
offsetX: 0,
offsetY: 0,
}
// dialog 图标的大小

View File

@ -313,6 +313,28 @@ export const useChartEditStore = defineStore({
loadingError()
}
},
// * 重置组件位置
resetComponentPosition(item: CreateComponentType | CreateComponentGroupType, isForward: boolean): void {
const index = this.fetchTargetIndex(item.id)
if (index > -1) {
const componentInstance = this.getComponentList[index]
if (isForward) {
componentInstance.attr = Object.assign(componentInstance.attr, {
x: item.attr.x + item.attr.offsetX,
y: item.attr.y + item.attr.offsetY
})
} else {
componentInstance.attr = Object.assign(componentInstance.attr, {
x: item.attr.x,
y: item.attr.y
})
}
}
},
// * 移动组件
moveComponentList(item: Array<CreateComponentType | CreateComponentGroupType>) {
chartHistoryStore.createMoveHistory(item)
},
// * 更新组件列表某一项的值
updateComponentList(index: number, newData: CreateComponentType | CreateComponentGroupType) {
if (index < 1 && index > this.getComponentList.length) return
@ -530,6 +552,15 @@ export const useChartEditStore = defineStore({
return
}
// 处理移动
const isMove = HistoryItem.actionType === HistoryActionTypeEnum.MOVE
if (isMove) {
historyData.forEach(item => {
this.resetComponentPosition(item, isForward)
})
return
}
// 处理层级
const isTop = HistoryItem.actionType === HistoryActionTypeEnum.TOP
const isBottom = HistoryItem.actionType === HistoryActionTypeEnum.BOTTOM
@ -562,12 +593,12 @@ export const useChartEditStore = defineStore({
if (isGroup || isUnGroup) {
if ((isGroup && isForward) || (isUnGroup && !isForward)) {
const ids: string[] = []
if(historyData.length > 1) {
if (historyData.length > 1) {
historyData.forEach(item => {
ids.push(item.id)
})
} else {
(historyData[0] as CreateComponentGroupType).groupList.forEach(item => {
;(historyData[0] as CreateComponentGroupType).groupList.forEach(item => {
ids.push(item.id)
})
}
@ -575,7 +606,7 @@ export const useChartEditStore = defineStore({
return
}
// 都需使用子组件的id去解组
if(historyData.length > 1) {
if (historyData.length > 1) {
this.setUnGroup([(historyData[0] as CreateComponentType).id], undefined, false)
} else {
this.setUnGroup([(historyData[0] as CreateComponentGroupType).groupList[0].id], undefined, false)

View File

@ -85,6 +85,8 @@ const canvasPositionList = computed(() => {
h: cloneDeep(chartEditStore.getEditCanvasConfig.height),
x: 0,
y: 0,
offsetX: 0,
offsetY: 0,
zIndex: 0
}
}

View File

@ -41,7 +41,10 @@ watch(
//
w: 0,
//
h: 0
h: 0,
//
offsetX: 0,
offsetY: 0
}
//

View File

@ -7,7 +7,7 @@ import { useContextMenu } from '@/views/chart/hooks/useContextMenu.hook'
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
import { EditCanvasTypeEnum } from '@/store/modules/chartEditStore/chartEditStore.d'
import { loadingStart, loadingFinish, loadingError } from '@/utils'
import throttle from 'lodash/throttle'
import { throttle, cloneDeep } from 'lodash'
const chartEditStore = useChartEditStore()
const { onClickOutSide } = useContextMenu()
@ -111,7 +111,7 @@ export const mousedownBoxSelect = (e: MouseEvent, item?: CreateComponentType | C
selectAttr.x1 = Math.round(startOffsetX - (startScreenX - moveEvent.screenX) / scale)
selectAttr.y1 = startOffsetY
selectAttr.x2 = startOffsetX
selectAttr.y2 = Math.round(startOffsetY + (moveEvent.screenY - startScreenY ) / scale)
selectAttr.y2 = Math.round(startOffsetY + (moveEvent.screenY - startScreenY) / scale)
// 左下方向
} else {
// 左上方向
@ -222,6 +222,16 @@ export const useMouseHandle = () => {
const startX = e.screenX
const startY = e.screenY
// 记录历史位置
let prevComponentInstance: Array<CreateComponentType | CreateComponentGroupType> = []
chartEditStore.getTargetChart.selectId.forEach(id => {
if (!targetMap.has(id)) return
const index = chartEditStore.fetchTargetIndex(id)
// 拿到初始位置数据
prevComponentInstance.push(cloneDeep(chartEditStore.getComponentList[index]))
})
// 记录初始位置
chartEditStore.setMousePosition(undefined, undefined, startX, startY)
@ -267,6 +277,24 @@ export const useMouseHandle = () => {
const mouseup = () => {
chartEditStore.setMousePosition(0, 0, 0, 0)
chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false)
// 加入历史栈
if (prevComponentInstance.length) {
chartEditStore.getTargetChart.selectId.forEach(id => {
if (!targetMap.has(id)) return
const index = chartEditStore.fetchTargetIndex(id)
const curComponentInstance = chartEditStore.getComponentList[index]
// 找到记录的所选组件
prevComponentInstance.forEach(preItem => {
if (preItem.id === id) {
preItem.attr = Object.assign(preItem.attr, {
offsetX: curComponentInstance.attr.x - preItem.attr.x,
offsetY: curComponentInstance.attr.y - preItem.attr.y
})
}
})
})
chartEditStore.moveComponentList(prevComponentInstance)
}
document.removeEventListener('mousemove', mousemove)
document.removeEventListener('mouseup', mouseup)
}