import { JupyterFrontEnd , JupyterFrontEndPlugin , ILayoutRestorer , } from '@jupyterlab/application' ; import { ICommandPalette , WidgetTracker } from '@jupyterlab/apputils' ; import { ISettingRegistry , Settings } from '@jupyterlab/settingregistry' ; import { IEditorServices } from '@jupyterlab/codeeditor' ; import { LabIcon } from '@jupyterlab/ui-components' ; import { Widget } from '@lumino/widgets' ; import { find } from '@lumino/algorithm' ; import editorIconSVGstr from '../style/icon/jupyter_snippeteditoricon.svg' ; import codeSnippetIconSVGstr from '../style/icon/jupyter_snippeticon.svg' ; import { CodeSnippetInputDialog } from './CodeSnippetInputDialog' ; import { CodeSnippetWidget } from './CodeSnippetWidget' ; import { CodeSnippetEditor , ICodeSnippetEditorMetadata , } from './CodeSnippetEditor' ; import { CodeSnippetService } from './CodeSnippetService' ; import { NotebookPanel } from '@jupyterlab/notebook' ; import { DocumentWidget } from '@jupyterlab/docregistry' ; const CODE_SNIPPET_EXTENSION_ID = 'code-snippet-extension' ; const CODE_SNIPPET_SETTING_ID = 'jupyterlab-code-snippets:snippets' ; /**
 * Snippet Editor Icon
 */ const editorIcon = new LabIcon ( { name : 'custom-ui-components:codeSnippetEditorIcon' , svgstr : editorIconSVGstr , } ) ; /**
 * Snippet Icon
 */ const codeSnippetIcon = new LabIcon ( { name : 'custom-ui-components:codeSnippetIcon' , svgstr : codeSnippetIconSVGstr , } ) ; /**
 * Initialization data for the code_snippets extension.
 */ const code_snippet_extension = { id : CODE_SNIPPET_EXTENSION_ID , autoStart : true , requires : [ ICommandPalette , ILayoutRestorer , IEditorServices ] , activate : activateCodeSnippet , } ; function activateCodeSnippet ( app , palette , restorer , editorServices ) { console . log ( 'JupyterLab extension code-snippets is activated!' ) ; const getCurrentWidget = ( ) : Widget => { return app . shell . currentWidget ; } ; const codeSnippetWidget = new CodeSnippetWidget ( getCurrentWidget , app , editorServices ) ; codeSnippetWidget . id = CODE_SNIPPET_EXTENSION_ID ; codeSnippetWidget . title . icon = codeSnippetIcon ; codeSnippetWidget . title . caption = 'Code Snippet Explorer' ; restorer . add ( codeSnippetWidget , CODE_SNIPPET_EXTENSION_ID ) ; app . shell . add ( codeSnippetWidget , 'left' , { rank : 900 } ) ; const openCodeSnippetEditor = ( args ) : void => { const widgetId = `jp-codeSnippet-editor-${ args . id }` ; const openEditor = find ( app . shell . widgets ( 'main' ) , ( widget , _ ) => { return widget . id === widgetId ; } ) ; if ( openEditor ) { app . shell . activateById ( widgetId ) ; return ; } const codeSnippetEditor = new CodeSnippetEditor ( editorServices , tracker , codeSnippetWidget , args ) ; codeSnippetEditor . id = widgetId ; codeSnippetEditor . addClass ( widgetId ) ; codeSnippetEditor . title . label = args . name === '' ? 'New Code Snippet' : '[' + args . language + '] ' + args . name ; codeSnippetEditor . title . closable = true ; codeSnippetEditor . title . icon = editorIcon ; if ( ! tracker . has ( codeSnippetEditor ) ) { tracker . add ( codeSnippetEditor ) ; } if ( ! codeSnippetEditor . isAttached ) { app . shell . add ( codeSnippetEditor , 'main' , { mode : 'tab-after' , } ) ; } app . shell . activateById ( codeSnippetEditor . id ) ; } ; const editorSaveCommand = 'jp-codeSnippet-editor:save' ; app . commands . addCommand ( editorSaveCommand , { execute : ( ) => { const editor = tracker . currentWidget ; editor . updateSnippet ( ) ; } , } ) ; app . commands . addKeyBinding ( { command : editorSaveCommand , args : { } , keys : [ 'Accel S' ] , selector : '.jp-codeSnippet-editor' , } ) ; const editorCommand = 'jp-codeSnippet-editor:open' ; app . commands . addCommand ( editorCommand , { execute : ( args ) => { openCodeSnippetEditor ( args ) ; } , } ) ; const saveCommand = 'codeSnippet:save-as-snippet' ; const toggled = false ; app . commands . addCommand ( saveCommand , { label : 'Save As Code Snippet' , isEnabled : ( ) => true , isVisible : ( ) => true , isToggled : ( ) => toggled , iconClass : 'some-css-icon-class' , execute : ( ) => { let language = '' ; if ( app . shell . currentWidget instanceof NotebookPanel ) { language = ( app . shell . currentWidget as NotebookPanel ) . sessionContext . kernelPreference . language ; } else if ( app . shell . currentWidget instanceof DocumentWidget ) { language = ( app . shell . currentWidget as DocumentWidget ) . context . model . defaultKernelLanguage ; } const highlightedCode = getSelectedText ( ) ; if ( highlightedCode === '' ) { const curr = document . getElementsByClassName ( 'jp-Cell jp-mod-selected' ) ; const resultArray = [ ] ; for ( let i = 0 ; i < curr . length ; i ++ ) { const text = curr [ i ] as HTMLElement ; const textContent = text . innerText ; const arrayInput = textContent . split ( '\n' ) ; const indexedInput = arrayInput . slice ( 1 ) ; for ( let i = 0 ; i < indexedInput . length ; i ++ ) { if ( indexedInput [ i ] . charCodeAt ( 0 ) === 8203 ) { indexedInput [ i ] = '' ; } resultArray . push ( indexedInput [ i ] ) ; } } CodeSnippetInputDialog ( codeSnippetWidget , resultArray , language , codeSnippetWidget . codeSnippetManager . snippets . length ) ; } else { CodeSnippetInputDialog ( codeSnippetWidget , highlightedCode . split ( '\n' ) , language , codeSnippetWidget . codeSnippetManager . snippets . length ) ; } } , } ) ; app . contextMenu . addItem ( { type : 'separator' , selector : '.jp-Notebook' , rank : 13 , } ) ; app . contextMenu . addItem ( { command : saveCommand , selector : '.jp-Notebook' , rank : 14 , } ) ; app . contextMenu . addItem ( { type : 'separator' , selector : '.jp-Notebook' , rank : 15 , } ) ; app . contextMenu . addItem ( { type : 'separator' , selector : '.jp-FileEditor' , rank : 7 , } ) ; app . contextMenu . addItem ( { command : saveCommand , selector : '.jp-FileEditor' , rank : 8 , } ) ; app . contextMenu . addItem ( { type : 'separator' , selector : '.jp-FileEditor' , rank : 9 , } ) ; const tracker = new WidgetTracker < CodeSnippetEditor > ( { namespace : 'codeSnippetEditor' , } ) ; restorer . restore ( tracker , { command : editorCommand , args : ( widget ) => { const editorMetadata = widget . codeSnippetEditorMetadata ; return { name : editorMetadata . name , description : editorMetadata . description , language : editorMetadata . language , code : editorMetadata . code , id : editorMetadata . id , selectedTags : editorMetadata . selectedTags , allSnippetTags : editorMetadata . allSnippetTags , allLangTags : editorMetadata . allLangTags , fromScratch : editorMetadata . fromScratch , } ; } , name : ( widget ) => { return widget . id ; } , } ) ; } const codeSnippetExtensionSetting = { id : CODE_SNIPPET_SETTING_ID , autoStart : true , requires : [ ISettingRegistry ] , activate : ( app , settingRegistry ) => { void settingRegistry . load ( CODE_SNIPPET_SETTING_ID ) . then ( ( settings ) => { CodeSnippetService . init ( settings as Settings , app ) ; console . log ( 'JupyterLab extension code-snippets setting is activated!' ) ; } ) . catch ( ( e ) => console . log ( e ) ) ; } , } ; function getSelectedText ( ) { let selectedText ; if ( window . getSelection ) { selectedText = window . getSelection ( ) ; } else if ( document . getSelection ) { selectedText = document . getSelection ( ) ; } return selectedText . toString ( ) ; } export default [ code_snippet_extension , codeSnippetExtensionSetting ] ;