@whzxc/agent-design-gallery 0.14.0 → 0.15.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/README.md +26 -16
- package/package.json +1 -1
- package/public/access.css +15 -2
- package/public/app.js +708 -483
- package/public/arrangement-worker.js +7 -7
- package/public/arrangement.js +35 -30
- package/public/assets.js +60 -38
- package/public/auth.js +59 -4
- package/public/boards.js +124 -0
- package/public/comment-anchors.js +50 -0
- package/public/comments.css +400 -0
- package/public/comments.js +1106 -0
- package/public/context-menu.js +97 -0
- package/public/frame-identity.js +26 -2
- package/public/hub.css +25 -3
- package/public/hub.html +10 -2
- package/public/hub.js +127 -23
- package/public/icons.js +2 -0
- package/public/index.html +13 -9
- package/public/instance-actions.js +9 -2
- package/public/layout.js +156 -86
- package/public/selection.js +1 -1
- package/public/style.css +74 -152
- package/public/switcher.css +5 -4
- package/public/switcher.js +109 -34
- package/public/token-catalog.js +8 -3
- package/public/tokens.css +14 -1
- package/public/view-route.js +0 -1
- package/src/boards/board.mjs +30 -0
- package/src/boards/token-board.mjs +196 -0
- package/src/cli-output.mjs +8 -5
- package/src/cli.mjs +49 -29
- package/src/comments.mjs +293 -0
- package/src/http.mjs +2 -3
- package/src/hub/project-link.mjs +41 -0
- package/src/hub/registry.mjs +5 -0
- package/src/hub.mjs +14 -10
- package/src/link.mjs +13 -1
- package/src/server.mjs +57 -93
- package/src/service.mjs +46 -12
- package/src/toolkit/arch.mjs +3 -2
- package/src/toolkit/asset-rules.mjs +24 -1
- package/src/toolkit/build-worker.mjs +6 -2
- package/src/toolkit/build.mjs +4 -2
- package/src/toolkit/commands.mjs +12 -7
- package/{public/review.js → src/toolkit/diff-document.mjs} +137 -43
- package/src/toolkit/diff-inputs.mjs +66 -30
- package/src/toolkit/diff.mjs +181 -39
- package/src/toolkit/impact-selection.mjs +25 -10
- package/src/toolkit/impact.mjs +32 -12
- package/src/toolkit/move-pairing.mjs +140 -0
- package/src/toolkit/project-index.mjs +2 -2
- package/src/toolkit/results.mjs +1 -1
- package/src/toolkit/review-document.mjs +435 -0
- package/src/toolkit/review-groups.mjs +29 -0
- package/src/toolkit/review-observation.mjs +131 -0
- package/src/toolkit/review-sheet.mjs +117 -0
- package/src/toolkit/review-specimens.mjs +60 -0
- package/src/toolkit/review.mjs +140 -0
- package/src/toolkit/runtime.mjs +1 -1
- package/src/toolkit/theme.mjs +16 -0
- package/public/annotations.js +0 -816
- package/public/labels.js +0 -122
- package/src/annotations.mjs +0 -238
|
@@ -0,0 +1,1106 @@
|
|
|
1
|
+
import { viewerBase } from './view-route.js';
|
|
2
|
+
|
|
3
|
+
const endpoint = 'api/comments';
|
|
4
|
+
const dragThreshold = 5;
|
|
5
|
+
const pinHeight = 26;
|
|
6
|
+
const gutter = 8;
|
|
7
|
+
const noticeTimeoutMs = 4000;
|
|
8
|
+
const relativeTime = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
|
|
9
|
+
|
|
10
|
+
function element(tag, className, text) {
|
|
11
|
+
const node = document.createElement(tag);
|
|
12
|
+
if (className) node.className = className;
|
|
13
|
+
if (text != null) node.textContent = text;
|
|
14
|
+
return node;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function button(text, className, action) {
|
|
18
|
+
const node = element('button', className, text);
|
|
19
|
+
node.type = 'button';
|
|
20
|
+
node.addEventListener('click', action);
|
|
21
|
+
return node;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function closeButton(label, action) {
|
|
25
|
+
const node = button('×', 'adg-comment-close', action);
|
|
26
|
+
node.setAttribute('aria-label', label);
|
|
27
|
+
return node;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function authorName(author) {
|
|
31
|
+
return author?.kind === 'user' ? 'You' : author?.name ?? author ?? 'Unknown';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function timeLabel(value) {
|
|
35
|
+
const node = element('time', 'adg-comment-meta');
|
|
36
|
+
const date = new Date(value);
|
|
37
|
+
if (!value || !Number.isFinite(+date)) return node;
|
|
38
|
+
node.dateTime = date.toISOString();
|
|
39
|
+
node.title = date.toLocaleString();
|
|
40
|
+
const seconds = (+date - Date.now()) / 1000;
|
|
41
|
+
const [unit, duration] = [['year', 31536000], ['month', 2592000], ['day', 86400], ['hour', 3600], ['minute', 60]]
|
|
42
|
+
.find(([, duration]) => Math.abs(seconds) >= duration) ?? ['second', 1];
|
|
43
|
+
node.textContent = relativeTime.format(Math.trunc(seconds / duration), unit);
|
|
44
|
+
return node;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function messageMeta(message) {
|
|
48
|
+
const node = element('div', 'adg-comment-message-meta');
|
|
49
|
+
node.append(element('strong', null, authorName(message.author)), timeLabel(message.at));
|
|
50
|
+
if (message.editedAt) node.append(element('span', 'adg-comment-meta', 'Edited'));
|
|
51
|
+
return node;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function label(thread) {
|
|
55
|
+
return thread.target?.label || thread.anchor?.frame || thread.anchor?.source || thread.anchor?.chapter || 'Board';
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function secondaryTarget(thread) {
|
|
59
|
+
const primary = label(thread);
|
|
60
|
+
const detail = thread.target?.displayName;
|
|
61
|
+
return detail && detail !== primary ? detail : '';
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function replyCount(thread) {
|
|
65
|
+
const count = Math.max(0, thread.messages.length - 1);
|
|
66
|
+
return `${count} ${count === 1 ? 'reply' : 'replies'}`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function clamp(value, min, max) {
|
|
70
|
+
return Math.max(min, Math.min(max, value));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function position(node, x, y) {
|
|
74
|
+
node.style.left = `${x}px`;
|
|
75
|
+
node.style.top = `${y}px`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function placeCard(node, anchor, width, height, panelBox) {
|
|
79
|
+
const cardWidth = node.offsetWidth;
|
|
80
|
+
const cardHeight = node.offsetHeight;
|
|
81
|
+
const right = panelBox && anchor.x < panelBox.left ? panelBox.left : width;
|
|
82
|
+
let x = anchor.x + 12;
|
|
83
|
+
if (x + cardWidth + gutter > right) x = anchor.x - cardWidth - 12;
|
|
84
|
+
x = clamp(x, gutter, Math.max(gutter, right - cardWidth - gutter));
|
|
85
|
+
if (panelBox && anchor.x >= panelBox.left) x = clamp(panelBox.left - cardWidth - gutter, gutter, width - cardWidth - gutter);
|
|
86
|
+
position(node, x, clamp(anchor.y - pinHeight, gutter, height - cardHeight - gutter));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function fanOut(point, width, occupied, viewportWidth, viewportHeight) {
|
|
90
|
+
const origin = {
|
|
91
|
+
x: clamp(point.x, 1, viewportWidth - width - 1),
|
|
92
|
+
y: clamp(point.y, pinHeight + 1, viewportHeight - 1),
|
|
93
|
+
};
|
|
94
|
+
const available = (x, y) => x >= 1 && x + width < viewportWidth && y > pinHeight && y < viewportHeight &&
|
|
95
|
+
!occupied.some(pin => Math.abs(pin.y - y) < pinHeight + 3 && pin.x < x + width + 3 && pin.x + pin.width + 3 > x);
|
|
96
|
+
if (available(origin.x, origin.y)) return origin;
|
|
97
|
+
const dx = width + 5;
|
|
98
|
+
const dy = pinHeight + 5;
|
|
99
|
+
const rings = Math.ceil(Math.max(viewportWidth / dx, viewportHeight / dy));
|
|
100
|
+
for (let ring = 1; ring <= rings; ring++) {
|
|
101
|
+
for (let row = -ring; row <= ring; row++) {
|
|
102
|
+
const step = Math.abs(row) === ring ? 1 : 2 * ring;
|
|
103
|
+
for (let column = -ring; column <= ring; column += step) {
|
|
104
|
+
const x = origin.x + column * dx;
|
|
105
|
+
const y = origin.y + row * dy;
|
|
106
|
+
if (available(x, y)) return { x, y };
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return origin;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function setStatus(node, text = '') {
|
|
114
|
+
node.textContent = text;
|
|
115
|
+
node.hidden = !text;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function makeStateBadge(state = 'changed') {
|
|
119
|
+
const node = element('span', 'adg-comment-changed', state === 'missing' ? 'Missing' : 'Changed');
|
|
120
|
+
node.dataset.state = state;
|
|
121
|
+
return node;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function makeConfirmBar(promptText, onConfirm, onCancel) {
|
|
125
|
+
const bar = element('div', 'adg-comment-confirm');
|
|
126
|
+
const text = element('span', 'adg-comment-confirm-label', promptText);
|
|
127
|
+
const actions = element('div', 'adg-comment-confirm-actions');
|
|
128
|
+
const cancel = button('Cancel', 'adg-comment-action', onCancel);
|
|
129
|
+
const confirm = button('Delete', 'adg-comment-action adg-comment-danger-button', async () => {
|
|
130
|
+
confirm.disabled = true;
|
|
131
|
+
cancel.disabled = true;
|
|
132
|
+
try {
|
|
133
|
+
await onConfirm();
|
|
134
|
+
} finally {
|
|
135
|
+
if (bar.isConnected) {
|
|
136
|
+
confirm.disabled = false;
|
|
137
|
+
cancel.disabled = false;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
actions.append(cancel, confirm);
|
|
142
|
+
bar.append(text, actions);
|
|
143
|
+
return { bar, cancel, confirm };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function editor(placeholder, action, onSubmit) {
|
|
147
|
+
const form = element('form', 'adg-comment-editor');
|
|
148
|
+
const field = element('textarea');
|
|
149
|
+
field.placeholder = placeholder;
|
|
150
|
+
field.setAttribute('aria-label', placeholder);
|
|
151
|
+
field.maxLength = 10000;
|
|
152
|
+
field.rows = 3;
|
|
153
|
+
const status = element('p', 'adg-comment-error');
|
|
154
|
+
status.setAttribute('role', 'alert');
|
|
155
|
+
status.hidden = true;
|
|
156
|
+
const submit = element('button', 'adg-comment-action adg-comment-primary', action);
|
|
157
|
+
submit.type = 'submit';
|
|
158
|
+
submit.disabled = true;
|
|
159
|
+
submit.title = '⌘↩';
|
|
160
|
+
submit.setAttribute('aria-keyshortcuts', 'Meta+Enter Control+Enter');
|
|
161
|
+
const actions = element('div', 'adg-comment-editor-actions');
|
|
162
|
+
actions.append(submit);
|
|
163
|
+
form.append(field, status, actions);
|
|
164
|
+
let pending = false;
|
|
165
|
+
function update() { submit.disabled = pending || !field.value.trim(); }
|
|
166
|
+
field.addEventListener('input', update);
|
|
167
|
+
field.addEventListener('keydown', event => {
|
|
168
|
+
if (event.key !== 'Enter' || !(event.metaKey || event.ctrlKey) || event.isComposing) return;
|
|
169
|
+
event.preventDefault();
|
|
170
|
+
event.stopPropagation();
|
|
171
|
+
form.requestSubmit();
|
|
172
|
+
});
|
|
173
|
+
form.addEventListener('submit', async event => {
|
|
174
|
+
event.preventDefault();
|
|
175
|
+
if (submit.disabled) return;
|
|
176
|
+
pending = true;
|
|
177
|
+
field.readOnly = true;
|
|
178
|
+
setStatus(status);
|
|
179
|
+
update();
|
|
180
|
+
try {
|
|
181
|
+
await onSubmit(field.value);
|
|
182
|
+
} catch (error) {
|
|
183
|
+
setStatus(status, error.message || 'Could not save the comment.');
|
|
184
|
+
} finally {
|
|
185
|
+
pending = false;
|
|
186
|
+
field.readOnly = false;
|
|
187
|
+
update();
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
return { form, field, actions, update, pending: () => pending };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function createComments({
|
|
194
|
+
toolButton, panelButton, panel, layer, hitTest, locate, navigate, boardPlace,
|
|
195
|
+
boardOrder = () => 0, request, onPanelToggle,
|
|
196
|
+
initialThreads = [], initialComment, storageKey = `adg-comments-visible:${viewerBase(location.pathname)}`,
|
|
197
|
+
}) {
|
|
198
|
+
panel.classList.add('adg-comments-panel');
|
|
199
|
+
panel.hidden = true;
|
|
200
|
+
panel.setAttribute('aria-label', 'Comments');
|
|
201
|
+
if (panel.id) panelButton.setAttribute('aria-controls', panel.id);
|
|
202
|
+
panelButton.setAttribute('aria-expanded', 'false');
|
|
203
|
+
toolButton.setAttribute('aria-keyshortcuts', 'C');
|
|
204
|
+
layer.classList.add('adg-comments-layer');
|
|
205
|
+
|
|
206
|
+
const header = element('header', 'adg-comment-panel-header');
|
|
207
|
+
const list = element('div', 'adg-comment-list');
|
|
208
|
+
const filterBar = element('div', 'adg-comment-filters');
|
|
209
|
+
filterBar.setAttribute('role', 'group');
|
|
210
|
+
filterBar.setAttribute('aria-label', 'Comment status');
|
|
211
|
+
const openFilter = button('Open', null, () => setFilter('open'));
|
|
212
|
+
const resolvedFilter = button('Resolved', null, () => setFilter('resolved'));
|
|
213
|
+
filterBar.append(openFilter, resolvedFilter);
|
|
214
|
+
|
|
215
|
+
const hiddenBanner = element('div', 'adg-comment-pins-banner');
|
|
216
|
+
hiddenBanner.append(
|
|
217
|
+
element('span', null, 'Pins hidden'),
|
|
218
|
+
button('Show (⇧C)', 'adg-comment-action', () => setVisible(true)),
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
header.append(element('h2', null, 'Comments'), closeButton('Close comments', () => setPanel(false, true)));
|
|
222
|
+
panel.replaceChildren(header, filterBar, hiddenBanner, list);
|
|
223
|
+
const count = element('span', 'adg-comment-count');
|
|
224
|
+
count.setAttribute('aria-hidden', 'true');
|
|
225
|
+
count.hidden = true;
|
|
226
|
+
panelButton.append(count);
|
|
227
|
+
|
|
228
|
+
const outline = element('div', 'adg-comment-outline');
|
|
229
|
+
const highlightLabel = element('div', 'adg-comment-highlight-label');
|
|
230
|
+
outline.setAttribute('aria-hidden', 'true');
|
|
231
|
+
highlightLabel.setAttribute('aria-hidden', 'true');
|
|
232
|
+
const pins = element('div', 'adg-comment-pins');
|
|
233
|
+
const preview = element('div', 'adg-comment-card adg-comment-preview');
|
|
234
|
+
preview.setAttribute('role', 'tooltip');
|
|
235
|
+
const notice = element('p', 'adg-comment-notice');
|
|
236
|
+
notice.setAttribute('role', 'alert');
|
|
237
|
+
notice.hidden = true;
|
|
238
|
+
layer.replaceChildren(outline, highlightLabel, pins, preview, notice);
|
|
239
|
+
|
|
240
|
+
let tool = false;
|
|
241
|
+
let available = !toolButton.disabled;
|
|
242
|
+
let unavailableReason = '';
|
|
243
|
+
let visible = true;
|
|
244
|
+
try { visible = localStorage.getItem(storageKey) !== 'false'; } catch {}
|
|
245
|
+
let threads = new Map(initialThreads.map(thread => [thread.id, thread]));
|
|
246
|
+
let filter = 'open';
|
|
247
|
+
let highlightPoint = null;
|
|
248
|
+
let previewId = null;
|
|
249
|
+
let draft = null;
|
|
250
|
+
let card = null;
|
|
251
|
+
let menu = null;
|
|
252
|
+
let drag = null;
|
|
253
|
+
let navigation = 0;
|
|
254
|
+
let version = 0;
|
|
255
|
+
let noticeTimer = 0;
|
|
256
|
+
let writeQueue = Promise.resolve();
|
|
257
|
+
let refreshing = null;
|
|
258
|
+
let refreshAgain = false;
|
|
259
|
+
let listSignature = '';
|
|
260
|
+
let previewSignature = '';
|
|
261
|
+
const pinNodes = new Map();
|
|
262
|
+
const rowNodes = new Map();
|
|
263
|
+
const replyDrafts = new Map();
|
|
264
|
+
const pendingMoves = new Map();
|
|
265
|
+
const unlocated = new Set();
|
|
266
|
+
|
|
267
|
+
function canUseTool() { return available && !toolButton.disabled; }
|
|
268
|
+
function active() { return tool && canUseTool(); }
|
|
269
|
+
|
|
270
|
+
function showNotice(text = '') {
|
|
271
|
+
clearTimeout(noticeTimer);
|
|
272
|
+
setStatus(notice, text);
|
|
273
|
+
if (text) noticeTimer = setTimeout(() => setStatus(notice), noticeTimeoutMs);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function updateTool() {
|
|
277
|
+
const isActive = active();
|
|
278
|
+
toolButton.setAttribute('aria-pressed', String(isActive));
|
|
279
|
+
toolButton.dataset.tooltip = isActive
|
|
280
|
+
? 'Exit comment mode (C)'
|
|
281
|
+
: (canUseTool() ? 'Comment (C)' : (unavailableReason || 'Comments unavailable in this view.'));
|
|
282
|
+
document.body.classList.toggle('adg-comment-tool-active', isActive);
|
|
283
|
+
if (!isActive) delete document.body.dataset.commentTarget;
|
|
284
|
+
hiddenBanner.hidden = visible;
|
|
285
|
+
panelButton.dataset.pinsHidden = String(!visible);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function setAvailable(nextAvailable, reason = '') {
|
|
289
|
+
const next = Boolean(nextAvailable);
|
|
290
|
+
const nextReason = next ? '' : String(reason || '');
|
|
291
|
+
const changedAvailability = available !== next || unavailableReason !== nextReason || toolButton.disabled !== !next;
|
|
292
|
+
available = next;
|
|
293
|
+
unavailableReason = nextReason;
|
|
294
|
+
toolButton.disabled = !available;
|
|
295
|
+
toolButton.toggleAttribute('aria-disabled', !available);
|
|
296
|
+
if (!available) {
|
|
297
|
+
tool = false;
|
|
298
|
+
highlightPoint = null;
|
|
299
|
+
}
|
|
300
|
+
updateTool();
|
|
301
|
+
if (changedAvailability) render();
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function toggle(force = !active()) {
|
|
305
|
+
tool = Boolean(force) && canUseTool();
|
|
306
|
+
if (tool && !visible) setVisible(true);
|
|
307
|
+
if (!tool) highlightPoint = null;
|
|
308
|
+
updateTool();
|
|
309
|
+
render();
|
|
310
|
+
return active();
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function setVisible(next = !visible) {
|
|
314
|
+
visible = Boolean(next);
|
|
315
|
+
try { localStorage.setItem(storageKey, String(visible)); } catch {}
|
|
316
|
+
if (!visible) {
|
|
317
|
+
cancelDrag();
|
|
318
|
+
previewId = null;
|
|
319
|
+
closeThread(true);
|
|
320
|
+
}
|
|
321
|
+
render();
|
|
322
|
+
return visible;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function setPanel(open, focus = false) {
|
|
326
|
+
open = Boolean(open);
|
|
327
|
+
panel.hidden = !open;
|
|
328
|
+
panelButton.setAttribute('aria-expanded', String(open));
|
|
329
|
+
onPanelToggle?.(open);
|
|
330
|
+
render();
|
|
331
|
+
if (focus) (open ? (filter === 'open' ? openFilter : resolvedFilter) : panelButton).focus({ preventScroll: true });
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function closePanel() {
|
|
335
|
+
if (panel.hidden) return false;
|
|
336
|
+
setPanel(false, false);
|
|
337
|
+
return true;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function setFilter(next) {
|
|
341
|
+
filter = next;
|
|
342
|
+
previewId = null;
|
|
343
|
+
if (card && threads.get(card.id)?.status !== filter) closeThread(false);
|
|
344
|
+
render();
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function report(error) { showNotice(error?.message || 'Could not update comments.'); }
|
|
348
|
+
|
|
349
|
+
function currentLocation(thread) {
|
|
350
|
+
const point = locate(thread);
|
|
351
|
+
return point && Number.isFinite(point.x) && Number.isFinite(point.y) ? point : null;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function threadState(thread, point, boardChanged = false) {
|
|
355
|
+
if (thread.state === 'gone' || (!point && unlocated.has(thread.id))) return 'missing';
|
|
356
|
+
if (point?.changed || thread.state === 'changed' || boardChanged) return 'changed';
|
|
357
|
+
return '';
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function renderPanel(locations) {
|
|
361
|
+
const countOpen = [...threads.values()].filter(thread => thread.status === 'open').length;
|
|
362
|
+
count.hidden = countOpen === 0;
|
|
363
|
+
if (count.textContent !== String(countOpen)) count.textContent = String(countOpen);
|
|
364
|
+
const nextAriaLabel = countOpen ? `Comments, ${countOpen} open` : 'Comments';
|
|
365
|
+
if (panelButton.getAttribute('aria-label') !== nextAriaLabel) panelButton.setAttribute('aria-label', nextAriaLabel);
|
|
366
|
+
hiddenBanner.hidden = visible;
|
|
367
|
+
panelButton.dataset.pinsHidden = String(!visible);
|
|
368
|
+
if (panel.hidden) return;
|
|
369
|
+
openFilter.setAttribute('aria-pressed', String(filter === 'open'));
|
|
370
|
+
resolvedFilter.setAttribute('aria-pressed', String(filter === 'resolved'));
|
|
371
|
+
const items = [...threads.values()].filter(thread => thread.status === filter).map(thread => {
|
|
372
|
+
const point = locations.get(thread.id);
|
|
373
|
+
const board = !point?.layer || !point?.chapter ? boardPlace?.(thread) : null;
|
|
374
|
+
const group = [point?.layer ?? board?.layer ?? thread.anchor?.layer, point?.chapter ?? board?.chapter ?? thread.anchor?.chapter]
|
|
375
|
+
.filter(Boolean).join(' / ');
|
|
376
|
+
const state = threadState(thread, point, !!board?.changed);
|
|
377
|
+
return { thread, group, state, detail: secondaryTarget(thread), order: filter === 'open' ? boardOrder(thread) : 0 };
|
|
378
|
+
});
|
|
379
|
+
items.sort((a, b) => filter === 'open'
|
|
380
|
+
? (a.order ?? Infinity) - (b.order ?? Infinity) || a.thread.number - b.thread.number
|
|
381
|
+
: (Date.parse(b.thread.resolvedAt) - Date.parse(a.thread.resolvedAt)) || b.thread.number - a.thread.number);
|
|
382
|
+
const emptyCopy = filter === 'open'
|
|
383
|
+
? (canUseTool() ? 'Press C and click a frame to start a comment.' : (unavailableReason || 'Comments unavailable in this view.'))
|
|
384
|
+
: 'No resolved comments.';
|
|
385
|
+
const signature = JSON.stringify([filter, card?.id, emptyCopy, items.map(({ thread, group, state, detail }) =>
|
|
386
|
+
[thread.id, thread.updatedAt, thread.messages.length, group, state, detail])]);
|
|
387
|
+
if (listSignature === signature) return;
|
|
388
|
+
listSignature = signature;
|
|
389
|
+
const focused = list.contains(document.activeElement) ? document.activeElement.dataset.commentId : null;
|
|
390
|
+
list.replaceChildren();
|
|
391
|
+
rowNodes.clear();
|
|
392
|
+
if (!items.length) {
|
|
393
|
+
list.append(element('p', 'adg-comment-empty', emptyCopy));
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
let group;
|
|
397
|
+
for (const item of items) {
|
|
398
|
+
const { thread } = item;
|
|
399
|
+
if (filter === 'open' && group !== item.group) {
|
|
400
|
+
group = item.group;
|
|
401
|
+
if (group) list.append(element('h3', 'adg-comment-group', group));
|
|
402
|
+
}
|
|
403
|
+
const row = button('', 'adg-comment-row', () => { void openThread(thread.id, { go: true, from: row }); });
|
|
404
|
+
row.dataset.commentId = thread.id;
|
|
405
|
+
if (card?.id === thread.id) row.setAttribute('aria-current', 'true');
|
|
406
|
+
const title = element('span', 'adg-comment-row-title');
|
|
407
|
+
title.append(element('span', 'adg-comment-number', `#${thread.number}`), element('strong', null, label(thread)));
|
|
408
|
+
if (item.state) title.append(makeStateBadge(item.state));
|
|
409
|
+
const first = thread.messages[0];
|
|
410
|
+
const last = thread.messages.at(-1);
|
|
411
|
+
const meta = element('span', 'adg-comment-row-meta adg-comment-meta');
|
|
412
|
+
if (last) meta.append(element('span', null, authorName(last.author)), timeLabel(last.at));
|
|
413
|
+
meta.append(element('span', 'adg-comment-reply-count', replyCount(thread)));
|
|
414
|
+
row.append(title);
|
|
415
|
+
if (item.detail) row.append(element('span', 'adg-comment-target adg-comment-meta', item.detail));
|
|
416
|
+
row.append(element('span', 'adg-comment-excerpt', first?.text), meta);
|
|
417
|
+
rowNodes.set(thread.id, row);
|
|
418
|
+
list.append(row);
|
|
419
|
+
}
|
|
420
|
+
rowNodes.get(focused)?.focus({ preventScroll: true });
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
function renderHighlight() {
|
|
424
|
+
const point = drag?.moved && !drag.cancelled ? drag : active() ? highlightPoint : null;
|
|
425
|
+
const target = point ? hitTest(point.x, point.y, point.deepest) : null;
|
|
426
|
+
const box = target?.box;
|
|
427
|
+
if (active()) document.body.dataset.commentTarget = String(!!box);
|
|
428
|
+
else delete document.body.dataset.commentTarget;
|
|
429
|
+
outline.hidden = !box;
|
|
430
|
+
highlightLabel.hidden = !box;
|
|
431
|
+
if (!box) return;
|
|
432
|
+
const x = box.x ?? box.left;
|
|
433
|
+
const y = box.y ?? box.top;
|
|
434
|
+
position(outline, x, y);
|
|
435
|
+
outline.style.width = `${box.width}px`;
|
|
436
|
+
outline.style.height = `${box.height}px`;
|
|
437
|
+
highlightLabel.textContent = target.hint || label(target);
|
|
438
|
+
position(highlightLabel, clamp(x, gutter, window.innerWidth - highlightLabel.offsetWidth - gutter),
|
|
439
|
+
clamp(y >= 28 ? y - 26 : y + box.height + 4, gutter, window.innerHeight - 28));
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function hover(x, y, deepest) {
|
|
443
|
+
highlightPoint = active() && Number.isFinite(x) && Number.isFinite(y) ? { x, y, deepest } : null;
|
|
444
|
+
renderHighlight();
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
function renderPreview(positions, locations, width, height) {
|
|
448
|
+
const thread = threads.get(previewId);
|
|
449
|
+
const point = positions.get(previewId);
|
|
450
|
+
preview.hidden = !thread || !point || !!card || !!draft || !!drag;
|
|
451
|
+
if (preview.hidden) return;
|
|
452
|
+
const state = threadState(thread, locations.get(previewId));
|
|
453
|
+
const detail = secondaryTarget(thread);
|
|
454
|
+
const nextSignature = JSON.stringify([thread.id, thread.updatedAt, thread.messages.length, state, detail]);
|
|
455
|
+
if (previewSignature !== nextSignature) {
|
|
456
|
+
previewSignature = nextSignature;
|
|
457
|
+
const first = thread.messages[0];
|
|
458
|
+
preview.replaceChildren();
|
|
459
|
+
const head = element('div', 'adg-comment-row-title');
|
|
460
|
+
head.append(element('span', 'adg-comment-number', `#${thread.number}`), element('strong', null, label(thread)));
|
|
461
|
+
if (state) head.append(makeStateBadge(state));
|
|
462
|
+
preview.append(head);
|
|
463
|
+
if (detail) preview.append(element('p', 'adg-comment-target adg-comment-meta', detail));
|
|
464
|
+
if (first) preview.append(messageMeta(first), element('p', 'adg-comment-excerpt', first.text));
|
|
465
|
+
preview.append(element('p', 'adg-comment-meta', replyCount(thread)));
|
|
466
|
+
}
|
|
467
|
+
placeCard(preview, point, width, height, panel.hidden ? null : panel.getBoundingClientRect());
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function cancelDrag() {
|
|
471
|
+
if (!drag?.moved || drag.cancelled) return false;
|
|
472
|
+
drag.cancelled = true;
|
|
473
|
+
render();
|
|
474
|
+
return true;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
async function dropPin(id, x, y, deepest) {
|
|
478
|
+
const move = { point: { x, y } };
|
|
479
|
+
pendingMoves.set(id, move);
|
|
480
|
+
render();
|
|
481
|
+
try {
|
|
482
|
+
if (pendingMoves.get(id) !== move || !threads.has(id)) return;
|
|
483
|
+
const target = hitTest(x, y, deepest);
|
|
484
|
+
if (!target) return;
|
|
485
|
+
move.placement = { id, ...placement(target) };
|
|
486
|
+
await mutate('move', move.placement);
|
|
487
|
+
} catch (error) {
|
|
488
|
+
report(error);
|
|
489
|
+
} finally {
|
|
490
|
+
if (pendingMoves.get(id) === move) pendingMoves.delete(id);
|
|
491
|
+
render();
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
function makePin(thread) {
|
|
496
|
+
const id = thread.id;
|
|
497
|
+
let suppressClick = false;
|
|
498
|
+
const pin = button(`#${thread.number}`, 'adg-comment-pin', event => {
|
|
499
|
+
if (suppressClick && event.detail) { suppressClick = false; return; }
|
|
500
|
+
if (card?.id === id) closeThread(true);
|
|
501
|
+
else void openThread(id, { from: pin });
|
|
502
|
+
render();
|
|
503
|
+
});
|
|
504
|
+
const stem = element('div', 'adg-comment-stem');
|
|
505
|
+
pin.dataset.commentId = id;
|
|
506
|
+
pin.addEventListener('pointerdown', event => {
|
|
507
|
+
if (event.button !== 0 || drag) return;
|
|
508
|
+
suppressClick = false;
|
|
509
|
+
drag = { id, pointerId: event.pointerId, x: event.clientX, y: event.clientY, startX: event.clientX, startY: event.clientY };
|
|
510
|
+
pin.setPointerCapture(event.pointerId);
|
|
511
|
+
});
|
|
512
|
+
pin.addEventListener('pointermove', event => {
|
|
513
|
+
if (drag?.pointerId !== event.pointerId || drag.cancelled) return;
|
|
514
|
+
drag.x = event.clientX;
|
|
515
|
+
drag.y = event.clientY;
|
|
516
|
+
drag.deepest = event.altKey;
|
|
517
|
+
if (!drag.moved) drag.moved = Math.hypot(drag.x - drag.startX, drag.y - drag.startY) >= dragThreshold;
|
|
518
|
+
if (!drag.moved) return;
|
|
519
|
+
suppressClick = true;
|
|
520
|
+
previewId = null;
|
|
521
|
+
render();
|
|
522
|
+
});
|
|
523
|
+
pin.addEventListener('pointerup', event => {
|
|
524
|
+
if (drag?.pointerId !== event.pointerId) return;
|
|
525
|
+
const previous = drag;
|
|
526
|
+
drag = null;
|
|
527
|
+
if (previous.moved && !previous.cancelled) void dropPin(id, event.clientX, event.clientY, event.altKey);
|
|
528
|
+
else render();
|
|
529
|
+
});
|
|
530
|
+
function stopPointer(event) {
|
|
531
|
+
if (drag?.pointerId !== event.pointerId) return;
|
|
532
|
+
drag = null;
|
|
533
|
+
render();
|
|
534
|
+
}
|
|
535
|
+
pin.addEventListener('pointercancel', stopPointer);
|
|
536
|
+
pin.addEventListener('lostpointercapture', stopPointer);
|
|
537
|
+
function showPreview() {
|
|
538
|
+
if (!drag) previewId = id;
|
|
539
|
+
render();
|
|
540
|
+
}
|
|
541
|
+
function hidePreview() {
|
|
542
|
+
if (previewId === id) previewId = null;
|
|
543
|
+
render();
|
|
544
|
+
}
|
|
545
|
+
pin.addEventListener('pointerenter', event => { if (event.pointerType !== 'touch') showPreview(); });
|
|
546
|
+
pin.addEventListener('pointerleave', hidePreview);
|
|
547
|
+
pin.addEventListener('focus', showPreview);
|
|
548
|
+
pin.addEventListener('blur', hidePreview);
|
|
549
|
+
pins.append(stem, pin);
|
|
550
|
+
return { pin, stem };
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
function renderPins(locations, width, height) {
|
|
554
|
+
const positions = new Map();
|
|
555
|
+
const occupied = [];
|
|
556
|
+
const ordered = [...threads.values()].sort((a, b) => a.number - b.number);
|
|
557
|
+
for (const thread of ordered) {
|
|
558
|
+
const selected = card?.id === thread.id;
|
|
559
|
+
const dragged = drag?.id === thread.id && drag.moved && !drag.cancelled;
|
|
560
|
+
const point = dragged ? drag : locations.get(thread.id);
|
|
561
|
+
const pinWidth = Math.max(34, 15 + String(thread.number).length * 7);
|
|
562
|
+
const onScreen = point && point.x >= 0 && point.x <= width && point.y >= 0 && point.y <= height;
|
|
563
|
+
const shown = visible && onScreen && (selected || thread.status !== 'resolved' || (!panel.hidden && filter === 'resolved'));
|
|
564
|
+
let nodes = pinNodes.get(thread.id);
|
|
565
|
+
if (!shown) {
|
|
566
|
+
if (nodes) nodes.pin.hidden = nodes.stem.hidden = true;
|
|
567
|
+
continue;
|
|
568
|
+
}
|
|
569
|
+
if (!nodes) {
|
|
570
|
+
nodes = makePin(thread);
|
|
571
|
+
pinNodes.set(thread.id, nodes);
|
|
572
|
+
}
|
|
573
|
+
const { pin, stem } = nodes;
|
|
574
|
+
const slot = fanOut(point, pinWidth, occupied, width, height);
|
|
575
|
+
const dx = slot.x - point.x;
|
|
576
|
+
const dy = slot.y - point.y;
|
|
577
|
+
occupied.push({ ...slot, width: pinWidth });
|
|
578
|
+
positions.set(thread.id, slot);
|
|
579
|
+
position(pin, slot.x, slot.y);
|
|
580
|
+
pin.hidden = false;
|
|
581
|
+
pin.style.width = `${pinWidth}px`;
|
|
582
|
+
const state = threadState(thread, locations.get(thread.id));
|
|
583
|
+
pin.toggleAttribute('data-changed', Boolean(state));
|
|
584
|
+
pin.toggleAttribute('data-resolved', thread.status === 'resolved');
|
|
585
|
+
pin.toggleAttribute('data-dragging', dragged);
|
|
586
|
+
pin.setAttribute('aria-expanded', String(selected));
|
|
587
|
+
pin.setAttribute('aria-label', `Comment #${thread.number}, ${label(thread)}${state ? `, ${state}` : ''}`);
|
|
588
|
+
stem.hidden = !dx && !dy;
|
|
589
|
+
if (!stem.hidden) {
|
|
590
|
+
position(stem, point.x, point.y);
|
|
591
|
+
stem.style.width = `${Math.hypot(dx, dy)}px`;
|
|
592
|
+
stem.style.transform = `rotate(${Math.atan2(dy, dx)}rad)`;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
return positions;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
function render(geometryOnly = false) {
|
|
599
|
+
const width = window.innerWidth;
|
|
600
|
+
const height = window.innerHeight;
|
|
601
|
+
const locations = new Map();
|
|
602
|
+
for (const thread of threads.values()) {
|
|
603
|
+
const move = pendingMoves.get(thread.id);
|
|
604
|
+
const point = move?.placement ? currentLocation(move.placement) : currentLocation(thread);
|
|
605
|
+
if (point) unlocated.delete(thread.id);
|
|
606
|
+
locations.set(thread.id, move ? { ...point, ...move.point } : point);
|
|
607
|
+
}
|
|
608
|
+
const positions = renderPins(locations, width, height);
|
|
609
|
+
renderHighlight();
|
|
610
|
+
if (!geometryOnly) {
|
|
611
|
+
updateTool();
|
|
612
|
+
renderPanel(locations);
|
|
613
|
+
}
|
|
614
|
+
renderPreview(positions, locations, width, height);
|
|
615
|
+
const panelBox = panel.hidden ? null : panel.getBoundingClientRect();
|
|
616
|
+
if (card) {
|
|
617
|
+
const thread = threads.get(card.id);
|
|
618
|
+
const point = locations.get(card.id);
|
|
619
|
+
if (thread) {
|
|
620
|
+
if (!geometryOnly) updateCard(card, thread, threadState(thread, point));
|
|
621
|
+
const fallback = (rowNodes.get(card.id) ?? panelButton).getBoundingClientRect();
|
|
622
|
+
placeCard(card.node, positions.get(card.id) ?? point ?? { x: fallback.left, y: fallback.bottom }, width, height, panelBox);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
if (draft) {
|
|
626
|
+
const point = currentLocation(draft.target) ?? draft.origin;
|
|
627
|
+
draft.pin.hidden = !visible;
|
|
628
|
+
position(draft.pin, point.x, point.y);
|
|
629
|
+
placeCard(draft.node, point, width, height, panelBox);
|
|
630
|
+
}
|
|
631
|
+
renderMenu();
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
function placement(target) {
|
|
635
|
+
const { anchor, target: snapshot, revision } = target;
|
|
636
|
+
return revision === undefined ? { anchor, target: snapshot } : { anchor, target: snapshot, revision };
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
function place(target) {
|
|
640
|
+
if (!target?.anchor) return false;
|
|
641
|
+
const box = target.box;
|
|
642
|
+
const origin = { x: target.x ?? box?.x ?? box?.left, y: target.y ?? box?.y ?? box?.top };
|
|
643
|
+
if (!Number.isFinite(origin.x) || !Number.isFinite(origin.y)) return false;
|
|
644
|
+
if (draft?.editor.pending()) return true;
|
|
645
|
+
const preservedText = draft?.editor.field.value.trim() ? draft.editor.field.value : '';
|
|
646
|
+
const preservedFrom = draft?.from;
|
|
647
|
+
if (!visible) setVisible(true);
|
|
648
|
+
closeDraft(false);
|
|
649
|
+
closeThread(false);
|
|
650
|
+
navigation++;
|
|
651
|
+
highlightPoint = null;
|
|
652
|
+
previewId = null;
|
|
653
|
+
const snapshot = placement(target);
|
|
654
|
+
const from = preservedFrom ?? document.activeElement;
|
|
655
|
+
const node = element('section', 'adg-comment-card adg-comment-composer');
|
|
656
|
+
const title = element('h2', null, 'New comment');
|
|
657
|
+
const heading = element('header', 'adg-comment-card-header');
|
|
658
|
+
heading.append(title, closeButton('Discard draft', () => { closeDraft(true); render(); }));
|
|
659
|
+
const input = editor(`Comment on ${target.hint || label(target)}…`, 'Post', async message => {
|
|
660
|
+
const thread = await mutate('create', { ...snapshot, message });
|
|
661
|
+
if (draft?.node !== node) return;
|
|
662
|
+
closeDraft(false);
|
|
663
|
+
if (visible && thread) await openThread(thread.id, { from });
|
|
664
|
+
render();
|
|
665
|
+
});
|
|
666
|
+
if (preservedText) {
|
|
667
|
+
input.field.value = preservedText;
|
|
668
|
+
input.update();
|
|
669
|
+
}
|
|
670
|
+
node.setAttribute('aria-label', 'New comment');
|
|
671
|
+
node.append(heading, input.form);
|
|
672
|
+
const pin = element('span', 'adg-comment-pin adg-comment-draft-pin', '+');
|
|
673
|
+
pin.setAttribute('aria-hidden', 'true');
|
|
674
|
+
draft = { target: snapshot, origin, node, pin, editor: input, from };
|
|
675
|
+
layer.append(pin, node);
|
|
676
|
+
render();
|
|
677
|
+
input.field.focus({ preventScroll: true });
|
|
678
|
+
if (preservedText) input.field.setSelectionRange(preservedText.length, preservedText.length);
|
|
679
|
+
return true;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
function closeDraft(restore) {
|
|
683
|
+
if (!draft) return false;
|
|
684
|
+
const previous = draft;
|
|
685
|
+
draft = null;
|
|
686
|
+
previous.node.remove();
|
|
687
|
+
previous.pin.remove();
|
|
688
|
+
if (restore) previous.from?.focus?.({ preventScroll: true });
|
|
689
|
+
return true;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
function dismiss() {
|
|
693
|
+
const closedMenu = closeMenu(false);
|
|
694
|
+
const closedCard = closeThread(false);
|
|
695
|
+
const closedDraft = draft && !draft.editor.pending() && !draft.editor.field.value.trim() ? closeDraft(false) : false;
|
|
696
|
+
if (closedMenu || closedCard || closedDraft) {
|
|
697
|
+
render();
|
|
698
|
+
return true;
|
|
699
|
+
}
|
|
700
|
+
return false;
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
function click(x, y, deepest) {
|
|
704
|
+
if (active()) {
|
|
705
|
+
const target = hitTest(x, y, deepest);
|
|
706
|
+
if (target) place({ ...target, x, y });
|
|
707
|
+
else dismiss();
|
|
708
|
+
return true;
|
|
709
|
+
}
|
|
710
|
+
dismiss();
|
|
711
|
+
return false;
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
function closeMenu(restore = false) {
|
|
715
|
+
if (!menu) return false;
|
|
716
|
+
const previous = menu;
|
|
717
|
+
menu = null;
|
|
718
|
+
previous.list.remove();
|
|
719
|
+
previous.button.setAttribute('aria-expanded', 'false');
|
|
720
|
+
if (restore) previous.button.focus({ preventScroll: true });
|
|
721
|
+
return true;
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
function renderMenu() {
|
|
725
|
+
if (!menu) return;
|
|
726
|
+
const anchor = menu.button.getBoundingClientRect();
|
|
727
|
+
const bounds = menu.button.closest('.adg-comment-messages')?.getBoundingClientRect();
|
|
728
|
+
if (bounds && (anchor.bottom <= bounds.top || anchor.top >= bounds.bottom)) { closeMenu(); return; }
|
|
729
|
+
const { offsetWidth, offsetHeight } = menu.list;
|
|
730
|
+
const y = anchor.bottom + offsetHeight + gutter <= window.innerHeight ? anchor.bottom : anchor.top - offsetHeight;
|
|
731
|
+
position(menu.list, clamp(anchor.right - offsetWidth, gutter, window.innerWidth - offsetWidth - gutter),
|
|
732
|
+
clamp(y, gutter, window.innerHeight - offsetHeight - gutter));
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
function makeMenu(name, actions) {
|
|
736
|
+
const wrapper = element('div', 'adg-comment-menu-wrapper');
|
|
737
|
+
const items = element('div', 'adg-comment-menu');
|
|
738
|
+
const trigger = button('⋯', 'adg-comment-close', () => {
|
|
739
|
+
if (menu?.button === trigger) { closeMenu(); return; }
|
|
740
|
+
closeMenu();
|
|
741
|
+
menu = { wrapper, list: items, button: trigger };
|
|
742
|
+
trigger.setAttribute('aria-expanded', 'true');
|
|
743
|
+
layer.append(items);
|
|
744
|
+
renderMenu();
|
|
745
|
+
items.firstElementChild?.focus({ preventScroll: true });
|
|
746
|
+
});
|
|
747
|
+
trigger.setAttribute('aria-label', name);
|
|
748
|
+
trigger.setAttribute('aria-expanded', 'false');
|
|
749
|
+
for (const [title, action, destructive] of actions) {
|
|
750
|
+
items.append(button(title, destructive ? 'adg-comment-danger' : null, () => { closeMenu(true); action(trigger); }));
|
|
751
|
+
}
|
|
752
|
+
wrapper.append(trigger);
|
|
753
|
+
return wrapper;
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
function closeThread(restore) {
|
|
757
|
+
if (!card) return false;
|
|
758
|
+
const previous = card;
|
|
759
|
+
card = null;
|
|
760
|
+
closeMenu();
|
|
761
|
+
previous.node.remove();
|
|
762
|
+
if (restore) {
|
|
763
|
+
const pin = pinNodes.get(previous.id)?.pin;
|
|
764
|
+
const row = panel.hidden ? null : rowNodes.get(previous.id);
|
|
765
|
+
(previous.from?.isConnected && !previous.from.hidden ? previous.from : pin && !pin.hidden ? pin : row ?? panelButton)
|
|
766
|
+
.focus({ preventScroll: true });
|
|
767
|
+
}
|
|
768
|
+
return true;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
async function openThread(id, { go = false, from } = {}) {
|
|
772
|
+
const thread = threads.get(id);
|
|
773
|
+
if (!thread) return false;
|
|
774
|
+
if (!visible) setVisible(true);
|
|
775
|
+
const sequence = ++navigation;
|
|
776
|
+
if (go && navigate) {
|
|
777
|
+
try {
|
|
778
|
+
const located = await navigate(thread);
|
|
779
|
+
if (sequence === navigation && located === false && !currentLocation(thread)) unlocated.add(id);
|
|
780
|
+
}
|
|
781
|
+
catch (error) { if (sequence === navigation) report(error); }
|
|
782
|
+
}
|
|
783
|
+
if (sequence !== navigation || !threads.has(id)) return false;
|
|
784
|
+
closeDraft(false);
|
|
785
|
+
closeThread(false);
|
|
786
|
+
if (threads.get(id).status === 'resolved') {
|
|
787
|
+
filter = 'resolved';
|
|
788
|
+
setPanel(true);
|
|
789
|
+
}
|
|
790
|
+
previewId = null;
|
|
791
|
+
card = makeCard(id, from);
|
|
792
|
+
layer.append(card.node);
|
|
793
|
+
render();
|
|
794
|
+
card.node.focus({ preventScroll: true });
|
|
795
|
+
return true;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
function commentURL(thread) {
|
|
799
|
+
const current = new URL(window.location.href);
|
|
800
|
+
const url = thread.link ? new URL(thread.link) : new URL(current.href);
|
|
801
|
+
const reviewPage = document.querySelector('#review-page');
|
|
802
|
+
const onReview = Boolean(reviewPage && !reviewPage.hidden);
|
|
803
|
+
if (onReview && current.searchParams.has('base')) url.searchParams.set('base', current.searchParams.get('base'));
|
|
804
|
+
else url.searchParams.delete('base');
|
|
805
|
+
url.pathname = viewerBase(url.pathname);
|
|
806
|
+
for (const parameter of ['scope', 'group', 'asset', 'frame', 'element']) url.searchParams.delete(parameter);
|
|
807
|
+
url.searchParams.set('comment', thread.number);
|
|
808
|
+
url.hash = '';
|
|
809
|
+
return url.href;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
function cancelConfirm(view, restore = false) {
|
|
813
|
+
if (!view?.confirm) return false;
|
|
814
|
+
const previous = view.confirm;
|
|
815
|
+
view.confirm = null;
|
|
816
|
+
if (previous.row) {
|
|
817
|
+
delete previous.row.dataset.content;
|
|
818
|
+
view.threadMessages = null;
|
|
819
|
+
} else {
|
|
820
|
+
previous.bar.remove();
|
|
821
|
+
}
|
|
822
|
+
if (restore && previous.trigger?.isConnected) previous.trigger.focus({ preventScroll: true });
|
|
823
|
+
return true;
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
function confirmDeleteThread(view, trigger) {
|
|
827
|
+
stopEditing(view);
|
|
828
|
+
cancelConfirm(view, false);
|
|
829
|
+
const { bar, confirm } = makeConfirmBar(
|
|
830
|
+
'Delete thread?',
|
|
831
|
+
() => view.safely(() => mutate('delete-thread', { id: view.id })),
|
|
832
|
+
() => { cancelConfirm(view, true); render(); },
|
|
833
|
+
);
|
|
834
|
+
view.confirm = { kind: 'thread', bar, trigger };
|
|
835
|
+
view.messages.before(bar);
|
|
836
|
+
render();
|
|
837
|
+
confirm.focus({ preventScroll: true });
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
function confirmDeleteMessage(view, thread, message, row, trigger) {
|
|
841
|
+
stopEditing(view);
|
|
842
|
+
cancelConfirm(view, false);
|
|
843
|
+
const { bar, confirm } = makeConfirmBar(
|
|
844
|
+
'Delete message?',
|
|
845
|
+
async () => {
|
|
846
|
+
await view.safely(() => mutate('delete', { id: thread.id, messageId: message.id }));
|
|
847
|
+
if (view.confirm?.messageId === message.id) cancelConfirm(view, false);
|
|
848
|
+
},
|
|
849
|
+
() => { cancelConfirm(view, true); render(); },
|
|
850
|
+
);
|
|
851
|
+
view.confirm = { kind: 'message', messageId: message.id, row, bar, trigger };
|
|
852
|
+
row.append(bar);
|
|
853
|
+
render();
|
|
854
|
+
confirm.focus({ preventScroll: true });
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
function makeCard(id, from) {
|
|
858
|
+
const node = element('section', 'adg-comment-card adg-comment-thread');
|
|
859
|
+
node.tabIndex = -1;
|
|
860
|
+
const title = element('h2');
|
|
861
|
+
const targetLabel = element('p', 'adg-comment-target adg-comment-meta');
|
|
862
|
+
targetLabel.hidden = true;
|
|
863
|
+
const stale = makeStateBadge();
|
|
864
|
+
const messages = element('div', 'adg-comment-messages');
|
|
865
|
+
messages.tabIndex = 0;
|
|
866
|
+
messages.setAttribute('aria-label', 'Messages');
|
|
867
|
+
messages.addEventListener('scroll', renderMenu);
|
|
868
|
+
node.addEventListener('scroll', renderMenu);
|
|
869
|
+
const resolution = element('p', 'adg-comment-resolution adg-comment-meta');
|
|
870
|
+
const status = element('p', 'adg-comment-error');
|
|
871
|
+
status.setAttribute('role', 'alert');
|
|
872
|
+
status.hidden = true;
|
|
873
|
+
const safely = async action => {
|
|
874
|
+
setStatus(status);
|
|
875
|
+
status.classList.remove('adg-comment-copied');
|
|
876
|
+
try { await action(); }
|
|
877
|
+
catch (error) { setStatus(status, error.message || 'Could not update the thread.'); }
|
|
878
|
+
if (node.isConnected) render();
|
|
879
|
+
};
|
|
880
|
+
let view;
|
|
881
|
+
const actions = makeMenu('Thread actions', [
|
|
882
|
+
['Copy link', () => void safely(async () => {
|
|
883
|
+
await navigator.clipboard.writeText(commentURL(threads.get(id)));
|
|
884
|
+
status.classList.add('adg-comment-copied');
|
|
885
|
+
setStatus(status, 'Link copied.');
|
|
886
|
+
})],
|
|
887
|
+
['Delete thread', trigger => confirmDeleteThread(view, trigger), true],
|
|
888
|
+
]);
|
|
889
|
+
const close = closeButton('Close thread', () => { navigation++; closeThread(true); render(); });
|
|
890
|
+
const heading = element('header', 'adg-comment-card-header');
|
|
891
|
+
heading.append(title, stale, actions, close);
|
|
892
|
+
const reply = editor('Reply…', 'Reply', async message => {
|
|
893
|
+
await mutate('reply', { id, message });
|
|
894
|
+
if (replyDrafts.get(id) !== message) return;
|
|
895
|
+
replyDrafts.delete(id);
|
|
896
|
+
reply.field.value = '';
|
|
897
|
+
if (card?.id === id && card.reply !== reply) {
|
|
898
|
+
card.reply.field.value = '';
|
|
899
|
+
card.reply.update();
|
|
900
|
+
}
|
|
901
|
+
});
|
|
902
|
+
reply.field.value = replyDrafts.get(id) ?? '';
|
|
903
|
+
reply.update();
|
|
904
|
+
reply.field.addEventListener('input', () => replyDrafts.set(id, reply.field.value));
|
|
905
|
+
const resolve = button('Resolve', 'adg-comment-action', async () => {
|
|
906
|
+
resolve.disabled = true;
|
|
907
|
+
await safely(async () => {
|
|
908
|
+
const action = threads.get(id)?.status === 'resolved' ? 'reopen' : 'resolve';
|
|
909
|
+
await mutate(action, { id });
|
|
910
|
+
});
|
|
911
|
+
resolve.disabled = false;
|
|
912
|
+
});
|
|
913
|
+
reply.actions.prepend(resolve);
|
|
914
|
+
node.append(heading, targetLabel, messages, resolution, status, reply.form);
|
|
915
|
+
view = { id, from, node, title, targetLabel, stale, messages, resolution, reply, resolve, safely, threadMessages: null, edit: null, confirm: null };
|
|
916
|
+
return view;
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
function updateCard(view, thread, state) {
|
|
920
|
+
const title = `#${thread.number} ${label(thread)}`;
|
|
921
|
+
if (view.title.textContent !== title) {
|
|
922
|
+
view.title.textContent = title;
|
|
923
|
+
view.node.setAttribute('aria-label', title);
|
|
924
|
+
}
|
|
925
|
+
const detail = secondaryTarget(thread);
|
|
926
|
+
view.targetLabel.hidden = !detail;
|
|
927
|
+
if (view.targetLabel.textContent !== detail) view.targetLabel.textContent = detail;
|
|
928
|
+
view.stale.hidden = !state;
|
|
929
|
+
if (state) {
|
|
930
|
+
view.stale.textContent = state === 'missing' ? 'Missing' : 'Changed';
|
|
931
|
+
view.stale.dataset.state = state;
|
|
932
|
+
}
|
|
933
|
+
const resolved = thread.status === 'resolved';
|
|
934
|
+
view.resolve.textContent = resolved ? 'Reopen' : 'Resolve';
|
|
935
|
+
view.resolution.hidden = !resolved;
|
|
936
|
+
const resolution = resolved ? `Resolved by ${authorName(thread.resolvedBy)}` : '';
|
|
937
|
+
if (view.resolution.textContent !== resolution) view.resolution.textContent = resolution;
|
|
938
|
+
if (view.threadMessages === thread.messages) return;
|
|
939
|
+
view.threadMessages = thread.messages;
|
|
940
|
+
const nearEnd = view.messages.scrollHeight - view.messages.scrollTop - view.messages.clientHeight < 24;
|
|
941
|
+
const scrollTop = view.messages.scrollTop;
|
|
942
|
+
const rows = new Map([...view.messages.children].map(node => [node.dataset.messageId, node]));
|
|
943
|
+
if (menu && view.messages.contains(menu.wrapper)) closeMenu();
|
|
944
|
+
let previous = null;
|
|
945
|
+
for (const message of thread.messages) {
|
|
946
|
+
const row = rows.get(message.id) ?? element('article', 'adg-comment-message');
|
|
947
|
+
rows.delete(message.id);
|
|
948
|
+
row.dataset.messageId = message.id;
|
|
949
|
+
const content = JSON.stringify(message);
|
|
950
|
+
const locked = view.edit?.id === message.id || view.confirm?.messageId === message.id;
|
|
951
|
+
if (!locked && row.dataset.content !== content) {
|
|
952
|
+
row.dataset.content = content;
|
|
953
|
+
const meta = messageMeta(message);
|
|
954
|
+
if (message.author?.kind === 'user') meta.append(makeMenu('Message actions', [
|
|
955
|
+
['Edit', () => editMessage(view, message, row)],
|
|
956
|
+
['Delete', trigger => confirmDeleteMessage(view, thread, message, row, trigger), true],
|
|
957
|
+
]));
|
|
958
|
+
row.replaceChildren(meta, element('p', 'adg-comment-message-text', message.text));
|
|
959
|
+
}
|
|
960
|
+
if (row !== (previous ? previous.nextElementSibling : view.messages.firstElementChild)) view.messages.insertBefore(row, previous ? previous.nextElementSibling : view.messages.firstElementChild);
|
|
961
|
+
previous = row;
|
|
962
|
+
}
|
|
963
|
+
for (const [id, row] of rows) {
|
|
964
|
+
if (view.edit?.id === id) view.edit = null;
|
|
965
|
+
if (view.confirm?.messageId === id) view.confirm = null;
|
|
966
|
+
row.remove();
|
|
967
|
+
}
|
|
968
|
+
view.messages.scrollTop = nearEnd ? view.messages.scrollHeight : scrollTop;
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
function stopEditing(view) {
|
|
972
|
+
if (!view.edit) return false;
|
|
973
|
+
delete view.edit.row.dataset.content;
|
|
974
|
+
view.edit = null;
|
|
975
|
+
view.threadMessages = null;
|
|
976
|
+
render();
|
|
977
|
+
return true;
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
function editMessage(view, message, row) {
|
|
981
|
+
cancelConfirm(view, false);
|
|
982
|
+
stopEditing(view);
|
|
983
|
+
const input = editor('Edit message', 'Save', async text => {
|
|
984
|
+
await mutate('edit', { id: view.id, messageId: message.id, message: text });
|
|
985
|
+
if (view.edit?.id === message.id) stopEditing(view);
|
|
986
|
+
});
|
|
987
|
+
input.field.value = message.text;
|
|
988
|
+
input.update();
|
|
989
|
+
input.actions.prepend(button('Cancel', 'adg-comment-action', () => stopEditing(view)));
|
|
990
|
+
view.edit = { id: message.id, row };
|
|
991
|
+
row.replaceChildren(messageMeta(message), input.form);
|
|
992
|
+
input.field.focus({ preventScroll: true });
|
|
993
|
+
render();
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
function removeThread(id) {
|
|
997
|
+
if (card?.id === id) closeThread(true);
|
|
998
|
+
if (drag?.id === id) drag = null;
|
|
999
|
+
pinNodes.get(id)?.pin.remove();
|
|
1000
|
+
pinNodes.get(id)?.stem.remove();
|
|
1001
|
+
pinNodes.delete(id);
|
|
1002
|
+
replyDrafts.delete(id);
|
|
1003
|
+
pendingMoves.delete(id);
|
|
1004
|
+
unlocated.delete(id);
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
function mutate(action, fields) {
|
|
1008
|
+
const write = writeQueue.then(async () => {
|
|
1009
|
+
showNotice();
|
|
1010
|
+
const result = await request(endpoint, { action, ...fields });
|
|
1011
|
+
version++;
|
|
1012
|
+
if (result?.thread) threads.set(result.thread.id, result.thread);
|
|
1013
|
+
else if (action === 'delete-thread' || action === 'delete') {
|
|
1014
|
+
threads.delete(fields.id);
|
|
1015
|
+
removeThread(fields.id);
|
|
1016
|
+
}
|
|
1017
|
+
if (action === 'reopen' || (action === 'reply' && result?.thread?.status === 'open' && card?.id === fields.id && filter === 'resolved')) filter = 'open';
|
|
1018
|
+
render();
|
|
1019
|
+
return result?.thread;
|
|
1020
|
+
});
|
|
1021
|
+
writeQueue = write.catch(() => {});
|
|
1022
|
+
return write;
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
async function refresh() {
|
|
1026
|
+
if (refreshing) {
|
|
1027
|
+
refreshAgain = true;
|
|
1028
|
+
return refreshing;
|
|
1029
|
+
}
|
|
1030
|
+
refreshing = (async () => {
|
|
1031
|
+
do {
|
|
1032
|
+
refreshAgain = false;
|
|
1033
|
+
const started = version;
|
|
1034
|
+
const result = await request(endpoint);
|
|
1035
|
+
if (started !== version) { refreshAgain = true; continue; }
|
|
1036
|
+
const next = new Map(result.threads.map(thread => [thread.id, thread]));
|
|
1037
|
+
for (const id of threads.keys()) if (!next.has(id)) removeThread(id);
|
|
1038
|
+
threads = next;
|
|
1039
|
+
render();
|
|
1040
|
+
} while (refreshAgain);
|
|
1041
|
+
})();
|
|
1042
|
+
try { await refreshing; }
|
|
1043
|
+
finally { refreshing = null; }
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
async function openLink(value = new URLSearchParams(location.search).get('comment')) {
|
|
1047
|
+
if (value == null || value === '') return false;
|
|
1048
|
+
const reference = String(value).replace(/^#/, '');
|
|
1049
|
+
const find = () => [...threads.values()].find(thread => thread.id === reference || String(thread.number) === reference);
|
|
1050
|
+
try {
|
|
1051
|
+
if (!find()) await refresh();
|
|
1052
|
+
const thread = find();
|
|
1053
|
+
if (!thread) throw new Error(`Comment #${reference} could not be found.`);
|
|
1054
|
+
return await openThread(thread.id, { go: true });
|
|
1055
|
+
} catch (error) {
|
|
1056
|
+
report(error);
|
|
1057
|
+
return false;
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
function escape() {
|
|
1062
|
+
const hadNotice = !notice.hidden;
|
|
1063
|
+
if (hadNotice) showNotice();
|
|
1064
|
+
const handled = cancelDrag() || closeMenu(true) || (card && cancelConfirm(card, true)) || (card && stopEditing(card)) || closeDraft(true) || closeThread(true);
|
|
1065
|
+
if (handled || hadNotice) {
|
|
1066
|
+
if (handled) navigation++;
|
|
1067
|
+
render();
|
|
1068
|
+
return true;
|
|
1069
|
+
}
|
|
1070
|
+
if (active()) { toggle(false); return true; }
|
|
1071
|
+
if (!panel.hidden) { setPanel(false, true); return true; }
|
|
1072
|
+
return false;
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
function key(event) {
|
|
1076
|
+
if (event.defaultPrevented || event.isComposing || document.querySelector('dialog[open], :popover-open')) return false;
|
|
1077
|
+
if (event.key === 'Escape') {
|
|
1078
|
+
if (!escape()) return false;
|
|
1079
|
+
} else {
|
|
1080
|
+
if (event.key.toLowerCase() !== 'c' || event.metaKey || event.ctrlKey || event.altKey ||
|
|
1081
|
+
event.target?.closest?.('input, textarea, select, [contenteditable]:not([contenteditable="false"])')) return false;
|
|
1082
|
+
if (!event.shiftKey && !canUseTool()) return false;
|
|
1083
|
+
if (!event.repeat) {
|
|
1084
|
+
if (event.shiftKey) setVisible();
|
|
1085
|
+
else toggle();
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
event.preventDefault();
|
|
1089
|
+
return true;
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
document.addEventListener('pointerdown', event => {
|
|
1093
|
+
if (menu && !menu.wrapper.contains(event.target) && !menu.list.contains(event.target)) closeMenu();
|
|
1094
|
+
});
|
|
1095
|
+
for (const type of ['keydown', 'keyup']) document.addEventListener(type, event => {
|
|
1096
|
+
if (event.key !== 'Alt' || (!highlightPoint && !drag?.moved)) return;
|
|
1097
|
+
if (highlightPoint) highlightPoint.deepest = event.altKey;
|
|
1098
|
+
if (drag) drag.deepest = event.altKey;
|
|
1099
|
+
renderHighlight();
|
|
1100
|
+
});
|
|
1101
|
+
toolButton.addEventListener('click', () => toggle());
|
|
1102
|
+
panelButton.addEventListener('click', () => setPanel(panel.hidden, panel.hidden));
|
|
1103
|
+
render();
|
|
1104
|
+
if (initialComment != null) queueMicrotask(() => { void openLink(initialComment); });
|
|
1105
|
+
return { active, place, click, hover, render, reposition: () => render(true), refresh, key, openLink, closePanel, dismiss, setAvailable };
|
|
1106
|
+
}
|