@@ -13,28 +13,34 @@ let _candraw = false;
1313let _penSize ;
1414let _penColor ;
1515let path ;
16- let counter = 0 ;
1716let lines = [ ] ;
1817
1918/**
20- * Handle document.mousedown event
19+ * Handle document.touchdown or document.pointerdown event
2120 */
2221function handleDocumentPointerdown ( e ) {
2322 path = null ;
2423 lines = [ ] ;
25- counter = 9 ;
2624 _candraw = true ;
2725}
2826
2927/**
30- * Handle document.mouseup event
28+ * Handle document.touchup or document.pointerup event
3129 *
3230 * @param {Event } e The DOM event to be handled
3331 */
32+ function handleDocumentKeyupChrome ( e ) {
33+ saveToStorage ( e . changedTouches [ 0 ] . clientX , e . changedTouches [ 0 ] . clientY ) ;
34+ }
35+
3436function handleDocumentPointerup ( e ) {
37+ saveToStorage ( e . clientX , e . clientY ) ;
38+ }
39+
40+ function saveToStorage ( x , y ) {
3541 _candraw = false ;
3642 let svg ;
37- if ( lines . length > 1 && ( svg = findSVGAtPoint ( e . clientX , e . clientY ) ) ) {
43+ if ( lines . length > 1 && ( svg = findSVGAtPoint ( x , y ) ) ) {
3844 let { documentId, pageNumber } = getMetadata ( svg ) ;
3945
4046 PDFJSAnnotate . getStoreAdapter ( ) . addAnnotation ( documentId , pageNumber , {
@@ -60,12 +66,14 @@ function handleDocumentPointerup(e) {
6066 */
6167function handleDocumentPointermove ( e ) {
6268 if ( _candraw ) {
63- console . log ( counter ) ;
64- counter ++ ;
6569 savePoint ( e . clientX , e . clientY ) ;
6670 }
6771}
6872
73+ function handleDocumentPointermoveChrome ( e ) {
74+ savePoint ( e . changedTouches [ 0 ] . clientX , e . changedTouches [ 0 ] . clientY ) ;
75+ }
76+
6977/**
7078 * Handle document.keyup event
7179 *
@@ -135,9 +143,19 @@ export function enablePen() {
135143 if ( _enabled ) { return ; }
136144
137145 _enabled = true ;
138- document . addEventListener ( 'pointerdown' , handleDocumentPointerdown ) ;
139- document . addEventListener ( 'pointermove' , handleDocumentPointermove ) ;
140- document . addEventListener ( 'pointerup' , handleDocumentPointerup ) ;
146+ // Chrome and Firefox has different behaviors with how pen works, so we need different events.
147+ if ( navigator . userAgent . indexOf ( "Chrome" ) !== - 1 ) {
148+ document . addEventListener ( 'touchstart' , handleDocumentPointerdown ) ;
149+ document . addEventListener ( 'touchmove' , handleDocumentPointermoveChrome ) ;
150+ document . addEventListener ( 'touchend' , handleDocumentKeyupChrome ) ;
151+ document . addEventListener ( 'mousedown' , handleDocumentPointerdown ) ;
152+ document . addEventListener ( 'mousemove' , handleDocumentPointermove ) ;
153+ document . addEventListener ( 'mouseup' , handleDocumentPointerup ) ;
154+ } else {
155+ document . addEventListener ( 'pointerdown' , handleDocumentPointerdown ) ;
156+ document . addEventListener ( 'pointermove' , handleDocumentPointermove ) ;
157+ document . addEventListener ( 'pointerup' , handleDocumentPointerup ) ;
158+ }
141159 document . addEventListener ( 'keyup' , handleDocumentKeyup ) ;
142160 disableUserSelect ( ) ;
143161}
@@ -149,7 +167,18 @@ export function disablePen() {
149167 if ( ! _enabled ) { return ; }
150168
151169 _enabled = false ;
152- document . removeEventListener ( 'pointerdown' , handleDocumentPointerdown ) ;
170+ if ( navigator . userAgent . indexOf ( "Chrome" ) !== - 1 ) {
171+ document . removeEventListener ( 'touchstart' , handleDocumentPointerdown ) ;
172+ document . removeEventListener ( 'touchmove' , handleDocumentPointermoveChrome ) ;
173+ document . removeEventListener ( 'touchend' , handleDocumentKeyupChrome ) ;
174+ document . removeEventListener ( 'mousedown' , handleDocumentPointerdown ) ;
175+ document . removeEventListener ( 'mousemove' , handleDocumentPointermove ) ;
176+ document . removeEventListener ( 'mouseup' , handleDocumentPointerup ) ;
177+ } else {
178+ document . removeEventListener ( 'pointerdown' , handleDocumentPointerdown ) ;
179+ document . removeEventListener ( 'pointermove' , handleDocumentPointermove ) ;
180+ document . removeEventListener ( 'pointerup' , handleDocumentPointerup ) ;
181+ }
153182 document . removeEventListener ( 'keyup' , handleDocumentKeyup ) ;
154183 enableUserSelect ( ) ;
155184}
0 commit comments