@karimsa/mdxserve 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/README.md +80 -10
  2. package/client/App.tsx +15 -1
  3. package/client/CodeBlock.tsx +24 -0
  4. package/client/DocView.tsx +9 -2
  5. package/client/ListingView.tsx +14 -10
  6. package/client/MarkdownTable.tsx +190 -0
  7. package/client/MdSection.tsx +63 -23
  8. package/client/MdSectionEditor.tsx +208 -2
  9. package/client/Mermaid.tsx +33 -7
  10. package/client/Settings.css +17 -0
  11. package/client/Settings.tsx +132 -0
  12. package/client/Table.tsx +2 -14
  13. package/client/app.css +341 -1
  14. package/client/block-state/README.md +39 -0
  15. package/client/block-state/storage.ts +73 -0
  16. package/client/block-state/useBlockState.ts +120 -0
  17. package/client/builtins/DataTable.tsx +269 -0
  18. package/client/builtins/chart-data.ts +22 -10
  19. package/client/builtins/index.ts +10 -0
  20. package/client/data-table/ColumnMenu.tsx +168 -0
  21. package/client/data-table/ColumnOptions.tsx +141 -0
  22. package/client/data-table/TableCell.tsx +76 -0
  23. package/client/data-table/TableHeaderCell.tsx +94 -0
  24. package/client/data-table/model.ts +194 -0
  25. package/client/data-table/navigation.ts +34 -0
  26. package/client/data-table/table-state.ts +196 -0
  27. package/client/data-table/useColumnMenu.ts +84 -0
  28. package/client/data-table/useTableState.ts +36 -0
  29. package/client/design/base/editor.css +59 -0
  30. package/client/design/base/prose.css +4 -0
  31. package/client/diagrams/Dialog.tsx +577 -0
  32. package/client/diagrams/EditDialog.tsx +67 -0
  33. package/client/diagrams/Preferences.tsx +213 -0
  34. package/client/diagrams/edit-block.ts +93 -0
  35. package/client/diagrams/edit-context.ts +4 -0
  36. package/client/diagrams/session-id.ts +8 -0
  37. package/client/diagrams/source.ts +73 -0
  38. package/client/diagrams/style.css +580 -0
  39. package/client/doc-module-cache.ts +9 -1
  40. package/client/editor/ImageDialog.tsx +69 -0
  41. package/client/editor/LinkDialog.tsx +60 -0
  42. package/client/editor/blocks.ts +295 -0
  43. package/client/editor/component-fields.css +57 -0
  44. package/client/editor/component-fields.ts +128 -0
  45. package/client/editor/image-dialog.css +44 -0
  46. package/client/editor/mdx-block.tsx +138 -0
  47. package/client/editor/slash.ts +166 -0
  48. package/client/entry.tsx +2 -0
  49. package/client/head-metadata.ts +32 -0
  50. package/client/mdx-components-base.ts +2 -2
  51. package/client/router.ts +56 -17
  52. package/client/shell/AppShell.tsx +38 -15
  53. package/client/shell/Sidebar.tsx +20 -2
  54. package/client/shell/StandaloneShell.tsx +19 -6
  55. package/client/shell/TocRail.tsx +4 -3
  56. package/client/shell/TopBar.tsx +17 -5
  57. package/client/standalone-entry.tsx +11 -2
  58. package/client/state.ts +14 -0
  59. package/client/ui/ConfirmDeleteDialog.tsx +48 -91
  60. package/client/ui/ExpandModal.tsx +63 -104
  61. package/client/ui/Modal.tsx +123 -0
  62. package/client/ui/ResizeHandle.tsx +1 -1
  63. package/client/ui/SearchDialog.tsx +81 -88
  64. package/client/ui/modal.css +42 -0
  65. package/dist/cli.js +4644 -2830
  66. package/dist/diagram-worker.js +17 -0
  67. package/dist/registry.json +297 -0
  68. package/package.json +44 -3
  69. package/skills/mdxserve/SKILL.md +56 -1
@@ -0,0 +1,166 @@
1
+ import { Extension, type Range } from "@tiptap/core";
2
+ import { Suggestion, type SuggestionProps } from "@tiptap/suggestion";
3
+ import { PluginKey } from "@tiptap/pm/state";
4
+ import { insertSlashBlock, matchingBlocks, type SlashBlock } from "./blocks";
5
+
6
+ export function blockSlash(
7
+ enabledDiagram: () => boolean,
8
+ onOpenDiagram: (range: Range) => void,
9
+ onOpenImage: (range: Range) => void,
10
+ onOpenLink?: (range: Range) => void,
11
+ ) {
12
+ return Extension.create({
13
+ name: "blockSlash",
14
+ addProseMirrorPlugins() {
15
+ const editor = this.editor;
16
+ return [
17
+ Suggestion<SlashBlock>({
18
+ editor,
19
+ pluginKey: new PluginKey("blockSlash"),
20
+ char: "/",
21
+ allowSpaces: true,
22
+ startOfLine: true,
23
+ allow: ({ editor: activeEditor, state, range }) =>
24
+ activeEditor.isEditable &&
25
+ state.selection.empty &&
26
+ !state.selection.$from.marks().some((mark) => mark.type.name === "code") &&
27
+ state.doc.resolve(range.from).parent.type.name === "paragraph" &&
28
+ state.doc.resolve(range.from).parent.textContent ===
29
+ state.doc.textBetween(range.from, range.to),
30
+ items: ({ query }) => matchingBlocks(query),
31
+ command: ({ range, props: block }) => {
32
+ if (block.kind === "image") onOpenImage(range);
33
+ else if (block.id === "link" && onOpenLink) onOpenLink(range);
34
+ else if (block.kind === "diagram" && enabledDiagram()) onOpenDiagram(range);
35
+ else insertSlashBlock(editor, range, block);
36
+ },
37
+ render: () => {
38
+ let menu: HTMLDivElement | undefined;
39
+ let activeProps: SuggestionProps<SlashBlock> | undefined;
40
+ let selected = 0;
41
+ let scrollTarget: Element | null = null;
42
+ const position = () => {
43
+ if (!menu || !activeProps) return;
44
+ const rect = activeProps.clientRect?.();
45
+ if (!rect) return;
46
+ const margin = 8;
47
+ const width = menu.getBoundingClientRect().width || 320;
48
+ const height = menu.getBoundingClientRect().height;
49
+ menu.style.left = `${Math.max(margin, Math.min(rect.left, window.innerWidth - width - margin))}px`;
50
+ const below = window.innerHeight - rect.bottom - margin;
51
+ const above = rect.top - margin;
52
+ menu.style.top = `${below >= height || below >= above ? Math.min(rect.bottom + 6, window.innerHeight - height - margin) : Math.max(margin, rect.top - height - 6)}px`;
53
+ };
54
+ const destroy = () => {
55
+ window.removeEventListener("resize", position);
56
+ window.removeEventListener("scroll", position, true);
57
+ scrollTarget?.removeEventListener("scroll", position);
58
+ scrollTarget = null;
59
+ menu?.remove();
60
+ menu = undefined;
61
+ activeProps = undefined;
62
+ };
63
+ const choose = (index: number) => {
64
+ const block = activeProps?.items[index];
65
+ if (!block || !activeProps) return;
66
+ activeProps.command(block);
67
+ destroy();
68
+ };
69
+ const draw = () => {
70
+ if (!menu || !activeProps) return;
71
+ menu.replaceChildren();
72
+ const blocks = activeProps.items;
73
+ if (selected >= blocks.length) selected = 0;
74
+ menu.hidden = blocks.length === 0;
75
+ if (blocks.length === 0) return;
76
+ let group = "";
77
+ blocks.forEach((block, index) => {
78
+ if (group !== block.group) {
79
+ group = block.group;
80
+ const heading = document.createElement("div");
81
+ heading.className = "block-slash-group";
82
+ heading.textContent = group;
83
+ menu?.append(heading);
84
+ }
85
+ const option = document.createElement("button");
86
+ option.type = "button";
87
+ option.className = "block-slash-option";
88
+ option.id = `block-slash-${block.id}`;
89
+ option.setAttribute("role", "option");
90
+ option.setAttribute("aria-selected", String(index === selected));
91
+ const title = document.createElement("strong");
92
+ title.textContent = block.label;
93
+ const description = document.createElement("small");
94
+ description.textContent = block.description;
95
+ option.append(title, description);
96
+ option.onmouseenter = () => {
97
+ selected = index;
98
+ updateSelection();
99
+ };
100
+ option.onmousedown = (event) => {
101
+ event.preventDefault();
102
+ choose(index);
103
+ };
104
+ menu?.append(option);
105
+ });
106
+ position();
107
+ };
108
+ const updateSelection = () => {
109
+ menu
110
+ ?.querySelectorAll<HTMLButtonElement>('[role="option"]')
111
+ .forEach((option, index) => {
112
+ option.setAttribute("aria-selected", String(index === selected));
113
+ if (index === selected) option.scrollIntoView?.({ block: "nearest" });
114
+ });
115
+ };
116
+ return {
117
+ onStart(props) {
118
+ activeProps = props;
119
+ selected = 0;
120
+ menu = document.createElement("div");
121
+ menu.className = "block-slash-menu";
122
+ menu.setAttribute("role", "listbox");
123
+ menu.setAttribute("aria-label", "Insert block");
124
+ document.body.append(menu);
125
+ window.addEventListener("resize", position);
126
+ window.addEventListener("scroll", position, true);
127
+ scrollTarget = editor.view.dom.closest("[data-editing]");
128
+ scrollTarget?.addEventListener("scroll", position);
129
+ draw();
130
+ },
131
+ onUpdate(props) {
132
+ activeProps = props;
133
+ selected = 0;
134
+ draw();
135
+ },
136
+ onKeyDown({ event }) {
137
+ if (event.key === "Escape") {
138
+ event.preventDefault();
139
+ event.stopPropagation();
140
+ destroy();
141
+ return true;
142
+ }
143
+ if (
144
+ !activeProps?.items.length ||
145
+ !["ArrowDown", "ArrowUp", "Enter"].includes(event.key)
146
+ )
147
+ return false;
148
+ event.preventDefault();
149
+ event.stopPropagation();
150
+ if (event.key === "Enter") choose(selected);
151
+ else {
152
+ selected =
153
+ (selected + (event.key === "ArrowDown" ? 1 : -1) + activeProps.items.length) %
154
+ activeProps.items.length;
155
+ updateSelection();
156
+ }
157
+ return true;
158
+ },
159
+ onExit: destroy,
160
+ };
161
+ },
162
+ }),
163
+ ];
164
+ },
165
+ });
166
+ }
package/client/entry.tsx CHANGED
@@ -14,7 +14,9 @@ function parseInitialRoute(): Route {
14
14
  if (!el?.textContent) return fallback;
15
15
  const rootCount = Number.parseInt(el.dataset.rootCount ?? "", 10);
16
16
  if (Number.isFinite(rootCount)) shellInfo.rootCount = rootCount;
17
+ shellInfo.immutableSite = el.dataset.immutableSite === "1";
17
18
  shellInfo.sameMachine = el.dataset.sameMachine === "1";
19
+ shellInfo.permissions = el.dataset.permissions === "restricted" ? "restricted" : "full";
18
20
  try {
19
21
  return JSON.parse(el.textContent) as Route;
20
22
  } catch {
@@ -0,0 +1,32 @@
1
+ import type { DocumentMetadata } from "../src/docs/metadata";
2
+
3
+ const marker = "data-mdxserve-metadata";
4
+
5
+ /** Replace only metadata owned by mdxserve; keep the shell's single title element. */
6
+ export function applyHeadMetadata(title: string, metadata?: DocumentMetadata): void {
7
+ document.title = metadata?.title ?? title;
8
+ for (const element of document.head.querySelectorAll(`[${marker}]`)) element.remove();
9
+ if (!metadata) return;
10
+
11
+ if (metadata.description !== undefined) {
12
+ const description = document.createElement("meta");
13
+ description.name = "description";
14
+ description.content = metadata.description;
15
+ description.setAttribute(marker, "");
16
+ document.head.append(description);
17
+ }
18
+ if (metadata.canonical !== undefined) {
19
+ const canonical = document.createElement("link");
20
+ canonical.rel = "canonical";
21
+ canonical.href = metadata.canonical;
22
+ canonical.setAttribute(marker, "");
23
+ document.head.append(canonical);
24
+ }
25
+ for (const item of metadata.openGraph) {
26
+ const graph = document.createElement("meta");
27
+ graph.setAttribute("property", item.property);
28
+ graph.content = item.content;
29
+ graph.setAttribute(marker, "");
30
+ document.head.append(graph);
31
+ }
32
+ }
@@ -11,7 +11,7 @@ import type { ComponentType } from "react";
11
11
  import { TaskCheckbox } from "./TaskCheckbox";
12
12
  import { Figure, Pre } from "./CodeBlock";
13
13
  import { H2, H3, H4 } from "./Heading";
14
- import { Table } from "./Table";
14
+ import { MarkdownTable } from "./MarkdownTable";
15
15
  import { builtinComponents } from "./builtins/index";
16
16
 
17
17
  /**
@@ -28,5 +28,5 @@ export const mdxComponentsBase: Record<string, ComponentType<any>> = {
28
28
  h3: H3,
29
29
  h4: H4,
30
30
  input: TaskCheckbox,
31
- table: Table,
31
+ table: MarkdownTable,
32
32
  };
package/client/router.ts CHANGED
@@ -5,6 +5,7 @@ import type { inferRouterOutputs } from "@trpc/server";
5
5
  import { queryClient, trpc } from "./api";
6
6
  // Type-only: see the comment in client/api.ts — src/ is not served by Vite.
7
7
  import type { AppRouter } from "../src/api/router";
8
+ import type { DocumentMetadata } from "../src/docs/metadata";
8
9
 
9
10
  type RouterOutputs = inferRouterOutputs<AppRouter>;
10
11
  type DocTreeOutput = RouterOutputs["getDocTree"];
@@ -23,7 +24,14 @@ export interface RootInfo {
23
24
  export type Route =
24
25
  | { kind: "home"; roots: RootInfo[] }
25
26
  | { kind: "listing"; path: string; rootName: string; rootDir: string; entries: ListingEntry[] }
26
- | { kind: "doc"; path: string; rootName: string; rootDir: string; mtime?: number }
27
+ | {
28
+ kind: "doc";
29
+ path: string;
30
+ rootName: string;
31
+ rootDir: string;
32
+ mtime?: number;
33
+ metadata?: DocumentMetadata;
34
+ }
27
35
  | { kind: "notfound"; path: string; rootName?: string; rootDir?: string };
28
36
 
29
37
  // Derived from the router's own output schema (src/listing/controller.ts's
@@ -39,31 +47,40 @@ export interface RootTree extends RootInfo {
39
47
  // The doc-module cache lives in client/doc-module-cache.ts (so an exported
40
48
  // file can use it without this module's tRPC graph); re-exported here for the
41
49
  // live viewer's callers.
42
- import { docModuleCache, type DocModuleState } from "./doc-module-cache";
50
+ import { docModuleCache, docModuleUrl, type DocModuleState } from "./doc-module-cache";
43
51
  import { decodeRoutePath } from "./route-path";
44
52
  export { docModuleCache, type DocModuleState };
45
53
  const docModulePromises = new Map<string, Promise<void>>();
54
+ const docModuleGenerations = new Map<string, number>();
46
55
 
47
- function ensureDocModule(path: string): Promise<void> {
48
- if (docModuleCache.has(path)) return Promise.resolve();
56
+ function ensureDocModule(path: string, refresh = false): Promise<void> {
57
+ if (!refresh && docModuleCache.has(path)) return Promise.resolve();
49
58
  const pending = docModulePromises.get(path);
50
- if (pending) return pending;
51
-
52
- // Vite serves absolute filesystem paths through its /@fs/ scheme.
53
- const promise = import(/* @vite-ignore */ "/@fs" + path)
54
- .then((mod: { default?: ComponentType }) => {
59
+ if (pending && !refresh) return pending;
60
+ const generation = (docModuleGenerations.get(path) ?? 0) + 1;
61
+ docModuleGenerations.set(path, generation);
62
+
63
+ const moduleUrl = docModuleUrl(path, refresh ? `${Date.now()}-${generation}` : undefined);
64
+ const promise = import(/* @vite-ignore */ moduleUrl)
65
+ .then((mod: { default?: ComponentType; __mdxserveMetadata?: DocumentMetadata }) => {
66
+ if (docModuleGenerations.get(path) !== generation) return;
55
67
  if (!mod.default) {
56
68
  docModuleCache.set(path, { status: "error", message: `${path} has no default export.` });
57
69
  } else {
58
- docModuleCache.set(path, { status: "ok", Component: mod.default });
70
+ docModuleCache.set(path, {
71
+ status: "ok",
72
+ Component: mod.default,
73
+ metadata: mod.__mdxserveMetadata ?? { openGraph: [] },
74
+ });
59
75
  }
60
76
  })
61
77
  .catch((error: unknown) => {
78
+ if (docModuleGenerations.get(path) !== generation) return;
62
79
  const message = error instanceof Error ? (error.stack ?? error.message) : String(error);
63
80
  docModuleCache.set(path, { status: "error", message });
64
81
  })
65
82
  .finally(() => {
66
- docModulePromises.delete(path);
83
+ if (docModulePromises.get(path) === promise) docModulePromises.delete(path);
67
84
  });
68
85
 
69
86
  docModulePromises.set(path, promise);
@@ -75,9 +92,16 @@ function ensureDocModule(path: string): Promise<void> {
75
92
  * entry.tsx before React mounts so multi-root navigation (home crumb, `..`
76
93
  * row) doesn't flash in or out while the tree query is still loading.
77
94
  */
78
- export const shellInfo: { rootCount: number | null; sameMachine: boolean } = {
95
+ export const shellInfo: {
96
+ rootCount: number | null;
97
+ immutableSite: boolean;
98
+ sameMachine: boolean;
99
+ permissions: "full" | "restricted";
100
+ } = {
79
101
  rootCount: null,
102
+ immutableSite: false,
80
103
  sameMachine: false,
104
+ permissions: "full",
81
105
  };
82
106
 
83
107
  function mapRoots(data: DocTreeOutput): RootTree[] {
@@ -215,7 +239,7 @@ export function useFolderListing(folder: string): FolderListing {
215
239
  return { entries: data?.entries ?? null, error: isError && !data, notFound };
216
240
  }
217
241
 
218
- function titleFor(route: Route): string {
242
+ export function titleFor(route: Route): string {
219
243
  if (route.kind === "home") return "mdxserve";
220
244
  if (route.kind === "listing") return `${route.rootName}${route.path.slice(route.rootDir.length)}`;
221
245
  if (route.kind === "doc") {
@@ -299,7 +323,6 @@ export function useRouter(initialRoute: Route) {
299
323
  // "/" resolves to that root's listing (mirroring the server's 302),
300
324
  // and the address bar should say so.
301
325
  history.pushState({}, "", next.kind === "home" ? "/" : next.path);
302
- document.title = titleFor(next);
303
326
  setRoute(next);
304
327
  });
305
328
  },
@@ -326,7 +349,6 @@ export function useRouter(initialRoute: Route) {
326
349
  }, []);
327
350
 
328
351
  useEffect(() => {
329
- document.title = titleFor(initialRoute);
330
352
  // The server embeds the initial listing's entries straight into the page,
331
353
  // but useFolderListing consumers still need them in the shared cache.
332
354
  if (initialRoute.kind === "listing") {
@@ -344,7 +366,6 @@ export function useRouter(initialRoute: Route) {
344
366
  function onPopState() {
345
367
  loadRoute(location.pathname).then((next) => {
346
368
  if (!next) return;
347
- document.title = titleFor(next);
348
369
  setRoute(next);
349
370
  });
350
371
  }
@@ -355,6 +376,25 @@ export function useRouter(initialRoute: Route) {
355
376
  return () => window.removeEventListener("popstate", onPopState);
356
377
  }, [loadRoute]);
357
378
 
379
+ // The file watcher names the changed source even when Vite attributes its
380
+ // HMR update to an importer. Re-import with a new URL so metadata removals
381
+ // replace the cached module as well as changes to the visible document.
382
+ useEffect(() => {
383
+ if (!import.meta.hot) return;
384
+ const handleDocChanged = (data: { files?: Array<{ path: string }> }) => {
385
+ for (const file of data?.files ?? []) {
386
+ if (!docModuleCache.has(file.path)) continue;
387
+ void ensureDocModule(file.path, true).then(() => {
388
+ setRoute((current) =>
389
+ current.kind === "doc" && current.path === file.path ? { ...current } : current,
390
+ );
391
+ });
392
+ }
393
+ };
394
+ import.meta.hot.on("mdxserve:doc-changed", handleDocChanged);
395
+ return () => import.meta.hot?.off?.("mdxserve:doc-changed", handleDocChanged);
396
+ }, []);
397
+
358
398
  useEffect(() => {
359
399
  function onClick(event: MouseEvent) {
360
400
  if (event.defaultPrevented || event.button !== 0) return;
@@ -441,7 +481,6 @@ export function useRouter(initialRoute: Route) {
441
481
  roots: remaining.map((info) => ({ name: info.name, dir: info.dir })),
442
482
  };
443
483
  history.pushState({}, "", "/");
444
- document.title = titleFor(homeRoute);
445
484
  setRoute(homeRoute);
446
485
  }
447
486
  window.addEventListener("mdxserve:roots-changed", onRootsChanged);
@@ -1,3 +1,4 @@
1
+ import { DiagramPreferences } from "../diagrams/Preferences";
1
2
  import { AnimatePresence, motion, useSpring } from "framer-motion";
2
3
  import { useAtomValue } from "jotai";
3
4
  import { keepPreviousData, useQuery } from "@tanstack/react-query";
@@ -30,17 +31,26 @@ import { TocRail } from "./TocRail";
30
31
  import { TopBar } from "./TopBar";
31
32
  import { useContentWidth, useDocMaxWidth } from "./use-doc-width";
32
33
  import { DESKTOP_MEDIA } from "../platform";
33
- import { docWidthAtom, LISTING_MAX_WIDTH, LISTING_MIN_WIDTH, listingWidthAtom } from "../state";
34
+ import {
35
+ contentLayoutAtom,
36
+ docWidthAtom,
37
+ LISTING_MAX_WIDTH,
38
+ LISTING_MIN_WIDTH,
39
+ listingWidthAtom,
40
+ } from "../state";
34
41
 
35
- const SIDEBAR_STORAGE_KEY = "mdxserve-sidebar";
42
+ function sidebarStorageKey(): string {
43
+ // Keep hosted navigation preferences separate from local reader defaults.
44
+ return shellInfo.immutableSite ? "mdxserve-site-sidebar" : "mdxserve-sidebar";
45
+ }
36
46
  const SEARCH_DEBOUNCE_MS = 120;
37
47
 
38
48
  function readStoredSidebarOpen(): boolean {
39
49
  try {
40
- const value = localStorage.getItem(SIDEBAR_STORAGE_KEY);
41
- return value === null ? true : value === "1";
50
+ const value = localStorage.getItem(sidebarStorageKey());
51
+ return value === null ? !shellInfo.immutableSite : value === "1";
42
52
  } catch {
43
- return true;
53
+ return !shellInfo.immutableSite;
44
54
  }
45
55
  }
46
56
 
@@ -132,6 +142,7 @@ function useListingMaxWidth(contentWidth: number, stored: number | null) {
132
142
  }
133
143
 
134
144
  export function AppShell({ route, navigate }: { route: Route; navigate: (path: string) => void }) {
145
+ const [diagramPreferences, setDiagramPreferences] = useState(false);
135
146
  const { theme, toggle } = useTheme();
136
147
  const { roots } = useTree();
137
148
 
@@ -171,6 +182,7 @@ export function AppShell({ route, navigate }: { route: Route; navigate: (path: s
171
182
  const [exporting, setExporting] = useState(false);
172
183
  const listingWidth = useAtomValue(listingWidthAtom);
173
184
  const docWidth = useAtomValue(docWidthAtom);
185
+ const fullWidth = useAtomValue(contentLayoutAtom) === "full-width";
174
186
  const contentRef = useRef<HTMLDivElement>(null);
175
187
  const contentWidth = useContentWidth(contentRef);
176
188
  const listingMaxWidth = useListingMaxWidth(contentWidth, listingWidth);
@@ -203,7 +215,7 @@ export function AppShell({ route, navigate }: { route: Route; navigate: (path: s
203
215
 
204
216
  useEffect(() => {
205
217
  try {
206
- localStorage.setItem(SIDEBAR_STORAGE_KEY, desktopOpen ? "1" : "0");
218
+ localStorage.setItem(sidebarStorageKey(), desktopOpen ? "1" : "0");
207
219
  } catch {
208
220
  // private mode / storage disabled: the choice just won't persist
209
221
  }
@@ -301,13 +313,22 @@ export function AppShell({ route, navigate }: { route: Route; navigate: (path: s
301
313
 
302
314
  return (
303
315
  <div className="min-h-screen bg-surface-page">
316
+ <AnimatePresence>
317
+ {diagramPreferences && (
318
+ <DiagramPreferences
319
+ canEditDiagrams={shellInfo.sameMachine && shellInfo.permissions !== "restricted"}
320
+ onClose={() => setDiagramPreferences(false)}
321
+ />
322
+ )}
323
+ </AnimatePresence>
304
324
  <TopBar
325
+ onDiagramPreferences={() => setDiagramPreferences(true)}
305
326
  homeHref="/"
306
327
  hostLabel={location.host}
307
- sidebar={{ open: showSidebar, onToggle: handleToggleSidebar }}
328
+ sidebar={{ open: showSidebar || mobileOpen, onToggle: handleToggleSidebar }}
308
329
  search={{ onOpen: () => setSearchOpen(true) }}
309
330
  exportDoc={
310
- route.kind === "doc" && shellInfo.sameMachine
331
+ route.kind === "doc" && shellInfo.permissions !== "restricted" && shellInfo.sameMachine
311
332
  ? { pending: exporting, onExport: handleExport }
312
333
  : undefined
313
334
  }
@@ -324,7 +345,7 @@ export function AppShell({ route, navigate }: { route: Route; navigate: (path: s
324
345
  mobileOpen={mobileOpen}
325
346
  onCloseMobile={() => setMobileOpen(false)}
326
347
  />
327
- <main className="flex flex-1 min-w-0 px-8 pt-10 pb-24">
348
+ <main className="flex flex-1 min-w-0 px-5 pt-6 pb-16 md:px-8 md:pt-10 md:pb-24">
328
349
  {/* The resize handles clamp drags to this area, and the max-width
329
350
  springs track it, so content never runs under the toc rail. */}
330
351
  <div ref={contentRef} data-content-area className="flex flex-1 min-w-0 justify-center">
@@ -336,16 +357,18 @@ export function AppShell({ route, navigate }: { route: Route; navigate: (path: s
336
357
  animate="enter"
337
358
  exit="exit"
338
359
  className={
339
- route.kind === "listing" || (route.kind === "doc" && docMaxWidth)
360
+ fullWidth || route.kind === "listing" || (route.kind === "doc" && docMaxWidth)
340
361
  ? "w-full"
341
362
  : "w-full max-w-prose"
342
363
  }
343
364
  style={
344
- route.kind === "listing"
345
- ? { maxWidth: listingMaxWidth }
346
- : route.kind === "doc" && docMaxWidth
347
- ? { maxWidth: docMaxWidth }
348
- : undefined
365
+ fullWidth
366
+ ? { maxWidth: "100%" }
367
+ : route.kind === "listing"
368
+ ? { maxWidth: listingMaxWidth }
369
+ : route.kind === "doc" && docMaxWidth
370
+ ? { maxWidth: docMaxWidth }
371
+ : undefined
349
372
  }
350
373
  >
351
374
  <Breadcrumb items={breadcrumbItems(route, rootCount)} />
@@ -1,8 +1,9 @@
1
1
  import { AnimatePresence, LayoutGroup, motion } from "framer-motion";
2
- import { useRef } from "react";
2
+ import { useEffect, useRef } from "react";
3
3
  import { useAtom } from "jotai";
4
4
  import { VARIANTS } from "../motion";
5
5
  import { Icon } from "../ui/Icon";
6
+ import { IconButton } from "../ui/IconButton";
6
7
  import { ResizeHandle } from "../ui/ResizeHandle";
7
8
  import { ListingView } from "../ListingView";
8
9
  import { useFolderListing } from "../router";
@@ -95,6 +96,15 @@ export function Sidebar({
95
96
  const [width, setWidth] = useAtom(sidebarWidthAtom);
96
97
  const asideRef = useRef<HTMLDivElement>(null);
97
98
 
99
+ useEffect(() => {
100
+ if (!mobileOpen) return;
101
+ function onKeyDown(event: KeyboardEvent) {
102
+ if (event.key === "Escape") onCloseMobile();
103
+ }
104
+ window.addEventListener("keydown", onKeyDown);
105
+ return () => window.removeEventListener("keydown", onKeyDown);
106
+ }, [mobileOpen, onCloseMobile]);
107
+
98
108
  return (
99
109
  <>
100
110
  {/* Mobile overlay: fixed drawer + scrim, closed by default. */}
@@ -121,9 +131,17 @@ export function Sidebar({
121
131
  if ((event.target as HTMLElement).closest("a[href]")) onCloseMobile();
122
132
  }}
123
133
  className={
124
- "fixed inset-y-0 left-0 z-[var(--z-modal)] overflow-y-auto border-r border-border-subtle bg-surface-raised px-3 py-6 shadow-lg md:hidden w-sidebar"
134
+ "fixed inset-y-0 left-0 z-[var(--z-modal)] overflow-y-auto border-r border-border-subtle bg-surface-raised px-3 py-6 shadow-lg md:hidden w-sidebar max-w-[calc(100vw-3rem)]"
125
135
  }
126
136
  >
137
+ <div className="mb-3 flex justify-end">
138
+ <IconButton
139
+ icon="x"
140
+ label="Close navigation"
141
+ className="h-11 w-11"
142
+ onClick={onCloseMobile}
143
+ />
144
+ </div>
127
145
  <SidebarBody
128
146
  rootDir={rootDir}
129
147
  activePath={activePath}
@@ -1,11 +1,12 @@
1
- import { motion } from "framer-motion";
1
+ import { AnimatePresence, motion } from "framer-motion";
2
2
  import { useAtomValue } from "jotai";
3
3
  import { useCallback, useRef, useState } from "react";
4
+ import { Settings } from "../Settings";
4
5
  import { DocView } from "../DocView";
5
6
  import { fadeRise } from "../motion";
6
7
  import { useTheme } from "../theme";
7
8
  import { docModuleCache } from "../doc-module-cache";
8
- import { docWidthAtom } from "../state";
9
+ import { contentLayoutAtom, docWidthAtom } from "../state";
9
10
  import { Footer } from "./Footer";
10
11
  import { TocRail } from "./TocRail";
11
12
  import { TopBar } from "./TopBar";
@@ -20,8 +21,10 @@ import type { StandaloneMeta } from "../standalone-entry";
20
21
  */
21
22
  export function StandaloneShell({ meta }: { meta: StandaloneMeta }) {
22
23
  const { theme, toggle } = useTheme();
24
+ const [settingsOpen, setSettingsOpen] = useState(false);
23
25
  const [tocVersion, setTocVersion] = useState(0);
24
26
  const docWidth = useAtomValue(docWidthAtom);
27
+ const fullWidth = useAtomValue(contentLayoutAtom) === "full-width";
25
28
  const contentRef = useRef<HTMLDivElement>(null);
26
29
  const contentWidth = useContentWidth(contentRef);
27
30
  const docMaxWidth = useDocMaxWidth(contentWidth, docWidth);
@@ -40,15 +43,25 @@ export function StandaloneShell({ meta }: { meta: StandaloneMeta }) {
40
43
 
41
44
  return (
42
45
  <div className="min-h-screen bg-surface-page">
43
- <TopBar theme={theme} onToggleTheme={toggle} hostLabel={meta.label} />
44
- <main className="flex flex-1 min-w-0 px-8 pt-10 pb-24">
46
+ <AnimatePresence>
47
+ {settingsOpen && <Settings onClose={() => setSettingsOpen(false)} />}
48
+ </AnimatePresence>
49
+ <TopBar
50
+ theme={theme}
51
+ onToggleTheme={toggle}
52
+ hostLabel={meta.label}
53
+ onDiagramPreferences={() => setSettingsOpen(true)}
54
+ />
55
+ <main className="flex flex-1 min-w-0 px-5 pt-6 pb-16 md:px-8 md:pt-10 md:pb-24">
45
56
  <div ref={contentRef} data-content-area className="flex flex-1 min-w-0 justify-center">
46
57
  <motion.div
47
58
  variants={fadeRise}
48
59
  initial="initial"
49
60
  animate="enter"
50
- className={docMaxWidth ? "w-full" : "w-full max-w-prose"}
51
- style={docMaxWidth ? { maxWidth: docMaxWidth } : undefined}
61
+ className={fullWidth || docMaxWidth ? "w-full" : "w-full max-w-prose"}
62
+ style={
63
+ fullWidth ? { maxWidth: "100%" } : docMaxWidth ? { maxWidth: docMaxWidth } : undefined
64
+ }
52
65
  >
53
66
  <DocView
54
67
  path={meta.path}
@@ -1,8 +1,8 @@
1
- import { useAtom } from "jotai";
1
+ import { useAtom, useAtomValue } from "jotai";
2
2
  import { useRef } from "react";
3
3
  import { ResizeHandle } from "../ui/ResizeHandle";
4
4
  import { TocList } from "../ui/TocList";
5
- import { TOC_MAX_WIDTH, TOC_MIN_WIDTH, tocWidthAtom } from "../state";
5
+ import { TOC_MAX_WIDTH, TOC_MIN_WIDTH, tocVisibleAtom, tocWidthAtom } from "../state";
6
6
  import { useToc } from "./useToc";
7
7
 
8
8
  function clampWidth(px: number): number {
@@ -13,9 +13,10 @@ function clampWidth(px: number): number {
13
13
  export function TocRail({ path, version }: { path: string; version: number }) {
14
14
  const { items, activeId, setActiveId } = useToc(path, version);
15
15
  const [width, setWidth] = useAtom(tocWidthAtom);
16
+ const visible = useAtomValue(tocVisibleAtom);
16
17
  const railRef = useRef<HTMLDivElement>(null);
17
18
  const hasH2 = items.some((item) => item.level === 2);
18
- if (!hasH2) return null;
19
+ if (!visible || !hasH2) return null;
19
20
 
20
21
  function handleSelect(id: string) {
21
22
  setActiveId(id);