fix: 新增图表居中拖拽

This commit is contained in:
MTrun 2022-01-27 23:16:51 +08:00
parent f57fa702d9
commit 5e07c2ca6d
9 changed files with 83 additions and 34 deletions

View File

@ -1,10 +1,15 @@
import { getUUID } from '@/utils' import { getUUID } from '@/utils'
import { BarCommonConfig } from './index' import { BarCommonConfig } from './index'
export const chartSize = {
w: 500,
h: 300
}
export default class Config { export default class Config {
private id: string = getUUID() private id: string = getUUID()
private key: string = BarCommonConfig.key private key: string = BarCommonConfig.key
public attr = { x: 0, y: 0, w: 500, h: 300 } public attr = { x: 0, y: 0, ...chartSize }
// 图表配置项 // 图表配置项
public config = { public config = {
@ -17,21 +22,21 @@ export default class Config {
}, },
xAxis: { xAxis: {
type: 'category', type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
}, },
yAxis: { yAxis: {
type: 'value', type: 'value'
}, },
series: [ series: [
{ {
data: [120, 200, 150, 80, 70, 110, 130], data: [120, 200, 150, 80, 70, 110, 130],
type: 'bar', type: 'bar'
}, }
], ]
} }
// 设置坐标 // 设置坐标
public setPosition(x: number, y: number):void { public setPosition(x: number, y: number): void {
this.attr.x = x this.attr.x = x
this.attr.y = y this.attr.y = y
} }

View File

@ -2,6 +2,7 @@ import BarCommon from './index.vue'
import image from '@/assets/images/chart/charts/bar_x.png' import image from '@/assets/images/chart/charts/bar_x.png'
import { ConfigType, PackagesCategoryEnum } from '@/packages/index.d' import { ConfigType, PackagesCategoryEnum } from '@/packages/index.d'
import { ChatCategoryEnum, ChatCategoryEnumName } from '../../index.d' import { ChatCategoryEnum, ChatCategoryEnumName } from '../../index.d'
import { chartSize } from './config'
export const BarCommonConfig: ConfigType = { export const BarCommonConfig: ConfigType = {
key: 'VBarCommon', key: 'VBarCommon',
@ -10,5 +11,6 @@ export const BarCommonConfig: ConfigType = {
categoryName: ChatCategoryEnumName.BAR, categoryName: ChatCategoryEnumName.BAR,
package: PackagesCategoryEnum.CHARTS, package: PackagesCategoryEnum.CHARTS,
node: BarCommon, node: BarCommon,
image: image image: image,
chartData: { chartSize }
} }

View File

@ -7,6 +7,12 @@ export type ConfigType = {
category: string category: string
categoryName: string categoryName: string
package: string package: string
chartData: {
chartSize: {
w: number
h: number
}
}
node: Component node: Component
image: string | (() => Promise<typeof import('*.png')>) image: string | (() => Promise<typeof import('*.png')>)
[T: string]: unknown [T: string]: unknown
@ -16,14 +22,14 @@ export enum PackagesCategoryEnum {
CHARTS = 'Charts', CHARTS = 'Charts',
TABLES = 'Tables', TABLES = 'Tables',
INFORMATION = 'Informations', INFORMATION = 'Informations',
DECORATES = 'Decorates', DECORATES = 'Decorates'
} }
export enum PackagesCategoryName { export enum PackagesCategoryName {
CHARTS = '图表', CHARTS = '图表',
TABLES = '表格', TABLES = '表格',
INFORMATION = '信息', INFORMATION = '信息',
DECORATES = '小组件', DECORATES = '小组件'
} }
export type PackagesType = { export type PackagesType = {

View File

@ -1,5 +1,6 @@
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import debounce from 'lodash/debounce' import debounce from 'lodash/debounce'
import { loadingStart, loadingFinish, loadingError } from '@/utils'
import { import {
chartEditStoreType, chartEditStoreType,
EditCanvasType, EditCanvasType,
@ -53,16 +54,23 @@ export const useChartEditStoreStore = defineStore({
}, },
// * 删除组件列表 // * 删除组件列表
removeComponentList<T extends { key: string }>(chartData: T | number): void { removeComponentList<T extends { key: string }>(chartData: T | number): void {
if(typeof chartData === 'number') { loadingStart()
this.componentList.splice(chartData, 1) try {
return 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()
} }
const i = this.componentList.findIndex(e => e.key === chartData.key)
if (i !== -1) {
this.componentList.splice(i, 1)
return
}
window['$message'].success(`图表删除失败,无法找到此元素`)
}, },
// * 设置数据项 // * 设置数据项
setEditCanvasItem< setEditCanvasItem<

View File

@ -1,3 +1,4 @@
import { nextTick } from 'vue'
import { icon } from '@/plugins' import { icon } from '@/plugins'
import { DialogEnum } from '@/enums/pluginEnum' import { DialogEnum } from '@/enums/pluginEnum'
import { dialogIconSize } from '@/settings/designSetting' import { dialogIconSize } from '@/settings/designSetting'
@ -6,6 +7,25 @@ import { DialogReactive } from 'naive-ui'
const { InformationCircleIcon } = icon.ionicons5 const { InformationCircleIcon } = icon.ionicons5
import { renderIcon } from '@/utils' import { renderIcon } from '@/utils'
// * 开启加载
export const loadingStart = () => {
window['$loading'].start()
}
// * 加载结束
export const loadingFinish = () => {
setTimeout(() => {
window['$loading'].finish()
})
}
// * 加载错误
export const loadingError = () => {
setTimeout(() => {
window['$loading'].error()
})
}
/** /**
* * render * * render
* @param { Object} params * @param { Object} params

View File

@ -1,16 +1,20 @@
<template> <template>
<div class="go-edit-range"> <div class="go-edit-range" :style="useSizeStyle(size)">
<slot></slot> <slot></slot>
</div> </div>
</template> </template>
<script setup lang="ts"></script> <script setup lang="ts">
import { useSizeStyle } from '../../hooks/useStyle.hook'
const size = {
w: 1920,
h: 1080
}
</script>
<style lang="scss" scoped> <style lang="scss" scoped>
@include go(edit-range) { @include go(edit-range) {
position: relative; position: relative;
height: 1080px;
width: 1920px;
border: 1px solid; border: 1px solid;
background-color: #333; background-color: #333;
border-radius: 15px; border-radius: 15px;

View File

@ -1,5 +1,7 @@
import { reactive, ref, nextTick } from 'vue' import { reactive, ref, nextTick } from 'vue'
import { getChartEditStore } from './useStore.hook' import { getChartEditStore } from './useStore.hook'
import { loadingError } from '@/utils'
const chartEditStore = getChartEditStore() const chartEditStore = getChartEditStore()
enum MenuEnum { enum MenuEnum {
@ -46,6 +48,7 @@ export const useContextMenu = () => {
case MenuEnum.DELETE: case MenuEnum.DELETE:
chartEditStore.removeComponentList(targetIndex.value) chartEditStore.removeComponentList(targetIndex.value)
break break
default: loadingError()
} }
} }

View File

@ -3,6 +3,7 @@ import { getChartEditStore } from './useStore.hook'
import { DragKeyEnum } from '@/enums/editPageEnum' import { DragKeyEnum } from '@/enums/editPageEnum'
import { createComponent } from '@/packages' import { createComponent } from '@/packages'
import { ConfigType } from '@/packages/index.d' import { ConfigType } from '@/packages/index.d'
import { loadingStart, loadingFinish, loadingError } from '@/utils'
const chartEditStore = getChartEditStore() const chartEditStore = getChartEditStore()
const { scale } = toRefs(chartEditStore.getEditCanvas) const { scale } = toRefs(chartEditStore.getEditCanvas)
@ -10,27 +11,26 @@ const { scale } = toRefs(chartEditStore.getEditCanvas)
// * 拖拽中 // * 拖拽中
export const handleDrop = async (e: DragEvent) => { export const handleDrop = async (e: DragEvent) => {
e.preventDefault() e.preventDefault()
const Loading = window['$loading']
try { try {
Loading.start() loadingStart()
// 获取拖拽数据
const drayDataString = e!.dataTransfer!.getData(DragKeyEnum.DROG_KEY) const drayDataString = e!.dataTransfer!.getData(DragKeyEnum.DROG_KEY)
const dropData: Exclude<ConfigType, ['node', 'image']> = JSON.parse( const dropData: Exclude<ConfigType, ['node', 'image']> = JSON.parse(
drayDataString drayDataString
) )
// 创建新图表组件
let newComponent = await createComponent(dropData)
let newComponent= await createComponent(dropData) const { w, h } = dropData.chartData.chartSize
newComponent.setPosition(e.offsetX, e.offsetY) newComponent.setPosition(e.offsetX - w / 2, e.offsetY - h / 2)
chartEditStore.addComponentList(newComponent) chartEditStore.addComponentList(newComponent)
setTimeout(() => { loadingFinish()
Loading.finish()
})
} catch (error) { } catch (error) {
Loading.error() loadingError()
window['$message'].success(`图表正在研发中, 敬请期待...`) window['$message'].warning(`图表正在研发中, 敬请期待...`)
} }
} }

View File

@ -13,6 +13,7 @@ export const useComponentStyle = (attr: AttrType, index: number) => {
} }
return componentStyle return componentStyle
} }
export const useSizeStyle = (attr: AttrType) => { export const useSizeStyle = (attr: AttrType) => {
const sizeStyle = { const sizeStyle = {
width: `${attr.w}px`, width: `${attr.w}px`,