annotate WebContent/jscripts/tiny_mce/plugins/spellchecker/editor_plugin_src.js @ 5:0be9d53a6967

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