Commented a bug.
[cocanwiki.git] / html / _js / editor.js
index 7be9b42..84b930e 100644 (file)
@@ -1,6 +1,6 @@
 /* Javascript for OCAMLWIKI.
  * Copyright (C) 2004 Merjis Ltd.
- * $Id: editor.js,v 1.2 2004/10/26 17:17:08 rich Exp $
+ * $Id: editor.js,v 1.4 2004/10/26 17:50:23 rich Exp $
  */
 
 var delay = 1000;              // Delay in milliseconds before updating.
@@ -52,21 +52,21 @@ function init_edit_buttons (div_id, content_id, preview_id)
   // HTML for the edit buttons.
   var args = "'" + content_id + "', '" + preview_id + "'";
   var edit_buttons_html =
-    "<a href=\"#\" onmousedown=\"link(" + args + "); return false;\" title=\"Select some text and click this to make a link\"><u>Link</u></a>" +
+    "<a onmousedown=\"link(" + args + "); return false;\" title=\"Select some text and click this to make a link\"><u>Link</u></a>" +
     "<span class=\"spacer\"></span>" +
-    "<a href=\"#\" onmousedown=\"bold(" + args + "); return false;\" title=\"Select some text and click this to make it bold\"><strong>Bold</strong></a>" +
-    "<a href=\"#\" onmousedown=\"italic(" + args + "); return false;\" title=\"Select some text and click this to make it italic\"><em>Italic</em></a>" +
-    "<a href=\"#\" onmousedown=\"strikeout(" + args + "); return false;\" title=\"Select some text and click this to strike through it\"><s>Strike</s></a>" +
+    "<a onmousedown=\"bold(" + args + "); return false;\" title=\"Select some text and click this to make it bold\"><strong>Bold</strong></a>" +
+    "<a onmousedown=\"italic(" + args + "); return false;\" title=\"Select some text and click this to make it italic\"><em>Italic</em></a>" +
+    "<a onmousedown=\"strikeout(" + args + "); return false;\" title=\"Select some text and click this to strike through it\"><s>Strike</s></a>" +
     "<span class=\"spacer\"></span>" +
-    "<a href=\"#\" onmousedown=\"bullet(" + args + "); return false;\" title=\"Select lines of text and click this to make it a bullet list\"><big>&#x2219;&#8212;</big></a>" +
-    "<a href=\"#\" onmousedown=\"numbered(" + args + "); return false;\" title=\"Select lines of text and click this to make it a numbered list\">1&#8212;</a>" +
+    "<a onmousedown=\"bullet(" + args + "); return false;\" title=\"Select lines of text and click this to make it a bullet list\"><big>&#x2219;&#8212;</big></a>" +
+    "<a onmousedown=\"numbered(" + args + "); return false;\" title=\"Select lines of text and click this to make it a numbered list\">1&#8212;</a>" +
     "<br/>"
     ;
   var div = document.getElementById (div_id);
   div.innerHTML = edit_buttons_html;
 }
 
-function replace_selection (content_id, fn)
+function replace_selection (content_id, fn, warning)
 {
   var textarea = document.getElementById (content_id);
 
@@ -78,7 +78,8 @@ function replace_selection (content_id, fn)
       var replacement = fn (text);
       textarea.value = textarea.value.substring (0, start) +
        replacement + textarea.value.substring (end);
-    }
+    } else
+      if (warning) alert (warning);
   } else if (document.selection) { // IE
     var range = document.selection.createRange ();
     if (range.parentElement () == textarea && range.text != "") {
@@ -97,15 +98,6 @@ function replace_selection (content_id, fn)
       // Replace \r\n with just \n.
       text = text.replace ("\r\n", "\n");
 
-      /*
-      var wtf;
-      for (i = 0; i < len; ++i) {
-       wtf += "," + text.charCodeAt (i);
-       if ((i % 32) == 0) wtf += "\n";
-      }
-      alert (wtf);
-      */
-
       // IE's selections often include a trailing space or carriage
       // return.  Ignore this whitespace when calling the replacement
       // function.
@@ -122,7 +114,8 @@ function replace_selection (content_id, fn)
       if (append) replacement += "\n";
 
       range.text = replacement;
-    }
+    } else
+       if (warning) alert (warning);
   }
 }
 
@@ -134,7 +127,8 @@ function link (content_id, preview_id)
     else
       return "[[" + text + "]]";
   };
-  replace_selection (content_id, fn);
+  var warning = "Select an area of text first.";
+  replace_selection (content_id, fn, warning);
   update_preview_now (content_id, preview_id);
 }
 
@@ -146,7 +140,8 @@ function bold (content_id, preview_id)
     else
       return "<b>" + text + "</b>";
   };
-  replace_selection (content_id, fn);
+  var warning = "Select an area of text first.";
+  replace_selection (content_id, fn, warning);
   update_preview_now (content_id, preview_id);
 }
 
@@ -158,7 +153,8 @@ function italic (content_id, preview_id)
     else
       return "<i>" + text + "</i>";
   };
-  replace_selection (content_id, fn);
+  var warning = "Select an area of text first.";
+  replace_selection (content_id, fn, warning);
   update_preview_now (content_id, preview_id);
 }
 
@@ -170,7 +166,8 @@ function strikeout (content_id, preview_id)
     else
       return "<s>" + text + "</s>";
   };
-  replace_selection (content_id, fn);
+  var warning = "Select an area of text first.";
+  replace_selection (content_id, fn, warning);
   update_preview_now (content_id, preview_id);
 }
 
@@ -182,7 +179,8 @@ function bullet (content_id, preview_id)
     else
       return "* " + text.replace (/\n/g, "\n* ");
   };
-  replace_selection (content_id, fn);
+  var warning = "Select some whole lines of text first.";
+  replace_selection (content_id, fn, warning);
   update_preview_now (content_id, preview_id);
 }
 
@@ -194,6 +192,7 @@ function numbered (content_id, preview_id)
     else
       return "# " + text.replace (/\n/g, "\n# ");
   };
-  replace_selection (content_id, fn);
+  var warning = "Select some whole lines of text first.";
+  replace_selection (content_id, fn, warning);
   update_preview_now (content_id, preview_id);
 }