fixed a bug in keyboard.js and made the introspection better...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2014-01-18 11:26:29 +04:00
parent 57e922c59c
commit 6ffd4e1e84
2 changed files with 108 additions and 44 deletions

View File

@ -39,7 +39,8 @@ function directionImage(reverse){
// XXX this is experimental...
// ...not sure yet how to go about this...
function Action(text, func){
func = func == null ? function(){return true}: func
var not_action = func === false ? true : false
func = !func ? function(){return true}: func
func.doc = text
var name = text.split('\n')[0].trim()
@ -48,12 +49,17 @@ function Action(text, func){
console.warn('Action: "'+name+'" is defined more than once.')
}
ACTIONS[name] = func
if(!not_action){
ACTIONS[name] = func
}
return func
}
doc = Action
var _doc = doc
var doc = Action
/*********************************************************************/
@ -119,17 +125,21 @@ var KEYBOARD_CONFIG = {
'image selection/marks.',
pattern: '.overlay-info:hover',
ignore: [ 'A' ],
ignore: [ 'A', 'C', 'D' ],
// NOTE: these are here only for documentation...
A: {
// NOTE: this is here only for documentation...
ctrl: doc('Select all'),
ctrl: doc('Select all', false),
},
C: {
ctrl: doc('Copy selection', false)
},
D: {
ctrl: doc('Clear selection',
function(){
console.log('!!!')
document.getSelection().empty()
return false
})
}
},
@ -145,7 +155,7 @@ var KEYBOARD_CONFIG = {
ignore: '*',
'insert-return': doc('Insert return'),
'insert-return': doc('Insert return', false),
Enter: {
default: doc('Accept dialog',
@ -387,16 +397,16 @@ var KEYBOARD_CONFIG = {
},
// zooming...
'#1': doc('Fit one image', function(){ fitNImages(1) }),
'#2': doc('Fit two images', function(){ fitNImages(2) }),
'#3': doc('Fit three images', function(){ fitNImages(3) }),
'#4': doc('Fit four images', function(){ fitNImages(4) }),
'#5': doc('Fit five images', function(){ fitNImages(5) }),
'#6': doc('Fit six images', function(){ fitNImages(6) }),
'#7': doc('Fit seven images', function(){ fitNImages(7) }),
'#8': doc('Fit eight images', function(){ fitNImages(8) }),
'#9': doc('Fit nine images', function(){ fitNImages(9) }),
'#0': doc('Fit maximum images', function(){ fitNImages(getScreenWidthInImages(CONFIG.min_image_size)) }),
'#1': doc('Fit one image', function(){ return !fitNImages(1) }),
'#2': doc('Fit two images', function(){ return !fitNImages(2) }),
'#3': doc('Fit three images', function(){ return !fitNImages(3) }),
'#4': doc('Fit four images', function(){ return !fitNImages(4) }),
'#5': doc('Fit five images', function(){ return !fitNImages(5) }),
'#6': doc('Fit six images', function(){ return !fitNImages(6) }),
'#7': doc('Fit seven images', function(){ return !fitNImages(7) }),
'#8': doc('Fit eight images', function(){ return !fitNImages(8) }),
'#9': doc('Fit nine images', function(){ return !fitNImages(9) }),
'#0': doc('Fit maximum images', function(){ return !fitNImages(getScreenWidthInImages(CONFIG.min_image_size)) }),
// cropping...
C: doc('Show ribbon crop dialog', cropImagesDialog),
@ -866,6 +876,10 @@ var KEYBOARD_CONFIG = {
'ctrl+alt': function(){ alert('ctrl-alt-`') },
},
*/
},
'.image': {
'#1': doc('mooo!')
}
}

View File

@ -10,6 +10,17 @@
/*********************************************************************/
// Attributes to be ignored my the key handler...
//
// These are used for system tasks.
var KEYBOARD_SYSTEM_ATTRS = [
'doc',
'title',
'ignore',
'pattern'
]
// Neither _SPECIAL_KEYS nor _KEY_CODES are meant for direct access, use
// toKeyName(<code>) and toKeyCode(<name>) for a more uniform access.
//
@ -109,7 +120,7 @@ function toKeyCode(c){
// documentation wrapper...
function doc(text, func){
func = func == null ? function(){return true}: func
func = !func ? function(){return true}: func
func.doc = text
return func
}
@ -152,12 +163,6 @@ function normalizeModifiers(c, a, s){
*
* 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
@ -171,6 +176,13 @@ function normalizeModifiers(c, a, s){
* NOTE: normalizeModifiers(...) can be used as
* a reference, if in doubt.
*
* 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
*
*
* 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).
@ -183,6 +195,19 @@ function normalizeModifiers(c, a, s){
* }
*
*
* <handler> can be:
* - <function> - handler
* - [<doc>, <function>]
* - lisp-style handler
* - 'IGNORE' - if mode is 'all' and key is in .ignore
* - [<function>, 'IGNORE NEXT']
* - if mode is 'all' and the key is both in .ignore
* and a handler is defined in the current section
* NOTE: in this case if this mode matches, all
* the subsequent handlers will get ignored
* in normal modes...
*
*
* 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.
@ -191,17 +216,16 @@ function normalizeModifiers(c, a, s){
* NOTE: modifiers can be a list of three bools...
* (see: normalizeModifiers(...) for further information)
*
* 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
// XXX I do not understand why this is here...
var did_handling = false
var did_ignore = false
modifiers = modifiers == null ? '' : modifiers
modifiers = modifiers != '?' ? normalizeModifiers(modifiers) : modifiers
modes = modes == null ? 'any' : modes
@ -224,6 +248,24 @@ function getKeyHandlers(key, modifiers, keybindings, modes, shifted_keys){
for(var title in keybindings){
// If a key is ignored then look no further...
/*
if(did_ignore && modes != 'all'){
break
}
*/
if(did_ignore){
if(modes != 'all'){
break
} else {
did_ignore = false
// XXX do we actually need this???
if(modifiers != '?' && res[mode] != 'IGNORE'){
res[mode] = [ res[mode], 'IGNORE NEXT']
}
}
}
// older version compatibility...
if(keybindings[title].pattern != null){
var mode = keybindings[title].pattern
@ -237,7 +279,7 @@ function getKeyHandlers(key, modifiers, keybindings, modes, shifted_keys){
|| modes == mode
// 'any' means we need to check the mode...
|| (modes == 'any'
// '*' allways matches...
// '*' always matches...
&& mode == '*'
// match the mode...
|| $(mode).length != 0))){
@ -257,6 +299,7 @@ function getKeyHandlers(key, modifiers, keybindings, modes, shifted_keys){
}
// alias...
// XXX should this be before after or combined with ignore handling...
while( handler != null
&& (typeof(handler) == typeof(123)
|| typeof(handler) == typeof('str')
@ -293,21 +336,24 @@ function getKeyHandlers(key, modifiers, keybindings, modes, shifted_keys){
}
}
// 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){
// NOTE: if a handler is defined in this section, this
// will be overwritten...
// XXX need to add the handler to this if it's defined...
res[mode] = 'IGNORE'
}
did_ignore = true
}
// 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
}
@ -531,7 +577,7 @@ function buildKeybindingsHelp(keybindings, shifted_keys){
// handlers...
for(var key in mode){
if(key == 'doc' || key == 'title' || key == 'ignore' || key == 'pattern'){
if(KEYBOARD_SYSTEM_ATTRS.indexOf(key) >= 0){
continue
}
var modifiers = getKeyHandlers(key, '?', keybindings, 'all')[pattern]
@ -542,12 +588,16 @@ function buildKeybindingsHelp(keybindings, shifted_keys){
var handler = getKeyHandlers(key, mod, keybindings, 'all')[pattern]
if(handler.constructor.name == 'Array' && handler[1] == 'IGNORE NEXT'){
handler = handler[0]
}
// standard object doc...
if('doc' in handler){
var doc = handler.doc
// lisp style...
} else if(typeof(handler) == typeof([]) && handler.constructor.name == 'Array'){
} else if(handler.constructor.name == 'Array'){
var doc = handler[1]
// no doc...