Skip to content
This repository has been archived by the owner on Apr 7, 2023. It is now read-only.

Commit

Permalink
Refactor GlobalKeyBind
Browse files Browse the repository at this point in the history
  • Loading branch information
k0kubun committed Mar 19, 2016
1 parent a5b4ff7 commit 0b530fe
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import GlobalKeyBind from './utils/global-key-bind';

let store = createStore(rootReducer);
new IpcAction(document).subscribe(store);
new GlobalKeyBind(document).subscribe(store);
new GlobalKeyBind.subscribe(store);

if (process.env.NODE_ENV !== 'production') {
//store.subscribe(() => console.log(store.getState()));
Expand Down
76 changes: 39 additions & 37 deletions src/utils/global-key-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,81 +4,84 @@ import Actions from '../actions';
import TwitterClient from './twitter-client';

export default class GlobalKeyBind {
constructor(document) {
this.document = document;
static subscribe(store) {
new GlobalKeyBind().subscribe(store);
}

subscribe(store) {
this.store = store;
this.document.addEventListener('keydown', (event) => {
switch (event.keyCode) {
case Keycode.TAB:
this.handleTab(event);
break;
case Keycode.DOWN:
case Keycode.J:
this.handleJ(event);
break;
case Keycode.UP:
case Keycode.K:
this.handleK(event);
break;
case Keycode.F:
this.handleF(event);
break;
case Keycode.SPACE:
case Keycode.ZERO:
this.handleZero(event);
break;
document.addEventListener('keydown', (event) => {
if (event.keyCode === Keycode.TAB) {
this.handleTab(event);
return;
}

if (!this.isEditing()) {
switch (event.keyCode) {
case Keycode.DOWN:
case Keycode.J:
this.handleJ(event);
break;
case Keycode.UP:
case Keycode.K:
this.handleK(event);
break;
case Keycode.F:
this.handleF(event);
break;
case Keycode.SPACE:
case Keycode.ZERO:
this.handleZero(event);
break;
}
}
});
}

handleTab(event) {
let editor = this.document.getElementById('tweet_editor');
let editor = document.getElementById('tweet_editor');

if (this.document.activeElement != editor) {
if (document.activeElement != editor) {
event.preventDefault();
editor.focus();
}
}

handleJ(event) {
if (this.isEditing() || event.altKey || event.metaKey) return;
if (event.altKey || event.metaKey) return;
event.preventDefault();

let state = new RichState(store);
let tweet = state.findNextTweet();
if (!tweet) return null;
store.dispatch(Actions.tweets.selectTweet(tweet, state.activeTab(), state.activeAccount()));

let visibleLimit = this.document.body.clientHeight;
let activeBottom = this.document.querySelector('.timeline.active .tweets.active .tweet.active').getBoundingClientRect().bottom;
let visibleLimit = document.body.clientHeight;
let activeBottom = document.querySelector('.timeline.active .tweets.active .tweet.active').getBoundingClientRect().bottom;
if (visibleLimit < activeBottom) {
let element = this.document.querySelector('.timeline.active .tweets.active');
let element = document.querySelector('.timeline.active .tweets.active');
element.scrollTop += element.clientHeight / 2;
}
}

handleK(event) {
if (this.isEditing() || event.altKey || event.metaKey) return;
if (event.altKey || event.metaKey) return;
event.preventDefault();

let state = new RichState(store);
let tweet = state.findPrevTweet();
if (!tweet) return null;
store.dispatch(Actions.tweets.selectTweet(tweet, state.activeTab(), state.activeAccount()));

let activeTop = this.document.querySelector('.timeline.active .tweets.active .tweet.active').getBoundingClientRect().top;
let visibleLimit = this.document.querySelector('.timeline.active .tweets.active').getBoundingClientRect().top;
let activeTop = document.querySelector('.timeline.active .tweets.active .tweet.active').getBoundingClientRect().top;
let visibleLimit = document.querySelector('.timeline.active .tweets.active').getBoundingClientRect().top;
if (activeTop < visibleLimit) {
let element = this.document.querySelector('.timeline.active .tweets.active');
let element = document.querySelector('.timeline.active .tweets.active');
element.scrollTop -= element.clientHeight / 2;
}
}

handleF(event) {
if (this.isEditing()) return;
event.preventDefault();

let state = new RichState(store);
Expand All @@ -92,20 +95,19 @@ export default class GlobalKeyBind {
}

handleZero(event) {
if (this.isEditing()) return;
event.preventDefault();

let state = new RichState(store);
let tweet = state.findFirstTweet();
if (!tweet) return null;

store.dispatch(Actions.tweets.selectTweet(tweet, state.activeTab(), state.activeAccount()));
let element = this.document.querySelector('.timeline.active .tweets.active');
let element = document.querySelector('.timeline.active .tweets.active');
element.scrollTop = 0;
}

isEditing() {
return this.document.activeElement.id === 'tweet_editor' ||
this.document.activeElement.className === 'search_field';
return document.activeElement.id === 'tweet_editor' ||
document.activeElement.className === 'search_field';
}
}

0 comments on commit 0b530fe

Please sign in to comment.