2 * editor_plugin_src.js
\r
4 * Copyright 2009, Moxiecode Systems AB
\r
5 * Released under LGPL License.
\r
7 * License: http://tinymce.moxiecode.com/license
\r
8 * Contributing: http://tinymce.moxiecode.com/contributing
\r
12 var JSONRequest = tinymce.util.JSONRequest, each = tinymce.each, DOM = tinymce.DOM;
\r
14 tinymce.create('tinymce.plugins.SpellcheckerPlugin', {
\r
15 getInfo : function() {
\r
17 longname : 'Spellchecker',
\r
18 author : 'Moxiecode Systems AB',
\r
19 authorurl : 'http://tinymce.moxiecode.com',
\r
20 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/spellchecker',
\r
21 version : tinymce.majorVersion + "." + tinymce.minorVersion
\r
25 init : function(ed, url) {
\r
30 t.rpcUrl = ed.getParam("spellchecker_rpc_url", "{backend}");
\r
32 if (t.rpcUrl == '{backend}') {
\r
33 // Sniff if the browser supports native spellchecking (Don't know of a better way)
\r
37 t.hasSupport = true;
\r
39 // Disable the context menu when spellchecking is active
\r
40 ed.onContextMenu.addToTop(function(ed, e) {
\r
46 // Register commands
\r
47 ed.addCommand('mceSpellCheck', function() {
\r
48 if (t.rpcUrl == '{backend}') {
\r
49 // Enable/disable native spellchecker
\r
50 t.editor.getBody().spellcheck = t.active = !t.active;
\r
55 ed.setProgressState(1);
\r
56 t._sendRPC('checkWords', [t.selectedLang, t._getWords()], function(r) {
\r
60 ed.setProgressState(0);
\r
63 ed.setProgressState(0);
\r
65 if (ed.getParam('spellchecker_report_no_misspellings', true))
\r
66 ed.windowManager.alert('spellchecker.no_mpell');
\r
73 ed.onInit.add(function() {
\r
74 if (ed.settings.content_css !== false)
\r
75 ed.dom.loadCSS(url + '/css/content.css');
\r
78 ed.onClick.add(t._showMenu, t);
\r
79 ed.onContextMenu.add(t._showMenu, t);
\r
80 ed.onBeforeGetContent.add(function() {
\r
85 ed.onNodeChange.add(function(ed, cm) {
\r
86 cm.setActive('spellchecker', t.active);
\r
89 ed.onSetContent.add(function() {
\r
93 ed.onBeforeGetContent.add(function() {
\r
97 ed.onBeforeExecCommand.add(function(ed, cmd) {
\r
98 if (cmd == 'mceFullScreen')
\r
102 // Find selected language
\r
104 each(ed.getParam('spellchecker_languages', '+English=en,Danish=da,Dutch=nl,Finnish=fi,French=fr,German=de,Italian=it,Polish=pl,Portuguese=pt,Spanish=es,Swedish=sv', 'hash'), function(v, k) {
\r
105 if (k.indexOf('+') === 0) {
\r
106 k = k.substring(1);
\r
107 t.selectedLang = v;
\r
110 t.languages[k] = v;
\r
114 createControl : function(n, cm) {
\r
115 var t = this, c, ed = t.editor;
\r
117 if (n == 'spellchecker') {
\r
118 // Use basic button if we use the native spellchecker
\r
119 if (t.rpcUrl == '{backend}') {
\r
120 // Create simple toggle button if we have native support
\r
122 c = cm.createButton(n, {title : 'spellchecker.desc', cmd : 'mceSpellCheck', scope : t});
\r
127 c = cm.createSplitButton(n, {title : 'spellchecker.desc', cmd : 'mceSpellCheck', scope : t});
\r
129 c.onRenderMenu.add(function(c, m) {
\r
130 m.add({title : 'spellchecker.langs', 'class' : 'mceMenuItemTitle'}).setDisabled(1);
\r
131 each(t.languages, function(v, k) {
\r
132 var o = {icon : 1}, mi;
\r
134 o.onclick = function() {
\r
136 t.selectedItem.setSelected(0);
\r
137 t.selectedItem = mi;
\r
138 t.selectedLang = v;
\r
143 mi.setSelected(v == t.selectedLang);
\r
145 if (v == t.selectedLang)
\r
146 t.selectedItem = mi;
\r
154 // Internal functions
\r
156 _walk : function(n, f) {
\r
157 var d = this.editor.getDoc(), w;
\r
159 if (d.createTreeWalker) {
\r
160 w = d.createTreeWalker(n, NodeFilter.SHOW_TEXT, null, false);
\r
162 while ((n = w.nextNode()) != null)
\r
165 tinymce.walk(n, f, 'childNodes');
\r
168 _getSeparators : function() {
\r
169 var re = '', i, str = this.editor.getParam('spellchecker_word_separator_chars', '\\s!"#$%&()*+,-./:;<=>?@[\]^_{|}§©«®±¶·¸»¼½¾¿×÷¤\u201d\u201c');
\r
171 // Build word separator regexp
\r
172 for (i=0; i<str.length; i++)
\r
173 re += '\\' + str.charAt(i);
\r
178 _getWords : function() {
\r
179 var ed = this.editor, wl = [], tx = '', lo = {}, rawWords = [];
\r
182 this._walk(ed.getBody(), function(n) {
\r
183 if (n.nodeType == 3)
\r
184 tx += n.nodeValue + ' ';
\r
187 // split the text up into individual words
\r
188 if (ed.getParam('spellchecker_word_pattern')) {
\r
189 // look for words that match the pattern
\r
190 rawWords = tx.match('(' + ed.getParam('spellchecker_word_pattern') + ')', 'gi');
\r
192 // Split words by separator
\r
193 tx = tx.replace(new RegExp('([0-9]|[' + this._getSeparators() + '])', 'g'), ' ');
\r
194 tx = tinymce.trim(tx.replace(/(\s+)/g, ' '));
\r
195 rawWords = tx.split(' ');
\r
198 // Build word array and remove duplicates
\r
199 each(rawWords, function(v) {
\r
209 _removeWords : function(w) {
\r
210 var ed = this.editor, dom = ed.dom, se = ed.selection, b = se.getBookmark();
\r
212 each(dom.select('span').reverse(), function(n) {
\r
213 if (n && (dom.hasClass(n, 'mceItemHiddenSpellWord') || dom.hasClass(n, 'mceItemHidden'))) {
\r
214 if (!w || dom.decode(n.innerHTML) == w)
\r
219 se.moveToBookmark(b);
\r
222 _markWords : function(wl) {
\r
223 var r1, r2, r3, r4, r5, w = '', ed = this.editor, re = this._getSeparators(), dom = ed.dom, nl = [];
\r
224 var se = ed.selection, b = se.getBookmark();
\r
226 each(wl, function(v) {
\r
227 w += (w ? '|' : '') + v;
\r
230 r1 = new RegExp('([' + re + '])(' + w + ')([' + re + '])', 'g');
\r
231 r2 = new RegExp('^(' + w + ')', 'g');
\r
232 r3 = new RegExp('(' + w + ')([' + re + ']?)$', 'g');
\r
233 r4 = new RegExp('^(' + w + ')([' + re + ']?)$', 'g');
\r
234 r5 = new RegExp('(' + w + ')([' + re + '])', 'g');
\r
236 // Collect all text nodes
\r
237 this._walk(this.editor.getBody(), function(n) {
\r
238 if (n.nodeType == 3) {
\r
243 // Wrap incorrect words in spans
\r
244 each(nl, function(n) {
\r
247 if (n.nodeType == 3) {
\r
250 if (r1.test(v) || r2.test(v) || r3.test(v) || r4.test(v)) {
\r
252 v = v.replace(r5, '<span class="mceItemHiddenSpellWord">$1</span>$2');
\r
253 v = v.replace(r3, '<span class="mceItemHiddenSpellWord">$1</span>$2');
\r
255 dom.replace(dom.create('span', {'class' : 'mceItemHidden'}, v), n);
\r
260 se.moveToBookmark(b);
\r
263 _showMenu : function(ed, e) {
\r
264 var t = this, ed = t.editor, m = t._menu, p1, dom = ed.dom, vp = dom.getViewPort(ed.getWin()), wordSpan = e.target;
\r
266 e = 0; // Fixes IE memory leak
\r
269 p1 = DOM.getPos(ed.getContentAreaContainer());
\r
270 //p2 = DOM.getPos(ed.getContainer());
\r
272 m = ed.controlManager.createDropMenu('spellcheckermenu', {
\r
275 'class' : 'mceNoIcons'
\r
281 if (dom.hasClass(wordSpan, 'mceItemHiddenSpellWord')) {
\r
283 m.add({title : 'spellchecker.wait', 'class' : 'mceMenuItemTitle'}).setDisabled(1);
\r
285 t._sendRPC('getSuggestions', [t.selectedLang, dom.decode(wordSpan.innerHTML)], function(r) {
\r
290 if (r.length > 0) {
\r
291 m.add({title : 'spellchecker.sug', 'class' : 'mceMenuItemTitle'}).setDisabled(1);
\r
292 each(r, function(v) {
\r
293 m.add({title : v, onclick : function() {
\r
294 dom.replace(ed.getDoc().createTextNode(v), wordSpan);
\r
301 m.add({title : 'spellchecker.no_sug', 'class' : 'mceMenuItemTitle'}).setDisabled(1);
\r
303 ignoreRpc = t.editor.getParam("spellchecker_enable_ignore_rpc", '');
\r
305 title : 'spellchecker.ignore_word',
\r
306 onclick : function() {
\r
307 var word = wordSpan.innerHTML;
\r
309 dom.remove(wordSpan, 1);
\r
312 // tell the server if we need to
\r
314 ed.setProgressState(1);
\r
315 t._sendRPC('ignoreWord', [t.selectedLang, word], function(r) {
\r
316 ed.setProgressState(0);
\r
323 title : 'spellchecker.ignore_words',
\r
324 onclick : function() {
\r
325 var word = wordSpan.innerHTML;
\r
327 t._removeWords(dom.decode(word));
\r
330 // tell the server if we need to
\r
332 ed.setProgressState(1);
\r
333 t._sendRPC('ignoreWords', [t.selectedLang, word], function(r) {
\r
334 ed.setProgressState(0);
\r
341 if (t.editor.getParam("spellchecker_enable_learn_rpc")) {
\r
343 title : 'spellchecker.learn_word',
\r
344 onclick : function() {
\r
345 var word = wordSpan.innerHTML;
\r
347 dom.remove(wordSpan, 1);
\r
350 ed.setProgressState(1);
\r
351 t._sendRPC('learnWord', [t.selectedLang, word], function(r) {
\r
352 ed.setProgressState(0);
\r
361 ed.selection.select(wordSpan);
\r
362 p1 = dom.getPos(wordSpan);
\r
363 m.showMenu(p1.x, p1.y + wordSpan.offsetHeight - vp.y);
\r
365 return tinymce.dom.Event.cancel(e);
\r
370 _checkDone : function() {
\r
371 var t = this, ed = t.editor, dom = ed.dom, o;
\r
373 each(dom.select('span'), function(n) {
\r
374 if (n && dom.hasClass(n, 'mceItemHiddenSpellWord')) {
\r
384 _done : function() {
\r
385 var t = this, la = t.active;
\r
392 t._menu.hideMenu();
\r
395 t.editor.nodeChanged();
\r
399 _sendRPC : function(m, p, cb) {
\r
402 JSONRequest.sendRPC({
\r
407 error : function(e, x) {
\r
408 t.editor.setProgressState(0);
\r
409 t.editor.windowManager.alert(e.errstr || ('Error response: ' + x.responseText));
\r
416 tinymce.PluginManager.add('spellchecker', tinymce.plugins.SpellcheckerPlugin);
\r