Skip to content

Commit 6604fbf

Browse files
committed
duplicate changes from #262
1 parent 107f417 commit 6604fbf

6 files changed

Lines changed: 103 additions & 17 deletions

File tree

index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@
8989
<li id="saveButton">Find</li>
9090
</ul>
9191
</div>
92+
<div class="menu" id="viewMenu">
93+
<span class="menuTitle">View</span>
94+
<ul class="menuContents">
95+
<li id="toggleHighlight">Highlight Off</li>
96+
</ul>
97+
</div>
9298
<div class="menu" id="shareMenu">
9399
<span class="menuTitle">Share</span>
94100
<ul class="menuContents">
@@ -197,5 +203,6 @@
197203
<script src='resources/js/blocks.js'></script>
198204
<script src='resources/js/block2json.js'></script>
199205
<script src='resources/js/fileManagement.js'></script>
206+
<script src='resources/js/menu.js'></script>
200207
</body>
201208
</html>

resources/css/blocks.css

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/css/blocks.scss

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,23 @@ body {
147147
background:#F45D5D;
148148
}
149149

150+
.highlightBlock {
151+
position: relative;
152+
z-index: 1;
153+
}
154+
.highlightBlock::after {
155+
content: "";
156+
display: block;
157+
background: gold;
158+
border-radius: 5px;
159+
position: absolute;
160+
top: -3px;
161+
right: -3px;
162+
bottom: -3px;
163+
left: -3px;
164+
z-index: -1;
165+
}
166+
150167
.remove-attr {
151168
display:inline-block;
152169
width:0px;
@@ -197,4 +214,4 @@ body {
197214
color:black;
198215
cursor:text;
199216
}
200-
}
217+
}

resources/js/block2json.js

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,39 @@ function blockToHTML(block) {
4141
}
4242
}
4343

44-
function blocksToJSON(fileName) {
45-
var ext = getExt(fileName);
46-
if (ext == 'html') {
47-
var expArray = [];
48-
for(let block of topLevelBlocks) {
49-
if(block) expArray.push(block.toStringable());
44+
function detachHtmlElem(block) {
45+
// attempt at garbage collection
46+
if(block.htmlElem) {
47+
block.htmlElem.removeEventListener('mouseover', block.block_mouse_over);
48+
block.htmlElem.removeEventListener('mouseout', block.block_mouse_out);
49+
if(block.htmlElem.parentNode) block.htmlElem.parentNode.removeChild(block.htmlElem);
50+
block.htmlElem = null;
51+
}
52+
}
53+
54+
function blockToHTML(block) {
55+
var out = null;
56+
detachHtmlElem(block)
57+
58+
if(block.type !== BLOCK_TYPES.stack && block.type !== BLOCK_TYPES.cblock) {
59+
out = null;
60+
} else if(block.name == 'text') {
61+
out = document.createTextNode(block.inputs[0].value);
62+
} else {
63+
out = document.createElement(block.name);
64+
for(let attr of block.attrs) {
65+
if(attr.name.trim() && attr.value.trim()) out.setAttribute(attr.name, attr.value);
5066
}
51-
fileData[fileName] = expArray;
52-
} else if (ext == 'css') {
53-
var expArray = [mainScript.toStringable()];
54-
for(let block of topLevelBlocks) {
55-
if(block && block != BODY && block.type != BLOCK_TYPES.CSSStart) {
56-
expArray.push(block.toStringable());
57-
}
67+
for(let child of block.children) {
68+
let parsedChild = blockToHTML(child);
69+
if(parsedChild) out.appendChild(parsedChild);
5870
}
59-
fileData[fileName] = expArray;
71+
block.htmlElem = out;
72+
73+
out.addEventListener('mouseover', block.block_mouse_over);
74+
out.addEventListener('mouseout', block.block_mouse_out);
6075
}
76+
return out;
6177
}
6278

6379
function setFrameContent(ext) {

resources/js/blocks.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var selected = null, // Object of the element to be moved
22
mousePos = {x: 0, y: 0}, // Stores x & y coordinates of the mouse pointer
33
dragOffset = {x: 0, y: 0}, // Stores offset between dragged element and mouse
4+
highlight = true,
45
DEFAULT_TEXT = 'breadfish',
56
SCRIPTING_AREA = $('.scriptingArea')[0];
67
var blocksDatabase, // all blocks by ID. Not an array in case we decide to use md5's or something later
@@ -68,6 +69,8 @@ function BlockWrapper(inPalette) {
6869
if(block.block_context_menu) block.elem.removeEventListener('contextmenu', block.block_context_menu);
6970
if(block.block_mouse_down) block.elem.removeEventListener('mousedown', block.block_mouse_down);
7071
if(block.block_mouse_up) block.elem.removeEventListener('mouseup', block.block_mouse_up);
72+
if(block.block_mouse_over) block.elem.removeEventListener('mouseover', block.block_mouse_over);
73+
if(block.block_mouse_out) block.elem.removeEventListener('mouseout', block.block_mouse_out);
7174
if(block.add_quicktext) block.quickText.removeEventListener('click', block.add_quicktext);
7275
if(block.add_attr_ev) block.addAttr.removeEventListener('click', block.add_attr_ev);
7376
if(block.remove_attr_ev) block.removeAttr.removeEventListener('click', block.remove_attr_ev);
@@ -278,8 +281,8 @@ function Block(type, name, opts) {
278281
this.addAttr = document.createElement('span');
279282
this.addAttr.classList.add('add-attr');
280283
this.attrControls.appendChild(this.addAttr);
281-
this.addAttr.addEventListener('click', block.add_attr_ev = function(e, name, value) {
282-
var attr = new BlockAttribute(name, value);
284+
this.addAttr.addEventListener('click', block.add_attr_ev = function(e) {
285+
var attr = new BlockAttribute();
283286
block.header.insertBefore(attr.elem, block.attrControls);
284287
block.attrs.push(attr);
285288
});
@@ -347,7 +350,23 @@ function Block(type, name, opts) {
347350
return false;
348351
}
349352

353+
this.elem.addEventListener('mouseover', block.block_mouse_over = function(ev) {
354+
if(highlight && block.htmlElem) {
355+
ev.stopPropagation();
356+
block.elem.classList.add('highlightBlock');
357+
block.htmlElem.style.outline = '3px solid gold';
358+
}
359+
});
360+
this.elem.addEventListener('mouseout', block.block_mouse_out = function(ev) {
361+
block.elem.classList.remove('highlightBlock');
362+
if(block.htmlElem) {
363+
block.htmlElem.style.outline = '';
364+
}
365+
});
366+
350367
this.elem.addEventListener('mousedown', block.block_mouse_down = function(ev) {
368+
block.block_mouse_out();
369+
detachHtmlElem(block)
351370
if (ev.which == 3
352371
|| testBlockContents(ev.target)) return;
353372
ev.stopPropagation();

resources/js/menu.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// toggle highlighting in menu-item
2+
var toggleHighlight = document.querySelector('#toggleHighlight');
3+
toggleHighlight.addEventListener('click', function(e) {
4+
if(highlight) {
5+
highlight = false;
6+
toggleHighlight.textContent = 'Highlight On';
7+
} else {
8+
highlight = true;
9+
toggleHighlight.textContent = 'Highlight Off';
10+
}
11+
});

0 commit comments

Comments
 (0)