bgneal@312: /** bgneal@312: * editor_plugin_src.js bgneal@312: * bgneal@312: * Copyright 2009, Moxiecode Systems AB bgneal@312: * Released under LGPL License. bgneal@312: * bgneal@312: * License: http://tinymce.moxiecode.com/license bgneal@312: * Contributing: http://tinymce.moxiecode.com/contributing bgneal@312: */ bgneal@312: bgneal@312: (function() { bgneal@312: var JSONRequest = tinymce.util.JSONRequest, each = tinymce.each, DOM = tinymce.DOM; bgneal@312: bgneal@312: tinymce.create('tinymce.plugins.SpellcheckerPlugin', { bgneal@312: getInfo : function() { bgneal@312: return { bgneal@312: longname : 'Spellchecker', bgneal@312: author : 'Moxiecode Systems AB', bgneal@312: authorurl : 'http://tinymce.moxiecode.com', bgneal@312: infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/spellchecker', bgneal@312: version : tinymce.majorVersion + "." + tinymce.minorVersion bgneal@312: }; bgneal@312: }, bgneal@312: bgneal@312: init : function(ed, url) { bgneal@312: var t = this, cm; bgneal@312: bgneal@312: t.url = url; bgneal@312: t.editor = ed; bgneal@312: t.rpcUrl = ed.getParam("spellchecker_rpc_url", "{backend}"); bgneal@312: bgneal@312: if (t.rpcUrl == '{backend}') { bgneal@312: // Sniff if the browser supports native spellchecking (Don't know of a better way) bgneal@312: if (tinymce.isIE) bgneal@312: return; bgneal@312: bgneal@312: t.hasSupport = true; bgneal@312: bgneal@312: // Disable the context menu when spellchecking is active bgneal@312: ed.onContextMenu.addToTop(function(ed, e) { bgneal@312: if (t.active) bgneal@312: return false; bgneal@312: }); bgneal@312: } bgneal@312: bgneal@312: // Register commands bgneal@312: ed.addCommand('mceSpellCheck', function() { bgneal@312: if (t.rpcUrl == '{backend}') { bgneal@312: // Enable/disable native spellchecker bgneal@312: t.editor.getBody().spellcheck = t.active = !t.active; bgneal@312: return; bgneal@312: } bgneal@312: bgneal@312: if (!t.active) { bgneal@312: ed.setProgressState(1); bgneal@312: t._sendRPC('checkWords', [t.selectedLang, t._getWords()], function(r) { bgneal@312: if (r.length > 0) { bgneal@312: t.active = 1; bgneal@312: t._markWords(r); bgneal@312: ed.setProgressState(0); bgneal@312: ed.nodeChanged(); bgneal@312: } else { bgneal@312: ed.setProgressState(0); bgneal@312: bgneal@312: if (ed.getParam('spellchecker_report_no_misspellings', true)) bgneal@312: ed.windowManager.alert('spellchecker.no_mpell'); bgneal@312: } bgneal@312: }); bgneal@312: } else bgneal@312: t._done(); bgneal@312: }); bgneal@312: bgneal@312: ed.onInit.add(function() { bgneal@312: if (ed.settings.content_css !== false) bgneal@312: ed.dom.loadCSS(url + '/css/content.css'); bgneal@312: }); bgneal@312: bgneal@312: ed.onClick.add(t._showMenu, t); bgneal@312: ed.onContextMenu.add(t._showMenu, t); bgneal@312: ed.onBeforeGetContent.add(function() { bgneal@312: if (t.active) bgneal@312: t._removeWords(); bgneal@312: }); bgneal@312: bgneal@312: ed.onNodeChange.add(function(ed, cm) { bgneal@312: cm.setActive('spellchecker', t.active); bgneal@312: }); bgneal@312: bgneal@312: ed.onSetContent.add(function() { bgneal@312: t._done(); bgneal@312: }); bgneal@312: bgneal@312: ed.onBeforeGetContent.add(function() { bgneal@312: t._done(); bgneal@312: }); bgneal@312: bgneal@312: ed.onBeforeExecCommand.add(function(ed, cmd) { bgneal@312: if (cmd == 'mceFullScreen') bgneal@312: t._done(); bgneal@312: }); bgneal@312: bgneal@312: // Find selected language bgneal@312: t.languages = {}; bgneal@312: 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) { bgneal@312: if (k.indexOf('+') === 0) { bgneal@312: k = k.substring(1); bgneal@312: t.selectedLang = v; bgneal@312: } bgneal@312: bgneal@312: t.languages[k] = v; bgneal@312: }); bgneal@312: }, bgneal@312: bgneal@312: createControl : function(n, cm) { bgneal@312: var t = this, c, ed = t.editor; bgneal@312: bgneal@312: if (n == 'spellchecker') { bgneal@312: // Use basic button if we use the native spellchecker bgneal@312: if (t.rpcUrl == '{backend}') { bgneal@312: // Create simple toggle button if we have native support bgneal@312: if (t.hasSupport) bgneal@312: c = cm.createButton(n, {title : 'spellchecker.desc', cmd : 'mceSpellCheck', scope : t}); bgneal@312: bgneal@312: return c; bgneal@312: } bgneal@312: bgneal@312: c = cm.createSplitButton(n, {title : 'spellchecker.desc', cmd : 'mceSpellCheck', scope : t}); bgneal@312: bgneal@312: c.onRenderMenu.add(function(c, m) { bgneal@312: m.add({title : 'spellchecker.langs', 'class' : 'mceMenuItemTitle'}).setDisabled(1); bgneal@312: each(t.languages, function(v, k) { bgneal@312: var o = {icon : 1}, mi; bgneal@312: bgneal@312: o.onclick = function() { bgneal@312: mi.setSelected(1); bgneal@312: t.selectedItem.setSelected(0); bgneal@312: t.selectedItem = mi; bgneal@312: t.selectedLang = v; bgneal@312: }; bgneal@312: bgneal@312: o.title = k; bgneal@312: mi = m.add(o); bgneal@312: mi.setSelected(v == t.selectedLang); bgneal@312: bgneal@312: if (v == t.selectedLang) bgneal@312: t.selectedItem = mi; bgneal@312: }) bgneal@312: }); bgneal@312: bgneal@312: return c; bgneal@312: } bgneal@312: }, bgneal@312: bgneal@312: // Internal functions bgneal@312: bgneal@312: _walk : function(n, f) { bgneal@312: var d = this.editor.getDoc(), w; bgneal@312: bgneal@312: if (d.createTreeWalker) { bgneal@312: w = d.createTreeWalker(n, NodeFilter.SHOW_TEXT, null, false); bgneal@312: bgneal@312: while ((n = w.nextNode()) != null) bgneal@312: f.call(this, n); bgneal@312: } else bgneal@312: tinymce.walk(n, f, 'childNodes'); bgneal@312: }, bgneal@312: bgneal@312: _getSeparators : function() { bgneal@312: var re = '', i, str = this.editor.getParam('spellchecker_word_separator_chars', '\\s!"#$%&()*+,-./:;<=>?@[\]^_{|}§©«®±¶·¸»¼½¾¿×÷¤\u201d\u201c'); bgneal@312: bgneal@312: // Build word separator regexp bgneal@312: for (i=0; i$1$2'); bgneal@312: v = v.replace(r3, '$1$2'); bgneal@312: bgneal@312: dom.replace(dom.create('span', {'class' : 'mceItemHidden'}, v), n); bgneal@312: } bgneal@312: } bgneal@312: }); bgneal@312: bgneal@312: se.moveToBookmark(b); bgneal@312: }, bgneal@312: bgneal@312: _showMenu : function(ed, e) { bgneal@312: var t = this, ed = t.editor, m = t._menu, p1, dom = ed.dom, vp = dom.getViewPort(ed.getWin()), wordSpan = e.target; bgneal@312: bgneal@312: e = 0; // Fixes IE memory leak bgneal@312: bgneal@312: if (!m) { bgneal@312: p1 = DOM.getPos(ed.getContentAreaContainer()); bgneal@312: //p2 = DOM.getPos(ed.getContainer()); bgneal@312: bgneal@312: m = ed.controlManager.createDropMenu('spellcheckermenu', { bgneal@312: offset_x : p1.x, bgneal@312: offset_y : p1.y, bgneal@312: 'class' : 'mceNoIcons' bgneal@312: }); bgneal@312: bgneal@312: t._menu = m; bgneal@312: } bgneal@312: bgneal@312: if (dom.hasClass(wordSpan, 'mceItemHiddenSpellWord')) { bgneal@312: m.removeAll(); bgneal@312: m.add({title : 'spellchecker.wait', 'class' : 'mceMenuItemTitle'}).setDisabled(1); bgneal@312: bgneal@312: t._sendRPC('getSuggestions', [t.selectedLang, dom.decode(wordSpan.innerHTML)], function(r) { bgneal@312: var ignoreRpc; bgneal@312: bgneal@312: m.removeAll(); bgneal@312: bgneal@312: if (r.length > 0) { bgneal@312: m.add({title : 'spellchecker.sug', 'class' : 'mceMenuItemTitle'}).setDisabled(1); bgneal@312: each(r, function(v) { bgneal@312: m.add({title : v, onclick : function() { bgneal@312: dom.replace(ed.getDoc().createTextNode(v), wordSpan); bgneal@312: t._checkDone(); bgneal@312: }}); bgneal@312: }); bgneal@312: bgneal@312: m.addSeparator(); bgneal@312: } else bgneal@312: m.add({title : 'spellchecker.no_sug', 'class' : 'mceMenuItemTitle'}).setDisabled(1); bgneal@312: bgneal@312: ignoreRpc = t.editor.getParam("spellchecker_enable_ignore_rpc", ''); bgneal@312: m.add({ bgneal@312: title : 'spellchecker.ignore_word', bgneal@312: onclick : function() { bgneal@312: var word = wordSpan.innerHTML; bgneal@312: bgneal@312: dom.remove(wordSpan, 1); bgneal@312: t._checkDone(); bgneal@312: bgneal@312: // tell the server if we need to bgneal@312: if (ignoreRpc) { bgneal@312: ed.setProgressState(1); bgneal@312: t._sendRPC('ignoreWord', [t.selectedLang, word], function(r) { bgneal@312: ed.setProgressState(0); bgneal@312: }); bgneal@312: } bgneal@312: } bgneal@312: }); bgneal@312: bgneal@312: m.add({ bgneal@312: title : 'spellchecker.ignore_words', bgneal@312: onclick : function() { bgneal@312: var word = wordSpan.innerHTML; bgneal@312: bgneal@312: t._removeWords(dom.decode(word)); bgneal@312: t._checkDone(); bgneal@312: bgneal@312: // tell the server if we need to bgneal@312: if (ignoreRpc) { bgneal@312: ed.setProgressState(1); bgneal@312: t._sendRPC('ignoreWords', [t.selectedLang, word], function(r) { bgneal@312: ed.setProgressState(0); bgneal@312: }); bgneal@312: } bgneal@312: } bgneal@312: }); bgneal@312: bgneal@312: bgneal@312: if (t.editor.getParam("spellchecker_enable_learn_rpc")) { bgneal@312: m.add({ bgneal@312: title : 'spellchecker.learn_word', bgneal@312: onclick : function() { bgneal@312: var word = wordSpan.innerHTML; bgneal@312: bgneal@312: dom.remove(wordSpan, 1); bgneal@312: t._checkDone(); bgneal@312: bgneal@312: ed.setProgressState(1); bgneal@312: t._sendRPC('learnWord', [t.selectedLang, word], function(r) { bgneal@312: ed.setProgressState(0); bgneal@312: }); bgneal@312: } bgneal@312: }); bgneal@312: } bgneal@312: bgneal@312: m.update(); bgneal@312: }); bgneal@312: bgneal@312: ed.selection.select(wordSpan); bgneal@312: p1 = dom.getPos(wordSpan); bgneal@312: m.showMenu(p1.x, p1.y + wordSpan.offsetHeight - vp.y); bgneal@312: bgneal@312: return tinymce.dom.Event.cancel(e); bgneal@312: } else bgneal@312: m.hideMenu(); bgneal@312: }, bgneal@312: bgneal@312: _checkDone : function() { bgneal@312: var t = this, ed = t.editor, dom = ed.dom, o; bgneal@312: bgneal@312: each(dom.select('span'), function(n) { bgneal@312: if (n && dom.hasClass(n, 'mceItemHiddenSpellWord')) { bgneal@312: o = true; bgneal@312: return false; bgneal@312: } bgneal@312: }); bgneal@312: bgneal@312: if (!o) bgneal@312: t._done(); bgneal@312: }, bgneal@312: bgneal@312: _done : function() { bgneal@312: var t = this, la = t.active; bgneal@312: bgneal@312: if (t.active) { bgneal@312: t.active = 0; bgneal@312: t._removeWords(); bgneal@312: bgneal@312: if (t._menu) bgneal@312: t._menu.hideMenu(); bgneal@312: bgneal@312: if (la) bgneal@312: t.editor.nodeChanged(); bgneal@312: } bgneal@312: }, bgneal@312: bgneal@312: _sendRPC : function(m, p, cb) { bgneal@312: var t = this; bgneal@312: bgneal@312: JSONRequest.sendRPC({ bgneal@312: url : t.rpcUrl, bgneal@312: method : m, bgneal@312: params : p, bgneal@312: success : cb, bgneal@312: error : function(e, x) { bgneal@312: t.editor.setProgressState(0); bgneal@312: t.editor.windowManager.alert(e.errstr || ('Error response: ' + x.responseText)); bgneal@312: } bgneal@312: }); bgneal@312: } bgneal@312: }); bgneal@312: bgneal@312: // Register plugin bgneal@312: tinymce.PluginManager.add('spellchecker', tinymce.plugins.SpellcheckerPlugin); bgneal@312: })();