Skip to content

Commit

Permalink
fix(popup): fix preloading selection on popup page
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed Jul 7, 2018
1 parent 6125ca8 commit 5912183
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
13 changes: 0 additions & 13 deletions src/background/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ message.addListener((data, sender: browser.runtime.MessageSender) => {
return playAudio(data as MsgAudioPlay)
case MsgType.FetchDictResult:
return fetchDictResult(data as MsgFetchDictResult)
case MsgType.PreloadSelection:
return preloadSelection()
case MsgType.GetClipboard:
return getClipboard()
case MsgType.RequestCSS:
Expand Down Expand Up @@ -122,17 +120,6 @@ function fetchDictResult (
})
}

function preloadSelection (): Promise<void> {
return browser.tabs.query({ active: true, currentWindow: true })
.then(tabs => {
if (tabs.length > 0 && tabs[0].id != null) {
return message.send(tabs[0].id as number, { type: MsgType.__PreloadSelection__ })
}
})
.then(text => text || '')
.catch(() => '')
}

function getClipboard (): Promise<string> {
if (process.env.NODE_ENV === 'development') {
return Promise.resolve('clipboard content')
Expand Down
24 changes: 18 additions & 6 deletions src/content/redux/modules/dictionaries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,16 +410,28 @@ function popupPageInit (

if (baPreload) {
const fetchInfo = baPreload === 'selection'
? message.send({ type: MsgType.__PreloadSelection__ })
? browser.tabs.query({ active: true, currentWindow: true })
.then(tabs => {
if (tabs.length > 0 && tabs[0].id != null) {
return message.send(tabs[0].id as number, { type: MsgType.PreloadSelection })
}
return null
})
: message.send({ type: MsgType.GetClipboard })
.then(text => getDefaultSelectionInfo({ text, title: 'From Clipboard' }))

fetchInfo.then(info => {
if (baAuto && info.text) {
dispatch(searchText({ info }))
} else {
dispatch(restoreDicts())
dispatch(addSearchHistory(info))
if (info) {
if (baAuto && info.text) {
dispatch(searchText({ info }))
} else {
dispatch(restoreDicts())
dispatch(addSearchHistory(info))
}
}
}).catch(e => {
if (process.env.NODE_ENV !== 'production') {
console.warn(e)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/typings/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export const enum MsgType {
* Background proxy sends back underlyingly
*/
__PageInfo__,
__PreloadSelection__,
}

/** iframe messaging */
Expand All @@ -75,6 +74,7 @@ export interface MsgSelection {
readonly mouseY: number
readonly dbClick?: boolean
readonly ctrlKey?: boolean
/** skip salad bowl and show panel directly */
readonly instant?: boolean
/** force panel to skip reconciling position */
readonly force?: boolean
Expand Down

0 comments on commit 5912183

Please sign in to comment.