Browse Source

fix: 修复页面编辑后markup报错bug

wzl 3 weeks ago
parent
commit
9b6dc298da

+ 7 - 9
packages/core/src/annotation/layer.js

@@ -46,7 +46,7 @@ class ComPDFAnnotationLayer {
     $t
   }) {
     this.documentViewer = documentViewer
-    this._cancelled = false;
+    this.annotationStore = annotationStore
     this.pageViewer = pageViewer
     this.pageDiv = pageDiv;
     this.pdfPage = pdfPage;
@@ -58,6 +58,10 @@ class ComPDFAnnotationLayer {
     this.scale = scale
     this.eventBus = eventBus
     this.page = pageIndex
+    this.annotationStore.$t = $t
+    this.enableReply = enableReply
+
+    this._cancelled = false
     this.tool = pageViewer.tool || ''
     this.color = pageViewer.color || ''
     this.annotateManager = null
@@ -67,20 +71,14 @@ class ComPDFAnnotationLayer {
     this.toolMode = pageViewer.toolMode || ''
     this.managerArray = ['freetextManager', 'textManager', 'textFieldManager', 'checkBoxManager', 'radioButtonManager', 'pushButtonManager', 'listBoxManager', 'comboBoxManager', 'annotateManager', 'linkManager', 'signFieldManager', 'redactionManager']
     this.ratio = window.devicePixelRatio || 1
-
-    this.annotationStore = annotationStore
+    this.markupType = ['highlight', 'underline', 'squiggly', 'strikeout']
+    this.pageViewer.canvas && (this.ctx = this.pageViewer.canvas.getContext('2d'))
 
     this.onHandleTool = this.handleTool.bind(this)
     this.onHandleToolMode = this.handleToolMode.bind(this)
     this.onHandlePropertyPanelChanged = this.handlePropertyPanelChanged.bind(this)
     this.onHandleAddSign = this.addSign.bind(this)
     this.onRedrawMarkups = this.redrawMarkups.bind(this)
-
-    this.annotationStore.$t = $t
-
-    this.enableReply = enableReply
-    this.markupType = ['highlight', 'underline', 'squiggly', 'strikeout']
-    this.ctx = this.pageViewer.canvas.getContext('2d')
   }
 
   // 处理注释工具切换

+ 1 - 1
packages/core/src/editor/text_editor.js

@@ -1180,7 +1180,7 @@ export class TextEditor {
     this.eventBus.dispatch('redrawMarkups', {
       pageIndex: this.pageViewer.pageIndex,
       afterContentEdit: true,
-      rect: this.getEntireArea(this.oldRect, this.rect),
+      rect: this.oldRect ? this.getEntireArea(this.oldRect, this.rect) : this.rect,
       pagePtr: this.pagePtr
     })
   }

+ 6 - 4
packages/core/src/markup/text_annotation.js

@@ -34,7 +34,7 @@ class TextAnnotation extends BaseAnnotation {
     this.markupContainer = null
     this.outerLineContainer = null
     this.ratio = window.devicePixelRatio || 1
-    this.ctx = this.pageViewer.canvas.getContext('2d', { willReadFrequently: true })
+    this.pageViewer.canvas && (this.ctx = this.pageViewer.canvas.getContext('2d', { willReadFrequently: true }))
     this.lineWidth = 2 * this.scale * this.ratio
 
     this.onHandleClick = this.handleClick.bind(this)
@@ -80,9 +80,11 @@ class TextAnnotation extends BaseAnnotation {
     }
     this.positionArray = positionArray
 
-    this.ctx.globalCompositeOperation = 'multiply'
-    this.renderCanvansRect(positionArray)
-    this.markupContainer.addEventListener('click', this.onHandleClick)
+    if (this.ctx) {
+      this.ctx.globalCompositeOperation = 'multiply'
+      this.renderCanvansRect(positionArray)
+      this.markupContainer.addEventListener('click', this.onHandleClick)
+    }
     
     this.outerLineContainer = document.createElement('div')
     this.outerLineContainer.className = 'outline-container'