Skip to content

Commit

Permalink
Merge pull request #9 from Automattic/update/selection-state
Browse files Browse the repository at this point in the history
Update selection state after moving a block
  • Loading branch information
mtias authored Feb 8, 2017
2 parents 9af9f25 + e619a7c commit 001f8d1
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 );
Expand All @@ -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 );
Expand Down

0 comments on commit 001f8d1

Please sign in to comment.