refactoring and cleanup...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-09-23 21:11:19 +03:00
parent ffe8c86a12
commit 5ef2cd18af
2 changed files with 86 additions and 101 deletions

View File

@ -3014,7 +3014,7 @@ var FileSystemWriterUIActions = actions.Actions({
// need to: // need to:
// - pass values to export dialog (load) // - pass values to export dialog (load)
// - pass values to export action (use) // - pass values to export action (use)
exportPresets: ['- File/', exportPresets: ['- File/Export history...',
widgets.makeUIDialog(function(mode){ widgets.makeUIDialog(function(mode){
var that = this var that = this
@ -3047,12 +3047,12 @@ var FileSystemWriterUIActions = actions.Actions({
// XXX // XXX
}], }],
// XXX not sure about the default icon... // XXX not sure about the default icon...
'TO_TOP', ['T', 'TO_TOP'],
'REMOVE', 'REMOVE',
], ],
// XXX export... // XXX export...
open: function(evt, title){ open: function(evt, title){
console.log('###', title console.log('###', title)
}, },
// XXX handle rename -> update index... // XXX handle rename -> update index...
// XXX // XXX
@ -3087,7 +3087,7 @@ var FileSystemWriterUIActions = actions.Actions({
], ],
// XXX export... // XXX export...
open: function(evt, title){ open: function(evt, title){
console.log('###', title console.log('###', title)
}, },
}) })
}) })

View File

@ -213,8 +213,7 @@ function parseActionCall(txt, context){
doc: doc, doc: doc,
no_default: no_default, no_default: no_default,
stop_propagation: false, stop_propagation: false,
} } }
}
@ -250,8 +249,7 @@ function event2key(evt){
// pressed a modifier key... // pressed a modifier key...
key.indexOf(k.toLowerCase()) < 0 && key.push(k) key.indexOf(k.toLowerCase()) < 0 && key.push(k)
return key return key }
}
// Get key code from key name... // Get key code from key name...
@ -329,8 +327,7 @@ function normalizeKey(key){
// special case: got a number... // special case: got a number...
if(typeof(key) == typeof(123)){ if(typeof(key) == typeof(123)){
return code2key(key) return code2key(key) }
}
// sort modifiers via .modifiers and keep the key last... // sort modifiers via .modifiers and keep the key last...
key = splitKey(key) key = splitKey(key)
@ -346,8 +343,7 @@ function normalizeKey(key){
k = parseInt(k) ? code2key(parseInt(k)) : k k = parseInt(k) ? code2key(parseInt(k)) : k
if(!k){ if(!k){
return k return k }
}
// get the propper name... // get the propper name...
k = SPECIAL_KEY_ALTERNATIVE_TITLES[k.toLowerCase()] || k k = SPECIAL_KEY_ALTERNATIVE_TITLES[k.toLowerCase()] || k
@ -510,9 +506,7 @@ var KeyboardPrototype = {
if(this.__keyboard instanceof Function){ if(this.__keyboard instanceof Function){
this.__keyboard(value) this.__keyboard(value)
} else { } else {
this.__keyboard = value this.__keyboard = value } },
}
},
// XXX is this needed??? // XXX is this needed???
//context: null, //context: null,
@ -603,8 +597,7 @@ var KeyboardPrototype = {
this.keyboard = ordered this.keyboard = ordered
} }
return this return this },
},
// Get keys for handler... // Get keys for handler...
// //
@ -672,10 +665,7 @@ var KeyboardPrototype = {
.unique())) .unique()))
res.indexOf(k) < 0 res.indexOf(k) < 0
&& res.push(k) && res.push(k)
&& walkAliases(res, rev, bindings, k, mod) && walkAliases(res, rev, bindings, k, mod) }) } }
})
}
}
var modes = handler == '?' ? this.modes() : '*' var modes = handler == '?' ? this.modes() : '*'
var drop = [] var drop = []
@ -684,14 +674,12 @@ var KeyboardPrototype = {
Object.keys(keyboard).forEach(function(mode){ Object.keys(keyboard).forEach(function(mode){
// skip non-applicable modes... // skip non-applicable modes...
if(modes != '*' && modes.indexOf(mode) < 0){ if(modes != '*' && modes.indexOf(mode) < 0){
return return }
}
var bindings = keyboard[mode] var bindings = keyboard[mode]
if(handler == '?'){ if(handler == '?'){
next = next.concat(bindings.NEXT || []) next = next.concat(bindings.NEXT || []) }
}
// build a reverse index... // build a reverse index...
var rev = {} var rev = {}
@ -706,8 +694,7 @@ var KeyboardPrototype = {
: (h.doc || h.name) : (h.doc || h.name)
rev[h] = (rev[h] || []) rev[h] = (rev[h] || [])
.concat((rev[bindings[key]] || []).concat([key])) .concat((rev[bindings[key]] || []).concat([key]))
.unique() .unique() })
})
var seen = [] var seen = []
var handlers = handler == '*' || handler == '?' ? var handlers = handler == '*' || handler == '?' ?
@ -729,8 +716,7 @@ var KeyboardPrototype = {
drop = drop == '*' ? '*' : drop.concat(keys) drop = drop == '*' ? '*' : drop.concat(keys)
next = next next = next
.filter(function(k){ return keys.indexOf(k) >= 0 }) .filter(function(k){ return keys.indexOf(k) >= 0 })
return return }
}
var keys = (rev[h] || []).map(that.normalizeKey.bind(that)) var keys = (rev[h] || []).map(that.normalizeKey.bind(that))
@ -759,25 +745,20 @@ var KeyboardPrototype = {
|| next.indexOf(k) >= 0 || next.indexOf(k) >= 0
|| (drop != '*' || (drop != '*'
&& drop.indexOf(key) < 0 && drop.indexOf(key) < 0
&& drop.indexOf(k) < 0) && drop.indexOf(k) < 0) }) }
})
}
if(keys.length > 0){ if(keys.length > 0){
var m = res[mode] = res[mode] || {} var m = res[mode] = res[mode] || {}
m[h] = keys m[h] = keys }
}
}) })
if(handler == '?'){ if(handler == '?'){
drop = drop == '*' || bindings.drop == '*' ? drop = drop == '*' || bindings.drop == '*' ?
'*' '*'
: drop.concat(bindings.drop || []) : drop.concat(bindings.drop || []) }
}
}) })
return res return res },
},
// Get/set/unset handler for key... // Get/set/unset handler for key...
// //
@ -870,12 +851,10 @@ var KeyboardPrototype = {
var key_separators = KEY_SEPARATORS var key_separators = KEY_SEPARATORS
if(mode == null){ if(mode == null){
return null return null }
}
if(key == null && this.isKey(mode)){ if(key == null && this.isKey(mode)){
key = mode key = mode
mode = '*' mode = '*' }
}
var joinKeys = function(key, shift_key){ var joinKeys = function(key, shift_key){
// match candidates... // match candidates...
@ -898,8 +877,7 @@ var KeyboardPrototype = {
var keyCombinations = function(key, shift_key, remove_single_keys){ var keyCombinations = function(key, shift_key, remove_single_keys){
if(key.length <= 1){ if(key.length <= 1){
//return shift_key ? [key, shift_key] : [key] //return shift_key ? [key, shift_key] : [key]
return key return key }
}
var k = remove_single_keys ? 1 : 0 var k = remove_single_keys ? 1 : 0
// generate recursively, breadth first... // generate recursively, breadth first...
var _combinations = function(level, res){ var _combinations = function(level, res){
@ -918,19 +896,15 @@ var KeyboardPrototype = {
// as they are searched separately... // as they are searched separately...
//s.length > 0 //s.length > 0
s.length > k s.length > k
&& next.push(s) && next.push(s) }) })
})
})
next.length > 0 next.length > 0
&& _combinations(next, res) && _combinations(next, res)
return res return res }
}
return _combinations(shift_key && shift_key.length > 0 ? return _combinations(shift_key && shift_key.length > 0 ?
[key, shift_key] [key, shift_key]
: [key], []) : [key], [])
.map(function(e){ return joinKeys(e.split(/\+\+\+/g)) }) .map(function(e){ return joinKeys(e.split(/\+\+\+/g)) })
.reduce(function(a, b){ return a.concat(b) }, []) .reduce(function(a, b){ return a.concat(b) }, []) }
}
var walkAliases = function(bindings, handler, modifiers){ var walkAliases = function(bindings, handler, modifiers){
var seen = [] var seen = []
var modifiers = modifiers || [] var modifiers = modifiers || []
@ -950,13 +924,10 @@ var KeyboardPrototype = {
// check for loops... // check for loops...
if(seen.indexOf(handler) >= 0){ if(seen.indexOf(handler) >= 0){
handler = null handler = null
break break }
} seen.push(handler) }
seen.push(handler)
}
return handler return handler }
}
key = this.normalizeKey(this.splitKey(key)) key = this.normalizeKey(this.splitKey(key))
var shift_key = this.shifted(key) var shift_key = this.shifted(key)
@ -1001,18 +972,15 @@ var KeyboardPrototype = {
handler = walkAliases( handler = walkAliases(
bindings, bindings,
[k, c].filter(function(k){ return bindings[k] })[0], [k, c].filter(function(k){ return bindings[k] })[0],
mod) mod) }
}
// explicit DROP -- ignore next sections... // explicit DROP -- ignore next sections...
if(drop && handler == 'DROP'){ if(drop && handler == 'DROP'){
break break }
}
// we got a match... // we got a match...
if(handler){ if(handler){
res[m] = handler res[m] = handler }
}
// if key in .drop then ignore the rest... // if key in .drop then ignore the rest...
if(drop if(drop
@ -1027,9 +995,7 @@ var KeyboardPrototype = {
// between X meaning drop only X and drop // between X meaning drop only X and drop
// all combos with X... // all combos with X...
|| (bindings.drop || []).indexOf(k) >= 0)){ || (bindings.drop || []).indexOf(k) >= 0)){
break break } }
}
}
return (typeof(mode) == typeof('str') return (typeof(mode) == typeof('str')
&& ['*', 'test', '?'].indexOf(mode) < 0) ? && ['*', 'test', '?'].indexOf(mode) < 0) ?
@ -1049,13 +1015,20 @@ var KeyboardPrototype = {
// set handler if given... // set handler if given...
if(handler && handler != ''){ if(handler && handler != ''){
keyboard[mode][that.joinKey(key)] = handler keyboard[mode][that.joinKey(key)] = handler } }) }
}
}) return this },
}
// Trigger handler...
//
// XXX can this be implemented here???
// ...we need context among other things...
// might be a good idea to add config options for everything
// and just set things up in wrappers...
handle: function(mode, key){
// XXX
return this },
return this
},
// get applicable modes... // get applicable modes...
// //
@ -1071,14 +1044,42 @@ var KeyboardPrototype = {
: Object.keys(this.keyboard) }, : Object.keys(this.keyboard) },
// XXX EXPERIMENTAL...
// Basic binding editing API...
//
// NOTE: this is an event-like proxy to the .handler(..)
//
// Bind handler key (top section)...
// .on(key, handler)
// -> this
//
// .on(key, section, handler)
// -> this
//
on: function(key, handler){
// normalize args...
if(arguments.length == 3){
[key, mode, handler] = arguments
} else {
[key, handler] = arguments }
mode = mode
|| Object.keys(this.keyboard)[0]
// bind...
return this.handler(mode, key, handler) },
off: function(key, mode){
// normalize args...
mode = mode
|| Object.keys(this.keyboard)[0]
// unbind...
return this.handler(mode, key, null) },
// init base data... // init base data...
__init__: function(keyboard, is_mode_applicable){ __init__: function(keyboard, is_mode_applicable){
this.keyboard = keyboard this.keyboard = keyboard
if(is_mode_applicable instanceof Function){ if(is_mode_applicable instanceof Function){
this.isModeApplicable = is_mode_applicable this.isModeApplicable = is_mode_applicable } },
}
},
} }
var Keyboard = var Keyboard =
@ -1108,8 +1109,7 @@ var KeyboardWithCSSModesPrototype = {
|| (context.matches ? || (context.matches ?
(context.matches(pattern) (context.matches(pattern)
|| !!context.querySelector(pattern)) || !!context.querySelector(pattern))
: false) : false) },
},
__init__: function(keyboard, context){ __init__: function(keyboard, context){
object.parentCall(KeyboardWithCSSModesPrototype.__init__, this, keyboard) object.parentCall(KeyboardWithCSSModesPrototype.__init__, this, keyboard)
@ -1120,9 +1120,7 @@ var KeyboardWithCSSModesPrototype = {
}) })
} else { } else {
this.context = context this.context = context } },
}
},
} }
var KeyboardWithCSSModes = var KeyboardWithCSSModes =
@ -1167,7 +1165,6 @@ KeyboardWithCSSModes.prototype.__proto__ = Keyboard.prototype
var makeKeyboardHandler = var makeKeyboardHandler =
module.makeKeyboardHandler = module.makeKeyboardHandler =
function makeKeyboardHandler(keyboard, unhandled, actions){ function makeKeyboardHandler(keyboard, unhandled, actions){
var kb = keyboard instanceof Keyboard ? var kb = keyboard instanceof Keyboard ?
keyboard keyboard
//: Keyboard(keyboard, checkGlobalMode) //: Keyboard(keyboard, checkGlobalMode)
@ -1192,8 +1189,7 @@ function makeKeyboardHandler(keyboard, unhandled, actions){
Object.keys(handlers).forEach(function(mode){ Object.keys(handlers).forEach(function(mode){
if(res === false){ if(res === false){
return return }
}
var handler = handlers[mode] var handler = handlers[mode]
@ -1224,17 +1220,13 @@ function makeKeyboardHandler(keyboard, unhandled, actions){
evt evt
&& h.stop_propagation && h.stop_propagation
&& evt.stopPropagation() && evt.stopPropagation()
&& (res = false) && (res = false) } } })
}
}
})
no_match no_match
&& !did_handling && !did_handling
&& no_match.call(actions, evt, key) && no_match.call(actions, evt, key)
return res return res }
}
} }
@ -1269,8 +1261,7 @@ function makePausableKeyboardHandler(keyboard, unhandled, actions, check_interva
}, },
(check_interval instanceof Function ? (check_interval instanceof Function ?
check_interval.call(actions) check_interval.call(actions)
: (check_interval || 100))) : (check_interval || 100))) }
}
return stoppableKeyboardRepeat( return stoppableKeyboardRepeat(
@ -1278,11 +1269,8 @@ function makePausableKeyboardHandler(keyboard, unhandled, actions, check_interva
function(){ function(){
if(kb.__repeat_pause_timeout){ if(kb.__repeat_pause_timeout){
kb.pauseRepeat() kb.pauseRepeat()
return false return false }
} return true }) }
return true
})
}
@ -1324,8 +1312,7 @@ function dropRepeatingkeys(handler, max_rate){
&& meta == evt.metaKey && meta == evt.metaKey
&& alt == evt.altKey && alt == evt.altKey
&& shift == evt.shiftKey){ && shift == evt.shiftKey){
return return }
}
key = evt.keyCode key = evt.keyCode
ctrl = evt.ctrlKey ctrl = evt.ctrlKey
@ -1339,9 +1326,7 @@ function dropRepeatingkeys(handler, max_rate){
// XXX is this the right way to go??? // XXX is this the right way to go???
typeof(max_rate) == typeof(123) ? max_rate : max_rate()) typeof(max_rate) == typeof(123) ? max_rate : max_rate())
return handler(evt) return handler(evt) } }
}
}