Skip to content

Commit

Permalink
fix: firefox input selection
Browse files Browse the repository at this point in the history
  • Loading branch information
noe132 committed Feb 3, 2022
1 parent 937ad77 commit 3704bf1
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 19 deletions.
5 changes: 3 additions & 2 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { translateService } from './service/translate'
import { viewService } from './service/view'
import { registerMenuCommand } from './util/gmapi'
import { scrollBarWidthService } from './service/scrollBarWidth'
import { getSelectionText } from './util/getSelectionText'

export default defineComponent({
name: 'IcibaAppRoot',
Expand Down Expand Up @@ -58,12 +59,12 @@ export default defineComponent({
bus.emit({
type: EVENTS.HOTKEY_SHOW,
mouseEvent: lastMouseMoveEvent,
word: window.getSelection()?.toString().trim() ?? undefined,
word: getSelectionText(),
})
}

const handleTranslateHotkeyPress = (keys: Array<string>, stop: () => void) => {
const word = window.getSelection()?.toString().trim() ?? ''
const word = getSelectionText()

if (!lastMouseUpEvent) {
return
Expand Down
2 changes: 1 addition & 1 deletion src/service/globalBus/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface OpenGoogleDictModalAction {
export interface HotKeyShowAction {
type: EVENTS.HOTKEY_SHOW
mouseEvent: MouseEvent
word?: string
word: string
}

export interface HotKeyTranslateAction {
Expand Down
9 changes: 7 additions & 2 deletions src/service/translate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,15 @@ const clearActiveProvider = () => {

const removeSelection = () => {
const selection = window.getSelection()
if (!selection) {
if (selection?.toString()) {
selection.removeAllRanges()
return
}
selection.removeAllRanges()

const active = document.activeElement
if (active instanceof HTMLInputElement || active instanceof HTMLTextAreaElement) {
active.setSelectionRange(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER)
}
}

export const translateService = {
Expand Down
18 changes: 18 additions & 0 deletions src/util/getSelectionText.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const getSelectionText = () => {
const selection = window.getSelection()
if (selection && String(selection)) {
return selection.toString().trim()
}

const active = document.activeElement
if (active instanceof HTMLInputElement || active instanceof HTMLTextAreaElement) {
const start = active.selectionStart
const end = active.selectionEnd
if (typeof start === 'number' && typeof end === 'number') {
const s = active.value.substring(start, end)
return s.trim()
}
}

return ''
}
12 changes: 2 additions & 10 deletions src/view/IcibaCircle/IcibaCircle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from 'vue'

import calcMouseEventPosition from '~/util/calcMouseEventPosition'
import { getSelectionText } from '~/util/getSelectionText'

import { bus, EVENTS } from '~/service/globalBus'
import { zIndexService, Z_INDEX_KEY } from '~/service/zIndex'
Expand Down Expand Up @@ -58,15 +59,6 @@ export default defineComponent({
}
}

const getSelectionString = () => {
const selection = window.getSelection()
if (!selection || !String(selection)) {
return ''
}

return selection.toString().trim()
}

const handleSelfMouseUp = (event: MouseEvent) => {
// have to wait handleContextmenu trigger
setTimeout(() => {
Expand Down Expand Up @@ -125,7 +117,7 @@ export default defineComponent({
// cleared on next frame
await new Promise((rs) => requestAnimationFrame(rs))

const selectionString = getSelectionString()
const selectionString = getSelectionText()
state.currentWord = selectionString
if (!selectionString) {
hide()
Expand Down
2 changes: 1 addition & 1 deletion src/view/IcibaMain/IcibaMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export default defineComponent({
/** 热键显示 */
onHotKeyShowUp: (action: HotKeyShowAction) => {
setPosition(action.mouseEvent)
state.inputText = action.word ?? ''
state.inputText = action.word
translateService.clearActiveProvider()
showIcibaMain(action.mouseEvent, store.core.hotkeyIcibaMainInputAutoFocus)
},
Expand Down
6 changes: 3 additions & 3 deletions src/view/SettingPage/subpage/hotKey/hotKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { providers } from '~/provider'

export default defineComponent({
name: 'HotKeySetting',
props: {
active: Boolean,
},
components: {
Foldable,
},
props: {
active: Boolean,
},
setup: () => ({
core: store.core,
providers,
Expand Down

0 comments on commit 3704bf1

Please sign in to comment.