@fabrika/canvas-kernel 0.1.44
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.
- package/LICENSE +21 -0
- package/README.md +74 -0
- package/annotate.ts +94 -0
- package/daemon.ts +46 -0
- package/index.ts +84 -0
- package/package.json +74 -0
- package/src/client/AnnotationDraftFooter.tsx +76 -0
- package/src/client/AnnotationEditor.tsx +320 -0
- package/src/client/AnnotationList.tsx +190 -0
- package/src/client/AnnotationProvider.tsx +294 -0
- package/src/client/AnnotationSidebar.tsx +374 -0
- package/src/client/FileIcon.tsx +76 -0
- package/src/client/PlanRenderer.tsx +587 -0
- package/src/client/Popover.tsx +361 -0
- package/src/client/RenderErrorContext.ts +11 -0
- package/src/client/ResponsePreview.tsx +186 -0
- package/src/client/SessionContext.tsx +3 -0
- package/src/client/annotationContext.ts +130 -0
- package/src/client/annotationDom.ts +115 -0
- package/src/client/annotationDraft.ts +130 -0
- package/src/client/annotationMarkdown.ts +102 -0
- package/src/client/annotationTarget.ts +73 -0
- package/src/client/appContext.tsx +61 -0
- package/src/client/blockTarget.ts +127 -0
- package/src/client/components/Callout.tsx +27 -0
- package/src/client/components/Checklist.tsx +37 -0
- package/src/client/components/Choice.tsx +178 -0
- package/src/client/components/CodeBlock.tsx +35 -0
- package/src/client/components/Diff.tsx +36 -0
- package/src/client/components/FilePreview.tsx +88 -0
- package/src/client/components/Image.tsx +48 -0
- package/src/client/components/Markdown.tsx +44 -0
- package/src/client/components/Mermaid.tsx +576 -0
- package/src/client/components/Note.tsx +14 -0
- package/src/client/components/Priority.tsx +19 -0
- package/src/client/components/ResponseNote.tsx +19 -0
- package/src/client/components/Section.tsx +33 -0
- package/src/client/components/Table.tsx +35 -0
- package/src/client/components/Task.tsx +76 -0
- package/src/client/components/UserInput.tsx +176 -0
- package/src/client/components/index.ts +18 -0
- package/src/client/components/renderMarkdownChildren.tsx +100 -0
- package/src/client/diffLines.ts +50 -0
- package/src/client/generateMarkdown.ts +250 -0
- package/src/client/highlightRange.ts +262 -0
- package/src/client/hostContext.tsx +55 -0
- package/src/client/index.ts +128 -0
- package/src/client/popoverPosition.ts +46 -0
- package/src/client/preact-all.ts +4 -0
- package/src/client/regionTarget.ts +180 -0
- package/src/client/runtime.ts +149 -0
- package/src/client/styles.css +492 -0
- package/src/client/testing/dom.ts +58 -0
- package/src/client/textTarget.ts +31 -0
- package/src/client/theme.css +66 -0
- package/src/client/useRegionAnnotation.tsx +188 -0
- package/src/client/useTextAnnotation.tsx +194 -0
- package/src/client/utils.ts +33 -0
- package/src/engine/channel.ts +278 -0
- package/src/engine/compiler.ts +415 -0
- package/src/engine/http-utils.ts +14 -0
- package/src/engine/lifecycle.ts +302 -0
- package/src/engine/paths.ts +63 -0
- package/src/engine/router.ts +16 -0
- package/src/engine/session.ts +678 -0
- package/src/engine/waiter.ts +188 -0
- package/src/engine/watcher.ts +84 -0
- package/src/engine/websocket.ts +159 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
interface FileIconProps {
|
|
4
|
+
name: string;
|
|
5
|
+
type: "file" | "dir";
|
|
6
|
+
expanded?: boolean;
|
|
7
|
+
className?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const extColors: Record<string, string> = {
|
|
11
|
+
// TypeScript / JavaScript
|
|
12
|
+
ts: "#3178c6", tsx: "#3178c6", js: "#e8a32e", jsx: "#4ba0c8",
|
|
13
|
+
// Web
|
|
14
|
+
html: "#e34c26", css: "#264de4", scss: "#c69",
|
|
15
|
+
// Data
|
|
16
|
+
json: "#7a7568", yaml: "#cb171e", yml: "#cb171e", toml: "#9c4221",
|
|
17
|
+
// Systems
|
|
18
|
+
rs: "#dea584", go: "#00add8", c: "#6a6a6a", cpp: "#f34b7d", h: "#6a6a6a",
|
|
19
|
+
// Scripting
|
|
20
|
+
py: "#3572a5", rb: "#cc342d", sh: "#5a8c3c", bash: "#5a8c3c",
|
|
21
|
+
// Markup / docs
|
|
22
|
+
md: "#3573b5", txt: "#6b6560",
|
|
23
|
+
// Config
|
|
24
|
+
lock: "#6b6560", gitignore: "#6b6560",
|
|
25
|
+
// Images
|
|
26
|
+
svg: "#d9952e", png: "#6a9c2e", jpg: "#6a9c2e", gif: "#6a9c2e",
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const extLabels: Record<string, string> = {
|
|
30
|
+
ts: "TS", tsx: "TX", js: "JS", jsx: "JX",
|
|
31
|
+
py: "PY", rs: "RS", go: "GO", rb: "RB",
|
|
32
|
+
json: "{}", yaml: "YM", yml: "YM", toml: "TM",
|
|
33
|
+
html: "<>", css: "#", scss: "#",
|
|
34
|
+
md: "M↓", sh: "$", bash: "$",
|
|
35
|
+
c: "C", cpp: "C+", h: "H",
|
|
36
|
+
svg: "◇", png: "▣", jpg: "▣",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export function FileIcon({ name, type, expanded, className = "" }: FileIconProps) {
|
|
40
|
+
if (type === "dir") {
|
|
41
|
+
return (
|
|
42
|
+
<svg width="14" height="14" viewBox="0 0 16 16" fill="none" className={`flex-shrink-0 ${className}`}>
|
|
43
|
+
{expanded ? (
|
|
44
|
+
<path d="M1.5 3.5h5l1.5 1.5h6.5v8h-13z" style={{ fill: "var(--color-folder-fill-active)", stroke: "var(--color-folder-stroke-active)" }} strokeWidth="1" />
|
|
45
|
+
) : (
|
|
46
|
+
<path d="M1.5 3.5h5l1.5 1.5h6.5v8h-13z" style={{ fill: "var(--color-folder-fill)", stroke: "var(--color-folder-stroke)" }} strokeWidth="1" />
|
|
47
|
+
)}
|
|
48
|
+
</svg>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const ext = name.includes(".") ? name.split(".").pop()!.toLowerCase() : "";
|
|
53
|
+
const color = extColors[ext] || "#6b6560";
|
|
54
|
+
const label = extLabels[ext];
|
|
55
|
+
|
|
56
|
+
return (
|
|
57
|
+
<svg width="14" height="14" viewBox="0 0 16 16" className={`flex-shrink-0 ${className}`}>
|
|
58
|
+
{/* File shape with folded corner */}
|
|
59
|
+
<path d="M2 1.5h8l3.5 3.5v9.5a1 1 0 01-1 1H3a1 1 0 01-1-1v-12a1 1 0 011-1z" fill={color} opacity="0.85" />
|
|
60
|
+
<path d="M10 1.5v2.5a1 1 0 001 1h2.5" fill="none" stroke="white" strokeWidth="0.5" opacity="0.5" />
|
|
61
|
+
{label && (
|
|
62
|
+
<text
|
|
63
|
+
x="8"
|
|
64
|
+
y="11.5"
|
|
65
|
+
textAnchor="middle"
|
|
66
|
+
fill="white"
|
|
67
|
+
fontSize="5.5"
|
|
68
|
+
fontFamily="monospace"
|
|
69
|
+
fontWeight="600"
|
|
70
|
+
>
|
|
71
|
+
{label}
|
|
72
|
+
</text>
|
|
73
|
+
)}
|
|
74
|
+
</svg>
|
|
75
|
+
);
|
|
76
|
+
}
|
|
@@ -0,0 +1,587 @@
|
|
|
1
|
+
import React, { useEffect, useState, useRef, useContext, useMemo } from "react";
|
|
2
|
+
import { createPortal } from "react-dom";
|
|
3
|
+
import { SessionContext, CanvasFileCtx } from "#canvas/runtime";
|
|
4
|
+
import { useAnnotations } from "./AnnotationProvider";
|
|
5
|
+
import { extractContext } from "./annotationContext";
|
|
6
|
+
import { ANNOTATABLE_SELECTOR, BLOCK_SELECTOR, getBlockSnippet } from "./annotationDom";
|
|
7
|
+
import { AnnotationCreatePopover, AnnotationEditPopover } from "./Popover";
|
|
8
|
+
import { generateAnnotationId } from "./utils";
|
|
9
|
+
import { useTextAnnotation } from "./useTextAnnotation";
|
|
10
|
+
import { useRegionAnnotation } from "./useRegionAnnotation";
|
|
11
|
+
import { useCanvasHost } from "./hostContext";
|
|
12
|
+
import { RenderErrorContext, type CanvasRenderError } from "./RenderErrorContext";
|
|
13
|
+
|
|
14
|
+
interface PlanRendererProps {
|
|
15
|
+
revision: number;
|
|
16
|
+
filename: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface CanvasErrorBoundaryProps {
|
|
20
|
+
revision: number;
|
|
21
|
+
filename: string;
|
|
22
|
+
reportError: (error: CanvasRenderError) => void;
|
|
23
|
+
children: React.ReactNode;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface CanvasErrorBoundaryState {
|
|
27
|
+
error: Error | null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
class CanvasErrorBoundary extends React.Component<CanvasErrorBoundaryProps, CanvasErrorBoundaryState> {
|
|
31
|
+
state: CanvasErrorBoundaryState = { error: null };
|
|
32
|
+
|
|
33
|
+
static getDerivedStateFromError(error: Error): CanvasErrorBoundaryState {
|
|
34
|
+
return { error };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
componentDidCatch(error: Error, info: React.ErrorInfo) {
|
|
38
|
+
this.props.reportError({
|
|
39
|
+
revision: this.props.revision,
|
|
40
|
+
filename: this.props.filename,
|
|
41
|
+
message: error.message,
|
|
42
|
+
...(error.stack ? { stack: error.stack } : {}),
|
|
43
|
+
...(info.componentStack ? { componentStack: info.componentStack } : {}),
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
render() {
|
|
48
|
+
if (!this.state.error) return this.props.children;
|
|
49
|
+
return <CanvasErrorDisplay message={this.state.error.message} />;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function CanvasErrorDisplay({ message }: { message: string }) {
|
|
54
|
+
return (
|
|
55
|
+
<div className="bg-accent-red-muted rounded-lg p-5">
|
|
56
|
+
<h3 className="font-body font-semibold text-accent-red mb-2">Canvas render failed</h3>
|
|
57
|
+
<pre className="text-code font-mono text-text-secondary whitespace-pre-wrap">{message}</pre>
|
|
58
|
+
<p className="text-meta font-body text-text-tertiary mt-3">The error was sent back to the canvas author.</p>
|
|
59
|
+
</div>
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function PlanRenderer({ revision, filename }: PlanRendererProps) {
|
|
64
|
+
const sessionId = useContext(SessionContext);
|
|
65
|
+
const host = useCanvasHost();
|
|
66
|
+
const reportError = useContext(RenderErrorContext);
|
|
67
|
+
const [PlanComponent, setPlanComponent] = useState<React.ComponentType | null>(null);
|
|
68
|
+
const [error, setError] = useState<string | null>(null);
|
|
69
|
+
const [loading, setLoading] = useState(true);
|
|
70
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
71
|
+
const { annotations: allAnnotations, addAnnotationWithId, removeAnnotation, updateAnnotation, addAnnotationImage, removeAnnotationImage, activeAnnotationId, setActiveAnnotationId, registerCanvasRendered } = useAnnotations();
|
|
72
|
+
// Filter annotations to this canvas file
|
|
73
|
+
const annotations = useMemo(() => allAnnotations.filter(a => !a.filePath && (!a.canvasFile || a.canvasFile === filename)), [allAnnotations, filename]);
|
|
74
|
+
const [editingAnn, setEditingAnn] = useState<{ id: string; note: string } | null>(null);
|
|
75
|
+
|
|
76
|
+
// Block annotation hover state
|
|
77
|
+
const [hoveredBlock, setHoveredBlock] = useState<HTMLElement | null>(null);
|
|
78
|
+
const [blockPopover, setBlockPopover] = useState<{ anchorEl: HTMLElement; snippet: string; annId?: string } | null>(null);
|
|
79
|
+
|
|
80
|
+
// Keyboard navigation state
|
|
81
|
+
const [focusedBlockIndex, setFocusedBlockIndex] = useState<number | null>(null);
|
|
82
|
+
const [keyboardNav, setKeyboardNav] = useState(false);
|
|
83
|
+
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
setLoading(true);
|
|
86
|
+
setError(null);
|
|
87
|
+
setFocusedBlockIndex(null);
|
|
88
|
+
void sessionId; // the host resolves the session from the URL (shared or local)
|
|
89
|
+
host.loadCanvasModule(filename, revision)
|
|
90
|
+
.then((mod) => { setPlanComponent(() => mod.default); setLoading(false); })
|
|
91
|
+
.catch((cause: unknown) => {
|
|
92
|
+
const moduleError = cause instanceof Error ? cause : new Error(String(cause));
|
|
93
|
+
setError(moduleError.message);
|
|
94
|
+
setLoading(false);
|
|
95
|
+
reportError({
|
|
96
|
+
revision,
|
|
97
|
+
filename,
|
|
98
|
+
message: moduleError.message,
|
|
99
|
+
...(moduleError.stack ? { stack: moduleError.stack } : {}),
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
}, [sessionId, revision, filename, reportError]);
|
|
103
|
+
|
|
104
|
+
// Report the canvas only once its tree is committed — the response controls
|
|
105
|
+
// inside register from their own effects, which React runs before this one.
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
if (PlanComponent) registerCanvasRendered(filename);
|
|
108
|
+
}, [PlanComponent, filename, registerCanvasRendered]);
|
|
109
|
+
|
|
110
|
+
const scrollContainer = document.getElementById("plan-scroll-container");
|
|
111
|
+
|
|
112
|
+
const { popovers: textAnnotationPopovers, isPopoverOpen: textPopoverOpen } = useTextAnnotation({
|
|
113
|
+
containerRef,
|
|
114
|
+
restoreKey: PlanComponent,
|
|
115
|
+
restoreAnnotations: annotations,
|
|
116
|
+
extractContext: (range) => extractContext(range, containerRef.current!),
|
|
117
|
+
canvasFile: filename,
|
|
118
|
+
scrollContainer,
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
const { popovers: regionPopovers, isDrawing, isPopoverOpen: regionPopoverOpen } = useRegionAnnotation({
|
|
122
|
+
containerRef,
|
|
123
|
+
restoreKey: PlanComponent,
|
|
124
|
+
canvasFile: filename,
|
|
125
|
+
scrollContainer,
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// Text selection and region drawing both raise a popover; the block layer
|
|
129
|
+
// stands down for either.
|
|
130
|
+
const annotationPopoverOpen = textPopoverOpen || regionPopoverOpen;
|
|
131
|
+
|
|
132
|
+
// Build a map of block snippets to annotation IDs for quick lookup
|
|
133
|
+
const blockAnnotationMap = useMemo(() => {
|
|
134
|
+
const map = new Map<string, string>();
|
|
135
|
+
for (const ann of annotations) {
|
|
136
|
+
if (ann.snippet.startsWith("[")) {
|
|
137
|
+
map.set(ann.snippet, ann.id);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return map;
|
|
141
|
+
}, [annotations]);
|
|
142
|
+
|
|
143
|
+
// Block annotation: hover detection
|
|
144
|
+
useEffect(() => {
|
|
145
|
+
const container = containerRef.current;
|
|
146
|
+
if (!container) return;
|
|
147
|
+
|
|
148
|
+
const handleMove = (e: MouseEvent) => {
|
|
149
|
+
// Don't change hover when a popover is open or a region is being drawn
|
|
150
|
+
if (blockPopover || annotationPopoverOpen || isDrawing) return;
|
|
151
|
+
const target = e.target as HTMLElement;
|
|
152
|
+
// Ignore events on the block comment button itself
|
|
153
|
+
if (target.closest("[data-block-comment-btn]")) return;
|
|
154
|
+
setKeyboardNav(false);
|
|
155
|
+
// Track keyboard nav position on any navigable block
|
|
156
|
+
const navBlock = target.closest(BLOCK_SELECTOR) as HTMLElement | null;
|
|
157
|
+
if (navBlock && container.contains(navBlock)) {
|
|
158
|
+
const blocks = getOrderedBlocks(container);
|
|
159
|
+
const idx = blocks.indexOf(navBlock);
|
|
160
|
+
setFocusedBlockIndex(idx >= 0 ? idx : null);
|
|
161
|
+
} else {
|
|
162
|
+
setFocusedBlockIndex(null);
|
|
163
|
+
}
|
|
164
|
+
// Only show comment icons on annotatable blocks
|
|
165
|
+
const block = target.closest(ANNOTATABLE_SELECTOR) as HTMLElement | null;
|
|
166
|
+
if (block && container.contains(block)) {
|
|
167
|
+
setHoveredBlock(block);
|
|
168
|
+
const snippet = getBlockSnippet(block);
|
|
169
|
+
const annId = snippet ? blockAnnotationMap.get(snippet) : undefined;
|
|
170
|
+
if (annId && annId !== activeAnnotationId) {
|
|
171
|
+
setActiveAnnotationId(annId);
|
|
172
|
+
}
|
|
173
|
+
} else {
|
|
174
|
+
setHoveredBlock(null);
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
const handleLeave = (e: MouseEvent) => {
|
|
179
|
+
if (blockPopover || annotationPopoverOpen) return;
|
|
180
|
+
// Don't clear if moving onto the block comment button
|
|
181
|
+
const related = e.relatedTarget as HTMLElement | null;
|
|
182
|
+
if (related?.closest("[data-block-comment-btn]")) return;
|
|
183
|
+
setHoveredBlock(null);
|
|
184
|
+
setActiveAnnotationId(null);
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
container.addEventListener("mousemove", handleMove);
|
|
188
|
+
container.addEventListener("mouseleave", handleLeave);
|
|
189
|
+
return () => {
|
|
190
|
+
container.removeEventListener("mousemove", handleMove);
|
|
191
|
+
container.removeEventListener("mouseleave", handleLeave);
|
|
192
|
+
};
|
|
193
|
+
}, [PlanComponent, blockPopover, annotationPopoverOpen, isDrawing, blockAnnotationMap, activeAnnotationId]);
|
|
194
|
+
|
|
195
|
+
// Keyboard navigation: document-level key listener
|
|
196
|
+
useEffect(() => {
|
|
197
|
+
const handleKeyDown = (e: KeyboardEvent) => {
|
|
198
|
+
// Guard: skip if input/textarea focused or popover open
|
|
199
|
+
const tag = (e.target as HTMLElement).tagName;
|
|
200
|
+
if (tag === "INPUT" || tag === "TEXTAREA" || (e.target as HTMLElement).isContentEditable) return;
|
|
201
|
+
if (blockPopover || annotationPopoverOpen) return;
|
|
202
|
+
|
|
203
|
+
const container = containerRef.current;
|
|
204
|
+
if (!container) return;
|
|
205
|
+
|
|
206
|
+
if (e.key === "ArrowDown") {
|
|
207
|
+
e.preventDefault();
|
|
208
|
+
setKeyboardNav(true);
|
|
209
|
+
const blocks = getOrderedBlocks(container);
|
|
210
|
+
if (blocks.length === 0) return;
|
|
211
|
+
setFocusedBlockIndex((prev) => {
|
|
212
|
+
if (prev === null) return 0;
|
|
213
|
+
return Math.min(prev + 1, blocks.length - 1);
|
|
214
|
+
});
|
|
215
|
+
} else if (e.key === "ArrowUp") {
|
|
216
|
+
e.preventDefault();
|
|
217
|
+
setKeyboardNav(true);
|
|
218
|
+
const blocks = getOrderedBlocks(container);
|
|
219
|
+
if (blocks.length === 0) return;
|
|
220
|
+
setFocusedBlockIndex((prev) => {
|
|
221
|
+
if (prev === null) return blocks.length - 1;
|
|
222
|
+
return Math.max(prev - 1, 0);
|
|
223
|
+
});
|
|
224
|
+
} else if (e.key === "a" || e.key === "A") {
|
|
225
|
+
if (focusedBlockIndex === null || !container) return;
|
|
226
|
+
const blocks = getOrderedBlocks(container);
|
|
227
|
+
const block = blocks[focusedBlockIndex];
|
|
228
|
+
if (!block) return;
|
|
229
|
+
// The arrows walk every block, but only an annotatable one can be found
|
|
230
|
+
// again — minting a snippet for anything else strands the annotation.
|
|
231
|
+
if (!block.matches(ANNOTATABLE_SELECTOR)) return;
|
|
232
|
+
e.preventDefault();
|
|
233
|
+
const snippet = getBlockSnippet(block);
|
|
234
|
+
if (!snippet) return;
|
|
235
|
+
const existingAnnId = blockAnnotationMap.get(snippet);
|
|
236
|
+
if (existingAnnId) {
|
|
237
|
+
setActiveAnnotationId(existingAnnId);
|
|
238
|
+
setBlockPopover({ anchorEl: block, snippet, annId: existingAnnId });
|
|
239
|
+
} else {
|
|
240
|
+
setBlockPopover({ anchorEl: block, snippet });
|
|
241
|
+
}
|
|
242
|
+
} else if (e.key === " " || e.key === "Enter") {
|
|
243
|
+
if (focusedBlockIndex === null) return;
|
|
244
|
+
const blocks = getOrderedBlocks(container);
|
|
245
|
+
const block = blocks[focusedBlockIndex];
|
|
246
|
+
if (!block) return;
|
|
247
|
+
const md = block.getAttribute("data-md");
|
|
248
|
+
if (md === "choice-option" || md === "multichoice-option") {
|
|
249
|
+
e.preventDefault();
|
|
250
|
+
block.dispatchEvent(new CustomEvent("kb-select", { bubbles: true }));
|
|
251
|
+
} else if (md === "userinput") {
|
|
252
|
+
e.preventDefault();
|
|
253
|
+
const textarea = block.querySelector("textarea");
|
|
254
|
+
if (textarea) {
|
|
255
|
+
textarea.focus();
|
|
256
|
+
const exitHandler = (te: KeyboardEvent) => {
|
|
257
|
+
if (te.key === "Escape") {
|
|
258
|
+
te.preventDefault();
|
|
259
|
+
textarea.blur();
|
|
260
|
+
setKeyboardNav(true);
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
textarea.addEventListener("keydown", exitHandler);
|
|
264
|
+
textarea.addEventListener("blur", () => textarea.removeEventListener("keydown", exitHandler), { once: true });
|
|
265
|
+
}
|
|
266
|
+
} else if (md === "rangeinput") {
|
|
267
|
+
e.preventDefault();
|
|
268
|
+
const input = block.querySelector("input[type='range']") as HTMLInputElement | null;
|
|
269
|
+
if (input) {
|
|
270
|
+
input.focus();
|
|
271
|
+
const exitHandler = (re: KeyboardEvent) => {
|
|
272
|
+
if (re.key === "Escape" || re.key === "ArrowUp" || re.key === "ArrowDown") {
|
|
273
|
+
re.preventDefault();
|
|
274
|
+
input.blur();
|
|
275
|
+
setKeyboardNav(true);
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
input.addEventListener("keydown", exitHandler);
|
|
279
|
+
input.addEventListener("blur", () => input.removeEventListener("keydown", exitHandler), { once: true });
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
} else if (e.key === "Escape") {
|
|
283
|
+
setFocusedBlockIndex(null);
|
|
284
|
+
setKeyboardNav(false);
|
|
285
|
+
setActiveAnnotationId(null);
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
document.addEventListener("keydown", handleKeyDown);
|
|
290
|
+
return () => document.removeEventListener("keydown", handleKeyDown);
|
|
291
|
+
}, [focusedBlockIndex, blockPopover, annotationPopoverOpen, blockAnnotationMap]);
|
|
292
|
+
|
|
293
|
+
// Keyboard navigation: visual focus outline + scroll into view + sidebar sync
|
|
294
|
+
useEffect(() => {
|
|
295
|
+
const container = containerRef.current;
|
|
296
|
+
if (!container || focusedBlockIndex === null || !keyboardNav) return;
|
|
297
|
+
const blocks = getOrderedBlocks(container);
|
|
298
|
+
const block = blocks[focusedBlockIndex];
|
|
299
|
+
if (!block) return;
|
|
300
|
+
|
|
301
|
+
block.style.outline = "2px solid var(--color-accent-blue)";
|
|
302
|
+
block.style.outlineOffset = "-2px";
|
|
303
|
+
block.style.borderRadius = "8px";
|
|
304
|
+
block.scrollIntoView({ block: "nearest", behavior: "smooth" });
|
|
305
|
+
|
|
306
|
+
// Sync sidebar if block has an annotation
|
|
307
|
+
const snippet = getBlockSnippet(block);
|
|
308
|
+
const annId = snippet ? blockAnnotationMap.get(snippet) : undefined;
|
|
309
|
+
if (annId) setActiveAnnotationId(annId);
|
|
310
|
+
|
|
311
|
+
return () => {
|
|
312
|
+
block.style.outline = "";
|
|
313
|
+
block.style.outlineOffset = "";
|
|
314
|
+
block.style.borderRadius = "";
|
|
315
|
+
};
|
|
316
|
+
}, [focusedBlockIndex, keyboardNav, blockAnnotationMap]);
|
|
317
|
+
|
|
318
|
+
// Memoize so context-triggered re-renders of PlanRenderer don't re-render the plan tree.
|
|
319
|
+
// This prevents remounting custom components defined inside the Plan function.
|
|
320
|
+
const planElement = useMemo(() => PlanComponent ? <PlanComponent /> : null, [PlanComponent]);
|
|
321
|
+
|
|
322
|
+
if (loading) {
|
|
323
|
+
return <div className="flex items-center justify-center h-64 text-text-tertiary font-body text-body">Loading canvas...</div>;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (error) {
|
|
327
|
+
return <CanvasErrorDisplay message={error} />;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
if (!PlanComponent) {
|
|
331
|
+
return <div className="text-text-tertiary text-center py-8 font-body text-body">No canvas loaded</div>;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
return (
|
|
335
|
+
<CanvasFileCtx.Provider value={filename}>
|
|
336
|
+
{/* Names the canvas a block annotation belongs to, so looking one up
|
|
337
|
+
cannot wander into another canvas mounted beside it. */}
|
|
338
|
+
<div ref={containerRef} className="plan-content plan-updated" data-canvas-file={filename}>
|
|
339
|
+
<CanvasErrorBoundary
|
|
340
|
+
key={`${revision}:${filename}`}
|
|
341
|
+
revision={revision}
|
|
342
|
+
filename={filename}
|
|
343
|
+
reportError={reportError}
|
|
344
|
+
>
|
|
345
|
+
{planElement}
|
|
346
|
+
</CanvasErrorBoundary>
|
|
347
|
+
</div>
|
|
348
|
+
{editingAnn && (
|
|
349
|
+
<EditAnnotationModal
|
|
350
|
+
note={editingAnn.note}
|
|
351
|
+
onSave={(note) => { updateAnnotation(editingAnn.id, note); setEditingAnn(null); }}
|
|
352
|
+
onCancel={() => setEditingAnn(null)}
|
|
353
|
+
/>
|
|
354
|
+
)}
|
|
355
|
+
|
|
356
|
+
{textAnnotationPopovers}
|
|
357
|
+
{regionPopovers}
|
|
358
|
+
|
|
359
|
+
{/* Block annotation floating buttons */}
|
|
360
|
+
<BlockCommentButtons
|
|
361
|
+
containerRef={containerRef}
|
|
362
|
+
hoveredBlock={hoveredBlock}
|
|
363
|
+
focusedBlockIndex={focusedBlockIndex}
|
|
364
|
+
blockPopover={blockPopover}
|
|
365
|
+
blockAnnotationMap={blockAnnotationMap}
|
|
366
|
+
activeAnnotationId={activeAnnotationId}
|
|
367
|
+
onOpen={(anchorEl, snippet, annId) => {
|
|
368
|
+
if (annId) {
|
|
369
|
+
setActiveAnnotationId(annId);
|
|
370
|
+
setBlockPopover({ anchorEl, snippet, annId });
|
|
371
|
+
} else {
|
|
372
|
+
setBlockPopover({ anchorEl, snippet });
|
|
373
|
+
}
|
|
374
|
+
}}
|
|
375
|
+
/>
|
|
376
|
+
|
|
377
|
+
{/* Block annotation popover */}
|
|
378
|
+
{blockPopover && !blockPopover.annId && (
|
|
379
|
+
<AnnotationCreatePopover
|
|
380
|
+
anchorEl={blockPopover.anchorEl}
|
|
381
|
+
scrollContainer={scrollContainer}
|
|
382
|
+
snippet={blockPopover.snippet}
|
|
383
|
+
truncateAt={80}
|
|
384
|
+
onAdd={(note, images) => {
|
|
385
|
+
const id = generateAnnotationId();
|
|
386
|
+
addAnnotationWithId(id, blockPopover.snippet, note, undefined, undefined, images, filename);
|
|
387
|
+
setBlockPopover(null);
|
|
388
|
+
}}
|
|
389
|
+
onCancel={() => setBlockPopover(null)}
|
|
390
|
+
/>
|
|
391
|
+
)}
|
|
392
|
+
{blockPopover?.annId && (() => {
|
|
393
|
+
const ann = annotations.find((a) => a.id === blockPopover.annId);
|
|
394
|
+
if (!ann) return null;
|
|
395
|
+
return (
|
|
396
|
+
<AnnotationEditPopover
|
|
397
|
+
anchorEl={blockPopover.anchorEl}
|
|
398
|
+
scrollContainer={scrollContainer}
|
|
399
|
+
initialNote={ann.note}
|
|
400
|
+
initialImages={ann.images}
|
|
401
|
+
onUpdate={(note, images) => {
|
|
402
|
+
updateAnnotation(blockPopover.annId!, note);
|
|
403
|
+
const current = ann.images || [];
|
|
404
|
+
for (const img of images) {
|
|
405
|
+
if (!current.includes(img)) addAnnotationImage(blockPopover.annId!, img);
|
|
406
|
+
}
|
|
407
|
+
for (const img of current) {
|
|
408
|
+
if (!images.includes(img)) removeAnnotationImage(blockPopover.annId!, img);
|
|
409
|
+
}
|
|
410
|
+
}}
|
|
411
|
+
onDelete={() => { removeAnnotation(blockPopover.annId!); setActiveAnnotationId(null); setBlockPopover(null); }}
|
|
412
|
+
onClose={() => setBlockPopover(null)}
|
|
413
|
+
/>
|
|
414
|
+
);
|
|
415
|
+
})()}
|
|
416
|
+
</CanvasFileCtx.Provider>
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/** Get all navigable blocks in DOM order */
|
|
421
|
+
function getOrderedBlocks(container: HTMLElement): HTMLElement[] {
|
|
422
|
+
return Array.from(container.querySelectorAll(BLOCK_SELECTOR)) as HTMLElement[];
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/** Renders comment icons: always visible on annotated blocks, on hover for others */
|
|
426
|
+
function BlockCommentButtons({
|
|
427
|
+
containerRef,
|
|
428
|
+
hoveredBlock,
|
|
429
|
+
focusedBlockIndex,
|
|
430
|
+
blockPopover,
|
|
431
|
+
blockAnnotationMap,
|
|
432
|
+
activeAnnotationId,
|
|
433
|
+
onOpen,
|
|
434
|
+
}: {
|
|
435
|
+
containerRef: React.RefObject<HTMLDivElement | null>;
|
|
436
|
+
hoveredBlock: HTMLElement | null;
|
|
437
|
+
focusedBlockIndex: number | null;
|
|
438
|
+
blockPopover: { anchorEl: HTMLElement; snippet: string; annId?: string } | null;
|
|
439
|
+
blockAnnotationMap: Map<string, string>;
|
|
440
|
+
activeAnnotationId: string | null;
|
|
441
|
+
onOpen: (anchorEl: HTMLElement, snippet: string, annId?: string) => void;
|
|
442
|
+
}) {
|
|
443
|
+
// Find all blocks that have annotations
|
|
444
|
+
const [annotatedBlocks, setAnnotatedBlocks] = useState<HTMLElement[]>([]);
|
|
445
|
+
|
|
446
|
+
useEffect(() => {
|
|
447
|
+
const container = containerRef.current;
|
|
448
|
+
if (!container || blockAnnotationMap.size === 0) { setAnnotatedBlocks([]); return; }
|
|
449
|
+
const blocks: HTMLElement[] = [];
|
|
450
|
+
for (const el of container.querySelectorAll(ANNOTATABLE_SELECTOR)) {
|
|
451
|
+
const snippet = getBlockSnippet(el as HTMLElement);
|
|
452
|
+
if (snippet && blockAnnotationMap.has(snippet)) {
|
|
453
|
+
blocks.push(el as HTMLElement);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
setAnnotatedBlocks(blocks);
|
|
457
|
+
}, [blockAnnotationMap, containerRef.current]);
|
|
458
|
+
|
|
459
|
+
// Also highlight block when its annotation is active from sidebar hover
|
|
460
|
+
const [activeBlock, setActiveBlock] = useState<HTMLElement | null>(null);
|
|
461
|
+
useEffect(() => {
|
|
462
|
+
if (!activeAnnotationId || !containerRef.current) { setActiveBlock(null); return; }
|
|
463
|
+
// Find block matching active annotation
|
|
464
|
+
for (const el of containerRef.current.querySelectorAll(BLOCK_SELECTOR)) {
|
|
465
|
+
const snippet = getBlockSnippet(el as HTMLElement);
|
|
466
|
+
if (snippet && blockAnnotationMap.get(snippet) === activeAnnotationId) {
|
|
467
|
+
setActiveBlock(el as HTMLElement);
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
setActiveBlock(null);
|
|
472
|
+
}, [activeAnnotationId, blockAnnotationMap]);
|
|
473
|
+
|
|
474
|
+
// Apply highlight style to active block
|
|
475
|
+
useEffect(() => {
|
|
476
|
+
if (!activeBlock) return;
|
|
477
|
+
activeBlock.style.boxShadow = "inset 0 0 0 1.5px var(--color-highlight-bg)";
|
|
478
|
+
activeBlock.style.borderRadius = "8px";
|
|
479
|
+
return () => {
|
|
480
|
+
activeBlock.style.boxShadow = "";
|
|
481
|
+
activeBlock.style.borderRadius = "";
|
|
482
|
+
};
|
|
483
|
+
}, [activeBlock]);
|
|
484
|
+
|
|
485
|
+
// Resolve keyboard-focused block element (only annotatable blocks get comment icons)
|
|
486
|
+
const focusedBlock = useMemo(() => {
|
|
487
|
+
if (focusedBlockIndex === null || !containerRef.current) return null;
|
|
488
|
+
const block = getOrderedBlocks(containerRef.current)[focusedBlockIndex];
|
|
489
|
+
if (!block) return null;
|
|
490
|
+
// Only show comment icon on annotatable blocks, not interactive controls
|
|
491
|
+
return block.matches(ANNOTATABLE_SELECTOR) ? block : null;
|
|
492
|
+
}, [focusedBlockIndex, containerRef.current]);
|
|
493
|
+
|
|
494
|
+
// Collect all blocks that need a button: annotated + hovered + focused + popover target
|
|
495
|
+
const targets = useMemo(() => {
|
|
496
|
+
const set = new Set<HTMLElement>(annotatedBlocks);
|
|
497
|
+
if (hoveredBlock) set.add(hoveredBlock);
|
|
498
|
+
if (focusedBlock) set.add(focusedBlock);
|
|
499
|
+
if (blockPopover?.anchorEl) set.add(blockPopover.anchorEl);
|
|
500
|
+
return Array.from(set);
|
|
501
|
+
}, [annotatedBlocks, hoveredBlock, focusedBlock, blockPopover]);
|
|
502
|
+
|
|
503
|
+
return (
|
|
504
|
+
<>
|
|
505
|
+
{targets.map((block) => (
|
|
506
|
+
<BlockCommentIcon
|
|
507
|
+
key={block.getAttribute("data-task-id") || block.getAttribute("data-md-title") || block.textContent?.slice(0, 30) || "block"}
|
|
508
|
+
block={block}
|
|
509
|
+
isHovered={block === hoveredBlock || block === focusedBlock || block === blockPopover?.anchorEl}
|
|
510
|
+
blockAnnotationMap={blockAnnotationMap}
|
|
511
|
+
activeAnnotationId={activeAnnotationId}
|
|
512
|
+
onOpen={onOpen}
|
|
513
|
+
/>
|
|
514
|
+
))}
|
|
515
|
+
</>
|
|
516
|
+
);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
function BlockCommentIcon({
|
|
520
|
+
block,
|
|
521
|
+
isHovered,
|
|
522
|
+
blockAnnotationMap,
|
|
523
|
+
activeAnnotationId,
|
|
524
|
+
onOpen,
|
|
525
|
+
}: {
|
|
526
|
+
block: HTMLElement;
|
|
527
|
+
isHovered: boolean;
|
|
528
|
+
blockAnnotationMap: Map<string, string>;
|
|
529
|
+
activeAnnotationId: string | null;
|
|
530
|
+
onOpen: (anchorEl: HTMLElement, snippet: string, annId?: string) => void;
|
|
531
|
+
}) {
|
|
532
|
+
const snippet = getBlockSnippet(block);
|
|
533
|
+
if (!snippet) return null;
|
|
534
|
+
|
|
535
|
+
const existingAnnId = blockAnnotationMap.get(snippet);
|
|
536
|
+
const hasAnnotation = !!existingAnnId;
|
|
537
|
+
const isActive = existingAnnId === activeAnnotationId;
|
|
538
|
+
|
|
539
|
+
return createPortal(
|
|
540
|
+
<button
|
|
541
|
+
data-block-comment-btn
|
|
542
|
+
onClick={(e) => {
|
|
543
|
+
e.stopPropagation();
|
|
544
|
+
onOpen(e.currentTarget as HTMLElement, snippet, existingAnnId);
|
|
545
|
+
}}
|
|
546
|
+
className={`absolute top-1 right-1 z-10 w-6 h-6 flex items-center justify-center rounded transition-all duration-150 ${
|
|
547
|
+
hasAnnotation
|
|
548
|
+
? isActive
|
|
549
|
+
? "text-accent-amber bg-accent-amber-muted"
|
|
550
|
+
: "text-accent-amber opacity-60 hover:opacity-100"
|
|
551
|
+
: isHovered
|
|
552
|
+
? "text-text-disabled hover:text-text-tertiary opacity-50 hover:opacity-100"
|
|
553
|
+
: "text-text-disabled opacity-0 group-hover/section:opacity-20 hover:!opacity-80"
|
|
554
|
+
}`}
|
|
555
|
+
title={hasAnnotation ? "Edit annotation" : "Add annotation"}
|
|
556
|
+
>
|
|
557
|
+
<svg width="13" height="13" viewBox="0 0 24 24" fill={hasAnnotation ? "currentColor" : "none"} stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
|
|
558
|
+
<path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z" />
|
|
559
|
+
</svg>
|
|
560
|
+
</button>,
|
|
561
|
+
block,
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
function EditAnnotationModal({ note, onSave, onCancel }: { note: string; onSave: (n: string) => void; onCancel: () => void }) {
|
|
566
|
+
const [text, setText] = React.useState(note);
|
|
567
|
+
const ref = React.useRef<HTMLTextAreaElement>(null);
|
|
568
|
+
React.useEffect(() => { ref.current?.focus(); ref.current?.select(); }, []);
|
|
569
|
+
return (
|
|
570
|
+
<div className="fixed inset-0 z-50 bg-black/40 flex items-center justify-center" onClick={onCancel}>
|
|
571
|
+
<div className="bg-bg-elevated border border-border-hover rounded-lg shadow-md p-4 w-80" onClick={(e) => e.stopPropagation()}>
|
|
572
|
+
<div className="text-tiny uppercase tracking-widest text-text-tertiary font-body mb-2">Edit annotation</div>
|
|
573
|
+
<textarea ref={ref} value={text} onChange={(e) => setText(e.currentTarget.value)}
|
|
574
|
+
className="w-full bg-transparent border-none text-body font-body text-text-primary resize-vertical focus:outline-none min-h-[60px]"
|
|
575
|
+
onKeyDown={(e) => {
|
|
576
|
+
if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) onSave(text.trim());
|
|
577
|
+
if (e.key === "Escape") onCancel();
|
|
578
|
+
}}
|
|
579
|
+
/>
|
|
580
|
+
<div className="flex justify-end gap-2 mt-2">
|
|
581
|
+
<button onClick={onCancel} className="text-meta text-text-tertiary font-body px-3 py-1">Cancel</button>
|
|
582
|
+
<button onClick={() => onSave(text.trim())} className="text-meta font-medium font-body px-3 py-1 rounded-md bg-highlight-bg text-text-primary border border-highlight-border">Save</button>
|
|
583
|
+
</div>
|
|
584
|
+
</div>
|
|
585
|
+
</div>
|
|
586
|
+
);
|
|
587
|
+
}
|