@mdbase-dev/ui 0.1.0-beta.165 → 0.1.0-beta.167

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.
@@ -0,0 +1,707 @@
1
+ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ // The comments on one record: threads in reading order, with replies,
3
+ // resolving, and suggested edits to accept or reject. A new comment starts
4
+ // from a passage the app hands in (`pending`). Style with
5
+ // `@mdbase-dev/ui/comments.css`.
6
+ import { i18n as _i18n } from "@lingui/core";
7
+ import { memo, useEffect, useRef, useState } from "react";
8
+ import { clipPassage, inlinePieces, personName, plainPassage, when as relativeTime, writtenByMe } from "./comments.js";
9
+ import { i18n, useLocaleRevision } from "./i18n.js";
10
+ const when = iso => relativeTime(iso, i18n.locale);
11
+ export function CommentsPanel({
12
+ threads,
13
+ people,
14
+ loading = false,
15
+ problem,
16
+ onRetry,
17
+ active,
18
+ pending,
19
+ onStart,
20
+ hint,
21
+ notice,
22
+ onSelect,
23
+ onSubmit,
24
+ onCancel,
25
+ onReply,
26
+ onChange,
27
+ onAccept,
28
+ onOpenRecord
29
+ }) {
30
+ useLocaleRevision();
31
+ const [filter, setFilter] = useState("open");
32
+ const panel = useRef(null);
33
+ const groups = {
34
+ open: threads.filter(p => p.thread.root.status === "open"),
35
+ resolved: threads.filter(p => p.thread.root.status === "resolved")
36
+ };
37
+ const shown = groups[filter];
38
+ // A thread selected in the editor comes into view (and shows, even when resolved).
39
+ useEffect(() => {
40
+ if (!active) return;
41
+ const hit = threads.find(p => p.thread.root.path === active);
42
+ if (hit) setFilter(hit.thread.root.status);
43
+ const frame = requestAnimationFrame(() => panel.current?.querySelector(`[data-thread="${CSS.escape(active)}"]`)?.scrollIntoView({
44
+ block: "nearest"
45
+ }));
46
+ return () => cancelAnimationFrame(frame);
47
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- only a new selection moves the list
48
+ }, [active]);
49
+ return _jsxs("section", {
50
+ ref: panel,
51
+ className: "mdbase-comments",
52
+ "aria-label": _i18n._(
53
+ /** i18n */
54
+ {
55
+ id: "VbyRUy",
56
+ message: "Comments"
57
+ }),
58
+ children: [_jsxs("div", {
59
+ className: "mdbase-comments-bar",
60
+ children: [_jsx("div", {
61
+ className: "mdbase-comments-filter",
62
+ role: "group",
63
+ "aria-label": _i18n._(
64
+ /** i18n */
65
+ {
66
+ id: "8vETh9",
67
+ message: "Show"
68
+ }),
69
+ children: ["open", "resolved"].map(f => _jsxs("button", {
70
+ type: "button",
71
+ "aria-pressed": filter === f,
72
+ onClick: () => setFilter(f),
73
+ children: [f === "open" ? _i18n._(
74
+ /** i18n */
75
+ {
76
+ id: "dawZoH",
77
+ message: "Open",
78
+ context: "Comment status"
79
+ }) : _i18n._(
80
+ /** i18n */
81
+ {
82
+ id: "3Swzbd",
83
+ message: "Resolved",
84
+ context: "Comment status"
85
+ }), " ", _jsx("span", {
86
+ className: "mdbase-comments-count",
87
+ children: groups[f].length
88
+ })]
89
+ }, f))
90
+ }), onStart && _jsx("button", {
91
+ type: "button",
92
+ className: "mdbase-button",
93
+ onClick: onStart,
94
+ disabled: pending !== null,
95
+ children: _i18n._(
96
+ /** i18n */
97
+ {
98
+ id: "ASnGmQ",
99
+ message: "New comment"
100
+ })
101
+ })]
102
+ }), notice, pending && _jsx(Composer, {
103
+ pending: pending,
104
+ signer: people.signing?.kind === "linked" ? people.me?.name : undefined,
105
+ onSubmit: onSubmit,
106
+ onCancel: onCancel
107
+ }, pending.quote ?? "\0whole"), loading && _jsx("p", {
108
+ className: "mdbase-comments-note",
109
+ role: "status",
110
+ children: _i18n._(
111
+ /** i18n */
112
+ {
113
+ id: "vLMZdC",
114
+ message: "Loading comments\u2026"
115
+ })
116
+ }), problem && _jsxs("p", {
117
+ className: "mdbase-comments-note",
118
+ children: [_i18n._(
119
+ /** i18n */
120
+ {
121
+ id: "ug2CpW",
122
+ message: "Comments could not be loaded: {problem}",
123
+ values: {
124
+ problem: problem
125
+ }
126
+ }), " ", onRetry && _jsx("button", {
127
+ type: "button",
128
+ className: "mdbase-thread-action",
129
+ onClick: onRetry,
130
+ children: _i18n._(
131
+ /** i18n */
132
+ {
133
+ id: "KDw4GX",
134
+ message: "Try again"
135
+ })
136
+ })]
137
+ }), !loading && !problem && !shown.length && !pending && _jsx("div", {
138
+ className: "mdbase-comments-empty",
139
+ children: filter === "open" ? _jsxs(_Fragment, {
140
+ children: [_jsx("p", {
141
+ children: _jsx("strong", {
142
+ children: _i18n._(
143
+ /** i18n */
144
+ {
145
+ id: "0Bm90s",
146
+ message: "No open comments"
147
+ })
148
+ })
149
+ }), hint && _jsx("p", {
150
+ children: hint
151
+ })]
152
+ }) : _jsx("p", {
153
+ children: _i18n._(
154
+ /** i18n */
155
+ {
156
+ id: "-enS2d",
157
+ message: "Nothing resolved yet."
158
+ })
159
+ })
160
+ }), _jsx("ul", {
161
+ className: "mdbase-threads",
162
+ children: shown.map(p => _jsx(Thread, {
163
+ placed: p,
164
+ people: people,
165
+ active: p.thread.root.path === active,
166
+ onSelect: onSelect,
167
+ onReply: onReply,
168
+ onChange: onChange,
169
+ onAccept: onAccept,
170
+ onOpenRecord: onOpenRecord
171
+ }, p.thread.root.path))
172
+ })]
173
+ });
174
+ }
175
+ /** Runs an action, showing it is busy and what went wrong. */
176
+ function useAction() {
177
+ const [busy, setBusy] = useState(false);
178
+ const [error, setError] = useState(null);
179
+ const run = async action => {
180
+ setBusy(true);
181
+ setError(null);
182
+ const result = await action();
183
+ setBusy(false);
184
+ if (!result.ok) setError(result.message);
185
+ return result.ok;
186
+ };
187
+ return {
188
+ busy,
189
+ error,
190
+ run
191
+ };
192
+ }
193
+ const submitsOnModEnter = event => event.key === "Enter" && (event.metaKey || event.ctrlKey);
194
+ function Composer({
195
+ pending,
196
+ signer,
197
+ onSubmit,
198
+ onCancel
199
+ }) {
200
+ const [text, setText] = useState("");
201
+ const field = useRef(null);
202
+ const {
203
+ busy,
204
+ error,
205
+ run
206
+ } = useAction();
207
+ useEffect(() => field.current?.focus(), []);
208
+ const empty = !text.trim();
209
+ const submit = () => void run(() => onSubmit(text));
210
+ return _jsxs("form", {
211
+ className: "mdbase-thread mdbase-comment-composer",
212
+ onSubmit: e => {
213
+ e.preventDefault();
214
+ if (!empty) submit();
215
+ },
216
+ onKeyDown: e => {
217
+ if (e.key === "Escape") {
218
+ e.stopPropagation();
219
+ onCancel();
220
+ } else if (submitsOnModEnter(e) && !empty) {
221
+ e.preventDefault();
222
+ submit();
223
+ }
224
+ },
225
+ children: [_jsxs("p", {
226
+ className: "mdbase-comment-composer-title",
227
+ children: [_jsx("strong", {
228
+ children: _i18n._(
229
+ /** i18n */
230
+ {
231
+ id: "ASnGmQ",
232
+ message: "New comment"
233
+ })
234
+ }), signer && _jsx("span", {
235
+ children: _i18n._(
236
+ /** i18n */
237
+ {
238
+ id: "Iy_6Bk",
239
+ message: "as {signer}",
240
+ values: {
241
+ signer: signer
242
+ }
243
+ })
244
+ })]
245
+ }), pending.quote ? _jsx("blockquote", {
246
+ className: "mdbase-thread-quote",
247
+ children: plainPassage(pending.quote)
248
+ }) : _jsx("p", {
249
+ className: "mdbase-thread-whole",
250
+ children: pending.quote === "" ? _i18n._(
251
+ /** i18n */
252
+ {
253
+ id: "ZDLF2F",
254
+ message: "At the cursor"
255
+ }) : _i18n._(
256
+ /** i18n */
257
+ {
258
+ id: "9-gb1N",
259
+ message: "On the whole note"
260
+ })
261
+ }), _jsx("textarea", {
262
+ ref: field,
263
+ className: "mdbase-field",
264
+ rows: 3,
265
+ value: text,
266
+ onChange: e => setText(e.target.value),
267
+ placeholder: _i18n._(
268
+ /** i18n */
269
+ {
270
+ id: "NBdIgR",
271
+ message: "Comment"
272
+ }),
273
+ "aria-label": _i18n._(
274
+ /** i18n */
275
+ {
276
+ id: "NBdIgR",
277
+ message: "Comment"
278
+ })
279
+ }), error && _jsx("p", {
280
+ className: "mdbase-comments-error",
281
+ role: "alert",
282
+ children: error
283
+ }), _jsxs("div", {
284
+ className: "mdbase-thread-actions",
285
+ children: [_jsx("button", {
286
+ type: "submit",
287
+ className: "mdbase-button is-primary",
288
+ disabled: busy || empty,
289
+ children: _i18n._(
290
+ /** i18n */
291
+ {
292
+ id: "NBdIgR",
293
+ message: "Comment"
294
+ })
295
+ }), _jsx("button", {
296
+ type: "button",
297
+ className: "mdbase-button",
298
+ onClick: onCancel,
299
+ disabled: busy,
300
+ children: _i18n._(
301
+ /** i18n */
302
+ {
303
+ id: "dEgA5A",
304
+ message: "Cancel"
305
+ })
306
+ })]
307
+ })]
308
+ });
309
+ }
310
+ const Thread = memo(function Thread({
311
+ placed,
312
+ people,
313
+ active,
314
+ onSelect,
315
+ onReply,
316
+ onChange,
317
+ onAccept,
318
+ onOpenRecord
319
+ }) {
320
+ const {
321
+ thread,
322
+ at
323
+ } = placed;
324
+ const {
325
+ root
326
+ } = thread;
327
+ const [reply, setReply] = useState("");
328
+ const [replying, setReplying] = useState(false);
329
+ const {
330
+ busy,
331
+ error,
332
+ run
333
+ } = useAction();
334
+ const suggestion = root.motivation === "editing" && root.suggestion ? root.suggestion : null;
335
+ const open = root.status === "open";
336
+ // An unsigned comment has no author to check, so whoever can write may withdraw it.
337
+ const mine = c => !c.createdBy || writtenByMe(c, people);
338
+ const withdraw = c => onChange && !c.deletedAt && mine(c) ? _jsx("button", {
339
+ type: "button",
340
+ className: "mdbase-thread-action is-quiet",
341
+ disabled: busy,
342
+ onClick: () => void run(() => onChange(c, {
343
+ kind: "withdraw"
344
+ })),
345
+ children: _i18n._(
346
+ /** i18n */
347
+ {
348
+ id: "OsyKSt",
349
+ message: "Withdraw"
350
+ })
351
+ }) : null;
352
+ const resolver = root.resolvedBy ? personName(root.resolvedBy, people) : undefined;
353
+ const state = suggestion?.outcome === "accepted" ? _i18n._(
354
+ /** i18n */
355
+ {
356
+ id: "bzMKg7",
357
+ message: "Accepted"
358
+ }) : suggestion?.outcome === "rejected" ? _i18n._(
359
+ /** i18n */
360
+ {
361
+ id: "ekCRTP",
362
+ message: "Rejected"
363
+ }) : _i18n._(
364
+ /** i18n */
365
+ {
366
+ id: "O6H89R",
367
+ message: "Resolved"
368
+ });
369
+ const resolvedNote = open ? null : [state, resolver ? _i18n._(
370
+ /** i18n */
371
+ {
372
+ id: "X0rakW",
373
+ message: "by {resolver}",
374
+ values: {
375
+ resolver: resolver
376
+ }
377
+ }) : "", root.resolvedAt ? when(root.resolvedAt) : ""].filter(Boolean).join(" ");
378
+ return _jsxs("li", {
379
+ className: `mdbase-thread${active ? " is-active" : ""}${open ? "" : " is-resolved"}`,
380
+ "data-thread": root.path,
381
+ children: [_jsxs("button", {
382
+ type: "button",
383
+ className: "mdbase-thread-anchor",
384
+ onClick: () => onSelect(placed),
385
+ title: at === null ? _i18n._(
386
+ /** i18n */
387
+ {
388
+ id: "wfpc2o",
389
+ message: "This passage is no longer in the text"
390
+ }) : _i18n._(
391
+ /** i18n */
392
+ {
393
+ id: "tZaM-o",
394
+ message: "Show in the note"
395
+ }),
396
+ children: [suggestion ? _jsx(Suggestion, {
397
+ root: root
398
+ }) : root.target ? _jsx("blockquote", {
399
+ className: "mdbase-thread-quote",
400
+ children: plainPassage(root.target.quote.exact) || _i18n._(
401
+ /** i18n */
402
+ {
403
+ id: "4K4jYB",
404
+ message: "(a point in the text)"
405
+ })
406
+ }) : _jsx("span", {
407
+ className: "mdbase-thread-whole",
408
+ children: _i18n._(
409
+ /** i18n */
410
+ {
411
+ id: "9-gb1N",
412
+ message: "On the whole note"
413
+ })
414
+ }), at === null && _jsx("span", {
415
+ className: "mdbase-thread-detached",
416
+ children: _i18n._(
417
+ /** i18n */
418
+ {
419
+ id: "niXmUu",
420
+ message: "The text it was on has changed"
421
+ })
422
+ })]
423
+ }), _jsx(Comment, {
424
+ comment: root,
425
+ people: people
426
+ }), thread.replies.map(r => _jsx(Comment, {
427
+ comment: r,
428
+ people: people,
429
+ reply: true,
430
+ children: withdraw(r)
431
+ }, r.path)), resolvedNote && _jsx("p", {
432
+ className: "mdbase-thread-resolved",
433
+ children: resolvedNote
434
+ }), error && _jsx("p", {
435
+ className: "mdbase-comments-error",
436
+ role: "alert",
437
+ children: error
438
+ }), replying && onReply ? _jsxs("form", {
439
+ className: "mdbase-thread-reply",
440
+ onSubmit: e => {
441
+ e.preventDefault();
442
+ if (reply.trim()) void run(() => onReply(thread, reply)).then(ok => ok && (setReply(""), setReplying(false)));
443
+ },
444
+ onKeyDown: e => {
445
+ if (e.key === "Escape") {
446
+ e.stopPropagation();
447
+ setReplying(false);
448
+ } else if (submitsOnModEnter(e)) {
449
+ e.preventDefault();
450
+ e.currentTarget.requestSubmit();
451
+ }
452
+ },
453
+ children: [_jsx("textarea", {
454
+ className: "mdbase-field",
455
+ rows: 2,
456
+ autoFocus: true,
457
+ value: reply,
458
+ onChange: e => setReply(e.target.value),
459
+ placeholder: _i18n._(
460
+ /** i18n */
461
+ {
462
+ id: "ImOQa9",
463
+ message: "Reply"
464
+ }),
465
+ "aria-label": _i18n._(
466
+ /** i18n */
467
+ {
468
+ id: "ImOQa9",
469
+ message: "Reply"
470
+ })
471
+ }), _jsxs("div", {
472
+ className: "mdbase-thread-actions",
473
+ children: [_jsx("button", {
474
+ type: "submit",
475
+ className: "mdbase-button is-primary",
476
+ disabled: busy || !reply.trim(),
477
+ children: _i18n._(
478
+ /** i18n */
479
+ {
480
+ id: "ImOQa9",
481
+ message: "Reply"
482
+ })
483
+ }), _jsx("button", {
484
+ type: "button",
485
+ className: "mdbase-button",
486
+ onClick: () => setReplying(false),
487
+ children: _i18n._(
488
+ /** i18n */
489
+ {
490
+ id: "dEgA5A",
491
+ message: "Cancel"
492
+ })
493
+ })]
494
+ })]
495
+ }) : _jsxs("div", {
496
+ className: "mdbase-thread-actions",
497
+ children: [open && suggestion && onChange && _jsxs(_Fragment, {
498
+ children: [onAccept && _jsx("button", {
499
+ type: "button",
500
+ className: "mdbase-button is-primary",
501
+ disabled: busy || at === null,
502
+ onClick: () => void run(() => onAccept(placed)),
503
+ title: at === null ? _i18n._(
504
+ /** i18n */
505
+ {
506
+ id: "0g4Jbf",
507
+ message: "The suggested text is no longer there"
508
+ }) : _i18n._(
509
+ /** i18n */
510
+ {
511
+ id: "EcXe4L",
512
+ message: "Make this edit"
513
+ }),
514
+ children: _i18n._(
515
+ /** i18n */
516
+ {
517
+ id: "g3UF2V",
518
+ message: "Accept"
519
+ })
520
+ }), _jsx("button", {
521
+ type: "button",
522
+ className: "mdbase-button",
523
+ disabled: busy,
524
+ onClick: () => void run(() => onChange(root, {
525
+ kind: "resolve",
526
+ outcome: "rejected"
527
+ })),
528
+ children: _i18n._(
529
+ /** i18n */
530
+ {
531
+ id: "1t_NnN",
532
+ message: "Reject"
533
+ })
534
+ })]
535
+ }), onReply && _jsx("button", {
536
+ type: "button",
537
+ className: "mdbase-thread-action",
538
+ onClick: () => setReplying(true),
539
+ children: _i18n._(
540
+ /** i18n */
541
+ {
542
+ id: "ImOQa9",
543
+ message: "Reply"
544
+ })
545
+ }), open && !suggestion && onChange && _jsx("button", {
546
+ type: "button",
547
+ className: "mdbase-thread-action",
548
+ disabled: busy,
549
+ onClick: () => void run(() => onChange(root, {
550
+ kind: "resolve"
551
+ })),
552
+ children: _i18n._(
553
+ /** i18n */
554
+ {
555
+ id: "2FoHU3",
556
+ message: "Resolve"
557
+ })
558
+ }), !open && onChange && _jsx("button", {
559
+ type: "button",
560
+ className: "mdbase-thread-action",
561
+ disabled: busy,
562
+ onClick: () => void run(() => onChange(root, {
563
+ kind: "reopen"
564
+ })),
565
+ children: _i18n._(
566
+ /** i18n */
567
+ {
568
+ id: "M7SqjM",
569
+ message: "Reopen"
570
+ })
571
+ }), !thread.replies.length && withdraw(root), onOpenRecord && _jsx("button", {
572
+ type: "button",
573
+ className: "mdbase-thread-action is-quiet",
574
+ onClick: () => onOpenRecord(root.path),
575
+ children: _i18n._(
576
+ /** i18n */
577
+ {
578
+ id: "9npOH9",
579
+ message: "Open file"
580
+ })
581
+ })]
582
+ })]
583
+ });
584
+ });
585
+ /** A comment's text with its light Markdown rendered. */
586
+ function InlineText({
587
+ text
588
+ }) {
589
+ return _jsx(_Fragment, {
590
+ children: inlinePieces(text).map((piece, i) => piece.kind === "link" ? _jsx("a", {
591
+ href: piece.href,
592
+ target: "_blank",
593
+ rel: "noopener noreferrer",
594
+ children: piece.text
595
+ }, i) : piece.kind === "em" ? _jsx("em", {
596
+ children: piece.text
597
+ }, i) : piece.kind === "strong" ? _jsx("strong", {
598
+ children: piece.text
599
+ }, i) : piece.kind === "code" ? _jsx("code", {
600
+ children: piece.text
601
+ }, i) : piece.text)
602
+ });
603
+ }
604
+ /** "Replace “x” with “y”", "Delete “x”" or "Insert “y”", for a suggestion. */
605
+ function describeSuggestion(root) {
606
+ const exact = clipPassage(root.target?.quote.exact ?? "");
607
+ const replacement = clipPassage(root.suggestion?.replacement ?? "");
608
+ if (exact && replacement) return _i18n._(
609
+ /** i18n */
610
+ {
611
+ id: "HB1jju",
612
+ message: "Replace \u201C{exact}\u201D with \u201C{replacement}\u201D",
613
+ values: {
614
+ exact: exact,
615
+ replacement: replacement
616
+ }
617
+ });
618
+ if (exact) return _i18n._(
619
+ /** i18n */
620
+ {
621
+ id: "L0Pqx1",
622
+ message: "Delete \u201C{exact}\u201D",
623
+ values: {
624
+ exact: exact
625
+ }
626
+ });
627
+ return _i18n._(
628
+ /** i18n */
629
+ {
630
+ id: "DPoqLN",
631
+ message: "Insert \u201C{replacement}\u201D",
632
+ values: {
633
+ replacement: replacement
634
+ }
635
+ });
636
+ }
637
+ /** A suggested edit as it will read: the text taken out, struck through, and the text put in. */
638
+ function Suggestion({
639
+ root
640
+ }) {
641
+ const exact = root.target?.quote.exact ?? "";
642
+ const replacement = root.suggestion?.replacement ?? "";
643
+ return _jsx("span", {
644
+ className: "mdbase-thread-suggestion",
645
+ "aria-label": describeSuggestion(root),
646
+ children: _jsxs("span", {
647
+ "aria-hidden": "true",
648
+ children: [exact && _jsx("del", {
649
+ children: clipPassage(exact)
650
+ }), exact && replacement && " ", replacement && _jsx("ins", {
651
+ children: clipPassage(replacement)
652
+ })]
653
+ })
654
+ });
655
+ }
656
+ function Comment({
657
+ comment,
658
+ people,
659
+ reply,
660
+ children
661
+ }) {
662
+ const author = personName(comment.createdBy, people) ?? _i18n._(
663
+ /** i18n */
664
+ {
665
+ id: "-rQt-n",
666
+ message: "Unsigned"
667
+ });
668
+ return _jsxs("div", {
669
+ className: `mdbase-comment${reply ? " is-reply" : ""}`,
670
+ children: [_jsxs("p", {
671
+ className: "mdbase-comment-meta",
672
+ children: [_jsx("strong", {
673
+ children: author
674
+ }), " ", _jsx("time", {
675
+ dateTime: comment.createdAt,
676
+ title: new Date(comment.createdAt).toLocaleString(i18n.locale),
677
+ children: when(comment.createdAt)
678
+ }), children]
679
+ }), comment.deletedAt ? _jsx("p", {
680
+ className: "mdbase-comment-withdrawn",
681
+ children: _i18n._(
682
+ /** i18n */
683
+ {
684
+ id: "C-gM9M",
685
+ message: "Withdrawn."
686
+ })
687
+ }) : comment.text ? _jsx("p", {
688
+ className: "mdbase-comment-text",
689
+ children: _jsx(InlineText, {
690
+ text: comment.text
691
+ })
692
+ }) : null]
693
+ });
694
+ }
695
+ /** "3 open comments", for a control that opens them. */
696
+ export function openCommentCount(threads) {
697
+ const count = threads.filter(p => p.thread.root.status === "open").length;
698
+ return _i18n._(
699
+ /** i18n */
700
+ {
701
+ id: "KGcfcd",
702
+ message: "{count, plural, one {# open comment} other {# open comments}}",
703
+ values: {
704
+ count: count
705
+ }
706
+ });
707
+ }