From 95a5f9c284bce316446fad2355be5d5b0620bf38 Mon Sep 17 00:00:00 2001 From: Marc Udoff Date: Thu, 16 Jan 2020 11:02:50 -0500 Subject: [PATCH] Add ability to disable plugin via settings --- .gitignore | 3 ++- package.json | 7 ++++-- schema/settings.json | 13 +++++++++++ src/index.ts | 47 +++++++++++++++++++++++++++++++------- yarn.lock | 54 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 113 insertions(+), 11 deletions(-) create mode 100644 schema/settings.json diff --git a/.gitignore b/.gitignore index 537d22f..bc186e0 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ lib/ node_modules/ *.egg-info/ .ipynb_checkpoints +.idea # Created by https://www.gitignore.io/api/node @@ -81,7 +82,7 @@ typings/ .LSOverride # Icon must end with two \r -Icon +Icon # Thumbnails ._* diff --git a/package.json b/package.json index 6dbfb40..764f8cb 100644 --- a/package.json +++ b/package.json @@ -28,15 +28,18 @@ }, "files": [ "lib/**/*.{d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,woff2,ttf}", - "style/**/*.{css,eot,gif,html,jpg,json,png,svg,woff2,ttf}" + "style/**/*.{css,eot,gif,html,jpg,json,png,svg,woff2,ttf}", + "schema/*" ], "jupyterlab": { - "extension": true + "extension": true, + "schemaDir": "schema" }, "dependencies": { "@jupyterlab/application": "^1.0.1", "@jupyterlab/codemirror": "^1.0.1", "@jupyterlab/cells": "^1.0.1", + "@jupyterlab/coreutils": "^3.2.0", "@jupyterlab/notebook": "^1.0.1", "@phosphor/commands": "^1.6.3", "@phosphor/coreutils": "^1.3.1", diff --git a/schema/settings.json b/schema/settings.json new file mode 100644 index 0000000..94f9883 --- /dev/null +++ b/schema/settings.json @@ -0,0 +1,13 @@ +{ + "title": "Notebook Vim", + "type": "object", + "additionalProperties": true, + "properties": { + "enabled": { + "type": "boolean", + "title": "Enabled", + "description": "Enable/disable notebook vim (may require a page refresh)", + "default": true + } + } +} diff --git a/src/index.ts b/src/index.ts index 11f0e05..bbd3997 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,6 +15,7 @@ import { import { CodeMirrorEditor } from '@jupyterlab/codemirror'; +import { ISettingRegistry } from '@jupyterlab/coreutils'; import { ReadonlyJSONObject @@ -33,15 +34,17 @@ import 'codemirror/keymap/vim.js'; * A boolean indicating whether the platform is Mac. */ const IS_MAC = !!navigator.platform.match(/Mac/i); +const PLUGIN_NAME = 'jupyterlab_vim'; +let enabled = false; /** * Initialization data for the jupyterlab_vim extension. */ const extension: JupyterFrontEndPlugin = { - id: 'jupyterlab_vim', + id: PLUGIN_NAME, autoStart: true, activate: activateCellVim, - requires: [INotebookTracker] + requires: [INotebookTracker, ISettingRegistry] }; class VimCell { @@ -54,6 +57,9 @@ class VimCell { } private _onActiveCellChanged(): void { + if(!enabled) { + return; + } // if (this._prevActive && !this._prevActive.isDisposed) { // this._prevActive.metadata.changed.disconnect(this._onMetadataChanged, this); // } @@ -189,9 +195,8 @@ class VimCell { private _app: JupyterFrontEnd; } -function activateCellVim(app: JupyterFrontEnd, tracker: INotebookTracker): Promise { - - Promise.all([app.restored]).then(([args]) => { +async function setupPlugin(app: JupyterFrontEnd, tracker: INotebookTracker) { + await app.restored; const { commands, shell } = app; function getCurrent(args: ReadonlyJSONObject): NotebookPanel | null { const widget = tracker.currentWidget; @@ -204,7 +209,7 @@ function activateCellVim(app: JupyterFrontEnd, tracker: INotebookTracker): Promi return widget; } function isEnabled(): boolean { - return tracker.currentWidget !== null && + return enabled && tracker.currentWidget !== null && tracker.currentWidget === app.shell.currentWidget; } @@ -598,8 +603,34 @@ function activateCellVim(app: JupyterFrontEnd, tracker: INotebookTracker): Promi }); // tslint:disable:no-unused-expression - new VimCell(app, tracker); - }); + return new VimCell(app, tracker); +} + +function activateCellVim(app: JupyterFrontEnd, tracker: INotebookTracker, settingRegistry: ISettingRegistry): Promise { + let hasEverBeenEnabled = false; + + function updateSettings(settings: ISettingRegistry.ISettings) { + // TODO: This does not reset any cells that have been used with VIM + enabled = settings.get('enabled').composite === true; + if (enabled && !hasEverBeenEnabled) { + hasEverBeenEnabled = true; + setupPlugin(app, tracker); + } + } + + settingRegistry.load(`${PLUGIN_NAME}:settings`).then( + (settings: ISettingRegistry.ISettings) => { + updateSettings(settings); + settings.changed.connect(updateSettings); + }, + (err: Error) => { + console.error( + `Could not load settings, so did not active ${PLUGIN_NAME}: ${err}` + ); + } + ); + + return Promise.resolve(); } diff --git a/yarn.lock b/yarn.lock index 249257e..608a4ee 100644 --- a/yarn.lock +++ b/yarn.lock @@ -171,6 +171,23 @@ path-posix "~1.0.0" url-parse "~1.4.3" +"@jupyterlab/coreutils@^3.2.0": + version "3.2.0" + resolved "http://npmrepo/@jupyterlab%2fcoreutils/-/coreutils-3.2.0.tgz#dd4d887bdedfea4c8545d46d297531749cb13724" + integrity sha512-LATiUsHuwze/h3JC2EZOBV+kGBoUKO3npqw/Pcgge4bz09xF/oTDrx4G8jl5eew3w1dCUNp9eLduNh8Orrw7xQ== + dependencies: + "@phosphor/commands" "^1.7.0" + "@phosphor/coreutils" "^1.3.1" + "@phosphor/disposable" "^1.3.0" + "@phosphor/properties" "^1.1.3" + "@phosphor/signaling" "^1.3.0" + ajv "^6.5.5" + json5 "^2.1.0" + minimist "~1.2.0" + moment "^2.24.0" + path-posix "~1.0.0" + url-parse "~1.4.3" + "@jupyterlab/docregistry@^1.0.1": version "1.0.1" resolved "https://registry.yarnpkg.com/@jupyterlab/docregistry/-/docregistry-1.0.1.tgz#28ca14e7c43c161384d5582e011b0a70522bb8eb" @@ -320,6 +337,11 @@ resolved "https://registry.yarnpkg.com/@phosphor/algorithm/-/algorithm-1.1.3.tgz#fb0e974f4e81aadc06f948770b3060c4ec8a1e27" integrity sha512-+dkdYTBglR+qGnLVQdCvYojNZMGxf+xSl1Jeksha3pm7niQktSFz2aR5gEPu/nI5LM8T8slTpqE4Pjvq8P+IVA== +"@phosphor/algorithm@^1.2.0": + version "1.2.0" + resolved "http://npmrepo/@phosphor%2falgorithm/-/algorithm-1.2.0.tgz#4a19aa59261b7270be696672dc3f0663f7bef152" + integrity sha512-C9+dnjXyU2QAkWCW6QVDGExk4hhwxzAKf5/FIuYlHAI9X5vFv99PYm0EREDxX1PbMuvfFBZhPNu0PvuSDQ7sFA== + "@phosphor/application@^1.6.3": version "1.6.4" resolved "https://registry.yarnpkg.com/@phosphor/application/-/application-1.6.4.tgz#8e8d649260d700ac77fb1cfb78af566730052fd0" @@ -348,6 +370,18 @@ "@phosphor/keyboard" "^1.1.3" "@phosphor/signaling" "^1.2.3" +"@phosphor/commands@^1.7.0": + version "1.7.2" + resolved "http://npmrepo/@phosphor%2fcommands/-/commands-1.7.2.tgz#df724f2896ae43c4a3a9e2b5a6445a15e0d60487" + integrity sha512-iSyBIWMHsus323BVEARBhuVZNnVel8USo+FIPaAxGcq+icTSSe6+NtSxVQSmZblGN6Qm4iw6I6VtiSx0e6YDgQ== + dependencies: + "@phosphor/algorithm" "^1.2.0" + "@phosphor/coreutils" "^1.3.1" + "@phosphor/disposable" "^1.3.1" + "@phosphor/domutils" "^1.1.4" + "@phosphor/keyboard" "^1.1.3" + "@phosphor/signaling" "^1.3.1" + "@phosphor/coreutils@^1.3.1": version "1.3.1" resolved "https://registry.yarnpkg.com/@phosphor/coreutils/-/coreutils-1.3.1.tgz#441e34f42340f7faa742a88b2a181947a88d7226" @@ -361,11 +395,24 @@ "@phosphor/algorithm" "^1.1.3" "@phosphor/signaling" "^1.2.3" +"@phosphor/disposable@^1.3.0", "@phosphor/disposable@^1.3.1": + version "1.3.1" + resolved "http://npmrepo/@phosphor%2fdisposable/-/disposable-1.3.1.tgz#be98fe12bd8c9a4600741cb83b0a305df28628f3" + integrity sha512-0NGzoTXTOizWizK/brKKd5EjJhuuEH4903tLika7q6wl/u0tgneJlTh7R+MBVeih0iNxtuJAfBa3IEY6Qmj+Sw== + dependencies: + "@phosphor/algorithm" "^1.2.0" + "@phosphor/signaling" "^1.3.1" + "@phosphor/domutils@^1.1.3": version "1.1.3" resolved "https://registry.yarnpkg.com/@phosphor/domutils/-/domutils-1.1.3.tgz#5aeeaefb4bbfcc7c0942e5287a29d3c7f2b1a2bc" integrity sha512-5CtLAhURQXXHhNXfQydDk/luG1cDVnhlu/qw7gz8/9pht0KXIAmNg/M0LKxx2oJ9+YMNCLVWxAnHAU0yrDpWSA== +"@phosphor/domutils@^1.1.4": + version "1.1.4" + resolved "http://npmrepo/@phosphor%2fdomutils/-/domutils-1.1.4.tgz#4c6aecf7902d3793b45db325319340e0a0b5543b" + integrity sha512-ivwq5TWjQpKcHKXO8PrMl+/cKqbgxPClPiCKc1gwbMd+6hnW5VLwNG0WBzJTxCzXK43HxX18oH+tOZ3E04wc3w== + "@phosphor/dragdrop@^1.3.3": version "1.3.3" resolved "https://registry.yarnpkg.com/@phosphor/dragdrop/-/dragdrop-1.3.3.tgz#9487d27a6eb8cd54bfe6d91eaffc9d0852817b61" @@ -399,6 +446,13 @@ dependencies: "@phosphor/algorithm" "^1.1.3" +"@phosphor/signaling@^1.3.0", "@phosphor/signaling@^1.3.1": + version "1.3.1" + resolved "http://npmrepo/@phosphor%2fsignaling/-/signaling-1.3.1.tgz#1cd10b069bdb2c9adb3ba74245b30141e5afc2d7" + integrity sha512-Eq3wVCPQAhUd9+gUGaYygMr+ov7dhSGblSBXiDzpZlSIfa8OVD4P3cCvYXr/acDTNmZ/gHTcSFO8/n3rDkeXzg== + dependencies: + "@phosphor/algorithm" "^1.2.0" + "@phosphor/virtualdom@^1.1.3": version "1.1.3" resolved "https://registry.yarnpkg.com/@phosphor/virtualdom/-/virtualdom-1.1.3.tgz#33ddebc710ad5bd136fd5f61d7adb4fa14e781e0"