4
4
// 3. expand range for char -> string
5
5
// 4. range to text
6
6
7
+ import { detect } from "tesseract.js" ;
7
8
import * as util from "/src/util" ;
8
9
import { debounce } from "lodash" ;
9
10
@@ -13,11 +14,14 @@ var _win;
13
14
var _isIframe = false ;
14
15
var styleElement ;
15
16
const PARENT_TAGS_TO_EXCLUDE = [ "STYLE" , "SCRIPT" , "TITLE" ] ;
17
+ var setting = { } ;
18
+ var mouseTarget = null ;
19
+
20
+ export function enableMouseoverTextEvent ( _window = window , settingPointer ) {
21
+ var textDetectTime = setting ?. [ "tooltipEventInterval" ] || 0.7 ;
22
+
23
+ setting = settingPointer ;
16
24
17
- export function enableMouseoverTextEvent (
18
- _window = window ,
19
- textDetectTime = 0.7
20
- ) {
21
25
_win = _window ;
22
26
textDetectTime = Number ( textDetectTime ) * 1000 ;
23
27
const triggerMouseoverTextWithDelay = debounce ( async ( ) => {
@@ -33,6 +37,29 @@ export function enableMouseoverTextEvent(
33
37
} ) ;
34
38
}
35
39
40
+ function getMouseoverType ( ) {
41
+ //if swap key pressed, swap detect type
42
+ //if mouse target is special web block, handle as block
43
+ var detectType = setting [ "mouseoverTextType" ] ;
44
+ // detectType = keyDownList[setting["keyDownMouseoverTextSwap"]]
45
+ // ? detectType == "word"
46
+ // ? "sentence"
47
+ // : "word"
48
+ // : detectType;
49
+
50
+ detectType = checkMouseTargetIsSpecialWebBlock ( ) ? "container" : detectType ;
51
+ return detectType ;
52
+ }
53
+
54
+ function checkMouseTargetIsSpecialWebBlock ( ) {
55
+ // if mouse targeted web element contain particular class name, return true
56
+ //mousetooltip ocr block
57
+ var classList = mouseTarget ?. classList ;
58
+ return [ "ocr_text_div" , "textFitted" ] . some ( ( className ) =>
59
+ classList ?. contains ( className )
60
+ ) ;
61
+ }
62
+
36
63
function updateMouseoverXY ( e ) {
37
64
updateEbookWindowPos ( e ) ;
38
65
updateWindowPos ( e ) ;
@@ -44,6 +71,7 @@ function updateEbookWindowPos(e) {
44
71
_isIframe = true ;
45
72
clientX = e . iframeX ;
46
73
clientY = e . iframeY ;
74
+ mouseTarget = e . target ;
47
75
}
48
76
}
49
77
function updateWindowPos ( e ) {
@@ -52,6 +80,7 @@ function updateWindowPos(e) {
52
80
}
53
81
clientX = e . clientX ;
54
82
clientY = e . clientY ;
83
+ mouseTarget = e . target ;
55
84
}
56
85
57
86
export const triggerMouseoverText = ( mouseoverText ) => {
@@ -64,39 +93,44 @@ export const triggerMouseoverText = (mouseoverText) => {
64
93
} ;
65
94
66
95
export async function getMouseoverText ( x , y ) {
96
+ var mouseoverType = getMouseoverType ( ) ;
97
+
67
98
//get google doc select
68
99
if ( util . isGoogleDoc ( ) ) {
69
- return await getGoogleDocText ( x , y ) ;
100
+ return await getGoogleDocText ( x , y , mouseoverType ) ;
70
101
}
71
102
72
103
//get range
73
104
var range = getPointedRange ( x , y ) ;
74
105
75
106
//get text from range
76
- var mouseoverText = await getTextFromRange ( range , x , y ) ;
107
+ var mouseoverText = await getTextFromRange ( range , x , y , false , mouseoverType ) ;
77
108
// if fail detect using expand range use seg range
78
109
if (
79
110
! isFirefox ( ) &&
80
- ! mouseoverText [ "word" ] &&
81
- ! mouseoverText [ "sentence" ] &&
82
- mouseoverText [ "container" ]
111
+ ( mouseoverType === "word" || mouseoverType === "sentence" ) &&
112
+ ! mouseoverText [ "mouseoverText" ]
83
113
) {
84
114
return await getTextFromRange ( range , x , y , true ) ;
85
115
}
86
116
87
117
return mouseoverText ;
88
118
}
89
119
90
- async function getTextFromRange ( range , x , y , useSegmentation = false ) {
91
- var output = { } ;
92
- for ( const detectType of [ "word" , "sentence" , "container" ] ) {
93
- output [ detectType ] = "" ;
94
- var wordRange = expandRange ( range , detectType , useSegmentation , x , y ) ;
95
- if ( checkXYInElement ( wordRange , clientX , clientY ) ) {
96
- output [ detectType ] = util . extractTextFromRange ( wordRange ) ;
97
- output [ detectType + "_range" ] = wordRange ;
98
- }
120
+ async function getTextFromRange (
121
+ range ,
122
+ x ,
123
+ y ,
124
+ useSegmentation = false ,
125
+ mouseoverType
126
+ ) {
127
+ var output = { mouseoverText : "" , mouseoverRange : range } ;
128
+ var wordRange = expandRange ( range , mouseoverType , useSegmentation , x , y ) ;
129
+ if ( checkXYInElement ( wordRange , clientX , clientY ) ) {
130
+ output [ "mouseoverText" ] = util . extractTextFromRange ( wordRange ) ;
131
+ output [ "mouseoverRange" ] = wordRange ;
99
132
}
133
+ console . log ( output ) ;
100
134
return output ;
101
135
}
102
136
@@ -108,7 +142,7 @@ function expandRange(range, type, useSegmentation, x, y) {
108
142
if ( type == "container" ) {
109
143
// get whole text paragraph
110
144
range = getContainerRange ( range ) ;
111
- } else if ( ! range . expand || useSegmentation ) {
145
+ } else if ( isFirefox ( ) || useSegmentation ) {
112
146
// for firefox, use segmentation to extract word
113
147
range = expandRangeWithSeg ( range , type , x , y ) ;
114
148
} else {
@@ -297,11 +331,7 @@ function expandRangeWithSeg(rangeOri, type = "word", x, y) {
297
331
// var wholeText2 = getNodeText(textNode);
298
332
var wordSliceInfo = getWordSegmentInfo ( wholeText , type ) ;
299
333
// get all word range by segment
300
- const wordSliceRanges = createWordRanges ( wordSliceInfo , textNode ) ;
301
- // get pointed pos range
302
- var currentWordNode = wordSliceRanges . find ( ( range ) =>
303
- isPointInRange ( range , x , y )
304
- ) ;
334
+ const currentWordNode = findWordRange ( wordSliceInfo , textNode , x , y ) ;
305
335
return currentWordNode ;
306
336
}
307
337
@@ -326,9 +356,9 @@ function getWordSegmentInfo(text, type) {
326
356
return wordsMeta ;
327
357
}
328
358
329
- function createWordRanges ( wordSegInfo , textNode ) {
359
+ function findWordRange ( wordSegInfo , textNode , x , y ) {
330
360
var newLineCount = 0 ;
331
- return wordSegInfo
361
+ var wordSegInfoExtract = wordSegInfo
332
362
. map ( ( wordMeta ) => {
333
363
var word = wordMeta . segment ;
334
364
var index = wordMeta . index ;
@@ -348,23 +378,28 @@ function createWordRanges(wordSegInfo, textNode) {
348
378
newLine : newLine ,
349
379
} ;
350
380
} )
351
- . filter ( ( wordMeta ) => wordMeta . segment . length > 0 )
352
- . map ( ( wordMeta ) => {
353
- try {
354
- var wordRange = document . createRange ( ) ;
355
- var index = wordMeta . index + wordMeta . newLine ;
356
- var word = wordMeta . segment ;
357
- var wordLen = word . length ;
358
- const selectedNode1 = selectNode ( textNode , index ) ;
359
- const selectedNode2 = selectNode ( textNode , index + wordLen ) ;
360
-
361
- wordRange . setStart ( selectedNode1 . node , selectedNode1 . index ) ;
362
- wordRange . setEnd ( selectedNode2 . node , selectedNode2 . index ) ;
363
- } catch ( error ) {
364
- console . log ( error ) ;
381
+ . filter ( ( wordMeta ) => wordMeta . segment . trim ( ) . length > 0 ) ;
382
+
383
+ for ( const wordMeta of wordSegInfoExtract ) {
384
+ try {
385
+ var wordRange = document . createRange ( ) ;
386
+ var index = wordMeta . index + wordMeta . newLine ;
387
+ var word = wordMeta . segment ;
388
+ var wordLen = word . length ;
389
+ const selectedNode1 = selectNode ( textNode , index ) ;
390
+ const selectedNode2 = selectNode ( textNode , index + wordLen ) ;
391
+
392
+ wordRange . setStart ( selectedNode1 . node , selectedNode1 . index ) ;
393
+ wordRange . setEnd ( selectedNode2 . node , selectedNode2 . index ) ;
394
+
395
+ if ( isPointInRange ( wordRange , x , y ) ) {
396
+ return wordRange ;
365
397
}
366
- return wordRange ;
367
- } ) ;
398
+ } catch ( error ) {
399
+ console . log ( error ) ;
400
+ }
401
+ }
402
+ return null ;
368
403
}
369
404
370
405
function selectNode ( node , offset ) {
@@ -456,17 +491,18 @@ function getNextEle(ele) {
456
491
}
457
492
458
493
function isFirefox ( ) {
459
- return typeof InstallTrigger !== "undefined" ;
494
+ return ! document . createRange ( ) . expand ;
495
+ // return true;
460
496
}
461
497
462
498
//google doc hover =========================================================
463
499
// https://github.com/Amaimersion/google-docs-utils/issues/10
464
500
465
- async function getGoogleDocText ( x , y ) {
501
+ async function getGoogleDocText ( x , y , mouseoverType ) {
466
502
var textElement ;
467
503
var rect = getGoogleDocRect ( x , y ) ;
468
504
var { textElement, range } = getGoogleDocCaretRange ( rect , x , y ) ;
469
- var mouseoverText = await getTextFromRange ( range ) ;
505
+ var mouseoverText = await getTextFromRange ( range , x , y , false , mouseoverType ) ;
470
506
textElement ?. remove ( ) ;
471
507
return mouseoverText ;
472
508
}
0 commit comments