Spaces:
Runtime error
Runtime error
File size: 8,803 Bytes
05b45a5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
export default class TextEditor {
constructor(container, options = {}) {
this.options = {
charsPerPage: 500, // Default to 500 chars per page
onTextChange: null,
...options
};
this.container = container;
this.currentPage = 1;
this.pages = [''];
this.charCount = 0;
this.fullText = '';
this.isTyping = false;
this.setupDOM();
this.bindEvents();
}
setupDOM() {
this.container.innerHTML = `
<div class="text-editor">
<div class="editor-view">
<div class="page-navigation">
<div class="pagination">
<button class="prev-btn">← Previous</button>
<span class="page-info">Page 1 of 1</span>
<button class="next-btn">Next →</button>
</div>
</div>
<textarea
class="page-content"
placeholder="Enter text to convert to speech..."
></textarea>
<div class="editor-footer">
<div class="file-controls">
<input type="file" class="file-input" accept=".txt" style="display: none;">
<button class="upload-btn">Upload Text</button>
<button class="clear-btn">Clear Text</button>
</div>
<div class="chars-per-page">
<input
type="number"
class="chars-input"
value="500"
min="100"
max="2000"
title="Characters per page"
>
<span class="chars-label">chars/page</span>
<button class="format-btn">Format Pages</button>
</div>
<div class="char-count">0 characters</div>
</div>
</div>
</div>
`;
// Cache DOM elements
this.elements = {
pageContent: this.container.querySelector('.page-content'),
prevBtn: this.container.querySelector('.prev-btn'),
nextBtn: this.container.querySelector('.next-btn'),
pageInfo: this.container.querySelector('.page-info'),
fileInput: this.container.querySelector('.file-input'),
uploadBtn: this.container.querySelector('.upload-btn'),
clearBtn: this.container.querySelector('.clear-btn'),
charCount: this.container.querySelector('.char-count'),
charsPerPage: this.container.querySelector('.chars-input'),
formatBtn: this.container.querySelector('.format-btn')
};
// Set initial chars per page value
this.elements.charsPerPage.value = this.options.charsPerPage;
}
bindEvents() {
// Handle page content changes
this.elements.pageContent.addEventListener('input', (e) => {
const newContent = e.target.value;
this.pages[this.currentPage - 1] = newContent;
// Only handle empty pages, otherwise just update the text
if (!newContent.trim() && this.pages.length > 1) {
// Remove the empty page and adjust
this.pages.splice(this.currentPage - 1, 1);
this.currentPage = Math.min(this.currentPage, this.pages.length);
this.updatePageDisplay();
}
// Update full text and char count - join with space since pages are just for UI
this.fullText = this.pages.join(' ');
this.updateCharCount();
if (this.options.onTextChange) {
this.options.onTextChange(this.fullText);
}
});
// Navigation
this.elements.prevBtn.addEventListener('click', () => {
if (this.currentPage > 1) {
this.currentPage--;
this.updatePageDisplay();
}
});
this.elements.nextBtn.addEventListener('click', () => {
if (this.currentPage < this.pages.length) {
this.currentPage++;
this.updatePageDisplay();
}
});
// File upload
this.elements.uploadBtn.addEventListener('click', () => {
this.elements.fileInput.click();
});
this.elements.fileInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
this.setText(e.target.result);
if (this.options.onTextChange) {
this.options.onTextChange(this.getText());
}
};
reader.readAsText(file);
}
});
// Clear text
this.elements.clearBtn.addEventListener('click', () => {
this.clear();
if (this.options.onTextChange) {
this.options.onTextChange('');
}
});
// Cache format button
this.elements.formatBtn = this.container.querySelector('.format-btn');
// Characters per page control - just update the value
this.elements.charsPerPage.addEventListener('change', (e) => {
const value = parseInt(e.target.value);
if (value >= 100 && value <= 2000) {
this.options.charsPerPage = value;
}
});
// Format pages button - trigger the split
this.elements.formatBtn.addEventListener('click', () => {
const value = parseInt(this.elements.charsPerPage.value);
if (value >= 100 && value <= 2000) {
this.options.charsPerPage = value;
this.splitIntoPages(this.fullText);
}
});
}
splitIntoPages(text) {
if (!text || !text.trim()) {
this.pages = [''];
this.fullText = '';
this.currentPage = 1;
this.updatePageDisplay();
this.updateCharCount();
return;
}
// Store original text to preserve natural line breaks
this.fullText = text.trim();
const words = text.trim().split(/\s+/);
this.pages = [];
let currentPage = '';
for (let i = 0; i < words.length; i++) {
const word = words[i];
const potentialPage = currentPage + (currentPage ? ' ' : '') + word;
if (potentialPage.length >= this.options.charsPerPage && currentPage) {
this.pages.push(currentPage);
currentPage = word;
} else {
currentPage = potentialPage;
}
}
if (currentPage) {
this.pages.push(currentPage);
}
if (this.pages.length === 0) {
this.pages = [''];
this.currentPage = 1;
} else {
// Keep current page in bounds
this.currentPage = Math.min(this.currentPage, this.pages.length);
}
this.updatePageDisplay();
this.updateCharCount();
}
setText(text) {
// Just set the text without splitting into pages
this.fullText = text;
this.pages = [text];
this.currentPage = 1;
this.updatePageDisplay();
this.updateCharCount();
}
updatePageDisplay() {
this.elements.pageContent.value = this.pages[this.currentPage - 1] || '';
this.elements.pageInfo.textContent = `Page ${this.currentPage} of ${this.pages.length}`;
// Update button states
this.elements.prevBtn.disabled = this.currentPage === 1;
this.elements.nextBtn.disabled = this.currentPage === this.pages.length;
}
updateCharCount() {
const totalChars = this.fullText.length;
this.elements.charCount.textContent = `${totalChars} characters`;
}
prevPage() {
if (this.currentPage > 1) {
this.currentPage--;
this.updatePageDisplay();
}
}
nextPage() {
if (this.currentPage < this.pages.length) {
this.currentPage++;
this.updatePageDisplay();
}
}
getText() {
return this.fullText;
}
clear() {
this.setText('');
}
} |