plugin.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * @license Copyright (c) CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.html or http://ckeditor.com/license
  4. */
  5. CKEDITOR.plugins.add('wordcount', {
  6. lang: ['ca', 'de', 'en', 'es', 'fr', 'pl'],
  7. init: function (editor) {
  8. var defaultFormat = '<span class="cke_path_item">';
  9. var intervalId;
  10. var lastWordCount;
  11. var lastCharCount = 0;
  12. var limitReachedNotified = false;
  13. var limitRestoredNotified = false;
  14. // Default Config
  15. var defaultConfig = {
  16. showWordCount: true,
  17. showCharCount: false,
  18. charLimit: 'unlimited',
  19. wordLimit: 'unlimited'
  20. };
  21. // Get Config & Lang
  22. var config = CKEDITOR.tools.extend(defaultConfig, editor.config.wordcount || {}, true);
  23. if (config.showCharCount) {
  24. defaultFormat += editor.lang.wordcount.CharCount + '&nbsp;%charCount%';
  25. if (config.charLimit != 'unlimited') {
  26. defaultFormat += '&nbsp;(' + editor.lang.wordcount.limit + '&nbsp;' + config.charLimit + ')';
  27. }
  28. }
  29. if (config.showCharCount && config.showWordCount) {
  30. defaultFormat += ',&nbsp;';
  31. }
  32. if (config.showWordCount) {
  33. defaultFormat += editor.lang.wordcount.WordCount + ' %wordCount%';
  34. if (config.wordLimit != 'unlimited') {
  35. defaultFormat += '&nbsp;(' + editor.lang.wordcount.limit + '&nbsp;' + config.wordLimit + ')';
  36. }
  37. }
  38. defaultFormat += '</span>';
  39. var format = defaultFormat;
  40. CKEDITOR.document.appendStyleSheet(this.path + 'css/wordcount.css');
  41. function counterId(editorInstance) {
  42. return 'cke_wordcount_' + editorInstance.name;
  43. }
  44. function counterElement(editorInstance) {
  45. return document.getElementById(counterId(editorInstance));
  46. }
  47. function strip(html) {
  48. var tmp = document.createElement("div");
  49. tmp.innerHTML = html;
  50. if (tmp.textContent == '' && typeof tmp.innerText == 'undefined') {
  51. return '0';
  52. }
  53. return tmp.textContent || tmp.innerText;
  54. }
  55. function updateCounter(editorInstance) {
  56. var wordCount = 0;
  57. var charCount = 0;
  58. if (editorInstance.getData()) {
  59. var text = editorInstance.getData().replace(/(\r\n|\n|\r)/gm, " ").replace("&nbsp;", " ");
  60. if (config.showWordCount) {
  61. wordCount = strip(text).split(/\s+/).length;
  62. }
  63. charCount = strip(text).length;
  64. }
  65. var html = format.replace('%wordCount%', wordCount).replace('%charCount%', charCount);
  66. counterElement(editorInstance).innerHTML = html;
  67. if (charCount == lastCharCount) {
  68. return true;
  69. } else {
  70. lastWordCount = wordCount;
  71. lastCharCount = charCount;
  72. }
  73. // Check for word limit
  74. if (config.showWordCount && wordCount > config.wordLimit) {
  75. limitReached(editor, limitReachedNotified);
  76. } else if (!limitRestoredNotified && wordCount < config.wordLimit) {
  77. limitRestored(editor);
  78. }
  79. // Check for char limit
  80. if (config.showCharCount && charCount > config.charLimit) {
  81. limitReached(editor, limitReachedNotified);
  82. } else if (!limitRestoredNotified && charCount < config.charLimit) {
  83. limitRestored(editor);
  84. }
  85. return true;
  86. }
  87. function limitReached(editorInstance, notify) {
  88. limitReachedNotified = true;
  89. limitRestoredNotified = false;
  90. editorInstance.execCommand('undo');
  91. if (!notify) {
  92. counterElement(editorInstance).className += " cke_wordcountLimitReached";
  93. editorInstance.fire('limitReached', {}, editor);
  94. }
  95. // lock editor
  96. editorInstance.config.Locked = 1;
  97. editorInstance.fire("change");
  98. }
  99. function limitRestored(editorInstance) {
  100. limitRestoredNotified = true;
  101. limitReachedNotified = false;
  102. editorInstance.config.Locked = 0;
  103. counterElement(editorInstance).className = "cke_wordcount";
  104. }
  105. editor.on('uiSpace', function(event) {
  106. if (event.data.space == 'bottom') {
  107. event.data.html += '<div id="' + counterId(event.editor) + '" class="cke_wordcount" style=""' + ' title="' + editor.lang.wordcount.title + '"' + '>&nbsp;</div>';
  108. }
  109. }, editor, null, 100);
  110. editor.on('dataReady', function(event) {
  111. var count = event.editor.getData().length;
  112. if (count > config.wordLimit) {
  113. limitReached(editor);
  114. }
  115. updateCounter(event.editor);
  116. }, editor, null, 100);
  117. editor.on('key', function (event) {
  118. updateCounter(event.editor);
  119. }, editor, null, 100);
  120. editor.on('afterPaste', function (event) {
  121. updateCounter(event.editor);
  122. }, editor, null, 100);
  123. /* editor.on('change', function (event) {
  124. updateCounter(event.editor);
  125. }, editor, null, 100);*/
  126. editor.on('focus', function(event) {
  127. //editorHasFocus = true;
  128. intervalId = window.setInterval(function() {
  129. updateCounter(editor);
  130. }, 300, event.editor);
  131. }, editor, null, 300);
  132. editor.on('blur', function() {
  133. //editorHasFocus = false;
  134. if (intervalId) {
  135. window.clearInterval(intervalId);
  136. }
  137. }, editor, null, 300);
  138. }
  139. });