0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2025-01-03 05:00:13 -05:00

Make forEachBlock method public.

And tidy a few bits and pieces.
This commit is contained in:
Neil Jenkins 2011-11-14 17:55:40 +11:00
parent 3f5233c61a
commit feca3e4d8f
4 changed files with 53 additions and 42 deletions

View file

@ -299,6 +299,19 @@ Sets the text alignment in all blocks at least partially contained by the select
Returns self. Returns self.
### forEachBlock ###
Executes a function on each block in the current selection, or until the function returns a truthy value.
#### Parameters ####
* **fn** The function to execute on each block node at least partially contained in the current selection. The function will be called with the block node as the only argument.
* **mutates** A boolean indicating whether your function may modify anything in the document in any way.
#### Returns ####
Returns self.
### modifyBlocks ### ### modifyBlocks ###
Extracts a portion of the DOM tree (up to the block boundaries of the current selection), modifies it and then reinserts it and merges the edges. See the code for examples if you're interested in using this function. Extracts a portion of the DOM tree (up to the block boundaries of the current selection), modifies it and then reinserts it and merges the edges. See the code for examples if you're interested in using this function.

View file

@ -620,30 +620,33 @@
// --- Block formatting --- // --- Block formatting ---
var forEachBlock = function ( fn, range ) { var forEachBlock = function ( fn, mutates, range ) {
if ( !range && !( range = getSelection() ) ) { if ( !range && !( range = getSelection() ) ) {
return; return;
} }
// Save undo checkpoint // Save undo checkpoint
recordUndoState( range ); if ( mutates ) {
getRangeAndRemoveBookmark( range ); recordUndoState( range );
getRangeAndRemoveBookmark( range );
}
var start = range.getStartBlock(), var start = range.getStartBlock(),
end = range.getEndBlock(); end = range.getEndBlock();
if ( start && end ) { if ( start && end ) {
while ( true ) { while ( true ) {
fn( start ); if ( fn( start ) || start === end ) { break; }
if ( start === end ) { break; }
start = start.getNextBlock(); start = start.getNextBlock();
} }
} }
// Path may have changed if ( mutates ) {
updatePath( 0, true ); // Path may have changed
updatePath( 0, true );
// We're not still in an undo state // We're not still in an undo state
docWasChanged(); docWasChanged();
}
}; };
var modifyBlocks = function ( modify, range ) { var modifyBlocks = function ( modify, range ) {
@ -1398,20 +1401,11 @@
// And insert new content // And insert new content
// Add the styles // Add the styles
if ( styles ) { if ( styles ) {
var head = doc.documentElement.firstChild, var style = createElement( 'STYLE', {
style = createElement( 'STYLE', {
type: 'text/css' type: 'text/css'
}); });
if ( style.styleSheet ) { style.appendChild( doc.createTextNode( styles ) );
// IE8: must append to document BEFORE adding styles doc.documentElement.firstChild.appendChild( style );
// or you get the IE7 CSS parser!
head.appendChild( style );
style.styleSheet.cssText = styles;
} else {
// Everyone else
style.appendChild( doc.createTextNode( styles ) );
head.appendChild( style );
}
} }
body.appendChild( frag ); body.appendChild( frag );
@ -1560,11 +1554,12 @@
forEachBlock( function ( block ) { forEachBlock( function ( block ) {
block.className = 'align-' + dir; block.className = 'align-' + dir;
block.style.textAlign = dir; block.style.textAlign = dir;
}); }, true );
focus(); focus();
return this; return this;
}, },
forEachBlock: chain( forEachBlock ),
modifyBlocks: chain( modifyBlocks ), modifyBlocks: chain( modifyBlocks ),
incQuoteLevel: command( modifyBlocks, increaseBlockQuoteLevel ), incQuoteLevel: command( modifyBlocks, increaseBlockQuoteLevel ),

View file

@ -102,8 +102,8 @@ implement( Node, {
}); });
implement( Text, { implement( Text, {
isInline: $True,
isLeaf: $True, isLeaf: $True,
isInline: $True,
getLength: function () { getLength: function () {
return this.length; return this.length;
}, },

View file

@ -51,36 +51,39 @@ var getNodeAfter = function ( node, offset ) {
}; };
implement( Range, { implement( Range, {
getTextContent: function () {
this.moveBoundariesDownTree(); forEachTextNode: function ( fn ) {
var range = this.cloneRange();
range.moveBoundariesDownTree();
var startContainer = this.startContainer, var startContainer = range.startContainer,
endContainer = this.endContainer, endContainer = range.endContainer,
root = this.commonAncestorContainer, root = range.commonAncestorContainer,
walker = root.ownerDocument.createTreeWalker( walker = root.ownerDocument.createTreeWalker(
root, SHOW_TEXT, function ( node ) { root, SHOW_TEXT, function ( node ) {
return FILTER_ACCEPT; return FILTER_ACCEPT;
}, false ), }, false ),
textnode = walker.currentNode = startContainer, textnode = walker.currentNode = startContainer;
textContent = '',
value;
do { while ( !fn( textnode, range ) &&
value = textnode.data; textnode !== endContainer &&
( textnode = walker.nextNode() ) ) {}
},
getTextContent: function () {
var textContent = '';
this.forEachTextNode( function ( textnode, range ) {
var value = textnode.data;
if ( value && ( /\S/.test( value ) ) ) { if ( value && ( /\S/.test( value ) ) ) {
if ( textnode === endContainer ) { if ( textnode === range.endContainer ) {
value = value.slice( 0, this.endOffset ); value = value.slice( 0, range.endOffset );
} }
if ( textnode === startContainer ) { if ( textnode === range.startContainer ) {
value = value.slice( this.startOffset ); value = value.slice( range.startOffset );
} }
textContent += value; textContent += value;
} }
if ( textnode === endContainer ) { });
break;
}
} while ( textnode = walker.nextNode() );
return textContent; return textContent;
}, },