@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/dist/FoliToolbar.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { acceptAllChanges, acceptChange, addComment, alignAt, canOutdent, changeIndent, colorAt, fontFamilyAt, fontSizeAt, insertPageBreak, isSafeHref, lineSpacingAt, linkAt, listKindAt, nextChange, normalizeHref, openLinkEditor, paragraphStyleAt, paragraphStyles, previousChange, redo, redoDepth, rejectAllChanges, rejectChange, removeLink, requestsLinkEditor, setAlign, setFontFamily, setFontSize, setHighlight, setLineSpacing, setLink, setParagraphStyle, setReviewView, setTextColor, setTrackChanges, toggleList, toggleMark, trackState, undo, undoDepth, wordContext, } from "@folijs/core";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { acceptAllChanges, acceptChange, addComment, addRepeatingItem, alignAt, canOutdent, changeIndent, colorAt, controlAt, controlProperties, convertNotes, crossReferenceTargets, deleteColumns, deleteRows, deleteTable, documentSections, fieldCodesShown, fieldsIn, fontFamilyAt, fontSizeAt, insertColumn, insertColumnBreak, insertControl, insertCrossReference, insertDate, insertNote, insertPageBreak, insertPageNumber, insertPicture, insertRow, insertSectionBreak, insertTable, insertTableOfContents, isHeaderRow, isSafeHref, lineSpacingAt, linkAt, listKindAt, mergeCells, nextChange, normalizeHref, noteAtSelection, noteProperties, openLinkEditor, paragraphStyleAt, paragraphStyles, previousChange, redo, redoDepth, rejectAllChanges, rejectChange, removeLink, removeSectionBreak, requestsLinkEditor, sectionAt, selectedPicture, setAlign, setCellShading, setControlProperties, setDifferentEvenPages, setDifferentFirstPage, setFontFamily, setFontSize, setHighlight, setLineSpacing, setLink, setNoteProperties, setParagraphStyle, setPictureAlt, setPictureSize, setReviewView, setSectionSetup, setStoryLinked, setTableStyle, setTextColor, setTrackChanges, splitCell, tableStyleAt, tableStyles, toggleFieldCodes, toggleHeaderRow, toggleList, toggleMark, trackState, undo, undoDepth, updateFields, wordContext, } from "@folijs/core";
|
|
3
|
+
import { EMU_PER_PX, measureImageFile, textWidthEmu } from "@folijs/view";
|
|
4
|
+
import { Fragment, useEffect, useId, useMemo, useRef, useState, } from "react";
|
|
5
|
+
import { useActiveEditor, useEditorOrContext, useEditorState } from "./editor.js";
|
|
5
6
|
/**
|
|
6
7
|
* The palettes (fixed: a document should not need a color picker). Text colors keep at least
|
|
7
8
|
* 4.5:1 contrast on white; highlights keep dark text readable.
|
|
@@ -24,6 +25,303 @@ const HIGHLIGHTS = [
|
|
|
24
25
|
{ name: "Pink", color: "#ff00ff" },
|
|
25
26
|
{ name: "Gray", color: "#c0c0c0" },
|
|
26
27
|
];
|
|
28
|
+
/** Cell shadings: light fills that keep text readable. */
|
|
29
|
+
const SHADINGS = [
|
|
30
|
+
{ name: "None", color: null },
|
|
31
|
+
{ name: "Light gray", color: "#d9d9d9" },
|
|
32
|
+
{ name: "Light blue", color: "#deeaf6" },
|
|
33
|
+
{ name: "Light green", color: "#e2efd9" },
|
|
34
|
+
{ name: "Light yellow", color: "#fff2cc" },
|
|
35
|
+
{ name: "Light orange", color: "#fbe4d5" },
|
|
36
|
+
];
|
|
37
|
+
/** The cell around the selection's start, if any (cheap: the start's ancestors). */
|
|
38
|
+
function cellAt(state) {
|
|
39
|
+
const { $from } = state.selection;
|
|
40
|
+
for (let d = $from.depth; d > 0; d--)
|
|
41
|
+
if ($from.node(d).type.name === "tableCell")
|
|
42
|
+
return $from.node(d);
|
|
43
|
+
// A cell selection starts before its first cell.
|
|
44
|
+
const after = $from.nodeAfter;
|
|
45
|
+
return after?.type.name === "tableCell" ? after : null;
|
|
46
|
+
}
|
|
47
|
+
const inTable = (state) => cellAt(state) !== null;
|
|
48
|
+
/** Table and cell properties change only when not tracking (Foli does not record them yet). */
|
|
49
|
+
const canFormatTable = (state) => inTable(state) && trackState(state)?.on !== true;
|
|
50
|
+
const TABLE_ACTIONS = [
|
|
51
|
+
{ key: "insertTable", label: "Insert table…", command: insertTable(1, 1), dialog: "insertTable" },
|
|
52
|
+
{ key: "rowAbove", label: "Insert row above", command: insertRow(-1) },
|
|
53
|
+
{ key: "rowBelow", label: "Insert row below", command: insertRow(1) },
|
|
54
|
+
{ key: "columnLeft", label: "Insert column left", command: insertColumn(-1) },
|
|
55
|
+
{ key: "columnRight", label: "Insert column right", command: insertColumn(1) },
|
|
56
|
+
{ key: "deleteRows", label: "Delete rows", command: deleteRows },
|
|
57
|
+
{ key: "deleteColumns", label: "Delete columns", command: deleteColumns },
|
|
58
|
+
{ key: "deleteTable", label: "Delete table", command: deleteTable },
|
|
59
|
+
{ key: "mergeCells", label: "Merge cells", command: mergeCells },
|
|
60
|
+
{ key: "splitCell", label: "Split cell", command: splitCell },
|
|
61
|
+
{
|
|
62
|
+
key: "headerRow",
|
|
63
|
+
label: "Repeat as header row",
|
|
64
|
+
command: toggleHeaderRow,
|
|
65
|
+
checked: isHeaderRow,
|
|
66
|
+
},
|
|
67
|
+
];
|
|
68
|
+
const SECTION_ACTIONS = [
|
|
69
|
+
{
|
|
70
|
+
key: "pageSetup",
|
|
71
|
+
label: "Page setup…",
|
|
72
|
+
command: (state) => sectionAt(state) !== null,
|
|
73
|
+
dialog: "pageSetup",
|
|
74
|
+
},
|
|
75
|
+
{ key: "breakNext", label: "Section break: next page", command: insertSectionBreak("nextPage") },
|
|
76
|
+
{
|
|
77
|
+
key: "breakContinuous",
|
|
78
|
+
label: "Section break: continuous",
|
|
79
|
+
command: insertSectionBreak("continuous"),
|
|
80
|
+
},
|
|
81
|
+
{ key: "breakEven", label: "Section break: even page", command: insertSectionBreak("evenPage") },
|
|
82
|
+
{ key: "breakOdd", label: "Section break: odd page", command: insertSectionBreak("oddPage") },
|
|
83
|
+
{ key: "removeBreak", label: "Remove section break", command: removeSectionBreak },
|
|
84
|
+
{ key: "columnBreak", label: "Column break", command: insertColumnBreak },
|
|
85
|
+
];
|
|
86
|
+
/**
|
|
87
|
+
* The header and footer menu: opening them on the caret's page, and, for the one open (else the
|
|
88
|
+
* caret's section), what a section does about them.
|
|
89
|
+
*/
|
|
90
|
+
function storyActions(body) {
|
|
91
|
+
const open = () => body.storyView?.open ?? null;
|
|
92
|
+
const setup = (state) => {
|
|
93
|
+
const index = open()?.section ?? sectionAt(state)?.index ?? 0;
|
|
94
|
+
return documentSections(state)[index]?.setup ?? {};
|
|
95
|
+
};
|
|
96
|
+
const edit = (side) => (_state, dispatch) => {
|
|
97
|
+
const view = body.storyView;
|
|
98
|
+
if (!view)
|
|
99
|
+
return false;
|
|
100
|
+
if (dispatch)
|
|
101
|
+
view.edit(side);
|
|
102
|
+
return true;
|
|
103
|
+
};
|
|
104
|
+
const linked = (state) => {
|
|
105
|
+
const story = open();
|
|
106
|
+
if (!story)
|
|
107
|
+
return false;
|
|
108
|
+
if (story.side === "note")
|
|
109
|
+
return false;
|
|
110
|
+
const references = story.side === "header" ? setup(state).headers : setup(state).footers;
|
|
111
|
+
return !references?.[story.variant];
|
|
112
|
+
};
|
|
113
|
+
const evenPages = (state) => wordContext(state)?.envelope.settings?.evenAndOddHeaders === true;
|
|
114
|
+
return [
|
|
115
|
+
{ key: "editHeader", label: "Edit header", command: edit("header"), movesFocus: true },
|
|
116
|
+
{ key: "editFooter", label: "Edit footer", command: edit("footer"), movesFocus: true },
|
|
117
|
+
{
|
|
118
|
+
key: "closeStory",
|
|
119
|
+
label: "Close header and footer",
|
|
120
|
+
movesFocus: true,
|
|
121
|
+
command: (_state, dispatch) => {
|
|
122
|
+
if (!open())
|
|
123
|
+
return false;
|
|
124
|
+
if (dispatch)
|
|
125
|
+
body.storyView?.close();
|
|
126
|
+
return true;
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
key: "firstPage",
|
|
131
|
+
label: "Different first page",
|
|
132
|
+
checked: (state) => setup(state).titlePage === true,
|
|
133
|
+
command: (state, dispatch) => setDifferentFirstPage(setup(state).titlePage !== true, open()?.section)(state, dispatch),
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
key: "evenPages",
|
|
137
|
+
label: "Different odd and even pages",
|
|
138
|
+
checked: evenPages,
|
|
139
|
+
command: (state, dispatch) => setDifferentEvenPages(!evenPages(state))(state, dispatch),
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
key: "linkPrevious",
|
|
143
|
+
label: "Link to previous",
|
|
144
|
+
checked: linked,
|
|
145
|
+
command: (state, dispatch) => {
|
|
146
|
+
const story = open();
|
|
147
|
+
return story && story.side !== "note"
|
|
148
|
+
? setStoryLinked(story.side, story.variant, !linked(state), story.section)(state, dispatch)
|
|
149
|
+
: false;
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
];
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* The notes menu: a new note (the caret goes into it), between a reference and its note, the
|
|
156
|
+
* conversions Word has, and the options dialog.
|
|
157
|
+
*/
|
|
158
|
+
function noteActions(body) {
|
|
159
|
+
const insert = (kind) => (state, dispatch) => {
|
|
160
|
+
if (!insertNote(kind)(state))
|
|
161
|
+
return false;
|
|
162
|
+
if (dispatch) {
|
|
163
|
+
insertNote(kind)(state, dispatch);
|
|
164
|
+
body.storyView?.editNote();
|
|
165
|
+
}
|
|
166
|
+
return true;
|
|
167
|
+
};
|
|
168
|
+
return [
|
|
169
|
+
{ key: "footnote", label: "Insert footnote", command: insert("footnote"), movesFocus: true },
|
|
170
|
+
{ key: "endnote", label: "Insert endnote", command: insert("endnote"), movesFocus: true },
|
|
171
|
+
{
|
|
172
|
+
key: "goToNote",
|
|
173
|
+
label: "Go to note or reference",
|
|
174
|
+
movesFocus: true,
|
|
175
|
+
command: (state, dispatch) => {
|
|
176
|
+
const view = body.storyView;
|
|
177
|
+
if (!view)
|
|
178
|
+
return false;
|
|
179
|
+
if (view.open?.side === "note") {
|
|
180
|
+
if (dispatch)
|
|
181
|
+
view.toReference();
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
if (!noteAtSelection(state))
|
|
185
|
+
return false;
|
|
186
|
+
if (dispatch)
|
|
187
|
+
view.editNote();
|
|
188
|
+
return true;
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
key: "toEndnotes",
|
|
193
|
+
label: "Convert footnotes to endnotes",
|
|
194
|
+
command: (_state, dispatch) => convertNotes("endnote")(body.state, dispatch && body.dispatch),
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
key: "toFootnotes",
|
|
198
|
+
label: "Convert endnotes to footnotes",
|
|
199
|
+
command: (_state, dispatch) => convertNotes("footnote")(body.state, dispatch && body.dispatch),
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
key: "noteOptions",
|
|
203
|
+
label: "Note options…",
|
|
204
|
+
dialog: "noteOptions",
|
|
205
|
+
command: (state) => wordContext(state) !== null,
|
|
206
|
+
},
|
|
207
|
+
];
|
|
208
|
+
}
|
|
209
|
+
/** The control the selection is in. */
|
|
210
|
+
const selectedControl = (state) => {
|
|
211
|
+
const range = controlAt(state.doc, state.selection.from);
|
|
212
|
+
const control = range && controlProperties(state, range.id);
|
|
213
|
+
return range && control ? { id: range.id, control } : null;
|
|
214
|
+
};
|
|
215
|
+
/** The content controls menu: each kind to insert, then what applies to the one at the caret. */
|
|
216
|
+
const CONTROL_ACTIONS = [
|
|
217
|
+
{ key: "richText", label: "Rich text", command: insertControl("richText") },
|
|
218
|
+
{ key: "plainText", label: "Plain text", command: insertControl("text") },
|
|
219
|
+
{ key: "dropDown", label: "Drop-down list", command: insertControl("dropDown") },
|
|
220
|
+
{ key: "comboBox", label: "Combo box", command: insertControl("comboBox") },
|
|
221
|
+
{ key: "datePicker", label: "Date picker", command: insertControl("date") },
|
|
222
|
+
{ key: "checkBox", label: "Check box", command: insertControl("checkbox") },
|
|
223
|
+
{ key: "pictureControl", label: "Picture (selected)", command: insertControl("picture") },
|
|
224
|
+
{ key: "repeating", label: "Repeating section", command: insertControl("repeating") },
|
|
225
|
+
{ key: "repeatingItem", label: "Add repeating item", command: addRepeatingItem() },
|
|
226
|
+
{
|
|
227
|
+
key: "controlProperties",
|
|
228
|
+
label: "Control properties…",
|
|
229
|
+
dialog: "controlProperties",
|
|
230
|
+
command: (state) => selectedControl(state) !== null,
|
|
231
|
+
},
|
|
232
|
+
];
|
|
233
|
+
/**
|
|
234
|
+
* The navigation menu: what `FoliNavigation` shows, with Word's keys. Nothing applies without it.
|
|
235
|
+
*/
|
|
236
|
+
function navigationActions(body) {
|
|
237
|
+
const open = (request) => (_state, dispatch) => {
|
|
238
|
+
if (!body.navigation)
|
|
239
|
+
return false;
|
|
240
|
+
if (dispatch)
|
|
241
|
+
body.navigation(request);
|
|
242
|
+
return true;
|
|
243
|
+
};
|
|
244
|
+
const item = (key, label) => ({
|
|
245
|
+
key,
|
|
246
|
+
label,
|
|
247
|
+
command: open(key),
|
|
248
|
+
movesFocus: true,
|
|
249
|
+
});
|
|
250
|
+
return [
|
|
251
|
+
item("find", "Find\u2026"),
|
|
252
|
+
item("replace", "Replace\u2026"),
|
|
253
|
+
item("goTo", "Go to\u2026"),
|
|
254
|
+
item("bookmarks", "Bookmarks\u2026"),
|
|
255
|
+
item("outline", "Outline"),
|
|
256
|
+
];
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* The fields menu: what can be inserted at the selection, updating (the view's pages when there is
|
|
260
|
+
* one) and the codes shown or hidden.
|
|
261
|
+
*/
|
|
262
|
+
function fieldActions(body) {
|
|
263
|
+
const layout = () => body.storyView?.fieldLayout ?? null;
|
|
264
|
+
const active = () => {
|
|
265
|
+
const open = body.storyView?.open;
|
|
266
|
+
return (open && body.story(open.id)) || body;
|
|
267
|
+
};
|
|
268
|
+
const update = (editor, options) => body.storyView ? body.storyView.updateFields(options, editor) : updateFields(editor, options);
|
|
269
|
+
return [
|
|
270
|
+
{
|
|
271
|
+
key: "pageNumber",
|
|
272
|
+
label: "Page number",
|
|
273
|
+
command: (state, dispatch) => insertPageNumber({ layout: layout() })(state, dispatch),
|
|
274
|
+
},
|
|
275
|
+
{ key: "date", label: "Date", command: insertDate() },
|
|
276
|
+
{
|
|
277
|
+
key: "crossReference",
|
|
278
|
+
label: "Cross-reference…",
|
|
279
|
+
dialog: "crossReference",
|
|
280
|
+
command: (state) => {
|
|
281
|
+
const { bookmarks, headings } = crossReferenceTargets(state);
|
|
282
|
+
return insertPageBreak(state) && bookmarks.length + headings.length > 0;
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
key: "tableOfContents",
|
|
287
|
+
label: "Table of contents",
|
|
288
|
+
command: (state, dispatch) => {
|
|
289
|
+
const done = insertTableOfContents({ layout: layout() })(state, dispatch);
|
|
290
|
+
// Its own lines moved the headings: their page numbers follow, in the same undo step.
|
|
291
|
+
if (done && dispatch)
|
|
292
|
+
update(body, { only: (field) => field.parent?.name === "TOC", continues: true });
|
|
293
|
+
return done;
|
|
294
|
+
},
|
|
295
|
+
},
|
|
296
|
+
{
|
|
297
|
+
key: "updateSelected",
|
|
298
|
+
label: "Update selected fields",
|
|
299
|
+
command: (state, dispatch) => {
|
|
300
|
+
if (!fieldsIn(state.doc).length)
|
|
301
|
+
return false;
|
|
302
|
+
const { from, to } = state.selection;
|
|
303
|
+
if (dispatch)
|
|
304
|
+
update(active(), { only: { from, to } });
|
|
305
|
+
return true;
|
|
306
|
+
},
|
|
307
|
+
},
|
|
308
|
+
{
|
|
309
|
+
key: "updateAll",
|
|
310
|
+
label: "Update all fields",
|
|
311
|
+
command: (_state, dispatch) => {
|
|
312
|
+
if (dispatch)
|
|
313
|
+
update(body, {});
|
|
314
|
+
return true;
|
|
315
|
+
},
|
|
316
|
+
},
|
|
317
|
+
{
|
|
318
|
+
key: "fieldCodes",
|
|
319
|
+
label: "Show field codes",
|
|
320
|
+
checked: fieldCodesShown,
|
|
321
|
+
command: toggleFieldCodes,
|
|
322
|
+
},
|
|
323
|
+
];
|
|
324
|
+
}
|
|
27
325
|
/** Whether the selection holds a textblock (a caret's own block first; else it walks the range). */
|
|
28
326
|
function hasTextblock(state) {
|
|
29
327
|
if (state.selection.$from.parent.isTextblock)
|
|
@@ -325,7 +623,93 @@ const GROUPS = [
|
|
|
325
623
|
command: () => redo,
|
|
326
624
|
},
|
|
327
625
|
],
|
|
328
|
-
[
|
|
626
|
+
[
|
|
627
|
+
{
|
|
628
|
+
key: "table",
|
|
629
|
+
label: "Table",
|
|
630
|
+
text: "Table",
|
|
631
|
+
enabled: (state) => wordContext(state) !== null && "table" in state.schema.nodes,
|
|
632
|
+
actions: TABLE_ACTIONS,
|
|
633
|
+
},
|
|
634
|
+
{
|
|
635
|
+
key: "tableStyle",
|
|
636
|
+
label: "Table style",
|
|
637
|
+
text: "Table style",
|
|
638
|
+
enabled: canFormatTable,
|
|
639
|
+
select: {
|
|
640
|
+
options: (state) => tableStyles(state).map((name) => ({ value: name, label: name })),
|
|
641
|
+
value: (state) => (inTable(state) ? (tableStyleAt(state) ?? "") : null),
|
|
642
|
+
pick: (name) => setTableStyle(name),
|
|
643
|
+
},
|
|
644
|
+
},
|
|
645
|
+
{
|
|
646
|
+
key: "cellShading",
|
|
647
|
+
label: "Cell shading",
|
|
648
|
+
text: "Shading",
|
|
649
|
+
enabled: canFormatTable,
|
|
650
|
+
menu: {
|
|
651
|
+
options: SHADINGS,
|
|
652
|
+
value: (state) => {
|
|
653
|
+
const fill = cellAt(state)?.attrs.shading?.fill;
|
|
654
|
+
return fill && fill.toLowerCase() !== "auto" ? `#${fill.toLowerCase()}` : null;
|
|
655
|
+
},
|
|
656
|
+
pick: (color) => setCellShading(color ? color.slice(1).toUpperCase() : null),
|
|
657
|
+
},
|
|
658
|
+
},
|
|
659
|
+
],
|
|
660
|
+
[
|
|
661
|
+
{
|
|
662
|
+
key: "picture",
|
|
663
|
+
label: "Insert a picture, or edit the selected one",
|
|
664
|
+
text: "Picture…",
|
|
665
|
+
enabled: (state) => wordContext(state) !== null && (selectedPicture(state) !== null || hasTextblock(state)),
|
|
666
|
+
// With a picture selected the dialog edits it.
|
|
667
|
+
dialog: "picture",
|
|
668
|
+
},
|
|
669
|
+
{ key: "pageBreak", label: "Page break", text: "Page break", command: () => insertPageBreak },
|
|
670
|
+
{
|
|
671
|
+
key: "section",
|
|
672
|
+
label: "Section",
|
|
673
|
+
text: "Section",
|
|
674
|
+
enabled: (state) => wordContext(state) !== null,
|
|
675
|
+
actions: SECTION_ACTIONS,
|
|
676
|
+
},
|
|
677
|
+
{
|
|
678
|
+
key: "headerFooter",
|
|
679
|
+
label: "Header and footer",
|
|
680
|
+
text: "Header",
|
|
681
|
+
enabled: (state) => wordContext(state) !== null,
|
|
682
|
+
actions: storyActions,
|
|
683
|
+
},
|
|
684
|
+
{
|
|
685
|
+
key: "fields",
|
|
686
|
+
label: "Fields",
|
|
687
|
+
text: "Fields",
|
|
688
|
+
enabled: (state) => wordContext(state) !== null,
|
|
689
|
+
actions: fieldActions,
|
|
690
|
+
},
|
|
691
|
+
{
|
|
692
|
+
key: "navigate",
|
|
693
|
+
label: "Find and go to",
|
|
694
|
+
text: "Navigate",
|
|
695
|
+
enabled: (state) => wordContext(state) !== null,
|
|
696
|
+
actions: navigationActions,
|
|
697
|
+
},
|
|
698
|
+
{
|
|
699
|
+
key: "notes",
|
|
700
|
+
label: "Footnotes and endnotes",
|
|
701
|
+
text: "Notes",
|
|
702
|
+
enabled: (state) => wordContext(state) !== null,
|
|
703
|
+
actions: noteActions,
|
|
704
|
+
},
|
|
705
|
+
{
|
|
706
|
+
key: "controls",
|
|
707
|
+
label: "Content controls",
|
|
708
|
+
text: "Controls",
|
|
709
|
+
enabled: (state) => wordContext(state) !== null,
|
|
710
|
+
actions: CONTROL_ACTIONS,
|
|
711
|
+
},
|
|
712
|
+
],
|
|
329
713
|
];
|
|
330
714
|
const ITEMS = GROUPS.flat();
|
|
331
715
|
/** Without "New comment": where no comments pane lets a comment be written. */
|
|
@@ -339,20 +723,25 @@ const sameStates = (a, b) => a.length === b.length &&
|
|
|
339
723
|
const statesOf = (items) => (state, editor) => items.map((item) => ({
|
|
340
724
|
pressed: item.pressed?.(state, editor),
|
|
341
725
|
enabled: item.enabled?.(state) ?? true,
|
|
342
|
-
value: item.menu?.value(state) ??
|
|
726
|
+
value: item.menu?.value(state) ??
|
|
727
|
+
item.select?.value(state) ??
|
|
728
|
+
(item.dialog ? (selectedPicture(state) ? "edit" : "insert") : undefined),
|
|
343
729
|
}));
|
|
344
730
|
const selectStates = statesOf(ITEMS);
|
|
345
731
|
const selectStatesNoComment = statesOf(ITEMS_NO_COMMENT);
|
|
346
732
|
/**
|
|
347
733
|
* Word's formatting controls for an editor: paragraph style, font and size boxes, marks, link (a
|
|
348
734
|
* dialog, also on Mod-K in the document), text color and highlight menus, alignment and line
|
|
349
|
-
* spacing, lists (bulleted, numbered, decrease and increase indent), undo, redo,
|
|
735
|
+
* spacing, lists (bulleted, numbered, decrease and increase indent), undo, redo, tables (a Table
|
|
736
|
+
* menu with an insert table dialog, a table style box, a cell shading menu), page break.
|
|
350
737
|
* One tab stop (arrow keys, Home and End move between buttons); toggles carry
|
|
351
738
|
* `aria-pressed`; a button whose command cannot run now is `aria-disabled` (still focusable).
|
|
352
739
|
* Clicks leave the focus and selection in the document.
|
|
353
740
|
*/
|
|
354
|
-
export function FoliToolbar({ editor: given, disabled = false, onClickCommand, label = "Formatting", className, comments = false, }) {
|
|
355
|
-
const
|
|
741
|
+
export function FoliToolbar({ editor: given, disabled = false, onClickCommand, label = "Formatting", className, comments = false, upload, }) {
|
|
742
|
+
const body = useEditorOrContext(given, "FoliToolbar");
|
|
743
|
+
// A story open on its page takes the formatting commands; the body otherwise.
|
|
744
|
+
const editor = useActiveEditor(body);
|
|
356
745
|
const groups = comments ? GROUPS : GROUPS_NO_COMMENT;
|
|
357
746
|
const items = comments ? ITEMS : ITEMS_NO_COMMENT;
|
|
358
747
|
const states = useEditorState(comments ? selectStates : selectStatesNoComment, {
|
|
@@ -373,7 +762,21 @@ export function FoliToolbar({ editor: given, disabled = false, onClickCommand, l
|
|
|
373
762
|
const item = items[index];
|
|
374
763
|
if (!item || disabled || !states[index]?.enabled)
|
|
375
764
|
return;
|
|
376
|
-
if (item.
|
|
765
|
+
if (item.dialog === "picture") {
|
|
766
|
+
const selected = selectedPicture(editor.state);
|
|
767
|
+
// Inserting needs the app's upload; editing a selected picture does not.
|
|
768
|
+
if (!selected && !upload)
|
|
769
|
+
return;
|
|
770
|
+
setDialog(null);
|
|
771
|
+
setTableDialog(null);
|
|
772
|
+
setPictureDialog({
|
|
773
|
+
pos: selected?.pos ?? null,
|
|
774
|
+
selection: editor.state.selection,
|
|
775
|
+
restore: buttons.current[index] ?? null,
|
|
776
|
+
});
|
|
777
|
+
return;
|
|
778
|
+
}
|
|
779
|
+
if (item.menu || item.actions) {
|
|
377
780
|
if (menu !== item.key) {
|
|
378
781
|
setMenu(item.key);
|
|
379
782
|
return;
|
|
@@ -402,6 +805,45 @@ export function FoliToolbar({ editor: given, disabled = false, onClickCommand, l
|
|
|
402
805
|
if (disabled)
|
|
403
806
|
return;
|
|
404
807
|
if (!transactions.some(requestsLinkEditor)) {
|
|
808
|
+
// The picture dialog too: what it edits or inserts at may have gone (another
|
|
809
|
+
// document, set with no transaction, included).
|
|
810
|
+
setPictureDialog((current) => current &&
|
|
811
|
+
(!transactions.length ||
|
|
812
|
+
transactions.some((tr) => tr.docChanged) ||
|
|
813
|
+
!state.selection.eq(current.selection))
|
|
814
|
+
? null
|
|
815
|
+
: current);
|
|
816
|
+
// The cross-reference dialog inserts at the selection it opened on.
|
|
817
|
+
setReferenceDialog((current) => current &&
|
|
818
|
+
(!transactions.length ||
|
|
819
|
+
transactions.some((tr) => tr.docChanged) ||
|
|
820
|
+
!state.selection.eq(current.selection))
|
|
821
|
+
? null
|
|
822
|
+
: current);
|
|
823
|
+
setNoteDialog((current) => current && (!transactions.length || transactions.some((tr) => tr.docChanged))
|
|
824
|
+
? null
|
|
825
|
+
: current);
|
|
826
|
+
// The control properties dialog is for the control it opened on.
|
|
827
|
+
setControlDialog((current) => current &&
|
|
828
|
+
(!transactions.length ||
|
|
829
|
+
transactions.some((tr) => tr.docChanged) ||
|
|
830
|
+
!state.selection.eq(current.selection))
|
|
831
|
+
? null
|
|
832
|
+
: current);
|
|
833
|
+
// So is the page setup dialog: its section may have changed.
|
|
834
|
+
setSetupDialog((current) => current &&
|
|
835
|
+
(!transactions.length ||
|
|
836
|
+
transactions.some((tr) => tr.docChanged) ||
|
|
837
|
+
!state.selection.eq(current.selection))
|
|
838
|
+
? null
|
|
839
|
+
: current);
|
|
840
|
+
// The insert table dialog is for the caret it opened on, as the link dialog is.
|
|
841
|
+
setTableDialog((current) => current &&
|
|
842
|
+
(!transactions.length ||
|
|
843
|
+
transactions.some((tr) => tr.docChanged) ||
|
|
844
|
+
!state.selection.eq(current.selection))
|
|
845
|
+
? null
|
|
846
|
+
: current);
|
|
405
847
|
setDialog((current) => current &&
|
|
406
848
|
(transactions.some((tr) => tr.docChanged) || !state.selection.eq(current.selection))
|
|
407
849
|
? null
|
|
@@ -409,6 +851,11 @@ export function FoliToolbar({ editor: given, disabled = false, onClickCommand, l
|
|
|
409
851
|
return;
|
|
410
852
|
}
|
|
411
853
|
const active = typeof document === "undefined" ? null : document.activeElement;
|
|
854
|
+
setTableDialog(null);
|
|
855
|
+
setPictureDialog(null);
|
|
856
|
+
setSetupDialog(null);
|
|
857
|
+
setReferenceDialog(null);
|
|
858
|
+
setControlDialog(null);
|
|
412
859
|
setDialog((current) => current
|
|
413
860
|
? { ...current, request: current.request + 1 }
|
|
414
861
|
: {
|
|
@@ -427,6 +874,42 @@ export function FoliToolbar({ editor: given, disabled = false, onClickCommand, l
|
|
|
427
874
|
dialog?.restore?.focus();
|
|
428
875
|
setDialog(null);
|
|
429
876
|
};
|
|
877
|
+
// The insert table dialog (from the table menu): the focus goes back to the menu's button.
|
|
878
|
+
const [tableDialog, setTableDialog] = useState(null);
|
|
879
|
+
useEffect(() => {
|
|
880
|
+
if (disabled)
|
|
881
|
+
setTableDialog(null);
|
|
882
|
+
}, [disabled]);
|
|
883
|
+
// The page setup dialog (from the section menu), for the section of the caret it opened on.
|
|
884
|
+
const [setupDialog, setSetupDialog] = useState(null);
|
|
885
|
+
useEffect(() => {
|
|
886
|
+
if (disabled)
|
|
887
|
+
setSetupDialog(null);
|
|
888
|
+
}, [disabled]);
|
|
889
|
+
// The cross-reference dialog (from the fields menu).
|
|
890
|
+
const [referenceDialog, setReferenceDialog] = useState(null);
|
|
891
|
+
useEffect(() => {
|
|
892
|
+
if (disabled)
|
|
893
|
+
setReferenceDialog(null);
|
|
894
|
+
}, [disabled]);
|
|
895
|
+
// The control properties dialog (from the content controls menu).
|
|
896
|
+
const [controlDialog, setControlDialog] = useState(null);
|
|
897
|
+
useEffect(() => {
|
|
898
|
+
if (disabled)
|
|
899
|
+
setControlDialog(null);
|
|
900
|
+
}, [disabled]);
|
|
901
|
+
// The note options dialog (from the notes menu).
|
|
902
|
+
const [noteDialog, setNoteDialog] = useState(null);
|
|
903
|
+
useEffect(() => {
|
|
904
|
+
if (disabled)
|
|
905
|
+
setNoteDialog(null);
|
|
906
|
+
}, [disabled]);
|
|
907
|
+
// The picture dialog: inserts at (or edits the picture at `pos`) the selection it opened on.
|
|
908
|
+
const [pictureDialog, setPictureDialog] = useState(null);
|
|
909
|
+
useEffect(() => {
|
|
910
|
+
if (disabled)
|
|
911
|
+
setPictureDialog(null);
|
|
912
|
+
}, [disabled]);
|
|
430
913
|
const onKeyDown = (event) => {
|
|
431
914
|
// Alt+Left (back) and other shortcuts are the browser's.
|
|
432
915
|
if (event.altKey || event.ctrlKey || event.metaKey)
|
|
@@ -449,7 +932,10 @@ export function FoliToolbar({ editor: given, disabled = false, onClickCommand, l
|
|
|
449
932
|
return (_jsxs(_Fragment, { children: [_jsx("div", { role: "toolbar", "aria-label": label, className: className ? `foli-toolbar ${className}` : "foli-toolbar", onKeyDown: onKeyDown, children: groups.map((group, g) => (_jsxs(Fragment, { children: [g > 0 && _jsx("hr", { className: "foli-toolbar-separator", "aria-orientation": "vertical" }), group.map((item) => {
|
|
450
933
|
const i = index++;
|
|
451
934
|
const state = states[i];
|
|
452
|
-
|
|
935
|
+
// Inserting a picture needs the app's upload; editing a selected one does not.
|
|
936
|
+
const off = disabled ||
|
|
937
|
+
!state?.enabled ||
|
|
938
|
+
(item.dialog === "picture" && state?.value === "insert" && !upload);
|
|
453
939
|
if (item.select) {
|
|
454
940
|
const { select } = item;
|
|
455
941
|
const value = state?.value ?? "";
|
|
@@ -473,15 +959,81 @@ export function FoliToolbar({ editor: given, disabled = false, onClickCommand, l
|
|
|
473
959
|
}
|
|
474
960
|
const button = (_jsxs("button", { ref: (element) => {
|
|
475
961
|
buttons.current[i] = element;
|
|
476
|
-
}, type: "button", className: `foli-toolbar-button foli-toolbar-${item.key}`, "aria-label": item.symbol ? item.label : undefined, title: item.label, "aria-pressed": item.pressed ? Boolean(state?.pressed) : undefined, "aria-haspopup": item.menu ? "menu" : undefined, "aria-expanded": item.menu ? menu === item.key : undefined, "aria-disabled": off || undefined, tabIndex: i === active ? 0 : -1,
|
|
962
|
+
}, type: "button", className: `foli-toolbar-button foli-toolbar-${item.key}`, "aria-label": item.symbol ? item.label : undefined, title: item.label, "aria-pressed": item.pressed ? Boolean(state?.pressed) : undefined, "aria-haspopup": item.menu || item.actions ? "menu" : item.dialog ? "dialog" : undefined, "aria-expanded": item.menu || item.actions ? menu === item.key : undefined, "aria-disabled": off || undefined, tabIndex: i === active ? 0 : -1,
|
|
477
963
|
// Keep the focus (and the selection) in the document on click.
|
|
478
964
|
onMouseDown: (event) => event.preventDefault(), onFocus: () => setActive(i), onClick: (event) => run(i, event), onKeyDown: (event) => {
|
|
479
965
|
// ArrowDown opens a menu (the WAI-ARIA menu button pattern).
|
|
480
|
-
if (!item.menu || off || event.key !== "ArrowDown")
|
|
966
|
+
if (!(item.menu || item.actions) || off || event.key !== "ArrowDown")
|
|
481
967
|
return;
|
|
482
968
|
event.preventDefault();
|
|
483
969
|
setMenu(item.key);
|
|
484
970
|
}, children: [item.icon ?? item.text, item.menu && (_jsx("span", { className: "foli-toolbar-swatch", "aria-hidden": "true", style: { background: state?.value ?? "transparent" } }))] }, item.key));
|
|
971
|
+
if (item.actions) {
|
|
972
|
+
const actions = typeof item.actions === "function" ? item.actions(body) : item.actions;
|
|
973
|
+
return (_jsxs("span", { className: "foli-toolbar-menu", children: [button, menu === item.key && !disabled && (_jsx(ActionMenu, { label: item.label, actions: actions, state: editor.state, onPick: (action, pointer) => {
|
|
974
|
+
setMenu(null);
|
|
975
|
+
if (action.dialog === "noteOptions") {
|
|
976
|
+
setDialog(null);
|
|
977
|
+
setTableDialog(null);
|
|
978
|
+
setSetupDialog(null);
|
|
979
|
+
setReferenceDialog(null);
|
|
980
|
+
setControlDialog(null);
|
|
981
|
+
setNoteDialog({ restore: buttons.current[i] ?? null });
|
|
982
|
+
return;
|
|
983
|
+
}
|
|
984
|
+
if (action.dialog === "controlProperties") {
|
|
985
|
+
setDialog(null);
|
|
986
|
+
setTableDialog(null);
|
|
987
|
+
setSetupDialog(null);
|
|
988
|
+
setReferenceDialog(null);
|
|
989
|
+
setControlDialog({
|
|
990
|
+
restore: buttons.current[i] ?? null,
|
|
991
|
+
selection: editor.state.selection,
|
|
992
|
+
});
|
|
993
|
+
return;
|
|
994
|
+
}
|
|
995
|
+
if (action.dialog === "crossReference") {
|
|
996
|
+
setDialog(null);
|
|
997
|
+
setTableDialog(null);
|
|
998
|
+
setSetupDialog(null);
|
|
999
|
+
setReferenceDialog({
|
|
1000
|
+
restore: buttons.current[i] ?? null,
|
|
1001
|
+
selection: editor.state.selection,
|
|
1002
|
+
});
|
|
1003
|
+
return;
|
|
1004
|
+
}
|
|
1005
|
+
if (action.dialog === "pageSetup") {
|
|
1006
|
+
setDialog(null);
|
|
1007
|
+
setTableDialog(null);
|
|
1008
|
+
setSetupDialog({
|
|
1009
|
+
restore: buttons.current[i] ?? null,
|
|
1010
|
+
selection: editor.state.selection,
|
|
1011
|
+
});
|
|
1012
|
+
return;
|
|
1013
|
+
}
|
|
1014
|
+
if (action.dialog) {
|
|
1015
|
+
setDialog(null);
|
|
1016
|
+
setSetupDialog(null);
|
|
1017
|
+
setTableDialog((current) => ({
|
|
1018
|
+
restore: buttons.current[i] ?? null,
|
|
1019
|
+
selection: editor.state.selection,
|
|
1020
|
+
request: (current?.request ?? -1) + 1,
|
|
1021
|
+
}));
|
|
1022
|
+
return;
|
|
1023
|
+
}
|
|
1024
|
+
editor.run(action.command);
|
|
1025
|
+
if (action.movesFocus)
|
|
1026
|
+
return;
|
|
1027
|
+
if (pointer)
|
|
1028
|
+
onClickCommand?.();
|
|
1029
|
+
else
|
|
1030
|
+
buttons.current[i]?.focus();
|
|
1031
|
+
}, onClose: (refocus) => {
|
|
1032
|
+
setMenu(null);
|
|
1033
|
+
if (refocus)
|
|
1034
|
+
buttons.current[i]?.focus();
|
|
1035
|
+
} }))] }, item.key));
|
|
1036
|
+
}
|
|
485
1037
|
const options = item.menu?.options;
|
|
486
1038
|
if (!options || !item.menu)
|
|
487
1039
|
return button;
|
|
@@ -498,7 +1050,49 @@ export function FoliToolbar({ editor: given, disabled = false, onClickCommand, l
|
|
|
498
1050
|
if (refocus)
|
|
499
1051
|
buttons.current[i]?.focus();
|
|
500
1052
|
} }))] }, item.key));
|
|
501
|
-
})] }, group[0]?.key))) }),
|
|
1053
|
+
})] }, group[0]?.key))) }), pictureDialog && !disabled && (_jsx(PictureDialog, { editor: editor, pos: pictureDialog.pos, upload: upload, onClose: (done) => {
|
|
1054
|
+
const { restore } = pictureDialog;
|
|
1055
|
+
setPictureDialog(null);
|
|
1056
|
+
if (done && onClickCommand)
|
|
1057
|
+
onClickCommand();
|
|
1058
|
+
else
|
|
1059
|
+
restore?.focus();
|
|
1060
|
+
} })), setupDialog && !disabled && (_jsx(PageSetupDialog, { editor: editor, onClose: (applied) => {
|
|
1061
|
+
const { restore } = setupDialog;
|
|
1062
|
+
setSetupDialog(null);
|
|
1063
|
+
if (applied && onClickCommand)
|
|
1064
|
+
onClickCommand();
|
|
1065
|
+
else
|
|
1066
|
+
restore?.focus();
|
|
1067
|
+
} })), referenceDialog && !disabled && (_jsx(CrossReferenceDialog, { editor: editor, layout: () => body.storyView?.fieldLayout ?? null, onClose: (inserted) => {
|
|
1068
|
+
const { restore } = referenceDialog;
|
|
1069
|
+
setReferenceDialog(null);
|
|
1070
|
+
if (inserted && onClickCommand)
|
|
1071
|
+
onClickCommand();
|
|
1072
|
+
else
|
|
1073
|
+
restore?.focus();
|
|
1074
|
+
} })), noteDialog && !disabled && (_jsx(NoteOptionsDialog, { editor: body, onClose: (applied) => {
|
|
1075
|
+
const { restore } = noteDialog;
|
|
1076
|
+
setNoteDialog(null);
|
|
1077
|
+
if (applied && onClickCommand)
|
|
1078
|
+
onClickCommand();
|
|
1079
|
+
else
|
|
1080
|
+
restore?.focus();
|
|
1081
|
+
} })), controlDialog && !disabled && (_jsx(ControlDialog, { editor: editor, onClose: (applied) => {
|
|
1082
|
+
const { restore } = controlDialog;
|
|
1083
|
+
setControlDialog(null);
|
|
1084
|
+
if (applied && onClickCommand)
|
|
1085
|
+
onClickCommand();
|
|
1086
|
+
else
|
|
1087
|
+
restore?.focus();
|
|
1088
|
+
} })), tableDialog && !disabled && (_jsx(InsertTableDialog, { editor: editor, request: tableDialog.request, onClose: (inserted) => {
|
|
1089
|
+
const { restore } = tableDialog;
|
|
1090
|
+
setTableDialog(null);
|
|
1091
|
+
if (inserted && onClickCommand)
|
|
1092
|
+
onClickCommand();
|
|
1093
|
+
else
|
|
1094
|
+
restore?.focus();
|
|
1095
|
+
} })), dialog && !disabled && (_jsx(LinkDialog, { editor: editor, href: dialog.href, request: dialog.request, onClose: closeDialog }))] }));
|
|
502
1096
|
}
|
|
503
1097
|
/**
|
|
504
1098
|
* A color menu (WAI-ARIA menu with radio items): the current color checked and focused on
|
|
@@ -585,4 +1179,620 @@ function LinkDialog({ editor, href, request, onClose, }) {
|
|
|
585
1179
|
setInvalid(false);
|
|
586
1180
|
} }), _jsx("button", { type: "submit", children: "Apply" }), href && (_jsx("button", { type: "button", onClick: remove, children: "Remove link" })), _jsx("button", { type: "button", onClick: onClose, children: "Cancel" }), invalid && (_jsx("p", { id: `${id}-error`, className: "foli-link-error", role: "alert", children: "Enter a web or email address, or a link starting with http, https, mailto or tel." }))] }) }));
|
|
587
1181
|
}
|
|
1182
|
+
/**
|
|
1183
|
+
* An action menu (WAI-ARIA menu: menuitems, a menuitemcheckbox for a toggle): the first item
|
|
1184
|
+
* focused on open; arrow keys, Home and End move; Enter or Space (or a click) runs an item;
|
|
1185
|
+
* Escape closes it back to its button, Tab closes it and moves on; leaving it closes it. An item
|
|
1186
|
+
* that cannot apply here is `aria-disabled` (still focusable, as in the toolbar).
|
|
1187
|
+
*/
|
|
1188
|
+
function ActionMenu({ label, actions, state, onPick, onClose, }) {
|
|
1189
|
+
const items = useRef([]);
|
|
1190
|
+
// Dry runs, once per opening (not after every transaction).
|
|
1191
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: the actions are a constant list.
|
|
1192
|
+
const enabled = useMemo(() => actions.map((action) => action.command(state)), [state]);
|
|
1193
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: focus once, on open.
|
|
1194
|
+
useEffect(() => {
|
|
1195
|
+
items.current[0]?.focus();
|
|
1196
|
+
}, []);
|
|
1197
|
+
const onKeyDown = (event) => {
|
|
1198
|
+
const count = actions.length;
|
|
1199
|
+
const at = items.current.indexOf(event.target);
|
|
1200
|
+
const moves = {
|
|
1201
|
+
ArrowDown: (at + 1) % count,
|
|
1202
|
+
ArrowUp: (at - 1 + count) % count,
|
|
1203
|
+
Home: 0,
|
|
1204
|
+
End: count - 1,
|
|
1205
|
+
};
|
|
1206
|
+
const next = moves[event.key];
|
|
1207
|
+
event.stopPropagation();
|
|
1208
|
+
if (next !== undefined) {
|
|
1209
|
+
event.preventDefault();
|
|
1210
|
+
items.current[next]?.focus();
|
|
1211
|
+
}
|
|
1212
|
+
else if (event.key === "Escape") {
|
|
1213
|
+
event.preventDefault();
|
|
1214
|
+
onClose(true);
|
|
1215
|
+
}
|
|
1216
|
+
else if (event.key === "Tab")
|
|
1217
|
+
onClose(true);
|
|
1218
|
+
};
|
|
1219
|
+
return (_jsx("div", { role: "menu", "aria-label": label, className: "foli-menu", onKeyDown: onKeyDown, onBlur: (event) => {
|
|
1220
|
+
if (!event.currentTarget.contains(event.relatedTarget))
|
|
1221
|
+
onClose(false);
|
|
1222
|
+
}, children: actions.map((action, i) => {
|
|
1223
|
+
const props = {
|
|
1224
|
+
ref: (element) => {
|
|
1225
|
+
items.current[i] = element;
|
|
1226
|
+
},
|
|
1227
|
+
type: "button",
|
|
1228
|
+
"aria-disabled": enabled[i] ? undefined : true,
|
|
1229
|
+
tabIndex: -1,
|
|
1230
|
+
className: "foli-menu-item",
|
|
1231
|
+
onMouseDown: (event) => event.preventDefault(),
|
|
1232
|
+
onClick: (event) => {
|
|
1233
|
+
if (enabled[i])
|
|
1234
|
+
onPick(action, event.detail > 0);
|
|
1235
|
+
},
|
|
1236
|
+
};
|
|
1237
|
+
return action.checked ? (_jsx("button", { ...props, role: "menuitemcheckbox", "aria-checked": action.checked(state), children: action.label }, action.key)) : (_jsx("button", { ...props, role: "menuitem", children: action.label }, action.key));
|
|
1238
|
+
}) }));
|
|
1239
|
+
}
|
|
1240
|
+
/** Word's limits for a new table. */
|
|
1241
|
+
const MAX_TABLE_ROWS = 1000;
|
|
1242
|
+
const MAX_TABLE_COLUMNS = 63;
|
|
1243
|
+
/**
|
|
1244
|
+
* The insert table dialog: rows and columns (whole numbers within Word's limits; an invalid one
|
|
1245
|
+
* is marked and announced), Insert and Cancel; Escape cancels.
|
|
1246
|
+
*/
|
|
1247
|
+
function InsertTableDialog({ editor, request, onClose, }) {
|
|
1248
|
+
const [rows, setRows] = useState("2");
|
|
1249
|
+
const [columns, setColumns] = useState("3");
|
|
1250
|
+
// What is wrong: a field out of range, or the place (the caret is not in a paragraph).
|
|
1251
|
+
const [error, setError] = useState(null);
|
|
1252
|
+
const rowsField = useRef(null);
|
|
1253
|
+
const columnsField = useRef(null);
|
|
1254
|
+
const id = useId();
|
|
1255
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: each request focuses the field again.
|
|
1256
|
+
useEffect(() => {
|
|
1257
|
+
rowsField.current?.focus();
|
|
1258
|
+
rowsField.current?.select();
|
|
1259
|
+
}, [request]);
|
|
1260
|
+
const parse = (value, max) => {
|
|
1261
|
+
const n = Number(value);
|
|
1262
|
+
return Number.isInteger(n) && n >= 1 && n <= max ? n : null;
|
|
1263
|
+
};
|
|
1264
|
+
const insert = (event) => {
|
|
1265
|
+
event.preventDefault();
|
|
1266
|
+
const r = parse(rows, MAX_TABLE_ROWS);
|
|
1267
|
+
const c = parse(columns, MAX_TABLE_COLUMNS);
|
|
1268
|
+
const wrong = r === null ? "rows" : c === null ? "columns" : null;
|
|
1269
|
+
if (!wrong && editor.run(insertTable(r, c))) {
|
|
1270
|
+
onClose(true);
|
|
1271
|
+
return;
|
|
1272
|
+
}
|
|
1273
|
+
setError(wrong ?? "place");
|
|
1274
|
+
(wrong === "columns" ? columnsField : rowsField).current?.focus();
|
|
1275
|
+
};
|
|
1276
|
+
const messages = {
|
|
1277
|
+
rows: `Enter a number of rows from 1 to ${MAX_TABLE_ROWS}.`,
|
|
1278
|
+
columns: `Enter a number of columns from 1 to ${MAX_TABLE_COLUMNS}.`,
|
|
1279
|
+
place: "A table cannot be inserted here: put the caret in a paragraph.",
|
|
1280
|
+
};
|
|
1281
|
+
const field = (name, label, value, set, max, ref) => (_jsxs(_Fragment, { children: [_jsx("label", { htmlFor: `${id}-${name}`, children: label }), _jsx("input", { ref: ref, id: `${id}-${name}`, type: "number", min: 1, max: max, step: 1, value: value, "aria-invalid": error === name || undefined, "aria-describedby": error === name ? `${id}-error` : undefined, onChange: (event) => {
|
|
1282
|
+
set(event.target.value);
|
|
1283
|
+
setError(null);
|
|
1284
|
+
} })] }));
|
|
1285
|
+
return (_jsx("div", { role: "dialog", "aria-label": "Insert table", className: "foli-link-dialog foli-table-dialog", onKeyDown: (event) => {
|
|
1286
|
+
if (event.key !== "Escape")
|
|
1287
|
+
return;
|
|
1288
|
+
event.preventDefault();
|
|
1289
|
+
onClose(false);
|
|
1290
|
+
}, children: _jsxs("form", { onSubmit: insert, noValidate: true, children: [field("rows", "Rows", rows, setRows, MAX_TABLE_ROWS, rowsField), field("columns", "Columns", columns, setColumns, MAX_TABLE_COLUMNS, columnsField), _jsx("button", { type: "submit", children: "Insert" }), _jsx("button", { type: "button", onClick: () => onClose(false), children: "Cancel" }), error && (_jsx("p", { id: `${id}-error`, className: "foli-link-error", role: "alert", children: messages[error] }))] }) }));
|
|
1291
|
+
}
|
|
1292
|
+
/**
|
|
1293
|
+
* The cross-reference dialog: what to refer to (the document's bookmarks and headings), what to
|
|
1294
|
+
* show of it, whether it links there; Insert and Cancel; Escape cancels.
|
|
1295
|
+
*/
|
|
1296
|
+
function CrossReferenceDialog({ editor, layout, onClose, }) {
|
|
1297
|
+
const [{ bookmarks, headings }] = useState(() => crossReferenceTargets(editor.state));
|
|
1298
|
+
const options = [
|
|
1299
|
+
...headings.map((h) => ({
|
|
1300
|
+
value: `h${h.pos}`,
|
|
1301
|
+
label: `${"\u00a0".repeat((h.level - 1) * 2)}${h.number ? `${h.number} ` : ""}${h.text}`,
|
|
1302
|
+
})),
|
|
1303
|
+
...bookmarks.map((b) => ({ value: `b${b.name}`, label: `Bookmark: ${b.name}` })),
|
|
1304
|
+
];
|
|
1305
|
+
const [target, setTarget] = useState(options[0]?.value ?? "");
|
|
1306
|
+
const [show, setShow] = useState("text");
|
|
1307
|
+
const [link, setLink] = useState(true);
|
|
1308
|
+
const [failed, setFailed] = useState(false);
|
|
1309
|
+
const first = useRef(null);
|
|
1310
|
+
const id = useId();
|
|
1311
|
+
useEffect(() => {
|
|
1312
|
+
first.current?.focus();
|
|
1313
|
+
}, []);
|
|
1314
|
+
const insert = (event) => {
|
|
1315
|
+
event.preventDefault();
|
|
1316
|
+
const to = target[0] === "h" ? Number(target.slice(1)) : target.slice(1);
|
|
1317
|
+
if (editor.run(insertCrossReference({ to, show, link, layout: layout() })))
|
|
1318
|
+
onClose(true);
|
|
1319
|
+
else
|
|
1320
|
+
setFailed(true);
|
|
1321
|
+
};
|
|
1322
|
+
return (_jsx("div", { role: "dialog", "aria-modal": "true", "aria-label": "Insert cross-reference", className: "foli-link-dialog foli-table-dialog", onKeyDown: (event) => {
|
|
1323
|
+
if (event.key === "Escape") {
|
|
1324
|
+
event.preventDefault();
|
|
1325
|
+
onClose(false);
|
|
1326
|
+
}
|
|
1327
|
+
if (event.key !== "Tab")
|
|
1328
|
+
return;
|
|
1329
|
+
// Tab goes round inside the dialog.
|
|
1330
|
+
const stops = [
|
|
1331
|
+
...event.currentTarget.querySelectorAll("select, input, button"),
|
|
1332
|
+
];
|
|
1333
|
+
const at = stops.indexOf(event.target);
|
|
1334
|
+
const next = (at + (event.shiftKey ? -1 : 1) + stops.length) % stops.length;
|
|
1335
|
+
event.preventDefault();
|
|
1336
|
+
stops[next]?.focus();
|
|
1337
|
+
}, children: _jsxs("form", { onSubmit: insert, noValidate: true, children: [_jsx("label", { htmlFor: `${id}-target`, children: "Refer to" }), _jsx("select", { ref: first, id: `${id}-target`, value: target, onChange: (event) => {
|
|
1338
|
+
setTarget(event.target.value);
|
|
1339
|
+
setFailed(false);
|
|
1340
|
+
}, children: options.map((option) => (_jsx("option", { value: option.value, children: option.label }, option.value))) }), _jsx("label", { htmlFor: `${id}-show`, children: "Show" }), _jsxs("select", { id: `${id}-show`, value: show, onChange: (event) => setShow(event.target.value), children: [_jsx("option", { value: "text", children: "Its text" }), _jsx("option", { value: "page", children: "Its page number" }), _jsx("option", { value: "number", children: "Its paragraph number" })] }), _jsxs("label", { children: [_jsx("input", { type: "checkbox", checked: link, onChange: (event) => setLink(event.target.checked) }), "Link to it"] }), _jsx("button", { type: "submit", children: "Insert" }), _jsx("button", { type: "button", onClick: () => onClose(false), children: "Cancel" }), failed && (_jsx("p", { className: "foli-link-error", role: "alert", children: "A cross-reference cannot be inserted here." }))] }) }));
|
|
1341
|
+
}
|
|
1342
|
+
/** Word's number formats for notes. */
|
|
1343
|
+
const NOTE_FORMATS = [
|
|
1344
|
+
["decimal", "1, 2, 3"],
|
|
1345
|
+
["lowerLetter", "a, b, c"],
|
|
1346
|
+
["upperLetter", "A, B, C"],
|
|
1347
|
+
["lowerRoman", "i, ii, iii"],
|
|
1348
|
+
["upperRoman", "I, II, III"],
|
|
1349
|
+
["chicago", "*, †, ‡"],
|
|
1350
|
+
];
|
|
1351
|
+
/**
|
|
1352
|
+
* How notes are placed and numbered, for the whole document as Word's dialog applies it: where
|
|
1353
|
+
* they go, the number format, where numbers start and when they restart; Escape cancels.
|
|
1354
|
+
*/
|
|
1355
|
+
function NoteOptionsDialog({ editor, onClose, }) {
|
|
1356
|
+
const [kind, setKind] = useState("footnote");
|
|
1357
|
+
const valuesOf = (of) => {
|
|
1358
|
+
const envelope = wordContext(editor.state)?.envelope;
|
|
1359
|
+
const section = sectionAt(editor.state)?.setup;
|
|
1360
|
+
return noteProperties(envelope ?? {}, of, section);
|
|
1361
|
+
};
|
|
1362
|
+
const [values, setValues] = useState(() => valuesOf("footnote"));
|
|
1363
|
+
const [start, setStart] = useState(String(values.start));
|
|
1364
|
+
const first = useRef(null);
|
|
1365
|
+
const id = useId();
|
|
1366
|
+
useEffect(() => {
|
|
1367
|
+
first.current?.focus();
|
|
1368
|
+
}, []);
|
|
1369
|
+
const set = (change) => setValues((v) => ({ ...v, ...change }));
|
|
1370
|
+
const restarts = values.restart !== "continuous";
|
|
1371
|
+
const number = Number(start);
|
|
1372
|
+
const invalid = !restarts && (!Number.isInteger(number) || number < 1 || number > 16383);
|
|
1373
|
+
const apply = (event) => {
|
|
1374
|
+
event.preventDefault();
|
|
1375
|
+
if (invalid)
|
|
1376
|
+
return;
|
|
1377
|
+
const properties = {
|
|
1378
|
+
position: values.position,
|
|
1379
|
+
format: values.format,
|
|
1380
|
+
restart: values.restart,
|
|
1381
|
+
start: restarts ? 1 : number,
|
|
1382
|
+
};
|
|
1383
|
+
onClose(editor.run(setNoteProperties(kind, properties)));
|
|
1384
|
+
};
|
|
1385
|
+
return (_jsx("div", { role: "dialog", "aria-label": "Note options", className: "foli-link-dialog foli-table-dialog", onKeyDown: (event) => {
|
|
1386
|
+
if (event.key !== "Escape")
|
|
1387
|
+
return;
|
|
1388
|
+
event.preventDefault();
|
|
1389
|
+
onClose(false);
|
|
1390
|
+
}, children: _jsxs("form", { onSubmit: apply, noValidate: true, children: [_jsx("label", { htmlFor: `${id}-kind`, children: "Notes" }), _jsxs("select", { ref: first, id: `${id}-kind`, value: kind, onChange: (event) => {
|
|
1391
|
+
const next = event.target.value;
|
|
1392
|
+
const shown = valuesOf(next);
|
|
1393
|
+
setKind(next);
|
|
1394
|
+
setValues(shown);
|
|
1395
|
+
setStart(String(shown.start));
|
|
1396
|
+
}, children: [_jsx("option", { value: "footnote", children: "Footnotes" }), _jsx("option", { value: "endnote", children: "Endnotes" })] }), _jsx("label", { htmlFor: `${id}-position`, children: "Position" }), _jsx("select", { id: `${id}-position`, value: values.position, onChange: (event) => set({ position: event.target.value }), children: kind === "footnote" ? (_jsxs(_Fragment, { children: [_jsx("option", { value: "pageBottom", children: "Bottom of page" }), _jsx("option", { value: "beneathText", children: "Below text" })] })) : (_jsxs(_Fragment, { children: [_jsx("option", { value: "docEnd", children: "End of document" }), _jsx("option", { value: "sectEnd", children: "End of section" })] })) }), _jsx("label", { htmlFor: `${id}-format`, children: "Number format" }), _jsx("select", { id: `${id}-format`, value: values.format, onChange: (event) => set({ format: event.target.value }), children: NOTE_FORMATS.map(([value, label]) => (_jsx("option", { value: value, children: label }, value))) }), _jsx("label", { htmlFor: `${id}-start`, children: "Start at" }), _jsx("input", { id: `${id}-start`, type: "number", min: 1, value: restarts ? "1" : start, disabled: restarts, "aria-invalid": invalid || undefined, onChange: (event) => setStart(event.target.value) }), _jsx("label", { htmlFor: `${id}-restart`, children: "Numbering" }), _jsxs("select", { id: `${id}-restart`, value: values.restart, onChange: (event) => set({ restart: event.target.value }), children: [_jsx("option", { value: "continuous", children: "Continuous" }), _jsx("option", { value: "eachSect", children: "Restart each section" }), kind === "footnote" && _jsx("option", { value: "eachPage", children: "Restart each page" })] }), _jsx("button", { type: "submit", children: "Apply" }), _jsx("button", { type: "button", onClick: () => onClose(false), children: "Cancel" }), invalid && (_jsx("p", { className: "foli-link-error", role: "alert", children: "The start must be a whole number from 1." }))] }) }));
|
|
1397
|
+
}
|
|
1398
|
+
/** A list's items as the dialog shows them: one a line, `shown | value` where they differ. */
|
|
1399
|
+
const itemsText = (items) => (items ?? [])
|
|
1400
|
+
.map((item) => item.display === undefined || item.display === item.value
|
|
1401
|
+
? item.value
|
|
1402
|
+
: `${item.display} | ${item.value}`)
|
|
1403
|
+
.join("\n");
|
|
1404
|
+
function parseItems(text) {
|
|
1405
|
+
const seen = new Set();
|
|
1406
|
+
return text.split("\n").flatMap((line) => {
|
|
1407
|
+
const [shown = "", value = shown] = line.split("|").map((part) => part.trim());
|
|
1408
|
+
// Word refuses two items of one value.
|
|
1409
|
+
if (!shown || seen.has(value))
|
|
1410
|
+
return [];
|
|
1411
|
+
seen.add(value);
|
|
1412
|
+
return [{ value, display: shown }];
|
|
1413
|
+
});
|
|
1414
|
+
}
|
|
1415
|
+
/**
|
|
1416
|
+
* The properties of the control at the caret: title, tag, locks and what its kind has (list items,
|
|
1417
|
+
* date format, line breaks); OK and Cancel; Escape cancels.
|
|
1418
|
+
*/
|
|
1419
|
+
function ControlDialog({ editor, onClose, }) {
|
|
1420
|
+
const [selected] = useState(() => selectedControl(editor.state));
|
|
1421
|
+
const control = selected?.control;
|
|
1422
|
+
const [title, setTitle] = useState(control?.alias ?? "");
|
|
1423
|
+
const [tag, setTag] = useState(control?.tag ?? "");
|
|
1424
|
+
const [noDelete, setNoDelete] = useState(control?.lock === "sdtLocked" || control?.lock === "sdtContentLocked");
|
|
1425
|
+
const [noEdit, setNoEdit] = useState(control?.lock === "contentLocked" || control?.lock === "sdtContentLocked");
|
|
1426
|
+
const [items, setItems] = useState(itemsText(control?.items));
|
|
1427
|
+
const [dateFormat, setDateFormat] = useState(control?.dateFormat ?? "d MMMM yyyy");
|
|
1428
|
+
const [multiLine, setMultiLine] = useState(control?.multiLine === true);
|
|
1429
|
+
const first = useRef(null);
|
|
1430
|
+
const id = useId();
|
|
1431
|
+
useEffect(() => {
|
|
1432
|
+
first.current?.focus();
|
|
1433
|
+
}, []);
|
|
1434
|
+
if (!selected || !control)
|
|
1435
|
+
return null;
|
|
1436
|
+
const { kind } = control;
|
|
1437
|
+
const hasList = kind === "dropDown" || kind === "comboBox";
|
|
1438
|
+
const apply = (event) => {
|
|
1439
|
+
event.preventDefault();
|
|
1440
|
+
const lock = noDelete
|
|
1441
|
+
? noEdit
|
|
1442
|
+
? "sdtContentLocked"
|
|
1443
|
+
: "sdtLocked"
|
|
1444
|
+
: noEdit
|
|
1445
|
+
? "contentLocked"
|
|
1446
|
+
: "unlocked";
|
|
1447
|
+
onClose(editor.run(setControlProperties(selected.id, {
|
|
1448
|
+
title,
|
|
1449
|
+
tag,
|
|
1450
|
+
lock,
|
|
1451
|
+
...(hasList ? { items: parseItems(items) } : {}),
|
|
1452
|
+
...(kind === "date" ? { dateFormat } : {}),
|
|
1453
|
+
...(kind === "text" ? { multiLine } : {}),
|
|
1454
|
+
})));
|
|
1455
|
+
};
|
|
1456
|
+
return (_jsx("div", { role: "dialog", "aria-label": "Content control properties", className: "foli-link-dialog foli-table-dialog", onKeyDown: (event) => {
|
|
1457
|
+
if (event.key !== "Escape")
|
|
1458
|
+
return;
|
|
1459
|
+
event.preventDefault();
|
|
1460
|
+
onClose(false);
|
|
1461
|
+
}, children: _jsxs("form", { onSubmit: apply, noValidate: true, children: [_jsx("label", { htmlFor: `${id}-title`, children: "Title" }), _jsx("input", { ref: first, id: `${id}-title`, value: title, onChange: (event) => setTitle(event.target.value) }), _jsx("label", { htmlFor: `${id}-tag`, children: "Tag" }), _jsx("input", { id: `${id}-tag`, value: tag, onChange: (event) => setTag(event.target.value) }), _jsxs("label", { children: [_jsx("input", { type: "checkbox", checked: noDelete, onChange: (event) => setNoDelete(event.target.checked) }), "The control cannot be deleted"] }), _jsxs("label", { children: [_jsx("input", { type: "checkbox", checked: noEdit, onChange: (event) => setNoEdit(event.target.checked) }), "The contents cannot be edited"] }), hasList && (_jsxs(_Fragment, { children: [_jsx("label", { htmlFor: `${id}-items`, children: "List items, one per line (shown | value)" }), _jsx("textarea", { id: `${id}-items`, rows: 4, value: items, onChange: (event) => setItems(event.target.value) })] })), kind === "date" && (_jsxs(_Fragment, { children: [_jsx("label", { htmlFor: `${id}-format`, children: "Date format" }), _jsx("input", { id: `${id}-format`, value: dateFormat, onChange: (event) => setDateFormat(event.target.value) })] })), kind === "text" && (_jsxs("label", { children: [_jsx("input", { type: "checkbox", checked: multiLine, onChange: (event) => setMultiLine(event.target.checked) }), "Allow line breaks"] })), _jsx("button", { type: "submit", children: "OK" }), _jsx("button", { type: "button", onClick: () => onClose(false), children: "Cancel" })] }) }));
|
|
1462
|
+
}
|
|
1463
|
+
const TWIPS_PER_CM = 1440 / 2.54;
|
|
1464
|
+
const MAX_TWIPS = 31_680;
|
|
1465
|
+
/** Word's limit on a section's columns. */
|
|
1466
|
+
const MAX_SECTION_COLUMNS = 45;
|
|
1467
|
+
const toCm = (twips) => String(Number((twips / TWIPS_PER_CM).toFixed(2)));
|
|
1468
|
+
/** A length in cm as whole twips (a decimal comma too), or null when it is not one in range. */
|
|
1469
|
+
function toTwips(text, min) {
|
|
1470
|
+
const value = Number(text.trim().replace(",", "."));
|
|
1471
|
+
const twips = Math.round(value * TWIPS_PER_CM);
|
|
1472
|
+
return text.trim() && Number.isFinite(value) && twips >= min && twips <= MAX_TWIPS ? twips : null;
|
|
1473
|
+
}
|
|
1474
|
+
const SECTION_STARTS = [
|
|
1475
|
+
["nextPage", "Next page"],
|
|
1476
|
+
["continuous", "Continuous"],
|
|
1477
|
+
["evenPage", "Even page"],
|
|
1478
|
+
["oddPage", "Odd page"],
|
|
1479
|
+
["nextColumn", "Next column"],
|
|
1480
|
+
];
|
|
1481
|
+
const SETUP_LABELS = {
|
|
1482
|
+
width: "Page width (cm)",
|
|
1483
|
+
height: "Page height (cm)",
|
|
1484
|
+
top: "Top margin (cm)",
|
|
1485
|
+
bottom: "Bottom margin (cm)",
|
|
1486
|
+
left: "Left margin (cm)",
|
|
1487
|
+
right: "Right margin (cm)",
|
|
1488
|
+
count: "Columns",
|
|
1489
|
+
space: "Space between columns (cm)",
|
|
1490
|
+
widths: "Set column widths (cm, separated by ;)",
|
|
1491
|
+
};
|
|
1492
|
+
/**
|
|
1493
|
+
* The page setup dialog, for the caret's section: how it starts, its page size and orientation,
|
|
1494
|
+
* margins and columns (equal, or set widths; the line between them), in centimetres. Only what
|
|
1495
|
+
* was changed is applied. Apply and Cancel; Escape cancels; a wrong field is marked and announced.
|
|
1496
|
+
*/
|
|
1497
|
+
function PageSetupDialog({ editor, onClose, }) {
|
|
1498
|
+
const id = useId();
|
|
1499
|
+
const [initial] = useState(() => {
|
|
1500
|
+
const setup = sectionAt(editor.state)?.setup ?? {};
|
|
1501
|
+
const width = setup.pageSize?.width ?? 12_240;
|
|
1502
|
+
const height = setup.pageSize?.height ?? 15_840;
|
|
1503
|
+
const m = setup.margins ?? {};
|
|
1504
|
+
const set = setup.columns?.equalWidth === false ? (setup.columns.widths ?? []) : [];
|
|
1505
|
+
return {
|
|
1506
|
+
start: setup.type ?? "nextPage",
|
|
1507
|
+
orientation: setup.pageSize?.orientation ?? (width > height ? "landscape" : "portrait"),
|
|
1508
|
+
separator: setup.columns?.separator === true,
|
|
1509
|
+
columns: setup.columns,
|
|
1510
|
+
fields: {
|
|
1511
|
+
width: toCm(width),
|
|
1512
|
+
height: toCm(height),
|
|
1513
|
+
top: toCm(Math.abs(m.top ?? 1440)),
|
|
1514
|
+
bottom: toCm(Math.abs(m.bottom ?? 1440)),
|
|
1515
|
+
left: toCm(m.left ?? 1440),
|
|
1516
|
+
right: toCm(m.right ?? 1440),
|
|
1517
|
+
count: String(set.length || (setup.columns?.count ?? 1)),
|
|
1518
|
+
space: toCm(setup.columns?.space ?? 720),
|
|
1519
|
+
widths: set.map((column) => toCm(column.width)).join("; "),
|
|
1520
|
+
},
|
|
1521
|
+
};
|
|
1522
|
+
});
|
|
1523
|
+
const [start, setStart] = useState(initial.start);
|
|
1524
|
+
const [orientation, setOrientation] = useState(initial.orientation);
|
|
1525
|
+
const [separator, setSeparator] = useState(initial.separator);
|
|
1526
|
+
const [fields, setFields] = useState(initial.fields);
|
|
1527
|
+
const [error, setError] = useState(null);
|
|
1528
|
+
const first = useRef(null);
|
|
1529
|
+
useEffect(() => first.current?.focus(), []);
|
|
1530
|
+
const apply = (event) => {
|
|
1531
|
+
event.preventDefault();
|
|
1532
|
+
const wrong = (field) => {
|
|
1533
|
+
setError(field);
|
|
1534
|
+
document.getElementById(`${id}-${field}`)?.focus();
|
|
1535
|
+
};
|
|
1536
|
+
const changed = (field) => fields[field] !== initial.fields[field];
|
|
1537
|
+
const setup = {};
|
|
1538
|
+
if (start !== initial.start)
|
|
1539
|
+
setup.type = start === "nextPage" ? undefined : start;
|
|
1540
|
+
const lengths = {};
|
|
1541
|
+
for (const field of ["width", "height", "top", "bottom", "left", "right"]) {
|
|
1542
|
+
if (!changed(field))
|
|
1543
|
+
continue;
|
|
1544
|
+
const twips = toTwips(fields[field], field === "width" || field === "height" ? 1 : 0);
|
|
1545
|
+
if (twips === null)
|
|
1546
|
+
return wrong(field);
|
|
1547
|
+
lengths[field] = twips;
|
|
1548
|
+
}
|
|
1549
|
+
if (lengths.width !== undefined ||
|
|
1550
|
+
lengths.height !== undefined ||
|
|
1551
|
+
orientation !== initial.orientation)
|
|
1552
|
+
setup.pageSize = {
|
|
1553
|
+
...(lengths.width !== undefined ? { width: lengths.width } : {}),
|
|
1554
|
+
...(lengths.height !== undefined ? { height: lengths.height } : {}),
|
|
1555
|
+
orientation: orientation,
|
|
1556
|
+
};
|
|
1557
|
+
const margins = {
|
|
1558
|
+
top: lengths.top,
|
|
1559
|
+
bottom: lengths.bottom,
|
|
1560
|
+
left: lengths.left,
|
|
1561
|
+
right: lengths.right,
|
|
1562
|
+
};
|
|
1563
|
+
const given = Object.entries(margins).filter(([, value]) => value !== undefined);
|
|
1564
|
+
if (given.length)
|
|
1565
|
+
setup.margins = Object.fromEntries(given);
|
|
1566
|
+
if (changed("count") ||
|
|
1567
|
+
changed("space") ||
|
|
1568
|
+
changed("widths") ||
|
|
1569
|
+
separator !== initial.separator) {
|
|
1570
|
+
// What was not changed stays exactly as it is, each column's own space included.
|
|
1571
|
+
const before = initial.columns;
|
|
1572
|
+
const set = before?.equalWidth === false ? (before.widths ?? []) : [];
|
|
1573
|
+
const space = changed("space") ? toTwips(fields.space, 0) : (before?.space ?? 720);
|
|
1574
|
+
if (space === null)
|
|
1575
|
+
return wrong("space");
|
|
1576
|
+
let widths = set.map((column) => column.width);
|
|
1577
|
+
if (changed("widths"))
|
|
1578
|
+
widths = fields.widths.trim()
|
|
1579
|
+
? fields.widths
|
|
1580
|
+
.trim()
|
|
1581
|
+
.split(/[;\s]+/)
|
|
1582
|
+
.map((width) => toTwips(width, 1))
|
|
1583
|
+
: [];
|
|
1584
|
+
if (widths.includes(null))
|
|
1585
|
+
return wrong("widths");
|
|
1586
|
+
const asked = Number(fields.count);
|
|
1587
|
+
if (widths.length && changed("count") && asked !== widths.length) {
|
|
1588
|
+
setError("mismatch");
|
|
1589
|
+
document.getElementById(`${id}-count`)?.focus();
|
|
1590
|
+
return;
|
|
1591
|
+
}
|
|
1592
|
+
const count = widths.length || asked;
|
|
1593
|
+
if (!Number.isInteger(count) || count < 1 || count > MAX_SECTION_COLUMNS)
|
|
1594
|
+
return wrong("count");
|
|
1595
|
+
const { separator: _line, equalWidth: _equal, widths: _widths, ...rest } = before ?? {};
|
|
1596
|
+
setup.columns = {
|
|
1597
|
+
...rest,
|
|
1598
|
+
count,
|
|
1599
|
+
...(changed("space") || before?.space === undefined ? { space } : {}),
|
|
1600
|
+
...(separator ? { separator } : {}),
|
|
1601
|
+
...(widths.length
|
|
1602
|
+
? {
|
|
1603
|
+
equalWidth: false,
|
|
1604
|
+
widths: widths.map((width, i) => {
|
|
1605
|
+
const own = changed("space") ? undefined : set[i]?.space;
|
|
1606
|
+
const gap = own ?? (i < widths.length - 1 ? space : undefined);
|
|
1607
|
+
return { width: width, ...(gap === undefined ? {} : { space: gap }) };
|
|
1608
|
+
}),
|
|
1609
|
+
}
|
|
1610
|
+
: {}),
|
|
1611
|
+
};
|
|
1612
|
+
}
|
|
1613
|
+
if (!Object.keys(setup).length)
|
|
1614
|
+
return onClose(false);
|
|
1615
|
+
if (editor.run(setSectionSetup(setup)))
|
|
1616
|
+
return onClose(true);
|
|
1617
|
+
setError("setup");
|
|
1618
|
+
};
|
|
1619
|
+
const setOrientationSwapping = (next) => {
|
|
1620
|
+
if (next !== orientation)
|
|
1621
|
+
setFields((current) => ({ ...current, width: current.height, height: current.width }));
|
|
1622
|
+
setOrientation(next);
|
|
1623
|
+
setError(null);
|
|
1624
|
+
};
|
|
1625
|
+
const marked = (name) => error === name || (name === "count" && error === "mismatch");
|
|
1626
|
+
const field = (name) => (_jsxs("div", { className: "foli-setup-field", children: [_jsx("label", { htmlFor: `${id}-${name}`, children: SETUP_LABELS[name] }), _jsx("input", { id: `${id}-${name}`, type: "text", inputMode: name === "widths" ? "text" : "decimal", value: fields[name], "aria-invalid": marked(name) || undefined, "aria-describedby": marked(name) ? `${id}-error` : undefined, onChange: (event) => {
|
|
1627
|
+
setFields((current) => ({ ...current, [name]: event.target.value }));
|
|
1628
|
+
setError(null);
|
|
1629
|
+
} })] }, name));
|
|
1630
|
+
return (_jsx("div", { role: "dialog", "aria-label": "Page setup", className: "foli-link-dialog foli-setup-dialog", onKeyDown: (event) => {
|
|
1631
|
+
if (event.key !== "Escape")
|
|
1632
|
+
return;
|
|
1633
|
+
event.preventDefault();
|
|
1634
|
+
onClose(false);
|
|
1635
|
+
}, children: _jsxs("form", { onSubmit: apply, noValidate: true, children: [_jsxs("div", { className: "foli-setup-field", children: [_jsx("label", { htmlFor: `${id}-start`, children: "Section starts" }), _jsx("select", { ref: first, id: `${id}-start`, value: start, onChange: (event) => setStart(event.target.value), children: SECTION_STARTS.map(([value, label]) => (_jsx("option", { value: value, children: label }, value))) })] }), _jsxs("div", { className: "foli-setup-field", children: [_jsx("label", { htmlFor: `${id}-orientation`, children: "Orientation" }), _jsxs("select", { id: `${id}-orientation`, value: orientation, onChange: (event) => setOrientationSwapping(event.target.value), children: [_jsx("option", { value: "portrait", children: "Portrait" }), _jsx("option", { value: "landscape", children: "Landscape" })] })] }), ["width", "height", "top", "bottom", "left", "right", "count", "space"].map(field), _jsxs("div", { className: "foli-setup-field foli-setup-check", children: [_jsx("input", { id: `${id}-separator`, type: "checkbox", checked: separator, onChange: (event) => setSeparator(event.target.checked) }), _jsx("label", { htmlFor: `${id}-separator`, children: "Line between columns" })] }), field("widths"), _jsx("button", { type: "submit", children: "Apply" }), _jsx("button", { type: "button", onClick: () => onClose(false), children: "Cancel" }), error && (_jsx("p", { id: `${id}-error`, className: "foli-link-error", role: "alert", children: error === "setup"
|
|
1636
|
+
? "This setup leaves no room for text, or its columns do not fit the page."
|
|
1637
|
+
: error === "mismatch"
|
|
1638
|
+
? "The number of columns differs from the set widths: list one width per column, or clear them for equal columns."
|
|
1639
|
+
: `Check this value: ${SETUP_LABELS[error]}.` }))] }) }));
|
|
1640
|
+
}
|
|
1641
|
+
/** The largest picture side the schema takes, in px (2^31 - 1 EMU). */
|
|
1642
|
+
const MAX_PICTURE_PX = Math.floor(2_147_483_647 / EMU_PER_PX);
|
|
1643
|
+
/**
|
|
1644
|
+
* The picture dialog. Inserting (`pos` null): an image file, its alt text (required unless the
|
|
1645
|
+
* picture is marked decorative), Insert and Cancel; the file goes through `upload`, the picture
|
|
1646
|
+
* in at the file's natural size no wider than the page's text. Editing the picture at `pos`:
|
|
1647
|
+
* its alt text, decorative, and its width and height in px (proportions kept unless unchecked;
|
|
1648
|
+
* not while tracking changes), Apply and Cancel. Escape cancels; a field that is wrong is marked
|
|
1649
|
+
* and announced.
|
|
1650
|
+
*/
|
|
1651
|
+
function PictureDialog({ editor, pos, upload, onClose, }) {
|
|
1652
|
+
const picture = pos === null ? null : editor.state.doc.nodeAt(pos);
|
|
1653
|
+
const attrs = picture?.attrs ?? {};
|
|
1654
|
+
const px = (emu) => String(Math.round(Number(emu) / EMU_PER_PX));
|
|
1655
|
+
const [file, setFile] = useState(null);
|
|
1656
|
+
const [alt, setAlt] = useState(String(attrs.alt ?? ""));
|
|
1657
|
+
const [decorative, setDecorative] = useState(attrs.decorative === true);
|
|
1658
|
+
const [width, setWidth] = useState(picture ? px(attrs.width) : "");
|
|
1659
|
+
const [height, setHeight] = useState(picture ? px(attrs.height) : "");
|
|
1660
|
+
const [keep, setKeep] = useState(true);
|
|
1661
|
+
const [error, setError] = useState(null);
|
|
1662
|
+
const [busy, setBusy] = useState(false);
|
|
1663
|
+
const fileField = useRef(null);
|
|
1664
|
+
const altField = useRef(null);
|
|
1665
|
+
const decorativeField = useRef(null);
|
|
1666
|
+
const widthField = useRef(null);
|
|
1667
|
+
const id = useId();
|
|
1668
|
+
const tracking = trackState(editor.state)?.on === true;
|
|
1669
|
+
const ratio = picture ? Number(attrs.height) / Number(attrs.width) : 1;
|
|
1670
|
+
// The first field that can take the focus (a decorative picture's alt text cannot).
|
|
1671
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: focus once, on open.
|
|
1672
|
+
useEffect(() => {
|
|
1673
|
+
(pos === null
|
|
1674
|
+
? fileField.current
|
|
1675
|
+
: decorative
|
|
1676
|
+
? decorativeField.current
|
|
1677
|
+
: altField.current)?.focus();
|
|
1678
|
+
}, []);
|
|
1679
|
+
// A dialog closed while an upload runs: what the upload finds then is not inserted. Set in
|
|
1680
|
+
// the effect too: StrictMode runs effects twice (setup, cleanup, setup).
|
|
1681
|
+
const open = useRef(true);
|
|
1682
|
+
useEffect(() => {
|
|
1683
|
+
open.current = true;
|
|
1684
|
+
return () => {
|
|
1685
|
+
open.current = false;
|
|
1686
|
+
};
|
|
1687
|
+
}, []);
|
|
1688
|
+
const messages = {
|
|
1689
|
+
file: "Choose an image file.",
|
|
1690
|
+
alt: "Describe the picture, or mark it as decorative.",
|
|
1691
|
+
size: `Enter a width and height in pixels, from 1 to ${MAX_PICTURE_PX}.`,
|
|
1692
|
+
failed: "The picture could not be inserted: the file could not be read or uploaded here.",
|
|
1693
|
+
};
|
|
1694
|
+
const invalid = (field) => (error === field ? true : undefined);
|
|
1695
|
+
const described = (field) => (error === field ? `${id}-error` : undefined);
|
|
1696
|
+
/** Marks `field` wrong and puts the focus on it. */
|
|
1697
|
+
const fail = (field) => {
|
|
1698
|
+
setError(field);
|
|
1699
|
+
const target = {
|
|
1700
|
+
file: fileField,
|
|
1701
|
+
alt: altField,
|
|
1702
|
+
size: widthField,
|
|
1703
|
+
failed: fileField,
|
|
1704
|
+
}[field].current;
|
|
1705
|
+
target?.focus();
|
|
1706
|
+
};
|
|
1707
|
+
const submit = async (event) => {
|
|
1708
|
+
event.preventDefault();
|
|
1709
|
+
if (busy)
|
|
1710
|
+
return;
|
|
1711
|
+
if (!decorative && !alt.trim()) {
|
|
1712
|
+
fail("alt");
|
|
1713
|
+
return;
|
|
1714
|
+
}
|
|
1715
|
+
if (pos !== null) {
|
|
1716
|
+
// The picture is gone (the dialog closes on a change; this is a guard).
|
|
1717
|
+
if (!picture) {
|
|
1718
|
+
onClose(false);
|
|
1719
|
+
return;
|
|
1720
|
+
}
|
|
1721
|
+
const w = Math.round(Number(width));
|
|
1722
|
+
const h = Math.round(Number(height));
|
|
1723
|
+
const sizeChanged = w !== Number(px(attrs.width)) || h !== Number(px(attrs.height));
|
|
1724
|
+
const valid = (n) => Number.isFinite(n) && n >= 1 && n <= MAX_PICTURE_PX;
|
|
1725
|
+
if (sizeChanged && !tracking && !(valid(w) && valid(h))) {
|
|
1726
|
+
fail("size");
|
|
1727
|
+
return;
|
|
1728
|
+
}
|
|
1729
|
+
editor.run(setPictureAlt(pos, alt, decorative));
|
|
1730
|
+
if (sizeChanged && !tracking)
|
|
1731
|
+
editor.run(setPictureSize(pos, Math.round(w * EMU_PER_PX), Math.round(h * EMU_PER_PX)));
|
|
1732
|
+
onClose(true);
|
|
1733
|
+
return;
|
|
1734
|
+
}
|
|
1735
|
+
if (!file || !upload || !file.type.startsWith("image/")) {
|
|
1736
|
+
fail("file");
|
|
1737
|
+
return;
|
|
1738
|
+
}
|
|
1739
|
+
setBusy(true);
|
|
1740
|
+
try {
|
|
1741
|
+
const size = await measureImageFile(file, document);
|
|
1742
|
+
const answer = size ? await upload(file) : null;
|
|
1743
|
+
const src = typeof answer === "string" ? answer : answer?.src;
|
|
1744
|
+
if (!open.current)
|
|
1745
|
+
return;
|
|
1746
|
+
if (!size || !src)
|
|
1747
|
+
throw new Error("no image");
|
|
1748
|
+
const scale = Math.min(1, textWidthEmu(editor.state) / (size.width * EMU_PER_PX));
|
|
1749
|
+
const type = /^data:([^;,]+)/i.exec(src)?.[1]?.toLowerCase() ?? file.type;
|
|
1750
|
+
const done = editor.run(insertPicture({
|
|
1751
|
+
contentType: type,
|
|
1752
|
+
...(src.startsWith("data:") ? { data: src } : { url: src }),
|
|
1753
|
+
width: Math.max(1, Math.round(size.width * EMU_PER_PX * scale)),
|
|
1754
|
+
height: Math.max(1, Math.round(size.height * EMU_PER_PX * scale)),
|
|
1755
|
+
...(decorative ? { decorative: true } : { alt }),
|
|
1756
|
+
}));
|
|
1757
|
+
if (!done)
|
|
1758
|
+
throw new Error("not inserted");
|
|
1759
|
+
onClose(true);
|
|
1760
|
+
}
|
|
1761
|
+
catch {
|
|
1762
|
+
if (!open.current)
|
|
1763
|
+
return;
|
|
1764
|
+
setBusy(false);
|
|
1765
|
+
fail("failed");
|
|
1766
|
+
}
|
|
1767
|
+
};
|
|
1768
|
+
const changeWidth = (value) => {
|
|
1769
|
+
setWidth(value);
|
|
1770
|
+
setError(null);
|
|
1771
|
+
if (keep && Number(value) > 0)
|
|
1772
|
+
setHeight(String(Math.max(1, Math.round(Number(value) * ratio))));
|
|
1773
|
+
};
|
|
1774
|
+
const changeHeight = (value) => {
|
|
1775
|
+
setHeight(value);
|
|
1776
|
+
setError(null);
|
|
1777
|
+
if (keep && Number(value) > 0)
|
|
1778
|
+
setWidth(String(Math.max(1, Math.round(Number(value) / ratio))));
|
|
1779
|
+
};
|
|
1780
|
+
const sizeNote = tracking ? `${id}-tracking` : undefined;
|
|
1781
|
+
const sizeDescribed = [described("size"), sizeNote].filter(Boolean).join(" ") || undefined;
|
|
1782
|
+
return (_jsx("div", { role: "dialog", "aria-label": pos === null ? "Insert picture" : "Edit picture", className: "foli-link-dialog foli-picture-dialog", "aria-busy": busy || undefined, onKeyDown: (event) => {
|
|
1783
|
+
if (event.key !== "Escape")
|
|
1784
|
+
return;
|
|
1785
|
+
event.preventDefault();
|
|
1786
|
+
onClose(false);
|
|
1787
|
+
}, children: _jsxs("form", { onSubmit: submit, noValidate: true, children: [pos === null && (_jsxs(_Fragment, { children: [_jsx("label", { htmlFor: `${id}-file`, children: "Image" }), _jsx("input", { ref: fileField, id: `${id}-file`, type: "file", accept: "image/*", required: true, "aria-invalid": invalid("file"), "aria-describedby": described("file"), onChange: (event) => {
|
|
1788
|
+
setFile(event.target.files?.[0] ?? null);
|
|
1789
|
+
setError(null);
|
|
1790
|
+
} })] })), _jsx("label", { htmlFor: `${id}-alt`, children: "Alt text" }), _jsx("input", { ref: altField, id: `${id}-alt`, type: "text", maxLength: 65_535, value: decorative ? "" : alt, disabled: decorative, required: !decorative, "aria-invalid": invalid("alt"), "aria-describedby": described("alt"), onChange: (event) => {
|
|
1791
|
+
setAlt(event.target.value);
|
|
1792
|
+
setError(null);
|
|
1793
|
+
} }), _jsxs("label", { children: [_jsx("input", { ref: decorativeField, type: "checkbox", checked: decorative, onChange: (event) => {
|
|
1794
|
+
setDecorative(event.target.checked);
|
|
1795
|
+
setError(null);
|
|
1796
|
+
} }), "Decorative (no alt text)"] }), pos !== null && (_jsxs(_Fragment, { children: [_jsx("label", { htmlFor: `${id}-width`, children: "Width (px)" }), _jsx("input", { ref: widthField, id: `${id}-width`, type: "number", min: 1, max: MAX_PICTURE_PX, step: 1, value: width, disabled: tracking, "aria-invalid": invalid("size"), "aria-describedby": sizeDescribed, onChange: (event) => changeWidth(event.target.value) }), _jsx("label", { htmlFor: `${id}-height`, children: "Height (px)" }), _jsx("input", { id: `${id}-height`, type: "number", min: 1, max: MAX_PICTURE_PX, step: 1, value: height, disabled: tracking, "aria-invalid": invalid("size"), "aria-describedby": sizeDescribed, onChange: (event) => changeHeight(event.target.value) }), _jsxs("label", { children: [_jsx("input", { type: "checkbox", checked: keep, disabled: tracking, "aria-describedby": sizeNote, onChange: (event) => setKeep(event.target.checked) }), "Keep proportions"] }), tracking && _jsx("p", { id: sizeNote, children: "The size does not change while changes are tracked." })] })), _jsx("button", { type: "submit", "aria-disabled": busy || undefined, children: pos === null ? "Insert" : "Apply" }), _jsx("button", { type: "button", onClick: () => onClose(false), children: "Cancel" }), _jsx("p", { role: "status", className: "foli-picture-status", children: busy ? "Inserting the picture…" : "" }), error && (_jsx("p", { id: `${id}-error`, className: "foli-link-error", role: "alert", children: messages[error] }))] }) }));
|
|
1797
|
+
}
|
|
588
1798
|
//# sourceMappingURL=FoliToolbar.js.map
|