11"use strict" ;
22
3+ /**
4+ * Derived functions
5+ */
6+ var getNextSibling = siblingGetter ( 'next' ) ;
7+ var getPreviousSibling = siblingGetter ( 'previous' ) ;
8+
9+ /**
10+ * Globals
11+ */
12+
313var editor = document . getElementsByClassName ( 'editor' ) [ 0 ] ;
414var controls = document . getElementsByClassName ( 'block-controls' ) [ 0 ] ;
15+ var selectedBlock = null ;
16+
17+ /**
18+ * Initialization
19+ */
520
621window . addEventListener ( 'click' , clearBlocks , false ) ;
722editor . addEventListener ( 'input' , attachBlockHandlers , false ) ;
823editor . addEventListener ( 'input' , clearBlocks , false ) ;
924
1025attachBlockHandlers ( ) ;
26+ attachControlActions ( ) ;
27+
28+ /**
29+ * Core logic
30+ */
1131
1232function attachBlockHandlers ( ) {
1333 var blocks = getBlocks ( ) ;
1434 Array . from ( blocks ) . forEach ( function ( block ) {
35+ block . removeEventListener ( 'click' , selectBlock , false ) ;
1536 block . addEventListener ( 'click' , selectBlock , false ) ;
1637 } ) ;
1738}
@@ -33,16 +54,85 @@ function selectBlock( event ) {
3354 // Show switcher
3455 controls . style . opacity = 1 ;
3556 controls . style . top = ( position . top + 18 ) + 'px' ;
57+ selectedBlock = event . target ;
3658}
3759
3860function clearBlocks ( ) {
3961 Array . from ( getBlocks ( ) ) . forEach ( function ( block ) {
4062 block . className = '' ;
4163 } ) ;
64+ var selectedBlock = null ;
4265
4366 hideControls ( ) ;
4467}
4568
4669function hideControls ( ) {
4770 controls . style . opacity = 0 ;
4871}
72+
73+ function attachControlActions ( ) {
74+ Array . from ( controls . childNodes ) . forEach ( function ( node ) {
75+ if ( 'svg' !== node . nodeName ) {
76+ return ;
77+ }
78+
79+ var classes = node . className . baseVal ;
80+
81+ if ( 'up' === classes ) {
82+ node . addEventListener ( 'click' , function ( ) {
83+ swapNodes ( selectedBlock , getPreviousSibling ( selectedBlock ) ) ;
84+ attachBlockHandlers ( ) ;
85+ } , false ) ;
86+ } else if ( 'down' === classes ) {
87+ node . addEventListener ( 'click' , function ( ) {
88+ swapNodes ( selectedBlock , getNextSibling ( selectedBlock ) ) ;
89+ attachBlockHandlers ( ) ;
90+ } , false ) ;
91+ }
92+ } ) ;
93+ }
94+
95+ function swapNodes ( a , b ) {
96+ if ( ! ( a && b ) ) {
97+ return false ;
98+ }
99+
100+ var parent = a . parentNode ;
101+ if ( ! parent ) {
102+ return false ;
103+ }
104+
105+ // insert node copies before removal
106+ parent . replaceChild ( b . cloneNode ( true ) , a ) ;
107+ parent . replaceChild ( a . cloneNode ( true ) , b ) ;
108+
109+ return true ;
110+ }
111+
112+ /**
113+ * Utility functions
114+ */
115+ function siblingGetter ( direction ) {
116+ var sibling = direction + 'Sibling' ;
117+
118+ return function getAdjacentSibling ( node ) {
119+ if ( null === node ) {
120+ return null ;
121+ }
122+
123+ if ( null === node [ sibling ] ) {
124+ return null ;
125+ }
126+
127+ if ( '#text' === node [ sibling ] . nodeName ) {
128+ return getAdjacentSibling ( node [ sibling ] ) ;
129+ }
130+
131+ return node [ sibling ] ;
132+ }
133+ }
134+
135+ function l ( data ) {
136+ console . log ( data ) ;
137+ return data ;
138+ }
0 commit comments