Files
docuseal/app/javascript/template_builder/page.vue
T

172 lines
3.8 KiB
Vue
Raw Normal View History

2023-05-21 17:59:36 +03:00
<template>
2023-06-04 01:08:45 +03:00
<div class="relative cursor-crosshair select-none">
2023-05-21 17:59:36 +03:00
<img
ref="image"
:src="image.url"
:width="width"
2023-05-31 00:50:43 +03:00
class="shadow-md mb-4"
2023-05-21 17:59:36 +03:00
:height="height"
loading="lazy"
>
<div
class="top-0 bottom-0 left-0 right-0 absolute"
2023-06-03 00:53:46 +03:00
@pointerdown="onStartDraw"
2023-05-21 17:59:36 +03:00
>
<FieldArea
v-for="(item, i) in areas"
:key="i"
2023-06-03 00:53:46 +03:00
:ref="setAreaRefs"
2023-06-04 01:08:45 +03:00
:area="item.area"
2023-05-21 17:59:36 +03:00
:field="item.field"
2023-06-07 01:16:43 +03:00
@start-resize="[showMask = true, isResize = true]"
@stop-resize="[showMask = false, isResize = false]"
@start-drag="[showMask = true, isMove = true]"
@stop-drag="[showMask = false, isMove = false]"
2023-06-09 01:43:18 +03:00
@remove="$emit('remove-area', item.area)"
2023-05-21 17:59:36 +03:00
/>
<FieldArea
v-if="newArea"
2023-06-07 01:16:43 +03:00
:field="{ submitter_uuid: selectedSubmitter.uuid }"
2023-06-04 01:08:45 +03:00
:area="newArea"
2023-05-21 17:59:36 +03:00
/>
</div>
<div
2023-06-03 00:53:46 +03:00
v-show="isDrag || showMask"
2023-05-21 17:59:36 +03:00
id="mask"
ref="mask"
2023-06-07 01:16:43 +03:00
class="top-0 bottom-0 left-0 right-0 absolute z-10"
2023-06-08 00:20:42 +03:00
:class="{ 'cursor-grab': isDrag || isMove, 'cursor-nwse-resize': isResize || isDraw }"
2023-05-21 17:59:36 +03:00
@pointermove="onPointermove"
2023-05-23 00:03:26 +03:00
@dragover.prevent
@drop="onDrop"
2023-05-21 17:59:36 +03:00
@pointerup="onPointerup"
/>
</div>
</template>
<script>
import FieldArea from './area'
export default {
2023-05-31 23:19:20 +03:00
name: 'TemplatePage',
2023-05-21 17:59:36 +03:00
components: {
FieldArea
},
props: {
image: {
type: Object,
required: true
},
areas: {
type: Array,
required: false,
default: () => []
},
2023-06-07 01:16:43 +03:00
selectedSubmitter: {
2023-06-04 01:08:45 +03:00
type: Object,
2023-06-07 01:16:43 +03:00
required: true
2023-06-04 01:08:45 +03:00
},
2023-06-08 00:20:42 +03:00
isDraw: {
type: Boolean,
required: false,
default: false
},
2023-05-23 00:03:26 +03:00
isDrag: {
type: Boolean,
required: false,
default: false
},
2023-05-21 17:59:36 +03:00
number: {
type: Number,
required: true
}
},
2023-06-04 01:08:45 +03:00
emits: ['draw', 'drop-field', 'remove-area'],
2023-05-21 17:59:36 +03:00
data () {
return {
2023-06-03 00:53:46 +03:00
areaRefs: [],
2023-05-21 17:59:36 +03:00
showMask: false,
2023-06-07 01:16:43 +03:00
isMove: false,
isResize: false,
2023-05-21 17:59:36 +03:00
newArea: null
}
},
computed: {
width () {
return this.image.metadata.width
},
height () {
return this.image.metadata.height
}
},
2023-06-03 00:53:46 +03:00
beforeUpdate () {
this.areaRefs = []
},
2023-05-21 17:59:36 +03:00
methods: {
2023-06-03 00:53:46 +03:00
setAreaRefs (el) {
if (el) {
this.areaRefs.push(el)
}
},
2023-05-23 00:03:26 +03:00
onDrop (e) {
this.$emit('drop-field', {
2023-06-04 01:08:45 +03:00
x: e.layerX,
y: e.layerY,
maskW: this.$refs.mask.clientWidth,
maskH: this.$refs.mask.clientHeight,
2023-05-23 00:03:26 +03:00
page: this.number
})
},
2023-06-03 00:53:46 +03:00
onStartDraw (e) {
this.showMask = true
this.$nextTick(() => {
2023-05-21 17:59:36 +03:00
this.newArea = {
2023-05-23 00:22:17 +03:00
initialX: e.layerX / this.$refs.mask.clientWidth,
initialY: e.layerY / this.$refs.mask.clientHeight,
x: e.layerX / this.$refs.mask.clientWidth,
y: e.layerY / this.$refs.mask.clientHeight,
2023-05-21 17:59:36 +03:00
w: 0,
h: 0
}
2023-06-03 00:53:46 +03:00
})
2023-05-21 17:59:36 +03:00
},
onPointermove (e) {
if (this.newArea) {
2023-05-23 00:22:17 +03:00
const dx = e.layerX / this.$refs.mask.clientWidth - this.newArea.initialX
const dy = e.layerY / this.$refs.mask.clientHeight - this.newArea.initialY
2023-05-21 17:59:36 +03:00
if (dx > 0) {
this.newArea.x = this.newArea.initialX
} else {
2023-05-23 00:22:17 +03:00
this.newArea.x = e.layerX / this.$refs.mask.clientWidth
2023-05-21 17:59:36 +03:00
}
if (dy > 0) {
this.newArea.y = this.newArea.initialY
} else {
2023-05-23 00:22:17 +03:00
this.newArea.y = e.layerY / this.$refs.mask.clientHeight
2023-05-21 17:59:36 +03:00
}
this.newArea.w = Math.abs(dx)
this.newArea.h = Math.abs(dy)
}
},
onPointerup (e) {
2023-06-03 00:53:46 +03:00
if (this.newArea) {
2023-05-21 17:59:36 +03:00
this.$emit('draw', {
x: this.newArea.x,
y: this.newArea.y,
2023-06-03 00:53:46 +03:00
w: this.newArea.w,
h: this.newArea.h,
2023-05-21 17:59:36 +03:00
page: this.number
})
}
2023-06-03 00:53:46 +03:00
this.showMask = false
2023-05-21 17:59:36 +03:00
this.newArea = null
}
}
}
</script>