Skip to content

Commit a9967db

Browse files
author
minjk-bl
committed
Add codeview, codeexport menu to board
1 parent 5a5ff34 commit a9967db

File tree

4 files changed

+124
-9
lines changed

4 files changed

+124
-9
lines changed

html/boardFrame.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
<li data-menu="save-as">Save as</li>
2727
<hr class="vp-extra-menu-line">
2828
<li data-menu="run-all">Run All</li>
29+
<li data-menu="code-view">View Code</li>
30+
<li data-menu="code-export">Export Code</li>
2931
<li data-menu="view-depth">Show Depth Number</li>
3032
<hr class="vp-extra-menu-line">
3133
<li data-menu="clear">Clear Note</li>

js/board/BoardFrame.js

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ define([
2222
'../com/component/Component',
2323
'../com/component/FileNavigation',
2424
'./Block',
25-
'./BlockMenu'
26-
], function(boardFrameHtml, boardFrameCss, com_Config, com_String, com_util, com_interface, Component, FileNavigation, Block, BlockMenu) {
25+
'./BlockMenu',
26+
'./CodeView'
27+
], function(boardFrameHtml, boardFrameCss, com_Config, com_String, com_util, com_interface,
28+
Component, FileNavigation, Block, BlockMenu, CodeView) {
2729
'use strict';
2830
//========================================================================
2931
// Define Variable
@@ -102,6 +104,12 @@ define([
102104
case 'run-all':
103105
that.runAll();
104106
break;
107+
case 'code-view':
108+
that.viewCode();
109+
break;
110+
case 'code-export':
111+
that.exportCode();
112+
break;
105113
case 'view-depth':
106114
that.viewDepthInfo();
107115
break;
@@ -525,11 +533,10 @@ define([
525533
});
526534
fileNavi.open();
527535
}
528-
runBlock(block, execute=true) {
536+
runBlock(block, execute=true, addcell=true) {
529537
if (block.id == 'apps_markdown') {
530538
// if markdown, run single
531-
block.popup.run();
532-
return;
539+
return block.popup.run(execute, addcell);
533540
}
534541
let rootBlockDepth = block.depth;
535542
let groupedBlocks = block.getGroupedBlocks();
@@ -543,7 +550,10 @@ define([
543550
thisBlockCode = thisBlockCode.replaceAll('\n', '\n' + indent);
544551
code.appendFormat('{0}{1}{2}', prevNewLine, indent, thisBlockCode);
545552
});
546-
com_interface.insertCell('code', code.toString(), execute, block.blockNumber);
553+
if (addcell) {
554+
com_interface.insertCell('code', code.toString(), execute, block.blockNumber);
555+
}
556+
return code.toString();
547557
}
548558
runAll() {
549559
let that = this;
@@ -553,6 +563,57 @@ define([
553563
}
554564
})
555565
}
566+
getOverallCode() {
567+
let overallCode = new com_String();
568+
let that = this;
569+
this.blockList.forEach((block) => {
570+
if (block.isGroup) {
571+
if (overallCode.toString() != '') {
572+
overallCode.appendLine();
573+
overallCode.appendLine();
574+
}
575+
let groupCode = that.runBlock(block, false, false);
576+
if (block.id == 'apps_markdown') {
577+
// if markdown, add #
578+
groupCode = '#' + groupCode.replaceAll('\n', '\n# ');
579+
}
580+
overallCode.appendFormatLine('# VisualPython [{0}]{1}', block.blockNumber,
581+
block.id == 'apps_markdown'? ' - Markdown':'');
582+
overallCode.append(groupCode);
583+
}
584+
});
585+
return overallCode.toString();
586+
}
587+
viewCode() {
588+
let overallCode = this.getOverallCode();
589+
let codeview = new CodeView({
590+
codeview: overallCode,
591+
config: {
592+
id: 'boardCodeview',
593+
name: 'Overall Codeview',
594+
path: ''
595+
}
596+
});
597+
codeview.open();
598+
}
599+
exportCode() {
600+
let that = this;
601+
// save .py file
602+
let fileNavi = new FileNavigation({
603+
type: 'save',
604+
fileName: this.tmpState.boardTitle,
605+
extensions: ['py'],
606+
finish: function(filesPath, status, error) {
607+
let fileName = filesPath[0].file;
608+
let filePath = filesPath[0].path;
609+
610+
// save py file
611+
let overallCode = that.getOverallCode();
612+
vpKernel.saveFile(fileName, filePath, overallCode);
613+
}
614+
});
615+
fileNavi.open();
616+
}
556617
viewDepthInfo() {
557618
this.state.viewDepthNumber = !this.state.viewDepthNumber;
558619

js/board/CodeView.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Project Name : Visual Python
3+
* Description : GUI-based Python code generator
4+
* File Name : CodeView.js
5+
* Author : Black Logic
6+
* Note : Render Code view
7+
* License : GNU GPLv3 with Visual Python special exception
8+
* Date : 2021. 09. 13
9+
* Change Date :
10+
*/
11+
12+
//============================================================================
13+
// [CLASS] CodeView
14+
//============================================================================
15+
define([
16+
'../com/component/PopupComponent'
17+
], function(PopupComponent) {
18+
'use strict';
19+
20+
/**
21+
* @class CodeView
22+
* @constructor
23+
*/
24+
class CodeView extends PopupComponent {
25+
_init() {
26+
super._init();
27+
28+
this.config.footer = false;
29+
this.config.sizeLevel = 1;
30+
31+
this.state = {
32+
codeview: '',
33+
...this.state
34+
}
35+
36+
this._addCodemirror('codeview', this.wrapSelector('#codeview'), "readonly");
37+
}
38+
39+
templateForBody() {
40+
return `<textarea id="codeview">${this.state.codeview}</textarea>`;
41+
}
42+
}
43+
44+
return CodeView;
45+
46+
});

js/com/component/PopupComponent.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ define([
3939
* Component
4040
*/
4141
class PopupComponent extends Component {
42-
constructor(state={}, prop={}) {
42+
constructor(state={ config: { id: 'popup', name: 'Popup title', path: 'path/file' }}, prop={}) {
4343
super($('#site'), state, prop);
4444
}
4545

@@ -111,6 +111,8 @@ define([
111111

112112
/**
113113
* Add codemirror object
114+
* usage:
115+
* this._addCodemirror('code', this.wrapSelector('#code'));
114116
* @param {String} key stateKey
115117
* @param {String} selector textarea class name
116118
* @param {boolean} type code(python)/readonly/markdown
@@ -562,15 +564,19 @@ define([
562564
vpLog.display(VP_LOG_TYPE.DEVELOP, 'savedState', that.state);
563565
}
564566

565-
run(execute=true) {
567+
run(execute=true, addcell=true) {
568+
let code = this.generateCode();
566569
let mode = this.config.executeMode;
567570
let blockNumber = -1;
568571
// check if it's block
569572
if (this.getTaskType() == 'block') {
570573
let block = this.taskItem;
571574
blockNumber = block.blockNumber;
572575
}
573-
com_interface.insertCell(mode, this.generateCode(), execute, blockNumber);
576+
if (addcell) {
577+
com_interface.insertCell(mode, code, execute, blockNumber);
578+
}
579+
return code;
574580
}
575581

576582
/**

0 commit comments

Comments
 (0)