1 |
3 |
ahitrov@rambler.ru |
/** |
2 |
|
|
* editor_plugin_src.js |
3 |
|
|
* |
4 |
|
|
* Copyright 2009, Moxiecode Systems AB |
5 |
|
|
* Released under LGPL License. |
6 |
|
|
* |
7 |
|
|
* License: http://tinymce.moxiecode.com/license |
8 |
|
|
* Contributing: http://tinymce.moxiecode.com/contributing |
9 |
|
|
*/ |
10 |
|
|
|
11 |
|
|
(function() { |
12 |
|
|
tinymce.create('tinymce.plugins.WordCount', { |
13 |
|
|
block : 0, |
14 |
|
|
id : null, |
15 |
|
|
countre : null, |
16 |
|
|
cleanre : null, |
17 |
|
|
|
18 |
|
|
init : function(ed, url) { |
19 |
|
|
var t = this, last = 0; |
20 |
|
|
|
21 |
|
|
t.countre = ed.getParam('wordcount_countregex', /\S\s+/g); |
22 |
|
|
t.cleanre = ed.getParam('wordcount_cleanregex', /[0-9.(),;:!?%#$�'"_+=\\/-]*/g); |
23 |
|
|
t.id = ed.id + '-word-count'; |
24 |
|
|
|
25 |
|
|
ed.onPostRender.add(function(ed, cm) { |
26 |
|
|
var row, id; |
27 |
|
|
|
28 |
|
|
// Add it to the specified id or the theme advanced path |
29 |
|
|
id = ed.getParam('wordcount_target_id'); |
30 |
|
|
if (!id) { |
31 |
|
|
row = tinymce.DOM.get(ed.id + '_path_row'); |
32 |
|
|
|
33 |
|
|
if (row) |
34 |
|
|
tinymce.DOM.add(row.parentNode, 'div', {'style': 'float: right'}, ed.getLang('wordcount.words', 'Words: ') + '<span id="' + t.id + '">0</span>'); |
35 |
|
|
} else |
36 |
|
|
tinymce.DOM.add(id, 'span', {}, '<span id="' + t.id + '">0</span>'); |
37 |
|
|
}); |
38 |
|
|
|
39 |
|
|
ed.onInit.add(function(ed) { |
40 |
|
|
ed.selection.onSetContent.add(function() { |
41 |
|
|
t._count(ed); |
42 |
|
|
}); |
43 |
|
|
|
44 |
|
|
t._count(ed); |
45 |
|
|
}); |
46 |
|
|
|
47 |
|
|
ed.onSetContent.add(function(ed) { |
48 |
|
|
t._count(ed); |
49 |
|
|
}); |
50 |
|
|
|
51 |
|
|
ed.onKeyUp.add(function(ed, e) { |
52 |
|
|
if (e.keyCode == last) |
53 |
|
|
return; |
54 |
|
|
|
55 |
|
|
if (13 == e.keyCode || 8 == last || 46 == last) |
56 |
|
|
t._count(ed); |
57 |
|
|
|
58 |
|
|
last = e.keyCode; |
59 |
|
|
}); |
60 |
|
|
}, |
61 |
|
|
|
62 |
|
|
_count : function(ed) { |
63 |
|
|
var t = this, tc = 0; |
64 |
|
|
|
65 |
|
|
// Keep multiple calls from happening at the same time |
66 |
|
|
if (t.block) |
67 |
|
|
return; |
68 |
|
|
|
69 |
|
|
t.block = 1; |
70 |
|
|
|
71 |
|
|
setTimeout(function() { |
72 |
|
|
var tx = ed.getContent({format : 'raw'}); |
73 |
|
|
|
74 |
|
|
if (tx) { |
75 |
|
|
tx = tx.replace(/<.[^<>]*?>/g, ' ').replace(/ | /gi, ' '); // remove html tags and space chars |
76 |
|
|
tx = tx.replace(t.cleanre, ''); // remove numbers and punctuation |
77 |
|
|
tx.replace(t.countre, function() {tc++;}); // count the words |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
tinymce.DOM.setHTML(t.id, tc.toString()); |
81 |
|
|
|
82 |
|
|
setTimeout(function() {t.block = 0;}, 2000); |
83 |
|
|
}, 1); |
84 |
|
|
}, |
85 |
|
|
|
86 |
|
|
getInfo: function() { |
87 |
|
|
return { |
88 |
|
|
longname : 'Word Count plugin', |
89 |
|
|
author : 'Moxiecode Systems AB', |
90 |
|
|
authorurl : 'http://tinymce.moxiecode.com', |
91 |
|
|
infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/wordcount', |
92 |
|
|
version : tinymce.majorVersion + "." + tinymce.minorVersion |
93 |
|
|
}; |
94 |
|
|
} |
95 |
|
|
}); |
96 |
|
|
|
97 |
|
|
tinymce.PluginManager.add('wordcount', tinymce.plugins.WordCount); |
98 |
|
|
})(); |