| 1 | const cmd_key = /Mac/.test(navigator.platform) ? 'Cmd' : 'Ctrl' |
| 2 | |
| 3 | const edit_actions = [ |
| 4 | { |
| 5 | name: 'R Assign', |
| 6 | shortcut: 'Alt--', |
| 7 | icon: 'fa-long-arrow-left', |
| 8 | help: 'R: Inserts the left-assign operator (<-)', |
| 9 | handler(cm) { |
| 10 | cm.replaceSelection(' <- ') |
| 11 | }, |
| 12 | }, |
| 13 | { |
| 14 | name: 'R Pipe', |
| 15 | shortcut: `Shift-${cmd_key}-M`, |
| 16 | icon: 'fa-angle-right', |
| 17 | help: 'R: Inserts the magrittr pipe operator (%>%)', |
| 18 | handler(cm) { |
| 19 | cm.replaceSelection(' %>% ') |
| 20 | }, |
| 21 | }, |
| 22 | { |
| 23 | name: 'R Help', |
| 24 | shortcut: 'F1', |
| 25 | icon: 'fa-book', |
| 26 | help: 'R: Shows the manpage for the item under the cursor', |
| 27 | handler(cm, cell) { |
| 28 | const {anchor, head} = cm.findWordAt(cm.getCursor()) |
| 29 | const word = cm.getRange(anchor, head) |
| 30 | |
| 31 | const callbacks = cell.get_callbacks() |
| 32 | const options = {silent: false, store_history: false, stop_on_error: true} |
| 33 | cell.last_msg_id = cell.notebook.kernel.execute(`help(\`${word}\`)`, callbacks, options) |
| 34 | }, |
| 35 | }, |
| 36 | ] |
| 37 | |
| 38 | const prefix = 'irkernel' |
| 39 | |
| 40 | function add_edit_shortcut(notebook, actions, keyboard_manager, edit_action) { |
| 41 | const {name, shortcut, icon, help, handler} = edit_action |
| 42 | |
| 43 | const action = { |
| 44 | icon, help, |
| 45 | help_index : 'zz', |
| 46 | handler: () => { |
| 47 | const cell = notebook.get_selected_cell() |
| 48 | handler(cell.code_mirror, cell) |
| 49 | }, |
| 50 | } |
| 51 | |
| 52 | const full_name = actions.register(action, name, prefix) |
| 53 | |
| 54 | Jupyter.keyboard_manager.edit_shortcuts.add_shortcut(shortcut, full_name) |
| 55 | } |
| 56 | |
| 57 | function render_math(pager, html) { |
| 58 | if (!html) return |
| 59 | const $container = pager.pager_element.find('#pager-container') |
| 60 | $container.find('p[style="text-align: center;"]').map((i, e) => |
| 61 | e.outerHTML = `\\[${e.querySelector('i').innerHTML}\\]`) |
| 62 | $container.find('i').map((i, e) => |
| 63 | e.outerHTML = `\\(${e.innerHTML}\\)`) |
| 64 | MathJax.Hub.Queue(['Typeset', MathJax.Hub, $container[0]]) |
| 65 | } |
| 66 | |
| 67 | define(['base/js/namespace'], ({ |
| 68 | notebook, |
| 69 | actions, |
| 70 | keyboard_manager, |
| 71 | pager, |
| 72 | }) => ({ |
| 73 | onload() { |
| 74 | edit_actions.forEach(a => add_edit_shortcut(notebook, actions, keyboard_manager, a)) |
| 75 | |
| 76 | pager.events.on('open_with_text.Pager', (event, {data: {'text/html': html}}) => |
| 77 | render_math(pager, html)) |
| 78 | }, |
| 79 | })) |