froala-editor
Version:
The next generation Javascript WYSIWYG HTML rich text editor made by devs for devs. High performance and modern design make it easy to use for developers and loved by users.
7 lines (6 loc) • 3.15 kB
JavaScript
/*!
* froala_editor v5.3.0 (https://www.froala.com/wysiwyg-editor)
* License https://froala.com/wysiwyg-editor/terms/
* Copyright 2014-2026 Froala Labs
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(require("froala-editor")):"function"==typeof define&&define.amd?define(["froala-editor"],t):t(("undefined"!=typeof globalThis?globalThis:e||self).FroalaEditor)}(this,function(d){"use strict";Object.assign(d.DEFAULTS,{wordCounterMax:-1,wordCounterCount:!0}),d.PLUGINS.wordCounter=function(i){var t,o=i.$;function s(){var e=i.el.innerText.toString(),t=(e=(e=(e=(e=(e=(e=(e=(e=e.replace(/(^\s*)|(\s*$)/gi," ")).replace(/\u00a0/g," ")).replace(/[ ]{2,}/gi," ")).replace(/\n /,"\n")).replace(/[\u200A\u200C-\u200D\uFEFF\u202F\u2000-\u2009\u205F\u3000\u1680\u00A0\u0020]/g," ")).replace(/[\u200B]/g,"")).replace(/\t+/g," ")).split(" ").join("\n"),i.$el.find(".fr-page-break")),o=(t.text()||"").trim().split(/\s+/).filter(Boolean).length,e=e.split(/\r?\n/),r=0;return e.forEach(function(e){0<(e=e.replace(/\s/g,"")).length&&r++}),r-t.length*o}function e(e){var t,o,r,n,u,a=!1;return i.opts.wordCounterMax<0||s()<i.opts.wordCounterMax||(t=e.which,u=(o=i.selection.get()).focusOffset,n=o.focusNode.textContent,r=(n=n.replace(/\u00a0/g," ")).toUpperCase().charCodeAt(u-1),n=n.toUpperCase().charCodeAt(u),!function(e,t){if(c(e)&&c(t)&&e!=d.KEYCODE.SPACE&&t!=d.KEYCODE.SPACE)return 1;return}(r=o.focusNode.data?r:n=NaN,n)||t!==d.KEYCODE.ENTER&&t!==d.KEYCODE.SPACE||(a=!0),u=n,(r===d.KEYCODE.SPACE&&u===d.KEYCODE.SPACE||function(e,t){if(isNaN(e)&&t===d.KEYCODE.SPACE||e===d.KEYCODE.SPACE&&isNaN(t))return 1;return}(r,n)||function(e,t){if(isNaN(e)&&isNaN(t))return 1;return}(r,n))&&i.keys.isCharacter(t)&&t!=d.KEYCODE.SPACE&&(a=!0),s()>i.opts.wordCounterMax&&(i.keys.isCharacter(t)||t===d.KEYCODE.ENTER)&&(a=!0),!(!i.keys.ctrlKey(e)&&a||t===d.KEYCODE.IME))||(e.preventDefault(),e.stopPropagation(),i.events.trigger("wordCounter.exceeded"),!1)}function c(e){if(!isNaN(e))return e=String.fromCharCode(e),/\S/.test(e)}function r(){var e;i.opts.wordCounterCount&&(e=s()+(0<i.opts.wordCounterMax?"/"+i.opts.wordCounterMax:""),t.text("".concat(i.language.translate("Words")," : ").concat(e)),i.opts.toolbarBottom&&t.css("margin-bottom",i.$tb.outerHeight(!0)),0<=(e=i.$wp.get(0).offsetWidth-i.$wp.get(0).clientWidth))&&("rtl"==i.opts.direction?t.css("margin-left",e):t.css("margin-right",e))}function n(e){return i.opts.wordCounterMax<0||o("<div>").html(e).text().split(" ").length+s()<=i.opts.wordCounterMax?e:(i.events.trigger("wordCounter.exceeded"),"")}return{_init:function(){return!!i.$wp&&!!i.opts.wordCounterCount&&((t=o(document.createElement("span")).attr("class","fr-counter fr-wCounter")).css("bottom",i.$wp.css("border-bottom-width")),(i.$second_tb||i.$wp).append(t),i.events.on("keydown",e,!0),i.events.on("paste.afterCleanup",n),i.events.on("keyup contentChanged input",function(){i.events.trigger("wordCounter.update")}),i.events.on("wordCounter.update",r),i.events.trigger("wordCounter.update"),void i.events.on("destroy",function(){o(i.o_win).off("resize.char".concat(i.id)),t.removeData().remove(),t=null}))},wordCount:s}}});