1+ function getSelectedTextDirection ( selection ) {
2+ const range = document . createRange ( ) ;
3+ range . setStart ( selection . anchorNode , selection . anchorOffset ) ;
4+ range . setEnd ( selection . focusNode , selection . focusOffset ) ;
5+ return range . collapsed ? 'backward' : 'forward' ;
6+ }
7+ function getTextSelectionDataSet ( ) {
8+ let ws = window . getSelection ( ) ;
9+ let data = {
10+ text : '' ,
11+ rect : null ,
12+ rectAnnotations : [ ]
13+ }
14+ if ( ws === null ) {
15+ return data ;
16+ }
17+ data = {
18+ text : ws ?. toString ( ) ,
19+ rect : ws ?. getRangeAt ( 0 ) . getBoundingClientRect ( ) ,
20+ rectAnnotations : [ ] ,
21+ }
22+ if ( data . text === "" ) {
23+ return data ;
24+ }
25+ let allText = ws . toString ( ) ;
26+ let direction = getSelectedTextDirection ( ws ) ;
27+ let ranges = [ ] ;
28+ let spanText , spanContainer , selectedText , offset ;
29+
30+ if ( direction === "backward" ) {
31+ spanText = ws . focusNode . parentNode ;
32+ spanContainer = spanText . parentNode ;
33+ offset = ws . focusOffset ;
34+ } else {
35+ spanText = ws . baseNode . parentNode ;
36+ spanContainer = spanText . parentNode ;
37+ offset = ws . anchorOffset ;
38+ }
39+
40+ let range = document . createRange ( ) ;
41+ range . setStart ( spanText . firstChild , offset ) ;
42+ selectedText = spanText . firstChild . data . toString ( ) . substring ( offset ) ;
43+ if ( allText . length <= selectedText . length ) {
44+ range . setEnd ( spanText . firstChild , offset + allText . length ) ;
45+ ranges . push ( range ) ;
46+ allText = "" ;
47+ } else {
48+ range . setEnd ( spanText . firstChild , offset + selectedText . length ) ;
49+ ranges . push ( range ) ;
50+ allText = allText . substring ( selectedText . length ) ;
51+ }
52+
53+ while ( allText !== "" ) {
54+ while ( allText . charAt ( 0 ) === "\n" ) { allText = allText . substring ( 1 ) } ;
55+ allText = allText . charAt ( 0 ) === "\n" ? allText . substring ( 1 ) : allText ;
56+ while ( spanText . nextSibling !== null ) {
57+ spanText = spanText . nextSibling ;
58+ }
59+ let range = document . createRange ( ) ;
60+ selectedText = spanText . firstChild . data . toString ( ) ;
61+ range . setStart ( spanText . firstChild , 0 ) ;
62+ if ( allText . length <= selectedText . length ) {
63+ range . setEnd ( spanText . firstChild , allText . length ) ;
64+ ranges . push ( range ) ;
65+ allText = "" ;
66+ } else {
67+ range . setEnd ( spanText . firstChild , selectedText . length ) ;
68+ ranges . push ( range ) ;
69+ allText = allText . substring ( selectedText . length ) ;
70+ }
71+ }
72+
73+ data . rectAnnotations = ranges . map ( el => {
74+ let dim = el . getBoundingClientRect ( ) ;
75+ return {
76+ x : dim . x - ( data . rect ? data . rect . x : 0 ) ,
77+ y : dim . y - ( data . rect ? data . rect . y : 0 ) ,
78+ width : dim . width ,
79+ height : dim . height
80+ }
81+ } ) ;
82+
83+ window . getSelection ( ) ?. removeAllRanges ( ) ;
84+ console . log ( "TEXT_SELECTION_RANGES" , data ) ;
85+ }
0 commit comments