New Metronic Docs!Added Integration Docs with Starter Kits Apps for Laravel, Laravel Livewire, Angular, Vue, Symfony, Blazor Server, Django & Flask & more
Browse Docs

How to Use CKEditor Plugins with Metronic Bootstrap


Hi,

On my page, HTML textarea elements are configured to be transformed into CKEditor instances using the following code:

const textareaElements = document.querySelectorAll('[data-ckeditor="true"]');
textareaElements.forEach(el => {
ClassicEditor.create(el);
});

I want to impose a limit on the maximum number of words that can be entered. Can you please guide how to achieve that?


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (2)


Hi,

To impose a word limit on CKEditor instances, you can make use of CKEditor's event system to listen to the input event and apply a word count logic. CKEditor does not provide a built-in word limit feature, so you'll need to implement a custom solution to count the words and prevent further input when the limit is reached.


const textareaElements = document.querySelectorAll("[data-ckeditor="true"]");
const MAX_WORDS = 100; // Set your word limit here

textareaElements.forEach(el => {
ClassicEditor.create(el).then(editor => {
// Listen to the input event in the editor
editor.model.document.on("change:data", () => {
// Get the editor content as plain text
const text = editor.getData().replace(/<[^>]*>/g, "").trim();

// Count the number of words
const words = text.split(/\s+/).filter(word => word.length > 0);
const wordCount = words.length;

// If the word count exceeds the limit, prevent more input
if (wordCount > MAX_WORDS) {
alert(`You have reached the maximum word limit of ${MAX_WORDS} words.`);

// Optionally, trim the content to the maximum allowed number of words
const limitedText = words.slice(0, MAX_WORDS).join(" ");

// Set the truncated content back into the editor
editor.setData(limitedText);
}
});
}).catch(error => {
console.error("There was a problem initializing CKEditor:", error);
});
});


For more info please check CKEditor Docs.

Regards,
Sean



Thanks for your reply, Sean. I'll give it a try.


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(