From 0b6bbe6bd602da2ef04f441177869b2d8293345a Mon Sep 17 00:00:00 2001 From: JW9506 Date: Tue, 15 Sep 2020 07:14:54 -0400 Subject: [PATCH 1/5] feat: use m and ~ like classic vim --- lib/jupyter/actions.js | 37 +++++++++++++++++++++++++++++++++++++ lib/jupyter/shortcuts.js | 2 ++ 2 files changed, 39 insertions(+) diff --git a/lib/jupyter/actions.js b/lib/jupyter/actions.js index 3116cf2..56d205a 100644 --- a/lib/jupyter/actions.js +++ b/lib/jupyter/actions.js @@ -381,6 +381,43 @@ define([ ); } }, 'extend-selection-below', 'vim-binding'); + + var cell_marker = {}; + + actions.register({ + 'handler': function(env, event) { + env.notebook.keyboard_manager.enabled = false + var oldCb = document.onkeydown; + var clear = () => { + document.onkeydown = oldCb; + env.notebook.keyboard_manager.enabled = true + }; + document.onkeydown = function (event) { + clear(); + var key = event.key; + var idx = env.notebook.get_selected_index(); + cell_marker[key] = idx; + }; + } + }, 'mark-cell', 'vim-binding-normal'); + + actions.register({ + 'handler': function(env, event) { + env.notebook.keyboard_manager.enabled = false + var oldCb = document.onkeydown; + var clear = () => { + document.onkeydown = oldCb; + env.notebook.keyboard_manager.enabled = true + }; + document.onkeydown = function (event) { + clear(); + var key = event.key, idx; + if ((idx = cell_marker[key]) == null) return; + env.notebook.select(idx); + env.notebook.scroll_cell_percent(idx, 0, 0); + }; + } + }, 'goto-mark-cell', 'vim-binding-normal'); }; exports.detach = function() { diff --git a/lib/jupyter/shortcuts.js b/lib/jupyter/shortcuts.js index 37c7cf3..7222570 100644 --- a/lib/jupyter/shortcuts.js +++ b/lib/jupyter/shortcuts.js @@ -75,6 +75,8 @@ define([ 'shift-l': 'jupyter-notebook:toggle-cell-line-numbers', 'shift-v': 'jupyter-notebook:toggle-cell-output-collapsed', 'shift-s': 'jupyter-notebook:toggle-cell-output-scrolled', + 'm': 'vim-binding-normal:mark-cell', + '~': 'vim-binding-normal:goto-mark-cell', // Defined in searchandreplace.js // https://github.com/jupyter/notebook/blob/4.x/notebook/static/notebook/js/searchandreplace.js#L375 '/': 'jupyter-notebook:find-and-replace' From 0eb28987a8270479e19dc825404e2e1c31864266 Mon Sep 17 00:00:00 2001 From: JW9506 Date: Tue, 15 Sep 2020 08:30:17 -0400 Subject: [PATCH 2/5] fix: update the focus state, may cause bug otherwise --- lib/jupyter/actions.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/jupyter/actions.js b/lib/jupyter/actions.js index 56d205a..3fdfae0 100644 --- a/lib/jupyter/actions.js +++ b/lib/jupyter/actions.js @@ -414,6 +414,7 @@ define([ var key = event.key, idx; if ((idx = cell_marker[key]) == null) return; env.notebook.select(idx); + env.notebook.get_cell(idx).focus_cell(); env.notebook.scroll_cell_percent(idx, 0, 0); }; } From f587849ff579b36aacec552ff2a897de094ed4b7 Mon Sep 17 00:00:00 2001 From: JW9506 Date: Sat, 19 Sep 2020 21:04:13 -0400 Subject: [PATCH 3/5] refactor: use event param use once --- lib/jupyter/actions.js | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/lib/jupyter/actions.js b/lib/jupyter/actions.js index 3fdfae0..9aa85f6 100644 --- a/lib/jupyter/actions.js +++ b/lib/jupyter/actions.js @@ -386,37 +386,27 @@ define([ actions.register({ 'handler': function(env, event) { - env.notebook.keyboard_manager.enabled = false - var oldCb = document.onkeydown; - var clear = () => { - document.onkeydown = oldCb; - env.notebook.keyboard_manager.enabled = true - }; - document.onkeydown = function (event) { - clear(); + env.notebook.keyboard_manager.enabled = false; + document.addEventListener('keydown', function (event) { var key = event.key; var idx = env.notebook.get_selected_index(); cell_marker[key] = idx; - }; + env.notebook.keyboard_manager.enabled = true; + }, { once: true }); } }, 'mark-cell', 'vim-binding-normal'); actions.register({ 'handler': function(env, event) { - env.notebook.keyboard_manager.enabled = false - var oldCb = document.onkeydown; - var clear = () => { - document.onkeydown = oldCb; - env.notebook.keyboard_manager.enabled = true - }; - document.onkeydown = function (event) { - clear(); + env.notebook.keyboard_manager.enabled = false; + document.addEventListener('keydown', function (event) { var key = event.key, idx; if ((idx = cell_marker[key]) == null) return; env.notebook.select(idx); env.notebook.get_cell(idx).focus_cell(); env.notebook.scroll_cell_percent(idx, 0, 0); - }; + env.notebook.keyboard_manager.enabled = true; + }, { once: true }); } }, 'goto-mark-cell', 'vim-binding-normal'); }; From bdac6ee372775c320daed882bddd374e2d068960 Mon Sep 17 00:00:00 2001 From: JW9506 <34848995+JW9506@users.noreply.github.com> Date: Mon, 21 Sep 2020 07:27:11 -0400 Subject: [PATCH 4/5] Update lib/jupyter/actions.js Co-authored-by: Alisue --- lib/jupyter/actions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jupyter/actions.js b/lib/jupyter/actions.js index 9aa85f6..94c20c6 100644 --- a/lib/jupyter/actions.js +++ b/lib/jupyter/actions.js @@ -388,10 +388,10 @@ define([ 'handler': function(env, event) { env.notebook.keyboard_manager.enabled = false; document.addEventListener('keydown', function (event) { + env.notebook.keyboard_manager.enabled = true; var key = event.key; var idx = env.notebook.get_selected_index(); cell_marker[key] = idx; - env.notebook.keyboard_manager.enabled = true; }, { once: true }); } }, 'mark-cell', 'vim-binding-normal'); From f7510490132fb61d2da039c3625720a122cab6d5 Mon Sep 17 00:00:00 2001 From: JW9506 <34848995+JW9506@users.noreply.github.com> Date: Mon, 21 Sep 2020 07:28:51 -0400 Subject: [PATCH 5/5] re-enable jupyter key event manager be first thing to do Co-authored-by: Alisue --- lib/jupyter/actions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jupyter/actions.js b/lib/jupyter/actions.js index 94c20c6..25cd17a 100644 --- a/lib/jupyter/actions.js +++ b/lib/jupyter/actions.js @@ -400,12 +400,12 @@ define([ 'handler': function(env, event) { env.notebook.keyboard_manager.enabled = false; document.addEventListener('keydown', function (event) { + env.notebook.keyboard_manager.enabled = true; var key = event.key, idx; if ((idx = cell_marker[key]) == null) return; env.notebook.select(idx); env.notebook.get_cell(idx).focus_cell(); env.notebook.scroll_cell_percent(idx, 0, 0); - env.notebook.keyboard_manager.enabled = true; }, { once: true }); } }, 'goto-mark-cell', 'vim-binding-normal');