feat: 优化拖拽效果 防止多次请求后台数据

This commit is contained in:
wangjiahao 2021-06-21 18:43:41 +08:00
parent f155f3d799
commit 394fb46b3c
2 changed files with 85 additions and 68 deletions

View File

@ -298,6 +298,10 @@ export default {
horizontal: {
type: Boolean,
default: true
},
changeStyle: {
require: true,
type: Object
}
},
data: function() {
@ -542,69 +546,21 @@ export default {
this.bounds = this.calcResizeLimits()
}
this.changeHeight(val)
},
changeStyle(val) {
this.beforeDestroyFunction()
this.createdFunction()
this.mountedFunction()
}
},
created: function() {
// eslint-disable-next-line propminWidthmaxWidth
if (this.maxWidth && this.minWidth > this.maxWidth) console.warn('[Vdr warn]: Invalid prop: minWidth cannot be greater than maxWidth')
// eslint-disable-next-line propminHeightmaxHeight'
if (this.maxWidth && this.minHeight > this.maxHeight) console.warn('[Vdr warn]: Invalid prop: minHeight cannot be greater than maxHeight')
this.elmX = 0
this.elmY = 0
this.elmW = 0
this.elmH = 0
this.lastCenterX = 0
this.lastCenterY = 0
this.fixedXName = ''
this.fixedYName = ''
this.fixedX = 0
this.fixedY = 0
this.TL = {}
this.TR = {}
this.BL = {}
this.BR = {}
this.resetBoundsAndMouseState()
this.createdFunction()
},
mounted: function() {
// private
// this.isConflictCheck = this.horizontal
// this.snap = this.horizontal
// this.snapTolerance = 5
// this.grid = [10, 10]
if (!this.enableNativeDrag) {
this.$el.ondragstart = () => false
}
const [parentWidth, parentHeight] = this.getParentSize()
this.parentWidth = parentWidth
this.parentHeight = parentHeight
const [width, height] = getComputedSize(this.$el)
this.aspectFactor = (this.w !== 'auto' ? this.w : width) / (this.h !== 'auto' ? this.h : height)
if (this.outsideAspectRatio) {
this.aspectFactor = this.outsideAspectRatio
}
this.width = this.w !== 'auto' ? this.w : width
// console.log('width1:' + this.width)
this.height = this.h !== 'auto' ? this.h : height
this.right = this.parentWidth - this.width - this.left
this.bottom = this.parentHeight - this.height - this.top
// data-*
this.settingAttribute()
//
addEvent(document.documentElement, 'mousedown', this.deselect)
addEvent(document.documentElement, 'touchend touchcancel', this.deselect)
//
addEvent(window, 'resize', this.checkParentSize)
this.mountedFunction()
},
beforeDestroy: function() {
removeEvent(document.documentElement, 'mousedown', this.deselect)
removeEvent(document.documentElement, 'touchstart', this.handleUp)
removeEvent(document.documentElement, 'mousemove', this.move)
removeEvent(document.documentElement, 'touchmove', this.move)
removeEvent(document.documentElement, 'mouseup', this.handleUp)
removeEvent(document.documentElement, 'touchend touchcancel', this.deselect)
removeEvent(window, 'resize', this.checkParentSize)
this.beforeDestroyFunction()
},
methods: {
//
@ -1506,6 +1462,68 @@ export default {
style.rotate = this.rotate
this.hasMove = true
this.$store.commit('setShapeStyle', style)
},
mountedFunction() {
// private
// this.isConflictCheck = this.horizontal
// this.snap = this.horizontal
// this.snapTolerance = 5
// this.grid = [10, 10]
if (!this.enableNativeDrag) {
this.$el.ondragstart = () => false
}
const [parentWidth, parentHeight] = this.getParentSize()
this.parentWidth = parentWidth
this.parentHeight = parentHeight
const [width, height] = getComputedSize(this.$el)
this.aspectFactor = (this.w !== 'auto' ? this.w : width) / (this.h !== 'auto' ? this.h : height)
if (this.outsideAspectRatio) {
this.aspectFactor = this.outsideAspectRatio
}
this.width = this.w !== 'auto' ? this.w : width
// console.log('width1:' + this.width)
this.height = this.h !== 'auto' ? this.h : height
this.right = this.parentWidth - this.width - this.left
this.bottom = this.parentHeight - this.height - this.top
// data-*
this.settingAttribute()
//
addEvent(document.documentElement, 'mousedown', this.deselect)
addEvent(document.documentElement, 'touchend touchcancel', this.deselect)
//
addEvent(window, 'resize', this.checkParentSize)
},
createdFunction() {
// eslint-disable-next-line propminWidthmaxWidth
if (this.maxWidth && this.minWidth > this.maxWidth) console.warn('[Vdr warn]: Invalid prop: minWidth cannot be greater than maxWidth')
// eslint-disable-next-line propminHeightmaxHeight'
if (this.maxWidth && this.minHeight > this.maxHeight) console.warn('[Vdr warn]: Invalid prop: minHeight cannot be greater than maxHeight')
this.elmX = 0
this.elmY = 0
this.elmW = 0
this.elmH = 0
this.lastCenterX = 0
this.lastCenterY = 0
this.fixedXName = ''
this.fixedYName = ''
this.fixedX = 0
this.fixedY = 0
this.TL = {}
this.TR = {}
this.BL = {}
this.BR = {}
this.resetBoundsAndMouseState()
},
beforeDestroyFunction() {
removeEvent(document.documentElement, 'mousedown', this.deselect)
removeEvent(document.documentElement, 'touchstart', this.handleUp)
removeEvent(document.documentElement, 'mousemove', this.move)
removeEvent(document.documentElement, 'touchmove', this.move)
removeEvent(document.documentElement, 'mouseup', this.handleUp)
removeEvent(document.documentElement, 'touchend touchcancel', this.deselect)
removeEvent(window, 'resize', this.checkParentSize)
}
}

View File

@ -29,7 +29,7 @@
:class="{'gap_class':canvasStyleData.panel.gap==='yes'}"
:snap="true"
:snap-tolerance="1"
:change-style="customStyleHistory"
:change-style="customStyle"
@refLineParams="getRefLineParams"
>
<component
@ -193,6 +193,16 @@ export default {
])
},
watch: {
customStyle: {
handler(newVal, oldVla) {
//
// if (oldVla && newVal !== oldVla) {
// this.showDrag = false
// this.$nextTick(() => (this.showDrag = true))
// }
},
deep: true
},
outStyle: {
handler(newVal, oldVla) {
this.changeScale()
@ -454,17 +464,6 @@ export default {
}
},
changeScale() {
//
const style = {
width: this.format(this.canvasStyleData.width, this.scaleWidth) + 'px',
height: this.format(this.canvasStyleData.height, this.scaleHeight) + 'px'
}
if (this.customStyleHistory && this.customStyleHistory !== style) {
this.showDrag = false
this.$nextTick(() => (this.showDrag = true))
}
this.customStyleHistory = style
if (this.canvasStyleData.matrixCount) {
this.matrixCount = this.canvasStyleData.matrixCount
}