2 * $Id: editor_plugin_src.js 425 2007-11-21 15:17:39Z spocke $
\r
5 * @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved.
\r
9 var JSONRequest = tinymce.util.JSONRequest, each = tinymce.each, DOM = tinymce.DOM;
\r
11 tinymce.create('tinymce.plugins.SpellcheckerPlugin', {
\r
12 getInfo : function() {
\r
14 longname : 'Spellchecker',
\r
15 author : 'Moxiecode Systems AB',
\r
16 authorurl : 'http://tinymce.moxiecode.com',
\r
17 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/spellchecker',
\r
18 version : tinymce.majorVersion + "." + tinymce.minorVersion
\r
22 init : function(ed, url) {
\r
28 // Register commands
\r
29 ed.addCommand('mceSpellCheck', function() {
\r
31 ed.setProgressState(1);
\r
32 t._sendRPC('checkWords', [t.selectedLang, t._getWords()], function(r) {
\r
36 ed.setProgressState(0);
\r
39 ed.setProgressState(0);
\r
40 ed.windowManager.alert('spellchecker.no_mpell');
\r
47 ed.onInit.add(function() {
\r
48 if (ed.settings.content_css !== false)
\r
49 ed.dom.loadCSS(url + '/css/content.css');
\r
52 ed.onClick.add(t._showMenu, t);
\r
53 ed.onContextMenu.add(t._showMenu, t);
\r
54 ed.onBeforeGetContent.add(function() {
\r
59 ed.onNodeChange.add(function(ed, cm) {
\r
60 cm.setActive('spellchecker', t.active);
\r
63 ed.onSetContent.add(function() {
\r
67 ed.onBeforeGetContent.add(function() {
\r
71 ed.onBeforeExecCommand.add(function(ed, cmd) {
\r
72 if (cmd == 'mceFullScreen')
\r
76 // Find selected language
\r
78 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
79 if (k.indexOf('+') === 0) {
\r
88 createControl : function(n, cm) {
\r
89 var t = this, c, ed = t.editor;
\r
91 if (n == 'spellchecker') {
\r
92 c = cm.createSplitButton(n, {title : 'spellchecker.desc', cmd : 'mceSpellCheck', scope : t});
\r
94 c.onRenderMenu.add(function(c, m) {
\r
95 m.add({title : 'spellchecker.langs', 'class' : 'mceMenuItemTitle'}).setDisabled(1);
\r
96 each(t.languages, function(v, k) {
\r
97 var o = {icon : 1}, mi;
\r
99 o.onclick = function() {
\r
101 t.selectedItem.setSelected(0);
\r
102 t.selectedItem = mi;
\r
103 t.selectedLang = v;
\r
108 mi.setSelected(v == t.selectedLang);
\r
110 if (v == t.selectedLang)
\r
111 t.selectedItem = mi;
\r
119 // Internal functions
\r
121 _walk : function(n, f) {
\r
122 var d = this.editor.getDoc(), w;
\r
124 if (d.createTreeWalker) {
\r
125 w = d.createTreeWalker(n, NodeFilter.SHOW_TEXT, null, false);
\r
127 while ((n = w.nextNode()) != null)
\r
130 tinymce.walk(n, f, 'childNodes');
\r
133 _getSeparators : function() {
\r
134 var re = '', i, str = this.editor.getParam('spellchecker_word_separator_chars', '\\s!"#$%&()*+,-./:;<=>?@[\]^_{|}§©«®±¶·¸»¼½¾¿×÷¤\u201d\u201c');
\r
136 // Build word separator regexp
\r
137 for (i=0; i<str.length; i++)
\r
138 re += '\\' + str.charAt(i);
\r
143 _getWords : function() {
\r
144 var ed = this.editor, wl = [], tx = '', lo = {};
\r
147 this._walk(ed.getBody(), function(n) {
\r
148 if (n.nodeType == 3)
\r
149 tx += n.nodeValue + ' ';
\r
152 // Split words by separator
\r
153 tx = tx.replace(new RegExp('([0-9]|[' + this._getSeparators() + '])', 'g'), ' ');
\r
154 tx = tinymce.trim(tx.replace(/(\s+)/g, ' '));
\r
156 // Build word array and remove duplicates
\r
157 each(tx.split(' '), function(v) {
\r
167 _removeWords : function(w) {
\r
168 var ed = this.editor, dom = ed.dom, se = ed.selection, b = se.getBookmark();
\r
170 each(dom.select('span').reverse(), function(n) {
\r
171 if (n && (dom.hasClass(n, 'mceItemHiddenSpellWord') || dom.hasClass(n, 'mceItemHidden'))) {
\r
172 if (!w || dom.decode(n.innerHTML) == w)
\r
177 se.moveToBookmark(b);
\r
180 _markWords : function(wl) {
\r
181 var r1, r2, r3, r4, r5, w = '', ed = this.editor, re = this._getSeparators(), dom = ed.dom, nl = [];
\r
182 var se = ed.selection, b = se.getBookmark();
\r
184 each(wl, function(v) {
\r
185 w += (w ? '|' : '') + v;
\r
188 r1 = new RegExp('([' + re + '])(' + w + ')([' + re + '])', 'g');
\r
189 r2 = new RegExp('^(' + w + ')', 'g');
\r
190 r3 = new RegExp('(' + w + ')([' + re + ']?)$', 'g');
\r
191 r4 = new RegExp('^(' + w + ')([' + re + ']?)$', 'g');
\r
192 r5 = new RegExp('(' + w + ')([' + re + '])', 'g');
\r
194 // Collect all text nodes
\r
195 this._walk(this.editor.getBody(), function(n) {
\r
196 if (n.nodeType == 3) {
\r
201 // Wrap incorrect words in spans
\r
202 each(nl, function(n) {
\r
205 if (n.nodeType == 3) {
\r
208 if (r1.test(v) || r2.test(v) || r3.test(v) || r4.test(v)) {
\r
210 v = v.replace(r5, '<span class="mceItemHiddenSpellWord">$1</span>$2');
\r
211 v = v.replace(r3, '<span class="mceItemHiddenSpellWord">$1</span>$2');
\r
213 dom.replace(dom.create('span', {'class' : 'mceItemHidden'}, v), n);
\r
218 se.moveToBookmark(b);
\r
221 _showMenu : function(ed, e) {
\r
222 var t = this, ed = t.editor, m = t._menu, p1, dom = ed.dom, vp = dom.getViewPort(ed.getWin());
\r
225 p1 = DOM.getPos(ed.getContentAreaContainer());
\r
226 //p2 = DOM.getPos(ed.getContainer());
\r
228 m = ed.controlManager.createDropMenu('spellcheckermenu', {
\r
231 'class' : 'mceNoIcons'
\r
237 if (dom.hasClass(e.target, 'mceItemHiddenSpellWord')) {
\r
239 m.add({title : 'spellchecker.wait', 'class' : 'mceMenuItemTitle'}).setDisabled(1);
\r
241 t._sendRPC('getSuggestions', [t.selectedLang, dom.decode(e.target.innerHTML)], function(r) {
\r
244 if (r.length > 0) {
\r
245 m.add({title : 'spellchecker.sug', 'class' : 'mceMenuItemTitle'}).setDisabled(1);
\r
246 each(r, function(v) {
\r
247 m.add({title : v, onclick : function() {
\r
248 dom.replace(ed.getDoc().createTextNode(v), e.target);
\r
255 m.add({title : 'spellchecker.no_sug', 'class' : 'mceMenuItemTitle'}).setDisabled(1);
\r
258 title : 'spellchecker.ignore_word',
\r
259 onclick : function() {
\r
260 dom.remove(e.target, 1);
\r
266 title : 'spellchecker.ignore_words',
\r
267 onclick : function() {
\r
268 t._removeWords(dom.decode(e.target.innerHTML));
\r
276 ed.selection.select(e.target);
\r
277 p1 = dom.getPos(e.target);
\r
278 m.showMenu(p1.x, p1.y + e.target.offsetHeight - vp.y);
\r
280 return tinymce.dom.Event.cancel(e);
\r
285 _checkDone : function() {
\r
286 var t = this, ed = t.editor, dom = ed.dom, o;
\r
288 each(dom.select('span'), function(n) {
\r
289 if (n && dom.hasClass(n, 'mceItemHiddenSpellWord')) {
\r
299 _done : function() {
\r
300 var t = this, la = t.active;
\r
307 t._menu.hideMenu();
\r
310 t.editor.nodeChanged();
\r
314 _sendRPC : function(m, p, cb) {
\r
315 var t = this, url = t.editor.getParam("spellchecker_rpc_url", "{backend}");
\r
317 if (url == '{backend}') {
\r
318 t.editor.setProgressState(0);
\r
319 alert('Please specify: spellchecker_rpc_url');
\r
323 JSONRequest.sendRPC({
\r
328 error : function(e, x) {
\r
329 t.editor.setProgressState(0);
\r
330 t.editor.windowManager.alert(e.errstr || ('Error response: ' + x.responseText));
\r
337 tinymce.PluginManager.add('spellchecker', tinymce.plugins.SpellcheckerPlugin);
\r