r56194 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r56193‎ | r56194 | r56195 >
Date:18:10, 11 September 2009
Author:catrope
Status:ok (Comments)
Tags:
Comment:
EditToolbar: (bug 20581, bug 20582, bug 20584) Make search&replace dialog select replaced text, inform the user of how many replacements were made if not equal to one, and make sure search+replacing headers updates the NTOC.

On a technical level, this adds a function to set the cursor position in a textarea, and changes the S&R code to use encapsulateSelection()
Modified paths:
  • /trunk/extensions/UsabilityInitiative/EditToolbar/EditToolbar.hooks.php (modified) (history)
  • /trunk/extensions/UsabilityInitiative/EditToolbar/EditToolbar.i18n.php (modified) (history)
  • /trunk/extensions/UsabilityInitiative/EditToolbar/EditToolbar.js (modified) (history)
  • /trunk/extensions/UsabilityInitiative/js/plugins/jquery.textSelection.js (modified) (history)

Diff [purge]

Index: trunk/extensions/UsabilityInitiative/EditToolbar/EditToolbar.hooks.php
@@ -104,6 +104,7 @@
105105 'edittoolbar-tool-replace-button',
106106 'edittoolbar-tool-replace-close',
107107 'edittoolbar-tool-replace-nomatch',
 108+ 'edittoolbar-tool-replace-success',
108109 /* Special Characters Section */
109110 'edittoolbar-section-characters',
110111 'edittoolbar-characters-page-latin',
Index: trunk/extensions/UsabilityInitiative/EditToolbar/EditToolbar.js
@@ -1156,15 +1156,26 @@
11571157 }
11581158 var regex = new RegExp( searchStr, flags );
11591159 var $textarea = $j(this).data( 'context' ).$textarea;
1160 - if ( !$textarea.val().match( regex ) ) {
 1160+ var matches = $textarea.val().match( regex );
 1161+ if ( !matches ) {
11611162 alert( gM( 'edittoolbar-tool-replace-nomatch' ) );
11621163 } else {
1163 - $textarea.val( $textarea.val().replace( regex, replaceStr ) );
 1164+ var start, end;
 1165+ for ( var i = 0; i < matches.length; i++ ) {
 1166+ start = $textarea.val().indexOf( matches[i] );
 1167+ end = start + matches[i].length;
 1168+ $textarea.setSelection( start, end );
 1169+ $textarea.encapsulateSelection( '', replaceStr, '', false, true );
 1170+ }
 1171+ if ( i > 1 )
 1172+ alert( gM( 'edittoolbar-tool-replace-success', i ) );
 1173+ $textarea.scrollToCaretPosition( start );
 1174+ $textarea.setSelection( start, start + replaceStr.length );
11641175 }
1165 - // TODO: Hook for wikEd
11661176 },
11671177 'edittoolbar-tool-replace-close': function() {
11681178 $j(this).dialog( 'close' );
 1179+ $j(this).data( 'context' ).$textarea.focus();
11691180 }
11701181 }
11711182 }
Index: trunk/extensions/UsabilityInitiative/EditToolbar/EditToolbar.i18n.php
@@ -91,6 +91,7 @@
9292 'edittoolbar-tool-replace-button' => 'Replace',
9393 'edittoolbar-tool-replace-close' => 'Close',
9494 'edittoolbar-tool-replace-nomatch' => 'Your search did not match anything.',
 95+ 'edittoolbar-tool-replace-success' => '$1 replacements made.',
9596 /* Special characters Section */
9697 'edittoolbar-section-characters' => 'Special characters',
9798 'edittoolbar-characters-page-latin' => 'Latin',
Index: trunk/extensions/UsabilityInitiative/js/plugins/jquery.textSelection.js
@@ -205,6 +205,21 @@
206206 }
207207 return getCaret( this.get( 0 ) );
208208 },
 209+setSelection: function( start, end ) {
 210+ return this.each( function() {
 211+ if ( this.selectionStart || this.selectionStart == '0' ) {
 212+ this.selectionStart = start;
 213+ this.selectionEnd = end;
 214+ } else if ( document.body.createTextRange ) {
 215+ var selection = document.body.createTextRange;
 216+ selection.setToElementText( this );
 217+ var length = selection.text.length;
 218+ selection.moveStart( 'character', start );
 219+ selection.moveEnd( 'character', -length + end );
 220+ selection.select();
 221+ }
 222+ });
 223+},
209224 /**
210225 * Ported from Wikia's LinkSuggest extension
211226 * https://svn.wikia-code.com/wikia/trunk/extensions/wikia/LinkSuggest

Follow-up revisions

RevisionCommit summaryAuthorDate
r56698EditToolbar: (bug 20581) Also pop up a dialog box when only one replacement w...catrope11:46, 21 September 2009

Comments

#Comment by Siebrand (talk | contribs)   18:24, 11 September 2009

+ 'edittoolbar-tool-replace-success' => '$1 replacements made.',

Needs plural support in JavaScript

#Comment by Catrope (talk | contribs)   18:33, 11 September 2009

Good catch. Does the fact that $1 is guaranteed to be at least 2 help?

#Comment by Siebrand (talk | contribs)   18:39, 11 September 2009

Not really when there are languages that have 3 plural forms or more (see languages/classes). This is a (known) major feature request for Ajax. Maybe we should tag these commits with something like 'ajax-plural' or whatever can make them recognisable.

#Comment by Catrope (talk | contribs)   18:42, 11 September 2009

Be my guest; or maybe you can tag the messages (dunno if that's possible in TranslateWiki)?

#Comment by Nikerabbit (talk | contribs)   20:17, 11 September 2009

I'm willing to help to get plural support done with someone who knows JS better than I do.

Status & tagging log