From 6b754d423c661c339a76bf4f89fba6fcde708d29 Mon Sep 17 00:00:00 2001 From: Neil Jenkins Date: Mon, 7 Apr 2014 13:05:44 +1000 Subject: [PATCH] Add support mult-level lists. * Hit tab to increase list depth, or call increaseListLevel method. * Hit enter on a blank item to decrease list depth, or call decreaseListLevel method. --- build/squire-raw.js | 227 +++++++++++++++++++++++++++++++------------- build/squire.js | 4 +- source/Editor.js | 205 ++++++++++++++++++++++++++------------- source/Node.js | 20 +++- source/intro.js | 2 +- 5 files changed, 320 insertions(+), 138 deletions(-) diff --git a/build/squire-raw.js b/build/squire-raw.js index aef88b9..28f83c8 100644 --- a/build/squire-raw.js +++ b/build/squire-raw.js @@ -1,6 +1,6 @@ /* Copyright © 2011-2013 by Neil Jenkins. MIT Licensed. */ -( function ( doc ) { +( function ( doc, undefined ) { "use strict"; /*global doc, navigator */ @@ -506,26 +506,40 @@ function mergeWithBlock ( block, next, range ) { function mergeContainers ( node ) { var prev = node.previousSibling, - first = node.firstChild; + first = node.firstChild, + isListItem = ( node.nodeName === 'LI' ); + + // Do not merge LIs, unless it only contains a UL + if ( isListItem && ( !first || !/^[OU]L$/.test( first.nodeName ) ) ) { + return; + } + if ( prev && areAlike( prev, node ) && isContainer( prev ) ) { detach( node ); prev.appendChild( empty( node ) ); if ( first ) { mergeContainers( first ); } + } else if ( isListItem ) { + prev = node.ownerDocument.createElement( 'div' ); + node.insertBefore( prev, first ); + fixCursor( prev ); } } function createElement ( doc, tag, props, children ) { var el = doc.createElement( tag ), - attr, i, l; + attr, value, i, l; if ( props instanceof Array ) { children = props; props = null; } if ( props ) { for ( attr in props ) { - el.setAttribute( attr, props[ attr ] ); + value = props[ attr ]; + if ( value !== undefined ) { + el.setAttribute( attr, props[ attr ] ); + } } } if ( children ) { @@ -1076,6 +1090,7 @@ var expandRangeToBlockBoundaries = function ( range ) { isInline, isBlock, isContainer, + getBlockWalker, getPreviousBlock, getNextBlock, getNearest, @@ -2023,83 +2038,128 @@ var removeBlockQuote = function ( frag ) { return frag; }; -var makeList = function ( self, nodes, type ) { - var i, l, node, tag, prev, replacement; - for ( i = 0, l = nodes.length; i < l; i += 1 ) { - node = nodes[i]; - tag = node.nodeName; - if ( isBlock( node ) ) { - if ( tag !== 'LI' ) { - replacement = self.createElement( 'LI', { - 'class': node.dir === 'rtl' ? 'dir-rtl' : '', - dir: node.dir - }, [ - empty( node ) - ]); - if ( node.parentNode.nodeName === type ) { - replaceWith( node, replacement ); - } - else if ( ( prev = node.previousSibling ) && - prev.nodeName === type ) { - prev.appendChild( replacement ); - detach( node ); - i -= 1; - l -= 1; - } - else { - replaceWith( - node, - self.createElement( type, [ - replacement - ]) - ); - } +var makeList = function ( self, frag, type ) { + var walker = getBlockWalker( frag ), + node, tag, prev, newLi; + + while ( node = walker.nextNode() ) { + tag = node.parentNode.nodeName; + if ( tag !== 'LI' ) { + newLi = self.createElement( 'LI', { + 'class': node.dir === 'rtl' ? 'dir-rtl' : undefined, + dir: node.dir || undefined + }); + // Have we replaced the previous block with a new