@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
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
// What a paste into the rich-text editor arrives as (milkdown-editor.tsx
|
|
2
|
+
// wires each piece in):
|
|
3
|
+
//
|
|
4
|
+
// - pasteHtml, before ProseMirror reads a paste's HTML: Word's list
|
|
5
|
+
// paragraphs (`mso-list`) become list items, a Notion or GitHub to-do a
|
|
6
|
+
// task item with its tick, and `language-*` on code a code block's
|
|
7
|
+
// language.
|
|
8
|
+
// - cleanPasted, on what ProseMirror read from the HTML: raw HTML chips
|
|
9
|
+
// become their text; a link whose address linkHref refuses
|
|
10
|
+
// (`javascript:`) keeps its words, not the link; an image whose address
|
|
11
|
+
// is refused is dropped, and one carried in the HTML as `data:` is taken
|
|
12
|
+
// out to be uploaded (with `upload`) or said no to; and in a table cell,
|
|
13
|
+
// the blocks join with spaces, as a GFM cell is one line.
|
|
14
|
+
// - pastePlainText, for a paste with no HTML: read as Markdown (still no
|
|
15
|
+
// HTML), its single line breaks kept as hard breaks (an address pasted
|
|
16
|
+
// as three lines stays three lines); a web or email address alone
|
|
17
|
+
// becomes a link, or links the words selected; text into a table cell
|
|
18
|
+
// joins its lines with spaces.
|
|
19
|
+
//
|
|
20
|
+
// React-free; the DOM only through DOMParser and File. Its rules are unit
|
|
21
|
+
// tested in a browser by e2e/rich-text-editor.spec.ts "paste".
|
|
22
|
+
import { Fragment, Slice } from '@milkdown/kit/prose/model';
|
|
23
|
+
import { linkHref } from '../../lib/link-href.js';
|
|
24
|
+
/** What a paste says when it carries images the field can't take. */
|
|
25
|
+
export const NO_IMAGES_HERE = "Images can't be added here.";
|
|
26
|
+
// A web or email address and nothing else (spaces at its ends aside):
|
|
27
|
+
// `https://…`, `http://…`, `www.…`, `name@example.com`, or `mailto:`.
|
|
28
|
+
const WEB = /^(?:https?:\/\/[^\s<>]+\.?[^\s<>]*|www\.[^\s<>.]+\.[^\s<>]+)$/i;
|
|
29
|
+
const EMAIL = /^(?:mailto:)?[^\s@<>()[\]]+@[^\s@<>()[\]]+\.[a-z]{2,}$/i;
|
|
30
|
+
/** The address `text` is, when it is one web or email address alone; else `null`. */
|
|
31
|
+
export function addressOf(text) {
|
|
32
|
+
const value = text.trim();
|
|
33
|
+
if (!WEB.test(value) && !EMAIL.test(value))
|
|
34
|
+
return null;
|
|
35
|
+
if (/^https?:\/\/$/i.test(value))
|
|
36
|
+
return null;
|
|
37
|
+
const href = linkHref(value);
|
|
38
|
+
return href ? href : null;
|
|
39
|
+
}
|
|
40
|
+
/** Whether the caret (or the selection's start) is in a table cell. */
|
|
41
|
+
function inTableCell(view) {
|
|
42
|
+
const { $from } = view.state.selection;
|
|
43
|
+
for (let depth = $from.depth; depth > 0; depth--) {
|
|
44
|
+
const role = $from.node(depth).type.spec.tableRole;
|
|
45
|
+
if (role === 'cell' || role === 'header_cell')
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
function inCode(view) {
|
|
51
|
+
const { $from, from, to } = view.state.selection;
|
|
52
|
+
if ($from.parent.type.spec.code)
|
|
53
|
+
return true;
|
|
54
|
+
let code = false;
|
|
55
|
+
view.state.doc.nodesBetween(from, Math.max(to, from + 1), (node) => {
|
|
56
|
+
if (node.marks.some((mark) => mark.type.spec.code))
|
|
57
|
+
code = true;
|
|
58
|
+
});
|
|
59
|
+
return code || $from.marks().some((mark) => mark.type.spec.code);
|
|
60
|
+
}
|
|
61
|
+
// ---------------------------------------------------------------- HTML
|
|
62
|
+
const MSO_LEVEL = /mso-list:\s*\S+\s+level(\d+)/i;
|
|
63
|
+
// Word's list marker, for whether its list is numbered: `1.`, `a)`, `iv.`.
|
|
64
|
+
function wordMarkerOrdered(marker) {
|
|
65
|
+
return /^\(?[0-9a-z]{1,4}[.)]$/i.test(marker.replace(/\s+/g, ''));
|
|
66
|
+
}
|
|
67
|
+
// Word writes a list as paragraphs, each with its level in its style and
|
|
68
|
+
// its marker (`·` and spaces, or `1.`) in a span it hides from itself.
|
|
69
|
+
// Each run of them becomes a list, nested by level.
|
|
70
|
+
function wordLists(doc) {
|
|
71
|
+
const paragraphs = Array.from(doc.querySelectorAll('p')).filter((p) => MSO_LEVEL.test(p.getAttribute('style') ?? ''));
|
|
72
|
+
const done = new Set();
|
|
73
|
+
for (const first of paragraphs) {
|
|
74
|
+
if (done.has(first))
|
|
75
|
+
continue;
|
|
76
|
+
const run = [];
|
|
77
|
+
let next = first;
|
|
78
|
+
while (next && next.tagName === 'P' && MSO_LEVEL.test(next.getAttribute('style') ?? '')) {
|
|
79
|
+
run.push(next);
|
|
80
|
+
done.add(next);
|
|
81
|
+
next = next.nextElementSibling;
|
|
82
|
+
}
|
|
83
|
+
const root = doc.createElement('div');
|
|
84
|
+
first.before(root);
|
|
85
|
+
// The open lists, one per level.
|
|
86
|
+
const stack = [];
|
|
87
|
+
for (const paragraph of run) {
|
|
88
|
+
const level = Number(MSO_LEVEL.exec(paragraph.getAttribute('style') ?? '')[1]);
|
|
89
|
+
const ignored = Array.from(paragraph.querySelectorAll('span')).filter((span) => /mso-list:\s*ignore/i.test(span.getAttribute('style') ?? ''));
|
|
90
|
+
const marker = ignored.map((span) => span.textContent ?? '').join('').trim();
|
|
91
|
+
ignored.forEach((span) => span.remove());
|
|
92
|
+
const tag = wordMarkerOrdered(marker) ? 'ol' : 'ul';
|
|
93
|
+
while (stack.length > 0 && stack[stack.length - 1].level > level)
|
|
94
|
+
stack.pop();
|
|
95
|
+
let top = stack[stack.length - 1];
|
|
96
|
+
if (!top || top.level < level || top.list.tagName.toLowerCase() !== tag) {
|
|
97
|
+
if (top && top.level === level)
|
|
98
|
+
stack.pop();
|
|
99
|
+
const list = doc.createElement(tag);
|
|
100
|
+
const parentItem = stack.length > 0 ? stack[stack.length - 1].list.lastElementChild : null;
|
|
101
|
+
(parentItem ?? root).append(list);
|
|
102
|
+
top = { level, list };
|
|
103
|
+
stack.push(top);
|
|
104
|
+
}
|
|
105
|
+
const item = doc.createElement('li');
|
|
106
|
+
const words = doc.createElement('p');
|
|
107
|
+
words.append(...Array.from(paragraph.childNodes));
|
|
108
|
+
item.append(words);
|
|
109
|
+
top.list.append(item);
|
|
110
|
+
paragraph.remove();
|
|
111
|
+
}
|
|
112
|
+
root.replaceWith(...Array.from(root.childNodes));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// A to-do: Notion's (`ul.to-do-list`, its box a `div.checkbox`, ticked
|
|
116
|
+
// `checkbox-on`) or a page's own (`li` holding a checkbox input, as
|
|
117
|
+
// GitHub's). Each becomes the task item Milkdown reads, its box gone.
|
|
118
|
+
function toDos(doc) {
|
|
119
|
+
for (const item of Array.from(doc.querySelectorAll('li'))) {
|
|
120
|
+
const notionBox = item.querySelector(':scope > .checkbox, :scope > div > .checkbox');
|
|
121
|
+
const input = item.querySelector(':scope > input[type=checkbox], :scope > p > input[type=checkbox]');
|
|
122
|
+
const notion = item.closest('ul.to-do-list') !== null || notionBox !== null;
|
|
123
|
+
if (!notion && !input)
|
|
124
|
+
continue;
|
|
125
|
+
const checked = input
|
|
126
|
+
? input.checked || input.hasAttribute('checked')
|
|
127
|
+
: Boolean(notionBox?.classList.contains('checkbox-on') || item.querySelector('.to-do-children-checked'));
|
|
128
|
+
notionBox?.remove();
|
|
129
|
+
input?.remove();
|
|
130
|
+
item.setAttribute('data-item-type', 'task');
|
|
131
|
+
item.setAttribute('data-checked', String(checked));
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
// `<pre><code class="language-js">`: the language onto the code block
|
|
135
|
+
// (Milkdown reads `pre[data-language]`).
|
|
136
|
+
function codeLanguages(doc) {
|
|
137
|
+
for (const pre of Array.from(doc.querySelectorAll('pre'))) {
|
|
138
|
+
if (pre.dataset.language)
|
|
139
|
+
continue;
|
|
140
|
+
const named = [pre, ...Array.from(pre.querySelectorAll('code'))]
|
|
141
|
+
.flatMap((element) => Array.from(element.classList))
|
|
142
|
+
.find((name) => /^(language|lang)-/.test(name));
|
|
143
|
+
const language = named?.replace(/^(language|lang)-/, '').toLowerCase();
|
|
144
|
+
if (language)
|
|
145
|
+
pre.dataset.language = language;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/** A paste's HTML, before ProseMirror reads it: Word's lists, to-dos and code languages made readable. */
|
|
149
|
+
export function pasteHtml(html) {
|
|
150
|
+
if (typeof DOMParser === 'undefined')
|
|
151
|
+
return html;
|
|
152
|
+
if (!/mso-list|checkbox|to-do|language-|lang-/i.test(html))
|
|
153
|
+
return html;
|
|
154
|
+
const doc = new DOMParser().parseFromString(html, 'text/html');
|
|
155
|
+
wordLists(doc);
|
|
156
|
+
toDos(doc);
|
|
157
|
+
codeLanguages(doc);
|
|
158
|
+
return doc.body.innerHTML;
|
|
159
|
+
}
|
|
160
|
+
// ---------------------------------------------------------------- slices
|
|
161
|
+
// A `data:` image's bytes, as a file to upload; `null` when it isn't an
|
|
162
|
+
// image in base64.
|
|
163
|
+
function dataImageFile(src, index) {
|
|
164
|
+
const match = /^data:(image\/[a-z0-9.+-]+);base64,([\s\S]*)$/i.exec(src);
|
|
165
|
+
if (!match)
|
|
166
|
+
return null;
|
|
167
|
+
try {
|
|
168
|
+
const binary = atob(match[2].replace(/\s+/g, ''));
|
|
169
|
+
const bytes = new Uint8Array(binary.length);
|
|
170
|
+
for (let i = 0; i < binary.length; i++)
|
|
171
|
+
bytes[i] = binary.charCodeAt(i);
|
|
172
|
+
const extension = match[1].split('/')[1].replace(/\+.*$/, '');
|
|
173
|
+
return new File([bytes], `pasted-${index + 1}.${extension}`, { type: match[1] });
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
function clean(fragment, found) {
|
|
180
|
+
const nodes = [];
|
|
181
|
+
fragment.forEach((node) => {
|
|
182
|
+
const schema = node.type.schema;
|
|
183
|
+
if (node.type.name === 'html') {
|
|
184
|
+
// Raw HTML as Milkdown's chip becomes the text it shows.
|
|
185
|
+
const value = String(node.attrs.value ?? '');
|
|
186
|
+
if (value)
|
|
187
|
+
nodes.push(schema.text(value, node.marks));
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
if (node.type.name === 'image') {
|
|
191
|
+
const src = String(node.attrs.src ?? '');
|
|
192
|
+
if (/^data:/i.test(src)) {
|
|
193
|
+
const file = dataImageFile(src, found.images.length);
|
|
194
|
+
if (file)
|
|
195
|
+
found.images.push(file);
|
|
196
|
+
else
|
|
197
|
+
found.droppedImages++;
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
if (!src || linkHref(src) === null) {
|
|
201
|
+
found.droppedImages++;
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
// A link linkHref refuses keeps its words.
|
|
206
|
+
const link = schema.marks.link;
|
|
207
|
+
const refused = link ? node.marks.find((mark) => mark.type === link && linkHref(String(mark.attrs.href ?? '')) === null) : undefined;
|
|
208
|
+
const marks = refused ? refused.removeFromSet(node.marks) : node.marks;
|
|
209
|
+
if (node.isText)
|
|
210
|
+
nodes.push(schema.text(node.text ?? '', marks));
|
|
211
|
+
else
|
|
212
|
+
nodes.push(node.type.create(node.attrs, clean(node.content, found), marks));
|
|
213
|
+
});
|
|
214
|
+
return Fragment.fromArray(nodes);
|
|
215
|
+
}
|
|
216
|
+
// What a cell can hold of a paste: every block's words, joined by spaces
|
|
217
|
+
// (a hard break is a space too). A pasted table is left alone: the
|
|
218
|
+
// table's own paste puts its cells in place.
|
|
219
|
+
function forCell(slice, schema) {
|
|
220
|
+
let hasTable = false;
|
|
221
|
+
let blocks = 0;
|
|
222
|
+
slice.content.descendants((node) => {
|
|
223
|
+
if (node.type.spec.tableRole)
|
|
224
|
+
hasTable = true;
|
|
225
|
+
if (node.isTextblock)
|
|
226
|
+
blocks++;
|
|
227
|
+
return !hasTable;
|
|
228
|
+
});
|
|
229
|
+
const onlyParagraph = slice.content.childCount === 1 && slice.content.firstChild?.type === schema.nodes.paragraph;
|
|
230
|
+
if (hasTable || (blocks <= 1 && (onlyParagraph || slice.content.firstChild?.isInline)))
|
|
231
|
+
return slice;
|
|
232
|
+
const inline = [];
|
|
233
|
+
slice.content.descendants((node) => {
|
|
234
|
+
if (!node.isTextblock)
|
|
235
|
+
return true;
|
|
236
|
+
if (node.content.size === 0)
|
|
237
|
+
return false;
|
|
238
|
+
if (inline.length > 0)
|
|
239
|
+
inline.push(schema.text(' '));
|
|
240
|
+
node.content.forEach((child) => inline.push(child.type.spec.linebreak || child.type.name === 'hardbreak' ? schema.text(' ') : child));
|
|
241
|
+
return false;
|
|
242
|
+
});
|
|
243
|
+
if (inline.length === 0)
|
|
244
|
+
return Slice.empty;
|
|
245
|
+
return new Slice(Fragment.from(schema.nodes.paragraph.create(null, inline)), 1, 1);
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* A paste, as ProseMirror read it, made safe to save: see the file's
|
|
249
|
+
* opening. `uploads`: whether the field takes images; without it a
|
|
250
|
+
* `data:` image is dropped and counted.
|
|
251
|
+
*/
|
|
252
|
+
export function cleanPasted(view, slice, uploads) {
|
|
253
|
+
const found = { content: Fragment.empty, images: [], droppedImages: 0 };
|
|
254
|
+
const content = clean(slice.content, found);
|
|
255
|
+
let next = new Slice(content, slice.openStart, slice.openEnd);
|
|
256
|
+
if (inTableCell(view))
|
|
257
|
+
next = forCell(next, view.state.schema);
|
|
258
|
+
const images = uploads ? found.images : [];
|
|
259
|
+
const dropped = found.droppedImages + (uploads ? 0 : found.images.length);
|
|
260
|
+
return { slice: next, images, dropped };
|
|
261
|
+
}
|
|
262
|
+
// ---------------------------------------------------------------- plain text
|
|
263
|
+
// A paste's soft line breaks (a newline inside a paragraph) as hard
|
|
264
|
+
// breaks, so lines pasted as lines stay lines.
|
|
265
|
+
function hardBreaks(fragment, schema) {
|
|
266
|
+
const hard = schema.nodes.hardbreak;
|
|
267
|
+
if (!hard)
|
|
268
|
+
return fragment;
|
|
269
|
+
const nodes = [];
|
|
270
|
+
fragment.forEach((node) => {
|
|
271
|
+
if (node.isText && node.text?.includes('\n')) {
|
|
272
|
+
node.text.split('\n').forEach((part, index) => {
|
|
273
|
+
if (index > 0)
|
|
274
|
+
nodes.push(hard.create(null, undefined, node.marks.filter((mark) => mark.type.name !== 'link')));
|
|
275
|
+
if (part)
|
|
276
|
+
nodes.push(schema.text(part, node.marks));
|
|
277
|
+
});
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
// Milkdown reads a soft break as an inline hard break, drawn and
|
|
281
|
+
// written as a space and a newline: made a real one.
|
|
282
|
+
if (node.type === hard && node.attrs.isInline) {
|
|
283
|
+
nodes.push(hard.create({ ...node.attrs, isInline: false }, undefined, node.marks));
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
if (node.isText || node.isLeaf || node.type.spec.code)
|
|
287
|
+
nodes.push(node);
|
|
288
|
+
else
|
|
289
|
+
nodes.push(node.copy(hardBreaks(node.content, schema)));
|
|
290
|
+
});
|
|
291
|
+
return Fragment.fromArray(nodes);
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Lines of plain text pasted into a table cell, joined with spaces (a GFM
|
|
295
|
+
* cell is one line). Called before the table's own paste, which would
|
|
296
|
+
* spread them over the row. Returns whether it was this paste.
|
|
297
|
+
*/
|
|
298
|
+
export function pasteLinesIntoCell(view, text) {
|
|
299
|
+
if (!inTableCell(view) || inCode(view) || !/\n/.test(text.trim()))
|
|
300
|
+
return false;
|
|
301
|
+
const line = text.replace(/\s*\r?\n\s*/g, ' ').trimEnd();
|
|
302
|
+
view.dispatch(view.state.tr.insertText(line).scrollIntoView());
|
|
303
|
+
return true;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* The paste of `text` (the clipboard's plain text). Returns whether it was
|
|
307
|
+
* handled; else ProseMirror's own paste goes on.
|
|
308
|
+
*/
|
|
309
|
+
export function pastePlainText(view, text, { parse, hasHtml, uploads, onImages }) {
|
|
310
|
+
const { state } = view;
|
|
311
|
+
const { selection, schema } = state;
|
|
312
|
+
if (!text || inCode(view))
|
|
313
|
+
return false;
|
|
314
|
+
const address = addressOf(text);
|
|
315
|
+
const link = schema.marks.link;
|
|
316
|
+
// An address pasted over selected words links them, the words kept.
|
|
317
|
+
if (address && link && !selection.empty && selection.$from.sameParent(selection.$to) && selection.$from.parent.isTextblock) {
|
|
318
|
+
view.dispatch(state.tr.removeMark(selection.from, selection.to, link).addMark(selection.from, selection.to, link.create({ href: address })).scrollIntoView());
|
|
319
|
+
return true;
|
|
320
|
+
}
|
|
321
|
+
if (hasHtml)
|
|
322
|
+
return false;
|
|
323
|
+
// An address alone: a link, its words as pasted.
|
|
324
|
+
if (address && link) {
|
|
325
|
+
const words = text.trim();
|
|
326
|
+
const tr = state.tr.replaceSelectionWith(schema.text(words, [link.create({ href: address })]), false);
|
|
327
|
+
tr.removeStoredMark(link);
|
|
328
|
+
view.dispatch(tr.scrollIntoView());
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
if (pasteLinesIntoCell(view, text))
|
|
332
|
+
return true;
|
|
333
|
+
const parsed = parse(text.replace(/\r\n?/g, '\n'));
|
|
334
|
+
if (!parsed || typeof parsed === 'string')
|
|
335
|
+
return false;
|
|
336
|
+
// Text with no Markdown in it (one plain line) goes in exactly as typed,
|
|
337
|
+
// spaces at its ends kept.
|
|
338
|
+
const only = parsed.childCount === 1 ? parsed.firstChild : null;
|
|
339
|
+
const plainLine = only?.type.name === 'paragraph' &&
|
|
340
|
+
only.childCount === 1 &&
|
|
341
|
+
only.firstChild.isText &&
|
|
342
|
+
only.firstChild.marks.length === 0 &&
|
|
343
|
+
!text.trim().includes('\n') &&
|
|
344
|
+
only.textContent === text.trim();
|
|
345
|
+
if (plainLine) {
|
|
346
|
+
view.dispatch(state.tr.insertText(text).scrollIntoView());
|
|
347
|
+
return true;
|
|
348
|
+
}
|
|
349
|
+
const content = hardBreaks(parsed.content, schema);
|
|
350
|
+
const { slice, images, dropped } = cleanPasted(view, Slice.maxOpen(content), uploads);
|
|
351
|
+
view.dispatch(state.tr.replaceSelection(slice).scrollIntoView());
|
|
352
|
+
if (images.length > 0 || dropped > 0)
|
|
353
|
+
onImages({ images, dropped });
|
|
354
|
+
return true;
|
|
355
|
+
}
|
|
@@ -4,9 +4,13 @@ export declare function isApple(): boolean;
|
|
|
4
4
|
export declare function ariaKeys(keys: string, apple: boolean): string;
|
|
5
5
|
/** What a person reads: ⌘⌥X on Apple, Ctrl+Alt+X elsewhere. */
|
|
6
6
|
export declare function shownKeys(keys: string, apple: boolean): string;
|
|
7
|
+
type Dispatch = (tr: Transaction) => void;
|
|
8
|
+
/** The editing keys: Backspace at a list item's start, Tab and Shift+Tab, undo and redo. True when the key was taken. */
|
|
9
|
+
export declare function handleEditingKey(state: EditorState, dispatch: Dispatch, event: KeyboardEvent): boolean;
|
|
7
10
|
/**
|
|
8
11
|
* Runs the shortcut a key press is, if it's one of these. True when it
|
|
9
12
|
* was (whether or not the format could apply where the caret is: the key
|
|
10
13
|
* is taken either way, so the browser's own never runs).
|
|
11
14
|
*/
|
|
12
|
-
export declare function handleShortcut(state: EditorState, dispatch:
|
|
15
|
+
export declare function handleShortcut(state: EditorState, dispatch: Dispatch, event: KeyboardEvent): boolean;
|
|
16
|
+
export {};
|
|
@@ -1,5 +1,35 @@
|
|
|
1
|
+
// The rich-text editor's own shortcuts, and how a shortcut is named.
|
|
2
|
+
// Notion's block keys (rich-text-format-items.ts BLOCK_KEYS): Ctrl+Shift
|
|
3
|
+
// and a digit turns the line into a block (0 plain text, 1 to 3 headings,
|
|
4
|
+
// 4 tasks, 5 bullets, 6 numbers, 8 a code block), and Cmd+Option and the
|
|
5
|
+
// digit does on a Mac, as in Notion (Cmd+Shift+3 to 5 are the Mac's
|
|
6
|
+
// screenshots). Mod+Shift+S strikes through, also Notion's, and
|
|
7
|
+
// Mod+Shift+H highlights (4.126), Notion's too. Each runs the same command
|
|
8
|
+
// as its toolbar button, so it toggles the same way.
|
|
9
|
+
//
|
|
10
|
+
// Read by the key's position (`event.code`), not the character it types:
|
|
11
|
+
// Shift+5 types "%", and Option+5 "∞".
|
|
12
|
+
//
|
|
13
|
+
// And the editing keys Notion, Docs and Obsidian share (4.126, the
|
|
14
|
+
// RichTextEditor review's F17 and its Tab row):
|
|
15
|
+
// - Backspace at the start of a list item's first line lifts the item out
|
|
16
|
+
// of its list (a nested one up a level), its words kept as a line; a
|
|
17
|
+
// second Backspace joins it to the line above. Milkdown's (joinBackward)
|
|
18
|
+
// fused a nested item into the item above as its second paragraph.
|
|
19
|
+
// - Tab and Shift+Tab in a list item indent and outdent it, and stay in
|
|
20
|
+
// the text where it can't move (a first item): Tab leaving the editor
|
|
21
|
+
// from one item and not the next was a surprise. In a code block they
|
|
22
|
+
// indent and outdent its lines by two spaces. On a plain line Tab still
|
|
23
|
+
// leaves the editor, the keyboard's way on to the rest of the page.
|
|
24
|
+
// - Undo and redo (Mod+Z; Mod+Shift+Z, and Ctrl+Y off Apple) are taken
|
|
25
|
+
// here whether or not there is a step to take (the review's F21).
|
|
26
|
+
// ProseMirror's keymap, finding nothing to redo, ran Shift+Mod+Z's
|
|
27
|
+
// unshifted key, Mod+Z, so a redo pressed once too often undid a step.
|
|
28
|
+
import { redo, undo } from '@milkdown/kit/prose/history';
|
|
29
|
+
import { liftListItem, sinkListItem } from '@milkdown/kit/prose/schema-list';
|
|
30
|
+
import { DEFAULT_HIGHLIGHT } from '../../lib/markdown-highlight.js';
|
|
1
31
|
import { BLOCK_KEYS } from './rich-text-format-items.js';
|
|
2
|
-
import { toggleFormat } from './rich-text-formats.js';
|
|
32
|
+
import { highlightColor, tableAt, toggleFormat } from './rich-text-formats.js';
|
|
3
33
|
export function isApple() {
|
|
4
34
|
return typeof navigator !== 'undefined' && /Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent);
|
|
5
35
|
}
|
|
@@ -36,17 +66,117 @@ function shortcutFormat(event, apple) {
|
|
|
36
66
|
const mod = apple ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey;
|
|
37
67
|
if (mod && event.shiftKey && !event.altKey && event.code === 'KeyS')
|
|
38
68
|
return 'strike';
|
|
69
|
+
if (mod && event.shiftKey && !event.altKey && event.code === 'KeyH')
|
|
70
|
+
return 'highlight';
|
|
39
71
|
return null;
|
|
40
72
|
}
|
|
73
|
+
// Backspace at the start of a list item's first line: the item lifted out.
|
|
74
|
+
function liftAtItemStart(state, dispatch) {
|
|
75
|
+
const { $from, empty } = state.selection;
|
|
76
|
+
if (!empty || $from.parentOffset > 0 || $from.depth < 2 || !$from.parent.isTextblock)
|
|
77
|
+
return false;
|
|
78
|
+
const item = $from.node(-1);
|
|
79
|
+
if (item.type.name !== 'list_item' || $from.index(-1) !== 0)
|
|
80
|
+
return false;
|
|
81
|
+
return liftListItem(item.type)(state, dispatch);
|
|
82
|
+
}
|
|
83
|
+
// Two spaces in at the start of each line of a code block the selection
|
|
84
|
+
// touches (`out`: up to two, or a tab, taken off). With nothing selected,
|
|
85
|
+
// Tab puts them in at the caret.
|
|
86
|
+
function indentCode(state, dispatch, out) {
|
|
87
|
+
const { $from, $to, from, to, empty } = state.selection;
|
|
88
|
+
if (!$from.sameParent($to))
|
|
89
|
+
return false;
|
|
90
|
+
if (!out && empty) {
|
|
91
|
+
dispatch(state.tr.insertText(' ').scrollIntoView());
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
const start = $from.start();
|
|
95
|
+
const text = $from.parent.textContent;
|
|
96
|
+
const lines = [];
|
|
97
|
+
for (let line = text.lastIndexOf('\n', from - start - 1) + 1;;) {
|
|
98
|
+
lines.push(line);
|
|
99
|
+
const next = text.indexOf('\n', line);
|
|
100
|
+
if (next === -1 || next >= to - start)
|
|
101
|
+
break;
|
|
102
|
+
line = next + 1;
|
|
103
|
+
}
|
|
104
|
+
const tr = state.tr;
|
|
105
|
+
// From the last line up, so the earlier lines' places hold.
|
|
106
|
+
for (const line of lines.reverse()) {
|
|
107
|
+
if (!out)
|
|
108
|
+
tr.insertText(' ', start + line);
|
|
109
|
+
else {
|
|
110
|
+
const lead = /^(?: {1,2}|\t)/.exec(text.slice(line))?.[0];
|
|
111
|
+
if (lead)
|
|
112
|
+
tr.delete(start + line, start + line + lead.length);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
dispatch(tr.scrollIntoView());
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
// Tab (Shift+Tab) in a list item or a code block; false anywhere else,
|
|
119
|
+
// where Tab is the browser's (a table's is its own, rich-text-table.ts).
|
|
120
|
+
function handleTab(state, dispatch, out) {
|
|
121
|
+
const { $from } = state.selection;
|
|
122
|
+
if ($from.parent.type.spec.code)
|
|
123
|
+
return indentCode(state, dispatch, out);
|
|
124
|
+
if (tableAt(state))
|
|
125
|
+
return false;
|
|
126
|
+
for (let depth = $from.depth; depth > 0; depth--) {
|
|
127
|
+
const node = $from.node(depth);
|
|
128
|
+
if (node.type.name !== 'list_item')
|
|
129
|
+
continue;
|
|
130
|
+
const move = out ? liftListItem(node.type) : sinkListItem(node.type);
|
|
131
|
+
move(state, dispatch);
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
// Undo or redo for a key press, or null when it's neither.
|
|
137
|
+
function historyKey(event, apple) {
|
|
138
|
+
const mod = apple ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey;
|
|
139
|
+
if (!mod || event.altKey)
|
|
140
|
+
return null;
|
|
141
|
+
if (event.code === 'KeyZ')
|
|
142
|
+
return event.shiftKey ? 'redo' : 'undo';
|
|
143
|
+
return !apple && !event.shiftKey && event.code === 'KeyY' ? 'redo' : null;
|
|
144
|
+
}
|
|
145
|
+
/** The editing keys: Backspace at a list item's start, Tab and Shift+Tab, undo and redo. True when the key was taken. */
|
|
146
|
+
export function handleEditingKey(state, dispatch, event) {
|
|
147
|
+
if (event.isComposing)
|
|
148
|
+
return false;
|
|
149
|
+
const step = historyKey(event, isApple());
|
|
150
|
+
if (step) {
|
|
151
|
+
;
|
|
152
|
+
(step === 'undo' ? undo : redo)(state, dispatch);
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
if (event.metaKey || event.ctrlKey || event.altKey)
|
|
156
|
+
return false;
|
|
157
|
+
if (event.key === 'Backspace' && !event.shiftKey)
|
|
158
|
+
return liftAtItemStart(state, dispatch);
|
|
159
|
+
if (event.key === 'Tab')
|
|
160
|
+
return handleTab(state, dispatch, event.shiftKey);
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
41
163
|
/**
|
|
42
164
|
* Runs the shortcut a key press is, if it's one of these. True when it
|
|
43
165
|
* was (whether or not the format could apply where the caret is: the key
|
|
44
166
|
* is taken either way, so the browser's own never runs).
|
|
45
167
|
*/
|
|
46
168
|
export function handleShortcut(state, dispatch, event) {
|
|
169
|
+
if (handleEditingKey(state, dispatch, event)) {
|
|
170
|
+
event.preventDefault();
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
47
173
|
const format = shortcutFormat(event, isApple());
|
|
48
174
|
if (!format)
|
|
49
175
|
return false;
|
|
50
|
-
|
|
176
|
+
// Highlight: the default colour on, or any colour off.
|
|
177
|
+
if (format === 'highlight')
|
|
178
|
+
toggleFormat(state, dispatch, highlightColor(state) ? 'highlight-off' : `highlight-${DEFAULT_HIGHLIGHT}`);
|
|
179
|
+
else
|
|
180
|
+
toggleFormat(state, dispatch, format);
|
|
51
181
|
return true;
|
|
52
182
|
}
|
|
@@ -9,20 +9,51 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
9
9
|
//
|
|
10
10
|
// Placed against the field's element like the floating toolbar, not in a
|
|
11
11
|
// portal (rich-text-floating-toolbar.tsx says why): under the caret, or
|
|
12
|
-
// over it when
|
|
12
|
+
// over it when there's more room there, and never taller than what is
|
|
13
|
+
// visible on its side (above a phone's keyboard, review F11).
|
|
13
14
|
import { useLayoutEffect, useState } from 'react';
|
|
14
15
|
import { cn } from 'cn';
|
|
15
16
|
import { slashOptionId } from './rich-text-slash.js';
|
|
16
17
|
// Its gap from the caret's line.
|
|
17
18
|
const GAP = 4;
|
|
19
|
+
// Its tallest: every block shows on a laptop.
|
|
20
|
+
const TALLEST = 512;
|
|
21
|
+
// Below the caret while at least this much of it fits there.
|
|
22
|
+
const ENOUGH = 170;
|
|
23
|
+
// What is visible of the window: above a phone's keyboard
|
|
24
|
+
// (`visualViewport`), and above the bar pinned to it
|
|
25
|
+
// (rich-text-keyboard-bar.tsx) when the field has one (review F11).
|
|
26
|
+
function visibleArea(field) {
|
|
27
|
+
const viewport = window.visualViewport;
|
|
28
|
+
const top = viewport?.offsetTop ?? 0;
|
|
29
|
+
let bottom = top + (viewport?.height ?? window.innerHeight);
|
|
30
|
+
const bar = field.querySelector('[data-slot="rich-text-keyboard-bar"] [role="toolbar"]');
|
|
31
|
+
if (bar)
|
|
32
|
+
bottom = Math.min(bottom, bar.getBoundingClientRect().top);
|
|
33
|
+
return { top, bottom };
|
|
34
|
+
}
|
|
35
|
+
// Under the caret, or over it; on a touch screen never taller than the
|
|
36
|
+
// room on its side, so it never runs under the keyboard.
|
|
18
37
|
function place(view, at, field, menu) {
|
|
19
38
|
const caret = view.coordsAtPos(at);
|
|
20
39
|
const box = field.getBoundingClientRect();
|
|
21
|
-
const
|
|
22
|
-
const
|
|
40
|
+
const area = visibleArea(field);
|
|
41
|
+
const { offsetWidth: width } = menu;
|
|
42
|
+
const natural = Math.min(menu.scrollHeight, TALLEST);
|
|
43
|
+
const roomBelow = area.bottom - caret.bottom - GAP * 2;
|
|
44
|
+
const roomAbove = caret.top - area.top - GAP * 2;
|
|
45
|
+
// A laptop's as before: below when it all fits there or can't above.
|
|
46
|
+
// A touch screen's: below while a few items fit, else wherever there's
|
|
47
|
+
// more room, and capped to it.
|
|
48
|
+
const touch = window.matchMedia('(pointer: coarse)').matches;
|
|
49
|
+
const below = touch
|
|
50
|
+
? roomBelow >= Math.min(natural, ENOUGH) || roomBelow >= roomAbove
|
|
51
|
+
: caret.bottom + GAP + natural <= window.innerHeight || caret.top - GAP - natural < 0;
|
|
52
|
+
const maxHeight = touch ? Math.max(80, Math.min(TALLEST, below ? roomBelow : roomAbove)) : TALLEST;
|
|
53
|
+
const height = Math.min(natural, maxHeight);
|
|
23
54
|
const top = below ? caret.bottom + GAP : caret.top - GAP - height;
|
|
24
55
|
const left = Math.max(0, Math.min(caret.left - box.left, box.width - width));
|
|
25
|
-
return { top: top - box.top, left, side: below ? 'bottom' : 'top' };
|
|
56
|
+
return { top: top - box.top, left, side: below ? 'bottom' : 'top', maxHeight };
|
|
26
57
|
}
|
|
27
58
|
export function RichTextSlashMenu({ id, view, at, field, items, active, onHighlight, onPick }) {
|
|
28
59
|
const [menu, setMenu] = useState(null);
|
|
@@ -35,20 +66,27 @@ export function RichTextSlashMenu({ id, view, at, field, items, active, onHighli
|
|
|
35
66
|
const update = () => setPlacement(place(view, at, field, menu));
|
|
36
67
|
update();
|
|
37
68
|
window.addEventListener('resize', update);
|
|
38
|
-
|
|
69
|
+
window.visualViewport?.addEventListener('resize', update);
|
|
70
|
+
return () => {
|
|
71
|
+
window.removeEventListener('resize', update);
|
|
72
|
+
window.visualViewport?.removeEventListener('resize', update);
|
|
73
|
+
};
|
|
39
74
|
}, [view, at, field, menu, items.length]);
|
|
40
75
|
// The highlighted item stays in sight.
|
|
41
76
|
useLayoutEffect(() => {
|
|
42
77
|
menu?.querySelector(`#${CSS.escape(slashOptionId(id, active))}`)?.scrollIntoView({ block: 'nearest' });
|
|
43
78
|
}, [menu, id, active]);
|
|
44
79
|
return (_jsx("div", { ref: setMenu, id: id, role: "listbox", "aria-label": "Blocks", "data-slot": "rich-text-slash-menu", "data-side": placement?.side,
|
|
45
|
-
//
|
|
46
|
-
//
|
|
47
|
-
|
|
80
|
+
// It can scroll (capped above a phone's keyboard, or in a short
|
|
81
|
+
// window): a tab stop for axe (scrollable-region-focusable), never
|
|
82
|
+
// reached while it's open (Tab picks), nor by a press.
|
|
83
|
+
tabIndex: 0, onMouseDown: (event) => event.preventDefault(), style: placement ? { top: placement.top, left: placement.left, maxHeight: placement.maxHeight } : undefined, className: cn('absolute z-50 flex max-h-128 w-56 flex-col gap-0.5 overflow-y-auto rounded-lg bg-popover p-1 text-sm text-popover-foreground shadow-md ring-1 ring-foreground/10', 'animate-in fade-in-0 zoom-in-95 duration-100 data-[side=bottom]:slide-in-from-top-2 data-[side=top]:slide-in-from-bottom-2',
|
|
48
84
|
// Measured before it's seen, so it never shows in the wrong place.
|
|
49
85
|
!placement && 'invisible'), children: items.map((item, index) => {
|
|
50
86
|
const Icon = item.icon;
|
|
51
|
-
return (_jsxs("div", { id: slashOptionId(id, index), role: "option", "aria-selected": index === active,
|
|
87
|
+
return (_jsxs("div", { id: slashOptionId(id, index), role: "option", "aria-selected": index === active,
|
|
88
|
+
// 44px rows on a touch screen (review F11).
|
|
89
|
+
className: "flex cursor-default items-center gap-2 rounded-md px-2 py-1.5 select-none aria-selected:bg-muted pointer-coarse:min-h-11",
|
|
52
90
|
// A press never takes focus from the text.
|
|
53
91
|
onMouseDown: (event) => event.preventDefault(), onMouseMove: () => index !== active && onHighlight(index), onClick: () => onPick(item.id), children: [_jsx(Icon, { "aria-hidden": "true", className: "size-4 shrink-0" }), item.label] }, item.id));
|
|
54
92
|
}) }));
|
|
@@ -27,13 +27,20 @@ export declare function slashPlugin(): Plugin<SlashState>;
|
|
|
27
27
|
export type SlashItem = Omit<FormatItem, 'id'> & {
|
|
28
28
|
id: FormatId | 'image' | 'page';
|
|
29
29
|
};
|
|
30
|
+
/** Where the menu is open, for `slashItems` to leave out what can't be made there. */
|
|
31
|
+
export type SlashPlace = {
|
|
32
|
+
state: EditorState;
|
|
33
|
+
span: SlashQuery;
|
|
34
|
+
};
|
|
30
35
|
/**
|
|
31
36
|
* The blocks matching what's typed after the "/": in the name, or the
|
|
32
37
|
* start of a keyword. With `image`, an image too; with `page`, the new page.
|
|
38
|
+
* With `at`, only the blocks that can be made there.
|
|
33
39
|
*/
|
|
34
40
|
export declare function slashItems(query: string, options?: {
|
|
35
41
|
page?: boolean;
|
|
36
42
|
image?: boolean;
|
|
43
|
+
at?: SlashPlace;
|
|
37
44
|
}): SlashItem[];
|
|
38
45
|
/** Picks the image item: the "/…" goes and the menu closes, the caret where it was, for the picked images. */
|
|
39
46
|
export declare function clearSlash(view: EditorView, span: SlashQuery): void;
|