Skip to content

Commit 504ac04

Browse files
author
Matt Swanson
committed
add keyboard shortcuts, closes stringer-rss#9, eat your heart out @jqr
1 parent 06e8dc1 commit 504ac04

7 files changed

Lines changed: 128 additions & 2 deletions

File tree

‎app/public/css/styles.css‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,15 @@ li.story .story-body h1 a:hover {
184184
color: #7F8281;
185185
}
186186

187+
li.story.cursor {
188+
border: 3px solid #484948;
189+
}
190+
191+
p.story-details {
192+
margin-right: 5px;
193+
overflow: hidden;
194+
}
195+
187196
.story-body h2 {
188197
font-size: 22px;
189198
margin-bottom: 11px;
@@ -459,4 +468,31 @@ li.feed .remove-feed a:hover {
459468
.footer-links li {
460469
display: inline;
461470
padding: 0 2px;
471+
}
472+
473+
kbd {
474+
padding: 0.1em 0.6em;
475+
border: 1px solid #CCC;
476+
font-family: Arial,Helvetica,sans-serif;
477+
color: #333;
478+
-moz-box-shadow: 0 1px 0px rgba(0, 0, 0, 0.2),0 0 0 2px #ffffff inset;
479+
-webkit-box-shadow: 0 1px 0px rgba(0, 0, 0, 0.2),0 0 0 2px #FFF inset;
480+
box-shadow: 0 1px 0px rgba(0, 0, 0, 0.2),0 0 0 2px #FFF inset;
481+
-moz-border-radius: 3px;
482+
-webkit-border-radius: 3px;
483+
border-radius: 3px;
484+
display: inline-block;
485+
margin: 0 0.1em;
486+
text-shadow: 0 1px 0 #FFF;
487+
line-height: 1.4;
488+
white-space: nowrap;
489+
}
490+
491+
ul.shortcut-legend li {
492+
margin-bottom: 10px;
493+
}
494+
495+
li.blank {
496+
list-style-type: none;
497+
height: 20px;
462498
}

‎app/public/js/app.js‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ $(document).ready(function() {
4343
}
4444
});
4545

46+
$("li.story").on("toggleStory", function(e) {
47+
e.preventDefault();
48+
var $this = $(this);
49+
50+
if($this.hasClass("open")) {
51+
$this.trigger("closeStory");
52+
} else {
53+
$this.trigger("openStory");
54+
}
55+
});
56+
4657
$("#mark-all").click(function(e) {
4758
e.preventDefault();
4859

@@ -65,4 +76,49 @@ $(document).ready(function() {
6576
.fail(function() { alert("something broke!"); });
6677
}
6778
});
79+
80+
Mousetrap.bind("?", function() {
81+
$("#shortcuts").modal('toggle');
82+
});
83+
84+
var cursor_position = -1;
85+
var MAX_POSITION = $("li.story").size();
86+
87+
Mousetrap.bind("k", function() {
88+
if (cursor_position < MAX_POSITION - 1) {
89+
cursor_position++;
90+
91+
$("li.story").removeClass("cursor");
92+
$("li.story").eq(cursor_position).addClass("cursor");
93+
}
94+
});
95+
96+
Mousetrap.bind("j", function() {
97+
$("li.story").removeClass("cursor");
98+
99+
if (cursor_position > 0) {
100+
cursor_position--;
101+
$("li.story").eq(cursor_position).addClass("cursor");
102+
} else {
103+
$("li.story").eq(0).addClass("cursor");
104+
}
105+
});
106+
107+
Mousetrap.bind(["o", "enter"], function() {
108+
$("li.story").eq(cursor_position).trigger("toggleStory");
109+
});
110+
111+
Mousetrap.bind("r", function() {
112+
var refresh = $("a#refresh")[0];
113+
if (refresh) refresh.click();
114+
});
115+
116+
Mousetrap.bind("shift+a", function() {
117+
$("form#mark-all-as-read").submit();
118+
});
119+
120+
Mousetrap.bind("v", function() {
121+
var permalink = $("li.story.open a#story-permalink")[0];
122+
if (permalink) permalink.click();
123+
});
68124
});

‎app/public/js/bootstrap.min.js‎

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎app/public/js/mousetrap.min.js‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎app/views/layout.erb‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
<link rel="stylesheet" type="text/css" href="/css/styles.css">
1414

1515
<script type="text/javascript" src="/js/jquery-1.9.1.min.js"></script>
16+
<script type="text/javascript" src="js/mousetrap.min.js"></script>
17+
<script type="text/javascript" src="js/bootstrap.min.js"></script>
1618
<script type="text/javascript" src="/js/app.js"></script>
1719

1820
<%= yield_content :head %>
@@ -21,6 +23,7 @@
2123
<div id="wrap">
2224
<div id="frame">
2325
<%= render_partial :flash %>
26+
<%= render_partial :shortcuts if current_user %>
2427
<div class="container">
2528
<div class="row-fluid">
2629
<div class="span12">

‎app/views/partials/_shortcuts.erb‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<div id="shortcuts" class="modal hide fade" tabindex="-1" role="dialog">
2+
<div class="modal-header">
3+
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
4+
<h3 id="myModalLabel">Keyboard shortcuts</h3>
5+
</div>
6+
<div class="modal-body">
7+
<ul class="shortcut-legend">
8+
<li><kbd>j</kbd>/<kbd>k</kbd>: Move up/down</li>
9+
<li><kbd>o</kbd> or <kbd>enter</kbd>: Toggle story open/closed</li>
10+
<li><kbd>v</kbd>: Go to story URL</li>
11+
<li class="blank"></li>
12+
<li><kbd>shift</kbd>+<kbd>a</kbd>: Mark all as read</li>
13+
<li><kbd>r</kbd>: Refresh</li>
14+
</ul>
15+
</div>
16+
</div>

‎app/views/partials/_story.erb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</p>
77
</div>
88
<div class="span9">
9-
<p>
9+
<p class="story-details">
1010
<span class="story-title">
1111
<%= story.headline %>
1212
</span>
@@ -29,7 +29,7 @@
2929
</span>
3030
</div>
3131
<div class="pull-right story-actions">
32-
<a href="<%=story.permalink %>">
32+
<a id="story-permalink" href="<%= story.permalink %>">
3333
<i class="icon-external-link"></i>
3434
</a>
3535
</div>

0 commit comments

Comments
 (0)