1 /* Javascript for OCAMLWIKI.
2 * Copyright (C) 2004 Merjis Ltd.
3 * $Id: editor.js,v 1.2 2004/10/26 17:17:08 rich Exp $
6 var delay = 1000; // Delay in milliseconds before updating.
8 function update_preview (content_id, preview_id)
10 // Updating is quite expensive, so only update after a period of apparent
12 var preview = document.getElementById (preview_id);
13 if (preview.timer) clearTimeout (preview.timer);
15 setTimeout ("update_preview_now ('" + content_id + "', '" +
16 preview_id + "')", delay);
19 function update_preview_now (content_id, preview_id)
22 var preview = document.getElementById (preview_id);
23 if (preview.timer) preview.timer = null;
25 // Get the Wiki-markup content from the content textarea.
26 var content = document.getElementById (content_id).value;
28 // Send the Wiki-markup to the server to be turned into XHTML.
29 var http = document.all ?
30 new ActiveXObject ('Microsoft.XMLHTTP') : new XMLHttpRequest ();
33 http.open ('POST', '/_bin/preview.cmo', false);
34 http.setRequestHeader ('Content-Type',
35 'application/x-www-form-urlencoded');
36 http.send ('content=' + encodeURIComponent (content));
38 var xhtml = http.responseText;
40 // Next line fails with my copy of IE if the text contains a
41 // link (ie. <a href...>). It is unclear why. The error is:
42 // "Unknown runtime error"
43 preview.innerHTML = xhtml;
47 // Initialise the edit_buttons for a section.
48 // We do this in Javascript so that non-Javascript users won't see
49 // any buttons (they wouldn't be functional for them anyway).
50 function init_edit_buttons (div_id, content_id, preview_id)
52 // HTML for the edit buttons.
53 var args = "'" + content_id + "', '" + preview_id + "'";
54 var edit_buttons_html =
55 "<a href=\"#\" onmousedown=\"link(" + args + "); return false;\" title=\"Select some text and click this to make a link\"><u>Link</u></a>" +
56 "<span class=\"spacer\"></span>" +
57 "<a href=\"#\" onmousedown=\"bold(" + args + "); return false;\" title=\"Select some text and click this to make it bold\"><strong>Bold</strong></a>" +
58 "<a href=\"#\" onmousedown=\"italic(" + args + "); return false;\" title=\"Select some text and click this to make it italic\"><em>Italic</em></a>" +
59 "<a href=\"#\" onmousedown=\"strikeout(" + args + "); return false;\" title=\"Select some text and click this to strike through it\"><s>Strike</s></a>" +
60 "<span class=\"spacer\"></span>" +
61 "<a href=\"#\" onmousedown=\"bullet(" + args + "); return false;\" title=\"Select lines of text and click this to make it a bullet list\"><big>∙—</big></a>" +
62 "<a href=\"#\" onmousedown=\"numbered(" + args + "); return false;\" title=\"Select lines of text and click this to make it a numbered list\">1—</a>" +
65 var div = document.getElementById (div_id);
66 div.innerHTML = edit_buttons_html;
69 function replace_selection (content_id, fn)
71 var textarea = document.getElementById (content_id);
73 if (textarea.setSelectionRange) { // Mozilla
74 var start = textarea.selectionStart;
75 var end = textarea.selectionEnd;
77 var text = textarea.value.substring (start, end);
78 var replacement = fn (text);
79 textarea.value = textarea.value.substring (0, start) +
80 replacement + textarea.value.substring (end);
82 } else if (document.selection) { // IE
83 var range = document.selection.createRange ();
84 if (range.parentElement () == textarea && range.text != "") {
85 var text = range.text;
86 var len = text.length;
88 // IE6 bug workaround: If the end of the selection is the end of
89 // a line, then we must remember to append a \n character after
90 // doing the replacement. Note the incredible lengths we have to
91 // go to here because IE is a piece of shit.
92 var dup = range.duplicate ();
93 dup.moveEnd ("character", 2);
95 dup.text.substring (dup.text.length-3, dup.text.length-1) == "\r\n";
97 // Replace \r\n with just \n.
98 text = text.replace ("\r\n", "\n");
102 for (i = 0; i < len; ++i) {
103 wtf += "," + text.charCodeAt (i);
104 if ((i % 32) == 0) wtf += "\n";
109 // IE's selections often include a trailing space or carriage
110 // return. Ignore this whitespace when calling the replacement
113 if ((ar = text.match (/(\s+)$/))) {
115 text = text.substring (0, len - ws.length);
116 replacement = fn (text);
117 replacement = replacement + ws;
119 replacement = fn (range.text);
121 // See IE bug workaround above.
122 if (append) replacement += "\n";
124 range.text = replacement;
129 function link (content_id, preview_id)
131 var fn = function (text) {
132 if (ar = text.match (/^\[\[(.*)\]\]$/))
135 return "[[" + text + "]]";
137 replace_selection (content_id, fn);
138 update_preview_now (content_id, preview_id);
141 function bold (content_id, preview_id)
143 var fn = function (text) {
144 if (ar = text.match (/^<b>(.*)<\/b>$/))
147 return "<b>" + text + "</b>";
149 replace_selection (content_id, fn);
150 update_preview_now (content_id, preview_id);
153 function italic (content_id, preview_id)
155 var fn = function (text) {
156 if (ar = text.match (/^<i>(.*)<\/i>$/))
159 return "<i>" + text + "</i>";
161 replace_selection (content_id, fn);
162 update_preview_now (content_id, preview_id);
165 function strikeout (content_id, preview_id)
167 var fn = function (text) {
168 if (ar = text.match (/^<s>(.*)<\/s>$/))
171 return "<s>" + text + "</s>";
173 replace_selection (content_id, fn);
174 update_preview_now (content_id, preview_id);
177 function bullet (content_id, preview_id)
179 var fn = function (text) {
180 if (text.match (/^\*/))
181 return text.replace (/^\* /gm, "");
183 return "* " + text.replace (/\n/g, "\n* ");
185 replace_selection (content_id, fn);
186 update_preview_now (content_id, preview_id);
189 function numbered (content_id, preview_id)
191 var fn = function (text) {
192 if (text.match (/^#/))
193 return text.replace (/^# /gm, "");
195 return "# " + text.replace (/\n/g, "\n# ");
197 replace_selection (content_id, fn);
198 update_preview_now (content_id, preview_id);