|
388 | 388 | return content.trim().split(/\s+/).filter(Boolean).length; |
389 | 389 | } |
390 | 390 |
|
| 391 | + getReadingTime(wordCount) { |
| 392 | + // Average reading speed: 200 words per minute |
| 393 | + const wordsPerMinute = 200; |
| 394 | + const minutes = Math.ceil(wordCount / wordsPerMinute); |
| 395 | + |
| 396 | + if (minutes === 0) { |
| 397 | + return '<1 min'; |
| 398 | + } else if (minutes < 60) { |
| 399 | + return `~${minutes} min${minutes !== 1 ? 's' : ''}`; |
| 400 | + } else if (minutes < 1440) { |
| 401 | + const hours = Math.ceil(minutes / 60); |
| 402 | + return `~${hours} hour${hours !== 1 ? 's' : ''}`; |
| 403 | + } else { |
| 404 | + const days = Math.ceil(minutes / 1440); |
| 405 | + return `~${days} day${days !== 1 ? 's' : ''}`; |
| 406 | + } |
| 407 | + } |
391 | 408 | updateCount() { |
392 | 409 | const text = this.editor.value; |
393 | | - let count, label; |
| 410 | + const wordCount = this.getWordsCount(text); |
| 411 | + let displayText; |
394 | 412 |
|
395 | 413 | if (this.countMode === 'words') { |
396 | | - count = this.getWordsCount(text); |
397 | | - label = `word${count !== 1 ? 's' : ''}`; |
| 414 | + displayText = `${wordCount} word${wordCount !== 1 ? 's' : ''}`; |
398 | 415 | } else { |
399 | | - count = text.length; |
400 | | - label = `character${count !== 1 ? 's' : ''}`; |
| 416 | + displayText = this.getReadingTime(wordCount); |
401 | 417 | } |
402 | 418 |
|
403 | | - this.countDisplay.textContent = `${count} ${label}`; |
| 419 | + this.countDisplay.textContent = displayText; |
404 | 420 | } |
405 | 421 |
|
406 | 422 | toggleCountMode() { |
407 | 423 | this.countDisplay.style.opacity = '0'; |
408 | 424 | setTimeout(() => { |
409 | | - this.countMode = this.countMode === 'words' ? 'characters' : 'words'; |
| 425 | + this.countMode = this.countMode === 'words' ? 'reading' : 'words'; |
410 | 426 | localStorage.setItem(SHIRO_COUNT_MODE, this.countMode); |
411 | 427 | this.updateCount(); |
412 | 428 | this.countDisplay.style.opacity = '1'; |
|
422 | 438 |
|
423 | 439 | show() { |
424 | 440 | if (!this.visible) { |
| 441 | + this.updateCount(); |
425 | 442 | this.controls.forEach(c => c.classList.remove('hidden')); |
426 | 443 | this.visible = true; |
427 | 444 | } |
|
0 commit comments