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

Introduce the ability to reorder blocks #5

Merged
merged 1 commit into from
Feb 8, 2017
Merged
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
90 changes: 90 additions & 0 deletions blocks.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
"use strict";

/**
* Derived functions
*/
var getNextSibling = siblingGetter( 'next' );
var getPreviousSibling = siblingGetter( 'previous' );

/**
* Globals
*/

var editor = document.getElementsByClassName( 'editor' )[0];
var controls = document.getElementsByClassName( 'block-controls' )[0];
var selectedBlock = null;

/**
* Initialization
*/

window.addEventListener( 'click', clearBlocks, false );
editor.addEventListener( 'input', attachBlockHandlers, false );
editor.addEventListener( 'input', clearBlocks, false );

attachBlockHandlers();
attachControlActions();

/**
* Core logic
*/

function attachBlockHandlers() {
var blocks = getBlocks();
Array.from( blocks ).forEach( function( block ) {
block.removeEventListener( 'click', selectBlock, false );
block.addEventListener( 'click', selectBlock, false );
} );
}
Expand All @@ -33,16 +54,85 @@ function selectBlock( event ) {
// Show switcher
controls.style.opacity = 1;
controls.style.top = ( position.top + 18 ) + 'px';
selectedBlock = event.target;
}

function clearBlocks() {
Array.from( getBlocks() ).forEach( function( block ) {
block.className = '';
} );
var selectedBlock = null;

hideControls();
}

function hideControls() {
controls.style.opacity = 0;
}

function attachControlActions() {
Array.from( controls.childNodes ).forEach( function( node ) {
if ( 'svg' !== node.nodeName ) {
return;
}

var classes = node.className.baseVal;

if ( 'up' === classes ) {
node.addEventListener( 'click', function() {
swapNodes( selectedBlock, getPreviousSibling( selectedBlock ) );
attachBlockHandlers();
}, false );
} else if ( 'down' === classes ) {
node.addEventListener( 'click', function() {
swapNodes( selectedBlock, getNextSibling( selectedBlock ) );
attachBlockHandlers();
}, false );
}
} );
}

function swapNodes( a, b ) {
if ( ! ( a && b ) ) {
return false;
}

var parent = a.parentNode;
if ( ! parent ) {
return false;
}

// insert node copies before removal
parent.replaceChild( b.cloneNode( true ), a );
parent.replaceChild( a.cloneNode( true ), b );

return true;
}

/**
* Utility functions
*/
function siblingGetter( direction ) {
var sibling = direction + 'Sibling';

return function getAdjacentSibling( node ) {
if ( null === node ) {
return null;
}

if ( null === node[ sibling ] ) {
return null;
}

if ( '#text' === node[ sibling ].nodeName ) {
return getAdjacentSibling( node[ sibling ] );
}

return node[ sibling ];
}
}

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