@milesroxas/design-hub 0.5.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/kit/dials.css +9 -8
- package/kit/dials.tsx +39 -20
- package/kit/prefs.ts +32 -6
- package/kit/ui/Menu.tsx +28 -4
- package/kit/ui/index.ts +9 -1
- package/package.json +2 -2
- package/shell/review/Bar.tsx +156 -0
- package/shell/review/Card.tsx +106 -0
- package/shell/review/Field.tsx +99 -0
- package/shell/review/Launcher.tsx +180 -0
- package/shell/review/People.tsx +168 -0
- package/shell/review/PickLayer.tsx +130 -0
- package/shell/review/Pins.tsx +97 -0
- package/shell/review/Review.tsx +282 -902
- package/shell/review/ThreadCard.tsx +498 -0
- package/shell/review/anchor.ts +2 -1
- package/shell/review/client.ts +3 -0
- package/shell/review/place.test.ts +72 -0
- package/shell/review/place.ts +78 -0
- package/shell/review/review.css +504 -205
- package/shell/review/sheet.ts +107 -0
- package/shell/review/snap.test.ts +44 -0
- package/shell/review/snap.ts +59 -0
- package/shell/review/view.test.ts +155 -0
- package/shell/review/view.ts +101 -0
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
// One thread in a card beside its pin. Reading and replying are the job, so
|
|
2
|
+
// the reply box owns the foot at full width; the thread's one decision,
|
|
3
|
+
// Resolve, sits in the head beside what the comment is on. Kind and state
|
|
4
|
+
// show as tags once set and are changed from ⋯. A resolved thread wears a
|
|
5
|
+
// check, a band naming who resolved it, and Reopen where Resolve was.
|
|
6
|
+
import { Icon } from '@kit/Icon';
|
|
7
|
+
import { Button, type MenuEntry, useMenu } from '@kit/ui';
|
|
8
|
+
import { type KeyboardEvent, useEffect, useState } from 'react';
|
|
9
|
+
import { type Comment, KINDS, type Kind, LIMITS } from '../../core/review/model';
|
|
10
|
+
import { Action, Card } from './Card';
|
|
11
|
+
import { act, type Me, type ReviewStore } from './client';
|
|
12
|
+
import { Field, useTextBox } from './Field';
|
|
13
|
+
import { describe } from './PickLayer';
|
|
14
|
+
|
|
15
|
+
const KIND_LABEL: Record<Kind, string> = { bug: 'Bug', taste: 'Taste', copy: 'Copy' };
|
|
16
|
+
const relative = new Intl.RelativeTimeFormat('en', { numeric: 'auto', style: 'short' });
|
|
17
|
+
const STEPS: [Intl.RelativeTimeFormatUnit, number][] = [
|
|
18
|
+
['year', 31_536_000],
|
|
19
|
+
['month', 2_592_000],
|
|
20
|
+
['week', 604_800],
|
|
21
|
+
['day', 86_400],
|
|
22
|
+
['hour', 3600],
|
|
23
|
+
['minute', 60],
|
|
24
|
+
];
|
|
25
|
+
function ago(iso: string): string {
|
|
26
|
+
const seconds = (Date.parse(iso) - Date.now()) / 1000;
|
|
27
|
+
for (const [unit, size] of STEPS)
|
|
28
|
+
if (Math.abs(seconds) >= size) return relative.format(Math.round(seconds / size), unit);
|
|
29
|
+
return 'just now';
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** What the comment is on, in a few words. */
|
|
33
|
+
function about(root: Comment): string {
|
|
34
|
+
const target = root.anchor?.target;
|
|
35
|
+
if (!target) return 'On the page';
|
|
36
|
+
const el = document.querySelector(target);
|
|
37
|
+
return el ? `On ${describe(el)}` : 'On another view';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function Avatar({ name }: { name: string }) {
|
|
41
|
+
const initials = name
|
|
42
|
+
.split(/[\s-]+/)
|
|
43
|
+
.filter(Boolean)
|
|
44
|
+
.map((w) => w[0])
|
|
45
|
+
.slice(0, 2)
|
|
46
|
+
.join('')
|
|
47
|
+
.toUpperCase();
|
|
48
|
+
return (
|
|
49
|
+
<span className="rv-avatar" aria-hidden="true">
|
|
50
|
+
{initials}
|
|
51
|
+
</span>
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Editing in place: the words stay where they were, in a quiet field, with Cancel and Save under them. */
|
|
56
|
+
function EditBox({
|
|
57
|
+
initial,
|
|
58
|
+
onSave,
|
|
59
|
+
onCancel,
|
|
60
|
+
}: {
|
|
61
|
+
initial: string;
|
|
62
|
+
onSave: (body: string) => Promise<unknown>;
|
|
63
|
+
onCancel: () => void;
|
|
64
|
+
}) {
|
|
65
|
+
const { body, setBody, busy, setBusy, area } = useTextBox(initial, 200);
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
const a = area.current;
|
|
68
|
+
if (!a || !matchMedia('(pointer: fine)').matches) return;
|
|
69
|
+
a.focus();
|
|
70
|
+
a.setSelectionRange(a.value.length, a.value.length);
|
|
71
|
+
}, [area]);
|
|
72
|
+
const save = async () => {
|
|
73
|
+
const next = body.trim();
|
|
74
|
+
if (!next || busy) return;
|
|
75
|
+
if (next === initial.trim()) return onCancel();
|
|
76
|
+
setBusy(true);
|
|
77
|
+
try {
|
|
78
|
+
await onSave(next);
|
|
79
|
+
} catch {
|
|
80
|
+
setBusy(false);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
const onKey = (e: KeyboardEvent<HTMLTextAreaElement>) => {
|
|
84
|
+
if (e.key === 'Escape') {
|
|
85
|
+
e.preventDefault();
|
|
86
|
+
e.stopPropagation();
|
|
87
|
+
onCancel();
|
|
88
|
+
} else if (e.key === 'Enter' && !e.shiftKey) {
|
|
89
|
+
e.preventDefault();
|
|
90
|
+
void save();
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
return (
|
|
94
|
+
<div className="rv-edit-box">
|
|
95
|
+
<textarea
|
|
96
|
+
ref={area}
|
|
97
|
+
rows={1}
|
|
98
|
+
value={body}
|
|
99
|
+
maxLength={LIMITS.body}
|
|
100
|
+
aria-label="Edit comment"
|
|
101
|
+
enterKeyHint="done"
|
|
102
|
+
onChange={(e) => setBody(e.target.value)}
|
|
103
|
+
onKeyDown={onKey}
|
|
104
|
+
/>
|
|
105
|
+
<div className="rv-edit-actions">
|
|
106
|
+
<Button kind="tertiary" size={null} label="Cancel" onClick={onCancel} />
|
|
107
|
+
<Button
|
|
108
|
+
kind="primary"
|
|
109
|
+
size={null}
|
|
110
|
+
label="Save"
|
|
111
|
+
busy={busy}
|
|
112
|
+
disabled={!body.trim()}
|
|
113
|
+
onClick={() => void save()}
|
|
114
|
+
/>
|
|
115
|
+
</div>
|
|
116
|
+
</div>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function EntryHead({ comment, canEdit, onEdit }: { comment: Comment; canEdit: boolean; onEdit: () => void }) {
|
|
121
|
+
return (
|
|
122
|
+
<header className="rv-msg-head">
|
|
123
|
+
<strong>{comment.author.name}</strong>
|
|
124
|
+
<time className="rv-muted" dateTime={comment.createdAt}>
|
|
125
|
+
{ago(comment.createdAt)}
|
|
126
|
+
{comment.editedAt && ' · edited'}
|
|
127
|
+
</time>
|
|
128
|
+
{canEdit && (
|
|
129
|
+
<Button
|
|
130
|
+
kind="tertiary"
|
|
131
|
+
size={null}
|
|
132
|
+
className="rv-act rv-edit"
|
|
133
|
+
icon="edit-02"
|
|
134
|
+
iconSize={14}
|
|
135
|
+
aria-label="Edit comment"
|
|
136
|
+
title="Edit"
|
|
137
|
+
onClick={onEdit}
|
|
138
|
+
/>
|
|
139
|
+
)}
|
|
140
|
+
</header>
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function Tags({ tags }: { tags: string[] }) {
|
|
145
|
+
if (!tags.length) return null;
|
|
146
|
+
return (
|
|
147
|
+
<div className="rv-tags">
|
|
148
|
+
{tags.map((t) => (
|
|
149
|
+
<span key={t} className="rv-tag">
|
|
150
|
+
{t}
|
|
151
|
+
</span>
|
|
152
|
+
))}
|
|
153
|
+
</div>
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function Entry({
|
|
158
|
+
store,
|
|
159
|
+
me,
|
|
160
|
+
comment,
|
|
161
|
+
tags = [],
|
|
162
|
+
}: {
|
|
163
|
+
store: ReviewStore;
|
|
164
|
+
me: Me | null;
|
|
165
|
+
comment: Comment;
|
|
166
|
+
tags?: string[];
|
|
167
|
+
}) {
|
|
168
|
+
const [editing, setEditing] = useState(false);
|
|
169
|
+
return (
|
|
170
|
+
<article className="rv-msg" data-editing={editing ? '' : undefined}>
|
|
171
|
+
<Avatar name={comment.author.name} />
|
|
172
|
+
<div className="rv-msg-main">
|
|
173
|
+
<EntryHead
|
|
174
|
+
comment={comment}
|
|
175
|
+
canEdit={me?.id === comment.author.id && !editing}
|
|
176
|
+
onEdit={() => setEditing(true)}
|
|
177
|
+
/>
|
|
178
|
+
{editing ? (
|
|
179
|
+
<EditBox
|
|
180
|
+
initial={comment.body}
|
|
181
|
+
onSave={async (body) => {
|
|
182
|
+
await store.edit(comment.id, body);
|
|
183
|
+
setEditing(false);
|
|
184
|
+
}}
|
|
185
|
+
onCancel={() => setEditing(false)}
|
|
186
|
+
/>
|
|
187
|
+
) : (
|
|
188
|
+
<>
|
|
189
|
+
<p className="rv-text">{comment.body}</p>
|
|
190
|
+
<Tags tags={tags} />
|
|
191
|
+
</>
|
|
192
|
+
)}
|
|
193
|
+
</div>
|
|
194
|
+
</article>
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** The kind and state a thread carries, as words. */
|
|
199
|
+
function tagsOf(root: Comment): string[] {
|
|
200
|
+
const { kind = null, state = null } = root.triage ?? {};
|
|
201
|
+
return [kind && KIND_LABEL[kind], state].filter((t): t is string => !!t);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
type TriageChange = { kind: string | null; state: string | null };
|
|
205
|
+
|
|
206
|
+
/** Kind and state as checked choices, for editors; then the thread's rarer actions. */
|
|
207
|
+
function menuItems(
|
|
208
|
+
root: Comment,
|
|
209
|
+
states: string[],
|
|
210
|
+
editor: boolean,
|
|
211
|
+
canClose: boolean,
|
|
212
|
+
show: boolean,
|
|
213
|
+
): MenuEntry[] {
|
|
214
|
+
const { kind = null, state = null } = root.triage ?? {};
|
|
215
|
+
const stateItems: MenuEntry[] = states.length
|
|
216
|
+
? [{ heading: 'State' }, ...states.map((s) => ({ label: s, value: `state:${s}`, checked: state === s }))]
|
|
217
|
+
: [];
|
|
218
|
+
const triage: MenuEntry[] = editor
|
|
219
|
+
? [
|
|
220
|
+
{ heading: 'Kind' },
|
|
221
|
+
...KINDS.map((k) => ({ label: KIND_LABEL[k], value: `kind:${k}`, checked: kind === k })),
|
|
222
|
+
...stateItems,
|
|
223
|
+
'-',
|
|
224
|
+
]
|
|
225
|
+
: [];
|
|
226
|
+
return [
|
|
227
|
+
...triage,
|
|
228
|
+
...(show ? [{ label: 'Show its state', value: 'show', icon: 'clock-rewind' as const }] : []),
|
|
229
|
+
...(canClose
|
|
230
|
+
? [{ label: 'Delete thread', value: 'delete', icon: 'trash-01' as const, danger: true }]
|
|
231
|
+
: []),
|
|
232
|
+
];
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Picking the checked choice again clears it. */
|
|
236
|
+
function triageAfter(root: Comment, value: string): TriageChange {
|
|
237
|
+
const { kind = null, state = null } = root.triage ?? {};
|
|
238
|
+
const at = value.indexOf(':');
|
|
239
|
+
const field = value.slice(0, at);
|
|
240
|
+
const choice = value.slice(at + 1);
|
|
241
|
+
if (field === 'kind') return { kind: kind === choice ? null : choice, state };
|
|
242
|
+
return { kind, state: state === choice ? null : choice };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function ThreadMenu({
|
|
246
|
+
store,
|
|
247
|
+
root,
|
|
248
|
+
states,
|
|
249
|
+
editor,
|
|
250
|
+
canClose,
|
|
251
|
+
onShow,
|
|
252
|
+
onDelete,
|
|
253
|
+
}: {
|
|
254
|
+
store: ReviewStore;
|
|
255
|
+
root: Comment;
|
|
256
|
+
states: string[];
|
|
257
|
+
editor: boolean;
|
|
258
|
+
canClose: boolean;
|
|
259
|
+
onShow?: () => void;
|
|
260
|
+
onDelete: () => void;
|
|
261
|
+
}) {
|
|
262
|
+
const items = menuItems(root, states, editor, canClose, !!onShow);
|
|
263
|
+
const menu = useMenu({
|
|
264
|
+
className: 'rv-menu',
|
|
265
|
+
label: 'Thread options',
|
|
266
|
+
width: 220,
|
|
267
|
+
items,
|
|
268
|
+
onSelect: (v) => {
|
|
269
|
+
if (v === 'show') onShow?.();
|
|
270
|
+
else if (v === 'delete') onDelete();
|
|
271
|
+
else if (v) act(store.triage(root.id, triageAfter(root, v), states));
|
|
272
|
+
},
|
|
273
|
+
});
|
|
274
|
+
if (!items.length) return null;
|
|
275
|
+
return (
|
|
276
|
+
<>
|
|
277
|
+
<Button
|
|
278
|
+
kind="tertiary"
|
|
279
|
+
size={null}
|
|
280
|
+
icon="dots-horizontal"
|
|
281
|
+
iconSize={16}
|
|
282
|
+
aria-label="Thread options"
|
|
283
|
+
{...menu.triggerProps}
|
|
284
|
+
/>
|
|
285
|
+
{menu.menu}
|
|
286
|
+
</>
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** The thread's one decision, in its head: Resolve while open, Reopen once resolved. */
|
|
291
|
+
function ResolveButton({ store, root }: { store: ReviewStore; root: Comment }) {
|
|
292
|
+
const open = root.status === 'open';
|
|
293
|
+
return (
|
|
294
|
+
<Button
|
|
295
|
+
kind="tertiary"
|
|
296
|
+
size={null}
|
|
297
|
+
className={open ? 'rv-resolve' : 'rv-resolve is-done'}
|
|
298
|
+
icon={open ? 'check' : 'refresh-cw-01'}
|
|
299
|
+
iconSize={open ? 16 : 14}
|
|
300
|
+
label={open ? 'Resolve' : 'Reopen'}
|
|
301
|
+
onClick={() => act(store.resolve(root.id, open))}
|
|
302
|
+
/>
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function Stepper({ index, total, onStep }: { index: number; total: number; onStep: (by: number) => void }) {
|
|
307
|
+
return (
|
|
308
|
+
// The bar steps through threads; on a phone it steps aside for the sheet, so the sheet carries the arrows.
|
|
309
|
+
<span className="rv-card-step">
|
|
310
|
+
<Button
|
|
311
|
+
kind="tertiary"
|
|
312
|
+
size={null}
|
|
313
|
+
icon="chevron-left"
|
|
314
|
+
iconSize={16}
|
|
315
|
+
aria-label="Previous comment"
|
|
316
|
+
disabled={total < 2}
|
|
317
|
+
onClick={() => onStep(-1)}
|
|
318
|
+
/>
|
|
319
|
+
<span className="rv-muted rv-step tabular">
|
|
320
|
+
{index + 1}/{total}
|
|
321
|
+
</span>
|
|
322
|
+
<Button
|
|
323
|
+
kind="tertiary"
|
|
324
|
+
size={null}
|
|
325
|
+
icon="chevron-right"
|
|
326
|
+
iconSize={16}
|
|
327
|
+
aria-label="Next comment"
|
|
328
|
+
disabled={total < 2}
|
|
329
|
+
onClick={() => onStep(1)}
|
|
330
|
+
/>
|
|
331
|
+
</span>
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function Foot({
|
|
336
|
+
store,
|
|
337
|
+
root,
|
|
338
|
+
confirming,
|
|
339
|
+
onKeep,
|
|
340
|
+
}: {
|
|
341
|
+
store: ReviewStore;
|
|
342
|
+
root: Comment;
|
|
343
|
+
confirming: boolean;
|
|
344
|
+
onKeep: () => void;
|
|
345
|
+
}) {
|
|
346
|
+
if (confirming)
|
|
347
|
+
return (
|
|
348
|
+
<div className="rv-card-foot rv-confirm" role="alert">
|
|
349
|
+
<span>Delete this thread and its replies?</span>
|
|
350
|
+
<Action label="Cancel" onClick={onKeep} />
|
|
351
|
+
<Button kind="primary-error" size={null} label="Delete" onClick={() => act(store.remove(root.id))} />
|
|
352
|
+
</div>
|
|
353
|
+
);
|
|
354
|
+
if (root.status === 'resolved') return null;
|
|
355
|
+
return (
|
|
356
|
+
<div className="rv-card-foot">
|
|
357
|
+
<Field
|
|
358
|
+
label={`Reply to ${root.author.name}`}
|
|
359
|
+
placeholder="Reply"
|
|
360
|
+
send="Reply"
|
|
361
|
+
autoFocus
|
|
362
|
+
onSend={(body) => store.reply(root.id, body)}
|
|
363
|
+
/>
|
|
364
|
+
</div>
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function ResolvedBand({ root }: { root: Comment }) {
|
|
369
|
+
if (root.status !== 'resolved') return null;
|
|
370
|
+
return (
|
|
371
|
+
<p className="rv-band">
|
|
372
|
+
<Icon name="check-circle" size={14} />
|
|
373
|
+
Resolved{root.resolvedBy ? ` by ${root.resolvedBy.name}` : ''}
|
|
374
|
+
</p>
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
type HeadProps = {
|
|
379
|
+
store: ReviewStore;
|
|
380
|
+
root: Comment;
|
|
381
|
+
n: number;
|
|
382
|
+
index: number;
|
|
383
|
+
total: number;
|
|
384
|
+
states: string[];
|
|
385
|
+
editor: boolean;
|
|
386
|
+
canClose: boolean;
|
|
387
|
+
onStep: (by: number) => void;
|
|
388
|
+
onClose: () => void;
|
|
389
|
+
onShow?: () => void;
|
|
390
|
+
onDelete: () => void;
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
/** What the comment is on, then its one decision and the rest. */
|
|
394
|
+
function ThreadHead({
|
|
395
|
+
store,
|
|
396
|
+
root,
|
|
397
|
+
n,
|
|
398
|
+
index,
|
|
399
|
+
total,
|
|
400
|
+
states,
|
|
401
|
+
editor,
|
|
402
|
+
canClose,
|
|
403
|
+
onStep,
|
|
404
|
+
onClose,
|
|
405
|
+
onShow,
|
|
406
|
+
onDelete,
|
|
407
|
+
}: HeadProps) {
|
|
408
|
+
return (
|
|
409
|
+
<header className="rv-card-head">
|
|
410
|
+
<span className="rv-no" aria-hidden="true">
|
|
411
|
+
{root.status === 'resolved' ? <Icon name="check" size={12} /> : n}
|
|
412
|
+
</span>
|
|
413
|
+
<span className="rv-about rv-ellipsis">{about(root)}</span>
|
|
414
|
+
<Stepper index={index} total={total} onStep={onStep} />
|
|
415
|
+
{canClose && <ResolveButton store={store} root={root} />}
|
|
416
|
+
<ThreadMenu
|
|
417
|
+
store={store}
|
|
418
|
+
root={root}
|
|
419
|
+
states={states}
|
|
420
|
+
editor={editor}
|
|
421
|
+
canClose={canClose}
|
|
422
|
+
onShow={onShow}
|
|
423
|
+
onDelete={onDelete}
|
|
424
|
+
/>
|
|
425
|
+
<Button
|
|
426
|
+
kind="tertiary"
|
|
427
|
+
size={null}
|
|
428
|
+
icon="x-close"
|
|
429
|
+
iconSize={16}
|
|
430
|
+
aria-label="Close comment"
|
|
431
|
+
onClick={onClose}
|
|
432
|
+
/>
|
|
433
|
+
</header>
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
export function ThreadCard({
|
|
438
|
+
store,
|
|
439
|
+
me,
|
|
440
|
+
root,
|
|
441
|
+
replies,
|
|
442
|
+
n,
|
|
443
|
+
index,
|
|
444
|
+
total,
|
|
445
|
+
states,
|
|
446
|
+
at,
|
|
447
|
+
onStep,
|
|
448
|
+
onClose,
|
|
449
|
+
onShow,
|
|
450
|
+
}: {
|
|
451
|
+
store: ReviewStore;
|
|
452
|
+
me: Me | null;
|
|
453
|
+
root: Comment;
|
|
454
|
+
replies: Comment[];
|
|
455
|
+
n: number;
|
|
456
|
+
index: number;
|
|
457
|
+
total: number;
|
|
458
|
+
states: string[];
|
|
459
|
+
at: { x: number; y: number } | null;
|
|
460
|
+
onStep: (by: number) => void;
|
|
461
|
+
onClose: () => void;
|
|
462
|
+
onShow?: () => void;
|
|
463
|
+
}) {
|
|
464
|
+
const [confirming, setConfirming] = useState(false);
|
|
465
|
+
const editor = me?.role === 'editor';
|
|
466
|
+
const resolved = root.status === 'resolved';
|
|
467
|
+
return (
|
|
468
|
+
<Card
|
|
469
|
+
at={at}
|
|
470
|
+
label={`Comment ${n}${resolved ? ', resolved' : ''}`}
|
|
471
|
+
resolved={resolved}
|
|
472
|
+
onDismiss={onClose}
|
|
473
|
+
>
|
|
474
|
+
<ThreadHead
|
|
475
|
+
store={store}
|
|
476
|
+
root={root}
|
|
477
|
+
n={n}
|
|
478
|
+
index={index}
|
|
479
|
+
total={total}
|
|
480
|
+
states={states}
|
|
481
|
+
editor={editor}
|
|
482
|
+
canClose={editor || me?.id === root.author.id}
|
|
483
|
+
onStep={onStep}
|
|
484
|
+
onClose={onClose}
|
|
485
|
+
onShow={onShow}
|
|
486
|
+
onDelete={() => setConfirming(true)}
|
|
487
|
+
/>
|
|
488
|
+
<ResolvedBand root={root} />
|
|
489
|
+
<div className="rv-card-body">
|
|
490
|
+
<Entry store={store} me={me} comment={root} tags={tagsOf(root)} />
|
|
491
|
+
{replies.map((r) => (
|
|
492
|
+
<Entry key={r.id} store={store} me={me} comment={r} />
|
|
493
|
+
))}
|
|
494
|
+
</div>
|
|
495
|
+
<Foot store={store} root={root} confirming={confirming} onKeep={() => setConfirming(false)} />
|
|
496
|
+
</Card>
|
|
497
|
+
);
|
|
498
|
+
}
|
package/shell/review/anchor.ts
CHANGED
|
@@ -10,8 +10,9 @@ const RESTORE_WAIT_MS = 3000;
|
|
|
10
10
|
// React's useId values (:r1:, «r1») change between renders of different trees; never anchor on them.
|
|
11
11
|
const STABLE_ID = /^[A-Za-z][\w-]*$/;
|
|
12
12
|
|
|
13
|
+
/** The hub's own parts and the popups they portal to <body>: never what a comment is about. */
|
|
13
14
|
export const isHarness = (el: Element | null) =>
|
|
14
|
-
!!el?.closest('[data-harness], .dialkit-root, .proto-picker');
|
|
15
|
+
!!el?.closest('[data-harness], .dialkit-root, .proto-picker, [data-lapse-panel], .rv-menu');
|
|
15
16
|
|
|
16
17
|
/** The element's own id when it is stable and unique on the page, so a path can stop there. */
|
|
17
18
|
function stableId(node: Element): string | null {
|
package/shell/review/client.ts
CHANGED
|
@@ -29,6 +29,9 @@ class ApiError extends Error {
|
|
|
29
29
|
|
|
30
30
|
const API = '/api/review';
|
|
31
31
|
|
|
32
|
+
/** For a click handler: the store shows every failure in the panel, so the handler has nothing left to do with one. */
|
|
33
|
+
export const act = (pending: Promise<unknown>) => void pending.catch(() => {});
|
|
34
|
+
|
|
32
35
|
async function call<T>(
|
|
33
36
|
op: string,
|
|
34
37
|
body?: Record<string, unknown>,
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { test } from 'node:test';
|
|
3
|
+
import { besideButton, besidePoint, distanceToRect, dragOffset, nearest, shouldDismiss } from './place.ts';
|
|
4
|
+
|
|
5
|
+
const vp = { width: 1280, height: 800 };
|
|
6
|
+
const button = (left: number, top: number) => ({ left, top, right: left + 40, bottom: top + 40 });
|
|
7
|
+
|
|
8
|
+
test('beside a pin: to its right when there is room, level with it', () => {
|
|
9
|
+
assert.deepEqual(besidePoint({ x: 400, y: 300 }, 200, vp), { left: 420, top: 272, origin: 'left top' });
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test('beside a pin near the right edge: flipped to its left', () => {
|
|
13
|
+
const p = besidePoint({ x: 1100, y: 300 }, 200, vp);
|
|
14
|
+
assert.equal(p.left, 1100 - 20 - 320);
|
|
15
|
+
assert.equal(p.origin, 'right top');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test('beside a pin near the bottom: lifted to stay on screen', () => {
|
|
19
|
+
assert.equal(besidePoint({ x: 400, y: 780 }, 300, vp).top, 800 - 300 - 16);
|
|
20
|
+
assert.equal(besidePoint({ x: 400, y: 0 }, 300, vp).top, 16);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test('beside the button at the bottom right: above it, right edges lined up', () => {
|
|
24
|
+
assert.deepEqual(besideButton(button(1224, 744), 200, vp), {
|
|
25
|
+
left: 1264 - 320,
|
|
26
|
+
top: 744 - 12 - 200,
|
|
27
|
+
origin: 'bottom right',
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('beside the button at the top left: below it, left edges lined up', () => {
|
|
32
|
+
assert.deepEqual(besideButton(button(16, 16), 200, vp), { left: 16, top: 68, origin: 'top left' });
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test('beside the button mid-edge: to its inner side, centred on it', () => {
|
|
36
|
+
assert.deepEqual(besideButton(button(1224, 380), 200, vp), {
|
|
37
|
+
left: 1224 - 12 - 320,
|
|
38
|
+
top: 300,
|
|
39
|
+
origin: 'right center',
|
|
40
|
+
});
|
|
41
|
+
assert.equal(besideButton(button(16, 380), 200, vp).left, 56 + 12);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('a dragged sheet follows the finger down and resists going up', () => {
|
|
45
|
+
assert.equal(dragOffset(120), 120);
|
|
46
|
+
assert.equal(dragOffset(0), 0);
|
|
47
|
+
assert.equal(dragOffset(-100), -20);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('a sheet leaves when pulled far enough or flicked, and settles back otherwise', () => {
|
|
51
|
+
assert.equal(shouldDismiss(140, 0.1), true);
|
|
52
|
+
assert.equal(shouldDismiss(40, 0.9), true);
|
|
53
|
+
assert.equal(shouldDismiss(40, 0.2), false);
|
|
54
|
+
assert.equal(shouldDismiss(8, 2), false);
|
|
55
|
+
assert.equal(shouldDismiss(-60, 1), false);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('a point inside a box is no distance from it; outside, the gap to its nearest edge', () => {
|
|
59
|
+
const r = { left: 100, top: 100, right: 124, bottom: 124 };
|
|
60
|
+
assert.equal(distanceToRect(110, 110, r), 0);
|
|
61
|
+
assert.equal(distanceToRect(134, 110, r), 10);
|
|
62
|
+
assert.equal(distanceToRect(127, 128, r), 5);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('a click near a pin lands on the closest pin within reach, or none', () => {
|
|
66
|
+
const a = { id: 'a', left: 100, top: 100, right: 124, bottom: 124 };
|
|
67
|
+
const b = { id: 'b', left: 150, top: 100, right: 174, bottom: 124 };
|
|
68
|
+
assert.equal(nearest(135, 110, [a, b], 16)?.id, 'a');
|
|
69
|
+
assert.equal(nearest(142, 110, [a, b], 16)?.id, 'b');
|
|
70
|
+
assert.equal(nearest(110, 160, [a, b], 16), null);
|
|
71
|
+
assert.equal(nearest(110, 110, [], 16), null);
|
|
72
|
+
});
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// Where the one card sits: beside its pin, or beside the Comments button when
|
|
2
|
+
// it has no pin. Pure, so node:test runs it; Card.tsx measures and applies it.
|
|
3
|
+
|
|
4
|
+
const CARD_WIDTH = 320;
|
|
5
|
+
const EDGE = 16;
|
|
6
|
+
const AWAY = 12;
|
|
7
|
+
const PIN_GAP = 20;
|
|
8
|
+
|
|
9
|
+
type Point = { x: number; y: number };
|
|
10
|
+
type Rect = { left: number; top: number; right: number; bottom: number };
|
|
11
|
+
type Viewport = { width: number; height: number };
|
|
12
|
+
export type Placement = { left: number; top: number; origin: string };
|
|
13
|
+
|
|
14
|
+
const clampTop = (top: number, h: number, vp: Viewport) =>
|
|
15
|
+
Math.min(Math.max(EDGE, top), vp.height - h - EDGE);
|
|
16
|
+
|
|
17
|
+
/** Beside a pin: to its right when that fits, else to its left; level with it, kept on screen. */
|
|
18
|
+
export function besidePoint(at: Point, h: number, vp: Viewport): Placement {
|
|
19
|
+
const right = at.x + PIN_GAP + CARD_WIDTH < vp.width - EDGE;
|
|
20
|
+
return {
|
|
21
|
+
left: right ? at.x + PIN_GAP : Math.max(EDGE, at.x - PIN_GAP - CARD_WIDTH),
|
|
22
|
+
top: clampTop(at.y - 28, h, vp),
|
|
23
|
+
origin: `${right ? 'left' : 'right'} top`,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Beside the button resting mid-edge: to its inner side, centred on it. */
|
|
28
|
+
function besideMiddle(b: Rect, onRight: boolean, h: number, vp: Viewport): Placement {
|
|
29
|
+
return {
|
|
30
|
+
left: onRight ? b.left - AWAY - CARD_WIDTH : b.right + AWAY,
|
|
31
|
+
top: clampTop((b.top + b.bottom) / 2 - h / 2, h, vp),
|
|
32
|
+
origin: onRight ? 'right center' : 'left center',
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Beside the button at a corner: above it at the bottom, below it at the top, its outer edge lined up with the button's. */
|
|
37
|
+
function besideCorner(b: Rect, onRight: boolean, h: number, vp: Viewport): Placement {
|
|
38
|
+
const below = (b.top + b.bottom) / 2 < vp.height / 2;
|
|
39
|
+
return {
|
|
40
|
+
left: onRight ? Math.max(EDGE, b.right - CARD_WIDTH) : b.left,
|
|
41
|
+
top: clampTop(below ? b.bottom + AWAY : b.top - AWAY - h, h, vp),
|
|
42
|
+
origin: `${below ? 'top' : 'bottom'} ${onRight ? 'right' : 'left'}`,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function besideButton(b: Rect, h: number, vp: Viewport): Placement {
|
|
47
|
+
const onRight = (b.left + b.right) / 2 > vp.width / 2;
|
|
48
|
+
const cy = (b.top + b.bottom) / 2;
|
|
49
|
+
const middle = cy > vp.height * 0.25 && cy < vp.height * 0.75;
|
|
50
|
+
return middle ? besideMiddle(b, onRight, h, vp) : besideCorner(b, onRight, h, vp);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** How far a dragged sheet moves for a finger `dy` from where it started: freely down, and only a little up, against resistance. */
|
|
54
|
+
export const dragOffset = (dy: number) => (dy >= 0 ? dy : -Math.sqrt(-dy) * 2);
|
|
55
|
+
|
|
56
|
+
/** A sheet let go this far down, or flicked down this fast (px per ms), leaves; anything less settles back. */
|
|
57
|
+
export const shouldDismiss = (dy: number, velocity: number) => dy > 96 || (dy > 12 && velocity > 0.5);
|
|
58
|
+
|
|
59
|
+
/** How far a point lies outside a box; 0 inside it. */
|
|
60
|
+
export function distanceToRect(x: number, y: number, r: Rect): number {
|
|
61
|
+
const dx = Math.max(r.left - x, 0, x - r.right);
|
|
62
|
+
const dy = Math.max(r.top - y, 0, y - r.bottom);
|
|
63
|
+
return Math.hypot(dx, dy);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The box a click near enough to counts as on, of those within `reach`: the closest. */
|
|
67
|
+
export function nearest<T extends Rect>(x: number, y: number, boxes: T[], reach: number): T | null {
|
|
68
|
+
let best: T | null = null;
|
|
69
|
+
let bestDistance = reach;
|
|
70
|
+
for (const box of boxes) {
|
|
71
|
+
const d = distanceToRect(x, y, box);
|
|
72
|
+
if (d <= bestDistance) {
|
|
73
|
+
best = box;
|
|
74
|
+
bestDistance = d;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return best;
|
|
78
|
+
}
|