@plannotator/ui 0.22.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 (305) hide show
  1. package/README.md +60 -0
  2. package/assets/diff-options.png +0 -0
  3. package/assets/icon-codex.png +0 -0
  4. package/assets/look-flat.png +0 -0
  5. package/assets/look-grid.png +0 -0
  6. package/assets/review-sections.png +0 -0
  7. package/assets/review-tree.png +0 -0
  8. package/assets/workspaces.webp +0 -0
  9. package/components/AISettingsTab.tsx +145 -0
  10. package/components/ActionMenu.tsx +101 -0
  11. package/components/AgentControls.tsx +216 -0
  12. package/components/AgentsTab.tsx +1294 -0
  13. package/components/AnnotationPanel.tsx +731 -0
  14. package/components/AnnotationSidebar.tsx +86 -0
  15. package/components/AnnotationToolbar.tsx +323 -0
  16. package/components/AnnotationToolstrip.tsx +355 -0
  17. package/components/ApproveDropdown.tsx +170 -0
  18. package/components/AttachmentsButton.tsx +410 -0
  19. package/components/BlockRenderer.tsx +163 -0
  20. package/components/BorderTrail.tsx +38 -0
  21. package/components/CodeFilePicker.tsx +64 -0
  22. package/components/CodeFilePopout.tsx +602 -0
  23. package/components/CodePathValidationContext.tsx +14 -0
  24. package/components/CommentPopover.tsx +512 -0
  25. package/components/CompletionOverlay.tsx +99 -0
  26. package/components/ConfirmDialog.tsx +128 -0
  27. package/components/DocBadges.tsx +231 -0
  28. package/components/EditorAnnotationCard.tsx +65 -0
  29. package/components/ExportModal.tsx +565 -0
  30. package/components/FloatingQuickLabelPicker.tsx +144 -0
  31. package/components/GitHubIcon.tsx +16 -0
  32. package/components/GitLabIcon.tsx +30 -0
  33. package/components/GraphvizBlock.tsx +511 -0
  34. package/components/ImageAnnotator/Canvas.tsx +118 -0
  35. package/components/ImageAnnotator/Toolbar.tsx +218 -0
  36. package/components/ImageAnnotator/index.tsx +272 -0
  37. package/components/ImageAnnotator/types.ts +39 -0
  38. package/components/ImageAnnotator/utils.ts +147 -0
  39. package/components/ImageThumbnail.tsx +126 -0
  40. package/components/ImportModal.tsx +146 -0
  41. package/components/InlineMarkdown.tsx +1094 -0
  42. package/components/KeyboardShortcuts.tsx +208 -0
  43. package/components/Landing.tsx +496 -0
  44. package/components/ListItemBody.tsx +62 -0
  45. package/components/ListMarker.tsx +73 -0
  46. package/components/LookAndFeelAnnouncementDialog.tsx +230 -0
  47. package/components/MarkdownEditor.tsx +44 -0
  48. package/components/MenuVersionSection.tsx +91 -0
  49. package/components/MermaidBlock.tsx +579 -0
  50. package/components/ModeToggle.tsx +68 -0
  51. package/components/OpenInAppButton.tsx +299 -0
  52. package/components/OverlayScrollArea.tsx +82 -0
  53. package/components/PermissionModeSetup.tsx +92 -0
  54. package/components/PinpointOverlay.tsx +99 -0
  55. package/components/PlanAIAnnouncementDialog.tsx +187 -0
  56. package/components/PlanHeaderMenu.tsx +299 -0
  57. package/components/PopoutDialog.tsx +91 -0
  58. package/components/Popover.tsx +27 -0
  59. package/components/ProviderIcons.tsx +51 -0
  60. package/components/PullRequestIcon.tsx +12 -0
  61. package/components/QuickLabelDropdown.tsx +63 -0
  62. package/components/RenderedMarkdown.tsx +54 -0
  63. package/components/RepoIcon.tsx +12 -0
  64. package/components/ResizeHandle.tsx +86 -0
  65. package/components/ReviewAgentsIcon.tsx +18 -0
  66. package/components/SearchableSelect.tsx +157 -0
  67. package/components/Settings.tsx +2220 -0
  68. package/components/SparklesIcon.tsx +45 -0
  69. package/components/StickyHeaderLane.tsx +260 -0
  70. package/components/TableOfContents.tsx +157 -0
  71. package/components/TaterSpritePullup.tsx +34 -0
  72. package/components/TaterSpriteRunning.tsx +54 -0
  73. package/components/TaterSpriteSitting.tsx +37 -0
  74. package/components/TextShimmer.tsx +57 -0
  75. package/components/ThemeProvider.tsx +162 -0
  76. package/components/ThemeTab.tsx +139 -0
  77. package/components/ToolbarButtons.tsx +116 -0
  78. package/components/Tooltip.tsx +45 -0
  79. package/components/Viewer.tsx +977 -0
  80. package/components/ai/AIProviderBar.tsx +95 -0
  81. package/components/ai/DocumentAIChatPanel.tsx +313 -0
  82. package/components/blocks/AlertBlock.tsx +58 -0
  83. package/components/blocks/Callout.tsx +60 -0
  84. package/components/blocks/CodeBlock.tsx +75 -0
  85. package/components/blocks/HtmlBlock.tsx +122 -0
  86. package/components/blocks/MathBlock.tsx +36 -0
  87. package/components/blocks/TableBlock.tsx +146 -0
  88. package/components/blocks/TablePopout.tsx +279 -0
  89. package/components/blocks/TableToolbar.tsx +153 -0
  90. package/components/blocks/proseBody.tsx +102 -0
  91. package/components/core/button.tsx +44 -0
  92. package/components/core/textarea.tsx +26 -0
  93. package/components/diagramLanguages.ts +14 -0
  94. package/components/goal-setup/GoalSetupSurface.tsx +1354 -0
  95. package/components/html-viewer/HtmlViewer.tsx +386 -0
  96. package/components/html-viewer/bridge-script.ts +505 -0
  97. package/components/html-viewer/index.ts +1 -0
  98. package/components/html-viewer/useHtmlAnnotation.ts +400 -0
  99. package/components/icons/AgentIcons.tsx +66 -0
  100. package/components/icons/AppIcon.tsx +56 -0
  101. package/components/icons/MessagesIcon.tsx +11 -0
  102. package/components/icons/ObsidianIcons.tsx +208 -0
  103. package/components/icons/app/android-studio.svg +369 -0
  104. package/components/icons/app/antigravity.svg +97 -0
  105. package/components/icons/app/cursor.svg +16 -0
  106. package/components/icons/app/file-explorer.svg +20 -0
  107. package/components/icons/app/finder.png +0 -0
  108. package/components/icons/app/ghostty.svg +13 -0
  109. package/components/icons/app/iterm2.svg +13 -0
  110. package/components/icons/app/powershell.svg +14 -0
  111. package/components/icons/app/sublime-text.svg +17 -0
  112. package/components/icons/app/terminal.png +0 -0
  113. package/components/icons/app/textmate.png +0 -0
  114. package/components/icons/app/vscode.svg +39 -0
  115. package/components/icons/app/warp.png +0 -0
  116. package/components/icons/app/xcode.png +0 -0
  117. package/components/icons/app/zed-dark.svg +15 -0
  118. package/components/icons/app/zed.svg +15 -0
  119. package/components/icons/themeIcons.tsx +47 -0
  120. package/components/mermaidSvg.ts +33 -0
  121. package/components/plan-diff/PlanCleanDiffView.tsx +884 -0
  122. package/components/plan-diff/PlanDiffBadge.tsx +48 -0
  123. package/components/plan-diff/PlanDiffModeSwitcher.tsx +103 -0
  124. package/components/plan-diff/PlanDiffViewer.tsx +214 -0
  125. package/components/plan-diff/PlanRawDiffView.tsx +102 -0
  126. package/components/plan-diff/VSCodeIcon.tsx +132 -0
  127. package/components/settings/HooksTab.tsx +208 -0
  128. package/components/sidebar/ArchiveBrowser.tsx +98 -0
  129. package/components/sidebar/CountBadge.tsx +12 -0
  130. package/components/sidebar/FileBrowser.tsx +498 -0
  131. package/components/sidebar/MessagesBrowser.tsx +109 -0
  132. package/components/sidebar/SidebarContainer.tsx +353 -0
  133. package/components/sidebar/SidebarTabs.tsx +155 -0
  134. package/components/sidebar/VersionBrowser.tsx +141 -0
  135. package/components/types.d.ts +7 -0
  136. package/components/ui/badge.tsx +44 -0
  137. package/components/ui/button.tsx +83 -0
  138. package/components/ui/card.tsx +58 -0
  139. package/components/ui/dialog.tsx +106 -0
  140. package/components/ui/dropdown-menu.tsx +240 -0
  141. package/components/ui/state-pill.tsx +45 -0
  142. package/components/ui/tabs.tsx +46 -0
  143. package/components/ui/textarea.tsx +25 -0
  144. package/config/configStore.ts +214 -0
  145. package/config/index.ts +4 -0
  146. package/config/reviewView.ts +42 -0
  147. package/config/settings.ts +310 -0
  148. package/config/useConfig.ts +20 -0
  149. package/configure.ts +69 -0
  150. package/globals.d.ts +14 -0
  151. package/hooks/pfm/useCodeFilePopout.ts +111 -0
  152. package/hooks/useAIChat.ts +532 -0
  153. package/hooks/useAIProviderConfig.ts +115 -0
  154. package/hooks/useActiveSection.ts +78 -0
  155. package/hooks/useAgentJobs.ts +306 -0
  156. package/hooks/useAgentSettings.ts +579 -0
  157. package/hooks/useAgents.ts +90 -0
  158. package/hooks/useAnnotationDraft.ts +523 -0
  159. package/hooks/useAnnotationHighlighter.ts +1170 -0
  160. package/hooks/useArchive.ts +171 -0
  161. package/hooks/useAutoClose.ts +104 -0
  162. package/hooks/useCodeAnnotationDraft.ts +194 -0
  163. package/hooks/useCodeFilePopout.ts +1 -0
  164. package/hooks/useDismissOnOutsideAndEscape.ts +50 -0
  165. package/hooks/useDraggable.ts +108 -0
  166. package/hooks/useEditorAnnotations.ts +64 -0
  167. package/hooks/useExternalAnnotationHighlights.ts +105 -0
  168. package/hooks/useExternalAnnotations.ts +268 -0
  169. package/hooks/useFileBrowser.ts +402 -0
  170. package/hooks/useInputMethodSwitch.ts +89 -0
  171. package/hooks/useIsMobile.ts +17 -0
  172. package/hooks/useLinkedDoc.ts +494 -0
  173. package/hooks/useOverlayViewport.ts +36 -0
  174. package/hooks/usePinpoint.ts +182 -0
  175. package/hooks/usePlanDiff.ts +176 -0
  176. package/hooks/usePrintMode.ts +27 -0
  177. package/hooks/useResizablePanel.ts +173 -0
  178. package/hooks/useScrollViewport.ts +38 -0
  179. package/hooks/useSharing.ts +441 -0
  180. package/hooks/useSidebar.ts +55 -0
  181. package/hooks/useUpdateCheck.ts +132 -0
  182. package/hooks/useValidatedCodePaths.ts +94 -0
  183. package/icons/GitUser.tsx +17 -0
  184. package/lib/utils.ts +11 -0
  185. package/package.json +117 -0
  186. package/plannotator.webp +0 -0
  187. package/print.css +427 -0
  188. package/shortcuts/code-review/ai.shortcuts.ts +25 -0
  189. package/shortcuts/code-review/allFilesDiff.shortcuts.ts +46 -0
  190. package/shortcuts/code-review/annotationToolbar.shortcuts.ts +30 -0
  191. package/shortcuts/code-review/fileTree.shortcuts.ts +35 -0
  192. package/shortcuts/code-review/prComments.shortcuts.ts +18 -0
  193. package/shortcuts/code-review/suggestionModal.shortcuts.ts +25 -0
  194. package/shortcuts/code-review/tourDialog.shortcuts.ts +18 -0
  195. package/shortcuts/core.ts +399 -0
  196. package/shortcuts/index.ts +21 -0
  197. package/shortcuts/plan-review/annotationPanel.shortcuts.ts +25 -0
  198. package/shortcuts/plan-review/annotationToolbar.shortcuts.ts +39 -0
  199. package/shortcuts/plan-review/commentPopover.shortcuts.ts +21 -0
  200. package/shortcuts/plan-review/goalSetup.shortcuts.ts +10 -0
  201. package/shortcuts/plan-review/imageAnnotator.shortcuts.ts +42 -0
  202. package/shortcuts/plan-review/inputMethod.shortcuts.ts +30 -0
  203. package/shortcuts/plan-review/sidebar.shortcuts.ts +34 -0
  204. package/shortcuts/plan-review/viewer.shortcuts.ts +24 -0
  205. package/shortcuts/runtime.ts +259 -0
  206. package/sprite_package_additional/index.html +34 -0
  207. package/sprite_package_additional/sprite.png +0 -0
  208. package/sprite_package_new/index.html +34 -0
  209. package/sprite_package_new/sprite.png +0 -0
  210. package/sprite_package_pulluphang/index.html +34 -0
  211. package/sprite_package_pulluphang/sprite.png +0 -0
  212. package/styles.css +1 -0
  213. package/theme.css +893 -0
  214. package/themes/adwaita.css +62 -0
  215. package/themes/andromeeda.css +66 -0
  216. package/themes/aurora-x.css +66 -0
  217. package/themes/ayu-dark.css +66 -0
  218. package/themes/caffeine.css +113 -0
  219. package/themes/catppuccin.css +62 -0
  220. package/themes/claude-plus.css +60 -0
  221. package/themes/cursor-hc.css +34 -0
  222. package/themes/cursor-midnight.css +34 -0
  223. package/themes/cursor.css +62 -0
  224. package/themes/dark-plus.css +66 -0
  225. package/themes/doom-64.css +109 -0
  226. package/themes/dracula.css +33 -0
  227. package/themes/everforest-hard.css +62 -0
  228. package/themes/everforest-soft.css +62 -0
  229. package/themes/everforest.css +62 -0
  230. package/themes/github.css +66 -0
  231. package/themes/gruvbox.css +62 -0
  232. package/themes/houston.css +66 -0
  233. package/themes/kanagawa-dragon.css +34 -0
  234. package/themes/kanagawa-lotus.css +34 -0
  235. package/themes/kanagawa-wave.css +34 -0
  236. package/themes/laserwave.css +66 -0
  237. package/themes/material.css +62 -0
  238. package/themes/min.css +66 -0
  239. package/themes/monokai-pro.css +34 -0
  240. package/themes/neutral.css +59 -0
  241. package/themes/night-owl.css +66 -0
  242. package/themes/nord.css +66 -0
  243. package/themes/one-dark-pro.css +66 -0
  244. package/themes/one-light.css +66 -0
  245. package/themes/paulmillr.css +34 -0
  246. package/themes/plannotator.css +60 -0
  247. package/themes/plastic.css +66 -0
  248. package/themes/poimandres.css +66 -0
  249. package/themes/quantum-rose.css +109 -0
  250. package/themes/red.css +66 -0
  251. package/themes/rose-pine.css +62 -0
  252. package/themes/simple.css +124 -0
  253. package/themes/slack.css +66 -0
  254. package/themes/snazzy-light.css +66 -0
  255. package/themes/soft-pop.css +60 -0
  256. package/themes/solar-dusk.css +109 -0
  257. package/themes/solarized.css +66 -0
  258. package/themes/synthwave-84.css +34 -0
  259. package/themes/terminal.css +62 -0
  260. package/themes/tinacious.css +57 -0
  261. package/themes/tokyo-night.css +62 -0
  262. package/themes/vesper.css +62 -0
  263. package/themes/vitesse-black.css +66 -0
  264. package/themes/vitesse.css +62 -0
  265. package/types.ts +264 -0
  266. package/utils/agentSwitch.ts +71 -0
  267. package/utils/aiChatFormat.ts +33 -0
  268. package/utils/aiPrompt.ts +29 -0
  269. package/utils/aiProvider.ts +217 -0
  270. package/utils/anchors.ts +11 -0
  271. package/utils/annotateAgentTerminal.ts +29 -0
  272. package/utils/annotationHelpers.ts +101 -0
  273. package/utils/bear.ts +59 -0
  274. package/utils/blockTargeting.ts +240 -0
  275. package/utils/callback.ts +99 -0
  276. package/utils/commentContent.ts +8 -0
  277. package/utils/defaultNotesApp.ts +20 -0
  278. package/utils/diffFonts.ts +33 -0
  279. package/utils/editorMode.ts +31 -0
  280. package/utils/fileBrowser.ts +40 -0
  281. package/utils/generateId.ts +7 -0
  282. package/utils/generateIdentity.ts +29 -0
  283. package/utils/identity.ts +112 -0
  284. package/utils/inlineTransforms.ts +41 -0
  285. package/utils/inputMethod.ts +17 -0
  286. package/utils/lookAndFeelAnnouncement.ts +18 -0
  287. package/utils/obsidian.ts +201 -0
  288. package/utils/octarine.ts +53 -0
  289. package/utils/parser.ts +991 -0
  290. package/utils/permissionMode.ts +77 -0
  291. package/utils/planAIAnnouncement.ts +17 -0
  292. package/utils/planAgentInstructions.ts +137 -0
  293. package/utils/planDiffEngine.ts +590 -0
  294. package/utils/planSave.ts +49 -0
  295. package/utils/platform.ts +11 -0
  296. package/utils/quickLabels.ts +77 -0
  297. package/utils/reviewAgentInstructions.ts +182 -0
  298. package/utils/sanitizeHtml.ts +45 -0
  299. package/utils/sharing.ts +350 -0
  300. package/utils/slugify.ts +37 -0
  301. package/utils/storage.ts +141 -0
  302. package/utils/themeRegistry.ts +567 -0
  303. package/utils/uiPreferences.ts +34 -0
  304. package/utils/upload.ts +56 -0
  305. package/utils/wideMode.ts +48 -0
@@ -0,0 +1,511 @@
1
+ import React, { useRef, useState, useEffect, useCallback } from 'react';
2
+ import { createPortal } from 'react-dom';
3
+ import { instance } from '@viz-js/viz';
4
+ import type { Block } from '../types';
5
+
6
+ interface ViewBox {
7
+ x: number;
8
+ y: number;
9
+ width: number;
10
+ height: number;
11
+ }
12
+
13
+ const ZOOM_STEP = 0.25;
14
+ const MIN_ZOOM = 0.25;
15
+ const MAX_ZOOM = 8;
16
+
17
+ let vizInstancePromise: ReturnType<typeof instance> | null = null;
18
+
19
+ function getVizInstance() {
20
+ vizInstancePromise ??= instance();
21
+ return vizInstancePromise;
22
+ }
23
+
24
+ function parseViewBox(svgEl: SVGSVGElement): ViewBox | null {
25
+ const raw = svgEl.getAttribute('viewBox');
26
+ if (!raw) return null;
27
+
28
+ const values = raw
29
+ .trim()
30
+ .split(/[\s,]+/)
31
+ .map((value) => Number.parseFloat(value));
32
+
33
+ if (values.length !== 4 || values.some((value) => Number.isNaN(value))) {
34
+ return null;
35
+ }
36
+
37
+ const [x, y, width, height] = values;
38
+ if (width <= 0 || height <= 0) return null;
39
+ return { x, y, width, height };
40
+ }
41
+
42
+ function parseViewBoxFromMarkup(markup: string): ViewBox | null {
43
+ const viewBoxMatch = markup.match(/viewBox\s*=\s*"([^"]+)"/i);
44
+ if (viewBoxMatch?.[1]) {
45
+ const values = viewBoxMatch[1]
46
+ .trim()
47
+ .split(/[\s,]+/)
48
+ .map((value) => Number.parseFloat(value));
49
+
50
+ if (values.length === 4 && values.every((value) => Number.isFinite(value))) {
51
+ const [x, y, width, height] = values;
52
+ if (width > 0 && height > 0) {
53
+ return { x, y, width, height };
54
+ }
55
+ }
56
+ }
57
+
58
+ const widthMatch = markup.match(/\bwidth\s*=\s*"([0-9.]+)(?:px|pt)?"/i);
59
+ const heightMatch = markup.match(/\bheight\s*=\s*"([0-9.]+)(?:px|pt)?"/i);
60
+ const width = widthMatch?.[1] ? Number.parseFloat(widthMatch[1]) : NaN;
61
+ const height = heightMatch?.[1] ? Number.parseFloat(heightMatch[1]) : NaN;
62
+ if (Number.isFinite(width) && Number.isFinite(height) && width > 0 && height > 0) {
63
+ return { x: 0, y: 0, width, height };
64
+ }
65
+
66
+ return null;
67
+ }
68
+
69
+ function applyView(svgEl: SVGSVGElement, base: ViewBox, zoom: number, pan: { x: number; y: number }): void {
70
+ const zoomedWidth = base.width / zoom;
71
+ const zoomedHeight = base.height / zoom;
72
+ const centerX = base.x + base.width / 2;
73
+ const centerY = base.y + base.height / 2;
74
+ const vbX = centerX - zoomedWidth / 2 + pan.x;
75
+ const vbY = centerY - zoomedHeight / 2 + pan.y;
76
+ svgEl.setAttribute('viewBox', `${vbX} ${vbY} ${zoomedWidth} ${zoomedHeight}`);
77
+ }
78
+
79
+ function fitBoundsToContainer(bounds: ViewBox, containerRect: DOMRect): ViewBox {
80
+ const containerWidth = Math.max(containerRect.width, 1);
81
+ const containerHeight = Math.max(containerRect.height, 1);
82
+ const contentRatio = bounds.width / bounds.height;
83
+ const containerRatio = containerWidth / containerHeight;
84
+
85
+ if (contentRatio > containerRatio) {
86
+ const targetHeight = bounds.width / containerRatio;
87
+ const extra = (targetHeight - bounds.height) / 2;
88
+ return {
89
+ x: bounds.x,
90
+ y: bounds.y - extra,
91
+ width: bounds.width,
92
+ height: targetHeight,
93
+ };
94
+ }
95
+
96
+ const targetWidth = bounds.height * containerRatio;
97
+ const extra = (targetWidth - bounds.width) / 2;
98
+ return {
99
+ x: bounds.x - extra,
100
+ y: bounds.y,
101
+ width: targetWidth,
102
+ height: bounds.height,
103
+ };
104
+ }
105
+
106
+ export const GraphvizBlock: React.FC<{ block: Block }> = ({ block }) => {
107
+ const containerRef = useRef<HTMLDivElement>(null);
108
+ const [svg, setSvg] = useState('');
109
+ const [error, setError] = useState<string | null>(null);
110
+ const [showSource, setShowSource] = useState(false);
111
+ const [isExpanded, setIsExpanded] = useState(false);
112
+
113
+ const zoomLevelRef = useRef(1);
114
+ const isDraggingRef = useRef(false);
115
+ const naturalBoundsRef = useRef<ViewBox | null>(null);
116
+ const baseViewBoxRef = useRef<ViewBox | null>(null);
117
+ const panOffsetRef = useRef({ x: 0, y: 0 });
118
+ const dragStartRef = useRef({ x: 0, y: 0 });
119
+ const panStartRef = useRef({ x: 0, y: 0 });
120
+
121
+ const zoomInBtnRef = useRef<HTMLButtonElement>(null);
122
+ const zoomOutBtnRef = useRef<HTMLButtonElement>(null);
123
+ const zoomDisplayRef = useRef<HTMLSpanElement>(null);
124
+
125
+ const updateZoom = useCallback((newZoom: number) => {
126
+ zoomLevelRef.current = newZoom;
127
+
128
+ if (containerRef.current && baseViewBoxRef.current) {
129
+ const svgEl = containerRef.current.querySelector('svg');
130
+ if (svgEl instanceof SVGSVGElement) {
131
+ applyView(svgEl, baseViewBoxRef.current, newZoom, panOffsetRef.current);
132
+ }
133
+ }
134
+
135
+ if (zoomInBtnRef.current) zoomInBtnRef.current.disabled = newZoom >= MAX_ZOOM;
136
+ if (zoomOutBtnRef.current) zoomOutBtnRef.current.disabled = newZoom <= MIN_ZOOM;
137
+ if (zoomDisplayRef.current) {
138
+ const show = Math.abs(newZoom - 1) > 0.001;
139
+ zoomDisplayRef.current.textContent = show ? `${Math.round(newZoom * 100)}%` : '';
140
+ zoomDisplayRef.current.hidden = !show;
141
+ }
142
+ }, []);
143
+
144
+ const fitToCurrentViewport = useCallback(() => {
145
+ if (!containerRef.current || !naturalBoundsRef.current) return;
146
+
147
+ const svgEl = containerRef.current.querySelector('svg');
148
+ if (!(svgEl instanceof SVGSVGElement)) return;
149
+
150
+ const fitted = fitBoundsToContainer(naturalBoundsRef.current, containerRef.current.getBoundingClientRect());
151
+ baseViewBoxRef.current = fitted;
152
+ panOffsetRef.current = { x: 0, y: 0 };
153
+ updateZoom(1);
154
+ applyView(svgEl, fitted, 1, { x: 0, y: 0 });
155
+ }, [updateZoom]);
156
+
157
+ useEffect(() => {
158
+ let cancelled = false;
159
+
160
+ const renderDiagram = async () => {
161
+ try {
162
+ const viz = await getVizInstance();
163
+ const renderedSvg = await viz.renderString(block.content, { format: 'svg' });
164
+ const cleaned = renderedSvg
165
+ .replace(/ width="[^"]*"/, ' width="100%"')
166
+ .replace(/ height="[^"]*"/, ' height="100%"')
167
+ .replace(/ style="[^"]*"/, '')
168
+ .replace(/<polygon[^>]*fill="white"[^>]*\/>/, '')
169
+ .replace(/fill="black"/g, 'fill="var(--foreground)"')
170
+ .replace(/fill="#000000"/g, 'fill="var(--foreground)"')
171
+ .replace(/stroke="black"/g, 'stroke="var(--muted-foreground)"')
172
+ .replace(/stroke="#000000"/g, 'stroke="var(--muted-foreground)"')
173
+ .replace(/fill="lightgrey"/g, 'fill="var(--muted)"')
174
+ .replace(/fill="lightgray"/g, 'fill="var(--muted)"');
175
+
176
+ if (!cancelled) {
177
+ naturalBoundsRef.current = parseViewBoxFromMarkup(cleaned);
178
+ setSvg(cleaned);
179
+ setError(null);
180
+ }
181
+ } catch (err) {
182
+ if (!cancelled) {
183
+ setError(err instanceof Error ? err.message : 'Failed to render diagram');
184
+ setSvg('');
185
+ }
186
+ }
187
+ };
188
+
189
+ renderDiagram();
190
+
191
+ return () => {
192
+ cancelled = true;
193
+ };
194
+ }, [block.content]);
195
+
196
+ useEffect(() => {
197
+ zoomLevelRef.current = 1;
198
+ naturalBoundsRef.current = null;
199
+ baseViewBoxRef.current = null;
200
+ panOffsetRef.current = { x: 0, y: 0 };
201
+ setIsExpanded(false);
202
+ }, [block.content]);
203
+
204
+ useEffect(() => {
205
+ if (showSource) {
206
+ setIsExpanded(false);
207
+ return;
208
+ }
209
+
210
+ zoomLevelRef.current = 1;
211
+ panOffsetRef.current = { x: 0, y: 0 };
212
+ baseViewBoxRef.current = null;
213
+ }, [showSource]);
214
+
215
+ useEffect(() => {
216
+ if (!isExpanded) return undefined;
217
+
218
+ const previousOverflow = document.body.style.overflow;
219
+ document.body.style.overflow = 'hidden';
220
+
221
+ const handleKeyDown = (event: KeyboardEvent) => {
222
+ if (event.key === 'Escape') {
223
+ setIsExpanded(false);
224
+ }
225
+ };
226
+
227
+ window.addEventListener('keydown', handleKeyDown);
228
+
229
+ return () => {
230
+ document.body.style.overflow = previousOverflow;
231
+ window.removeEventListener('keydown', handleKeyDown);
232
+ };
233
+ }, [isExpanded]);
234
+
235
+ useEffect(() => {
236
+ if (!svg || showSource || !containerRef.current) return;
237
+
238
+ const svgEl = containerRef.current.querySelector('svg');
239
+ if (!(svgEl instanceof SVGSVGElement)) return;
240
+
241
+ svgEl.style.maxWidth = 'none';
242
+ svgEl.style.width = '100%';
243
+ svgEl.style.height = '100%';
244
+ svgEl.style.display = 'block';
245
+ svgEl.style.filter = 'none';
246
+ svgEl.style.willChange = 'auto';
247
+ svgEl.setAttribute('width', '100%');
248
+ svgEl.setAttribute('height', '100%');
249
+ svgEl.setAttribute('preserveAspectRatio', 'xMidYMid meet');
250
+
251
+ let cancelled = false;
252
+
253
+ const applyInitialView = () => {
254
+ if (cancelled) return;
255
+
256
+ try {
257
+ const base = naturalBoundsRef.current ?? parseViewBox(svgEl);
258
+
259
+ if (!base) return;
260
+
261
+ naturalBoundsRef.current = base;
262
+ fitToCurrentViewport();
263
+ } catch {
264
+ setError('Failed to measure diagram bounds');
265
+ setSvg('');
266
+ }
267
+ };
268
+
269
+ const raf = requestAnimationFrame(() => requestAnimationFrame(applyInitialView));
270
+ const timer = window.setTimeout(applyInitialView, 120);
271
+
272
+ return () => {
273
+ cancelled = true;
274
+ cancelAnimationFrame(raf);
275
+ window.clearTimeout(timer);
276
+ };
277
+ }, [fitToCurrentViewport, isExpanded, showSource, svg]);
278
+
279
+ useEffect(() => {
280
+ if (showSource || !containerRef.current) return;
281
+
282
+ const container = containerRef.current;
283
+ const handleWheel = (e: WheelEvent) => {
284
+ e.preventDefault();
285
+ const delta = e.deltaY > 0 ? -ZOOM_STEP : ZOOM_STEP;
286
+ const newZoom = Math.max(MIN_ZOOM, Math.min(MAX_ZOOM, zoomLevelRef.current + delta));
287
+ updateZoom(newZoom);
288
+ };
289
+
290
+ container.addEventListener('wheel', handleWheel, { passive: false });
291
+ return () => container.removeEventListener('wheel', handleWheel);
292
+ }, [showSource, isExpanded, updateZoom]);
293
+
294
+ const handleZoomIn = useCallback(() => {
295
+ updateZoom(Math.min(zoomLevelRef.current + ZOOM_STEP, MAX_ZOOM));
296
+ }, [updateZoom]);
297
+
298
+ const handleZoomOut = useCallback(() => {
299
+ updateZoom(Math.max(zoomLevelRef.current - ZOOM_STEP, MIN_ZOOM));
300
+ }, [updateZoom]);
301
+
302
+ const handleFitToScreen = useCallback(() => {
303
+ fitToCurrentViewport();
304
+ }, [fitToCurrentViewport]);
305
+
306
+ useEffect(() => {
307
+ if (showSource || !containerRef.current || !naturalBoundsRef.current) return;
308
+ if (typeof ResizeObserver === 'undefined') return;
309
+
310
+ const observer = new ResizeObserver(() => {
311
+ if (Math.abs(zoomLevelRef.current - 1) > 0.001) return;
312
+ fitToCurrentViewport();
313
+ });
314
+
315
+ observer.observe(containerRef.current);
316
+ return () => observer.disconnect();
317
+ }, [fitToCurrentViewport, isExpanded, showSource, svg]);
318
+
319
+ const handleMouseDown = useCallback((event: React.MouseEvent) => {
320
+ if (event.button !== 0) return;
321
+ event.preventDefault();
322
+ isDraggingRef.current = true;
323
+ dragStartRef.current = { x: event.clientX, y: event.clientY };
324
+ panStartRef.current = { ...panOffsetRef.current };
325
+ if (containerRef.current) containerRef.current.style.cursor = 'grabbing';
326
+ }, []);
327
+
328
+ const handleMouseMove = useCallback((event: React.MouseEvent) => {
329
+ if (!isDraggingRef.current || !containerRef.current || !baseViewBoxRef.current) return;
330
+
331
+ const svgEl = containerRef.current.querySelector('svg');
332
+ if (!(svgEl instanceof SVGSVGElement)) return;
333
+
334
+ const rect = svgEl.getBoundingClientRect();
335
+ const base = baseViewBoxRef.current;
336
+ const zoom = zoomLevelRef.current;
337
+ const scaleX = (base.width / zoom) / rect.width;
338
+ const scaleY = (base.height / zoom) / rect.height;
339
+
340
+ const dx = event.clientX - dragStartRef.current.x;
341
+ const dy = event.clientY - dragStartRef.current.y;
342
+
343
+ panOffsetRef.current = {
344
+ x: panStartRef.current.x - dx * scaleX,
345
+ y: panStartRef.current.y - dy * scaleY,
346
+ };
347
+
348
+ applyView(svgEl, base, zoom, panOffsetRef.current);
349
+ }, []);
350
+
351
+ const stopDragging = useCallback(() => {
352
+ if (!isDraggingRef.current) return;
353
+ isDraggingRef.current = false;
354
+ if (containerRef.current) containerRef.current.style.cursor = 'grab';
355
+ }, []);
356
+
357
+ if (error) {
358
+ return (
359
+ <div className="my-5 rounded-lg border border-destructive/30 bg-destructive/5 overflow-hidden">
360
+ <div className="px-3 py-2 bg-destructive/10 border-b border-destructive/20 flex items-center gap-2">
361
+ <svg className="w-4 h-4 text-destructive" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
362
+ <path strokeLinecap="round" strokeLinejoin="round" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
363
+ </svg>
364
+ <span className="text-xs text-destructive font-medium">Graphviz Error</span>
365
+ </div>
366
+ <pre className="p-3 text-xs text-destructive/80 overflow-x-auto">{error}</pre>
367
+ <pre className="p-3 text-xs text-muted-foreground bg-muted/30 border-t border-border/30 overflow-x-auto">
368
+ <code>{block.content}</code>
369
+ </pre>
370
+ </div>
371
+ );
372
+ }
373
+
374
+ const controls = (
375
+ <div className={`absolute top-2 right-2 flex flex-col gap-1 items-center z-10 ${isExpanded ? 'opacity-100' : 'opacity-0 group-hover:opacity-100 transition-opacity'}`}>
376
+ <button
377
+ onClick={() => setShowSource(!showSource)}
378
+ className="p-1.5 rounded-md bg-muted/85 hover:bg-muted text-muted-foreground hover:text-foreground"
379
+ title={showSource ? 'Show diagram' : 'Show source'}
380
+ >
381
+ {showSource ? (
382
+ <svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
383
+ <path strokeLinecap="round" strokeLinejoin="round" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z" />
384
+ </svg>
385
+ ) : (
386
+ <svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
387
+ <path strokeLinecap="round" strokeLinejoin="round" d="M10 20l4-16m4 4l4 4-4 4M6 16l-4-4 4-4" />
388
+ </svg>
389
+ )}
390
+ </button>
391
+
392
+ {!showSource && svg && (
393
+ <>
394
+ <div className="flex w-10 flex-col items-center gap-0.5 bg-muted/85 rounded-md p-0.5">
395
+ <button
396
+ onClick={() => setIsExpanded(!isExpanded)}
397
+ className="p-1 rounded hover:bg-muted text-muted-foreground hover:text-foreground"
398
+ title={isExpanded ? 'Exit expanded view' : 'Expand diagram'}
399
+ aria-label={isExpanded ? 'Exit expanded view' : 'Expand diagram'}
400
+ >
401
+ {isExpanded ? (
402
+ <svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
403
+ <path strokeLinecap="round" strokeLinejoin="round" d="M6 10h4V6M18 10h-4V6M6 14h4v4M18 14h-4v4" />
404
+ </svg>
405
+ ) : (
406
+ <svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
407
+ <path strokeLinecap="round" strokeLinejoin="round" d="M4 9V4h5M20 9V4h-5M4 15v5h5M20 15v5h-5" />
408
+ </svg>
409
+ )}
410
+ </button>
411
+
412
+ <button
413
+ ref={zoomInBtnRef}
414
+ onClick={handleZoomIn}
415
+ className="p-1 rounded hover:bg-muted text-muted-foreground hover:text-foreground disabled:opacity-30 disabled:cursor-not-allowed"
416
+ title="Zoom in"
417
+ aria-label="Zoom in"
418
+ >
419
+ <svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
420
+ <path strokeLinecap="round" strokeLinejoin="round" d="M12 5v14M5 12h14" />
421
+ </svg>
422
+ </button>
423
+
424
+ <button
425
+ onClick={handleFitToScreen}
426
+ className="p-1 rounded hover:bg-muted text-muted-foreground hover:text-foreground"
427
+ title="Fit to view"
428
+ aria-label="Fit to view"
429
+ >
430
+ <svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
431
+ <circle cx="12" cy="12" r="4" strokeLinecap="round" strokeLinejoin="round" />
432
+ <path strokeLinecap="round" strokeLinejoin="round" d="M12 2v4M12 18v4M2 12h4M18 12h4" />
433
+ </svg>
434
+ </button>
435
+
436
+ <button
437
+ ref={zoomOutBtnRef}
438
+ onClick={handleZoomOut}
439
+ className="p-1 rounded hover:bg-muted text-muted-foreground hover:text-foreground disabled:opacity-30 disabled:cursor-not-allowed"
440
+ title="Zoom out"
441
+ aria-label="Zoom out"
442
+ >
443
+ <svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
444
+ <path strokeLinecap="round" strokeLinejoin="round" d="M5 12h14" />
445
+ </svg>
446
+ </button>
447
+ </div>
448
+
449
+ <span
450
+ ref={zoomDisplayRef}
451
+ hidden
452
+ className="min-w-10 rounded bg-muted/85 px-1 py-0.5 text-[10px] text-center text-muted-foreground tabular-nums leading-tight"
453
+ />
454
+ </>
455
+ )}
456
+ </div>
457
+ );
458
+
459
+ const inlineSource = (
460
+ <pre className="rounded-lg text-[13px] overflow-x-auto bg-muted/50 border border-border/30 p-4">
461
+ <code className={`hljs font-mono language-${block.language ?? 'graphviz'}`}>{block.content}</code>
462
+ </pre>
463
+ );
464
+
465
+ const naturalHeight = naturalBoundsRef.current
466
+ ? `min(65vh, ${Math.min(36 * 16, Math.max(4 * 16, Math.round(naturalBoundsRef.current.height * (800 / naturalBoundsRef.current.width))))}px)`
467
+ : 'min(65vh, 36rem)';
468
+
469
+ const diagramBody = (
470
+ <div
471
+ ref={containerRef}
472
+ className={`rounded-xl bg-muted/30 border border-border/30 overflow-hidden select-none cursor-grab ${isExpanded ? 'h-full min-h-0' : ''}`}
473
+ style={isExpanded ? undefined : { height: naturalHeight }}
474
+ dangerouslySetInnerHTML={{ __html: svg }}
475
+ onMouseDown={handleMouseDown}
476
+ onMouseMove={handleMouseMove}
477
+ onMouseUp={stopDragging}
478
+ onMouseLeave={stopDragging}
479
+ />
480
+ );
481
+
482
+ return (
483
+ <>
484
+ <div className="my-5 group relative" data-block-id={block.id}>
485
+ {!isExpanded && controls}
486
+ {showSource || !svg ? inlineSource : !isExpanded ? diagramBody : <div className="rounded-xl border border-border/30 bg-muted/10" style={{ height: naturalHeight }} />}
487
+ </div>
488
+
489
+ {!showSource && svg && isExpanded && typeof document !== 'undefined' && createPortal(
490
+ <div className="fixed inset-0 z-[9999] bg-background/90 backdrop-blur-sm p-4 md:p-6">
491
+ <div className="mx-auto flex h-full max-w-[min(96vw,110rem)] flex-col gap-3">
492
+ <div className="flex items-center justify-between gap-4 text-xs text-muted-foreground">
493
+ <span className="truncate">Graphviz diagram</span>
494
+ <button
495
+ onClick={() => setIsExpanded(false)}
496
+ className="rounded-md border border-border/60 bg-card/70 px-2.5 py-1.5 text-foreground hover:bg-card"
497
+ >
498
+ Close
499
+ </button>
500
+ </div>
501
+ <div className="group relative flex-1 min-h-0">
502
+ {controls}
503
+ {diagramBody}
504
+ </div>
505
+ </div>
506
+ </div>,
507
+ document.body
508
+ )}
509
+ </>
510
+ );
511
+ };
@@ -0,0 +1,118 @@
1
+ import React, { useRef, useEffect, useCallback } from 'react';
2
+ import type { Point, Stroke, Tool } from './types';
3
+ import { renderStroke } from './utils';
4
+
5
+ interface CanvasProps {
6
+ imageSrc: string;
7
+ strokes: Stroke[];
8
+ currentStroke: Stroke | null;
9
+ tool: Tool;
10
+ color: string;
11
+ onStrokeStart: (point: Point) => void;
12
+ onStrokeMove: (point: Point) => void;
13
+ onStrokeEnd: () => void;
14
+ onImageLoad: (img: HTMLImageElement) => void;
15
+ }
16
+
17
+ export const Canvas: React.FC<CanvasProps> = ({
18
+ imageSrc,
19
+ strokes,
20
+ currentStroke,
21
+ onStrokeStart,
22
+ onStrokeMove,
23
+ onStrokeEnd,
24
+ onImageLoad,
25
+ }) => {
26
+ const containerRef = useRef<HTMLDivElement>(null);
27
+ const canvasRef = useRef<HTMLCanvasElement>(null);
28
+ const imageRef = useRef<HTMLImageElement>(null);
29
+ const isDrawing = useRef(false);
30
+
31
+ // Render all strokes
32
+ const render = useCallback(() => {
33
+ const canvas = canvasRef.current;
34
+ const ctx = canvas?.getContext('2d');
35
+ if (!canvas || !ctx) return;
36
+
37
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
38
+
39
+ // Render completed strokes
40
+ strokes.forEach(stroke => renderStroke(ctx, stroke));
41
+
42
+ // Render current stroke
43
+ if (currentStroke) {
44
+ renderStroke(ctx, currentStroke);
45
+ }
46
+ }, [strokes, currentStroke]);
47
+
48
+ // Re-render when strokes change
49
+ useEffect(() => {
50
+ render();
51
+ }, [render]);
52
+
53
+ // Handle image load and canvas sizing
54
+ const handleImageLoad = () => {
55
+ const img = imageRef.current;
56
+ const canvas = canvasRef.current;
57
+ if (!img || !canvas) return;
58
+
59
+ // Match canvas to displayed image size
60
+ canvas.width = img.clientWidth;
61
+ canvas.height = img.clientHeight;
62
+
63
+ onImageLoad(img);
64
+ render();
65
+ };
66
+
67
+ // Get point from pointer event relative to canvas
68
+ const getPoint = (e: React.PointerEvent): Point => {
69
+ const canvas = canvasRef.current;
70
+ if (!canvas) return { x: 0, y: 0 };
71
+
72
+ const rect = canvas.getBoundingClientRect();
73
+ return {
74
+ x: e.clientX - rect.left,
75
+ y: e.clientY - rect.top,
76
+ pressure: e.pressure,
77
+ };
78
+ };
79
+
80
+ const handlePointerDown = (e: React.PointerEvent) => {
81
+ e.preventDefault();
82
+ isDrawing.current = true;
83
+ (e.target as HTMLElement).setPointerCapture(e.pointerId);
84
+ onStrokeStart(getPoint(e));
85
+ };
86
+
87
+ const handlePointerMove = (e: React.PointerEvent) => {
88
+ if (!isDrawing.current) return;
89
+ onStrokeMove(getPoint(e));
90
+ };
91
+
92
+ const handlePointerUp = () => {
93
+ if (!isDrawing.current) return;
94
+ isDrawing.current = false;
95
+ onStrokeEnd();
96
+ };
97
+
98
+ return (
99
+ <div ref={containerRef} className="relative inline-block">
100
+ <img
101
+ ref={imageRef}
102
+ src={imageSrc}
103
+ alt="Annotate"
104
+ onLoad={handleImageLoad}
105
+ className="max-w-[85vw] max-h-[70vh] rounded-lg"
106
+ draggable={false}
107
+ />
108
+ <canvas
109
+ ref={canvasRef}
110
+ className="absolute inset-0 cursor-crosshair touch-none"
111
+ onPointerDown={handlePointerDown}
112
+ onPointerMove={handlePointerMove}
113
+ onPointerUp={handlePointerUp}
114
+ onPointerLeave={handlePointerUp}
115
+ />
116
+ </div>
117
+ );
118
+ };