Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.

Commit a9655c6

Browse files
committed
refactor: should not keep parentElement
1 parent c370fa8 commit a9655c6

File tree

6 files changed

+39
-28
lines changed

6 files changed

+39
-28
lines changed

src/pages/Content/common/translation-stack.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { v4 as uuid } from 'uuid'
2-
3-
import { TextSelection, TranslateJob } from './types'
1+
import { TranslateJob } from './types'
42

53
class TranslationStack {
64
stack: Array<TranslateJob> = []
@@ -19,17 +17,11 @@ class TranslationStack {
1917
this.onPush = undefined
2018
}
2119

22-
push(job: TextSelection) {
20+
push(job: TranslateJob) {
2321
if (this.onPush) {
24-
this.onPush({
25-
...job,
26-
id: uuid(),
27-
})
22+
this.onPush(job)
2823
} else {
29-
this.stack.push({
30-
...job,
31-
id: uuid(),
32-
})
24+
this.stack.push(job)
3325
}
3426
}
3527
}

src/pages/Content/common/types.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { SupportLanguages } from '../../../common/types'
22

33
export interface TextSelection {
4+
text: string
5+
selection: RangySelection
46
parentElement?: HTMLElement
57
sourceLang?: SupportLanguages
6-
text: string
8+
id?: string
9+
anchorId?: string
710
}
811

9-
export interface TranslateJob extends TextSelection {
12+
export interface TranslateJob {
1013
id: string
14+
anchorId: string
15+
text: string
16+
sourceLang?: SupportLanguages
1117
}

src/pages/Content/components/TranslationItem.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@ const TranslationItem: React.FC<{
2626
}, [job.text])
2727

2828
const findOriginal = () => {
29-
const { parentElement } = job
29+
const { anchorId } = job
30+
const parentElement = document.querySelector(`.${anchorId}`)
3031

3132
if (!parentElement) {
3233
enqueueSnackbar('已找不到原文', { variant: 'info' })
3334
return
3435
}
3536

36-
window.scrollTo(0, parentElement.offsetTop - 20)
37+
if (parentElement instanceof HTMLElement) {
38+
window.scrollTo(0, parentElement.offsetTop - 20)
39+
}
3740
}
3841

3942
useEffect(() => {

src/pages/Content/index.tsx

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react'
22
import { render } from 'react-dom'
3+
import { v4 as uuid } from 'uuid'
34
// @ts-ignore
45
import smoothScrollPolyfill from 'smoothscroll-polyfill'
56
import * as rangy from 'rangy'
@@ -11,14 +12,14 @@ import logger from '../../common/logger'
1112
import { SupportLanguages } from '../../common/types'
1213
import server from './common/server'
1314
import translationStack from './common/translation-stack'
14-
import { TextSelection } from './common/types'
15+
import { TextSelection, TranslateJob } from './common/types'
1516
import { getFirstRange } from './common/utils'
1617
import App from './components/App'
1718
import './styles/index.scss'
1819
import { TranslateJobsProvider } from './providers/translate-jobs'
1920

2021
let isAppAttached = false
21-
let lastSelection: (TextSelection & { selection: RangySelection }) | undefined
22+
let lastSelection: TextSelection | undefined
2223
let highlighter: any
2324

2425
const 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

107110
const 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
}

src/pages/Content/styles/components/App.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
position: absolute;
33
width: 450px;
44
height: 600px;
5-
z-index: 99998;
5+
z-index: 1;
66
@apply bg-white shadow rounded-lg overflow-hidden flex flex-col;
77

88
&--inactive {

src/pages/Content/styles/index.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
margin: 0;
1414
padding: 0;
1515
font-size: 16px;
16-
z-index: 99997;
16+
z-index: 999999;
1717

1818
@apply font-sans leading-normal;
1919
}
@@ -32,7 +32,7 @@
3232

3333
#ate-icon {
3434
display: none;
35-
z-index: 99999;
35+
z-index: 2;
3636
position: absolute;
3737
top: 0;
3838
left: 0;

0 commit comments

Comments
 (0)