@@ -75,75 +75,96 @@ const onMouseUp = (e: MouseEvent) => {
7575 const selection = rangy . getSelection ( )
7676 const iconElement = document . querySelector < HTMLSpanElement > ( '#ate-icon' )
7777
78- if ( selection . toString ( ) . trim ( ) && iconElement ) {
78+ if ( ! iconElement || ! ( e . target instanceof Element ) ) {
79+ return
80+ }
81+
82+ /**
83+ * 点击翻译按钮
84+ */
85+ if ( e . target === iconElement ) {
86+ e . stopPropagation ( )
87+ e . preventDefault ( )
88+
89+ logger . debug ( {
90+ msg : 'lastSelection' ,
91+ lastSelection,
92+ } )
93+
94+ if ( lastSelection ) {
95+ const id = uuid ( )
96+ const anchorId = `ate_anchor_${ id } `
97+
98+ if ( lastSelection . selection . anchorNode ?. parentElement ) {
99+ lastSelection . selection . anchorNode ?. parentElement . classList . add (
100+ anchorId ,
101+ )
102+ }
103+
104+ highlightSelection ( lastSelection . selection )
105+
106+ addTranslateJob ( {
107+ anchorId,
108+ id,
109+ text : lastSelection . text ,
110+ sourceLang : lastSelection . sourceLang ,
111+ } )
112+
113+ lastSelection ?. selection . removeAllRanges ( )
114+ lastSelection = undefined
115+ iconElement . classList . remove ( 'active' )
116+ }
117+
118+ return
119+ }
120+
121+ /**
122+ * 没有点击翻译按钮
123+ */
124+ if ( selection . toString ( ) . trim ( ) ) {
125+ /**
126+ * 选择了文字
127+ */
128+
79129 const appElement = document . querySelector < HTMLDivElement > ( '.ate_App' )
80130
81- if (
82- appElement &&
83- e . target instanceof Element &&
84- appElement . contains ( e . target )
85- ) {
131+ if ( appElement && appElement . contains ( e . target ) ) {
132+ // 焦点处在 App 内,让按钮消失,清空上一次的选择
133+ iconElement . classList . remove ( 'active' )
134+ lastSelection = undefined
135+
86136 return
87137 }
88138
89- // update last selection
90139 lastSelection = getTextSelection ( selection )
91140
92141 iconElement . style . top = e . pageY + 20 + 'px'
93142 iconElement . style . left = e . pageX + 'px'
94143 iconElement . classList . add ( 'active' )
95144 } else {
96- // @ts -ignore
97- if ( e . target ?. id !== 'ate-icon' ) {
98- lastSelection = undefined
99- }
100-
101- iconElement ?. classList . remove ( 'active' )
145+ /**
146+ * 没有选择文字,有以下情况
147+ *
148+ * 1. 点击鼠标
149+ * 2. 空选择
150+ * 3. 拖拽
151+ */
152+
153+ lastSelection = undefined
154+
155+ // 只要没有选中文字都让按钮消失
156+ iconElement . classList . remove ( 'active' )
102157 }
103158}
104159
105- const onClickTranslate = ( job : TranslateJob ) => {
160+ const addTranslateJob = ( job : TranslateJob ) => {
106161 initApp ( )
107162 translationStack . push ( job )
108163}
109164
110165const attachListeners = ( ) => {
111166 document . addEventListener ( 'mouseup' , onMouseUp , false )
112167
113- document
114- . querySelector < HTMLSpanElement > ( '#ate-icon' )
115- ?. addEventListener ( 'click' , function ( ) {
116- logger . debug ( {
117- msg : 'lastSelection' ,
118- lastSelection,
119- } )
120-
121- if ( lastSelection ) {
122- const id = uuid ( )
123- const anchorId = `ate_anchor_${ id } `
124-
125- if ( lastSelection . selection . anchorNode ?. parentElement ) {
126- lastSelection . selection . anchorNode ?. parentElement . classList . add (
127- anchorId ,
128- )
129- }
130-
131- highlightSelection ( lastSelection . selection )
132-
133- onClickTranslate ( {
134- anchorId,
135- id,
136- text : lastSelection . text ,
137- sourceLang : lastSelection . sourceLang ,
138- } )
139-
140- setTimeout ( ( ) => {
141- lastSelection ?. selection . removeAllRanges ( )
142- this . classList . remove ( 'active' )
143- } , 0 )
144- }
145- } )
146-
147168 server . on ( 'connect' , ( client ) => {
148169 client . on ( 'open_extension' , ( ) => {
149170 initApp ( )
0 commit comments