Skip to content
This repository has been archived by the owner on Apr 16, 2018. It is now read-only.

Commit

Permalink
fixed issue #1 + merged the changes from the sabf fork + moved the st…
Browse files Browse the repository at this point in the history
…yle code to an external css file
  • Loading branch information
w8tcha committed Feb 3, 2013
1 parent 364e919 commit 1dd87a1
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 72 deletions.
1 change: 1 addition & 0 deletions wordcount/css/wordcount.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.cke_wordcount {display:block;float:right;margin-top:-1px;margin-right:3px;color:black;}
154 changes: 82 additions & 72 deletions wordcount/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* For licensing, see LICENSE.html or http://ckeditor.com/license
*/

CKEDITOR.plugins.add('wordcount', {
CKEDITOR.plugins.add('wordcount', {
lang: ['de', 'en'],
init: function (editor) {
init: function (editor) {
var defaultLimit = 'unlimited';
var defaultFormat = '<span class="cke_path_item">' + editor.lang.wordcount.WordCount + ' %count%</span>';
var limit = defaultLimit;
Expand All @@ -14,81 +14,91 @@
var lastCount = 0;
var limitReachedNotified = false;
var limitRestoredNotified = false;
if (true) {
function counterId(editor) {
return 'cke_wordcount_' + editor.name

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;
return tmp.textContent || tmp.innerText
}

function updateCounter(editor) {
var count = 0;
if (editor.getData()) {
count = strip(editor.getData()).trim().split(/\s+/).length;
}
function counterElement(editor) {
return document.getElementById(counterId(editor))
var html = format.replace('%count%', count);
counterElement(editor).innerHTML = html
if (count == lastCount) {
return true
} else {
lastCount = count
}
function strip(html) {
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText
if (count > limit) {
limitReached(editor, limitReachedNotified)
} else if (!limitRestoredNotified && count < limit) {
limitRestored(editor)
}
function updateCounter(editor) {
var count = 0;
if (editor.getData() != undefined) {
count = strip(editor.getData()).trim().split(/\s+/).length;
}
if (count == lastCount) {
return true
} else {
lastCount = count
}
if (!limitReachedNotified && count > limit) {
limitReached(editor)
} else if (!limitRestoredNotified && count < limit) {
limitRestored(editor)
}
var html = format.replace('%count%', count);
counterElement(editor).innerHTML = html
}

function limitReached(editor, notify) {
limitReachedNotified = true;
limitRestoredNotified = false;
editor.execCommand('undo');
if (!notify) {
editor.fire('limitReached', {}, editor);
}
function limitReached(editor) {
limitReachedNotified = true;
limitRestoredNotified = false;
editor.execCommand( 'undo' );
// lock editor
editor.config.Locked = 1;
// 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>'
}
function limitRestored(editor) {
limitRestoredNotified = true;
limitReachedNotified = false;

editor.config.Locked = 0;

}, editor, null, 100);
editor.on('instanceReady', function (event) {
if (editor.config.wordcount_limit != undefined) {
limit = editor.config.wordcount_limit
}
editor.on('uiSpace', function (event) {
if (event.data.space == 'bottom') {
event.data.html += '<div id="' + counterId(event.editor) + '" class="cke_wordcount" style="display:block;float:right;margin-top:5px;margin-right:3px;color:black;"' + ' title="' + CKEDITOR.tools.htmlEncode('Words Counter') + '"' + '>&nbsp;</div>'
}
}, editor, null, 100);
editor.on('instanceReady', function (event) {
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) {}, editor, null, 100);
editor.on('focus', function (event) {
editorHasFocus = true;
intervalId = window.setInterval(function (editor) {
updateCounter(editor)
}, 1000, event.editor)
}, editor, null, 100);
editor.on('blur', function (event) {
editorHasFocus = false;
if (intervalId) clearInterval(intervalId)
}, editor, null, 100)
}
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('change', function (event) {
updateCounter(event.editor)
}, editor, null, 100);
editor.on('focus', function (event) {
editorHasFocus = true;
intervalId = window.setInterval(function (editor) {
updateCounter(editor)
}, 1000, event.editor)
}, editor, null, 100);
editor.on('blur', function (event) {
editorHasFocus = false;
if (intervalId) clearInterval(intervalId)
}, editor, null, 100)
}
});

0 comments on commit 1dd87a1

Please sign in to comment.