Skip to content

Commit

Permalink
Merge pull request #95 from WordPress/fix/92
Browse files Browse the repository at this point in the history
Attempt to make controls appear in the correct spot
  • Loading branch information
mtias authored Feb 20, 2017
2 parents 8967e57 + 9cd0cfe commit 365d6c3
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ function showControls( node ) {
switcherButtons.forEach( function( element ) {
element.style.display = 'none';
} );

var blockType = getTagType( node.nodeName );
var switcherQuery = '.type-icon-' + blockType;
queryFirst( switcherQuery ).style.display = 'block';
Expand All @@ -185,8 +186,35 @@ function showControls( node ) {
blockControls.style.display = 'block';

// reposition block-specific block controls
blockControls.style.top = ( position.top - 36 + window.scrollY ) + 'px';
updateBlockControlsPosition();
}

function updateBlockControlsPosition( newClassName ) {
var isImage = selectedBlock.tagName === 'IMG';
var className = selectedBlock.className;
var position = selectedBlock.getBoundingClientRect();
var alignedRight = className.match( /align-right/ );
var alignedLeft = className.match( /align-left/ );
var fullBleed = className.match( /full-bleed/ );

var topPosition = position.top - 36 + window.scrollY;
var leftPosition = null;

if ( isImage && alignedRight ) {
leftPosition = position.left;
topPosition = newClassName ? topPosition - 15 : topPosition;
} else if ( isImage && alignedLeft && newClassName ) {
topPosition = topPosition - 15;
} else if ( isImage && className === 'is-selected' && blockControls.style.left ) {
leftPosition = null;
topPosition = topPosition + 15;
} else if ( fullBleed ) {
leftPosition = ( window.innerWidth / 2 ) - ( blockControls.clientWidth / 2 );
}

blockControls.style.maxHeight = 'none';
blockControls.style.top = topPosition + 'px';
blockControls.style.left = leftPosition ? leftPosition + 'px' : null;
}

function hideControls() {
Expand Down Expand Up @@ -436,6 +464,7 @@ function setElementState( className, event ) {
if ( className ) {
selectedBlock.classList.add( className );
}
updateBlockControlsPosition( className );
}

function l( data ) {
Expand Down

0 comments on commit 365d6c3

Please sign in to comment.