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

Preserve block style if pasting on blank line

If the clipboard contains block contents, e.g.

<blockquote><p>Foo</p></blockquote>

Then if you paste it into a block that already has content we merge the inline
content from the first block and discard its surrounding block.

However, if you paste into an empty block, we'll now keep the block and remove
the empty one in the document. This seems a reasonable heuristic for determining
user intent.
This commit is contained in:
Neil Jenkins 2017-11-01 10:41:53 +11:00
parent 306230d0df
commit 50fb7c7c53
3 changed files with 24 additions and 8 deletions

View file

@ -912,7 +912,8 @@ var deleteContentsOfRange = function ( range, root ) {
// After method, range will be around inserted content
var insertTreeFragmentIntoRange = function ( range, frag, root ) {
var node, block, blockContentsAfterSplit, stopPoint, container, offset;
var firstBlockInFrag, nodeAfterSplit, nodeBeforeSplit, tempRange;
var replaceBlock, firstBlockInFrag, nodeAfterSplit, nodeBeforeSplit;
var tempRange;
// Fixup content: ensure no top-level inline, and add cursor fix elements.
fixContainer( frag, root );
@ -935,10 +936,12 @@ var insertTreeFragmentIntoRange = function ( range, frag, root ) {
// Merge the contents of the first block in the frag with the focused block.
// If there are contents in the block after the focus point, collect this
// up to insert in the last block later
// up to insert in the last block later. If the block is empty, replace
// it instead of merging.
block = getStartBlockOfRange( range, root );
firstBlockInFrag = getNextBlock( frag, frag );
if ( block && firstBlockInFrag &&
replaceBlock = !!block && isEmptyBlock( block );
if ( block && firstBlockInFrag && !replaceBlock &&
// Don't merge table cells or PRE elements into block
!getNearest( firstBlockInFrag, frag, 'PRE' ) &&
!getNearest( firstBlockInFrag, frag, 'TABLE' ) ) {
@ -975,6 +978,11 @@ var insertTreeFragmentIntoRange = function ( range, frag, root ) {
// Is there still any content in the fragment?
if ( getLength( frag ) ) {
if ( replaceBlock ) {
range.setEndBefore( block );
range.collapse( false );
detach( block );
}
moveRangeBoundariesUpTree( range, stopPoint, stopPoint, root );
// Now split after block up to blockquote (if a parent) or root
nodeAfterSplit = split(

File diff suppressed because one or more lines are too long

View file

@ -180,7 +180,8 @@ var deleteContentsOfRange = function ( range, root ) {
// After method, range will be around inserted content
var insertTreeFragmentIntoRange = function ( range, frag, root ) {
var node, block, blockContentsAfterSplit, stopPoint, container, offset;
var firstBlockInFrag, nodeAfterSplit, nodeBeforeSplit, tempRange;
var replaceBlock, firstBlockInFrag, nodeAfterSplit, nodeBeforeSplit;
var tempRange;
// Fixup content: ensure no top-level inline, and add cursor fix elements.
fixContainer( frag, root );
@ -203,10 +204,12 @@ var insertTreeFragmentIntoRange = function ( range, frag, root ) {
// Merge the contents of the first block in the frag with the focused block.
// If there are contents in the block after the focus point, collect this
// up to insert in the last block later
// up to insert in the last block later. If the block is empty, replace
// it instead of merging.
block = getStartBlockOfRange( range, root );
firstBlockInFrag = getNextBlock( frag, frag );
if ( block && firstBlockInFrag &&
replaceBlock = !!block && isEmptyBlock( block );
if ( block && firstBlockInFrag && !replaceBlock &&
// Don't merge table cells or PRE elements into block
!getNearest( firstBlockInFrag, frag, 'PRE' ) &&
!getNearest( firstBlockInFrag, frag, 'TABLE' ) ) {
@ -243,6 +246,11 @@ var insertTreeFragmentIntoRange = function ( range, frag, root ) {
// Is there still any content in the fragment?
if ( getLength( frag ) ) {
if ( replaceBlock ) {
range.setEndBefore( block );
range.collapse( false );
detach( block );
}
moveRangeBoundariesUpTree( range, stopPoint, stopPoint, root );
// Now split after block up to blockquote (if a parent) or root
nodeAfterSplit = split(