@@ -6,6 +6,7 @@ import { customElement } from 'lit/decorators.js';
66export class TextSelectionManager {
77 /** @type {brHighlightBar } */
88 highlightBar ;
9+ highlightEnabled ;
910 options = {
1011 // Current Translation plugin implementation does not have words, will limit to one BRlineElement for now
1112 maxProtectedWords : 200 ,
@@ -28,6 +29,7 @@ export class TextSelectionManager {
2829 this . selectionObserver = new SelectionObserver ( this . layer , this . _onSelectionChange ) ;
2930 this . options . maxProtectedWords = maxWords ? maxWords : 200 ;
3031 this . highlightBar = new brHighlightBar ( br ) ;
32+
3133 this . highlightBar . className = "textFragmentBar" ;
3234 }
3335
@@ -42,23 +44,32 @@ export class TextSelectionManager {
4244 this . br . refs . $br . find ( '.BRpagecontainer--hasSelection' ) . removeClass ( 'BRpagecontainer--hasSelection' ) ;
4345 $ ( window . getSelection ( ) . anchorNode ) . closest ( '.BRpagecontainer' ) . addClass ( 'BRpagecontainer--hasSelection' ) ;
4446 this . highlightBar . addHighlightButtonStyling ( ) ;
47+
4548 }
4649
4750 if ( selectEvent == 'focusChanged' ) {
4851 // hide the button as user changes their selection
4952 if ( this . mouseIsDown ) {
5053 this . highlightBar . hideHighlightStyle ( ) ;
54+
5155 } else {
52- this . highlightBar . addHighlightButtonStyling ( ) ;
56+ if ( window . getSelection ( ) . toString ( ) . length != 0 ) {
57+ this . highlightBar . addHighlightButtonStyling ( ) ;
58+
59+ }
5360 }
5461 }
62+
63+ if ( selectEvent == 'cleared' ) {
64+ this . highlightBar . hideHighlightStyle ( ) ;
65+ }
5566 } ) . attach ( ) ;
5667 }
5768
5869 // Need attach + detach methods to toggle w/ Translation plugin
5970 attach ( ) {
6071 this . selectionObserver . attach ( ) ;
61- if ( ! this . br . plugins . translate ) {
72+ if ( ! this . br . plugins . translate && this . highlightEnabled ) {
6273 document . body . append ( this . highlightBar ) ;
6374 }
6475 if ( this . br . protected ) {
@@ -86,6 +97,12 @@ export class TextSelectionManager {
8697 }
8798 }
8899
100+ addHighlightBar ( ) {
101+ if ( document . querySelector ( '.textFragmentBar' ) ) return ;
102+ if ( this . highlightEnabled && ! this . br . plugins . translate ) {
103+ document . body . append ( this . highlightBar ) ;
104+ }
105+ }
89106 /**
90107 * @param {'started' | 'cleared' | 'focusChanged' } type
91108 * @param {HTMLElement } target
@@ -146,6 +163,7 @@ export class TextSelectionManager {
146163 // blocking selection
147164 $ ( textLayer ) . on ( "mousedown.textSelectPluginHandler" , ( event ) => {
148165 this . mouseIsDown = true ;
166+ this . highlightBar . hideHighlightStyle ( ) ;
149167 if ( $ ( event . target ) . is ( this . selectionElement . join ( ", " ) ) ) {
150168 event . stopPropagation ( ) ;
151169 }
@@ -179,6 +197,7 @@ export class TextSelectionManager {
179197 if ( event . which != 1 ) return ;
180198 this . mouseIsDown = true ;
181199 event . stopPropagation ( ) ;
200+ this . highlightBar . hideHighlightStyle ( ) ;
182201 } ) ;
183202
184203 // Prevent page flip on click
@@ -462,14 +481,13 @@ class brHighlightBar extends LitElement {
462481 currentParams = currentParams . replace ( / ( t e x t = ) [ \w \W \d % ] + / , createParam ( currentSelection , textLayer ) ) ;
463482 } else {
464483 if ( this . br . options . urlMode === 'history' ) {
465- currentParams = `?${ createParam ( currentSelection , textLayer ) } `
484+ currentParams = `?${ createParam ( currentSelection , textLayer ) } ` ;
466485 } else {
467486 currentParams = `${ currentParams } &${ createParam ( currentSelection , textLayer ) } ` ;
468487 }
469488 }
470- console . log ( "this is after copying" , currentParams , currentUrl . pathname ) ;
471489 if ( this . br . options . urlMode === 'history' ) {
472- navigator . clipboard . writeText ( `${ currentUrl . origin } ${ currentUrl . pathname } ${ currentParams } ` )
490+ navigator . clipboard . writeText ( `${ currentUrl . origin } ${ currentUrl . pathname } ${ currentParams } ` ) ;
473491 } else {
474492 navigator . clipboard . writeText ( `${ currentUrl . origin } ${ currentUrl . pathname } ${ currentParams } ${ currentUrl ?. hash } ` ) ;
475493 }
0 commit comments