|
| 1 | +// Copyright (c) Jupyter-Contrib Team. |
| 2 | +// Distributed under the terms of the Modified BSD License. |
| 3 | + |
| 4 | +// This is an extension allows you to jump to the current running cell. |
| 5 | +// You can also activate this functionality automatically, |
| 6 | +// i.e., your view is always scolling to the current cell. |
| 7 | + |
| 8 | +// |
| 9 | +// Keyboard shortcuts: Alt-I and Alt-down (works with single cells also -- this is useful!) |
| 10 | +// The extension is simple, create function and then register the action and shortkey separately, |
| 11 | +// so that user can update the shortkey according to their need. |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +define([ |
| 16 | + 'base/js/namespace', |
| 17 | + 'jquery', |
| 18 | + 'require', |
| 19 | + 'base/js/events' |
| 20 | +], function (Jupyter, $, requirejs, events) { |
| 21 | + "use strict"; |
| 22 | + |
| 23 | + var action_follow_cell_on; // set on registration |
| 24 | + var action_follow_cell_off; // set on registration |
| 25 | + var action_go_to_runing_cell; // set on registration |
| 26 | + var params = { |
| 27 | + is_follow_cell: false, |
| 28 | + go_to_running_cell_shortcut: 'Alt-I', |
| 29 | + follow_cell_on_shortcut: "Alt-;", |
| 30 | + follow_cell_off_shortcut: "Alt-'", |
| 31 | + button_icon: 'fa-anchor' |
| 32 | + }; |
| 33 | + |
| 34 | + function scrollIntoRunningCell(event, data) { |
| 35 | + $('.running')[0].scrollIntoView({ behavior: 'smooth', inline: 'center' }); |
| 36 | + } |
| 37 | + |
| 38 | + // update params with any specified in the server's config file |
| 39 | + var update_params = function () { |
| 40 | + var config = Jupyter.notebook.config; |
| 41 | + for (var key in params) { |
| 42 | + if (config.data.hasOwnProperty(key)) |
| 43 | + params[key] = config.data[key]; |
| 44 | + } |
| 45 | + }; |
| 46 | + |
| 47 | + // Go to Running cell shortcut |
| 48 | + function go_to_running_cell(event) { |
| 49 | + |
| 50 | + // Find running cell and click the first one |
| 51 | + if ($('.running').length > 0) { |
| 52 | + $('.running')[0].scrollIntoView(); |
| 53 | + } |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + function follow_running_cell_on(event) { |
| 58 | + Jupyter.notebook.events.on('finished_execute.CodeCell', scrollIntoRunningCell); |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + function follow_running_cell_off(event) { |
| 63 | + Jupyter.notebook.events.off('finished_execute.CodeCell', scrollIntoRunningCell); |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + // Register actions to collapse and uncollapse the selected heading cell |
| 68 | + |
| 69 | + function register_new_actions() { |
| 70 | + action_go_to_runing_cell = Jupyter.keyboard_manager.actions.register({ |
| 71 | + handler: go_to_running_cell, |
| 72 | + help: "Go to first executing cell", |
| 73 | + help_index: 'aa', |
| 74 | + icon: params.button_icon |
| 75 | + }, 'Go to first running cell', 'Go To Running Cell' |
| 76 | + ) |
| 77 | + action_follow_cell_on = Jupyter.keyboard_manager.actions.register({ |
| 78 | + handler: follow_running_cell_on, |
| 79 | + help: "Follow running cell on", |
| 80 | + help_index: 'aa' |
| 81 | + }, 'Follow running cell on', 'Go To Running Cell' |
| 82 | + ) |
| 83 | + action_follow_cell_off = Jupyter.keyboard_manager.actions.register({ |
| 84 | + handler: follow_running_cell_off, |
| 85 | + help: "Follow running cell off", |
| 86 | + help_index: 'aa' |
| 87 | + }, 'Follow running cell off', 'Go To Running Cell' |
| 88 | + ); |
| 89 | + |
| 90 | + if (params.is_follow_cell) { |
| 91 | + Jupyter.notebook.events.on('finished_execute.CodeCell', scrollIntoRunningCell); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Register keyboard shortcuts according to parameters |
| 96 | + function register_keyboard_shortcuts() { |
| 97 | + |
| 98 | + var shortcut, edit_shortcuts = Jupyter.keyboard_manager.command_shortcuts; |
| 99 | + shortcut = params.go_to_running_cell_shortcut; |
| 100 | + if (shortcut) { |
| 101 | + edit_shortcuts.add_shortcut(shortcut, action_go_to_runing_cell); |
| 102 | + } |
| 103 | + |
| 104 | + shortcut = params.follow_cell_on_shortcut; |
| 105 | + if (shortcut) { |
| 106 | + edit_shortcuts.add_shortcut(shortcut, action_follow_cell_on); |
| 107 | + } |
| 108 | + |
| 109 | + shortcut = params.follow_cell_off_shortcut; |
| 110 | + if (shortcut) { |
| 111 | + edit_shortcuts.add_shortcut(shortcut, action_follow_cell_off); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + function load_ipython_extension() { |
| 116 | + update_params(); |
| 117 | + register_new_actions(); |
| 118 | + register_keyboard_shortcuts(); |
| 119 | + Jupyter.toolbar.add_buttons_group([action_go_to_runing_cell]) |
| 120 | + } |
| 121 | + |
| 122 | + return { |
| 123 | + load_ipython_extension: load_ipython_extension, |
| 124 | + }; |
| 125 | + |
| 126 | +}); |
0 commit comments