@@ -34,10 +34,21 @@ export default{
34
34
render ( { model, el} ) {
35
35
36
36
var unique_funcbody_id = generateRandomString ( 6 )
37
+
38
+ var docstring_present = 0 ;
37
39
38
40
39
41
model . attributes = { function_body_id :unique_funcbody_id }
40
-
42
+ const theTextareaId = model . attributes [ 'function_body_id' ] ;
43
+ const docstringId = theTextareaId + '-docstring' ;
44
+ let docstringTextArea = null ; // Define it at a higher scope
45
+
46
+
47
+
48
+ // print the value of docstring to the console
49
+ console . log ( "docstring = %s" , model . get ( 'docstring' ) ) ;
50
+
51
+
41
52
// Note: I think this inadvertanly changes the code cells inside the entire notebook
42
53
const cssStyles =
43
54
'<style>\
@@ -50,11 +61,18 @@ export default{
50
61
51
62
52
63
53
- const theTextareaId = model . attributes [ 'function_body_id' ] ;
64
+
65
+
54
66
console . log ( "theTextareaId = %s" , theTextareaId ) ;
55
67
let textArea = document . createElement ( "textArea" ) ;
56
68
el . appendChild ( textArea )
57
-
69
+
70
+
71
+
72
+
73
+ // if docstring is not empty, create outer html with docstring
74
+ if ( model . get ( 'docstring' ) !== '' ) {
75
+ docstring_present = 1 ;
58
76
textArea . outerHTML = cssStyles +
59
77
'<textarea id="' +
60
78
theTextareaId +
@@ -64,8 +82,35 @@ export default{
64
82
'-docstring" class="CodeMirror.widget-code-input-docstring"></textarea>' +
65
83
'<textarea id="' +
66
84
theTextareaId +
67
- '-body" class="CodeMirror.widget-code-input-body"></textarea>' ;
85
+ '-body" class="CodeMirror.widget-code-input-body"></textarea>' ;
68
86
87
+ // const docstringId = theTextareaId + '-docstring';
88
+
89
+ } else {
90
+ // if docstring is empty, create outer html without docstring
91
+ textArea . outerHTML = cssStyles +
92
+ '<textarea id="' +
93
+ theTextareaId +
94
+ '-signature" class="CodeMirror.widget-code-input-signature"></textarea>' +
95
+ '<textarea id="' +
96
+ theTextareaId +
97
+ '-body" class="CodeMirror.widget-code-input-body"></textarea>' ;
98
+ }
99
+
100
+
101
+
102
+ docstringTextArea = document . getElementById ( docstringId ) ;
103
+ console . log ( "docstringTextArea = %s" , docstringTextArea ) ;
104
+ // textArea.outerHTML = cssStyles +
105
+ // '<textarea id="' +
106
+ // theTextareaId +
107
+ // '-signature" class="CodeMirror.widget-code-input-signature"></textarea>' +
108
+ // '<textarea id="' +
109
+ // theTextareaId +
110
+ // '-docstring" class="CodeMirror.widget-code-input-docstring"></textarea>' +
111
+ // '<textarea id="' +
112
+ // theTextareaId +
113
+ // '-body" class="CodeMirror.widget-code-input-body"></textarea>';
69
114
70
115
function editorFromTextArea ( textarea , extensions ) {
71
116
let view = new EditorView ( { state : EditorState . create ( { doc : textarea . value , extensions} ) } )
@@ -85,7 +130,7 @@ export default{
85
130
var theme_compartment_sig = new Compartment ;
86
131
var theme_compartment_doc = new Compartment ;
87
132
var theme_compartment_body = new Compartment ;
88
-
133
+ var myDocstringCodeMirror ;
89
134
90
135
const mySignatureCodeMirror = editorFromTextArea ( document . getElementById ( theTextareaId + '-signature' ) , [ EditorState . readOnly . of ( true ) , lineNumbers ( ) , indentUnit . of ( " " )
91
136
,
@@ -115,8 +160,11 @@ export default{
115
160
) ;
116
161
117
162
mySignatureCodeMirror . dom . classList . add ( "widget-code-input-signature" )
118
-
119
- var myDocstringCodeMirror = editorFromTextArea ( document . getElementById ( theTextareaId + '-docstring' ) , [ lineNumbers ( ) , EditorState . readOnly . of ( true ) , EditorView . editorAttributes . of ( { class :"widget-code-input-docstring" } ) , gutter ( { class :"forced-indent" } ) , guttercomp . of ( lineNumbers ( ) ) ,
163
+
164
+
165
+ if ( model . get ( 'docstring' ) !== '' ) {
166
+
167
+ myDocstringCodeMirror = editorFromTextArea ( document . getElementById ( theTextareaId + '-docstring' ) , [ lineNumbers ( ) , EditorState . readOnly . of ( true ) , EditorView . editorAttributes . of ( { class :"widget-code-input-docstring" } ) , gutter ( { class :"forced-indent" } ) , guttercomp . of ( lineNumbers ( ) ) ,
120
168
121
169
history ( ) ,
122
170
drawSelection ( ) ,
@@ -137,6 +185,15 @@ export default{
137
185
...completionKeymap ,
138
186
...lintKeymap ,
139
187
] ) , python ( ) , indentUnit . of ( " " ) , theme_compartment_doc . of ( basicLight ) ] ) ;
188
+ }
189
+
190
+ // if (model.get('docstring') !== '') {
191
+ // console.log("docstring is not empty")
192
+ // document.getElementById(theTextareaId + '-docstring').style.display = "block";
193
+ // } else {
194
+ // console.log("docstring is empty")
195
+ // document.getElementById(theTextareaId + '-docstring').style.display = "none";
196
+ // }
140
197
141
198
142
199
mySignatureCodeMirror . dom . classList . add ( "widget-code-input-signature" ) ; // add css for border to docstring element
@@ -175,7 +232,7 @@ export default{
175
232
176
233
model . set ( "function_body" , myBodyCodeMirror . state . doc . toString ( ) ) ;
177
234
model . save_changes ( ) ;
178
- console . log ( "BodyChange event registered. " ) ;
235
+ console . log ( "BodyChange event registered! " ) ;
179
236
}
180
237
} ) ;
181
238
@@ -205,34 +262,171 @@ function signatureValueChanged() {
205
262
206
263
function updateLineNumbers ( ) {
207
264
const linesSignature = mySignatureCodeMirror . state . doc . toString ( ) . split ( '\n' ) . length ;
208
- const linesDocstring = myDocstringCodeMirror . state . doc . toString ( ) . split ( '\n' ) . length ;
265
+ var docstring = '' ;
266
+ var linesDocstring = 0 ;
267
+ console . log ( "update line numbers. docstring_present = %s" , docstring_present ) ;
268
+ console . log ( "myDocstringcodemirror = %s" , myDocstringCodeMirror ) ;
269
+ if ( model . get ( 'docstring' ) !== '' && myDocstringCodeMirror ) {
270
+ docstring = myDocstringCodeMirror . state . doc . toString ( ) ;
271
+ linesDocstring = docstring . split ( '\n' ) . length ;
272
+
209
273
274
+ console . log ( "dispatch docstring linum update." ) ;
275
+ // increment line numbers in docstring text area by the number of lines in the signature
210
276
211
- // increment line numbers in docstring text area by the number of lines in the signature
212
277
myDocstringCodeMirror . dispatch ( {
213
- effects : guttercomp . reconfigure (
214
- lineNumbers ( { formatNumber : n => linesSignature + n } ) )
278
+ effects : guttercomp . reconfigure (
279
+ lineNumbers ( { formatNumber : n => linesDocstring == 0 ? "" : linesSignature + n } ) )
215
280
} ) ;
216
-
281
+ }
282
+ console . log ( "linesSignature = %s" , linesSignature ) ;
283
+ console . log ( "linesDocstring = %s" , linesDocstring ) ;
217
284
myBodyCodeMirror . dispatch ( {
218
- effects : guttercomp . reconfigure (
219
- lineNumbers ( { formatNumber : n => linesSignature + linesDocstring + n } ) )
285
+ effects : guttercomp . reconfigure (
286
+ lineNumbers ( { formatNumber : n => linesSignature + linesDocstring + n } ) )
220
287
} ) ;
221
288
222
289
}
290
+
291
+
223
292
function docstringValueChanged ( ) {
224
293
console . log ( "updating docstring" ) ;
225
294
// Set the value from python into the CodeMirror widget in the
226
295
// frontend.
227
296
228
- const newDocstring = '"""' + model . get ( 'docstring' ) + '"""' ;
297
+ const newDocstring = model . get ( 'docstring' ) ;
298
+ const docstringElement = document . getElementById ( theTextareaId + '-docstring' ) ;
299
+
300
+ console . log ( "docstring_present = %s" , docstring_present ) ;
301
+ // if user has changed the docstring from empty to non-empty, add the
302
+
303
+ docstringTextArea = document . getElementById ( docstringId ) ;
304
+
305
+ console . log ( "docstring is present" ) ;
306
+ console . log ( "newDocstring = %s" , newDocstring ) ;
307
+ if ( newDocstring == '' ) {
308
+ console . log ( "New docstring is None" ) ;
309
+ docstring_present == 0 ;
310
+ if ( docstringTextArea ) {
311
+ console . log ( "docstringTextareaId = %s" , docstringId ) ;
312
+
313
+ // docstringTextArea.remove(); // remove docstring text area
314
+ myDocstringCodeMirror . destroy ( ) ;
315
+ console . log ( "destroyed docstring" ) ;
316
+ if ( docstringElement && docstringElement . parentNode ) {
317
+ console . log ( "removing docstring element" ) ;
318
+ docstringElement . parentNode . removeChild ( docstringElement ) ;
319
+ console . log ( "removed docstring element" ) ;
320
+ }
321
+
322
+ // Clear the reference
323
+ myDocstringCodeMirror = null ;
324
+ console . log ( "cleared docstring" ) ;
325
+ docstringTextArea = null ;
326
+ console . log ( "cleared docstring textarea" ) ;
327
+
328
+ }
329
+
330
+ } else if ( docstringTextArea == null ) { //
331
+ // add back docstring text area
332
+ // Find the sibling element (body) and insert the new docstring before it
333
+
334
+ const signatureTextArea = document . getElementById ( `${ theTextareaId } -signature` ) ;
335
+
336
+ if ( signatureTextArea ) {
337
+ // Create the new docstring textarea
338
+ const docstringTextArea = document . createElement ( 'textarea' ) ;
339
+ docstringTextArea . id = `${ theTextareaId } -docstring` ;
340
+ docstringTextArea . classList . add ( 'widget-code-input-docstring' ) ;
341
+ signatureTextArea . insertAdjacentElement ( 'beforebegin' , docstringTextArea ) ;
342
+
343
+ // apply old theme
344
+ var theme ;
345
+ var theme_string = model . get ( "code_theme" )
346
+
347
+ var themeMap = {
348
+ "nord" : nord ,
349
+ "solarizedLight" : solarizedLight ,
350
+ "basicLight" : basicLight
351
+ } ;
352
+
353
+ if ( theme_string in themeMap ) {
354
+ theme = themeMap [ theme_string ] ;
355
+ } else {
356
+ throw new Error ( "Specified code theme is not supported." ) ;
357
+ }
358
+
229
359
230
- if ( newDocstring !== myDocstringCodeMirror . state . doc . toString ( ) ) {
360
+ // Initialize CodeMirror for the new textarea
361
+ myDocstringCodeMirror = editorFromTextArea ( docstringTextArea , [
362
+ lineNumbers ( ) ,
363
+ EditorState . readOnly . of ( true ) ,
364
+ EditorView . editorAttributes . of ( { class : 'widget-code-input-docstring' } ) ,
365
+ gutter ( { class : 'forced-indent' } ) , guttercomp . of ( lineNumbers ( ) ) ,
366
+ syntaxHighlighting ( defaultHighlightStyle , { fallback : true } ) ,
367
+ python ( ) ,
368
+ indentUnit . of ( ' ' ) ,
369
+ theme_compartment_doc . of ( theme ) ,
370
+ ] ) ;
371
+
372
+ // Set the initial value for the new editor
373
+ myDocstringCodeMirror . dispatch ( {
374
+ changes : { from : 0 , to : myDocstringCodeMirror . state . doc . length , insert : newDocstring } ,
375
+ } ) ;
376
+
377
+ docstring_present = 1 ;
378
+ }
379
+
380
+ // docstring_present = 1;
381
+ // textArea.outerHTML = cssStyles +
382
+ // '<textarea id="' +
383
+ // theTextareaId +
384
+ // '-signature" class="CodeMirror.widget-code-input-signature"></textarea>' +
385
+ // '<textarea id="' +
386
+ // theTextareaId +
387
+ // '-docstring" class="CodeMirror.widget-code-input-docstring"></textarea>' +
388
+ // '<textarea id="' +
389
+ // theTextareaId +
390
+ // '-body" class="CodeMirror.widget-code-input-body"></textarea>';
391
+
392
+
393
+ // myDocstringCodeMirror = editorFromTextArea(document.getElementById(theTextareaId + '-docstring'), [lineNumbers(), EditorState.readOnly.of(true), EditorView.editorAttributes.of({class:"widget-code-input-docstring"}), gutter({class:"forced-indent"}), guttercomp.of(lineNumbers()),
394
+
395
+ // history(),
396
+ // drawSelection(),
397
+ // dropCursor(),
398
+ // EditorState.allowMultipleSelections.of(true),
399
+ // syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
400
+ // bracketMatching(),
401
+ // closeBrackets(),
402
+ // autocompletion(),
403
+ // rectangularSelection(),
404
+ // crosshairCursor(),
405
+ // keymap.of([
406
+ // ...closeBracketsKeymap,
407
+ // ...defaultKeymap,
408
+ // ...searchKeymap,
409
+ // ...historyKeymap,
410
+ // ...foldKeymap,
411
+ // ...completionKeymap,
412
+ // ...lintKeymap,
413
+ // ]), python(), indentUnit.of(" "), theme_compartment_doc.of(basicLight)]);
414
+ // }
415
+
416
+
417
+ } else {
418
+
419
+ // if (newDocstring !== myDocstringCodeMirror.state.doc.toString()) {
420
+ console . log ( "dispatch docstring update" ) ;
231
421
myDocstringCodeMirror . dispatch ( {
232
422
changes : { from : 0 , to : myDocstringCodeMirror . state . doc . length , insert : newDocstring }
233
423
} )
234
- }
235
- updateLineNumbers ( ) ;
424
+ // }
425
+ // Add docstring now
426
+ // const docstringId = theTextareaId + '-docstring';
427
+ // let docstringTextArea = document.getElementById(docstringId);
428
+ }
429
+ updateLineNumbers ( ) ;
236
430
}
237
431
238
432
function bodyValueChanged ( ) {
@@ -269,9 +463,11 @@ function bodyValueChanged() {
269
463
mySignatureCodeMirror . dispatch ( {
270
464
effects : theme_compartment_sig . reconfigure ( theme )
271
465
} )
466
+ if ( model . get ( 'docstring' ) !== '' ) {
272
467
myDocstringCodeMirror . dispatch ( {
273
468
effects : theme_compartment_doc . reconfigure ( theme )
274
469
} )
470
+ }
275
471
myBodyCodeMirror . dispatch ( {
276
472
effects : theme_compartment_body . reconfigure ( theme )
277
473
} )
@@ -282,7 +478,9 @@ function bodyValueChanged() {
282
478
283
479
284
480
signatureValueChanged ( ) ;
481
+
285
482
docstringValueChanged ( ) ;
483
+
286
484
bodyValueChanged ( ) ;
287
485
themeChanged ( ) ;
288
486
0 commit comments