Skip to content

Commit 0c91b7c

Browse files
author
minjk-bl
committed
CHROME: Fixed api to insert cells
1 parent 2b6c4ec commit 0c91b7c

File tree

1 file changed

+98
-35
lines changed

1 file changed

+98
-35
lines changed

js/com/com_interface.js

Lines changed: 98 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ define([
1414
], function(com_util, com_String) {
1515

1616
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+
}
1826
}
1927

2028
/**
@@ -25,9 +33,6 @@ define([
2533
* @param {int} sigNum
2634
*/
2735
var insertCell = function(type, command, exec=true, sigText='') {
28-
var selectedIndex = getSelectedCell();
29-
var targetCell = Jupyter.notebook.insert_cell_below(type, selectedIndex);
30-
3136
// Add signature
3237
if (type == 'code') {
3338
if (sigText !== '') {
@@ -36,20 +41,46 @@ define([
3641
command = '# Visual Python\n' + command;
3742
}
3843
}
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+
}
4960
}
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:
5083
}
51-
// move to executed cell
52-
Jupyter.notebook.scroll_to_cell(Jupyter.notebook.get_selected_index());
5384

5485
com_util.renderSuccessMessage('Your code is successfully generated.');
5586
}
@@ -62,9 +93,7 @@ define([
6293
* @param {int} sigNum
6394
*/
6495
var insertCells = function(type, commands, exec=true, sigText='') {
65-
var selectedIndex = getSelectedCell();
66-
var targetCell = Jupyter.notebook.insert_cell_below(type, selectedIndex);
67-
96+
6897
commands && commands.forEach((command, idx) => {
6998
// Add signature
7099
if (type == 'code') {
@@ -74,37 +103,71 @@ define([
74103
command = com_util.formatString('# Visual Python') + command;
75104
}
76105
}
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+
}
87140
}
88141
}
89-
90-
selectedIndex = getSelectedCell();
91-
targetCell = Jupyter.notebook.insert_cell_below(type, selectedIndex);
92142
});
93143

94144
// 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+
}
96151

97152
com_util.renderSuccessMessage('Your code is successfully generated.');
98153
}
99154

100155
var enableOtherShortcut = function() {
101156
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+
}
103162
}
104163

105164
var disableOtherShortcut = function() {
106165
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+
}
108171
}
109172

110173
return {

0 commit comments

Comments
 (0)