mirror of
https://github.com/fastmail/Squire.git
synced 2025-01-18 12:42:37 -05:00
Make editor.insertElement support block elements.
This commit is contained in:
parent
af1720282c
commit
e1bae30a16
3 changed files with 65 additions and 9 deletions
|
@ -1388,10 +1388,38 @@ var setHTML = function ( html ) {
|
||||||
var insertElement = function ( el, range ) {
|
var insertElement = function ( el, range ) {
|
||||||
if ( !range ) { range = getSelection(); }
|
if ( !range ) { range = getSelection(); }
|
||||||
range.collapse( true );
|
range.collapse( true );
|
||||||
|
if ( isInline( el ) ) {
|
||||||
range._insertNode( el );
|
range._insertNode( el );
|
||||||
range.setStartAfter( el );
|
range.setStartAfter( el );
|
||||||
|
} else {
|
||||||
|
// Get containing block node.
|
||||||
|
var splitNode = range.getStartBlock() || body,
|
||||||
|
parent, nodeAfterSplit;
|
||||||
|
// While at end of container node, move up DOM tree.
|
||||||
|
while ( splitNode !== body && !splitNode.nextSibling ) {
|
||||||
|
splitNode = splitNode.parentNode;
|
||||||
|
}
|
||||||
|
// If in the middle of a container node, split up to body.
|
||||||
|
if ( splitNode !== body ) {
|
||||||
|
parent = splitNode.parentNode;
|
||||||
|
nodeAfterSplit = split( parent, splitNode.nextSibling, body );
|
||||||
|
}
|
||||||
|
if ( nodeAfterSplit ) {
|
||||||
|
body.insertBefore( el, nodeAfterSplit );
|
||||||
|
range.setStart( nodeAfterSplit, 0 );
|
||||||
|
range.setStart( nodeAfterSplit, 0 );
|
||||||
|
range.moveBoundariesDownTree();
|
||||||
|
} else {
|
||||||
|
body.appendChild( el );
|
||||||
|
// Insert blank line below block.
|
||||||
|
body.appendChild( fixCursor( createElement( 'div' ) ) );
|
||||||
|
range.setStart( el, 0 );
|
||||||
|
range.setEnd( el, 0 );
|
||||||
|
}
|
||||||
|
focus();
|
||||||
setSelection( range );
|
setSelection( range );
|
||||||
updatePath( range );
|
updatePath( range );
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// --- Bookmarking ---
|
// --- Bookmarking ---
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -307,10 +307,38 @@ var setHTML = function ( html ) {
|
||||||
var insertElement = function ( el, range ) {
|
var insertElement = function ( el, range ) {
|
||||||
if ( !range ) { range = getSelection(); }
|
if ( !range ) { range = getSelection(); }
|
||||||
range.collapse( true );
|
range.collapse( true );
|
||||||
|
if ( isInline( el ) ) {
|
||||||
range._insertNode( el );
|
range._insertNode( el );
|
||||||
range.setStartAfter( el );
|
range.setStartAfter( el );
|
||||||
|
} else {
|
||||||
|
// Get containing block node.
|
||||||
|
var splitNode = range.getStartBlock() || body,
|
||||||
|
parent, nodeAfterSplit;
|
||||||
|
// While at end of container node, move up DOM tree.
|
||||||
|
while ( splitNode !== body && !splitNode.nextSibling ) {
|
||||||
|
splitNode = splitNode.parentNode;
|
||||||
|
}
|
||||||
|
// If in the middle of a container node, split up to body.
|
||||||
|
if ( splitNode !== body ) {
|
||||||
|
parent = splitNode.parentNode;
|
||||||
|
nodeAfterSplit = split( parent, splitNode.nextSibling, body );
|
||||||
|
}
|
||||||
|
if ( nodeAfterSplit ) {
|
||||||
|
body.insertBefore( el, nodeAfterSplit );
|
||||||
|
range.setStart( nodeAfterSplit, 0 );
|
||||||
|
range.setStart( nodeAfterSplit, 0 );
|
||||||
|
range.moveBoundariesDownTree();
|
||||||
|
} else {
|
||||||
|
body.appendChild( el );
|
||||||
|
// Insert blank line below block.
|
||||||
|
body.appendChild( fixCursor( createElement( 'div' ) ) );
|
||||||
|
range.setStart( el, 0 );
|
||||||
|
range.setEnd( el, 0 );
|
||||||
|
}
|
||||||
|
focus();
|
||||||
setSelection( range );
|
setSelection( range );
|
||||||
updatePath( range );
|
updatePath( range );
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// --- Bookmarking ---
|
// --- Bookmarking ---
|
||||||
|
|
Loading…
Add table
Reference in a new issue