857 lines
27 KiB
TypeScript
Raw Normal View History

import { toRaw } from 'vue'
2022-01-19 21:29:04 +08:00
import { defineStore } from 'pinia'
import { CreateComponentType, CreateComponentGroupType } from '@/packages/index.d'
import { PublicGroupConfigClass } from '@/packages/public/publicConfig'
2022-02-03 22:54:31 +08:00
import debounce from 'lodash/debounce'
import cloneDeep from 'lodash/cloneDeep'
2022-02-21 19:45:11 +08:00
import { defaultTheme, globalThemeJson } from '@/settings/chartThemes/index'
2022-07-11 09:01:19 +08:00
import { requestInterval, previewScaleType, requestIntervalUnit } from '@/settings/designSetting'
2022-02-06 01:04:05 +08:00
// 记录记录
import { useChartHistoryStore } from '@/store/modules/chartHistoryStore/chartHistoryStore'
// 全局设置
import { useSettingStore } from '@/store/modules/settingStore/settingStore'
2022-08-17 16:18:00 +08:00
import {
HistoryActionTypeEnum,
HistoryItemType,
HistoryTargetTypeEnum
} from '@/store/modules/chartHistoryStore/chartHistoryStore.d'
import { MenuEnum } from '@/enums/editPageEnum'
2022-08-17 16:18:00 +08:00
import { getUUID, loadingStart, loadingFinish, loadingError, isString, isArray } from '@/utils'
2022-01-20 21:25:35 +08:00
import {
2022-03-06 02:08:14 +08:00
ChartEditStoreEnum,
ChartEditStorage,
ChartEditStoreType,
2022-01-20 21:25:35 +08:00
EditCanvasType,
2022-01-29 21:44:22 +08:00
MousePositionType,
2022-02-04 12:17:50 +08:00
TargetChartType,
2022-02-06 01:04:05 +08:00
RecordChartType,
RequestGlobalConfigType,
2022-02-06 01:04:05 +08:00
EditCanvasConfigType
2022-01-20 21:25:35 +08:00
} from './chartEditStore.d'
2022-01-19 21:29:04 +08:00
const chartHistoryStore = useChartHistoryStore()
const settingStore = useSettingStore()
2022-02-03 22:54:31 +08:00
2022-01-20 21:25:35 +08:00
// 编辑区域内容
2022-03-04 20:57:36 +08:00
export const useChartEditStore = defineStore({
id: 'useChartEditStore',
2022-03-06 02:08:14 +08:00
state: (): ChartEditStoreType => ({
2022-02-06 01:04:05 +08:00
// 画布属性
2022-01-20 21:25:35 +08:00
editCanvas: {
// 编辑区域 Dom
2022-01-24 14:28:31 +08:00
editLayoutDom: null,
editContentDom: null,
2022-01-20 21:25:35 +08:00
// 偏移量
offset: 20,
2022-02-04 12:19:02 +08:00
// 系统控制缩放
2022-01-20 21:25:35 +08:00
scale: 1,
2022-01-20 22:13:51 +08:00
// 用户控制的缩放
userScale: 1,
2022-01-20 21:25:35 +08:00
// 锁定缩放
lockScale: false,
2022-04-11 18:17:09 +08:00
// 初始化
isCreate: false,
// 拖拽中
2022-08-18 20:30:02 +08:00
isDrag: false,
// 框选中
isSelect: false
2022-02-06 01:04:05 +08:00
},
2022-02-21 19:45:11 +08:00
// 右键菜单
rightMenuShow: false,
// 鼠标定位
mousePosition: {
2022-03-04 20:57:36 +08:00
startX: 0,
startY: 0,
2022-02-21 19:45:11 +08:00
x: 0,
y: 0
},
// 目标图表
targetChart: {
hoverId: undefined,
selectId: []
2022-02-21 19:45:11 +08:00
},
// 记录临时数据(复制等)
recordChart: undefined,
2022-04-10 17:56:51 +08:00
// -----------------------
2022-02-06 01:04:05 +08:00
// 画布属性(需存储给后端)
editCanvasConfig: {
// 默认宽度
width: 1920,
// 默认高度
height: 1080,
// 色相
hueRotate: 0,
// 饱和度
2022-04-25 16:17:22 +08:00
saturate: 1,
2022-02-06 01:04:05 +08:00
// 对比度
2022-04-25 16:17:22 +08:00
contrast: 1,
// 亮度
brightness: 1,
// 透明度
opacity: 1,
// 变换(暂不更改)
rotateZ: 0,
rotateX: 0,
rotateY: 0,
skewX: 0,
skewY: 0,
2022-01-20 21:25:35 +08:00
// 默认背景色
2022-02-06 01:04:05 +08:00
background: undefined,
2022-02-06 21:35:38 +08:00
backgroundImage: undefined,
// 是否使用纯颜色
selectColor: true,
2022-02-06 01:04:05 +08:00
// chart 主题色
2022-02-21 19:45:11 +08:00
chartThemeColor: defaultTheme || 'dark',
// 全局配置
2022-05-04 01:46:56 +08:00
chartThemeSetting: globalThemeJson,
// 预览方式
previewScaleType: previewScaleType
2022-01-20 21:25:35 +08:00
},
2022-03-22 10:33:40 +08:00
// 数据请求处理(需存储给后端)
requestGlobalConfig: {
requestOriginUrl: '',
2022-07-11 09:01:19 +08:00
requestInterval: requestInterval,
requestIntervalUnit: requestIntervalUnit,
requestParams: {
Body: {
2022-08-17 16:18:00 +08:00
'form-data': {},
'x-www-form-urlencoded': {},
2022-07-15 00:11:42 +08:00
json: '',
2022-07-11 09:01:19 +08:00
xml: ''
},
Header: {},
Params: {}
}
2022-03-17 20:18:46 +08:00
},
2022-02-06 01:04:05 +08:00
// 图表数组(需存储给后端)
2022-01-24 21:12:18 +08:00
componentList: []
2022-01-20 21:25:35 +08:00
}),
getters: {
getMousePosition(): MousePositionType {
return this.mousePosition
},
getRightMenuShow(): boolean {
return this.rightMenuShow
},
2022-01-20 21:25:35 +08:00
getEditCanvas(): EditCanvasType {
return this.editCanvas
},
2022-02-06 01:04:05 +08:00
getEditCanvasConfig(): EditCanvasConfigType {
return this.editCanvasConfig
},
2022-08-15 16:50:52 +08:00
getTargetChart(): TargetChartType {
2022-01-29 21:44:22 +08:00
return this.targetChart
},
2022-02-04 12:17:50 +08:00
getRecordChart(): RecordChartType | undefined {
return this.recordChart
2022-02-03 22:54:31 +08:00
},
getRequestGlobalConfig(): RequestGlobalConfigType {
return this.requestGlobalConfig
2022-03-17 20:18:46 +08:00
},
getComponentList(): Array<CreateComponentType | CreateComponentGroupType> {
2022-01-24 21:12:18 +08:00
return this.componentList
2022-03-06 02:08:14 +08:00
},
// 获取需要存储的数据项
getStorageInfo(): ChartEditStorage {
return {
[ChartEditStoreEnum.EDIT_CANVAS_CONFIG]: this.getEditCanvasConfig,
2022-03-17 20:18:46 +08:00
[ChartEditStoreEnum.COMPONENT_LIST]: this.getComponentList,
[ChartEditStoreEnum.REQUEST_GLOBAL_CONFIG]: this.getRequestGlobalConfig
2022-03-06 02:08:14 +08:00
}
2022-01-25 18:19:44 +08:00
}
2022-01-20 21:25:35 +08:00
},
actions: {
2022-01-29 21:44:22 +08:00
// * 设置 editCanvas 数据项
2022-08-17 16:18:00 +08:00
setEditCanvas<T extends keyof EditCanvasType, K extends EditCanvasType[T]>(key: T, value: K) {
2022-01-29 21:44:22 +08:00
this.editCanvas[key] = value
},
2022-03-06 02:08:14 +08:00
// * 设置 editCanvasConfig需保存后端 数据项
2022-08-17 16:18:00 +08:00
setEditCanvasConfig<T extends keyof EditCanvasConfigType, K extends EditCanvasConfigType[T]>(key: T, value: K) {
2022-02-06 01:04:05 +08:00
this.editCanvasConfig[key] = value
},
// * 设置右键菜单
setRightMenuShow(value: boolean) {
this.rightMenuShow = value
},
2022-01-29 23:58:56 +08:00
// * 设置目标数据 hover
2022-08-17 16:18:00 +08:00
setTargetHoverChart(hoverId?: TargetChartType['hoverId']) {
2022-02-01 01:20:00 +08:00
this.targetChart.hoverId = hoverId
2022-01-29 21:44:22 +08:00
},
2022-01-29 23:58:56 +08:00
// * 设置目标数据 select
setTargetSelectChart(selectId?: string | string[], push: boolean = false) {
// 重复选中
if (this.targetChart.selectId.find((e: string) => e === selectId)) return
// 无 id 清空
if (!selectId) {
this.targetChart.selectId = []
return
}
// 多选
if (push) {
// 字符串
if (isString(selectId)) {
this.targetChart.selectId.push(selectId)
return
}
// 数组
if (isArray(selectId)) {
this.targetChart.selectId.push(...selectId)
return
}
} else {
// 字符串
if (isString(selectId)) {
this.targetChart.selectId = [selectId]
return
}
// 数组
if (isArray(selectId)) {
this.targetChart.selectId = selectId
return
}
}
2022-01-29 21:44:22 +08:00
},
2022-02-03 22:54:31 +08:00
// * 设置记录数据
2022-02-04 12:17:50 +08:00
setRecordChart(item: RecordChartType | undefined) {
this.recordChart = cloneDeep(item)
2022-02-03 22:54:31 +08:00
},
2022-03-04 20:57:36 +08:00
// * 设置鼠标位置
setMousePosition(x?: number, y?: number, startX?: number, startY?: number): void {
if (x) this.mousePosition.x = x
if (y) this.mousePosition.y = y
2022-08-18 20:30:02 +08:00
if (startX) this.mousePosition.startX = startX
if (startY) this.mousePosition.startY = startY
2022-03-04 20:57:36 +08:00
},
// * 找到目标 id 数据的下标位置id可为父级或子集数组无则返回-1
2022-04-03 15:06:32 +08:00
fetchTargetIndex(id?: string): number {
2022-08-17 16:18:00 +08:00
const targetId = id || (this.getTargetChart.selectId.length && this.getTargetChart.selectId[0]) || undefined
if (!targetId) {
2022-02-25 21:26:56 +08:00
loadingFinish()
return -1
}
const targetIndex = this.componentList.findIndex(e => e.id === targetId)
// 当前
if (targetIndex !== -1) {
return targetIndex
} else {
const length = this.getComponentList.length
2022-08-17 16:18:00 +08:00
for (let i = 0; i < length; i++) {
if (this.getComponentList[i].isGroup) {
2022-08-17 16:18:00 +08:00
for (const cItem of (this.getComponentList[i] as CreateComponentGroupType).groupList) {
if (cItem.id === targetId) {
return i
}
}
}
}
}
return -1
},
2022-08-15 16:50:52 +08:00
// * 统一格式化处理入参 id
idPreFormat(id?: string | string[]) {
const idArr = []
if (!id) {
idArr.push(...this.getTargetChart.selectId)
return idArr
2022-08-17 16:18:00 +08:00
}
2022-08-15 16:50:52 +08:00
if (isString(id)) idArr.push(id)
if (isArray(id)) idArr.push(...id)
return idArr
},
2022-02-03 22:54:31 +08:00
/**
* *
* @param componentInstance
2022-04-03 15:06:32 +08:00
* @param isHead
2022-02-03 22:54:31 +08:00
* @param isHistory
2022-02-04 12:19:02 +08:00
* @returns
2022-02-03 22:54:31 +08:00
*/
2022-08-17 16:18:00 +08:00
addComponentList(
componentInstance:
| CreateComponentType
| CreateComponentGroupType
2022-08-17 16:18:00 +08:00
| Array<CreateComponentType | CreateComponentGroupType>,
isHead = false,
isHistory = false
): void {
2022-08-17 16:18:00 +08:00
if (componentInstance instanceof Array) {
componentInstance.forEach(item => {
this.addComponentList(item, isHead, isHistory)
})
return
}
2022-02-04 18:28:02 +08:00
if (isHistory) {
chartHistoryStore.createAddHistory([componentInstance])
2022-02-03 22:54:31 +08:00
}
2022-04-03 15:06:32 +08:00
if (isHead) {
this.componentList.unshift(componentInstance)
2022-02-01 17:12:16 +08:00
return
}
this.componentList.push(componentInstance)
2022-01-24 21:12:18 +08:00
},
2022-08-15 16:50:52 +08:00
// * 删除组件
2022-08-17 16:18:00 +08:00
removeComponentList(id?: string | string[], isHistory = true): void {
2022-01-27 23:16:51 +08:00
try {
2022-08-15 16:50:52 +08:00
const idArr = this.idPreFormat(id)
const history: Array<CreateComponentType | CreateComponentGroupType> = []
2022-08-15 16:50:52 +08:00
// 遍历所有对象
if (!idArr.length) return
2022-08-17 16:18:00 +08:00
loadingStart()
2022-08-15 16:50:52 +08:00
idArr.forEach(ids => {
2022-08-17 16:18:00 +08:00
const index = this.fetchTargetIndex(ids)
2022-08-15 16:50:52 +08:00
if (index !== -1) {
history.push(this.getComponentList[index])
2022-08-15 16:50:52 +08:00
this.componentList.splice(index, 1)
}
})
isHistory && chartHistoryStore.createDeleteHistory(history)
2022-08-15 16:50:52 +08:00
loadingFinish()
return
2022-08-17 16:18:00 +08:00
} catch (value) {
2022-02-01 17:12:16 +08:00
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)
},
2022-02-01 20:57:54 +08:00
// * 更新组件列表某一项的值
updateComponentList(index: number, newData: CreateComponentType | CreateComponentGroupType) {
2022-02-04 18:28:02 +08:00
if (index < 1 && index > this.getComponentList.length) return
2022-02-01 20:57:54 +08:00
this.componentList[index] = newData
},
2022-02-03 22:54:31 +08:00
// * 设置页面样式属性
2022-08-17 16:18:00 +08:00
setPageStyle<T extends keyof CSSStyleDeclaration>(key: T, value: any): void {
2022-02-03 22:54:31 +08:00
const dom = this.getEditCanvas.editContentDom
if (dom) {
dom.style[key] = value
}
},
2022-02-25 21:26:56 +08:00
// * 移动组件列表层级位置到两端
2022-02-04 18:28:02 +08:00
setBothEnds(isEnd = false, isHistory = true): void {
2022-02-01 17:12:16 +08:00
try {
2022-08-15 16:50:52 +08:00
// 暂不支持多选
if (this.getTargetChart.selectId.length > 1) return
2022-02-01 17:12:16 +08:00
loadingStart()
const length = this.getComponentList.length
2022-02-04 18:28:02 +08:00
if (length < 2) {
2022-02-01 20:57:54 +08:00
loadingFinish()
return
}
2022-02-01 17:12:16 +08:00
2022-08-17 16:18:00 +08:00
const index = this.fetchTargetIndex()
2022-02-04 18:28:02 +08:00
const targetData = this.getComponentList[index]
2022-02-01 17:12:16 +08:00
if (index !== -1) {
// 置底排除最底层, 置顶排除最顶层
2022-08-17 16:18:00 +08:00
if ((isEnd && index === 0) || (!isEnd && index === length - 1)) {
2022-02-01 17:12:16 +08:00
loadingFinish()
return
}
2022-02-04 18:28:02 +08:00
// 记录原有位置
2022-08-17 16:18:00 +08:00
const setIndex = (componentInstance: CreateComponentType | CreateComponentGroupType, i: number) => {
const temp = cloneDeep(componentInstance)
2022-02-04 18:28:02 +08:00
temp.attr.zIndex = i
return temp
}
// 历史记录
if (isHistory) {
2022-07-21 14:38:32 +08:00
chartHistoryStore.createLayerHistory(
[setIndex(targetData, index)],
2022-02-04 18:28:02 +08:00
isEnd ? HistoryActionTypeEnum.BOTTOM : HistoryActionTypeEnum.TOP
)
}
2022-02-01 17:12:16 +08:00
// 插入两端
2022-02-04 18:28:02 +08:00
this.addComponentList(targetData, isEnd)
2022-08-17 16:18:00 +08:00
this.getComponentList.splice(isEnd ? index + 1 : index, 1)
2022-01-27 23:16:51 +08:00
loadingFinish()
return
}
2022-08-17 16:18:00 +08:00
} catch (value) {
2022-01-27 23:16:51 +08:00
loadingError()
2022-01-27 22:30:35 +08:00
}
2022-01-25 18:19:44 +08:00
},
2022-02-01 17:12:16 +08:00
// * 置顶
2022-02-04 18:28:02 +08:00
setTop(isHistory = true): void {
this.setBothEnds(false, isHistory)
2022-02-01 17:12:16 +08:00
},
// * 置底
2022-02-04 18:28:02 +08:00
setBottom(isHistory = true): void {
this.setBothEnds(true, isHistory)
2022-02-01 17:12:16 +08:00
},
2022-04-03 15:06:32 +08:00
// * 上移/下移互换图表位置
2022-02-04 18:28:02 +08:00
wrap(isDown = false, isHistory = true) {
2022-02-01 20:57:54 +08:00
try {
2022-08-15 16:50:52 +08:00
// 暂不支持多选
if (this.getTargetChart.selectId.length > 1) return
2022-02-01 20:57:54 +08:00
loadingStart()
const length = this.getComponentList.length
2022-02-04 12:19:02 +08:00
if (length < 2) {
2022-02-01 20:57:54 +08:00
loadingFinish()
return
}
2022-08-17 16:18:00 +08:00
const index: number = this.fetchTargetIndex()
2022-02-01 20:57:54 +08:00
if (index !== -1) {
// 下移排除最底层, 上移排除最顶层
if ((isDown && index === 0) || (!isDown && index === length - 1)) {
loadingFinish()
return
}
// 互换位置
const swapIndex = isDown ? index - 1 : index + 1
const targetItem = this.getComponentList[index]
const swapItem = this.getComponentList[swapIndex]
2022-02-04 12:19:02 +08:00
2022-02-04 18:28:02 +08:00
// 历史记录
if (isHistory) {
2022-07-21 14:38:32 +08:00
chartHistoryStore.createLayerHistory(
[targetItem],
2022-02-04 18:28:02 +08:00
isDown ? HistoryActionTypeEnum.DOWN : HistoryActionTypeEnum.UP
)
}
2022-02-01 20:57:54 +08:00
this.updateComponentList(index, swapItem)
this.updateComponentList(swapIndex, targetItem)
loadingFinish()
return
}
2022-08-17 16:18:00 +08:00
} catch (value) {
2022-02-01 20:57:54 +08:00
loadingError()
}
},
// * 图层上移
2022-02-04 18:28:02 +08:00
setUp(isHistory = true) {
this.wrap(false, isHistory)
2022-02-01 20:57:54 +08:00
},
// * 图层下移
2022-02-04 18:28:02 +08:00
setDown(isHistory = true) {
this.wrap(true, isHistory)
2022-02-01 20:57:54 +08:00
},
2022-02-03 22:54:31 +08:00
// * 复制
2022-02-04 12:17:50 +08:00
setCopy(isCut = false) {
2022-02-03 22:54:31 +08:00
try {
2022-08-15 16:50:52 +08:00
// 暂不支持多选
if (this.getTargetChart.selectId.length > 1) return
// 处理弹窗普通复制的场景
if (document.getElementsByClassName('n-modal-body-wrapper').length) return
2022-08-15 16:50:52 +08:00
2022-02-03 22:54:31 +08:00
loadingStart()
2022-08-17 16:18:00 +08:00
const index: number = this.fetchTargetIndex()
2022-02-03 22:54:31 +08:00
if (index !== -1) {
2022-08-17 16:18:00 +08:00
const copyData: RecordChartType = {
charts: this.getComponentList[index],
type: isCut ? HistoryActionTypeEnum.CUT : HistoryActionTypeEnum.COPY
2022-02-04 12:17:50 +08:00
}
this.setRecordChart(copyData)
2022-07-20 20:42:24 +08:00
window['$message'].success(isCut ? '剪切图表成功' : '复制图表成功!')
2022-02-03 22:54:31 +08:00
loadingFinish()
}
2022-08-17 16:18:00 +08:00
} catch (value) {
2022-02-03 22:54:31 +08:00
loadingError()
}
},
2022-02-04 12:17:50 +08:00
// * 剪切
setCut() {
this.setCopy(true)
},
2022-02-03 22:54:31 +08:00
// * 粘贴
setParse() {
try {
loadingStart()
2022-02-04 12:17:50 +08:00
const recordCharts = this.getRecordChart
2022-02-03 22:54:31 +08:00
if (recordCharts === undefined) {
loadingFinish()
return
}
const parseHandle = (e: CreateComponentType | CreateComponentGroupType) => {
2022-02-03 22:54:31 +08:00
e = cloneDeep(e)
// 生成新 id
e.id = getUUID()
e.attr.x = this.getMousePosition.x + 30
e.attr.y = this.getMousePosition.y + 30
2022-02-03 22:54:31 +08:00
return e
}
2022-02-04 12:17:50 +08:00
const isCut = recordCharts.type === HistoryActionTypeEnum.CUT
// 多项
if (Array.isArray(recordCharts.charts)) {
recordCharts.charts.forEach((e: CreateComponentType) => {
2022-02-03 22:54:31 +08:00
this.addComponentList(parseHandle(e), undefined, true)
2022-02-04 12:17:50 +08:00
// 剪切需删除原数据
if (isCut) {
this.setTargetSelectChart(e.id)
this.removeComponentList(undefined, true)
2022-02-04 12:17:50 +08:00
}
2022-02-03 22:54:31 +08:00
})
2022-02-04 12:17:50 +08:00
if (isCut) this.setRecordChart(undefined)
2022-02-03 22:54:31 +08:00
loadingFinish()
return
}
2022-02-04 12:17:50 +08:00
// 单项
this.addComponentList(parseHandle(recordCharts.charts), undefined, true)
2022-02-04 18:28:02 +08:00
if (isCut) {
2022-02-04 12:17:50 +08:00
this.setTargetSelectChart(recordCharts.charts.id)
this.removeComponentList()
this.setRecordChart(undefined)
}
2022-02-03 22:54:31 +08:00
loadingFinish()
2022-08-17 16:18:00 +08:00
} catch (value) {
2022-02-03 22:54:31 +08:00
loadingError()
}
},
2022-02-04 18:28:02 +08:00
// * 撤回/前进 目标处理
setBackAndSetForwardHandle(HistoryItem: HistoryItemType, isForward = false) {
2022-02-04 18:28:02 +08:00
// 处理画布
if (HistoryItem.targetType === HistoryTargetTypeEnum.CANVAS) {
this.editCanvas = HistoryItem.historyData[0] as EditCanvasType
2022-02-04 12:17:50 +08:00
return
}
2022-02-04 18:28:02 +08:00
let historyData = HistoryItem.historyData as Array<CreateComponentType | CreateComponentGroupType>
2022-08-17 16:18:00 +08:00
if (isArray(historyData)) {
// 选中目标元素,支持多个
historyData.forEach((item: CreateComponentType | CreateComponentGroupType) => {
this.setTargetSelectChart(item.id, true)
})
}
2022-02-04 18:28:02 +08:00
// 处理新增类型
const isAdd = HistoryItem.actionType === HistoryActionTypeEnum.ADD
const isDel = HistoryItem.actionType === HistoryActionTypeEnum.DELETE
2022-02-04 18:28:02 +08:00
if (isAdd || isDel) {
if ((isAdd && isForward) || (isDel && !isForward)) {
historyData.forEach(item => {
this.addComponentList(item)
})
return
2022-02-04 18:28:02 +08:00
}
historyData.forEach(item => {
this.removeComponentList(item.id, false)
})
2022-02-04 18:28:02 +08:00
return
}
// 处理移动
const isMove = HistoryItem.actionType === HistoryActionTypeEnum.MOVE
if (isMove) {
historyData.forEach(item => {
this.resetComponentPosition(item, isForward)
})
return
}
2022-02-04 18:28:02 +08:00
// 处理层级
const isTop = HistoryItem.actionType === HistoryActionTypeEnum.TOP
const isBottom = HistoryItem.actionType === HistoryActionTypeEnum.BOTTOM
2022-02-04 18:28:02 +08:00
if (isTop || isBottom) {
if (!isForward) {
// 插入到原有位置
if (isTop) this.getComponentList.pop()
if (isBottom) this.getComponentList.shift()
this.getComponentList.splice(historyData[0].attr.zIndex, 0, historyData[0])
2022-02-04 18:28:02 +08:00
return
}
if (isTop) this.setTop(false)
if (isBottom) this.setBottom(false)
}
const isUp = HistoryItem.actionType === HistoryActionTypeEnum.UP
const isDown = HistoryItem.actionType === HistoryActionTypeEnum.DOWN
2022-02-04 18:28:02 +08:00
if (isUp || isDown) {
if ((isUp && isForward) || (isDown && !isForward)) {
this.setUp(false)
return
}
this.setDown(false)
return
}
// 处理分组
const isGroup = HistoryItem.actionType === HistoryActionTypeEnum.GROUP
const isUnGroup = HistoryItem.actionType === HistoryActionTypeEnum.UN_GROUP
if (isGroup || isUnGroup) {
if ((isGroup && isForward) || (isUnGroup && !isForward)) {
2022-08-17 16:18:00 +08:00
const ids: string[] = []
if (historyData.length > 1) {
2022-08-17 16:18:00 +08:00
historyData.forEach(item => {
ids.push(item.id)
})
} else {
;(historyData[0] as CreateComponentGroupType).groupList.forEach(item => {
2022-08-17 16:18:00 +08:00
ids.push(item.id)
})
}
this.setGroup(ids, false)
return
}
2022-08-17 16:18:00 +08:00
// 都需使用子组件的id去解组
if (historyData.length > 1) {
2022-08-17 16:18:00 +08:00
this.setUnGroup([(historyData[0] as CreateComponentType).id], undefined, false)
} else {
this.setUnGroup([(historyData[0] as CreateComponentGroupType).groupList[0].id], undefined, false)
}
return
}
2022-02-04 12:17:50 +08:00
},
// * 撤回
setBack() {
try {
loadingStart()
const targetData = chartHistoryStore.backAction()
2022-02-04 12:17:50 +08:00
if (!targetData) {
loadingFinish()
return
}
this.setBackAndSetForwardHandle(targetData)
loadingFinish()
2022-08-17 16:18:00 +08:00
} catch (value) {
2022-02-04 12:17:50 +08:00
loadingError()
}
},
// * 前进
setForward() {
try {
loadingStart()
const targetData = chartHistoryStore.forwardAction()
2022-02-04 12:17:50 +08:00
if (!targetData) {
loadingFinish()
return
}
this.setBackAndSetForwardHandle(targetData, true)
loadingFinish()
2022-08-17 16:18:00 +08:00
} catch (value) {
2022-02-04 12:17:50 +08:00
loadingError()
}
},
// * 移动位置
setMove(keyboardValue: MenuEnum) {
2022-08-17 16:18:00 +08:00
const index = this.fetchTargetIndex()
if (index === -1) return
const attr = this.getComponentList[index].attr
const distance = settingStore.getChartMoveDistance
switch (keyboardValue) {
case MenuEnum.ARROW_UP:
attr.y -= distance
2022-08-17 16:18:00 +08:00
break
case MenuEnum.ARROW_RIGHT:
attr.x += distance
2022-08-17 16:18:00 +08:00
break
case MenuEnum.ARROW_DOWN:
attr.y += distance
2022-08-17 16:18:00 +08:00
break
case MenuEnum.ARROW_LEFT:
attr.x -= distance
2022-08-17 16:18:00 +08:00
break
}
},
// * 创建分组
2022-08-17 16:18:00 +08:00
setGroup(id?: string | string[], isHistory = true) {
2022-08-06 18:21:50 +08:00
try {
2022-08-17 16:18:00 +08:00
const selectIds = this.idPreFormat(id) || this.getTargetChart.selectId
if (selectIds.length < 2) return
2022-08-09 20:12:44 +08:00
2022-08-06 18:21:50 +08:00
loadingStart()
const groupClass = new PublicGroupConfigClass()
2022-08-07 17:24:05 +08:00
// 记录整体坐标
const groupAttr = {
l: this.getEditCanvasConfig.width,
t: this.getEditCanvasConfig.height,
r: 0,
b: 0
}
const targetList: CreateComponentType[] = []
const historyList: CreateComponentType[] = []
// 若目标中有数组则先解组
const newSelectIds: string[] = []
selectIds.forEach((id: string) => {
const targetIndex = this.fetchTargetIndex(id)
if (targetIndex !== -1 && this.getComponentList[targetIndex].isGroup) {
2022-08-17 16:18:00 +08:00
this.setUnGroup(
[id],
(e: CreateComponentType[]) => {
e.forEach(e => {
this.addComponentList(e)
newSelectIds.push(e.id)
})
},
false
)
} else if (targetIndex !== -1) {
newSelectIds.push(id)
}
})
newSelectIds.forEach((id: string) => {
2022-08-06 18:21:50 +08:00
// 获取目标数据并从 list 中移除 (成组后不可再次成组, 断言处理)
const item = this.componentList.splice(this.fetchTargetIndex(id), 1)[0] as CreateComponentType
2022-08-07 17:24:05 +08:00
const { x, y, w, h } = item.attr
const { l, t, r, b } = groupAttr
// 左
groupAttr.l = l > x ? x : l
// 上
groupAttr.t = t > y ? y : t
// 宽
groupAttr.r = r < x + w ? x + w : r
// 高
2022-08-17 16:18:00 +08:00
groupAttr.b = b < y + h ? y + h : b
2022-08-07 17:24:05 +08:00
targetList.push(item)
historyList.push(toRaw(item))
2022-08-07 17:24:05 +08:00
})
// 修改原数据之前,先记录
2022-08-17 16:18:00 +08:00
if (isHistory) chartHistoryStore.createGroupHistory(historyList)
2022-08-07 17:24:05 +08:00
// 设置子组件的位置
targetList.forEach((item: CreateComponentType) => {
item.attr.x = item.attr.x - groupAttr.l
item.attr.y = item.attr.y - groupAttr.t
2022-08-06 18:21:50 +08:00
groupClass.groupList.push(item)
})
2022-08-07 17:24:05 +08:00
// 设置 group 属性
groupClass.attr.x = groupAttr.l
groupClass.attr.y = groupAttr.t
groupClass.attr.w = groupAttr.r - groupAttr.l
groupClass.attr.h = groupAttr.b - groupAttr.t
2022-08-06 18:21:50 +08:00
this.addComponentList(groupClass)
2022-08-09 20:12:44 +08:00
this.setTargetSelectChart(groupClass.id)
2022-08-06 18:21:50 +08:00
loadingFinish()
} catch (error) {
console.log(error)
2022-08-06 18:21:50 +08:00
window['$message'].error('创建分组失败,请联系管理员!')
loadingFinish()
}
},
// * 解除分组
2022-08-17 16:18:00 +08:00
setUnGroup(ids?: string[], callBack?: (e: CreateComponentType[]) => void, isHistory = true) {
2022-08-06 18:21:50 +08:00
try {
const selectGroupIdArr = ids || this.getTargetChart.selectId
if (selectGroupIdArr.length !== 1) return
2022-08-09 20:12:44 +08:00
loadingStart()
2022-08-06 18:21:50 +08:00
// 解组
const unGroup = (targetIndex: number) => {
const targetGroup = this.getComponentList[targetIndex] as CreateComponentGroupType
if (!targetGroup.isGroup) return
// 记录数据
2022-08-17 16:18:00 +08:00
if (isHistory) chartHistoryStore.createUnGroupHistory(cloneDeep([targetGroup]))
2022-08-08 00:02:58 +08:00
// 分离组件并还原位置属性
2022-08-06 18:21:50 +08:00
targetGroup.groupList.forEach(item => {
2022-08-08 00:02:58 +08:00
item.attr.x = item.attr.x + targetGroup.attr.x
item.attr.y = item.attr.y + targetGroup.attr.y
if (!callBack) {
this.addComponentList(item)
}
2022-08-06 18:21:50 +08:00
})
this.setTargetSelectChart(targetGroup.id)
// 删除分组
this.removeComponentList(targetGroup.id, false)
if (callBack) {
2022-08-17 16:18:00 +08:00
callBack(targetGroup.groupList)
}
2022-08-06 18:21:50 +08:00
}
const targetIndex = this.fetchTargetIndex(selectGroupIdArr[0])
2022-08-06 18:21:50 +08:00
// 判断目标是否为分组父级
if (targetIndex !== -1) {
2022-08-06 18:21:50 +08:00
unGroup(targetIndex)
}
2022-08-09 20:12:44 +08:00
2022-08-06 18:21:50 +08:00
loadingFinish()
} catch (error) {
2022-08-09 20:12:44 +08:00
console.log(error)
window['$message'].error('解除分组失败,请联系管理员!')
2022-08-06 18:21:50 +08:00
loadingFinish()
}
},
2022-02-04 12:17:50 +08:00
// ----------------
2022-01-20 21:25:35 +08:00
// * 设置页面大小
2022-03-10 17:55:59 +08:00
setPageSize(scale: number): void {
this.setPageStyle('height', `${this.editCanvasConfig.height * scale}px`)
this.setPageStyle('width', `${this.editCanvasConfig.width * scale}px`)
2022-01-20 21:25:35 +08:00
},
// * 计算缩放
computedScale() {
2022-01-20 22:13:51 +08:00
if (this.getEditCanvas.editLayoutDom) {
2022-01-20 21:25:35 +08:00
// 现有展示区域
2022-08-17 16:18:00 +08:00
const width = this.getEditCanvas.editLayoutDom.clientWidth - this.getEditCanvas.offset * 2 - 5
const height = this.getEditCanvas.editLayoutDom.clientHeight - this.getEditCanvas.offset * 4
2022-01-20 21:25:35 +08:00
// 用户设定大小
2022-02-06 01:04:05 +08:00
const editCanvasWidth = this.editCanvasConfig.width
const editCanvasHeight = this.editCanvasConfig.height
2022-01-20 21:25:35 +08:00
2022-01-20 22:13:51 +08:00
// 需保持的比例
2022-08-17 16:18:00 +08:00
const baseProportion = parseFloat((editCanvasWidth / editCanvasHeight).toFixed(5))
2022-01-20 22:13:51 +08:00
const currentRate = parseFloat((width / height).toFixed(5))
2022-01-20 21:25:35 +08:00
if (currentRate > baseProportion) {
// 表示更宽
2022-08-17 16:18:00 +08:00
const scaleWidth = parseFloat(((height * baseProportion) / editCanvasWidth).toFixed(5))
this.setScale(scaleWidth > 1 ? 1 : scaleWidth)
2022-01-20 21:25:35 +08:00
} else {
// 表示更高
2022-08-17 16:18:00 +08:00
const scaleHeight = parseFloat((width / baseProportion / editCanvasHeight).toFixed(5))
2022-01-28 20:54:13 +08:00
this.setScale(scaleHeight > 1 ? 1 : scaleHeight)
2022-01-20 21:25:35 +08:00
}
} else {
2022-01-23 19:22:54 +08:00
window['$message'].warning('请先创建画布,再进行缩放')
2022-01-20 21:25:35 +08:00
}
},
// * 监听缩放
listenerScale(): Function {
const resize = debounce(this.computedScale, 200)
// 默认执行一次
resize()
// 开始监听
window.addEventListener('resize', resize)
// 销毁函数
const remove = () => {
window.removeEventListener('resize', resize)
}
return remove
},
2022-04-05 19:01:52 +08:00
/**
* *
* @param scale 0~1 number ;
* @param force boolean
*/
setScale(scale: number, force = false): void {
if (!this.getEditCanvas.lockScale || force) {
2022-03-10 17:55:59 +08:00
this.setPageSize(scale)
2022-01-21 17:55:35 +08:00
this.getEditCanvas.userScale = scale
2022-04-05 19:01:52 +08:00
this.getEditCanvas.scale = scale
2022-01-20 22:13:51 +08:00
}
2022-01-20 21:25:35 +08:00
}
}
2022-08-17 16:18:00 +08:00
})