Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const editor = document.getElementById('editor');
+ const wordCount = document.getElementById('wordCount');
+ const charCount = document.getElementById('charCount');
+ const newBtn = document.getElementById('newBtn');
+ const downloadBtn = document.getElementById('downloadBtn');
+
+ function updateCounts() {
+ const text = editor.value;
+ const words = text.trim() ? text.trim().split(/\s+/).length : 0;
+ const chars = text.length;
+
+ wordCount.textContent = `${words} words`;
+ charCount.textContent = `${chars} characters`;
+ }
+
+ function createNewDocument() {
+ if (editor.value && !confirm('Discard current document?')) return;
+ editor.value = '';
+ updateCounts();
+ editor.focus();
+ }
+
+ function downloadText() {
+ if (!editor.value.trim()) return;
+
+ const blob = new Blob([editor.value], { type: 'text/plain' });
+ const url = URL.createObjectURL(blob);
+ const a = document.createElement('a');
+
+ a.href = url;
+ a.download = `document-${new Date().toISOString().slice(0,10)}.txt`;
+ document.body.appendChild(a);
+ a.click();
+ document.body.removeChild(a);
+ URL.revokeObjectURL(url);
+ }
+
+ // Auto-save functionality
+ function saveToLocalStorage() {
+ localStorage.setItem('ink-editor-content', editor.value);
+ }
+
+ function loadFromLocalStorage() {
+ const saved = localStorage.getItem('ink-editor-content');
+ if (saved) editor.value = saved;
+ updateCounts();
+ }
+
+ // Event listeners
+ editor.addEventListener('input', () => {
+ updateCounts();
+ saveToLocalStorage();
+ });
+
+ editor.addEventListener('keydown', (e) => {
+ if (e.key === 'Tab') {
+ e.preventDefault();
+ const start = editor.selectionStart;
+ const end = editor.selectionEnd;
+ editor.value = editor.value.substring(0, start) + ' ' + editor.value.substring(end);
+ editor.selectionStart = editor.selectionEnd = start + 2;
+ }
+ });
+
+ newBtn.addEventListener('click', createNewDocument);
+ downloadBtn.addEventListener('click', downloadText);
+
+ // Initial load
+ loadFromLocalStorage();
+ editor.focus();
+ });