Edit toolbar - steal a little trick from DokuWiki: When you click a button
authorErik Moeller <erik@users.mediawiki.org>
Mon, 29 Aug 2005 19:43:36 +0000 (19:43 +0000)
committerErik Moeller <erik@users.mediawiki.org>
Mon, 29 Aug 2005 19:43:36 +0000 (19:43 +0000)
with no selection, the example text within the formatting, e.g. the "Link
title" in [[Link title]], is highlighted so that you can immediately type
over it.

Gecko only for now due to lack of testability under IE, if anyone wants to
hack it for IE, the code is at
http://wiki.splitbrain.org/lib/scripts/script.js

Note that they copied it from us first, so it's fair game ;-)

skins/common/wikibits.js

index b19bfc3..7ee99f7 100644 (file)
@@ -318,8 +318,10 @@ function insertTags(tagOpen, tagClose, sampleText) {
 
        // Mozilla
        } else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
+               var replaced = false;
                var startPos = txtarea.selectionStart;
                var endPos = txtarea.selectionEnd;
+               if(endPos-startPos) replaced=true;
                var scrollTop=txtarea.scrollTop;
                var myText = (txtarea.value).substring(startPos, endPos);
                if(!myText) { myText=sampleText;}
@@ -331,10 +333,15 @@ function insertTags(tagOpen, tagClose, sampleText) {
                txtarea.value = txtarea.value.substring(0, startPos) + subst +
                  txtarea.value.substring(endPos, txtarea.value.length);
                txtarea.focus();
-
-               var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
-               txtarea.selectionStart=cPos;
-               txtarea.selectionEnd=cPos;
+               //set new selection
+               if(replaced){
+                       var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
+                       txtarea.selectionStart=cPos;
+                       txtarea.selectionEnd=cPos;
+               }else{
+                       txtarea.selectionStart=startPos+tagOpen.length;   
+                       txtarea.selectionEnd=startPos+tagOpen.length+myText.length;
+               }       
                txtarea.scrollTop=scrollTop;
 
        // All other browsers get no toolbar.