From a1eb84d8a7c277ddf29d739e46cde14c4c14b4f7 Mon Sep 17 00:00:00 2001 From: Labhansh Agrawal Date: Mon, 11 Nov 2019 20:51:42 +0530 Subject: [PATCH] port js files in lib and lib/containers to ts (#3957) * rename files in lib and lib/containers to ts * add types * fix ts errors --- lib/actions/sessions.ts | 6 +- lib/actions/ui.ts | 4 +- ...ommand-registry.js => command-registry.ts} | 12 ++-- lib/containers/{header.js => header.ts} | 23 ++++---- lib/containers/{hyper.js => hyper.tsx} | 34 +++++++----- .../{notifications.js => notifications.ts} | 11 ++-- lib/containers/{terms.js => terms.ts} | 18 +++--- lib/{index.d.ts => ext-modules.d.ts} | 4 ++ lib/hyper.d.ts | 55 +++++++++++++++++-- lib/{index.js => index.tsx} | 2 +- lib/{rpc.js => rpc.ts} | 0 lib/{selectors.js => selectors.ts} | 3 +- lib/{terms.js => terms.ts} | 0 lib/utils/plugins.ts | 14 +++-- package.json | 3 + webpack.config.js | 2 +- yarn.lock | 31 +++++++++++ 17 files changed, 163 insertions(+), 59 deletions(-) rename lib/{command-registry.js => command-registry.ts} (69%) rename lib/containers/{header.js => header.ts} (75%) rename lib/containers/{hyper.js => hyper.tsx} (87%) rename lib/containers/{notifications.js => notifications.ts} (90%) rename lib/containers/{terms.js => terms.ts} (85%) rename lib/{index.d.ts => ext-modules.d.ts} (71%) rename lib/{index.js => index.tsx} (99%) rename lib/{rpc.js => rpc.ts} (100%) rename lib/{selectors.js => selectors.ts} (65%) rename lib/{terms.js => terms.ts} (100%) diff --git a/lib/actions/sessions.ts b/lib/actions/sessions.ts index 1e78a04b..9daa5085 100644 --- a/lib/actions/sessions.ts +++ b/lib/actions/sessions.ts @@ -136,7 +136,7 @@ export function resizeSession(uid: string, cols: number, rows: number) { }; } -export function onSearch(uid: string) { +export function onSearch(uid?: string) { return (dispatch: Dispatch, getState: () => HyperState) => { const targetUid = uid || getState().sessions.activeUid; dispatch({ @@ -146,7 +146,7 @@ export function onSearch(uid: string) { }; } -export function closeSearch(uid: string) { +export function closeSearch(uid?: string) { return (dispatch: Dispatch, getState: () => HyperState) => { const targetUid = uid || getState().sessions.activeUid; dispatch({ @@ -156,7 +156,7 @@ export function closeSearch(uid: string) { }; } -export function sendSessionData(uid: string, data: any, escaped: any) { +export function sendSessionData(uid: string | null, data: any, escaped?: any) { return (dispatch: Dispatch, getState: () => HyperState) => { dispatch({ type: SESSION_USER_DATA, diff --git a/lib/actions/ui.ts b/lib/actions/ui.ts index 3002082c..31840f58 100644 --- a/lib/actions/ui.ts +++ b/lib/actions/ui.ts @@ -170,7 +170,7 @@ export function moveLeft() { type: UI_MOVE_LEFT, effect() { const state = getState(); - const uid = state.termGroups.activeRootGroup; + const uid = state.termGroups.activeRootGroup!; const groupUids = getGroupUids(state); const index = groupUids.indexOf(uid); const next = groupUids[index - 1] || groupUids[groupUids.length - 1]; @@ -192,7 +192,7 @@ export function moveRight() { effect() { const state = getState(); const groupUids = getGroupUids(state); - const uid = state.termGroups.activeRootGroup; + const uid = state.termGroups.activeRootGroup!; const index = groupUids.indexOf(uid); const next = groupUids[index + 1] || groupUids[0]; if (!next || uid === next) { diff --git a/lib/command-registry.js b/lib/command-registry.ts similarity index 69% rename from lib/command-registry.js rename to lib/command-registry.ts index 3f2a547a..d585f1f4 100644 --- a/lib/command-registry.js +++ b/lib/command-registry.ts @@ -3,21 +3,21 @@ import {remote} from 'electron'; const {getDecoratedKeymaps} = remote.require('./plugins'); -let commands = {}; +let commands: Record = {}; export const getRegisteredKeys = () => { const keymaps = getDecoratedKeymaps(); - return Object.keys(keymaps).reduce((result, actionName) => { + return Object.keys(keymaps).reduce((result: Record, actionName) => { const commandKeys = keymaps[actionName]; - commandKeys.forEach(shortcut => { + commandKeys.forEach((shortcut: string) => { result[shortcut] = actionName; }); return result; }, {}); }; -export const registerCommandHandlers = cmds => { +export const registerCommandHandlers = (cmds: typeof commands) => { if (!cmds) { return; } @@ -25,7 +25,7 @@ export const registerCommandHandlers = cmds => { commands = Object.assign(commands, cmds); }; -export const getCommandHandler = command => { +export const getCommandHandler = (command: string) => { return commands[command]; }; @@ -44,4 +44,4 @@ const roleCommands = [ 'window:toggleFullScreen' ]; -export const shouldPreventDefault = command => !roleCommands.includes(command); +export const shouldPreventDefault = (command: string) => !roleCommands.includes(command); diff --git a/lib/containers/header.js b/lib/containers/header.ts similarity index 75% rename from lib/containers/header.js rename to lib/containers/header.ts index 42fdcbeb..cb7c1ef9 100644 --- a/lib/containers/header.js +++ b/lib/containers/header.ts @@ -5,13 +5,15 @@ import Header from '../components/header'; import {closeTab, changeTab, maximize, openHamburgerMenu, unmaximize, minimize, close} from '../actions/header'; import {connect} from '../utils/plugins'; import getRootGroups from '../selectors'; +import {HyperState} from '../hyper'; +import {Dispatch} from 'redux'; const isMac = /Mac/.test(navigator.userAgent); -const getSessions = ({sessions}) => sessions.sessions; -const getActiveRootGroup = ({termGroups}) => termGroups.activeRootGroup; -const getActiveSessions = ({termGroups}) => termGroups.activeSessions; -const getActivityMarkers = ({ui}) => ui.activityMarkers; +const getSessions = ({sessions}: HyperState) => sessions.sessions; +const getActiveRootGroup = ({termGroups}: HyperState) => termGroups.activeRootGroup; +const getActiveSessions = ({termGroups}: HyperState) => termGroups.activeSessions; +const getActivityMarkers = ({ui}: HyperState) => ui.activityMarkers; const getTabs = createSelector( [getSessions, getRootGroups, getActiveSessions, getActiveRootGroup, getActivityMarkers], (sessions, rootGroups, activeSessions, activeRootGroup, activityMarkers) => @@ -28,7 +30,7 @@ const getTabs = createSelector( ); const HeaderContainer = connect( - state => { + (state: HyperState) => { return { // active is an index isMac, @@ -42,13 +44,13 @@ const HeaderContainer = connect( showWindowControls: state.ui.showWindowControls }; }, - dispatch => { + (dispatch: Dispatch) => { return { - onCloseTab: i => { + onCloseTab: (i: string) => { dispatch(closeTab(i)); }, - onChangeTab: i => { + onChangeTab: (i: string) => { dispatch(changeTab(i)); }, @@ -60,7 +62,7 @@ const HeaderContainer = connect( dispatch(unmaximize()); }, - openHamburgerMenu: coordinates => { + openHamburgerMenu: (coordinates: {x: number; y: number}) => { dispatch(openHamburgerMenu(coordinates)); }, @@ -72,7 +74,8 @@ const HeaderContainer = connect( dispatch(close()); } }; - } + }, + null )(Header, 'Header'); export default HeaderContainer; diff --git a/lib/containers/hyper.js b/lib/containers/hyper.tsx similarity index 87% rename from lib/containers/hyper.js rename to lib/containers/hyper.tsx index ebea39ad..7451f140 100644 --- a/lib/containers/hyper.js +++ b/lib/containers/hyper.tsx @@ -11,22 +11,26 @@ import stylis from 'stylis'; import HeaderContainer from './header'; import TermsContainer from './terms'; import NotificationsContainer from './notifications'; +import {HyperState} from '../hyper'; +import {Dispatch} from 'redux'; const isMac = /Mac/.test(navigator.userAgent); -class Hyper extends React.PureComponent { - constructor(props) { +class Hyper extends React.PureComponent { + mousetrap!: MousetrapInstance; + terms: any; + constructor(props: any) { super(props); this.handleFocusActive = this.handleFocusActive.bind(this); this.handleSelectAll = this.handleSelectAll.bind(this); this.onTermsRef = this.onTermsRef.bind(this); - this.mousetrap = null; + this.state = { lastConfigUpdate: 0 }; } //TODO: Remove usage of legacy and soon deprecated lifecycle methods - UNSAFE_componentWillReceiveProps(next) { + UNSAFE_componentWillReceiveProps(next: any) { if (this.props.backgroundColor !== next.backgroundColor) { // this can be removed when `setBackgroundColor` in electron // starts working again @@ -39,7 +43,7 @@ class Hyper extends React.PureComponent { } } - handleFocusActive(uid) { + handleFocusActive(uid: string) { const term = this.terms.getTermByUid(uid); if (term) { term.focus(); @@ -55,7 +59,7 @@ class Hyper extends React.PureComponent { attachKeyListeners() { if (!this.mousetrap) { - this.mousetrap = new Mousetrap(window, true); + this.mousetrap = new Mousetrap(); this.mousetrap.stopCallback = () => { // All events should be intercepted even if focus is in an input/textarea return false; @@ -64,11 +68,11 @@ class Hyper extends React.PureComponent { this.mousetrap.reset(); } - const keys = getRegisteredKeys(); + const keys: Record = getRegisteredKeys(); Object.keys(keys).forEach(commandKeys => { this.mousetrap.bind( commandKeys, - e => { + (e: any) => { const command = keys[commandKeys]; // We should tell to xterm that it should ignore this event. e.catched = true; @@ -85,12 +89,12 @@ class Hyper extends React.PureComponent { window.rpc.on('term selectAll', this.handleSelectAll); } - onTermsRef(terms) { + onTermsRef(terms: any) { this.terms = terms; window.focusActiveTerm = this.handleFocusActive; } - componentDidUpdate(prev) { + componentDidUpdate(prev: any) { if (prev.activeSession !== this.props.activeSession) { this.handleFocusActive(this.props.activeSession); } @@ -104,7 +108,7 @@ class Hyper extends React.PureComponent { render() { const {isMac: isMac_, customCSS, uiFontFamily, borderColor, maximized, fullScreen} = this.props; const borderWidth = isMac_ ? '' : `${maximized ? '0' : '1'}px`; - + stylis.set({prefix: false}); return (
+