Skip to content

Commit

Permalink
wow closure-compiler is crazy good
Browse files Browse the repository at this point in the history
  • Loading branch information
piranha committed Jan 3, 2016
1 parent bafb090 commit 0e2a05c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 28 deletions.
3 changes: 1 addition & 2 deletions Makefile
@@ -1,12 +1,11 @@
.PHONY: test

UGLIFYJS=node ./node_modules/.bin/uglifyjs
PHANTOMJS=node ./node_modules/.bin/phantomjs

all: keymage.min.js

%.min.js: %.js
$(UGLIFYJS) -c sequences --comments='/^\//' $< > $@
closure-compiler $< > $@

test:
$(PHANTOMJS) test/phantom.js
36 changes: 20 additions & 16 deletions README.md
@@ -1,8 +1,8 @@
# keymage.js

Keymage is a small (`keymage.min.gz` is 2kb) library for handling key bindings in
JavaScript. It supports nested application scopes, has a simple DSL for defining
keys and can handle key chords.
Keymage is a small (1.6kb after Closure Compiler and gzip) library for handling
key bindings in JavaScript. It supports nested application scopes, has a simple
DSL for defining keys and can handle key chords.

[![Build Status](https://travis-ci.org/piranha/keymage.svg?branch=master)](https://travis-ci.org/piranha/keymage) - or [check tests](https://rawgithub.com/piranha/keymage/master/test/test.html) in browser

Expand Down Expand Up @@ -66,34 +66,38 @@ keymage('alt-c', function(e, ctx) {
// -> "alt-c", "", ""
```


## Sequences

Keymage supports key sequences:

```javascript
keymage('ctrl-j k', function() { alert("Nice!"); });
keymage('ctrl-k j', function() { alert("Nice!"); });
```

For this to fire you have to first press both `ctrl` and `j`, and then
`k`. Here's the catch though: `ctrl-j` in most browsers means "open
downloads". Which will break your sequence obviously.
For this to fire you have to first press both `ctrl` and `k`, and then
`j`. This will fire an alert.

There is something to remember: browsers have their own shortcuts, for example
`ctrl-j` in most browsers means "open downloads". And while you can always call
`e.preventDefault()` in usual situation, if `ctrl-j` is part of a sequence, it's
not that easy - you'll get control over what's happening only when the whole
sequence is pressed.

And while I encourage you to not override browser hotkeys, let's imagine you
have to do that. For this, you can pass an option object as last parameter,
having 'preventDefault' property set to `true`:
So keymage provides you with a means to support this use case. I do not
encourage you to override browser hotkeys, but let's imagine you want to do
that. For this, you can pass an option object as last parameter, having
`preventDefault` property set to `true`:

```javascript
keymage('ctrl-t ctrl-j k',
function() { alert("wow"); },
{preventDefault: true});
```

This option will prevent default on every key press which looks like a valid
part of a bound sequence (including the one triggering your handler). And in
this case it's perfectly legitimate - you're overriding `ctrl-j` in the middle
of sequence, so common browser hotkey will still work.

This option will prevent default on *every* key press which is a valid part of a
bound sequence, including the one triggering your handler. Note that pressing
only `ctrl-j` (without `ctrl-t`) will still open downloads, keymage looks for
sequence of `ctrl-t ctrl-j`.

## Scopes

Expand Down
12 changes: 7 additions & 5 deletions keymage.js
@@ -1,7 +1,9 @@
/// keymage.js - Javascript keyboard bindings handling
/// http://github.com/piranha/keymage
///
/// (c) 2012-2016 Alexander Solovyov under terms of ISC License
/** @license
* keymage.js - Javascript keyboard bindings handling
* http://github.com/piranha/keymage
*
* (c) 2012-2016 Alexander Solovyov under terms of ISC License
*/

(function(define, undefined) {
define(function() {
Expand Down Expand Up @@ -66,7 +68,7 @@ define(function() {
}
// top row 0-9
for (i = 0; i < 10; i++) {
KEYS[i.toString()] = i + 48;
KEYS['' + i] = i + 48;
}
// f1-f24
for (i = 1; i < 25; i++) {
Expand Down
19 changes: 14 additions & 5 deletions keymage.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0e2a05c

Please sign in to comment.