@peaske7/readit 0.1.8 → 0.2.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 (221) hide show
  1. package/.claude/CLAUDE.md +118 -76
  2. package/.claude/commands/review.md +1 -1
  3. package/.claude/roadmap.md +32 -9
  4. package/.claude/user-stories.md +100 -15
  5. package/AGENTS.md +30 -26
  6. package/Makefile +32 -0
  7. package/README.md +90 -5
  8. package/biome.json +18 -8
  9. package/bun.lock +426 -710
  10. package/bunfig.toml +2 -0
  11. package/docs/perf-baseline.md +130 -0
  12. package/docs/superpowers/plans/2026-03-26-surgical-pruning.md +1176 -0
  13. package/docs/superpowers/specs/2026-03-27-go-server-rewrite-design.md +284 -0
  14. package/e2e/comments.spec.ts +14 -58
  15. package/e2e/document-load.spec.ts +1 -23
  16. package/e2e/export.spec.ts +4 -4
  17. package/e2e/perf/add-comment.spec.ts +116 -0
  18. package/e2e/perf/fixtures/generate.ts +327 -0
  19. package/e2e/perf/initial-load.spec.ts +49 -0
  20. package/e2e/perf/perf.setup.ts +23 -0
  21. package/e2e/perf/perf.teardown.ts +9 -0
  22. package/e2e/perf/screenshot-final.png +0 -0
  23. package/e2e/perf/scroll.spec.ts +39 -0
  24. package/e2e/perf/tab-switch.spec.ts +69 -0
  25. package/e2e/perf/text-selection.spec.ts +119 -0
  26. package/e2e/perf/utils/metrics.ts +350 -0
  27. package/e2e/perf/utils/perf-cli.ts +86 -0
  28. package/e2e/persistence-file.spec.ts +41 -26
  29. package/e2e/utils/selection.ts +17 -73
  30. package/go/cmd/readit/main.go +416 -0
  31. package/go/go.mod +20 -0
  32. package/go/go.sum +41 -0
  33. package/go/internal/server/anchor.go +302 -0
  34. package/go/internal/server/anchor_test.go +111 -0
  35. package/go/internal/server/comments.go +390 -0
  36. package/go/internal/server/documents.go +113 -0
  37. package/go/internal/server/embed.go +17 -0
  38. package/go/internal/server/headings.go +33 -0
  39. package/go/internal/server/headings_test.go +75 -0
  40. package/go/internal/server/htmltext.go +123 -0
  41. package/go/internal/server/markdown.go +157 -0
  42. package/go/internal/server/markdown_bench_test.go +42 -0
  43. package/go/internal/server/markdown_test.go +79 -0
  44. package/go/internal/server/server.go +453 -0
  45. package/go/internal/server/server_bench_test.go +122 -0
  46. package/go/internal/server/settings.go +110 -0
  47. package/go/internal/server/sse.go +140 -0
  48. package/go/internal/server/storage.go +275 -0
  49. package/go/internal/server/storage_test.go +118 -0
  50. package/go/internal/server/template.go +66 -0
  51. package/go/internal/server/types.go +101 -0
  52. package/go/internal/server/watcher.go +74 -0
  53. package/index.html +4 -14
  54. package/nvim-readit/lua/readit/health.lua +64 -0
  55. package/nvim-readit/lua/readit/init.lua +463 -0
  56. package/nvim-readit/plugin/readit.lua +19 -0
  57. package/package.json +24 -41
  58. package/playwright.config.ts +12 -0
  59. package/shell/_readit +158 -0
  60. package/shell/readit.zsh +87 -0
  61. package/src/App.svelte +881 -0
  62. package/src/{cli/index.ts → cli.ts} +216 -70
  63. package/src/components/ActionsMenu.svelte +95 -0
  64. package/src/components/CommentBadge.svelte +67 -0
  65. package/src/components/CommentErrorBanner.svelte +33 -0
  66. package/src/components/CommentInput.svelte +75 -0
  67. package/src/components/CommentListItem.svelte +95 -0
  68. package/src/components/CommentManager.svelte +129 -0
  69. package/src/components/CommentNav.svelte +109 -0
  70. package/src/components/DocumentViewer.svelte +218 -0
  71. package/src/components/FloatingComment.svelte +107 -0
  72. package/src/components/Header.svelte +76 -0
  73. package/src/components/InlineEditor.svelte +72 -0
  74. package/src/components/MarginNote.svelte +167 -0
  75. package/src/components/MarginNotesContainer.svelte +33 -0
  76. package/src/components/RawModal.svelte +126 -0
  77. package/src/components/ReanchorConfirm.svelte +30 -0
  78. package/src/components/SettingsModal.svelte +220 -0
  79. package/src/components/ShortcutCapture.svelte +82 -0
  80. package/src/components/ShortcutList.svelte +145 -0
  81. package/src/components/TabBar.svelte +52 -0
  82. package/src/components/TableOfContents.svelte +125 -0
  83. package/src/components/ui/ActionLink.svelte +40 -0
  84. package/src/components/ui/Button.svelte +53 -0
  85. package/src/components/ui/Dialog.svelte +97 -0
  86. package/src/components/ui/DropdownMenu.svelte +85 -0
  87. package/src/components/ui/DropdownMenuItem.svelte +38 -0
  88. package/src/components/ui/DropdownMenuSeparator.svelte +11 -0
  89. package/src/components/ui/Text.svelte +42 -0
  90. package/src/env.d.ts +6 -0
  91. package/src/index.css +36 -166
  92. package/src/lib/__fixtures__/bench-data.ts +1 -54
  93. package/src/lib/anchor.bench.ts +47 -68
  94. package/src/lib/anchor.test.ts +5 -9
  95. package/src/lib/anchor.ts +9 -93
  96. package/src/lib/comment-storage.bench.ts +6 -20
  97. package/src/lib/comment-storage.test.ts +45 -37
  98. package/src/lib/comment-storage.ts +23 -64
  99. package/src/lib/export.bench.ts +9 -23
  100. package/src/lib/export.ts +7 -14
  101. package/src/lib/headings.test.ts +103 -0
  102. package/src/lib/headings.ts +44 -0
  103. package/src/lib/highlight/core.test.ts +1 -6
  104. package/src/lib/highlight/dom.ts +53 -280
  105. package/src/lib/highlight/highlight-registry.ts +221 -0
  106. package/src/lib/highlight/highlight.bench.ts +92 -0
  107. package/src/lib/highlight/highlighter.ts +122 -302
  108. package/src/lib/highlight/{core.ts → resolver.ts} +3 -19
  109. package/src/lib/highlight/types.ts +0 -40
  110. package/src/lib/html-text.test.ts +162 -0
  111. package/src/lib/html-text.ts +161 -0
  112. package/src/lib/i18n/en.ts +13 -36
  113. package/src/lib/i18n/ja.ts +14 -37
  114. package/src/lib/i18n/types.ts +13 -36
  115. package/src/lib/margin-layout.bench.ts +48 -15
  116. package/src/lib/margin-layout.ts +2 -31
  117. package/src/lib/markdown-renderer.test.ts +154 -0
  118. package/src/lib/markdown-renderer.ts +177 -0
  119. package/src/lib/mermaid-config.ts +38 -0
  120. package/src/lib/mermaid-renderer.ts +162 -0
  121. package/src/lib/mermaid-worker.ts +60 -0
  122. package/src/lib/positions.ts +157 -0
  123. package/src/lib/shortcut-registry.ts +138 -103
  124. package/src/lib/utils.ts +2 -48
  125. package/src/main.ts +16 -0
  126. package/src/schema.ts +92 -0
  127. package/src/{server/index.ts → server.ts} +427 -163
  128. package/src/stores/app.svelte.ts +231 -0
  129. package/src/stores/locale.svelte.ts +46 -0
  130. package/src/stores/settings.svelte.ts +90 -0
  131. package/src/stores/shortcuts.svelte.ts +104 -0
  132. package/src/stores/ui.svelte.ts +12 -0
  133. package/src/template.ts +104 -0
  134. package/src/test-setup.ts +47 -0
  135. package/svelte.config.js +5 -0
  136. package/tsconfig.json +2 -2
  137. package/vite.config.ts +31 -3
  138. package/vscode-readit/.mcp.json +7 -0
  139. package/vscode-readit/.vscodeignore +7 -0
  140. package/vscode-readit/bun.lock +78 -0
  141. package/vscode-readit/icon.svg +10 -0
  142. package/vscode-readit/package.json +110 -0
  143. package/vscode-readit/src/extension.ts +117 -0
  144. package/vscode-readit/src/server-manager.ts +272 -0
  145. package/vscode-readit/src/webview-provider.ts +204 -0
  146. package/vscode-readit/tsconfig.json +20 -0
  147. package/e2e/fixtures/sample.html +0 -13
  148. package/src/App.tsx +0 -416
  149. package/src/components/ActionsMenu.tsx +0 -112
  150. package/src/components/DocumentViewer/CodeBlock.tsx +0 -160
  151. package/src/components/DocumentViewer/DocumentViewer.tsx +0 -259
  152. package/src/components/DocumentViewer/IframeContainer.tsx +0 -251
  153. package/src/components/DocumentViewer/InlineCode.tsx +0 -60
  154. package/src/components/DocumentViewer/MermaidDiagram.tsx +0 -137
  155. package/src/components/DocumentViewer/index.ts +0 -1
  156. package/src/components/FloatingTOC.tsx +0 -61
  157. package/src/components/Header.tsx +0 -65
  158. package/src/components/InlineEditor.tsx +0 -74
  159. package/src/components/MarginNote.tsx +0 -207
  160. package/src/components/MarginNotes.tsx +0 -50
  161. package/src/components/RawModal.tsx +0 -143
  162. package/src/components/ReanchorConfirm.tsx +0 -36
  163. package/src/components/SettingsModal.tsx +0 -310
  164. package/src/components/ShortcutCapture.tsx +0 -48
  165. package/src/components/ShortcutList.tsx +0 -198
  166. package/src/components/TabBar.tsx +0 -60
  167. package/src/components/TableOfContents.tsx +0 -108
  168. package/src/components/comments/CommentBadge.tsx +0 -49
  169. package/src/components/comments/CommentInput.tsx +0 -114
  170. package/src/components/comments/CommentListItem.tsx +0 -92
  171. package/src/components/comments/CommentManager.tsx +0 -113
  172. package/src/components/comments/CommentMinimap.tsx +0 -62
  173. package/src/components/comments/CommentNav.tsx +0 -109
  174. package/src/components/ui/ActionBar.tsx +0 -16
  175. package/src/components/ui/ActionLink.tsx +0 -32
  176. package/src/components/ui/Button.tsx +0 -55
  177. package/src/components/ui/Dialog.tsx +0 -156
  178. package/src/components/ui/DropdownMenu.tsx +0 -114
  179. package/src/components/ui/SeparatorDot.tsx +0 -9
  180. package/src/components/ui/Text.tsx +0 -54
  181. package/src/contexts/CommentContext.tsx +0 -229
  182. package/src/contexts/LayoutContext.tsx +0 -88
  183. package/src/contexts/LocaleContext.tsx +0 -35
  184. package/src/hooks/useClickOutside.ts +0 -35
  185. package/src/hooks/useClipboard.ts +0 -82
  186. package/src/hooks/useCommentNavigation.ts +0 -130
  187. package/src/hooks/useComments.ts +0 -323
  188. package/src/hooks/useDocument.ts +0 -156
  189. package/src/hooks/useEditorScheme.ts +0 -51
  190. package/src/hooks/useFontPreference.ts +0 -59
  191. package/src/hooks/useHeadings.test.ts +0 -159
  192. package/src/hooks/useHeadings.ts +0 -129
  193. package/src/hooks/useKeybindings.ts +0 -108
  194. package/src/hooks/useKeyboardShortcuts.ts +0 -63
  195. package/src/hooks/useLayoutMode.ts +0 -44
  196. package/src/hooks/useLocalePreference.ts +0 -42
  197. package/src/hooks/useReanchorMode.ts +0 -33
  198. package/src/hooks/useScrollMetrics.ts +0 -56
  199. package/src/hooks/useScrollSpy.ts +0 -81
  200. package/src/hooks/useTextSelection.ts +0 -123
  201. package/src/hooks/useThemePreference.ts +0 -66
  202. package/src/lib/context.bench.ts +0 -41
  203. package/src/lib/context.test.ts +0 -224
  204. package/src/lib/context.ts +0 -193
  205. package/src/lib/editor-links.ts +0 -59
  206. package/src/lib/highlight/colors.ts +0 -37
  207. package/src/lib/highlight/index.ts +0 -23
  208. package/src/lib/highlight/script-builder.ts +0 -485
  209. package/src/lib/html-processor.test.tsx +0 -170
  210. package/src/lib/html-processor.tsx +0 -95
  211. package/src/lib/i18n/completeness.test.ts +0 -51
  212. package/src/lib/i18n/translations.test.ts +0 -39
  213. package/src/lib/layout-constants.ts +0 -12
  214. package/src/lib/scroll.test.ts +0 -118
  215. package/src/lib/scroll.ts +0 -47
  216. package/src/lib/shortcut-registry.test.ts +0 -173
  217. package/src/lib/utils.test.ts +0 -110
  218. package/src/main.tsx +0 -13
  219. package/src/store/index.test.ts +0 -242
  220. package/src/store/index.ts +0 -254
  221. package/src/types/index.ts +0 -127
@@ -1,112 +0,0 @@
1
- import {
2
- BotMessageSquare,
3
- FileDown,
4
- FileText,
5
- Maximize2,
6
- Minimize2,
7
- MoreHorizontal,
8
- RefreshCw,
9
- Settings,
10
- TextQuote,
11
- } from "lucide-react";
12
- import { useState } from "react";
13
- import { useCommentContext } from "../contexts/CommentContext";
14
- import { useLayoutContext } from "../contexts/LayoutContext";
15
- import { useLocale } from "../contexts/LocaleContext";
16
- import { RawModal } from "./RawModal";
17
- import { SettingsModal } from "./SettingsModal";
18
- import { Button } from "./ui/Button";
19
- import {
20
- DropdownMenu,
21
- DropdownMenuContent,
22
- DropdownMenuItem,
23
- DropdownMenuSeparator,
24
- DropdownMenuTrigger,
25
- } from "./ui/DropdownMenu";
26
-
27
- interface ActionsMenuProps {
28
- onCopyAll: () => void;
29
- onCopyAllRaw: () => void;
30
- onExportJson: () => void;
31
- onReload: () => void;
32
- }
33
-
34
- export function ActionsMenu({
35
- onCopyAll,
36
- onCopyAllRaw,
37
- onExportJson,
38
- onReload,
39
- }: ActionsMenuProps) {
40
- const { commentCount } = useCommentContext();
41
- const { isFullscreen, toggleLayoutMode } = useLayoutContext();
42
- const { t } = useLocale();
43
-
44
- const [menuOpen, setMenuOpen] = useState(false);
45
- const [rawModalOpen, setRawModalOpen] = useState(false);
46
- const [settingsOpen, setSettingsOpen] = useState(false);
47
-
48
- return (
49
- <>
50
- <DropdownMenu open={menuOpen} onOpenChange={setMenuOpen}>
51
- <DropdownMenuTrigger asChild>
52
- <Button
53
- variant="ghost"
54
- size="icon"
55
- className="size-7"
56
- aria-label={t("actions.ariaLabel")}
57
- >
58
- <MoreHorizontal className="w-4 h-4" />
59
- </Button>
60
- </DropdownMenuTrigger>
61
- <DropdownMenuContent align="end" className="min-w-[160px]">
62
- <DropdownMenuItem onSelect={() => toggleLayoutMode()}>
63
- {isFullscreen ? <Minimize2 /> : <Maximize2 />}
64
- {isFullscreen ? t("actions.centered") : t("actions.fullscreen")}
65
- </DropdownMenuItem>
66
- <DropdownMenuItem onSelect={() => setSettingsOpen(true)}>
67
- <Settings />
68
- {t("actions.settings")}
69
- </DropdownMenuItem>
70
- <DropdownMenuSeparator />
71
- <DropdownMenuItem onSelect={() => onReload()}>
72
- <RefreshCw />
73
- {t("actions.reload")}
74
- </DropdownMenuItem>
75
- {commentCount > 0 && (
76
- <>
77
- <DropdownMenuItem
78
- onSelect={() => onCopyAll()}
79
- title={t("actions.copyAllAITitle")}
80
- >
81
- <BotMessageSquare />
82
- {t("actions.copyAllAI")}
83
- </DropdownMenuItem>
84
- <DropdownMenuItem
85
- onSelect={() => onCopyAllRaw()}
86
- title={t("actions.copyAllRawTitle")}
87
- >
88
- <TextQuote />
89
- {t("actions.copyAllRaw")}
90
- </DropdownMenuItem>
91
- <DropdownMenuItem onSelect={() => onExportJson()}>
92
- <FileDown />
93
- {t("actions.exportJson")}
94
- </DropdownMenuItem>
95
- <DropdownMenuItem onSelect={() => setRawModalOpen(true)}>
96
- <FileText />
97
- {t("actions.viewRaw")}
98
- </DropdownMenuItem>
99
- </>
100
- )}
101
- </DropdownMenuContent>
102
- </DropdownMenu>
103
-
104
- <RawModal isOpen={rawModalOpen} onClose={() => setRawModalOpen(false)} />
105
-
106
- <SettingsModal
107
- isOpen={settingsOpen}
108
- onClose={() => setSettingsOpen(false)}
109
- />
110
- </>
111
- );
112
- }
@@ -1,160 +0,0 @@
1
- import { useEffect, useState } from "react";
2
- import { MermaidDiagram } from "./MermaidDiagram";
3
-
4
- const CODE_BLOCK_STYLE = {
5
- margin: "1.5em 0",
6
- borderRadius: "0.5em",
7
- fontSize: "0.875em",
8
- };
9
-
10
- interface SyntaxHighlighterModule {
11
- SyntaxHighlighter: typeof import("react-syntax-highlighter").PrismLight;
12
- oneDark: typeof import("react-syntax-highlighter/dist/esm/styles/prism").oneDark;
13
- }
14
-
15
- interface CodeBlockProps {
16
- className?: string;
17
- children?: React.ReactNode;
18
- }
19
-
20
- let syntaxHighlighterPromise: Promise<SyntaxHighlighterModule> | null = null;
21
-
22
- async function loadSyntaxHighlighter(): Promise<SyntaxHighlighterModule> {
23
- if (syntaxHighlighterPromise) {
24
- return syntaxHighlighterPromise;
25
- }
26
-
27
- syntaxHighlighterPromise = Promise.all([
28
- import("react-syntax-highlighter"),
29
- import("react-syntax-highlighter/dist/esm/styles/prism"),
30
- import("react-syntax-highlighter/dist/esm/languages/prism/bash"),
31
- import("react-syntax-highlighter/dist/esm/languages/prism/css"),
32
- import("react-syntax-highlighter/dist/esm/languages/prism/diff"),
33
- import("react-syntax-highlighter/dist/esm/languages/prism/go"),
34
- import("react-syntax-highlighter/dist/esm/languages/prism/graphql"),
35
- import("react-syntax-highlighter/dist/esm/languages/prism/javascript"),
36
- import("react-syntax-highlighter/dist/esm/languages/prism/json"),
37
- import("react-syntax-highlighter/dist/esm/languages/prism/jsx"),
38
- import("react-syntax-highlighter/dist/esm/languages/prism/markdown"),
39
- import("react-syntax-highlighter/dist/esm/languages/prism/python"),
40
- import("react-syntax-highlighter/dist/esm/languages/prism/rust"),
41
- import("react-syntax-highlighter/dist/esm/languages/prism/sql"),
42
- import("react-syntax-highlighter/dist/esm/languages/prism/tsx"),
43
- import("react-syntax-highlighter/dist/esm/languages/prism/typescript"),
44
- import("react-syntax-highlighter/dist/esm/languages/prism/yaml"),
45
- ]).then(
46
- ([
47
- syntaxModule,
48
- styleModule,
49
- bash,
50
- css,
51
- diff,
52
- go,
53
- graphql,
54
- javascript,
55
- json,
56
- jsx,
57
- markdown,
58
- python,
59
- rust,
60
- sql,
61
- tsx,
62
- typescript,
63
- yaml,
64
- ]) => {
65
- const SyntaxHighlighter = syntaxModule.PrismLight;
66
-
67
- SyntaxHighlighter.registerLanguage("bash", bash.default);
68
- SyntaxHighlighter.registerLanguage("sh", bash.default);
69
- SyntaxHighlighter.registerLanguage("shell", bash.default);
70
- SyntaxHighlighter.registerLanguage("css", css.default);
71
- SyntaxHighlighter.registerLanguage("diff", diff.default);
72
- SyntaxHighlighter.registerLanguage("go", go.default);
73
- SyntaxHighlighter.registerLanguage("graphql", graphql.default);
74
- SyntaxHighlighter.registerLanguage("javascript", javascript.default);
75
- SyntaxHighlighter.registerLanguage("js", javascript.default);
76
- SyntaxHighlighter.registerLanguage("json", json.default);
77
- SyntaxHighlighter.registerLanguage("jsx", jsx.default);
78
- SyntaxHighlighter.registerLanguage("markdown", markdown.default);
79
- SyntaxHighlighter.registerLanguage("md", markdown.default);
80
- SyntaxHighlighter.registerLanguage("python", python.default);
81
- SyntaxHighlighter.registerLanguage("py", python.default);
82
- SyntaxHighlighter.registerLanguage("rust", rust.default);
83
- SyntaxHighlighter.registerLanguage("rs", rust.default);
84
- SyntaxHighlighter.registerLanguage("sql", sql.default);
85
- SyntaxHighlighter.registerLanguage("tsx", tsx.default);
86
- SyntaxHighlighter.registerLanguage("typescript", typescript.default);
87
- SyntaxHighlighter.registerLanguage("ts", typescript.default);
88
- SyntaxHighlighter.registerLanguage("yaml", yaml.default);
89
- SyntaxHighlighter.registerLanguage("yml", yaml.default);
90
-
91
- return {
92
- SyntaxHighlighter,
93
- oneDark: styleModule.oneDark,
94
- };
95
- },
96
- );
97
-
98
- return syntaxHighlighterPromise;
99
- }
100
-
101
- function LazySyntaxCodeBlock({
102
- codeString,
103
- language,
104
- }: {
105
- codeString: string;
106
- language: string;
107
- }) {
108
- const [module, setModule] = useState<SyntaxHighlighterModule | null>(null);
109
-
110
- useEffect(() => {
111
- let cancelled = false;
112
-
113
- loadSyntaxHighlighter().then((loaded) => {
114
- if (!cancelled) {
115
- setModule(loaded);
116
- }
117
- });
118
-
119
- return () => {
120
- cancelled = true;
121
- };
122
- }, []);
123
-
124
- if (!module) {
125
- return (
126
- <pre style={CODE_BLOCK_STYLE}>
127
- <code>{codeString}</code>
128
- </pre>
129
- );
130
- }
131
-
132
- const { SyntaxHighlighter, oneDark } = module;
133
-
134
- return (
135
- <SyntaxHighlighter
136
- style={oneDark}
137
- language={language}
138
- PreTag="div"
139
- customStyle={CODE_BLOCK_STYLE}
140
- >
141
- {codeString}
142
- </SyntaxHighlighter>
143
- );
144
- }
145
-
146
- export function CodeBlock({ className, children }: CodeBlockProps) {
147
- const langMatch = className?.match(/language-(\w+)/);
148
- const language = langMatch?.[1] ?? "";
149
- const codeString = String(children).replace(/\n$/, "");
150
-
151
- if (language === "mermaid") {
152
- return <MermaidDiagram code={codeString} />;
153
- }
154
-
155
- if (!langMatch && !String(children).includes("\n")) {
156
- return <code className={className}>{children}</code>;
157
- }
158
-
159
- return <LazySyntaxCodeBlock codeString={codeString} language={language} />;
160
- }
@@ -1,259 +0,0 @@
1
- import {
2
- type ComponentPropsWithoutRef,
3
- type MutableRefObject,
4
- useEffect,
5
- useMemo,
6
- useRef,
7
- } from "react";
8
- import Markdown from "react-markdown";
9
- import rehypeRaw from "rehype-raw";
10
- import remarkGfm from "remark-gfm";
11
- import { useLayoutContext } from "../../contexts/LayoutContext";
12
- import type { Heading } from "../../hooks/useHeadings";
13
- import {
14
- createHighlighter,
15
- type HighlightComment,
16
- type Highlighter,
17
- } from "../../lib/highlight";
18
- import { cn, getTextContent } from "../../lib/utils";
19
- import { useAppStore } from "../../store";
20
- import {
21
- AnchorConfidences,
22
- type Comment,
23
- type DocumentType,
24
- FontFamilies,
25
- type SelectionRange,
26
- } from "../../types";
27
- import { IframeContainer } from "./IframeContainer";
28
- import { createCodeComponent } from "./InlineCode";
29
-
30
- function createHeadingComponent(
31
- level: 1 | 2 | 3 | 4 | 5 | 6,
32
- headings: Heading[],
33
- headingIndexRef: MutableRefObject<number>,
34
- ) {
35
- const Tag = `h${level}` as const;
36
-
37
- return function HeadingComponent({
38
- children,
39
- ...props
40
- }: ComponentPropsWithoutRef<typeof Tag>) {
41
- const text = getTextContent(children);
42
-
43
- // Find the next heading in the pre-computed list that matches this level and text
44
- // This handles React Strict Mode double-renders by always looking forward from current index
45
- let id = "";
46
- for (let i = headingIndexRef.current; i < headings.length; i++) {
47
- const heading = headings[i];
48
- if (heading.level === level && heading.text === text) {
49
- id = heading.id;
50
- headingIndexRef.current = i + 1;
51
- break;
52
- }
53
- }
54
-
55
- // Fallback: if not found (shouldn't happen), search from beginning
56
- if (!id) {
57
- for (const heading of headings) {
58
- if (heading.level === level && heading.text === text) {
59
- id = heading.id;
60
- break;
61
- }
62
- }
63
- }
64
-
65
- return (
66
- <Tag id={id} {...props}>
67
- {children}
68
- </Tag>
69
- );
70
- };
71
- }
72
-
73
- interface DocumentViewerProps {
74
- content: string;
75
- type: DocumentType;
76
- comments: Comment[];
77
- headings: Heading[];
78
- pendingSelection?: SelectionRange;
79
- onTextSelect: (
80
- text: string,
81
- startOffset: number,
82
- endOffset: number,
83
- selectionTop: number,
84
- ) => void;
85
- onHighlightPositionsChange?: (
86
- positions: Record<string, number>,
87
- documentPositions: Record<string, number>,
88
- pendingTop?: number,
89
- ) => void;
90
- onHighlightHover?: (commentId: string | undefined) => void;
91
- onHighlightClick?: (commentId: string) => void;
92
- }
93
-
94
- export function DocumentViewer({
95
- content,
96
- type,
97
- comments,
98
- headings,
99
- pendingSelection,
100
- onTextSelect,
101
- onHighlightPositionsChange,
102
- onHighlightHover,
103
- onHighlightClick,
104
- }: DocumentViewerProps) {
105
- const { isFullscreen, fontFamily, editorScheme } = useLayoutContext();
106
- const workingDirectory = useAppStore((s) => s.workingDirectory);
107
- const contentRef = useRef<HTMLDivElement>(null);
108
- const containerRef = useRef<HTMLDivElement>(null);
109
- const adapterRef = useRef<Highlighter | null>(null);
110
- const headingIndexRef = useRef(0);
111
-
112
- useEffect(() => {
113
- if (type !== "markdown") return;
114
- if (!contentRef.current || !containerRef.current) return;
115
-
116
- const adapter = createHighlighter({
117
- type: "markdown",
118
- root: contentRef.current,
119
- container: containerRef.current,
120
- onSelect: onTextSelect,
121
- });
122
-
123
- adapterRef.current = adapter;
124
-
125
- const unsubPositions = onHighlightPositionsChange
126
- ? adapter.onPositionsChange((pos) => {
127
- onHighlightPositionsChange(
128
- pos.positions,
129
- pos.documentPositions,
130
- pos.pendingTop,
131
- );
132
- })
133
- : () => {};
134
-
135
- const unsubHover = onHighlightHover
136
- ? adapter.onHighlightHover(onHighlightHover)
137
- : () => {};
138
-
139
- const unsubClick = onHighlightClick
140
- ? adapter.onHighlightClick(onHighlightClick)
141
- : () => {};
142
-
143
- return () => {
144
- unsubPositions();
145
- unsubHover();
146
- unsubClick();
147
- adapter.dispose();
148
- adapterRef.current = null;
149
- };
150
- }, [
151
- type,
152
- onTextSelect,
153
- onHighlightPositionsChange,
154
- onHighlightHover,
155
- onHighlightClick,
156
- ]);
157
-
158
- // Double RAF: ensures React commit phase completes before DOM queries.
159
- // See: https://github.com/facebook/react/issues/20863
160
- // biome-ignore lint/correctness/useExhaustiveDependencies: must reapply highlights when content or components change
161
- useEffect(() => {
162
- if (type !== "markdown") return;
163
-
164
- let outerFrameId: number;
165
- let innerFrameId: number;
166
-
167
- outerFrameId = requestAnimationFrame(() => {
168
- innerFrameId = requestAnimationFrame(() => {
169
- const adapter = adapterRef.current;
170
- if (!adapter) return;
171
-
172
- const highlightComments: HighlightComment[] = comments
173
- .filter((c) => c.anchorConfidence !== AnchorConfidences.UNRESOLVED)
174
- .map((c) => ({
175
- id: c.id,
176
- selectedText: c.selectedText,
177
- startOffset: c.startOffset,
178
- endOffset: c.endOffset,
179
- }));
180
-
181
- adapter.applyHighlights(highlightComments);
182
- });
183
- });
184
-
185
- return () => {
186
- cancelAnimationFrame(outerFrameId);
187
- cancelAnimationFrame(innerFrameId);
188
- };
189
- // editorScheme/workingDirectory: when these change, markdownComponents memo recomputes,
190
- // react-markdown replaces the DOM, so highlights must be reapplied
191
- }, [comments, content, type, editorScheme, workingDirectory]);
192
-
193
- useEffect(() => {
194
- if (type !== "markdown") return;
195
-
196
- const handleTestSelect = (e: Event) => {
197
- const { text, startOffset, endOffset } = (e as CustomEvent).detail;
198
- onTextSelect(text, startOffset, endOffset, 0);
199
- };
200
-
201
- window.addEventListener("test:select-text", handleTestSelect);
202
- return () =>
203
- window.removeEventListener("test:select-text", handleTestSelect);
204
- }, [type, onTextSelect]);
205
-
206
- // Memoized to prevent DOM node replacement (breaks highlight persistence)
207
- const markdownComponents = useMemo(
208
- () => ({
209
- h1: createHeadingComponent(1, headings, headingIndexRef),
210
- h2: createHeadingComponent(2, headings, headingIndexRef),
211
- h3: createHeadingComponent(3, headings, headingIndexRef),
212
- h4: createHeadingComponent(4, headings, headingIndexRef),
213
- h5: createHeadingComponent(5, headings, headingIndexRef),
214
- h6: createHeadingComponent(6, headings, headingIndexRef),
215
- code: createCodeComponent(editorScheme, workingDirectory),
216
- }),
217
- [headings, editorScheme, workingDirectory],
218
- );
219
-
220
- if (type === "html") {
221
- return (
222
- <main className="flex-1 min-w-0 flex flex-col">
223
- <IframeContainer
224
- html={content}
225
- comments={comments}
226
- pendingSelection={pendingSelection}
227
- onTextSelect={onTextSelect}
228
- onHighlightPositionsChange={onHighlightPositionsChange}
229
- onHighlightHover={onHighlightHover}
230
- onHighlightClick={onHighlightClick}
231
- fontFamily={fontFamily}
232
- />
233
- </main>
234
- );
235
- }
236
-
237
- headingIndexRef.current = 0;
238
-
239
- return (
240
- <div ref={containerRef} className="flex-1 min-w-0">
241
- <article
242
- ref={contentRef}
243
- className={cn(
244
- "prose",
245
- isFullscreen && "prose-fullscreen",
246
- fontFamily === FontFamilies.SANS_SERIF ? "prose-sans" : "prose-serif",
247
- )}
248
- >
249
- <Markdown
250
- components={markdownComponents}
251
- remarkPlugins={[remarkGfm]}
252
- rehypePlugins={[rehypeRaw]}
253
- >
254
- {content}
255
- </Markdown>
256
- </article>
257
- </div>
258
- );
259
- }