@@ -14,7 +14,15 @@ define([
14
14
] , function ( com_util , com_String ) {
15
15
16
16
var getSelectedCell = function ( ) {
17
- return Jupyter . notebook . get_selected_index ( ) ;
17
+ if ( vpConfig . extensionType === 'notebook' ) {
18
+ return Jupyter . notebook . get_selected_index ( ) ;
19
+ } else if ( vpConfig . extensionType === 'chrome' ) {
20
+ if ( colab . global . notebook . focusedCell ) {
21
+ return colab . global . notebook . focusedCell . cellId ;
22
+ } else {
23
+ return '' ;
24
+ }
25
+ }
18
26
}
19
27
20
28
/**
@@ -25,9 +33,6 @@ define([
25
33
* @param {int } sigNum
26
34
*/
27
35
var insertCell = function ( type , command , exec = true , sigText = '' ) {
28
- var selectedIndex = getSelectedCell ( ) ;
29
- var targetCell = Jupyter . notebook . insert_cell_below ( type , selectedIndex ) ;
30
-
31
36
// Add signature
32
37
if ( type == 'code' ) {
33
38
if ( sigText !== '' ) {
@@ -36,20 +41,46 @@ define([
36
41
command = '# Visual Python\n' + command ;
37
42
}
38
43
}
39
- targetCell . set_text ( command ) ;
40
- Jupyter . notebook . select_next ( ) ;
41
- if ( exec ) {
42
- switch ( type ) {
43
- case "markdown" :
44
- targetCell . render ( ) ;
45
- break ;
46
- case "code" :
47
- default :
48
- targetCell . execute ( ) ;
44
+
45
+ if ( vpConfig . extensionType === 'notebook' ) {
46
+ var selectedIndex = getSelectedCell ( ) ;
47
+ var targetCell = Jupyter . notebook . insert_cell_below ( type , selectedIndex ) ;
48
+
49
+ targetCell . set_text ( command ) ;
50
+ Jupyter . notebook . select_next ( ) ;
51
+ if ( exec ) {
52
+ switch ( type ) {
53
+ case "markdown" :
54
+ targetCell . render ( ) ;
55
+ break ;
56
+ case "code" :
57
+ default :
58
+ targetCell . execute ( ) ;
59
+ }
49
60
}
61
+ // move to executed cell
62
+ Jupyter . notebook . scroll_to_cell ( Jupyter . notebook . get_selected_index ( ) ) ;
63
+ } else if ( vpConfig . extensionType === 'chrome' ) {
64
+ // CHROME: use colab api to add cell
65
+ colab . global . notebook . notebookToolbar . toolbarButtons . get ( "insert-cell-below" ) . click ( ) ;
66
+ let cell = colab . global . notebook . focusedCell ;
67
+ cell . setText ( command ) ;
68
+ if ( exec ) {
69
+ switch ( type ) {
70
+ case "markdown" :
71
+ // trigger esc
72
+ var esc = $ . Event ( "keydown" , { keyCode : 27 } ) ;
73
+ // cell.trigger(esc); // CHROME: FIXME:
74
+ console . log ( 'this is your cell' , cell ) ;
75
+ break ;
76
+ case "code" :
77
+ default :
78
+ cell . runButton . click ( ) ;
79
+ }
80
+ }
81
+ // move to executed cell
82
+ // CHROME: TODO:
50
83
}
51
- // move to executed cell
52
- Jupyter . notebook . scroll_to_cell ( Jupyter . notebook . get_selected_index ( ) ) ;
53
84
54
85
com_util . renderSuccessMessage ( 'Your code is successfully generated.' ) ;
55
86
}
@@ -62,9 +93,7 @@ define([
62
93
* @param {int } sigNum
63
94
*/
64
95
var insertCells = function ( type , commands , exec = true , sigText = '' ) {
65
- var selectedIndex = getSelectedCell ( ) ;
66
- var targetCell = Jupyter . notebook . insert_cell_below ( type , selectedIndex ) ;
67
-
96
+
68
97
commands && commands . forEach ( ( command , idx ) => {
69
98
// Add signature
70
99
if ( type == 'code' ) {
@@ -74,37 +103,71 @@ define([
74
103
command = com_util . formatString ( '# Visual Python' ) + command ;
75
104
}
76
105
}
77
- targetCell . set_text ( command ) ;
78
- Jupyter . notebook . select_next ( ) ;
79
- if ( exec ) {
80
- switch ( type ) {
81
- case "markdown" :
82
- targetCell . render ( ) ;
83
- break ;
84
- case "code" :
85
- default :
86
- targetCell . execute ( ) ;
106
+
107
+ if ( vpConfig . extensionType === 'notebook' ) {
108
+ var selectedIndex = getSelectedCell ( ) ;
109
+ var targetCell = Jupyter . notebook . insert_cell_below ( type , selectedIndex ) ;
110
+
111
+ targetCell . set_text ( command ) ;
112
+ Jupyter . notebook . select_next ( ) ;
113
+ if ( exec ) {
114
+ switch ( type ) {
115
+ case "markdown" :
116
+ targetCell . render ( ) ;
117
+ break ;
118
+ case "code" :
119
+ default :
120
+ targetCell . execute ( ) ;
121
+ }
122
+ }
123
+ } else if ( vpConfig . extensionType === 'chrome' ) {
124
+ // CHROME: use colab api to add cell
125
+ colab . global . notebook . notebookToolbar . toolbarButtons . get ( "insert-cell-below" ) . click ( ) ;
126
+ let cell = colab . global . notebook . focusedCell ;
127
+ cell . setText ( command ) ;
128
+ if ( exec ) {
129
+ switch ( type ) {
130
+ case "markdown" :
131
+ // trigger esc
132
+ var esc = $ . Event ( "keydown" , { keyCode : 27 } ) ;
133
+ // cell.trigger(esc); // CHROME: FIXME:
134
+ console . log ( 'this is your cell' , cell ) ;
135
+ break ;
136
+ case "code" :
137
+ default :
138
+ cell . runButton . click ( ) ;
139
+ }
87
140
}
88
141
}
89
-
90
- selectedIndex = getSelectedCell ( ) ;
91
- targetCell = Jupyter . notebook . insert_cell_below ( type , selectedIndex ) ;
92
142
} ) ;
93
143
94
144
// move to executed cell
95
- Jupyter . notebook . scroll_to_cell ( Jupyter . notebook . get_selected_index ( ) ) ;
145
+ if ( vpConfig . extensionType === 'notebook' ) {
146
+ Jupyter . notebook . scroll_to_cell ( Jupyter . notebook . get_selected_index ( ) ) ;
147
+ } else if ( vpConfig . extensionType === 'chrome' ) {
148
+ // CHROME: TODO:
149
+
150
+ }
96
151
97
152
com_util . renderSuccessMessage ( 'Your code is successfully generated.' ) ;
98
153
}
99
154
100
155
var enableOtherShortcut = function ( ) {
101
156
vpLog . display ( VP_LOG_TYPE . DEVELOP , 'enable short cut' ) ;
102
- Jupyter . notebook . keyboard_manager . enable ( ) ;
157
+ if ( vpConfig . extensionType == 'notebook' ) {
158
+ Jupyter . notebook . keyboard_manager . enable ( ) ;
159
+ } else if ( vpConfig . extensionType == 'chrome' ) {
160
+ ;
161
+ }
103
162
}
104
163
105
164
var disableOtherShortcut = function ( ) {
106
165
vpLog . display ( VP_LOG_TYPE . DEVELOP , 'disable short cut' ) ;
107
- Jupyter . notebook . keyboard_manager . disable ( ) ;
166
+ if ( vpConfig . extensionType == 'notebook' ) {
167
+ Jupyter . notebook . keyboard_manager . disable ( ) ;
168
+ } else if ( vpConfig . extensionType == 'chrome' ) {
169
+ ;
170
+ }
108
171
}
109
172
110
173
return {
0 commit comments