forked from visualpython/visualpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcom_interface.js
More file actions
109 lines (96 loc) · 3.43 KB
/
Copy pathcom_interface.js
File metadata and controls
109 lines (96 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Project Name : Visual Python
* Description : GUI-based Python code generator
* File Name : com_interface.js
* Author : Black Logic
* Note : Interface for jupyter
* License : GNU GPLv3 with Visual Python special exception
* Date : 2021. 09. 16
* Change Date :
*/
define([
'./com_util',
'./com_String'
], function(com_util, com_String) {
var getSelectedCell = function() {
return Jupyter.notebook.get_selected_index();
}
/**
*
* @param {String} type code / markdown
* @param {String} command
* @param {boolean} exec true(default) / false
* @param {int} sigNum
*/
var insertCell = function(type, command, exec=true, sigNum=-1) {
var selectedIndex = getSelectedCell();
var targetCell = Jupyter.notebook.insert_cell_below(type, selectedIndex);
// Add signature
if (type == 'code' && sigNum >= 0) {
command = com_util.formatString('# VisualPython [{0}]\n', sigNum) + command
}
targetCell.set_text(command);
Jupyter.notebook.select_next();
if (exec) {
switch (type) {
case "markdown":
targetCell.render();
break;
case "code":
default:
targetCell.execute();
}
}
// move to executed cell
Jupyter.notebook.scroll_to_cell(Jupyter.notebook.get_selected_index());
com_util.renderSuccessMessage('Your code has been executed');
}
/**
* Insert multiple cells
* @param {String} type
* @param {Array} commands
* @param {boolean} exec
* @param {int} sigNum
*/
var insertCells = function(type, commands, exec=true, sigNum=-1) {
var selectedIndex = getSelectedCell();
var targetCell = Jupyter.notebook.insert_cell_below(type, selectedIndex);
commands && commands.forEach((command, idx) => {
// Add signature
if (type == 'code' && sigNum >= 0) {
command = com_util.formatString('# VisualPython [{0}] - {1}\n', sigNum, idx + 1) + command
}
targetCell.set_text(command);
Jupyter.notebook.select_next();
if (exec) {
switch (type) {
case "markdown":
targetCell.render();
break;
case "code":
default:
targetCell.execute();
}
}
selectedIndex = getSelectedCell();
targetCell = Jupyter.notebook.insert_cell_below(type, selectedIndex);
});
// move to executed cell
Jupyter.notebook.scroll_to_cell(Jupyter.notebook.get_selected_index());
com_util.renderSuccessMessage('Your code has been executed');
}
var enableOtherShortcut = function() {
vpLog.display(VP_LOG_TYPE.DEVELOP, 'enable short cut');
Jupyter.notebook.keyboard_manager.enable();
}
var disableOtherShortcut = function() {
vpLog.display(VP_LOG_TYPE.DEVELOP, 'disable short cut');
Jupyter.notebook.keyboard_manager.disable();
}
return {
insertCell: insertCell,
insertCells: insertCells,
enableOtherShortcut: enableOtherShortcut,
disableOtherShortcut: disableOtherShortcut,
};
});