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

Make keyboard shortcuts declarative #1234

Merged
merged 4 commits into from
Mar 24, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 8 additions & 2 deletions notebook/static/base/js/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,11 @@ define([
* Remove the binding of shortcut `sortcut` with its action.
* throw an error if trying to remove a non-exiting shortcut
**/
if(!shortcut){
console.warn('trying to remove empty shortcut');
return;

}
shortcut = normalize_shortcut(shortcut);
if( typeof(shortcut) === 'string'){
shortcut = shortcut.split(',');
Expand All @@ -404,14 +409,15 @@ define([
* The shortcut error should be explicit here, because it will be
* seen by users.
*/
try
{
var that = this;
Copy link
Contributor

Choose a reason for hiding this comment

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

I might be missing something but it doesn't look like that is being used anywhere in this function?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, yeah, just had an experiment here that I removed.

try {
this._remove_leaf(shortcut, this._shortcuts);
if (!suppress_help_update) {
// update the keyboard shortcuts notebook help
this.events.trigger('rebuild.QuickHelp');
}
} catch (ex) {
console.warn('shortbut', shortcut, '...',this._shortcuts);
Copy link
Member

Choose a reason for hiding this comment

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

shortbut?

throw new Error('trying to remove a non-existent shortcut', shortcut);
}
};
Expand Down
2 changes: 1 addition & 1 deletion notebook/static/deprecated-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@
"notebookApp['" + modulePath + "']});`"].join(' '));
return notebookApp[modulePath];
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ define([
attachments_preset.push('attachments.edit');

CellToolbar.register_preset('Attachments', attachments_preset, notebook);
console.log('Attachments editing toolbar loaded.');

};
return {'register' : register}
return {'register' : register};
});
1 change: 0 additions & 1 deletion notebook/static/notebook/js/celltoolbarpresets/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ define([
example_preset.push('default.rawedit');

CellToolbar.register_preset('Edit Metadata', example_preset, notebook);
console.log('Default extension for cell metadata editing loaded.');
};
return {'register': register};
});
1 change: 0 additions & 1 deletion notebook/static/notebook/js/celltoolbarpresets/rawcell.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ define([
raw_cell_preset.push('raw_cell.select');

CellToolbar.register_preset('Raw Cell Format', raw_cell_preset, notebook);
console.log('Raw Cell Format toolbar preset loaded.');
};
return {'register': register};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ define([
slideshow_preset.push('slideshow.select');

CellToolbar.register_preset('Slideshow',slideshow_preset, notebook);
console.log('Slideshow extension for metadata editing loaded.');
};
return {'register': register};
});
2 changes: 1 addition & 1 deletion notebook/static/notebook/js/commandpalette.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ define(function(require){
group: ["group", "{{group}} command group"],
searchOnFocus: true,
mustSelectItem: true,
template: '<i class="fa fa-icon {{icon}}"></i>{{display}} <div class="pull-right {{mode_shortcut}}">{{shortcut}}</div>',
template: '<i class="fa fa-icon {{icon}}"></i>{{display}} <div title={{key}} class="pull-right {{mode_shortcut}}">{{shortcut}}</div>',
order: "asc",
source: src,
callback: {
Expand Down
20 changes: 20 additions & 0 deletions notebook/static/notebook/js/keyboardmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ define([
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.config = options.config;
var that = this;

this.config.loaded.then(function(){

(((that.config.data.keys||{}).edit||{})
Copy link
Member

Choose a reason for hiding this comment

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

This is extremely dense, and I can't really follow where the different parentheses start and stop. Maybe expand it a little bit?

Copy link
Member Author

Choose a reason for hiding this comment

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

expanded, but that look a bit ugly for just avoiding typeerror if one of them is undefined:

 +            try {
 +                edit_unbind = that.config.data.keys.edit.unbind;
 +            } catch (e) {
 +                if (e instanceof TypeError) {
 +                    edit_unbind = [];
 +                } else {
 +                    throw e;
 +                }
 +            }

.unbind||[])
.forEach(function(u){that.edit_shortcuts.remove_shortcut(u)});
(((that.config.data.keys||{}).command||{})
.unbind||[])
.forEach(function(u){that.command_shortcuts.remove_shortcut(u)});

that.command_shortcuts.add_shortcuts( ((that.config.data.keys||{}).command||{}).bind);
that.edit_shortcuts.add_shortcuts( ((that.config.data.keys||{}).edit ||{}).bind);

}
);

Object.seal(this);
};

Expand Down
4 changes: 3 additions & 1 deletion notebook/static/notebook/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ require([
var keyboard_manager = new keyboardmanager.KeyboardManager({
pager: pager,
events: events,
actions: acts });
actions: acts,
config: config_section,
});
var save_widget = new savewidget.SaveWidget('span#save_widget', {
events: events,
keyboard_manager: keyboard_manager});
Expand Down
3 changes: 2 additions & 1 deletion notebook/static/notebook/js/notebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ define(function (require) {
Object.seal(this);
};


Notebook.options_default = {
// can be any cell type, or the special values of
// 'above', 'below', or 'selected' to get the value from another cell.
Expand Down Expand Up @@ -2410,7 +2411,7 @@ define(function (require) {
var cell = cells[i];
cell.remove_unused_attachments();
}
}
};

/**
* Load a notebook from JSON (.ipynb).
Expand Down
2 changes: 1 addition & 1 deletion notebook/static/notebook/js/quickhelp.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ define([
doc.append(
'The Jupyter Notebook has two different keyboard input modes. <b>Edit mode</b> '+
'allows you to type code/text into a cell and is indicated by a green cell '+
'border. <b>Command mode</b> binds the keyboard to notebook level actions '+
'border. <b>Command mode</b> binds the keyboard to notebook level commands '+
'and is indicated by a grey cell border with a blue left margin.'
);
element.append(doc);
Expand Down
5 changes: 3 additions & 2 deletions notebook/static/notebook/less/commandpalette.less
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ ul.typeahead-list {
}

.no-shortcut{
display:none;
min-width: 20px;
color: transparent;
}

.command-shortcut:before{
content:"(command)";
content:"(command mode)";
padding-right:3px;
color:@gray-light;
}
Expand Down
4 changes: 2 additions & 2 deletions notebook/static/services/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function(utils) {
if (this.classname) {
return this.section.data[this.classname] || {};
} else {
return this.section.data
return this.section.data;
}
};

Expand All @@ -92,7 +92,7 @@ function(utils) {
ConfigWithDefaults.prototype.get = function(key) {
var that = this;
return this.section.loaded.then(function() {
return that._class_data()[key] || that.defaults[key]
return that._class_data()[key] || that.defaults[key];
});
};

Expand Down