On my website I have a text editor and I want to add real time statistics like the…
- User
On my website I have a text editor and I want to add real time statistics like the number of words, characters, paragraphes. Here is the current code:
<!-- Fixed editor container --> <div class="px-5 md:px-8 pb-8 flex-grow relative"> <div class="absolute inset-0 bg-noir-800 rounded-xl shadow-inner-lg overflow-hidden border border-noir-500"> <div id="text-editor" contenteditable="true" class="editor-text h-full w-full p-4 md:p-8 overflow-y-auto focus:outline-none text-base md:text-lg leading-relaxed prose max-w-none prose-invert prose-p:my-4 prose-headings:text-white"></div> </div> <div class="absolute bottom-4 right-4 text-xs text-white/50"> <span id="word-count">0</span> words </div> </div> Assistant
To add real-time statistics (word count, character count, and paragraph count) to your text editor, you'll need to use JavaScript to monitor changes in the contenteditable div and update the counts accordingly. Here's how you can implement this:
Updated HTML
<div class="px-5 md:px-8 pb-8 flex-grow relative"> <div class="absolute inset-0 bg-noir-800 rounded-xl shadow-inner-lg overflow-hidden border border-noir-500"> <div id="text-editor" contenteditable="true" class="editor-text h-full w-full p-4 md:p-8 overflow-y-auto focus:outline-none text-base md:text-lg leading-relaxed prose max-w-none prose-invert prose-p:my-4 prose-headings:text-white"></div> </div> <div class="absolute bottom-4 right-4 text-xs text-white/50 space-x-4"> <span id="word-count">0</span> words <span id="char-count">0</span> chars <span id="paragraph-count">0</span> paragraphs </div> </div>JavaScript
document.addEventListener('DOMContentLoaded', function() { const editor = document.getElementById('text-editor'); const wordCountElement = document.getElementById('word-count'); const charCountElement = document.getElementById('char-count'); const paragraphCountElement = document.getElementById('paragraph-count'); function updateStats() { const text = editor.innerText || editor.textContent; // Character count (including spaces) const charCount = text.length; charCountElement.textContent = charCount; // Word count const words = text.trim() ? text.trim().split(/\s+/).filter(word => word.length > 0) : []; wordCountElement.textContent = words.length; // Paragraph count const paragraphs = editor.querySelectorAll('p').length || (text.trim() ? 1 : 0); paragraphCountElement.textContent = paragraphs; } // Update stats on input editor.addEventListener('input', updateStats); // Update stats on paste (with a slight delay to allow paste to complete) editor.addEventListener('paste', () => setTimeout(updateStats, 10)); // Initial update updateStats(); });Explanation:
-
HTML Changes:
- Added two more counters for characters and paragraphs
- Used
space-x-4to add spacing between the counters
-
JavaScript:
- Character Count: Simply counts all characters in the text (including spaces)
- Word Count:
- Trims whitespace
- Splits by any whitespace (using
\s+regex) - Filters out empty strings
- Paragraph Count:
- Counts
<p>elements if they exist - Falls back to 1 if there's any text (for plain text without paragraphs)
- Shows 0 if editor is empty
- Counts
- Event listeners for:
input(typing, deleting)paste(with a small delay to ensure paste completes)
-
Performance:
- The function is lightweight enough to run on every keystroke
- For very large documents, you might want to debounce the function
Optional Enhancements:
- Add debouncing for better performance with large documents:
function debounce(func, timeout = 100) { let timer; return (...args) => { clearTimeout(timer); timer = setTimeout(() => { func.apply(this, args); }, timeout); }; } editor.addEventListener('input', debounce(updateStats));- Add more statistics like character count without spaces:
const charCountNoSpaces = text.replace(/\s+/g, '').length;- Style the counters differently when limits are reached (like Twitter's character counter).
This implementation will give you real-time updating statistics as users type in your editor.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.