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

Commit 477de29

Browse files
committed
feat: add config for hover button
1 parent ccf87a5 commit 477de29

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

src/common/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface Config {
66
region: APIRegions
77
ocrSecretId?: string
88
ocrSecretKey?: string
9+
hoverButton?: boolean
910
}
1011

1112
export type SupportLanguageKeys = keyof typeof supportedLanguages

src/pages/Content/index.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import './common/polyfill'
2+
import cc from 'chrome-call'
23

34
import React from 'react'
45
import { render } from 'react-dom'
@@ -11,6 +12,7 @@ import './styles/index.scss'
1112

1213
import logger from '../../common/logger'
1314
import rangy from '../../common/rangy'
15+
import { Config } from '../../common/types'
1416
import server from './common/server'
1517
import translationStack from './common/translation-stack'
1618
import { TextSelection, TranslateJob } from './common/types'
@@ -52,10 +54,18 @@ const main = async () => {
5254
document.querySelector<HTMLBodyElement>('body')?.append(iconContainer)
5355
document.querySelector<HTMLBodyElement>('body')?.append(container)
5456

55-
attachListeners()
57+
cc(chrome.storage.sync, 'get').then((config: Partial<Config>) => {
58+
const hoverButton =
59+
config.hoverButton === undefined || config.hoverButton
5660

57-
// TODO: remove before deploying
58-
// initApp()
61+
if (hoverButton) {
62+
document.querySelector<HTMLBodyElement>('body')?.append(iconContainer)
63+
}
64+
65+
attachListeners({
66+
hoverButton,
67+
})
68+
})
5969
} catch (err) {
6070
logger.error({
6171
err,
@@ -156,8 +166,10 @@ const addTranslateJob = (job: TranslateJob) => {
156166
translationStack.push(job)
157167
}
158168

159-
const attachListeners = () => {
160-
document.addEventListener('mouseup', onMouseUp, false)
169+
const attachListeners = (config: { hoverButton: boolean }) => {
170+
if (config.hoverButton) {
171+
document.addEventListener('mouseup', onMouseUp, false)
172+
}
161173

162174
server.on('connect', (client) => {
163175
client.on('open_extension', () => {

src/pages/Options/Options.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import React, {
44
useEffect,
55
useState,
66
} from 'react'
7-
import tw, { css } from 'twin.macro'
7+
import tw, { css, styled } from 'twin.macro'
88
import { Global } from '@emotion/react'
99
import cc from 'chrome-call'
1010

@@ -13,12 +13,17 @@ import { supportedLanguages } from '../../common/constant'
1313
import { APIRegions, Config, SupportLanguageKeys } from '../../common/types'
1414
import OptionSection from './components/OptionSection'
1515

16+
const InputGroup = styled('div')`
17+
${tw`flex space-x-3 items-center`}
18+
`
19+
1620
const Options: React.FC = () => {
1721
const [targetLang, setTargetLang] = useState('ZH')
1822
const [token, setToken] = useState('')
1923
const [region, setRegion] = useState<APIRegions>('default')
2024
const [ocrSecretId, setOCRSecretId] = useState('')
2125
const [ocrSecretKey, setOCRSecretKey] = useState('')
26+
const [hoverButton, setHoverButton] = useState(true)
2227

2328
const onSubmit: FormEventHandler = (e) => {
2429
e.preventDefault()
@@ -29,6 +34,7 @@ const Options: React.FC = () => {
2934
region,
3035
ocrSecretId,
3136
ocrSecretKey,
37+
hoverButton,
3238
})
3339

3440
window.alert('保存成功')
@@ -63,6 +69,7 @@ const Options: React.FC = () => {
6369
if (config.ocrSecretId !== undefined) setOCRSecretId(config.ocrSecretId)
6470
if (config.ocrSecretKey !== undefined)
6571
setOCRSecretKey(config.ocrSecretKey)
72+
if (config.hoverButton !== undefined) setHoverButton(config.hoverButton)
6673
})
6774
}, [])
6875

@@ -165,6 +172,18 @@ const Options: React.FC = () => {
165172
</div>
166173
</OptionSection>
167174

175+
<OptionSection title={'其它设置'}>
176+
<InputGroup>
177+
<input
178+
type="checkbox"
179+
id="hover-button"
180+
checked={hoverButton}
181+
onChange={(e) => setHoverButton(e.target.checked)}
182+
/>
183+
<label htmlFor="hover-button">开启网页悬浮按钮</label>
184+
</InputGroup>
185+
</OptionSection>
186+
168187
<OptionSection title={'🔗 相关链接'}>
169188
<ul tw="space-y-2">
170189
<li>

0 commit comments

Comments
 (0)