@tristan2828/ui-foundation 4.154.0 → 4.155.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/cell-patterns.md +24 -18
- package/dist/components/app/milkdown-editor.js +1 -1
- package/dist/components/app/rich-text-editor.js +13 -7
- package/dist/components/app/rich-text-held-keys.d.ts +18 -2
- package/dist/components/app/rich-text-held-keys.js +59 -2
- package/dist/components/app/rich-text-hold-keys.d.ts +7 -3
- package/dist/components/app/rich-text-hold-keys.js +118 -5
- package/package.json +1 -1
|
@@ -26,26 +26,26 @@
|
|
|
26
26
|
- [A variant: a pressed icon inside another value's cell](#a-variant-a-pressed-icon-inside-another-values-cell) · lines 636–715
|
|
27
27
|
- [16. Title linking to the record's view](#16-title-linking-to-the-records-view) · lines 717–758
|
|
28
28
|
- [17. Quick action on the view](#17-quick-action-on-the-view) · lines 760–832
|
|
29
|
-
- [18. Editable value](#18-editable-value) · lines 834–
|
|
29
|
+
- [18. Editable value](#18-editable-value) · lines 834–1702
|
|
30
30
|
- [Presets: one line per field](#presets-one-line-per-field) · lines 1240–1312
|
|
31
31
|
- [Several fields edited as one](#several-fields-edited-as-one) · lines 1314–1350
|
|
32
32
|
- [A link and its label](#a-link-and-its-label) · lines 1352–1376
|
|
33
33
|
- [A list of sub-items](#a-list-of-sub-items) · lines 1378–1511
|
|
34
|
-
- [On a page of the app's own: `EditInPlaceScope`](#on-a-page-of-the-apps-own-editinplacescope) · lines 1513–
|
|
35
|
-
- [In a table's rows](#in-a-tables-rows) · lines
|
|
36
|
-
- [A table of a few short columns: `width="content"`](#a-table-of-a-few-short-columns-widthcontent) · lines
|
|
37
|
-
- [19. Stage circle for an ordered status](#19-stage-circle-for-an-ordered-status) · lines
|
|
38
|
-
- [20. Count linking to the related records](#20-count-linking-to-the-related-records) · lines
|
|
39
|
-
- [21. Rows grouped under headings, and a pill filter](#21-rows-grouped-under-headings-and-a-pill-filter) · lines
|
|
40
|
-
- [22. A highlighted row, and rows that are done](#22-a-highlighted-row-and-rows-that-are-done) · lines
|
|
41
|
-
- [28. A list's rows: hover, lines, rows per page](#28-a-lists-rows-hover-lines-rows-per-page) · lines
|
|
42
|
-
- [23. A Group menu, and a record read beside the list](#23-a-group-menu-and-a-record-read-beside-the-list) · lines
|
|
43
|
-
- [24. A stored colour beside its name](#24-a-stored-colour-beside-its-name) · lines
|
|
44
|
-
- [Picking one on a form: `ColorField`](#picking-one-on-a-form-colorfield) · lines
|
|
45
|
-
- [25. A list's tools: views, filters, columns, row actions, a phone's rows](#25-a-lists-tools-views-filters-columns-row-actions-a-phones-rows) · lines
|
|
46
|
-
- [The framed look, and sibling lists beside the title](#the-framed-look-and-sibling-lists-beside-the-title) · lines
|
|
47
|
-
- [26. A record's related list, capped](#26-a-records-related-list-capped) · lines
|
|
48
|
-
- [27. A repeat and its next time](#27-a-repeat-and-its-next-time) · lines
|
|
34
|
+
- [On a page of the app's own: `EditInPlaceScope`](#on-a-page-of-the-apps-own-editinplacescope) · lines 1513–1619
|
|
35
|
+
- [In a table's rows](#in-a-tables-rows) · lines 1621–1695
|
|
36
|
+
- [A table of a few short columns: `width="content"`](#a-table-of-a-few-short-columns-widthcontent) · lines 1697–1702
|
|
37
|
+
- [19. Stage circle for an ordered status](#19-stage-circle-for-an-ordered-status) · lines 1704–1770
|
|
38
|
+
- [20. Count linking to the related records](#20-count-linking-to-the-related-records) · lines 1772–1826
|
|
39
|
+
- [21. Rows grouped under headings, and a pill filter](#21-rows-grouped-under-headings-and-a-pill-filter) · lines 1828–1910
|
|
40
|
+
- [22. A highlighted row, and rows that are done](#22-a-highlighted-row-and-rows-that-are-done) · lines 1912–1937
|
|
41
|
+
- [28. A list's rows: hover, lines, rows per page](#28-a-lists-rows-hover-lines-rows-per-page) · lines 1939–1972
|
|
42
|
+
- [23. A Group menu, and a record read beside the list](#23-a-group-menu-and-a-record-read-beside-the-list) · lines 1974–2025
|
|
43
|
+
- [24. A stored colour beside its name](#24-a-stored-colour-beside-its-name) · lines 2027–2101
|
|
44
|
+
- [Picking one on a form: `ColorField`](#picking-one-on-a-form-colorfield) · lines 2047–2101
|
|
45
|
+
- [25. A list's tools: views, filters, columns, row actions, a phone's rows](#25-a-lists-tools-views-filters-columns-row-actions-a-phones-rows) · lines 2103–2315
|
|
46
|
+
- [The framed look, and sibling lists beside the title](#the-framed-look-and-sibling-lists-beside-the-title) · lines 2264–2315
|
|
47
|
+
- [26. A record's related list, capped](#26-a-records-related-list-capped) · lines 2317–2351
|
|
48
|
+
- [27. A repeat and its next time](#27-a-repeat-and-its-next-time) · lines 2353–2400
|
|
49
49
|
|
|
50
50
|
<!-- /index -->
|
|
51
51
|
|
|
@@ -1595,14 +1595,20 @@ const body = editInPlace<string>({
|
|
|
1595
1595
|
is safe to call more than once; a failed load is the editor's to meet
|
|
1596
1596
|
when one mounts, and the next editor to mount tries again. Keys typed
|
|
1597
1597
|
into an `autoFocus` editor while its code still loads are kept, and
|
|
1598
|
-
typed in once it's up
|
|
1598
|
+
typed in once it's up; so is a paste, which goes in as a paste, in
|
|
1599
|
+
order with the keys around it.
|
|
1599
1600
|
- **Keys typed before the editor mounts:** call
|
|
1600
1601
|
`holdKeysForNextEditor()` on the press that leads to a note (New
|
|
1601
1602
|
Note, before its request) and, for a cold start, in `main.tsx` beside
|
|
1602
1603
|
`preloadRichTextEditor()`. Until the next `autoFocus` editor mounts,
|
|
1603
1604
|
what's typed is held (not delivered to the page, so Enter doesn't
|
|
1604
1605
|
press New Note again) and typed into it in order, an IME's composition
|
|
1605
|
-
|
|
1606
|
+
and a paste included (a paste goes in through the editor's own paste
|
|
1607
|
+
handling, after the text it opened with is read). The hidden textarea
|
|
1608
|
+
that holds them sits in the page's `<main>` (the body's, before the app
|
|
1609
|
+
has rendered one), then in the editor's own field once it mounts, so
|
|
1610
|
+
it's inside the page's landmarks for axe; each move keeps focus and
|
|
1611
|
+
what's typed. It never takes focus from an input, and lets go, dropping
|
|
1606
1612
|
what it held, when focus moves to another control, on a click
|
|
1607
1613
|
elsewhere, on Esc, or after `timeout` (5 s); call the function it
|
|
1608
1614
|
returns to let go yourself (the request failed). It also starts
|
|
@@ -926,7 +926,7 @@ export default function MilkdownEditor({ id, defaultValue, onChange, onProblem,
|
|
|
926
926
|
if (!area || document.activeElement === area)
|
|
927
927
|
view.focus();
|
|
928
928
|
if (text)
|
|
929
|
-
replayKeys(view, text);
|
|
929
|
+
replayKeys(view, text, held?.pastes());
|
|
930
930
|
if (autoFocus === 'end' || text)
|
|
931
931
|
view.dispatch(view.state.tr.scrollIntoView());
|
|
932
932
|
held?.release();
|
|
@@ -47,10 +47,10 @@ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-run
|
|
|
47
47
|
// takes its place; an empty one shows skeleton lines. An app that opens on
|
|
48
48
|
// an editor loads it ahead with `preloadRichTextEditor()`. Once loaded it's
|
|
49
49
|
// drawn at once; a load that failed is tried again by the next editor. Keys
|
|
50
|
-
// typed into an `autoFocus` editor while it loads are held
|
|
51
|
-
// once it's up (rich-text-held-keys.ts), and saved; so are
|
|
52
|
-
// before it mounted, after `holdKeysForNextEditor()` (a New Note
|
|
53
|
-
// app's start; rich-text-hold-keys.ts).
|
|
50
|
+
// typed and pastes made into an `autoFocus` editor while it loads are held
|
|
51
|
+
// and put in once it's up (rich-text-held-keys.ts), and saved; so are those
|
|
52
|
+
// from before it mounted, after `holdKeysForNextEditor()` (a New Note
|
|
53
|
+
// press, an app's start; rich-text-hold-keys.ts).
|
|
54
54
|
//
|
|
55
55
|
// What Markdown can't hold isn't saved: spaces at a line's start or end
|
|
56
56
|
// (rich-text-empty-paragraph.ts; inline code keeps its own) and blank lines
|
|
@@ -59,6 +59,7 @@ import { lazy, Suspense, useLayoutEffect, useRef, useState } from 'react';
|
|
|
59
59
|
import { cn } from 'cn';
|
|
60
60
|
import { Skeleton } from '../ui/skeleton.js';
|
|
61
61
|
import { Markdown } from './markdown.js';
|
|
62
|
+
import { holdPaste } from './rich-text-held-keys.js';
|
|
62
63
|
import { claimKeyHold, keyHoldWaiting } from './rich-text-hold-keys.js';
|
|
63
64
|
import { loadedRichTextEditor, loadRichTextEditor } from './rich-text-load.js';
|
|
64
65
|
// When the editor opens again: each time `defaultValue` changes to
|
|
@@ -96,15 +97,20 @@ export function RichTextEditor(props) {
|
|
|
96
97
|
// (rich-text-held-keys.ts). Beside the Suspense boundary, not in its
|
|
97
98
|
// fallback, which goes the moment the editor's code arrives.
|
|
98
99
|
// Keys held from before this editor mounted (`holdKeysForNextEditor()`,
|
|
99
|
-
// rich-text-hold-keys.ts) are taken over instead, loaded or not
|
|
100
|
+
// rich-text-hold-keys.ts) are taken over instead, loaded or not: that
|
|
101
|
+
// textarea moves into the place its own would be, inside the page's
|
|
102
|
+
// landmarks.
|
|
100
103
|
const [adopting] = useState(() => Boolean(props.autoFocus) && !props.readOnly && keyHoldWaiting());
|
|
101
104
|
const [holding, setHolding] = useState(() => adopting || (loadedRichTextEditor() === null && Boolean(props.autoFocus) && !props.readOnly));
|
|
102
105
|
const heldRef = useRef(null);
|
|
106
|
+
const placeRef = useRef(null);
|
|
103
107
|
const composing = useRef(false);
|
|
108
|
+
const pastes = useRef([]);
|
|
104
109
|
const claimed = useRef(null);
|
|
105
110
|
const [held] = useState(() => ({
|
|
106
111
|
element: () => (adopting ? (claimed.current?.element() ?? null) : heldRef.current),
|
|
107
112
|
composing: () => (adopting ? (claimed.current?.composing() ?? false) : composing.current),
|
|
113
|
+
pastes: () => (adopting ? (claimed.current?.pastes() ?? []) : pastes.current),
|
|
108
114
|
release: () => {
|
|
109
115
|
claimed.current?.release();
|
|
110
116
|
claimed.current = null;
|
|
@@ -114,7 +120,7 @@ export function RichTextEditor(props) {
|
|
|
114
120
|
useLayoutEffect(() => {
|
|
115
121
|
if (!adopting)
|
|
116
122
|
return;
|
|
117
|
-
claimed.current = claimKeyHold();
|
|
123
|
+
claimed.current = claimKeyHold(placeRef.current);
|
|
118
124
|
return () => {
|
|
119
125
|
claimed.current?.giveBack();
|
|
120
126
|
claimed.current = null;
|
|
@@ -129,7 +135,7 @@ export function RichTextEditor(props) {
|
|
|
129
135
|
focusHandled.current = key;
|
|
130
136
|
return true;
|
|
131
137
|
});
|
|
132
|
-
return (_jsxs(_Fragment, { children: [holding && !adopting && (_jsx("textarea", { ref: heldRef, id: props.id, "data-slot": "rich-text-editor-held", autoFocus: true, "aria-label": props['aria-label'], "aria-labelledby": props['aria-labelledby'], "aria-describedby": props['aria-describedby'], className: "sr-only", onCompositionStart: () => (composing.current = true), onCompositionEnd: () => (composing.current = false) })), _jsx(Suspense, { fallback: _jsxs("div", { "data-slot": "rich-text-editor", "data-state": "loading", className: cn('flex w-full flex-col', !props.inline && 'rounded-lg border border-input', props.className), children: [(props.toolbar ?? true) === true && _jsx("div", { className: "h-9 border-b border-input pointer-coarse:hidden" }), _jsx("div", { "aria-busy": "true", className: cn('flex flex-col gap-2', props.inline ? 'py-0.5' : 'min-h-24 px-2.5 py-2',
|
|
138
|
+
return (_jsxs(_Fragment, { children: [holding && !adopting && (_jsx("textarea", { ref: heldRef, id: props.id, "data-slot": "rich-text-editor-held", autoFocus: true, "aria-label": props['aria-label'], "aria-labelledby": props['aria-labelledby'], "aria-describedby": props['aria-describedby'], className: "sr-only", onCompositionStart: () => (composing.current = true), onCompositionEnd: () => (composing.current = false), onPaste: (event) => holdPaste(event.currentTarget, pastes.current, event.nativeEvent) })), holding && adopting && _jsx("span", { ref: placeRef, "data-slot": "rich-text-editor-held-place", className: "contents" }), _jsx(Suspense, { fallback: _jsxs("div", { "data-slot": "rich-text-editor", "data-state": "loading", className: cn('flex w-full flex-col', !props.inline && 'rounded-lg border border-input', props.className), children: [(props.toolbar ?? true) === true && _jsx("div", { className: "h-9 border-b border-input pointer-coarse:hidden" }), _jsx("div", { "aria-busy": "true", className: cn('flex flex-col gap-2', props.inline ? 'py-0.5' : 'min-h-24 px-2.5 py-2',
|
|
133
139
|
// The room of the add-row bar the editor draws under a table.
|
|
134
140
|
!props.readOnly && '[&_table]:mb-5'), children: props.defaultValue.trim() ? (_jsx(Markdown, { headingLevel: props.headingLevel, headingFrom: props.headingFrom, foldKey: props.foldable === false ? undefined : props.foldKey, children: props.defaultValue })) : (_jsxs(_Fragment, { children: [_jsx(Skeleton, { className: "h-4 w-3/4" }), _jsx(Skeleton, { className: "h-4 w-1/2" })] })) })] }), children: _jsx(MilkdownEditor, { ...props, onChange: (markdown) => {
|
|
135
141
|
if (live.current !== opening)
|
|
@@ -1,12 +1,28 @@
|
|
|
1
1
|
import type { EditorView } from '@milkdown/kit/prose/view';
|
|
2
|
+
/** A paste held until the editor is up: what the clipboard had. */
|
|
3
|
+
export type HeldPaste = {
|
|
4
|
+
text: string;
|
|
5
|
+
html: string;
|
|
6
|
+
};
|
|
2
7
|
/** The textarea's side of the hand-over (rich-text-editor.tsx). */
|
|
3
8
|
export type HeldKeys = {
|
|
4
9
|
/** The textarea holding the keys, if it's still there. */
|
|
5
10
|
element: () => HTMLTextAreaElement | null;
|
|
6
11
|
/** A composition (an IME, a phone's suggestions) is open in it. */
|
|
7
12
|
composing: () => boolean;
|
|
13
|
+
/** The pastes held, in the order they came (`holdPaste`). */
|
|
14
|
+
pastes: () => readonly HeldPaste[];
|
|
8
15
|
/** The editor has the text: the textarea goes. */
|
|
9
16
|
release: () => void;
|
|
10
17
|
};
|
|
11
|
-
/**
|
|
12
|
-
|
|
18
|
+
/**
|
|
19
|
+
* A paste into the held textarea: kept in `pastes`, its mark put in the
|
|
20
|
+
* textarea where the paste would have gone. A paste with neither text
|
|
21
|
+
* nor HTML (files alone) is left alone.
|
|
22
|
+
*/
|
|
23
|
+
export declare function holdPaste(area: HTMLTextAreaElement, pastes: HeldPaste[], event: ClipboardEvent): void;
|
|
24
|
+
/**
|
|
25
|
+
* Types `text` into the view at its caret, as typed: each character as
|
|
26
|
+
* text input, each newline as Enter, and each paste's mark as that paste.
|
|
27
|
+
*/
|
|
28
|
+
export declare function replayKeys(view: EditorView, text: string, pastes?: readonly HeldPaste[]): void;
|
|
@@ -1,6 +1,63 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// Marks are characters from Unicode's private use area, one per paste.
|
|
2
|
+
const FIRST_MARK = 0xe000;
|
|
3
|
+
const LAST_MARK = 0xf8ff;
|
|
4
|
+
/**
|
|
5
|
+
* A paste into the held textarea: kept in `pastes`, its mark put in the
|
|
6
|
+
* textarea where the paste would have gone. A paste with neither text
|
|
7
|
+
* nor HTML (files alone) is left alone.
|
|
8
|
+
*/
|
|
9
|
+
export function holdPaste(area, pastes, event) {
|
|
10
|
+
const data = event.clipboardData;
|
|
11
|
+
if (!data || FIRST_MARK + pastes.length > LAST_MARK)
|
|
12
|
+
return;
|
|
13
|
+
const text = data.getData('text/plain');
|
|
14
|
+
const html = data.getData('text/html');
|
|
15
|
+
if (!text && !html)
|
|
16
|
+
return;
|
|
17
|
+
event.preventDefault();
|
|
18
|
+
const mark = String.fromCharCode(FIRST_MARK + pastes.length);
|
|
19
|
+
pastes.push({ text, html });
|
|
20
|
+
area.setRangeText(mark, area.selectionStart, area.selectionEnd, 'end');
|
|
21
|
+
}
|
|
22
|
+
// A held paste going in through the editor's own paste handling, as a
|
|
23
|
+
// paste from the keyboard does: a paste event on the editable element,
|
|
24
|
+
// carrying the clipboard's text and HTML.
|
|
25
|
+
// Where a browser can't build one, ProseMirror's own paste of the HTML or
|
|
26
|
+
// the text.
|
|
27
|
+
function clipboardEvent(text, html) {
|
|
28
|
+
try {
|
|
29
|
+
const data = new DataTransfer();
|
|
30
|
+
if (text)
|
|
31
|
+
data.setData('text/plain', text);
|
|
32
|
+
if (html)
|
|
33
|
+
data.setData('text/html', html);
|
|
34
|
+
const event = new ClipboardEvent('paste', { clipboardData: data, bubbles: true, cancelable: true });
|
|
35
|
+
return event.clipboardData ? event : null;
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function paste(view, { text, html }) {
|
|
42
|
+
const event = clipboardEvent(text, html);
|
|
43
|
+
if (event)
|
|
44
|
+
view.dom.dispatchEvent(event);
|
|
45
|
+
else if (html)
|
|
46
|
+
view.pasteHTML(html);
|
|
47
|
+
else
|
|
48
|
+
view.pasteText(text);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Types `text` into the view at its caret, as typed: each character as
|
|
52
|
+
* text input, each newline as Enter, and each paste's mark as that paste.
|
|
53
|
+
*/
|
|
54
|
+
export function replayKeys(view, text, pastes = []) {
|
|
3
55
|
for (const character of text.replace(/\r\n?/g, '\n')) {
|
|
56
|
+
const held = character.length === 1 ? pastes[character.charCodeAt(0) - FIRST_MARK] : undefined;
|
|
57
|
+
if (held) {
|
|
58
|
+
paste(view, held);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
4
61
|
if (character === '\n') {
|
|
5
62
|
const enter = new KeyboardEvent('keydown', { key: 'Enter', code: 'Enter', bubbles: true, cancelable: true });
|
|
6
63
|
if (!view.someProp('handleKeyDown', (handle) => handle(view, enter)))
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type HeldKeys } from './rich-text-held-keys.js';
|
|
2
2
|
export type HoldKeysOptions = {
|
|
3
3
|
/** How long to wait for an editor, in milliseconds. Default 5000. */
|
|
4
4
|
timeout?: number;
|
|
@@ -19,5 +19,9 @@ export type ClaimedHold = HeldKeys & {
|
|
|
19
19
|
/** The editor went before it took the keys: the hold waits again. */
|
|
20
20
|
giveBack: () => void;
|
|
21
21
|
};
|
|
22
|
-
/**
|
|
23
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Takes over the waiting hold, if there is one: from now on it's the
|
|
24
|
+
* editor's to let go. Its textarea moves to the end of `place`, in the
|
|
25
|
+
* editor's markup (mid-composition, once the composition ends).
|
|
26
|
+
*/
|
|
27
|
+
export declare function claimKeyHold(place: Element | null): ClaimedHold | null;
|
|
@@ -1,3 +1,31 @@
|
|
|
1
|
+
// Keys typed before any editor is on the page: a notes app's New Note
|
|
2
|
+
// (the note is made by a request, then its page mounts) or its cold start
|
|
3
|
+
// (what's typed before the note's page shows). `holdKeysForNextEditor()`
|
|
4
|
+
// puts a hidden textarea at the end of the page's <main> (the body's,
|
|
5
|
+
// while the page has none) and focuses it, so what's typed is kept there,
|
|
6
|
+
// an IME's composition and a paste included, and isn't delivered to the
|
|
7
|
+
// page: Enter doesn't press the button that was just pressed again. The
|
|
8
|
+
// next RichTextEditor with `autoFocus` to mount takes it over, as it takes
|
|
9
|
+
// its own while its code loads (rich-text-held-keys.ts), and types it in
|
|
10
|
+
// once it has read the text it opened with.
|
|
11
|
+
//
|
|
12
|
+
// Where the textarea sits follows the page, so it's always inside its
|
|
13
|
+
// landmarks: a <main> that arrives (the app rendering after a hold at its
|
|
14
|
+
// start) takes it in, one that goes (a page replaced) gives it back to
|
|
15
|
+
// the body, and the editor that takes it over moves it into its own
|
|
16
|
+
// markup, where its own held textarea would be. Each move keeps focus,
|
|
17
|
+
// the caret and what's typed, and happens within the task that caused it,
|
|
18
|
+
// so no key falls between; no focus or blur event reaches the page for it.
|
|
19
|
+
//
|
|
20
|
+
// It never takes focus from something that takes text (an input, a
|
|
21
|
+
// textarea, a select, editable text): called with focus in one, it holds
|
|
22
|
+
// nothing. Until an editor takes it over it lets go, dropping what it
|
|
23
|
+
// held, when focus moves to any other element (Tab, a field's own
|
|
24
|
+
// autoFocus), on a click or tap anywhere else, on Esc, or when `timeout`
|
|
25
|
+
// passes; on Esc and the timeout focus goes back where it was. Nothing a
|
|
26
|
+
// control would have taken is held. Once an editor has taken it over,
|
|
27
|
+
// only that editor lets it go.
|
|
28
|
+
import { holdPaste } from './rich-text-held-keys.js';
|
|
1
29
|
import { preloadRichTextEditor } from './rich-text-load.js';
|
|
2
30
|
// The hold waiting for an editor. One taken over by an editor is that
|
|
3
31
|
// editor's, and no longer here.
|
|
@@ -10,6 +38,49 @@ function takesText(element) {
|
|
|
10
38
|
return true;
|
|
11
39
|
return element instanceof HTMLInputElement && TEXT_INPUT.test(element.type);
|
|
12
40
|
}
|
|
41
|
+
// Where a hold no editor has taken sits: in the page's <main>, else (an
|
|
42
|
+
// app's start, before it has rendered) on the body.
|
|
43
|
+
function home() {
|
|
44
|
+
return document.querySelector('main, [role=main]') ?? document.body;
|
|
45
|
+
}
|
|
46
|
+
const FOCUS_EVENTS = ['focus', 'blur', 'focusin', 'focusout'];
|
|
47
|
+
// Moves the textarea to the end of `parent`. Focused, it stays focused
|
|
48
|
+
// with its caret: moved in place where the browser can (`moveBefore`),
|
|
49
|
+
// else put back in focus at once, the focus and blur events of the move
|
|
50
|
+
// stopped before anything on the page hears them.
|
|
51
|
+
function moveTo(area, parent) {
|
|
52
|
+
if (area.parentElement === parent && area === parent.lastElementChild)
|
|
53
|
+
return;
|
|
54
|
+
if (document.activeElement !== area) {
|
|
55
|
+
parent.append(area);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if ('moveBefore' in parent && area.isConnected) {
|
|
59
|
+
try {
|
|
60
|
+
parent.moveBefore(area, null);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
// Not movable in place: moved as below.
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const { selectionStart, selectionEnd, selectionDirection } = area;
|
|
68
|
+
const quiet = (event) => {
|
|
69
|
+
if (event.target === area)
|
|
70
|
+
event.stopImmediatePropagation();
|
|
71
|
+
};
|
|
72
|
+
for (const type of FOCUS_EVENTS)
|
|
73
|
+
window.addEventListener(type, quiet, true);
|
|
74
|
+
try {
|
|
75
|
+
parent.append(area);
|
|
76
|
+
area.focus({ preventScroll: true });
|
|
77
|
+
area.setSelectionRange(selectionStart, selectionEnd, selectionDirection ?? undefined);
|
|
78
|
+
}
|
|
79
|
+
finally {
|
|
80
|
+
for (const type of FOCUS_EVENTS)
|
|
81
|
+
window.removeEventListener(type, quiet, true);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
13
84
|
// Keys that are text (or Enter) stop at the textarea: no listener on the
|
|
14
85
|
// page sees them while they're held. Tab, Esc and shortcuts with Ctrl,
|
|
15
86
|
// Cmd or Alt go on.
|
|
@@ -22,7 +93,7 @@ function stopTyped(event) {
|
|
|
22
93
|
}
|
|
23
94
|
function makeHold(before, timeout) {
|
|
24
95
|
const area = document.createElement('textarea');
|
|
25
|
-
const hold = { area, composing: false, before, timeout, stop: undefined };
|
|
96
|
+
const hold = { area, composing: false, pastes: [], before, timeout, stop: undefined };
|
|
26
97
|
area.setAttribute('data-slot', 'rich-text-editor-held');
|
|
27
98
|
area.setAttribute('aria-label', 'Note, opening');
|
|
28
99
|
area.className = 'sr-only';
|
|
@@ -30,6 +101,11 @@ function makeHold(before, timeout) {
|
|
|
30
101
|
area.addEventListener(type, stopTyped);
|
|
31
102
|
area.addEventListener('compositionstart', () => (hold.composing = true));
|
|
32
103
|
area.addEventListener('compositionend', () => (hold.composing = false));
|
|
104
|
+
// A paste is held with the keys, and no paste listener on the page sees it.
|
|
105
|
+
area.addEventListener('paste', (event) => {
|
|
106
|
+
event.stopPropagation();
|
|
107
|
+
holdPaste(area, hold.pastes, event);
|
|
108
|
+
});
|
|
33
109
|
return hold;
|
|
34
110
|
}
|
|
35
111
|
// Lets go of a waiting hold: what it held is dropped and, asked to, focus
|
|
@@ -48,7 +124,8 @@ function letGo(hold, giveFocusBack) {
|
|
|
48
124
|
hold.area.remove();
|
|
49
125
|
}
|
|
50
126
|
// While waiting: focus or a pointer going anywhere else, Esc, or the
|
|
51
|
-
// timeout lets go.
|
|
127
|
+
// timeout lets go. The page changing moves the textarea to its home: a
|
|
128
|
+
// <main> arriving, or the element it was in going with focus in it.
|
|
52
129
|
function listen(hold) {
|
|
53
130
|
const onFocus = (event) => {
|
|
54
131
|
if (event.target !== hold.area)
|
|
@@ -62,11 +139,31 @@ function listen(hold) {
|
|
|
62
139
|
if (event.key === 'Escape')
|
|
63
140
|
letGo(hold, true);
|
|
64
141
|
};
|
|
142
|
+
const rehome = () => {
|
|
143
|
+
const area = hold.area;
|
|
144
|
+
if (area.isConnected) {
|
|
145
|
+
// Mid-composition it stays put: a move would end it.
|
|
146
|
+
if (hold.composing)
|
|
147
|
+
return;
|
|
148
|
+
const to = home();
|
|
149
|
+
if (area.parentElement === document.body && to !== document.body)
|
|
150
|
+
moveTo(area, to);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
// Taken off the page with its parent: back, with the focus a waiting
|
|
154
|
+
// hold always has (anything else focused would have let it go).
|
|
155
|
+
home().append(area);
|
|
156
|
+
area.focus({ preventScroll: true });
|
|
157
|
+
area.setSelectionRange(area.value.length, area.value.length);
|
|
158
|
+
};
|
|
159
|
+
const watch = new MutationObserver(rehome);
|
|
160
|
+
watch.observe(document.documentElement, { childList: true, subtree: true });
|
|
65
161
|
document.addEventListener('focusin', onFocus, true);
|
|
66
162
|
document.addEventListener('pointerdown', onPointer, true);
|
|
67
163
|
hold.area.addEventListener('keydown', onKey);
|
|
68
164
|
const timer = setTimeout(() => letGo(hold, true), hold.timeout);
|
|
69
165
|
hold.stop = () => {
|
|
166
|
+
watch.disconnect();
|
|
70
167
|
document.removeEventListener('focusin', onFocus, true);
|
|
71
168
|
document.removeEventListener('pointerdown', onPointer, true);
|
|
72
169
|
hold.area.removeEventListener('keydown', onKey);
|
|
@@ -97,7 +194,7 @@ export function holdKeysForNextEditor(options = {}) {
|
|
|
97
194
|
if (takesText(focused))
|
|
98
195
|
return () => { };
|
|
99
196
|
hold = makeHold(focused, timeout);
|
|
100
|
-
|
|
197
|
+
home().append(hold.area);
|
|
101
198
|
waiting = hold;
|
|
102
199
|
}
|
|
103
200
|
if (focused !== hold.area && !takesText(focused)) {
|
|
@@ -115,17 +212,30 @@ export function holdKeysForNextEditor(options = {}) {
|
|
|
115
212
|
export function keyHoldWaiting() {
|
|
116
213
|
return waiting !== null;
|
|
117
214
|
}
|
|
118
|
-
/**
|
|
119
|
-
|
|
215
|
+
/**
|
|
216
|
+
* Takes over the waiting hold, if there is one: from now on it's the
|
|
217
|
+
* editor's to let go. Its textarea moves to the end of `place`, in the
|
|
218
|
+
* editor's markup (mid-composition, once the composition ends).
|
|
219
|
+
*/
|
|
220
|
+
export function claimKeyHold(place) {
|
|
120
221
|
const hold = waiting;
|
|
121
222
|
if (!hold)
|
|
122
223
|
return null;
|
|
123
224
|
hold.stop?.();
|
|
124
225
|
waiting = null;
|
|
125
226
|
let done = false;
|
|
227
|
+
const moveIn = () => {
|
|
228
|
+
if (!done && place?.isConnected)
|
|
229
|
+
moveTo(hold.area, place);
|
|
230
|
+
};
|
|
231
|
+
if (hold.composing)
|
|
232
|
+
hold.area.addEventListener('compositionend', moveIn, { once: true });
|
|
233
|
+
else
|
|
234
|
+
moveIn();
|
|
126
235
|
return {
|
|
127
236
|
element: () => (done ? null : hold.area),
|
|
128
237
|
composing: () => hold.composing,
|
|
238
|
+
pastes: () => hold.pastes,
|
|
129
239
|
release: () => {
|
|
130
240
|
done = true;
|
|
131
241
|
hold.area.remove();
|
|
@@ -134,10 +244,13 @@ export function claimKeyHold() {
|
|
|
134
244
|
if (done)
|
|
135
245
|
return;
|
|
136
246
|
done = true;
|
|
247
|
+
hold.area.removeEventListener('compositionend', moveIn);
|
|
137
248
|
if (waiting || !hold.area.isConnected) {
|
|
138
249
|
hold.area.remove();
|
|
139
250
|
return;
|
|
140
251
|
}
|
|
252
|
+
// Out of the editor's markup before it goes.
|
|
253
|
+
moveTo(hold.area, home());
|
|
141
254
|
waiting = hold;
|
|
142
255
|
listen(hold);
|
|
143
256
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tristan2828/ui-foundation",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.155.0",
|
|
4
4
|
"description": "Shared UI foundation for contract-first, database-backed React apps: shadcn primitives, DataTable/EntityForm composites, auth, the gateway error seam, design tokens, lint rules and the AI playbook.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|