Skip to content

Commit 656feb2

Browse files
author
minjk-bl
committed
Add codeview menu to block, Handle block menu hidden problem
1 parent a9967db commit 656feb2

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

js/board/BlockMenu.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ define([
1616
'vp_base/js/com/com_util',
1717
'vp_base/js/com/com_String',
1818
'vp_base/js/com/component/Component',
19-
], function (com_util, com_String, Component) {
19+
'./CodeView'
20+
], function (com_util, com_String, Component, CodeView) {
2021
'use strict';
2122

2223
class BlockMenu extends Component {
@@ -32,6 +33,8 @@ define([
3233
left: 0,
3334
top: 0
3435
};
36+
37+
this.prevBlockCodeview = null;
3538
}
3639

3740
_bindEvent() {
@@ -48,7 +51,7 @@ define([
4851
});
4952
/** add block */
5053
$(this.wrapSelector('#vp_block_menu_add')).on('click', function () {
51-
that.boardFrame.runBlock(that.block, false);
54+
that.boardFrame.runBlock(that.block, true, false);
5255
that.close();
5356
});
5457
/** duplicate block */
@@ -62,6 +65,36 @@ define([
6265
that.boardFrame.removeBlock(that.block);
6366
that.close();
6467
});
68+
/** code view */
69+
$(this.wrapSelector('#vp_block_menu_codeview')).on('click', function() {
70+
let overallCode = new com_String();
71+
let groupCode = that.boardFrame.runBlock(that.block, false, false);
72+
if (that.block.id == 'apps_markdown') {
73+
// if markdown, add #
74+
groupCode = '#' + groupCode.replaceAll('\n', '\n# ');
75+
}
76+
overallCode.appendFormatLine('# VisualPython [{0}]{1}', that.block.blockNumber,
77+
that.block.id == 'apps_markdown'? ' - Markdown':'');
78+
overallCode.append(groupCode);
79+
80+
// open codeview
81+
let codeview = new CodeView({
82+
codeview: overallCode.toString(),
83+
config: {
84+
id: 'blockCodeview',
85+
name: 'Block Codeview',
86+
path: ''
87+
}
88+
});
89+
if (that.prevBlockCodeview != null) {
90+
// remove prev code view
91+
that.prevBlockCodeview.remove();
92+
}
93+
that.prevBlockCodeview = codeview;
94+
codeview.open();
95+
96+
that.close();
97+
});
6598
}
6699

67100
template() {
@@ -79,6 +112,9 @@ define([
79112
sbBlockMenu.appendLine('<div id="vp_block_menu_duplicate" class="vp-block-menu-item">Duplicate</div>');
80113
// delete button
81114
sbBlockMenu.appendLine('<div id="vp_block_menu_delete" class="vp-block-menu-item">Delete</div>');
115+
// codeview button
116+
sbBlockMenu.appendLine('<hr class="vp-extra-menu-line" id="vp_block_menu_line_2">');
117+
sbBlockMenu.appendLine('<div id="vp_block_menu_codeview" class="vp-block-menu-item">Code view</div>');
82118
sbBlockMenu.appendLine('</div>');
83119
return sbBlockMenu.toString();
84120
}
@@ -89,6 +125,20 @@ define([
89125
left: left,
90126
top: top
91127
};
128+
129+
// handling menu box to show inside visible area
130+
let docWidth = $(document).width();
131+
let docHeight = $(document).height();
132+
let menuWidth = $(this.wrapSelector()).outerWidth();
133+
let menuHeight = $(this.wrapSelector()).outerHeight();
134+
if (docWidth < left + menuWidth) {
135+
// horizontally out of view
136+
this.position.left -= menuWidth;
137+
}
138+
if (docHeight < top + menuHeight) {
139+
// vertically out of view
140+
this.position.top -= menuHeight;
141+
}
92142

93143
$(this.wrapSelector()).css(this.position);
94144
// show items

0 commit comments

Comments
 (0)