UNPKG

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.12 kB
/*! * froala_editor v5.0.1 (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(c){"use strict";Object.assign(c.DEFAULTS,{wordCounterMax:-1,wordCounterCount:!0}),c.PLUGINS.wordCounter=function(i){var t,r=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")),r=(t.text()||"").trim().split(/\s+/).filter(Boolean).length,e=e.split(/\r?\n/),o=0;return e.forEach(function(e){0<(e=e.replace(/\s/g,"")).length&&o++}),o-t.length*r}function e(e){var t,r,o,n,a,u=!1;return i.opts.wordCounterMax<0||s()<i.opts.wordCounterMax||(t=e.which,a=(r=i.selection.get()).focusOffset,n=r.focusNode.textContent,o=(n=n.replace(/\u00a0/g," ")).toUpperCase().charCodeAt(a-1),n=n.toUpperCase().charCodeAt(a),!function(e,t){if(i.keys.isCharacter(e)&&i.keys.isCharacter(t)&&e!=c.KEYCODE.SPACE&&t!=c.KEYCODE.SPACE)return 1;return}(o=r.focusNode.data?o:n=NaN,n)||t!==c.KEYCODE.ENTER&&t!==c.KEYCODE.SPACE||(u=!0),a=n,(o===c.KEYCODE.SPACE&&a===c.KEYCODE.SPACE||function(e,t){if(isNaN(e)&&t===c.KEYCODE.SPACE||e===c.KEYCODE.SPACE&&isNaN(t))return 1;return}(o,n)||function(e,t){if(isNaN(e)&&isNaN(t))return 1;return}(o,n))&&i.keys.isCharacter(t)&&t!=c.KEYCODE.SPACE&&(u=!0),s()>i.opts.wordCounterMax&&(i.keys.isCharacter(t)||t===c.KEYCODE.ENTER)&&(u=!0),!(!i.keys.ctrlKey(e)&&u||t===c.KEYCODE.IME))||(e.preventDefault(),e.stopPropagation(),i.events.trigger("wordCounter.exceeded"),!1)}function o(){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||r("<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=r(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",o),i.events.trigger("wordCounter.update"),void i.events.on("destroy",function(){r(i.o_win).off("resize.char".concat(i.id)),t.removeData().remove(),t=null}))},wordCount:s}}});