Skip to content

Instantly share code, notes, and snippets.

@imanilchaudhari
Last active March 8, 2024 01:41
Show Gist options
  • Save imanilchaudhari/5a121ea6420eb4b8aa7ee70a5f7074e3 to your computer and use it in GitHub Desktop.
Save imanilchaudhari/5a121ea6420eb4b8aa7ee70a5f7074e3 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>charwordcount TinyMCE plugin example</title>
<script src="tinymce/tinymce.min.js"></script>
<script>
tinymce.init({
selector: "textarea",
width: 800,
height: 300,
resize: false,
plugins: [
"charwordcount advlist link image lists preview pagebreak",
"searchreplace code insertdatetime nonbreaking",
"table textcolor paste textcolor"
],
// optional, includes HTML tags and entities (like &nbsp;) in count; disabled by default;
charwordcount_include_tags: false,
toolbar1: "preview code | restoredraft undo redo | cut copy paste searchreplace",
toolbar2: "formatselect fontselect fontsizeselect | bold italic underline strikethrough",
menubar: false,
toolbar_items_size: 'small'
});
</script>
</head>
<body>
<textarea>TinyMCE count words plugin in action!</textarea>
</body>
</html>
tinymce.PluginManager.add("charwordcount",function(b){function f(){b.theme.panel.find("#wordcount").text(["Words: {0}, Characters: {1}",c.getCount(),c.getCountCharacters()])}var c=this,d,e;d=b.getParam("wordcount_countregex",/[\w\u2019\x27\-]+/g);e=b.getParam("wordcount_cleanregex",/[0-9.(),;:!?%#$?\x27\x22_+=\\\/\-]*/g);b.on("init",function(){var a=b.theme.panel&&b.theme.panel.find("#statusbar")[0];a&&window.setTimeout(function(){a.insert({type:"label",name:"wordcount",text:["Words: {0}, Characters: {1}",
c.getCount(),c.getCountCharacters()],classes:"wordcount"},0);b.on("setcontent beforeaddundo keyup",f)},0)});c.getCount=function(){var a=b.getContent({format:"raw"}),c=0;a&&(a=a.replace(/\.\.\./g," "),a=a.replace(/<.[^<>]*?>/g," ").replace(/&nbsp;|&#160;/gi," "),a=a.replace(/(\w+)(&.+?;)+(\w+)/,"$1$3").replace(/&.+?;/g," "),a=a.replace(e,""),a=a.match(d))&&(c=a.length);return c};c.getCountCharacters=function(){return!0==b.settings.charwordcount_include_tags?b.getContent({format:"raw"}).length:b.getContent({format:"raw"}).replace(/<.[^<>]*?>/g,
"").replace(/&[^;]+;/g,"?").length}});
@Faq
Copy link

Faq commented Aug 18, 2022

Hello, this did not work for tinymce 5.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment