File size: 5,417 Bytes
1abfaca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**

 * @license Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.

 * For licensing, see LICENSE.html or http://ckeditor.com/license

 */
 
CKEDITOR.plugins.add('wordcount', {
    lang: ['de', 'en'],
    init: function (editor) {
        
        var defaultLimit = 'unlimited';
        var defaultFormat = '<span class="cke_path_item">';
        var limit = defaultLimit;
        
        var intervalId;
        var lastWordCount = 0;
        var lastCharCount = 0;
        var limitReachedNotified = false;
        var limitRestoredNotified = false;
        
        // Default Config
        var defaultConfig = {
            showWordCount: true,
            showCharCount: false
        };
        
        // Get Config & Lang
        var config = CKEDITOR.tools.extend(defaultConfig, editor.config.wordcount || {}, true);
        
        if (config.showCharCount) {
            defaultFormat += editor.lang.wordcount.CharCount + '&nbsp;%charCount%';
        }

        if (config.showCharCount && config.showWordCount) {
            defaultFormat += ',&nbsp;';
        }

        if (config.showWordCount) {
            defaultFormat += editor.lang.wordcount.WordCount + ' %wordCount%';
        }
        
        defaultFormat += '</span>';
        
        var format = defaultFormat;

        CKEDITOR.document.appendStyleSheet(this.path + 'css/wordcount.css');

        function counterId(editor) {
            return 'cke_wordcount_' + editor.name;
        }

        function counterElement(editor) {
            return document.getElementById(counterId(editor));
        }

        function strip(html) {
            var tmp = document.createElement("div");
            tmp.innerHTML = html;

            if (tmp.textContent == '' && typeof tmp.innerText == 'undefined') {
               return '0';
            }
            return tmp.textContent || tmp.innerText;
        }

        function updateCounter(editor) {
            var wordCount = 0;
            var charCount = 0;

            if (editor.getData()) {
                var text = editor.getData().replace(/(\r\n|\n|\r)/gm, "").replace(/(&nbsp;)/g, " ");
				
                if (config.showWordCount) {
					wordCount = strip(text).trim().split(/\s+/).length;
                }

                if (config.showCharCount) {
					charCount = strip(text).trim().length;
                }
            }
            var html = format.replace('%wordCount%', wordCount).replace('%charCount%', charCount);

            counterElement(editor).innerHTML = html;

            if (charCount == lastCharCount) {
                return true;
            } else {
                lastWordCount = wordCount;
                lastCharCount = charCount;
            }
            if (wordCount > limit) {
                limitReached(editor, limitReachedNotified);
            } else if (!limitRestoredNotified && wordCount < limit) {
                limitRestored(editor);
            }
            
            return true;
        }

        function limitReached(editor, notify) {
            limitReachedNotified = true;
            limitRestoredNotified = false;
            editor.execCommand('undo');
            if (!notify) {
                editor.fire('limitReached', {}, editor);
            }
            // lock editor
            editor.config.Locked = 1;
            editor.fire("change");
        }

        function limitRestored(editor) {
            limitRestoredNotified = true;
            limitReachedNotified = false;
            editor.config.Locked = 0;
        }

        editor.on('uiSpace', function(event) {
            if (event.data.space == 'bottom') {
                event.data.html += '<div id="' + counterId(event.editor) + '" class="cke_wordcount" style=""' + ' title="' + CKEDITOR.tools.htmlEncode('Words Counter') + '"' + '>&nbsp;</div>';
            }
        }, editor, null, 100);
        editor.on('instanceReady', function() {
            if (editor.config.wordcount_limit != undefined) {
                limit = editor.config.wordcount_limit;
            }
            if (editor.config.wordcount_format != undefined) {
                format = editor.config.wordcount_format;
            }
        }, editor, null, 100);
        editor.on('dataReady', function(event) {
            var count = event.editor.getData().length;
            if (count > limit) {
                limitReached(editor);
            }
            updateCounter(event.editor);
        }, editor, null, 100);
		editor.on('key', function (event) {
		    updateCounter(event.editor);
		}, editor, null, 100);
		editor.on('afterPaste', function (event) {
		    updateCounter(event.editor);
		}, editor, null, 100);
       /* editor.on('change', function (event) {

            updateCounter(event.editor);

        }, editor, null, 100);*/
        editor.on('focus', function(event) {
            editorHasFocus = true;
            intervalId = window.setInterval(function(editor) {
                updateCounter(editor);
            }, 100, event.editor);
        }, editor, null, 100);
        editor.on('blur', function() {
            editorHasFocus = false;
            if (intervalId) clearInterval(intervalId);
        }, editor, null, 100);
    }
});