migrated to new version of lib/keyboard.js

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2013-10-03 18:01:59 +04:00
parent b67505dbc0
commit 2b8081ce9d
2 changed files with 423 additions and 149 deletions

View File

@ -343,18 +343,21 @@ var KEYBOARD_CONFIG = {
Home: firstPage, Home: firstPage,
End: lastPage, End: lastPage,
Left: { Left: {
default: prevPage, default: function(){ prevPage() },
shift: prevBookmark,
ctrl: prevArticle, ctrl: prevArticle,
}, },
Right: { Right: {
default: nextPage, default: function(){ nextPage() },
shift: nextBookmark,
ctrl: nextArticle, ctrl: nextArticle,
}, },
Space: { Space: {
default: nextPage, default: 'Right',
shift: prevPage shift: 'Left'
}, },
Tab: 'Space', //Tab: 'Space',
Tab: function(){ return false },
Enter: function(){ togglePageView('on') }, Enter: function(){ togglePageView('on') },
// combined navigation with actions.. // combined navigation with actions..
Up: function(){ togglePageView() }, Up: function(){ togglePageView() },
@ -362,7 +365,7 @@ var KEYBOARD_CONFIG = {
F: function(){ togglePageFitMode() }, F: function(){ togglePageFitMode() },
B: { B: {
default: toggleBookmark, default: function(){ toggleBookmark() },
ctrl: function(){ toggleThemes() }, ctrl: function(){ toggleThemes() },
}, },

View File

@ -25,7 +25,7 @@ var _SPECIAL_KEYS = {
// Special Keys... // Special Keys...
9: 'Tab', 33: 'PgUp', 45: 'Ins', 9: 'Tab', 33: 'PgUp', 45: 'Ins',
13: 'Enter', 34: 'PgDown', 46: 'Del', 13: 'Enter', 34: 'PgDown', 46: 'Del',
16: 'Shift', 35: 'End', 80: 'Backspace', 16: 'Shift', 35: 'End', 8: 'Backspace',
17: 'Ctrl', 36: 'Home', 91: 'Win', 17: 'Ctrl', 36: 'Home', 91: 'Win',
18: 'Alt', 37: 'Left', 93: 'Menu', 18: 'Alt', 37: 'Left', 93: 'Menu',
20: 'Caps Lock',38: 'Up', 20: 'Caps Lock',38: 'Up',
@ -39,8 +39,19 @@ var _SPECIAL_KEYS = {
115: 'F4', 119: 'F8', 123: 'F12', 115: 'F4', 119: 'F8', 123: 'F12',
// Number row.. // Number row..
49: '1', 50: '2', 51: '3', 52: '4', 53: '5', // NOTE: to avoid conflicts with keys that have a code the same as
54: '6', 55: '7', 56: '8', 57: '9', 48: '0', // the value of a number key...
// Ex:
// 'Backspace' (8) vs. '8' (56)
// 'Tab' (9) vs. '9' (57)
// ...all of the numbers start with a '#'
// this is a problem due to JS coercing the types to string
// on object attr access.
// Ex:
// o = {1: 2}
// o[1] == o['1'] == true
49: '#1', 50: '#2', 51: '#3', 52: '#4', 53: '#5',
54: '#6', 55: '#7', 56: '#8', 57: '#9', 48: '#0',
// Punctuation... // Punctuation...
// top row... // top row...
@ -51,6 +62,20 @@ var _SPECIAL_KEYS = {
188: ',', 190: '.', 191: '/', 188: ',', 190: '.', 191: '/',
} }
var _SHIFT_KEYS = {
'`': '~', '-': '_', '=':'+',
1: '!', 2: '@', 3: '#', 4: '$', 5: '%',
6:'^', 7:'&', 8: '*', 9: '(', 0: ')',
'[': '{', ']': '}', '\\': '|',
';': ':', '\'': '"',
',': '<', '.': '>', '/': '?'
}
// build a reverse map of _SPECIAL_KEYS
var _KEY_CODES = {} var _KEY_CODES = {}
for(var k in _SPECIAL_KEYS){ for(var k in _SPECIAL_KEYS){
_KEY_CODES[_SPECIAL_KEYS[k]] = k _KEY_CODES[_SPECIAL_KEYS[k]] = k
@ -73,6 +98,7 @@ function toKeyName(code){
return null return null
} }
function toKeyCode(c){ function toKeyCode(c){
if(c in _KEY_CODES){ if(c in _KEY_CODES){
return _KEY_CODES[c] return _KEY_CODES[c]
@ -81,8 +107,203 @@ function toKeyCode(c){
} }
// if set to false the event handlers will always return false... // documentation wrapper...
var KEYBOARD_HANDLER_PROPAGATE = true function doc(text, func){
func = func == null ? function(){return true}: func
func.doc = text
return func
}
/* Key handler getter
*
* For doc on format see makeKeyboardHandler(...)
*
* modes can be:
* - 'any' (default) - Get list of all applicable handlers up until
* the first applicable ignore.
* - 'all' - Get ALL handlers, including ignores
* - <mode> - Get handlers for an explicit mode
*
* modifiers can be:
* - '' (default) - No modifiers
* - '?' - Return list of applicable modifiers per mode
* - <modifier> - Any of 'ctrl', 'alt' or 'shift' alone or in
* combination.
* Combinations MUST be ordered as shown above.
* Combination elements are separated by '+'
* Ex:
* 'ctrl+shift'
* NOTE: 'shift+ctrl' is wrong.
*
* This will also resolve several shifted keys by name, for example:
* 'shift-/' is the same as '?', and either can be used, but the shorter
* direct notation has priority (see _SHIFT_KEYS for supported keys).
*
*
* Returns:
* {
* <mode>: <handler>,
* ...
* }
*
*
* NOTE: it is not possible to do a shift-? as it is already shifted.
* NOTE: if a key is not handled in a mode, that mode will not be
* present in the resulting object.
* NOTE: this will not unwrap lisp-style (see below) handlers.
* NOTE: modes are prioritized by order of occurrence.
*
* XXX need an explicit way to prioritize modes, avoiding object attr
* ordering...
* XXX check do we need did_handling here...
*
* XXX BUG explicitly given modes do not yield results if the pattern
* does not match...
*/
function getKeyHandlers(key, modifiers, keybindings, modes, shifted_keys){
var chr = null
var s_chr = null
var did_handling = false
modifiers = modifiers == null ? '' : modifiers
modes = modes == null ? 'any' : modes
shifted_keys = shifted_keys == null ? _SHIFT_KEYS : shifted_keys
if(typeof(key) == typeof(123)){
key = key
chr = toKeyName(key)
} else {
chr = key
key = toKeyCode(key)
}
// XXX this is not done yet...
if(shifted_keys != false && /shift/i.test(modifiers)){
var s_chr = shifted_keys[chr]
}
res = {}
for(var mode in keybindings){
// test for mode compatibility...
// XXX this fails for explicitly given mode...
if(modes != 'all'
&& (modes != 'any'
&& modes != mode
|| $(mode).length == 0)){
continue
}
var bindings = keybindings[mode]
if(s_chr != null && s_chr in bindings){
var handler = bindings[s_chr]
chr = s_chr
modifiers = modifiers.replace(/\+?shift/i, '')
} else if(chr in bindings){
var handler = bindings[chr]
} else {
var handler = bindings[key]
}
// alias...
while( handler != null
&& (typeof(handler) == typeof(123)
|| typeof(handler) == typeof('str')
|| typeof(handler) == typeof({})
&& handler.constructor.name == 'Object') ){
// do the complex handler aliases...
if(typeof(handler) == typeof({}) && handler.constructor.name == 'Object'){
// build modifier list...
if(modifiers == '?'){
break
}
if(modifiers in handler){
if(typeof(handler[modifiers]) == typeof('str')){
handler = handler[modifiers]
} else {
break
}
} else if(typeof(handler['default']) == typeof('str')){
handler = handler['default']
} else {
break
}
}
// simple handlers...
if(handler in bindings){
// XXX need to take care of that we can always be a number or a string...
handler = bindings[handler]
} else if(typeof(handler) == typeof(1)) {
handler = bindings[toKeyName(handler)]
} else {
handler = bindings[toKeyCode(handler)]
}
}
// no handler...
if(handler == null){
// if something is ignored then just breakout and stop handling...
if(bindings.ignore == '*'
|| bindings.ignore != null
&& (bindings.ignore.indexOf(key) != -1
|| bindings.ignore.indexOf(chr) != -1)){
did_handling = true
// ignoring a key will stop processing it...
if(modes == 'all' || mode == modes){
res[mode] = 'IGNORE'
} else {
break
}
}
continue
}
// complex handler...
if(typeof(handler) == typeof({}) && handler.constructor.name == 'Object'){
// build modifier list...
if(modifiers == '?'){
res[mode] = Object.keys(handler)
did_handling = true
continue
}
var callback = handler[modifiers]
if(callback == null){
callback = handler['default']
}
if(callback != null){
res[mode] = callback
did_handling = true
continue
}
// simple callback...
} else {
// build modifier list...
if(modifiers == '?'){
res[mode] = 'none'
} else {
res[mode] = handler
}
did_handling = true
continue
}
if(modes != 'all' && did_handling){
break
}
}
return res
}
/* Basic key binding format: /* Basic key binding format:
* *
@ -95,14 +316,29 @@ var KEYBOARD_HANDLER_PROPAGATE = true
* // this defines the list of keys to ignore by the handler. * // this defines the list of keys to ignore by the handler.
* // NOTE: use "*" to ignore all keys other than explicitly * // NOTE: use "*" to ignore all keys other than explicitly
* // defined in the current section. * // defined in the current section.
* // NOTE: ignoring a key will stop processing it in other
* // compatible modes.
* ignore: <ignored-keys> * ignore: <ignored-keys>
* *
* // NOTE: a callback can have a .doc attr containing
* // documentation...
* <key-def> : <callback>, * <key-def> : <callback>,
* *
* <key-def> : [
* // this can be any type of handler except for an alias...
* <handler>,
* <doc>
* ],
*
* <key-def> : { * <key-def> : {
* 'default': <callback>, * // modifiers can either have a callback or an alias as
* // a value...
* // NOTE: when the alias is resolved, the same modifiers
* // will be applied to the final resolved handler.
* default: <callback> | <key-def-x>,
*
* // a modifier can be any single modifier, like shift or a * // a modifier can be any single modifier, like shift or a
* // combination of modifers like 'ctrl+shift', given in order * // combination of modifiers like 'ctrl+shift', given in order
* // of priority. * // of priority.
* // supported modifiers are (in order of priority): * // supported modifiers are (in order of priority):
* // - ctrl * // - ctrl
@ -112,12 +348,6 @@ var KEYBOARD_HANDLER_PROPAGATE = true
* ... * ...
* }, * },
* *
* <key-def> : [
* // this can be any type of handler except for an alias...
* <handler>,
* <doc>
* ],
*
* // alias... * // alias...
* <key-def-a> : <key-def-b>, * <key-def-a> : <key-def-b>,
* *
@ -132,117 +362,79 @@ var KEYBOARD_HANDLER_PROPAGATE = true
* - explicit key code, e.g. 65 * - explicit key code, e.g. 65
* - key name, if present in _SPECIAL_KEYS, e.g. Enter * - key name, if present in _SPECIAL_KEYS, e.g. Enter
* - key char (uppercase), as is returned by String.fromCharCode(...) e.g. A * - key char (uppercase), as is returned by String.fromCharCode(...) e.g. A
* - action -- any arbitrary string that is not in the above categories.
* *
* *
* NOTE: to rest what to use as <key-def> use toKeyCode(..) / toKeyName(..). * NOTE: actions,the last case, are used for alias referencing, they will
* never match a real key, but will get resolved in alias searches.
* NOTE: to test what to use as <key-def> use toKeyCode(..) / toKeyName(..).
* NOTE: all fields are optional. * NOTE: all fields are optional.
* NOTE: if a handler explicitly returns false then that will break the * NOTE: if a handler explicitly returns false then that will break the
* event propagation chain and exit the handler. * event propagation chain and exit the handler.
* i.e. no other matching handlers will be called. * i.e. no other matching handlers will be called.
* NOTE: if more than one match is found all matching handlers will be
* called in sequence until one returns false explicitly.
* NOTE: a <css-selector> is used as a predicate to select a section to * NOTE: a <css-selector> is used as a predicate to select a section to
* use. if multiple selectors match something then multiple sections * use. if multiple selectors match something then multiple sections
* will be resolved in order of occurrence. * will be resolved in order of occurrence.
* NOTE: the number keys are named with a leading hash '#' (e.g. '#8')
* to avoid conflicsts with keys that have the code with the same
* value (e.g. 'backspace' (8)).
* NOTE: one can use a doc(<doc-string>, <callback>) as a shorthand to assign
* a docstring to a handler.
* it will only assign .doc attr and return the original function.
* *
* XXX might need to add meta information to generate sensible help... * XXX need an explicit way to prioritize modes...
* XXX will aliases get resolved if they are in a different mode??
*/ */
function makeKeyboardHandler(keybindings, unhandled){ function makeKeyboardHandler(keybindings, unhandled){
if(unhandled == null){ if(unhandled == null){
//unhandled = function(){return false} unhandled = function(){}
unhandled = function(){return KEYBOARD_HANDLER_PROPAGATE}
} }
return function(evt){ return function(evt){
var did_handling = false var did_handling = false
var res = null var res = null
for(var mode in keybindings){
if($(mode).length > 0){
var bindings = keybindings[mode]
// key data...
var key = evt.keyCode var key = evt.keyCode
var chr = toKeyName(evt.keyCode)
// normalize the modifiers... // normalize the modifiers...
var modifers = evt.ctrlKey ? 'ctrl' : '' var modifiers = evt.ctrlKey ? 'ctrl' : ''
modifers += evt.altKey ? (modifers != '' ? '+alt' : 'alt') : '' modifiers += evt.altKey ? (modifiers != '' ? '+alt' : 'alt') : ''
modifers += evt.shiftKey ? (modifers != '' ? '+shift' : 'shift') : '' modifiers += evt.shiftKey ? (modifiers != '' ? '+shift' : 'shift') : ''
if(chr in bindings){ //window.DEBUG && console.log('KEY:', key, chr, modifiers)
var handler = bindings[chr]
} else { var handlers = getKeyHandlers(key, modifiers, keybindings)
var handler = bindings[key]
} for(var mode in handlers){
var handler = handlers[mode]
if(handler != null){
// alias...
while (typeof(handler) == typeof(123) || typeof(handler) == typeof('str')) {
if(handler in bindings){
// XXX need to take care of that we can always be a number or a string...
handler = bindings[handler]
} else if(typeof(h) == typeof(1)) {
handler = bindings[toKeyName(handler)]
} else {
handler = bindings[toKeyCode(handler)]
}
}
// no handler...
if(handler == null){
// if something is ignored then just breakout and stop handling...
if(bindings.ignore == '*'
|| bindings.ignore != null && bindings.ignore.indexOf(key) != -1){
res = res == null ? true : res
did_handling = true
// ignoring a key will stop processing it...
break
}
continue
}
// Array, lisp style with docs... // Array, lisp style with docs...
// XXX for some odd reason typeof([]) == typeof({})!!!
if(typeof(handler) == typeof([]) && handler.constructor.name == 'Array'){ if(typeof(handler) == typeof([]) && handler.constructor.name == 'Array'){
// we do not care about docs here, so just get the handler... // we do not care about docs here, so just get the handler...
handler = handler[0] handler = handler[0]
} }
// complex handler...
if(typeof(handler) == typeof({})){
var callback = handler[modifers]
if(callback == null){
callback = handler['default']
}
if(callback != null){
res = callback()
did_handling = true did_handling = true
continue res = handler(evt)
}
} else {
// simple callback...
//res = handler(evt)
res = handler()
// if the handler explicitly returned false break out...
if(res === false){ if(res === false){
// XXX is this corrent??? break
// XXX should we just break here instead of return...
return KEYBOARD_HANDLER_PROPAGATE ? res : null
}
did_handling = true
continue
} }
} }
} }
if(!did_handling){ if(!did_handling){
// key is unhandled by any modes...
return unhandled(key) return unhandled(key)
} else {
// XXX should we handle multiple hits???
return KEYBOARD_HANDLER_PROPAGATE&&res?true:false
} }
return res
} }
} }
// helper...
function doc(text, func){
func.doc = text
return func
}
/* Build structure ready for conversion to HTML help. /* Build structure ready for conversion to HTML help.
*
* Structure: * Structure:
* { * {
* <section-title>: { * <section-title>: {
@ -255,72 +447,151 @@ function doc(text, func){
* *
* <keys-spec> - list of key names. * <keys-spec> - list of key names.
* *
*
* NOTE: this will not add keys (key names) that are not explicit key names.
*/ */
function getKeyHandler(key, keybindings){ function buildKeybindingsHelp(keybindings, shifted_keys){
} shifted_keys = shifted_keys == null ? _SHIFT_KEYS : shifted_keys
function buildKeyindingsHelp(keybindings){
var res = {} var res = {}
var mode, title
for(var selector in keybindings){ for(var pattern in keybindings){
var section = keybindings[selector] mode = keybindings[pattern]
var title = section.title == null ? selector : section.title
var res_sec = { // titles and docs...
doc: section.doc, title = mode.title == null ? pattern : mode.title
res[title] = {
doc: mode.doc == null ? '' : mode.doc
} }
res.title = res_sec section = res[title]
for(var k in section){ // handlers...
// handler... for(var key in mode){
var h = section[k] if(key == 'doc' || key == 'title' || key == 'ignore'){
var doc continue
var key = typeof(k) == typeof(1) ? toKeyName(k) : k }
//var modifiers = getKeyHandlers(key, '?', keybindings, pattern)[pattern]
var modifiers = getKeyHandlers(key, '?', keybindings, 'all')[pattern]
modifiers = modifiers == 'none' || modifiers == undefined ? [''] : modifiers
// an alias... for(var i=0; i < modifiers.length; i++){
while(typeof(h) == typeof(1) || typeof(h) == typeof('s')){ var mod = modifiers[i]
if(h in section){
// XXX need to take care of that we can always be a number or a string... //var handler = getKeyHandlers(key, mod, keybindings, pattern)[pattern]
h = section[h] var handler = getKeyHandlers(key, mod, keybindings, 'all')[pattern]
} else if(typeof(h) == typeof(1)) {
h = section[toKeyName(h)] // standard object doc...
if('doc' in handler){
var doc = handler.doc
// lisp style...
} else if(typeof(handler) == typeof([]) && handler.constructor.name == 'Array'){
var doc = handler[1]
// no doc...
} else { } else {
h = section[toKeyCode(h)] if('name' in handler && handler.name != ''){
} var doc = handler.name
}
// no handler...
if(h == null){
doc = 'Nothing'
// a handler with doc (array)...
} else if(typeof(h) == typeof([]) && handler.constructor.name == 'Array'){
doc = h[1]
// complex handler (object)...
} else if(typeof(h) == typeof({})){
// XXX
// simple handler (function)...
} else { } else {
doc = h.doc != null ? h.doc : h // XXX is this the right way to do this?
var doc = handler
}
} }
// push the actual data... // populate the section...
if(doc in res_sec){ // NOTE: we need a list of keys per action...
res_sec[doc].push(key) if(doc in section){
var keys = section[doc]
} else { } else {
res_sec[doc] = [key] var keys = []
} section[doc] = keys
}
// translate shifted keys...
if(shifted_keys != false && mod == 'shift' && key in shifted_keys){
mod = ''
key = shifted_keys[key]
}
// skip anything that is not a key...
if(key.length > 1 && !(key in _KEY_CODES)){
continue
}
keys.push((mod == '' || mod == 'default') ? key : (mod +'+'+ key))
}
} }
} }
return res return res
} }
// Build a basic HTML table with keyboard help...
//
// The table will look like this:
//
// <table class="keyboard-help">
//
// <!-- section head -->
// <tr class="section-title">
// <th colspan=2> SECTION TITLE <th>
// </tr>
// <tr class="section-doc">
// <td colspan=2> SECTION DESCRIPTION <td>
// </tr>
//
// <!-- section keys -->
// <tr>
// <td> KEYS <td>
// <td> ACTION DESCRIPTION <td>
// </tr>
//
// ...
//
// </table>
//
// NOTE: section are not separated in any way other than the <th> element.
// NOTE: the actual HTML is created by jQuery, so the table may get
// slightly structurally changed, i.e. a <tbody> element will be
// added etc.
//
function buildKeybindingsHelpHTML(keybindings){
var doc = buildKeybindingsHelp(keybindings)
var res = '<table class="keyboard-help">'
for(var mode in doc){
if(mode == 'doc'){
continue
}
// section head...
res += ' <tr class="section-title"><th colspan=2>' + mode + '</th></tr>\n'
mode = doc[mode]
res += ' <tr class="section-doc"><td colspan=2>'+ mode.doc + '</td></tr>\n'
// keys...
for(var action in mode){
if(action == 'doc'){
continue
}
res += ' <tr><td>' + mode[action].join(', ') +'</td><td>'+ action + '</td></tr>\n'
}
}
res += '</table>'
return $(res)
}
/**********************************************************************
* Key binding editor...
*/
// XXX
/********************************************************************** /**********************************************************************
* vim:set ts=4 sw=4 : */ * vim:set ts=4 sw=4 : */