@@ -353,7 +353,7 @@ export default {
353353 : { x: 0 , y: 0 }
354354
355355 this .cachePageBounds ()
356- const pageRect = this .getPageBoundsMap ()[ ` ${ docIndex} - ${ pageIndex} ` ] ? . rect
356+ const pageRect = this .getPageRect ( docIndex, pageIndex)
357357 if (pointerOffset && typeof pointerOffset .x === ' number' && typeof pointerOffset .y === ' number' ) {
358358 this .draggingInitialMouseOffset .x = pointerOffset .x
359359 this .draggingInitialMouseOffset .y = pointerOffset .y
@@ -483,6 +483,21 @@ export default {
483483 getPageBoundsList () {
484484 return this ._pagesBoundingRectsList || []
485485 },
486+ getPageRect (docIndex , pageIndex ) {
487+ return this .getPageBoundsMap ()[` ${ docIndex} -${ pageIndex} ` ]? .rect || null
488+ },
489+ getPointerPosition (event ) {
490+ if (event ? .type ? .includes ? .(' touch' )) {
491+ return {
492+ x: event .touches ? .[0 ]? .clientX ,
493+ y: event .touches ? .[0 ]? .clientY ,
494+ }
495+ }
496+ return {
497+ x: event ? .clientX ,
498+ y: event ? .clientY ,
499+ }
500+ },
486501
487502 getDisplayedPageScale (docIndex , pageIndex ) {
488503 this .pageBoundsVersion
@@ -546,9 +561,9 @@ export default {
546561
547562 handleMouseMove (event ) {
548563 if (! this .isAddingMode || ! this .previewElement ) return
549- const clientX = event . type . includes ( ' touch ' ) ? event . touches [ 0 ]. clientX : event . clientX
550- const clientY = event . type . includes ( ' touch ' ) ? event . touches [ 0 ]. clientY : event . clientY
551- this .pendingHoverClientPos = { x: clientX , y: clientY }
564+ const { x , y } = this . getPointerPosition ( event )
565+ if (x === undefined || y === undefined ) return
566+ this .pendingHoverClientPos = { x, y }
552567 if (this .hoverRafId ) return
553568 this .hoverRafId = window .requestAnimationFrame (() => {
554569 this .hoverRafId = 0
@@ -833,7 +848,7 @@ export default {
833848 this .cachePageBounds ()
834849 }
835850
836- const currentPageRect = this .getPageBoundsMap ()[ ` ${ docIndex} - ${ currentPageIndex} ` ] ? . rect
851+ const currentPageRect = this .getPageRect ( docIndex, currentPageIndex)
837852 if (currentPageRect) {
838853 const pagesScale = this .getDisplayedPageScale (docIndex, currentPageIndex)
839854 const relX = (mouseX - currentPageRect .left - this .draggingElementShift .x ) / pagesScale - (this .draggingInitialMouseOffset .x / pagesScale)
@@ -850,8 +865,7 @@ export default {
850865 const objWidth = payload .width !== undefined ? payload .width : targetObject .width
851866 const objHeight = payload .height !== undefined ? payload .height : targetObject .height
852867
853- const currentPageWidth = this .getPageWidth (docIndex, currentPageIndex)
854- const currentPageHeight = this .getPageHeight (docIndex, currentPageIndex)
868+ const { width: currentPageWidth , height: currentPageHeight } = this .getPageSize (docIndex, currentPageIndex)
855869 if (newX >= 0 && newY >= 0 &&
856870 newX + objWidth <= currentPageWidth &&
857871 newY + objHeight <= currentPageHeight) {
@@ -866,26 +880,16 @@ export default {
866880 const pageWidth = this .getPageWidth (docIndex, pIndex)
867881 const pageHeight = this .getPageHeight (docIndex, pIndex)
868882
869- const visibleLeft = Math .max (0 , newX)
870- const visibleTop = Math .max (0 , newY)
871- const visibleRight = Math .min (pageWidth, newX + objWidth)
872- const visibleBottom = Math .min (pageHeight, newY + objHeight)
873-
874- if (visibleRight > visibleLeft && visibleBottom > visibleTop) {
875- const visibleArea = (visibleRight - visibleLeft) * (visibleBottom - visibleTop)
876- if (visibleArea > maxVisibleArea) {
877- maxVisibleArea = visibleArea
878- bestPageIndex = pIndex
879- }
883+ const visibleArea = this .getVisibleArea (newX, newY, objWidth, objHeight, pageWidth, pageHeight)
884+ if (visibleArea > maxVisibleArea) {
885+ maxVisibleArea = visibleArea
886+ bestPageIndex = pIndex
880887 }
881888 }
882889
883890 if (bestPageIndex !== currentPageIndex) {
884- const pageWidth = this .getPageWidth (docIndex, bestPageIndex)
885- const pageHeight = this .getPageHeight (docIndex, bestPageIndex)
886-
887- const adjustedX = Math .max (0 , Math .min (newX, pageWidth - objWidth))
888- const adjustedY = Math .max (0 , Math .min (newY, pageHeight - objHeight))
891+ const { width: pageWidth , height: pageHeight } = this .getPageSize (docIndex, bestPageIndex)
892+ const { x: adjustedX , y: adjustedY } = this .clampPosition (newX, newY, objWidth, objHeight, pageWidth, pageHeight)
889893
890894 this .removeObjectFromPage (docIndex, currentPageIndex, objectId)
891895 const updatedObject = {
@@ -899,8 +903,7 @@ export default {
899903 return
900904 }
901905
902- const pageWidth = this .getPageWidth (docIndex, currentPageIndex)
903- const pageHeight = this .getPageHeight (docIndex, currentPageIndex)
906+ const { width: pageWidth , height: pageHeight } = this .getPageSize (docIndex, currentPageIndex)
904907
905908 if (newX < 0 || newY < 0 ||
906909 newX + objWidth > pageWidth ||
@@ -971,18 +974,22 @@ export default {
971974 }
972975 }
973976
974- const targetPageRect = this .getPageBoundsMap ()[ ` ${ docIndex} - ${ targetPageIndex} ` ] ? . rect
977+ const targetPageRect = this .getPageRect ( docIndex, targetPageIndex)
975978 if (! targetPageRect) return currentPageIndex
976979
977980 const pagesScale = this .getDisplayedPageScale (docIndex, targetPageIndex)
978981 const relX = (mouseX - targetPageRect .left - this .draggingElementShift .x ) / pagesScale - (this .draggingInitialMouseOffset .x / pagesScale)
979982 const relY = (mouseY - targetPageRect .top - this .draggingElementShift .y ) / pagesScale - (this .draggingInitialMouseOffset .y / pagesScale)
980983
981- const pageWidth = this .getPageWidth (docIndex, targetPageIndex)
982- const pageHeight = this .getPageHeight (docIndex, targetPageIndex)
983-
984- const clampedX = Math .max (0 , Math .min (relX, pageWidth - targetObject .width ))
985- const clampedY = Math .max (0 , Math .min (relY, pageHeight - targetObject .height ))
984+ const { width: pageWidth , height: pageHeight } = this .getPageSize (docIndex, targetPageIndex)
985+ const { x: clampedX , y: clampedY } = this .clampPosition (
986+ relX,
987+ relY,
988+ targetObject .width ,
989+ targetObject .height ,
990+ pageWidth,
991+ pageHeight,
992+ )
986993
987994 if (targetPageIndex !== currentPageIndex) {
988995 this .removeObjectFromPage (docIndex, currentPageIndex, objectId)
@@ -1026,6 +1033,28 @@ export default {
10261033 const measurement = this .getCachedMeasurement (docIndex, pageIndex, pageRef)
10271034 return measurement .height
10281035 },
1036+ getPageSize (docIndex , pageIndex ) {
1037+ return {
1038+ width: this .getPageWidth (docIndex, pageIndex),
1039+ height: this .getPageHeight (docIndex, pageIndex),
1040+ }
1041+ },
1042+ clampPosition (x , y , width , height , pageWidth , pageHeight ) {
1043+ return {
1044+ x: Math .max (0 , Math .min (x, pageWidth - width)),
1045+ y: Math .max (0 , Math .min (y, pageHeight - height)),
1046+ }
1047+ },
1048+ getVisibleArea (newX , newY , objWidth , objHeight , pageWidth , pageHeight ) {
1049+ const visibleLeft = Math .max (0 , newX)
1050+ const visibleTop = Math .max (0 , newY)
1051+ const visibleRight = Math .min (pageWidth, newX + objWidth)
1052+ const visibleBottom = Math .min (pageHeight, newY + objHeight)
1053+ if (visibleRight <= visibleLeft || visibleBottom <= visibleTop) {
1054+ return 0
1055+ }
1056+ return (visibleRight - visibleLeft) * (visibleBottom - visibleTop)
1057+ },
10291058 getCachedMeasurement (docIndex , pageIndex , pageRef ) {
10301059 const cacheKey = ` ${ docIndex} -${ pageIndex} `
10311060 const cached = this ._pageMeasurementCache [cacheKey]
0 commit comments