@tristan2828/ui-foundation 4.127.0 → 4.128.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/conventions/docs/add-an-entity.md +52 -16
- package/conventions/docs/design-language.md +25 -8
- package/dist/components/app/edit-in-place-store.d.ts +2 -3
- package/dist/components/app/edit-in-place-store.js +53 -9
- package/dist/components/app/entity-form.d.ts +9 -1
- package/dist/components/app/entity-form.js +9 -2
- package/dist/components/app/equal-items.d.ts +2 -1
- package/dist/components/app/equal-items.js +9 -25
- package/dist/components/app/form-conflict-notice.d.ts +8 -0
- package/dist/components/app/form-conflict-notice.js +15 -0
- package/dist/components/app/form-dialog.d.ts +8 -1
- package/dist/components/app/form-dialog.js +9 -2
- package/dist/components/app/milkdown-editor.js +21 -329
- package/dist/components/app/notify.d.ts +18 -1
- package/dist/components/app/notify.js +26 -3
- package/dist/components/app/page-restore.js +1 -1
- package/dist/components/app/rich-text-box-anchor.d.ts +6 -0
- package/dist/components/app/rich-text-box-anchor.js +8 -0
- package/dist/components/app/rich-text-heading-view.d.ts +4 -0
- package/dist/components/app/rich-text-heading-view.js +91 -0
- package/dist/components/app/rich-text-link-box.d.ts +9 -0
- package/dist/components/app/rich-text-link-box.js +40 -0
- package/dist/components/app/rich-text-links.d.ts +14 -0
- package/dist/components/app/rich-text-links.js +92 -0
- package/dist/components/app/rich-text-page-box.d.ts +10 -0
- package/dist/components/app/rich-text-page-box.js +45 -0
- package/dist/components/app/rich-text-task-item-view.d.ts +4 -0
- package/dist/components/app/rich-text-task-item-view.js +59 -0
- package/dist/components/app/rich-text-toggle-heading.js +1 -1
- package/dist/components/app/step-row.d.ts +15 -0
- package/dist/components/app/step-row.js +27 -0
- package/dist/components/app/step-track.d.ts +82 -0
- package/dist/components/app/step-track.js +98 -0
- package/dist/components/ui/sonner.js +1 -1
- package/dist/hooks/use-form-version.d.ts +101 -0
- package/dist/hooks/use-form-version.js +187 -0
- package/dist/hooks/use-record-update.d.ts +2 -7
- package/dist/hooks/use-record-update.js +2 -11
- package/dist/index.d.ts +3 -1
- package/dist/index.js +2 -0
- package/dist/lib/refusal.d.ts +9 -0
- package/dist/lib/refusal.js +19 -0
- package/package.json +1 -1
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
// The editor's link box (Ctrl/Cmd+K, milkdown-editor.tsx).
|
|
3
|
+
import { useId, useRef, useState } from 'react';
|
|
4
|
+
import { Button } from '../ui/button.js';
|
|
5
|
+
import { Field, FieldError, FieldLabel } from '../ui/field.js';
|
|
6
|
+
import { Input } from '../ui/input.js';
|
|
7
|
+
import { PopoverContent } from '../ui/popover.js';
|
|
8
|
+
import { linkHref, linkRefusal } from '../../lib/link-href.js';
|
|
9
|
+
// The link box: the address, Apply, and Remove link for an existing one.
|
|
10
|
+
// Enter (Ctrl/Cmd+Enter too) applies the link and stays its own: editing
|
|
11
|
+
// in place listens for Ctrl/Cmd+Enter through the portal, and would save
|
|
12
|
+
// the field without it. Esc is the popover's: it closes the box only.
|
|
13
|
+
export function LinkForm({ box, onApply }) {
|
|
14
|
+
const [typed, setTyped] = useState(box.href);
|
|
15
|
+
const [error, setError] = useState(null);
|
|
16
|
+
const inputRef = useRef(null);
|
|
17
|
+
const inputId = useId();
|
|
18
|
+
const errorId = `${inputId}-error`;
|
|
19
|
+
const apply = () => {
|
|
20
|
+
const href = linkHref(typed);
|
|
21
|
+
if (href === null) {
|
|
22
|
+
setError(linkRefusal(typed) === 'whitespace'
|
|
23
|
+
? 'An address has no spaces. Remove them, or write each as %20.'
|
|
24
|
+
: 'Enter a web or email address, like https://example.com.');
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
onApply(href);
|
|
28
|
+
};
|
|
29
|
+
const onKeyDown = (event) => {
|
|
30
|
+
if (event.key === 'Enter' && event.target === inputRef.current) {
|
|
31
|
+
event.preventDefault();
|
|
32
|
+
event.stopPropagation();
|
|
33
|
+
apply();
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
return (_jsxs(PopoverContent, { anchor: box.anchor, align: "start", "aria-label": box.href ? 'Edit Link' : 'Add Link', initialFocus: inputRef, finalFocus: false, className: "w-80", onKeyDown: onKeyDown, children: [_jsxs(Field, { "data-invalid": error ? true : undefined, children: [_jsx(FieldLabel, { htmlFor: inputId, children: "Link Address" }), _jsx(Input, { ref: inputRef, id: inputId, value: typed, placeholder: "https://", autoComplete: "off", "aria-invalid": error ? true : undefined, "aria-describedby": error ? errorId : undefined, onChange: (event) => {
|
|
37
|
+
setTyped(event.target.value);
|
|
38
|
+
setError(null);
|
|
39
|
+
} }), error && _jsx(FieldError, { id: errorId, children: error })] }), _jsxs("div", { className: "flex gap-2", children: [_jsx(Button, { size: "sm", onClick: apply, children: "Apply" }), box.href && (_jsx(Button, { size: "sm", variant: "outline", onClick: () => onApply(''), children: "Remove Link" }))] })] }));
|
|
40
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { EditorState } from '@milkdown/kit/prose/state';
|
|
2
|
+
import type { EditorView } from '@milkdown/kit/prose/view';
|
|
3
|
+
import type { PageLinkResolver } from '../../lib/page-link.js';
|
|
4
|
+
export type LinkTarget = {
|
|
5
|
+
from: number;
|
|
6
|
+
to: number;
|
|
7
|
+
href: string;
|
|
8
|
+
};
|
|
9
|
+
export declare function linkTarget(state: EditorState): LinkTarget;
|
|
10
|
+
export declare function followLink(view: EditorView, event: MouseEvent, pages: {
|
|
11
|
+
resolve: PageLinkResolver | undefined;
|
|
12
|
+
open: (href: string) => void;
|
|
13
|
+
}): boolean;
|
|
14
|
+
export declare function applyLink(view: EditorView, target: LinkTarget, href: string): void;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { linkHref } from '../../lib/link-href.js';
|
|
2
|
+
import { pageLinkInfo } from './rich-text-page-links.js';
|
|
3
|
+
export function linkTarget(state) {
|
|
4
|
+
const link = state.schema.marks.link;
|
|
5
|
+
const { from, to, empty, $from } = state.selection;
|
|
6
|
+
if (!empty) {
|
|
7
|
+
let href = '';
|
|
8
|
+
state.doc.nodesBetween(from, to, (node) => {
|
|
9
|
+
const mark = link.isInSet(node.marks);
|
|
10
|
+
if (mark && !href)
|
|
11
|
+
href = String(mark.attrs.href);
|
|
12
|
+
});
|
|
13
|
+
return { from, to, href };
|
|
14
|
+
}
|
|
15
|
+
const mark = link.isInSet($from.marks());
|
|
16
|
+
if (!mark)
|
|
17
|
+
return { from, to, href: '' };
|
|
18
|
+
const start = $from.start();
|
|
19
|
+
let target = { from, to, href: String(mark.attrs.href) };
|
|
20
|
+
let runFrom = -1;
|
|
21
|
+
$from.parent.forEach((child, offset) => {
|
|
22
|
+
const childFrom = start + offset;
|
|
23
|
+
const childTo = childFrom + child.nodeSize;
|
|
24
|
+
if (!mark.isInSet(child.marks)) {
|
|
25
|
+
runFrom = -1;
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (runFrom < 0)
|
|
29
|
+
runFrom = childFrom;
|
|
30
|
+
if (runFrom <= from && from <= childTo)
|
|
31
|
+
target = { from: runFrom, to: childTo, href: target.href };
|
|
32
|
+
});
|
|
33
|
+
return target;
|
|
34
|
+
}
|
|
35
|
+
// A plain click (or a tap) on a link opens it in a new tab, the way the
|
|
36
|
+
// read view's links open (markdown.tsx), so notes that are always the
|
|
37
|
+
// editor still work as notes. Not when the click chose words (a drag ends
|
|
38
|
+
// in a click too), extends the selection (Shift) or is Alt's; the address
|
|
39
|
+
// goes through linkHref again, so a link that came in with the Markdown
|
|
40
|
+
// can't run code (`javascript:`) either: that one stays text to click
|
|
41
|
+
// into. Read-only too, where the browser would otherwise follow it in this
|
|
42
|
+
// tab. To edit a link's words, put the caret in them from the keyboard or
|
|
43
|
+
// drag over them; Ctrl/Cmd+K edits its address.
|
|
44
|
+
//
|
|
45
|
+
// A page link (`pageLink`) opens its page in this tab instead (`open`:
|
|
46
|
+
// the router's, when there is one), and a removed page's link opens
|
|
47
|
+
// nothing.
|
|
48
|
+
export function followLink(view, event, pages) {
|
|
49
|
+
if (event.button !== 0 || event.shiftKey || event.altKey)
|
|
50
|
+
return false;
|
|
51
|
+
const anchor = event.target instanceof Element ? event.target.closest('a[href]') : null;
|
|
52
|
+
if (!anchor || !view.dom.contains(anchor))
|
|
53
|
+
return false;
|
|
54
|
+
if (!view.state.selection.empty)
|
|
55
|
+
return false;
|
|
56
|
+
const written = anchor.getAttribute('href') ?? '';
|
|
57
|
+
const page = pageLinkInfo(pages.resolve, written);
|
|
58
|
+
if (page) {
|
|
59
|
+
event.preventDefault();
|
|
60
|
+
if (page.removed)
|
|
61
|
+
return false;
|
|
62
|
+
const href = linkHref(written);
|
|
63
|
+
if (!href)
|
|
64
|
+
return false;
|
|
65
|
+
pages.open(href);
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
const href = linkHref(written);
|
|
69
|
+
if (!href)
|
|
70
|
+
return false;
|
|
71
|
+
event.preventDefault();
|
|
72
|
+
window.open(href, '_blank', 'noopener,noreferrer');
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
// Sets (or, with `href` empty, removes) the link on the target. With
|
|
76
|
+
// nothing selected and no link there, the address goes in as the link's
|
|
77
|
+
// own text.
|
|
78
|
+
export function applyLink(view, target, href) {
|
|
79
|
+
const link = view.state.schema.marks.link;
|
|
80
|
+
const tr = view.state.tr;
|
|
81
|
+
if (target.from === target.to) {
|
|
82
|
+
if (!href)
|
|
83
|
+
return;
|
|
84
|
+
tr.insertText(href, target.from).addMark(target.from, target.from + href.length, link.create({ href }));
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
tr.removeMark(target.from, target.to, link);
|
|
88
|
+
if (href)
|
|
89
|
+
tr.addMark(target.from, target.to, link.create({ href }));
|
|
90
|
+
}
|
|
91
|
+
view.dispatch(tr.scrollIntoView());
|
|
92
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { BoxAnchor } from './rich-text-box-anchor.js';
|
|
2
|
+
export type PageBox = {
|
|
3
|
+
title: string;
|
|
4
|
+
anchor: BoxAnchor;
|
|
5
|
+
};
|
|
6
|
+
export declare function PageForm({ box, busy, onCreate }: {
|
|
7
|
+
box: PageBox;
|
|
8
|
+
busy: boolean;
|
|
9
|
+
onCreate: (title: string) => Promise<string | null>;
|
|
10
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
// The editor's new-page box ("/page" and Turn into page, milkdown-editor.tsx).
|
|
3
|
+
import { useId, useRef, useState } from 'react';
|
|
4
|
+
import { Button } from '../ui/button.js';
|
|
5
|
+
import { Field, FieldError, FieldLabel } from '../ui/field.js';
|
|
6
|
+
import { Input } from '../ui/input.js';
|
|
7
|
+
import { PopoverContent } from '../ui/popover.js';
|
|
8
|
+
// The new page's title box: the title (the selected words to start with),
|
|
9
|
+
// and Create page. While the app makes the page it waits, its controls
|
|
10
|
+
// held; a refusal shows under the title, in the gateway's words, and the
|
|
11
|
+
// text is left as it was. Enter creates; Esc is the popover's, and closes
|
|
12
|
+
// it (not while the app is still working).
|
|
13
|
+
export function PageForm({ box, busy, onCreate }) {
|
|
14
|
+
const [typed, setTyped] = useState(box.title);
|
|
15
|
+
const [error, setError] = useState(null);
|
|
16
|
+
const inputRef = useRef(null);
|
|
17
|
+
const inputId = useId();
|
|
18
|
+
const errorId = `${inputId}-error`;
|
|
19
|
+
const create = () => {
|
|
20
|
+
const title = typed.replace(/\s+/g, ' ').trim();
|
|
21
|
+
if (!title) {
|
|
22
|
+
setError('Give the page a title.');
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
setError(null);
|
|
26
|
+
void onCreate(title).then((refused) => {
|
|
27
|
+
if (refused) {
|
|
28
|
+
setError(refused);
|
|
29
|
+
inputRef.current?.focus();
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
const onKeyDown = (event) => {
|
|
34
|
+
if (event.key === 'Enter' && event.target === inputRef.current) {
|
|
35
|
+
event.preventDefault();
|
|
36
|
+
event.stopPropagation();
|
|
37
|
+
if (!busy)
|
|
38
|
+
create();
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
return (_jsxs(PopoverContent, { anchor: box.anchor, align: "start", "aria-label": "New Page", initialFocus: inputRef, finalFocus: false, className: "w-80", "data-rich-text-menu": "", "data-slot": "page-form", onKeyDown: onKeyDown, children: [_jsxs(Field, { "data-invalid": error ? true : undefined, children: [_jsx(FieldLabel, { htmlFor: inputId, children: "Page Title" }), _jsx(Input, { ref: inputRef, id: inputId, value: typed, autoComplete: "off", readOnly: busy, "aria-invalid": error ? true : undefined, "aria-describedby": error ? errorId : undefined, onChange: (event) => {
|
|
42
|
+
setTyped(event.target.value);
|
|
43
|
+
setError(null);
|
|
44
|
+
} }), error && _jsx(FieldError, { id: errorId, children: error })] }), _jsx("div", { className: "flex gap-2", children: _jsx(Button, { size: "sm", onClick: create, disabled: busy, children: busy ? 'Creating…' : 'Create Page' }) })] }));
|
|
45
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Node as ProseNode } from '@milkdown/kit/prose/model';
|
|
2
|
+
import type { EditorView, NodeView } from '@milkdown/kit/prose/view';
|
|
3
|
+
export declare function taskItemView(node: ProseNode, view: EditorView, getPos: () => number | undefined): NodeView;
|
|
4
|
+
export declare function toggleTaskItem(view: EditorView): boolean;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { TASK_ROW, taskBoxHit } from './rich-text-tasks.js';
|
|
2
|
+
// A task-list item: its checkbox toggles `checked`. Any other list item
|
|
3
|
+
// renders as Milkdown's own.
|
|
4
|
+
export function taskItemView(node, view, getPos) {
|
|
5
|
+
const dom = document.createElement('li');
|
|
6
|
+
const box = document.createElement('input');
|
|
7
|
+
box.type = 'checkbox';
|
|
8
|
+
box.contentEditable = 'false';
|
|
9
|
+
box.tabIndex = -1;
|
|
10
|
+
box.setAttribute('aria-label', 'Done');
|
|
11
|
+
const contentDOM = document.createElement('div');
|
|
12
|
+
// Takes the row's width, so an empty item's caret and placeholder show.
|
|
13
|
+
contentDOM.className = 'min-w-0 flex-1';
|
|
14
|
+
dom.append(taskBoxHit(box), contentDOM);
|
|
15
|
+
dom.className = TASK_ROW;
|
|
16
|
+
dom.dataset.itemType = 'task';
|
|
17
|
+
let current = node;
|
|
18
|
+
const render = () => {
|
|
19
|
+
box.checked = Boolean(current.attrs.checked);
|
|
20
|
+
dom.dataset.checked = String(Boolean(current.attrs.checked));
|
|
21
|
+
};
|
|
22
|
+
render();
|
|
23
|
+
box.addEventListener('mousedown', (event) => event.preventDefault());
|
|
24
|
+
// The browser's own toggle is left to happen (cancelling a checkbox's
|
|
25
|
+
// click puts its old state back after this handler), and the document
|
|
26
|
+
// follows it.
|
|
27
|
+
box.addEventListener('click', () => {
|
|
28
|
+
const pos = getPos();
|
|
29
|
+
if (pos === undefined || !view.editable)
|
|
30
|
+
return;
|
|
31
|
+
view.dispatch(view.state.tr.setNodeMarkup(pos, undefined, { ...current.attrs, checked: !current.attrs.checked }));
|
|
32
|
+
});
|
|
33
|
+
return {
|
|
34
|
+
dom,
|
|
35
|
+
contentDOM,
|
|
36
|
+
update: (next) => {
|
|
37
|
+
if (next.type !== current.type || next.attrs.checked == null)
|
|
38
|
+
return false;
|
|
39
|
+
current = next;
|
|
40
|
+
render();
|
|
41
|
+
return true;
|
|
42
|
+
},
|
|
43
|
+
stopEvent: (event) => event.target === box || event.target === box.parentElement,
|
|
44
|
+
ignoreMutation: (mutation) => mutation.target === box || mutation.target === box.parentElement,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
// The task-list item the caret is in, ticked or unticked. False when the
|
|
48
|
+
// caret isn't in one.
|
|
49
|
+
export function toggleTaskItem(view) {
|
|
50
|
+
const { $from } = view.state.selection;
|
|
51
|
+
for (let depth = $from.depth; depth > 0; depth--) {
|
|
52
|
+
const node = $from.node(depth);
|
|
53
|
+
if (node.type.name === 'list_item' && node.attrs.checked != null) {
|
|
54
|
+
view.dispatch(view.state.tr.setNodeMarkup($from.before(depth), undefined, { ...node.attrs, checked: !node.attrs.checked }));
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
// - Picked: the slash menu's Toggle heading 1 to 3, and the toolbar's
|
|
11
11
|
// Toggle heading (rich-text-formats.ts).
|
|
12
12
|
// Only a toggle heading folds, and only it has a fold button
|
|
13
|
-
// (
|
|
13
|
+
// (rich-text-heading-view.ts, rich-text-fold.ts).
|
|
14
14
|
import { headingSchema } from '@milkdown/kit/preset/commonmark';
|
|
15
15
|
import { textblockTypeInputRule } from '@milkdown/kit/prose/inputrules';
|
|
16
16
|
import { $inputRule, $remark } from '@milkdown/kit/utils';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type RefObject } from 'react';
|
|
2
|
+
/** Where a step is in a sequence (StepTrackState). */
|
|
3
|
+
type StepState = 'done' | 'current' | 'upcoming';
|
|
4
|
+
/** A step's box: at least 4.5rem wide and 44px tall, sharing the row. */
|
|
5
|
+
export declare const STEP_CELL = "group/step relative flex min-h-11 min-w-18 flex-1 basis-0 flex-col items-center gap-1 rounded-md px-1 pt-1 pb-1.5 text-center text-xs text-muted-foreground";
|
|
6
|
+
/** The line from the step before, under the marks: the primary up to the current one. */
|
|
7
|
+
export declare const stepLine: (step: StepState) => string;
|
|
8
|
+
/** The row's scroll box: sideways in its own box, never the page. */
|
|
9
|
+
export declare const STEP_ROW = "relative flex max-w-full overflow-x-auto overflow-y-hidden [scrollbar-width:thin]";
|
|
10
|
+
/**
|
|
11
|
+
* Keeps the step `selector` finds in the row's view when the row scrolls
|
|
12
|
+
* sideways: the row only, never the page. Runs again when `key` changes.
|
|
13
|
+
*/
|
|
14
|
+
export declare function useKeepStepInView(rowRef: RefObject<HTMLElement | null>, selector: string, key: string | undefined): void;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// The step row's classes and its keep-in-view hook, shared by StepTrack
|
|
2
|
+
// (step-track.tsx) and the stepper look (equal-items.tsx), so the two rows
|
|
3
|
+
// never drift apart. Internal: never exported from the package's index.
|
|
4
|
+
import { useLayoutEffect } from 'react';
|
|
5
|
+
import { cn } from 'cn';
|
|
6
|
+
/** A step's box: at least 4.5rem wide and 44px tall, sharing the row. */
|
|
7
|
+
export const STEP_CELL = 'group/step relative flex min-h-11 min-w-18 flex-1 basis-0 flex-col items-center gap-1 rounded-md px-1 pt-1 pb-1.5 text-center text-xs text-muted-foreground';
|
|
8
|
+
/** The line from the step before, under the marks: the primary up to the current one. */
|
|
9
|
+
export const stepLine = (step) => cn('before:absolute before:top-4 before:right-1/2 before:h-0.5 before:w-full first:before:hidden', step === 'upcoming' ? 'before:bg-border' : 'before:bg-primary');
|
|
10
|
+
/** The row's scroll box: sideways in its own box, never the page. */
|
|
11
|
+
export const STEP_ROW = 'relative flex max-w-full overflow-x-auto overflow-y-hidden [scrollbar-width:thin]';
|
|
12
|
+
/**
|
|
13
|
+
* Keeps the step `selector` finds in the row's view when the row scrolls
|
|
14
|
+
* sideways: the row only, never the page. Runs again when `key` changes.
|
|
15
|
+
*/
|
|
16
|
+
export function useKeepStepInView(rowRef, selector, key) {
|
|
17
|
+
useLayoutEffect(() => {
|
|
18
|
+
const row = rowRef.current;
|
|
19
|
+
const step = row?.querySelector(selector);
|
|
20
|
+
if (!row || !step || row.scrollWidth <= row.clientWidth)
|
|
21
|
+
return;
|
|
22
|
+
const left = step.offsetLeft - row.offsetLeft;
|
|
23
|
+
if (left < row.scrollLeft || left + step.offsetWidth > row.scrollLeft + row.clientWidth) {
|
|
24
|
+
row.scrollLeft = left - (row.clientWidth - step.offsetWidth) / 2;
|
|
25
|
+
}
|
|
26
|
+
}, [rowRef, selector, key]);
|
|
27
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a step is in a sequence: `done` (a filled step with a check),
|
|
3
|
+
* `current` (ringed, "Current" or a caption under its name) or `upcoming`
|
|
4
|
+
* (outlined, the default).
|
|
5
|
+
*/
|
|
6
|
+
export type StepTrackState = 'done' | 'current' | 'upcoming';
|
|
7
|
+
/**
|
|
8
|
+
* One step. An `EqualItem` is one too (its `id`, `title`, `step` and
|
|
9
|
+
* `attention`), so a record's stepper items can be passed as they are.
|
|
10
|
+
*/
|
|
11
|
+
export type StepTrackStep = {
|
|
12
|
+
/** Unique in the track. */
|
|
13
|
+
id: string;
|
|
14
|
+
/** Its name ("Phase 2: Build"). Plain text. */
|
|
15
|
+
title: string;
|
|
16
|
+
/** Done, current or upcoming (the default). */
|
|
17
|
+
step?: StepTrackState;
|
|
18
|
+
/**
|
|
19
|
+
* A step that needs a look: a small warning dot on its mark, and these
|
|
20
|
+
* words at the end of its name ("needs an answer").
|
|
21
|
+
*/
|
|
22
|
+
attention?: string;
|
|
23
|
+
};
|
|
24
|
+
export type StepTrackProps = {
|
|
25
|
+
steps: readonly StepTrackStep[];
|
|
26
|
+
/** The list's name for screen readers ("Phases"). */
|
|
27
|
+
label: string;
|
|
28
|
+
/**
|
|
29
|
+
* A few words under the current step in place of "Current" ("Day 12").
|
|
30
|
+
* The step's name still says it's the current one.
|
|
31
|
+
*/
|
|
32
|
+
caption?: string;
|
|
33
|
+
/** What no steps says. Defaults to "Nothing yet". */
|
|
34
|
+
emptyText?: string;
|
|
35
|
+
className?: string;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* A step's numbered mark. `ring` is `active` for the stepper (a ring when
|
|
39
|
+
* its tab is the one shown) or `on` (the track's current step).
|
|
40
|
+
*/
|
|
41
|
+
export declare function StepMark({ step, number, attention, ring, slot, }: {
|
|
42
|
+
step: StepTrackState;
|
|
43
|
+
number: number;
|
|
44
|
+
attention: string | undefined;
|
|
45
|
+
ring: 'active' | 'on' | 'off';
|
|
46
|
+
slot: string;
|
|
47
|
+
}): import("react").JSX.Element;
|
|
48
|
+
/**
|
|
49
|
+
* A step's name: what it shows plus hidden words ("Step 2: Phase 2: Build,
|
|
50
|
+
* done"), never an aria-label, so the visible text stays in the name. The
|
|
51
|
+
* hidden words sit inside the clamped line, with no gap a block would add.
|
|
52
|
+
*/
|
|
53
|
+
export declare function StepName({ step, number, title, attention, current, }: {
|
|
54
|
+
step: StepTrackState;
|
|
55
|
+
number: number;
|
|
56
|
+
title: string;
|
|
57
|
+
attention: string | undefined;
|
|
58
|
+
/** Hidden ", current", where no visible "Current" says so. */
|
|
59
|
+
current?: boolean;
|
|
60
|
+
}): import("react").JSX.Element;
|
|
61
|
+
/**
|
|
62
|
+
* A read-only row of numbered steps (4.128): the stepper look's marks
|
|
63
|
+
* without its panels, for showing where a sequence is (a list card's
|
|
64
|
+
* phases), with a `caption` under the current step.
|
|
65
|
+
*
|
|
66
|
+
* ```tsx
|
|
67
|
+
* <StepTrack
|
|
68
|
+
* label="Phases"
|
|
69
|
+
* caption="Day 12"
|
|
70
|
+
* steps={[
|
|
71
|
+
* { id: 'p1', title: 'Phase 1', step: 'done' },
|
|
72
|
+
* { id: 'p2', title: 'Phase 2', step: 'current' },
|
|
73
|
+
* { id: 'p3', title: 'Phase 3' },
|
|
74
|
+
* ]}
|
|
75
|
+
* />
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* A named list, not a tablist: nothing is picked. To look at each step's
|
|
79
|
+
* details in turn, use `EqualItems look="stepper"` (design-language.md "A
|
|
80
|
+
* few equal things").
|
|
81
|
+
*/
|
|
82
|
+
export declare function StepTrack({ steps, label, caption, emptyText, className }: StepTrackProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
// A row of numbered steps joined by a line, each done, current or upcoming:
|
|
3
|
+
// the marks of `EqualItems look="stepper"` (4.127), on their own and read
|
|
4
|
+
// only (4.128). For a display of where a sequence is (a list card showing a
|
|
5
|
+
// program's phases, a caption such as "Day 12" under the current one),
|
|
6
|
+
// where nothing is picked and no panel shows.
|
|
7
|
+
//
|
|
8
|
+
// The stepper draws its steps from the same parts (StepMark and StepName
|
|
9
|
+
// below, the cell and line classes and the keep-in-view hook in
|
|
10
|
+
// step-row.ts), so the two never
|
|
11
|
+
// drift apart: change a mark here and both change.
|
|
12
|
+
//
|
|
13
|
+
// It is not interactive, so it's a named list (`<ol>`), never a tablist:
|
|
14
|
+
// each step is a list item whose text is what it shows plus hidden words
|
|
15
|
+
// ("Step 2: Phase 2: Build, done"). Wider than its column (a phone, many
|
|
16
|
+
// steps), the row scrolls sideways in its own box, never the page, with the
|
|
17
|
+
// current step in view; only while it overflows is the box a tab stop
|
|
18
|
+
// (axe's scrollable-region-focusable), so the arrow keys can scroll it.
|
|
19
|
+
import { useEffect, useRef, useState } from 'react';
|
|
20
|
+
import { CheckIcon } from 'lucide-react';
|
|
21
|
+
import { cn } from 'cn';
|
|
22
|
+
import { STEP_CELL, STEP_ROW, stepLine, useKeepStepInView } from './step-row.js';
|
|
23
|
+
// The stepper's and the track's shared marks (step-row.ts has the row's
|
|
24
|
+
// classes and its keep-in-view hook). Internal: exported for
|
|
25
|
+
// equal-items.tsx only, never from the package's index.
|
|
26
|
+
/**
|
|
27
|
+
* A step's numbered mark. `ring` is `active` for the stepper (a ring when
|
|
28
|
+
* its tab is the one shown) or `on` (the track's current step).
|
|
29
|
+
*/
|
|
30
|
+
export function StepMark({ step, number, attention, ring, slot, }) {
|
|
31
|
+
return (_jsxs("span", { "aria-hidden": "true", "data-slot": `${slot}-mark`, className: cn('relative z-10 grid size-6.5 shrink-0 place-items-center rounded-full border-2 text-xs font-bold tabular-nums', step === 'done'
|
|
32
|
+
? 'border-primary bg-primary text-primary-foreground'
|
|
33
|
+
: step === 'current'
|
|
34
|
+
? 'border-primary bg-card text-foreground'
|
|
35
|
+
: 'border-border bg-card text-muted-foreground', ring === 'active' &&
|
|
36
|
+
'group-data-active/step:ring-2 group-data-active/step:ring-ring group-data-active/step:ring-offset-2 group-data-active/step:ring-offset-card', ring === 'on' && 'ring-2 ring-ring ring-offset-2 ring-offset-card'), children: [step === 'done' ? _jsx(CheckIcon, { className: "size-3.5" }) : number, attention && (_jsx("span", { "data-slot": `${slot}-attention`, className: "absolute -top-0.5 -right-0.5 size-2.5 rounded-full border border-card bg-warning-text" }))] }));
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* A step's name: what it shows plus hidden words ("Step 2: Phase 2: Build,
|
|
40
|
+
* done"), never an aria-label, so the visible text stays in the name. The
|
|
41
|
+
* hidden words sit inside the clamped line, with no gap a block would add.
|
|
42
|
+
*/
|
|
43
|
+
export function StepName({ step, number, title, attention, current, }) {
|
|
44
|
+
return (_jsxs("span", { className: "line-clamp-3 max-w-full min-w-0 break-words", children: [_jsx("span", { className: "sr-only", children: `Step ${number}: ` }), title, step === 'done' && _jsx("span", { className: "sr-only", children: ", done" }), current && _jsx("span", { className: "sr-only", children: ", current" }), attention && _jsx("span", { className: "sr-only", children: `, ${attention}` })] }));
|
|
45
|
+
}
|
|
46
|
+
// Whether the row is wider than its box, followed as either changes size.
|
|
47
|
+
function useOverflows(rowRef) {
|
|
48
|
+
const [overflows, setOverflows] = useState(false);
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
const row = rowRef.current;
|
|
51
|
+
if (!row)
|
|
52
|
+
return;
|
|
53
|
+
const check = () => setOverflows(row.scrollWidth > row.clientWidth + 1);
|
|
54
|
+
check();
|
|
55
|
+
if (typeof ResizeObserver === 'undefined')
|
|
56
|
+
return;
|
|
57
|
+
const observer = new ResizeObserver(check);
|
|
58
|
+
observer.observe(row);
|
|
59
|
+
return () => observer.disconnect();
|
|
60
|
+
}, [rowRef]);
|
|
61
|
+
return overflows;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* A read-only row of numbered steps (4.128): the stepper look's marks
|
|
65
|
+
* without its panels, for showing where a sequence is (a list card's
|
|
66
|
+
* phases), with a `caption` under the current step.
|
|
67
|
+
*
|
|
68
|
+
* ```tsx
|
|
69
|
+
* <StepTrack
|
|
70
|
+
* label="Phases"
|
|
71
|
+
* caption="Day 12"
|
|
72
|
+
* steps={[
|
|
73
|
+
* { id: 'p1', title: 'Phase 1', step: 'done' },
|
|
74
|
+
* { id: 'p2', title: 'Phase 2', step: 'current' },
|
|
75
|
+
* { id: 'p3', title: 'Phase 3' },
|
|
76
|
+
* ]}
|
|
77
|
+
* />
|
|
78
|
+
* ```
|
|
79
|
+
*
|
|
80
|
+
* A named list, not a tablist: nothing is picked. To look at each step's
|
|
81
|
+
* details in turn, use `EqualItems look="stepper"` (design-language.md "A
|
|
82
|
+
* few equal things").
|
|
83
|
+
*/
|
|
84
|
+
export function StepTrack({ steps, label, caption, emptyText = 'Nothing yet', className }) {
|
|
85
|
+
const rowRef = useRef(null);
|
|
86
|
+
const currentId = steps.find((step) => step.step === 'current')?.id;
|
|
87
|
+
useKeepStepInView(rowRef, '[data-slot="step-track-step"][aria-current]', currentId);
|
|
88
|
+
const overflows = useOverflows(rowRef);
|
|
89
|
+
if (steps.length === 0) {
|
|
90
|
+
return (_jsx("p", { "data-slot": "step-track", "data-state": "empty", className: cn('type-body text-muted-foreground', className), children: emptyText }));
|
|
91
|
+
}
|
|
92
|
+
return (_jsx("ol", { ref: rowRef, "data-slot": "step-track", "aria-label": label, tabIndex: overflows ? 0 : undefined, className: cn(STEP_ROW, 'm-0 list-none p-0 outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50', className), children: steps.map((item, index) => {
|
|
93
|
+
const step = item.step ?? 'upcoming';
|
|
94
|
+
const current = step === 'current';
|
|
95
|
+
const showCaption = current && caption !== undefined && caption !== '';
|
|
96
|
+
return (_jsxs("li", { "data-slot": "step-track-step", "data-step": step, "aria-current": current ? 'step' : undefined, className: cn(STEP_CELL, stepLine(step), current && 'font-semibold text-foreground'), children: [_jsx(StepMark, { step: step, number: index + 1, attention: item.attention, ring: current ? 'on' : 'off', slot: "step-track-step" }), _jsx(StepName, { step: step, number: index + 1, title: item.title, attention: item.attention, current: showCaption }), current && (_jsx("span", { "data-slot": "step-track-caption", className: "font-normal text-muted-foreground", children: showCaption ? caption : 'Current' }))] }, item.id));
|
|
97
|
+
}) }));
|
|
98
|
+
}
|
|
@@ -15,7 +15,7 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
15
15
|
// - Toasts last 4 seconds, Undo too. An error from `notify.failed` stays
|
|
16
16
|
// until it's closed.
|
|
17
17
|
//
|
|
18
|
-
// What a toast says and when one shows: `notify` (components/app/notify.
|
|
18
|
+
// What a toast says and when one shows: `notify` (components/app/notify.tsx).
|
|
19
19
|
import { useTheme } from "next-themes";
|
|
20
20
|
import { Toaster as Sonner } from "sonner";
|
|
21
21
|
import { CircleCheckIcon, InfoIcon, TriangleAlertIcon, OctagonXIcon, Loader2Icon } from "lucide-react";
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { AppError } from '../api/contracts.js';
|
|
2
|
+
type RecordId = string | number;
|
|
3
|
+
/** What EntityForm's and FormDialog's `conflict` take: a save refused because the record changed elsewhere. */
|
|
4
|
+
export type FormConflict = {
|
|
5
|
+
/** Save what's typed over the newer version (the form submits again). */
|
|
6
|
+
onKeepMine: () => void;
|
|
7
|
+
/** Drop what's typed and show the newer version. */
|
|
8
|
+
onShowSaved: () => void;
|
|
9
|
+
/** The newer version is still being fetched: both wait for it. */
|
|
10
|
+
loading?: boolean;
|
|
11
|
+
};
|
|
12
|
+
export type FormVersionOptions<TRecord extends {
|
|
13
|
+
id: RecordId;
|
|
14
|
+
}, TUpdate> = {
|
|
15
|
+
/** The record as the form's query has it now (its `data`); undefined on a create form or while loading. */
|
|
16
|
+
record: TRecord | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Adds the version to what's sent: `(update, widget) => ({ ...update,
|
|
19
|
+
* baseUpdatedAt: widget.updatedAt })`, the same function the entity's
|
|
20
|
+
* `useRecordUpdate` takes.
|
|
21
|
+
*/
|
|
22
|
+
withVersion: (update: TUpdate, record: TRecord) => TUpdate;
|
|
23
|
+
/** Fetches the record again (the query's `refetch`), after a 409, so Keep Mine and Show Saved have the newer version. */
|
|
24
|
+
refetch: () => Promise<unknown> | unknown;
|
|
25
|
+
};
|
|
26
|
+
type Phase = 'none' | 'loading' | 'ready';
|
|
27
|
+
type Snapshot<TRecord> = {
|
|
28
|
+
/** The record the form shows: what it opened with, or what Show Saved took. */
|
|
29
|
+
opened: TRecord | undefined;
|
|
30
|
+
phase: Phase;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* The state behind `useFormVersion`, outside React (exported for tests).
|
|
34
|
+
* `latest` is the query's record, kept current by the hook; `opened` the
|
|
35
|
+
* one the form's values came from; `base` the one the next save is built
|
|
36
|
+
* on (Keep Mine moves it to the newer version without touching `opened`).
|
|
37
|
+
*/
|
|
38
|
+
export declare class FormVersionStore<TRecord extends {
|
|
39
|
+
id: RecordId;
|
|
40
|
+
}, TUpdate> {
|
|
41
|
+
private latest;
|
|
42
|
+
private base;
|
|
43
|
+
private snapshot;
|
|
44
|
+
private listeners;
|
|
45
|
+
private turn;
|
|
46
|
+
private withVersion;
|
|
47
|
+
private refetch;
|
|
48
|
+
constructor(withVersion: (update: TUpdate, record: TRecord) => TUpdate, refetch: () => Promise<unknown> | unknown);
|
|
49
|
+
subscribe: (listener: () => void) => () => boolean;
|
|
50
|
+
getSnapshot: () => Snapshot<TRecord>;
|
|
51
|
+
private set;
|
|
52
|
+
setOptions(withVersion: FormVersionStore<TRecord, TUpdate>['withVersion'], refetch: () => Promise<unknown> | unknown): void;
|
|
53
|
+
/**
|
|
54
|
+
* The query's record now. The first one (or one with another id: the
|
|
55
|
+
* form moved to another record) is the one the form opens with; later
|
|
56
|
+
* ones are only remembered, for Keep Mine and Show Saved.
|
|
57
|
+
*/
|
|
58
|
+
sync(record: TRecord | undefined): void;
|
|
59
|
+
/** The record the form shows, given the query's record this render (before `sync` has run). */
|
|
60
|
+
shown(record: TRecord | undefined): TRecord | undefined;
|
|
61
|
+
/** `update` with the version the form's values were built on. No record yet (a create): unchanged. */
|
|
62
|
+
send(update: TUpdate): TUpdate;
|
|
63
|
+
/** A save was refused: true when it's a conflict, which this now shows. */
|
|
64
|
+
refused(error: AppError | unknown): boolean;
|
|
65
|
+
/** Keep Mine: the next save is built on the newer version; what's typed stays. */
|
|
66
|
+
keepMine(): void;
|
|
67
|
+
/** Show Saved: the form takes the newer record (its values reset to it). */
|
|
68
|
+
showSaved(): void;
|
|
69
|
+
/** A save went through: the next one builds on what it wrote. What the form shows is unchanged. */
|
|
70
|
+
saved(record: TRecord): void;
|
|
71
|
+
/** EntityForm's / FormDialog's `conflict` while one stands. */
|
|
72
|
+
conflict(): FormConflict | undefined;
|
|
73
|
+
}
|
|
74
|
+
export type FormVersion<TRecord, TUpdate> = {
|
|
75
|
+
/** The record the form was opened with (or Show Saved took): the form's `values` come from it. */
|
|
76
|
+
record: TRecord | undefined;
|
|
77
|
+
/** What to send: `update` with that record's version. */
|
|
78
|
+
send: (update: TUpdate) => TUpdate;
|
|
79
|
+
/** First in the save's `onError`: true when the save was refused as a conflict, which `conflict` now shows. */
|
|
80
|
+
refused: (error: AppError) => boolean;
|
|
81
|
+
/** In the save's `onSuccess` when the form stays open: the next save builds on this one. */
|
|
82
|
+
saved: (record: TRecord) => void;
|
|
83
|
+
/** EntityForm's or FormDialog's `conflict`. */
|
|
84
|
+
conflict: FormConflict | undefined;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* An edit form's version check (see the top of this file):
|
|
88
|
+
*
|
|
89
|
+
* ```tsx
|
|
90
|
+
* const version = useFormVersion({ record: widgetQuery.data, withVersion, refetch: widgetQuery.refetch })
|
|
91
|
+
* useForm({ values: version.record && widgetToFormValues(version.record) })
|
|
92
|
+
* updateWidget.mutate(version.send(formValuesToWidgetUpdate(values)), {
|
|
93
|
+
* onError: (error) => { if (!version.refused(error)) setSubmitError(error) },
|
|
94
|
+
* })
|
|
95
|
+
* <EntityForm conflict={version.conflict} … />
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export declare function useFormVersion<TRecord extends {
|
|
99
|
+
id: RecordId;
|
|
100
|
+
}, TUpdate>({ record, withVersion, refetch, }: FormVersionOptions<TRecord, TUpdate>): FormVersion<TRecord, TUpdate>;
|
|
101
|
+
export {};
|