Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple kinds for each block type #35

Merged
merged 1 commit into from
Feb 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 65 additions & 35 deletions blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,43 @@
*/
var getNextSibling = siblingGetter( 'next' );
var getPreviousSibling = siblingGetter( 'previous' );
var getTagType = getConfig.bind( null, 'tagTypes' );
var getTypeKinds = getConfig.bind( null, 'typeKinds' );

/**
* Globals
*/
var editor = document.getElementsByClassName( 'editor' )[0];
var switcher = document.getElementsByClassName( 'block-switcher' )[0];
var blockControls = document.getElementsByClassName( 'block-controls' )[0];
var inlineControls = document.getElementsByClassName( 'inline-controls' )[0];
var insertBlockButton = document.getElementsByClassName( 'insert-block__button' )[0];
var config = {
tagTypes: {
'IMG': 'image',
'H1': 'heading',
'H2': 'heading',
'H3': 'heading',
'H4': 'heading',
'H5': 'heading',
'H6': 'heading',
'default': 'paragraph'
},
typeKinds: {
'paragraph': [ 'text' ],
'heading': [ 'heading', 'text' ],
'image': [ 'image' ],
'default': []
}
};
var editor = queryFirst( '.editor' );
var switcher = queryFirst( '.block-switcher' );
var switcherButtons = query( '.block-switcher .type svg' );
var blockControls = queryFirst( '.block-controls' );
var inlineControls = queryFirst( '.inline-controls' );
var insertBlockButton = queryFirst( '.insert-block__button' );
var imageFullBleed = queryFirst( '.block-image__full-width' );
var imageAlignNone = queryFirst( '.block-image__no-align' );
var imageAlignLeft = queryFirst( '.block-image__align-left' );
var imageAlignRight = queryFirst( '.block-image__align-right' );
var selectedBlock = null;


/**
* Initialization
*/
Expand All @@ -32,8 +58,7 @@ attachControlActions();
* Core logic
*/
function attachBlockHandlers() {
var blocks = getBlocks();
Array.from( blocks ).forEach( function( block ) {
getBlocks().forEach( function( block ) {
block.removeEventListener( 'click', selectBlock, false );
block.addEventListener( 'click', selectBlock, false );
} );
Expand All @@ -54,7 +79,7 @@ function selectBlock( event ) {
}

function clearBlocks() {
Array.from( getBlocks() ).forEach( function( block ) {
getBlocks().forEach( function( block ) {
block.className = block.className.replace( 'is-selected', '' );
} );
var selectedBlock = null;
Expand All @@ -64,44 +89,35 @@ function clearBlocks() {
}

function showControls( node ) {
// show element type
document.getElementsByClassName( 'type-icon-image' )[0].style.display = 'none';
document.getElementsByClassName( 'type-icon-heading' )[0].style.display = 'none';
document.getElementsByClassName( 'type-icon-paragraph' )[0].style.display = 'none';
if ( node.nodeName == 'IMG' ) {
document.getElementsByClassName( 'type-icon-image' )[0].style.display = 'block';
blockControls.className = 'block-controls is-image';
} else if ( node.nodeName == 'H1' || node.nodeName == 'H2' || node.nodeName == 'H3' || node.nodeName == 'H4' || node.nodeName == 'H5' || node.nodeName == 'H6' ) {
document.getElementsByClassName( 'type-icon-heading' )[0].style.display = 'block';
blockControls.className = 'block-controls is-heading';
} else {
document.getElementsByClassName( 'type-icon-paragraph' )[0].style.display = 'block';
blockControls.className = 'block-controls is-text';
}

var imageFullBleed = document.getElementsByClassName( 'block-image__full-width' )[0];
var imageAlignNone = document.getElementsByClassName( 'block-image__no-align' )[0];
var imageAlignLeft = document.getElementsByClassName( 'block-image__align-left' )[0];
var imageAlignRight = document.getElementsByClassName( 'block-image__align-right' )[0];
imageFullBleed.addEventListener( 'click', setImageFullBleed, false );
imageAlignNone.addEventListener( 'click', setImageAlignNone, false );
imageAlignLeft.addEventListener( 'click', setImageAlignLeft, false );
imageAlignRight.addEventListener( 'click', setImageAlignRight, false );
// toggle block-specific switcher
switcherButtons.forEach( function( element ) {
element.style.display = 'none';
} );
var blockType = getTagType( node.nodeName );
var switcherQuery = '.type-icon-' + blockType;
queryFirst( switcherQuery ).style.display = 'block';

// show controls
// reposition switcher
var position = node.getBoundingClientRect();
switcher.style.opacity = 1;
switcher.style.top = ( position.top + 18 + window.scrollY ) + 'px';

// show/hide block-specific block controls
var kinds = getTypeKinds( blockType );
var kindClasses = kinds.map( function( kind ) {
return 'is-' + kind;
} ).join( ' ' );
blockControls.className = 'block-controls ' + kindClasses;
blockControls.style.display = 'block';

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

function hideControls() {
switcher.style.opacity = 0;
blockControls.style.display = 'none';
blockControls.style.maxHeight = 0;
}

// Show popup on text selection
Expand Down Expand Up @@ -155,10 +171,15 @@ function attachControlActions() {
}, false );
}
} );

imageFullBleed.addEventListener( 'click', setImageFullBleed, false );
imageAlignNone.addEventListener( 'click', setImageAlignNone, false );
imageAlignLeft.addEventListener( 'click', setImageAlignLeft, false );
imageAlignRight.addEventListener( 'click', setImageAlignRight, false );
}

function reselect() {
document.getElementsByClassName( 'is-selected' )[0].click();
queryFirst( '.is-selected' ).click();
}

function swapNodes( a, b ) {
Expand Down Expand Up @@ -242,10 +263,19 @@ function setImageAlignRight( event ) {
}

function l( data ) {
console.log( data );
console.log.apply( console.log, arguments );
return data;
}

function query( selector ) {
return Array.from( document.querySelectorAll( selector ) );
}

function queryFirst( selector ) {
return query( selector )[ 0 ];
}

function getConfig( configName, tagName ) {
return config[ configName ][ tagName ] ||
config[ configName ].default;
}
14 changes: 7 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@
</span>
</div>
<div class="block-controls">
<button class="is-active">
<button class="block-text is-active">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><title>Align Left</title><rect x="0" fill="none" width="24" height="24"/><g><path d="M4 19h16v-2H4v2zm10-6H4v2h10v-2zM4 9v2h16V9H4zm10-4H4v2h10V5z"/></g></svg>
</button>
<button>
<button class="block-text">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><title>Align Center</title><rect x="0" fill="none" width="24" height="24"/><g><path d="M4 19h16v-2H4v2zm13-6H7v2h10v-2zM4 9v2h16V9H4zm13-4H7v2h10V5z"/></g></svg>
</button>
<button>
<button class="block-text">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><title>Align Right</title><rect x="0" fill="none" width="24" height="24"/><g><path d="M20 17H4v2h16v-2zm-10-2h10v-2H10v2zM4 9v2h16V9H4zm6-2h10V5H10v2z"/></g></svg>
</button>
<button class="block-image__no-align">
<button class="block-image block-image__no-align">
<svg class="gridicon gridicons-align-image-center" height="24" width="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><g><path d="M3 5h18v2H3V5zm0 14h18v-2H3v2zm5-4h8V9H8v6z"></path></g></svg>
</button>
<button class="block-image__align-left">
<button class="block-image block-image__align-left">
<svg class="gridicon gridicons-align-image-left" height="24" width="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><g><path d="M3 5h18v2H3V5zm0 14h18v-2H3v2zm0-4h8V9H3v6zm10 0h8v-2h-8v2zm0-4h8V9h-8v2z"></path></g></svg>
</button>
<button class="block-image__align-right">
<button class="block-image block-image__align-right">
<svg class="gridicon gridicons-align-image-right" height="24" width="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><g><path d="M21 7H3V5h18v2zm0 10H3v2h18v-2zm0-8h-8v6h8V9zm-10 4H3v2h8v-2zm0-4H3v2h8V9z"></path></g></svg>
</button>
<button class="block-image__full-width">
<button class="block-image block-image__full-width">
<svg class="gridicon gridicons-fullscreen" height="24" width="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><g><title>Full Bleed</title><path d="M21 3v6h-2V6.41l-3.29 3.3-1.42-1.42L17.59 5H15V3zM3 3v6h2V6.41l3.29 3.3 1.42-1.42L6.41 5H9V3zm18 18v-6h-2v2.59l-3.29-3.29-1.41 1.41L17.59 19H15v2zM9 21v-2H6.41l3.29-3.29-1.41-1.42L5 17.59V15H3v6z"></path></g></svg>
</button>
</div>
Expand Down
32 changes: 10 additions & 22 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ p {
* Block Controls
*/

.block-controls {
.block-controls {
background: #191e23;
display: inline-block;
max-height: 36px;
Expand All @@ -242,7 +242,7 @@ p {
transform: translateZ( 0 );
}

.block-controls button {
.block-controls button {
background: #191e23;
color: #fff;
border: none;
Expand All @@ -258,6 +258,14 @@ p {
background: #6d7882;
}

.block-controls button {
display: none;
}

.block-controls.is-image .block-image,
.block-controls.is-text .block-text {
display: block;
}

/**
* Inserter
Expand Down Expand Up @@ -355,23 +363,3 @@ img.align-right {
width: 340px;
margin: 0 0 0 16px;
}

button.block-image__full-width,
button.block-image__no-alignment,
button.block-image__no-align,
button.block-image__align-left,
button.block-image__align-right {
display: none;
}

.block-controls.is-image button {
display: none;
}

.block-controls.is-image .block-image__full-width,
.block-controls.is-image .block-image__no-alignment,
.block-controls.is-image .block-image__no-align,
.block-controls.is-image .block-image__align-left,
.block-controls.is-image .block-image__align-right {
display: block;
}