176 lines
4.9 KiB
TypeScript
Raw Normal View History

2022-01-19 21:29:04 +08:00
import { defineStore } from 'pinia'
2022-01-20 21:25:35 +08:00
import debounce from 'lodash/debounce'
2022-01-27 23:16:51 +08:00
import { loadingStart, loadingFinish, loadingError } from '@/utils'
2022-01-20 21:25:35 +08:00
import {
chartEditStoreType,
EditCanvasType,
MousePositionType
} from './chartEditStore.d'
2022-01-19 21:29:04 +08:00
2022-01-20 21:25:35 +08:00
// 编辑区域内容
2022-01-19 21:29:04 +08:00
export const useChartEditStoreStore = defineStore({
id: 'useChartEditStoreStore',
2022-01-20 21:25:35 +08:00
state: (): chartEditStoreType => ({
editCanvas: {
// 编辑区域 Dom
2022-01-24 14:28:31 +08:00
editLayoutDom: null,
editContentDom: null,
2022-01-20 21:25:35 +08:00
// 默认宽度
width: 1920,
// 默认高度
height: 1080,
// 偏移量
offset: 20,
2022-01-20 22:13:51 +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,
// 默认背景色
background: undefined
},
mousePosition: {
x: 0,
y: 0
2022-01-24 21:12:18 +08:00
},
2022-01-29 11:44:51 +08:00
targetChart: {
index: 0
},
2022-01-24 21:12:18 +08:00
componentList: []
2022-01-20 21:25:35 +08:00
}),
getters: {
getMousePosition(): MousePositionType {
return this.mousePosition
},
getEditCanvas(): EditCanvasType {
return this.editCanvas
},
2022-01-24 21:12:18 +08:00
getComponentList(): any[] {
return this.componentList
2022-01-25 18:19:44 +08:00
}
2022-01-20 21:25:35 +08:00
},
actions: {
2022-01-24 21:12:18 +08:00
// * 新增组件列表
2022-01-25 18:19:44 +08:00
addComponentList<T>(chartData: T): void {
2022-01-24 21:12:18 +08:00
this.componentList.push(chartData)
},
2022-01-25 18:19:44 +08:00
// * 删除组件列表
2022-01-27 22:30:35 +08:00
removeComponentList<T extends { key: string }>(chartData: T | number): void {
2022-01-27 23:16:51 +08:00
loadingStart()
try {
if(typeof chartData === 'number') {
this.componentList.splice(chartData, 1)
loadingFinish()
return
}
const i = this.componentList.findIndex(e => e.key === chartData.key)
if (i !== -1) {
this.componentList.splice(i, 1)
loadingFinish()
return
}
window['$message'].success(`图表删除失败,无法找到此元素`)
} catch(value) {
loadingError()
2022-01-27 22:30:35 +08:00
}
2022-01-25 18:19:44 +08:00
},
2022-01-20 21:25:35 +08:00
// * 设置数据项
2022-01-25 18:19:44 +08:00
setEditCanvasItem<
T extends keyof EditCanvasType,
K extends EditCanvasType[T]
>(key: T, value: K) {
2022-01-20 21:25:35 +08:00
this.editCanvas[key] = value
},
// * 设置页面样式属性
setPageStyle<T extends keyof CSSStyleDeclaration>(
2022-01-20 21:25:35 +08:00
key: T,
value: any
): void {
2022-01-20 22:13:51 +08:00
const dom = this.getEditCanvas.editContentDom
2022-01-20 21:25:35 +08:00
if (dom) {
dom.style[key] = value
}
},
// * 设置页面变换时候的 Class
setPageSizeClass(): void {
const dom = this.getEditCanvas.editContentDom
if (dom) {
dom.classList.add('content-resize')
setTimeout(() => {
dom.classList.remove('content-resize')
2022-01-25 18:19:44 +08:00
}, 600)
}
},
2022-01-20 21:25:35 +08:00
// * 设置页面大小
setPageSize(): void {
this.setPageStyle('height', `${this.getEditCanvas.height}px`)
this.setPageStyle('width', `${this.getEditCanvas.width}px`)
2022-01-20 21:25:35 +08:00
},
// * 设置鼠标位置
setMousePosition(x: number, y: number): void {
this.mousePosition.x = x
this.mousePosition.y = y
},
// * 计算缩放
computedScale() {
2022-01-20 22:13:51 +08:00
if (this.getEditCanvas.editLayoutDom) {
2022-01-20 21:25:35 +08:00
// 现有展示区域
2022-01-20 22:13:51 +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-01-20 22:13:51 +08:00
const editCanvasWidth = this.getEditCanvas.width
const editCanvasHeight = this.getEditCanvas.height
2022-01-20 21:25:35 +08:00
2022-01-20 22:13:51 +08:00
// 需保持的比例
const baseProportion = parseFloat(
(editCanvasWidth / editCanvasHeight).toFixed(5)
)
const currentRate = parseFloat((width / height).toFixed(5))
2022-01-20 21:25:35 +08:00
if (currentRate > baseProportion) {
// 表示更宽
2022-01-20 22:13:51 +08:00
const scaleWidth = parseFloat(
((height * baseProportion) / editCanvasWidth).toFixed(5)
)
2022-01-28 20:54:13 +08:00
this.setScale( scaleWidth > 1 ? 1 : scaleWidth)
2022-01-20 21:25:35 +08:00
} else {
// 表示更高
2022-01-20 22:13:51 +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-01-20 22:13:51 +08:00
setScale(scale: number, sys = true): void {
if (!this.getEditCanvas.lockScale) {
this.setPageSizeClass()
this.setPageStyle('transform', `scale(${scale})`)
2022-01-21 17:55:35 +08:00
this.getEditCanvas.userScale = scale
if (sys) {
this.getEditCanvas.scale = scale
}
2022-01-20 22:13:51 +08:00
}
2022-01-20 21:25:35 +08:00
}
}
2022-01-23 19:22:54 +08:00
})