|
| 1 | +// Copyright (c) Jupyter Development Team. |
| 2 | +// Distributed under the terms of the Modified BSD License. |
| 3 | + |
| 4 | + |
| 5 | +import QH from "notebook/js/quickhelp"; |
| 6 | +import dialog from "base/js/dialog"; |
| 7 | +import {render} from "preact"; |
| 8 | +import {createElement, createClass} from "preact-compat"; |
| 9 | +import marked from 'components/marked/lib/marked'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Humanize the action name to be consumed by user. |
| 13 | + * internally the actions name are of the form |
| 14 | + * <namespace>:<description-with-dashes> |
| 15 | + * we drop <namespace> and replace dashes for space. |
| 16 | + */ |
| 17 | +const humanize_action_id = function(str) { |
| 18 | + return str.split(':')[1].replace(/-/g, ' ').replace(/_/g, '-'); |
| 19 | +}; |
| 20 | + |
| 21 | +/** |
| 22 | + * given an action id return 'command-shortcut', 'edit-shortcut' or 'no-shortcut' |
| 23 | + * for the action. This allows us to tag UI in order to visually distinguish |
| 24 | + * Wether an action have a keybinding or not. |
| 25 | + **/ |
| 26 | + |
| 27 | +const KeyBinding = createClass({ |
| 28 | + displayName: 'KeyBindings', |
| 29 | + getInitialState: function() { |
| 30 | + return {shrt:''}; |
| 31 | + }, |
| 32 | + handleShrtChange: function (element){ |
| 33 | + this.setState({shrt:element.target.value}); |
| 34 | + }, |
| 35 | + render: function(){ |
| 36 | + const that = this; |
| 37 | + const available = this.props.available(this.state.shrt); |
| 38 | + const empty = (this.state.shrt === ''); |
| 39 | + return createElement('div', {className:'jupyter-keybindings'}, |
| 40 | + createElement('i', {className: "pull-right fa fa-plus", alt: 'add-keyboard-shortcut', |
| 41 | + onClick:()=>{ |
| 42 | + available?that.props.onAddBindings(that.state.shrt, that.props.ckey):null; |
| 43 | + that.state.shrt=''; |
| 44 | + } |
| 45 | + }), |
| 46 | + createElement('input', { |
| 47 | + type:'text', |
| 48 | + placeholder:'add shortcut', |
| 49 | + className:'pull-right'+((available||empty)?'':' alert alert-danger'), |
| 50 | + value:this.state.shrt, |
| 51 | + onChange:this.handleShrtChange |
| 52 | + }), |
| 53 | + this.props.shortcuts?this.props.shortcuts.map((item, index) => { |
| 54 | + return createElement('span', {className: 'pull-right'}, |
| 55 | + createElement('kbd', {}, [ |
| 56 | + item.h, |
| 57 | + createElement('i', {className: "fa fa-times", alt: 'remove '+item.h, |
| 58 | + onClick:()=>{ |
| 59 | + that.props.unbind(item.raw); |
| 60 | + } |
| 61 | + }) |
| 62 | + ]) |
| 63 | + ); |
| 64 | + }):null, |
| 65 | + createElement('div', {title: '(' +this.props.ckey + ')' , className:'jupyter-keybindings-text'}, this.props.display ) |
| 66 | + ); |
| 67 | + } |
| 68 | +}); |
| 69 | + |
| 70 | +const KeyBindingList = createClass({ |
| 71 | + displayName: 'KeyBindingList', |
| 72 | + getInitialState: function(){ |
| 73 | + return {data:[]}; |
| 74 | + }, |
| 75 | + componentDidMount: function(){ |
| 76 | + this.setState({data:this.props.callback()}); |
| 77 | + }, |
| 78 | + render: function() { |
| 79 | + const childrens = this.state.data.map((binding)=>{ |
| 80 | + return createElement(KeyBinding, Object.assign({}, binding, {onAddBindings:(shortcut, action)=>{ |
| 81 | + this.props.bind(shortcut, action); |
| 82 | + this.setState({data:this.props.callback()}); |
| 83 | + }, |
| 84 | + available:this.props.available, |
| 85 | + unbind: (shortcut) => { |
| 86 | + this.props.unbind(shortcut); |
| 87 | + this.setState({data:this.props.callback()}); |
| 88 | + } |
| 89 | + })); |
| 90 | + }); |
| 91 | + childrens.unshift(createElement('div', {className:'well', key:'disclamer', dangerouslySetInnerHTML: |
| 92 | + {__html: |
| 93 | + marked( |
| 94 | + |
| 95 | + "This dialog allows you to modify the keymap of the command mode, and persist the changes. "+ |
| 96 | + "You can define many type of shorctuts and sequences of keys. "+ |
| 97 | + "\n\n"+ |
| 98 | + " - Use dashes `-` to represent keys that should be pressed with modifiers, "+ |
| 99 | + "for example `Shift-a`, or `Ctrl-;`. \n"+ |
| 100 | + " - Separate by commas if the keys need to be pressed in sequence: `h,a,l,t`.\n"+ |
| 101 | + "\n\nYou can combine the two: `Ctrl-x,Meta-c,Meta-b,u,t,t,e,r,f,l,y`.\n"+ |
| 102 | + "Casing will have no effects: (e.g: `;` and `:` are the same on english keyboards)."+ |
| 103 | + " You need to explicitelty write the `Shift` modifier.\n"+ |
| 104 | + "Valid modifiers are `Cmd`, `Ctrl`, `Alt` ,`Meta`, `Cmdtrl`. Refer to developper docs "+ |
| 105 | + "for their signification depending on the platform." |
| 106 | + )} |
| 107 | + })); |
| 108 | + return createElement('div',{}, childrens); |
| 109 | + } |
| 110 | +}); |
| 111 | + |
| 112 | +const get_shortcuts_data = function(notebook) { |
| 113 | + const actions = Object.keys(notebook.keyboard_manager.actions._actions); |
| 114 | + const src = []; |
| 115 | + |
| 116 | + for (let i = 0; i < actions.length; i++) { |
| 117 | + const action_id = actions[i]; |
| 118 | + const action = notebook.keyboard_manager.actions.get(action_id); |
| 119 | + |
| 120 | + let shortcuts = notebook.keyboard_manager.command_shortcuts.get_action_shortcuts(action_id); |
| 121 | + let hshortcuts; |
| 122 | + if (shortcuts.length > 0) { |
| 123 | + hshortcuts = shortcuts.map((raw)=>{ |
| 124 | + return {h:QH._humanize_sequence(raw),raw:raw};} |
| 125 | + ); |
| 126 | + } |
| 127 | + src.push({ |
| 128 | + display: humanize_action_id(action_id), |
| 129 | + shortcuts: hshortcuts, |
| 130 | + key:action_id, // react specific thing |
| 131 | + ckey: action_id |
| 132 | + }); |
| 133 | + } |
| 134 | + return src; |
| 135 | +}; |
| 136 | + |
| 137 | + |
| 138 | +export const ShortcutEditor = function(notebook) { |
| 139 | + |
| 140 | + if(!notebook){ |
| 141 | + throw new Error("CommandPalette takes a notebook non-null mandatory arguement"); |
| 142 | + } |
| 143 | + |
| 144 | + const body = $('<div>'); |
| 145 | + const mod = dialog.modal({ |
| 146 | + notebook: notebook, |
| 147 | + keyboard_manager: notebook.keyboard_manager, |
| 148 | + title : "Edit Command mode Shortcuts", |
| 149 | + body : body, |
| 150 | + buttons : { |
| 151 | + OK : {} |
| 152 | + } |
| 153 | + }); |
| 154 | + |
| 155 | + const src = get_shortcuts_data(notebook); |
| 156 | + |
| 157 | + mod.addClass("modal_stretch"); |
| 158 | + |
| 159 | + mod.modal('show'); |
| 160 | + render( |
| 161 | + createElement(KeyBindingList, { |
| 162 | + callback:()=>{ return get_shortcuts_data(notebook);}, |
| 163 | + bind: (shortcut, command) => { |
| 164 | + return notebook.keyboard_manager.command_shortcuts._persist_shortcut(shortcut, command); |
| 165 | + }, |
| 166 | + unbind: (shortcut) => { |
| 167 | + return notebook.keyboard_manager.command_shortcuts._persist_remove_shortcut(shortcut); |
| 168 | + }, |
| 169 | + available: (shrt) => { return notebook.keyboard_manager.command_shortcuts.is_available_shortcut(shrt);} |
| 170 | + }), |
| 171 | + body.get(0) |
| 172 | + ); |
| 173 | +}; |
0 commit comments