Skip to content

Commit

Permalink
Merge pull request #3 from jaanauati/organize-modules
Browse files Browse the repository at this point in the history
reorganize code into modules.
  • Loading branch information
jaanauati authored Jun 7, 2017
2 parents 5ada66b + d336fe4 commit 8914bc2
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 108 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ Usage:


## TODO:

- Code lacks of organization, would be great to all the things splited in files (redurcers, ations, containers, etc)
- case sensitive / insensite searchs.
- regular expressions.

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "hyper-search",
"version": "0.0.3",
"version": "0.0.4",
"description": "text searhing support for hyper.js",
"main": "index.js",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 0",
"eslint": "node_modules/eslint/bin/eslint.js ."
"eslint": "node_modules/eslint/bin/eslint.js src/*.js"
},
"repository": {
"type": "git",
Expand Down
3 changes: 3 additions & 0 deletions src/actionTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.TOGGLE_SEARCH_INPUT = 'HS_TOGGLE_SEARCH_INPUT';
exports.UPDATE_SEARCH_TEXT = 'HS_UPDATE_SEARCH_TEXT';
exports.CURRENT_MATCH = 'HS_CURRENT_MATCH';
19 changes: 19 additions & 0 deletions src/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { CURRENT_MATCH, TOGGLE_SEARCH_INPUT, UPDATE_SEARCH_TEXT } = require('./actionTypes');

module.exports.setCurrentMatch = function setCurrentMatch(uid, row, startIndex, endIndex) {
return (dispatch) => {
dispatch({ type: CURRENT_MATCH, data: { uid, row, startIndex, endIndex } });
};
};

module.exports.toggleSearchInput = function toggleSearchInput(uid) {
return (dispatch) => {
dispatch({ type: TOGGLE_SEARCH_INPUT, uid });
};
};

module.exports.updateSearchText = function updateSearchText(uid, text) {
return (dispatch) => {
dispatch({ type: UPDATE_SEARCH_TEXT, uid, text });
};
};
5 changes: 5 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports.LEAD_KEY = process.platform === 'darwin' ? 'Cmd' : 'Ctrl';
module.exports.EDIT = 'Edit';
module.exports.ENTER = 'Enter';
module.exports.DIRECTION_NEXT = 1;
module.exports.DIRECTION_PREV = -1;
108 changes: 5 additions & 103 deletions index.js → src/containers.js
Original file line number Diff line number Diff line change
@@ -1,102 +1,5 @@
// some constants
const prefix = process.platform === 'darwin' ? 'Cmd' : 'Ctrl';

const EDIT = 'Edit';
const ENTER = 'Enter';
const DIRECTION_NEXT = 1;
const DIRECTION_PREV = -1;

// action types
const TOGGLE_SEARCH_INPUT = 'HS_TOGGLE_SEARCH_INPUT';
const UPDATE_SEARCH_TEXT = 'HS_UPDATE_SEARCH_TEXT';
const CURRENT_MATCH = 'HS_CURRENT_MATCH';

// action creators
function setCurrentMatch(uid, row, startIndex, endIndex) {
return (dispatch) => {
dispatch({ type: CURRENT_MATCH, data: { uid, row, startIndex, endIndex } });
};
}

function toggleSearchInput(uid) {
return (dispatch) => {
dispatch({ type: TOGGLE_SEARCH_INPUT, uid });
};
}

function updateSearchText(uid, text) {
return (dispatch) => {
dispatch({ type: UPDATE_SEARCH_TEXT, uid, text });
};
}

// reducer
exports.reduceUI = (state, action) => {
switch (action.type) {
case TOGGLE_SEARCH_INPUT: {
const uid = action.uid;
const { hyperSearchToggleInput = {} } = state;
return state.set(
'hyperSearchToggleInput',
Object.assign({}, hyperSearchToggleInput, { [uid]: !hyperSearchToggleInput[uid] })
);
}
case UPDATE_SEARCH_TEXT: {
const { hyperSearchInputText = {} } = state;
return state.set('hyperSearchInputText',
Object.assign({}, hyperSearchInputText, { [action.uid]: action.text }));
}
case CURRENT_MATCH: {
const { hyperSearchCurrentRow = {} } = state;
const { uid, row, startIndex, endIndex } = action.data;
return state.set('hyperSearchCurrentRow',
Object.assign({}, hyperSearchCurrentRow, { [uid]: { row, startIndex, endIndex } }));
}
default:
return state;
}
};

exports.decorateMenu = (menu) => {
for (const menuItem of menu) {
if (menuItem.label === EDIT) {
menuItem.submenu = menuItem.submenu.concat({
label: 'Find',
submenu: [
{
label: 'Toggle Find Bar',
accelerator: `${prefix}+F`,
click(item, focusedWindow) {
if (focusedWindow !== null) {
focusedWindow.rpc.emit('hyper-search:toggle:input', { focusedWindow });
}
},
},
{
label: 'Find Next',
accelerator: `${prefix}+G`,
click(item, focusedWindow) {
if (focusedWindow !== null) {
focusedWindow.rpc.emit('hyper-search:seach:next', { focusedWindow });
}
},
},
{
label: 'Find Previous',
accelerator: `${prefix}+Shift+G`,
click(item, focusedWindow) {
if (focusedWindow !== null) {
focusedWindow.rpc.emit('hyper-search:seach:prev', { focusedWindow });
}
},
},
],
});
break;
}
}
return menu;
};
const { setCurrentMatch, toggleSearchInput, updateSearchText } = require('./actions');
const { DIRECTION_NEXT, DIRECTION_PREV, ENTER } = require('./constants');

exports.mapTermsState = (state, map) => (
Object.assign(map, {
Expand All @@ -108,8 +11,7 @@ exports.mapTermsState = (state, map) => (
})
);


const passProps = (uid, parentProps, props) => (
exports.passProps = (uid, parentProps, props) => (
Object.assign(props, {
focussedSessionUid: parentProps.focussedSessionUid,
hyperSearchToggleInput: parentProps.hyperSearchToggleInput,
Expand All @@ -119,8 +21,8 @@ const passProps = (uid, parentProps, props) => (
})
);

exports.getTermGroupProps = passProps;
exports.getTermProps = passProps;
exports.getTermGroupProps = exports.passProps;
exports.getTermProps = exports.passProps;

exports.decorateTerm = (Term, { React }) => {
class HyperSearchTerm extends React.Component {
Expand Down
12 changes: 12 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

const { decorateTerm, mapTermsState, passProps, getTermGroupProps, getTermProps } = require('./containers');
const { decorateMenu } = require('./menu');
const { reduceUI } = require('./reducers');

exports.reduceUI = reduceUI;
exports.decorateMenu = decorateMenu;
exports.decorateTerm = decorateTerm;
exports.mapTermsState = mapTermsState;
exports.passProps = passProps;
exports.getTermGroupProps = getTermGroupProps;
exports.getTermProps = getTermProps;
42 changes: 42 additions & 0 deletions src/menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { EDIT, LEAD_KEY } = require('./constants');

exports.decorateMenu = (menu) => {
for (const menuItem of menu) {
if (menuItem.label === EDIT) {
menuItem.submenu = menuItem.submenu.concat({
label: 'Find',
submenu: [
{
label: 'Toggle Find Bar',
accelerator: `${LEAD_KEY}+F`,
click(item, focusedWindow) {
if (focusedWindow !== null) {
focusedWindow.rpc.emit('hyper-search:toggle:input', { focusedWindow });
}
},
},
{
label: 'Find Next',
accelerator: `${LEAD_KEY}+G`,
click(item, focusedWindow) {
if (focusedWindow !== null) {
focusedWindow.rpc.emit('hyper-search:seach:next', { focusedWindow });
}
},
},
{
label: 'Find Previous',
accelerator: `${LEAD_KEY}+Shift+G`,
click(item, focusedWindow) {
if (focusedWindow !== null) {
focusedWindow.rpc.emit('hyper-search:seach:prev', { focusedWindow });
}
},
},
],
});
break;
}
}
return menu;
};
27 changes: 27 additions & 0 deletions src/reducers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { TOGGLE_SEARCH_INPUT, UPDATE_SEARCH_TEXT, CURRENT_MATCH } = require('./actionTypes');

exports.reduceUI = (state, action) => {
switch (action.type) {
case TOGGLE_SEARCH_INPUT: {
const uid = action.uid;
const { hyperSearchToggleInput = {} } = state;
return state.set(
'hyperSearchToggleInput',
Object.assign({}, hyperSearchToggleInput, { [uid]: !hyperSearchToggleInput[uid] })
);
}
case UPDATE_SEARCH_TEXT: {
const { hyperSearchInputText = {} } = state;
return state.set('hyperSearchInputText',
Object.assign({}, hyperSearchInputText, { [action.uid]: action.text }));
}
case CURRENT_MATCH: {
const { hyperSearchCurrentRow = {} } = state;
const { uid, row, startIndex, endIndex } = action.data;
return state.set('hyperSearchCurrentRow',
Object.assign({}, hyperSearchCurrentRow, { [uid]: { row, startIndex, endIndex } }));
}
default:
return state;
}
};

0 comments on commit 8914bc2

Please sign in to comment.