textSelection: Use execcommand to replace text
authorDerk-Jan Hartman <hartman.wiki@gmail.com>
Sun, 18 Nov 2018 00:48:56 +0000 (01:48 +0100)
committerTheDJ <hartman.wiki@gmail.com>
Thu, 29 Nov 2018 20:42:58 +0000 (20:42 +0000)
This preserves the undo history.
Should be supported by all browsers but Firefox.

Bug: T33780
Change-Id: I857965141e8e3aeabacf505d6cfce798f88a9711

resources/src/jquery/jquery.textSelection.js

index 6b4ab97..cbbed99 100644 (file)
                        setContents: function ( content ) {
                                return this.each( function () {
                                        var scrollTop = this.scrollTop;
-                                       $( this ).val( content );
+                                       this.select();
+                                       if ( !document.execCommand( 'insertText', false, content ) ) {
+                                               $( this ).val( content );
+                                       }
                                        // Setting this.value may scroll the textarea, restore the scroll position
                                        this.scrollTop = scrollTop;
                                } );
                                return this.each( function () {
                                        var allText, currSelection, startPos, endPos;
 
-                                       allText = $( this ).textSelection( 'getContents' );
-                                       currSelection = $( this ).textSelection( 'getCaretPosition', { startAndEnd: true } );
-                                       startPos = currSelection[ 0 ];
-                                       endPos = currSelection[ 1 ];
+                                       if ( !document.execCommand( 'insertText', false, value ) ) {
+                                               allText = $( this ).textSelection( 'getContents' );
+                                               currSelection = $( this ).textSelection( 'getCaretPosition', { startAndEnd: true } );
+                                               startPos = currSelection[ 0 ];
+                                               endPos = currSelection[ 1 ];
 
-                                       $( this ).textSelection( 'setContents', allText.slice( 0, startPos ) + value +
-                                               allText.slice( endPos ) );
-                                       $( this ).textSelection( 'setSelection', {
-                                               start: startPos,
-                                               end: startPos + value.length
-                                       } );
+                                               $( this ).textSelection( 'setContents', allText.slice( 0, startPos ) + value +
+                                                       allText.slice( endPos ) );
+                                               $( this ).textSelection( 'setSelection', {
+                                                       start: startPos,
+                                                       end: startPos + value.length
+                                               } );
+                                       }
                                } );
                        },