Skip to content
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "presets": ["es2015"] }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll probably want react in your presets here, especially if you use JSX. I'm fine either way and won't bikeshed on the myriad alternatives.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't use JSX for now. I sticked to doing things manually.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, cool!

14 changes: 14 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"rules": {
"semi": 2,
"comma-dangle": 2,
"no-unreachable" : 2
}
}
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ before_install:
- nvm install 5.6
- nvm use 5.6
- npm upgrade -g npm
- npm install
- 'if [[ $GROUP == js* ]]; then npm install -g casperjs; fi'
- git clone --quiet --depth 1 https://github.com/minrk/travis-wheels travis-wheels

Expand Down
99 changes: 91 additions & 8 deletions notebook/static/base/js/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

define([
'base/js/utils',
'underscore',
'underscore'
], function(utils, _) {
"use strict";

Expand Down Expand Up @@ -42,7 +42,7 @@ define([
'backspace': 8, 'tab': 9, 'enter': 13, 'shift': 16, 'ctrl': 17, 'alt': 18,
'meta': 91, 'capslock': 20, 'esc': 27, 'space': 32, 'pageup': 33, 'pagedown': 34,
'end': 35, 'home': 36, 'left': 37, 'up': 38, 'right': 39, 'down': 40,
'insert': 45, 'delete': 46, 'numlock': 144,
'insert': 45, 'delete': 46, 'numlock': 144
};

// These apply to Firefox and Opera
Expand Down Expand Up @@ -168,20 +168,26 @@ define([

// Shortcut manager class

var ShortcutManager = function (delay, events, actions, env) {
var ShortcutManager = function (delay, events, actions, env, config, mode='command') {
/**
* A class to deal with keyboard event and shortcut
*
* @class ShortcutManager
* @constructor
*
* :config: configobjet on which to call `update(....)` to persist the config.
* :mode: mode of this shortcut manager where to persist config.
*/
this._shortcuts = {};
this._defaults_bindings = [];
this.delay = delay || 800; // delay in milliseconds
this.events = events;
this.actions = actions;
this.actions.extend_env(env);
this._queue = [];
this._cleartimeout = null;
this._config = config;
this._mode = mode;
Object.seal(this);
};

Expand Down Expand Up @@ -334,8 +340,30 @@ define([
}
};

ShortcutManager.prototype._set_leaf = function(shortcut_array, action_name, tree){
ShortcutManager.prototype.is_available_shortcut = function(shortcut){
const shortcut_array = shortcut.split(',');
return this._is_available_shortcut(shortcut_array, this._shortcuts);
};

ShortcutManager.prototype._is_available_shortcut = function(shortcut_array, tree){
var current_node = tree[shortcut_array[0]];
if(!shortcut_array[0]){
return false;
}
if(current_node === undefined){
return true;
} else {
if (typeof(current_node) == 'string'){
return false;
} else { // assume is a sub-shortcut tree
return this._is_available_shortcut(shortcut_array.slice(1), current_node);
}
}
};

ShortcutManager.prototype._set_leaf = function(shortcut_array, action_name, tree){
const current_node = tree[shortcut_array[0]];

if(shortcut_array.length === 1){
if(current_node !== undefined && typeof(current_node) !== 'string'){
console.warn('[warning], you are overriting a long shortcut with a shorter one');
Expand All @@ -356,6 +384,51 @@ define([
}
};

ShortcutManager.prototype._persist_shortcut = function(shortcut, data) {
/**
* add a shortcut to this manager and persist it to the config file.
**/
shortcut = shortcut.toLowerCase()
this.add_shortcut(shortcut, data);
const patch = {keys:{}};
const b = {bind:{}}
patch.keys[this._mode] = {bind:{}};
patch.keys[this._mode].bind[shortcut] = data;
this._config.update(patch);
}

ShortcutManager.prototype._persist_remove_shortcut = function(shortcut){
/**
* Remove a shortcut from this manager and persist its removal.
*/

shortcut = shortcut.toLowerCase()
this.remove_shortcut(shortcut);
const patch = {keys:{}};
const b = {bind:{}}
patch.keys[this._mode] = {bind:{}};
patch.keys[this._mode].bind[shortcut] = null;
this._config.update(patch);
console.info(patch)

// if the shortcut we unbind is a default one, we add it to the list of
// things to unbind at startup

if(this._defaults_bindings.indexOf(shortcut) !== -1){
const cnf = (this._config.data.keys||{})[this._mode]
const unbind_array = cnf.unbind||[];

// unless it's already there (like if we have remapped a default
// shortcut to another command, and unbind it)
if(unbind_array.indexOf(shortcut) !== -1){
unbind_array.concat(shortcut);
const unbind_patch = {keys:{unbind:unbind_array}};
this._config._update(unbind_patch)
}
}
}


ShortcutManager.prototype.add_shortcut = function (shortcut, data, suppress_help_update) {
/**
* Add a action to be handled by shortcut manager.
Expand All @@ -369,8 +442,8 @@ define([
if (! action_name){
throw new Error('does not know how to deal with : ' + data);
}
shortcut = normalize_shortcut(shortcut);
this.set_shortcut(shortcut, action_name);
const _shortcut = normalize_shortcut(shortcut);
this.set_shortcut(_shortcut, action_name);

if (!suppress_help_update) {
// update the keyboard shortcuts notebook help
Expand All @@ -391,6 +464,16 @@ define([
this.events.trigger('rebuild.QuickHelp');
};

ShortcutManager.prototype._add_default_shortcuts = function (data) {
/**
* same as add_shortcuts, but register them as "default" that if persistently unbound, with
* persist_remove_shortcut, need to be on the "unbind" list.
**/
this._defaults_bindings = this._defaults_bindings.concat(Object.keys(data))
this.add_shortcuts(data);

}

ShortcutManager.prototype.remove_shortcut = function (shortcut, suppress_help_update) {
/**
* Remove the binding of shortcut `sortcut` with its action.
Expand All @@ -415,7 +498,7 @@ define([
this.events.trigger('rebuild.QuickHelp');
}
} catch (ex) {
throw new Error('trying to remove a non-existent shortcut', shortcut);
throw new Error('trying to remove a non-existent shortcut', shortcut, typeof shortcut);
}
};

Expand Down Expand Up @@ -470,7 +553,7 @@ define([
normalize_key : normalize_key,
normalize_shortcut : normalize_shortcut,
shortcut_to_event : shortcut_to_event,
event_to_shortcut : event_to_shortcut,
event_to_shortcut : event_to_shortcut
};

return keyboard;
Expand Down
1 change: 1 addition & 0 deletions notebook/static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module.exports = {
'notebook/js/outputarea': require('./notebook/js/outputarea.js'),
'notebook/js/celltoolbar': require('./notebook/js/celltoolbar.js'),
'notebook/js/commandpalette': require('./notebook/js/commandpalette.js'),
'notebook/js/shortcuts_editor': require('./notebook/js/shortcuts_editor.js'),
'tree/js/sessionlist': require('./tree/js/sessionlist.js'),
// 'tree/js/main': require('./tree/js/main.js'),
'tree/js/kernellist': require('./tree/js/kernellist.js'),
Expand Down
16 changes: 11 additions & 5 deletions notebook/static/notebook/js/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,17 @@ define(function(require){
*
**/
var _actions = {
'edit-command-mode-keyboard-shortcuts': {
help: 'Open a dialog to edit the command mode keyboard shortcuts',
handler: function (env) {
env.notebook.show_shortcuts_editor();
}
},
'restart-kernel': {
help: 'restart the kernel (no confirmation dialog)',
handler: function (env) {
env.notebook.restart_kernel({confirm: false});
},
}
},
'confirm-restart-kernel':{
icon: 'fa-repeat',
Expand Down Expand Up @@ -479,7 +485,7 @@ define(function(require){
env.pager.collapse();
}
}
},
}
};

/**
Expand Down Expand Up @@ -543,15 +549,15 @@ define(function(require){
event.preventDefault();
}
return env.notebook.scroll_manager.scroll(1);
},
}
},
'scroll-notebook-up': {
handler: function(env, event) {
if(event){
event.preventDefault();
}
return env.notebook.scroll_manager.scroll(-1);
},
}
},
'scroll-cell-center': {
help: "Scroll the current cell to the center",
Expand Down Expand Up @@ -621,7 +627,7 @@ define(function(require){
}
return false;
}
},
}
};

// private stuff that prepend `jupyter-notebook:` to actions names
Expand Down
18 changes: 9 additions & 9 deletions notebook/static/notebook/js/keyboardmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

define([
'base/js/utils',
'base/js/keyboard',
'base/js/keyboard'
], function(utils, keyboard) {
"use strict";

Expand All @@ -36,12 +36,12 @@ define([
this.bind_events();
this.env = {pager:this.pager};
this.actions = options.actions;
this.command_shortcuts = new keyboard.ShortcutManager(undefined, options.events, this.actions, this.env );
this.command_shortcuts.add_shortcuts(this.get_default_common_shortcuts());
this.command_shortcuts.add_shortcuts(this.get_default_command_shortcuts());
this.command_shortcuts = new keyboard.ShortcutManager(undefined, options.events, this.actions, this.env, options.config, 'command');
this.command_shortcuts._add_default_shortcuts(this.get_default_common_shortcuts());
this.command_shortcuts._add_default_shortcuts(this.get_default_command_shortcuts());
this.edit_shortcuts = new keyboard.ShortcutManager(undefined, options.events, this.actions, this.env);
this.edit_shortcuts.add_shortcuts(this.get_default_common_shortcuts());
this.edit_shortcuts.add_shortcuts(this.get_default_edit_shortcuts());
this.edit_shortcuts._add_default_shortcuts(this.get_default_common_shortcuts());
this.edit_shortcuts._add_default_shortcuts(this.get_default_edit_shortcuts());


this.config = options.config;
Expand Down Expand Up @@ -106,7 +106,7 @@ define([
'ctrl-enter' : 'jupyter-notebook:run-cell',
'alt-enter' : 'jupyter-notebook:run-cell-and-insert-below',
// cmd on mac, ctrl otherwise
'cmdtrl-s' : 'jupyter-notebook:save-notebook',
'cmdtrl-s' : 'jupyter-notebook:save-notebook'
};
};

Expand All @@ -117,7 +117,7 @@ define([
'ctrl-m' : 'jupyter-notebook:enter-command-mode',
'up' : 'jupyter-notebook:move-cursor-up',
'down' : 'jupyter-notebook:move-cursor-down',
'ctrl-shift--' : 'jupyter-notebook:split-cell-at-cursor',
'ctrl-shift--' : 'jupyter-notebook:split-cell-at-cursor'
};
};

Expand Down Expand Up @@ -161,7 +161,7 @@ define([
'l' : 'jupyter-notebook:toggle-cell-line-numbers',
'h' : 'jupyter-notebook:show-keyboard-shortcuts',
'z' : 'jupyter-notebook:undo-cell-deletion',
'q' : 'jupyter-notebook:close-pager',
'q' : 'jupyter-notebook:close-pager'
};
};

Expand Down
7 changes: 6 additions & 1 deletion notebook/static/notebook/js/notebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ define(function (require) {
var attachments_celltoolbar = require('notebook/js/celltoolbarpresets/attachments');
var scrollmanager = require('notebook/js/scrollmanager');
var commandpalette = require('notebook/js/commandpalette');
var shortcuts_editor = require('notebook/js/shortcuts_editor');

var _SOFT_SELECTION_CLASS = 'jupyter-soft-selected';

Expand Down Expand Up @@ -248,7 +249,7 @@ define(function (require) {
that.metadata.kernelspec = {
name: data.name,
display_name: data.spec.display_name,
language: data.spec.language,
language: data.spec.language
};
if (!existing_spec || ! _.isEqual(existing_spec, that.metadata.kernelspec)) {
that.set_dirty(true);
Expand Down Expand Up @@ -361,6 +362,10 @@ define(function (require) {
var x = new commandpalette.CommandPalette(this);
};

Notebook.prototype.show_shortcuts_editor = function() {
var x = new shortcuts_editor.ShortcutEditor(this);
};

/**
* Trigger a warning dialog about missing functionality from newer minor versions
*/
Expand Down
18 changes: 14 additions & 4 deletions notebook/static/notebook/js/quickhelp.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,22 @@ define([
return hum;
}

function humanize_shortcut(shortcut){
function _humanize_sequence(sequence){
var joinchar = ',';
var hum = _.map(sequence.replace(/meta/g, 'cmd').split(','), _humanize_shortcut).join(joinchar);
return hum;
}

function _humanize_shortcut(shortcut){
var joinchar = '-';
if (platform === 'MacOS'){
joinchar = '';
}
var sh = _.map(shortcut.split('-'), humanize_key ).join(joinchar);
return '<kbd>'+sh+'</kbd>';
return _.map(shortcut.split('-'), humanize_key ).join(joinchar);
}

function humanize_shortcut(shortcut){
return '<kbd>'+_humanize_shortcut(shortcut)+'</kbd>';
}


Expand Down Expand Up @@ -301,6 +310,7 @@ define([

return {'QuickHelp': QuickHelp,
humanize_shortcut: humanize_shortcut,
humanize_sequence: humanize_sequence
humanize_sequence: humanize_sequence,
_humanize_sequence: _humanize_sequence,
};
});
Loading