mirror of
https://github.com/wassname/jupyter_contrib_nbextensions.git
synced 2026-08-11 11:19:52 +08:00
68 lines
1.9 KiB
JavaScript
68 lines
1.9 KiB
JavaScript
// assign dedent to shift-tab
|
|
|
|
var shift_tab_extension = (function() {
|
|
|
|
var dedentKey = { "Shift-Tab":"indentLess" };
|
|
var key = IPython.keyboard.keycodes;
|
|
|
|
/**
|
|
* Concatenate associative array objects
|
|
*
|
|
* Source: http://stackoverflow.com/questions/2454295/javascript-concatenate-properties-from-multiple-objects-associative-array
|
|
*/
|
|
function collect() {
|
|
var ret = {};
|
|
var len = arguments.length;
|
|
for (var i=0; i<len; i++) {
|
|
for (p in arguments[i]) {
|
|
if (arguments[i].hasOwnProperty(p)) {
|
|
ret[p] = arguments[i][p];
|
|
}
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* Intercept codemirror onKeyEvent in codecell
|
|
*
|
|
* @return {Boolean} returns false if hotkey is found, otherwise call original function
|
|
*/
|
|
var intercept_codemirror_keyevent = function (cm, event) {
|
|
/* Dummy for shift+Tab, who knows why */
|
|
if (event.type == 'keydown' && event.which == key.tab && event.shiftKey) {
|
|
return false;
|
|
};
|
|
return this.handle_codemirror_keyevent(cm,event);
|
|
}
|
|
|
|
function assign_key(cell) {
|
|
var keys = cell.code_mirror.getOption('extraKeys');
|
|
cell.code_mirror.setOption('onKeyEvent',$.proxy(intercept_codemirror_keyevent,cell));
|
|
cell.code_mirror.setOption('extraKeys', collect(keys, dedentKey ));
|
|
}
|
|
|
|
/**
|
|
* Initialize newly created cell
|
|
*
|
|
* @param {Object} event
|
|
* @param {Object} nbcell notebook cell
|
|
*/
|
|
create_cell = function (event,nbcell,nbindex) {
|
|
var cell = nbcell.cell;
|
|
if ((cell instanceof IPython.CodeCell)) { assign_key(cell); }
|
|
};
|
|
|
|
/**
|
|
* Initialize all cells
|
|
*
|
|
*/
|
|
var cells = IPython.notebook.get_cells();
|
|
for(var i in cells){
|
|
var cell = cells[i];
|
|
if ((cell instanceof IPython.CodeCell)) { assign_key(cell); }
|
|
};
|
|
|
|
$([IPython.events]).on('create.Cell',create_cell);
|
|
})();
|