bgneal@45
|
1 /**
|
bgneal@183
|
2 * editor_plugin_src.js
|
bgneal@45
|
3 *
|
bgneal@183
|
4 * Copyright 2009, Moxiecode Systems AB
|
bgneal@183
|
5 * Released under LGPL License.
|
bgneal@183
|
6 *
|
bgneal@183
|
7 * License: http://tinymce.moxiecode.com/license
|
bgneal@183
|
8 * Contributing: http://tinymce.moxiecode.com/contributing
|
bgneal@45
|
9 */
|
bgneal@45
|
10
|
bgneal@45
|
11 (function() {
|
bgneal@45
|
12 var JSONRequest = tinymce.util.JSONRequest, each = tinymce.each, DOM = tinymce.DOM;
|
bgneal@45
|
13
|
bgneal@45
|
14 tinymce.create('tinymce.plugins.SpellcheckerPlugin', {
|
bgneal@45
|
15 getInfo : function() {
|
bgneal@45
|
16 return {
|
bgneal@45
|
17 longname : 'Spellchecker',
|
bgneal@45
|
18 author : 'Moxiecode Systems AB',
|
bgneal@45
|
19 authorurl : 'http://tinymce.moxiecode.com',
|
bgneal@45
|
20 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/spellchecker',
|
bgneal@45
|
21 version : tinymce.majorVersion + "." + tinymce.minorVersion
|
bgneal@45
|
22 };
|
bgneal@45
|
23 },
|
bgneal@45
|
24
|
bgneal@45
|
25 init : function(ed, url) {
|
bgneal@45
|
26 var t = this, cm;
|
bgneal@45
|
27
|
bgneal@45
|
28 t.url = url;
|
bgneal@45
|
29 t.editor = ed;
|
bgneal@217
|
30 t.rpcUrl = ed.getParam("spellchecker_rpc_url", "{backend}");
|
bgneal@217
|
31
|
bgneal@217
|
32 if (t.rpcUrl == '{backend}') {
|
bgneal@217
|
33 // Sniff if the browser supports native spellchecking (Don't know of a better way)
|
bgneal@217
|
34 if (tinymce.isIE)
|
bgneal@217
|
35 return;
|
bgneal@217
|
36
|
bgneal@217
|
37 t.hasSupport = true;
|
bgneal@217
|
38
|
bgneal@217
|
39 // Disable the context menu when spellchecking is active
|
bgneal@217
|
40 ed.onContextMenu.addToTop(function(ed, e) {
|
bgneal@217
|
41 if (t.active)
|
bgneal@217
|
42 return false;
|
bgneal@217
|
43 });
|
bgneal@217
|
44 }
|
bgneal@45
|
45
|
bgneal@45
|
46 // Register commands
|
bgneal@45
|
47 ed.addCommand('mceSpellCheck', function() {
|
bgneal@217
|
48 if (t.rpcUrl == '{backend}') {
|
bgneal@217
|
49 // Enable/disable native spellchecker
|
bgneal@217
|
50 t.editor.getBody().spellcheck = t.active = !t.active;
|
bgneal@217
|
51 return;
|
bgneal@217
|
52 }
|
bgneal@217
|
53
|
bgneal@45
|
54 if (!t.active) {
|
bgneal@45
|
55 ed.setProgressState(1);
|
bgneal@45
|
56 t._sendRPC('checkWords', [t.selectedLang, t._getWords()], function(r) {
|
bgneal@45
|
57 if (r.length > 0) {
|
bgneal@45
|
58 t.active = 1;
|
bgneal@45
|
59 t._markWords(r);
|
bgneal@45
|
60 ed.setProgressState(0);
|
bgneal@45
|
61 ed.nodeChanged();
|
bgneal@45
|
62 } else {
|
bgneal@45
|
63 ed.setProgressState(0);
|
bgneal@217
|
64
|
bgneal@217
|
65 if (ed.getParam('spellchecker_report_no_misspellings', true))
|
bgneal@217
|
66 ed.windowManager.alert('spellchecker.no_mpell');
|
bgneal@45
|
67 }
|
bgneal@45
|
68 });
|
bgneal@45
|
69 } else
|
bgneal@45
|
70 t._done();
|
bgneal@45
|
71 });
|
bgneal@45
|
72
|
bgneal@45
|
73 ed.onInit.add(function() {
|
bgneal@45
|
74 if (ed.settings.content_css !== false)
|
bgneal@45
|
75 ed.dom.loadCSS(url + '/css/content.css');
|
bgneal@45
|
76 });
|
bgneal@45
|
77
|
bgneal@45
|
78 ed.onClick.add(t._showMenu, t);
|
bgneal@45
|
79 ed.onContextMenu.add(t._showMenu, t);
|
bgneal@45
|
80 ed.onBeforeGetContent.add(function() {
|
bgneal@45
|
81 if (t.active)
|
bgneal@45
|
82 t._removeWords();
|
bgneal@45
|
83 });
|
bgneal@45
|
84
|
bgneal@45
|
85 ed.onNodeChange.add(function(ed, cm) {
|
bgneal@45
|
86 cm.setActive('spellchecker', t.active);
|
bgneal@45
|
87 });
|
bgneal@45
|
88
|
bgneal@45
|
89 ed.onSetContent.add(function() {
|
bgneal@45
|
90 t._done();
|
bgneal@45
|
91 });
|
bgneal@45
|
92
|
bgneal@45
|
93 ed.onBeforeGetContent.add(function() {
|
bgneal@45
|
94 t._done();
|
bgneal@45
|
95 });
|
bgneal@45
|
96
|
bgneal@45
|
97 ed.onBeforeExecCommand.add(function(ed, cmd) {
|
bgneal@45
|
98 if (cmd == 'mceFullScreen')
|
bgneal@45
|
99 t._done();
|
bgneal@45
|
100 });
|
bgneal@45
|
101
|
bgneal@45
|
102 // Find selected language
|
bgneal@45
|
103 t.languages = {};
|
bgneal@45
|
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) {
|
bgneal@45
|
105 if (k.indexOf('+') === 0) {
|
bgneal@45
|
106 k = k.substring(1);
|
bgneal@45
|
107 t.selectedLang = v;
|
bgneal@45
|
108 }
|
bgneal@45
|
109
|
bgneal@45
|
110 t.languages[k] = v;
|
bgneal@45
|
111 });
|
bgneal@45
|
112 },
|
bgneal@45
|
113
|
bgneal@45
|
114 createControl : function(n, cm) {
|
bgneal@45
|
115 var t = this, c, ed = t.editor;
|
bgneal@45
|
116
|
bgneal@45
|
117 if (n == 'spellchecker') {
|
bgneal@217
|
118 // Use basic button if we use the native spellchecker
|
bgneal@217
|
119 if (t.rpcUrl == '{backend}') {
|
bgneal@217
|
120 // Create simple toggle button if we have native support
|
bgneal@217
|
121 if (t.hasSupport)
|
bgneal@217
|
122 c = cm.createButton(n, {title : 'spellchecker.desc', cmd : 'mceSpellCheck', scope : t});
|
bgneal@217
|
123
|
bgneal@217
|
124 return c;
|
bgneal@217
|
125 }
|
bgneal@217
|
126
|
bgneal@45
|
127 c = cm.createSplitButton(n, {title : 'spellchecker.desc', cmd : 'mceSpellCheck', scope : t});
|
bgneal@45
|
128
|
bgneal@45
|
129 c.onRenderMenu.add(function(c, m) {
|
bgneal@45
|
130 m.add({title : 'spellchecker.langs', 'class' : 'mceMenuItemTitle'}).setDisabled(1);
|
bgneal@45
|
131 each(t.languages, function(v, k) {
|
bgneal@45
|
132 var o = {icon : 1}, mi;
|
bgneal@45
|
133
|
bgneal@45
|
134 o.onclick = function() {
|
bgneal@45
|
135 mi.setSelected(1);
|
bgneal@45
|
136 t.selectedItem.setSelected(0);
|
bgneal@45
|
137 t.selectedItem = mi;
|
bgneal@45
|
138 t.selectedLang = v;
|
bgneal@45
|
139 };
|
bgneal@45
|
140
|
bgneal@45
|
141 o.title = k;
|
bgneal@45
|
142 mi = m.add(o);
|
bgneal@45
|
143 mi.setSelected(v == t.selectedLang);
|
bgneal@45
|
144
|
bgneal@45
|
145 if (v == t.selectedLang)
|
bgneal@45
|
146 t.selectedItem = mi;
|
bgneal@45
|
147 })
|
bgneal@45
|
148 });
|
bgneal@45
|
149
|
bgneal@45
|
150 return c;
|
bgneal@45
|
151 }
|
bgneal@45
|
152 },
|
bgneal@45
|
153
|
bgneal@45
|
154 // Internal functions
|
bgneal@45
|
155
|
bgneal@45
|
156 _walk : function(n, f) {
|
bgneal@45
|
157 var d = this.editor.getDoc(), w;
|
bgneal@45
|
158
|
bgneal@45
|
159 if (d.createTreeWalker) {
|
bgneal@45
|
160 w = d.createTreeWalker(n, NodeFilter.SHOW_TEXT, null, false);
|
bgneal@45
|
161
|
bgneal@45
|
162 while ((n = w.nextNode()) != null)
|
bgneal@45
|
163 f.call(this, n);
|
bgneal@45
|
164 } else
|
bgneal@45
|
165 tinymce.walk(n, f, 'childNodes');
|
bgneal@45
|
166 },
|
bgneal@45
|
167
|
bgneal@45
|
168 _getSeparators : function() {
|
bgneal@45
|
169 var re = '', i, str = this.editor.getParam('spellchecker_word_separator_chars', '\\s!"#$%&()*+,-./:;<=>?@[\]^_{|}§©«®±¶·¸»¼½¾¿×÷¤\u201d\u201c');
|
bgneal@45
|
170
|
bgneal@45
|
171 // Build word separator regexp
|
bgneal@45
|
172 for (i=0; i<str.length; i++)
|
bgneal@45
|
173 re += '\\' + str.charAt(i);
|
bgneal@45
|
174
|
bgneal@45
|
175 return re;
|
bgneal@45
|
176 },
|
bgneal@45
|
177
|
bgneal@45
|
178 _getWords : function() {
|
bgneal@217
|
179 var ed = this.editor, wl = [], tx = '', lo = {}, rawWords = [];
|
bgneal@45
|
180
|
bgneal@45
|
181 // Get area text
|
bgneal@45
|
182 this._walk(ed.getBody(), function(n) {
|
bgneal@45
|
183 if (n.nodeType == 3)
|
bgneal@45
|
184 tx += n.nodeValue + ' ';
|
bgneal@45
|
185 });
|
bgneal@45
|
186
|
bgneal@217
|
187 // split the text up into individual words
|
bgneal@217
|
188 if (ed.getParam('spellchecker_word_pattern')) {
|
bgneal@217
|
189 // look for words that match the pattern
|
bgneal@217
|
190 rawWords = tx.match('(' + ed.getParam('spellchecker_word_pattern') + ')', 'gi');
|
bgneal@217
|
191 } else {
|
bgneal@217
|
192 // Split words by separator
|
bgneal@217
|
193 tx = tx.replace(new RegExp('([0-9]|[' + this._getSeparators() + '])', 'g'), ' ');
|
bgneal@217
|
194 tx = tinymce.trim(tx.replace(/(\s+)/g, ' '));
|
bgneal@217
|
195 rawWords = tx.split(' ');
|
bgneal@217
|
196 }
|
bgneal@45
|
197
|
bgneal@45
|
198 // Build word array and remove duplicates
|
bgneal@217
|
199 each(rawWords, function(v) {
|
bgneal@45
|
200 if (!lo[v]) {
|
bgneal@45
|
201 wl.push(v);
|
bgneal@45
|
202 lo[v] = 1;
|
bgneal@45
|
203 }
|
bgneal@45
|
204 });
|
bgneal@45
|
205
|
bgneal@45
|
206 return wl;
|
bgneal@45
|
207 },
|
bgneal@45
|
208
|
bgneal@45
|
209 _removeWords : function(w) {
|
bgneal@45
|
210 var ed = this.editor, dom = ed.dom, se = ed.selection, b = se.getBookmark();
|
bgneal@45
|
211
|
bgneal@45
|
212 each(dom.select('span').reverse(), function(n) {
|
bgneal@45
|
213 if (n && (dom.hasClass(n, 'mceItemHiddenSpellWord') || dom.hasClass(n, 'mceItemHidden'))) {
|
bgneal@45
|
214 if (!w || dom.decode(n.innerHTML) == w)
|
bgneal@45
|
215 dom.remove(n, 1);
|
bgneal@45
|
216 }
|
bgneal@45
|
217 });
|
bgneal@45
|
218
|
bgneal@45
|
219 se.moveToBookmark(b);
|
bgneal@45
|
220 },
|
bgneal@45
|
221
|
bgneal@45
|
222 _markWords : function(wl) {
|
bgneal@45
|
223 var r1, r2, r3, r4, r5, w = '', ed = this.editor, re = this._getSeparators(), dom = ed.dom, nl = [];
|
bgneal@45
|
224 var se = ed.selection, b = se.getBookmark();
|
bgneal@45
|
225
|
bgneal@45
|
226 each(wl, function(v) {
|
bgneal@45
|
227 w += (w ? '|' : '') + v;
|
bgneal@45
|
228 });
|
bgneal@45
|
229
|
bgneal@45
|
230 r1 = new RegExp('([' + re + '])(' + w + ')([' + re + '])', 'g');
|
bgneal@45
|
231 r2 = new RegExp('^(' + w + ')', 'g');
|
bgneal@45
|
232 r3 = new RegExp('(' + w + ')([' + re + ']?)$', 'g');
|
bgneal@45
|
233 r4 = new RegExp('^(' + w + ')([' + re + ']?)$', 'g');
|
bgneal@45
|
234 r5 = new RegExp('(' + w + ')([' + re + '])', 'g');
|
bgneal@45
|
235
|
bgneal@45
|
236 // Collect all text nodes
|
bgneal@45
|
237 this._walk(this.editor.getBody(), function(n) {
|
bgneal@45
|
238 if (n.nodeType == 3) {
|
bgneal@45
|
239 nl.push(n);
|
bgneal@45
|
240 }
|
bgneal@45
|
241 });
|
bgneal@45
|
242
|
bgneal@45
|
243 // Wrap incorrect words in spans
|
bgneal@45
|
244 each(nl, function(n) {
|
bgneal@45
|
245 var v;
|
bgneal@45
|
246
|
bgneal@45
|
247 if (n.nodeType == 3) {
|
bgneal@45
|
248 v = n.nodeValue;
|
bgneal@45
|
249
|
bgneal@45
|
250 if (r1.test(v) || r2.test(v) || r3.test(v) || r4.test(v)) {
|
bgneal@45
|
251 v = dom.encode(v);
|
bgneal@45
|
252 v = v.replace(r5, '<span class="mceItemHiddenSpellWord">$1</span>$2');
|
bgneal@45
|
253 v = v.replace(r3, '<span class="mceItemHiddenSpellWord">$1</span>$2');
|
bgneal@45
|
254
|
bgneal@45
|
255 dom.replace(dom.create('span', {'class' : 'mceItemHidden'}, v), n);
|
bgneal@45
|
256 }
|
bgneal@45
|
257 }
|
bgneal@45
|
258 });
|
bgneal@45
|
259
|
bgneal@45
|
260 se.moveToBookmark(b);
|
bgneal@45
|
261 },
|
bgneal@45
|
262
|
bgneal@45
|
263 _showMenu : function(ed, e) {
|
bgneal@247
|
264 var t = this, ed = t.editor, m = t._menu, p1, dom = ed.dom, vp = dom.getViewPort(ed.getWin()), wordSpan = e.target;
|
bgneal@247
|
265
|
bgneal@247
|
266 e = 0; // Fixes IE memory leak
|
bgneal@45
|
267
|
bgneal@45
|
268 if (!m) {
|
bgneal@45
|
269 p1 = DOM.getPos(ed.getContentAreaContainer());
|
bgneal@45
|
270 //p2 = DOM.getPos(ed.getContainer());
|
bgneal@45
|
271
|
bgneal@45
|
272 m = ed.controlManager.createDropMenu('spellcheckermenu', {
|
bgneal@45
|
273 offset_x : p1.x,
|
bgneal@45
|
274 offset_y : p1.y,
|
bgneal@45
|
275 'class' : 'mceNoIcons'
|
bgneal@45
|
276 });
|
bgneal@45
|
277
|
bgneal@45
|
278 t._menu = m;
|
bgneal@45
|
279 }
|
bgneal@45
|
280
|
bgneal@247
|
281 if (dom.hasClass(wordSpan, 'mceItemHiddenSpellWord')) {
|
bgneal@45
|
282 m.removeAll();
|
bgneal@45
|
283 m.add({title : 'spellchecker.wait', 'class' : 'mceMenuItemTitle'}).setDisabled(1);
|
bgneal@45
|
284
|
bgneal@247
|
285 t._sendRPC('getSuggestions', [t.selectedLang, dom.decode(wordSpan.innerHTML)], function(r) {
|
bgneal@217
|
286 var ignoreRpc;
|
bgneal@217
|
287
|
bgneal@45
|
288 m.removeAll();
|
bgneal@45
|
289
|
bgneal@45
|
290 if (r.length > 0) {
|
bgneal@45
|
291 m.add({title : 'spellchecker.sug', 'class' : 'mceMenuItemTitle'}).setDisabled(1);
|
bgneal@45
|
292 each(r, function(v) {
|
bgneal@45
|
293 m.add({title : v, onclick : function() {
|
bgneal@247
|
294 dom.replace(ed.getDoc().createTextNode(v), wordSpan);
|
bgneal@45
|
295 t._checkDone();
|
bgneal@45
|
296 }});
|
bgneal@45
|
297 });
|
bgneal@45
|
298
|
bgneal@45
|
299 m.addSeparator();
|
bgneal@45
|
300 } else
|
bgneal@45
|
301 m.add({title : 'spellchecker.no_sug', 'class' : 'mceMenuItemTitle'}).setDisabled(1);
|
bgneal@45
|
302
|
bgneal@217
|
303 ignoreRpc = t.editor.getParam("spellchecker_enable_ignore_rpc", '');
|
bgneal@45
|
304 m.add({
|
bgneal@45
|
305 title : 'spellchecker.ignore_word',
|
bgneal@45
|
306 onclick : function() {
|
bgneal@247
|
307 var word = wordSpan.innerHTML;
|
bgneal@217
|
308
|
bgneal@247
|
309 dom.remove(wordSpan, 1);
|
bgneal@45
|
310 t._checkDone();
|
bgneal@217
|
311
|
bgneal@217
|
312 // tell the server if we need to
|
bgneal@217
|
313 if (ignoreRpc) {
|
bgneal@217
|
314 ed.setProgressState(1);
|
bgneal@217
|
315 t._sendRPC('ignoreWord', [t.selectedLang, word], function(r) {
|
bgneal@217
|
316 ed.setProgressState(0);
|
bgneal@217
|
317 });
|
bgneal@217
|
318 }
|
bgneal@45
|
319 }
|
bgneal@45
|
320 });
|
bgneal@45
|
321
|
bgneal@45
|
322 m.add({
|
bgneal@45
|
323 title : 'spellchecker.ignore_words',
|
bgneal@45
|
324 onclick : function() {
|
bgneal@247
|
325 var word = wordSpan.innerHTML;
|
bgneal@217
|
326
|
bgneal@217
|
327 t._removeWords(dom.decode(word));
|
bgneal@45
|
328 t._checkDone();
|
bgneal@217
|
329
|
bgneal@217
|
330 // tell the server if we need to
|
bgneal@217
|
331 if (ignoreRpc) {
|
bgneal@217
|
332 ed.setProgressState(1);
|
bgneal@217
|
333 t._sendRPC('ignoreWords', [t.selectedLang, word], function(r) {
|
bgneal@217
|
334 ed.setProgressState(0);
|
bgneal@217
|
335 });
|
bgneal@217
|
336 }
|
bgneal@45
|
337 }
|
bgneal@45
|
338 });
|
bgneal@45
|
339
|
bgneal@217
|
340
|
bgneal@217
|
341 if (t.editor.getParam("spellchecker_enable_learn_rpc")) {
|
bgneal@217
|
342 m.add({
|
bgneal@217
|
343 title : 'spellchecker.learn_word',
|
bgneal@217
|
344 onclick : function() {
|
bgneal@247
|
345 var word = wordSpan.innerHTML;
|
bgneal@217
|
346
|
bgneal@247
|
347 dom.remove(wordSpan, 1);
|
bgneal@217
|
348 t._checkDone();
|
bgneal@217
|
349
|
bgneal@217
|
350 ed.setProgressState(1);
|
bgneal@217
|
351 t._sendRPC('learnWord', [t.selectedLang, word], function(r) {
|
bgneal@217
|
352 ed.setProgressState(0);
|
bgneal@217
|
353 });
|
bgneal@217
|
354 }
|
bgneal@217
|
355 });
|
bgneal@217
|
356 }
|
bgneal@217
|
357
|
bgneal@45
|
358 m.update();
|
bgneal@45
|
359 });
|
bgneal@45
|
360
|
bgneal@247
|
361 ed.selection.select(wordSpan);
|
bgneal@247
|
362 p1 = dom.getPos(wordSpan);
|
bgneal@247
|
363 m.showMenu(p1.x, p1.y + wordSpan.offsetHeight - vp.y);
|
bgneal@45
|
364
|
bgneal@45
|
365 return tinymce.dom.Event.cancel(e);
|
bgneal@45
|
366 } else
|
bgneal@45
|
367 m.hideMenu();
|
bgneal@45
|
368 },
|
bgneal@45
|
369
|
bgneal@45
|
370 _checkDone : function() {
|
bgneal@45
|
371 var t = this, ed = t.editor, dom = ed.dom, o;
|
bgneal@45
|
372
|
bgneal@45
|
373 each(dom.select('span'), function(n) {
|
bgneal@45
|
374 if (n && dom.hasClass(n, 'mceItemHiddenSpellWord')) {
|
bgneal@45
|
375 o = true;
|
bgneal@45
|
376 return false;
|
bgneal@45
|
377 }
|
bgneal@45
|
378 });
|
bgneal@45
|
379
|
bgneal@45
|
380 if (!o)
|
bgneal@45
|
381 t._done();
|
bgneal@45
|
382 },
|
bgneal@45
|
383
|
bgneal@45
|
384 _done : function() {
|
bgneal@45
|
385 var t = this, la = t.active;
|
bgneal@45
|
386
|
bgneal@45
|
387 if (t.active) {
|
bgneal@45
|
388 t.active = 0;
|
bgneal@45
|
389 t._removeWords();
|
bgneal@45
|
390
|
bgneal@45
|
391 if (t._menu)
|
bgneal@45
|
392 t._menu.hideMenu();
|
bgneal@45
|
393
|
bgneal@45
|
394 if (la)
|
bgneal@45
|
395 t.editor.nodeChanged();
|
bgneal@45
|
396 }
|
bgneal@45
|
397 },
|
bgneal@45
|
398
|
bgneal@45
|
399 _sendRPC : function(m, p, cb) {
|
bgneal@217
|
400 var t = this;
|
bgneal@45
|
401
|
bgneal@45
|
402 JSONRequest.sendRPC({
|
bgneal@217
|
403 url : t.rpcUrl,
|
bgneal@45
|
404 method : m,
|
bgneal@45
|
405 params : p,
|
bgneal@45
|
406 success : cb,
|
bgneal@45
|
407 error : function(e, x) {
|
bgneal@45
|
408 t.editor.setProgressState(0);
|
bgneal@45
|
409 t.editor.windowManager.alert(e.errstr || ('Error response: ' + x.responseText));
|
bgneal@45
|
410 }
|
bgneal@45
|
411 });
|
bgneal@45
|
412 }
|
bgneal@45
|
413 });
|
bgneal@45
|
414
|
bgneal@45
|
415 // Register plugin
|
bgneal@45
|
416 tinymce.PluginManager.add('spellchecker', tinymce.plugins.SpellcheckerPlugin);
|
bgneal@217
|
417 })();
|