Bladeren bron

add: 文字选中新增单词双击选中

liutian 7 maanden geleden
bovenliggende
commit
0bf4ce64b1
2 gewijzigde bestanden met toevoegingen van 58 en 0 verwijderingen
  1. 41 0
      packages/core/src/TextSelection.ts
  2. 17 0
      packages/core/src/worker/compdfkit_worker.js

+ 41 - 0
packages/core/src/TextSelection.ts

@@ -66,6 +66,7 @@ export default class TextSelection {
     this.handleToolMode = this.handleToolMode.bind(this)
     this.handleMouseDown = this.handleMouseDown.bind(this)
     this.handleMouseMove = this.handleMouseMove.bind(this)
+    this.handleDobuleClick = this.handleDobuleClick.bind(this)
     this.handleMouseUp = this.handleMouseUp.bind(this)
     this.handleKeyDown = this.handleKeyDown.bind(this)
     this.handleTextPopup = this.handleTextPopup.bind(this)
@@ -87,6 +88,7 @@ export default class TextSelection {
     document.removeEventListener('touchstart', this.handleMouseDown)
     document.removeEventListener('mousemove', this.handleMouseMove)
     document.removeEventListener('touchmove', this.handleMouseMove)
+    document.removeEventListener('dblclick', this.handleDobuleClick)
 
     if (this.toolMode === 'view' || this.toolMode === 'annotation') {
       document.addEventListener('keydown', this.handleKeyDown)
@@ -94,6 +96,7 @@ export default class TextSelection {
       document.addEventListener('touchstart', this.handleMouseDown)
       document.addEventListener('mousemove', this.handleMouseMove)
       document.addEventListener('touchmove', this.handleMouseMove)
+      document.addEventListener('dblclick', this.handleDobuleClick)
     }
   }
 
@@ -129,6 +132,7 @@ export default class TextSelection {
       document.removeEventListener('touchstart', this.handleMouseDown)
       document.removeEventListener('mousemove', this.handleMouseMove)
       document.removeEventListener('touchmove', this.handleMouseMove)
+      document.removeEventListener('dblclick', this.handleDobuleClick)
       this._tool = toolType
       return
     }
@@ -138,6 +142,7 @@ export default class TextSelection {
       document.addEventListener('touchstart', this.handleMouseDown)
       document.addEventListener('mousemove', this.handleMouseMove)
       document.addEventListener('touchmove', this.handleMouseMove)
+      document.addEventListener('dblclick', this.handleDobuleClick)
     }
     this._tool = toolType
   }
@@ -215,6 +220,8 @@ export default class TextSelection {
       textPtr: this.#textPtr,
       point
     })
+    const startPoint = this.startPoint
+    if (startPoint && (startPoint.x === point.x || startPoint.y === point.y)) return
     if (isText) {
       this.container?.classList.add('text')
       this.endPoint = point
@@ -234,6 +241,39 @@ export default class TextSelection {
     this.updateSelection()
   }
 
+  async handleDobuleClick(event: MouseEvent) {
+    event.stopPropagation()
+    event.preventDefault()
+    const inPage = this.testPoint(event)
+    if (!inPage || document.querySelector('.annotationContainer .outline-container')) {
+      this.container?.classList.remove('text')
+      return
+    }
+    const { x, y } = inPage
+    const point = getInitialPoint({ x, y }, this.viewport, this.scale)
+
+    const isText = await this.messageHandler.sendWithPromise('GetCharIndexAtPos', {
+      pagePtr: this.#pagePtr,
+      textPtr: this.#textPtr,
+      point
+    })
+
+    if (!isText) return
+
+    const selection = await this.messageHandler.sendWithPromise('GetSelectionForWordAtPos', {
+      pagePtr: this.#pagePtr,
+      textPtr: this.#textPtr,
+      start: point,
+      end: point
+    })
+    this._selection = selection
+    this.updateSelection()
+
+    if (this._selection && this.textContainer) {
+      this.showTextPopup()
+    }
+  }
+
   handleMouseUp() {
     if (this.selecting && this._selection && markupTypeNotNull.includes(this.tool)) {
       const annotationData = {
@@ -362,6 +402,7 @@ export default class TextSelection {
     document.removeEventListener('touchmove', this.handleMouseMove)
     document.removeEventListener('mouseup', this.handleMouseUp)
     document.removeEventListener('touchend', this.handleMouseUp)
+    document.removeEventListener('dblclick', this.handleDobuleClick)
 
     this.eventBus._off('toolChanged', this.handleTool)
     this.eventBus._off('toolModeChanged', this.handleToolMode)

+ 17 - 0
packages/core/src/worker/compdfkit_worker.js

@@ -718,6 +718,23 @@ class CPDFWorker {
       }
     })
 
+    messageHandler.on('GetSelectionForWordAtPos', (data) => {
+      const { pagePtr, textPtr, start, end } = data
+      Rect = {}
+      Module._GetSelectionForWordAtPos(pagePtr, textPtr, start.x, start.y, end.x, end.y, 5, 5)
+      PDFRange = {}
+      Module._GetCharsRangeAtPos(pagePtr, textPtr, Rect.Left, Rect.Top, Rect.Right, Rect.Bottom, 5, 5)
+
+      const rawTextContent = Module._GetTextContent(textPtr, PDFRange.Location, PDFRange.Length)
+      const textContent = UTF8ToString(rawTextContent)
+      TextRectArray = []
+      Module._GetRects(pagePtr, textPtr, PDFRange.Location, PDFRange.Length)
+      return {
+        textContent,
+        textRects: TextRectArray
+      }
+    })
+
     messageHandler.on('XFDFExportAnnotations', (data) => {
       const { doc } = data
       const result = exportXFDFByStream(doc)