1
1
import React , { cloneElement } from 'react'
2
2
import ReactDom from 'react-dom'
3
- import throttle from 'utils/throttle'
4
3
import { getPageFromRange } from 'components/pdf-highlighter/utils/utils'
5
4
import { groupRectsByPage } from 'components/pdf-highlighter/utils/rects'
6
5
import withAppContext from 'store/withAppContext'
@@ -17,33 +16,20 @@ class PDFHighlighter extends React.Component {
17
16
position : {
18
17
left : null ,
19
18
top : null ,
19
+ height : null ,
20
20
} ,
21
21
annotationId : null ,
22
22
selectedText : '' ,
23
23
}
24
24
25
25
componentDidMount ( ) {
26
- document . addEventListener ( 'selectionchange' , this . onSelectionChange )
27
- document . addEventListener ( 'mouseup' , this . onAfterSelection . bind ( this ) )
26
+ window . addEventListener ( 'mouseup' , this . onAfterSelection . bind ( this ) )
28
27
}
29
28
30
29
componentWillUnmount ( ) {
31
- document . removeEventListener ( 'mouseup' , this . onAfterSelection )
30
+ window . removeEventListener ( 'mouseup' , this . onAfterSelection )
32
31
}
33
32
34
- onSelectionChange = ( ) => {
35
- const selection = window . getSelection ( )
36
-
37
- // no text was selected
38
- // https://developer.mozilla.org/en-US/docs/Web/API/Selection/isCollapsed
39
- if ( selection . isCollapsed ) {
40
- this . setState ( {
41
- isVisible : false ,
42
- } )
43
- }
44
- }
45
-
46
- @throttle ( 250 )
47
33
onAfterSelection ( ) {
48
34
const {
49
35
state : {
@@ -97,32 +83,11 @@ class PDFHighlighter extends React.Component {
97
83
} )
98
84
}
99
85
100
- onUpdateContextMenu = ( {
101
- isVisible,
102
- left,
103
- top,
104
- annotationId,
105
- contextRoot,
106
- width,
107
- height,
108
- } ) => {
109
- if ( ! isVisible ) {
110
- this . setState ( { isVisible } )
111
- return
112
- }
113
-
114
- const pdfRect = contextRoot . getBoundingClientRect ( )
115
-
116
- this . setState ( {
117
- isVisible,
118
- position : {
119
- left : `${ ( ( left + width / 2 ) * 100 ) / pdfRect . width } %` ,
120
- top : `${ ( top * 100 ) / pdfRect . height } %` ,
121
- height : `${ height } px` ,
122
- } ,
123
- annotationId,
124
- contextRoot,
125
- } )
86
+ onUpdateContextMenu = partialUpdate => {
87
+ this . setState ( state => ( {
88
+ ...state ,
89
+ ...partialUpdate ,
90
+ } ) )
126
91
}
127
92
128
93
render ( ) {
@@ -151,14 +116,18 @@ class PDFHighlighter extends React.Component {
151
116
height = { height }
152
117
selectedText = { selectedText }
153
118
annotationId = { annotationId }
119
+ updateContextMenu = { this . onUpdateContextMenu }
154
120
/> ,
155
121
contextRoot
156
122
)
157
123
158
124
return (
159
125
< >
160
126
{ contextRoot && contextMenu }
161
- < Highlights updateContextMenu = { this . onUpdateContextMenu } />
127
+ < Highlights
128
+ updateContextMenu = { this . onUpdateContextMenu }
129
+ currentAnnotationId = { annotationId }
130
+ />
162
131
{ children }
163
132
</ >
164
133
)
0 commit comments