0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 07:13:08 -05:00

Unify cut and copy implementations

* Data added to clipboard should now always be the same for cut as for copy
* Fixes bug when cutting across blocks, where not all parents would be included
  in the data added to the clipboard
This commit is contained in:
Neil Jenkins 2017-01-13 10:24:01 +11:00
parent 15fdf05e76
commit 79ffc02557
3 changed files with 68 additions and 16 deletions

View file

@ -2160,9 +2160,9 @@ var setClipboardData = function ( clipboardData, node, root ) {
var onCut = function ( event ) {
var clipboardData = event.clipboardData;
var range = this.getSelection();
var node = this.createElement( 'div' );
var root = this._root;
var self = this;
var startBlock, endBlock, copyRoot, contents, parent, newContents, node;
// Nothing to do
if ( range.collapsed ) {
@ -2177,7 +2177,27 @@ var onCut = function ( event ) {
// Mobile Safari flat out doesn't work:
// https://bugs.webkit.org/show_bug.cgi?id=143776
if ( !isEdge && !isIOS && clipboardData ) {
node.appendChild( deleteContentsOfRange( range, root ) );
// Clipboard content should include all parents within block, or all
// parents up to root if selection across blocks
startBlock = getStartBlockOfRange( range, root );
endBlock = getEndBlockOfRange( range, root );
copyRoot = ( ( startBlock === endBlock ) && startBlock ) || root;
// Extract the contents
parent = range.commonAncestorContainer;
contents = deleteContentsOfRange( range, root );
// Add any other parents not in extracted content, up to copy root
if ( parent.nodeType === TEXT_NODE ) {
parent = parent.parentNode;
}
while ( parent && parent !== copyRoot ) {
newContents = parent.cloneNode( false );
newContents.appendChild( contents );
contents = newContents;
parent = parent.parentNode;
}
// Set clipboard data
node = this.createElement( 'div' );
node.appendChild( contents );
setClipboardData( clipboardData, node, root );
event.preventDefault();
} else {
@ -2197,22 +2217,27 @@ var onCut = function ( event ) {
var onCopy = function ( event ) {
var clipboardData = event.clipboardData;
var range = this.getSelection();
var node = this.createElement( 'div' );
var root = this._root;
var startBlock, endBlock, copyRoot, contents, parent, newContents;
var startBlock, endBlock, copyRoot, contents, parent, newContents, node;
// Edge only seems to support setting plain text as of 2016-03-11.
// Mobile Safari flat out doesn't work:
// https://bugs.webkit.org/show_bug.cgi?id=143776
if ( !isEdge && !isIOS && clipboardData ) {
range = range.cloneRange();
// Clipboard content should include all parents within block, or all
// parents up to root if selection across blocks
startBlock = getStartBlockOfRange( range, root );
endBlock = getEndBlockOfRange( range, root );
copyRoot = ( ( startBlock === endBlock ) && startBlock ) || root;
// Clone range to mutate, then move up as high as possible without
// passing the copy root node.
range = range.cloneRange();
moveRangeBoundariesDownTree( range );
moveRangeBoundariesUpTree( range, copyRoot, copyRoot );
contents = range.cloneContents();
// Extract the contents
parent = range.commonAncestorContainer;
contents = range.cloneContents();
// Add any other parents not in extracted content, up to copy root
if ( parent.nodeType === TEXT_NODE ) {
parent = parent.parentNode;
}
@ -2222,8 +2247,9 @@ var onCopy = function ( event ) {
contents = newContents;
parent = parent.parentNode;
}
// Set clipboard data
node = this.createElement( 'div' );
node.appendChild( contents );
setClipboardData( clipboardData, node, root );
event.preventDefault();
}

File diff suppressed because one or more lines are too long

View file

@ -34,9 +34,9 @@ var setClipboardData = function ( clipboardData, node, root ) {
var onCut = function ( event ) {
var clipboardData = event.clipboardData;
var range = this.getSelection();
var node = this.createElement( 'div' );
var root = this._root;
var self = this;
var startBlock, endBlock, copyRoot, contents, parent, newContents, node;
// Nothing to do
if ( range.collapsed ) {
@ -51,7 +51,27 @@ var onCut = function ( event ) {
// Mobile Safari flat out doesn't work:
// https://bugs.webkit.org/show_bug.cgi?id=143776
if ( !isEdge && !isIOS && clipboardData ) {
node.appendChild( deleteContentsOfRange( range, root ) );
// Clipboard content should include all parents within block, or all
// parents up to root if selection across blocks
startBlock = getStartBlockOfRange( range, root );
endBlock = getEndBlockOfRange( range, root );
copyRoot = ( ( startBlock === endBlock ) && startBlock ) || root;
// Extract the contents
parent = range.commonAncestorContainer;
contents = deleteContentsOfRange( range, root );
// Add any other parents not in extracted content, up to copy root
if ( parent.nodeType === TEXT_NODE ) {
parent = parent.parentNode;
}
while ( parent && parent !== copyRoot ) {
newContents = parent.cloneNode( false );
newContents.appendChild( contents );
contents = newContents;
parent = parent.parentNode;
}
// Set clipboard data
node = this.createElement( 'div' );
node.appendChild( contents );
setClipboardData( clipboardData, node, root );
event.preventDefault();
} else {
@ -71,22 +91,27 @@ var onCut = function ( event ) {
var onCopy = function ( event ) {
var clipboardData = event.clipboardData;
var range = this.getSelection();
var node = this.createElement( 'div' );
var root = this._root;
var startBlock, endBlock, copyRoot, contents, parent, newContents;
var startBlock, endBlock, copyRoot, contents, parent, newContents, node;
// Edge only seems to support setting plain text as of 2016-03-11.
// Mobile Safari flat out doesn't work:
// https://bugs.webkit.org/show_bug.cgi?id=143776
if ( !isEdge && !isIOS && clipboardData ) {
range = range.cloneRange();
// Clipboard content should include all parents within block, or all
// parents up to root if selection across blocks
startBlock = getStartBlockOfRange( range, root );
endBlock = getEndBlockOfRange( range, root );
copyRoot = ( ( startBlock === endBlock ) && startBlock ) || root;
// Clone range to mutate, then move up as high as possible without
// passing the copy root node.
range = range.cloneRange();
moveRangeBoundariesDownTree( range );
moveRangeBoundariesUpTree( range, copyRoot, copyRoot );
contents = range.cloneContents();
// Extract the contents
parent = range.commonAncestorContainer;
contents = range.cloneContents();
// Add any other parents not in extracted content, up to copy root
if ( parent.nodeType === TEXT_NODE ) {
parent = parent.parentNode;
}
@ -96,8 +121,9 @@ var onCopy = function ( event ) {
contents = newContents;
parent = parent.parentNode;
}
// Set clipboard data
node = this.createElement( 'div' );
node.appendChild( contents );
setClipboardData( clipboardData, node, root );
event.preventDefault();
}