From 120e4d321bc4abb8c635048b2f989eb76c13a090 Mon Sep 17 00:00:00 2001 From: jahn Date: Wed, 23 Sep 2020 12:50:57 -0700 Subject: [PATCH 1/3] Add save as code snippet command to non-notebook file --- src/index.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 67a226d..d52bdee 100644 --- a/src/index.ts +++ b/src/index.ts @@ -169,12 +169,18 @@ function activateCodeSnippet( } }); - //Put the command above in context menu + // Put the saveCommand above in context menu app.contextMenu.addItem({ command: saveCommand, selector: '.jp-Cell' }); + // Put the saveCommand in non-notebook file context menu + app.contextMenu.addItem({ + command: saveCommand, + selector: '.jp-FileEditor' + }); + // Add keybinding to save app.commands.addKeyBinding({ command: saveCommand, From ee6cb9157fbf6be1a58a4571af1620814baab530 Mon Sep 17 00:00:00 2001 From: jahn Date: Thu, 24 Sep 2020 11:42:09 -0700 Subject: [PATCH 2/3] Fix #126 --- package.json | 7 ++++--- schema/settings.json | 10 ++++++++++ src/index.ts | 27 +++++++++++++++++---------- 3 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 schema/settings.json diff --git a/package.json b/package.json index bf1a707..906a1dd 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "author": "Jay Ahn, Kiran Pinnipati", "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/*.json" ], "main": "lib/index.js", "types": "lib/index.d.ts", @@ -43,7 +44,6 @@ "@jupyterlab/application": "^2.1.2", "@jupyterlab/apputils": "^2.2.4", "@jupyterlab/cells": "^2.2.4", - "@jupyterlab/coreutils": "^4.1.0", "@jupyterlab/docmanager": "^2.1.2", "@jupyterlab/docregistry": "^2.1.2", "@jupyterlab/fileeditor": "^2.1.2", @@ -77,7 +77,8 @@ "style/*.css" ], "jupyterlab": { - "extension": true + "extension": true, + "schemaDir": "schema" }, "husky": { "hooks": { diff --git a/schema/settings.json b/schema/settings.json new file mode 100644 index 0000000..9ef9fa1 --- /dev/null +++ b/schema/settings.json @@ -0,0 +1,10 @@ +{ + "jupyter.lab.shortcuts": [ + { + "command": "codeSnippet:save-as-snippet", + "keys": ["Accel Shift S"], + "selector": ".jp-Cell" + } + ], + "type": "object" +} diff --git a/src/index.ts b/src/index.ts index d52bdee..8dd8f1d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,8 @@ import { ILayoutRestorer } from '@jupyterlab/application'; import { ICommandPalette, WidgetTracker } from '@jupyterlab/apputils'; +import { ISettingRegistry } from '@jupyterlab/settingregistry'; + import { IEditorServices } from '@jupyterlab/codeeditor'; import { LabIcon } from '@jupyterlab/ui-components'; @@ -23,6 +25,7 @@ import { const CODE_SNIPPET_EXTENSION_ID = 'code-snippet-extension'; +const CODE_SNIPPET_SETTING_ID = 'jupyterlab-code-snippets:settings'; /** * Snippet Editor Icon */ @@ -150,7 +153,7 @@ function activateCodeSnippet( }); //Add an application command - const saveCommand = 'save as code snippet'; + const saveCommand = 'codeSnippet:save-as-snippet'; const toggled = false; app.commands.addCommand(saveCommand, { label: 'Save As Code Snippet', @@ -181,14 +184,6 @@ function activateCodeSnippet( selector: '.jp-FileEditor' }); - // Add keybinding to save - app.commands.addKeyBinding({ - command: saveCommand, - args: {}, - keys: ['Accel Shift S'], - selector: '.jp-Cell' - }); - // Track and restore the widget state const tracker = new WidgetTracker({ namespace: 'codeSnippetEditor' @@ -217,6 +212,18 @@ function activateCodeSnippet( }); } +const codeSnippetExtensionSetting: JupyterFrontEndPlugin = { + id: CODE_SNIPPET_SETTING_ID, + autoStart: true, + requires: [ISettingRegistry], + activate: (app: JupyterFrontEnd, settingRegistry: ISettingRegistry) => { + void settingRegistry + .load(CODE_SNIPPET_SETTING_ID) + .then(_ => console.log('settingRegistry successfully loaded!')) + .catch(e => console.log(e)); + } +}; + function getSelectedText(): string { let selectedText; // window.getSelection @@ -230,4 +237,4 @@ function getSelectedText(): string { return selectedText.toString(); } -export default code_snippet_extension; +export default [code_snippet_extension, codeSnippetExtensionSetting]; From 92da7a62d15986335f7a59d57e7cd32cadb25700 Mon Sep 17 00:00:00 2001 From: jahn Date: Tue, 29 Sep 2020 10:20:09 -0700 Subject: [PATCH 3/3] Change keyboard shortcut from accel shift s to accel shift a --- schema/settings.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/schema/settings.json b/schema/settings.json index 9ef9fa1..88710a4 100644 --- a/schema/settings.json +++ b/schema/settings.json @@ -2,8 +2,13 @@ "jupyter.lab.shortcuts": [ { "command": "codeSnippet:save-as-snippet", - "keys": ["Accel Shift S"], + "keys": ["Accel Shift A"], "selector": ".jp-Cell" + }, + { + "command": "codeSnippet:save-as-snippet", + "keys": ["Accel Shift A"], + "selector": ".jp-FileEditor" } ], "type": "object"