@milesroxas/design-hub 0.5.0 → 0.6.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/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,180 @@
|
|
|
1
|
+
// The way into the review layer: one round button that turns comment mode on
|
|
2
|
+
// and off. Drag it anywhere and it glides to the nearest of six spots on the
|
|
3
|
+
// screen's edge (snap.ts); Alt and an arrow move it by keyboard. Hidden, it
|
|
4
|
+
// tucks into its edge and leaves a tab that brings it back.
|
|
5
|
+
import { Icon } from '@kit/Icon';
|
|
6
|
+
import { type KeyboardEvent, type PointerEvent, useEffect, useLayoutEffect, useRef } from 'react';
|
|
7
|
+
import { nearestSpot, type Spot, stepSpot } from './snap';
|
|
8
|
+
|
|
9
|
+
const SIZE = 40;
|
|
10
|
+
const GAP = 16;
|
|
11
|
+
/** Pointer travel before a press becomes a drag, so a slightly moving click still clicks. */
|
|
12
|
+
const SLOP = 6;
|
|
13
|
+
/** A drag shorter than this that lands back on its own spot was a click with a wobble: it still toggles. */
|
|
14
|
+
const WOBBLE = 24;
|
|
15
|
+
|
|
16
|
+
type Sample = { x: number; y: number; t: number };
|
|
17
|
+
|
|
18
|
+
type Props = {
|
|
19
|
+
hidden: boolean;
|
|
20
|
+
commenting: boolean;
|
|
21
|
+
openCount: number;
|
|
22
|
+
spot: Spot;
|
|
23
|
+
onSpot: (spot: Spot) => void;
|
|
24
|
+
onToggle: () => void;
|
|
25
|
+
onShow: () => void;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** Plays the move from where the button was to where it now rests: the new spot is already laid out, the transform runs it back. */
|
|
29
|
+
function glide(el: HTMLElement, from: DOMRect) {
|
|
30
|
+
el.style.transition = 'none';
|
|
31
|
+
el.style.transform = '';
|
|
32
|
+
const to = el.getBoundingClientRect();
|
|
33
|
+
el.style.transform = `translate(${from.left - to.left}px, ${from.top - to.top}px)`;
|
|
34
|
+
el.getBoundingClientRect();
|
|
35
|
+
el.style.transition = '';
|
|
36
|
+
el.style.transform = '';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function velocity(samples: Sample[]) {
|
|
40
|
+
const a = samples[0];
|
|
41
|
+
const b = samples.at(-1) ?? a;
|
|
42
|
+
const dt = Math.max(1, b.t - a.t);
|
|
43
|
+
return { x: (b.x - a.x) / dt, y: (b.y - a.y) / dt };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function useDrag(spot: Spot, onSpot: (s: Spot) => void) {
|
|
47
|
+
const ref = useRef<HTMLButtonElement>(null);
|
|
48
|
+
const press = useRef<{ id: number; x: number; y: number; moved: boolean; samples: Sample[] } | null>(null);
|
|
49
|
+
const dragged = useRef(false);
|
|
50
|
+
const from = useRef<DOMRect | null>(null);
|
|
51
|
+
|
|
52
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: runs when the spot changes, to play the move into it
|
|
53
|
+
useLayoutEffect(() => {
|
|
54
|
+
if (ref.current && from.current) glide(ref.current, from.current);
|
|
55
|
+
from.current = null;
|
|
56
|
+
}, [spot]);
|
|
57
|
+
|
|
58
|
+
const settle = (next: Spot) => {
|
|
59
|
+
const el = ref.current;
|
|
60
|
+
if (!el) return;
|
|
61
|
+
if (next === spot) glide(el, el.getBoundingClientRect());
|
|
62
|
+
else {
|
|
63
|
+
from.current = el.getBoundingClientRect();
|
|
64
|
+
onSpot(next);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const handlers = {
|
|
69
|
+
onPointerDown(e: PointerEvent<HTMLButtonElement>) {
|
|
70
|
+
if (e.button !== 0 || !e.isPrimary) return;
|
|
71
|
+
dragged.current = false;
|
|
72
|
+
press.current = { id: e.pointerId, x: e.clientX, y: e.clientY, moved: false, samples: [] };
|
|
73
|
+
// Captured from the press, so a quick first move that leaves the 40px button is still this drag.
|
|
74
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
75
|
+
},
|
|
76
|
+
onPointerMove(e: PointerEvent<HTMLButtonElement>) {
|
|
77
|
+
const p = press.current;
|
|
78
|
+
const el = ref.current;
|
|
79
|
+
if (!p || !el || e.pointerId !== p.id) return;
|
|
80
|
+
const dx = e.clientX - p.x;
|
|
81
|
+
const dy = e.clientY - p.y;
|
|
82
|
+
if (!p.moved) {
|
|
83
|
+
if (Math.hypot(dx, dy) < SLOP) return;
|
|
84
|
+
p.moved = true;
|
|
85
|
+
el.dataset.dragging = '';
|
|
86
|
+
}
|
|
87
|
+
el.style.transform = `translate(${dx}px, ${dy}px)`;
|
|
88
|
+
p.samples = [...p.samples, { x: e.clientX, y: e.clientY, t: e.timeStamp }].filter(
|
|
89
|
+
(s) => e.timeStamp - s.t < 100,
|
|
90
|
+
);
|
|
91
|
+
},
|
|
92
|
+
onPointerUp(e: PointerEvent<HTMLButtonElement>) {
|
|
93
|
+
const p = press.current;
|
|
94
|
+
const el = ref.current;
|
|
95
|
+
press.current = null;
|
|
96
|
+
if (!p?.moved || !el) return;
|
|
97
|
+
delete el.dataset.dragging;
|
|
98
|
+
const r = el.getBoundingClientRect();
|
|
99
|
+
const centre = { x: r.left + r.width / 2, y: r.top + r.height / 2 };
|
|
100
|
+
const next = nearestSpot(
|
|
101
|
+
centre,
|
|
102
|
+
velocity(p.samples),
|
|
103
|
+
{ width: innerWidth, height: innerHeight },
|
|
104
|
+
SIZE,
|
|
105
|
+
GAP,
|
|
106
|
+
);
|
|
107
|
+
dragged.current = next !== spot || Math.hypot(e.clientX - p.x, e.clientY - p.y) >= WOBBLE;
|
|
108
|
+
settle(next);
|
|
109
|
+
},
|
|
110
|
+
onPointerCancel() {
|
|
111
|
+
press.current = null;
|
|
112
|
+
if (!ref.current) return;
|
|
113
|
+
delete ref.current.dataset.dragging;
|
|
114
|
+
glide(ref.current, ref.current.getBoundingClientRect());
|
|
115
|
+
},
|
|
116
|
+
onKeyDown(e: KeyboardEvent<HTMLButtonElement>) {
|
|
117
|
+
if (!e.altKey || !e.key.startsWith('Arrow')) return;
|
|
118
|
+
e.preventDefault();
|
|
119
|
+
settle(stepSpot(spot, e.key));
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
// A drag ends in a click on the same button; it moved the button, so it must not also toggle comment mode.
|
|
123
|
+
const swallowClick = () => {
|
|
124
|
+
const was = dragged.current;
|
|
125
|
+
dragged.current = false;
|
|
126
|
+
return was;
|
|
127
|
+
};
|
|
128
|
+
return { ref, handlers, swallowClick };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function Launcher({ hidden, commenting, openCount, spot, onSpot, onToggle, onShow }: Props) {
|
|
132
|
+
const { ref, handlers, swallowClick } = useDrag(spot, onSpot);
|
|
133
|
+
const tab = useRef<HTMLButtonElement>(null);
|
|
134
|
+
const was = useRef(hidden);
|
|
135
|
+
// Hiding and showing hand focus across, so a keyboard never loses its place.
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
if (was.current !== hidden) (hidden ? tab : ref).current?.focus();
|
|
138
|
+
was.current = hidden;
|
|
139
|
+
}, [hidden, ref]);
|
|
140
|
+
|
|
141
|
+
const label = commenting ? 'Stop commenting' : 'Comment on this page';
|
|
142
|
+
return (
|
|
143
|
+
<>
|
|
144
|
+
<button
|
|
145
|
+
ref={ref}
|
|
146
|
+
type="button"
|
|
147
|
+
className="rv-launch"
|
|
148
|
+
data-spot={spot}
|
|
149
|
+
data-away={hidden ? '' : undefined}
|
|
150
|
+
inert={hidden}
|
|
151
|
+
aria-label={openCount > 0 ? `${label}, ${openCount} open` : label}
|
|
152
|
+
aria-pressed={commenting}
|
|
153
|
+
aria-keyshortcuts="Alt+ArrowLeft Alt+ArrowRight Alt+ArrowUp Alt+ArrowDown"
|
|
154
|
+
title={`${label}. Drag to move.`}
|
|
155
|
+
onClick={() => swallowClick() || onToggle()}
|
|
156
|
+
{...handlers}
|
|
157
|
+
>
|
|
158
|
+
<Icon name="message-text-square-01" size={18} />
|
|
159
|
+
{openCount > 0 && (
|
|
160
|
+
<span className="rv-count" aria-hidden="true">
|
|
161
|
+
{openCount > 99 ? '99+' : openCount}
|
|
162
|
+
</span>
|
|
163
|
+
)}
|
|
164
|
+
</button>
|
|
165
|
+
<button
|
|
166
|
+
ref={tab}
|
|
167
|
+
type="button"
|
|
168
|
+
className="rv-tab"
|
|
169
|
+
data-spot={spot}
|
|
170
|
+
data-away={hidden ? undefined : ''}
|
|
171
|
+
inert={!hidden}
|
|
172
|
+
aria-label="Show comments"
|
|
173
|
+
title="Show comments"
|
|
174
|
+
onClick={onShow}
|
|
175
|
+
>
|
|
176
|
+
<Icon name="message-text-square-01" size={16} />
|
|
177
|
+
</button>
|
|
178
|
+
</>
|
|
179
|
+
);
|
|
180
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
// The People card, for editors: invite someone, hand over their link, and see
|
|
2
|
+
// who can comment.
|
|
3
|
+
import { Button, Select } from '@kit/ui';
|
|
4
|
+
import { type FormEvent, useId, useRef, useState } from 'react';
|
|
5
|
+
import { INVITE_DAYS, LIMITS, type Role } from '../../core/review/model';
|
|
6
|
+
import { Action } from './Card';
|
|
7
|
+
import { act, type Me, type ReviewStore } from './client';
|
|
8
|
+
|
|
9
|
+
type Link = { name: string; href: string };
|
|
10
|
+
|
|
11
|
+
const ROLES: { value: Role; label: string }[] = [
|
|
12
|
+
{ value: 'reviewer', label: 'Reviewer: comments and replies' },
|
|
13
|
+
{ value: 'editor', label: 'Editor: also resolves, sorts and invites' },
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
function InviteForm({ store, onLink }: { store: ReviewStore; onLink: (link: Link) => void }) {
|
|
17
|
+
const [role, setRole] = useState<Role>('reviewer');
|
|
18
|
+
const [busy, setBusy] = useState(false);
|
|
19
|
+
const roleLabel = useId();
|
|
20
|
+
const invite = (e: FormEvent<HTMLFormElement>) => {
|
|
21
|
+
e.preventDefault();
|
|
22
|
+
const form = e.currentTarget;
|
|
23
|
+
const f = new FormData(form);
|
|
24
|
+
const name = String(f.get('name') ?? '');
|
|
25
|
+
setBusy(true);
|
|
26
|
+
act(
|
|
27
|
+
store
|
|
28
|
+
.invite(name, String(f.get('team') ?? ''), role)
|
|
29
|
+
.then((href) => {
|
|
30
|
+
onLink({ name, href });
|
|
31
|
+
form.reset();
|
|
32
|
+
})
|
|
33
|
+
.finally(() => setBusy(false)),
|
|
34
|
+
);
|
|
35
|
+
};
|
|
36
|
+
return (
|
|
37
|
+
<form className="rv-invite" onSubmit={invite}>
|
|
38
|
+
<label className="field">
|
|
39
|
+
<span className="field-label">Name</span>
|
|
40
|
+
<input
|
|
41
|
+
className="input"
|
|
42
|
+
name="name"
|
|
43
|
+
required
|
|
44
|
+
maxLength={LIMITS.name}
|
|
45
|
+
autoComplete="off"
|
|
46
|
+
data-autofocus=""
|
|
47
|
+
/>
|
|
48
|
+
</label>
|
|
49
|
+
<label className="field">
|
|
50
|
+
<span className="field-label">Team</span>
|
|
51
|
+
<input
|
|
52
|
+
className="input"
|
|
53
|
+
name="team"
|
|
54
|
+
required
|
|
55
|
+
maxLength={LIMITS.team}
|
|
56
|
+
autoComplete="off"
|
|
57
|
+
placeholder="Studio, or the client’s company"
|
|
58
|
+
/>
|
|
59
|
+
</label>
|
|
60
|
+
<div className="field">
|
|
61
|
+
<span id={roleLabel} className="field-label">
|
|
62
|
+
Role
|
|
63
|
+
</span>
|
|
64
|
+
<Select
|
|
65
|
+
labelId={roleLabel}
|
|
66
|
+
menuClassName="rv-menu"
|
|
67
|
+
value={role}
|
|
68
|
+
options={ROLES}
|
|
69
|
+
onChange={(next) => setRole(next as Role)}
|
|
70
|
+
/>
|
|
71
|
+
</div>
|
|
72
|
+
<Button type="submit" kind="primary" size={null} label="Create invite link" busy={busy} />
|
|
73
|
+
</form>
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function ShareLink({ link }: { link: Link }) {
|
|
78
|
+
const [copied, setCopied] = useState(false);
|
|
79
|
+
const field = useRef<HTMLInputElement>(null);
|
|
80
|
+
const copy = () =>
|
|
81
|
+
navigator.clipboard.writeText(link.href).then(
|
|
82
|
+
() => setCopied(true),
|
|
83
|
+
// No clipboard access (an insecure origin, a denied permission): select the link to copy by hand.
|
|
84
|
+
() => field.current?.select(),
|
|
85
|
+
);
|
|
86
|
+
return (
|
|
87
|
+
<div className="rv-share" role="status">
|
|
88
|
+
<p>
|
|
89
|
+
Send this link to {link.name}. It works for {INVITE_DAYS} days, and a new link replaces it.
|
|
90
|
+
</p>
|
|
91
|
+
<div className="rv-share-row">
|
|
92
|
+
<input
|
|
93
|
+
ref={field}
|
|
94
|
+
className="input"
|
|
95
|
+
readOnly
|
|
96
|
+
value={link.href}
|
|
97
|
+
aria-label={`Invite link for ${link.name}`}
|
|
98
|
+
onFocus={(e) => e.currentTarget.select()}
|
|
99
|
+
/>
|
|
100
|
+
<Button size={null} className="rv-copy" label={copied ? 'Copied' : 'Copy'} onClick={copy} />
|
|
101
|
+
</div>
|
|
102
|
+
</div>
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function PersonRow({
|
|
107
|
+
person,
|
|
108
|
+
store,
|
|
109
|
+
onLink,
|
|
110
|
+
}: {
|
|
111
|
+
person: Me;
|
|
112
|
+
store: ReviewStore;
|
|
113
|
+
onLink: (link: Link) => void;
|
|
114
|
+
}) {
|
|
115
|
+
const [confirming, setConfirming] = useState(false);
|
|
116
|
+
return (
|
|
117
|
+
<div className="rv-row rv-acts">
|
|
118
|
+
{confirming ? (
|
|
119
|
+
<>
|
|
120
|
+
<span className="rv-confirm">Remove {person.name}’s access?</span>
|
|
121
|
+
<Action
|
|
122
|
+
label="Revoke"
|
|
123
|
+
danger
|
|
124
|
+
onClick={() => act(store.revoke(person.id).then(() => setConfirming(false)))}
|
|
125
|
+
/>
|
|
126
|
+
<Action label="Keep" onClick={() => setConfirming(false)} />
|
|
127
|
+
</>
|
|
128
|
+
) : (
|
|
129
|
+
<>
|
|
130
|
+
<Action
|
|
131
|
+
label="New link"
|
|
132
|
+
onClick={() => act(store.reissue(person.id).then((href) => onLink({ name: person.name, href })))}
|
|
133
|
+
/>
|
|
134
|
+
<Action label="Revoke" onClick={() => setConfirming(true)} />
|
|
135
|
+
</>
|
|
136
|
+
)}
|
|
137
|
+
</div>
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function People({ store, me, people }: { store: ReviewStore; me: Me | null; people: Me[] | null }) {
|
|
142
|
+
const [link, setLink] = useState<Link | null>(null);
|
|
143
|
+
return (
|
|
144
|
+
<div className="rv-body">
|
|
145
|
+
<InviteForm store={store} onLink={setLink} />
|
|
146
|
+
{/* Keyed by the link, so a second one starts on Copy again. */}
|
|
147
|
+
{link && <ShareLink key={link.href} link={link} />}
|
|
148
|
+
{people === null ? (
|
|
149
|
+
<p className="rv-muted">Loading people…</p>
|
|
150
|
+
) : (
|
|
151
|
+
<ul className="rv-people">
|
|
152
|
+
{people.map((p) => (
|
|
153
|
+
<li key={p.id} data-revoked={p.revokedAt ? '' : undefined}>
|
|
154
|
+
<div className="rv-person">
|
|
155
|
+
<strong>{p.name}</strong>
|
|
156
|
+
<span className="rv-muted">
|
|
157
|
+
{p.team} · {p.role}
|
|
158
|
+
{p.revokedAt && ' · revoked'}
|
|
159
|
+
</span>
|
|
160
|
+
</div>
|
|
161
|
+
{p.id !== me?.id && !p.revokedAt && <PersonRow person={p} store={store} onLink={setLink} />}
|
|
162
|
+
</li>
|
|
163
|
+
))}
|
|
164
|
+
</ul>
|
|
165
|
+
)}
|
|
166
|
+
</div>
|
|
167
|
+
);
|
|
168
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// Comment mode's hold on the page. No surface goes over it: a click or tap
|
|
2
|
+
// on the page is caught at the document before the page sees it, so the page
|
|
3
|
+
// and every panel in it still scroll natively, by wheel or by finger. A mouse
|
|
4
|
+
// gets an outline on the element under it; a keyboard comments on the focused
|
|
5
|
+
// element with Enter.
|
|
6
|
+
import { useEffect, useRef } from 'react';
|
|
7
|
+
import { isHarness } from './anchor';
|
|
8
|
+
import { nearest } from './place';
|
|
9
|
+
import type { Draft } from './view';
|
|
10
|
+
|
|
11
|
+
/** How close, in px, a click must land to a pin's edge to open it rather than start a new comment. */
|
|
12
|
+
const PIN_REACH = 16;
|
|
13
|
+
|
|
14
|
+
/** The pin a point lands on or near, by its thread id. */
|
|
15
|
+
function pinAt(x: number, y: number): HTMLElement | null {
|
|
16
|
+
const pins = [...document.querySelectorAll<HTMLElement>('.rv-pin[data-pin]:not([hidden])')].map((el) => {
|
|
17
|
+
const r = el.getBoundingClientRect();
|
|
18
|
+
return { el, left: r.left, top: r.top, right: r.right, bottom: r.bottom };
|
|
19
|
+
});
|
|
20
|
+
return nearest(x, y, pins, PIN_REACH)?.el ?? null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Marks the pin a mouse is near, so it lifts and the cursor says a click opens it. */
|
|
24
|
+
function markNear(pin: HTMLElement | null) {
|
|
25
|
+
for (const el of document.querySelectorAll('.rv-pin[data-near]'))
|
|
26
|
+
if (el !== pin) delete (el as HTMLElement).dataset.near;
|
|
27
|
+
if (pin) pin.dataset.near = '';
|
|
28
|
+
if (pin) document.documentElement.dataset.rvNear = '';
|
|
29
|
+
else delete document.documentElement.dataset.rvNear;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const isPage = (t: EventTarget | null): t is Element =>
|
|
33
|
+
t instanceof Element && !isHarness(t) && t !== document.body && t !== document.documentElement;
|
|
34
|
+
|
|
35
|
+
function outline(box: HTMLElement | null, el: Element | null) {
|
|
36
|
+
if (!box) return;
|
|
37
|
+
box.hidden = !el;
|
|
38
|
+
if (!el) return;
|
|
39
|
+
const r = el.getBoundingClientRect();
|
|
40
|
+
box.style.transform = `translate(${r.left}px, ${r.top}px)`;
|
|
41
|
+
box.style.width = `${r.width}px`;
|
|
42
|
+
box.style.height = `${r.height}px`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const fraction = (v: number, size: number) => (size ? Math.min(1, Math.max(0, v / size)) : 0.5);
|
|
46
|
+
|
|
47
|
+
/** Where on the element the click landed; the middle for a keyboard click, which has no point. */
|
|
48
|
+
function offsetOf(el: Element, e: MouseEvent) {
|
|
49
|
+
if (e.detail === 0) return { x: 0.5, y: 0.5 };
|
|
50
|
+
const r = el.getBoundingClientRect();
|
|
51
|
+
return { x: fraction(e.clientX - r.left, r.width), y: fraction(e.clientY - r.top, r.height) };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Where a draft's marker sits now, in viewport pixels, or null for the whole page. */
|
|
55
|
+
export function draftPoint(d: Draft): { x: number; y: number } | null {
|
|
56
|
+
if (!d.target || !d.offset || !d.target.isConnected) return null;
|
|
57
|
+
const r = d.target.getBoundingClientRect();
|
|
58
|
+
return { x: r.left + r.width * d.offset.x, y: r.top + r.height * d.offset.y };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** What a comment is about, in words: the element's label or its first words. */
|
|
62
|
+
export const describe = (el: Element | null) => {
|
|
63
|
+
if (!el) return 'the whole page';
|
|
64
|
+
// biome-ignore lint/performance/useDomNodeTextContent: innerText keeps the spaces between a row's parts; textContent runs them together
|
|
65
|
+
const text = (el.getAttribute('aria-label') || (el as HTMLElement).innerText || el.textContent || '')
|
|
66
|
+
.trim()
|
|
67
|
+
.replace(/\s+/g, ' ');
|
|
68
|
+
return text ? `“${text.length > 40 ? `${text.slice(0, 40)}…` : text}”` : `a ${el.localName} element`;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export function PickLayer({
|
|
72
|
+
onPick,
|
|
73
|
+
onOpen,
|
|
74
|
+
}: {
|
|
75
|
+
onPick: (draft: Draft) => void;
|
|
76
|
+
onOpen: (id: string) => void;
|
|
77
|
+
}) {
|
|
78
|
+
const box = useRef<HTMLDivElement>(null);
|
|
79
|
+
const pick = useRef(onPick);
|
|
80
|
+
pick.current = onPick;
|
|
81
|
+
const open = useRef(onOpen);
|
|
82
|
+
open.current = onOpen;
|
|
83
|
+
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
const root = document.documentElement;
|
|
86
|
+
root.dataset.rvCommenting = '';
|
|
87
|
+
// Pressing the page must not press its controls: a menu that opens on pointerdown stays shut.
|
|
88
|
+
const hold = (e: Event) => {
|
|
89
|
+
if (!isPage(e.target)) return;
|
|
90
|
+
e.stopPropagation();
|
|
91
|
+
if (e.type !== 'pointerdown') e.preventDefault();
|
|
92
|
+
};
|
|
93
|
+
const onClick = (e: MouseEvent) => {
|
|
94
|
+
const t = e.target;
|
|
95
|
+
if (!(t instanceof Element) || isHarness(t)) return;
|
|
96
|
+
// A click near a pin opens it, wherever the press began or ended: a near miss is not a new comment.
|
|
97
|
+
const pin = e.detail ? pinAt(e.clientX, e.clientY) : null;
|
|
98
|
+
if (!pin && !isPage(t)) return;
|
|
99
|
+
e.preventDefault();
|
|
100
|
+
e.stopPropagation();
|
|
101
|
+
outline(box.current, null);
|
|
102
|
+
if (pin?.dataset.pin) open.current(pin.dataset.pin);
|
|
103
|
+
else if (isPage(t)) pick.current({ target: t, offset: offsetOf(t, e) });
|
|
104
|
+
};
|
|
105
|
+
const onMove = (e: PointerEvent) => {
|
|
106
|
+
if (e.pointerType !== 'mouse') return;
|
|
107
|
+
const pin = pinAt(e.clientX, e.clientY);
|
|
108
|
+
markNear(pin);
|
|
109
|
+
outline(box.current, !pin && isPage(e.target) ? e.target : null);
|
|
110
|
+
};
|
|
111
|
+
const onScroll = () => outline(box.current, null);
|
|
112
|
+
const opts = { capture: true };
|
|
113
|
+
document.addEventListener('pointerdown', hold, opts);
|
|
114
|
+
document.addEventListener('mousedown', hold, opts);
|
|
115
|
+
document.addEventListener('click', onClick, opts);
|
|
116
|
+
document.addEventListener('pointermove', onMove, { capture: true, passive: true });
|
|
117
|
+
addEventListener('scroll', onScroll, { capture: true, passive: true });
|
|
118
|
+
return () => {
|
|
119
|
+
delete root.dataset.rvCommenting;
|
|
120
|
+
markNear(null);
|
|
121
|
+
document.removeEventListener('pointerdown', hold, opts);
|
|
122
|
+
document.removeEventListener('mousedown', hold, opts);
|
|
123
|
+
document.removeEventListener('click', onClick, opts);
|
|
124
|
+
document.removeEventListener('pointermove', onMove, opts);
|
|
125
|
+
removeEventListener('scroll', onScroll, opts);
|
|
126
|
+
};
|
|
127
|
+
}, []);
|
|
128
|
+
|
|
129
|
+
return <div ref={box} className="rv-pick-box" hidden />;
|
|
130
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// Pins on the elements comments are about, and the marker for one being
|
|
2
|
+
// written. In comment mode they are the way into a thread, so each is a real
|
|
3
|
+
// button in the tab order. The button is the hit area and never changes size;
|
|
4
|
+
// the face inside it is what presses and lifts, so a press near the edge
|
|
5
|
+
// still ends on the pin. An open thread's pin is solid and numbered; a
|
|
6
|
+
// resolved one is pale and carries a check.
|
|
7
|
+
import { Icon } from '@kit/Icon';
|
|
8
|
+
import { useEffect, useRef } from 'react';
|
|
9
|
+
import type { Comment } from '../../core/review/model';
|
|
10
|
+
import { pinPoint } from './anchor';
|
|
11
|
+
import { draftPoint } from './PickLayer';
|
|
12
|
+
import type { Draft } from './view';
|
|
13
|
+
|
|
14
|
+
const moveTo = (el: HTMLElement, at: { x: number; y: number } | null) => {
|
|
15
|
+
el.hidden = !at;
|
|
16
|
+
if (at) el.style.transform = `translate(${at.x}px, ${at.y}px)`;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export function Pins({
|
|
20
|
+
comments,
|
|
21
|
+
number,
|
|
22
|
+
active,
|
|
23
|
+
draft,
|
|
24
|
+
onOpen,
|
|
25
|
+
}: {
|
|
26
|
+
comments: Comment[];
|
|
27
|
+
number: Map<string, number>;
|
|
28
|
+
active: string | null;
|
|
29
|
+
draft: Draft | null;
|
|
30
|
+
onOpen: (id: string) => void;
|
|
31
|
+
}) {
|
|
32
|
+
const layer = useRef<HTMLDivElement>(null);
|
|
33
|
+
|
|
34
|
+
// Positions are written straight to the pins' transforms on scroll, resize
|
|
35
|
+
// and DOM changes, at most once a frame, so a scroll re-renders nothing.
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
let frame = 0;
|
|
38
|
+
const place = () => {
|
|
39
|
+
frame = 0;
|
|
40
|
+
for (const pin of layer.current?.querySelectorAll<HTMLElement>('[data-pin]') ?? []) {
|
|
41
|
+
const c = comments.find((x) => x.id === pin.dataset.pin);
|
|
42
|
+
moveTo(pin, c?.anchor ? pinPoint(c.anchor) : null);
|
|
43
|
+
}
|
|
44
|
+
const d = layer.current?.querySelector<HTMLElement>('[data-draft]');
|
|
45
|
+
if (d && draft) moveTo(d, draftPoint(draft));
|
|
46
|
+
};
|
|
47
|
+
const schedule = () => {
|
|
48
|
+
if (!frame) frame = requestAnimationFrame(place);
|
|
49
|
+
};
|
|
50
|
+
place();
|
|
51
|
+
const observer = new MutationObserver(schedule);
|
|
52
|
+
observer.observe(document.body, {
|
|
53
|
+
subtree: true,
|
|
54
|
+
childList: true,
|
|
55
|
+
attributes: true,
|
|
56
|
+
attributeFilter: ['class', 'style', 'hidden'],
|
|
57
|
+
});
|
|
58
|
+
addEventListener('scroll', schedule, { capture: true, passive: true });
|
|
59
|
+
addEventListener('resize', schedule);
|
|
60
|
+
return () => {
|
|
61
|
+
cancelAnimationFrame(frame);
|
|
62
|
+
observer.disconnect();
|
|
63
|
+
removeEventListener('scroll', schedule, { capture: true });
|
|
64
|
+
removeEventListener('resize', schedule);
|
|
65
|
+
};
|
|
66
|
+
}, [comments, draft]);
|
|
67
|
+
|
|
68
|
+
return (
|
|
69
|
+
<div ref={layer} className="rv-pins">
|
|
70
|
+
{comments.map((c) => {
|
|
71
|
+
const n = number.get(c.id);
|
|
72
|
+
const resolved = c.status === 'resolved';
|
|
73
|
+
return (
|
|
74
|
+
<button
|
|
75
|
+
key={c.id}
|
|
76
|
+
type="button"
|
|
77
|
+
className="rv-pin"
|
|
78
|
+
data-pin={c.id}
|
|
79
|
+
data-resolved={resolved ? '' : undefined}
|
|
80
|
+
data-active={active === c.id ? '' : undefined}
|
|
81
|
+
aria-label={`Comment ${n}${resolved ? ', resolved,' : ''} by ${c.author.name}`}
|
|
82
|
+
aria-pressed={active === c.id}
|
|
83
|
+
hidden
|
|
84
|
+
onClick={() => onOpen(c.id)}
|
|
85
|
+
>
|
|
86
|
+
<span className="rv-pin-face">{resolved ? <Icon name="check" size={12} /> : n}</span>
|
|
87
|
+
</button>
|
|
88
|
+
);
|
|
89
|
+
})}
|
|
90
|
+
{draft?.offset && (
|
|
91
|
+
<span className="rv-pin" data-draft="" data-new="" aria-hidden="true" hidden>
|
|
92
|
+
<span className="rv-pin-face" />
|
|
93
|
+
</span>
|
|
94
|
+
)}
|
|
95
|
+
</div>
|
|
96
|
+
);
|
|
97
|
+
}
|