|
1 /** |
|
2 * plugin.js |
|
3 * |
|
4 * Copyright, Moxiecode Systems AB |
|
5 * Released under LGPL License. |
|
6 * |
|
7 * License: http://www.tinymce.com/license |
|
8 * Contributing: http://www.tinymce.com/contributing |
|
9 */ |
|
10 |
|
11 /*global tinymce:true */ |
|
12 |
|
13 tinymce.PluginManager.add('wordcount', function(editor) { |
|
14 var self = this, countre, cleanre; |
|
15 |
|
16 // Included most unicode blocks see: http://en.wikipedia.org/wiki/Unicode_block |
|
17 // Latin-1_Supplement letters, a-z, u2019 == ’ |
|
18 countre = editor.getParam('wordcount_countregex', /[\w\u2019\x27\-\u00C0-\u1FFF]+/g); |
|
19 cleanre = editor.getParam('wordcount_cleanregex', /[0-9.(),;:!?%#$?\x27\x22_+=\\\/\-]*/g); |
|
20 |
|
21 function update() { |
|
22 editor.theme.panel.find('#wordcount').text(['Words: {0}', self.getCount()]); |
|
23 } |
|
24 |
|
25 editor.on('init', function() { |
|
26 var statusbar = editor.theme.panel && editor.theme.panel.find('#statusbar')[0]; |
|
27 |
|
28 if (statusbar) { |
|
29 window.setTimeout(function() { |
|
30 statusbar.insert({ |
|
31 type: 'label', |
|
32 name: 'wordcount', |
|
33 text: ['Words: {0}', self.getCount()], |
|
34 classes: 'wordcount', |
|
35 disabled: editor.settings.readonly |
|
36 }, 0); |
|
37 |
|
38 editor.on('setcontent beforeaddundo', update); |
|
39 |
|
40 editor.on('keyup', function(e) { |
|
41 if (e.keyCode == 32) { |
|
42 update(); |
|
43 } |
|
44 }); |
|
45 }, 0); |
|
46 } |
|
47 }); |
|
48 |
|
49 self.getCount = function() { |
|
50 var tx = editor.getContent({format: 'raw'}); |
|
51 var tc = 0; |
|
52 |
|
53 if (tx) { |
|
54 tx = tx.replace(/\.\.\./g, ' '); // convert ellipses to spaces |
|
55 tx = tx.replace(/<.[^<>]*?>/g, ' ').replace(/ | /gi, ' '); // remove html tags and space chars |
|
56 |
|
57 // deal with html entities |
|
58 tx = tx.replace(/(\w+)(&#?[a-z0-9]+;)+(\w+)/i, "$1$3").replace(/&.+?;/g, ' '); |
|
59 tx = tx.replace(cleanre, ''); // remove numbers and punctuation |
|
60 |
|
61 var wordArray = tx.match(countre); |
|
62 if (wordArray) { |
|
63 tc = wordArray.length; |
|
64 } |
|
65 } |
|
66 |
|
67 return tc; |
|
68 }; |
|
69 }); |