@@ -4,13 +4,10 @@ import { ReactWidget, showDialog, Dialog } from '@jupyterlab/apputils';
44import React from 'react' ;
55import { Message } from '@lumino/messaging' ;
66import { Button } from '@jupyterlab/ui-components' ;
7+ // import { DocumentWidget } from '@jupyterlab/docregistry';
78// import { Widget } from '@lumino/widgets';
89
9- interface ICodeSnippetEditor {
10- namespace : string ;
11- codeSnippet : ICodeSnippet ;
12- // onSave: () => void;
13- }
10+ // const CODE_SNIPPET_EDITOR_ID = 'jp-codeSnippet-editor';
1411
1512export class CodeSnippetEditor extends ReactWidget {
1613 editorServices : IEditorServices ;
@@ -25,17 +22,17 @@ export class CodeSnippetEditor extends ReactWidget {
2522 this._deactivateField(e, target, label)*/
2623 editor : CodeEditor . IEditor ;
2724 saved : boolean ;
28- namespace : string ;
2925 codeSnippet : ICodeSnippet ;
26+ _hasRefreshedSinceAttach : boolean ;
3027
31- constructor ( editorServices : IEditorServices , args : ICodeSnippetEditor ) {
28+ constructor ( editorServices : IEditorServices , args : ICodeSnippet ) {
3229 super ( ) ;
3330 // this.id = 'Code-Snippet-Edit';
3431
3532 this . editorServices = editorServices ;
36- this . namespace = args . namespace ;
37- this . codeSnippet = args . codeSnippet ;
33+ this . codeSnippet = args ;
3834 this . saved = false ;
35+ this . _hasRefreshedSinceAttach = false ;
3936
4037 this . renderCodeInput = this . renderCodeInput . bind ( this ) ;
4138 }
@@ -45,7 +42,6 @@ export class CodeSnippetEditor extends ReactWidget {
4542 target : HTMLElement ,
4643 label : HTMLElement
4744 ) : void {
48- console . log ( e . target ) ;
4945 const clickedElement = e . target as HTMLElement ;
5046 if ( clickedElement . className !== target . className ) {
5147 target . classList . remove ( 'jp-snippet-editor-active' ) ;
@@ -66,12 +62,9 @@ export class CodeSnippetEditor extends ReactWidget {
6662 const label = document . getElementsByClassName (
6763 labelClassName
6864 ) [ 0 ] as HTMLElement ;
69- console . log ( target . classList ) ;
7065 if ( ! target . classList . contains ( 'jp-snippet-editor-active' ) ) {
71- console . log ( 'YES REAChed.' ) ;
7266 target . classList . add ( 'jp-snippet-editor-active' ) ;
7367 label . classList . add ( 'jp-snippet-editor-label-active' ) ;
74- console . log ( labelClassName ) ;
7568 }
7669 window . addEventListener (
7770 'click' ,
@@ -80,31 +73,27 @@ export class CodeSnippetEditor extends ReactWidget {
8073 ) ;
8174 }
8275
83- /**
84- * Gets called by update() call or when first rendered
85- * @param msg
86- */
87- onActivateRequest ( msg : Message ) : void {
88- super . onActivateRequest ( msg ) ;
89- console . log ( 'updating' ) ;
76+ // updateCodeSnippetCode(): void {
77+
78+ // }
79+
80+ // /**
81+ // * Gets called by update() call or when first rendered
82+ // * @param msg
83+ // */
84+ onUpdateRequest ( msg : Message ) : void {
85+ super . onUpdateRequest ( msg ) ;
86+
9087 if (
9188 ! this . editor &&
9289 document . getElementById ( 'code-' + this . codeSnippet . id )
9390 ) {
9491 const editorFactory = this . editorServices . factoryService . newInlineEditor ;
95-
9692 const getMimeTypeByLanguage = this . editorServices . mimeTypeService
9793 . getMimeTypeByLanguage ;
98- if ( typeof this . codeSnippet === 'string' ) {
99- this . codeSnippet = JSON . parse ( this . codeSnippet ) ;
100- }
101- console . log ( this ) ;
102- // console.log(this.args);
103- const editor = editorFactory ( {
104- // config: { readOnly: false, rulers: [1, 2, 3, 4] },
105- host : document . querySelector (
106- `#code-${ this . codeSnippet . id } `
107- ) as HTMLElement ,
94+
95+ this . editor = editorFactory ( {
96+ host : document . getElementById ( 'code-' + this . codeSnippet . id ) ,
10897 model : new CodeEditor . Model ( {
10998 value : this . codeSnippet . code . join ( '\n' ) ,
11099 mimeType : getMimeTypeByLanguage ( {
@@ -113,10 +102,40 @@ export class CodeSnippetEditor extends ReactWidget {
113102 } )
114103 } )
115104 } ) ;
116- this . editor = editor ;
105+ // this.editor.model.value.changed.connect((args: any) => {
106+ // this.
107+ // })
108+ // console.log(document.querySelector(`#code-${this.codeSnippet.id}`));
109+ }
110+ if ( this . isVisible ) {
111+ this . _hasRefreshedSinceAttach = true ;
112+ this . editor . refresh ( ) ;
117113 }
118114 }
119115
116+ onAfterAttach ( msg : Message ) : void {
117+ super . onAfterAttach ( msg ) ;
118+
119+ this . _hasRefreshedSinceAttach = false ;
120+ if ( this . isVisible ) {
121+ this . update ( ) ;
122+ }
123+ }
124+
125+ onAfterShow ( msg : Message ) : void {
126+ if ( ! this . _hasRefreshedSinceAttach ) {
127+ this . update ( ) ;
128+ }
129+ }
130+
131+ /**
132+ * Initial focus on the editor when it gets activated!
133+ * @param msg
134+ */
135+ onActivateRequest ( msg : Message ) : void {
136+ this . editor . focus ( ) ;
137+ }
138+
120139 onCloseRequest ( msg : Message ) : void {
121140 if ( this . saved ) {
122141 showDialog ( {
@@ -150,7 +169,6 @@ export class CodeSnippetEditor extends ReactWidget {
150169 let target = event . target as HTMLElement ;
151170 while ( target && target . parentElement ) {
152171 if ( target . classList . contains ( 'jp-codeSnippetInput-editor' ) ) {
153- // console.log('hello');
154172 break ;
155173 }
156174 target = target . parentElement ;
@@ -244,15 +262,19 @@ export class CodeSnippetEditor extends ReactWidget {
244262 < input
245263 className = "jp-snippet-editor-name"
246264 defaultValue = { this . codeSnippet . displayName }
247- onClick = { ( event ) : void => this . activeFieldState ( event ) }
265+ onClick = { (
266+ event : React . MouseEvent < HTMLInputElement , MouseEvent >
267+ ) : void => this . activeFieldState ( event ) }
248268 > </ input >
249269 < label className = "jp-snippet-editor-description-label" >
250270 Description
251271 </ label >
252272 < input
253273 className = "jp-snippet-editor-description"
254274 defaultValue = { this . codeSnippet . description }
255- onClick = { ( event ) : void => this . activeFieldState ( event ) }
275+ onClick = { (
276+ event : React . MouseEvent < HTMLInputElement , MouseEvent >
277+ ) : void => this . activeFieldState ( event ) }
256278 > </ input >
257279 { /* <input
258280 className="jp-snippet-editor-language"
0 commit comments