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

Export onCopy function

This commit is contained in:
Nicholas Wylie 2020-07-08 16:12:32 +10:00 committed by Neil Jenkins
parent 73f18523a2
commit b952894d10
4 changed files with 86 additions and 64 deletions

View file

@ -460,7 +460,6 @@ function fixContainer ( container, root ) {
var doc = container.ownerDocument; var doc = container.ownerDocument;
var wrapper = null; var wrapper = null;
var i, l, child, isBR; var i, l, child, isBR;
var config = root.__squire__._config;
for ( i = 0, l = children.length; i < l; i += 1 ) { for ( i = 0, l = children.length; i < l; i += 1 ) {
child = children[i]; child = children[i];
@ -1762,7 +1761,7 @@ var keyHandlers = {
} }
}, },
space: function ( self, _, range ) { space: function ( self, _, range ) {
var node, parent; var node;
var root = self._root; var root = self._root;
self._recordUndoState( range ); self._recordUndoState( range );
if ( self._config.addLinks ) { if ( self._config.addLinks ) {
@ -2225,23 +2224,32 @@ var cleanupBRs = function ( node, root, keepForBlankLine ) {
// The (non-standard but supported enough) innerText property is based on the // The (non-standard but supported enough) innerText property is based on the
// render tree in Firefox and possibly other browsers, so we must insert the // render tree in Firefox and possibly other browsers, so we must insert the
// 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, root, config ) { var setClipboardData =
var body = node.ownerDocument.body; function ( event, contents, root, willCutCopy, toPlainText, plainTextOnly ) {
var willCutCopy = config.willCutCopy; var clipboardData = event.clipboardData;
var doc = event.target.ownerDocument;
var body = doc.body;
var node = createElement( doc, 'div' );
var html, text; var html, text;
node.appendChild( contents );
// Firefox will add an extra new line for BRs at the end of block when // 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. // calculating innerText, even though they don't actually affect display.
// So we need to remove them first. // So we need to remove them first.
cleanupBRs( node, root, true ); cleanupBRs( node, root, true );
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 ); html = plainTextOnly ? '' : node.innerHTML;
html = node.innerHTML; if ( toPlainText ) {
text = node.innerText || node.textContent; text = toPlainText( html );
} else {
body.appendChild( node );
text = node.innerText || node.textContent;
text = text.replace( / /g, ' ' ); // Replace nbsp with regular space
body.removeChild( node );
}
if ( willCutCopy ) { if ( !plainTextOnly && willCutCopy ) {
html = willCutCopy( html ); html = willCutCopy( html );
} }
@ -2252,18 +2260,18 @@ var setClipboardData = function ( clipboardData, node, root, config ) {
text = text.replace( /\r?\n/g, '\r\n' ); text = text.replace( /\r?\n/g, '\r\n' );
} }
clipboardData.setData( 'text/html', html ); if ( !plainTextOnly ) {
clipboardData.setData( 'text/html', html );
}
clipboardData.setData( 'text/plain', text ); clipboardData.setData( 'text/plain', text );
event.preventDefault();
body.removeChild( node );
}; };
var onCut = function ( event ) { var onCut = function ( event ) {
var clipboardData = event.clipboardData;
var range = this.getSelection(); var range = this.getSelection();
var root = this._root; var root = this._root;
var self = this; var self = this;
var startBlock, endBlock, copyRoot, contents, parent, newContents, node; var startBlock, endBlock, copyRoot, contents, parent, newContents;
// Nothing to do // Nothing to do
if ( range.collapsed ) { if ( range.collapsed ) {
@ -2275,7 +2283,7 @@ var onCut = function ( event ) {
this.saveUndoState( range ); this.saveUndoState( range );
// Edge only seems to support setting plain text as of 2016-03-11. // Edge only seems to support setting plain text as of 2016-03-11.
if ( !isEdge && clipboardData ) { if ( !isEdge && event.clipboardData ) {
// Clipboard content should include all parents within block, or all // Clipboard content should include all parents within block, or all
// parents up to root if selection across blocks // parents up to root if selection across blocks
startBlock = getStartBlockOfRange( range, root ); startBlock = getStartBlockOfRange( range, root );
@ -2295,10 +2303,8 @@ var onCut = function ( event ) {
parent = parent.parentNode; parent = parent.parentNode;
} }
// Set clipboard data // Set clipboard data
node = this.createElement( 'div' ); setClipboardData(
node.appendChild( contents ); event, contents, root, this._config.willCutCopy, null, false );
setClipboardData( clipboardData, node, root, this._config );
event.preventDefault();
} else { } else {
setTimeout( function () { setTimeout( function () {
try { try {
@ -2313,14 +2319,10 @@ var onCut = function ( event ) {
this.setSelection( range ); this.setSelection( range );
}; };
var onCopy = function ( event ) { var _onCopy = function ( event, range, root, willCutCopy, toPlainText, plainTextOnly ) {
var clipboardData = event.clipboardData; var startBlock, endBlock, copyRoot, contents, parent, newContents;
var range = this.getSelection();
var root = this._root;
var startBlock, endBlock, copyRoot, contents, parent, newContents, node;
// Edge only seems to support setting plain text as of 2016-03-11. // Edge only seems to support setting plain text as of 2016-03-11.
if ( !isEdge && clipboardData ) { if ( !isEdge && event.clipboardData ) {
// Clipboard content should include all parents within block, or all // Clipboard content should include all parents within block, or all
// parents up to root if selection across blocks // parents up to root if selection across blocks
startBlock = getStartBlockOfRange( range, root ); startBlock = getStartBlockOfRange( range, root );
@ -2345,13 +2347,21 @@ var onCopy = function ( event ) {
parent = parent.parentNode; parent = parent.parentNode;
} }
// Set clipboard data // Set clipboard data
node = this.createElement( 'div' ); setClipboardData( event, contents, root, willCutCopy, toPlainText, plainTextOnly );
node.appendChild( contents );
setClipboardData( clipboardData, node, root, this._config );
event.preventDefault();
} }
}; };
var onCopy = function ( event ) {
_onCopy(
event,
this.getSelection(),
this._root,
this._config.willCutCopy,
null,
false
);
};
// Need to monitor for shift key like this, as event.shiftKey is not available // Need to monitor for shift key like this, as event.shiftKey is not available
// in paste event. // in paste event.
function monitorShiftKey ( event ) { function monitorShiftKey ( event ) {
@ -3160,7 +3170,7 @@ proto._updatePath = function ( range, force ) {
// selectionchange is fired synchronously in IE when removing current selection // selectionchange is fired synchronously in IE when removing current selection
// and when setting new selection; keyup/mouseup may have processing we want // and when setting new selection; keyup/mouseup may have processing we want
// to do first. Either way, send to next event loop. // to do first. Either way, send to next event loop.
proto._updatePathOnEvent = function ( event ) { proto._updatePathOnEvent = function () {
var self = this; var self = this;
if ( self._isFocused && !self._willUpdatePath ) { if ( self._isFocused && !self._willUpdatePath ) {
self._willUpdatePath = true; self._willUpdatePath = true;
@ -4172,8 +4182,7 @@ proto._setHTML = function ( html ) {
}; };
proto.getHTML = function ( withBookMark ) { proto.getHTML = function ( withBookMark ) {
var brs = [], var html, range;
root, node, fixer, html, l, range;
if ( withBookMark && ( range = this.getSelection() ) ) { if ( withBookMark && ( range = this.getSelection() ) ) {
this._saveRangeToBookmark( range ); this._saveRangeToBookmark( range );
} }
@ -4968,6 +4977,7 @@ Squire.rangeDoesEndAtBlockBoundary = rangeDoesEndAtBlockBoundary;
Squire.expandRangeToBlockBoundaries = expandRangeToBlockBoundaries; Squire.expandRangeToBlockBoundaries = expandRangeToBlockBoundaries;
// Clipboard.js exports // Clipboard.js exports
Squire.onCopy = _onCopy;
Squire.onPaste = onPaste; Squire.onPaste = onPaste;
// Editor.js exports // Editor.js exports

File diff suppressed because one or more lines are too long

View file

@ -3,23 +3,32 @@
// The (non-standard but supported enough) innerText property is based on the // The (non-standard but supported enough) innerText property is based on the
// render tree in Firefox and possibly other browsers, so we must insert the // render tree in Firefox and possibly other browsers, so we must insert the
// 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, root, config ) { var setClipboardData =
var body = node.ownerDocument.body; function ( event, contents, root, willCutCopy, toPlainText, plainTextOnly ) {
var willCutCopy = config.willCutCopy; var clipboardData = event.clipboardData;
var doc = event.target.ownerDocument;
var body = doc.body;
var node = createElement( doc, 'div' );
var html, text; var html, text;
node.appendChild( contents );
// Firefox will add an extra new line for BRs at the end of block when // 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. // calculating innerText, even though they don't actually affect display.
// So we need to remove them first. // So we need to remove them first.
cleanupBRs( node, root, true ); cleanupBRs( node, root, true );
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 ); html = plainTextOnly ? '' : node.innerHTML;
html = node.innerHTML; if ( toPlainText ) {
text = node.innerText || node.textContent; text = toPlainText( html );
} else {
body.appendChild( node );
text = node.innerText || node.textContent;
text = text.replace( / /g, ' ' ); // Replace nbsp with regular space
body.removeChild( node );
}
if ( willCutCopy ) { if ( !plainTextOnly && willCutCopy ) {
html = willCutCopy( html ); html = willCutCopy( html );
} }
@ -30,18 +39,18 @@ var setClipboardData = function ( clipboardData, node, root, config ) {
text = text.replace( /\r?\n/g, '\r\n' ); text = text.replace( /\r?\n/g, '\r\n' );
} }
clipboardData.setData( 'text/html', html ); if ( !plainTextOnly ) {
clipboardData.setData( 'text/html', html );
}
clipboardData.setData( 'text/plain', text ); clipboardData.setData( 'text/plain', text );
event.preventDefault();
body.removeChild( node );
}; };
var onCut = function ( event ) { var onCut = function ( event ) {
var clipboardData = event.clipboardData;
var range = this.getSelection(); var range = this.getSelection();
var root = this._root; var root = this._root;
var self = this; var self = this;
var startBlock, endBlock, copyRoot, contents, parent, newContents, node; var startBlock, endBlock, copyRoot, contents, parent, newContents;
// Nothing to do // Nothing to do
if ( range.collapsed ) { if ( range.collapsed ) {
@ -53,7 +62,7 @@ var onCut = function ( event ) {
this.saveUndoState( range ); this.saveUndoState( range );
// Edge only seems to support setting plain text as of 2016-03-11. // Edge only seems to support setting plain text as of 2016-03-11.
if ( !isEdge && clipboardData ) { if ( !isEdge && event.clipboardData ) {
// Clipboard content should include all parents within block, or all // Clipboard content should include all parents within block, or all
// parents up to root if selection across blocks // parents up to root if selection across blocks
startBlock = getStartBlockOfRange( range, root ); startBlock = getStartBlockOfRange( range, root );
@ -73,10 +82,8 @@ var onCut = function ( event ) {
parent = parent.parentNode; parent = parent.parentNode;
} }
// Set clipboard data // Set clipboard data
node = this.createElement( 'div' ); setClipboardData(
node.appendChild( contents ); event, contents, root, this._config.willCutCopy, null, false );
setClipboardData( clipboardData, node, root, this._config );
event.preventDefault();
} else { } else {
setTimeout( function () { setTimeout( function () {
try { try {
@ -91,14 +98,10 @@ var onCut = function ( event ) {
this.setSelection( range ); this.setSelection( range );
}; };
var onCopy = function ( event ) { var _onCopy = function ( event, range, root, willCutCopy, toPlainText, plainTextOnly ) {
var clipboardData = event.clipboardData; var startBlock, endBlock, copyRoot, contents, parent, newContents;
var range = this.getSelection();
var root = this._root;
var startBlock, endBlock, copyRoot, contents, parent, newContents, node;
// Edge only seems to support setting plain text as of 2016-03-11. // Edge only seems to support setting plain text as of 2016-03-11.
if ( !isEdge && clipboardData ) { if ( !isEdge && event.clipboardData ) {
// Clipboard content should include all parents within block, or all // Clipboard content should include all parents within block, or all
// parents up to root if selection across blocks // parents up to root if selection across blocks
startBlock = getStartBlockOfRange( range, root ); startBlock = getStartBlockOfRange( range, root );
@ -123,13 +126,21 @@ var onCopy = function ( event ) {
parent = parent.parentNode; parent = parent.parentNode;
} }
// Set clipboard data // Set clipboard data
node = this.createElement( 'div' ); setClipboardData( event, contents, root, willCutCopy, toPlainText, plainTextOnly );
node.appendChild( contents );
setClipboardData( clipboardData, node, root, this._config );
event.preventDefault();
} }
}; };
var onCopy = function ( event ) {
_onCopy(
event,
this.getSelection(),
this._root,
this._config.willCutCopy,
null,
false
);
};
// Need to monitor for shift key like this, as event.shiftKey is not available // Need to monitor for shift key like this, as event.shiftKey is not available
// in paste event. // in paste event.
function monitorShiftKey ( event ) { function monitorShiftKey ( event ) {

View file

@ -33,6 +33,7 @@ Squire.rangeDoesEndAtBlockBoundary = rangeDoesEndAtBlockBoundary;
Squire.expandRangeToBlockBoundaries = expandRangeToBlockBoundaries; Squire.expandRangeToBlockBoundaries = expandRangeToBlockBoundaries;
// Clipboard.js exports // Clipboard.js exports
Squire.onCopy = _onCopy;
Squire.onPaste = onPaste; Squire.onPaste = onPaste;
// Editor.js exports // Editor.js exports