Esanders has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/80301


Change subject: WIP copy-paste fixes
......................................................................

WIP copy-paste fixes

Change-Id: Ib3702f9441f6ee3fa34ec071f00994dd7e591d99
---
M modules/ve/ce/ve.ce.Surface.js
1 file changed, 49 insertions(+), 19 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor 
refs/changes/01/80301/1

diff --git a/modules/ve/ce/ve.ce.Surface.js b/modules/ve/ce/ve.ce.Surface.js
index 3c554f4..b3aefe0 100644
--- a/modules/ve/ce/ve.ce.Surface.js
+++ b/modules/ve/ce/ve.ce.Surface.js
@@ -63,13 +63,16 @@
                $documentNode.focus();
        } );
 
-       this.$.on( {
+       this.$document.on( {
                'cut': ve.bind( this.onCut, this ),
                'copy': ve.bind( this.onCopy, this ),
-               'paste': ve.bind( this.onPaste, this ),
+               'paste': ve.bind( this.onPaste, this )
+       } );
+       this.$.on( {
                'dragover': ve.bind( this.onDocumentDragOver, this ),
                'drop': ve.bind( this.onDocumentDrop, this )
        } );
+
        if ( $.browser.msie ) {
                this.$.on( 'beforepaste', ve.bind( this.onPaste, this ) );
        }
@@ -590,21 +593,37 @@
  * @method
  * @param {jQuery.Event} e Copy event
  */
-ve.ce.Surface.prototype.onCopy = function () {
-       var sel = rangy.getSelection( this.$document[0] ),
-               $frag = this.$$( sel.getRangeAt(0).cloneContents() ),
+ve.ce.Surface.prototype.onCopy = function ( e ) {
+       var fragText, key = '',
+               sel = rangy.getSelection( this.$document[0] ),
+               $frag = sel.rangeCount ? this.$$( 
sel.getRangeAt(0).cloneContents() ) : null,
                slice = this.documentView.model.getSlice( 
this.model.getSelection() ),
-               key = '';
+               clipboardData = e.originalEvent.clipboardData;
 
        // CLone the elements in the slice
        slice.cloneElements();
 
-       // Create key from text and element names
-       $frag.contents().each( function () {
-               key += this.textContent || this.nodeName;
-       } );
-       key = 've-' + key.replace( /\s/gm, '' );
-
+       // Check we have setData and that it actually works (returns true)
+       if ( clipboardData && clipboardData.setData && clipboardData.setData( 
'text/xcustom', '' ) ) {
+               // Webkit
+               fragText = $frag ? $frag.text() : '';
+               key = 've-range-' + ve.getHash( slice.range );
+               e.preventDefault();
+               clipboardData.setData( 'text/xcustom', key );
+               clipboardData.setData( 'text/plain', fragText );
+       } else if ( window.clipboardData ) {
+               // IE
+               fragText = $frag ? $frag.text() : '';
+               e.originalEvent.returnValue = false;
+               window.clipboardData.setData( 'text/plain', fragText );
+               key = 've-' + fragText.replace( /\s/gm, '' );
+       } else {
+               // Create key from text and element names
+               $frag.contents().each( function () {
+                       key += this.textContent || '';//this.nodeName;
+               } );
+               key = 've-' + key.replace( /\s/gm, '' );
+       }
        // Set clipboard
        this.clipboard[key] = slice;
 };
@@ -615,7 +634,7 @@
  * @method
  * @param {jQuery.Event} e Paste event
  */
-ve.ce.Surface.prototype.onPaste = function () {
+ve.ce.Surface.prototype.onPaste = function ( e ) {
        // Prevent pasting until after we are done
        if ( this.pasting ) {
                return false;
@@ -625,7 +644,10 @@
        var tx, scrollTop,
                $window = $( ve.Element.getWindow( this.$$.context ) ),
                view = this,
-               selection = this.model.getSelection();
+               selection = this.model.getSelection(),
+               clipboardData = e.originalEvent.clipboardData,
+               eventPasteKey = clipboardData.getData( 'text/xcustom' ) || null,
+               eventPasteText = clipboardData.getData( 'text/plain' ) || null;
 
        this.surfaceObserver.stop();
 
@@ -644,11 +666,19 @@
                var pasteData, slice, tx,
                        key = '';
 
-               // Create key from text and element names
-               view.$pasteTarget.hide().contents().each( function () {
-                       key += this.textContent || this.nodeName;
-               } );
-               key = 've-' + key.replace( /\s/gm, '' );
+               if ( eventPasteKey ) {
+                       key = eventPasteKey;
+               } else {
+                       if ( eventPasteText ) {
+                               key = eventPasteText;
+                       } else {
+                               // Create key from text and element names
+                               view.$pasteTarget.hide().contents().each( 
function () {
+                                       key += this.textContent || 
this.nodeName;
+                               } );
+                       }
+                       key = 've-' + key.replace( /\s/gm, '' );
+               }
 
                // Get linear model from clipboard or create array from unknown 
pasted content
                if ( view.clipboard[key] ) {

-- 
To view, visit https://gerrit.wikimedia.org/r/80301
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib3702f9441f6ee3fa34ec071f00994dd7e591d99
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Esanders <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to