Add a keybinding behavior for operations

This commit is contained in:
Bryan Housel
2016-12-19 16:55:11 -05:00
parent 82665a6883
commit 1884c7070e
2 changed files with 41 additions and 0 deletions
+1
View File
@@ -8,6 +8,7 @@ export { behaviorEdit } from './edit';
export { behaviorHash } from './hash';
export { behaviorHover } from './hover';
export { behaviorLasso } from './lasso';
export { behaviorOperation } from './operation';
export { behaviorPaste } from './paste';
export { behaviorSelect } from './select';
export { behaviorTail } from './tail';
+40
View File
@@ -0,0 +1,40 @@
import * as d3 from 'd3';
import { d3keybinding } from '../lib/d3.keybinding.js';
/* Creates a keybinding behavior for an operation */
export function behaviorOperation(context) {
var which, keybinding;
var behavior = function () {
if (which) {
keybinding = d3keybinding('behavior.key.' + which.id);
keybinding.on(which.keys, function() {
d3.event.preventDefault();
if (!(context.inIntro() || which.disabled())) {
which();
}
});
d3.select(document).call(keybinding);
}
return behavior;
};
behavior.off = function() {
if (keybinding) {
d3.select(document).call(keybinding.off);
}
};
behavior.which = function (_) {
if (!arguments.length) return which;
which = _;
return behavior;
};
return behavior;
}