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
[POC] Shortcut editor (for command mode) #1265
Closed
Closed
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1ae42a4
Start working on ShortcutEditor
Carreau 22b159f
Move toward componentDidMount
Carreau 6b5f979
'Working addition'
Carreau b8cdea7
'dev deps not deps'
Carreau c765548
cleanup things
Carreau 7564623
'whyyyyy'
Carreau e0e02ce
'cleanup'
Carreau aacc1fe
'add ability to require'
Carreau 97007b9
'moar cleanup'
Carreau 64fc6d7
'react not existing'
Carreau 4e99146
'perisit unbind'
Carreau ac45f24
'more persistance'
Carreau File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "presets": ["es2015"] } | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// Copyright (c) Jupyter Development Team. | ||
// Distributed under the terms of the Modified BSD License. | ||
|
||
define(function(require){ | ||
"use strict"; | ||
|
||
const QH = require("notebook/js/quickhelp"); | ||
const dialog = require("base/js/dialog"); | ||
const React = require("React"); | ||
const ReactDom = require("react-dom"); | ||
|
||
/** | ||
* Humanize the action name to be consumed by user. | ||
* internaly the actions anem are of the form | ||
* <namespace>:<description-with-dashes> | ||
* we drop <namesapce> and replace dashes for space. | ||
*/ | ||
const humanize_action_id = function(str) { | ||
return str.split(':')[1].replace(/-/g, ' ').replace(/_/g, '-'); | ||
}; | ||
|
||
/** | ||
* given an action id return 'command-shortcut', 'edit-shortcut' or 'no-shortcut' | ||
* for the action. This allows us to tag UI in order to visually distinguish | ||
* wether an action have a keybinding or not. | ||
**/ | ||
|
||
const KeyBinding = React.createClass({ | ||
displayName: 'KeyBindings', | ||
getInitialState: function() { | ||
return {shrt:''}; | ||
}, | ||
handleShrtChange: function (element){ | ||
this.setState({shrt:element.target.value}); | ||
}, | ||
render: function(){ | ||
const that = this; | ||
const av = this.props.available(this.state.shrt); | ||
return React.createElement('div',{style:{borderBottom: '1px solid gray'}, className:'jupyter-keybindings'}, | ||
this.props.shortcut? | ||
React.createElement('i', {className: "pull-right fa fa-times", alt: 'remove title'+this.props.shortcut, | ||
onClick:()=>{ | ||
console.log('will unbind ::', this.props.shortcuts, this.props); | ||
that.props.unbind(this.props.shortcut); | ||
}}): | ||
React.createElement('i', {className: "pull-right fa fa-plus", alt: 'add-keyboard-shortcut', | ||
onClick:()=>{ | ||
av?that.props.onAddBindings(that.state.shrt, that.props.ckey):undefined; | ||
} | ||
}), | ||
this.props.shortcut? undefined : | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When you don't want an element to show, return |
||
React.createElement('input', {type:'text', placeholder:'add shortcut', | ||
className:'pull-right'+(av?'':' alert alert-danger'), value:this.state.shrt, onChange:this.handleShrtChange | ||
}), | ||
this.props.shortcut? React.createElement('span', {className: 'pull-right'}, React.createElement('kbd', {}, this.props.shortcut)): undefined, | ||
React.createElement('div', {title: '(' +this.props.ckey + ')' , className:'jupyter-keybindings-text'}, this.props.display ) | ||
); | ||
} | ||
}); | ||
|
||
const KeyBindingList = React.createClass({ | ||
displayName: 'KeyBindingList', | ||
getInitialState: function(){ | ||
return {data:[]}; | ||
}, | ||
componentDidMount: function(){ | ||
this.setState({data:this.props.callback()}); | ||
}, | ||
render: function() { | ||
const childrens = this.state.data.map((binding)=>{ | ||
return React.createElement(KeyBinding, Object.assign({}, binding, {onAddBindings:(shortcut, action)=>{ | ||
this.props.bind(shortcut, action); | ||
this.setState({data:this.props.callback()}); | ||
}, | ||
available:this.props.available, | ||
unbind: this.props.unbind | ||
})); | ||
}); | ||
|
||
return React.createElement('div',{}, childrens); | ||
} | ||
}); | ||
|
||
const get_shortcuts_data = function(notebook) { | ||
const actions = Object.keys(notebook.keyboard_manager.actions._actions); | ||
const src = []; | ||
|
||
for (let i = 0; i < actions.length; i++) { | ||
const action_id = actions[i]; | ||
const action = notebook.keyboard_manager.actions.get(action_id); | ||
|
||
let shortcut = notebook.keyboard_manager.command_shortcuts.get_action_shortcut(action_id) || | ||
notebook.keyboard_manager.edit_shortcuts.get_action_shortcut(action_id); | ||
if (shortcut) { | ||
shortcut = QH._humanize_sequence(shortcut); | ||
} | ||
|
||
src.push({ | ||
display: humanize_action_id(action_id), | ||
shortcut: shortcut, | ||
key:action_id, // react specific thing | ||
ckey: action_id | ||
}); | ||
} | ||
|
||
return src; | ||
}; | ||
|
||
|
||
const ShortcutEditor = function(notebook) { | ||
|
||
if(!notebook){ | ||
throw new Error("CommandPalette takes a notebook non-null mandatory arguement"); | ||
} | ||
|
||
const body = $('<div>'); | ||
const mod = dialog.modal({ | ||
notebook: notebook, | ||
keyboard_manager: notebook.keyboard_manager, | ||
title : "Edit Command mode Shortcuts", | ||
body : body, | ||
buttons : { | ||
OK : {} | ||
} | ||
}); | ||
|
||
const src = get_shortcuts_data(notebook); | ||
|
||
mod.addClass("modal_stretch"); | ||
|
||
mod.modal('show'); | ||
ReactDom.render( | ||
React.createElement(KeyBindingList, { | ||
callback:()=>{ return get_shortcuts_data(notebook);}, | ||
bind: (shortcut, command)=>{return notebook.keyboard_manager.command_shortcuts.add_shortcut(shortcut, command);}, | ||
unbind: (shortcut) => { return notebook.keyboard_manager.command_shortcuts.remove_shortcut(shortcut);}, | ||
available: (shrt) => { return notebook.keyboard_manager.command_shortcuts.is_available_shortcut(shrt);} | ||
}), | ||
body.get(0) | ||
); | ||
}; | ||
return {'ShortcutEditor': ShortcutEditor}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, cool!