|
| 1 | +import { useEffect, useMemo, useRef, useState, type KeyboardEvent } from "react"; |
| 2 | +import { useStore } from "@/store/useStore"; |
| 3 | +import { useT } from "@/lib/i18n"; |
| 4 | +import { getEditorView } from "@/lib/editorRef"; |
| 5 | +import { fuzzyFilter } from "@/lib/fuzzy"; |
| 6 | +import { parseOutline } from "@/lib/outline"; |
| 7 | + |
| 8 | +export function Outline() { |
| 9 | + const open = useStore((s) => s.outlineOpen); |
| 10 | + const setOpen = useStore((s) => s.setOutlineOpen); |
| 11 | + const activeTabId = useStore((s) => s.activeTabId); |
| 12 | + const content = useStore((s) => s.tabs.find((t) => t.id === s.activeTabId)?.content); |
| 13 | + const t = useT(); |
| 14 | + |
| 15 | + const [query, setQuery] = useState(""); |
| 16 | + const [index, setIndex] = useState(0); |
| 17 | + const inputRef = useRef<HTMLInputElement>(null); |
| 18 | + const listRef = useRef<HTMLDivElement>(null); |
| 19 | + |
| 20 | + // 打开时重置;tab 切换时保持打开并重置过滤(内容变化由 content 依赖驱动重解析) |
| 21 | + useEffect(() => { |
| 22 | + if (!open) return; |
| 23 | + setQuery(""); |
| 24 | + setIndex(0); |
| 25 | + requestAnimationFrame(() => inputRef.current?.focus()); |
| 26 | + }, [open, activeTabId]); |
| 27 | + |
| 28 | + const items = useMemo(() => parseOutline(content ?? ""), [content]); |
| 29 | + |
| 30 | + const filtered = useMemo( |
| 31 | + () => (query.trim() ? fuzzyFilter(items, query.trim(), (it) => it.text) : items), |
| 32 | + [items, query], |
| 33 | + ); |
| 34 | + |
| 35 | + useEffect(() => { |
| 36 | + setIndex(0); |
| 37 | + }, [query]); |
| 38 | + |
| 39 | + useEffect(() => { |
| 40 | + const el = listRef.current?.querySelector<HTMLElement>(`[data-oi="${index}"]`); |
| 41 | + el?.scrollIntoView({ block: "nearest" }); |
| 42 | + }, [index, filtered.length]); |
| 43 | + |
| 44 | + if (!open) return null; |
| 45 | + |
| 46 | + const close = () => { |
| 47 | + setOpen(false); |
| 48 | + getEditorView()?.focus(); |
| 49 | + }; |
| 50 | + |
| 51 | + const jump = (from: number) => { |
| 52 | + setOpen(false); |
| 53 | + requestAnimationFrame(() => { |
| 54 | + const v = getEditorView(); |
| 55 | + if (v) { |
| 56 | + const pos = Math.min(from, v.state.doc.length); |
| 57 | + v.dispatch({ selection: { anchor: pos }, scrollIntoView: true }); |
| 58 | + v.focus(); |
| 59 | + } |
| 60 | + }); |
| 61 | + }; |
| 62 | + |
| 63 | + const onKeyDown = (e: KeyboardEvent) => { |
| 64 | + if (e.key === "Escape") { |
| 65 | + e.preventDefault(); |
| 66 | + close(); |
| 67 | + return; |
| 68 | + } |
| 69 | + if (e.key === "ArrowDown") { |
| 70 | + e.preventDefault(); |
| 71 | + if (!filtered.length) return; |
| 72 | + setIndex((i) => (i + 1) % filtered.length); |
| 73 | + return; |
| 74 | + } |
| 75 | + if (e.key === "ArrowUp") { |
| 76 | + e.preventDefault(); |
| 77 | + if (!filtered.length) return; |
| 78 | + setIndex((i) => (i - 1 + filtered.length) % filtered.length); |
| 79 | + return; |
| 80 | + } |
| 81 | + if (e.key === "Enter") { |
| 82 | + e.preventDefault(); |
| 83 | + const it = filtered[index]; |
| 84 | + if (it) jump(it.from); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + return ( |
| 89 | + <div |
| 90 | + className="overlay quick-open-overlay outline-overlay" |
| 91 | + onMouseDown={(e) => e.target === e.currentTarget && close()} |
| 92 | + > |
| 93 | + <div className="quick-open outline-panel" role="dialog" aria-label={t("outline.title")}> |
| 94 | + <input |
| 95 | + ref={inputRef} |
| 96 | + className="quick-open-input" |
| 97 | + value={query} |
| 98 | + onChange={(e) => setQuery(e.target.value)} |
| 99 | + placeholder={t("outline.placeholder")} |
| 100 | + onKeyDown={onKeyDown} |
| 101 | + spellCheck={false} |
| 102 | + aria-autocomplete="list" |
| 103 | + aria-controls="outline-list" |
| 104 | + /> |
| 105 | + <div className="quick-open-list" id="outline-list" ref={listRef} role="listbox"> |
| 106 | + {filtered.length === 0 ? ( |
| 107 | + <div className="quick-open-empty muted"> |
| 108 | + {items.length === 0 ? t("outline.noHeadings") : t("quickOpen.empty")} |
| 109 | + </div> |
| 110 | + ) : ( |
| 111 | + filtered.map((it, i) => ( |
| 112 | + <div |
| 113 | + key={`${it.line}-${it.from}`} |
| 114 | + data-oi={i} |
| 115 | + role="option" |
| 116 | + aria-selected={i === index} |
| 117 | + className={"quick-open-row outline-row" + (i === index ? " active" : "")} |
| 118 | + style={{ paddingLeft: 10 + (it.level - 1) * 14 }} |
| 119 | + onMouseEnter={() => setIndex(i)} |
| 120 | + onClick={() => jump(it.from)} |
| 121 | + > |
| 122 | + <span className="outline-badge">H{it.level}</span> |
| 123 | + <span className="quick-open-label">{it.text}</span> |
| 124 | + <span className="quick-open-detail muted">L{it.line}</span> |
| 125 | + </div> |
| 126 | + )) |
| 127 | + )} |
| 128 | + </div> |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + ); |
| 132 | +} |
0 commit comments