mirror of
https://github.com/palxiao/poster-design.git
synced 2025-07-15 16:02:19 +08:00
fix: text component drag bug & perf
This commit is contained in:
parent
2ef36d883f
commit
11b7602a08
@ -2,8 +2,8 @@
|
|||||||
* @Author: ShawnPhang
|
* @Author: ShawnPhang
|
||||||
* @Date: 2022-02-22 15:06:14
|
* @Date: 2022-02-22 15:06:14
|
||||||
* @Description: 设置图片类型元素
|
* @Description: 设置图片类型元素
|
||||||
* @LastEditors: ShawnPhang <site: book.palxp.com>
|
* @LastEditors: ShawnPhang <https://m.palxp.cn>
|
||||||
* @LastEditTime: 2023-07-10 17:37:27
|
* @LastEditTime: 2024-01-11 17:36:44
|
||||||
*/
|
*/
|
||||||
import store from '@/store'
|
import store from '@/store'
|
||||||
import { getImage } from '../getImgDetail'
|
import { getImage } from '../getImgDetail'
|
||||||
@ -29,6 +29,5 @@ export default async function setItem2Data(item: any) {
|
|||||||
|
|
||||||
cloneItem.canvasWidth = cloneItem.width * (store.getters.dZoom / 100)
|
cloneItem.canvasWidth = cloneItem.width * (store.getters.dZoom / 100)
|
||||||
// cloneItem.canvasHeight = cloneItem.height * (store.getters.dZoom / 100)
|
// cloneItem.canvasHeight = cloneItem.height * (store.getters.dZoom / 100)
|
||||||
|
|
||||||
return cloneItem
|
return cloneItem
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
* @Date: 2021-08-27 15:16:07
|
* @Date: 2021-08-27 15:16:07
|
||||||
* @Description: 素材列表,主要用于文字组合列表
|
* @Description: 素材列表,主要用于文字组合列表
|
||||||
* @LastEditors: ShawnPhang <https://m.palxp.cn>
|
* @LastEditors: ShawnPhang <https://m.palxp.cn>
|
||||||
* @LastEditTime: 2023-11-27 18:26:08
|
* @LastEditTime: 2024-01-11 17:59:57
|
||||||
-->
|
-->
|
||||||
<template>
|
<template>
|
||||||
<div class="wrap">
|
<div class="wrap">
|
||||||
@ -43,19 +43,21 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { defineComponent, reactive, toRefs, onMounted, watch } from 'vue'
|
import { defineComponent, reactive, toRefs, onMounted, watch } from 'vue'
|
||||||
import api from '@/api'
|
import api from '@/api'
|
||||||
import { mapActions } from 'vuex'
|
import { useStore } from 'vuex'
|
||||||
import getComponentsData from '@/common/methods/DesignFeatures/setComponents'
|
import getComponentsData from '@/common/methods/DesignFeatures/setComponents'
|
||||||
import DragHelper from '@/common/hooks/dragHelper'
|
import DragHelper from '@/common/hooks/dragHelper'
|
||||||
import setImageData from '@/common/methods/DesignFeatures/setImage'
|
import setItem2Data from '@/common/methods/DesignFeatures/setImage'
|
||||||
|
|
||||||
const dragHelper = new DragHelper()
|
|
||||||
let isDrag = false
|
|
||||||
let startPoint = { x: 99999, y: 99999 }
|
|
||||||
let tempDetail: any = null
|
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
components: {},
|
components: {},
|
||||||
setup(props) {
|
setup(props) {
|
||||||
|
// 拖拽效果相关
|
||||||
|
const dragHelper = new DragHelper()
|
||||||
|
let isDrag = false
|
||||||
|
let startPoint = { x: 99999, y: 99999 }
|
||||||
|
let tempDetail: any = null
|
||||||
|
// 缓存组件用以减少接口请求的次数
|
||||||
|
const compsCache: any = {}
|
||||||
const state = reactive({
|
const state = reactive({
|
||||||
loading: false,
|
loading: false,
|
||||||
loadDone: false,
|
loadDone: false,
|
||||||
@ -65,6 +67,7 @@ export default defineComponent({
|
|||||||
types: [],
|
types: [],
|
||||||
showList: [],
|
showList: [],
|
||||||
})
|
})
|
||||||
|
const store = useStore()
|
||||||
const pageOptions = { type: 1, page: 0, pageSize: 20 }
|
const pageOptions = { type: 1, page: 0, pageSize: 20 }
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
@ -144,28 +147,31 @@ export default defineComponent({
|
|||||||
state.currentCategory = null
|
state.currentCategory = null
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
const dragStart = async (e: any, { id, width, height, cover }: any) => {
|
||||||
...toRefs(state),
|
startPoint = { x: e.x, y: e.y }
|
||||||
load,
|
// tempDetail = await api.home.getTempDetail({ id, type: 1 })
|
||||||
action,
|
// let finalWidth = tempDetail.width
|
||||||
back,
|
// 计算出拖拽到画布数值
|
||||||
selectTypes,
|
const img = await setItem2Data({ width, height, url: cover })
|
||||||
mouseup,
|
dragHelper.start(e, img.canvasWidth)
|
||||||
mousemove,
|
tempDetail = await getCompDetail({ id, type: 1 })
|
||||||
|
if (Array.isArray(JSON.parse(tempDetail.data))) {
|
||||||
|
store.commit('selectItem', { data: JSON.parse(tempDetail.data), type: 'group' })
|
||||||
|
} else {
|
||||||
|
store.commit('selectItem', { data: JSON.parse(tempDetail.data), type: 'text' })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
methods: {
|
const selectItem = async (item: any) => {
|
||||||
...mapActions(['addGroup', 'addWidget']),
|
|
||||||
async selectItem(item: any) {
|
|
||||||
if (isDrag) {
|
if (isDrag) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
this.$store.commit('setShowMoveable', false) // 清理掉上一次的选择
|
store.commit('setShowMoveable', false) // 清理掉上一次的选择
|
||||||
tempDetail = tempDetail || (await api.home.getTempDetail({ id: item.id, type: 1 }))
|
tempDetail = tempDetail || (await getCompDetail({ id: item.id, type: 1 }))
|
||||||
// let group = JSON.parse(tempDetail.data)
|
// let group = JSON.parse(tempDetail.data)
|
||||||
const group: any = await getComponentsData(tempDetail.data)
|
const group: any = await getComponentsData(tempDetail.data)
|
||||||
let parent: any = { x: 0, y: 0 }
|
let parent: any = { x: 0, y: 0 }
|
||||||
const { width: pW, height: pH } = this.$store.getters.dPage
|
const { width: pW, height: pH } = store.getters.dPage
|
||||||
|
|
||||||
Array.isArray(group) &&
|
Array.isArray(group) &&
|
||||||
group.forEach((element: any) => {
|
group.forEach((element: any) => {
|
||||||
@ -176,31 +182,38 @@ export default defineComponent({
|
|||||||
element.left += (pW - parent.width) / 2
|
element.left += (pW - parent.width) / 2
|
||||||
element.top += (pH - parent.height) / 2
|
element.top += (pH - parent.height) / 2
|
||||||
})
|
})
|
||||||
this.addGroup(group)
|
store.dispatch('addGroup', group)
|
||||||
} else {
|
} else {
|
||||||
group.text && (group.text = decodeURIComponent(group.text))
|
group.text && (group.text = decodeURIComponent(group.text))
|
||||||
group.left = pW / 2 - group.fontSize * (group.text.length / 2)
|
group.left = pW / 2 - group.fontSize * (group.text.length / 2)
|
||||||
group.top = pH / 2 - group.fontSize / 2
|
group.top = pH / 2 - group.fontSize / 2
|
||||||
this.addWidget(group)
|
store.dispatch('addWidget', group)
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
async dragStart(e: any, { id, width, height, cover }: any) {
|
|
||||||
startPoint = { x: e.x, y: e.y }
|
function getCompDetail(params: any) {
|
||||||
// tempDetail = await api.home.getTempDetail({ id, type: 1 })
|
// 有缓存则直接返回组件数据,否则请求获取数据
|
||||||
// let finalWidth = tempDetail.width
|
return new Promise((resolve) => {
|
||||||
let finalWidth = 0
|
if (compsCache[params.id]) {
|
||||||
if (finalWidth) {
|
resolve(compsCache[params.id])
|
||||||
const img = await setImageData({ width, height, url: cover })
|
} else api.home.getTempDetail(params).then((res: any) => {
|
||||||
finalWidth = img.canvasWidth
|
resolve(res)
|
||||||
}
|
compsCache[params.id] = res // 缓存请求的组件数据
|
||||||
dragHelper.start(e, finalWidth)
|
})
|
||||||
tempDetail = await api.home.getTempDetail({ id, type: 1 })
|
})
|
||||||
if (Array.isArray(JSON.parse(tempDetail.data))) {
|
}
|
||||||
this.$store.commit('selectItem', { data: JSON.parse(tempDetail.data), type: 'group' })
|
|
||||||
} else {
|
return {
|
||||||
this.$store.commit('selectItem', { data: JSON.parse(tempDetail.data), type: 'text' })
|
...toRefs(state),
|
||||||
}
|
load,
|
||||||
},
|
action,
|
||||||
|
back,
|
||||||
|
selectTypes,
|
||||||
|
mouseup,
|
||||||
|
mousemove,
|
||||||
|
dragStart,
|
||||||
|
selectItem,
|
||||||
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
* @Date: 2021-07-13 02:48:38
|
* @Date: 2021-07-13 02:48:38
|
||||||
* @Description: 本地测试项目请勿修改此文件
|
* @Description: 本地测试项目请勿修改此文件
|
||||||
* @LastEditors: ShawnPhang <https://m.palxp.cn>
|
* @LastEditors: ShawnPhang <https://m.palxp.cn>
|
||||||
* @LastEditTime: 2023-12-11 10:19:28
|
* @LastEditTime: 2024-01-11 17:36:33
|
||||||
*/
|
*/
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import store from '@/store'
|
import store from '@/store'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user