diff --git a/blocks.js b/blocks.js index 3c9d5d31043818..bb7557938bf1c3 100644 --- a/blocks.js +++ b/blocks.js @@ -48,12 +48,8 @@ function selectBlock( event ) { event.stopPropagation(); event.target.className = 'is-selected'; - var position = event.target.getBoundingClientRect(); - - // Show switcher - controls.style.opacity = 1; - controls.style.top = ( position.top + 18 ) + 'px'; selectedBlock = event.target; + showControls( selectedBlock ); } function clearBlocks() { @@ -65,6 +61,12 @@ function clearBlocks() { hideControls(); } +function showControls( node ) { + var position = node.getBoundingClientRect(); + controls.style.opacity = 1; + controls.style.top = ( position.top + 18 ) + 'px'; +} + function hideControls() { controls.style.opacity = 0; } @@ -104,11 +106,13 @@ function attachControlActions() { if ( 'up' === classes ) { node.addEventListener( 'click', function() { + event.stopPropagation(); swapNodes( selectedBlock, getPreviousSibling( selectedBlock ) ); attachBlockHandlers(); }, false ); } else if ( 'down' === classes ) { node.addEventListener( 'click', function() { + event.stopPropagation(); swapNodes( selectedBlock, getNextSibling( selectedBlock ) ); attachBlockHandlers(); }, false ); @@ -126,6 +130,9 @@ function swapNodes( a, b ) { return false; } + // how do we handle controls? + showControls( b ); + // insert node copies before removal parent.replaceChild( b.cloneNode( true ), a ); parent.replaceChild( a.cloneNode( true ), b );