From 9cd0cfedf0ede4b8096b9839258875d1e8e2820c Mon Sep 17 00:00:00 2001 From: Timmy Crawford Date: Fri, 17 Feb 2017 14:40:55 -0800 Subject: [PATCH] Attempt to make controls appear in the correct spot --- blocks.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/blocks.js b/blocks.js index 8873400246e0d..4c1ce3e6a54ea 100644 --- a/blocks.js +++ b/blocks.js @@ -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'; @@ -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() { @@ -436,6 +464,7 @@ function setElementState( className, event ) { if ( className ) { selectedBlock.classList.add( className ); } + updateBlockControlsPosition( className ); } function l( data ) {