@ckeditor/ckeditor5-word-count
Version:
Word and character count feature for CKEditor 5.
5 lines • 5.15 kB
JavaScript
!function(t){const e=t.en=t.en||{};e.dictionary=Object.assign(e.dictionary||{},{"Characters: %0":"Characters: %0","Words: %0":"Words: %0"})}(window.CKEDITOR_TRANSLATIONS||(window.CKEDITOR_TRANSLATIONS={})),
/*!
* @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md.
*/(()=>{var t={237:t=>{"use strict";t.exports=CKEditor5.dll},311:(t,e,r)=>{t.exports=r(237)("./src/ui.js")},584:(t,e,r)=>{t.exports=r(237)("./src/utils.js")},782:(t,e,r)=>{t.exports=r(237)("./src/core.js")}},e={};function r(o){var n=e[o];if(void 0!==n)return n.exports;var i=e[o]={exports:{}};return t[o](i,i.exports,r),i.exports}r.d=(t,e)=>{for(var o in e)r.o(e,o)&&!r.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:e[o]})},r.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var o={};(()=>{"use strict";r.r(o),r.d(o,{WordCount:()=>l});var t=r(782),e=r(311),n=r(584);function i(t){if(t.is("$text")||t.is("$textProxy"))return t.data;const e=t;let r="",o=null;for(const t of e.getChildren()){const e=i(t);o&&o.is("element")&&(r+="\n"),r+=e,o=t}return r}function s(t,e,{signal:r,edges:o}={}){let n,i=null;const s=null!=o&&o.includes("leading"),c=null==o||o.includes("trailing"),a=()=>{null!==i&&(t.apply(n,i),n=void 0,i=null)};let l=null;const u=()=>{null!=l&&clearTimeout(l),l=setTimeout((()=>{l=null,c&&a(),h()}),e)},d=()=>{null!==l&&(clearTimeout(l),l=null)},h=()=>{d(),n=void 0,i=null},g=function(...t){if(r?.aborted)return;n=this,i=t;const e=null==l;u(),s&&e&&a()};return g.schedule=u,g.cancel=h,g.flush=()=>{d(),a()},r?.addEventListener("abort",h,{once:!0}),g}function c(t,e=0,r={}){"object"!=typeof r&&(r={});const{leading:o=!0,trailing:n=!0,signal:i}=r;return function(t,e=0,r={}){"object"!=typeof r&&(r={});const{signal:o,leading:n=!1,trailing:i=!0,maxWait:c}=r,a=Array(2);let l;n&&(a[0]="leading"),i&&(a[1]="trailing");let u=null;const d=s((function(...e){l=t.apply(this,e),u=null}),e,{signal:o,edges:a}),h=function(...e){if(null!=c)if(null===u)u=Date.now();else if(Date.now()-u>=c)return l=t.apply(this,e),u=Date.now(),d.cancel(),d.schedule(),l;return d.apply(this,e),l};return h.cancel=d.cancel,h.flush=()=>(d.flush(),l),h}(t,e,{leading:o,trailing:n,signal:i,maxWait:e})}function a(t){return function(t){return"object"==typeof t&&null!==t}(t)&&1===t.nodeType&&!function(t){if("object"!=typeof t)return!1;if(null==t)return!1;if(null===Object.getPrototypeOf(t))return!0;if("[object Object]"!==Object.prototype.toString.call(t)){const e=t[Symbol.toStringTag];return null!=e&&(!!Object.getOwnPropertyDescriptor(t,Symbol.toStringTag)?.writable&&t.toString()===`[object ${e}]`)}let e=t;for(;null!==Object.getPrototypeOf(e);)e=Object.getPrototypeOf(e);return Object.getPrototypeOf(t)===e}(t)}class l extends t.Plugin{_config;_outputView;_wordsMatchRegExp;constructor(t){super(t),this.set("characters",0),this.set("words",0),Object.defineProperties(this,{characters:{get(){return this.characters=this._getCharacters(this._getText())}},words:{get(){return this.words=this._getWords(this._getText())}}}),this.set("_wordsLabel",void 0),this.set("_charactersLabel",void 0),this._config=t.config.get("wordCount")||{},this._outputView=void 0,this._wordsMatchRegExp=n.env.features.isRegExpUnicodePropertySupported?new RegExp("([\\p{L}\\p{N}]+\\S?)+","gu"):/([a-zA-Z0-9À-ž]+\S?)+/gu}static get pluginName(){return"WordCount"}static get isOfficialPlugin(){return!0}init(){this.editor.model.document.on("change:data",c(this._refreshStats.bind(this),250)),"function"==typeof this._config.onUpdate&&this.on("update",((t,e)=>{this._config.onUpdate(e)})),a(this._config.container)&&this._config.container.appendChild(this.wordCountContainer)}destroy(){this._outputView&&(this._outputView.element.remove(),this._outputView.destroy()),super.destroy()}get wordCountContainer(){const t=this.editor,r=t.t,o=t.config.get("wordCount.displayWords"),n=t.config.get("wordCount.displayCharacters"),i=e.Template.bind(this,this),s=[];return this._outputView||(this._outputView=new e.View,(o||void 0===o)&&(this.bind("_wordsLabel").to(this,"words",(t=>r("Words: %0",t))),s.push({tag:"div",children:[{text:[i.to("_wordsLabel")]}],attributes:{class:"ck-word-count__words"}})),(n||void 0===n)&&(this.bind("_charactersLabel").to(this,"characters",(t=>r("Characters: %0",t))),s.push({tag:"div",children:[{text:[i.to("_charactersLabel")]}],attributes:{class:"ck-word-count__characters"}})),this._outputView.setTemplate({tag:"div",attributes:{class:["ck","ck-word-count"]},children:s}),this._outputView.render()),this._outputView.element}_getText(){let t="";for(const e of this.editor.model.document.getRoots())""!==t&&(t+="\n"),t+=i(e);return t}_getCharacters(t){return t.replace(/\n/g,"").length}_getWords(t){return(t.match(this._wordsMatchRegExp)||[]).length}_refreshStats(){const t=this._getText(),e=this.words=this._getWords(t),r=this.characters=this._getCharacters(t);this.fire("update",{words:e,characters:r})}}})(),(window.CKEditor5=window.CKEditor5||{}).wordCount=o})();