Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce prefixes in bindAll #296

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion Slate/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,52 @@
throw "bind failed, second parameter must be an operation or a function. was: "+callback;
},

bindAll: function(bindMap) {
/*
* Bind all mappings.
*
* prefixMap - an optional object of key prefix mappings.
* bindMap - an object of key -> operation mappings.
*
* In Slate config (non-JS), you can just "bind a:${someprefix} relaunch".
* In JS version of config, however, you cannot, because you can't use
* expressions in the key part of object literal in JS, it becomes rather
* cumbersome to repeat same prefix in every mapping. To address these
* issues, you can pass an object of prefix mappings to `bindAll` along
* with the bind mappings. In bind mappings then, you can use hash symbol
* and name of your prefix as a placeholder.
*
* Examples
*
* // simple mapping
* slate.bindAll({ "esc:ctrl;alt" : S.op("relaunch") });
*
* // with prefixes
* slate.bindAll({ layoutKeys: "ctrl;cmd" }, {
* "l:#layoutKeys": laptop,
* "t:#layoutKeys": thunderbolt,
* "b:#layoutKeys": twoMonitor
* });
*/
bindAll: function(prefixMap, bindMap) {
if (prefixMap && bindMap === undefined) {
bindMap = prefixMap;
prefixMap = null;
} else {
_bindMap = bindMap;
bindMap = {};

for(key in _bindMap) {
var match, newKey = key;

if (match = key.match(/.*:\s*#(.*)/)) {
val = prefixMap[match[1]];
newKey = key.replace("#" + match[1], val).replace(/\s+/, '');
}

bindMap[newKey] = _bindMap[key];
}
}

for(key in bindMap) {
if (_.isArray(bindMap[key]) && _.size(bindMap[key]) >= 2) {
slate.bind(key, bindMap[key][0], bindMap[key][1]);
Expand Down