Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const editor = document.getElementById('editor');
+ const darkModeToggle = document.getElementById('darkModeToggle');
+
+ // Load saved content
+ editor.value = localStorage.getItem('writeflow-content') || '';
+
+ // Load dark mode preference
+ if (localStorage.getItem('writeflow-dark-mode') === 'true') {
+ document.body.classList.add('dark-mode');
+ }
+
+ let lastKeyPress = Date.now();
+ let wordCount = 0;
+ let charCount = 0;
+ let typeSpeed = 0;
+ let words = [];
+
+ function updateStats() {
+ const text = editor.value;
+
+ // Word count
+ words = text.trim().split(/\s+/).filter(word => word.length > 0);
+ wordCount = words.length;
+ document.getElementById('wordCount').textContent = wordCount;
+
+ // Character count
+ charCount = text.length;
+ document.getElementById('charCount').textContent = charCount;
+
+ // Sentence count
+ const sentences = text.split(/[.!?]+/).filter(sentence => sentence.trim().length > 0);
+ document.getElementById('sentenceCount').textContent = sentences.length;
+
+ // Reading level (simplified Flesch-Kincaid)
+ if (words.length > 0 && sentences.length > 0) {
+ const avgWordsPerSentence = words.length / sentences.length;
+ const avgSyllablesPerWord = words.reduce((sum, word) => {
+ return sum + countSyllables(word);
+ }, 0) / words.length;
+
+ const readingLevel = Math.max(1, Math.min(12,
+ Math.round(0.39 * avgWordsPerSentence + 11.8 * avgSyllablesPerWord - 15.59)
+ ));
+ document.getElementById('readingLevel').textContent = `Grade ${readingLevel}`;
+ }
+
+ // Save content
+ localStorage.setItem('writeflow-content', text);
+ }
+
+ function updateTypeSpeed() {
+ const now = Date.now();
+ const timeDiff = (now - lastKeyPress) / 1000; // seconds
+ if (timeDiff < 5) { // Only update if actively typing
+ typeSpeed = Math.round((wordCount / timeDiff) * 60);
+ document.getElementById('typeSpeed').textContent = Math.min(typeSpeed, 999);
+ }
+ }
+
+ function countSyllables(word) {
+ word = word.toLowerCase();
+ if (word.length <= 3) return 1;
+ return word.replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '')
+ .match(/[aeiouy]{1,2}/g)?.length || 1;
+ }
+
+ editor.addEventListener('input', () => {
+ updateStats();
+ lastKeyPress = Date.now();
+ });
+
+ setInterval(updateTypeSpeed, 1000);
+
+ darkModeToggle.addEventListener('click', () => {
+ document.body.classList.toggle('dark-mode');
+ localStorage.setItem('writeflow-dark-mode', document.body.classList.contains('dark-mode'));
+ });
+
+ // Initial stats update
+ updateStats();
+ });