Jelajahi Sumber

fix: 注释跨页移动坐标优化

liutian 3 minggu lalu
induk
melakukan
0e1863cccb

+ 4 - 2
packages/core/src/annotation/freetext.js

@@ -747,11 +747,13 @@ export default class Shape extends Base {
       let windowCoordinates = this.getMouseLocation(event);
       const page = documentViewer.getSelectedPage(windowCoordinates, windowCoordinates)
       if (page.first && page.first !== (this.page + 1)) {
+        const pagePoint = documentViewer.windowToPage(windowCoordinates, page.first, false)
+
         const annotationHeight = Math.abs(leftTop.y - rightBottom.y)
         left = pageX - (startState.clickX - leftTop.x)
-        top = page.first > (this.page + 1) ? 0 : height - annotationHeight
+        top = page.first > (this.page + 1) ? pagePoint.y : pagePoint.y - annotationHeight
         right = pageX - (startState.clickX - rightBottom.x)
-        bottom = page.first > (this.page + 1) ? annotationHeight : height
+        bottom = page.first > (this.page + 1) ? pagePoint.y + annotationHeight : pagePoint.y
         this.page = page.first - 1
 
         this.annotationContainer.remove()

+ 4 - 2
packages/core/src/annotation/ink.js

@@ -391,11 +391,13 @@ export default class Ink extends Base {
       let windowCoordinates = this.getMouseLocation(event)
       const page = documentViewer.getSelectedPage(windowCoordinates, windowCoordinates)
       if (page.first && page.first !== (this.page + 1)) {
+        const pagePoint = documentViewer.windowToPage(windowCoordinates, page.first, false)
+
         const annotationHeight = Math.abs(start.y - end.y)
         x1 = pageX - (startState.clickX - start.x)
-        y1 = page.first > (this.page + 1) ? 0 : height - annotationHeight
+        y1 = page.first > (this.page + 1) ? pagePoint.y : pagePoint.y - annotationHeight
         x2 = pageX - (startState.clickX - this.end.x)
-        y2 = page.first > (this.page + 1) ? annotationHeight : height
+        y2 = page.first > (this.page + 1) ? pagePoint.y + annotationHeight : pagePoint.y
         this.page = page.first - 1
 
         this.annotationContainer.remove()

+ 4 - 2
packages/core/src/annotation/line.js

@@ -452,11 +452,13 @@ export default class Line extends Base {
       let windowCoordinates = this.getMouseLocation(event);
       const page = documentViewer.getSelectedPage(windowCoordinates, windowCoordinates)
       if (page.first && page.first !== (this.page + 1)) {
+        const pagePoint = documentViewer.windowToPage(windowCoordinates, page.first, false)
+
         const annotationHeight = Math.abs(start.y - end.y)
         x1 = pageX - (startState.clickX - this.start.x)
-        y1 = page.first > (this.page + 1) ? 0 : height - annotationHeight
+        y1 = page.first > (this.page + 1) ? pagePoint.y : pagePoint.y - annotationHeight
         x2 = pageX - (startState.clickX - this.end.x)
-        y2 = page.first > (this.page + 1) ? annotationHeight : height
+        y2 = page.first > (this.page + 1) ? pagePoint.y + annotationHeight : pagePoint.y
         this.page = page.first - 1
 
         this.annotationContainer.remove()

+ 4 - 2
packages/core/src/annotation/link.js

@@ -591,11 +591,13 @@ export default class Link extends Base {
       let windowCoordinates = this.getMouseLocation(event);
       const page = documentViewer.getSelectedPage(windowCoordinates, windowCoordinates)
       if (page.first && page.first !== (this.page + 1)) {
+        const pagePoint = documentViewer.windowToPage(windowCoordinates, page.first, false)
+
         const annotationHeight = Math.abs(start.y - end.y)
         x1 = pageX - (startState.clickX - this.start.x)
-        y1 = page.first > (this.page + 1) ? 0 : height - annotationHeight
+        y1 = page.first > (this.page + 1) ? pagePoint.y : pagePoint.y - annotationHeight
         x2 = pageX - (startState.clickX - this.end.x)
-        y2 = page.first > (this.page + 1) ? annotationHeight : height
+        y2 = page.first > (this.page + 1) ? pagePoint.y + annotationHeight : pagePoint.y
         this.page = page.first - 1
 
         this.annotationContainer.remove()

+ 4 - 2
packages/core/src/annotation/shape.js

@@ -573,11 +573,13 @@ export default class Shape extends Base {
       let windowCoordinates = this.getMouseLocation(event);
       const page = documentViewer.getSelectedPage(windowCoordinates, windowCoordinates)
       if (page.first && page.first !== (this.page + 1)) {
+        const pagePoint = documentViewer.windowToPage(windowCoordinates, page.first, false)
+
         const annotationHeight = Math.abs(start.y - end.y)
         x1 = pageX - (startState.clickX - this.start.x)
-        y1 = page.first > (this.page + 1) ? 0 : height - annotationHeight
+        y1 = page.first > (this.page + 1) ? pagePoint.y : pagePoint.y - annotationHeight
         x2 = pageX - (startState.clickX - this.end.x)
-        y2 = page.first > (this.page + 1) ? annotationHeight : height
+        y2 = page.first > (this.page + 1) ? pagePoint.y + annotationHeight : pagePoint.y
         this.page = page.first - 1
 
         this.annotationContainer.remove()

+ 4 - 2
packages/core/src/annotation/stamp.js

@@ -552,11 +552,13 @@ export default class Stamp extends Base {
       let windowCoordinates = this.getMouseLocation(event);
       const page = documentViewer.getSelectedPage(windowCoordinates, windowCoordinates)
       if (page.first && page.first !== (this.page + 1)) {
+        const pagePoint = documentViewer.windowToPage(windowCoordinates, page.first, false)
+
         const annotationHeight = Math.abs(start.y - end.y)
         x1 = pageX - (startState.clickX - this.start.x)
-        y1 = page.first > (this.page + 1) ? 0 : height - annotationHeight
+        y1 = page.first > (this.page + 1) ? pagePoint.y : pagePoint.y - annotationHeight
         x2 = pageX - (startState.clickX - this.end.x)
-        y2 = page.first > (this.page + 1) ? annotationHeight : height
+        y2 = page.first > (this.page + 1) ? pagePoint.y + annotationHeight : pagePoint.y
         this.page = page.first - 1
 
         this.annotationContainer.remove()

+ 4 - 2
packages/core/src/annotation/text.js

@@ -178,11 +178,13 @@ export default class Text extends Base {
     let windowCoordinates = this.getMouseLocation(event);
     const page = documentViewer.getSelectedPage(windowCoordinates, windowCoordinates)
     if (page.first && page.first !== (this.page + 1)) {
+      const pagePoint = documentViewer.windowToPage(windowCoordinates, page.first, false)
+
       const annotationHeight = Math.abs(start.y - end.y)
       x1 = pageX - (startState.clickX - this.start.x)
-      y1 = page.first > (this.page + 1) ? 0 : height - annotationHeight
+      y1 = page.first > (this.page + 1) ? pagePoint.y : pagePoint.y - annotationHeight
       x2 = pageX - (startState.clickX - this.end.x)
-      y2 = page.first > (this.page + 1) ? annotationHeight : height
+      y2 = page.first > (this.page + 1) ? pagePoint.y + annotationHeight : pagePoint.y
       this.page = page.first - 1
 
       this.annotationContainer.remove()

+ 4 - 2
packages/core/src/form/check_box.js

@@ -712,11 +712,13 @@ export default class CheckBox extends Base {
       let windowCoordinates = this.getMouseLocation(event);
       const page = documentViewer.getSelectedPage(windowCoordinates, windowCoordinates)
       if (page.first && page.first !== (this.page + 1)) {
+        const pagePoint = documentViewer.windowToPage(windowCoordinates, page.first, false)
+
         const annotationHeight = Math.abs(start.y - end.y)
         x1 = pageX - (startState.clickX - this.start.x)
-        y1 = page.first > (this.page + 1) ? 0 : height - annotationHeight
+        y1 = page.first > (this.page + 1) ? pagePoint.y : pagePoint.y - annotationHeight
         x2 = pageX - (startState.clickX - this.end.x)
-        y2 = page.first > (this.page + 1) ? annotationHeight : height
+        y2 = page.first > (this.page + 1) ? pagePoint.y + annotationHeight : pagePoint.y
         this.page = page.first - 1
 
         this.annotationContainer.remove()

+ 4 - 2
packages/core/src/form/combo_box.js

@@ -686,11 +686,13 @@ export default class ComboBox extends Base {
       let windowCoordinates = this.getMouseLocation(event);
       const page = documentViewer.getSelectedPage(windowCoordinates, windowCoordinates)
       if (page.first && page.first !== (this.page + 1)) {
+        const pagePoint = documentViewer.windowToPage(windowCoordinates, page.first, false)
+
         const annotationHeight = Math.abs(start.y - end.y)
         x1 = pageX - (startState.clickX - this.start.x)
-        y1 = page.first > (this.page + 1) ? 0 : height - annotationHeight
+        y1 = page.first > (this.page + 1) ? pagePoint.y : pagePoint.y - annotationHeight
         x2 = pageX - (startState.clickX - this.end.x)
-        y2 = page.first > (this.page + 1) ? annotationHeight : height
+        y2 = page.first > (this.page + 1) ? pagePoint.y + annotationHeight : pagePoint.y
         this.page = page.first - 1
 
         this.annotationContainer.remove()

+ 4 - 2
packages/core/src/form/list_box.js

@@ -662,11 +662,13 @@ export default class ListBox extends Base {
       let windowCoordinates = this.getMouseLocation(event);
       const page = documentViewer.getSelectedPage(windowCoordinates, windowCoordinates)
       if (page.first && page.first !== (this.page + 1)) {
+        const pagePoint = documentViewer.windowToPage(windowCoordinates, page.first, false)
+
         const annotationHeight = Math.abs(start.y - end.y)
         x1 = pageX - (startState.clickX - this.start.x)
-        y1 = page.first > (this.page + 1) ? 0 : height - annotationHeight
+        y1 = page.first > (this.page + 1) ? pagePoint.y : pagePoint.y - annotationHeight
         x2 = pageX - (startState.clickX - this.end.x)
-        y2 = page.first > (this.page + 1) ? annotationHeight : height
+        y2 = page.first > (this.page + 1) ? pagePoint.y + annotationHeight : pagePoint.y
         this.page = page.first - 1
 
         this.annotationContainer.remove()

+ 4 - 2
packages/core/src/form/push_button.js

@@ -638,11 +638,13 @@ export default class PushButton extends Base {
       let windowCoordinates = this.getMouseLocation(event);
       const page = documentViewer.getSelectedPage(windowCoordinates, windowCoordinates)
       if (page.first && page.first !== (this.page + 1)) {
+        const pagePoint = documentViewer.windowToPage(windowCoordinates, page.first, false)
+
         const annotationHeight = Math.abs(start.y - end.y)
         x1 = pageX - (startState.clickX - this.start.x)
-        y1 = page.first > (this.page + 1) ? 0 : height - annotationHeight
+        y1 = page.first > (this.page + 1) ? pagePoint.y : pagePoint.y - annotationHeight
         x2 = pageX - (startState.clickX - this.end.x)
-        y2 = page.first > (this.page + 1) ? annotationHeight : height
+        y2 = page.first > (this.page + 1) ? pagePoint.y + annotationHeight : pagePoint.y
         this.page = page.first - 1
 
         this.annotationContainer.remove()

+ 4 - 2
packages/core/src/form/radio_button.js

@@ -715,11 +715,13 @@ export default class RadioButton extends Base {
       let windowCoordinates = this.getMouseLocation(event);
       const page = documentViewer.getSelectedPage(windowCoordinates, windowCoordinates)
       if (page.first && page.first !== (this.page + 1)) {
+        const pagePoint = documentViewer.windowToPage(windowCoordinates, page.first, false)
+
         const annotationHeight = Math.abs(start.y - end.y)
         x1 = pageX - (startState.clickX - this.start.x)
-        y1 = page.first > (this.page + 1) ? 0 : height - annotationHeight
+        y1 = page.first > (this.page + 1) ? pagePoint.y : pagePoint.y - annotationHeight
         x2 = pageX - (startState.clickX - this.end.x)
-        y2 = page.first > (this.page + 1) ? annotationHeight : height
+        y2 = page.first > (this.page + 1) ? pagePoint.y + annotationHeight : pagePoint.y
         this.page = page.first - 1
 
         this.annotationContainer.remove()

+ 4 - 2
packages/core/src/form/signature_fields.js

@@ -573,11 +573,13 @@ export default class SignatureFields extends Base {
       let windowCoordinates = this.getMouseLocation(event);
       const page = documentViewer.getSelectedPage(windowCoordinates, windowCoordinates)
       if (page.first && page.first !== (this.page + 1)) {
+        const pagePoint = documentViewer.windowToPage(windowCoordinates, page.first, false)
+
         const annotationHeight = Math.abs(start.y - end.y)
         x1 = pageX - (startState.clickX - this.start.x)
-        y1 = page.first > (this.page + 1) ? 0 : height - annotationHeight
+        y1 = page.first > (this.page + 1) ? pagePoint.y : pagePoint.y - annotationHeight
         x2 = pageX - (startState.clickX - this.end.x)
-        y2 = page.first > (this.page + 1) ? annotationHeight : height
+        y2 = page.first > (this.page + 1) ? pagePoint.y + annotationHeight : pagePoint.y
         this.page = page.first - 1
 
         this.annotationContainer.remove()

+ 4 - 2
packages/core/src/form/text_field.js

@@ -644,11 +644,13 @@ export default class TextField extends Base {
       let windowCoordinates = this.getMouseLocation(event);
       const page = documentViewer.getSelectedPage(windowCoordinates, windowCoordinates)
       if (page.first && page.first !== (this.page + 1)) {
+        const pagePoint = documentViewer.windowToPage(windowCoordinates, page.first, false)
+
         const annotationHeight = Math.abs(start.y - end.y)
         x1 = pageX - (startState.clickX - this.start.x)
-        y1 = page.first > (this.page + 1) ? 0 : height - annotationHeight
+        y1 = page.first > (this.page + 1) ? pagePoint.y : pagePoint.y - annotationHeight
         x2 = pageX - (startState.clickX - this.end.x)
-        y2 = page.first > (this.page + 1) ? annotationHeight : height
+        y2 = page.first > (this.page + 1) ? pagePoint.y + annotationHeight : pagePoint.y
         this.page = page.first - 1
 
         this.annotationContainer.remove()

+ 5 - 3
packages/core/src/index.js

@@ -2306,17 +2306,19 @@ class ComPDFKitViewer {
     }
   }
 
-  windowToPage(coordinates, pageNumber) {
+  windowToPage(coordinates, pageNumber, initial = true) {
     if (pageNumber > this.page && pageNumber < 1) return null
     const el = this.pdfViewer._pages[pageNumber - 1].div
     const left = el.offsetLeft
     const top = el.offsetTop + el.offsetParent?.offsetTop
 
     const scale = this.scale
+    const x = initial ? (coordinates.x - left) / scale : coordinates.x - left
+    const y = initial ? (coordinates.y - top) / scale : coordinates.y - top
     return {
       pageNumber,
-      x: (coordinates.x - left) / scale,
-      y: (coordinates.y - top) / scale
+      x,
+      y
     }
   }