@karimsa/mdxserve 0.5.1 → 0.6.0-rc.1

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 (37) hide show
  1. package/README.md +3 -0
  2. package/client/DocView.tsx +3 -8
  3. package/client/ListingView.tsx +3 -3
  4. package/client/Settings.tsx +26 -21
  5. package/client/app.css +20 -0
  6. package/client/comments/CommentsPanel.tsx +163 -0
  7. package/client/comments/CommentsPopover.tsx +197 -0
  8. package/client/comments/CommentsSheet.tsx +63 -0
  9. package/client/comments/CommentsSidebar.tsx +18 -0
  10. package/client/comments/CtlButton.tsx +33 -0
  11. package/client/comments/DraftThread.tsx +83 -0
  12. package/client/comments/GutterCounters.tsx +113 -0
  13. package/client/comments/ReplyField.tsx +98 -0
  14. package/client/comments/SelectionButton.tsx +142 -0
  15. package/client/comments/Thread.tsx +278 -0
  16. package/client/comments/anchor.ts +204 -0
  17. package/client/comments/data.ts +98 -0
  18. package/client/comments/dom.ts +212 -0
  19. package/client/comments/drafts.ts +36 -0
  20. package/client/comments/highlights.ts +83 -0
  21. package/client/comments/model.ts +241 -0
  22. package/client/comments/pins.ts +37 -0
  23. package/client/comments/self-writes.ts +22 -0
  24. package/client/comments/state.ts +60 -0
  25. package/client/comments/time.ts +21 -0
  26. package/client/hooks.ts +19 -0
  27. package/client/platform.ts +8 -0
  28. package/client/router.ts +5 -0
  29. package/client/shell/AppShell.tsx +60 -14
  30. package/client/shell/StandaloneShell.tsx +3 -2
  31. package/client/shell/TopBar.tsx +25 -0
  32. package/client/ui/ConfirmDeleteDialog.tsx +1 -1
  33. package/client/ui/Toast.tsx +5 -1
  34. package/client/ui/icon-set.ts +10 -0
  35. package/dist/cli.js +872 -229
  36. package/package.json +1 -1
  37. package/skills/mdxserve/SKILL.md +86 -0
@@ -0,0 +1,21 @@
1
+ const MINUTE = 60;
2
+ const HOUR = 60 * MINUTE;
3
+ const DAY = 24 * HOUR;
4
+ const WEEK = 7 * DAY;
5
+
6
+ /**
7
+ * A compact age for a message header: "now", "12m", "3h", "2d", then a short
8
+ * date ("Nov 24") once it is a week old. Empty for a missing or unparsable
9
+ * timestamp. The full date and time is on the element's title.
10
+ */
11
+ export function relativeTime(iso: string | undefined, now: number = Date.now()): string {
12
+ if (!iso) return "";
13
+ const then = Date.parse(iso);
14
+ if (Number.isNaN(then)) return "";
15
+ const elapsed = Math.round((now - then) / 1000);
16
+ if (elapsed < 45) return "now";
17
+ if (elapsed < HOUR) return `${Math.max(1, Math.floor(elapsed / MINUTE))}m`;
18
+ if (elapsed < DAY) return `${Math.floor(elapsed / HOUR)}h`;
19
+ if (elapsed < WEEK) return `${Math.floor(elapsed / DAY)}d`;
20
+ return new Date(then).toLocaleDateString(undefined, { month: "short", day: "numeric" });
21
+ }
package/client/hooks.ts CHANGED
@@ -1,4 +1,7 @@
1
+ import { useAtomValue } from "jotai";
1
2
  import { useCallback, useEffect, useState, useSyncExternalStore } from "react";
3
+ import { COMPACT_MEDIA } from "./platform";
4
+ import { contentLayoutAtom } from "./state";
2
5
 
3
6
  /** Returns `value`, updated only after it has stayed unchanged for `delayMs`. */
4
7
  export function useDebounced<Value>(value: Value, delayMs: number): Value {
@@ -32,3 +35,19 @@ export function useMediaQuery(query: string, serverDefault = false): boolean {
32
35
  () => serverDefault,
33
36
  );
34
37
  }
38
+
39
+ /** Whether the viewport is phone-sized, in either orientation. */
40
+ export function useCompactScreen(): boolean {
41
+ return useMediaQuery(COMPACT_MEDIA);
42
+ }
43
+
44
+ /**
45
+ * Whether content fills the available width. Phones always do, whatever the
46
+ * saved preference says; the preference itself is left untouched so it
47
+ * applies again on a larger screen.
48
+ */
49
+ export function useFullWidth(): boolean {
50
+ const layout = useAtomValue(contentLayoutAtom);
51
+ const compact = useCompactScreen();
52
+ return compact || layout === "full-width";
53
+ }
@@ -14,3 +14,11 @@ export function isApplePlatform(): boolean {
14
14
  * screen by every component that adapts its layout.
15
15
  */
16
16
  export const DESKTOP_MEDIA = "(min-width: 768px)";
17
+
18
+ /**
19
+ * A phone in either orientation: narrower than the desktop breakpoint, or a
20
+ * touch screen too short to be anything but a landscape phone (tablets are
21
+ * never under ~740px tall). Layout preferences that only make sense with room
22
+ * to spare are overridden here.
23
+ */
24
+ export const COMPACT_MEDIA = "(max-width: 767px), (pointer: coarse) and (max-height: 500px)";
package/client/router.ts CHANGED
@@ -383,6 +383,11 @@ export function useRouter(initialRoute: Route) {
383
383
  if (!import.meta.hot) return;
384
384
  const handleDocChanged = (data: { files?: Array<{ path: string }> }) => {
385
385
  for (const file of data?.files ?? []) {
386
+ // An agent's reply lives in the frontmatter, so refetch the comments too.
387
+ void queryClient.invalidateQueries({
388
+ ...trpc.getDocComments.queryFilter({ path: file.path }),
389
+ refetchType: "all",
390
+ });
386
391
  if (!docModuleCache.has(file.path)) continue;
387
392
  void ensureDocModule(file.path, true).then(() => {
388
393
  setRoute((current) =>
@@ -6,7 +6,7 @@ import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } fr
6
6
  import { trpc } from "../api";
7
7
  import { DocView } from "../DocView";
8
8
  import { HomeView } from "../HomeView";
9
- import { useDebounced } from "../hooks";
9
+ import { useDebounced, useFullWidth, useMediaQuery } from "../hooks";
10
10
  import { ListingView } from "../ListingView";
11
11
  import { fadeRise, TRANSITIONS } from "../motion";
12
12
  import { useTheme } from "../theme";
@@ -22,6 +22,12 @@ import {
22
22
  import type { ExportFormat } from "../../src/export/service";
23
23
  import { exportDocFromViewer } from "../export-doc";
24
24
  import { Breadcrumb, type BreadcrumbItem } from "../ui/Breadcrumb";
25
+ import { CommentsPopover } from "../comments/CommentsPopover";
26
+ import { CommentsSheet } from "../comments/CommentsSheet";
27
+ import { CommentsSidebar } from "../comments/CommentsSidebar";
28
+ import { GutterCounters } from "../comments/GutterCounters";
29
+ import { useCommentsModel, useCommentsPanel } from "../comments/model";
30
+ import { SelectionButton } from "../comments/SelectionButton";
25
31
  import { PageNav, type PageNavLink } from "../ui/PageNav";
26
32
  import { SearchDialog, type SearchResult } from "../ui/SearchDialog";
27
33
  import { Footer } from "./Footer";
@@ -31,13 +37,7 @@ import { TocRail } from "./TocRail";
31
37
  import { TopBar } from "./TopBar";
32
38
  import { useContentWidth, useDocMaxWidth } from "./use-doc-width";
33
39
  import { DESKTOP_MEDIA } from "../platform";
34
- import {
35
- contentLayoutAtom,
36
- docWidthAtom,
37
- LISTING_MAX_WIDTH,
38
- LISTING_MIN_WIDTH,
39
- listingWidthAtom,
40
- } from "../state";
40
+ import { docWidthAtom, LISTING_MAX_WIDTH, LISTING_MIN_WIDTH, listingWidthAtom } from "../state";
41
41
 
42
42
  function sidebarStorageKey(): string {
43
43
  // Keep hosted navigation preferences separate from local reader defaults.
@@ -177,12 +177,15 @@ export function AppShell({ route, navigate }: { route: Route; navigate: (path: s
177
177
 
178
178
  const [desktopOpen, setDesktopOpen] = useState(readStoredSidebarOpen);
179
179
  const [mobileOpen, setMobileOpen] = useState(false);
180
+ // Tracks rotation and resizes across the dock/drawer breakpoint, so the
181
+ // toggle and its pressed state always describe the layout on screen.
182
+ const isDesktop = useMediaQuery(DESKTOP_MEDIA, true);
180
183
 
181
184
  const [searchOpen, setSearchOpen] = useState(false);
182
185
  const [exporting, setExporting] = useState(false);
183
186
  const listingWidth = useAtomValue(listingWidthAtom);
184
187
  const docWidth = useAtomValue(docWidthAtom);
185
- const fullWidth = useAtomValue(contentLayoutAtom) === "full-width";
188
+ const fullWidth = useFullWidth();
186
189
  const contentRef = useRef<HTMLDivElement>(null);
187
190
  const contentWidth = useContentWidth(contentRef);
188
191
  const listingMaxWidth = useListingMaxWidth(contentWidth, listingWidth);
@@ -213,6 +216,14 @@ export function AppShell({ route, navigate }: { route: Route; navigate: (path: s
213
216
  }
214
217
  }, []);
215
218
 
219
+ const comments = useCommentsModel(route.kind === "doc" ? route.path : null, tocVersion);
220
+ const commentsPanel = useCommentsPanel();
221
+ // The sidebar is docked beside <main>; the table-of-contents rail is hidden while it is open.
222
+ const commentsOpen = comments.available && commentsPanel.open;
223
+ const commentsSidebarOpen = comments.available && commentsPanel.sidebarOpen;
224
+ const setCommentsOpen = commentsPanel.setOpen;
225
+ const closeComments = useCallback(() => setCommentsOpen(false), [setCommentsOpen]);
226
+
216
227
  useEffect(() => {
217
228
  try {
218
229
  localStorage.setItem(sidebarStorageKey(), desktopOpen ? "1" : "0");
@@ -230,6 +241,12 @@ export function AppShell({ route, navigate }: { route: Route; navigate: (path: s
230
241
  setMobileOpen(false);
231
242
  }, [activePath]);
232
243
 
244
+ // Widening past the breakpoint docks the sidebar; a drawer left open on the
245
+ // narrow layout must not reappear the next time the screen narrows.
246
+ useEffect(() => {
247
+ if (isDesktop) setMobileOpen(false);
248
+ }, [isDesktop]);
249
+
233
250
  // One export at a time: the trigger is disabled while a build runs, and the
234
251
  // server queues builds anyway (see startServer).
235
252
  const handleExport = useCallback(
@@ -246,7 +263,7 @@ export function AppShell({ route, navigate }: { route: Route; navigate: (path: s
246
263
  );
247
264
 
248
265
  const handleToggleSidebar = useCallback(() => {
249
- if (window.matchMedia(DESKTOP_MEDIA).matches) {
266
+ if (isDesktop) {
250
267
  // A click here would otherwise silently flip (and persist) the
251
268
  // preference for the next doc.
252
269
  if (sidebarHidden) return;
@@ -254,7 +271,7 @@ export function AppShell({ route, navigate }: { route: Route; navigate: (path: s
254
271
  } else {
255
272
  setMobileOpen((previous) => !previous);
256
273
  }
257
- }, [sidebarHidden]);
274
+ }, [isDesktop, sidebarHidden]);
258
275
 
259
276
  // Global ⌘K / Ctrl+K to open search, from anywhere on the page.
260
277
  useEffect(() => {
@@ -312,7 +329,7 @@ export function AppShell({ route, navigate }: { route: Route; navigate: (path: s
312
329
  const showSidebar = desktopOpen && !sidebarHidden;
313
330
 
314
331
  return (
315
- <div className="min-h-screen bg-surface-page">
332
+ <div className="relative min-h-screen bg-surface-page">
316
333
  <AnimatePresence>
317
334
  {diagramPreferences && (
318
335
  <DiagramPreferences
@@ -325,7 +342,12 @@ export function AppShell({ route, navigate }: { route: Route; navigate: (path: s
325
342
  onDiagramPreferences={() => setDiagramPreferences(true)}
326
343
  homeHref="/"
327
344
  hostLabel={location.host}
328
- sidebar={{ open: showSidebar || mobileOpen, onToggle: handleToggleSidebar }}
345
+ sidebar={{
346
+ // Only the docked sidebar shows a pressed toggle; on a phone the
347
+ // drawer covers the bar, and a pressed state just looks stuck.
348
+ open: isDesktop && showSidebar,
349
+ onToggle: handleToggleSidebar,
350
+ }}
329
351
  search={{ onOpen: () => setSearchOpen(true) }}
330
352
  exportDoc={
331
353
  route.kind === "doc" && shellInfo.permissions !== "restricted" && shellInfo.sameMachine
@@ -334,6 +356,15 @@ export function AppShell({ route, navigate }: { route: Route; navigate: (path: s
334
356
  }
335
357
  theme={theme}
336
358
  onToggleTheme={toggle}
359
+ comments={
360
+ route.kind === "doc" && comments.available
361
+ ? {
362
+ count: comments.openCount,
363
+ open: commentsOpen,
364
+ onToggle: () => commentsPanel.setOpen(!commentsPanel.open),
365
+ }
366
+ : undefined
367
+ }
337
368
  />
338
369
  <div className="flex items-start">
339
370
  <Sidebar
@@ -408,9 +439,24 @@ export function AppShell({ route, navigate }: { route: Route; navigate: (path: s
408
439
  </motion.div>
409
440
  </AnimatePresence>
410
441
  </div>
411
- {route.kind === "doc" ? <TocRail path={route.path} version={tocVersion} /> : null}
442
+ {route.kind === "doc" && !commentsSidebarOpen ? (
443
+ <TocRail path={route.path} version={tocVersion} />
444
+ ) : null}
412
445
  </main>
446
+ {route.kind === "doc" && commentsSidebarOpen ? (
447
+ <CommentsSidebar model={comments} onClose={closeComments} />
448
+ ) : null}
413
449
  </div>
450
+ {route.kind === "doc" && comments.available && !commentsPanel.isDesktop ? (
451
+ <CommentsSheet model={comments} open={commentsPanel.open} onClose={closeComments} />
452
+ ) : null}
453
+ {route.kind === "doc" && commentsPanel.isDesktop ? (
454
+ <>
455
+ {comments.available && !commentsSidebarOpen ? <GutterCounters model={comments} /> : null}
456
+ <CommentsPopover model={comments} sidebarOpen={commentsSidebarOpen} />
457
+ </>
458
+ ) : null}
459
+ {route.kind === "doc" && !comments.readOnly ? <SelectionButton model={comments} /> : null}
414
460
  <SearchDialog
415
461
  open={searchOpen}
416
462
  query={query}
@@ -6,7 +6,8 @@ import { DocView } from "../DocView";
6
6
  import { fadeRise } from "../motion";
7
7
  import { useTheme } from "../theme";
8
8
  import { docModuleCache } from "../doc-module-cache";
9
- import { contentLayoutAtom, docWidthAtom } from "../state";
9
+ import { docWidthAtom } from "../state";
10
+ import { useFullWidth } from "../hooks";
10
11
  import { Footer } from "./Footer";
11
12
  import { TocRail } from "./TocRail";
12
13
  import { TopBar } from "./TopBar";
@@ -24,7 +25,7 @@ export function StandaloneShell({ meta }: { meta: StandaloneMeta }) {
24
25
  const [settingsOpen, setSettingsOpen] = useState(false);
25
26
  const [tocVersion, setTocVersion] = useState(0);
26
27
  const docWidth = useAtomValue(docWidthAtom);
27
- const fullWidth = useAtomValue(contentLayoutAtom) === "full-width";
28
+ const fullWidth = useFullWidth();
28
29
  const contentRef = useRef<HTMLDivElement>(null);
29
30
  const contentWidth = useContentWidth(contentRef);
30
31
  const docMaxWidth = useDocMaxWidth(contentWidth, docWidth);
@@ -45,6 +45,7 @@ export function TopBar({
45
45
  theme,
46
46
  onToggleTheme,
47
47
  onDiagramPreferences,
48
+ comments,
48
49
  }: {
49
50
  /** Wordmark's link target; undefined renders it as a plain (non-link) span. */
50
51
  homeHref?: string;
@@ -59,6 +60,8 @@ export function TopBar({
59
60
  theme: Theme;
60
61
  onToggleTheme: () => void;
61
62
  onDiagramPreferences?: () => void;
63
+ /** Comments panel toggle (doc pages); omitted when undefined. */
64
+ comments?: { count: number; open: boolean; onToggle: () => void };
62
65
  }) {
63
66
  const mac = isApplePlatform();
64
67
 
@@ -77,6 +80,8 @@ export function TopBar({
77
80
  label="Toggle sidebar"
78
81
  size="sm"
79
82
  active={sidebar.open}
83
+ // No grey flash on tap in mobile Safari.
84
+ className="[-webkit-tap-highlight-color:transparent]"
80
85
  onClick={sidebar.onToggle}
81
86
  />
82
87
  ) : null}
@@ -115,6 +120,26 @@ export function TopBar({
115
120
  onChange={(format) => exportDoc.onExport(format as ExportFormat)}
116
121
  />
117
122
  ) : null}
123
+ {comments ? (
124
+ <span className="relative inline-flex shrink-0">
125
+ <IconButton
126
+ icon="message-square"
127
+ label={comments.count > 0 ? `Comments (${comments.count} open)` : "Comments"}
128
+ size="sm"
129
+ active={comments.open}
130
+ className="max-md:h-11 max-md:w-11"
131
+ onClick={comments.onToggle}
132
+ />
133
+ {comments.count > 0 ? (
134
+ <span
135
+ aria-hidden
136
+ className="pointer-events-none absolute -top-1 -right-1 flex h-4 min-w-4 items-center justify-center rounded-pill bg-surface-accent px-1 text-[10px] font-semibold leading-none text-n-0 tabular-nums"
137
+ >
138
+ {comments.count > 99 ? "99+" : comments.count}
139
+ </span>
140
+ ) : null}
141
+ </span>
142
+ ) : null}
118
143
  {onDiagramPreferences && (
119
144
  <IconButton icon="settings" label="Settings" size="sm" onClick={onDiagramPreferences} />
120
145
  )}
@@ -26,7 +26,7 @@ export function ConfirmDeleteDialog({
26
26
  placement="top"
27
27
  role="alertdialog"
28
28
  aria-labelledby="confirm-delete-title"
29
- className="w-[560px] overflow-hidden"
29
+ className="w-[560px] max-w-[calc(100vw-2rem)] overflow-hidden"
30
30
  >
31
31
  <div className="flex items-center gap-2 border-b border-border-subtle px-4 py-3">
32
32
  <Icon name="trash-2" size="md" className="text-status-danger-fg" />
@@ -11,6 +11,7 @@ import {
11
11
  import { Icon } from "./Icon";
12
12
  import { nextCount } from "./toast-count";
13
13
  import { docModuleCache } from "../doc-module-cache";
14
+ import { isSelfWrite } from "../comments/self-writes";
14
15
 
15
16
  export type ToastTone = "info" | "ok" | "warn" | "danger";
16
17
 
@@ -148,7 +149,10 @@ export function ToastStack() {
148
149
  const handleDocChanged = (data: {
149
150
  files?: Array<{ path: string; root: string; rel: string }>;
150
151
  }) => {
151
- const files = (data?.files ?? []).filter((file) => docModuleCache.has(file.path));
152
+ // A comment this tab just wrote is not an outside change to announce.
153
+ const files = (data?.files ?? []).filter(
154
+ (file) => docModuleCache.has(file.path) && !isSelfWrite(file.path),
155
+ );
152
156
  const [first] = files;
153
157
  if (!first) return;
154
158
  if (files.length === 1) {
@@ -50,16 +50,21 @@ import Info from "lucide-react/dist/esm/icons/info.mjs";
50
50
  import Lightbulb from "lucide-react/dist/esm/icons/lightbulb.mjs";
51
51
  import ListChecks from "lucide-react/dist/esm/icons/list-checks.mjs";
52
52
  import Maximize from "lucide-react/dist/esm/icons/maximize.mjs";
53
+ import MessageSquare from "lucide-react/dist/esm/icons/message-square.mjs";
53
54
  import Minus from "lucide-react/dist/esm/icons/minus.mjs";
54
55
  import Moon from "lucide-react/dist/esm/icons/moon.mjs";
55
56
  import OctagonAlert from "lucide-react/dist/esm/icons/octagon-alert.mjs";
56
57
  import PanelLeft from "lucide-react/dist/esm/icons/panel-left.mjs";
58
+ import PanelRight from "lucide-react/dist/esm/icons/panel-right.mjs";
57
59
  import Pencil from "lucide-react/dist/esm/icons/pencil.mjs";
58
60
  import Plus from "lucide-react/dist/esm/icons/plus.mjs";
61
+ import RotateCcw from "lucide-react/dist/esm/icons/rotate-ccw.mjs";
59
62
  import Search from "lucide-react/dist/esm/icons/search.mjs";
63
+ import Sparkles from "lucide-react/dist/esm/icons/sparkles.mjs";
60
64
  import Sun from "lucide-react/dist/esm/icons/sun.mjs";
61
65
  import Trash2 from "lucide-react/dist/esm/icons/trash-2.mjs";
62
66
  import TriangleAlert from "lucide-react/dist/esm/icons/triangle-alert.mjs";
67
+ import User from "lucide-react/dist/esm/icons/user.mjs";
63
68
  import XIcon from "lucide-react/dist/esm/icons/x.mjs";
64
69
 
65
70
  export const staticIcons: Record<string, LucideIcon> = {
@@ -88,15 +93,20 @@ export const staticIcons: Record<string, LucideIcon> = {
88
93
  lightbulb: Lightbulb,
89
94
  "list-checks": ListChecks,
90
95
  maximize: Maximize,
96
+ "message-square": MessageSquare,
91
97
  minus: Minus,
92
98
  moon: Moon,
93
99
  "octagon-alert": OctagonAlert,
94
100
  "panel-left": PanelLeft,
101
+ "panel-right": PanelRight,
95
102
  pencil: Pencil,
96
103
  plus: Plus,
104
+ "rotate-ccw": RotateCcw,
97
105
  search: Search,
106
+ sparkles: Sparkles,
98
107
  sun: Sun,
99
108
  "trash-2": Trash2,
100
109
  "triangle-alert": TriangleAlert,
110
+ user: User,
101
111
  x: XIcon,
102
112
  };