From b099b5f1ec2e781380d4fcc9f43b0a0ebf27f5c7 Mon Sep 17 00:00:00 2001 From: CRIMX Date: Fri, 25 May 2018 20:06:55 +0800 Subject: [PATCH] fix(panel): hide dicts when the selection lang does not match --- src/content/components/DictPanel/index.tsx | 4 ++-- src/content/redux/modules/dictionaries.ts | 22 +++++++++++++++++----- src/content/redux/modules/widget.ts | 2 +- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/content/components/DictPanel/index.tsx b/src/content/components/DictPanel/index.tsx index 3345f487e..47d4712e2 100644 --- a/src/content/components/DictPanel/index.tsx +++ b/src/content/components/DictPanel/index.tsx @@ -78,7 +78,7 @@ export default class DictPanel extends React.Component { const { dicts: dictsInfo, - selected: selectedDicts, + active: activeDicts, } = dictionaries // wrap iframe into DictPanel so that react @@ -105,7 +105,7 @@ export default class DictPanel extends React.Component { closePanel, })}
- {selectedDicts.map(id => React.createElement(DictItem, { + {activeDicts.map(id => React.createElement(DictItem, { key: id, id, text: (dictionaries.searchHistory[0] || selection.selectionInfo).text, diff --git a/src/content/redux/modules/dictionaries.ts b/src/content/redux/modules/dictionaries.ts index 015668e7c..569fa8723 100644 --- a/src/content/redux/modules/dictionaries.ts +++ b/src/content/redux/modules/dictionaries.ts @@ -36,6 +36,7 @@ interface DictionariesPayload { [ActionType.SEARCH_START]: { toOnhold: DictID[] toStart: DictID[] + toActive?: DictID[] info: SelectionInfo } [ActionType.SEARCH_END]: { @@ -63,6 +64,7 @@ type DictState = { export type DictionariesState = { readonly dictionaries: { readonly selected: AppConfig['dicts']['selected'] + readonly active: DictID[] readonly dicts: { readonly [k in DictID]?: DictState } @@ -75,6 +77,7 @@ const initConfig = appConfigFactory() export const initState: DictionariesState = { dictionaries: { selected: initConfig.dicts.selected, + active: [], dicts: initConfig.dicts.selected .reduce((state, id) => { state[id] = { @@ -109,6 +112,7 @@ export const reducer: DictsReducer = { dictionaries: { ...dictionaries, selected, + active: [], dicts: selected.reduce((newState, id) => { newState[id] = dictionaries.dicts[id] || { searchStatus: SearchStatus.OnHold, @@ -138,7 +142,7 @@ export const reducer: DictsReducer = { } } }, - [ActionType.SEARCH_START] (state, { toStart, toOnhold, info }) { + [ActionType.SEARCH_START] (state, { toStart, toOnhold, toActive, info }) { const { dictionaries } = state const history = dictionaries.searchHistory @@ -166,6 +170,7 @@ export const reducer: DictsReducer = { ...state, dictionaries: { ...dictionaries, + active: toActive || dictionaries.active, // don't create history for same info searchHistory: info === history[0] ? history @@ -295,19 +300,26 @@ export function searchText (arg?: { id?: DictID, info?: SelectionInfo }): Dispat const { selected: selectedDicts, all: allDicts } = state.config.dicts const toStart: DictID[] = [] const toOnhold: DictID[] = [] + const toActive: DictID[] = [] + selectedDicts.forEach(id => { - if ( - !allDicts[id].defaultUnfold || + const isInvalidLang = ( (!allDicts[id].selectionLang.chs && isContainChinese(info.text)) || (!allDicts[id].selectionLang.eng && isContainEnglish(info.text)) - ) { + ) + + if (!isInvalidLang) { + toActive.push(id) + } + + if (!allDicts[id].defaultUnfold || isInvalidLang) { toOnhold.push(id) } else { toStart.push(id) } }) - dispatch(searchStart({ toStart, toOnhold, info })) + dispatch(searchStart({ toStart, toOnhold, toActive, info })) toStart.forEach(doSearch) if (!isSaladictInternalPage && diff --git a/src/content/redux/modules/widget.ts b/src/content/redux/modules/widget.ts index d38819f8b..fb281eb80 100644 --- a/src/content/redux/modules/widget.ts +++ b/src/content/redux/modules/widget.ts @@ -485,7 +485,7 @@ export function updateItemHeight (id: DictID, height: number): DispatcherThunk { const winHeight = window.innerHeight const newHeight = Math.min( winHeight * state.config.panelMaxHeightRatio, - 30 + state.config.dicts.selected + 30 + state.dictionaries.active .reduce((sum, id) => sum + (dictHeights[id] || 30), 0), )