started refactoring keyboard.js...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2016-04-03 20:57:03 +03:00
parent c732687433
commit 7756a3b8ca
2 changed files with 65 additions and 24 deletions

View File

@ -22,12 +22,19 @@ var core = require('features/core')
// XXX add this to the global doc...
var GLOBAL_KEYBOARD =
module.GLOBAL_KEYBOARD = {
'Global':{
doc: 'Global bindings that take priority over other sections.',
pattern: '*',
// XXX
},
'Slideshow': {
pattern: '.slideshow-running',
ignore: [
'Esc',
'Up', 'Down', 'Enter',
'R', 'L',
'R', 'L', 'G',
],
Esc: 'toggleSlideshow: "off"',
@ -57,7 +64,13 @@ module.GLOBAL_KEYBOARD = {
Esc: 'toggleSingleImage: "off"',
},
'Global': {
// XXX cropped -- needs a class to indicate a crop...
'Cropped': {
// XXX
},
// XXX cleanup...
'Viewer': {
doc: 'NOTE: binding priority is the same as the order of sections '+
'on this page.',
pattern: '*',
@ -68,6 +81,7 @@ module.GLOBAL_KEYBOARD = {
Q: {
meta: 'close',
},
// XXX
F5: keyboard.doc('Full reload viewer',
function(){
//a.stop()
@ -331,7 +345,8 @@ var KeyboardActions = actions.Actions({
delete this.__keyboard_handler
}
},
['on', 'off'])]
['on', 'off'])],
})
var Keyboard =

View File

@ -224,6 +224,44 @@ function dropRepeatingkeys(handler, max_rate){
}
// supported action format:
// <actio-name>[!][: <args>][-- <doc>]
//
// <args> can contain space seporated:
// - numbers
// - strings
// - non-nested arrays or objects
var parseActionCall =
module.parseActionCall =
function parseActionCall(txt){
// split off the doc...
var c = txt.split('--')
var doc = (c[1] || '').trim()
// the actual code...
c = c[0].split(':')
// action and no default flag...
var action = c[0].trim()
var no_default = action.slice(-1) == '!'
action = no_default ? action.slice(0, -1) : action
// parse arguments...
var args = JSON.parse('['+(
((c[1] || '')
.match(/"[^"]*"|'[^']*'|\{[^\}]*\}|\[[^\]]*\]|\d+|\d+\.\d*/gm)
|| [])
.join(','))+']')
return {
action: action,
arguments: args,
doc: doc,
'no-default': no_default,
}
}
/* Key handler getter
*
* For doc on format see makeKeyboardHandler(...)
@ -387,7 +425,7 @@ function getKeyHandlers(key, modifiers, keybindings, modes, shifted_keys, action
// alias...
// XXX should this be before after or combined with ignore handling...
while( handler != null && typeof(handler) != 'function'){
while(handler != null && typeof(handler) != 'function'){
// do the complex handler aliases...
if(handler != null && handler.constructor == Object){
@ -418,29 +456,12 @@ function getKeyHandlers(key, modifiers, keybindings, modes, shifted_keys, action
handler = keybindings[handler]
// actions...
//
// supported action format:
// <actio-name>[!][: <args>]
//
// <args> can contain space seporated:
// - numbers
// - strings
// - non-nested arrays or arrays
} else if(handler in actions
|| handler.split(/!?\s*:\s*|!/)[0].trim() in actions){
var c = handler.split(':')
handler = c[0].trim()
var no_default = handler.slice(-1) == '!'
handler = no_default ? handler.slice(0, -1) : handler
var c = parseActionCall(handler)
var args = JSON.parse('['+(
((c[1] || '')
.match(/"[^"]*"|'[^']*'|\{[^\}]*\}|\[[^\]]*\]|\d+|\d+\.\d*/gm)
|| [])
.join(','))+']')
handler = function(n, no_default, args){
handler = function(n, no_default, args, doc){
if(no_default){
var f = function(){
event.preventDefault()
@ -459,8 +480,13 @@ function getKeyHandlers(key, modifiers, keybindings, modes, shifted_keys, action
f.args = args
f.no_default = no_default
// use the inherited doc if no specific doc is defined...
if(doc && doc.length > 0){
f.doc = doc
}
return f
}(handler, no_default, args.slice())
}(c.action, c['no-default'], c.args, c.doc)
// key code...
} else if(typeof(handler) == typeof(1)) {