@openartifactsai/ui 0.0.0-stage → 0.23.0-rc.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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/package.json +82 -4
- package/src/button.d.ts +30 -0
- package/src/button.js +29 -0
- package/src/clicks.d.ts +63 -0
- package/src/clicks.js +75 -0
- package/src/cn.d.ts +3 -0
- package/src/cn.js +5 -0
- package/src/color-picker.d.ts +98 -0
- package/src/color-picker.js +395 -0
- package/src/color.d.ts +150 -0
- package/src/color.js +543 -0
- package/src/crash-boundary.d.ts +29 -0
- package/src/crash-boundary.js +32 -0
- package/src/data/google-fonts.json +1775 -0
- package/src/drag-cover.d.ts +48 -0
- package/src/drag-cover.js +41 -0
- package/src/effect-editors.d.ts +48 -0
- package/src/effect-editors.js +230 -0
- package/src/effects.d.ts +118 -0
- package/src/effects.js +425 -0
- package/src/escape-to-popup.d.ts +53 -0
- package/src/escape-to-popup.js +78 -0
- package/src/field-announce.d.ts +28 -0
- package/src/field-announce.js +43 -0
- package/src/fields.d.ts +176 -0
- package/src/fields.js +377 -0
- package/src/focusables.d.ts +50 -0
- package/src/focusables.js +61 -0
- package/src/font-picker.d.ts +62 -0
- package/src/font-picker.js +242 -0
- package/src/fonts.d.ts +121 -0
- package/src/fonts.js +273 -0
- package/src/history-diff.d.ts +64 -0
- package/src/history-diff.js +151 -0
- package/src/history.d.ts +95 -0
- package/src/history.js +160 -0
- package/src/index.d.ts +38 -0
- package/src/index.js +38 -0
- package/src/key-chord.d.ts +74 -0
- package/src/key-chord.js +228 -0
- package/src/keys.d.ts +142 -0
- package/src/keys.js +172 -0
- package/src/layout.d.ts +74 -0
- package/src/layout.js +173 -0
- package/src/link-card-view.d.ts +97 -0
- package/src/link-card-view.js +175 -0
- package/src/link-card.d.ts +136 -0
- package/src/link-card.js +158 -0
- package/src/menu-shortcuts.d.ts +31 -0
- package/src/menu-shortcuts.js +91 -0
- package/src/menu.d.ts +120 -0
- package/src/menu.js +100 -0
- package/src/number-entry.d.ts +140 -0
- package/src/number-entry.js +202 -0
- package/src/number-limits.d.ts +2 -0
- package/src/number-limits.js +2 -0
- package/src/number-wheel.d.ts +29 -0
- package/src/number-wheel.js +27 -0
- package/src/numbers.d.ts +28 -0
- package/src/numbers.js +124 -0
- package/src/overlays.d.ts +71 -0
- package/src/overlays.js +131 -0
- package/src/pressed.d.ts +5 -0
- package/src/pressed.js +10 -0
- package/src/scroll.d.ts +24 -0
- package/src/scroll.js +111 -0
- package/src/scrub-number-field.d.ts +79 -0
- package/src/scrub-number-field.js +254 -0
- package/src/select-announce.d.ts +50 -0
- package/src/select-announce.js +71 -0
- package/src/slider-session.d.ts +43 -0
- package/src/slider-session.js +39 -0
- package/src/styles.css +96 -0
- package/src/tab-strip-layout.d.ts +76 -0
- package/src/tab-strip-layout.js +111 -0
- package/src/tab-strip.d.ts +73 -0
- package/src/tab-strip.js +498 -0
- package/src/text-area-view.d.ts +35 -0
- package/src/text-area-view.js +142 -0
- package/src/text-area.d.ts +38 -0
- package/src/text-area.js +51 -0
- package/src/theme.css +34 -0
- package/src/toast-fit.d.ts +21 -0
- package/src/toast-fit.js +30 -0
- package/src/toast-tone.d.ts +27 -0
- package/src/toast-tone.js +22 -0
- package/src/tooltip.d.ts +23 -0
- package/src/tooltip.js +18 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* The link cards.
|
|
4
|
+
*
|
|
5
|
+
* - {@link LinkCard}: the card at a link in rich text being edited. It
|
|
6
|
+
* shows the address, and Open, Copy link, Edit and Remove. Editing the
|
|
7
|
+
* address saves on Enter and puts it back on Escape; Escape on the card
|
|
8
|
+
* gives the focus back to the text. The editor brings what differs: what
|
|
9
|
+
* a typed address becomes (`normalize`), how it is copied and how the
|
|
10
|
+
* link changes.
|
|
11
|
+
* - {@link LinkDetailsCard}: the read-only card that shows while the
|
|
12
|
+
* pointer rests on a link in a page being viewed: the address (itself a
|
|
13
|
+
* link that opens in a new tab) and Copy link.
|
|
14
|
+
*/
|
|
15
|
+
import { Check, Copy, ExternalLink, Pencil, Unlink } from 'lucide-react';
|
|
16
|
+
import { forwardRef, useEffect, useLayoutEffect, useRef, useState } from 'react';
|
|
17
|
+
import { IconButton } from "./button.js";
|
|
18
|
+
import { cn } from "./cn.js";
|
|
19
|
+
import { INPUT_CLASS } from "./fields.js";
|
|
20
|
+
import { isComposingKey } from "./keys.js";
|
|
21
|
+
import { canOpenLink, LINK_COPIED_MS, LINK_CARD_SETTLE_MS, linkAddressParts, linkCardPlace, MAX_SHOWN_ADDRESS_CHARS, placeLinkCard, screenBox, shownLink, } from "./link-card.js";
|
|
22
|
+
import { Tooltip } from "./tooltip.js";
|
|
23
|
+
/** Said once the address is on the clipboard. */
|
|
24
|
+
export const LINK_COPIED_MESSAGE = 'Link copied';
|
|
25
|
+
/** Said when copying the address failed. */
|
|
26
|
+
export const LINK_COPY_FAILED_MESSAGE = 'The link couldn’t be copied. Try again.';
|
|
27
|
+
/** Under an address the card can't open. */
|
|
28
|
+
export const LINK_BLOCKED_MESSAGE = 'This link can’t be opened from here.';
|
|
29
|
+
/** What the address link does, for screen readers. */
|
|
30
|
+
export const LINK_OPENS_NEW_TAB = 'Opens in a new tab';
|
|
31
|
+
/** The size a card is taken to have before it is measured. */
|
|
32
|
+
export const LINK_CARD_ESTIMATE = { w: 500, h: 44 };
|
|
33
|
+
/** The card starts this far left of the link (screen pixels), so its address lines up with the link's text. */
|
|
34
|
+
export const LINK_CARD_SHIFT_X_PX = -8;
|
|
35
|
+
const keepTextFocus = (event) => event.preventDefault();
|
|
36
|
+
export function LinkCard({ link, target, href, normalize, invalidMessage = 'Not a link address', onCopy, onEdit, onRemove, onReturn, above = false, avoid, editOnOpen = false, className }) {
|
|
37
|
+
const card = useRef(null);
|
|
38
|
+
const [editing, setEditing] = useState(editOnOpen);
|
|
39
|
+
const [draft, setDraft] = useState(editOnOpen ? target : '');
|
|
40
|
+
const [invalid, setInvalid] = useState(false);
|
|
41
|
+
const [place, setPlace] = useState(null);
|
|
42
|
+
const shown = shownLink(target);
|
|
43
|
+
const openable = canOpenLink(href);
|
|
44
|
+
useLayoutEffect(() => {
|
|
45
|
+
const measure = () => {
|
|
46
|
+
if (!card.current || !link.isConnected)
|
|
47
|
+
return;
|
|
48
|
+
const box = card.current.getBoundingClientRect();
|
|
49
|
+
if (above) {
|
|
50
|
+
const place = linkCardPlace(screenBox(link.getBoundingClientRect()), { w: box.width, h: box.height }, { w: window.innerWidth, h: window.innerHeight }, avoid?.() ?? null);
|
|
51
|
+
setPlace({ top: place.y, left: place.x });
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
setPlace(placeLinkCard(link.getBoundingClientRect(), box, { width: window.innerWidth, height: window.innerHeight }));
|
|
55
|
+
};
|
|
56
|
+
measure();
|
|
57
|
+
window.addEventListener('scroll', measure, true);
|
|
58
|
+
window.addEventListener('resize', measure);
|
|
59
|
+
return () => {
|
|
60
|
+
window.removeEventListener('scroll', measure, true);
|
|
61
|
+
window.removeEventListener('resize', measure);
|
|
62
|
+
};
|
|
63
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
64
|
+
}, [link, editing, above]);
|
|
65
|
+
const startEdit = () => {
|
|
66
|
+
setDraft(target);
|
|
67
|
+
setInvalid(false);
|
|
68
|
+
setEditing(true);
|
|
69
|
+
};
|
|
70
|
+
const save = () => {
|
|
71
|
+
const next = normalize(draft);
|
|
72
|
+
if (next === null) {
|
|
73
|
+
setInvalid(true);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (next !== target && !onEdit(next))
|
|
77
|
+
return;
|
|
78
|
+
setEditing(false);
|
|
79
|
+
if (next === target)
|
|
80
|
+
onReturn();
|
|
81
|
+
};
|
|
82
|
+
return (_jsx("div", { ref: card, role: "dialog", "aria-label": "Link", "data-link-card": "", className: cn('fixed z-50 flex max-w-[min(420px,calc(100vw-12px))] items-center gap-1 rounded-control border border-line bg-surface p-1 text-[12px] shadow-lg', className), style: place ? { top: place.top, left: place.left } : { top: -9999, left: -9999 }, onKeyDown: (event) => {
|
|
83
|
+
if (event.key === 'Escape' && !isComposingKey(event.nativeEvent)) {
|
|
84
|
+
event.preventDefault();
|
|
85
|
+
event.stopPropagation();
|
|
86
|
+
if (editing)
|
|
87
|
+
setEditing(false);
|
|
88
|
+
onReturn();
|
|
89
|
+
}
|
|
90
|
+
}, children: editing ? (_jsxs(_Fragment, { children: [_jsx("input", { autoFocus: true, "aria-label": "Link address", "aria-invalid": invalid || undefined, value: draft, spellCheck: false, className: cn(INPUT_CLASS, 'h-7 w-72 font-mono text-[12px]', invalid && 'border-danger'), onChange: (event) => {
|
|
91
|
+
setDraft(event.target.value);
|
|
92
|
+
setInvalid(false);
|
|
93
|
+
}, onKeyDown: (event) => {
|
|
94
|
+
if (event.key !== 'Enter' || isComposingKey(event.nativeEvent))
|
|
95
|
+
return;
|
|
96
|
+
event.preventDefault();
|
|
97
|
+
save();
|
|
98
|
+
} }), invalid ? _jsx("span", { role: "alert", className: "px-1 text-danger", children: invalidMessage }) : null] })) : (_jsxs(_Fragment, { children: [_jsxs("span", { className: "min-w-0 flex-1 truncate px-1.5", title: target, children: [_jsx("span", { className: "text-fg", children: shown.site }), _jsx("span", { className: "text-fg-subtle", children: shown.rest })] }), openable ? (_jsx(IconButton, { size: "sm", label: "Open link", icon: _jsx(ExternalLink, { className: "size-3.5" }), onMouseDown: keepTextFocus, onClick: () => window.open(href, '_blank', 'noopener,noreferrer') })) : null, _jsx(IconButton, { size: "sm", label: "Copy link", icon: _jsx(Copy, { className: "size-3.5" }), onMouseDown: keepTextFocus, onClick: () => onCopy(target) }), _jsx(IconButton, { size: "sm", label: "Edit link", icon: _jsx(Pencil, { className: "size-3.5" }), onClick: startEdit }), _jsx(IconButton, { size: "sm", label: "Remove link", icon: _jsx(Unlink, { className: "size-3.5" }), onMouseDown: keepTextFocus, onClick: onRemove })] })) }));
|
|
99
|
+
}
|
|
100
|
+
/** The card's controls, in Tab order. */
|
|
101
|
+
function cardStops(card) {
|
|
102
|
+
return [...card.querySelectorAll('a[href], button:not([disabled]), [tabindex="0"]')];
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* The read-only link card: a group named "Link details" with the address on
|
|
106
|
+
* one line (the site in full, the path after it lighter and cut from its
|
|
107
|
+
* end; a site too long for the card is cut from its start), and Copy link.
|
|
108
|
+
* The address is a link that opens in a new tab; one that can't be opened
|
|
109
|
+
* from here is greyed out and says so under it. It sits above the link,
|
|
110
|
+
* 6px away, or below it when above does not fit, and
|
|
111
|
+
* takes no clicks for {@link LINK_CARD_SETTLE_MS} after it lands somewhere.
|
|
112
|
+
*/
|
|
113
|
+
export const LinkDetailsCard = forwardRef(function LinkDetailsCard({ href, anchor, avoid = null, openable = canOpenLink(href), onOpen, onCopy, onClose, onTabOut, className, style, onKeyDown, ...rest }, ref) {
|
|
114
|
+
const root = useRef(null);
|
|
115
|
+
const address = useRef(null);
|
|
116
|
+
const [place, setPlace] = useState(() => linkCardPlace(anchor, LINK_CARD_ESTIMATE, { w: window.innerWidth, h: window.innerHeight }, avoid, LINK_CARD_SHIFT_X_PX));
|
|
117
|
+
const [settling, setSettling] = useState(true);
|
|
118
|
+
const [copied, setCopied] = useState(false);
|
|
119
|
+
const [cut, setCut] = useState(false);
|
|
120
|
+
const parts = linkAddressParts(href, { trimWww: true, dropControls: true });
|
|
121
|
+
const fullText = href.replace(/[\p{Cc}\p{Cf}]/gu, '');
|
|
122
|
+
useLayoutEffect(() => {
|
|
123
|
+
const card = root.current;
|
|
124
|
+
if (!card)
|
|
125
|
+
return;
|
|
126
|
+
setPlace(linkCardPlace(anchor, { w: card.offsetWidth, h: card.offsetHeight }, { w: window.innerWidth, h: window.innerHeight }, avoid, LINK_CARD_SHIFT_X_PX));
|
|
127
|
+
const shown = address.current;
|
|
128
|
+
setCut([...fullText].length > MAX_SHOWN_ADDRESS_CHARS || (shown !== null && [...shown.querySelectorAll('[data-link-part]')].some((part) => part.scrollWidth > part.clientWidth)));
|
|
129
|
+
}, [anchor.x, anchor.y, anchor.w, anchor.h, avoid?.x, avoid?.y, avoid?.w, avoid?.h, href]);
|
|
130
|
+
// A card that just landed somewhere takes no clicks for a moment.
|
|
131
|
+
useEffect(() => {
|
|
132
|
+
setSettling(true);
|
|
133
|
+
const timer = setTimeout(() => setSettling(false), LINK_CARD_SETTLE_MS);
|
|
134
|
+
return () => clearTimeout(timer);
|
|
135
|
+
}, [place.x, place.y]);
|
|
136
|
+
useEffect(() => {
|
|
137
|
+
if (!copied)
|
|
138
|
+
return;
|
|
139
|
+
const timer = setTimeout(() => setCopied(false), LINK_COPIED_MS);
|
|
140
|
+
return () => clearTimeout(timer);
|
|
141
|
+
}, [copied]);
|
|
142
|
+
const keyDown = (event) => {
|
|
143
|
+
onKeyDown?.(event);
|
|
144
|
+
if (event.defaultPrevented || isComposingKey(event.nativeEvent))
|
|
145
|
+
return;
|
|
146
|
+
if (event.key === 'Escape' && onClose) {
|
|
147
|
+
event.preventDefault();
|
|
148
|
+
event.stopPropagation();
|
|
149
|
+
onClose();
|
|
150
|
+
}
|
|
151
|
+
else if (event.key === 'Tab' && onTabOut && root.current) {
|
|
152
|
+
const stops = cardStops(root.current);
|
|
153
|
+
const at = stops.findIndex((stop) => stop === event.target || stop.contains(event.target));
|
|
154
|
+
const leaving = event.shiftKey ? at <= 0 : at === stops.length - 1;
|
|
155
|
+
if (leaving && onTabOut(event.shiftKey))
|
|
156
|
+
event.preventDefault();
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
const copy = async () => {
|
|
160
|
+
if (await onCopy(href))
|
|
161
|
+
setCopied(true);
|
|
162
|
+
};
|
|
163
|
+
const site = (_jsx("span", { "data-link-part": "", dir: "rtl", className: "min-w-0 shrink truncate text-left font-medium text-fg", children: _jsx("bdi", { dir: "ltr", children: parts.host }) }));
|
|
164
|
+
const path = parts.path !== '' ? (_jsx("span", { "data-link-part": "", className: "min-w-[1.3em] truncate text-fg-subtle", style: { flex: '0 1000 auto' }, children: parts.path })) : null;
|
|
165
|
+
const addressClass = 'flex min-w-0 flex-1 items-baseline rounded-control px-1.5 py-0.5 text-[13px] leading-5 outline-none focus-visible:outline-2 focus-visible:outline-accent';
|
|
166
|
+
const row = !openable ? (_jsxs("span", { ref: address, role: "link", "aria-disabled": "true", tabIndex: 0, "aria-label": fullText, className: cn(addressClass, 'opacity-50'), children: [site, path] })) : onOpen ? (_jsxs("button", { ref: (node) => { address.current = node; }, type: "button", "aria-label": fullText, "aria-description": LINK_OPENS_NEW_TAB, className: cn(addressClass, 'text-left hover:bg-hover'), onClick: () => onOpen(href), children: [site, path] })) : (_jsxs("a", { ref: (node) => { address.current = node; }, href: href, target: "_blank", rel: "noopener noreferrer", "aria-label": fullText, "aria-description": LINK_OPENS_NEW_TAB, className: cn(addressClass, 'hover:bg-hover'), children: [site, path] }));
|
|
167
|
+
const cardStyle = { ...style, top: place.y, left: place.x, pointerEvents: settling ? 'none' : style?.pointerEvents };
|
|
168
|
+
return (_jsxs("div", { ref: (node) => {
|
|
169
|
+
root.current = node;
|
|
170
|
+
if (typeof ref === 'function')
|
|
171
|
+
ref(node);
|
|
172
|
+
else if (ref)
|
|
173
|
+
ref.current = node;
|
|
174
|
+
}, role: "group", "aria-label": "Link details", "data-link-details": "", "data-side": place.side, className: cn('fixed z-50 flex max-w-[min(500px,calc(100vw-16px))] flex-col rounded-control border border-line bg-surface p-1 shadow-lg', className), style: cardStyle, onKeyDown: keyDown, ...rest, children: [_jsxs("div", { className: "flex min-w-0 items-center gap-1", children: [cut ? _jsx(Tooltip, { label: fullText, children: row }) : row, _jsx("span", { "aria-hidden": "true", className: "mx-0.5 h-4 w-px shrink-0 bg-line" }), _jsx(IconButton, { size: "sm", label: "Copy link", icon: copied ? _jsx(Check, { className: "size-3.5" }) : _jsx(Copy, { className: "size-3.5" }), onClick: () => void copy() })] }), !openable ? _jsx("p", { className: "px-1.5 pb-0.5 text-[12px] text-fg-subtle", children: LINK_BLOCKED_MESSAGE }) : null] }));
|
|
175
|
+
});
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rules behind the link cards: how a card shows an address, what it may
|
|
3
|
+
* open, where it sits, and the timing of a card that shows while the
|
|
4
|
+
* pointer rests on a link. Pure. What a typed address becomes is the
|
|
5
|
+
* editor's own rule (see {@link LinkCard}).
|
|
6
|
+
*/
|
|
7
|
+
/** The card shows this long (ms) after the pointer rests on a link (or the caret in one). */
|
|
8
|
+
export declare const LINK_CARD_SHOW_MS = 300;
|
|
9
|
+
/** The card hides this long (ms) after the pointer leaves the link and the card. */
|
|
10
|
+
export declare const LINK_CARD_HIDE_MS = 300;
|
|
11
|
+
/** After a link opened, its card shows again only after this long (ms). */
|
|
12
|
+
export declare const LINK_CARD_COOLDOWN_MS = 700;
|
|
13
|
+
/** A card that just showed in a new place takes no clicks for this long (ms), so a click meant for what was there misses it. */
|
|
14
|
+
export declare const LINK_CARD_SETTLE_MS = 250;
|
|
15
|
+
/** How often (ms) a card that can't watch every frame checks that its link stayed put. */
|
|
16
|
+
export declare const LINK_CARD_WATCH_MS = 150;
|
|
17
|
+
/** How long "Link copied" stays (ms). */
|
|
18
|
+
export declare const LINK_COPIED_MS = 1500;
|
|
19
|
+
/** A touch counts as a tap when it lasts at most this long (ms)… */
|
|
20
|
+
export declare const TAP_MAX_MS = 400;
|
|
21
|
+
/** …and moves at most this far (screen pixels). */
|
|
22
|
+
export declare const TAP_MAX_MOVE_PX = 10;
|
|
23
|
+
/** A mouse or pen press becomes a drag past this distance (screen pixels). */
|
|
24
|
+
export declare const CLICK_MAX_MOVE_PX = 4;
|
|
25
|
+
/** How long to wait before showing a card, given when a link last opened (ms timestamps). */
|
|
26
|
+
export declare function linkCardShowDelay(now: number, lastOpened: number | null): number;
|
|
27
|
+
/** What a click on a link does: open it now, the editor's own plain click, or nothing. */
|
|
28
|
+
export type LinkClickAction = 'now' | 'later' | 'none';
|
|
29
|
+
/** The modifier keys of a click. */
|
|
30
|
+
export interface ClickModifiers {
|
|
31
|
+
metaKey: boolean;
|
|
32
|
+
ctrlKey: boolean;
|
|
33
|
+
shiftKey: boolean;
|
|
34
|
+
altKey: boolean;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* What a click on a link does: Mod (⌘ on Apple platforms, Ctrl elsewhere)
|
|
38
|
+
* without Shift or Alt opens at once (`now`); a click without any modifier
|
|
39
|
+
* is a plain click (`later`: the editor follows it, or waits out a double
|
|
40
|
+
* click first); anything else (Shift picks, Alt copies, Ctrl on a Mac opens
|
|
41
|
+
* the context menu) leaves it (`none`).
|
|
42
|
+
*/
|
|
43
|
+
export declare function linkClickAction(click: ClickModifiers, apple: boolean): LinkClickAction;
|
|
44
|
+
/** One press, from going down to coming up. */
|
|
45
|
+
export interface PressSpan {
|
|
46
|
+
pointerType: string;
|
|
47
|
+
/** How long it lasted, ms. */
|
|
48
|
+
ms: number;
|
|
49
|
+
/** How far it got from where it went down, screen pixels. */
|
|
50
|
+
moved: number;
|
|
51
|
+
}
|
|
52
|
+
/** Whether a press counts as a click: a short, still tap on touch (or with a stylus on a touch screen, `touch-pen`); a press that never became a drag otherwise. */
|
|
53
|
+
export declare function pressIsClick(press: PressSpan): boolean;
|
|
54
|
+
/** The longest address a card shows; longer ones are cut. */
|
|
55
|
+
export declare const MAX_SHOWN_ADDRESS_CHARS = 300;
|
|
56
|
+
/** The address split for display: the site in front, the rest after it. */
|
|
57
|
+
export declare function shownLink(target: string): {
|
|
58
|
+
site: string;
|
|
59
|
+
rest: string;
|
|
60
|
+
};
|
|
61
|
+
/** Whether a resolved address may be opened from the card. */
|
|
62
|
+
export declare function canOpenLink(resolved: string): boolean;
|
|
63
|
+
/** An address as the card shows it. */
|
|
64
|
+
export interface LinkAddressParts {
|
|
65
|
+
/** The site: the host of a web address, else the whole address. */
|
|
66
|
+
host: string;
|
|
67
|
+
/** The rest (path, query and fragment), `''` when there is none. */
|
|
68
|
+
path: string;
|
|
69
|
+
/** The whole address, cut at {@link MAX_SHOWN_ADDRESS_CHARS}. */
|
|
70
|
+
full: string;
|
|
71
|
+
}
|
|
72
|
+
/** How {@link linkAddressParts} writes an address. */
|
|
73
|
+
export interface LinkAddressOptions {
|
|
74
|
+
/** Leave `www.` off the site. */
|
|
75
|
+
trimWww?: boolean;
|
|
76
|
+
/** Leave control and format characters out (by default they show as the replacement character). */
|
|
77
|
+
dropControls?: boolean;
|
|
78
|
+
}
|
|
79
|
+
/** The site and the path of an address, for the card. Credentials never show. */
|
|
80
|
+
export declare function linkAddressParts(href: string, options?: LinkAddressOptions): LinkAddressParts;
|
|
81
|
+
/** Room between the link and its card (screen pixels). */
|
|
82
|
+
export declare const LINK_CARD_GAP_PX = 6;
|
|
83
|
+
/** Closest the card comes to the viewport's edges (screen pixels). */
|
|
84
|
+
export declare const LINK_CARD_MARGIN_PX = 8;
|
|
85
|
+
/** A box on screen (viewport pixels). */
|
|
86
|
+
export interface ScreenBox {
|
|
87
|
+
x: number;
|
|
88
|
+
y: number;
|
|
89
|
+
w: number;
|
|
90
|
+
h: number;
|
|
91
|
+
}
|
|
92
|
+
/** Where the card goes. */
|
|
93
|
+
export interface LinkCardPlace {
|
|
94
|
+
x: number;
|
|
95
|
+
y: number;
|
|
96
|
+
side: 'above' | 'below';
|
|
97
|
+
}
|
|
98
|
+
/** A `DOMRect`-like box as a {@link ScreenBox}. */
|
|
99
|
+
export declare function screenBox(rect: {
|
|
100
|
+
left: number;
|
|
101
|
+
top: number;
|
|
102
|
+
width: number;
|
|
103
|
+
height: number;
|
|
104
|
+
}): ScreenBox;
|
|
105
|
+
/**
|
|
106
|
+
* Where a card of `card` size sits for a link at `link`: above it when it
|
|
107
|
+
* fits there (inside the viewport and clear of `avoid`, a bar the card must
|
|
108
|
+
* not cover), else below it, {@link LINK_CARD_GAP_PX} away, starting at the
|
|
109
|
+
* link's left edge moved by `shiftX` and kept inside the viewport. Below,
|
|
110
|
+
* it moves past `avoid` when the two would overlap; when that runs off the
|
|
111
|
+
* bottom of the viewport, it sits above `avoid` instead.
|
|
112
|
+
*/
|
|
113
|
+
export declare function linkCardPlace(link: ScreenBox, card: {
|
|
114
|
+
w: number;
|
|
115
|
+
h: number;
|
|
116
|
+
}, viewport: {
|
|
117
|
+
w: number;
|
|
118
|
+
h: number;
|
|
119
|
+
}, avoid?: ScreenBox | null, shiftX?: number): LinkCardPlace;
|
|
120
|
+
/** Where the card goes: below the link, or above it when below would leave the window. */
|
|
121
|
+
export declare function placeLinkCard(link: {
|
|
122
|
+
top: number;
|
|
123
|
+
bottom: number;
|
|
124
|
+
left: number;
|
|
125
|
+
}, card: {
|
|
126
|
+
width: number;
|
|
127
|
+
height: number;
|
|
128
|
+
}, window: {
|
|
129
|
+
width: number;
|
|
130
|
+
height: number;
|
|
131
|
+
}, gap?: number): {
|
|
132
|
+
top: number;
|
|
133
|
+
left: number;
|
|
134
|
+
};
|
|
135
|
+
/** True when a link's box moved or resized by more than `tolerance` pixels (default half a pixel). */
|
|
136
|
+
export declare function linkBoxMoved(before: ScreenBox, after: ScreenBox, tolerance?: number): boolean;
|
package/src/link-card.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rules behind the link cards: how a card shows an address, what it may
|
|
3
|
+
* open, where it sits, and the timing of a card that shows while the
|
|
4
|
+
* pointer rests on a link. Pure. What a typed address becomes is the
|
|
5
|
+
* editor's own rule (see {@link LinkCard}).
|
|
6
|
+
*/
|
|
7
|
+
/* ------------------------------------------------------------------------ */
|
|
8
|
+
/* Timing */
|
|
9
|
+
/* ------------------------------------------------------------------------ */
|
|
10
|
+
/** The card shows this long (ms) after the pointer rests on a link (or the caret in one). */
|
|
11
|
+
export const LINK_CARD_SHOW_MS = 300;
|
|
12
|
+
/** The card hides this long (ms) after the pointer leaves the link and the card. */
|
|
13
|
+
export const LINK_CARD_HIDE_MS = 300;
|
|
14
|
+
/** After a link opened, its card shows again only after this long (ms). */
|
|
15
|
+
export const LINK_CARD_COOLDOWN_MS = 700;
|
|
16
|
+
/** A card that just showed in a new place takes no clicks for this long (ms), so a click meant for what was there misses it. */
|
|
17
|
+
export const LINK_CARD_SETTLE_MS = 250;
|
|
18
|
+
/** How often (ms) a card that can't watch every frame checks that its link stayed put. */
|
|
19
|
+
export const LINK_CARD_WATCH_MS = 150;
|
|
20
|
+
/** How long "Link copied" stays (ms). */
|
|
21
|
+
export const LINK_COPIED_MS = 1500;
|
|
22
|
+
/** A touch counts as a tap when it lasts at most this long (ms)… */
|
|
23
|
+
export const TAP_MAX_MS = 400;
|
|
24
|
+
/** …and moves at most this far (screen pixels). */
|
|
25
|
+
export const TAP_MAX_MOVE_PX = 10;
|
|
26
|
+
/** A mouse or pen press becomes a drag past this distance (screen pixels). */
|
|
27
|
+
export const CLICK_MAX_MOVE_PX = 4;
|
|
28
|
+
/** How long to wait before showing a card, given when a link last opened (ms timestamps). */
|
|
29
|
+
export function linkCardShowDelay(now, lastOpened) {
|
|
30
|
+
if (lastOpened === null)
|
|
31
|
+
return LINK_CARD_SHOW_MS;
|
|
32
|
+
return Math.max(LINK_CARD_SHOW_MS, lastOpened + LINK_CARD_COOLDOWN_MS - now);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* What a click on a link does: Mod (⌘ on Apple platforms, Ctrl elsewhere)
|
|
36
|
+
* without Shift or Alt opens at once (`now`); a click without any modifier
|
|
37
|
+
* is a plain click (`later`: the editor follows it, or waits out a double
|
|
38
|
+
* click first); anything else (Shift picks, Alt copies, Ctrl on a Mac opens
|
|
39
|
+
* the context menu) leaves it (`none`).
|
|
40
|
+
*/
|
|
41
|
+
export function linkClickAction(click, apple) {
|
|
42
|
+
const mod = apple ? click.metaKey : click.ctrlKey;
|
|
43
|
+
const other = apple ? click.ctrlKey : click.metaKey;
|
|
44
|
+
if (click.shiftKey || click.altKey || other)
|
|
45
|
+
return 'none';
|
|
46
|
+
return mod ? 'now' : 'later';
|
|
47
|
+
}
|
|
48
|
+
/** Whether a press counts as a click: a short, still tap on touch (or with a stylus on a touch screen, `touch-pen`); a press that never became a drag otherwise. */
|
|
49
|
+
export function pressIsClick(press) {
|
|
50
|
+
if (press.pointerType === 'touch' || press.pointerType === 'touch-pen')
|
|
51
|
+
return press.ms <= TAP_MAX_MS && press.moved <= TAP_MAX_MOVE_PX;
|
|
52
|
+
return press.moved <= CLICK_MAX_MOVE_PX;
|
|
53
|
+
}
|
|
54
|
+
/* ------------------------------------------------------------------------ */
|
|
55
|
+
/* The address */
|
|
56
|
+
/* ------------------------------------------------------------------------ */
|
|
57
|
+
/** The longest address a card shows; longer ones are cut. */
|
|
58
|
+
export const MAX_SHOWN_ADDRESS_CHARS = 300;
|
|
59
|
+
/** The address split for display: the site in front, the rest after it. */
|
|
60
|
+
export function shownLink(target) {
|
|
61
|
+
const web = /^https?:\/\/([^/?#]+)(.*)$/i.exec(target);
|
|
62
|
+
if (web)
|
|
63
|
+
return { site: web[1].replace(/^www\./i, ''), rest: web[2] === '/' ? '' : web[2] };
|
|
64
|
+
const mail = /^mailto:(.*)$/i.exec(target);
|
|
65
|
+
if (mail) {
|
|
66
|
+
try {
|
|
67
|
+
return { site: decodeURIComponent(mail[1]), rest: '' };
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return { site: mail[1], rest: '' };
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return { site: target, rest: '' };
|
|
74
|
+
}
|
|
75
|
+
/** Whether a resolved address may be opened from the card. */
|
|
76
|
+
export function canOpenLink(resolved) {
|
|
77
|
+
try {
|
|
78
|
+
return ['http:', 'https:', 'mailto:'].includes(new URL(resolved).protocol);
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/** Cuts text at `max` characters (whole code points), ending it with an ellipsis. */
|
|
85
|
+
function capChars(text, max) {
|
|
86
|
+
const chars = [...text];
|
|
87
|
+
return chars.length <= max ? text : `${chars.slice(0, max - 1).join('')}…`;
|
|
88
|
+
}
|
|
89
|
+
const CONTROL_CHARS = /[\p{Cc}\p{Cf}]/gu;
|
|
90
|
+
/** The site and the path of an address, for the card. Credentials never show. */
|
|
91
|
+
export function linkAddressParts(href, options = {}) {
|
|
92
|
+
const visible = (text) => text.replace(CONTROL_CHARS, options.dropControls ? '' : '�');
|
|
93
|
+
const full = capChars(visible(href), MAX_SHOWN_ADDRESS_CHARS);
|
|
94
|
+
try {
|
|
95
|
+
const url = new URL(href);
|
|
96
|
+
if (url.protocol === 'http:' || url.protocol === 'https:') {
|
|
97
|
+
const rest = `${url.pathname === '/' ? '' : url.pathname}${url.search}${url.hash}`;
|
|
98
|
+
const host = options.trimWww ? url.host.replace(/^www\./i, '') : url.host;
|
|
99
|
+
return { host, path: capChars(visible(rest), MAX_SHOWN_ADDRESS_CHARS), full: capChars(visible(`${url.protocol}//${url.host}${rest}`), MAX_SHOWN_ADDRESS_CHARS) };
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
/* Not a URL: shown whole. */
|
|
104
|
+
}
|
|
105
|
+
return { host: full, path: '', full };
|
|
106
|
+
}
|
|
107
|
+
/* ------------------------------------------------------------------------ */
|
|
108
|
+
/* Where the card sits */
|
|
109
|
+
/* ------------------------------------------------------------------------ */
|
|
110
|
+
/** Room between the link and its card (screen pixels). */
|
|
111
|
+
export const LINK_CARD_GAP_PX = 6;
|
|
112
|
+
/** Closest the card comes to the viewport's edges (screen pixels). */
|
|
113
|
+
export const LINK_CARD_MARGIN_PX = 8;
|
|
114
|
+
/** A `DOMRect`-like box as a {@link ScreenBox}. */
|
|
115
|
+
export function screenBox(rect) {
|
|
116
|
+
return { x: rect.left, y: rect.top, w: rect.width, h: rect.height };
|
|
117
|
+
}
|
|
118
|
+
function overlaps(a, b) {
|
|
119
|
+
const separated = a.x + a.w <= b.x || b.x + b.w <= a.x || a.y + a.h <= b.y || b.y + b.h <= a.y;
|
|
120
|
+
return !separated;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Where a card of `card` size sits for a link at `link`: above it when it
|
|
124
|
+
* fits there (inside the viewport and clear of `avoid`, a bar the card must
|
|
125
|
+
* not cover), else below it, {@link LINK_CARD_GAP_PX} away, starting at the
|
|
126
|
+
* link's left edge moved by `shiftX` and kept inside the viewport. Below,
|
|
127
|
+
* it moves past `avoid` when the two would overlap; when that runs off the
|
|
128
|
+
* bottom of the viewport, it sits above `avoid` instead.
|
|
129
|
+
*/
|
|
130
|
+
export function linkCardPlace(link, card, viewport, avoid = null, shiftX = 0) {
|
|
131
|
+
const margin = LINK_CARD_MARGIN_PX;
|
|
132
|
+
const x = Math.max(margin, Math.min(link.x + shiftX, viewport.w - card.w - margin));
|
|
133
|
+
const aboveY = link.y - LINK_CARD_GAP_PX - card.h;
|
|
134
|
+
const above = { x, y: aboveY, w: card.w, h: card.h };
|
|
135
|
+
if (aboveY >= margin && (avoid === null || !overlaps(above, avoid)))
|
|
136
|
+
return { x, y: aboveY, side: 'above' };
|
|
137
|
+
let y = link.y + link.h + LINK_CARD_GAP_PX;
|
|
138
|
+
if (avoid !== null && overlaps({ x, y, w: card.w, h: card.h }, avoid))
|
|
139
|
+
y = avoid.y + avoid.h + LINK_CARD_GAP_PX;
|
|
140
|
+
if (avoid !== null && y + card.h > viewport.h - margin) {
|
|
141
|
+
const overY = Math.min(aboveY, avoid.y - LINK_CARD_GAP_PX - card.h);
|
|
142
|
+
if (overY >= margin)
|
|
143
|
+
return { x, y: overY, side: 'above' };
|
|
144
|
+
}
|
|
145
|
+
return { x, y: Math.max(margin, y), side: 'below' };
|
|
146
|
+
}
|
|
147
|
+
/** Where the card goes: below the link, or above it when below would leave the window. */
|
|
148
|
+
export function placeLinkCard(link, card, window, gap = 6) {
|
|
149
|
+
const below = link.bottom + gap;
|
|
150
|
+
const above = link.top - gap - card.height;
|
|
151
|
+
const top = below + card.height <= window.height - gap || above < gap ? below : above;
|
|
152
|
+
const left = Math.max(gap, Math.min(link.left, window.width - card.width - gap));
|
|
153
|
+
return { top, left };
|
|
154
|
+
}
|
|
155
|
+
/** True when a link's box moved or resized by more than `tolerance` pixels (default half a pixel). */
|
|
156
|
+
export function linkBoxMoved(before, after, tolerance = 0.5) {
|
|
157
|
+
return Math.abs(before.x - after.x) > tolerance || Math.abs(before.y - after.y) > tolerance || Math.abs(before.w - after.w) > tolerance || Math.abs(before.h - after.h) > tolerance;
|
|
158
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shortcuts inside an open menu: a row that carries a chord (see `key-chord.ts`) is clicked when that chord is pressed
|
|
3
|
+
* while its menu, or a submenu of it, is open. No React: the menus wire {@link pressMenuShortcut} to their popups.
|
|
4
|
+
*/
|
|
5
|
+
import { type ChordPress } from './key-chord.js';
|
|
6
|
+
/** Marks every popup of one menu tree (a menu and its submenus) with the tree's id. */
|
|
7
|
+
export declare const MENU_TREE_ATTRIBUTE = "data-menu-tree";
|
|
8
|
+
/** Marks a row whose `aria-keyshortcuts` the open menu acts on. */
|
|
9
|
+
export declare const MENU_SHORTCUT_ATTRIBUTE = "data-menu-shortcut";
|
|
10
|
+
/** The keydown as the popup's handler gets it. */
|
|
11
|
+
export interface MenuShortcutEvent extends ChordPress {
|
|
12
|
+
target: EventTarget | null;
|
|
13
|
+
repeat: boolean;
|
|
14
|
+
defaultPrevented: boolean;
|
|
15
|
+
nativeEvent: {
|
|
16
|
+
isComposing?: boolean;
|
|
17
|
+
getModifierState?(key: string): boolean;
|
|
18
|
+
};
|
|
19
|
+
isPropagationStopped(): boolean;
|
|
20
|
+
preventDefault(): void;
|
|
21
|
+
stopPropagation(): void;
|
|
22
|
+
preventBaseUIHandler?(): void;
|
|
23
|
+
}
|
|
24
|
+
/** The row of the open menu tree around `target` whose chord the keydown is, newest popup first. */
|
|
25
|
+
export declare function findMenuShortcut(event: MenuShortcutEvent): HTMLElement | null;
|
|
26
|
+
/**
|
|
27
|
+
* A menu popup's keydown: when the key is the chord of a row in the open menu tree, the menu's own handling is skipped
|
|
28
|
+
* and that row is focused, then clicked, once per press. `focusNow` runs the focus synchronously (React's `flushSync`),
|
|
29
|
+
* so the row is highlighted before its click closes the menu.
|
|
30
|
+
*/
|
|
31
|
+
export declare function pressMenuShortcut(event: MenuShortcutEvent, focusNow: (focus: () => void) => void): void;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shortcuts inside an open menu: a row that carries a chord (see `key-chord.ts`) is clicked when that chord is pressed
|
|
3
|
+
* while its menu, or a submenu of it, is open. No React: the menus wire {@link pressMenuShortcut} to their popups.
|
|
4
|
+
*/
|
|
5
|
+
import { chordMatchesPress, editsText, parseAriaKeys } from "./key-chord.js";
|
|
6
|
+
import { isComposingKey } from "./keys.js";
|
|
7
|
+
/** Marks every popup of one menu tree (a menu and its submenus) with the tree's id. */
|
|
8
|
+
export const MENU_TREE_ATTRIBUTE = 'data-menu-tree';
|
|
9
|
+
/** Marks a row whose `aria-keyshortcuts` the open menu acts on. */
|
|
10
|
+
export const MENU_SHORTCUT_ATTRIBUTE = 'data-menu-shortcut';
|
|
11
|
+
const SHORTCUT_ROWS = `[${MENU_SHORTCUT_ATTRIBUTE}][aria-keyshortcuts]:not([aria-disabled="true"]):not([data-disabled])`;
|
|
12
|
+
const TEXT_FIELDS = 'input, textarea, [contenteditable]:not([contenteditable="false"])';
|
|
13
|
+
function asElement(target) {
|
|
14
|
+
return target !== null && typeof target.closest === 'function' ? target : null;
|
|
15
|
+
}
|
|
16
|
+
/** Whether a key that lands in `target` belongs to the text there rather than to the menu's shortcuts. */
|
|
17
|
+
function keyBelongsToText(event, target, popup) {
|
|
18
|
+
const field = target.closest(TEXT_FIELDS);
|
|
19
|
+
if (field !== null && popup.contains(field))
|
|
20
|
+
return field.matches('[contenteditable]') || editsText(event);
|
|
21
|
+
const copy = (event.ctrlKey || event.metaKey) && !event.shiftKey && !event.altKey && (event.code === 'KeyC' || event.keyCode === 67);
|
|
22
|
+
return copy && target.ownerDocument.getSelection()?.isCollapsed === false;
|
|
23
|
+
}
|
|
24
|
+
/** The row of the open menu tree around `target` whose chord the keydown is, newest popup first. */
|
|
25
|
+
export function findMenuShortcut(event) {
|
|
26
|
+
const target = asElement(event.target);
|
|
27
|
+
const popup = target?.closest(`[${MENU_TREE_ATTRIBUTE}]`) ?? null;
|
|
28
|
+
if (target === null || popup === null)
|
|
29
|
+
return null;
|
|
30
|
+
if (event.defaultPrevented || isComposingKey({ isComposing: event.nativeEvent.isComposing, keyCode: event.keyCode }))
|
|
31
|
+
return null;
|
|
32
|
+
if (keyBelongsToText(event, target, popup))
|
|
33
|
+
return null;
|
|
34
|
+
const tree = popup.getAttribute(MENU_TREE_ATTRIBUTE);
|
|
35
|
+
const root = popup.getRootNode();
|
|
36
|
+
const press = { ...pickPress(event), altGraph: event.nativeEvent.getModifierState?.('AltGraph') === true };
|
|
37
|
+
const popups = [...root.querySelectorAll(`[${MENU_TREE_ATTRIBUTE}][data-open]`)].filter((open) => open.getAttribute(MENU_TREE_ATTRIBUTE) === tree);
|
|
38
|
+
for (const open of popups.reverse()) {
|
|
39
|
+
for (const row of open.querySelectorAll(SHORTCUT_ROWS)) {
|
|
40
|
+
const held = parseAriaKeys(row.getAttribute('aria-keyshortcuts') ?? '');
|
|
41
|
+
if (held !== null && chordMatchesPress(held, press) && row.checkVisibility?.() !== false)
|
|
42
|
+
return row;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
function pickPress(event) {
|
|
48
|
+
const { key, code, keyCode, metaKey, ctrlKey, altKey, shiftKey } = event;
|
|
49
|
+
return { key, code, keyCode, metaKey, ctrlKey, altKey, shiftKey };
|
|
50
|
+
}
|
|
51
|
+
/** Swallows the auto-repeats of the key that pressed a shortcut until it, or ⌘, is released. */
|
|
52
|
+
function swallowRepeats(view, code) {
|
|
53
|
+
if (view === null)
|
|
54
|
+
return;
|
|
55
|
+
const onKey = (event) => {
|
|
56
|
+
const sameKey = event.code === code;
|
|
57
|
+
if (event.type === 'keydown' && event.repeat) {
|
|
58
|
+
if (!sameKey)
|
|
59
|
+
return;
|
|
60
|
+
event.preventDefault();
|
|
61
|
+
event.stopImmediatePropagation();
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (event.type === 'keydown' || sameKey || event.key === 'Meta') {
|
|
65
|
+
view.removeEventListener('keydown', onKey, true);
|
|
66
|
+
view.removeEventListener('keyup', onKey, true);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
view.addEventListener('keydown', onKey, true);
|
|
70
|
+
view.addEventListener('keyup', onKey, true);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* A menu popup's keydown: when the key is the chord of a row in the open menu tree, the menu's own handling is skipped
|
|
74
|
+
* and that row is focused, then clicked, once per press. `focusNow` runs the focus synchronously (React's `flushSync`),
|
|
75
|
+
* so the row is highlighted before its click closes the menu.
|
|
76
|
+
*/
|
|
77
|
+
export function pressMenuShortcut(event, focusNow) {
|
|
78
|
+
if (event.isPropagationStopped())
|
|
79
|
+
return;
|
|
80
|
+
const row = findMenuShortcut(event);
|
|
81
|
+
if (row === null)
|
|
82
|
+
return;
|
|
83
|
+
event.preventDefault();
|
|
84
|
+
event.stopPropagation();
|
|
85
|
+
event.preventBaseUIHandler?.();
|
|
86
|
+
if (event.repeat)
|
|
87
|
+
return;
|
|
88
|
+
swallowRepeats(row.ownerDocument.defaultView, event.code);
|
|
89
|
+
focusNow(() => row.focus({ preventScroll: true }));
|
|
90
|
+
row.click();
|
|
91
|
+
}
|