mirror of
https://github.com/outline/outline.git
synced 2026-12-01 01:14:11 +03:00
* 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>
118 lines
3.2 KiB
TypeScript
118 lines
3.2 KiB
TypeScript
import { action, computed, observable } from "mobx";
|
|
import type { PropsWithChildren } from "react";
|
|
import { createContext, useContext, useMemo } from "react";
|
|
import type { Node } from "prosemirror-model";
|
|
import { ProsemirrorHelper } from "@shared/utils/ProsemirrorHelper";
|
|
import type { Heading } from "@shared/utils/ProsemirrorHelper";
|
|
import type { TextStats } from "~/hooks/useTextStats";
|
|
import { getTextStats } from "~/hooks/useTextStats";
|
|
import type Document from "~/models/Document";
|
|
import type { Editor } from "~/editor";
|
|
|
|
class DocumentContext {
|
|
/** The current document */
|
|
document?: Document;
|
|
|
|
/** The editor instance for this document */
|
|
editor?: Editor;
|
|
|
|
/** The ID of the currently focused comment, or null if no comment is focused */
|
|
@observable
|
|
focusedCommentId: string | null = null;
|
|
|
|
/** Whether the editor has been initialized */
|
|
@observable
|
|
isEditorInitialized: boolean = false;
|
|
|
|
/** The headings in the document */
|
|
@observable
|
|
headings: Heading[] = [];
|
|
|
|
@computed
|
|
get hasHeadings() {
|
|
return this.headings.length > 0;
|
|
}
|
|
|
|
/** Statistics for the text content of the document, kept up to date as it is edited */
|
|
@computed
|
|
get stats(): TextStats {
|
|
return getTextStats(
|
|
this.editorDoc ? ProsemirrorHelper.toPlainText(this.editorDoc) : ""
|
|
);
|
|
}
|
|
|
|
@action
|
|
setDocument = (document: Document) => {
|
|
// Reset the focused comment when navigating between documents
|
|
if (this.document && this.document.id !== document.id) {
|
|
this.focusedCommentId = null;
|
|
}
|
|
this.document = document;
|
|
this.updateState();
|
|
};
|
|
|
|
@action
|
|
setEditor = (editor: Editor) => {
|
|
this.editor = editor;
|
|
this.updateState();
|
|
};
|
|
|
|
@action
|
|
setEditorInitialized = (initialized: boolean) => {
|
|
this.isEditorInitialized = initialized;
|
|
};
|
|
|
|
@action
|
|
setFocusedCommentId = (commentId: string | null) => {
|
|
this.focusedCommentId = commentId;
|
|
};
|
|
|
|
@action
|
|
updateState = () => {
|
|
this.editorDoc = this.editor?.view.state.doc;
|
|
this.updateHeadings();
|
|
this.updateTasks();
|
|
};
|
|
|
|
/** The ProseMirror document currently held by the editor */
|
|
@observable.ref
|
|
private editorDoc: Node | undefined;
|
|
|
|
private updateHeadings() {
|
|
const currHeadings = this.editor?.getHeadings() ?? [];
|
|
const hasChanged =
|
|
currHeadings.map((h) => h.level + h.title).join("") !==
|
|
this.headings.map((h) => h.level + h.title).join("");
|
|
|
|
if (hasChanged) {
|
|
this.headings = currHeadings;
|
|
}
|
|
}
|
|
|
|
private updateTasks() {
|
|
const tasks = this.editor?.getTasks() ?? [];
|
|
const total = tasks.length ?? 0;
|
|
const completed = tasks.filter((t) => t.completed).length ?? 0;
|
|
this.document?.updateTasks(total, completed);
|
|
}
|
|
}
|
|
|
|
const Context = createContext<DocumentContext | null>(null);
|
|
|
|
export const useDocumentContext = () => {
|
|
const ctx = useContext(Context);
|
|
if (!ctx) {
|
|
throw new Error(
|
|
"useDocumentContext must be used within DocumentContextProvider"
|
|
);
|
|
}
|
|
return ctx;
|
|
};
|
|
|
|
export const DocumentContextProvider = ({
|
|
children,
|
|
}: PropsWithChildren<unknown>) => {
|
|
const context = useMemo(() => new DocumentContext(), []);
|
|
return <Context.Provider value={context}>{children}</Context.Provider>;
|
|
};
|