@folijs/react 0.4.0 → 0.6.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 +61 -0
- package/README.md +4 -2
- package/dist/FoliCanvas.d.ts +8 -0
- package/dist/FoliCanvas.d.ts.map +1 -1
- package/dist/FoliCanvas.js +9 -4
- package/dist/FoliCanvas.js.map +1 -1
- package/dist/FoliEditor.d.ts.map +1 -1
- package/dist/FoliEditor.js +2 -1
- package/dist/FoliEditor.js.map +1 -1
- package/dist/FoliNavigation.d.ts +15 -0
- package/dist/FoliNavigation.d.ts.map +1 -0
- package/dist/FoliNavigation.js +491 -0
- package/dist/FoliNavigation.js.map +1 -0
- package/dist/FoliToolbar.d.ts +9 -2
- package/dist/FoliToolbar.d.ts.map +1 -1
- package/dist/FoliToolbar.js +1223 -13
- package/dist/FoliToolbar.js.map +1 -1
- package/dist/editor.d.ts +2 -0
- package/dist/editor.d.ts.map +1 -1
- package/dist/editor.js +6 -0
- 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/FoliCanvas.tsx +26 -5
- package/src/FoliEditor.tsx +3 -0
- package/src/FoliNavigation.tsx +895 -0
- package/src/FoliToolbar.tsx +1900 -10
- package/src/editor.tsx +11 -0
- package/src/index.ts +1 -0
- package/styles.css +202 -1
package/src/FoliToolbar.tsx
CHANGED
|
@@ -3,22 +3,54 @@ import {
|
|
|
3
3
|
acceptAllChanges,
|
|
4
4
|
acceptChange,
|
|
5
5
|
addComment,
|
|
6
|
+
addRepeatingItem,
|
|
6
7
|
alignAt,
|
|
7
8
|
type Command,
|
|
9
|
+
type ControlJSON,
|
|
8
10
|
canOutdent,
|
|
9
11
|
changeIndent,
|
|
10
12
|
colorAt,
|
|
13
|
+
controlAt,
|
|
14
|
+
controlProperties,
|
|
15
|
+
convertNotes,
|
|
16
|
+
crossReferenceTargets,
|
|
17
|
+
deleteColumns,
|
|
18
|
+
deleteRows,
|
|
19
|
+
deleteTable,
|
|
20
|
+
documentSections,
|
|
11
21
|
type Editor,
|
|
12
22
|
type EditorState,
|
|
23
|
+
type FieldLayout,
|
|
24
|
+
fieldCodesShown,
|
|
25
|
+
fieldsIn,
|
|
13
26
|
fontFamilyAt,
|
|
14
27
|
fontSizeAt,
|
|
28
|
+
insertColumn,
|
|
29
|
+
insertColumnBreak,
|
|
30
|
+
insertControl,
|
|
31
|
+
insertCrossReference,
|
|
32
|
+
insertDate,
|
|
33
|
+
insertNote,
|
|
15
34
|
insertPageBreak,
|
|
35
|
+
insertPageNumber,
|
|
36
|
+
insertPicture,
|
|
37
|
+
insertRow,
|
|
38
|
+
insertSectionBreak,
|
|
39
|
+
insertTable,
|
|
40
|
+
insertTableOfContents,
|
|
41
|
+
isHeaderRow,
|
|
16
42
|
isSafeHref,
|
|
17
43
|
lineSpacingAt,
|
|
18
44
|
linkAt,
|
|
19
45
|
listKindAt,
|
|
46
|
+
mergeCells,
|
|
47
|
+
type NavigationRequest,
|
|
48
|
+
type NoteKind,
|
|
49
|
+
type NotePropertiesJSON,
|
|
20
50
|
nextChange,
|
|
21
51
|
normalizeHref,
|
|
52
|
+
noteAtSelection,
|
|
53
|
+
noteProperties,
|
|
22
54
|
openLinkEditor,
|
|
23
55
|
paragraphStyleAt,
|
|
24
56
|
paragraphStyles,
|
|
@@ -29,24 +61,48 @@ import {
|
|
|
29
61
|
rejectAllChanges,
|
|
30
62
|
rejectChange,
|
|
31
63
|
removeLink,
|
|
64
|
+
removeSectionBreak,
|
|
32
65
|
requestsLinkEditor,
|
|
66
|
+
type SectionJSON,
|
|
67
|
+
type SectionSetup,
|
|
68
|
+
type StorySide,
|
|
69
|
+
sectionAt,
|
|
70
|
+
selectedPicture,
|
|
33
71
|
setAlign,
|
|
72
|
+
setCellShading,
|
|
73
|
+
setControlProperties,
|
|
74
|
+
setDifferentEvenPages,
|
|
75
|
+
setDifferentFirstPage,
|
|
34
76
|
setFontFamily,
|
|
35
77
|
setFontSize,
|
|
36
78
|
setHighlight,
|
|
37
79
|
setLineSpacing,
|
|
38
80
|
setLink,
|
|
81
|
+
setNoteProperties,
|
|
39
82
|
setParagraphStyle,
|
|
83
|
+
setPictureAlt,
|
|
84
|
+
setPictureSize,
|
|
40
85
|
setReviewView,
|
|
86
|
+
setSectionSetup,
|
|
87
|
+
setStoryLinked,
|
|
88
|
+
setTableStyle,
|
|
41
89
|
setTextColor,
|
|
42
90
|
setTrackChanges,
|
|
91
|
+
splitCell,
|
|
92
|
+
tableStyleAt,
|
|
93
|
+
tableStyles,
|
|
94
|
+
toggleFieldCodes,
|
|
95
|
+
toggleHeaderRow,
|
|
43
96
|
toggleList,
|
|
44
97
|
toggleMark,
|
|
45
98
|
trackState,
|
|
99
|
+
type UpdateFieldsOptions,
|
|
46
100
|
undo,
|
|
47
101
|
undoDepth,
|
|
102
|
+
updateFields,
|
|
48
103
|
wordContext,
|
|
49
104
|
} from "@folijs/core";
|
|
105
|
+
import { type EditorViewOptions, EMU_PER_PX, measureImageFile, textWidthEmu } from "@folijs/view";
|
|
50
106
|
import {
|
|
51
107
|
type FormEvent,
|
|
52
108
|
Fragment,
|
|
@@ -55,10 +111,11 @@ import {
|
|
|
55
111
|
type ReactNode,
|
|
56
112
|
useEffect,
|
|
57
113
|
useId,
|
|
114
|
+
useMemo,
|
|
58
115
|
useRef,
|
|
59
116
|
useState,
|
|
60
117
|
} from "react";
|
|
61
|
-
import { useEditorOrContext, useEditorState } from "./editor.js";
|
|
118
|
+
import { useActiveEditor, useEditorOrContext, useEditorState } from "./editor.js";
|
|
62
119
|
|
|
63
120
|
export interface FoliToolbarProps {
|
|
64
121
|
/** Default: the editor of the nearest `FoliEditorProvider`. */
|
|
@@ -80,6 +137,11 @@ export interface FoliToolbarProps {
|
|
|
80
137
|
* (`FoliEditor` passes it). Default false.
|
|
81
138
|
*/
|
|
82
139
|
comments?: boolean | undefined;
|
|
140
|
+
/**
|
|
141
|
+
* Where an inserted picture's image file goes (`EditorViewOptions.upload`): with it, "Picture"
|
|
142
|
+
* inserts one from a file; without it, it only edits a selected picture.
|
|
143
|
+
*/
|
|
144
|
+
upload?: EditorViewOptions["upload"] | undefined;
|
|
83
145
|
className?: string | undefined;
|
|
84
146
|
}
|
|
85
147
|
|
|
@@ -109,6 +171,10 @@ interface Item {
|
|
|
109
171
|
value: (state: EditorState) => string | null;
|
|
110
172
|
pick: (color: string | null) => Command;
|
|
111
173
|
};
|
|
174
|
+
/** An action menu button (the table menu): its commands, in order. */
|
|
175
|
+
actions?: readonly Action[] | ((editor: Editor) => readonly Action[]);
|
|
176
|
+
/** A button opening a dialog (the picture's). */
|
|
177
|
+
dialog?: "picture";
|
|
112
178
|
/** A drop-down list (a `select`): its options, the current value, and what picking runs. */
|
|
113
179
|
select?: {
|
|
114
180
|
options: (state: EditorState) => readonly { value: string; label: string }[];
|
|
@@ -117,6 +183,18 @@ interface Item {
|
|
|
117
183
|
};
|
|
118
184
|
}
|
|
119
185
|
|
|
186
|
+
/** A command in an action menu (a menuitem, or a menuitemcheckbox when it has `checked`). */
|
|
187
|
+
interface Action {
|
|
188
|
+
key: string;
|
|
189
|
+
label: string;
|
|
190
|
+
/** The command it runs; with `dialog`, whether it applies: the dialog does the rest. */
|
|
191
|
+
command: Command;
|
|
192
|
+
dialog?: "insertTable" | "pageSetup" | "crossReference" | "controlProperties" | "noteOptions";
|
|
193
|
+
checked?: (state: EditorState) => boolean;
|
|
194
|
+
/** It takes the focus where it belongs (into a story, or back to the body). */
|
|
195
|
+
movesFocus?: boolean;
|
|
196
|
+
}
|
|
197
|
+
|
|
120
198
|
interface ColorOption {
|
|
121
199
|
name: string;
|
|
122
200
|
/** Lowercase `#rrggbb`; null removes the color. */
|
|
@@ -146,6 +224,311 @@ const HIGHLIGHTS: readonly ColorOption[] = [
|
|
|
146
224
|
{ name: "Gray", color: "#c0c0c0" },
|
|
147
225
|
];
|
|
148
226
|
|
|
227
|
+
/** Cell shadings: light fills that keep text readable. */
|
|
228
|
+
const SHADINGS: readonly ColorOption[] = [
|
|
229
|
+
{ name: "None", color: null },
|
|
230
|
+
{ name: "Light gray", color: "#d9d9d9" },
|
|
231
|
+
{ name: "Light blue", color: "#deeaf6" },
|
|
232
|
+
{ name: "Light green", color: "#e2efd9" },
|
|
233
|
+
{ name: "Light yellow", color: "#fff2cc" },
|
|
234
|
+
{ name: "Light orange", color: "#fbe4d5" },
|
|
235
|
+
];
|
|
236
|
+
|
|
237
|
+
/** The cell around the selection's start, if any (cheap: the start's ancestors). */
|
|
238
|
+
function cellAt(state: EditorState) {
|
|
239
|
+
const { $from } = state.selection;
|
|
240
|
+
for (let d = $from.depth; d > 0; d--)
|
|
241
|
+
if ($from.node(d).type.name === "tableCell") return $from.node(d);
|
|
242
|
+
// A cell selection starts before its first cell.
|
|
243
|
+
const after = $from.nodeAfter;
|
|
244
|
+
return after?.type.name === "tableCell" ? after : null;
|
|
245
|
+
}
|
|
246
|
+
const inTable = (state: EditorState) => cellAt(state) !== null;
|
|
247
|
+
/** Table and cell properties change only when not tracking (Foli does not record them yet). */
|
|
248
|
+
const canFormatTable = (state: EditorState) => inTable(state) && trackState(state)?.on !== true;
|
|
249
|
+
|
|
250
|
+
const TABLE_ACTIONS: readonly Action[] = [
|
|
251
|
+
{ key: "insertTable", label: "Insert table…", command: insertTable(1, 1), dialog: "insertTable" },
|
|
252
|
+
{ key: "rowAbove", label: "Insert row above", command: insertRow(-1) },
|
|
253
|
+
{ key: "rowBelow", label: "Insert row below", command: insertRow(1) },
|
|
254
|
+
{ key: "columnLeft", label: "Insert column left", command: insertColumn(-1) },
|
|
255
|
+
{ key: "columnRight", label: "Insert column right", command: insertColumn(1) },
|
|
256
|
+
{ key: "deleteRows", label: "Delete rows", command: deleteRows },
|
|
257
|
+
{ key: "deleteColumns", label: "Delete columns", command: deleteColumns },
|
|
258
|
+
{ key: "deleteTable", label: "Delete table", command: deleteTable },
|
|
259
|
+
{ key: "mergeCells", label: "Merge cells", command: mergeCells },
|
|
260
|
+
{ key: "splitCell", label: "Split cell", command: splitCell },
|
|
261
|
+
{
|
|
262
|
+
key: "headerRow",
|
|
263
|
+
label: "Repeat as header row",
|
|
264
|
+
command: toggleHeaderRow,
|
|
265
|
+
checked: isHeaderRow,
|
|
266
|
+
},
|
|
267
|
+
];
|
|
268
|
+
|
|
269
|
+
const SECTION_ACTIONS: readonly Action[] = [
|
|
270
|
+
{
|
|
271
|
+
key: "pageSetup",
|
|
272
|
+
label: "Page setup…",
|
|
273
|
+
command: (state) => sectionAt(state) !== null,
|
|
274
|
+
dialog: "pageSetup",
|
|
275
|
+
},
|
|
276
|
+
{ key: "breakNext", label: "Section break: next page", command: insertSectionBreak("nextPage") },
|
|
277
|
+
{
|
|
278
|
+
key: "breakContinuous",
|
|
279
|
+
label: "Section break: continuous",
|
|
280
|
+
command: insertSectionBreak("continuous"),
|
|
281
|
+
},
|
|
282
|
+
{ key: "breakEven", label: "Section break: even page", command: insertSectionBreak("evenPage") },
|
|
283
|
+
{ key: "breakOdd", label: "Section break: odd page", command: insertSectionBreak("oddPage") },
|
|
284
|
+
{ key: "removeBreak", label: "Remove section break", command: removeSectionBreak },
|
|
285
|
+
{ key: "columnBreak", label: "Column break", command: insertColumnBreak },
|
|
286
|
+
];
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* The header and footer menu: opening them on the caret's page, and, for the one open (else the
|
|
290
|
+
* caret's section), what a section does about them.
|
|
291
|
+
*/
|
|
292
|
+
function storyActions(body: Editor): readonly Action[] {
|
|
293
|
+
const open = () => body.storyView?.open ?? null;
|
|
294
|
+
const setup = (state: EditorState) => {
|
|
295
|
+
const index = open()?.section ?? sectionAt(state)?.index ?? 0;
|
|
296
|
+
return documentSections(state)[index]?.setup ?? {};
|
|
297
|
+
};
|
|
298
|
+
const edit =
|
|
299
|
+
(side: StorySide): Command =>
|
|
300
|
+
(_state, dispatch) => {
|
|
301
|
+
const view = body.storyView;
|
|
302
|
+
if (!view) return false;
|
|
303
|
+
if (dispatch) view.edit(side);
|
|
304
|
+
return true;
|
|
305
|
+
};
|
|
306
|
+
const linked = (state: EditorState) => {
|
|
307
|
+
const story = open();
|
|
308
|
+
if (!story) return false;
|
|
309
|
+
if (story.side === "note") return false;
|
|
310
|
+
const references = story.side === "header" ? setup(state).headers : setup(state).footers;
|
|
311
|
+
return !references?.[story.variant];
|
|
312
|
+
};
|
|
313
|
+
const evenPages = (state: EditorState) =>
|
|
314
|
+
wordContext(state)?.envelope.settings?.evenAndOddHeaders === true;
|
|
315
|
+
return [
|
|
316
|
+
{ key: "editHeader", label: "Edit header", command: edit("header"), movesFocus: true },
|
|
317
|
+
{ key: "editFooter", label: "Edit footer", command: edit("footer"), movesFocus: true },
|
|
318
|
+
{
|
|
319
|
+
key: "closeStory",
|
|
320
|
+
label: "Close header and footer",
|
|
321
|
+
movesFocus: true,
|
|
322
|
+
command: (_state, dispatch) => {
|
|
323
|
+
if (!open()) return false;
|
|
324
|
+
if (dispatch) body.storyView?.close();
|
|
325
|
+
return true;
|
|
326
|
+
},
|
|
327
|
+
},
|
|
328
|
+
{
|
|
329
|
+
key: "firstPage",
|
|
330
|
+
label: "Different first page",
|
|
331
|
+
checked: (state) => setup(state).titlePage === true,
|
|
332
|
+
command: (state, dispatch) =>
|
|
333
|
+
setDifferentFirstPage(setup(state).titlePage !== true, open()?.section)(state, dispatch),
|
|
334
|
+
},
|
|
335
|
+
{
|
|
336
|
+
key: "evenPages",
|
|
337
|
+
label: "Different odd and even pages",
|
|
338
|
+
checked: evenPages,
|
|
339
|
+
command: (state, dispatch) => setDifferentEvenPages(!evenPages(state))(state, dispatch),
|
|
340
|
+
},
|
|
341
|
+
{
|
|
342
|
+
key: "linkPrevious",
|
|
343
|
+
label: "Link to previous",
|
|
344
|
+
checked: linked,
|
|
345
|
+
command: (state, dispatch) => {
|
|
346
|
+
const story = open();
|
|
347
|
+
return story && story.side !== "note"
|
|
348
|
+
? setStoryLinked(
|
|
349
|
+
story.side,
|
|
350
|
+
story.variant,
|
|
351
|
+
!linked(state),
|
|
352
|
+
story.section,
|
|
353
|
+
)(state, dispatch)
|
|
354
|
+
: false;
|
|
355
|
+
},
|
|
356
|
+
},
|
|
357
|
+
];
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* The notes menu: a new note (the caret goes into it), between a reference and its note, the
|
|
362
|
+
* conversions Word has, and the options dialog.
|
|
363
|
+
*/
|
|
364
|
+
function noteActions(body: Editor): readonly Action[] {
|
|
365
|
+
const insert =
|
|
366
|
+
(kind: NoteKind): Command =>
|
|
367
|
+
(state, dispatch) => {
|
|
368
|
+
if (!insertNote(kind)(state)) return false;
|
|
369
|
+
if (dispatch) {
|
|
370
|
+
insertNote(kind)(state, dispatch);
|
|
371
|
+
body.storyView?.editNote();
|
|
372
|
+
}
|
|
373
|
+
return true;
|
|
374
|
+
};
|
|
375
|
+
return [
|
|
376
|
+
{ key: "footnote", label: "Insert footnote", command: insert("footnote"), movesFocus: true },
|
|
377
|
+
{ key: "endnote", label: "Insert endnote", command: insert("endnote"), movesFocus: true },
|
|
378
|
+
{
|
|
379
|
+
key: "goToNote",
|
|
380
|
+
label: "Go to note or reference",
|
|
381
|
+
movesFocus: true,
|
|
382
|
+
command: (state, dispatch) => {
|
|
383
|
+
const view = body.storyView;
|
|
384
|
+
if (!view) return false;
|
|
385
|
+
if (view.open?.side === "note") {
|
|
386
|
+
if (dispatch) view.toReference();
|
|
387
|
+
return true;
|
|
388
|
+
}
|
|
389
|
+
if (!noteAtSelection(state)) return false;
|
|
390
|
+
if (dispatch) view.editNote();
|
|
391
|
+
return true;
|
|
392
|
+
},
|
|
393
|
+
},
|
|
394
|
+
{
|
|
395
|
+
key: "toEndnotes",
|
|
396
|
+
label: "Convert footnotes to endnotes",
|
|
397
|
+
command: (_state, dispatch) => convertNotes("endnote")(body.state, dispatch && body.dispatch),
|
|
398
|
+
},
|
|
399
|
+
{
|
|
400
|
+
key: "toFootnotes",
|
|
401
|
+
label: "Convert endnotes to footnotes",
|
|
402
|
+
command: (_state, dispatch) =>
|
|
403
|
+
convertNotes("footnote")(body.state, dispatch && body.dispatch),
|
|
404
|
+
},
|
|
405
|
+
{
|
|
406
|
+
key: "noteOptions",
|
|
407
|
+
label: "Note options…",
|
|
408
|
+
dialog: "noteOptions",
|
|
409
|
+
command: (state) => wordContext(state) !== null,
|
|
410
|
+
},
|
|
411
|
+
];
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/** The control the selection is in. */
|
|
415
|
+
const selectedControl = (state: EditorState) => {
|
|
416
|
+
const range = controlAt(state.doc, state.selection.from);
|
|
417
|
+
const control = range && controlProperties(state, range.id);
|
|
418
|
+
return range && control ? { id: range.id, control } : null;
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
/** The content controls menu: each kind to insert, then what applies to the one at the caret. */
|
|
422
|
+
const CONTROL_ACTIONS: readonly Action[] = [
|
|
423
|
+
{ key: "richText", label: "Rich text", command: insertControl("richText") },
|
|
424
|
+
{ key: "plainText", label: "Plain text", command: insertControl("text") },
|
|
425
|
+
{ key: "dropDown", label: "Drop-down list", command: insertControl("dropDown") },
|
|
426
|
+
{ key: "comboBox", label: "Combo box", command: insertControl("comboBox") },
|
|
427
|
+
{ key: "datePicker", label: "Date picker", command: insertControl("date") },
|
|
428
|
+
{ key: "checkBox", label: "Check box", command: insertControl("checkbox") },
|
|
429
|
+
{ key: "pictureControl", label: "Picture (selected)", command: insertControl("picture") },
|
|
430
|
+
{ key: "repeating", label: "Repeating section", command: insertControl("repeating") },
|
|
431
|
+
{ key: "repeatingItem", label: "Add repeating item", command: addRepeatingItem() },
|
|
432
|
+
{
|
|
433
|
+
key: "controlProperties",
|
|
434
|
+
label: "Control properties…",
|
|
435
|
+
dialog: "controlProperties",
|
|
436
|
+
command: (state) => selectedControl(state) !== null,
|
|
437
|
+
},
|
|
438
|
+
];
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* The navigation menu: what `FoliNavigation` shows, with Word's keys. Nothing applies without it.
|
|
442
|
+
*/
|
|
443
|
+
function navigationActions(body: Editor): readonly Action[] {
|
|
444
|
+
const open =
|
|
445
|
+
(request: NavigationRequest): Command =>
|
|
446
|
+
(_state, dispatch) => {
|
|
447
|
+
if (!body.navigation) return false;
|
|
448
|
+
if (dispatch) body.navigation(request);
|
|
449
|
+
return true;
|
|
450
|
+
};
|
|
451
|
+
const item = (key: NavigationRequest, label: string): Action => ({
|
|
452
|
+
key,
|
|
453
|
+
label,
|
|
454
|
+
command: open(key),
|
|
455
|
+
movesFocus: true,
|
|
456
|
+
});
|
|
457
|
+
return [
|
|
458
|
+
item("find", "Find\u2026"),
|
|
459
|
+
item("replace", "Replace\u2026"),
|
|
460
|
+
item("goTo", "Go to\u2026"),
|
|
461
|
+
item("bookmarks", "Bookmarks\u2026"),
|
|
462
|
+
item("outline", "Outline"),
|
|
463
|
+
];
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* The fields menu: what can be inserted at the selection, updating (the view's pages when there is
|
|
468
|
+
* one) and the codes shown or hidden.
|
|
469
|
+
*/
|
|
470
|
+
function fieldActions(body: Editor): readonly Action[] {
|
|
471
|
+
const layout = () => body.storyView?.fieldLayout ?? null;
|
|
472
|
+
const active = () => {
|
|
473
|
+
const open = body.storyView?.open;
|
|
474
|
+
return (open && body.story(open.id)) || body;
|
|
475
|
+
};
|
|
476
|
+
const update = (editor: Editor, options: UpdateFieldsOptions) =>
|
|
477
|
+
body.storyView ? body.storyView.updateFields(options, editor) : updateFields(editor, options);
|
|
478
|
+
return [
|
|
479
|
+
{
|
|
480
|
+
key: "pageNumber",
|
|
481
|
+
label: "Page number",
|
|
482
|
+
command: (state, dispatch) => insertPageNumber({ layout: layout() })(state, dispatch),
|
|
483
|
+
},
|
|
484
|
+
{ key: "date", label: "Date", command: insertDate() },
|
|
485
|
+
{
|
|
486
|
+
key: "crossReference",
|
|
487
|
+
label: "Cross-reference…",
|
|
488
|
+
dialog: "crossReference",
|
|
489
|
+
command: (state) => {
|
|
490
|
+
const { bookmarks, headings } = crossReferenceTargets(state);
|
|
491
|
+
return insertPageBreak(state) && bookmarks.length + headings.length > 0;
|
|
492
|
+
},
|
|
493
|
+
},
|
|
494
|
+
{
|
|
495
|
+
key: "tableOfContents",
|
|
496
|
+
label: "Table of contents",
|
|
497
|
+
command: (state, dispatch) => {
|
|
498
|
+
const done = insertTableOfContents({ layout: layout() })(state, dispatch);
|
|
499
|
+
// Its own lines moved the headings: their page numbers follow, in the same undo step.
|
|
500
|
+
if (done && dispatch)
|
|
501
|
+
update(body, { only: (field) => field.parent?.name === "TOC", continues: true });
|
|
502
|
+
return done;
|
|
503
|
+
},
|
|
504
|
+
},
|
|
505
|
+
{
|
|
506
|
+
key: "updateSelected",
|
|
507
|
+
label: "Update selected fields",
|
|
508
|
+
command: (state, dispatch) => {
|
|
509
|
+
if (!fieldsIn(state.doc).length) return false;
|
|
510
|
+
const { from, to } = state.selection;
|
|
511
|
+
if (dispatch) update(active(), { only: { from, to } });
|
|
512
|
+
return true;
|
|
513
|
+
},
|
|
514
|
+
},
|
|
515
|
+
{
|
|
516
|
+
key: "updateAll",
|
|
517
|
+
label: "Update all fields",
|
|
518
|
+
command: (_state, dispatch) => {
|
|
519
|
+
if (dispatch) update(body, {});
|
|
520
|
+
return true;
|
|
521
|
+
},
|
|
522
|
+
},
|
|
523
|
+
{
|
|
524
|
+
key: "fieldCodes",
|
|
525
|
+
label: "Show field codes",
|
|
526
|
+
checked: fieldCodesShown,
|
|
527
|
+
command: toggleFieldCodes,
|
|
528
|
+
},
|
|
529
|
+
];
|
|
530
|
+
}
|
|
531
|
+
|
|
149
532
|
/** Whether the selection holds a textblock (a caret's own block first; else it walks the range). */
|
|
150
533
|
function hasTextblock(state: EditorState): boolean {
|
|
151
534
|
if (state.selection.$from.parent.isTextblock) return true;
|
|
@@ -460,7 +843,94 @@ const GROUPS: Item[][] = [
|
|
|
460
843
|
command: () => redo,
|
|
461
844
|
},
|
|
462
845
|
],
|
|
463
|
-
[
|
|
846
|
+
[
|
|
847
|
+
{
|
|
848
|
+
key: "table",
|
|
849
|
+
label: "Table",
|
|
850
|
+
text: "Table",
|
|
851
|
+
enabled: (state) => wordContext(state) !== null && "table" in state.schema.nodes,
|
|
852
|
+
actions: TABLE_ACTIONS,
|
|
853
|
+
},
|
|
854
|
+
{
|
|
855
|
+
key: "tableStyle",
|
|
856
|
+
label: "Table style",
|
|
857
|
+
text: "Table style",
|
|
858
|
+
enabled: canFormatTable,
|
|
859
|
+
select: {
|
|
860
|
+
options: (state) => tableStyles(state).map((name) => ({ value: name, label: name })),
|
|
861
|
+
value: (state) => (inTable(state) ? (tableStyleAt(state) ?? "") : null),
|
|
862
|
+
pick: (name) => setTableStyle(name),
|
|
863
|
+
},
|
|
864
|
+
},
|
|
865
|
+
{
|
|
866
|
+
key: "cellShading",
|
|
867
|
+
label: "Cell shading",
|
|
868
|
+
text: "Shading",
|
|
869
|
+
enabled: canFormatTable,
|
|
870
|
+
menu: {
|
|
871
|
+
options: SHADINGS,
|
|
872
|
+
value: (state) => {
|
|
873
|
+
const fill = (cellAt(state)?.attrs.shading as { fill?: string } | null)?.fill;
|
|
874
|
+
return fill && fill.toLowerCase() !== "auto" ? `#${fill.toLowerCase()}` : null;
|
|
875
|
+
},
|
|
876
|
+
pick: (color) => setCellShading(color ? color.slice(1).toUpperCase() : null),
|
|
877
|
+
},
|
|
878
|
+
},
|
|
879
|
+
],
|
|
880
|
+
[
|
|
881
|
+
{
|
|
882
|
+
key: "picture",
|
|
883
|
+
label: "Insert a picture, or edit the selected one",
|
|
884
|
+
text: "Picture…",
|
|
885
|
+
enabled: (state) =>
|
|
886
|
+
wordContext(state) !== null && (selectedPicture(state) !== null || hasTextblock(state)),
|
|
887
|
+
// With a picture selected the dialog edits it.
|
|
888
|
+
dialog: "picture",
|
|
889
|
+
},
|
|
890
|
+
{ key: "pageBreak", label: "Page break", text: "Page break", command: () => insertPageBreak },
|
|
891
|
+
{
|
|
892
|
+
key: "section",
|
|
893
|
+
label: "Section",
|
|
894
|
+
text: "Section",
|
|
895
|
+
enabled: (state) => wordContext(state) !== null,
|
|
896
|
+
actions: SECTION_ACTIONS,
|
|
897
|
+
},
|
|
898
|
+
{
|
|
899
|
+
key: "headerFooter",
|
|
900
|
+
label: "Header and footer",
|
|
901
|
+
text: "Header",
|
|
902
|
+
enabled: (state) => wordContext(state) !== null,
|
|
903
|
+
actions: storyActions,
|
|
904
|
+
},
|
|
905
|
+
{
|
|
906
|
+
key: "fields",
|
|
907
|
+
label: "Fields",
|
|
908
|
+
text: "Fields",
|
|
909
|
+
enabled: (state) => wordContext(state) !== null,
|
|
910
|
+
actions: fieldActions,
|
|
911
|
+
},
|
|
912
|
+
{
|
|
913
|
+
key: "navigate",
|
|
914
|
+
label: "Find and go to",
|
|
915
|
+
text: "Navigate",
|
|
916
|
+
enabled: (state) => wordContext(state) !== null,
|
|
917
|
+
actions: navigationActions,
|
|
918
|
+
},
|
|
919
|
+
{
|
|
920
|
+
key: "notes",
|
|
921
|
+
label: "Footnotes and endnotes",
|
|
922
|
+
text: "Notes",
|
|
923
|
+
enabled: (state) => wordContext(state) !== null,
|
|
924
|
+
actions: noteActions,
|
|
925
|
+
},
|
|
926
|
+
{
|
|
927
|
+
key: "controls",
|
|
928
|
+
label: "Content controls",
|
|
929
|
+
text: "Controls",
|
|
930
|
+
enabled: (state) => wordContext(state) !== null,
|
|
931
|
+
actions: CONTROL_ACTIONS,
|
|
932
|
+
},
|
|
933
|
+
],
|
|
464
934
|
];
|
|
465
935
|
|
|
466
936
|
const ITEMS = GROUPS.flat();
|
|
@@ -500,7 +970,10 @@ const statesOf =
|
|
|
500
970
|
items.map((item) => ({
|
|
501
971
|
pressed: item.pressed?.(state, editor),
|
|
502
972
|
enabled: item.enabled?.(state) ?? true,
|
|
503
|
-
value:
|
|
973
|
+
value:
|
|
974
|
+
item.menu?.value(state) ??
|
|
975
|
+
item.select?.value(state) ??
|
|
976
|
+
(item.dialog ? (selectedPicture(state) ? "edit" : "insert") : undefined),
|
|
504
977
|
}));
|
|
505
978
|
const selectStates = statesOf(ITEMS);
|
|
506
979
|
const selectStatesNoComment = statesOf(ITEMS_NO_COMMENT);
|
|
@@ -508,7 +981,8 @@ const selectStatesNoComment = statesOf(ITEMS_NO_COMMENT);
|
|
|
508
981
|
/**
|
|
509
982
|
* Word's formatting controls for an editor: paragraph style, font and size boxes, marks, link (a
|
|
510
983
|
* 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,
|
|
984
|
+
* spacing, lists (bulleted, numbered, decrease and increase indent), undo, redo, tables (a Table
|
|
985
|
+
* menu with an insert table dialog, a table style box, a cell shading menu), page break.
|
|
512
986
|
* One tab stop (arrow keys, Home and End move between buttons); toggles carry
|
|
513
987
|
* `aria-pressed`; a button whose command cannot run now is `aria-disabled` (still focusable).
|
|
514
988
|
* Clicks leave the focus and selection in the document.
|
|
@@ -520,8 +994,11 @@ export function FoliToolbar({
|
|
|
520
994
|
label = "Formatting",
|
|
521
995
|
className,
|
|
522
996
|
comments = false,
|
|
997
|
+
upload,
|
|
523
998
|
}: FoliToolbarProps) {
|
|
524
|
-
const
|
|
999
|
+
const body = useEditorOrContext(given, "FoliToolbar");
|
|
1000
|
+
// A story open on its page takes the formatting commands; the body otherwise.
|
|
1001
|
+
const editor = useActiveEditor(body);
|
|
525
1002
|
const groups = comments ? GROUPS : GROUPS_NO_COMMENT;
|
|
526
1003
|
const items = comments ? ITEMS : ITEMS_NO_COMMENT;
|
|
527
1004
|
const states = useEditorState(comments ? selectStates : selectStatesNoComment, {
|
|
@@ -541,7 +1018,20 @@ export function FoliToolbar({
|
|
|
541
1018
|
const run = (index: number, event: MouseEvent) => {
|
|
542
1019
|
const item = items[index];
|
|
543
1020
|
if (!item || disabled || !states[index]?.enabled) return;
|
|
544
|
-
if (item.
|
|
1021
|
+
if (item.dialog === "picture") {
|
|
1022
|
+
const selected = selectedPicture(editor.state);
|
|
1023
|
+
// Inserting needs the app's upload; editing a selected picture does not.
|
|
1024
|
+
if (!selected && !upload) return;
|
|
1025
|
+
setDialog(null);
|
|
1026
|
+
setTableDialog(null);
|
|
1027
|
+
setPictureDialog({
|
|
1028
|
+
pos: selected?.pos ?? null,
|
|
1029
|
+
selection: editor.state.selection,
|
|
1030
|
+
restore: buttons.current[index] ?? null,
|
|
1031
|
+
});
|
|
1032
|
+
return;
|
|
1033
|
+
}
|
|
1034
|
+
if (item.menu || item.actions) {
|
|
545
1035
|
if (menu !== item.key) {
|
|
546
1036
|
setMenu(item.key);
|
|
547
1037
|
return;
|
|
@@ -568,6 +1058,57 @@ export function FoliToolbar({
|
|
|
568
1058
|
editor.subscribe((state, transactions) => {
|
|
569
1059
|
if (disabled) return;
|
|
570
1060
|
if (!transactions.some(requestsLinkEditor)) {
|
|
1061
|
+
// The picture dialog too: what it edits or inserts at may have gone (another
|
|
1062
|
+
// document, set with no transaction, included).
|
|
1063
|
+
setPictureDialog((current) =>
|
|
1064
|
+
current &&
|
|
1065
|
+
(!transactions.length ||
|
|
1066
|
+
transactions.some((tr) => tr.docChanged) ||
|
|
1067
|
+
!state.selection.eq(current.selection))
|
|
1068
|
+
? null
|
|
1069
|
+
: current,
|
|
1070
|
+
);
|
|
1071
|
+
// The cross-reference dialog inserts at the selection it opened on.
|
|
1072
|
+
setReferenceDialog((current) =>
|
|
1073
|
+
current &&
|
|
1074
|
+
(!transactions.length ||
|
|
1075
|
+
transactions.some((tr) => tr.docChanged) ||
|
|
1076
|
+
!state.selection.eq(current.selection))
|
|
1077
|
+
? null
|
|
1078
|
+
: current,
|
|
1079
|
+
);
|
|
1080
|
+
setNoteDialog((current) =>
|
|
1081
|
+
current && (!transactions.length || transactions.some((tr) => tr.docChanged))
|
|
1082
|
+
? null
|
|
1083
|
+
: current,
|
|
1084
|
+
);
|
|
1085
|
+
// The control properties dialog is for the control it opened on.
|
|
1086
|
+
setControlDialog((current) =>
|
|
1087
|
+
current &&
|
|
1088
|
+
(!transactions.length ||
|
|
1089
|
+
transactions.some((tr) => tr.docChanged) ||
|
|
1090
|
+
!state.selection.eq(current.selection))
|
|
1091
|
+
? null
|
|
1092
|
+
: current,
|
|
1093
|
+
);
|
|
1094
|
+
// So is the page setup dialog: its section may have changed.
|
|
1095
|
+
setSetupDialog((current) =>
|
|
1096
|
+
current &&
|
|
1097
|
+
(!transactions.length ||
|
|
1098
|
+
transactions.some((tr) => tr.docChanged) ||
|
|
1099
|
+
!state.selection.eq(current.selection))
|
|
1100
|
+
? null
|
|
1101
|
+
: current,
|
|
1102
|
+
);
|
|
1103
|
+
// The insert table dialog is for the caret it opened on, as the link dialog is.
|
|
1104
|
+
setTableDialog((current) =>
|
|
1105
|
+
current &&
|
|
1106
|
+
(!transactions.length ||
|
|
1107
|
+
transactions.some((tr) => tr.docChanged) ||
|
|
1108
|
+
!state.selection.eq(current.selection))
|
|
1109
|
+
? null
|
|
1110
|
+
: current,
|
|
1111
|
+
);
|
|
571
1112
|
setDialog((current) =>
|
|
572
1113
|
current &&
|
|
573
1114
|
(transactions.some((tr) => tr.docChanged) || !state.selection.eq(current.selection))
|
|
@@ -577,6 +1118,11 @@ export function FoliToolbar({
|
|
|
577
1118
|
return;
|
|
578
1119
|
}
|
|
579
1120
|
const active = typeof document === "undefined" ? null : document.activeElement;
|
|
1121
|
+
setTableDialog(null);
|
|
1122
|
+
setPictureDialog(null);
|
|
1123
|
+
setSetupDialog(null);
|
|
1124
|
+
setReferenceDialog(null);
|
|
1125
|
+
setControlDialog(null);
|
|
580
1126
|
setDialog((current) =>
|
|
581
1127
|
current
|
|
582
1128
|
? { ...current, request: current.request + 1 }
|
|
@@ -598,6 +1144,53 @@ export function FoliToolbar({
|
|
|
598
1144
|
dialog?.restore?.focus();
|
|
599
1145
|
setDialog(null);
|
|
600
1146
|
};
|
|
1147
|
+
// The insert table dialog (from the table menu): the focus goes back to the menu's button.
|
|
1148
|
+
const [tableDialog, setTableDialog] = useState<{
|
|
1149
|
+
restore: HTMLElement | null;
|
|
1150
|
+
selection: EditorState["selection"];
|
|
1151
|
+
request: number;
|
|
1152
|
+
} | null>(null);
|
|
1153
|
+
useEffect(() => {
|
|
1154
|
+
if (disabled) setTableDialog(null);
|
|
1155
|
+
}, [disabled]);
|
|
1156
|
+
// The page setup dialog (from the section menu), for the section of the caret it opened on.
|
|
1157
|
+
const [setupDialog, setSetupDialog] = useState<{
|
|
1158
|
+
restore: HTMLElement | null;
|
|
1159
|
+
selection: EditorState["selection"];
|
|
1160
|
+
} | null>(null);
|
|
1161
|
+
useEffect(() => {
|
|
1162
|
+
if (disabled) setSetupDialog(null);
|
|
1163
|
+
}, [disabled]);
|
|
1164
|
+
// The cross-reference dialog (from the fields menu).
|
|
1165
|
+
const [referenceDialog, setReferenceDialog] = useState<{
|
|
1166
|
+
restore: HTMLElement | null;
|
|
1167
|
+
selection: EditorState["selection"];
|
|
1168
|
+
} | null>(null);
|
|
1169
|
+
useEffect(() => {
|
|
1170
|
+
if (disabled) setReferenceDialog(null);
|
|
1171
|
+
}, [disabled]);
|
|
1172
|
+
// The control properties dialog (from the content controls menu).
|
|
1173
|
+
const [controlDialog, setControlDialog] = useState<{
|
|
1174
|
+
restore: HTMLElement | null;
|
|
1175
|
+
selection: EditorState["selection"];
|
|
1176
|
+
} | null>(null);
|
|
1177
|
+
useEffect(() => {
|
|
1178
|
+
if (disabled) setControlDialog(null);
|
|
1179
|
+
}, [disabled]);
|
|
1180
|
+
// The note options dialog (from the notes menu).
|
|
1181
|
+
const [noteDialog, setNoteDialog] = useState<{ restore: HTMLElement | null } | null>(null);
|
|
1182
|
+
useEffect(() => {
|
|
1183
|
+
if (disabled) setNoteDialog(null);
|
|
1184
|
+
}, [disabled]);
|
|
1185
|
+
// The picture dialog: inserts at (or edits the picture at `pos`) the selection it opened on.
|
|
1186
|
+
const [pictureDialog, setPictureDialog] = useState<{
|
|
1187
|
+
pos: number | null;
|
|
1188
|
+
selection: EditorState["selection"];
|
|
1189
|
+
restore: HTMLElement | null;
|
|
1190
|
+
} | null>(null);
|
|
1191
|
+
useEffect(() => {
|
|
1192
|
+
if (disabled) setPictureDialog(null);
|
|
1193
|
+
}, [disabled]);
|
|
601
1194
|
|
|
602
1195
|
const onKeyDown = (event: KeyboardEvent) => {
|
|
603
1196
|
// Alt+Left (back) and other shortcuts are the browser's.
|
|
@@ -631,7 +1224,11 @@ export function FoliToolbar({
|
|
|
631
1224
|
{group.map((item) => {
|
|
632
1225
|
const i = index++;
|
|
633
1226
|
const state = states[i];
|
|
634
|
-
|
|
1227
|
+
// Inserting a picture needs the app's upload; editing a selected one does not.
|
|
1228
|
+
const off =
|
|
1229
|
+
disabled ||
|
|
1230
|
+
!state?.enabled ||
|
|
1231
|
+
(item.dialog === "picture" && state?.value === "insert" && !upload);
|
|
635
1232
|
if (item.select) {
|
|
636
1233
|
const { select } = item;
|
|
637
1234
|
const value = state?.value ?? "";
|
|
@@ -689,8 +1286,10 @@ export function FoliToolbar({
|
|
|
689
1286
|
aria-label={item.symbol ? item.label : undefined}
|
|
690
1287
|
title={item.label}
|
|
691
1288
|
aria-pressed={item.pressed ? Boolean(state?.pressed) : undefined}
|
|
692
|
-
aria-haspopup={
|
|
693
|
-
|
|
1289
|
+
aria-haspopup={
|
|
1290
|
+
item.menu || item.actions ? "menu" : item.dialog ? "dialog" : undefined
|
|
1291
|
+
}
|
|
1292
|
+
aria-expanded={item.menu || item.actions ? menu === item.key : undefined}
|
|
694
1293
|
aria-disabled={off || undefined}
|
|
695
1294
|
tabIndex={i === active ? 0 : -1}
|
|
696
1295
|
// Keep the focus (and the selection) in the document on click.
|
|
@@ -699,7 +1298,7 @@ export function FoliToolbar({
|
|
|
699
1298
|
onClick={(event) => run(i, event)}
|
|
700
1299
|
onKeyDown={(event) => {
|
|
701
1300
|
// ArrowDown opens a menu (the WAI-ARIA menu button pattern).
|
|
702
|
-
if (!item.menu || off || event.key !== "ArrowDown") return;
|
|
1301
|
+
if (!(item.menu || item.actions) || off || event.key !== "ArrowDown") return;
|
|
703
1302
|
event.preventDefault();
|
|
704
1303
|
setMenu(item.key);
|
|
705
1304
|
}}
|
|
@@ -714,6 +1313,82 @@ export function FoliToolbar({
|
|
|
714
1313
|
)}
|
|
715
1314
|
</button>
|
|
716
1315
|
);
|
|
1316
|
+
if (item.actions) {
|
|
1317
|
+
const actions =
|
|
1318
|
+
typeof item.actions === "function" ? item.actions(body) : item.actions;
|
|
1319
|
+
return (
|
|
1320
|
+
<span key={item.key} className="foli-toolbar-menu">
|
|
1321
|
+
{button}
|
|
1322
|
+
{menu === item.key && !disabled && (
|
|
1323
|
+
<ActionMenu
|
|
1324
|
+
label={item.label}
|
|
1325
|
+
actions={actions}
|
|
1326
|
+
state={editor.state}
|
|
1327
|
+
onPick={(action, pointer) => {
|
|
1328
|
+
setMenu(null);
|
|
1329
|
+
if (action.dialog === "noteOptions") {
|
|
1330
|
+
setDialog(null);
|
|
1331
|
+
setTableDialog(null);
|
|
1332
|
+
setSetupDialog(null);
|
|
1333
|
+
setReferenceDialog(null);
|
|
1334
|
+
setControlDialog(null);
|
|
1335
|
+
setNoteDialog({ restore: buttons.current[i] ?? null });
|
|
1336
|
+
return;
|
|
1337
|
+
}
|
|
1338
|
+
if (action.dialog === "controlProperties") {
|
|
1339
|
+
setDialog(null);
|
|
1340
|
+
setTableDialog(null);
|
|
1341
|
+
setSetupDialog(null);
|
|
1342
|
+
setReferenceDialog(null);
|
|
1343
|
+
setControlDialog({
|
|
1344
|
+
restore: buttons.current[i] ?? null,
|
|
1345
|
+
selection: editor.state.selection,
|
|
1346
|
+
});
|
|
1347
|
+
return;
|
|
1348
|
+
}
|
|
1349
|
+
if (action.dialog === "crossReference") {
|
|
1350
|
+
setDialog(null);
|
|
1351
|
+
setTableDialog(null);
|
|
1352
|
+
setSetupDialog(null);
|
|
1353
|
+
setReferenceDialog({
|
|
1354
|
+
restore: buttons.current[i] ?? null,
|
|
1355
|
+
selection: editor.state.selection,
|
|
1356
|
+
});
|
|
1357
|
+
return;
|
|
1358
|
+
}
|
|
1359
|
+
if (action.dialog === "pageSetup") {
|
|
1360
|
+
setDialog(null);
|
|
1361
|
+
setTableDialog(null);
|
|
1362
|
+
setSetupDialog({
|
|
1363
|
+
restore: buttons.current[i] ?? null,
|
|
1364
|
+
selection: editor.state.selection,
|
|
1365
|
+
});
|
|
1366
|
+
return;
|
|
1367
|
+
}
|
|
1368
|
+
if (action.dialog) {
|
|
1369
|
+
setDialog(null);
|
|
1370
|
+
setSetupDialog(null);
|
|
1371
|
+
setTableDialog((current) => ({
|
|
1372
|
+
restore: buttons.current[i] ?? null,
|
|
1373
|
+
selection: editor.state.selection,
|
|
1374
|
+
request: (current?.request ?? -1) + 1,
|
|
1375
|
+
}));
|
|
1376
|
+
return;
|
|
1377
|
+
}
|
|
1378
|
+
editor.run(action.command);
|
|
1379
|
+
if (action.movesFocus) return;
|
|
1380
|
+
if (pointer) onClickCommand?.();
|
|
1381
|
+
else buttons.current[i]?.focus();
|
|
1382
|
+
}}
|
|
1383
|
+
onClose={(refocus) => {
|
|
1384
|
+
setMenu(null);
|
|
1385
|
+
if (refocus) buttons.current[i]?.focus();
|
|
1386
|
+
}}
|
|
1387
|
+
/>
|
|
1388
|
+
)}
|
|
1389
|
+
</span>
|
|
1390
|
+
);
|
|
1391
|
+
}
|
|
717
1392
|
const options = item.menu?.options;
|
|
718
1393
|
if (!options || !item.menu) return button;
|
|
719
1394
|
const { pick } = item.menu;
|
|
@@ -743,6 +1418,76 @@ export function FoliToolbar({
|
|
|
743
1418
|
</Fragment>
|
|
744
1419
|
))}
|
|
745
1420
|
</div>
|
|
1421
|
+
{pictureDialog && !disabled && (
|
|
1422
|
+
<PictureDialog
|
|
1423
|
+
editor={editor}
|
|
1424
|
+
pos={pictureDialog.pos}
|
|
1425
|
+
upload={upload}
|
|
1426
|
+
onClose={(done) => {
|
|
1427
|
+
const { restore } = pictureDialog;
|
|
1428
|
+
setPictureDialog(null);
|
|
1429
|
+
if (done && onClickCommand) onClickCommand();
|
|
1430
|
+
else restore?.focus();
|
|
1431
|
+
}}
|
|
1432
|
+
/>
|
|
1433
|
+
)}
|
|
1434
|
+
{setupDialog && !disabled && (
|
|
1435
|
+
<PageSetupDialog
|
|
1436
|
+
editor={editor}
|
|
1437
|
+
onClose={(applied) => {
|
|
1438
|
+
const { restore } = setupDialog;
|
|
1439
|
+
setSetupDialog(null);
|
|
1440
|
+
if (applied && onClickCommand) onClickCommand();
|
|
1441
|
+
else restore?.focus();
|
|
1442
|
+
}}
|
|
1443
|
+
/>
|
|
1444
|
+
)}
|
|
1445
|
+
{referenceDialog && !disabled && (
|
|
1446
|
+
<CrossReferenceDialog
|
|
1447
|
+
editor={editor}
|
|
1448
|
+
layout={() => body.storyView?.fieldLayout ?? null}
|
|
1449
|
+
onClose={(inserted) => {
|
|
1450
|
+
const { restore } = referenceDialog;
|
|
1451
|
+
setReferenceDialog(null);
|
|
1452
|
+
if (inserted && onClickCommand) onClickCommand();
|
|
1453
|
+
else restore?.focus();
|
|
1454
|
+
}}
|
|
1455
|
+
/>
|
|
1456
|
+
)}
|
|
1457
|
+
{noteDialog && !disabled && (
|
|
1458
|
+
<NoteOptionsDialog
|
|
1459
|
+
editor={body}
|
|
1460
|
+
onClose={(applied) => {
|
|
1461
|
+
const { restore } = noteDialog;
|
|
1462
|
+
setNoteDialog(null);
|
|
1463
|
+
if (applied && onClickCommand) onClickCommand();
|
|
1464
|
+
else restore?.focus();
|
|
1465
|
+
}}
|
|
1466
|
+
/>
|
|
1467
|
+
)}
|
|
1468
|
+
{controlDialog && !disabled && (
|
|
1469
|
+
<ControlDialog
|
|
1470
|
+
editor={editor}
|
|
1471
|
+
onClose={(applied) => {
|
|
1472
|
+
const { restore } = controlDialog;
|
|
1473
|
+
setControlDialog(null);
|
|
1474
|
+
if (applied && onClickCommand) onClickCommand();
|
|
1475
|
+
else restore?.focus();
|
|
1476
|
+
}}
|
|
1477
|
+
/>
|
|
1478
|
+
)}
|
|
1479
|
+
{tableDialog && !disabled && (
|
|
1480
|
+
<InsertTableDialog
|
|
1481
|
+
editor={editor}
|
|
1482
|
+
request={tableDialog.request}
|
|
1483
|
+
onClose={(inserted) => {
|
|
1484
|
+
const { restore } = tableDialog;
|
|
1485
|
+
setTableDialog(null);
|
|
1486
|
+
if (inserted && onClickCommand) onClickCommand();
|
|
1487
|
+
else restore?.focus();
|
|
1488
|
+
}}
|
|
1489
|
+
/>
|
|
1490
|
+
)}
|
|
746
1491
|
{dialog && !disabled && (
|
|
747
1492
|
<LinkDialog
|
|
748
1493
|
editor={editor}
|
|
@@ -933,3 +1678,1148 @@ function LinkDialog({
|
|
|
933
1678
|
</div>
|
|
934
1679
|
);
|
|
935
1680
|
}
|
|
1681
|
+
|
|
1682
|
+
/**
|
|
1683
|
+
* An action menu (WAI-ARIA menu: menuitems, a menuitemcheckbox for a toggle): the first item
|
|
1684
|
+
* focused on open; arrow keys, Home and End move; Enter or Space (or a click) runs an item;
|
|
1685
|
+
* Escape closes it back to its button, Tab closes it and moves on; leaving it closes it. An item
|
|
1686
|
+
* that cannot apply here is `aria-disabled` (still focusable, as in the toolbar).
|
|
1687
|
+
*/
|
|
1688
|
+
function ActionMenu({
|
|
1689
|
+
label,
|
|
1690
|
+
actions,
|
|
1691
|
+
state,
|
|
1692
|
+
onPick,
|
|
1693
|
+
onClose,
|
|
1694
|
+
}: {
|
|
1695
|
+
label: string;
|
|
1696
|
+
actions: readonly Action[];
|
|
1697
|
+
state: EditorState;
|
|
1698
|
+
onPick: (action: Action, pointer: boolean) => void;
|
|
1699
|
+
onClose: (refocus: boolean) => void;
|
|
1700
|
+
}) {
|
|
1701
|
+
const items = useRef<(HTMLButtonElement | null)[]>([]);
|
|
1702
|
+
// Dry runs, once per opening (not after every transaction).
|
|
1703
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: the actions are a constant list.
|
|
1704
|
+
const enabled = useMemo(() => actions.map((action) => action.command(state)), [state]);
|
|
1705
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: focus once, on open.
|
|
1706
|
+
useEffect(() => {
|
|
1707
|
+
items.current[0]?.focus();
|
|
1708
|
+
}, []);
|
|
1709
|
+
|
|
1710
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
1711
|
+
const count = actions.length;
|
|
1712
|
+
const at = items.current.indexOf(event.target as HTMLButtonElement);
|
|
1713
|
+
const moves: Record<string, number> = {
|
|
1714
|
+
ArrowDown: (at + 1) % count,
|
|
1715
|
+
ArrowUp: (at - 1 + count) % count,
|
|
1716
|
+
Home: 0,
|
|
1717
|
+
End: count - 1,
|
|
1718
|
+
};
|
|
1719
|
+
const next = moves[event.key];
|
|
1720
|
+
event.stopPropagation();
|
|
1721
|
+
if (next !== undefined) {
|
|
1722
|
+
event.preventDefault();
|
|
1723
|
+
items.current[next]?.focus();
|
|
1724
|
+
} else if (event.key === "Escape") {
|
|
1725
|
+
event.preventDefault();
|
|
1726
|
+
onClose(true);
|
|
1727
|
+
} else if (event.key === "Tab") onClose(true);
|
|
1728
|
+
};
|
|
1729
|
+
|
|
1730
|
+
return (
|
|
1731
|
+
<div
|
|
1732
|
+
role="menu"
|
|
1733
|
+
aria-label={label}
|
|
1734
|
+
className="foli-menu"
|
|
1735
|
+
onKeyDown={onKeyDown}
|
|
1736
|
+
onBlur={(event) => {
|
|
1737
|
+
if (!event.currentTarget.contains(event.relatedTarget as Node | null)) onClose(false);
|
|
1738
|
+
}}
|
|
1739
|
+
>
|
|
1740
|
+
{actions.map((action, i) => {
|
|
1741
|
+
const props = {
|
|
1742
|
+
ref: (element: HTMLButtonElement | null) => {
|
|
1743
|
+
items.current[i] = element;
|
|
1744
|
+
},
|
|
1745
|
+
type: "button" as const,
|
|
1746
|
+
"aria-disabled": enabled[i] ? undefined : true,
|
|
1747
|
+
tabIndex: -1,
|
|
1748
|
+
className: "foli-menu-item",
|
|
1749
|
+
onMouseDown: (event: MouseEvent) => event.preventDefault(),
|
|
1750
|
+
onClick: (event: MouseEvent) => {
|
|
1751
|
+
if (enabled[i]) onPick(action, event.detail > 0);
|
|
1752
|
+
},
|
|
1753
|
+
};
|
|
1754
|
+
return action.checked ? (
|
|
1755
|
+
<button
|
|
1756
|
+
key={action.key}
|
|
1757
|
+
{...props}
|
|
1758
|
+
role="menuitemcheckbox"
|
|
1759
|
+
aria-checked={action.checked(state)}
|
|
1760
|
+
>
|
|
1761
|
+
{action.label}
|
|
1762
|
+
</button>
|
|
1763
|
+
) : (
|
|
1764
|
+
<button key={action.key} {...props} role="menuitem">
|
|
1765
|
+
{action.label}
|
|
1766
|
+
</button>
|
|
1767
|
+
);
|
|
1768
|
+
})}
|
|
1769
|
+
</div>
|
|
1770
|
+
);
|
|
1771
|
+
}
|
|
1772
|
+
|
|
1773
|
+
/** Word's limits for a new table. */
|
|
1774
|
+
const MAX_TABLE_ROWS = 1000;
|
|
1775
|
+
const MAX_TABLE_COLUMNS = 63;
|
|
1776
|
+
|
|
1777
|
+
/**
|
|
1778
|
+
* The insert table dialog: rows and columns (whole numbers within Word's limits; an invalid one
|
|
1779
|
+
* is marked and announced), Insert and Cancel; Escape cancels.
|
|
1780
|
+
*/
|
|
1781
|
+
function InsertTableDialog({
|
|
1782
|
+
editor,
|
|
1783
|
+
request,
|
|
1784
|
+
onClose,
|
|
1785
|
+
}: {
|
|
1786
|
+
editor: Editor;
|
|
1787
|
+
/** Counts the open requests: each one focuses the first field. */
|
|
1788
|
+
request: number;
|
|
1789
|
+
/** `inserted`: a table was inserted (the focus may go to the document). */
|
|
1790
|
+
onClose: (inserted: boolean) => void;
|
|
1791
|
+
}) {
|
|
1792
|
+
const [rows, setRows] = useState("2");
|
|
1793
|
+
const [columns, setColumns] = useState("3");
|
|
1794
|
+
// What is wrong: a field out of range, or the place (the caret is not in a paragraph).
|
|
1795
|
+
const [error, setError] = useState<"rows" | "columns" | "place" | null>(null);
|
|
1796
|
+
const rowsField = useRef<HTMLInputElement>(null);
|
|
1797
|
+
const columnsField = useRef<HTMLInputElement>(null);
|
|
1798
|
+
const id = useId();
|
|
1799
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: each request focuses the field again.
|
|
1800
|
+
useEffect(() => {
|
|
1801
|
+
rowsField.current?.focus();
|
|
1802
|
+
rowsField.current?.select();
|
|
1803
|
+
}, [request]);
|
|
1804
|
+
|
|
1805
|
+
const parse = (value: string, max: number) => {
|
|
1806
|
+
const n = Number(value);
|
|
1807
|
+
return Number.isInteger(n) && n >= 1 && n <= max ? n : null;
|
|
1808
|
+
};
|
|
1809
|
+
const insert = (event: FormEvent) => {
|
|
1810
|
+
event.preventDefault();
|
|
1811
|
+
const r = parse(rows, MAX_TABLE_ROWS);
|
|
1812
|
+
const c = parse(columns, MAX_TABLE_COLUMNS);
|
|
1813
|
+
const wrong = r === null ? "rows" : c === null ? "columns" : null;
|
|
1814
|
+
if (!wrong && editor.run(insertTable(r as number, c as number))) {
|
|
1815
|
+
onClose(true);
|
|
1816
|
+
return;
|
|
1817
|
+
}
|
|
1818
|
+
setError(wrong ?? "place");
|
|
1819
|
+
(wrong === "columns" ? columnsField : rowsField).current?.focus();
|
|
1820
|
+
};
|
|
1821
|
+
const messages = {
|
|
1822
|
+
rows: `Enter a number of rows from 1 to ${MAX_TABLE_ROWS}.`,
|
|
1823
|
+
columns: `Enter a number of columns from 1 to ${MAX_TABLE_COLUMNS}.`,
|
|
1824
|
+
place: "A table cannot be inserted here: put the caret in a paragraph.",
|
|
1825
|
+
};
|
|
1826
|
+
const field = (
|
|
1827
|
+
name: "rows" | "columns",
|
|
1828
|
+
label: string,
|
|
1829
|
+
value: string,
|
|
1830
|
+
set: (value: string) => void,
|
|
1831
|
+
max: number,
|
|
1832
|
+
ref: typeof rowsField,
|
|
1833
|
+
) => (
|
|
1834
|
+
<>
|
|
1835
|
+
<label htmlFor={`${id}-${name}`}>{label}</label>
|
|
1836
|
+
<input
|
|
1837
|
+
ref={ref}
|
|
1838
|
+
id={`${id}-${name}`}
|
|
1839
|
+
type="number"
|
|
1840
|
+
min={1}
|
|
1841
|
+
max={max}
|
|
1842
|
+
step={1}
|
|
1843
|
+
value={value}
|
|
1844
|
+
aria-invalid={error === name || undefined}
|
|
1845
|
+
aria-describedby={error === name ? `${id}-error` : undefined}
|
|
1846
|
+
onChange={(event) => {
|
|
1847
|
+
set(event.target.value);
|
|
1848
|
+
setError(null);
|
|
1849
|
+
}}
|
|
1850
|
+
/>
|
|
1851
|
+
</>
|
|
1852
|
+
);
|
|
1853
|
+
|
|
1854
|
+
return (
|
|
1855
|
+
<div
|
|
1856
|
+
role="dialog"
|
|
1857
|
+
aria-label="Insert table"
|
|
1858
|
+
className="foli-link-dialog foli-table-dialog"
|
|
1859
|
+
onKeyDown={(event) => {
|
|
1860
|
+
if (event.key !== "Escape") return;
|
|
1861
|
+
event.preventDefault();
|
|
1862
|
+
onClose(false);
|
|
1863
|
+
}}
|
|
1864
|
+
>
|
|
1865
|
+
<form onSubmit={insert} noValidate>
|
|
1866
|
+
{field("rows", "Rows", rows, setRows, MAX_TABLE_ROWS, rowsField)}
|
|
1867
|
+
{field("columns", "Columns", columns, setColumns, MAX_TABLE_COLUMNS, columnsField)}
|
|
1868
|
+
<button type="submit">Insert</button>
|
|
1869
|
+
<button type="button" onClick={() => onClose(false)}>
|
|
1870
|
+
Cancel
|
|
1871
|
+
</button>
|
|
1872
|
+
{error && (
|
|
1873
|
+
<p id={`${id}-error`} className="foli-link-error" role="alert">
|
|
1874
|
+
{messages[error]}
|
|
1875
|
+
</p>
|
|
1876
|
+
)}
|
|
1877
|
+
</form>
|
|
1878
|
+
</div>
|
|
1879
|
+
);
|
|
1880
|
+
}
|
|
1881
|
+
|
|
1882
|
+
/**
|
|
1883
|
+
* The cross-reference dialog: what to refer to (the document's bookmarks and headings), what to
|
|
1884
|
+
* show of it, whether it links there; Insert and Cancel; Escape cancels.
|
|
1885
|
+
*/
|
|
1886
|
+
function CrossReferenceDialog({
|
|
1887
|
+
editor,
|
|
1888
|
+
layout,
|
|
1889
|
+
onClose,
|
|
1890
|
+
}: {
|
|
1891
|
+
editor: Editor;
|
|
1892
|
+
layout: () => FieldLayout | null;
|
|
1893
|
+
onClose: (inserted: boolean) => void;
|
|
1894
|
+
}) {
|
|
1895
|
+
const [{ bookmarks, headings }] = useState(() => crossReferenceTargets(editor.state));
|
|
1896
|
+
const options = [
|
|
1897
|
+
...headings.map((h) => ({
|
|
1898
|
+
value: `h${h.pos}`,
|
|
1899
|
+
label: `${"\u00a0".repeat((h.level - 1) * 2)}${h.number ? `${h.number} ` : ""}${h.text}`,
|
|
1900
|
+
})),
|
|
1901
|
+
...bookmarks.map((b) => ({ value: `b${b.name}`, label: `Bookmark: ${b.name}` })),
|
|
1902
|
+
];
|
|
1903
|
+
const [target, setTarget] = useState(options[0]?.value ?? "");
|
|
1904
|
+
const [show, setShow] = useState<"text" | "page" | "number">("text");
|
|
1905
|
+
const [link, setLink] = useState(true);
|
|
1906
|
+
const [failed, setFailed] = useState(false);
|
|
1907
|
+
const first = useRef<HTMLSelectElement>(null);
|
|
1908
|
+
const id = useId();
|
|
1909
|
+
useEffect(() => {
|
|
1910
|
+
first.current?.focus();
|
|
1911
|
+
}, []);
|
|
1912
|
+
const insert = (event: FormEvent) => {
|
|
1913
|
+
event.preventDefault();
|
|
1914
|
+
const to = target[0] === "h" ? Number(target.slice(1)) : target.slice(1);
|
|
1915
|
+
if (editor.run(insertCrossReference({ to, show, link, layout: layout() }))) onClose(true);
|
|
1916
|
+
else setFailed(true);
|
|
1917
|
+
};
|
|
1918
|
+
return (
|
|
1919
|
+
<div
|
|
1920
|
+
role="dialog"
|
|
1921
|
+
aria-modal="true"
|
|
1922
|
+
aria-label="Insert cross-reference"
|
|
1923
|
+
className="foli-link-dialog foli-table-dialog"
|
|
1924
|
+
onKeyDown={(event) => {
|
|
1925
|
+
if (event.key === "Escape") {
|
|
1926
|
+
event.preventDefault();
|
|
1927
|
+
onClose(false);
|
|
1928
|
+
}
|
|
1929
|
+
if (event.key !== "Tab") return;
|
|
1930
|
+
// Tab goes round inside the dialog.
|
|
1931
|
+
const stops = [
|
|
1932
|
+
...event.currentTarget.querySelectorAll<HTMLElement>("select, input, button"),
|
|
1933
|
+
];
|
|
1934
|
+
const at = stops.indexOf(event.target as HTMLElement);
|
|
1935
|
+
const next = (at + (event.shiftKey ? -1 : 1) + stops.length) % stops.length;
|
|
1936
|
+
event.preventDefault();
|
|
1937
|
+
stops[next]?.focus();
|
|
1938
|
+
}}
|
|
1939
|
+
>
|
|
1940
|
+
<form onSubmit={insert} noValidate>
|
|
1941
|
+
<label htmlFor={`${id}-target`}>Refer to</label>
|
|
1942
|
+
<select
|
|
1943
|
+
ref={first}
|
|
1944
|
+
id={`${id}-target`}
|
|
1945
|
+
value={target}
|
|
1946
|
+
onChange={(event) => {
|
|
1947
|
+
setTarget(event.target.value);
|
|
1948
|
+
setFailed(false);
|
|
1949
|
+
}}
|
|
1950
|
+
>
|
|
1951
|
+
{options.map((option) => (
|
|
1952
|
+
<option key={option.value} value={option.value}>
|
|
1953
|
+
{option.label}
|
|
1954
|
+
</option>
|
|
1955
|
+
))}
|
|
1956
|
+
</select>
|
|
1957
|
+
<label htmlFor={`${id}-show`}>Show</label>
|
|
1958
|
+
<select
|
|
1959
|
+
id={`${id}-show`}
|
|
1960
|
+
value={show}
|
|
1961
|
+
onChange={(event) => setShow(event.target.value as typeof show)}
|
|
1962
|
+
>
|
|
1963
|
+
<option value="text">Its text</option>
|
|
1964
|
+
<option value="page">Its page number</option>
|
|
1965
|
+
<option value="number">Its paragraph number</option>
|
|
1966
|
+
</select>
|
|
1967
|
+
<label>
|
|
1968
|
+
<input
|
|
1969
|
+
type="checkbox"
|
|
1970
|
+
checked={link}
|
|
1971
|
+
onChange={(event) => setLink(event.target.checked)}
|
|
1972
|
+
/>
|
|
1973
|
+
Link to it
|
|
1974
|
+
</label>
|
|
1975
|
+
<button type="submit">Insert</button>
|
|
1976
|
+
<button type="button" onClick={() => onClose(false)}>
|
|
1977
|
+
Cancel
|
|
1978
|
+
</button>
|
|
1979
|
+
{failed && (
|
|
1980
|
+
<p className="foli-link-error" role="alert">
|
|
1981
|
+
A cross-reference cannot be inserted here.
|
|
1982
|
+
</p>
|
|
1983
|
+
)}
|
|
1984
|
+
</form>
|
|
1985
|
+
</div>
|
|
1986
|
+
);
|
|
1987
|
+
}
|
|
1988
|
+
|
|
1989
|
+
/** Word's number formats for notes. */
|
|
1990
|
+
const NOTE_FORMATS = [
|
|
1991
|
+
["decimal", "1, 2, 3"],
|
|
1992
|
+
["lowerLetter", "a, b, c"],
|
|
1993
|
+
["upperLetter", "A, B, C"],
|
|
1994
|
+
["lowerRoman", "i, ii, iii"],
|
|
1995
|
+
["upperRoman", "I, II, III"],
|
|
1996
|
+
["chicago", "*, †, ‡"],
|
|
1997
|
+
] as const;
|
|
1998
|
+
|
|
1999
|
+
/**
|
|
2000
|
+
* How notes are placed and numbered, for the whole document as Word's dialog applies it: where
|
|
2001
|
+
* they go, the number format, where numbers start and when they restart; Escape cancels.
|
|
2002
|
+
*/
|
|
2003
|
+
function NoteOptionsDialog({
|
|
2004
|
+
editor,
|
|
2005
|
+
onClose,
|
|
2006
|
+
}: {
|
|
2007
|
+
editor: Editor;
|
|
2008
|
+
onClose: (applied: boolean) => void;
|
|
2009
|
+
}) {
|
|
2010
|
+
const [kind, setKind] = useState<NoteKind>("footnote");
|
|
2011
|
+
const valuesOf = (of: NoteKind) => {
|
|
2012
|
+
const envelope = wordContext(editor.state)?.envelope;
|
|
2013
|
+
const section = sectionAt(editor.state)?.setup;
|
|
2014
|
+
return noteProperties(envelope ?? {}, of, section);
|
|
2015
|
+
};
|
|
2016
|
+
const [values, setValues] = useState(() => valuesOf("footnote"));
|
|
2017
|
+
const [start, setStart] = useState(String(values.start));
|
|
2018
|
+
const first = useRef<HTMLSelectElement>(null);
|
|
2019
|
+
const id = useId();
|
|
2020
|
+
useEffect(() => {
|
|
2021
|
+
first.current?.focus();
|
|
2022
|
+
}, []);
|
|
2023
|
+
const set = (change: Partial<typeof values>) => setValues((v) => ({ ...v, ...change }));
|
|
2024
|
+
const restarts = values.restart !== "continuous";
|
|
2025
|
+
const number = Number(start);
|
|
2026
|
+
const invalid = !restarts && (!Number.isInteger(number) || number < 1 || number > 16383);
|
|
2027
|
+
const apply = (event: FormEvent) => {
|
|
2028
|
+
event.preventDefault();
|
|
2029
|
+
if (invalid) return;
|
|
2030
|
+
const properties: NotePropertiesJSON = {
|
|
2031
|
+
position: values.position,
|
|
2032
|
+
format: values.format,
|
|
2033
|
+
restart: values.restart,
|
|
2034
|
+
start: restarts ? 1 : number,
|
|
2035
|
+
};
|
|
2036
|
+
onClose(editor.run(setNoteProperties(kind, properties)));
|
|
2037
|
+
};
|
|
2038
|
+
return (
|
|
2039
|
+
<div
|
|
2040
|
+
role="dialog"
|
|
2041
|
+
aria-label="Note options"
|
|
2042
|
+
className="foli-link-dialog foli-table-dialog"
|
|
2043
|
+
onKeyDown={(event) => {
|
|
2044
|
+
if (event.key !== "Escape") return;
|
|
2045
|
+
event.preventDefault();
|
|
2046
|
+
onClose(false);
|
|
2047
|
+
}}
|
|
2048
|
+
>
|
|
2049
|
+
<form onSubmit={apply} noValidate>
|
|
2050
|
+
<label htmlFor={`${id}-kind`}>Notes</label>
|
|
2051
|
+
<select
|
|
2052
|
+
ref={first}
|
|
2053
|
+
id={`${id}-kind`}
|
|
2054
|
+
value={kind}
|
|
2055
|
+
onChange={(event) => {
|
|
2056
|
+
const next = event.target.value as NoteKind;
|
|
2057
|
+
const shown = valuesOf(next);
|
|
2058
|
+
setKind(next);
|
|
2059
|
+
setValues(shown);
|
|
2060
|
+
setStart(String(shown.start));
|
|
2061
|
+
}}
|
|
2062
|
+
>
|
|
2063
|
+
<option value="footnote">Footnotes</option>
|
|
2064
|
+
<option value="endnote">Endnotes</option>
|
|
2065
|
+
</select>
|
|
2066
|
+
<label htmlFor={`${id}-position`}>Position</label>
|
|
2067
|
+
<select
|
|
2068
|
+
id={`${id}-position`}
|
|
2069
|
+
value={values.position}
|
|
2070
|
+
onChange={(event) =>
|
|
2071
|
+
set({ position: event.target.value as NonNullable<NotePropertiesJSON["position"]> })
|
|
2072
|
+
}
|
|
2073
|
+
>
|
|
2074
|
+
{kind === "footnote" ? (
|
|
2075
|
+
<>
|
|
2076
|
+
<option value="pageBottom">Bottom of page</option>
|
|
2077
|
+
<option value="beneathText">Below text</option>
|
|
2078
|
+
</>
|
|
2079
|
+
) : (
|
|
2080
|
+
<>
|
|
2081
|
+
<option value="docEnd">End of document</option>
|
|
2082
|
+
<option value="sectEnd">End of section</option>
|
|
2083
|
+
</>
|
|
2084
|
+
)}
|
|
2085
|
+
</select>
|
|
2086
|
+
<label htmlFor={`${id}-format`}>Number format</label>
|
|
2087
|
+
<select
|
|
2088
|
+
id={`${id}-format`}
|
|
2089
|
+
value={values.format}
|
|
2090
|
+
onChange={(event) => set({ format: event.target.value })}
|
|
2091
|
+
>
|
|
2092
|
+
{NOTE_FORMATS.map(([value, label]) => (
|
|
2093
|
+
<option key={value} value={value}>
|
|
2094
|
+
{label}
|
|
2095
|
+
</option>
|
|
2096
|
+
))}
|
|
2097
|
+
</select>
|
|
2098
|
+
<label htmlFor={`${id}-start`}>Start at</label>
|
|
2099
|
+
<input
|
|
2100
|
+
id={`${id}-start`}
|
|
2101
|
+
type="number"
|
|
2102
|
+
min={1}
|
|
2103
|
+
value={restarts ? "1" : start}
|
|
2104
|
+
disabled={restarts}
|
|
2105
|
+
aria-invalid={invalid || undefined}
|
|
2106
|
+
onChange={(event) => setStart(event.target.value)}
|
|
2107
|
+
/>
|
|
2108
|
+
<label htmlFor={`${id}-restart`}>Numbering</label>
|
|
2109
|
+
<select
|
|
2110
|
+
id={`${id}-restart`}
|
|
2111
|
+
value={values.restart}
|
|
2112
|
+
onChange={(event) =>
|
|
2113
|
+
set({ restart: event.target.value as NonNullable<NotePropertiesJSON["restart"]> })
|
|
2114
|
+
}
|
|
2115
|
+
>
|
|
2116
|
+
<option value="continuous">Continuous</option>
|
|
2117
|
+
<option value="eachSect">Restart each section</option>
|
|
2118
|
+
{kind === "footnote" && <option value="eachPage">Restart each page</option>}
|
|
2119
|
+
</select>
|
|
2120
|
+
<button type="submit">Apply</button>
|
|
2121
|
+
<button type="button" onClick={() => onClose(false)}>
|
|
2122
|
+
Cancel
|
|
2123
|
+
</button>
|
|
2124
|
+
{invalid && (
|
|
2125
|
+
<p className="foli-link-error" role="alert">
|
|
2126
|
+
The start must be a whole number from 1.
|
|
2127
|
+
</p>
|
|
2128
|
+
)}
|
|
2129
|
+
</form>
|
|
2130
|
+
</div>
|
|
2131
|
+
);
|
|
2132
|
+
}
|
|
2133
|
+
|
|
2134
|
+
/** A list's items as the dialog shows them: one a line, `shown | value` where they differ. */
|
|
2135
|
+
const itemsText = (items: ControlJSON["items"]) =>
|
|
2136
|
+
(items ?? [])
|
|
2137
|
+
.map((item) =>
|
|
2138
|
+
item.display === undefined || item.display === item.value
|
|
2139
|
+
? item.value
|
|
2140
|
+
: `${item.display} | ${item.value}`,
|
|
2141
|
+
)
|
|
2142
|
+
.join("\n");
|
|
2143
|
+
|
|
2144
|
+
function parseItems(text: string): NonNullable<ControlJSON["items"]> {
|
|
2145
|
+
const seen = new Set<string>();
|
|
2146
|
+
return text.split("\n").flatMap((line) => {
|
|
2147
|
+
const [shown = "", value = shown] = line.split("|").map((part) => part.trim());
|
|
2148
|
+
// Word refuses two items of one value.
|
|
2149
|
+
if (!shown || seen.has(value)) return [];
|
|
2150
|
+
seen.add(value);
|
|
2151
|
+
return [{ value, display: shown }];
|
|
2152
|
+
});
|
|
2153
|
+
}
|
|
2154
|
+
|
|
2155
|
+
/**
|
|
2156
|
+
* The properties of the control at the caret: title, tag, locks and what its kind has (list items,
|
|
2157
|
+
* date format, line breaks); OK and Cancel; Escape cancels.
|
|
2158
|
+
*/
|
|
2159
|
+
function ControlDialog({
|
|
2160
|
+
editor,
|
|
2161
|
+
onClose,
|
|
2162
|
+
}: {
|
|
2163
|
+
editor: Editor;
|
|
2164
|
+
onClose: (applied: boolean) => void;
|
|
2165
|
+
}) {
|
|
2166
|
+
const [selected] = useState(() => selectedControl(editor.state));
|
|
2167
|
+
const control = selected?.control;
|
|
2168
|
+
const [title, setTitle] = useState(control?.alias ?? "");
|
|
2169
|
+
const [tag, setTag] = useState(control?.tag ?? "");
|
|
2170
|
+
const [noDelete, setNoDelete] = useState(
|
|
2171
|
+
control?.lock === "sdtLocked" || control?.lock === "sdtContentLocked",
|
|
2172
|
+
);
|
|
2173
|
+
const [noEdit, setNoEdit] = useState(
|
|
2174
|
+
control?.lock === "contentLocked" || control?.lock === "sdtContentLocked",
|
|
2175
|
+
);
|
|
2176
|
+
const [items, setItems] = useState(itemsText(control?.items));
|
|
2177
|
+
const [dateFormat, setDateFormat] = useState(control?.dateFormat ?? "d MMMM yyyy");
|
|
2178
|
+
const [multiLine, setMultiLine] = useState(control?.multiLine === true);
|
|
2179
|
+
const first = useRef<HTMLInputElement>(null);
|
|
2180
|
+
const id = useId();
|
|
2181
|
+
useEffect(() => {
|
|
2182
|
+
first.current?.focus();
|
|
2183
|
+
}, []);
|
|
2184
|
+
if (!selected || !control) return null;
|
|
2185
|
+
const { kind } = control;
|
|
2186
|
+
const hasList = kind === "dropDown" || kind === "comboBox";
|
|
2187
|
+
const apply = (event: FormEvent) => {
|
|
2188
|
+
event.preventDefault();
|
|
2189
|
+
const lock = noDelete
|
|
2190
|
+
? noEdit
|
|
2191
|
+
? "sdtContentLocked"
|
|
2192
|
+
: "sdtLocked"
|
|
2193
|
+
: noEdit
|
|
2194
|
+
? "contentLocked"
|
|
2195
|
+
: "unlocked";
|
|
2196
|
+
onClose(
|
|
2197
|
+
editor.run(
|
|
2198
|
+
setControlProperties(selected.id, {
|
|
2199
|
+
title,
|
|
2200
|
+
tag,
|
|
2201
|
+
lock,
|
|
2202
|
+
...(hasList ? { items: parseItems(items) } : {}),
|
|
2203
|
+
...(kind === "date" ? { dateFormat } : {}),
|
|
2204
|
+
...(kind === "text" ? { multiLine } : {}),
|
|
2205
|
+
}),
|
|
2206
|
+
),
|
|
2207
|
+
);
|
|
2208
|
+
};
|
|
2209
|
+
return (
|
|
2210
|
+
<div
|
|
2211
|
+
role="dialog"
|
|
2212
|
+
aria-label="Content control properties"
|
|
2213
|
+
className="foli-link-dialog foli-table-dialog"
|
|
2214
|
+
onKeyDown={(event) => {
|
|
2215
|
+
if (event.key !== "Escape") return;
|
|
2216
|
+
event.preventDefault();
|
|
2217
|
+
onClose(false);
|
|
2218
|
+
}}
|
|
2219
|
+
>
|
|
2220
|
+
<form onSubmit={apply} noValidate>
|
|
2221
|
+
<label htmlFor={`${id}-title`}>Title</label>
|
|
2222
|
+
<input
|
|
2223
|
+
ref={first}
|
|
2224
|
+
id={`${id}-title`}
|
|
2225
|
+
value={title}
|
|
2226
|
+
onChange={(event) => setTitle(event.target.value)}
|
|
2227
|
+
/>
|
|
2228
|
+
<label htmlFor={`${id}-tag`}>Tag</label>
|
|
2229
|
+
<input id={`${id}-tag`} value={tag} onChange={(event) => setTag(event.target.value)} />
|
|
2230
|
+
<label>
|
|
2231
|
+
<input
|
|
2232
|
+
type="checkbox"
|
|
2233
|
+
checked={noDelete}
|
|
2234
|
+
onChange={(event) => setNoDelete(event.target.checked)}
|
|
2235
|
+
/>
|
|
2236
|
+
The control cannot be deleted
|
|
2237
|
+
</label>
|
|
2238
|
+
<label>
|
|
2239
|
+
<input
|
|
2240
|
+
type="checkbox"
|
|
2241
|
+
checked={noEdit}
|
|
2242
|
+
onChange={(event) => setNoEdit(event.target.checked)}
|
|
2243
|
+
/>
|
|
2244
|
+
The contents cannot be edited
|
|
2245
|
+
</label>
|
|
2246
|
+
{hasList && (
|
|
2247
|
+
<>
|
|
2248
|
+
<label htmlFor={`${id}-items`}>List items, one per line (shown | value)</label>
|
|
2249
|
+
<textarea
|
|
2250
|
+
id={`${id}-items`}
|
|
2251
|
+
rows={4}
|
|
2252
|
+
value={items}
|
|
2253
|
+
onChange={(event) => setItems(event.target.value)}
|
|
2254
|
+
/>
|
|
2255
|
+
</>
|
|
2256
|
+
)}
|
|
2257
|
+
{kind === "date" && (
|
|
2258
|
+
<>
|
|
2259
|
+
<label htmlFor={`${id}-format`}>Date format</label>
|
|
2260
|
+
<input
|
|
2261
|
+
id={`${id}-format`}
|
|
2262
|
+
value={dateFormat}
|
|
2263
|
+
onChange={(event) => setDateFormat(event.target.value)}
|
|
2264
|
+
/>
|
|
2265
|
+
</>
|
|
2266
|
+
)}
|
|
2267
|
+
{kind === "text" && (
|
|
2268
|
+
<label>
|
|
2269
|
+
<input
|
|
2270
|
+
type="checkbox"
|
|
2271
|
+
checked={multiLine}
|
|
2272
|
+
onChange={(event) => setMultiLine(event.target.checked)}
|
|
2273
|
+
/>
|
|
2274
|
+
Allow line breaks
|
|
2275
|
+
</label>
|
|
2276
|
+
)}
|
|
2277
|
+
<button type="submit">OK</button>
|
|
2278
|
+
<button type="button" onClick={() => onClose(false)}>
|
|
2279
|
+
Cancel
|
|
2280
|
+
</button>
|
|
2281
|
+
</form>
|
|
2282
|
+
</div>
|
|
2283
|
+
);
|
|
2284
|
+
}
|
|
2285
|
+
|
|
2286
|
+
const TWIPS_PER_CM = 1440 / 2.54;
|
|
2287
|
+
const MAX_TWIPS = 31_680;
|
|
2288
|
+
/** Word's limit on a section's columns. */
|
|
2289
|
+
const MAX_SECTION_COLUMNS = 45;
|
|
2290
|
+
const toCm = (twips: number) => String(Number((twips / TWIPS_PER_CM).toFixed(2)));
|
|
2291
|
+
/** A length in cm as whole twips (a decimal comma too), or null when it is not one in range. */
|
|
2292
|
+
function toTwips(text: string, min: number): number | null {
|
|
2293
|
+
const value = Number(text.trim().replace(",", "."));
|
|
2294
|
+
const twips = Math.round(value * TWIPS_PER_CM);
|
|
2295
|
+
return text.trim() && Number.isFinite(value) && twips >= min && twips <= MAX_TWIPS ? twips : null;
|
|
2296
|
+
}
|
|
2297
|
+
|
|
2298
|
+
const SECTION_STARTS = [
|
|
2299
|
+
["nextPage", "Next page"],
|
|
2300
|
+
["continuous", "Continuous"],
|
|
2301
|
+
["evenPage", "Even page"],
|
|
2302
|
+
["oddPage", "Odd page"],
|
|
2303
|
+
["nextColumn", "Next column"],
|
|
2304
|
+
] as const;
|
|
2305
|
+
type SetupField =
|
|
2306
|
+
| "width"
|
|
2307
|
+
| "height"
|
|
2308
|
+
| "top"
|
|
2309
|
+
| "bottom"
|
|
2310
|
+
| "left"
|
|
2311
|
+
| "right"
|
|
2312
|
+
| "count"
|
|
2313
|
+
| "space"
|
|
2314
|
+
| "widths";
|
|
2315
|
+
const SETUP_LABELS: Record<SetupField, string> = {
|
|
2316
|
+
width: "Page width (cm)",
|
|
2317
|
+
height: "Page height (cm)",
|
|
2318
|
+
top: "Top margin (cm)",
|
|
2319
|
+
bottom: "Bottom margin (cm)",
|
|
2320
|
+
left: "Left margin (cm)",
|
|
2321
|
+
right: "Right margin (cm)",
|
|
2322
|
+
count: "Columns",
|
|
2323
|
+
space: "Space between columns (cm)",
|
|
2324
|
+
widths: "Set column widths (cm, separated by ;)",
|
|
2325
|
+
};
|
|
2326
|
+
|
|
2327
|
+
/**
|
|
2328
|
+
* The page setup dialog, for the caret's section: how it starts, its page size and orientation,
|
|
2329
|
+
* margins and columns (equal, or set widths; the line between them), in centimetres. Only what
|
|
2330
|
+
* was changed is applied. Apply and Cancel; Escape cancels; a wrong field is marked and announced.
|
|
2331
|
+
*/
|
|
2332
|
+
function PageSetupDialog({
|
|
2333
|
+
editor,
|
|
2334
|
+
onClose,
|
|
2335
|
+
}: {
|
|
2336
|
+
editor: Editor;
|
|
2337
|
+
/** `applied`: the setup changed (the focus may go to the document). */
|
|
2338
|
+
onClose: (applied: boolean) => void;
|
|
2339
|
+
}) {
|
|
2340
|
+
const id = useId();
|
|
2341
|
+
const [initial] = useState(() => {
|
|
2342
|
+
const setup = sectionAt(editor.state)?.setup ?? {};
|
|
2343
|
+
const width = setup.pageSize?.width ?? 12_240;
|
|
2344
|
+
const height = setup.pageSize?.height ?? 15_840;
|
|
2345
|
+
const m = setup.margins ?? {};
|
|
2346
|
+
const set = setup.columns?.equalWidth === false ? (setup.columns.widths ?? []) : [];
|
|
2347
|
+
return {
|
|
2348
|
+
start: setup.type ?? "nextPage",
|
|
2349
|
+
orientation: setup.pageSize?.orientation ?? (width > height ? "landscape" : "portrait"),
|
|
2350
|
+
separator: setup.columns?.separator === true,
|
|
2351
|
+
columns: setup.columns,
|
|
2352
|
+
fields: {
|
|
2353
|
+
width: toCm(width),
|
|
2354
|
+
height: toCm(height),
|
|
2355
|
+
top: toCm(Math.abs(m.top ?? 1440)),
|
|
2356
|
+
bottom: toCm(Math.abs(m.bottom ?? 1440)),
|
|
2357
|
+
left: toCm(m.left ?? 1440),
|
|
2358
|
+
right: toCm(m.right ?? 1440),
|
|
2359
|
+
count: String(set.length || (setup.columns?.count ?? 1)),
|
|
2360
|
+
space: toCm(setup.columns?.space ?? 720),
|
|
2361
|
+
widths: set.map((column) => toCm(column.width)).join("; "),
|
|
2362
|
+
} satisfies Record<SetupField, string>,
|
|
2363
|
+
};
|
|
2364
|
+
});
|
|
2365
|
+
const [start, setStart] = useState<string>(initial.start);
|
|
2366
|
+
const [orientation, setOrientation] = useState<string>(initial.orientation);
|
|
2367
|
+
const [separator, setSeparator] = useState(initial.separator);
|
|
2368
|
+
const [fields, setFields] = useState(initial.fields);
|
|
2369
|
+
const [error, setError] = useState<SetupField | "setup" | "mismatch" | null>(null);
|
|
2370
|
+
const first = useRef<HTMLSelectElement>(null);
|
|
2371
|
+
useEffect(() => first.current?.focus(), []);
|
|
2372
|
+
|
|
2373
|
+
const apply = (event: FormEvent) => {
|
|
2374
|
+
event.preventDefault();
|
|
2375
|
+
const wrong = (field: SetupField) => {
|
|
2376
|
+
setError(field);
|
|
2377
|
+
document.getElementById(`${id}-${field}`)?.focus();
|
|
2378
|
+
};
|
|
2379
|
+
const changed = (field: SetupField) => fields[field] !== initial.fields[field];
|
|
2380
|
+
const setup: SectionSetup = {};
|
|
2381
|
+
if (start !== initial.start)
|
|
2382
|
+
setup.type = start === "nextPage" ? undefined : (start as SectionJSON["type"]);
|
|
2383
|
+
const lengths: Partial<Record<SetupField, number>> = {};
|
|
2384
|
+
for (const field of ["width", "height", "top", "bottom", "left", "right"] as const) {
|
|
2385
|
+
if (!changed(field)) continue;
|
|
2386
|
+
const twips = toTwips(fields[field], field === "width" || field === "height" ? 1 : 0);
|
|
2387
|
+
if (twips === null) return wrong(field);
|
|
2388
|
+
lengths[field] = twips;
|
|
2389
|
+
}
|
|
2390
|
+
if (
|
|
2391
|
+
lengths.width !== undefined ||
|
|
2392
|
+
lengths.height !== undefined ||
|
|
2393
|
+
orientation !== initial.orientation
|
|
2394
|
+
)
|
|
2395
|
+
setup.pageSize = {
|
|
2396
|
+
...(lengths.width !== undefined ? { width: lengths.width } : {}),
|
|
2397
|
+
...(lengths.height !== undefined ? { height: lengths.height } : {}),
|
|
2398
|
+
orientation: orientation as "portrait" | "landscape",
|
|
2399
|
+
};
|
|
2400
|
+
const margins = {
|
|
2401
|
+
top: lengths.top,
|
|
2402
|
+
bottom: lengths.bottom,
|
|
2403
|
+
left: lengths.left,
|
|
2404
|
+
right: lengths.right,
|
|
2405
|
+
};
|
|
2406
|
+
const given = Object.entries(margins).filter(([, value]) => value !== undefined);
|
|
2407
|
+
if (given.length) setup.margins = Object.fromEntries(given);
|
|
2408
|
+
if (
|
|
2409
|
+
changed("count") ||
|
|
2410
|
+
changed("space") ||
|
|
2411
|
+
changed("widths") ||
|
|
2412
|
+
separator !== initial.separator
|
|
2413
|
+
) {
|
|
2414
|
+
// What was not changed stays exactly as it is, each column's own space included.
|
|
2415
|
+
const before = initial.columns;
|
|
2416
|
+
const set = before?.equalWidth === false ? (before.widths ?? []) : [];
|
|
2417
|
+
const space = changed("space") ? toTwips(fields.space, 0) : (before?.space ?? 720);
|
|
2418
|
+
if (space === null) return wrong("space");
|
|
2419
|
+
let widths: (number | null)[] = set.map((column) => column.width);
|
|
2420
|
+
if (changed("widths"))
|
|
2421
|
+
widths = fields.widths.trim()
|
|
2422
|
+
? fields.widths
|
|
2423
|
+
.trim()
|
|
2424
|
+
.split(/[;\s]+/)
|
|
2425
|
+
.map((width) => toTwips(width, 1))
|
|
2426
|
+
: [];
|
|
2427
|
+
if (widths.includes(null)) return wrong("widths");
|
|
2428
|
+
const asked = Number(fields.count);
|
|
2429
|
+
if (widths.length && changed("count") && asked !== widths.length) {
|
|
2430
|
+
setError("mismatch");
|
|
2431
|
+
document.getElementById(`${id}-count`)?.focus();
|
|
2432
|
+
return;
|
|
2433
|
+
}
|
|
2434
|
+
const count = widths.length || asked;
|
|
2435
|
+
if (!Number.isInteger(count) || count < 1 || count > MAX_SECTION_COLUMNS)
|
|
2436
|
+
return wrong("count");
|
|
2437
|
+
const { separator: _line, equalWidth: _equal, widths: _widths, ...rest } = before ?? {};
|
|
2438
|
+
setup.columns = {
|
|
2439
|
+
...rest,
|
|
2440
|
+
count,
|
|
2441
|
+
...(changed("space") || before?.space === undefined ? { space } : {}),
|
|
2442
|
+
...(separator ? { separator } : {}),
|
|
2443
|
+
...(widths.length
|
|
2444
|
+
? {
|
|
2445
|
+
equalWidth: false,
|
|
2446
|
+
widths: widths.map((width, i) => {
|
|
2447
|
+
const own = changed("space") ? undefined : set[i]?.space;
|
|
2448
|
+
const gap = own ?? (i < widths.length - 1 ? space : undefined);
|
|
2449
|
+
return { width: width as number, ...(gap === undefined ? {} : { space: gap }) };
|
|
2450
|
+
}),
|
|
2451
|
+
}
|
|
2452
|
+
: {}),
|
|
2453
|
+
};
|
|
2454
|
+
}
|
|
2455
|
+
if (!Object.keys(setup).length) return onClose(false);
|
|
2456
|
+
if (editor.run(setSectionSetup(setup))) return onClose(true);
|
|
2457
|
+
setError("setup");
|
|
2458
|
+
};
|
|
2459
|
+
const setOrientationSwapping = (next: string) => {
|
|
2460
|
+
if (next !== orientation)
|
|
2461
|
+
setFields((current) => ({ ...current, width: current.height, height: current.width }));
|
|
2462
|
+
setOrientation(next);
|
|
2463
|
+
setError(null);
|
|
2464
|
+
};
|
|
2465
|
+
const marked = (name: SetupField) => error === name || (name === "count" && error === "mismatch");
|
|
2466
|
+
const field = (name: SetupField) => (
|
|
2467
|
+
<div key={name} className="foli-setup-field">
|
|
2468
|
+
<label htmlFor={`${id}-${name}`}>{SETUP_LABELS[name]}</label>
|
|
2469
|
+
<input
|
|
2470
|
+
id={`${id}-${name}`}
|
|
2471
|
+
type="text"
|
|
2472
|
+
inputMode={name === "widths" ? "text" : "decimal"}
|
|
2473
|
+
value={fields[name]}
|
|
2474
|
+
aria-invalid={marked(name) || undefined}
|
|
2475
|
+
aria-describedby={marked(name) ? `${id}-error` : undefined}
|
|
2476
|
+
onChange={(event) => {
|
|
2477
|
+
setFields((current) => ({ ...current, [name]: event.target.value }));
|
|
2478
|
+
setError(null);
|
|
2479
|
+
}}
|
|
2480
|
+
/>
|
|
2481
|
+
</div>
|
|
2482
|
+
);
|
|
2483
|
+
|
|
2484
|
+
return (
|
|
2485
|
+
<div
|
|
2486
|
+
role="dialog"
|
|
2487
|
+
aria-label="Page setup"
|
|
2488
|
+
className="foli-link-dialog foli-setup-dialog"
|
|
2489
|
+
onKeyDown={(event) => {
|
|
2490
|
+
if (event.key !== "Escape") return;
|
|
2491
|
+
event.preventDefault();
|
|
2492
|
+
onClose(false);
|
|
2493
|
+
}}
|
|
2494
|
+
>
|
|
2495
|
+
<form onSubmit={apply} noValidate>
|
|
2496
|
+
<div className="foli-setup-field">
|
|
2497
|
+
<label htmlFor={`${id}-start`}>Section starts</label>
|
|
2498
|
+
<select
|
|
2499
|
+
ref={first}
|
|
2500
|
+
id={`${id}-start`}
|
|
2501
|
+
value={start}
|
|
2502
|
+
onChange={(event) => setStart(event.target.value)}
|
|
2503
|
+
>
|
|
2504
|
+
{SECTION_STARTS.map(([value, label]) => (
|
|
2505
|
+
<option key={value} value={value}>
|
|
2506
|
+
{label}
|
|
2507
|
+
</option>
|
|
2508
|
+
))}
|
|
2509
|
+
</select>
|
|
2510
|
+
</div>
|
|
2511
|
+
<div className="foli-setup-field">
|
|
2512
|
+
<label htmlFor={`${id}-orientation`}>Orientation</label>
|
|
2513
|
+
<select
|
|
2514
|
+
id={`${id}-orientation`}
|
|
2515
|
+
value={orientation}
|
|
2516
|
+
onChange={(event) => setOrientationSwapping(event.target.value)}
|
|
2517
|
+
>
|
|
2518
|
+
<option value="portrait">Portrait</option>
|
|
2519
|
+
<option value="landscape">Landscape</option>
|
|
2520
|
+
</select>
|
|
2521
|
+
</div>
|
|
2522
|
+
{(["width", "height", "top", "bottom", "left", "right", "count", "space"] as const).map(
|
|
2523
|
+
field,
|
|
2524
|
+
)}
|
|
2525
|
+
<div className="foli-setup-field foli-setup-check">
|
|
2526
|
+
<input
|
|
2527
|
+
id={`${id}-separator`}
|
|
2528
|
+
type="checkbox"
|
|
2529
|
+
checked={separator}
|
|
2530
|
+
onChange={(event) => setSeparator(event.target.checked)}
|
|
2531
|
+
/>
|
|
2532
|
+
<label htmlFor={`${id}-separator`}>Line between columns</label>
|
|
2533
|
+
</div>
|
|
2534
|
+
{field("widths")}
|
|
2535
|
+
<button type="submit">Apply</button>
|
|
2536
|
+
<button type="button" onClick={() => onClose(false)}>
|
|
2537
|
+
Cancel
|
|
2538
|
+
</button>
|
|
2539
|
+
{error && (
|
|
2540
|
+
<p id={`${id}-error`} className="foli-link-error" role="alert">
|
|
2541
|
+
{error === "setup"
|
|
2542
|
+
? "This setup leaves no room for text, or its columns do not fit the page."
|
|
2543
|
+
: error === "mismatch"
|
|
2544
|
+
? "The number of columns differs from the set widths: list one width per column, or clear them for equal columns."
|
|
2545
|
+
: `Check this value: ${SETUP_LABELS[error]}.`}
|
|
2546
|
+
</p>
|
|
2547
|
+
)}
|
|
2548
|
+
</form>
|
|
2549
|
+
</div>
|
|
2550
|
+
);
|
|
2551
|
+
}
|
|
2552
|
+
|
|
2553
|
+
/** The largest picture side the schema takes, in px (2^31 - 1 EMU). */
|
|
2554
|
+
const MAX_PICTURE_PX = Math.floor(2_147_483_647 / EMU_PER_PX);
|
|
2555
|
+
|
|
2556
|
+
/**
|
|
2557
|
+
* The picture dialog. Inserting (`pos` null): an image file, its alt text (required unless the
|
|
2558
|
+
* picture is marked decorative), Insert and Cancel; the file goes through `upload`, the picture
|
|
2559
|
+
* in at the file's natural size no wider than the page's text. Editing the picture at `pos`:
|
|
2560
|
+
* its alt text, decorative, and its width and height in px (proportions kept unless unchecked;
|
|
2561
|
+
* not while tracking changes), Apply and Cancel. Escape cancels; a field that is wrong is marked
|
|
2562
|
+
* and announced.
|
|
2563
|
+
*/
|
|
2564
|
+
function PictureDialog({
|
|
2565
|
+
editor,
|
|
2566
|
+
pos,
|
|
2567
|
+
upload,
|
|
2568
|
+
onClose,
|
|
2569
|
+
}: {
|
|
2570
|
+
editor: Editor;
|
|
2571
|
+
pos: number | null;
|
|
2572
|
+
upload: EditorViewOptions["upload"] | undefined;
|
|
2573
|
+
/** `done`: a picture was inserted or changed (the focus may go to the document). */
|
|
2574
|
+
onClose: (done: boolean) => void;
|
|
2575
|
+
}) {
|
|
2576
|
+
const picture = pos === null ? null : editor.state.doc.nodeAt(pos);
|
|
2577
|
+
const attrs = picture?.attrs ?? {};
|
|
2578
|
+
const px = (emu: unknown) => String(Math.round(Number(emu) / EMU_PER_PX));
|
|
2579
|
+
const [file, setFile] = useState<File | null>(null);
|
|
2580
|
+
const [alt, setAlt] = useState(String(attrs.alt ?? ""));
|
|
2581
|
+
const [decorative, setDecorative] = useState(attrs.decorative === true);
|
|
2582
|
+
const [width, setWidth] = useState(picture ? px(attrs.width) : "");
|
|
2583
|
+
const [height, setHeight] = useState(picture ? px(attrs.height) : "");
|
|
2584
|
+
const [keep, setKeep] = useState(true);
|
|
2585
|
+
const [error, setError] = useState<"file" | "alt" | "size" | "failed" | null>(null);
|
|
2586
|
+
const [busy, setBusy] = useState(false);
|
|
2587
|
+
const fileField = useRef<HTMLInputElement>(null);
|
|
2588
|
+
const altField = useRef<HTMLInputElement>(null);
|
|
2589
|
+
const decorativeField = useRef<HTMLInputElement>(null);
|
|
2590
|
+
const widthField = useRef<HTMLInputElement>(null);
|
|
2591
|
+
const id = useId();
|
|
2592
|
+
const tracking = trackState(editor.state)?.on === true;
|
|
2593
|
+
const ratio = picture ? Number(attrs.height) / Number(attrs.width) : 1;
|
|
2594
|
+
// The first field that can take the focus (a decorative picture's alt text cannot).
|
|
2595
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: focus once, on open.
|
|
2596
|
+
useEffect(() => {
|
|
2597
|
+
(pos === null
|
|
2598
|
+
? fileField.current
|
|
2599
|
+
: decorative
|
|
2600
|
+
? decorativeField.current
|
|
2601
|
+
: altField.current
|
|
2602
|
+
)?.focus();
|
|
2603
|
+
}, []);
|
|
2604
|
+
// A dialog closed while an upload runs: what the upload finds then is not inserted. Set in
|
|
2605
|
+
// the effect too: StrictMode runs effects twice (setup, cleanup, setup).
|
|
2606
|
+
const open = useRef(true);
|
|
2607
|
+
useEffect(() => {
|
|
2608
|
+
open.current = true;
|
|
2609
|
+
return () => {
|
|
2610
|
+
open.current = false;
|
|
2611
|
+
};
|
|
2612
|
+
}, []);
|
|
2613
|
+
|
|
2614
|
+
const messages = {
|
|
2615
|
+
file: "Choose an image file.",
|
|
2616
|
+
alt: "Describe the picture, or mark it as decorative.",
|
|
2617
|
+
size: `Enter a width and height in pixels, from 1 to ${MAX_PICTURE_PX}.`,
|
|
2618
|
+
failed: "The picture could not be inserted: the file could not be read or uploaded here.",
|
|
2619
|
+
};
|
|
2620
|
+
const invalid = (field: string) => (error === field ? true : undefined);
|
|
2621
|
+
const described = (field: string) => (error === field ? `${id}-error` : undefined);
|
|
2622
|
+
/** Marks `field` wrong and puts the focus on it. */
|
|
2623
|
+
const fail = (field: NonNullable<typeof error>) => {
|
|
2624
|
+
setError(field);
|
|
2625
|
+
const target = {
|
|
2626
|
+
file: fileField,
|
|
2627
|
+
alt: altField,
|
|
2628
|
+
size: widthField,
|
|
2629
|
+
failed: fileField,
|
|
2630
|
+
}[field].current;
|
|
2631
|
+
target?.focus();
|
|
2632
|
+
};
|
|
2633
|
+
|
|
2634
|
+
const submit = async (event: FormEvent) => {
|
|
2635
|
+
event.preventDefault();
|
|
2636
|
+
if (busy) return;
|
|
2637
|
+
if (!decorative && !alt.trim()) {
|
|
2638
|
+
fail("alt");
|
|
2639
|
+
return;
|
|
2640
|
+
}
|
|
2641
|
+
if (pos !== null) {
|
|
2642
|
+
// The picture is gone (the dialog closes on a change; this is a guard).
|
|
2643
|
+
if (!picture) {
|
|
2644
|
+
onClose(false);
|
|
2645
|
+
return;
|
|
2646
|
+
}
|
|
2647
|
+
const w = Math.round(Number(width));
|
|
2648
|
+
const h = Math.round(Number(height));
|
|
2649
|
+
const sizeChanged = w !== Number(px(attrs.width)) || h !== Number(px(attrs.height));
|
|
2650
|
+
const valid = (n: number) => Number.isFinite(n) && n >= 1 && n <= MAX_PICTURE_PX;
|
|
2651
|
+
if (sizeChanged && !tracking && !(valid(w) && valid(h))) {
|
|
2652
|
+
fail("size");
|
|
2653
|
+
return;
|
|
2654
|
+
}
|
|
2655
|
+
editor.run(setPictureAlt(pos, alt, decorative));
|
|
2656
|
+
if (sizeChanged && !tracking)
|
|
2657
|
+
editor.run(setPictureSize(pos, Math.round(w * EMU_PER_PX), Math.round(h * EMU_PER_PX)));
|
|
2658
|
+
onClose(true);
|
|
2659
|
+
return;
|
|
2660
|
+
}
|
|
2661
|
+
if (!file || !upload || !file.type.startsWith("image/")) {
|
|
2662
|
+
fail("file");
|
|
2663
|
+
return;
|
|
2664
|
+
}
|
|
2665
|
+
setBusy(true);
|
|
2666
|
+
try {
|
|
2667
|
+
const size = await measureImageFile(file, document);
|
|
2668
|
+
const answer = size ? await upload(file) : null;
|
|
2669
|
+
const src = typeof answer === "string" ? answer : answer?.src;
|
|
2670
|
+
if (!open.current) return;
|
|
2671
|
+
if (!size || !src) throw new Error("no image");
|
|
2672
|
+
const scale = Math.min(1, textWidthEmu(editor.state) / (size.width * EMU_PER_PX));
|
|
2673
|
+
const type = /^data:([^;,]+)/i.exec(src)?.[1]?.toLowerCase() ?? file.type;
|
|
2674
|
+
const done = editor.run(
|
|
2675
|
+
insertPicture({
|
|
2676
|
+
contentType: type,
|
|
2677
|
+
...(src.startsWith("data:") ? { data: src } : { url: src }),
|
|
2678
|
+
width: Math.max(1, Math.round(size.width * EMU_PER_PX * scale)),
|
|
2679
|
+
height: Math.max(1, Math.round(size.height * EMU_PER_PX * scale)),
|
|
2680
|
+
...(decorative ? { decorative: true } : { alt }),
|
|
2681
|
+
}),
|
|
2682
|
+
);
|
|
2683
|
+
if (!done) throw new Error("not inserted");
|
|
2684
|
+
onClose(true);
|
|
2685
|
+
} catch {
|
|
2686
|
+
if (!open.current) return;
|
|
2687
|
+
setBusy(false);
|
|
2688
|
+
fail("failed");
|
|
2689
|
+
}
|
|
2690
|
+
};
|
|
2691
|
+
|
|
2692
|
+
const changeWidth = (value: string) => {
|
|
2693
|
+
setWidth(value);
|
|
2694
|
+
setError(null);
|
|
2695
|
+
if (keep && Number(value) > 0)
|
|
2696
|
+
setHeight(String(Math.max(1, Math.round(Number(value) * ratio))));
|
|
2697
|
+
};
|
|
2698
|
+
const changeHeight = (value: string) => {
|
|
2699
|
+
setHeight(value);
|
|
2700
|
+
setError(null);
|
|
2701
|
+
if (keep && Number(value) > 0) setWidth(String(Math.max(1, Math.round(Number(value) / ratio))));
|
|
2702
|
+
};
|
|
2703
|
+
const sizeNote = tracking ? `${id}-tracking` : undefined;
|
|
2704
|
+
const sizeDescribed = [described("size"), sizeNote].filter(Boolean).join(" ") || undefined;
|
|
2705
|
+
|
|
2706
|
+
return (
|
|
2707
|
+
<div
|
|
2708
|
+
role="dialog"
|
|
2709
|
+
aria-label={pos === null ? "Insert picture" : "Edit picture"}
|
|
2710
|
+
className="foli-link-dialog foli-picture-dialog"
|
|
2711
|
+
aria-busy={busy || undefined}
|
|
2712
|
+
onKeyDown={(event) => {
|
|
2713
|
+
if (event.key !== "Escape") return;
|
|
2714
|
+
event.preventDefault();
|
|
2715
|
+
onClose(false);
|
|
2716
|
+
}}
|
|
2717
|
+
>
|
|
2718
|
+
<form onSubmit={submit} noValidate>
|
|
2719
|
+
{pos === null && (
|
|
2720
|
+
<>
|
|
2721
|
+
<label htmlFor={`${id}-file`}>Image</label>
|
|
2722
|
+
<input
|
|
2723
|
+
ref={fileField}
|
|
2724
|
+
id={`${id}-file`}
|
|
2725
|
+
type="file"
|
|
2726
|
+
accept="image/*"
|
|
2727
|
+
required
|
|
2728
|
+
aria-invalid={invalid("file")}
|
|
2729
|
+
aria-describedby={described("file")}
|
|
2730
|
+
onChange={(event) => {
|
|
2731
|
+
setFile(event.target.files?.[0] ?? null);
|
|
2732
|
+
setError(null);
|
|
2733
|
+
}}
|
|
2734
|
+
/>
|
|
2735
|
+
</>
|
|
2736
|
+
)}
|
|
2737
|
+
<label htmlFor={`${id}-alt`}>Alt text</label>
|
|
2738
|
+
<input
|
|
2739
|
+
ref={altField}
|
|
2740
|
+
id={`${id}-alt`}
|
|
2741
|
+
type="text"
|
|
2742
|
+
maxLength={65_535}
|
|
2743
|
+
value={decorative ? "" : alt}
|
|
2744
|
+
disabled={decorative}
|
|
2745
|
+
required={!decorative}
|
|
2746
|
+
aria-invalid={invalid("alt")}
|
|
2747
|
+
aria-describedby={described("alt")}
|
|
2748
|
+
onChange={(event) => {
|
|
2749
|
+
setAlt(event.target.value);
|
|
2750
|
+
setError(null);
|
|
2751
|
+
}}
|
|
2752
|
+
/>
|
|
2753
|
+
<label>
|
|
2754
|
+
<input
|
|
2755
|
+
ref={decorativeField}
|
|
2756
|
+
type="checkbox"
|
|
2757
|
+
checked={decorative}
|
|
2758
|
+
onChange={(event) => {
|
|
2759
|
+
setDecorative(event.target.checked);
|
|
2760
|
+
setError(null);
|
|
2761
|
+
}}
|
|
2762
|
+
/>
|
|
2763
|
+
Decorative (no alt text)
|
|
2764
|
+
</label>
|
|
2765
|
+
{pos !== null && (
|
|
2766
|
+
<>
|
|
2767
|
+
<label htmlFor={`${id}-width`}>Width (px)</label>
|
|
2768
|
+
<input
|
|
2769
|
+
ref={widthField}
|
|
2770
|
+
id={`${id}-width`}
|
|
2771
|
+
type="number"
|
|
2772
|
+
min={1}
|
|
2773
|
+
max={MAX_PICTURE_PX}
|
|
2774
|
+
step={1}
|
|
2775
|
+
value={width}
|
|
2776
|
+
disabled={tracking}
|
|
2777
|
+
aria-invalid={invalid("size")}
|
|
2778
|
+
aria-describedby={sizeDescribed}
|
|
2779
|
+
onChange={(event) => changeWidth(event.target.value)}
|
|
2780
|
+
/>
|
|
2781
|
+
<label htmlFor={`${id}-height`}>Height (px)</label>
|
|
2782
|
+
<input
|
|
2783
|
+
id={`${id}-height`}
|
|
2784
|
+
type="number"
|
|
2785
|
+
min={1}
|
|
2786
|
+
max={MAX_PICTURE_PX}
|
|
2787
|
+
step={1}
|
|
2788
|
+
value={height}
|
|
2789
|
+
disabled={tracking}
|
|
2790
|
+
aria-invalid={invalid("size")}
|
|
2791
|
+
aria-describedby={sizeDescribed}
|
|
2792
|
+
onChange={(event) => changeHeight(event.target.value)}
|
|
2793
|
+
/>
|
|
2794
|
+
<label>
|
|
2795
|
+
<input
|
|
2796
|
+
type="checkbox"
|
|
2797
|
+
checked={keep}
|
|
2798
|
+
disabled={tracking}
|
|
2799
|
+
aria-describedby={sizeNote}
|
|
2800
|
+
onChange={(event) => setKeep(event.target.checked)}
|
|
2801
|
+
/>
|
|
2802
|
+
Keep proportions
|
|
2803
|
+
</label>
|
|
2804
|
+
{tracking && <p id={sizeNote}>The size does not change while changes are tracked.</p>}
|
|
2805
|
+
</>
|
|
2806
|
+
)}
|
|
2807
|
+
{/* Not `disabled` while uploading: the focus stays on it (Escape still cancels). */}
|
|
2808
|
+
<button type="submit" aria-disabled={busy || undefined}>
|
|
2809
|
+
{pos === null ? "Insert" : "Apply"}
|
|
2810
|
+
</button>
|
|
2811
|
+
<button type="button" onClick={() => onClose(false)}>
|
|
2812
|
+
Cancel
|
|
2813
|
+
</button>
|
|
2814
|
+
<p role="status" className="foli-picture-status">
|
|
2815
|
+
{busy ? "Inserting the picture…" : ""}
|
|
2816
|
+
</p>
|
|
2817
|
+
{error && (
|
|
2818
|
+
<p id={`${id}-error`} className="foli-link-error" role="alert">
|
|
2819
|
+
{messages[error]}
|
|
2820
|
+
</p>
|
|
2821
|
+
)}
|
|
2822
|
+
</form>
|
|
2823
|
+
</div>
|
|
2824
|
+
);
|
|
2825
|
+
}
|