a6ff7623db
The editor lagged while typing (worse with doc size, and under collaboration the same cost is paid for every REMOTE keystroke). ProseMirror itself was fine — the overhead was the surrounding work done on every transaction. Behavior is 1:1; only WHEN work runs changed. - getJSON() off the keystroke path: `onUpdate` no longer serializes the whole doc synchronously — the serialization now runs inside a 3s debounce (new hook use-page-content-cache.ts), flushed on unmount so the last snapshot isn't lost. - footnote numbering: merged 3 per-docChanged O(n) doc walks into one, and short-circuit the whole-doc renumber when the doc has no footnotes and the transaction didn't insert one (step-slice scan — covers typing/paste/collab). - toolbar: replaced per-keystroke `editor.can().undo()/.redo()` dry-runs with cheap history-depth reads (Yjs undoManager stack length / pm-history depth). - render side-effect bug: `remote.attach()` moved out of the render body into a useEffect. - debounced the TOC all-headings rescan and memoized the slash-command suggestion build (was rebuilt twice per keystroke). - node menus (image/video/audio/pdf/callout/subpages): the per-transaction selectors early-return a cheap isActive check instead of running getAttributes + multiple alignment probes while their node type is inactive (shouldShow still controls display — appears exactly when it did). - code blocks: the global selectionUpdate listener is now added only for mermaid blocks (the only consumer of the selected state), eliminating N listeners + N setStates per caret move for normal code blocks. Deferred (documented, collab hot-path risk): full conditional menu MOUNTING (menu-less-frame risk on same-tx context switch) and code-block re-tokenization debounce / language-persist (self-dispatching meta tx + node-attr writes interact with collab/undo). The route split from #342 already keeps lowlight off startup. Gate: editor-ext build + 252/252 tests, client editor tests pass, tsc --noEmit 0, client build ok. New tests: footnote no-footnote-doc → 0 traversals + numbering unchanged; page-content-cache onUpdate-no-sync-getJSON + flush-on-unmount. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
131 lines
3.9 KiB
TypeScript
131 lines
3.9 KiB
TypeScript
import { BubbleMenu as BaseBubbleMenu } from "@tiptap/react/menus";
|
|
import { posToDOMRect, findParentNode, useEditorState } from "@tiptap/react";
|
|
import { Node as PMNode } from "@tiptap/pm/model";
|
|
import React, { useCallback } from "react";
|
|
import { ActionIcon, Group, Tooltip } from "@mantine/core";
|
|
import { IconTrash, IconList, IconSitemap } from "@tabler/icons-react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { Editor } from "@tiptap/core";
|
|
import { isEditorReady } from "@docmost/editor-ext";
|
|
|
|
interface SubpagesMenuProps {
|
|
editor: Editor;
|
|
}
|
|
|
|
interface ShouldShowProps {
|
|
state: any;
|
|
from?: number;
|
|
to?: number;
|
|
}
|
|
|
|
export const SubpagesMenu = React.memo(
|
|
({ editor }: SubpagesMenuProps): JSX.Element => {
|
|
const { t } = useTranslation();
|
|
|
|
const shouldShow = useCallback(
|
|
({ state }: ShouldShowProps) => {
|
|
if (!state) {
|
|
return false;
|
|
}
|
|
|
|
return editor.isActive("subpages");
|
|
},
|
|
[editor]
|
|
);
|
|
|
|
const getReferenceClientRect = useCallback(() => {
|
|
if (!isEditorReady(editor)) return new DOMRect();
|
|
const { selection } = editor.state;
|
|
const predicate = (node: PMNode) => node.type.name === "subpages";
|
|
const parent = findParentNode(predicate)(selection);
|
|
|
|
if (parent) {
|
|
const dom = editor.view.nodeDOM(parent?.pos) as HTMLElement;
|
|
return dom.getBoundingClientRect();
|
|
}
|
|
|
|
return posToDOMRect(editor.view, selection.from, selection.to);
|
|
}, [editor]);
|
|
|
|
const toggleRecursive = useCallback(() => {
|
|
const current = editor.getAttributes("subpages")?.recursive ?? false;
|
|
editor.commands.updateAttributes("subpages", {
|
|
recursive: !current,
|
|
});
|
|
}, [editor]);
|
|
|
|
const deleteNode = useCallback(() => {
|
|
const { selection } = editor.state;
|
|
editor
|
|
.chain()
|
|
.focus()
|
|
.setNodeSelection(selection.from)
|
|
.deleteSelection()
|
|
.run();
|
|
}, [editor]);
|
|
|
|
// Subscribe to the live `recursive` attribute the standard way (as the
|
|
// sibling bubble menus do): useEditorState re-renders only when the selected
|
|
// value actually changes, so the mode icon/tooltip stay current after a
|
|
// toggle without re-rendering on every keystroke.
|
|
const isRecursive = useEditorState({
|
|
editor,
|
|
// #343 PART 1: skip getAttributes unless a subpages node is active. The
|
|
// menu only shows for an active subpages node (shouldShow), so the value
|
|
// is only read then; getAttributes on an inactive node returns the default
|
|
// (recursive === false) anyway, so this is behavior-preserving.
|
|
selector: (ctx) =>
|
|
ctx.editor?.isActive("subpages")
|
|
? (ctx.editor.getAttributes("subpages")?.recursive ?? false)
|
|
: false,
|
|
});
|
|
|
|
return (
|
|
<BaseBubbleMenu
|
|
editor={editor}
|
|
pluginKey={`subpages-menu`}
|
|
updateDelay={0}
|
|
shouldShow={shouldShow}
|
|
>
|
|
<Group gap={4} wrap="nowrap">
|
|
<Tooltip
|
|
position="top"
|
|
label={
|
|
isRecursive
|
|
? t("Switch to flat list")
|
|
: t("Switch to tree")
|
|
}
|
|
>
|
|
<ActionIcon
|
|
onClick={toggleRecursive}
|
|
variant="default"
|
|
size="lg"
|
|
aria-label={t("Toggle subpages display mode")}
|
|
>
|
|
{isRecursive ? (
|
|
<IconList size={18} />
|
|
) : (
|
|
<IconSitemap size={18} />
|
|
)}
|
|
</ActionIcon>
|
|
</Tooltip>
|
|
|
|
<Tooltip position="top" label={t("Delete")}>
|
|
<ActionIcon
|
|
onClick={deleteNode}
|
|
variant="default"
|
|
size="lg"
|
|
color="red"
|
|
aria-label={t("Delete")}
|
|
>
|
|
<IconTrash size={18} />
|
|
</ActionIcon>
|
|
</Tooltip>
|
|
</Group>
|
|
</BaseBubbleMenu>
|
|
);
|
|
}
|
|
);
|
|
|
|
export default SubpagesMenu;
|