@open-artifact/editor-kit 0.0.0-stage → 0.1.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.
- package/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +128 -2
- package/package.json +58 -4
- package/src/Announcer.d.ts +2 -0
- package/src/Announcer.js +8 -0
- package/src/FindBar.d.ts +31 -0
- package/src/FindBar.js +34 -0
- package/src/agent.d.ts +20 -0
- package/src/agent.js +40 -0
- package/src/announce.d.ts +18 -0
- package/src/announce.js +21 -0
- package/src/chrome.d.ts +180 -0
- package/src/chrome.js +108 -0
- package/src/collab/CommentList.d.ts +23 -0
- package/src/collab/CommentList.js +117 -0
- package/src/collab/CommentViews.d.ts +116 -0
- package/src/collab/CommentViews.js +531 -0
- package/src/collab/HistoryViews.d.ts +38 -0
- package/src/collab/HistoryViews.js +103 -0
- package/src/collab/PresenceViews.d.ts +52 -0
- package/src/collab/PresenceViews.js +46 -0
- package/src/collab/cds/cds.d.ts +148 -0
- package/src/collab/cds/cds.js +230 -0
- package/src/collab/cds/comment-card.d.ts +93 -0
- package/src/collab/cds/comment-card.js +211 -0
- package/src/collab/cds/comment-composer.d.ts +28 -0
- package/src/collab/cds/comment-composer.js +101 -0
- package/src/collab/cds/comments-css.d.ts +2 -0
- package/src/collab/cds/comments-css.js +3 -0
- package/src/collab/cds/comments.css +497 -0
- package/src/collab/cds/edge-piles.d.ts +69 -0
- package/src/collab/cds/edge-piles.js +178 -0
- package/src/collab/cds/layer.d.ts +41 -0
- package/src/collab/cds/layer.js +143 -0
- package/src/collab/cds/markdown.d.ts +17 -0
- package/src/collab/cds/markdown.js +507 -0
- package/src/collab/cds/shield.d.ts +28 -0
- package/src/collab/cds/shield.js +114 -0
- package/src/collab/cds/time.d.ts +2 -0
- package/src/collab/cds/time.js +41 -0
- package/src/collab/collab.d.ts +100 -0
- package/src/collab/collab.js +119 -0
- package/src/collab/comments.d.ts +147 -0
- package/src/collab/comments.js +287 -0
- package/src/collab/history-wiring.d.ts +32 -0
- package/src/collab/history-wiring.js +53 -0
- package/src/collab/history.d.ts +53 -0
- package/src/collab/history.js +94 -0
- package/src/collab/peer-colors.d.ts +123 -0
- package/src/collab/peer-colors.js +273 -0
- package/src/collab/pins.d.ts +70 -0
- package/src/collab/pins.js +44 -0
- package/src/collab/presence.d.ts +92 -0
- package/src/collab/presence.js +158 -0
- package/src/design-systems.d.ts +46 -0
- package/src/design-systems.js +61 -0
- package/src/downloads.d.ts +24 -0
- package/src/downloads.js +36 -0
- package/src/find.d.ts +64 -0
- package/src/find.js +147 -0
- package/src/index.d.ts +25 -0
- package/src/index.js +25 -0
- package/src/keys.d.ts +13 -0
- package/src/keys.js +20 -0
- package/src/merge.d.ts +23 -0
- package/src/merge.js +116 -0
- package/src/page-link.d.ts +33 -0
- package/src/page-link.js +75 -0
- package/src/project-sync.d.ts +118 -0
- package/src/project-sync.js +174 -0
- package/src/react.d.ts +16 -0
- package/src/react.js +16 -0
- package/src/roles.d.ts +33 -0
- package/src/roles.js +26 -0
- package/src/session.d.ts +42 -0
- package/src/session.js +37 -0
- package/src/shell.d.ts +56 -0
- package/src/shell.js +96 -0
- package/src/stash.d.ts +26 -0
- package/src/stash.js +66 -0
- package/src/styles.css +17 -0
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
3
|
+
import { Avatar, Button, cn, Icon, Menu, MenuItems, MenuPopup, Tooltip } from "./cds/cds.js";
|
|
4
|
+
import { Actions, Comment, CommentCard, DeleteConfirm, MoreMenu, Replies, ReplyField, ResolveAction, useLayerMenu } from "./cds/comment-card.js";
|
|
5
|
+
import { CommentComposer as CdsComposer } from "./cds/comment-composer.js";
|
|
6
|
+
import { EdgePiles, edgePiles, stackPins } from "./cds/edge-piles.js";
|
|
7
|
+
import { CommentsLayer, InLayer } from "./cds/layer.js";
|
|
8
|
+
import { commentMarkdown } from "./cds/markdown.js";
|
|
9
|
+
import { commentTime } from "./cds/time.js";
|
|
10
|
+
import { openMessageCount } from "./comments.js";
|
|
11
|
+
import { ALL_COMMENTS_RIGHT } from "./CommentList.js";
|
|
12
|
+
import { screenBox, toScreenPoint, toWorldPoint } from "./pins.js";
|
|
13
|
+
/** Pixels the pointer must travel before a press becomes a drag. */
|
|
14
|
+
const DRAG_THRESHOLD = 5;
|
|
15
|
+
/** A dragged area narrower or shorter than this is dropped. */
|
|
16
|
+
const AREA_MIN = 12;
|
|
17
|
+
export const COMMENT_TARGET = '#ae7303';
|
|
18
|
+
const BUBBLE_SVG = "<svg xmlns='http://www.w3.org/2000/svg' width='28' height='28' viewBox='0 0 28 28'><path d='M 2 26 L 2 14 A 12 12 0 1 1 14 26 Z' fill='#000' stroke='#fff' stroke-width='2'/></svg>";
|
|
19
|
+
export const COMMENT_CURSOR = `url("data:image/svg+xml,${BUBBLE_SVG.replace(/[ '<>#]/g, (c) => '%' + c.charCodeAt(0).toString(16).toUpperCase())}") 2 26, crosshair`;
|
|
20
|
+
/** Stops surface gestures from starting inside comment UI. */
|
|
21
|
+
const stop = (event) => event.stopPropagation();
|
|
22
|
+
/** A short "5m ago" style time. */
|
|
23
|
+
export function timeAgo(iso, now = Date.now()) {
|
|
24
|
+
const seconds = Math.max(0, (now - Date.parse(iso)) / 1000);
|
|
25
|
+
if (!Number.isFinite(seconds))
|
|
26
|
+
return '';
|
|
27
|
+
if (seconds < 60)
|
|
28
|
+
return 'just now';
|
|
29
|
+
if (seconds < 3600)
|
|
30
|
+
return `${Math.floor(seconds / 60)}m ago`;
|
|
31
|
+
if (seconds < 86400)
|
|
32
|
+
return `${Math.floor(seconds / 3600)}h ago`;
|
|
33
|
+
return new Date(iso).toLocaleDateString();
|
|
34
|
+
}
|
|
35
|
+
/** In comment mode: clicks place threads, drags draw areas. */
|
|
36
|
+
export function CommentCapture({ comments, enabled, panning = false, camera, anchorAt, areaAnchor, highlight, onHover, revision }) {
|
|
37
|
+
const on = comments.store((s) => s.on);
|
|
38
|
+
const open = comments.store((s) => s.draft !== null || Boolean(s.active));
|
|
39
|
+
const element = useRef(null);
|
|
40
|
+
const press = useRef(null);
|
|
41
|
+
const [area, setArea] = useState(null);
|
|
42
|
+
// The box under the pointer, in this layer's pixels (one pick per frame).
|
|
43
|
+
const [hover, setHover] = useState(null);
|
|
44
|
+
const hoverFrame = useRef(0);
|
|
45
|
+
/** Where the pointer rests (layer pixels), to pick again on `revision`. */
|
|
46
|
+
const resting = useRef(null);
|
|
47
|
+
const pickAgain = useRef(null);
|
|
48
|
+
useEffect(() => () => cancelAnimationFrame(hoverFrame.current), []);
|
|
49
|
+
const live = on && enabled;
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
if (live)
|
|
52
|
+
pickAgain.current?.();
|
|
53
|
+
}, [revision]);
|
|
54
|
+
// Escape during a drag drops the area (before the editor's own Escape).
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
if (!area)
|
|
57
|
+
return;
|
|
58
|
+
const onKey = (event) => {
|
|
59
|
+
if (event.key !== 'Escape')
|
|
60
|
+
return;
|
|
61
|
+
event.preventDefault();
|
|
62
|
+
event.stopPropagation();
|
|
63
|
+
press.current = null;
|
|
64
|
+
setArea(null);
|
|
65
|
+
};
|
|
66
|
+
window.addEventListener('keydown', onKey, true);
|
|
67
|
+
return () => window.removeEventListener('keydown', onKey, true);
|
|
68
|
+
}, [area !== null]);
|
|
69
|
+
// Leaving comment mode forgets the pointer.
|
|
70
|
+
useEffect(() => {
|
|
71
|
+
if (live)
|
|
72
|
+
return;
|
|
73
|
+
resting.current = null;
|
|
74
|
+
setHover(null);
|
|
75
|
+
}, [live]);
|
|
76
|
+
if (!on || !enabled || !comments.canComment())
|
|
77
|
+
return null;
|
|
78
|
+
const local = (event) => {
|
|
79
|
+
const bounds = element.current.getBoundingClientRect();
|
|
80
|
+
return { x: event.clientX - bounds.left, y: event.clientY - bounds.top };
|
|
81
|
+
};
|
|
82
|
+
const onDown = (event) => {
|
|
83
|
+
if (event.button !== 0 || panning)
|
|
84
|
+
return;
|
|
85
|
+
event.stopPropagation();
|
|
86
|
+
event.preventDefault();
|
|
87
|
+
press.current = local(event);
|
|
88
|
+
try {
|
|
89
|
+
element.current.setPointerCapture(event.pointerId);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
// Synthetic events carry no capturable pointer.
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
const clearHover = () => {
|
|
96
|
+
cancelAnimationFrame(hoverFrame.current);
|
|
97
|
+
resting.current = null;
|
|
98
|
+
setHover(null);
|
|
99
|
+
onHover?.(null);
|
|
100
|
+
};
|
|
101
|
+
const trackHover = (point) => {
|
|
102
|
+
resting.current = point;
|
|
103
|
+
if (!highlight && !onHover)
|
|
104
|
+
return;
|
|
105
|
+
cancelAnimationFrame(hoverFrame.current);
|
|
106
|
+
hoverFrame.current = requestAnimationFrame(() => {
|
|
107
|
+
const cam = camera();
|
|
108
|
+
const anchor = anchorAt(toWorldPoint(cam, point));
|
|
109
|
+
onHover?.(anchor);
|
|
110
|
+
if (!highlight)
|
|
111
|
+
return;
|
|
112
|
+
const box = highlight(anchor);
|
|
113
|
+
const next = box ? { x: box.x * cam.zoom + cam.x, y: box.y * cam.zoom + cam.y, w: box.w * cam.zoom, h: box.h * cam.zoom } : null;
|
|
114
|
+
setHover((last) => (last && next && last.x === next.x && last.y === next.y && last.w === next.w && last.h === next.h ? last : next));
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
pickAgain.current = () => {
|
|
118
|
+
if (resting.current && !press.current)
|
|
119
|
+
trackHover(resting.current);
|
|
120
|
+
};
|
|
121
|
+
const cancelDrag = () => {
|
|
122
|
+
press.current = null;
|
|
123
|
+
setArea(null);
|
|
124
|
+
};
|
|
125
|
+
const onMove = (event) => {
|
|
126
|
+
const start = press.current;
|
|
127
|
+
if (!start) {
|
|
128
|
+
if (panning || event.buttons)
|
|
129
|
+
clearHover();
|
|
130
|
+
else
|
|
131
|
+
trackHover(local(event));
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
// The button came up out of sight (another window took the release): the drag is over.
|
|
135
|
+
if ((event.buttons & 1) === 0) {
|
|
136
|
+
cancelDrag();
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const point = local(event);
|
|
140
|
+
// Hidden while pressed; what lies under the pointer stays as it was (the release picks it).
|
|
141
|
+
if (hover)
|
|
142
|
+
setHover(null);
|
|
143
|
+
if (!area && Math.hypot(point.x - start.x, point.y - start.y) < DRAG_THRESHOLD)
|
|
144
|
+
return;
|
|
145
|
+
setArea({ x: Math.min(start.x, point.x), y: Math.min(start.y, point.y), w: Math.abs(point.x - start.x), h: Math.abs(point.y - start.y) });
|
|
146
|
+
};
|
|
147
|
+
const onUp = (event) => {
|
|
148
|
+
const start = press.current;
|
|
149
|
+
press.current = null;
|
|
150
|
+
if (!start)
|
|
151
|
+
return;
|
|
152
|
+
event.stopPropagation();
|
|
153
|
+
const cam = camera();
|
|
154
|
+
const point = local(event);
|
|
155
|
+
const state = comments.store.getState();
|
|
156
|
+
// Decided from the press and the release, not the drawn area: the last move may not have rendered yet.
|
|
157
|
+
const w = Math.abs(point.x - start.x);
|
|
158
|
+
const h = Math.abs(point.y - start.y);
|
|
159
|
+
if (area || Math.hypot(w, h) >= DRAG_THRESHOLD) {
|
|
160
|
+
setArea(null);
|
|
161
|
+
// Too small to be an area: dropped.
|
|
162
|
+
if (w < AREA_MIN || h < AREA_MIN)
|
|
163
|
+
return;
|
|
164
|
+
state.setDraft(areaAnchor(toWorldPoint(cam, start), toWorldPoint(cam, point)));
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
state.setDraft(anchorAt(toWorldPoint(cam, point)));
|
|
168
|
+
};
|
|
169
|
+
return (_jsxs("div", { ref: element, "data-comment-capture": "", "aria-label": "Comment mode: click to comment, drag to comment on an area", className: "absolute inset-0 z-[2]", style: { cursor: panning ? 'grab' : COMMENT_CURSOR }, onPointerDown: onDown, onPointerMove: onMove, onPointerUp: onUp, onPointerCancel: cancelDrag, onPointerLeave: clearHover,
|
|
170
|
+
// The camera moves under a still pointer: the next move picks again.
|
|
171
|
+
onWheel: clearHover, onDoubleClick: stop, onContextMenu: (event) => event.preventDefault(), children: [hover && !area && !open && !panning ? (_jsx("div", { "data-comment-hover": "", className: "pointer-events-none absolute rounded", style: { left: hover.x, top: hover.y, width: hover.w, height: hover.h, border: `1.5px solid ${COMMENT_TARGET}` } })) : null, area && press.current ? _jsx(AreaOutline, { area: area, start: press.current }) : null] }));
|
|
172
|
+
}
|
|
173
|
+
function AreaOutline({ area, start }) {
|
|
174
|
+
return (_jsxs(_Fragment, { children: [_jsx("div", { "data-comment-area": "", className: "pointer-events-none absolute", style: { left: area.x, top: area.y, width: area.w, height: area.h, border: `1px dashed ${COMMENT_TARGET}` } }), _jsx("div", { className: "pointer-events-none absolute rounded-full", style: { left: start.x - 5, top: start.y - 5, width: 10, height: 10, background: COMMENT_TARGET } })] }));
|
|
175
|
+
}
|
|
176
|
+
/* -------------------------------------------------------------------------- */
|
|
177
|
+
/* Placing pins and cards */
|
|
178
|
+
/* -------------------------------------------------------------------------- */
|
|
179
|
+
const PIN = 36;
|
|
180
|
+
const PIN_GAP = 4;
|
|
181
|
+
const EDGE = 8;
|
|
182
|
+
/** A thread's card and the composer: 330px wide, 6px from the pin. */
|
|
183
|
+
const CARD_WIDTH = 330;
|
|
184
|
+
const CARD_GAP = 6;
|
|
185
|
+
/** The least room a card keeps beside its pin, however near the edge the pin is. */
|
|
186
|
+
const CARD_MIN_ROOM = 48;
|
|
187
|
+
export function pinBox(point, viewport) {
|
|
188
|
+
const below = viewport.height - (point.y + PIN_GAP);
|
|
189
|
+
const above = point.y - PIN_GAP;
|
|
190
|
+
const flip = below < 320 && above > below && (above >= 240 || (below < 240 && above >= 48));
|
|
191
|
+
const left = Math.min(Math.max(point.x, 0), Math.max(0, viewport.width - (PIN + EDGE)));
|
|
192
|
+
return { left, top: flip ? point.y - (PIN + PIN_GAP) : point.y + PIN_GAP, flip };
|
|
193
|
+
}
|
|
194
|
+
const cardCap = (height) => Math.max(320, Math.round(height * 0.6));
|
|
195
|
+
/** How tall the card beside `pin` may be: the cap, or the room on the pin's side, whichever is less. */
|
|
196
|
+
function cardMaxHeight(pin, viewport) {
|
|
197
|
+
const top = EDGE;
|
|
198
|
+
const bottom = viewport.height - EDGE;
|
|
199
|
+
const clamp = (value, min, max) => Math.min(Math.max(value, min), max);
|
|
200
|
+
const room = pin.flip
|
|
201
|
+
? clamp(pin.top + PIN + 2, Math.min(bottom, top + CARD_MIN_ROOM), bottom) - top
|
|
202
|
+
: bottom - clamp(pin.top - 2, top, Math.max(top, bottom - CARD_MIN_ROOM));
|
|
203
|
+
return Math.min(cardCap(viewport.height), Math.max(0, room));
|
|
204
|
+
}
|
|
205
|
+
export function CommentPopover({ pin, corner, viewport, onEscape, children }) {
|
|
206
|
+
const ref = useRef(null);
|
|
207
|
+
const [height, setHeight] = useState(0);
|
|
208
|
+
useLayoutEffect(() => {
|
|
209
|
+
const el = ref.current;
|
|
210
|
+
if (!el)
|
|
211
|
+
return;
|
|
212
|
+
setHeight(el.offsetHeight);
|
|
213
|
+
const observer = new ResizeObserver(() => setHeight(el.offsetHeight));
|
|
214
|
+
observer.observe(el);
|
|
215
|
+
return () => observer.disconnect();
|
|
216
|
+
}, []);
|
|
217
|
+
const width = Math.max(0, Math.min(CARD_WIDTH, viewport.width - 2 * EDGE));
|
|
218
|
+
let left;
|
|
219
|
+
let top;
|
|
220
|
+
let maxHeight;
|
|
221
|
+
if (corner || !pin) {
|
|
222
|
+
const at = corner ?? { x: EDGE, y: EDGE };
|
|
223
|
+
left = Math.max(EDGE, Math.min(at.x, viewport.width - EDGE - width));
|
|
224
|
+
top = Math.max(EDGE, Math.min(at.y, viewport.height - EDGE - height));
|
|
225
|
+
if (typeof children === 'function')
|
|
226
|
+
maxHeight = Math.min(cardCap(viewport.height), Math.max(CARD_MIN_ROOM, viewport.height - EDGE - Math.max(EDGE, at.y)));
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
left = pin.left + PIN + CARD_GAP;
|
|
230
|
+
if (left + width > viewport.width - EDGE) {
|
|
231
|
+
const before = pin.left - CARD_GAP - width;
|
|
232
|
+
left = before >= EDGE ? before : Math.max(EDGE, viewport.width - EDGE - width);
|
|
233
|
+
}
|
|
234
|
+
top = pin.flip ? pin.top + PIN + 2 - height : pin.top - 2;
|
|
235
|
+
top = Math.max(EDGE, Math.min(top, viewport.height - EDGE - height));
|
|
236
|
+
if (typeof children === 'function')
|
|
237
|
+
maxHeight = cardMaxHeight(pin, viewport);
|
|
238
|
+
}
|
|
239
|
+
// Unmeasured, it is see-through rather than hidden: a hidden textarea can't take focus.
|
|
240
|
+
const style = {
|
|
241
|
+
position: 'absolute',
|
|
242
|
+
left,
|
|
243
|
+
top,
|
|
244
|
+
width,
|
|
245
|
+
pointerEvents: 'auto',
|
|
246
|
+
...(maxHeight === undefined ? {} : { maxHeight, display: 'flex', flexDirection: 'column' }),
|
|
247
|
+
...(height ? {} : { opacity: 0, pointerEvents: 'none' }),
|
|
248
|
+
};
|
|
249
|
+
return (_jsx("div", { ref: ref, style: style, "data-comment-card": "", onKeyDown: (event) => {
|
|
250
|
+
if (event.key !== 'Escape' || event.defaultPrevented)
|
|
251
|
+
return;
|
|
252
|
+
event.preventDefault();
|
|
253
|
+
onEscape();
|
|
254
|
+
}, children: typeof children === 'function' ? children(maxHeight) : children }));
|
|
255
|
+
}
|
|
256
|
+
const PIN_CLASS = 'cds-reset absolute flex items-center justify-center rounded-full rounded-tl-none border-0 p-0 cursor-[var(--cds-cursor-interactive)] outline-none focus-visible:outline-hidden focus-visible:shadow-focus';
|
|
257
|
+
function PinButton({ pin, avatar, draft = false, label, title, expanded, faded = false, ...handlers }) {
|
|
258
|
+
const { pinId, ...events } = handlers;
|
|
259
|
+
const style = {
|
|
260
|
+
left: pin.left,
|
|
261
|
+
top: pin.top,
|
|
262
|
+
width: PIN,
|
|
263
|
+
height: PIN,
|
|
264
|
+
pointerEvents: draft ? 'none' : 'auto',
|
|
265
|
+
touchAction: 'none',
|
|
266
|
+
...(pin.flip ? { borderRadius: '9999px 9999px 9999px 0' } : {}),
|
|
267
|
+
...(faded ? { opacity: 0.7 } : {}),
|
|
268
|
+
};
|
|
269
|
+
return (_jsx("button", { type: "button", "data-comment-pin": pinId, "aria-label": label, title: title, "aria-expanded": expanded, tabIndex: draft ? -1 : undefined, className: cn(PIN_CLASS, draft ? 'bg-fill-accent' : 'bg-surface-2 shadow-panel-sm'), style: style, ...events, children: _jsx("span", { className: "flex rounded-full bg-surface-1", children: avatar }) }));
|
|
270
|
+
}
|
|
271
|
+
/** A message's author as the cards name them. */
|
|
272
|
+
function authorName(message, agent) {
|
|
273
|
+
if (message.author.kind === 'agent')
|
|
274
|
+
return message.author.name || agent.name;
|
|
275
|
+
return message.author.name || 'Someone';
|
|
276
|
+
}
|
|
277
|
+
function threadAvatar(thread, agent) {
|
|
278
|
+
const first = thread.messages[0];
|
|
279
|
+
if (!first)
|
|
280
|
+
return _jsx(Avatar, { name: "?", size: "sm" });
|
|
281
|
+
return _jsx(Avatar, { name: authorName(first, agent), variant: first.author.kind === 'agent' ? 'agent' : 'default', size: "sm" });
|
|
282
|
+
}
|
|
283
|
+
export function CommentPins({ comments, camera, viewport, place, highlight, anchorAt, movable, onMove, agent, revision, piles = true, insets }) {
|
|
284
|
+
const canMove = (movable ?? true) && Boolean(anchorAt || onMove);
|
|
285
|
+
const available = comments.store((s) => s.available);
|
|
286
|
+
const threads = comments.store((s) => s.threads);
|
|
287
|
+
const active = comments.store((s) => s.active);
|
|
288
|
+
const draft = comments.store((s) => s.draft);
|
|
289
|
+
const on = comments.store((s) => s.on);
|
|
290
|
+
const showResolved = comments.store((s) => s.showResolved);
|
|
291
|
+
const listOpen = comments.store((s) => s.listOpen);
|
|
292
|
+
// A dragged pin's offset from its place.
|
|
293
|
+
const [drag, setDrag] = useState(null);
|
|
294
|
+
const placed = useMemo(() => {
|
|
295
|
+
if (!available)
|
|
296
|
+
return [];
|
|
297
|
+
const out = [];
|
|
298
|
+
for (const thread of threads) {
|
|
299
|
+
if (thread.messages.length === 0)
|
|
300
|
+
continue;
|
|
301
|
+
if (thread.status !== 'open' && !showResolved && thread.id !== active)
|
|
302
|
+
continue;
|
|
303
|
+
const anchor = comments.anchorOf(thread);
|
|
304
|
+
const at = place(anchor);
|
|
305
|
+
if (at)
|
|
306
|
+
out.push({ key: thread.id, thread, anchor, state: at.state, screen: toScreenPoint(camera, at.world) });
|
|
307
|
+
}
|
|
308
|
+
if (draft !== null) {
|
|
309
|
+
const at = place(draft);
|
|
310
|
+
if (at)
|
|
311
|
+
out.push({ key: 'draft', thread: null, anchor: draft, state: at.state, screen: toScreenPoint(camera, at.world) });
|
|
312
|
+
}
|
|
313
|
+
return out;
|
|
314
|
+
}, [available, threads, active, draft, showResolved, camera, place, revision, comments]);
|
|
315
|
+
if (!available)
|
|
316
|
+
return null;
|
|
317
|
+
if (!on && !active && draft === null && placed.length === 0)
|
|
318
|
+
return null;
|
|
319
|
+
const focusKey = draft !== null ? 'draft' : active;
|
|
320
|
+
const focus = placed.find((p) => p.key === focusKey);
|
|
321
|
+
const lit = focus && highlight ? highlight(focus.anchor) : null;
|
|
322
|
+
const pinned = placed.filter((p) => p.thread);
|
|
323
|
+
const pileSet = piles
|
|
324
|
+
? edgePiles({
|
|
325
|
+
points: pinned.map((p) => ({ id: p.key, x: p.screen.x, y: p.screen.y })),
|
|
326
|
+
openId: active,
|
|
327
|
+
box: { left: 0, top: 0, width: viewport.width, height: viewport.height },
|
|
328
|
+
top: 0,
|
|
329
|
+
floor: insets?.top ?? 0,
|
|
330
|
+
foot: insets?.bottom ? insets.bottom + 8 : 0,
|
|
331
|
+
reserveRight: listOpen ? ALL_COMMENTS_RIGHT : 0,
|
|
332
|
+
})
|
|
333
|
+
: null;
|
|
334
|
+
const shown = pinned.filter((p) => !pileSet?.piled.has(p.key)).map((p) => ({ ...p, box: pinBox(p.screen, viewport) }));
|
|
335
|
+
const stacked = stackPins(shown.map((p) => ({ id: p.key, x: p.box.left, y: p.box.top, w: PIN })), viewport.width, PIN);
|
|
336
|
+
const select = (id) => comments.store.getState().setActive(id);
|
|
337
|
+
const open = focus?.thread ? focus : undefined;
|
|
338
|
+
return (_jsxs(_Fragment, { children: [lit ? (
|
|
339
|
+
// An area is drawn as it was dragged (dashed); an element, a board or a note gets the hover ring.
|
|
340
|
+
_jsx("div", { "data-comment-target": "", className: cn('pointer-events-none absolute', !lit.area && 'rounded'), style: { ...screenBox(camera, lit), border: lit.area ? `1px dashed ${COMMENT_TARGET}` : `1.5px solid ${COMMENT_TARGET}` } })) : null, _jsxs(CommentsLayer, { className: "pointer-events-none absolute inset-0 z-[3]", mark: "data-comment-layer", onEscape: () => {
|
|
341
|
+
const state = comments.store.getState();
|
|
342
|
+
if (state.draft !== null || state.active) {
|
|
343
|
+
state.setDraft(null);
|
|
344
|
+
state.setActive(null);
|
|
345
|
+
return true;
|
|
346
|
+
}
|
|
347
|
+
return false;
|
|
348
|
+
}, children: [shown.map((p) => {
|
|
349
|
+
const thread = p.thread;
|
|
350
|
+
const dragging = drag?.id === thread.id ? drag : null;
|
|
351
|
+
const box = { ...p.box, left: p.box.left + (stacked.get(p.key) ?? 0) + (dragging?.x ?? 0), top: p.box.top + (dragging?.y ?? 0) };
|
|
352
|
+
return (_jsx(ThreadPin, { comments: comments, thread: thread, box: box, avatar: threadAvatar(thread, agent), expanded: active === thread.id, detached: p.state === 'detached', onDrag: (delta) => setDrag(delta ? { id: thread.id, ...delta } : null), movable: canMove, onDrop: (delta) => {
|
|
353
|
+
const screen = { x: p.screen.x + delta.x, y: p.screen.y + delta.y };
|
|
354
|
+
if (onMove)
|
|
355
|
+
onMove(thread.id, screen);
|
|
356
|
+
else if (anchorAt)
|
|
357
|
+
void comments.move(thread.id, anchorAt(toWorldPoint(camera, screen)));
|
|
358
|
+
} }, p.key));
|
|
359
|
+
}), pileSet ? (_jsx("div", { style: { display: 'contents', pointerEvents: 'auto' }, children: _jsx(EdgePiles, { piles: pileSet, onOpen: (id) => {
|
|
360
|
+
const thread = threads.find((t) => t.id === id);
|
|
361
|
+
if (thread)
|
|
362
|
+
comments.show(thread);
|
|
363
|
+
} }) })) : null, open ? (_jsx(CommentPopover, { pin: pinBox(open.screen, viewport), viewport: viewport, onEscape: () => select(null), children: (maxHeight) => _jsx(ThreadCard, { comments: comments, agent: agent, thread: open.thread, state: open.state, maxHeight: maxHeight }) }, open.key)) : null, focus && !focus.thread && draft !== null ? (_jsxs(_Fragment, { children: [_jsx(PinButton, { pin: pinBox(focus.screen, viewport), draft: true, label: "New comment", avatar: _jsx(Avatar, { name: comments.viewerName(), size: "sm" }) }), _jsx(CommentPopover, { pin: pinBox(focus.screen, viewport), viewport: viewport, onEscape: () => comments.store.getState().setDraft(null), children: _jsx(CommentComposer, { comments: comments, agent: agent, anchor: draft }) })] })) : null] })] }));
|
|
364
|
+
}
|
|
365
|
+
/** One thread's pin: a click opens its card, a drag moves the thread. */
|
|
366
|
+
function ThreadPin({ comments, thread, box, avatar, expanded, detached, movable, onDrag, onDrop, }) {
|
|
367
|
+
const press = useRef(null);
|
|
368
|
+
/** The last press was a drag: the click that follows it does nothing. */
|
|
369
|
+
const dragged = useRef(false);
|
|
370
|
+
const resolved = thread.status === 'resolved';
|
|
371
|
+
return (_jsx(PinButton, { pinId: thread.id, pin: box, avatar: avatar, expanded: expanded, faded: detached, label: `Comment: ${thread.label}${resolved ? ' (resolved)' : ''}`, title: detached ? 'What this comment was on is gone' : thread.label, onPointerDown: (event) => {
|
|
372
|
+
if (event.button !== 0)
|
|
373
|
+
return;
|
|
374
|
+
press.current = { x: event.clientX, y: event.clientY, moved: false };
|
|
375
|
+
dragged.current = false;
|
|
376
|
+
try {
|
|
377
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
378
|
+
}
|
|
379
|
+
catch {
|
|
380
|
+
// no capturable pointer
|
|
381
|
+
}
|
|
382
|
+
}, onPointerMove: (event) => {
|
|
383
|
+
const p = press.current;
|
|
384
|
+
if (!p || !movable || !comments.canComment())
|
|
385
|
+
return;
|
|
386
|
+
const dx = event.clientX - p.x;
|
|
387
|
+
const dy = event.clientY - p.y;
|
|
388
|
+
if (!p.moved && Math.hypot(dx, dy) < DRAG_THRESHOLD)
|
|
389
|
+
return;
|
|
390
|
+
p.moved = true;
|
|
391
|
+
onDrag({ x: dx, y: dy });
|
|
392
|
+
}, onPointerUp: (event) => {
|
|
393
|
+
const p = press.current;
|
|
394
|
+
press.current = null;
|
|
395
|
+
if (!p?.moved)
|
|
396
|
+
return;
|
|
397
|
+
dragged.current = true;
|
|
398
|
+
onDrag(null);
|
|
399
|
+
onDrop({ x: event.clientX - p.x, y: event.clientY - p.y });
|
|
400
|
+
}, onClick: () => {
|
|
401
|
+
if (dragged.current) {
|
|
402
|
+
dragged.current = false;
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
const state = comments.store.getState();
|
|
406
|
+
state.setActive(state.active === thread.id ? null : thread.id);
|
|
407
|
+
} }));
|
|
408
|
+
}
|
|
409
|
+
/* -------------------------------------------------------------------------- */
|
|
410
|
+
/* A thread's card */
|
|
411
|
+
/* -------------------------------------------------------------------------- */
|
|
412
|
+
function useFollowReplies(region, thread) {
|
|
413
|
+
const follow = useRef(null);
|
|
414
|
+
useLayoutEffect(() => {
|
|
415
|
+
const f = follow.current;
|
|
416
|
+
const el = region.current;
|
|
417
|
+
if (!f || !el)
|
|
418
|
+
return;
|
|
419
|
+
el.scrollTop = el.scrollHeight;
|
|
420
|
+
const newest = thread.messages.at(-1);
|
|
421
|
+
if (newest && newest.id !== f.after && newest.author.kind === 'agent') {
|
|
422
|
+
const item = [...el.querySelectorAll('[data-cds-part="comment"]')].at(-1);
|
|
423
|
+
const over = item ? el.getBoundingClientRect().top - item.getBoundingClientRect().top : 0;
|
|
424
|
+
if (over > 0) {
|
|
425
|
+
el.scrollTop -= over;
|
|
426
|
+
follow.current = null;
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
f.at = el.scrollTop;
|
|
431
|
+
});
|
|
432
|
+
const stop = () => {
|
|
433
|
+
follow.current = null;
|
|
434
|
+
};
|
|
435
|
+
return {
|
|
436
|
+
start: () => {
|
|
437
|
+
follow.current = { after: thread.messages.at(-1)?.id, at: 0 };
|
|
438
|
+
},
|
|
439
|
+
regionProps: {
|
|
440
|
+
onPointerDownCapture: stop,
|
|
441
|
+
onWheelCapture: (event) => {
|
|
442
|
+
if (event.deltaY < 0)
|
|
443
|
+
stop();
|
|
444
|
+
},
|
|
445
|
+
onScroll: (event) => {
|
|
446
|
+
const el = event.currentTarget;
|
|
447
|
+
const f = follow.current;
|
|
448
|
+
if (f && Math.abs(el.scrollTop - f.at) > 2 && el.scrollHeight - el.scrollTop - el.clientHeight > 2)
|
|
449
|
+
stop();
|
|
450
|
+
},
|
|
451
|
+
},
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
export function ThreadCard(props) {
|
|
455
|
+
return (_jsx(InLayer, { children: _jsx(ThreadCardBody, { ...props }) }));
|
|
456
|
+
}
|
|
457
|
+
function ThreadCardBody({ comments, agent, thread, state, maxHeight, onClose }) {
|
|
458
|
+
const [confirming, setConfirming] = useState(false);
|
|
459
|
+
const [resolving, setResolving] = useState(false);
|
|
460
|
+
const [deleting, setDeleting] = useState(false);
|
|
461
|
+
const [replying, setReplying] = useState(false);
|
|
462
|
+
const nameId = useId();
|
|
463
|
+
const region = useRef(null);
|
|
464
|
+
const follow = useFollowReplies(region, thread);
|
|
465
|
+
const draft = comments.store((s) => s.replies[thread.id]) ?? { text: '', send: false };
|
|
466
|
+
const sendable = comments.canSendToAgent() && agent.available;
|
|
467
|
+
const commenting = comments.canComment();
|
|
468
|
+
const [top, ...replies] = thread.messages;
|
|
469
|
+
if (!top)
|
|
470
|
+
return null;
|
|
471
|
+
const close = onClose ?? (() => comments.store.getState().setActive(null));
|
|
472
|
+
const note = comments.stateNote(state ?? comments.stateOf(comments.anchorOf(thread)));
|
|
473
|
+
const sendLabel = `Send to ${agent.name}`;
|
|
474
|
+
const topItems = comments.canRemove()
|
|
475
|
+
? [{ label: 'Delete thread', icon: 'Trash', variant: 'danger', onSelect: () => setConfirming(true) }]
|
|
476
|
+
: [];
|
|
477
|
+
const status = thread.status === 'open' ? thread.agent?.status : undefined;
|
|
478
|
+
const working = status === 'pending' ? 'queued' : status === 'delivered' ? 'working' : undefined;
|
|
479
|
+
const setDraft = (next) => comments.store.getState().setReply(thread.id, next);
|
|
480
|
+
return (_jsxs(CommentCard, { expanded: true, active: true, onClose: close, labelledBy: nameId, maxHeight: maxHeight, scrollRef: region, scrollProps: follow.regionProps, className: "min-h-0 flex-1", reply: commenting && (_jsx(ReplyField, { children: _jsx(CdsComposer, { label: "Reply", placeholder: thread.status === 'resolved' ? 'Reply to reopen…' : sendable && draft.send ? 'What do you want to edit?' : undefined, value: draft.text, onChange: (text) => setDraft({ ...draft, text }), sendBox: sendable ? { label: sendLabel, checked: draft.send, onCheckedChange: (send) => setDraft({ ...draft, send }) } : undefined, busy: replying, onSubmit: (text, { send }) => {
|
|
481
|
+
follow.start();
|
|
482
|
+
setReplying(true);
|
|
483
|
+
void comments.reply(thread.id, text, { send: sendable && send }).finally(() => setReplying(false));
|
|
484
|
+
}, onCancel: close }) })), children: [_jsx(Comment, { nameId: nameId, authorName: authorName(top, agent), agent: top.author.kind === 'agent', timestamp: commentTime(top.at), footnote: note ?? undefined, confirm: confirming ? (_jsx(DeleteConfirm, { busy: deleting, onCancel: () => setConfirming(false), onDelete: () => {
|
|
485
|
+
setDeleting(true);
|
|
486
|
+
void comments.remove(thread.id).finally(() => setDeleting(false));
|
|
487
|
+
} })) : undefined, actions: commenting || topItems.length > 0 ? (_jsxs(Actions, { children: [commenting ? (_jsx(ResolveAction, { resolved: thread.status === 'resolved', busy: resolving, onResolve: () => {
|
|
488
|
+
setResolving(true);
|
|
489
|
+
void comments.resolve(thread.id, true).finally(() => setResolving(false));
|
|
490
|
+
} })) : null, _jsx(MoreMenu, { items: topItems })] })) : undefined, children: commentMarkdown(top.text) }), replies.length > 0 || working ? (_jsxs(Replies, { children: [replies.map((message) => (_jsx(Comment, { authorName: authorName(message, agent), agent: message.author.kind === 'agent', timestamp: commentTime(message.at), children: commentMarkdown(message.text) }, message.id))), working ? _jsx(Comment, { authorName: agent.name, agent: true, working: working, footnote: working === 'queued' ? 'Waiting for the agent' : 'Working…' }) : null] })) : null] }));
|
|
491
|
+
}
|
|
492
|
+
export function CommentComposer(props) {
|
|
493
|
+
return (_jsx(InLayer, { children: _jsx(ComposerBody, { ...props }) }));
|
|
494
|
+
}
|
|
495
|
+
function ComposerBody({ comments, agent, anchor, onCancel }) {
|
|
496
|
+
const text = comments.store((s) => s.draftText);
|
|
497
|
+
const send = comments.store((s) => s.sendNew);
|
|
498
|
+
const [busy, setBusy] = useState(false);
|
|
499
|
+
const field = useRef(null);
|
|
500
|
+
const sendable = comments.canSendToAgent() && agent.available;
|
|
501
|
+
useEffect(() => {
|
|
502
|
+
field.current?.focus({ preventScroll: true });
|
|
503
|
+
}, [anchor]);
|
|
504
|
+
const cancel = () => (onCancel ? onCancel() : comments.store.getState().setDraft(null));
|
|
505
|
+
return (_jsx(CdsComposer, { textareaRef: field, context: comments.labelOf(anchor), value: text, onChange: (next) => comments.store.getState().setDraftText(next), sendBox: sendable ? { label: `Send to ${agent.name}`, checked: send, onCheckedChange: (on) => comments.store.getState().setSendNew(on) } : undefined, busy: busy, onSubmit: (body, how) => {
|
|
506
|
+
if (busy)
|
|
507
|
+
return;
|
|
508
|
+
setBusy(true);
|
|
509
|
+
void comments.create(anchor, body, { send: sendable && how.send }).finally(() => setBusy(false));
|
|
510
|
+
}, onCancel: cancel }));
|
|
511
|
+
}
|
|
512
|
+
export function CommentsButton({ comments, shortcut = 'C', side = 'bottom' }) {
|
|
513
|
+
const available = comments.store((s) => s.available);
|
|
514
|
+
if (!available)
|
|
515
|
+
return null;
|
|
516
|
+
return (_jsx(CommentsLayer, { inline: true, mark: "data-comments-button", children: _jsx(ButtonBody, { comments: comments, shortcut: shortcut, side: side }) }));
|
|
517
|
+
}
|
|
518
|
+
function ButtonBody({ comments, shortcut, side }) {
|
|
519
|
+
const [menuOpen, setMenuOpen] = useLayerMenu();
|
|
520
|
+
const count = comments.store((s) => openMessageCount(s.threads));
|
|
521
|
+
const on = comments.store((s) => s.on);
|
|
522
|
+
const panelOpen = comments.store((s) => s.listOpen);
|
|
523
|
+
const waiting = !on && count > 0;
|
|
524
|
+
const tip = on ? 'Done commenting' : count > 0 ? `${count} open ${count === 1 ? 'comment' : 'comments'}` : 'Comments';
|
|
525
|
+
const setPanel = (open) => {
|
|
526
|
+
setMenuOpen(false);
|
|
527
|
+
comments.store.getState().setListOpen(open);
|
|
528
|
+
};
|
|
529
|
+
const item = panelOpen ? { label: 'Hide comments', onSelect: () => setPanel(false) } : { label: 'Show all comments', trailing: count, onSelect: () => setPanel(true) };
|
|
530
|
+
return (_jsxs("span", { className: "flex shrink-0 items-center", children: [comments.canComment() ? (_jsx(Tooltip, { content: tip, shortcut: shortcut, side: side, children: _jsx(Button, { variant: "ghost", iconOnly: true, tooltip: false, "aria-label": "Comments", "aria-pressed": on, icon: _jsxs("span", { className: "relative inline-flex", children: [_jsx(Icon, { name: on ? 'CommentFilled' : 'Comment', size: "sm" }), waiting ? (_jsx("span", { "aria-hidden": true, className: "absolute rounded-full bg-fill-accent", style: { top: -1, right: -2, width: 6, height: 6, boxShadow: '0 0 0 1.5px var(--cds-page-bg)' } })) : null] }), onClick: () => comments.store.getState().setOn(!on) }) })) : null, _jsxs(Menu.Root, { open: menuOpen, onOpenChange: setMenuOpen, modal: false, children: [_jsx(Tooltip, { content: "Comment options", disabled: menuOpen, side: side, children: _jsx(Menu.Trigger, { render: _jsx(Button, { variant: "ghost", "aria-label": "Comment options", style: { paddingInline: 2 }, children: _jsx(Icon, { name: "CaretDown", size: "xs" }) }) }) }), _jsx(MenuPopup, { align: "end", children: _jsx(MenuItems, { items: [item] }) })] })] }));
|
|
531
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Version history on the page, for any editor: the panel (`HistoryPanel`
|
|
3
|
+
* from the UI package) on the right, and the frame of a read-only preview
|
|
4
|
+
* of a past version over the editor: its banner (restore it, back to the
|
|
5
|
+
* latest), a pan-and-zoom surface fitted to what the editor draws, Esc to
|
|
6
|
+
* leave.
|
|
7
|
+
*
|
|
8
|
+
* Editor-agnostic: the editor draws
|
|
9
|
+
* the version's content in world px (`renderWorld`) and anything that
|
|
10
|
+
* follows it on screen (`renderOverlay`).
|
|
11
|
+
*/
|
|
12
|
+
import { type ReactNode } from 'react';
|
|
13
|
+
import type { HistoryController } from './history.ts';
|
|
14
|
+
import type { Box, Camera } from './pins.ts';
|
|
15
|
+
/** The history panel, on the right. */
|
|
16
|
+
export declare function HistoryAside<P>({ history, viewerId }: {
|
|
17
|
+
history: HistoryController<P>;
|
|
18
|
+
viewerId: string;
|
|
19
|
+
}): ReactNode;
|
|
20
|
+
/** A camera that shows `rect` whole (at most 100%) in a `w` × `h` viewport. */
|
|
21
|
+
export declare function fitCamera(rect: Box | null, w: number, h: number, margin?: number): Camera;
|
|
22
|
+
export interface VersionPreviewProps<P> {
|
|
23
|
+
history: HistoryController<P>;
|
|
24
|
+
/** The viewer cannot edit (no restore, no "go back to edit" hint). */
|
|
25
|
+
readOnly: boolean;
|
|
26
|
+
/** World bounds of what the preview shows (fitted when it changes), `null` when empty. */
|
|
27
|
+
bounds(preview: P): Box | null;
|
|
28
|
+
/** The version's content in world px. */
|
|
29
|
+
renderWorld(preview: P): ReactNode;
|
|
30
|
+
/** Screen-space content that follows the camera (labels, per-part restore buttons). */
|
|
31
|
+
renderOverlay?(preview: P, camera: Camera, n: number, restorable: boolean): ReactNode;
|
|
32
|
+
/** A key that refits when it changes (the page shown…). */
|
|
33
|
+
fitKey?: unknown;
|
|
34
|
+
/** What the preview says when there is nothing to draw. */
|
|
35
|
+
emptyText?(n: number): string;
|
|
36
|
+
}
|
|
37
|
+
/** The read-only preview of a past version, over the editor. */
|
|
38
|
+
export declare function VersionPreview<P>({ history, readOnly, bounds, renderWorld, renderOverlay, fitKey, emptyText }: VersionPreviewProps<P>): ReactNode;
|