Skip to content

Commit 0b78b1b

Browse files
authored
Merge pull request #111 from LibreSign/fix/prevent-move-element-to-outside-of-page
chore: prevent to move element to outside of page
2 parents f5c8448 + d1cc361 commit 0b78b1b

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

src/Components/Image.vue

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
:style="{
77
width: `${width + dw}px`,
88
height: `${Math.round((width + dw) / ratio)}px`,
9-
transform: `translate(${x + dx}px, ${y + dy}px)`,
9+
transform: translateCoordinates(),
1010
}">
1111
<div class="absolute w-full h-full cursor-grab"
1212
:class="[
@@ -74,6 +74,8 @@ export default {
7474
'x',
7575
'y',
7676
'pageScale',
77+
'pageWidth',
78+
'pageHeight',
7779
'fixSize',
7880
],
7981
data() {
@@ -177,15 +179,15 @@ export default {
177179
if (!coordinate) return console.log('ERROR')
178180
if (this.operation === 'move') {
179181
this.$emit('onUpdate', {
180-
x: this.x + this.dx,
181-
y: this.y + this.dy,
182+
x: coordinate.detail.x,
183+
y: coordinate.detail.y,
182184
})
183185
this.dx = 0
184186
this.dy = 0
185187
} else if (this.operation === 'scale') {
186188
this.$emit('onUpdate', {
187-
x: this.x + this.dx,
188-
y: this.y + this.dy,
189+
x: coordinate.detail.x,
190+
y: coordinate.detail.y,
189191
width: this.width + this.dw,
190192
height: Math.round((this.width + this.dw) / this.ratio),
191193
})

src/Components/ItemEventsMixin.vue

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ export default {
4444
this.y_mixin = event.clientY
4545
window.removeEventListener('mousemove', this.handlePanMove)
4646
window.removeEventListener('mouseup', this.handlePanEnd)
47+
const x = Math.max(0, Math.min(this.x + this.dx, this.pageWidth - this.width))
48+
const y = Math.max(0, Math.min(this.y + this.dy, this.pageHeight - this.height))
4749
return {
48-
detail: { x: this.x_mixin, y: this.y_mixin },
50+
detail: { x, y },
4951
}
5052
},
5153
handleTouchStart(event) {
@@ -82,10 +84,17 @@ export default {
8284
8385
window.removeEventListener('touchmove', this.handlePanMove)
8486
window.removeEventListener('touchend', this.handlePanEnd)
87+
const x = Math.max(0, Math.min(this.x + this.dx, this.pageWidth - this.width))
88+
const y = Math.max(0, Math.min(this.y + this.dy, this.pageHeight - this.height))
8589
return {
86-
detail: { x: this.x_mixin, y: this.y_mixin },
90+
detail: { x, y },
8791
}
8892
},
93+
translateCoordinates() {
94+
const x = Math.max(0, Math.min(this.x + this.dx, this.pageWidth - this.width))
95+
const y = Math.max(0, Math.min(this.y + this.dy, this.pageHeight - this.height))
96+
return 'translate(' + x + 'px, ' + y + 'px)'
97+
}
8998
},
9099
}
91100
</script>

src/Components/TextItem.vue

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<!-- </toolbar-component>-->
66
<TapoutComponent class="absolute left-0 top-0 select-none"
7-
:style="{ transform: `translate(${x + dx}px, ${y + dy}px)` }"
7+
:style="{ transform: translateCoordinates() }"
88
@tapout="onBlur">
99
<TapoutComponent v-if="operation === 'edit' || operation === 'tool'"
1010
ref="toolBox"
@@ -141,7 +141,21 @@ export default {
141141
CloseCircleIcon,
142142
},
143143
mixins: [itemEventsMixin],
144-
props: ['size', 'text', 'lineHeight', 'x', 'y', 'fontFamily', 'pageScale', 'currentPage', 'showLineSizeSelect', 'showFontSelect', 'showFontSizeSelect'],
144+
props: [
145+
'size',
146+
'text',
147+
'lineHeight',
148+
'x',
149+
'y',
150+
'pageWidth',
151+
'pageHeight',
152+
'fontFamily',
153+
'pageScale',
154+
'currentPage',
155+
'showLineSizeSelect',
156+
'showFontSelect',
157+
'showFontSizeSelect'
158+
],
145159
data() {
146160
return {
147161
Families: Object.keys(Fonts),
@@ -211,8 +225,8 @@ export default {
211225
return this.$refs.editable.focus()
212226
}
213227
this.$emit('onUpdate', {
214-
x: this.x + this.dx,
215-
y: this.y + this.dy,
228+
x: coordinate.detail.x,
229+
y: coordinate.detail.y,
216230
})
217231
this.dx = 0
218232
this.dy = 0

src/VuePdfEditor.vue

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@
141141
:origin-width="object.originWidth"
142142
:origin-height="object.originHeight"
143143
:page-scale="pagesScale[pIndex]"
144+
:page-width="pageSizes[pIndex + 1].width"
145+
:page-height="pageSizes[pIndex + 1].height"
144146
@onUpdate="updateObject(object.id, $event)"
145147
@onDelete="deleteObject(object.id)" />
146148
</div>
@@ -157,6 +159,8 @@
157159
:font-family="object.fontFamily"
158160
:current-page="object.currentPage"
159161
:page-scale="pagesScale[pIndex]"
162+
:page-width="pageSizes[pIndex + 1].width"
163+
:page-height="pageSizes[pIndex + 1].height"
160164
@onUpdate="updateObject(object.id, $event)"
161165
@onDelete="deleteObject(object.id)"
162166
@onSelectFont="selectFontFamily" />
@@ -473,6 +477,7 @@ export default {
473477
this.pdfDocument = null
474478
this.pages = []
475479
this.pagesScale = []
480+
this.pageSizes = []
476481
this.allObjects = []
477482
},
478483
async addPDF(file) {
@@ -516,6 +521,7 @@ export default {
516521
width: measurement[2],
517522
height: measurement[3],
518523
}
524+
this.pageSizes[page.pageNumber] = data.measurement[page.pageNumber]
519525
})
520526
this.$emit('pdf-editor:end-init', data)
521527
}
@@ -558,6 +564,8 @@ export default {
558564
originHeight: height,
559565
canvasWidth,
560566
canvasHeight,
567+
pageWidth: this.pageSizes[this.selectedPageIndex + 1].width,
568+
pageHeight: this.pageSizes[this.selectedPageIndex + 1].height,
561569
x,
562570
y,
563571
isSealImage,
@@ -586,6 +594,8 @@ export default {
586594
width: 0, // recalculate after editing
587595
lineHeight: 1.4,
588596
fontFamily: this.currentFont,
597+
pageWidth: this.pageSizes[currentPage + 1].width,
598+
pageHeight: this.pageSizes[currentPage + 1].height,
589599
x,
590600
y,
591601
currentPage,

0 commit comments

Comments
 (0)