@tristan2828/ui-foundation 4.124.2 → 4.126.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/conventions/docs/add-an-entity.md +42 -21
- package/conventions/docs/cell-patterns.md +80 -39
- package/dist/api/transport/index.js +10 -1
- package/dist/components/app/edit-caret.d.ts +25 -0
- package/dist/components/app/edit-caret.js +118 -0
- package/dist/components/app/edit-in-place-store.d.ts +43 -4
- package/dist/components/app/edit-in-place-store.js +169 -19
- package/dist/components/app/editable-value.js +164 -22
- package/dist/components/app/entity-form.js +4 -0
- package/dist/components/app/entity-view.js +1 -1
- package/dist/components/app/form-leave-guard.js +7 -1
- package/dist/components/app/milkdown-editor.d.ts +1 -0
- package/dist/components/app/milkdown-editor.js +224 -63
- package/dist/components/app/prose-look.d.ts +1 -1
- package/dist/components/app/prose-look.js +3 -1
- package/dist/components/app/rich-text-autolink.d.ts +6 -0
- package/dist/components/app/rich-text-autolink.js +79 -0
- package/dist/components/app/rich-text-edges.d.ts +8 -0
- package/dist/components/app/rich-text-edges.js +144 -0
- package/dist/components/app/rich-text-editor.d.ts +15 -2
- package/dist/components/app/rich-text-editor.js +16 -4
- package/dist/components/app/rich-text-empty-paragraph.d.ts +14 -6
- package/dist/components/app/rich-text-empty-paragraph.js +19 -8
- package/dist/components/app/rich-text-formats.js +88 -15
- package/dist/components/app/rich-text-full-screen.d.ts +2 -0
- package/dist/components/app/rich-text-full-screen.js +78 -0
- package/dist/components/app/rich-text-highlight-menu.d.ts +3 -1
- package/dist/components/app/rich-text-highlight-menu.js +3 -2
- package/dist/components/app/rich-text-input-rules.d.ts +7 -0
- package/dist/components/app/rich-text-input-rules.js +83 -0
- package/dist/components/app/rich-text-keyboard-bar.d.ts +24 -0
- package/dist/components/app/rich-text-keyboard-bar.js +214 -0
- package/dist/components/app/rich-text-node-views.js +2 -1
- package/dist/components/app/rich-text-paste.d.ts +43 -0
- package/dist/components/app/rich-text-paste.js +355 -0
- package/dist/components/app/rich-text-shortcuts.d.ts +5 -1
- package/dist/components/app/rich-text-shortcuts.js +132 -2
- package/dist/components/app/rich-text-slash-menu.js +47 -9
- package/dist/components/app/rich-text-slash.d.ts +7 -0
- package/dist/components/app/rich-text-slash.js +27 -14
- package/dist/components/app/rich-text-table-grips.d.ts +8 -6
- package/dist/components/app/rich-text-table-grips.js +11 -12
- package/dist/components/app/rich-text-tasks.d.ts +8 -0
- package/dist/components/app/rich-text-tasks.js +20 -0
- package/dist/components/app/rich-text-toolbar.d.ts +5 -1
- package/dist/components/app/rich-text-toolbar.js +13 -5
- package/dist/components/app/rich-text-touch.d.ts +2 -0
- package/dist/components/app/rich-text-touch.js +18 -0
- package/dist/components/app/rich-text-widgets.js +1 -1
- package/dist/hooks/table-url-changes.d.ts +4 -0
- package/dist/hooks/table-url-changes.js +13 -0
- package/dist/hooks/use-table-url-state.js +14 -8
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -0
- package/dist/lib/markdown-merge.d.ts +26 -0
- package/dist/lib/markdown-merge.js +112 -17
- package/dist/lib/page-leaving.d.ts +8 -0
- package/dist/lib/page-leaving.js +30 -0
- package/dist/lib/pending-edits.d.ts +14 -0
- package/dist/lib/pending-edits.js +51 -0
- package/dist/mocks/e2e-hooks.js +10 -0
- package/dist/mocks/override.d.ts +8 -0
- package/dist/mocks/start-worker.d.ts +24 -0
- package/dist/mocks/start-worker.js +120 -3
- package/dist/testing/index.d.ts +84 -5
- package/dist/testing/index.js +177 -10
- package/package.json +1 -1
- package/styles/index.css +7 -0
|
@@ -37,7 +37,9 @@ export const PROSE = {
|
|
|
37
37
|
* so it sits on the first line of a heading that wraps. Its chevron points
|
|
38
38
|
* down while the section shows and right while it's folded.
|
|
39
39
|
*/
|
|
40
|
-
export const FOLD_BUTTON = '-ml-1 flex h-[1lh] w-5 shrink-0 items-center justify-center rounded-sm text-muted-foreground outline-none hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50'
|
|
40
|
+
export const FOLD_BUTTON = '-ml-1 flex h-[1lh] w-5 shrink-0 items-center justify-center rounded-sm text-muted-foreground outline-none hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50' +
|
|
41
|
+
// On a touch screen, an invisible 40px square round it takes the tap (review F11).
|
|
42
|
+
" relative pointer-coarse:before:absolute pointer-coarse:before:top-1/2 pointer-coarse:before:left-1/2 pointer-coarse:before:size-10 pointer-coarse:before:-translate-1/2 pointer-coarse:before:content-['']";
|
|
41
43
|
/** A heading with a fold button: the button and the words side by side, the look on both. */
|
|
42
44
|
export const FOLD_HEADING = 'flex items-start gap-1';
|
|
43
45
|
/**
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Plugin } from '@milkdown/kit/prose/state';
|
|
2
|
+
import type { EditorView } from '@milkdown/kit/prose/view';
|
|
3
|
+
/** Links the address that ends at the caret, if there is one: for Enter, before the line splits. */
|
|
4
|
+
export declare function linkWordBefore(view: EditorView): void;
|
|
5
|
+
/** A space typed after an address links it. */
|
|
6
|
+
export declare function autolinkPlugin(): Plugin;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// A web or email address typed into the rich-text editor becomes a link
|
|
2
|
+
// as the word ends: on a space (autolinkPlugin) or on Enter (linkWordBefore,
|
|
3
|
+
// which the editor's Enter calls before the line splits). Web and email
|
|
4
|
+
// addresses only (`https://…`, `www.…`, `name@example.com`), through
|
|
5
|
+
// linkHref: `www.` gains https://, an email mailto:. Punctuation that ends
|
|
6
|
+
// a sentence after it (`.`, `,`, `)`) stays outside the link. Not in code,
|
|
7
|
+
// and not in words already linked. One undo takes the link off, the
|
|
8
|
+
// typing kept.
|
|
9
|
+
//
|
|
10
|
+
// The link's words are the address as typed, so the Markdown saves it as
|
|
11
|
+
// an address (`<https://…>` or `[www.x.org](https://www.x.org)`), never
|
|
12
|
+
// as escaped text (`https\://`).
|
|
13
|
+
import { Plugin, PluginKey } from '@milkdown/kit/prose/state';
|
|
14
|
+
import { addressOf } from './rich-text-paste.js';
|
|
15
|
+
const TRAILING = /[.,;:!?'")\]}]+$/;
|
|
16
|
+
// The address ending at `pos` in its textblock, and its span; `null` when
|
|
17
|
+
// the word before `pos` isn't one, or can't be linked there.
|
|
18
|
+
function addressBefore(state, pos) {
|
|
19
|
+
const $pos = state.doc.resolve(pos);
|
|
20
|
+
const parent = $pos.parent;
|
|
21
|
+
if (!parent.isTextblock || parent.type.spec.code)
|
|
22
|
+
return null;
|
|
23
|
+
const link = state.schema.marks.link;
|
|
24
|
+
if (!link)
|
|
25
|
+
return null;
|
|
26
|
+
const before = parent.textBetween(0, $pos.parentOffset, undefined, '');
|
|
27
|
+
const word = /\S+$/.exec(before)?.[0];
|
|
28
|
+
if (!word)
|
|
29
|
+
return null;
|
|
30
|
+
const trimmed = word.replace(TRAILING, '');
|
|
31
|
+
// An opening bracket before the address stays outside it too.
|
|
32
|
+
const lead = /^[(['"<]+/.exec(trimmed)?.[0] ?? '';
|
|
33
|
+
const address = trimmed.slice(lead.length);
|
|
34
|
+
const href = addressOf(address);
|
|
35
|
+
if (!href)
|
|
36
|
+
return null;
|
|
37
|
+
const from = pos - word.length + lead.length;
|
|
38
|
+
const to = from + address.length;
|
|
39
|
+
let blocked = false;
|
|
40
|
+
state.doc.nodesBetween(from, to, (node) => {
|
|
41
|
+
if (node.isText && node.marks.some((mark) => mark.type === link || mark.type.spec.code))
|
|
42
|
+
blocked = true;
|
|
43
|
+
if (node.isInline && !node.isText)
|
|
44
|
+
blocked = true;
|
|
45
|
+
});
|
|
46
|
+
return blocked ? null : { from, to, href };
|
|
47
|
+
}
|
|
48
|
+
function linkAt(tr, state, span) {
|
|
49
|
+
return tr.addMark(span.from, span.to, state.schema.marks.link.create({ href: span.href }));
|
|
50
|
+
}
|
|
51
|
+
/** Links the address that ends at the caret, if there is one: for Enter, before the line splits. */
|
|
52
|
+
export function linkWordBefore(view) {
|
|
53
|
+
const { selection } = view.state;
|
|
54
|
+
if (!selection.empty || !view.editable)
|
|
55
|
+
return;
|
|
56
|
+
const span = addressBefore(view.state, selection.from);
|
|
57
|
+
if (span)
|
|
58
|
+
view.dispatch(linkAt(view.state.tr, view.state, span));
|
|
59
|
+
}
|
|
60
|
+
const autolinkKey = new PluginKey('rich-text-autolink');
|
|
61
|
+
/** A space typed after an address links it. */
|
|
62
|
+
export function autolinkPlugin() {
|
|
63
|
+
return new Plugin({
|
|
64
|
+
key: autolinkKey,
|
|
65
|
+
props: {
|
|
66
|
+
handleTextInput: (view, from, to, text) => {
|
|
67
|
+
if (text !== ' ' || from !== to || view.composing)
|
|
68
|
+
return false;
|
|
69
|
+
const span = addressBefore(view.state, from);
|
|
70
|
+
if (!span)
|
|
71
|
+
return false;
|
|
72
|
+
// The space first, so it takes the words' marks and not the link's.
|
|
73
|
+
const tr = linkAt(view.state.tr.insertText(text, from, to), view.state, span);
|
|
74
|
+
view.dispatch(tr);
|
|
75
|
+
return true;
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Plugin } from '@milkdown/kit/prose/state';
|
|
2
|
+
import type { EditorView } from '@milkdown/kit/prose/view';
|
|
3
|
+
/** Whether a key press leads out of the block at the note's edge, and does it if so. */
|
|
4
|
+
export declare function handleEdgeKey(view: EditorView, event: KeyboardEvent): boolean;
|
|
5
|
+
/** The plugin: the keys at the edges, a click past them, and focus kept on a ticked box. */
|
|
6
|
+
export declare function edgesPlugin(): Plugin;
|
|
7
|
+
/** The plugin, for the editor's `.use()`. */
|
|
8
|
+
export declare const edges: import("@milkdown/utils").$Prose;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// The note's two edges (4.126, the RichTextEditor review's F10): a code
|
|
2
|
+
// block, a table, a quote, a callout or a divider that ends a note had no
|
|
3
|
+
// way past it (Ctrl+Enter, shown nowhere, and none on a phone), and one
|
|
4
|
+
// that starts it nothing above it.
|
|
5
|
+
// - ArrowDown at the end of such a last block, or ArrowRight at its very
|
|
6
|
+
// end, puts a line under it and the caret there; ArrowUp (ArrowLeft) at
|
|
7
|
+
// the start of such a first block, a line above it. Anywhere else the
|
|
8
|
+
// keys are left alone.
|
|
9
|
+
// - A click under the last block (in the text's padding or the space under
|
|
10
|
+
// a short note) puts the caret on a line under it: a new one, unless the
|
|
11
|
+
// last block is already a paragraph. A click above the first block, a
|
|
12
|
+
// line above it when that block isn't one.
|
|
13
|
+
// - Ticking a task's box keeps focus in the text (the review's F24), so
|
|
14
|
+
// Ctrl+Z takes the tick back: from outside, the caret lands in that item.
|
|
15
|
+
// No trailing-paragraph rule: a note isn't given a line nobody typed, so
|
|
16
|
+
// nothing is saved until one is asked for. And no gap cursor, which can't
|
|
17
|
+
// stand by a code block (a textblock).
|
|
18
|
+
import { Plugin, Selection, TextSelection } from '@milkdown/kit/prose/state';
|
|
19
|
+
import { $prose } from '@milkdown/kit/utils';
|
|
20
|
+
// The blocks the keys lead out of: those Enter doesn't (a code block keeps
|
|
21
|
+
// its lines, a table's last row has its own Enter), or does only twice.
|
|
22
|
+
const BOXED = new Set(['code_block', 'table', 'blockquote', 'callout', 'hr']);
|
|
23
|
+
// Whether the selection is at the note's edge, in the block there that the
|
|
24
|
+
// keys lead out of: on its last (first) line, or its table's last (first)
|
|
25
|
+
// row; for `whole`, at its very end (start), as ArrowRight needs.
|
|
26
|
+
function atEdge(view, side, whole) {
|
|
27
|
+
const { doc, selection } = view.state;
|
|
28
|
+
const block = side === 'end' ? doc.lastChild : doc.firstChild;
|
|
29
|
+
if (!block || !BOXED.has(block.type.name))
|
|
30
|
+
return false;
|
|
31
|
+
const edge = side === 'end' ? Selection.atEnd(doc) : Selection.atStart(doc);
|
|
32
|
+
if (selection.eq(edge))
|
|
33
|
+
return true;
|
|
34
|
+
if (!selection.empty || whole)
|
|
35
|
+
return false;
|
|
36
|
+
const $head = selection.$head;
|
|
37
|
+
if ($head.index(0) !== (side === 'end' ? doc.childCount - 1 : 0))
|
|
38
|
+
return false;
|
|
39
|
+
if (!view.endOfTextblock(side === 'end' ? 'down' : 'up'))
|
|
40
|
+
return false;
|
|
41
|
+
if ($head.parent === edge.$head.parent)
|
|
42
|
+
return true;
|
|
43
|
+
// In a table: any cell of its last (first) row.
|
|
44
|
+
return block.type.name === 'table' && $head.depth >= 2 && $head.index(1) === (side === 'end' ? block.childCount - 1 : 0);
|
|
45
|
+
}
|
|
46
|
+
// A paragraph at the note's edge, the caret in it, scrolled to.
|
|
47
|
+
function addLine(view, side) {
|
|
48
|
+
const { state } = view;
|
|
49
|
+
const at = side === 'end' ? state.doc.content.size : 0;
|
|
50
|
+
const tr = state.tr.insert(at, state.schema.nodes.paragraph.create());
|
|
51
|
+
view.dispatch(tr.setSelection(TextSelection.create(tr.doc, at + 1)).scrollIntoView());
|
|
52
|
+
}
|
|
53
|
+
// The caret at the note's edge (a paragraph there), or a new line past the
|
|
54
|
+
// block that's there.
|
|
55
|
+
function toEdge(view, side) {
|
|
56
|
+
const { doc } = view.state;
|
|
57
|
+
const block = (side === 'end' ? doc.lastChild : doc.firstChild);
|
|
58
|
+
if (block.type.name === 'paragraph') {
|
|
59
|
+
const selection = side === 'end' ? Selection.atEnd(doc) : Selection.atStart(doc);
|
|
60
|
+
view.dispatch(view.state.tr.setSelection(selection).scrollIntoView());
|
|
61
|
+
}
|
|
62
|
+
else
|
|
63
|
+
addLine(view, side);
|
|
64
|
+
view.focus();
|
|
65
|
+
}
|
|
66
|
+
/** Whether a key press leads out of the block at the note's edge, and does it if so. */
|
|
67
|
+
export function handleEdgeKey(view, event) {
|
|
68
|
+
if (event.metaKey || event.ctrlKey || event.altKey || event.shiftKey || event.isComposing)
|
|
69
|
+
return false;
|
|
70
|
+
const side = event.key === 'ArrowDown' || event.key === 'ArrowRight' ? 'end' : event.key === 'ArrowUp' || event.key === 'ArrowLeft' ? 'start' : null;
|
|
71
|
+
if (!side || !atEdge(view, side, event.key === 'ArrowRight' || event.key === 'ArrowLeft'))
|
|
72
|
+
return false;
|
|
73
|
+
event.preventDefault();
|
|
74
|
+
addLine(view, side);
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
// Which edge a click is past: under the last block's box or above the
|
|
78
|
+
// first's, or neither.
|
|
79
|
+
function clickedPast(view, event) {
|
|
80
|
+
const first = view.dom.firstElementChild;
|
|
81
|
+
const last = view.dom.lastElementChild;
|
|
82
|
+
if (!first || !last)
|
|
83
|
+
return null;
|
|
84
|
+
if (event.clientY > last.getBoundingClientRect().bottom)
|
|
85
|
+
return 'end';
|
|
86
|
+
if (event.clientY < first.getBoundingClientRect().top)
|
|
87
|
+
return 'start';
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
// What a press below or above the text is for: the element the text sits
|
|
91
|
+
// in, padding and all (Milkdown's root, around its own wrapper).
|
|
92
|
+
function frameOf(view) {
|
|
93
|
+
return view.dom.parentElement?.parentElement ?? view.dom;
|
|
94
|
+
}
|
|
95
|
+
function isTaskBox(target) {
|
|
96
|
+
return target instanceof HTMLInputElement && target.type === 'checkbox' && target.closest('li')?.dataset.itemType === 'task';
|
|
97
|
+
}
|
|
98
|
+
/** The plugin: the keys at the edges, a click past them, and focus kept on a ticked box. */
|
|
99
|
+
export function edgesPlugin() {
|
|
100
|
+
return new Plugin({
|
|
101
|
+
props: {
|
|
102
|
+
handleKeyDown: (view, event) => view.editable && handleEdgeKey(view, event),
|
|
103
|
+
},
|
|
104
|
+
view: (view) => {
|
|
105
|
+
const frame = frameOf(view);
|
|
106
|
+
const press = (event) => {
|
|
107
|
+
if (event.button !== 0 || event.shiftKey || event.metaKey || event.ctrlKey || !view.editable)
|
|
108
|
+
return;
|
|
109
|
+
// Only presses on the text's own surface: not a node view's
|
|
110
|
+
// control, a menu or a toolbar in the frame.
|
|
111
|
+
const target = event.target;
|
|
112
|
+
if (target !== frame && target !== view.dom && target !== view.dom.parentElement)
|
|
113
|
+
return;
|
|
114
|
+
const side = clickedPast(view, event);
|
|
115
|
+
if (!side)
|
|
116
|
+
return;
|
|
117
|
+
event.preventDefault();
|
|
118
|
+
toEdge(view, side);
|
|
119
|
+
};
|
|
120
|
+
// The box's own click (milkdown-editor.tsx) ticks it; this brings focus
|
|
121
|
+
// back to the text after, the caret in that item when it was elsewhere.
|
|
122
|
+
const ticked = (event) => {
|
|
123
|
+
if (!isTaskBox(event.target) || !view.editable)
|
|
124
|
+
return;
|
|
125
|
+
const text = event.target.nextElementSibling;
|
|
126
|
+
if (!view.hasFocus() && text) {
|
|
127
|
+
const $at = view.state.doc.resolve(view.posAtDOM(text, 0));
|
|
128
|
+
view.dispatch(view.state.tr.setSelection(Selection.near($at)));
|
|
129
|
+
}
|
|
130
|
+
view.focus();
|
|
131
|
+
};
|
|
132
|
+
frame.addEventListener('mousedown', press);
|
|
133
|
+
view.dom.addEventListener('click', ticked);
|
|
134
|
+
return {
|
|
135
|
+
destroy: () => {
|
|
136
|
+
frame.removeEventListener('mousedown', press);
|
|
137
|
+
view.dom.removeEventListener('click', ticked);
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
/** The plugin, for the editor's `.use()`. */
|
|
144
|
+
export const edges = $prose(() => edgesPlugin());
|
|
@@ -10,9 +10,22 @@ export type RichTextEditorProps = {
|
|
|
10
10
|
* value it reported, passed back, changes nothing.
|
|
11
11
|
*/
|
|
12
12
|
defaultValue: string;
|
|
13
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* The Markdown to save, untouched blocks as originally written. Called a
|
|
15
|
+
* short pause after typing stops (300 ms), not on every
|
|
16
|
+
* key: turning a long note back into Markdown costs time in proportion
|
|
17
|
+
* to its length. Always called first, synchronously, when the
|
|
18
|
+
* text loses focus, on Esc and Ctrl/Cmd+Enter, when the page is hidden,
|
|
19
|
+
* when it unmounts, and when anything calls `flushPendingEdits()`
|
|
20
|
+
* (EntityForm's submit, the edit-in-place store's saves, the leave
|
|
21
|
+
* guards), so a save never reads a value from before the last key.
|
|
22
|
+
*/
|
|
14
23
|
onChange: (markdown: string) => void;
|
|
15
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* Why the edit can't be saved as it stands, or `null` once it can. A form
|
|
26
|
+
* sets it as the field's error and refuses submit while it stands. Left
|
|
27
|
+
* out, the editor shows the message inside the field instead.
|
|
28
|
+
*/
|
|
16
29
|
onProblem?: (message: string | null) => void;
|
|
17
30
|
/** Shown but not editable (while a save is in flight). */
|
|
18
31
|
readOnly?: boolean;
|
|
@@ -7,8 +7,12 @@ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-run
|
|
|
7
7
|
// and saving round-trip exactly: an untouched document comes back byte for
|
|
8
8
|
// byte, and an edited one keeps every block the person didn't change as it
|
|
9
9
|
// was written (src/lib/markdown-merge.ts). If an edit can't be saved
|
|
10
|
-
// without rewriting another block, `
|
|
11
|
-
//
|
|
10
|
+
// without rewriting another block, `onChange` isn't called (the draft
|
|
11
|
+
// stays on screen, nothing is saved in its place), the editor marks itself
|
|
12
|
+
// invalid, and `onProblem` says why; with no `onProblem`, the editor says
|
|
13
|
+
// it itself, inside the field. A form gives `onProblem` and holds its
|
|
14
|
+
// submit on it (add-an-entity.md "The form"), or Save would send the last
|
|
15
|
+
// text that could be saved and say it worked.
|
|
12
16
|
//
|
|
13
17
|
// Raw HTML in the text shows as its characters, as <Markdown> shows it,
|
|
14
18
|
// and nothing pasted or typed can add markup.
|
|
@@ -53,7 +57,7 @@ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-run
|
|
|
53
57
|
// Markdown never downloads it. Until it arrives the text shows as
|
|
54
58
|
// <Markdown> draws it (4.123), the field's size, so nothing moves when the
|
|
55
59
|
// editor takes its place; an empty one shows skeleton lines.
|
|
56
|
-
import { lazy, Suspense, useState } from 'react';
|
|
60
|
+
import { lazy, Suspense, useLayoutEffect, useRef, useState } from 'react';
|
|
57
61
|
import { cn } from 'cn';
|
|
58
62
|
import { Skeleton } from '../ui/skeleton.js';
|
|
59
63
|
import { Markdown } from './markdown.js';
|
|
@@ -78,9 +82,17 @@ function useOpenings(defaultValue) {
|
|
|
78
82
|
export function RichTextEditor(props) {
|
|
79
83
|
const { opening, report } = useOpenings(props.defaultValue);
|
|
80
84
|
const { onChange } = props;
|
|
81
|
-
|
|
85
|
+
// The editor reports what was typed as it unmounts (F6); one replaced
|
|
86
|
+
// by a new value (a reset) has nothing left to say.
|
|
87
|
+
const live = useRef(opening);
|
|
88
|
+
useLayoutEffect(() => {
|
|
89
|
+
live.current = opening;
|
|
90
|
+
});
|
|
91
|
+
return (_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',
|
|
82
92
|
// The room of the add-row bar the editor draws under a table.
|
|
83
93
|
!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) => {
|
|
94
|
+
if (live.current !== opening)
|
|
95
|
+
return;
|
|
84
96
|
report(markdown);
|
|
85
97
|
onChange(markdown);
|
|
86
98
|
}, foldKey: props.foldable === false ? undefined : props.foldKey }, opening) }));
|
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
import type { NodeSchema } from '@milkdown/kit/transformer';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
3
|
+
* How a paragraph goes to Markdown, past Milkdown's own:
|
|
4
|
+
*
|
|
5
|
+
* An empty paragraph goes with `children: []`. Milkdown's own runner opens
|
|
6
|
+
* and closes the node with nothing in it, and its serializer then leaves
|
|
7
|
+
* `children` out. mdast says a paragraph always has the array, and
|
|
8
|
+
* mdast-util-gfm-task-list-item 2.0.1 reads its length: an empty task item
|
|
9
|
+
* ("- [ ] ", Enter at a list's end) threw there, and the edit was never
|
|
10
|
+
* saved.
|
|
11
|
+
*
|
|
12
|
+
* And a paragraph's spaces at its end are left out (4.126, the
|
|
13
|
+
* RichTextEditor review's F22). Markdown drops them on reading, so a pause
|
|
14
|
+
* after a space saved a note that read back without it (and remark-stringify
|
|
15
|
+
* sometimes wrote it as ` `). A hard break's spaces aren't these: it is
|
|
16
|
+
* a node of its own.
|
|
9
17
|
*/
|
|
10
18
|
export declare function withEmptyParagraphChildren<Ctx>(previous: (ctx: Ctx) => NodeSchema): (ctx: Ctx) => NodeSchema;
|
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
2
|
+
* How a paragraph goes to Markdown, past Milkdown's own:
|
|
3
|
+
*
|
|
4
|
+
* An empty paragraph goes with `children: []`. Milkdown's own runner opens
|
|
5
|
+
* and closes the node with nothing in it, and its serializer then leaves
|
|
6
|
+
* `children` out. mdast says a paragraph always has the array, and
|
|
7
|
+
* mdast-util-gfm-task-list-item 2.0.1 reads its length: an empty task item
|
|
8
|
+
* ("- [ ] ", Enter at a list's end) threw there, and the edit was never
|
|
9
|
+
* saved.
|
|
10
|
+
*
|
|
11
|
+
* And a paragraph's spaces at its end are left out (4.126, the
|
|
12
|
+
* RichTextEditor review's F22). Markdown drops them on reading, so a pause
|
|
13
|
+
* after a space saved a note that read back without it (and remark-stringify
|
|
14
|
+
* sometimes wrote it as ` `). A hard break's spaces aren't these: it is
|
|
15
|
+
* a node of its own.
|
|
8
16
|
*/
|
|
9
17
|
export function withEmptyParagraphChildren(previous) {
|
|
10
18
|
return (ctx) => {
|
|
@@ -15,10 +23,13 @@ export function withEmptyParagraphChildren(previous) {
|
|
|
15
23
|
toMarkdown: {
|
|
16
24
|
...base.toMarkdown,
|
|
17
25
|
runner: (state, node) => {
|
|
18
|
-
|
|
26
|
+
const last = node.lastChild;
|
|
27
|
+
const spaces = last?.isText ? (/[ \t]+$/.exec(last.text ?? '')?.[0].length ?? 0) : 0;
|
|
28
|
+
const kept = spaces ? node.copy(node.content.cut(0, node.content.size - spaces)) : node;
|
|
29
|
+
if (kept.content.size === 0)
|
|
19
30
|
state.addNode('paragraph', []);
|
|
20
31
|
else
|
|
21
|
-
runner(state,
|
|
32
|
+
runner(state, kept);
|
|
22
33
|
},
|
|
23
34
|
},
|
|
24
35
|
};
|
|
@@ -165,6 +165,25 @@ function convertList(tr, type, checked) {
|
|
|
165
165
|
}
|
|
166
166
|
return tr;
|
|
167
167
|
}
|
|
168
|
+
// The list the caret is in, joined to a list of its type right before or
|
|
169
|
+
// after it. Two lists side by side are one in Markdown ("- a" then "- b"
|
|
170
|
+
// read back as one list), so left apart the note would read back other
|
|
171
|
+
// than it shows, and the merge refuses the edit.
|
|
172
|
+
function joinNeighbours(tr) {
|
|
173
|
+
const $from = tr.selection.$from;
|
|
174
|
+
for (let depth = $from.depth; depth > 0; depth--) {
|
|
175
|
+
if (!isList($from.node(depth)))
|
|
176
|
+
continue;
|
|
177
|
+
const after = $from.after(depth);
|
|
178
|
+
if (tr.doc.resolve(after).nodeAfter?.type === $from.node(depth).type)
|
|
179
|
+
tr.join(after);
|
|
180
|
+
const before = $from.before(depth);
|
|
181
|
+
if (tr.doc.resolve(before).nodeBefore?.type === $from.node(depth).type)
|
|
182
|
+
tr.join(before);
|
|
183
|
+
return tr;
|
|
184
|
+
}
|
|
185
|
+
return tr;
|
|
186
|
+
}
|
|
168
187
|
/**
|
|
169
188
|
* Turns a format on, or off when it's already on, where the caret is.
|
|
170
189
|
* Returns false when it can't apply there. `link` isn't run here: the
|
|
@@ -190,13 +209,13 @@ export function toggleFormat(state, dispatch, id) {
|
|
|
190
209
|
if (level) {
|
|
191
210
|
if (on)
|
|
192
211
|
return setBlockType(paragraph)(state, dispatch);
|
|
193
|
-
return setBlockType(schema.nodes.heading, { level, toggle: id in TOGGLE_LEVEL })
|
|
212
|
+
return outOfLists(state, dispatch, setBlockType(schema.nodes.heading, { level, toggle: id in TOGGLE_LEVEL }));
|
|
194
213
|
}
|
|
195
214
|
// The toolbar's Toggle heading: a heading turned toggle or plain, its
|
|
196
215
|
// level kept; any other line a toggle `##`.
|
|
197
216
|
if (id === 'toggleHeading') {
|
|
198
217
|
const here = headingHere(state);
|
|
199
|
-
return setBlockType(schema.nodes.heading, { level: here?.level ?? 2, toggle: !here?.toggle })
|
|
218
|
+
return outOfLists(state, dispatch, setBlockType(schema.nodes.heading, { level: here?.level ?? 2, toggle: !here?.toggle }));
|
|
200
219
|
}
|
|
201
220
|
const kind = calloutKindOf(id);
|
|
202
221
|
if (kind)
|
|
@@ -209,9 +228,12 @@ export function toggleFormat(state, dispatch, id) {
|
|
|
209
228
|
case 'table':
|
|
210
229
|
return on ? false : insertBlock(state, dispatch, createTable(schema), true);
|
|
211
230
|
case 'codeBlock':
|
|
212
|
-
return on ? setBlockType(paragraph)(state, dispatch) : setBlockType(schema.nodes.code_block)
|
|
231
|
+
return on ? setBlockType(paragraph)(state, dispatch) : outOfLists(state, dispatch, setBlockType(schema.nodes.code_block));
|
|
213
232
|
case 'quote':
|
|
214
|
-
|
|
233
|
+
if (on)
|
|
234
|
+
return lift(state, dispatch);
|
|
235
|
+
// A list item's line can't be wrapped where it is: out of the list first.
|
|
236
|
+
return wrapIn(schema.nodes.blockquote)(state, dispatch) || outOfLists(state, dispatch, wrapIn(schema.nodes.blockquote));
|
|
215
237
|
case 'bullets':
|
|
216
238
|
case 'numbers':
|
|
217
239
|
case 'tasks': {
|
|
@@ -223,16 +245,62 @@ export function toggleFormat(state, dispatch, id) {
|
|
|
223
245
|
dispatch(convertList(state.tr, type, checked));
|
|
224
246
|
return true;
|
|
225
247
|
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
248
|
+
const toList = (from, send) => {
|
|
249
|
+
let wrapped = null;
|
|
250
|
+
if (!wrapInList(type)(from, (tr) => (wrapped = tr)))
|
|
251
|
+
return false;
|
|
252
|
+
send?.(joinNeighbours(convertList(wrapped, type, checked)));
|
|
253
|
+
return true;
|
|
254
|
+
};
|
|
255
|
+
// A heading or a code block is made a plain line first: a list
|
|
256
|
+
// item starts with one (Ctrl+Shift+5 on a heading, the review's F24).
|
|
257
|
+
const parent = state.selection.$from.parent;
|
|
258
|
+
if (parent.type !== paragraph && parent.isTextblock)
|
|
259
|
+
return chain(state, dispatch, [setBlockType(paragraph), toList]);
|
|
260
|
+
return toList(state, dispatch);
|
|
231
261
|
}
|
|
232
262
|
default:
|
|
233
263
|
return false;
|
|
234
264
|
}
|
|
235
265
|
}
|
|
266
|
+
// Commands run one after another, each on the state the last left, as one
|
|
267
|
+
// transaction (one undo step). False, with nothing done, when one can't.
|
|
268
|
+
function chain(state, dispatch, commands) {
|
|
269
|
+
const tr = state.tr;
|
|
270
|
+
let current = state;
|
|
271
|
+
for (const command of commands) {
|
|
272
|
+
let made = null;
|
|
273
|
+
if (!command(current, (next) => (made = next)))
|
|
274
|
+
return false;
|
|
275
|
+
const done = made;
|
|
276
|
+
if (!done)
|
|
277
|
+
continue;
|
|
278
|
+
for (const step of done.steps)
|
|
279
|
+
tr.step(step);
|
|
280
|
+
tr.setSelection(done.selection.getBookmark().resolve(tr.doc));
|
|
281
|
+
current = current.apply(done);
|
|
282
|
+
}
|
|
283
|
+
dispatch(tr.scrollIntoView());
|
|
284
|
+
return true;
|
|
285
|
+
}
|
|
286
|
+
// `command` on the caret's line once it's out of every list it's in: a
|
|
287
|
+
// list item's line can't be a heading or a code block (Ctrl+Shift+1 on an
|
|
288
|
+
// item, the review's F24).
|
|
289
|
+
function outOfLists(state, dispatch, command) {
|
|
290
|
+
if (!ancestor(state, isList))
|
|
291
|
+
return command(state, dispatch);
|
|
292
|
+
const lift = liftListItem(state.schema.nodes.list_item);
|
|
293
|
+
const commands = [];
|
|
294
|
+
let current = state;
|
|
295
|
+
while (ancestor(current, isList)) {
|
|
296
|
+
let lifted = null;
|
|
297
|
+
if (!lift(current, (tr) => (lifted = tr)) || !lifted)
|
|
298
|
+
return false;
|
|
299
|
+
current = current.apply(lifted);
|
|
300
|
+
commands.push(lift);
|
|
301
|
+
}
|
|
302
|
+
return chain(state, dispatch, [...commands, command]);
|
|
303
|
+
}
|
|
236
304
|
// The line the caret is in as plain text (Notion's "Text"): out of a
|
|
237
305
|
// heading or code block, a list, a quote. False when it already is.
|
|
238
306
|
function toText(state, dispatch) {
|
|
@@ -251,8 +319,10 @@ function toText(state, dispatch) {
|
|
|
251
319
|
// callout taken away, its blocks kept.
|
|
252
320
|
function toggleCallout(state, dispatch, kind) {
|
|
253
321
|
const callout = ancestor(state, (node) => node.type.name === 'callout');
|
|
254
|
-
if (!callout)
|
|
255
|
-
|
|
322
|
+
if (!callout) {
|
|
323
|
+
const wrap = wrapIn(state.schema.nodes.callout, { kind });
|
|
324
|
+
return wrap(state, dispatch) || outOfLists(state, dispatch, wrap);
|
|
325
|
+
}
|
|
256
326
|
const { node, pos } = callout;
|
|
257
327
|
if (node.attrs.kind !== kind) {
|
|
258
328
|
dispatch(state.tr.setNodeMarkup(pos, undefined, { ...node.attrs, kind }));
|
|
@@ -283,11 +353,14 @@ function insertBlock(state, dispatch, block, into) {
|
|
|
283
353
|
const empty = $from.parent.content.size === 0;
|
|
284
354
|
const followed = index + 1 < container.childCount;
|
|
285
355
|
const content = Fragment.from(into && followed ? [block] : [block, state.schema.nodes.paragraph.create()]);
|
|
286
|
-
|
|
287
|
-
|
|
356
|
+
// An empty line is replaced, unless it can't be (a list item's first
|
|
357
|
+
// line): then, as after words, the block goes under it.
|
|
358
|
+
const replace = empty && container.canReplace(index, index + 1, content);
|
|
359
|
+
const at = replace ? index : index + 1;
|
|
360
|
+
if (!container.canReplace(at, replace ? index + 1 : at, content))
|
|
288
361
|
return false;
|
|
289
|
-
const from =
|
|
290
|
-
const tr = state.tr.replaceWith(from,
|
|
362
|
+
const from = replace ? $from.before(depth) : $from.after(depth);
|
|
363
|
+
const tr = state.tr.replaceWith(from, replace ? $from.after(depth) : from, content);
|
|
291
364
|
tr.setSelection(TextSelection.near(tr.doc.resolve(into ? from + 1 : from + block.nodeSize), 1));
|
|
292
365
|
dispatch(tr.scrollIntoView());
|
|
293
366
|
return true;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// While the rich-text editor is full screen, the page behind it is out of
|
|
2
|
+
// reach: every sibling of the field, and of each element above it up to
|
|
3
|
+
// <body>, is made `inert` (no focus, no clicks, hidden from screen
|
|
4
|
+
// readers), so Tab stays in the field and a reader reads only it. The
|
|
5
|
+
// toasts stay live wherever the app renders them (a save's failure still
|
|
6
|
+
// says so), and what a portal puts under <body> once full screen is on (a
|
|
7
|
+
// menu, a popover, the link box) is none of those siblings, so it works
|
|
8
|
+
// as ever. Each element gets back the `inert` it had when full screen
|
|
9
|
+
// ends or the field goes.
|
|
10
|
+
import { useEffect } from 'react';
|
|
11
|
+
// What stays live beside the field: the toasts' region.
|
|
12
|
+
function liveRegions() {
|
|
13
|
+
return Array.from(document.querySelectorAll('[data-sonner-toaster]')).map((list) => list.closest('section') ?? list);
|
|
14
|
+
}
|
|
15
|
+
const TABBABLE = 'a[href], button, input, select, textarea, [tabindex], [contenteditable="true"]';
|
|
16
|
+
// The field's tab stops, in order: what Tab can reach in it now. The
|
|
17
|
+
// editable text is one (whatever its tabIndex says); what's inside it,
|
|
18
|
+
// its links included, is none.
|
|
19
|
+
function tabStops(field) {
|
|
20
|
+
return Array.from(field.querySelectorAll(TABBABLE)).filter((element) => (element.getAttribute('contenteditable') === 'true' ||
|
|
21
|
+
(element.tabIndex >= 0 && !element.parentElement?.closest('[contenteditable="true"]'))) &&
|
|
22
|
+
!element.matches(':disabled') &&
|
|
23
|
+
!element.closest('[inert], [hidden]') &&
|
|
24
|
+
element.getClientRects().length > 0);
|
|
25
|
+
}
|
|
26
|
+
/** Holds focus in `field` while `on`: the rest of the page inert. */
|
|
27
|
+
export function useHoldFocus(field, on) {
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (!on || !field)
|
|
30
|
+
return;
|
|
31
|
+
const live = [field, ...liveRegions()];
|
|
32
|
+
// Every element on the way from a live one up to <body>.
|
|
33
|
+
const path = new Set();
|
|
34
|
+
for (const element of live) {
|
|
35
|
+
for (let node = element; node && node !== document.documentElement; node = node.parentElement)
|
|
36
|
+
path.add(node);
|
|
37
|
+
}
|
|
38
|
+
const changed = [];
|
|
39
|
+
for (const element of path) {
|
|
40
|
+
if (live.includes(element))
|
|
41
|
+
continue;
|
|
42
|
+
for (const child of Array.from(element.children)) {
|
|
43
|
+
if (path.has(child) || !(child instanceof HTMLElement) || child.inert)
|
|
44
|
+
continue;
|
|
45
|
+
if (child.tagName === 'SCRIPT' || child.tagName === 'STYLE')
|
|
46
|
+
continue;
|
|
47
|
+
child.inert = true;
|
|
48
|
+
changed.push(child);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// Tab from the field's last stop goes round to its first (and
|
|
52
|
+
// Shift+Tab the other way), rather than out to the browser.
|
|
53
|
+
const wrap = (event) => {
|
|
54
|
+
if (event.key !== 'Tab' || event.altKey || event.ctrlKey || event.metaKey || event.defaultPrevented)
|
|
55
|
+
return;
|
|
56
|
+
const stops = tabStops(field);
|
|
57
|
+
if (stops.length === 0)
|
|
58
|
+
return;
|
|
59
|
+
const first = stops[0];
|
|
60
|
+
const last = stops[stops.length - 1];
|
|
61
|
+
const active = document.activeElement;
|
|
62
|
+
if (!event.shiftKey && active === last) {
|
|
63
|
+
event.preventDefault();
|
|
64
|
+
first.focus();
|
|
65
|
+
}
|
|
66
|
+
else if (event.shiftKey && active === first) {
|
|
67
|
+
event.preventDefault();
|
|
68
|
+
last.focus();
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
field.addEventListener('keydown', wrap);
|
|
72
|
+
return () => {
|
|
73
|
+
field.removeEventListener('keydown', wrap);
|
|
74
|
+
for (const element of changed)
|
|
75
|
+
element.inert = false;
|
|
76
|
+
};
|
|
77
|
+
}, [field, on]);
|
|
78
|
+
}
|
|
@@ -13,6 +13,8 @@ export interface RichTextHighlightMenuProps {
|
|
|
13
13
|
onFormat: (id: FormatId) => void;
|
|
14
14
|
/** The editable element, where focus goes once an item is picked. */
|
|
15
15
|
text: () => HTMLElement | null;
|
|
16
|
+
/** More for the trigger's look (a touch screen's 40px). */
|
|
17
|
+
className?: string;
|
|
16
18
|
ref?: Ref<HTMLButtonElement>;
|
|
17
19
|
}
|
|
18
|
-
export declare function RichTextHighlightMenu({ state, readOnly, tabIndex, onFocus, onKeyDown, onFormat, text, ref }: RichTextHighlightMenuProps): import("react").JSX.Element;
|
|
20
|
+
export declare function RichTextHighlightMenu({ state, readOnly, tabIndex, onFocus, onKeyDown, onFormat, text, className, ref }: RichTextHighlightMenuProps): import("react").JSX.Element;
|