11import React from 'react'
22import { render } from 'react-dom'
3+ import { v4 as uuid } from 'uuid'
34// @ts -ignore
45import smoothScrollPolyfill from 'smoothscroll-polyfill'
56import * as rangy from 'rangy'
@@ -11,14 +12,14 @@ import logger from '../../common/logger'
1112import { SupportLanguages } from '../../common/types'
1213import server from './common/server'
1314import translationStack from './common/translation-stack'
14- import { TextSelection } from './common/types'
15+ import { TextSelection , TranslateJob } from './common/types'
1516import { getFirstRange } from './common/utils'
1617import App from './components/App'
1718import './styles/index.scss'
1819import { TranslateJobsProvider } from './providers/translate-jobs'
1920
2021let isAppAttached = false
21- let lastSelection : ( TextSelection & { selection : RangySelection } ) | undefined
22+ let lastSelection : TextSelection | undefined
2223let highlighter : any
2324
2425const main = async ( ) => {
@@ -85,7 +86,9 @@ const onMouseUp = (e: MouseEvent) => {
8586 return
8687 }
8788
89+ // update last selection
8890 lastSelection = getTextSelection ( selection )
91+
8992 iconElement . style . top = e . pageY + 20 + 'px'
9093 iconElement . style . left = e . pageX + 'px'
9194 iconElement . classList . add ( 'active' )
@@ -99,9 +102,9 @@ const onMouseUp = (e: MouseEvent) => {
99102 }
100103}
101104
102- const onClickTranslate = ( selection : TextSelection ) => {
105+ const onClickTranslate = ( job : TranslateJob ) => {
103106 initApp ( )
104- translationStack . push ( selection )
107+ translationStack . push ( job )
105108}
106109
107110const attachListeners = ( ) => {
@@ -116,11 +119,21 @@ const attachListeners = () => {
116119 } )
117120
118121 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+
119131 highlightSelection ( lastSelection . selection )
120132
121133 onClickTranslate ( {
134+ anchorId,
135+ id,
122136 text : lastSelection . text ,
123- parentElement : lastSelection . parentElement ,
124137 sourceLang : lastSelection . sourceLang ,
125138 } )
126139
@@ -148,15 +161,12 @@ const highlightSelection = (selection: RangySelection) => {
148161 highlighter . highlightSelection ( 'ate-highlight' )
149162}
150163
151- const getTextSelection = (
152- selection : RangySelection ,
153- ) : TextSelection & { selection : RangySelection } => {
164+ const getTextSelection = ( selection : RangySelection ) : TextSelection => {
154165 const text = selection . toString ( ) . trim ( )
155166 // const html = selection.toHtml().trim()
156167
157168 return {
158169 selection,
159- parentElement : selection . anchorNode ?. parentElement ?? undefined ,
160170 sourceLang : getSourceLang ( ) ,
161171 text,
162172 }
0 commit comments