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