Files
outline/app/hooks/useTextStats.ts
Tom MoorandClaude Opus 5 6d97282045 Live document stats in the corner of the editor (#13318)
* feat: Live document stats in the corner of the editor

Moves word and character counts out of the Insights dialog into an
optional bar in the bottom corner of the document that updates as you
type, adding paragraph count and reading time.

Enabled with a new "Show editing stats" preference, or toggled with
Cmd+Shift+G. The counts switch to the selection while text is selected,
and fade out while idle.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix: Stats bar should not intercept clicks in the editor

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* Add toggle to document menu and command menu

* fix: Empty documents should not report a one minute read

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-08 12:07:55 -04:00

40 lines
983 B
TypeScript

export type TextStats = ReturnType<typeof getTextStats>;
/**
* Calculate statistics for a piece of text.
*
* @param text The string to calculate statistics for
* @returns An object containing the statistics
*/
export function getTextStats(text: string) {
const words = countWords(text);
return {
words,
characters: text.length,
paragraphs: countParagraphs(text),
readingTime: words ? Math.max(1, Math.floor(words / 200)) : 0,
};
}
/**
* Hook to calculate text statistics
*
* @param text The string to calculate statistics for
* @returns An object containing the statistics
*/
export function useTextStats(text: string) {
return getTextStats(text);
}
function countWords(text: string): number {
const t = text.trim();
// Hyphenated words are counted as two words
return t ? t.replace(/-/g, " ").split(/\s+/g).length : 0;
}
function countParagraphs(text: string): number {
return text.split("\n").filter((line) => line.trim()).length;
}