@folijs/react 0.2.0 → 0.4.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 +57 -0
- package/README.md +3 -2
- package/dist/FoliComments.d.ts +22 -0
- package/dist/FoliComments.d.ts.map +1 -0
- package/dist/FoliComments.js +275 -0
- package/dist/FoliComments.js.map +1 -0
- package/dist/FoliEditor.d.ts +18 -4
- package/dist/FoliEditor.d.ts.map +1 -1
- package/dist/FoliEditor.js +11 -7
- package/dist/FoliEditor.js.map +1 -1
- package/dist/FoliToolbar.d.ts +10 -5
- package/dist/FoliToolbar.d.ts.map +1 -1
- package/dist/FoliToolbar.js +210 -88
- package/dist/FoliToolbar.js.map +1 -1
- package/dist/editor.d.ts +16 -5
- package/dist/editor.d.ts.map +1 -1
- package/dist/editor.js +11 -4
- package/dist/editor.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/FoliComments.tsx +499 -0
- package/src/FoliEditor.tsx +60 -11
- package/src/FoliToolbar.tsx +281 -102
- package/src/editor.tsx +25 -7
- package/src/index.ts +1 -0
- package/styles.css +105 -40
package/src/FoliToolbar.tsx
CHANGED
|
@@ -1,35 +1,51 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type Alignment,
|
|
3
|
-
|
|
3
|
+
acceptAllChanges,
|
|
4
|
+
acceptChange,
|
|
5
|
+
addComment,
|
|
4
6
|
alignAt,
|
|
5
7
|
type Command,
|
|
8
|
+
canOutdent,
|
|
9
|
+
changeIndent,
|
|
6
10
|
colorAt,
|
|
7
11
|
type Editor,
|
|
8
12
|
type EditorState,
|
|
9
|
-
|
|
13
|
+
fontFamilyAt,
|
|
14
|
+
fontSizeAt,
|
|
10
15
|
insertPageBreak,
|
|
11
16
|
isSafeHref,
|
|
12
|
-
|
|
17
|
+
lineSpacingAt,
|
|
13
18
|
linkAt,
|
|
14
|
-
|
|
15
|
-
|
|
19
|
+
listKindAt,
|
|
20
|
+
nextChange,
|
|
16
21
|
normalizeHref,
|
|
17
22
|
openLinkEditor,
|
|
23
|
+
paragraphStyleAt,
|
|
24
|
+
paragraphStyles,
|
|
25
|
+
previousChange,
|
|
26
|
+
type ReviewView,
|
|
18
27
|
redo,
|
|
19
28
|
redoDepth,
|
|
29
|
+
rejectAllChanges,
|
|
30
|
+
rejectChange,
|
|
20
31
|
removeLink,
|
|
21
32
|
requestsLinkEditor,
|
|
22
33
|
setAlign,
|
|
23
|
-
|
|
34
|
+
setFontFamily,
|
|
35
|
+
setFontSize,
|
|
24
36
|
setHighlight,
|
|
37
|
+
setLineSpacing,
|
|
25
38
|
setLink,
|
|
39
|
+
setParagraphStyle,
|
|
40
|
+
setReviewView,
|
|
26
41
|
setTextColor,
|
|
27
|
-
|
|
42
|
+
setTrackChanges,
|
|
28
43
|
toggleList,
|
|
29
44
|
toggleMark,
|
|
45
|
+
trackState,
|
|
30
46
|
undo,
|
|
31
47
|
undoDepth,
|
|
32
|
-
|
|
48
|
+
wordContext,
|
|
33
49
|
} from "@folijs/core";
|
|
34
50
|
import {
|
|
35
51
|
type FormEvent,
|
|
@@ -59,6 +75,11 @@ export interface FoliToolbarProps {
|
|
|
59
75
|
onClickCommand?: (() => void) | undefined;
|
|
60
76
|
/** Accessible name of the toolbar (default "Formatting"). */
|
|
61
77
|
label?: string | undefined;
|
|
78
|
+
/**
|
|
79
|
+
* Show "New comment": only where a comments pane (`FoliComments`) lets the comment be written
|
|
80
|
+
* (`FoliEditor` passes it). Default false.
|
|
81
|
+
*/
|
|
82
|
+
comments?: boolean | undefined;
|
|
62
83
|
className?: string | undefined;
|
|
63
84
|
}
|
|
64
85
|
|
|
@@ -74,8 +95,8 @@ interface Item {
|
|
|
74
95
|
/** Whether the button shows as pressed (toggle buttons only). */
|
|
75
96
|
pressed?: (state: EditorState, editor: Editor) => boolean;
|
|
76
97
|
/**
|
|
77
|
-
* Whether the command can run now (default: always;
|
|
78
|
-
*
|
|
98
|
+
* Whether the command can run now (default: always; a paragraph takes every mark and a page
|
|
99
|
+
* break). A cheap check, not a dry
|
|
79
100
|
* run: it runs after every transaction, and a dry run builds the whole transaction (for a
|
|
80
101
|
* block type over a large selection, quadratic in the number of blocks).
|
|
81
102
|
*/
|
|
@@ -88,6 +109,12 @@ interface Item {
|
|
|
88
109
|
value: (state: EditorState) => string | null;
|
|
89
110
|
pick: (color: string | null) => Command;
|
|
90
111
|
};
|
|
112
|
+
/** A drop-down list (a `select`): its options, the current value, and what picking runs. */
|
|
113
|
+
select?: {
|
|
114
|
+
options: (state: EditorState) => readonly { value: string; label: string }[];
|
|
115
|
+
value: (state: EditorState) => string | null;
|
|
116
|
+
pick: (value: string) => Command;
|
|
117
|
+
};
|
|
91
118
|
}
|
|
92
119
|
|
|
93
120
|
interface ColorOption {
|
|
@@ -109,14 +136,14 @@ const TEXT_COLORS: readonly ColorOption[] = [
|
|
|
109
136
|
{ name: "Blue", color: "#1f5fbf" },
|
|
110
137
|
{ name: "Purple", color: "#7030a0" },
|
|
111
138
|
];
|
|
139
|
+
/** Word's highlight colors (a document opened in Word shows these exactly). */
|
|
112
140
|
const HIGHLIGHTS: readonly ColorOption[] = [
|
|
113
141
|
{ name: "None", color: null },
|
|
114
|
-
{ name: "Yellow", color: "#
|
|
115
|
-
{ name: "
|
|
116
|
-
{ name: "
|
|
117
|
-
{ name: "Pink", color: "#
|
|
118
|
-
{ name: "
|
|
119
|
-
{ name: "Gray", color: "#e0e0e0" },
|
|
142
|
+
{ name: "Yellow", color: "#ffff00" },
|
|
143
|
+
{ name: "Bright green", color: "#00ff00" },
|
|
144
|
+
{ name: "Turquoise", color: "#00ffff" },
|
|
145
|
+
{ name: "Pink", color: "#ff00ff" },
|
|
146
|
+
{ name: "Gray", color: "#c0c0c0" },
|
|
120
147
|
];
|
|
121
148
|
|
|
122
149
|
/** Whether the selection holds a textblock (a caret's own block first; else it walks the range). */
|
|
@@ -151,22 +178,6 @@ const align = (value: Alignment, label: string, bars: readonly [number, number][
|
|
|
151
178
|
command: () => setAlign(value),
|
|
152
179
|
});
|
|
153
180
|
|
|
154
|
-
/** Whether the selection holds textblocks and all of them are `type` with `level` (if given). */
|
|
155
|
-
function blockIs(state: EditorState, type: string, level?: number): boolean {
|
|
156
|
-
const { from, to } = state.selection;
|
|
157
|
-
let all = true;
|
|
158
|
-
let any = false;
|
|
159
|
-
state.doc.nodesBetween(from, to, (node) => {
|
|
160
|
-
if (!all) return false;
|
|
161
|
-
if (node.isTextblock) {
|
|
162
|
-
any = true;
|
|
163
|
-
if (node.type.name !== type || (level && node.attrs.level !== level)) all = false;
|
|
164
|
-
}
|
|
165
|
-
return !node.isTextblock;
|
|
166
|
-
});
|
|
167
|
-
return any && all;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
181
|
const mark = (key: string, label: string, text: string): Item => ({
|
|
171
182
|
key,
|
|
172
183
|
label,
|
|
@@ -176,19 +187,74 @@ const mark = (key: string, label: string, text: string): Item => ({
|
|
|
176
187
|
command: () => toggleMark(key),
|
|
177
188
|
});
|
|
178
189
|
|
|
179
|
-
const
|
|
180
|
-
key: `h${level}`,
|
|
181
|
-
label: `Heading ${level}`,
|
|
182
|
-
text: `H${level}`,
|
|
183
|
-
pressed: (state) => blockIs(state, "heading", level),
|
|
184
|
-
// Pressing an active heading turns it back into a paragraph.
|
|
185
|
-
command: (state) =>
|
|
186
|
-
blockIs(state, "heading", level)
|
|
187
|
-
? setBlockType("paragraph")
|
|
188
|
-
: setBlockType("heading", { level }),
|
|
189
|
-
});
|
|
190
|
+
const capitalized = (name: string) => name.charAt(0).toUpperCase() + name.slice(1);
|
|
190
191
|
|
|
192
|
+
/** The fonts the font box offers (with the document's theme fonts, and the one shown now). */
|
|
193
|
+
const FONTS = [
|
|
194
|
+
"Aptos",
|
|
195
|
+
"Arial",
|
|
196
|
+
"Calibri",
|
|
197
|
+
"Cambria",
|
|
198
|
+
"Courier New",
|
|
199
|
+
"Georgia",
|
|
200
|
+
"Times New Roman",
|
|
201
|
+
"Verdana",
|
|
202
|
+
];
|
|
203
|
+
const SIZES = [8, 9, 10, 10.5, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72];
|
|
204
|
+
const LINE_SPACINGS = [1, 1.15, 1.5, 2, 2.5, 3];
|
|
205
|
+
const REVIEW_VIEWS = [
|
|
206
|
+
{ value: "markup", label: "All markup" },
|
|
207
|
+
{ value: "simple", label: "Simple markup" },
|
|
208
|
+
{ value: "final", label: "No markup" },
|
|
209
|
+
];
|
|
210
|
+
|
|
211
|
+
/** Word's controls. */
|
|
191
212
|
const GROUPS: Item[][] = [
|
|
213
|
+
[
|
|
214
|
+
{
|
|
215
|
+
key: "style",
|
|
216
|
+
label: "Paragraph style",
|
|
217
|
+
text: "Style",
|
|
218
|
+
enabled: hasTextblock,
|
|
219
|
+
select: {
|
|
220
|
+
options: (state) =>
|
|
221
|
+
paragraphStyles(state).map((s) => ({ value: s.id, label: capitalized(s.name) })),
|
|
222
|
+
value: paragraphStyleAt,
|
|
223
|
+
pick: (id) => setParagraphStyle(id),
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
key: "font",
|
|
228
|
+
label: "Font",
|
|
229
|
+
text: "Font",
|
|
230
|
+
enabled: hasTextblock,
|
|
231
|
+
select: {
|
|
232
|
+
options: (state) => {
|
|
233
|
+
const theme = wordContext(state)?.envelope.theme?.fonts;
|
|
234
|
+
const names = [theme?.minor.latin, theme?.major.latin, ...FONTS];
|
|
235
|
+
return [...new Set(names.filter((n): n is string => !!n))]
|
|
236
|
+
.sort((a, b) => a.localeCompare(b))
|
|
237
|
+
.map((name) => ({ value: name, label: name }));
|
|
238
|
+
},
|
|
239
|
+
value: fontFamilyAt,
|
|
240
|
+
pick: (name) => setFontFamily(name),
|
|
241
|
+
},
|
|
242
|
+
},
|
|
243
|
+
{
|
|
244
|
+
key: "size",
|
|
245
|
+
label: "Font size",
|
|
246
|
+
text: "Size",
|
|
247
|
+
enabled: hasTextblock,
|
|
248
|
+
select: {
|
|
249
|
+
options: () => SIZES.map((size) => ({ value: String(size), label: String(size) })),
|
|
250
|
+
value: (state) => {
|
|
251
|
+
const size = fontSizeAt(state);
|
|
252
|
+
return size === null ? null : String(size);
|
|
253
|
+
},
|
|
254
|
+
pick: (size) => setFontSize(Number(size)),
|
|
255
|
+
},
|
|
256
|
+
},
|
|
257
|
+
],
|
|
192
258
|
[
|
|
193
259
|
mark("bold", "Bold", "B"),
|
|
194
260
|
mark("italic", "Italic", "I"),
|
|
@@ -228,19 +294,6 @@ const GROUPS: Item[][] = [
|
|
|
228
294
|
},
|
|
229
295
|
},
|
|
230
296
|
],
|
|
231
|
-
[
|
|
232
|
-
{
|
|
233
|
-
key: "paragraph",
|
|
234
|
-
label: "Paragraph",
|
|
235
|
-
text: "¶",
|
|
236
|
-
symbol: true,
|
|
237
|
-
pressed: (state) => blockIs(state, "paragraph"),
|
|
238
|
-
command: () => setBlockType("paragraph"),
|
|
239
|
-
},
|
|
240
|
-
heading(1),
|
|
241
|
-
heading(2),
|
|
242
|
-
heading(3),
|
|
243
|
-
],
|
|
244
297
|
[
|
|
245
298
|
align("start", "Align left", [
|
|
246
299
|
[1, 14],
|
|
@@ -266,6 +319,21 @@ const GROUPS: Item[][] = [
|
|
|
266
319
|
[1, 14],
|
|
267
320
|
[1, 14],
|
|
268
321
|
]),
|
|
322
|
+
{
|
|
323
|
+
key: "lineSpacing",
|
|
324
|
+
label: "Line spacing",
|
|
325
|
+
text: "Line spacing",
|
|
326
|
+
enabled: hasTextblock,
|
|
327
|
+
select: {
|
|
328
|
+
options: () =>
|
|
329
|
+
LINE_SPACINGS.map((n) => ({ value: String(n), label: n.toFixed(n % 1 ? 2 : 1) })),
|
|
330
|
+
value: (state) => {
|
|
331
|
+
const spacing = lineSpacingAt(state);
|
|
332
|
+
return spacing === null ? null : String(spacing);
|
|
333
|
+
},
|
|
334
|
+
pick: (n) => setLineSpacing(Number(n)),
|
|
335
|
+
},
|
|
336
|
+
},
|
|
269
337
|
],
|
|
270
338
|
[
|
|
271
339
|
{
|
|
@@ -273,8 +341,8 @@ const GROUPS: Item[][] = [
|
|
|
273
341
|
label: "Bulleted list",
|
|
274
342
|
text: "•",
|
|
275
343
|
symbol: true,
|
|
276
|
-
pressed: (state) =>
|
|
277
|
-
enabled:
|
|
344
|
+
pressed: (state) => listKindAt(state) === "bulletList",
|
|
345
|
+
enabled: hasTextblock,
|
|
278
346
|
command: () => toggleList("bulletList"),
|
|
279
347
|
},
|
|
280
348
|
{
|
|
@@ -283,8 +351,8 @@ const GROUPS: Item[][] = [
|
|
|
283
351
|
// A symbol, not "1.": visible text that is not part of the name fails WCAG 2.5.3.
|
|
284
352
|
text: "№",
|
|
285
353
|
symbol: true,
|
|
286
|
-
pressed: (state) =>
|
|
287
|
-
enabled:
|
|
354
|
+
pressed: (state) => listKindAt(state) === "orderedList",
|
|
355
|
+
enabled: hasTextblock,
|
|
288
356
|
command: () => toggleList("orderedList"),
|
|
289
357
|
},
|
|
290
358
|
{
|
|
@@ -292,16 +360,88 @@ const GROUPS: Item[][] = [
|
|
|
292
360
|
label: "Decrease indent",
|
|
293
361
|
text: "⇤",
|
|
294
362
|
symbol: true,
|
|
295
|
-
enabled:
|
|
296
|
-
command: () =>
|
|
363
|
+
enabled: canOutdent,
|
|
364
|
+
command: () => changeIndent(-1),
|
|
297
365
|
},
|
|
298
366
|
{
|
|
299
367
|
key: "indent",
|
|
300
368
|
label: "Increase indent",
|
|
301
369
|
text: "⇥",
|
|
302
370
|
symbol: true,
|
|
303
|
-
enabled:
|
|
304
|
-
command: () =>
|
|
371
|
+
enabled: hasTextblock,
|
|
372
|
+
command: () => changeIndent(1),
|
|
373
|
+
},
|
|
374
|
+
],
|
|
375
|
+
[
|
|
376
|
+
{
|
|
377
|
+
key: "newComment",
|
|
378
|
+
label: "New comment on the selection",
|
|
379
|
+
text: "New comment",
|
|
380
|
+
enabled: (state) => wordContext(state) !== null && hasTextblock(state),
|
|
381
|
+
command: () => addComment(""),
|
|
382
|
+
},
|
|
383
|
+
{
|
|
384
|
+
key: "trackChanges",
|
|
385
|
+
label: "Track changes",
|
|
386
|
+
text: "Track changes",
|
|
387
|
+
pressed: (state) => trackState(state)?.on === true,
|
|
388
|
+
enabled: (state) => trackState(state) !== null,
|
|
389
|
+
command: (state) => setTrackChanges(!trackState(state)?.on),
|
|
390
|
+
},
|
|
391
|
+
{
|
|
392
|
+
key: "reviewView",
|
|
393
|
+
label: "Show changes as",
|
|
394
|
+
text: "Show",
|
|
395
|
+
enabled: (state) => trackState(state) !== null,
|
|
396
|
+
select: {
|
|
397
|
+
options: () => REVIEW_VIEWS,
|
|
398
|
+
value: (state) => trackState(state)?.view ?? "markup",
|
|
399
|
+
pick: (view) => setReviewView(view as ReviewView),
|
|
400
|
+
},
|
|
401
|
+
},
|
|
402
|
+
// Enabled while tracking can be used: whether there is a change to go to or resolve would
|
|
403
|
+
// walk the whole document after every transaction.
|
|
404
|
+
{
|
|
405
|
+
key: "previousChange",
|
|
406
|
+
label: "Previous change",
|
|
407
|
+
text: "Previous change",
|
|
408
|
+
enabled: (state) => trackState(state) !== null,
|
|
409
|
+
command: () => previousChange,
|
|
410
|
+
},
|
|
411
|
+
{
|
|
412
|
+
key: "nextChange",
|
|
413
|
+
label: "Next change",
|
|
414
|
+
text: "Next change",
|
|
415
|
+
enabled: (state) => trackState(state) !== null,
|
|
416
|
+
command: () => nextChange,
|
|
417
|
+
},
|
|
418
|
+
{
|
|
419
|
+
key: "acceptChange",
|
|
420
|
+
label: "Accept the change here, or the changes selected",
|
|
421
|
+
text: "Accept",
|
|
422
|
+
enabled: (state) => trackState(state) !== null,
|
|
423
|
+
command: () => acceptChange,
|
|
424
|
+
},
|
|
425
|
+
{
|
|
426
|
+
key: "rejectChange",
|
|
427
|
+
label: "Reject the change here, or the changes selected",
|
|
428
|
+
text: "Reject",
|
|
429
|
+
enabled: (state) => trackState(state) !== null,
|
|
430
|
+
command: () => rejectChange,
|
|
431
|
+
},
|
|
432
|
+
{
|
|
433
|
+
key: "acceptAll",
|
|
434
|
+
label: "Accept all changes",
|
|
435
|
+
text: "Accept all",
|
|
436
|
+
enabled: (state) => trackState(state) !== null,
|
|
437
|
+
command: () => acceptAllChanges(),
|
|
438
|
+
},
|
|
439
|
+
{
|
|
440
|
+
key: "rejectAll",
|
|
441
|
+
label: "Reject all changes",
|
|
442
|
+
text: "Reject all",
|
|
443
|
+
enabled: (state) => trackState(state) !== null,
|
|
444
|
+
command: () => rejectAllChanges(),
|
|
305
445
|
},
|
|
306
446
|
],
|
|
307
447
|
[
|
|
@@ -320,31 +460,13 @@ const GROUPS: Item[][] = [
|
|
|
320
460
|
command: () => redo,
|
|
321
461
|
},
|
|
322
462
|
],
|
|
323
|
-
[
|
|
324
|
-
{
|
|
325
|
-
key: "horizontalRule",
|
|
326
|
-
label: "Horizontal rule",
|
|
327
|
-
text: "―",
|
|
328
|
-
symbol: true,
|
|
329
|
-
enabled: (state) =>
|
|
330
|
-
state.selection.$from.parent.isTextblock ||
|
|
331
|
-
state.selection instanceof AllSelection ||
|
|
332
|
-
(state.selection instanceof NodeSelection && state.selection.node.isBlock),
|
|
333
|
-
command: () => insertHorizontalRule,
|
|
334
|
-
},
|
|
335
|
-
{ key: "pageBreak", label: "Page break", text: "Page break", command: () => insertPageBreak },
|
|
336
|
-
],
|
|
463
|
+
[{ key: "pageBreak", label: "Page break", text: "Page break", command: () => insertPageBreak }],
|
|
337
464
|
];
|
|
338
|
-
const ITEMS = GROUPS.flat();
|
|
339
465
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
if ($from.node(depth).type.name === "listItem") return $from.index(depth - 1) > 0;
|
|
345
|
-
}
|
|
346
|
-
return false;
|
|
347
|
-
}
|
|
466
|
+
const ITEMS = GROUPS.flat();
|
|
467
|
+
/** Without "New comment": where no comments pane lets a comment be written. */
|
|
468
|
+
const GROUPS_NO_COMMENT = GROUPS.map((group) => group.filter((i) => i.key !== "newComment"));
|
|
469
|
+
const ITEMS_NO_COMMENT = GROUPS_NO_COMMENT.flat();
|
|
348
470
|
|
|
349
471
|
interface LinkDialogState {
|
|
350
472
|
/** The link at the selection when the dialog opened ("" for none). */
|
|
@@ -372,18 +494,22 @@ const sameStates = (a: ItemState[], b: ItemState[]) =>
|
|
|
372
494
|
);
|
|
373
495
|
|
|
374
496
|
/** Runs after every transaction: O(selection), no transactions built. */
|
|
375
|
-
const
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
497
|
+
const statesOf =
|
|
498
|
+
(items: readonly Item[]) =>
|
|
499
|
+
(state: EditorState, editor: Editor): ItemState[] =>
|
|
500
|
+
items.map((item) => ({
|
|
501
|
+
pressed: item.pressed?.(state, editor),
|
|
502
|
+
enabled: item.enabled?.(state) ?? true,
|
|
503
|
+
value: item.menu?.value(state) ?? item.select?.value(state),
|
|
504
|
+
}));
|
|
505
|
+
const selectStates = statesOf(ITEMS);
|
|
506
|
+
const selectStatesNoComment = statesOf(ITEMS_NO_COMMENT);
|
|
381
507
|
|
|
382
508
|
/**
|
|
383
|
-
*
|
|
384
|
-
*
|
|
385
|
-
* numbered, decrease and increase indent), undo, redo,
|
|
386
|
-
* Home and End move between buttons); toggles carry
|
|
509
|
+
* Word's formatting controls for an editor: paragraph style, font and size boxes, marks, link (a
|
|
510
|
+
* dialog, also on Mod-K in the document), text color and highlight menus, alignment and line
|
|
511
|
+
* spacing, lists (bulleted, numbered, decrease and increase indent), undo, redo, page break.
|
|
512
|
+
* One tab stop (arrow keys, Home and End move between buttons); toggles carry
|
|
387
513
|
* `aria-pressed`; a button whose command cannot run now is `aria-disabled` (still focusable).
|
|
388
514
|
* Clicks leave the focus and selection in the document.
|
|
389
515
|
*/
|
|
@@ -393,11 +519,19 @@ export function FoliToolbar({
|
|
|
393
519
|
onClickCommand,
|
|
394
520
|
label = "Formatting",
|
|
395
521
|
className,
|
|
522
|
+
comments = false,
|
|
396
523
|
}: FoliToolbarProps) {
|
|
397
524
|
const editor = useEditorOrContext(given, "FoliToolbar");
|
|
398
|
-
const
|
|
525
|
+
const groups = comments ? GROUPS : GROUPS_NO_COMMENT;
|
|
526
|
+
const items = comments ? ITEMS : ITEMS_NO_COMMENT;
|
|
527
|
+
const states = useEditorState(comments ? selectStates : selectStatesNoComment, {
|
|
528
|
+
editor,
|
|
529
|
+
isEqual: sameStates,
|
|
530
|
+
});
|
|
399
531
|
const [active, setActive] = useState(0);
|
|
400
|
-
const buttons = useRef<(HTMLButtonElement | null)[]>([]);
|
|
532
|
+
const buttons = useRef<(HTMLButtonElement | HTMLSelectElement | null)[]>([]);
|
|
533
|
+
// Whether a select box was last used with the pointer (its pick then goes back to the document).
|
|
534
|
+
const pointer = useRef(false);
|
|
401
535
|
// The open menu (a menu button's key), if any.
|
|
402
536
|
const [menu, setMenu] = useState<string | null>(null);
|
|
403
537
|
useEffect(() => {
|
|
@@ -405,7 +539,7 @@ export function FoliToolbar({
|
|
|
405
539
|
}, [disabled]);
|
|
406
540
|
|
|
407
541
|
const run = (index: number, event: MouseEvent) => {
|
|
408
|
-
const item =
|
|
542
|
+
const item = items[index];
|
|
409
543
|
if (!item || disabled || !states[index]?.enabled) return;
|
|
410
544
|
if (item.menu) {
|
|
411
545
|
if (menu !== item.key) {
|
|
@@ -468,7 +602,7 @@ export function FoliToolbar({
|
|
|
468
602
|
const onKeyDown = (event: KeyboardEvent) => {
|
|
469
603
|
// Alt+Left (back) and other shortcuts are the browser's.
|
|
470
604
|
if (event.altKey || event.ctrlKey || event.metaKey) return;
|
|
471
|
-
const count =
|
|
605
|
+
const count = items.length;
|
|
472
606
|
const moves: Record<string, number> = {
|
|
473
607
|
ArrowRight: (active + 1) % count,
|
|
474
608
|
ArrowLeft: (active - 1 + count) % count,
|
|
@@ -491,13 +625,58 @@ export function FoliToolbar({
|
|
|
491
625
|
className={className ? `foli-toolbar ${className}` : "foli-toolbar"}
|
|
492
626
|
onKeyDown={onKeyDown}
|
|
493
627
|
>
|
|
494
|
-
{
|
|
628
|
+
{groups.map((group, g) => (
|
|
495
629
|
<Fragment key={group[0]?.key}>
|
|
496
630
|
{g > 0 && <hr className="foli-toolbar-separator" aria-orientation="vertical" />}
|
|
497
631
|
{group.map((item) => {
|
|
498
632
|
const i = index++;
|
|
499
633
|
const state = states[i];
|
|
500
634
|
const off = disabled || !state?.enabled;
|
|
635
|
+
if (item.select) {
|
|
636
|
+
const { select } = item;
|
|
637
|
+
const value = state?.value ?? "";
|
|
638
|
+
const options = select.options(editor.state);
|
|
639
|
+
return (
|
|
640
|
+
<select
|
|
641
|
+
key={item.key}
|
|
642
|
+
ref={(element) => {
|
|
643
|
+
buttons.current[i] = element;
|
|
644
|
+
}}
|
|
645
|
+
className={`foli-toolbar-select foli-toolbar-${item.key}`}
|
|
646
|
+
aria-label={item.label}
|
|
647
|
+
title={item.label}
|
|
648
|
+
// Still focusable when it cannot apply, like the buttons.
|
|
649
|
+
aria-disabled={off || undefined}
|
|
650
|
+
tabIndex={i === active ? 0 : -1}
|
|
651
|
+
value={value}
|
|
652
|
+
onFocus={() => setActive(i)}
|
|
653
|
+
onMouseDown={() => {
|
|
654
|
+
pointer.current = true;
|
|
655
|
+
}}
|
|
656
|
+
onKeyDown={() => {
|
|
657
|
+
pointer.current = false;
|
|
658
|
+
}}
|
|
659
|
+
// shortcut: Up and Down on a closed box apply each value they pass (Chrome
|
|
660
|
+
// and Firefox on Windows), one undo step each; apply on Enter if it matters.
|
|
661
|
+
onChange={(event) => {
|
|
662
|
+
if (off) return;
|
|
663
|
+
editor.run(select.pick(event.target.value));
|
|
664
|
+
if (pointer.current) onClickCommand?.();
|
|
665
|
+
}}
|
|
666
|
+
>
|
|
667
|
+
{/* Mixed (or nothing to show): a blank entry; a value not offered: itself. */}
|
|
668
|
+
{value === "" && <option value="" />}
|
|
669
|
+
{value !== "" && !options.some((o) => o.value === value) && (
|
|
670
|
+
<option value={value}>{value}</option>
|
|
671
|
+
)}
|
|
672
|
+
{options.map((o) => (
|
|
673
|
+
<option key={o.value} value={o.value}>
|
|
674
|
+
{o.label}
|
|
675
|
+
</option>
|
|
676
|
+
))}
|
|
677
|
+
</select>
|
|
678
|
+
);
|
|
679
|
+
}
|
|
501
680
|
const button = (
|
|
502
681
|
<button
|
|
503
682
|
key={item.key}
|
package/src/editor.tsx
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createEditor,
|
|
3
3
|
type Editor,
|
|
4
|
+
type EditorDocument,
|
|
4
5
|
type EditorState,
|
|
5
|
-
type FoliDocument,
|
|
6
6
|
type Plugin,
|
|
7
|
+
type ReviewView,
|
|
7
8
|
} from "@folijs/core";
|
|
8
9
|
import {
|
|
9
10
|
createContext,
|
|
@@ -25,20 +26,37 @@ export const useIsomorphicLayoutEffect =
|
|
|
25
26
|
typeof document === "undefined" ? useEffect : useLayoutEffect;
|
|
26
27
|
|
|
27
28
|
export interface UseFoliEditorOptions {
|
|
28
|
-
/**
|
|
29
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Read once, when the editor is created: a document of any version (0.1.0 and 0.2.0 documents
|
|
31
|
+
* are validated and migrated to schema v3, `parseDocument`). Default: an empty document. To
|
|
32
|
+
* load another document, `editor.setDocument()`.
|
|
33
|
+
*/
|
|
34
|
+
initialContent?: EditorDocument | undefined;
|
|
30
35
|
/** Read once, when the editor is created. */
|
|
31
36
|
plugins?: readonly Plugin[] | undefined;
|
|
37
|
+
/** Read once: who edits (tracked changes are made in this name; `createEditor`'s `author`). */
|
|
38
|
+
author?: string | undefined;
|
|
39
|
+
/** Read once: record edits as tracked changes from the start (a `.docx`'s `trackRevisions`). */
|
|
40
|
+
trackChanges?: boolean | undefined;
|
|
41
|
+
/** Read once: how tracked changes show at first (default `markup`). */
|
|
42
|
+
reviewView?: ReviewView | undefined;
|
|
32
43
|
}
|
|
33
44
|
|
|
34
45
|
/**
|
|
35
|
-
* A headless `Editor` (state, commands, history)
|
|
36
|
-
* first render and kept for its lifetime. Later changes to `options`
|
|
46
|
+
* A headless `Editor` (state, commands, history) of a schema v3 document, owned by the calling
|
|
47
|
+
* component: created on the first render and kept for its lifetime. Later changes to `options`
|
|
48
|
+
* are ignored. Throws FoliDocumentError when `initialContent` is not a valid document.
|
|
37
49
|
*/
|
|
38
50
|
export function useFoliEditor(options: UseFoliEditorOptions = {}): Editor {
|
|
39
51
|
const [editor] = useState(() => {
|
|
40
|
-
const { initialContent: document, plugins } = options;
|
|
41
|
-
return createEditor({
|
|
52
|
+
const { initialContent: document, plugins, author, trackChanges, reviewView } = options;
|
|
53
|
+
return createEditor({
|
|
54
|
+
...(document && { document }),
|
|
55
|
+
...(plugins && { plugins }),
|
|
56
|
+
...(author !== undefined && { author }),
|
|
57
|
+
...(trackChanges !== undefined && { trackChanges }),
|
|
58
|
+
...(reviewView !== undefined && { reviewView }),
|
|
59
|
+
});
|
|
42
60
|
});
|
|
43
61
|
return editor;
|
|
44
62
|
}
|
package/src/index.ts
CHANGED