diff --git a/README.md b/README.md index 520a16c..e5a52cf 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ side-by-side work: MarkPad file search sidebar with Open files selected: a search for open file finds three matching lines across two unsaved drafts beside the editor and preview +- **Document outline.** Open the outline sidebar from the toolbar or with `Ctrl/⌘+Shift+O` to browse every heading of the current Markdown file as an indented table of contents that updates as you type. Clicking a heading jumps straight to it — the editor puts it at the top of the view, and the preview follows in split and preview modes. - **In-document link navigation.** Headings get anchor ids, so clicking an in-page link in the preview — like a table of contents `[Section](#section)` — smooth-scrolls to that heading within the preview pane. - **Links open where they belong.** Web and mail links in the preview open in your default browser or mail app, and links to other Markdown, JSON, or YAML files (`[Guide](docs/guide.md)`) open them in markpad, resolved relative to the current file. The markpad window itself only ever shows markpad: links, forms, and pop-ups can never turn it into a browser. - **Diagrams from fenced code.** A ` ```mermaid ` block renders as a diagram in the preview — flowcharts, sequence, class, state, ER, gantt, pie, mindmap, timeline, git graphs and the rest of [Mermaid](https://mermaid.js.org/)'s catalogue — and ` ```dot ` (or `graphviz`, `gv`) renders [Graphviz](https://graphviz.org/) DOT source. Both engines run entirely on your machine, follow the app's light/dark theme, and load only when a document actually has a diagram in it. Source that does not parse shows the engine's message inline with the block, so a half-typed diagram never blanks the preview. diff --git a/src/App.tsx b/src/App.tsx index 6b9d1e1..ece6d72 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -12,6 +12,8 @@ import ErrorBanner from "./components/ErrorBanner"; import ExternalChangeBanner from "./components/ExternalChangeBanner"; import RecentsPanel, { type RecentEntry } from "./components/RecentsPanel"; import WorkspaceSearchPanel from "./components/WorkspaceSearchPanel"; +import OutlinePanel from "./components/OutlinePanel"; +import { markdownHeadings } from "./lib/markdown"; import ConfirmDialog from "./components/ConfirmDialog"; import type { EditorHandle } from "./components/Editor"; import type { PreviewHandle } from "./components/Preview"; @@ -111,7 +113,7 @@ type ItemSnapshot = { previewScrollTop: number; }; -type SidebarView = "recents" | "workspaceSearch"; +type SidebarView = "recents" | "workspaceSearch" | "outline"; type PendingLocation = { itemId: ItemId; @@ -276,6 +278,13 @@ function App() { const searchOpen = activeId !== null && searchItemId === activeId; const workspaceSearchOpen = !sidebarCollapsed && sidebarView === "workspaceSearch"; + const outlineOpen = !sidebarCollapsed && sidebarView === "outline"; + const activeIsMarkdown = activeItem !== null && activeLanguage === "markdown"; + // Parsed only while the outline is on screen; it follows every keystroke. + const outlineHeadings = useMemo( + () => (outlineOpen && activeIsMarkdown ? markdownHeadings(activeText) : []), + [outlineOpen, activeIsMarkdown, activeText], + ); const recentEntries = useMemo( () => @@ -1115,6 +1124,29 @@ function App() { setSidebarView("recents"); } + function handleToggleOutline() { + if (outlineOpen) { + setSidebarView("recents"); + return; + } + setSidebarView("outline"); + if (sidebarCollapsed) { + setSidebarCollapsed(false); + persistSidebarCollapsed(false); + } + } + + // Jump to a heading picked in the outline, placing it at the top of the view + // so its section reads downward. In split view the preview follows the + // editor through scroll sync. + function handleOutlineSelect(line: number) { + if (viewMode === "preview") { + previewRef.current?.scrollToSyncLine(line); + } else { + editorRef.current?.revealLine(line, "start"); + } + } + async function handleChooseWorkspaceRoot() { const result = await openWorkspaceFolder(); if (result.kind === "cancelled") return; @@ -1283,6 +1315,7 @@ function App() { const handleToggleSidebarRef = useRef(handleToggleSidebar); const handleOpenSearchRef = useRef(handleOpenSearch); const handleOpenWorkspaceSearchRef = useRef(handleOpenWorkspaceSearch); + const handleToggleOutlineRef = useRef(handleToggleOutline); const handleCloseSearchRef = useRef(handleCloseSearch); const checkAgainstDiskRef = useRef(checkOpenFilesAgainstDisk); @@ -1293,6 +1326,7 @@ function App() { handleToggleSidebarRef.current = handleToggleSidebar; handleOpenSearchRef.current = handleOpenSearch; handleOpenWorkspaceSearchRef.current = handleOpenWorkspaceSearch; + handleToggleOutlineRef.current = handleToggleOutline; handleCloseSearchRef.current = handleCloseSearch; checkAgainstDiskRef.current = checkOpenFilesAgainstDisk; }); @@ -1331,6 +1365,12 @@ function App() { return; } const mod = e.ctrlKey || e.metaKey; + if (mod && e.shiftKey && !e.altKey && e.key.toLowerCase() === "o") { + e.preventDefault(); + e.stopPropagation(); + handleToggleOutlineRef.current(); + return; + } if (!mod || e.shiftKey || e.altKey) return; const key = e.key.toLowerCase(); if (key === "s") { @@ -1388,6 +1428,14 @@ function App() { onRemove={handleRemove} onRemoveMany={handleRemoveMany} /> + ) : sidebarView === "outline" ? ( + setSidebarView("recents")} + /> ) : ( (function Editor( return view.state.sliceDoc(from, to); }, focus: () => viewRef.current?.focus(), - revealLine: (lineNumber) => { + revealLine: (lineNumber, align = "center") => { const view = viewRef.current; if (!view) return; const line = view.state.doc.line( @@ -438,7 +441,7 @@ const Editor = forwardRef(function Editor( ); view.dispatch({ selection: { anchor: line.from }, - effects: EditorView.scrollIntoView(line.from, { y: "center" }), + effects: EditorView.scrollIntoView(line.from, { y: align }), }); view.focus(); }, diff --git a/src/components/OutlinePanel.tsx b/src/components/OutlinePanel.tsx new file mode 100644 index 0000000..1ddcd9f --- /dev/null +++ b/src/components/OutlinePanel.tsx @@ -0,0 +1,153 @@ +import type { Heading } from "../lib/markdown"; + +type OutlinePanelProps = { + /** Name of the active document, or null when nothing is open. */ + fileName: string | null; + /** Only Markdown has headings; JSON and YAML get an explanation instead. */ + isMarkdown: boolean; + headings: Heading[]; + onSelect: (line: number) => void; + onClose: () => void; +}; + +const iconButton = + "inline-flex size-7 items-center justify-center rounded-md text-[color:var(--muted)] transition-colors hover:bg-[color:var(--hover)] hover:text-[color:var(--text)] focus:outline-none focus-visible:ring-2 focus-visible:ring-[color:var(--accent)]"; + +// Each level below the document's shallowest heading indents by this much, so a +// document that starts at `##` is not pushed in by an empty first column. +const INDENT_REM = 0.75; + +function OutlineIcon({ size = 18 }: { size?: number }) { + return ( + + ); +} + +function CloseIcon() { + return ( + + ); +} + +function EmptyState({ title, detail }: { title: string; detail: string }) { + return ( +
+
+ +
+

{title}

+

+ {detail} +

+
+ ); +} + +export default function OutlinePanel({ + fileName, + isMarkdown, + headings, + onSelect, + onClose, +}: OutlinePanelProps) { + const topLevel = Math.min(...headings.map((heading) => heading.level)); + + return ( +
+
+
+ +
+
+

+ Outline +

+

+ {fileName ?? "No document"} +

+
+ +
+ +
+ {fileName === null ? ( + + ) : !isMarkdown ? ( + + ) : headings.length === 0 ? ( + + ) : ( + + )} +
+
+ ); +} diff --git a/src/components/Toolbar.tsx b/src/components/Toolbar.tsx index 82bf6f5..6eb59fd 100644 --- a/src/components/Toolbar.tsx +++ b/src/components/Toolbar.tsx @@ -12,6 +12,7 @@ type ToolbarProps = { sidebarCollapsed: boolean; searchEnabled: boolean; workspaceSearchOpen: boolean; + outlineOpen: boolean; modKey: string; onToggleSidebar: () => void; onNewFile: () => void; @@ -19,6 +20,7 @@ type ToolbarProps = { onSave: () => void; onFind: () => void; onSearchFiles: () => void; + onToggleOutline: () => void; onToggleAutoSave: (next: boolean) => void; onSetViewMode: (mode: ViewMode) => void; onToggleTheme: () => void; @@ -197,6 +199,24 @@ function SearchFilesIcon() { ); } +function OutlineIcon() { + return ( + + ); +} + const segments: ReadonlyArray<{ mode: ViewMode; label: string }> = [ { mode: "editor", label: "Editor" }, { mode: "split", label: "Split" }, @@ -218,6 +238,7 @@ export default function Toolbar({ sidebarCollapsed, searchEnabled, workspaceSearchOpen, + outlineOpen, modKey, onToggleSidebar, onNewFile, @@ -225,6 +246,7 @@ export default function Toolbar({ onSave, onFind, onSearchFiles, + onToggleOutline, onToggleAutoSave, onSetViewMode, onToggleTheme, @@ -262,6 +284,21 @@ export default function Toolbar({ > +