Netbrain has uploaded a new change for review.

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


Change subject: Added "search and replace within selection" functionality
......................................................................

Added "search and replace within selection" functionality

This enables users to first select some text in the textarea, and
click the checkbox labeled "Search/replace within text selection"
when searching / replacing text.

When this checkbox is checked, the text searched/replaced will a subset
of the original text determined by the user's text selection.

Change-Id: Id0060b03f080106520f7a28ea6411b4d60086392
---
M WikiEditor.i18n.php
M WikiEditor.php
M modules/jquery.wikiEditor.dialogs.config.js
3 files changed, 40 insertions(+), 15 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikiEditor 
refs/changes/22/87322/1

diff --git a/WikiEditor.i18n.php b/WikiEditor.i18n.php
index 0cd1f0b..980476b 100644
--- a/WikiEditor.i18n.php
+++ b/WikiEditor.i18n.php
@@ -173,6 +173,7 @@
        'wikieditor-toolbar-tool-replace-search' => 'Search for:',
        'wikieditor-toolbar-tool-replace-replace' => 'Replace with:',
        'wikieditor-toolbar-tool-replace-case' => 'Match case',
+       'wikieditor-toolbar-tool-replace-selection' => 'Search/replace within 
text selection',
        'wikieditor-toolbar-tool-replace-regex' => 'Treat search string as a 
regular expression',
        'wikieditor-toolbar-tool-replace-button-findnext' => 'Find next',
        'wikieditor-toolbar-tool-replace-button-replace' => 'Replace',
@@ -460,6 +461,7 @@
        'wikieditor-toolbar-tool-replace-search' => '{{Identical|Search}}',
        'wikieditor-toolbar-tool-replace-case' => 'This could as well be 
translated as "case sensitive search" -
 "case" refers to character case, that is upper case, or lower case.',
+       'wikieditor-toolbar-tool-replace-selection' => 'This could be 
translated as search/replace using selection, giving the meaning that 
search/replace should focus on a subset of the thext, specified by the text 
selection.',
        'wikieditor-toolbar-tool-replace-regex' => 'A "regular expression" is a 
syntax to find strings based on patterns, for more see [[wikipedia:Regular 
expression|the Wikipedia article]].
 
 More notes:
diff --git a/WikiEditor.php b/WikiEditor.php
index 103c9eb..10eaa53 100644
--- a/WikiEditor.php
+++ b/WikiEditor.php
@@ -309,6 +309,7 @@
                        'wikieditor-toolbar-tool-replace-search',
                        'wikieditor-toolbar-tool-replace-replace',
                        'wikieditor-toolbar-tool-replace-case',
+                       'wikieditor-toolbar-tool-replace-selection',
                        'wikieditor-toolbar-tool-replace-regex',
                        'wikieditor-toolbar-tool-replace-button-findnext',
                        'wikieditor-toolbar-tool-replace-button-replace',
diff --git a/modules/jquery.wikiEditor.dialogs.config.js 
b/modules/jquery.wikiEditor.dialogs.config.js
index 0d76916..f81baa2 100644
--- a/modules/jquery.wikiEditor.dialogs.config.js
+++ b/modules/jquery.wikiEditor.dialogs.config.js
@@ -1127,6 +1127,10 @@
                                                        <input type="checkbox" 
id="wikieditor-toolbar-replace-case"/>\
                                                        <label 
for="wikieditor-toolbar-replace-case" 
rel="wikieditor-toolbar-tool-replace-case"></label>\
                                                </div>\
+                                           <div 
class="wikieditor-toolbar-field-wrapper">\
+                                                       <input type="checkbox" 
id="wikieditor-toolbar-replace-selection"/>\
+                                                       <label 
for="wikieditor-toolbar-replace-selection" 
rel="wikieditor-toolbar-tool-replace-selection"></label>\
+                                               </div>\
                                                <div 
class="wikieditor-toolbar-field-wrapper">\
                                                        <input type="checkbox" 
id="wikieditor-toolbar-replace-regex"/>\
                                                        <label 
for="wikieditor-toolbar-replace-regex" 
rel="wikieditor-toolbar-tool-replace-regex"></label>\
@@ -1180,9 +1184,22 @@
                                                        return;
                                                }
 
-                                               var $textarea = $(this).data( 
'context' ).$textarea;
-                                               var text = 
$textarea.textSelection( 'getContents' );
-                                               var match = false;
+
+                        var $textarea = $(this).data( 'context' ).$textarea;
+                        var text = $textarea.textSelection( 'getContents' );
+                        var selection = $(this).data( 'selection' );
+                        var searchStart,searchEnd;
+                        if ( $( '#wikieditor-toolbar-replace-selection' ).is( 
':checked' ) && selection[0] !== selection[1] ){
+                            var selection = $(this).data( 'selection' );
+                            searchStart = selection[0];
+                            searchEnd = selection[1];
+                            text = text.substring( searchStart, searchEnd );
+                        } else {
+                            searchStart = 0;
+                            searchEnd = text.length;
+                        }
+
+                        var match = false;
                                                if ( mode !== 'replaceAll' ) {
                                                        if ( mode === 'replace' 
) {
                                                                offset = 
$(this).data( 'matchIndex' );
@@ -1218,7 +1235,7 @@
                                                                var matchedText 
= textRemainder.substr( index, match[i].length );
                                                                textRemainder = 
textRemainder.substr( index + match[i].length );
 
-                                                               start = index + 
offset;
+                                                               start = index + 
offset + searchStart;
                                                                end = start + 
match[i].length;
                                                                // Make regex 
placeholder substitution ($1) work
                                                                var replace = 
isRegex ? matchedText.replace( regex, replaceStr ) : replaceStr;
@@ -1229,7 +1246,7 @@
                                                                                
        'peri': replace,
                                                                                
        'replace': true } )
                                                                        
.textSelection( 'setSelection', { 'start': start, 'end': newEnd } );
-                                                               offset = newEnd;
+                                                               offset = index 
+ offset + replace.length;
                                                        }
                                                        $( 
'#wikieditor-toolbar-replace-success' )
                                                                .text( mw.msg( 
'wikieditor-toolbar-tool-replace-success', match.length ) )
@@ -1249,20 +1266,24 @@
 
                                                                if (match) {
                                                                        // Do 
the replacement
-                                                                       
$textarea.textSelection( 'encapsulateSelection', {
+                                                                       
$textarea
+                                        .textSelection( 
'encapsulateSelection', {
                                                                                
        'peri': actualReplacement,
-                                                                               
        'replace': true } );
+                                                                               
        'replace': true,
+                                                                               
        'selectionStart': searchStart + match.index + offset,
+                                            'selectionEnd': searchStart + 
match.index + offset + match[0].length
+                                        } );
                                                                        // 
Reload the text after replacement
-                                                                       text = 
$textarea.textSelection( 'getContents' );
+                                                                       text = 
$textarea.textSelection( 'getContents').substring( searchStart, searchEnd );
                                                                }
 
                                                                // Find the 
next instance
-                                                               offset = offset 
+ match[0].length + actualReplacement.length;
+                                                               offset = offset 
+ match.index + actualReplacement.length;
                                                                textRemainder = 
text.substr( offset );
                                                                match = 
textRemainder.match( regex );
 
                                                                if (match) {
-                                                                       start = 
offset + match.index;
+                                                                       start = 
offset + match.index + searchStart;
                                                                        end = 
start + match[0].length;
                                                                } else {
                                                                        // If 
no new string was found, try searching from the beginning.
@@ -1270,7 +1291,7 @@
                                                                        
textRemainder = text;
                                                                        match = 
textRemainder.match( regex );
                                                                        if 
(match) {
-                                                                               
start = match.index;
+                                                                               
start = match.index + searchStart;
                                                                                
end = start + match[0].length;
                                                                        } else {
                                                                                
// Give up
@@ -1279,18 +1300,17 @@
                                                                        }
                                                                }
                                                        } else {
-                                                               start = offset 
+ match.index;
+                                                               start = offset 
+ match.index + searchStart;
                                                                end = start + 
match[0].length;
                                                        }
 
-                                                       $( this ).data( 
'matchIndex', start);
-
+                                                       $( this ).data( 
'matchIndex', start - searchStart);
                                                        
$textarea.textSelection( 'setSelection', {
                                                                        
'start': start,
                                                                        'end': 
end
                                                        } );
                                                        
$textarea.textSelection( 'scrollToCaretPosition' );
-                                                       $( this ).data( 
'offset', end );
+                                                       $( this ).data( 
'offset', end - searchStart );
                                                        var context = $( this 
).data( 'context' );
                                                        var textbox = typeof 
context.$iframe !== 'undefined' ?
                                                                        
context.$iframe[0].contentWindow : $textarea[0];
@@ -1360,6 +1380,8 @@
                                                                        
$(that).dialog( 'close' );
                                                                }
                                                        });
+
+                        $(this).data( 'selection', $(textbox).textSelection( 
'getCaretPosition',{startAndEnd:true}));
                                        },
                                        close: function () {
                                                var context = $(this).data( 
'context' );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id0060b03f080106520f7a28ea6411b4d60086392
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikiEditor
Gerrit-Branch: master
Gerrit-Owner: Netbrain <k...@heldig.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to