0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 15:23:29 -05:00

Improve copying of plain text on windows

This commit is contained in:
Neil Jenkins 2016-12-11 12:30:37 +11:00
parent 75c524720b
commit 2ddbd53a5a
2 changed files with 24 additions and 6 deletions

View file

@ -28,10 +28,10 @@ var win = doc.defaultView;
var ua = navigator.userAgent; var ua = navigator.userAgent;
var isAndroid = /Android/.test( ua );
var isIOS = /iP(?:ad|hone|od)/.test( ua ); var isIOS = /iP(?:ad|hone|od)/.test( ua );
var isMac = /Mac OS X/.test( ua ); var isMac = /Mac OS X/.test( ua );
var isWin = /Windows NT/.test( ua );
var isAndroid = /Android/.test( ua );
var isGecko = /Gecko\//.test( ua ); var isGecko = /Gecko\//.test( ua );
var isIElt11 = /Trident\/[456]\./.test( ua ); var isIElt11 = /Trident\/[456]\./.test( ua );
@ -2131,11 +2131,29 @@ var cleanupBRs = function ( node, root ) {
// DOM node into the document to ensure the text part is correct. // DOM node into the document to ensure the text part is correct.
var setClipboardData = function ( clipboardData, node ) { var setClipboardData = function ( clipboardData, node ) {
var body = node.ownerDocument.body; var body = node.ownerDocument.body;
var html, text;
// Firefox will add an extra new line for BRs at the end of block when
// calculating innerText, even though they don't actually affect display.
// So we need to remove them first.
cleanupBRs( node, root );
node.setAttribute( 'style', node.setAttribute( 'style',
'position:fixed;overflow:hidden;bottom:100%;right:100%;' ); 'position:fixed;overflow:hidden;bottom:100%;right:100%;' );
body.appendChild( node ); body.appendChild( node );
clipboardData.setData( 'text/html', node.innerHTML ); html = node.innerHTML;
clipboardData.setData( 'text/plain', node.innerText || node.textContent ); text = node.innerText || node.textContent;
// Firefox (and others?) returns unix line endings (\n) even on Windows.
// If on Windows, normalise to \r\n, since Notepad and some other crappy
// apps do not understand just \n.
if ( isWin ) {
text = text.replace( /\r?\n/g, '\r\n' );
}
clipboardData.setData( 'text/html', html );
clipboardData.setData( 'text/plain', text );
body.removeChild( node ); body.removeChild( node );
}; };

File diff suppressed because one or more lines are too long