fix: 解决重叠拖拽放置位置错误的bug

This commit is contained in:
MTrun 2022-02-16 19:18:39 +08:00
parent c968853092
commit 0b58dd4d17
11 changed files with 60 additions and 34 deletions

View File

@ -7,6 +7,8 @@
<style lang="scss" scoped> <style lang="scss" scoped>
@include go('loading-svg') { @include go('loading-svg') {
width: 100%; width: 100%;
height: 100%;
background-color: transparent;
img { img {
height: 50px; height: 50px;
} }

View File

@ -9,6 +9,7 @@ export enum EditCanvasTypeEnum {
SCALE = 'scale', SCALE = 'scale',
USER_SCALE = 'userScale', USER_SCALE = 'userScale',
LOCK_SCALE = 'lockScale', LOCK_SCALE = 'lockScale',
Is_Drag= 'isDrag',
} }
// 编辑区域 // 编辑区域
@ -24,6 +25,8 @@ export type EditCanvasType = {
[EditCanvasTypeEnum.USER_SCALE]: number [EditCanvasTypeEnum.USER_SCALE]: number
// 锁定缩放 // 锁定缩放
[EditCanvasTypeEnum.LOCK_SCALE]: boolean [EditCanvasTypeEnum.LOCK_SCALE]: boolean
// 拖拽中
[EditCanvasTypeEnum.Is_Drag]: boolean
} }
// 滤镜 // 滤镜

View File

@ -35,6 +35,8 @@ export const useChartEditStoreStore = defineStore({
userScale: 1, userScale: 1,
// 锁定缩放 // 锁定缩放
lockScale: false, lockScale: false,
// 拖拽中
isDrag: false
}, },
// 画布属性(需存储给后端) // 画布属性(需存储给后端)
editCanvasConfig: { editCanvasConfig: {
@ -102,10 +104,10 @@ export const useChartEditStoreStore = defineStore({
}, },
actions: { actions: {
// * 设置 editCanvas 数据项 // * 设置 editCanvas 数据项
setEditCanvasItem<T extends keyof EditCanvasType, K extends EditCanvasType[T]>(key: T, value: K) { setEditCanvas<T extends keyof EditCanvasType, K extends EditCanvasType[T]>(key: T, value: K) {
this.editCanvas[key] = value this.editCanvas[key] = value
}, },
setCanvasConfig<T extends keyof EditCanvasConfigType, K extends EditCanvasConfigType[T]>(key: T, value: K) { setEditCanvasConfig<T extends keyof EditCanvasConfigType, K extends EditCanvasConfigType[T]>(key: T, value: K) {
this.editCanvasConfig[key] = value this.editCanvasConfig[key] = value
}, },
// * 设置右键菜单 // * 设置右键菜单

View File

@ -57,7 +57,7 @@ const fetchShowColors = (colors: Array<string>) => {
} }
const selectTheme = (theme: string) => { const selectTheme = (theme: string) => {
chartEditStoreStore.setCanvasConfig(EditCanvasConfigEnum.CHART_THEME, theme) chartEditStoreStore.setEditCanvasConfig(EditCanvasConfigEnum.CHART_THEME, theme)
} }
</script> </script>

View File

@ -179,21 +179,21 @@ const beforeUploadHandle = async ({ file }) => {
// //
const clearImage = () => { const clearImage = () => {
chartEditStoreStore.setCanvasConfig( chartEditStoreStore.setEditCanvasConfig(
EditCanvasConfigEnum.BACKGROUND_IAMGE, EditCanvasConfigEnum.BACKGROUND_IAMGE,
undefined undefined
) )
chartEditStoreStore.setCanvasConfig(EditCanvasConfigEnum.SELECT_COLOR, true) chartEditStoreStore.setEditCanvasConfig(EditCanvasConfigEnum.SELECT_COLOR, true)
} }
// //
const clearColor = () => { const clearColor = () => {
chartEditStoreStore.setCanvasConfig( chartEditStoreStore.setEditCanvasConfig(
EditCanvasConfigEnum.BACKGROUND, EditCanvasConfigEnum.BACKGROUND,
undefined undefined
) )
if (canvasConfig.backgroundImage) { if (canvasConfig.backgroundImage) {
chartEditStoreStore.setCanvasConfig( chartEditStoreStore.setEditCanvasConfig(
EditCanvasConfigEnum.SELECT_COLOR, EditCanvasConfigEnum.SELECT_COLOR,
false false
) )
@ -224,11 +224,11 @@ const customRequest = (options: UploadCustomRequestOptions) => {
nextTick(() => { nextTick(() => {
if (file.file) { if (file.file) {
const ImageUrl = fileToUrl(file.file) const ImageUrl = fileToUrl(file.file)
chartEditStoreStore.setCanvasConfig( chartEditStoreStore.setEditCanvasConfig(
EditCanvasConfigEnum.BACKGROUND_IAMGE, EditCanvasConfigEnum.BACKGROUND_IAMGE,
ImageUrl ImageUrl
) )
chartEditStoreStore.setCanvasConfig( chartEditStoreStore.setEditCanvasConfig(
EditCanvasConfigEnum.SELECT_COLOR, EditCanvasConfigEnum.SELECT_COLOR,
false false
) )

View File

@ -115,7 +115,7 @@ const selectHandle = (v: number) => {
// //
const lockHandle = () => { const lockHandle = () => {
chartEditStore.setEditCanvasItem( chartEditStore.setEditCanvas(
ChartEditStoreEnum.LOCK_SCALE, ChartEditStoreEnum.LOCK_SCALE,
!lockScale.value !lockScale.value
) )

View File

@ -1,22 +1,23 @@
<template> <template>
<div <div
class="go-edit-range" class="go-edit-range"
:style="style" :style="rangeStyle"
@mousedown="mousedownHandleUnStop($event, undefined)" @mousedown="mousedownHandleUnStop($event, undefined)"
> >
<slot></slot> <slot></slot>
</div> </div>
<div class="go-edit-range-model" :style="rangeModelStyle"></div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { toRefs, computed } from 'vue' import { toRefs, computed } from 'vue'
import { useChartEditStoreStore } from '@/store/modules/chartEditStore/chartEditStore' import { useChartEditStoreStore } from '@/store/modules/chartEditStore/chartEditStore'
import { useSizeStyle } from '../../hooks/useStyle.hook' import { useSizeStyle } from '../../hooks/useStyle.hook'
import { mousedownHandleUnStop } from '../../hooks/useDrop.hook' import { mousedownHandleUnStop } from '../../hooks/useDrag.hook'
const chartEditStoreStore = useChartEditStoreStore() const chartEditStoreStore = useChartEditStoreStore()
const { getEditCanvasConfig } = toRefs(chartEditStoreStore) const { getEditCanvasConfig, getEditCanvas } = toRefs(chartEditStoreStore)
const size = computed(() => { const size = computed(() => {
return { return {
@ -25,16 +26,23 @@ const size = computed(() => {
} }
}) })
const style = computed(() => { const rangeStyle = computed(() => {
const background = getEditCanvasConfig.value.background const background = getEditCanvasConfig.value.background
const backgroundImage = getEditCanvasConfig.value.backgroundImage const backgroundImage = getEditCanvasConfig.value.backgroundImage
const selectColor = getEditCanvasConfig.value.selectColor const selectColor = getEditCanvasConfig.value.selectColor
const backgroundColor = background ? background : null const backgroundColor = background ? background : null
const computed = selectColor const computedBackground = selectColor
? { background: backgroundColor } ? { background: backgroundColor }
: { background: `url(${backgroundImage}) no-repeat center/100% !important` } : { background: `url(${backgroundImage}) no-repeat center/100% !important` }
// @ts-ignore // @ts-ignore
return { ...useSizeStyle(size.value), ...computed } return { ...useSizeStyle(size.value), ...computedBackground }
})
//
const rangeModelStyle = computed(() => {
const dragStyle = getEditCanvas.value.isDrag && {'z-index': 99999 }
// @ts-ignore
return { ...useSizeStyle(size.value), ...dragStyle}
}) })
</script> </script>
@ -48,4 +56,12 @@ const style = computed(() => {
@include filter-border-color('hover-border-color'); @include filter-border-color('hover-border-color');
@include fetch-theme-custom('border-color', 'background-color4'); @include fetch-theme-custom('border-color', 'background-color4');
} }
@include go(edit-range-model) {
z-index: -1;
position: absolute;
left: 0;
top: 0;
border-radius: 15px;
border: 1px solid rgba(0, 0, 0, 0);
}
</style> </style>

View File

@ -1,20 +1,21 @@
import { toRefs } from 'vue' import { toRefs } from 'vue'
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'
import { CreateComponentType } from '@/packages/index.d' import { CreateComponentType } from '@/packages/index.d'
import throttle from 'lodash/throttle'
import { useContextMenu } from '@/views/chart/hooks/useContextMenu.hook' import { useContextMenu } from '@/views/chart/hooks/useContextMenu.hook'
import { getChartEditStore, getChartEditStoreEnum } from './useStore.hook'
import { loadingStart, loadingFinish, loadingError } from '@/utils'
import throttle from 'lodash/throttle'
const { onClickoutside } = useContextMenu() const { onClickoutside } = useContextMenu()
const chartEditStore = getChartEditStore() const chartEditStore = getChartEditStore()
const chartEditStoreEnum = getChartEditStoreEnum()
const { scale } = toRefs(chartEditStore.getEditCanvas) const { scale } = toRefs(chartEditStore.getEditCanvas)
// * 拖拽 // * 拖拽到编辑区域里
export const handleDrop = async (e: DragEvent) => { export const handleDrag = async (e: DragEvent) => {
e.preventDefault() e.preventDefault()
try { try {
@ -26,6 +27,9 @@ export const handleDrop = async (e: DragEvent) => {
return return
} }
// 设置拖拽状态
chartEditStore.setEditCanvas(chartEditStoreEnum.Is_Drag, false)
const dropData: Exclude<ConfigType, ['node', 'image']> = JSON.parse( const dropData: Exclude<ConfigType, ['node', 'image']> = JSON.parse(
drayDataString drayDataString
) )
@ -42,10 +46,13 @@ export const handleDrop = async (e: DragEvent) => {
} }
} }
// * 拖拽结束 // * 拖拽
export const handleDragOver = (e: DragEvent) => { export const handleDragOver = (e: DragEvent) => {
e.preventDefault() e.preventDefault()
e.stopPropagation() e.stopPropagation()
// 设置拖拽状态
chartEditStore.setEditCanvas(chartEditStoreEnum.Is_Drag, true)
if (e.dataTransfer) e.dataTransfer.dropEffect = 'copy' if (e.dataTransfer) e.dataTransfer.dropEffect = 'copy'
} }

View File

@ -8,11 +8,11 @@ const chartEditStore = getChartEditStore()
export const useLayout = () => { export const useLayout = () => {
onMounted(() => { onMounted(() => {
// 设置 Dom 值(ref 不生效先用 document) // 设置 Dom 值(ref 不生效先用 document)
chartEditStore.setEditCanvasItem( chartEditStore.setEditCanvas(
EditCanvasTypeEnum.EDIT_LAYOUT_DOM, EditCanvasTypeEnum.EDIT_LAYOUT_DOM,
document.getElementById('go-chart-edit-layout') document.getElementById('go-chart-edit-layout')
) )
chartEditStore.setEditCanvasItem( chartEditStore.setEditCanvas(
EditCanvasTypeEnum.EDIT_CONTENT_DOM, EditCanvasTypeEnum.EDIT_CONTENT_DOM,
document.getElementById('go-chart-edit-content') document.getElementById('go-chart-edit-content')
) )

View File

@ -1,9 +1,5 @@
interface AttrType { import { PublicConfigType } from '@/packages/index.d'
x: number type AttrType = Pick<PublicConfigType, 'attr'>['attr']
y: number
w: number
h: number
}
export const useComponentStyle = (attr: AttrType, index: number) => { export const useComponentStyle = (attr: AttrType, index: number) => {
const componentStyle = { const componentStyle = {

View File

@ -7,7 +7,7 @@
:showBottom="true" :showBottom="true"
:depth="1" :depth="1"
:xScroll="true" :xScroll="true"
@drop="handleDrop" @drop="handleDrag"
@dragover="handleDragOver" @dragover="handleDragOver"
> >
<div id="go-chart-edit-content"> <div id="go-chart-edit-content">
@ -52,7 +52,7 @@ import { EditShapeBox } from './components/EditShapeBox/index'
import { useLayout } from './hooks/useLayout.hook' import { useLayout } from './hooks/useLayout.hook'
import { useAddKeyboard } from '../hooks/useKeyboard.hook' import { useAddKeyboard } from '../hooks/useKeyboard.hook'
import { handleDrop, handleDragOver, useMouseHandle } from './hooks/useDrop.hook' import { handleDrag, handleDragOver, useMouseHandle } from './hooks/useDrag.hook'
import { useContextMenu } from '@/views/chart/hooks/useContextMenu.hook' import { useContextMenu } from '@/views/chart/hooks/useContextMenu.hook'
import { getChartEditStore } from './hooks/useStore.hook' import { getChartEditStore } from './hooks/useStore.hook'
import { useComponentStyle, useSizeStyle } from './hooks/useStyle.hook' import { useComponentStyle, useSizeStyle } from './hooks/useStyle.hook'
@ -93,7 +93,7 @@ onMounted(() => {
@extend .go-point-bg; @extend .go-point-bg;
@include goId(chart-edit-content) { @include goId(chart-edit-content) {
margin: 20px; margin: 20px;
overflow: hidden; /* overflow: hidden; */
transform-origin: left top; transform-origin: left top;
border: 1px solid rgba(0, 0, 0, 0); border: 1px solid rgba(0, 0, 0, 0);
@extend .go-transition; @extend .go-transition;