refactor search redux actions

explicit open and close instead of toggle and close
This commit is contained in:
Labhansh Agrawal 2022-01-09 11:05:57 +05:30
parent bd764b078a
commit 089b90fff1
9 changed files with 36 additions and 28 deletions

View file

@ -13,8 +13,7 @@ import {
SESSION_CLEAR_ACTIVE, SESSION_CLEAR_ACTIVE,
SESSION_USER_DATA, SESSION_USER_DATA,
SESSION_SET_XTERM_TITLE, SESSION_SET_XTERM_TITLE,
SESSION_SEARCH, SESSION_SEARCH
SESSION_SEARCH_CLOSE
} from '../constants/sessions'; } from '../constants/sessions';
import {HyperState, session, HyperDispatch, HyperActions} from '../hyper'; import {HyperState, session, HyperDispatch, HyperActions} from '../hyper';
@ -135,12 +134,13 @@ export function resizeSession(uid: string, cols: number, rows: number) {
}; };
} }
export function onSearch(uid?: string) { export function openSearch(uid?: string) {
return (dispatch: HyperDispatch, getState: () => HyperState) => { return (dispatch: HyperDispatch, getState: () => HyperState) => {
const targetUid = uid || getState().sessions.activeUid!; const targetUid = uid || getState().sessions.activeUid!;
dispatch({ dispatch({
type: SESSION_SEARCH, type: SESSION_SEARCH,
uid: targetUid uid: targetUid,
value: true
}); });
}; };
} }
@ -150,8 +150,9 @@ export function closeSearch(uid?: string, keyEvent?: any) {
const targetUid = uid || getState().sessions.activeUid!; const targetUid = uid || getState().sessions.activeUid!;
if (getState().sessions.sessions[targetUid]?.search) { if (getState().sessions.sessions[targetUid]?.search) {
dispatch({ dispatch({
type: SESSION_SEARCH_CLOSE, type: SESSION_SEARCH,
uid: targetUid uid: targetUid,
value: false
}); });
} else { } else {
if (keyEvent) { if (keyEvent) {

View file

@ -95,7 +95,8 @@ class TermGroup_ extends React.PureComponent<TermGroupProps> {
onResize: this.bind(this.props.onResize, null, uid), onResize: this.bind(this.props.onResize, null, uid),
onTitle: this.bind(this.props.onTitle, null, uid), onTitle: this.bind(this.props.onTitle, null, uid),
onData: this.bind(this.props.onData, null, uid), onData: this.bind(this.props.onData, null, uid),
toggleSearch: this.bind(this.props.toggleSearch, null, uid), onOpenSearch: this.bind(this.props.onOpenSearch, null, uid),
onCloseSearch: this.bind(this.props.onCloseSearch, null, uid),
onContextMenu: this.bind(this.props.onContextMenu, null, uid), onContextMenu: this.bind(this.props.onContextMenu, null, uid),
borderColor: this.props.borderColor, borderColor: this.props.borderColor,
selectionColor: this.props.selectionColor, selectionColor: this.props.selectionColor,

View file

@ -309,7 +309,7 @@ export default class Term extends React.PureComponent<TermProps> {
}; };
closeSearchBox = () => { closeSearchBox = () => {
this.props.toggleSearch(); this.props.onCloseSearch();
}; };
resize(cols: number, rows: number) { resize(cols: number, rows: number) {

View file

@ -109,7 +109,8 @@ export default class Terms extends React.Component<TermsProps> {
onResize: this.props.onResize, onResize: this.props.onResize,
onTitle: this.props.onTitle, onTitle: this.props.onTitle,
onData: this.props.onData, onData: this.props.onData,
toggleSearch: this.props.toggleSearch, onOpenSearch: this.props.onOpenSearch,
onCloseSearch: this.props.onCloseSearch,
onContextMenu: this.props.onContextMenu, onContextMenu: this.props.onContextMenu,
quickEdit: this.props.quickEdit, quickEdit: this.props.quickEdit,
webGLRenderer: this.props.webGLRenderer, webGLRenderer: this.props.webGLRenderer,

View file

@ -13,7 +13,6 @@ export const SESSION_URL_UNSET = 'SESSION_URL_UNSET';
export const SESSION_SET_XTERM_TITLE = 'SESSION_SET_XTERM_TITLE'; export const SESSION_SET_XTERM_TITLE = 'SESSION_SET_XTERM_TITLE';
export const SESSION_SET_CWD = 'SESSION_SET_CWD'; export const SESSION_SET_CWD = 'SESSION_SET_CWD';
export const SESSION_SEARCH = 'SESSION_SEARCH'; export const SESSION_SEARCH = 'SESSION_SEARCH';
export const SESSION_SEARCH_CLOSE = 'SESSION_SEARCH_CLOSE';
export interface SessionAddAction { export interface SessionAddAction {
type: typeof SESSION_ADD; type: typeof SESSION_ADD;
@ -83,10 +82,7 @@ export interface SessionSetCwdAction {
export interface SessionSearchAction { export interface SessionSearchAction {
type: typeof SESSION_SEARCH; type: typeof SESSION_SEARCH;
uid: string; uid: string;
} value: boolean;
export interface SessionSearchCloseAction {
type: typeof SESSION_SEARCH_CLOSE;
uid: string;
} }
export type SessionActions = export type SessionActions =
@ -104,5 +100,4 @@ export type SessionActions =
| SessionUrlUnsetAction | SessionUrlUnsetAction
| SessionSetXtermTitleAction | SessionSetXtermTitleAction
| SessionSetCwdAction | SessionSetCwdAction
| SessionSearchAction | SessionSearchAction;
| SessionSearchCloseAction;

View file

@ -1,6 +1,13 @@
import Terms from '../components/terms'; import Terms from '../components/terms';
import {connect} from '../utils/plugins'; import {connect} from '../utils/plugins';
import {resizeSession, sendSessionData, setSessionXtermTitle, setActiveSession, onSearch} from '../actions/sessions'; import {
resizeSession,
sendSessionData,
setSessionXtermTitle,
setActiveSession,
openSearch,
closeSearch
} from '../actions/sessions';
import {openContextMenu} from '../actions/ui'; import {openContextMenu} from '../actions/ui';
import {getRootGroups} from '../selectors'; import {getRootGroups} from '../selectors';
@ -67,8 +74,13 @@ const mapDispatchToProps = (dispatch: HyperDispatch) => {
onActive(uid: string) { onActive(uid: string) {
dispatch(setActiveSession(uid)); dispatch(setActiveSession(uid));
}, },
toggleSearch(uid: string) {
dispatch(onSearch(uid)); onOpenSearch(uid: string) {
dispatch(openSearch(uid));
},
onCloseSearch(uid: string) {
dispatch(closeSearch(uid));
}, },
onContextMenu(uid: string, selection: any) { onContextMenu(uid: string, selection: any) {

6
lib/hyper.d.ts vendored
View file

@ -283,7 +283,9 @@ export type TermGroupOwnProps = {
| 'modifierKeys' | 'modifierKeys'
| 'onActive' | 'onActive'
| 'onContextMenu' | 'onContextMenu'
| 'onCloseSearch'
| 'onData' | 'onData'
| 'onOpenSearch'
| 'onResize' | 'onResize'
| 'onTitle' | 'onTitle'
| 'padding' | 'padding'
@ -292,7 +294,6 @@ export type TermGroupOwnProps = {
| 'scrollback' | 'scrollback'
| 'selectionColor' | 'selectionColor'
| 'sessions' | 'sessions'
| 'toggleSearch'
| 'uiFontFamily' | 'uiFontFamily'
| 'webGLRenderer' | 'webGLRenderer'
| 'webLinksActivationKey' | 'webLinksActivationKey'
@ -338,9 +339,11 @@ export type TermProps = {
macOptionSelectionMode: string; macOptionSelectionMode: string;
modifierKeys: Immutable<{altIsMeta: boolean; cmdIsMeta: boolean}>; modifierKeys: Immutable<{altIsMeta: boolean; cmdIsMeta: boolean}>;
onActive: () => void; onActive: () => void;
onCloseSearch: () => void;
onContextMenu: (selection: any) => void; onContextMenu: (selection: any) => void;
onCursorMove?: (cursorFrame: {x: number; y: number; width: number; height: number; col: number; row: number}) => void; onCursorMove?: (cursorFrame: {x: number; y: number; width: number; height: number; col: number; row: number}) => void;
onData: (data: string) => void; onData: (data: string) => void;
onOpenSearch: () => void;
onResize: (cols: number, rows: number) => void; onResize: (cols: number, rows: number) => void;
onTitle: (title: string) => void; onTitle: (title: string) => void;
padding: string; padding: string;
@ -352,7 +355,6 @@ export type TermProps = {
searchAddon: SearchAddon | null; searchAddon: SearchAddon | null;
selectionColor: string; selectionColor: string;
term: Terminal | null; term: Terminal | null;
toggleSearch: () => void;
uid: string; uid: string;
uiFontFamily: string; uiFontFamily: string;
url: string | null; url: string | null;

View file

@ -148,7 +148,7 @@ rpc.on('session tmux req', () => {
}); });
rpc.on('session search', () => { rpc.on('session search', () => {
store_.dispatch(sessionActions.onSearch()); store_.dispatch(sessionActions.openSearch());
}); });
rpc.on('session search close', () => { rpc.on('session search close', () => {

View file

@ -10,8 +10,7 @@ import {
SESSION_RESIZE, SESSION_RESIZE,
SESSION_SET_XTERM_TITLE, SESSION_SET_XTERM_TITLE,
SESSION_SET_CWD, SESSION_SET_CWD,
SESSION_SEARCH, SESSION_SEARCH
SESSION_SEARCH_CLOSE
} from '../constants/sessions'; } from '../constants/sessions';
import {sessionState, session, Mutable, ISessionReducer} from '../hyper'; import {sessionState, session, Mutable, ISessionReducer} from '../hyper';
@ -61,10 +60,7 @@ const reducer: ISessionReducer = (state = initialState, action) => {
return state.set('activeUid', action.uid); return state.set('activeUid', action.uid);
case SESSION_SEARCH: case SESSION_SEARCH:
return state.setIn(['sessions', action.uid, 'search'], !state.sessions[action.uid].search); return state.setIn(['sessions', action.uid, 'search'], action.value);
case SESSION_SEARCH_CLOSE:
return state.setIn(['sessions', action.uid, 'search'], false);
case SESSION_CLEAR_ACTIVE: case SESSION_CLEAR_ACTIVE:
return state.merge( return state.merge(