Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

feat(web): add fixed editor outline

tianma-if4e0d814ada18
diff --git a/apps/web/src/components/EditorOutline.tsx b/apps/web/src/components/EditorOutline.tsx
new file mode 100644
index 00000000..8f772a37
--- /dev/null
+++ b/apps/web/src/components/EditorOutline.tsx
@@ -0,0 +1,166 @@
+import { useCallback, useEffect, useState } from "react";
+import type { Editor } from "@tiptap/react";
+import { ChevronDown, ListTree } from "lucide-react";
+import { useTranslation } from "react-i18next";
+import { cn } from "@/lib/utils";
+
+type OutlineItem = {
+ level: number;
+ pos: number;
+ text: string;
+};
+
+type EditorOutlineProps = {
+ editor: Editor | null;
+ scrollContainer: HTMLDivElement | null;
+};
+
+const getOutlineItems = (editor: Editor): OutlineItem[] => {
+ const items: OutlineItem[] = [];
+
+ editor.state.doc.descendants((node, pos) => {
+ if (node.type.name !== "heading") {
+ return;
+ }
+
+ const text = node.textContent.trim();
+ if (text) {
+ items.push({
+ level: Number(node.attrs.level) || 1,
+ pos,
+ text,
+ });
+ }
+ });
+
+ return items;
+};
+
+export const EditorOutline = ({ editor, scrollContainer }: EditorOutlineProps) => {
+ const { t } = useTranslation();
+ const [items, setItems] = useState<OutlineItem[]>([]);
+ const [collapsed, setCollapsed] = useState(false);
+ const [activePos, setActivePos] = useState<number | null>(null);
+
+ const refresh = useCallback(() => {
+ if (!editor || editor.isDestroyed) {
+ setItems([]);
+ return;
+ }
+
+ setItems(getOutlineItems(editor));
+ }, [editor]);
+
+ const updateActiveItem = useCallback(() => {
+ if (!editor || editor.isDestroyed || items.length === 0) {
+ setActivePos(null);
+ return;
+ }
+
+ const selectionPos = editor.state.selection.from;
+ const activeItem = items.reduce<OutlineItem | null>((current, item) => (
+ item.pos <= selectionPos ? item : current
+ ), null);
+
+ setActivePos(activeItem?.pos ?? items[0]?.pos ?? null);
+ }, [editor, items]);
+
+ useEffect(() => {
+ refresh();
+ if (!editor) {
+ return;
+ }
+
+ editor.on("update", refresh);
+ editor.on("selectionUpdate", updateActiveItem);
+ return () => {
+ editor.off("update", refresh);
+ editor.off("selectionUpdate", updateActiveItem);
+ };
+ }, [editor, refresh, updateActiveItem]);
+
+ useEffect(() => {
+ updateActiveItem();
+ }, [updateActiveItem]);
+
+ useEffect(() => {
+ if (!scrollContainer || items.length === 0) {
+ return;
+ }
+
+ const updateFromScroll = () => {
+ const threshold = scrollContainer.getBoundingClientRect().top + 96;
+ let activeItem: OutlineItem | null = null;
+
+ for (const item of items) {
+ const element = editor?.view.nodeDOM(item.pos);
+ if (element instanceof HTMLElement && element.getBoundingClientRect().top <= threshold) {
+ activeItem = item;
+ }
+ }
+
+ if (activeItem) {
+ setActivePos(activeItem.pos);
+ }
+ };
+
+ scrollContainer.addEventListener("scroll", updateFromScroll, { passive: true });
+ updateFromScroll();
+ return () => scrollContainer.removeEventListener("scroll", updateFromScroll);
+ }, [editor, items, scrollContainer]);
+
+ const jumpToHeading = (item: OutlineItem) => {
+ if (!editor || editor.isDestroyed) {
+ return;
+ }
+
+ editor.chain().focus().setTextSelection(item.pos + 1).scrollIntoView().run();
+ setActivePos(item.pos);
+ };
+
+ if (items.length === 0) {
+ return null;
+ }
+
+ return (
+ <aside className="sticky top-0 h-fit max-h-[calc(100vh-8rem)] w-56 shrink-0 overflow-y-auto border-r border-slate-200 bg-slate-50/80 px-4 py-5" aria-label={t("editor.outline")}>
+ <div className="flex items-center gap-2">
+ <ListTree className="h-4 w-4 shrink-0 text-slate-500" aria-hidden="true" />
+ <button
+ type="button"
+ className="flex min-w-0 flex-1 items-center gap-2 text-left text-xs font-semibold uppercase tracking-wide text-slate-600"
+ onClick={() => setCollapsed((value) => !value)}
+ aria-expanded={!collapsed}
+ >
+ <span>{t("editor.outline")}</span>
+ <ChevronDown className={cn("h-3.5 w-3.5 transition-transform", collapsed && "-rotate-90")} aria-hidden="true" />
+ </button>
+ <span className="text-xs text-slate-400">{items.length}</span>
+ </div>
+ {!collapsed && (
+ <nav className="mt-3" aria-label={t("editor.outline")}>
+ <ol className="space-y-0.5">
+ {items.map((item) => (
+ <li key={item.pos}>
+ <button
+ type="button"
+ className={cn(
+ "block w-full truncate rounded px-2 py-1 text-left text-sm transition-colors",
+ activePos === item.pos
+ ? "bg-emerald-100 font-medium text-emerald-800"
+ : "text-slate-600 hover:bg-slate-100 hover:text-slate-900"
+ )}
+ style={{ paddingLeft: `${8 + Math.max(0, item.level - 1) * 14}px` }}
+ onClick={() => jumpToHeading(item)}
+ title={item.text}
+ >
+ {item.text}
+ </button>
+ </li>
+ ))}
+ </ol>
+ </nav>
+ )}
+ </aside>
+ );
+};
diff --git a/apps/web/src/components/EditorPane.tsx b/apps/web/src/components/EditorPane.tsx
index 7212dea7..6d08000f 100644
--- a/apps/web/src/components/EditorPane.tsx
+++ b/apps/web/src/components/EditorPane.tsx
@@ -59,6 +59,7 @@ import {
import { Select, SelectTrigger, SelectValue, SelectContent, SelectItem } from "@/components/ui/select";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
import { EditorToolbar } from "./EditorToolbar";
+import { EditorOutline } from "./EditorOutline";
import { WeChatIcon } from "./WeChatIcon";
import { ThemeToggle } from "./ThemeToggle";
import { useTheme } from "./ThemeProvider";
@@ -1169,6 +1170,11 @@ const RichEditorPane = ({
const [mobileImeDebugActiveElement, setMobileImeDebugActiveElement] = useState(getActiveElementLabel);
const [mobileImeDebugEvents, setMobileImeDebugEvents] = useState<MobileImeDebugEntry[]>([]);
const [wechatCopyState, setWechatCopyState] = useState<"idle" | "copying" | "copied" | "error">("idle");
+ const [editorScrollContainer, setEditorScrollContainer] = useState<HTMLDivElement | null>(null);
+ const setEditorScrollContainerRef = useCallback((element: HTMLDivElement | null) => {
+ editorScrollContainerRef.current = element;
+ setEditorScrollContainer(element);
+ }, []);
const notebookOptions = useMemo(() => getNotebookMoveOptions(notebooks), [notebooks]);
const readOnly = isTrashView || Boolean(memo?.isDeleted);
const mobileDefaultEditRequested = Boolean(memo?.id && memo.id === mobileDefaultEditMemoId && !readOnly);
@@ -2947,7 +2953,7 @@ const RichEditorPane = ({
</header>
<div
- ref={editorScrollContainerRef}
+ ref={setEditorScrollContainerRef}
data-editor-theme={editorTheme}
style={editorTheme === "custom" ? {
"--editor-theme-bg": customEditorTheme.background,
@@ -2962,56 +2968,63 @@ const RichEditorPane = ({
useMobilePlainTextEditor ? "overflow-visible" : "overflow-y-auto"
)}
>
- {useMobilePlainTextEditor ? (
- <>
- <textarea
- ref={(element) => {
- mobileTextAreaRef.current = element;
- }}
- defaultValue={mobilePlainText}
- autoCapitalize="sentences"
- autoComplete="on"
- autoCorrect="on"
- enterKeyHint="enter"
- inputMode="text"
- name="memo-body"
- spellCheck
- data-edgeever-mobile-editor="plain-textarea"
- aria-label={t("editor.noteBodyAria")}
- className="block min-h-[60dvh] w-full resize-none border border-slate-200 bg-white px-4 py-3 pr-32 text-base leading-7 text-slate-950 outline-none placeholder:text-slate-400 sm:px-7"
- placeholder={t("editor.placeholder")}
- style={{ WebkitUserSelect: "text", userSelect: "text", caretColor: "auto" }}
- />
- <div className="absolute right-3 top-3 flex gap-2">
- <button
- className="rounded-full border border-emerald-200 bg-emerald-50 px-3 py-1.5 text-sm font-semibold text-emerald-800 shadow-sm"
- type="button"
- onClick={() => void handleMobileClipboardInput()}
- >
- {t("editor.paste")}
- </button>
- <button
- className="rounded-full border border-slate-200 bg-white px-3 py-1.5 text-sm font-semibold text-slate-700 shadow-sm"
- type="button"
- onClick={handleMobilePromptInput}
- >
- {t("editor.typeInput")}
- </button>
- </div>
- </>
- ) : useMarkdownSourceEditor ? (
- <textarea
- value={markdownSource}
- onChange={(event) => handleMarkdownSourceChange(event.target.value)}
- readOnly={effectiveReadOnly}
- spellCheck={false}
- aria-label={t("editor.markdownSourceAria")}
- className="block min-h-[300px] h-full w-full resize-none border-0 bg-slate-950 px-4 py-3 font-mono text-sm leading-6 text-slate-100 outline-none placeholder:text-slate-500 sm:px-7"
- placeholder={`# ${t("editor.placeholder")}`}
- />
- ) : (
- <EditorContent editor={editor} />
- )}
+ <div className="flex min-h-full items-stretch">
+ {!isMobileViewport && !useMobilePlainTextEditor && !useMarkdownSourceEditor && (
+ <EditorOutline editor={editor} scrollContainer={editorScrollContainer} />
+ )}
+ <div className="min-w-0 flex-1">
+ {useMobilePlainTextEditor ? (
+ <>
+ <textarea
+ ref={(element) => {
+ mobileTextAreaRef.current = element;
+ }}
+ defaultValue={mobilePlainText}
+ autoCapitalize="sentences"
+ autoComplete="on"
+ autoCorrect="on"
+ enterKeyHint="enter"
+ inputMode="text"
+ name="memo-body"
+ spellCheck
+ data-edgeever-mobile-editor="plain-textarea"
+ aria-label={t("editor.noteBodyAria")}
+ className="block min-h-[60dvh] w-full resize-none border border-slate-200 bg-white px-4 py-3 pr-32 text-base leading-7 text-slate-950 outline-none placeholder:text-slate-400 sm:px-7"
+ placeholder={t("editor.placeholder")}
+ style={{ WebkitUserSelect: "text", userSelect: "text", caretColor: "auto" }}
+ />
+ <div className="absolute right-3 top-3 flex gap-2">
+ <button
+ className="rounded-full border border-emerald-200 bg-emerald-50 px-3 py-1.5 text-sm font-semibold text-emerald-800 shadow-sm"
+ type="button"
+ onClick={() => void handleMobileClipboardInput()}
+ >
+ {t("editor.paste")}
+ </button>
+ <button
+ className="rounded-full border border-slate-200 bg-white px-3 py-1.5 text-sm font-semibold text-slate-700 shadow-sm"
+ type="button"
+ onClick={handleMobilePromptInput}
+ >
+ {t("editor.typeInput")}
+ </button>
+ </div>
+ </>
+ ) : useMarkdownSourceEditor ? (
+ <textarea
+ value={markdownSource}
+ onChange={(event) => handleMarkdownSourceChange(event.target.value)}
+ readOnly={effectiveReadOnly}
+ spellCheck={false}
+ aria-label={t("editor.markdownSourceAria")}
+ className="block min-h-[300px] h-full w-full resize-none border-0 bg-slate-950 px-4 py-3 font-mono text-sm leading-6 text-slate-100 outline-none placeholder:text-slate-500 sm:px-7"
+ placeholder={`# ${t("editor.placeholder")}`}
+ />
+ ) : (
+ <EditorContent editor={editor} />
+ )}
+ </div>
+ </div>
</div>
{false && useMobilePlainTextEditor && (
diff --git a/packages/shared/src/i18n/en-US.ts b/packages/shared/src/i18n/en-US.ts
index dc7edf8c..8d63ae2a 100644
--- a/packages/shared/src/i18n/en-US.ts
+++ b/packages/shared/src/i18n/en-US.ts
@@ -568,6 +568,7 @@ export const enUS = {
copyingToWeChat: "Preparing WeChat content",
copiedToWeChat: "Copied",
copyToWeChatFailed: "Copy failed",
+ outline: "Outline",
},
editorToolbar: {
toolbar: "Editor toolbar",
diff --git a/packages/shared/src/i18n/zh-CN.ts b/packages/shared/src/i18n/zh-CN.ts
index 1544d798..aa7f12bf 100644
--- a/packages/shared/src/i18n/zh-CN.ts
+++ b/packages/shared/src/i18n/zh-CN.ts
@@ -568,6 +568,7 @@ export const zhCN = {
copyingToWeChat: "正在准备公众号内容",
copiedToWeChat: "已复制",
copyToWeChatFailed: "复制失败",
+ outline: "文档大纲",
},
editorToolbar: {
toolbar: "编辑器工具栏",