@folijs/react 0.1.0 → 0.3.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/CHANGELOG.md +56 -0
- package/README.md +3 -2
- package/dist/FoliEditor.d.ts +7 -4
- package/dist/FoliEditor.d.ts.map +1 -1
- package/dist/FoliEditor.js +5 -3
- package/dist/FoliEditor.js.map +1 -1
- package/dist/FoliToolbar.d.ts +4 -2
- package/dist/FoliToolbar.d.ts.map +1 -1
- package/dist/FoliToolbar.js +420 -52
- package/dist/FoliToolbar.js.map +1 -1
- package/dist/editor.d.ts +10 -5
- package/dist/editor.d.ts.map +1 -1
- package/dist/editor.js +3 -2
- package/dist/editor.js.map +1 -1
- package/package.json +3 -3
- package/src/FoliEditor.tsx +12 -7
- package/src/FoliToolbar.tsx +684 -81
- package/src/editor.tsx +10 -5
- package/styles.css +144 -7
package/src/FoliToolbar.tsx
CHANGED
|
@@ -1,16 +1,52 @@
|
|
|
1
1
|
import {
|
|
2
|
+
type Alignment,
|
|
3
|
+
alignAt,
|
|
2
4
|
type Command,
|
|
5
|
+
canOutdent,
|
|
6
|
+
changeIndent,
|
|
7
|
+
colorAt,
|
|
3
8
|
type Editor,
|
|
4
9
|
type EditorState,
|
|
10
|
+
fontFamilyAt,
|
|
11
|
+
fontSizeAt,
|
|
5
12
|
insertPageBreak,
|
|
13
|
+
isSafeHref,
|
|
14
|
+
lineSpacingAt,
|
|
15
|
+
linkAt,
|
|
16
|
+
listKindAt,
|
|
17
|
+
normalizeHref,
|
|
18
|
+
openLinkEditor,
|
|
19
|
+
paragraphStyleAt,
|
|
20
|
+
paragraphStyles,
|
|
6
21
|
redo,
|
|
7
22
|
redoDepth,
|
|
8
|
-
|
|
23
|
+
removeLink,
|
|
24
|
+
requestsLinkEditor,
|
|
25
|
+
setAlign,
|
|
26
|
+
setFontFamily,
|
|
27
|
+
setFontSize,
|
|
28
|
+
setHighlight,
|
|
29
|
+
setLineSpacing,
|
|
30
|
+
setLink,
|
|
31
|
+
setParagraphStyle,
|
|
32
|
+
setTextColor,
|
|
33
|
+
toggleList,
|
|
9
34
|
toggleMark,
|
|
10
35
|
undo,
|
|
11
36
|
undoDepth,
|
|
37
|
+
wordContext,
|
|
12
38
|
} from "@folijs/core";
|
|
13
|
-
import {
|
|
39
|
+
import {
|
|
40
|
+
type FormEvent,
|
|
41
|
+
Fragment,
|
|
42
|
+
type KeyboardEvent,
|
|
43
|
+
type MouseEvent,
|
|
44
|
+
type ReactNode,
|
|
45
|
+
useEffect,
|
|
46
|
+
useId,
|
|
47
|
+
useRef,
|
|
48
|
+
useState,
|
|
49
|
+
} from "react";
|
|
14
50
|
import { useEditorOrContext, useEditorState } from "./editor.js";
|
|
15
51
|
|
|
16
52
|
export interface FoliToolbarProps {
|
|
@@ -38,34 +74,94 @@ interface Item {
|
|
|
38
74
|
/** Visible text: the accessible name, unless it is a symbol (then the label is). */
|
|
39
75
|
text: string;
|
|
40
76
|
symbol?: boolean;
|
|
77
|
+
/** Shown instead of `text` (a symbol: the label is the name). */
|
|
78
|
+
icon?: ReactNode;
|
|
41
79
|
/** Whether the button shows as pressed (toggle buttons only). */
|
|
42
80
|
pressed?: (state: EditorState, editor: Editor) => boolean;
|
|
43
81
|
/**
|
|
44
|
-
* Whether the command can run now (default: always;
|
|
45
|
-
*
|
|
82
|
+
* Whether the command can run now (default: always; a paragraph takes every mark and a page
|
|
83
|
+
* break). A cheap check, not a dry
|
|
46
84
|
* run: it runs after every transaction, and a dry run builds the whole transaction (for a
|
|
47
85
|
* block type over a large selection, quadratic in the number of blocks).
|
|
48
86
|
*/
|
|
49
87
|
enabled?: (state: EditorState) => boolean;
|
|
50
|
-
command
|
|
88
|
+
/** The command a press runs (not for a menu button). */
|
|
89
|
+
command?: (state: EditorState) => Command;
|
|
90
|
+
/** A menu button: its options, the current value, and what picking one runs. */
|
|
91
|
+
menu?: {
|
|
92
|
+
options: readonly ColorOption[];
|
|
93
|
+
value: (state: EditorState) => string | null;
|
|
94
|
+
pick: (color: string | null) => Command;
|
|
95
|
+
};
|
|
96
|
+
/** A drop-down list (a `select`): its options, the current value, and what picking runs. */
|
|
97
|
+
select?: {
|
|
98
|
+
options: (state: EditorState) => readonly { value: string; label: string }[];
|
|
99
|
+
value: (state: EditorState) => string | null;
|
|
100
|
+
pick: (value: string) => Command;
|
|
101
|
+
};
|
|
51
102
|
}
|
|
52
103
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
104
|
+
interface ColorOption {
|
|
105
|
+
name: string;
|
|
106
|
+
/** Lowercase `#rrggbb`; null removes the color. */
|
|
107
|
+
color: string | null;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The palettes (fixed: a document should not need a color picker). Text colors keep at least
|
|
112
|
+
* 4.5:1 contrast on white; highlights keep dark text readable.
|
|
113
|
+
*/
|
|
114
|
+
const TEXT_COLORS: readonly ColorOption[] = [
|
|
115
|
+
{ name: "Automatic", color: null },
|
|
116
|
+
{ name: "Gray", color: "#5f6368" },
|
|
117
|
+
{ name: "Red", color: "#c00000" },
|
|
118
|
+
{ name: "Orange", color: "#b45309" },
|
|
119
|
+
{ name: "Green", color: "#2e7d32" },
|
|
120
|
+
{ name: "Blue", color: "#1f5fbf" },
|
|
121
|
+
{ name: "Purple", color: "#7030a0" },
|
|
122
|
+
];
|
|
123
|
+
/** Word's highlight colors (a document opened in Word shows these exactly). */
|
|
124
|
+
const HIGHLIGHTS: readonly ColorOption[] = [
|
|
125
|
+
{ name: "None", color: null },
|
|
126
|
+
{ name: "Yellow", color: "#ffff00" },
|
|
127
|
+
{ name: "Bright green", color: "#00ff00" },
|
|
128
|
+
{ name: "Turquoise", color: "#00ffff" },
|
|
129
|
+
{ name: "Pink", color: "#ff00ff" },
|
|
130
|
+
{ name: "Gray", color: "#c0c0c0" },
|
|
131
|
+
];
|
|
132
|
+
|
|
133
|
+
/** Whether the selection holds a textblock (a caret's own block first; else it walks the range). */
|
|
134
|
+
function hasTextblock(state: EditorState): boolean {
|
|
135
|
+
if (state.selection.$from.parent.isTextblock) return true;
|
|
136
|
+
let found = false;
|
|
137
|
+
state.doc.nodesBetween(state.selection.from, state.selection.to, (node) => {
|
|
138
|
+
if (node.isTextblock) found = true;
|
|
139
|
+
return !found;
|
|
65
140
|
});
|
|
66
|
-
return
|
|
141
|
+
return found;
|
|
67
142
|
}
|
|
68
143
|
|
|
144
|
+
/** Four bars, like the alignment they stand for (x and width per bar). */
|
|
145
|
+
const alignIcon = (bars: readonly [number, number][]) => (
|
|
146
|
+
<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true" focusable="false">
|
|
147
|
+
{bars.map(([x, width], i) => (
|
|
148
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: a fixed drawing.
|
|
149
|
+
<rect key={i} x={x} y={2.5 + i * 3.5} width={width} height="1.5" fill="currentColor" />
|
|
150
|
+
))}
|
|
151
|
+
</svg>
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
const align = (value: Alignment, label: string, bars: readonly [number, number][]): Item => ({
|
|
155
|
+
key: `align-${value}`,
|
|
156
|
+
label,
|
|
157
|
+
text: label,
|
|
158
|
+
symbol: true,
|
|
159
|
+
icon: alignIcon(bars),
|
|
160
|
+
pressed: (state) => alignAt(state) === value,
|
|
161
|
+
enabled: hasTextblock,
|
|
162
|
+
command: () => setAlign(value),
|
|
163
|
+
});
|
|
164
|
+
|
|
69
165
|
const mark = (key: string, label: string, text: string): Item => ({
|
|
70
166
|
key,
|
|
71
167
|
label,
|
|
@@ -75,37 +171,185 @@ const mark = (key: string, label: string, text: string): Item => ({
|
|
|
75
171
|
command: () => toggleMark(key),
|
|
76
172
|
});
|
|
77
173
|
|
|
78
|
-
const
|
|
79
|
-
key: `h${level}`,
|
|
80
|
-
label: `Heading ${level}`,
|
|
81
|
-
text: `H${level}`,
|
|
82
|
-
pressed: (state) => blockIs(state, "heading", level),
|
|
83
|
-
// Pressing an active heading turns it back into a paragraph.
|
|
84
|
-
command: (state) =>
|
|
85
|
-
blockIs(state, "heading", level)
|
|
86
|
-
? setBlockType("paragraph")
|
|
87
|
-
: setBlockType("heading", { level }),
|
|
88
|
-
});
|
|
174
|
+
const capitalized = (name: string) => name.charAt(0).toUpperCase() + name.slice(1);
|
|
89
175
|
|
|
176
|
+
/** The fonts the font box offers (with the document's theme fonts, and the one shown now). */
|
|
177
|
+
const FONTS = [
|
|
178
|
+
"Aptos",
|
|
179
|
+
"Arial",
|
|
180
|
+
"Calibri",
|
|
181
|
+
"Cambria",
|
|
182
|
+
"Courier New",
|
|
183
|
+
"Georgia",
|
|
184
|
+
"Times New Roman",
|
|
185
|
+
"Verdana",
|
|
186
|
+
];
|
|
187
|
+
const SIZES = [8, 9, 10, 10.5, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72];
|
|
188
|
+
const LINE_SPACINGS = [1, 1.15, 1.5, 2, 2.5, 3];
|
|
189
|
+
|
|
190
|
+
/** Word's controls. */
|
|
90
191
|
const GROUPS: Item[][] = [
|
|
192
|
+
[
|
|
193
|
+
{
|
|
194
|
+
key: "style",
|
|
195
|
+
label: "Paragraph style",
|
|
196
|
+
text: "Style",
|
|
197
|
+
enabled: hasTextblock,
|
|
198
|
+
select: {
|
|
199
|
+
options: (state) =>
|
|
200
|
+
paragraphStyles(state).map((s) => ({ value: s.id, label: capitalized(s.name) })),
|
|
201
|
+
value: paragraphStyleAt,
|
|
202
|
+
pick: (id) => setParagraphStyle(id),
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
{
|
|
206
|
+
key: "font",
|
|
207
|
+
label: "Font",
|
|
208
|
+
text: "Font",
|
|
209
|
+
enabled: hasTextblock,
|
|
210
|
+
select: {
|
|
211
|
+
options: (state) => {
|
|
212
|
+
const theme = wordContext(state)?.envelope.theme?.fonts;
|
|
213
|
+
const names = [theme?.minor.latin, theme?.major.latin, ...FONTS];
|
|
214
|
+
return [...new Set(names.filter((n): n is string => !!n))]
|
|
215
|
+
.sort((a, b) => a.localeCompare(b))
|
|
216
|
+
.map((name) => ({ value: name, label: name }));
|
|
217
|
+
},
|
|
218
|
+
value: fontFamilyAt,
|
|
219
|
+
pick: (name) => setFontFamily(name),
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
key: "size",
|
|
224
|
+
label: "Font size",
|
|
225
|
+
text: "Size",
|
|
226
|
+
enabled: hasTextblock,
|
|
227
|
+
select: {
|
|
228
|
+
options: () => SIZES.map((size) => ({ value: String(size), label: String(size) })),
|
|
229
|
+
value: (state) => {
|
|
230
|
+
const size = fontSizeAt(state);
|
|
231
|
+
return size === null ? null : String(size);
|
|
232
|
+
},
|
|
233
|
+
pick: (size) => setFontSize(Number(size)),
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
],
|
|
91
237
|
[
|
|
92
238
|
mark("bold", "Bold", "B"),
|
|
93
239
|
mark("italic", "Italic", "I"),
|
|
94
240
|
mark("underline", "Underline", "U"),
|
|
95
241
|
mark("strike", "Strikethrough", "S"),
|
|
242
|
+
{
|
|
243
|
+
key: "link",
|
|
244
|
+
label: "Link",
|
|
245
|
+
text: "Link",
|
|
246
|
+
pressed: (state) => linkAt(state) !== null,
|
|
247
|
+
enabled: (state) => openLinkEditor(state),
|
|
248
|
+
// Asks for the link dialog, as Mod-K does: the toolbar listens for that request.
|
|
249
|
+
command: () => openLinkEditor,
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
key: "textColor",
|
|
253
|
+
label: "Text color",
|
|
254
|
+
text: "A",
|
|
255
|
+
symbol: true,
|
|
256
|
+
enabled: hasTextblock,
|
|
257
|
+
menu: {
|
|
258
|
+
options: TEXT_COLORS,
|
|
259
|
+
value: (state) => colorAt(state, "textColor"),
|
|
260
|
+
pick: setTextColor,
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
key: "highlight",
|
|
265
|
+
label: "Highlight",
|
|
266
|
+
text: "H",
|
|
267
|
+
symbol: true,
|
|
268
|
+
enabled: hasTextblock,
|
|
269
|
+
menu: {
|
|
270
|
+
options: HIGHLIGHTS,
|
|
271
|
+
value: (state) => colorAt(state, "highlight"),
|
|
272
|
+
pick: setHighlight,
|
|
273
|
+
},
|
|
274
|
+
},
|
|
96
275
|
],
|
|
97
276
|
[
|
|
277
|
+
align("start", "Align left", [
|
|
278
|
+
[1, 14],
|
|
279
|
+
[1, 9],
|
|
280
|
+
[1, 14],
|
|
281
|
+
[1, 9],
|
|
282
|
+
]),
|
|
283
|
+
align("center", "Center", [
|
|
284
|
+
[1, 14],
|
|
285
|
+
[3.5, 9],
|
|
286
|
+
[1, 14],
|
|
287
|
+
[3.5, 9],
|
|
288
|
+
]),
|
|
289
|
+
align("end", "Align right", [
|
|
290
|
+
[1, 14],
|
|
291
|
+
[6, 9],
|
|
292
|
+
[1, 14],
|
|
293
|
+
[6, 9],
|
|
294
|
+
]),
|
|
295
|
+
align("justify", "Justify", [
|
|
296
|
+
[1, 14],
|
|
297
|
+
[1, 14],
|
|
298
|
+
[1, 14],
|
|
299
|
+
[1, 14],
|
|
300
|
+
]),
|
|
98
301
|
{
|
|
99
|
-
key: "
|
|
100
|
-
label: "
|
|
101
|
-
text: "
|
|
302
|
+
key: "lineSpacing",
|
|
303
|
+
label: "Line spacing",
|
|
304
|
+
text: "Line spacing",
|
|
305
|
+
enabled: hasTextblock,
|
|
306
|
+
select: {
|
|
307
|
+
options: () =>
|
|
308
|
+
LINE_SPACINGS.map((n) => ({ value: String(n), label: n.toFixed(n % 1 ? 2 : 1) })),
|
|
309
|
+
value: (state) => {
|
|
310
|
+
const spacing = lineSpacingAt(state);
|
|
311
|
+
return spacing === null ? null : String(spacing);
|
|
312
|
+
},
|
|
313
|
+
pick: (n) => setLineSpacing(Number(n)),
|
|
314
|
+
},
|
|
315
|
+
},
|
|
316
|
+
],
|
|
317
|
+
[
|
|
318
|
+
{
|
|
319
|
+
key: "bulletList",
|
|
320
|
+
label: "Bulleted list",
|
|
321
|
+
text: "•",
|
|
102
322
|
symbol: true,
|
|
103
|
-
pressed: (state) =>
|
|
104
|
-
|
|
323
|
+
pressed: (state) => listKindAt(state) === "bulletList",
|
|
324
|
+
enabled: hasTextblock,
|
|
325
|
+
command: () => toggleList("bulletList"),
|
|
326
|
+
},
|
|
327
|
+
{
|
|
328
|
+
key: "orderedList",
|
|
329
|
+
label: "Numbered list",
|
|
330
|
+
// A symbol, not "1.": visible text that is not part of the name fails WCAG 2.5.3.
|
|
331
|
+
text: "№",
|
|
332
|
+
symbol: true,
|
|
333
|
+
pressed: (state) => listKindAt(state) === "orderedList",
|
|
334
|
+
enabled: hasTextblock,
|
|
335
|
+
command: () => toggleList("orderedList"),
|
|
336
|
+
},
|
|
337
|
+
{
|
|
338
|
+
key: "outdent",
|
|
339
|
+
label: "Decrease indent",
|
|
340
|
+
text: "⇤",
|
|
341
|
+
symbol: true,
|
|
342
|
+
enabled: canOutdent,
|
|
343
|
+
command: () => changeIndent(-1),
|
|
344
|
+
},
|
|
345
|
+
{
|
|
346
|
+
key: "indent",
|
|
347
|
+
label: "Increase indent",
|
|
348
|
+
text: "⇥",
|
|
349
|
+
symbol: true,
|
|
350
|
+
enabled: hasTextblock,
|
|
351
|
+
command: () => changeIndent(1),
|
|
105
352
|
},
|
|
106
|
-
heading(1),
|
|
107
|
-
heading(2),
|
|
108
|
-
heading(3),
|
|
109
353
|
],
|
|
110
354
|
[
|
|
111
355
|
{
|
|
@@ -125,27 +369,50 @@ const GROUPS: Item[][] = [
|
|
|
125
369
|
],
|
|
126
370
|
[{ key: "pageBreak", label: "Page break", text: "Page break", command: () => insertPageBreak }],
|
|
127
371
|
];
|
|
372
|
+
|
|
128
373
|
const ITEMS = GROUPS.flat();
|
|
129
374
|
|
|
375
|
+
interface LinkDialogState {
|
|
376
|
+
/** The link at the selection when the dialog opened ("" for none). */
|
|
377
|
+
href: string;
|
|
378
|
+
selection: EditorState["selection"];
|
|
379
|
+
/** Where the focus goes back to on close. */
|
|
380
|
+
restore: HTMLElement | null;
|
|
381
|
+
request: number;
|
|
382
|
+
}
|
|
383
|
+
|
|
130
384
|
interface ItemState {
|
|
131
385
|
pressed: boolean | undefined;
|
|
132
386
|
enabled: boolean;
|
|
387
|
+
/** A menu button's current value (a color, or null). */
|
|
388
|
+
value: string | null | undefined;
|
|
133
389
|
}
|
|
134
390
|
|
|
135
391
|
const sameStates = (a: ItemState[], b: ItemState[]) =>
|
|
136
392
|
a.length === b.length &&
|
|
137
|
-
a.every(
|
|
393
|
+
a.every(
|
|
394
|
+
(item, i) =>
|
|
395
|
+
item.pressed === b[i]?.pressed &&
|
|
396
|
+
item.enabled === b[i]?.enabled &&
|
|
397
|
+
item.value === b[i]?.value,
|
|
398
|
+
);
|
|
138
399
|
|
|
139
400
|
/** Runs after every transaction: O(selection), no transactions built. */
|
|
140
|
-
const
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
401
|
+
const statesOf =
|
|
402
|
+
(items: readonly Item[]) =>
|
|
403
|
+
(state: EditorState, editor: Editor): ItemState[] =>
|
|
404
|
+
items.map((item) => ({
|
|
405
|
+
pressed: item.pressed?.(state, editor),
|
|
406
|
+
enabled: item.enabled?.(state) ?? true,
|
|
407
|
+
value: item.menu?.value(state) ?? item.select?.value(state),
|
|
408
|
+
}));
|
|
409
|
+
const selectStates = statesOf(ITEMS);
|
|
145
410
|
|
|
146
411
|
/**
|
|
147
|
-
*
|
|
148
|
-
*
|
|
412
|
+
* Word's formatting controls for an editor: paragraph style, font and size boxes, marks, link (a
|
|
413
|
+
* dialog, also on Mod-K in the document), text color and highlight menus, alignment and line
|
|
414
|
+
* spacing, lists (bulleted, numbered, decrease and increase indent), undo, redo, page break.
|
|
415
|
+
* One tab stop (arrow keys, Home and End move between buttons); toggles carry
|
|
149
416
|
* `aria-pressed`; a button whose command cannot run now is `aria-disabled` (still focusable).
|
|
150
417
|
* Clicks leave the focus and selection in the document.
|
|
151
418
|
*/
|
|
@@ -157,22 +424,84 @@ export function FoliToolbar({
|
|
|
157
424
|
className,
|
|
158
425
|
}: FoliToolbarProps) {
|
|
159
426
|
const editor = useEditorOrContext(given, "FoliToolbar");
|
|
427
|
+
const groups = GROUPS;
|
|
428
|
+
const items = ITEMS;
|
|
160
429
|
const states = useEditorState(selectStates, { editor, isEqual: sameStates });
|
|
161
430
|
const [active, setActive] = useState(0);
|
|
162
|
-
const buttons = useRef<(HTMLButtonElement | null)[]>([]);
|
|
431
|
+
const buttons = useRef<(HTMLButtonElement | HTMLSelectElement | null)[]>([]);
|
|
432
|
+
// Whether a select box was last used with the pointer (its pick then goes back to the document).
|
|
433
|
+
const pointer = useRef(false);
|
|
434
|
+
// The open menu (a menu button's key), if any.
|
|
435
|
+
const [menu, setMenu] = useState<string | null>(null);
|
|
436
|
+
useEffect(() => {
|
|
437
|
+
if (disabled) setMenu(null);
|
|
438
|
+
}, [disabled]);
|
|
163
439
|
|
|
164
440
|
const run = (index: number, event: MouseEvent) => {
|
|
165
|
-
const item =
|
|
441
|
+
const item = items[index];
|
|
166
442
|
if (!item || disabled || !states[index]?.enabled) return;
|
|
443
|
+
if (item.menu) {
|
|
444
|
+
if (menu !== item.key) {
|
|
445
|
+
setMenu(item.key);
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
// Closing by its button: the focus was in the menu, which goes away.
|
|
449
|
+
setMenu(null);
|
|
450
|
+
if (event.detail > 0 && onClickCommand) onClickCommand();
|
|
451
|
+
else buttons.current[index]?.focus();
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
if (!item.command) return;
|
|
167
455
|
editor.run(item.command(editor.state));
|
|
168
456
|
// `detail` counts pointer clicks; Enter and Space on a button give 0.
|
|
169
457
|
if (event.detail > 0) onClickCommand?.();
|
|
170
458
|
};
|
|
171
459
|
|
|
460
|
+
// The link dialog opens on a request from the link button or Mod-K in the document, and
|
|
461
|
+
// gives the focus back to where it was (the document, or the button) when it closes. It is
|
|
462
|
+
// for the selection it opened on: a change to the document or the selection (a click in the
|
|
463
|
+
// document) closes it. A second request while it is open focuses it again.
|
|
464
|
+
const [dialog, setDialog] = useState<LinkDialogState | null>(null);
|
|
465
|
+
useEffect(
|
|
466
|
+
() =>
|
|
467
|
+
editor.subscribe((state, transactions) => {
|
|
468
|
+
if (disabled) return;
|
|
469
|
+
if (!transactions.some(requestsLinkEditor)) {
|
|
470
|
+
setDialog((current) =>
|
|
471
|
+
current &&
|
|
472
|
+
(transactions.some((tr) => tr.docChanged) || !state.selection.eq(current.selection))
|
|
473
|
+
? null
|
|
474
|
+
: current,
|
|
475
|
+
);
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
const active = typeof document === "undefined" ? null : document.activeElement;
|
|
479
|
+
setDialog((current) =>
|
|
480
|
+
current
|
|
481
|
+
? { ...current, request: current.request + 1 }
|
|
482
|
+
: {
|
|
483
|
+
href: linkAt(state) ?? "",
|
|
484
|
+
selection: state.selection,
|
|
485
|
+
restore: active instanceof HTMLElement ? active : null,
|
|
486
|
+
request: 0,
|
|
487
|
+
},
|
|
488
|
+
);
|
|
489
|
+
}),
|
|
490
|
+
[editor, disabled],
|
|
491
|
+
);
|
|
492
|
+
// A dialog never outlives a disabled toolbar (it would come back when enabled again).
|
|
493
|
+
useEffect(() => {
|
|
494
|
+
if (disabled) setDialog(null);
|
|
495
|
+
}, [disabled]);
|
|
496
|
+
const closeDialog = () => {
|
|
497
|
+
dialog?.restore?.focus();
|
|
498
|
+
setDialog(null);
|
|
499
|
+
};
|
|
500
|
+
|
|
172
501
|
const onKeyDown = (event: KeyboardEvent) => {
|
|
173
502
|
// Alt+Left (back) and other shortcuts are the browser's.
|
|
174
503
|
if (event.altKey || event.ctrlKey || event.metaKey) return;
|
|
175
|
-
const count =
|
|
504
|
+
const count = items.length;
|
|
176
505
|
const moves: Record<string, number> = {
|
|
177
506
|
ArrowRight: (active + 1) % count,
|
|
178
507
|
ArrowLeft: (active - 1 + count) % count,
|
|
@@ -187,45 +516,319 @@ export function FoliToolbar({
|
|
|
187
516
|
};
|
|
188
517
|
|
|
189
518
|
let index = 0;
|
|
519
|
+
return (
|
|
520
|
+
<>
|
|
521
|
+
<div
|
|
522
|
+
role="toolbar"
|
|
523
|
+
aria-label={label}
|
|
524
|
+
className={className ? `foli-toolbar ${className}` : "foli-toolbar"}
|
|
525
|
+
onKeyDown={onKeyDown}
|
|
526
|
+
>
|
|
527
|
+
{groups.map((group, g) => (
|
|
528
|
+
<Fragment key={group[0]?.key}>
|
|
529
|
+
{g > 0 && <hr className="foli-toolbar-separator" aria-orientation="vertical" />}
|
|
530
|
+
{group.map((item) => {
|
|
531
|
+
const i = index++;
|
|
532
|
+
const state = states[i];
|
|
533
|
+
const off = disabled || !state?.enabled;
|
|
534
|
+
if (item.select) {
|
|
535
|
+
const { select } = item;
|
|
536
|
+
const value = state?.value ?? "";
|
|
537
|
+
const options = select.options(editor.state);
|
|
538
|
+
return (
|
|
539
|
+
<select
|
|
540
|
+
key={item.key}
|
|
541
|
+
ref={(element) => {
|
|
542
|
+
buttons.current[i] = element;
|
|
543
|
+
}}
|
|
544
|
+
className={`foli-toolbar-select foli-toolbar-${item.key}`}
|
|
545
|
+
aria-label={item.label}
|
|
546
|
+
title={item.label}
|
|
547
|
+
// Still focusable when it cannot apply, like the buttons.
|
|
548
|
+
aria-disabled={off || undefined}
|
|
549
|
+
tabIndex={i === active ? 0 : -1}
|
|
550
|
+
value={value}
|
|
551
|
+
onFocus={() => setActive(i)}
|
|
552
|
+
onMouseDown={() => {
|
|
553
|
+
pointer.current = true;
|
|
554
|
+
}}
|
|
555
|
+
onKeyDown={() => {
|
|
556
|
+
pointer.current = false;
|
|
557
|
+
}}
|
|
558
|
+
// shortcut: Up and Down on a closed box apply each value they pass (Chrome
|
|
559
|
+
// and Firefox on Windows), one undo step each; apply on Enter if it matters.
|
|
560
|
+
onChange={(event) => {
|
|
561
|
+
if (off) return;
|
|
562
|
+
editor.run(select.pick(event.target.value));
|
|
563
|
+
if (pointer.current) onClickCommand?.();
|
|
564
|
+
}}
|
|
565
|
+
>
|
|
566
|
+
{/* Mixed (or nothing to show): a blank entry; a value not offered: itself. */}
|
|
567
|
+
{value === "" && <option value="" />}
|
|
568
|
+
{value !== "" && !options.some((o) => o.value === value) && (
|
|
569
|
+
<option value={value}>{value}</option>
|
|
570
|
+
)}
|
|
571
|
+
{options.map((o) => (
|
|
572
|
+
<option key={o.value} value={o.value}>
|
|
573
|
+
{o.label}
|
|
574
|
+
</option>
|
|
575
|
+
))}
|
|
576
|
+
</select>
|
|
577
|
+
);
|
|
578
|
+
}
|
|
579
|
+
const button = (
|
|
580
|
+
<button
|
|
581
|
+
key={item.key}
|
|
582
|
+
ref={(element) => {
|
|
583
|
+
buttons.current[i] = element;
|
|
584
|
+
}}
|
|
585
|
+
type="button"
|
|
586
|
+
className={`foli-toolbar-button foli-toolbar-${item.key}`}
|
|
587
|
+
// Speech input users say what they see: "H1", "Undo"; symbols get a name.
|
|
588
|
+
aria-label={item.symbol ? item.label : undefined}
|
|
589
|
+
title={item.label}
|
|
590
|
+
aria-pressed={item.pressed ? Boolean(state?.pressed) : undefined}
|
|
591
|
+
aria-haspopup={item.menu ? "menu" : undefined}
|
|
592
|
+
aria-expanded={item.menu ? menu === item.key : undefined}
|
|
593
|
+
aria-disabled={off || undefined}
|
|
594
|
+
tabIndex={i === active ? 0 : -1}
|
|
595
|
+
// Keep the focus (and the selection) in the document on click.
|
|
596
|
+
onMouseDown={(event) => event.preventDefault()}
|
|
597
|
+
onFocus={() => setActive(i)}
|
|
598
|
+
onClick={(event) => run(i, event)}
|
|
599
|
+
onKeyDown={(event) => {
|
|
600
|
+
// ArrowDown opens a menu (the WAI-ARIA menu button pattern).
|
|
601
|
+
if (!item.menu || off || event.key !== "ArrowDown") return;
|
|
602
|
+
event.preventDefault();
|
|
603
|
+
setMenu(item.key);
|
|
604
|
+
}}
|
|
605
|
+
>
|
|
606
|
+
{item.icon ?? item.text}
|
|
607
|
+
{item.menu && (
|
|
608
|
+
<span
|
|
609
|
+
className="foli-toolbar-swatch"
|
|
610
|
+
aria-hidden="true"
|
|
611
|
+
style={{ background: state?.value ?? "transparent" }}
|
|
612
|
+
/>
|
|
613
|
+
)}
|
|
614
|
+
</button>
|
|
615
|
+
);
|
|
616
|
+
const options = item.menu?.options;
|
|
617
|
+
if (!options || !item.menu) return button;
|
|
618
|
+
const { pick } = item.menu;
|
|
619
|
+
return (
|
|
620
|
+
<span key={item.key} className="foli-toolbar-menu">
|
|
621
|
+
{button}
|
|
622
|
+
{menu === item.key && !disabled && (
|
|
623
|
+
<ColorMenu
|
|
624
|
+
label={item.label}
|
|
625
|
+
options={options}
|
|
626
|
+
current={state?.value ?? null}
|
|
627
|
+
onPick={(color, pointer) => {
|
|
628
|
+
editor.run(pick(color));
|
|
629
|
+
setMenu(null);
|
|
630
|
+
if (pointer) onClickCommand?.();
|
|
631
|
+
else buttons.current[i]?.focus();
|
|
632
|
+
}}
|
|
633
|
+
onClose={(refocus) => {
|
|
634
|
+
setMenu(null);
|
|
635
|
+
if (refocus) buttons.current[i]?.focus();
|
|
636
|
+
}}
|
|
637
|
+
/>
|
|
638
|
+
)}
|
|
639
|
+
</span>
|
|
640
|
+
);
|
|
641
|
+
})}
|
|
642
|
+
</Fragment>
|
|
643
|
+
))}
|
|
644
|
+
</div>
|
|
645
|
+
{dialog && !disabled && (
|
|
646
|
+
<LinkDialog
|
|
647
|
+
editor={editor}
|
|
648
|
+
href={dialog.href}
|
|
649
|
+
request={dialog.request}
|
|
650
|
+
onClose={closeDialog}
|
|
651
|
+
/>
|
|
652
|
+
)}
|
|
653
|
+
</>
|
|
654
|
+
);
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* A color menu (WAI-ARIA menu with radio items): the current color checked and focused on
|
|
659
|
+
* open; arrow keys, Home and End move; Enter or Space (or a click) picks; Escape closes it back
|
|
660
|
+
* to its button, Tab closes it and moves on; leaving it (a click elsewhere) closes it.
|
|
661
|
+
*/
|
|
662
|
+
function ColorMenu({
|
|
663
|
+
label,
|
|
664
|
+
options,
|
|
665
|
+
current,
|
|
666
|
+
onPick,
|
|
667
|
+
onClose,
|
|
668
|
+
}: {
|
|
669
|
+
label: string;
|
|
670
|
+
options: readonly ColorOption[];
|
|
671
|
+
current: string | null;
|
|
672
|
+
onPick: (color: string | null, pointer: boolean) => void;
|
|
673
|
+
onClose: (refocus: boolean) => void;
|
|
674
|
+
}) {
|
|
675
|
+
const items = useRef<(HTMLButtonElement | null)[]>([]);
|
|
676
|
+
const checked = Math.max(
|
|
677
|
+
0,
|
|
678
|
+
options.findIndex((option) => option.color === current),
|
|
679
|
+
);
|
|
680
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: focus once, on open.
|
|
681
|
+
useEffect(() => {
|
|
682
|
+
items.current[checked]?.focus();
|
|
683
|
+
}, []);
|
|
684
|
+
|
|
685
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
686
|
+
const count = options.length;
|
|
687
|
+
const at = items.current.indexOf(event.target as HTMLButtonElement);
|
|
688
|
+
const moves: Record<string, number> = {
|
|
689
|
+
ArrowDown: (at + 1) % count,
|
|
690
|
+
ArrowUp: (at - 1 + count) % count,
|
|
691
|
+
Home: 0,
|
|
692
|
+
End: count - 1,
|
|
693
|
+
};
|
|
694
|
+
const next = moves[event.key];
|
|
695
|
+
// The toolbar's own arrow keys must not see these.
|
|
696
|
+
event.stopPropagation();
|
|
697
|
+
if (next !== undefined) {
|
|
698
|
+
event.preventDefault();
|
|
699
|
+
items.current[next]?.focus();
|
|
700
|
+
} else if (event.key === "Escape") {
|
|
701
|
+
event.preventDefault();
|
|
702
|
+
onClose(true);
|
|
703
|
+
} else if (event.key === "Tab") {
|
|
704
|
+
// Back to the button, then the browser's Tab moves on from it (out of the toolbar).
|
|
705
|
+
onClose(true);
|
|
706
|
+
}
|
|
707
|
+
};
|
|
708
|
+
|
|
190
709
|
return (
|
|
191
710
|
<div
|
|
192
|
-
role="
|
|
711
|
+
role="menu"
|
|
193
712
|
aria-label={label}
|
|
194
|
-
className=
|
|
713
|
+
className="foli-menu"
|
|
195
714
|
onKeyDown={onKeyDown}
|
|
715
|
+
onBlur={(event) => {
|
|
716
|
+
if (!event.currentTarget.contains(event.relatedTarget as Node | null)) onClose(false);
|
|
717
|
+
}}
|
|
196
718
|
>
|
|
197
|
-
{
|
|
198
|
-
<
|
|
199
|
-
{
|
|
200
|
-
{
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
// Keep the focus (and the selection) in the document on click.
|
|
219
|
-
onMouseDown={(event) => event.preventDefault()}
|
|
220
|
-
onFocus={() => setActive(i)}
|
|
221
|
-
onClick={(event) => run(i, event)}
|
|
222
|
-
>
|
|
223
|
-
{item.text}
|
|
224
|
-
</button>
|
|
225
|
-
);
|
|
226
|
-
})}
|
|
227
|
-
</Fragment>
|
|
719
|
+
{options.map((option, i) => (
|
|
720
|
+
<button
|
|
721
|
+
key={option.name}
|
|
722
|
+
ref={(element) => {
|
|
723
|
+
items.current[i] = element;
|
|
724
|
+
}}
|
|
725
|
+
type="button"
|
|
726
|
+
role="menuitemradio"
|
|
727
|
+
aria-checked={option.color === current}
|
|
728
|
+
tabIndex={-1}
|
|
729
|
+
className="foli-menu-item"
|
|
730
|
+
onMouseDown={(event) => event.preventDefault()}
|
|
731
|
+
onClick={(event) => onPick(option.color, event.detail > 0)}
|
|
732
|
+
>
|
|
733
|
+
<span
|
|
734
|
+
className="foli-menu-swatch"
|
|
735
|
+
aria-hidden="true"
|
|
736
|
+
style={{ background: option.color ?? "transparent" }}
|
|
737
|
+
/>
|
|
738
|
+
{option.name}
|
|
739
|
+
</button>
|
|
228
740
|
))}
|
|
229
741
|
</div>
|
|
230
742
|
);
|
|
231
743
|
}
|
|
744
|
+
|
|
745
|
+
/**
|
|
746
|
+
* The link dialog: a URL field (prefilled with the link at the selection), Apply, Remove link
|
|
747
|
+
* (for an existing link) and Cancel; Escape cancels. An unsafe or invalid URL is not applied:
|
|
748
|
+
* the field is marked invalid and the message is announced.
|
|
749
|
+
*/
|
|
750
|
+
function LinkDialog({
|
|
751
|
+
editor,
|
|
752
|
+
href,
|
|
753
|
+
request,
|
|
754
|
+
onClose,
|
|
755
|
+
}: {
|
|
756
|
+
editor: Editor;
|
|
757
|
+
href: string;
|
|
758
|
+
/** Counts the open requests: each one focuses the field. */
|
|
759
|
+
request: number;
|
|
760
|
+
onClose: () => void;
|
|
761
|
+
}) {
|
|
762
|
+
const [value, setValue] = useState(href);
|
|
763
|
+
const [invalid, setInvalid] = useState(false);
|
|
764
|
+
const input = useRef<HTMLInputElement>(null);
|
|
765
|
+
const id = useId();
|
|
766
|
+
|
|
767
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: each request focuses the field again.
|
|
768
|
+
useEffect(() => {
|
|
769
|
+
input.current?.focus();
|
|
770
|
+
input.current?.select();
|
|
771
|
+
}, [request]);
|
|
772
|
+
|
|
773
|
+
const apply = (event: FormEvent) => {
|
|
774
|
+
event.preventDefault();
|
|
775
|
+
// The same link again changes nothing (the command does not apply): that is not an error.
|
|
776
|
+
const normalized = normalizeHref(value);
|
|
777
|
+
const unchanged = isSafeHref(normalized) && linkAt(editor.state) === normalized;
|
|
778
|
+
if (unchanged || editor.run(setLink(value))) onClose();
|
|
779
|
+
else {
|
|
780
|
+
setInvalid(true);
|
|
781
|
+
input.current?.focus();
|
|
782
|
+
}
|
|
783
|
+
};
|
|
784
|
+
const remove = () => {
|
|
785
|
+
editor.run(removeLink);
|
|
786
|
+
onClose();
|
|
787
|
+
};
|
|
788
|
+
|
|
789
|
+
return (
|
|
790
|
+
<div
|
|
791
|
+
role="dialog"
|
|
792
|
+
aria-label={href ? "Edit link" : "Add link"}
|
|
793
|
+
className="foli-link-dialog"
|
|
794
|
+
onKeyDown={(event) => {
|
|
795
|
+
if (event.key !== "Escape") return;
|
|
796
|
+
event.preventDefault();
|
|
797
|
+
onClose();
|
|
798
|
+
}}
|
|
799
|
+
>
|
|
800
|
+
<form onSubmit={apply} noValidate>
|
|
801
|
+
<label htmlFor={`${id}-url`}>URL</label>
|
|
802
|
+
<input
|
|
803
|
+
ref={input}
|
|
804
|
+
id={`${id}-url`}
|
|
805
|
+
type="text"
|
|
806
|
+
inputMode="url"
|
|
807
|
+
autoComplete="off"
|
|
808
|
+
spellCheck={false}
|
|
809
|
+
value={value}
|
|
810
|
+
aria-invalid={invalid || undefined}
|
|
811
|
+
aria-describedby={invalid ? `${id}-error` : undefined}
|
|
812
|
+
onChange={(event) => {
|
|
813
|
+
setValue(event.target.value);
|
|
814
|
+
setInvalid(false);
|
|
815
|
+
}}
|
|
816
|
+
/>
|
|
817
|
+
<button type="submit">Apply</button>
|
|
818
|
+
{href && (
|
|
819
|
+
<button type="button" onClick={remove}>
|
|
820
|
+
Remove link
|
|
821
|
+
</button>
|
|
822
|
+
)}
|
|
823
|
+
<button type="button" onClick={onClose}>
|
|
824
|
+
Cancel
|
|
825
|
+
</button>
|
|
826
|
+
{invalid && (
|
|
827
|
+
<p id={`${id}-error`} className="foli-link-error" role="alert">
|
|
828
|
+
Enter a web or email address, or a link starting with http, https, mailto or tel.
|
|
829
|
+
</p>
|
|
830
|
+
)}
|
|
831
|
+
</form>
|
|
832
|
+
</div>
|
|
833
|
+
);
|
|
834
|
+
}
|