@folijs/react 0.6.0 → 1.0.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 +144 -0
- package/README.md +11 -7
- package/dist/FoliComments.d.ts.map +1 -1
- package/dist/FoliComments.js +5 -1
- package/dist/FoliComments.js.map +1 -1
- package/dist/FoliEditor.d.ts +17 -4
- package/dist/FoliEditor.d.ts.map +1 -1
- package/dist/FoliEditor.js +15 -8
- package/dist/FoliEditor.js.map +1 -1
- package/dist/FoliNavigation.d.ts.map +1 -1
- package/dist/FoliNavigation.js +2 -1
- package/dist/FoliNavigation.js.map +1 -1
- package/dist/FoliToolbar.d.ts +81 -6
- package/dist/FoliToolbar.d.ts.map +1 -1
- package/dist/FoliToolbar.js +100 -66
- package/dist/FoliToolbar.js.map +1 -1
- package/dist/editor.d.ts +38 -12
- package/dist/editor.d.ts.map +1 -1
- package/dist/editor.js +57 -3
- package/dist/editor.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/src/FoliComments.tsx +3 -1
- package/src/FoliEditor.tsx +39 -17
- package/src/FoliNavigation.tsx +1 -2
- package/src/FoliToolbar.tsx +248 -91
- package/src/editor.tsx +127 -4
- package/src/index.ts +14 -1
package/src/FoliToolbar.tsx
CHANGED
|
@@ -100,8 +100,8 @@ import {
|
|
|
100
100
|
undo,
|
|
101
101
|
undoDepth,
|
|
102
102
|
updateFields,
|
|
103
|
-
wordContext,
|
|
104
103
|
} from "@folijs/core";
|
|
104
|
+
import { wordContext } from "@folijs/core/internal";
|
|
105
105
|
import { type EditorViewOptions, EMU_PER_PX, measureImageFile, textWidthEmu } from "@folijs/view";
|
|
106
106
|
import {
|
|
107
107
|
type FormEvent,
|
|
@@ -115,7 +115,13 @@ import {
|
|
|
115
115
|
useRef,
|
|
116
116
|
useState,
|
|
117
117
|
} from "react";
|
|
118
|
-
import {
|
|
118
|
+
import {
|
|
119
|
+
development,
|
|
120
|
+
sameItems,
|
|
121
|
+
useActiveEditor,
|
|
122
|
+
useEditorOrContext,
|
|
123
|
+
useEditorState,
|
|
124
|
+
} from "./editor.js";
|
|
119
125
|
|
|
120
126
|
export interface FoliToolbarProps {
|
|
121
127
|
/** Default: the editor of the nearest `FoliEditorProvider`. */
|
|
@@ -142,29 +148,82 @@ export interface FoliToolbarProps {
|
|
|
142
148
|
* inserts one from a file; without it, it only edits a selected picture.
|
|
143
149
|
*/
|
|
144
150
|
upload?: EditorViewOptions["upload"] | undefined;
|
|
151
|
+
/**
|
|
152
|
+
* The items, in groups (a separator between groups), in place of `defaultToolbarItems`: the
|
|
153
|
+
* built-in ones from `toolbarItems`, and the app's own. "New comment" shows only with
|
|
154
|
+
* `comments`; an empty group is left out.
|
|
155
|
+
*/
|
|
156
|
+
items?: readonly (readonly ToolbarItem[])[] | undefined;
|
|
145
157
|
className?: string | undefined;
|
|
146
158
|
}
|
|
147
159
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
160
|
+
/** What every toolbar item has. */
|
|
161
|
+
interface ToolbarItemBase {
|
|
162
|
+
/** Unique in the toolbar; also its class, `foli-toolbar-<key>`. */
|
|
163
|
+
readonly key: string;
|
|
164
|
+
/** What it does: the tooltip, and the accessible name when an `icon` stands for the text. */
|
|
165
|
+
readonly label: string;
|
|
166
|
+
/** Visible text, which is then the accessible name (default `label`). */
|
|
167
|
+
readonly text?: string | undefined;
|
|
168
|
+
/** Shown instead of the text (a symbol or an SVG): `label` is then the accessible name. */
|
|
169
|
+
readonly icon?: ReactNode | undefined;
|
|
170
|
+
/**
|
|
171
|
+
* Whether it can be used now. Default: a button's dry run of its `command` (`editor.can`); for
|
|
172
|
+
* a menu, whether it has entries. Runs after every transaction: keep it cheap.
|
|
173
|
+
*/
|
|
174
|
+
readonly enabled?: ((state: EditorState, editor: Editor) => boolean) | undefined;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** A toolbar button that runs a command. */
|
|
178
|
+
export interface ToolbarButton extends ToolbarItemBase {
|
|
179
|
+
/**
|
|
180
|
+
* What a press runs, on the editor of the header, footer or note open on its page, else the
|
|
181
|
+
* body's (as the built-in buttons do).
|
|
182
|
+
*/
|
|
183
|
+
readonly command: Command;
|
|
184
|
+
/** A toggle: whether it shows as pressed (`aria-pressed`). Runs after every transaction. */
|
|
185
|
+
readonly pressed?: ((state: EditorState, editor: Editor) => boolean) | undefined;
|
|
186
|
+
readonly actions?: undefined;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** A toolbar menu button: a WAI-ARIA menu of its `actions`. */
|
|
190
|
+
export interface ToolbarMenu extends ToolbarItemBase {
|
|
159
191
|
/**
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
* run: it runs after every transaction, and a dry run builds the whole transaction (for a
|
|
163
|
-
* block type over a large selection, quadratic in the number of blocks).
|
|
192
|
+
* Its entries, in order, or a function of the body's editor making them (as the built-in
|
|
193
|
+
* menus do). Each entry is dry-run when the menu opens; without entries the button is disabled.
|
|
164
194
|
*/
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
195
|
+
readonly actions: readonly ToolbarAction[] | ((editor: Editor) => readonly ToolbarAction[]);
|
|
196
|
+
readonly command?: undefined;
|
|
197
|
+
readonly pressed?: undefined;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* A toolbar item, built-in (`toolbarItems`) or the app's own: a button running a command, or a
|
|
202
|
+
* menu button. The toolbar keeps one tab stop for all its items (arrow keys, Home and End move
|
|
203
|
+
* between them), gives toggles `aria-pressed` and an item that cannot apply now `aria-disabled`
|
|
204
|
+
* (still focusable), and leaves the focus and selection in the document on click.
|
|
205
|
+
*/
|
|
206
|
+
export type ToolbarItem = ToolbarButton | ToolbarMenu;
|
|
207
|
+
|
|
208
|
+
/** An entry of a toolbar menu (a `menuitem`, or a `menuitemcheckbox` when it has `checked`). */
|
|
209
|
+
export interface ToolbarAction {
|
|
210
|
+
/** Unique in its menu. */
|
|
211
|
+
readonly key: string;
|
|
212
|
+
readonly label: string;
|
|
213
|
+
/** What it runs; a dry run when the menu opens decides whether it is enabled. */
|
|
214
|
+
readonly command: Command;
|
|
215
|
+
/** A checkable entry: whether it is checked (with the same arguments as `pressed`). */
|
|
216
|
+
readonly checked?: ((state: EditorState, editor: Editor) => boolean) | undefined;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* A built-in item: boxes, color menus and dialogs are only the toolbar's own. Each has its own
|
|
221
|
+
* cheap `enabled`, not a dry run: that runs after every transaction, and a dry run builds the
|
|
222
|
+
* whole transaction (for a block type over a large selection, quadratic in the number of blocks).
|
|
223
|
+
*/
|
|
224
|
+
interface Item extends ToolbarItemBase {
|
|
225
|
+
command?: Command | undefined;
|
|
226
|
+
pressed?: ((state: EditorState, editor: Editor) => boolean) | undefined;
|
|
168
227
|
/** A menu button: its options, the current value, and what picking one runs. */
|
|
169
228
|
menu?: {
|
|
170
229
|
options: readonly ColorOption[];
|
|
@@ -172,7 +231,7 @@ interface Item {
|
|
|
172
231
|
pick: (color: string | null) => Command;
|
|
173
232
|
};
|
|
174
233
|
/** An action menu button (the table menu): its commands, in order. */
|
|
175
|
-
actions?: readonly Action[] | ((editor: Editor) => readonly Action[]);
|
|
234
|
+
actions?: readonly Action[] | ((editor: Editor) => readonly Action[]) | undefined;
|
|
176
235
|
/** A button opening a dialog (the picture's). */
|
|
177
236
|
dialog?: "picture";
|
|
178
237
|
/** A drop-down list (a `select`): its options, the current value, and what picking runs. */
|
|
@@ -183,14 +242,10 @@ interface Item {
|
|
|
183
242
|
};
|
|
184
243
|
}
|
|
185
244
|
|
|
186
|
-
/** A
|
|
187
|
-
interface Action {
|
|
188
|
-
|
|
189
|
-
label: string;
|
|
190
|
-
/** The command it runs; with `dialog`, whether it applies: the dialog does the rest. */
|
|
191
|
-
command: Command;
|
|
245
|
+
/** A built-in menu's entry: it may open a dialog or move the focus. */
|
|
246
|
+
interface Action extends ToolbarAction {
|
|
247
|
+
/** With `dialog`, `command` says whether it applies: the dialog does the rest. */
|
|
192
248
|
dialog?: "insertTable" | "pageSetup" | "crossReference" | "controlProperties" | "noteOptions";
|
|
193
|
-
checked?: (state: EditorState) => boolean;
|
|
194
249
|
/** It takes the focus where it belongs (into a story, or back to the body). */
|
|
195
250
|
movesFocus?: boolean;
|
|
196
251
|
}
|
|
@@ -550,24 +605,25 @@ const alignIcon = (bars: readonly [number, number][]) => (
|
|
|
550
605
|
</svg>
|
|
551
606
|
);
|
|
552
607
|
|
|
608
|
+
/** A paragraph takes every mark and a page break: no need for a dry run after each change. */
|
|
609
|
+
const always = () => true;
|
|
610
|
+
|
|
553
611
|
const align = (value: Alignment, label: string, bars: readonly [number, number][]): Item => ({
|
|
554
612
|
key: `align-${value}`,
|
|
555
613
|
label,
|
|
556
|
-
text: label,
|
|
557
|
-
symbol: true,
|
|
558
614
|
icon: alignIcon(bars),
|
|
559
615
|
pressed: (state) => alignAt(state) === value,
|
|
560
616
|
enabled: hasTextblock,
|
|
561
|
-
command:
|
|
617
|
+
command: setAlign(value),
|
|
562
618
|
});
|
|
563
619
|
|
|
564
|
-
const mark = (key: string, label: string,
|
|
620
|
+
const mark = (key: string, label: string, icon: string): Item => ({
|
|
565
621
|
key,
|
|
566
622
|
label,
|
|
567
|
-
|
|
568
|
-
symbol: true,
|
|
623
|
+
icon,
|
|
569
624
|
pressed: (_state, editor) => editor.isMarkActive(key),
|
|
570
|
-
|
|
625
|
+
enabled: always,
|
|
626
|
+
command: toggleMark(key),
|
|
571
627
|
});
|
|
572
628
|
|
|
573
629
|
const capitalized = (name: string) => name.charAt(0).toUpperCase() + name.slice(1);
|
|
@@ -650,13 +706,12 @@ const GROUPS: Item[][] = [
|
|
|
650
706
|
pressed: (state) => linkAt(state) !== null,
|
|
651
707
|
enabled: (state) => openLinkEditor(state),
|
|
652
708
|
// Asks for the link dialog, as Mod-K does: the toolbar listens for that request.
|
|
653
|
-
command:
|
|
709
|
+
command: openLinkEditor,
|
|
654
710
|
},
|
|
655
711
|
{
|
|
656
712
|
key: "textColor",
|
|
657
713
|
label: "Text color",
|
|
658
|
-
|
|
659
|
-
symbol: true,
|
|
714
|
+
icon: "A",
|
|
660
715
|
enabled: hasTextblock,
|
|
661
716
|
menu: {
|
|
662
717
|
options: TEXT_COLORS,
|
|
@@ -667,8 +722,7 @@ const GROUPS: Item[][] = [
|
|
|
667
722
|
{
|
|
668
723
|
key: "highlight",
|
|
669
724
|
label: "Highlight",
|
|
670
|
-
|
|
671
|
-
symbol: true,
|
|
725
|
+
icon: "H",
|
|
672
726
|
enabled: hasTextblock,
|
|
673
727
|
menu: {
|
|
674
728
|
options: HIGHLIGHTS,
|
|
@@ -722,37 +776,33 @@ const GROUPS: Item[][] = [
|
|
|
722
776
|
{
|
|
723
777
|
key: "bulletList",
|
|
724
778
|
label: "Bulleted list",
|
|
725
|
-
|
|
726
|
-
symbol: true,
|
|
779
|
+
icon: "•",
|
|
727
780
|
pressed: (state) => listKindAt(state) === "bulletList",
|
|
728
781
|
enabled: hasTextblock,
|
|
729
|
-
command:
|
|
782
|
+
command: toggleList("bulletList"),
|
|
730
783
|
},
|
|
731
784
|
{
|
|
732
785
|
key: "orderedList",
|
|
733
786
|
label: "Numbered list",
|
|
734
787
|
// A symbol, not "1.": visible text that is not part of the name fails WCAG 2.5.3.
|
|
735
|
-
|
|
736
|
-
symbol: true,
|
|
788
|
+
icon: "№",
|
|
737
789
|
pressed: (state) => listKindAt(state) === "orderedList",
|
|
738
790
|
enabled: hasTextblock,
|
|
739
|
-
command:
|
|
791
|
+
command: toggleList("orderedList"),
|
|
740
792
|
},
|
|
741
793
|
{
|
|
742
794
|
key: "outdent",
|
|
743
795
|
label: "Decrease indent",
|
|
744
|
-
|
|
745
|
-
symbol: true,
|
|
796
|
+
icon: "⇤",
|
|
746
797
|
enabled: canOutdent,
|
|
747
|
-
command:
|
|
798
|
+
command: changeIndent(-1),
|
|
748
799
|
},
|
|
749
800
|
{
|
|
750
801
|
key: "indent",
|
|
751
802
|
label: "Increase indent",
|
|
752
|
-
|
|
753
|
-
symbol: true,
|
|
803
|
+
icon: "⇥",
|
|
754
804
|
enabled: hasTextblock,
|
|
755
|
-
command:
|
|
805
|
+
command: changeIndent(1),
|
|
756
806
|
},
|
|
757
807
|
],
|
|
758
808
|
[
|
|
@@ -761,7 +811,7 @@ const GROUPS: Item[][] = [
|
|
|
761
811
|
label: "New comment on the selection",
|
|
762
812
|
text: "New comment",
|
|
763
813
|
enabled: (state) => wordContext(state) !== null && hasTextblock(state),
|
|
764
|
-
command:
|
|
814
|
+
command: addComment(""),
|
|
765
815
|
},
|
|
766
816
|
{
|
|
767
817
|
key: "trackChanges",
|
|
@@ -769,7 +819,7 @@ const GROUPS: Item[][] = [
|
|
|
769
819
|
text: "Track changes",
|
|
770
820
|
pressed: (state) => trackState(state)?.on === true,
|
|
771
821
|
enabled: (state) => trackState(state) !== null,
|
|
772
|
-
command: (state) => setTrackChanges(!trackState(state)?.on),
|
|
822
|
+
command: (state, dispatch) => setTrackChanges(!trackState(state)?.on)(state, dispatch),
|
|
773
823
|
},
|
|
774
824
|
{
|
|
775
825
|
key: "reviewView",
|
|
@@ -789,42 +839,42 @@ const GROUPS: Item[][] = [
|
|
|
789
839
|
label: "Previous change",
|
|
790
840
|
text: "Previous change",
|
|
791
841
|
enabled: (state) => trackState(state) !== null,
|
|
792
|
-
command:
|
|
842
|
+
command: previousChange,
|
|
793
843
|
},
|
|
794
844
|
{
|
|
795
845
|
key: "nextChange",
|
|
796
846
|
label: "Next change",
|
|
797
847
|
text: "Next change",
|
|
798
848
|
enabled: (state) => trackState(state) !== null,
|
|
799
|
-
command:
|
|
849
|
+
command: nextChange,
|
|
800
850
|
},
|
|
801
851
|
{
|
|
802
852
|
key: "acceptChange",
|
|
803
853
|
label: "Accept the change here, or the changes selected",
|
|
804
854
|
text: "Accept",
|
|
805
855
|
enabled: (state) => trackState(state) !== null,
|
|
806
|
-
command:
|
|
856
|
+
command: acceptChange,
|
|
807
857
|
},
|
|
808
858
|
{
|
|
809
859
|
key: "rejectChange",
|
|
810
860
|
label: "Reject the change here, or the changes selected",
|
|
811
861
|
text: "Reject",
|
|
812
862
|
enabled: (state) => trackState(state) !== null,
|
|
813
|
-
command:
|
|
863
|
+
command: rejectChange,
|
|
814
864
|
},
|
|
815
865
|
{
|
|
816
866
|
key: "acceptAll",
|
|
817
867
|
label: "Accept all changes",
|
|
818
868
|
text: "Accept all",
|
|
819
869
|
enabled: (state) => trackState(state) !== null,
|
|
820
|
-
command:
|
|
870
|
+
command: acceptAllChanges(),
|
|
821
871
|
},
|
|
822
872
|
{
|
|
823
873
|
key: "rejectAll",
|
|
824
874
|
label: "Reject all changes",
|
|
825
875
|
text: "Reject all",
|
|
826
876
|
enabled: (state) => trackState(state) !== null,
|
|
827
|
-
command:
|
|
877
|
+
command: rejectAllChanges(),
|
|
828
878
|
},
|
|
829
879
|
],
|
|
830
880
|
[
|
|
@@ -833,14 +883,14 @@ const GROUPS: Item[][] = [
|
|
|
833
883
|
label: "Undo",
|
|
834
884
|
text: "Undo",
|
|
835
885
|
enabled: (state) => undoDepth(state) > 0,
|
|
836
|
-
command:
|
|
886
|
+
command: undo,
|
|
837
887
|
},
|
|
838
888
|
{
|
|
839
889
|
key: "redo",
|
|
840
890
|
label: "Redo",
|
|
841
891
|
text: "Redo",
|
|
842
892
|
enabled: (state) => redoDepth(state) > 0,
|
|
843
|
-
command:
|
|
893
|
+
command: redo,
|
|
844
894
|
},
|
|
845
895
|
],
|
|
846
896
|
[
|
|
@@ -887,7 +937,13 @@ const GROUPS: Item[][] = [
|
|
|
887
937
|
// With a picture selected the dialog edits it.
|
|
888
938
|
dialog: "picture",
|
|
889
939
|
},
|
|
890
|
-
{
|
|
940
|
+
{
|
|
941
|
+
key: "pageBreak",
|
|
942
|
+
label: "Page break",
|
|
943
|
+
text: "Page break",
|
|
944
|
+
enabled: always,
|
|
945
|
+
command: insertPageBreak,
|
|
946
|
+
},
|
|
891
947
|
{
|
|
892
948
|
key: "section",
|
|
893
949
|
label: "Section",
|
|
@@ -933,10 +989,73 @@ const GROUPS: Item[][] = [
|
|
|
933
989
|
],
|
|
934
990
|
];
|
|
935
991
|
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
992
|
+
/** The names of the built-in items in `toolbarItems`. */
|
|
993
|
+
export type ToolbarItemName =
|
|
994
|
+
| "style"
|
|
995
|
+
| "font"
|
|
996
|
+
| "size"
|
|
997
|
+
| "bold"
|
|
998
|
+
| "italic"
|
|
999
|
+
| "underline"
|
|
1000
|
+
| "strike"
|
|
1001
|
+
| "link"
|
|
1002
|
+
| "textColor"
|
|
1003
|
+
| "highlight"
|
|
1004
|
+
| "alignStart"
|
|
1005
|
+
| "alignCenter"
|
|
1006
|
+
| "alignEnd"
|
|
1007
|
+
| "alignJustify"
|
|
1008
|
+
| "lineSpacing"
|
|
1009
|
+
| "bulletList"
|
|
1010
|
+
| "orderedList"
|
|
1011
|
+
| "outdent"
|
|
1012
|
+
| "indent"
|
|
1013
|
+
| "newComment"
|
|
1014
|
+
| "trackChanges"
|
|
1015
|
+
| "reviewView"
|
|
1016
|
+
| "previousChange"
|
|
1017
|
+
| "nextChange"
|
|
1018
|
+
| "acceptChange"
|
|
1019
|
+
| "rejectChange"
|
|
1020
|
+
| "acceptAll"
|
|
1021
|
+
| "rejectAll"
|
|
1022
|
+
| "undo"
|
|
1023
|
+
| "redo"
|
|
1024
|
+
| "table"
|
|
1025
|
+
| "tableStyle"
|
|
1026
|
+
| "cellShading"
|
|
1027
|
+
| "picture"
|
|
1028
|
+
| "pageBreak"
|
|
1029
|
+
| "section"
|
|
1030
|
+
| "headerFooter"
|
|
1031
|
+
| "fields"
|
|
1032
|
+
| "navigate"
|
|
1033
|
+
| "notes"
|
|
1034
|
+
| "controls";
|
|
1035
|
+
|
|
1036
|
+
/** The built-in menu buttons of `actions` (the color menus and boxes are buttons to an app). */
|
|
1037
|
+
type ToolbarMenuName =
|
|
1038
|
+
| "table"
|
|
1039
|
+
| "section"
|
|
1040
|
+
| "headerFooter"
|
|
1041
|
+
| "fields"
|
|
1042
|
+
| "navigate"
|
|
1043
|
+
| "notes"
|
|
1044
|
+
| "controls";
|
|
1045
|
+
|
|
1046
|
+
/**
|
|
1047
|
+
* The built-in items by name, to place in `FoliToolbarProps.items` (an item's `key` is its name,
|
|
1048
|
+
* except the alignments: `align-start`, `align-center`, `align-end`, `align-justify`). A copy
|
|
1049
|
+
* with another `label` or `icon` keeps what the built-in does.
|
|
1050
|
+
*/
|
|
1051
|
+
export const toolbarItems = Object.fromEntries(
|
|
1052
|
+
GROUPS.flat().map((item) => [item.key.replace(/-(\w)/, (_, c: string) => c.toUpperCase()), item]),
|
|
1053
|
+
) as unknown as {
|
|
1054
|
+
readonly [Name in ToolbarItemName]: Name extends ToolbarMenuName ? ToolbarMenu : ToolbarButton;
|
|
1055
|
+
};
|
|
1056
|
+
|
|
1057
|
+
/** The default toolbar: groups of `toolbarItems`, a separator between groups. */
|
|
1058
|
+
export const defaultToolbarItems = GROUPS as unknown as readonly (readonly ToolbarItem[])[];
|
|
940
1059
|
|
|
941
1060
|
interface LinkDialogState {
|
|
942
1061
|
/** The link at the selection when the dialog opened ("" for none). */
|
|
@@ -963,26 +1082,43 @@ const sameStates = (a: ItemState[], b: ItemState[]) =>
|
|
|
963
1082
|
item.value === b[i]?.value,
|
|
964
1083
|
);
|
|
965
1084
|
|
|
966
|
-
/**
|
|
1085
|
+
/**
|
|
1086
|
+
* Runs after every transaction: the built-ins' checks are O(selection), no transactions built;
|
|
1087
|
+
* an app's item without `enabled` gets a dry run of its command.
|
|
1088
|
+
*/
|
|
967
1089
|
const statesOf =
|
|
968
|
-
(items: readonly Item[]) =>
|
|
1090
|
+
(items: readonly Item[], body: Editor) =>
|
|
969
1091
|
(state: EditorState, editor: Editor): ItemState[] =>
|
|
970
1092
|
items.map((item) => ({
|
|
971
1093
|
pressed: item.pressed?.(state, editor),
|
|
972
|
-
enabled: item.enabled
|
|
1094
|
+
enabled: item.enabled
|
|
1095
|
+
? item.enabled(state, editor)
|
|
1096
|
+
: item.actions
|
|
1097
|
+
? actionsOf(item, body).length > 0
|
|
1098
|
+
: !item.command || editor.can(item.command),
|
|
973
1099
|
value:
|
|
974
1100
|
item.menu?.value(state) ??
|
|
975
1101
|
item.select?.value(state) ??
|
|
976
1102
|
(item.dialog ? (selectedPicture(state) ? "edit" : "insert") : undefined),
|
|
977
1103
|
}));
|
|
978
|
-
|
|
979
|
-
|
|
1104
|
+
|
|
1105
|
+
/** A menu button's entries (made from the body's editor when a function). */
|
|
1106
|
+
const actionsOf = (item: Item, body: Editor): readonly Action[] =>
|
|
1107
|
+
typeof item.actions === "function" ? item.actions(body) : (item.actions ?? []);
|
|
1108
|
+
|
|
1109
|
+
/** The same groups of the same items (an inline `items` array is not a change). */
|
|
1110
|
+
const sameGroups = (
|
|
1111
|
+
a: readonly (readonly ToolbarItem[])[] | undefined,
|
|
1112
|
+
b: readonly (readonly ToolbarItem[])[] | undefined,
|
|
1113
|
+
) =>
|
|
1114
|
+
a === b || (!!a && !!b && a.length === b.length && a.every((group, i) => sameItems(group, b[i])));
|
|
980
1115
|
|
|
981
1116
|
/**
|
|
982
|
-
* Word's formatting controls for an editor
|
|
983
|
-
* dialog, also on Mod-K in the document), text color
|
|
984
|
-
* spacing, lists (bulleted, numbered, decrease and
|
|
985
|
-
* menu with an insert table dialog, a table style
|
|
1117
|
+
* Word's formatting controls for an editor (`defaultToolbarItems`, or `items`): paragraph
|
|
1118
|
+
* style, font and size boxes, marks, link (a dialog, also on Mod-K in the document), text color
|
|
1119
|
+
* and highlight menus, alignment and line spacing, lists (bulleted, numbered, decrease and
|
|
1120
|
+
* increase indent), undo, redo, tables (a Table menu with an insert table dialog, a table style
|
|
1121
|
+
* box, a cell shading menu), page break.
|
|
986
1122
|
* One tab stop (arrow keys, Home and End move between buttons); toggles carry
|
|
987
1123
|
* `aria-pressed`; a button whose command cannot run now is `aria-disabled` (still focusable).
|
|
988
1124
|
* Clicks leave the focus and selection in the document.
|
|
@@ -995,17 +1131,32 @@ export function FoliToolbar({
|
|
|
995
1131
|
className,
|
|
996
1132
|
comments = false,
|
|
997
1133
|
upload,
|
|
1134
|
+
items: itemGroups,
|
|
998
1135
|
}: FoliToolbarProps) {
|
|
999
1136
|
const body = useEditorOrContext(given, "FoliToolbar");
|
|
1000
1137
|
// A story open on its page takes the formatting commands; the body otherwise.
|
|
1001
1138
|
const editor = useActiveEditor(body);
|
|
1002
|
-
|
|
1003
|
-
const
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1139
|
+
// Without "New comment" where no comments pane lets a comment be written.
|
|
1140
|
+
const lastGroups = useRef(itemGroups);
|
|
1141
|
+
if (!sameGroups(lastGroups.current, itemGroups)) lastGroups.current = itemGroups;
|
|
1142
|
+
const stableGroups = lastGroups.current;
|
|
1143
|
+
const { groups, items, selectStates } = useMemo(() => {
|
|
1144
|
+
const groups = ((stableGroups ?? GROUPS) as readonly (readonly Item[])[])
|
|
1145
|
+
.map((group) => group.filter((item) => comments || item.key !== "newComment"))
|
|
1146
|
+
.filter((group) => group.length > 0);
|
|
1147
|
+
const items = groups.flat();
|
|
1148
|
+
if (development()) {
|
|
1149
|
+
const keys = items.map((item) => item.key);
|
|
1150
|
+
const twice = keys.find((key, i) => keys.indexOf(key) !== i);
|
|
1151
|
+
if (twice !== undefined)
|
|
1152
|
+
console.warn(`FoliToolbar: two items have the key "${twice}"; each needs its own`);
|
|
1153
|
+
}
|
|
1154
|
+
return { groups, items, selectStates: statesOf(items, body) };
|
|
1155
|
+
}, [stableGroups, comments, body]);
|
|
1156
|
+
const states = useEditorState(selectStates, { editor, isEqual: sameStates });
|
|
1157
|
+
const [focused, setActive] = useState(0);
|
|
1158
|
+
// The tab stop stays on the toolbar when items are taken away.
|
|
1159
|
+
const active = focused < items.length ? focused : 0;
|
|
1009
1160
|
const buttons = useRef<(HTMLButtonElement | HTMLSelectElement | null)[]>([]);
|
|
1010
1161
|
// Whether a select box was last used with the pointer (its pick then goes back to the document).
|
|
1011
1162
|
const pointer = useRef(false);
|
|
@@ -1031,6 +1182,7 @@ export function FoliToolbar({
|
|
|
1031
1182
|
});
|
|
1032
1183
|
return;
|
|
1033
1184
|
}
|
|
1185
|
+
if (item.actions && !actionsOf(item, body).length) return;
|
|
1034
1186
|
if (item.menu || item.actions) {
|
|
1035
1187
|
if (menu !== item.key) {
|
|
1036
1188
|
setMenu(item.key);
|
|
@@ -1043,7 +1195,7 @@ export function FoliToolbar({
|
|
|
1043
1195
|
return;
|
|
1044
1196
|
}
|
|
1045
1197
|
if (!item.command) return;
|
|
1046
|
-
editor.run(item.command
|
|
1198
|
+
editor.run(item.command);
|
|
1047
1199
|
// `detail` counts pointer clicks; Enter and Space on a button give 0.
|
|
1048
1200
|
if (event.detail > 0) onClickCommand?.();
|
|
1049
1201
|
};
|
|
@@ -1225,10 +1377,12 @@ export function FoliToolbar({
|
|
|
1225
1377
|
const i = index++;
|
|
1226
1378
|
const state = states[i];
|
|
1227
1379
|
// Inserting a picture needs the app's upload; editing a selected one does not.
|
|
1380
|
+
// A menu without entries cannot open, whatever its `enabled` says.
|
|
1228
1381
|
const off =
|
|
1229
1382
|
disabled ||
|
|
1230
1383
|
!state?.enabled ||
|
|
1231
|
-
(item.dialog === "picture" && state?.value === "insert" && !upload)
|
|
1384
|
+
(item.dialog === "picture" && state?.value === "insert" && !upload) ||
|
|
1385
|
+
(item.actions !== undefined && actionsOf(item, body).length === 0);
|
|
1232
1386
|
if (item.select) {
|
|
1233
1387
|
const { select } = item;
|
|
1234
1388
|
const value = state?.value ?? "";
|
|
@@ -1283,7 +1437,7 @@ export function FoliToolbar({
|
|
|
1283
1437
|
type="button"
|
|
1284
1438
|
className={`foli-toolbar-button foli-toolbar-${item.key}`}
|
|
1285
1439
|
// Speech input users say what they see: "H1", "Undo"; symbols get a name.
|
|
1286
|
-
aria-label={item.
|
|
1440
|
+
aria-label={item.icon !== undefined ? item.label : undefined}
|
|
1287
1441
|
title={item.label}
|
|
1288
1442
|
aria-pressed={item.pressed ? Boolean(state?.pressed) : undefined}
|
|
1289
1443
|
aria-haspopup={
|
|
@@ -1303,7 +1457,7 @@ export function FoliToolbar({
|
|
|
1303
1457
|
setMenu(item.key);
|
|
1304
1458
|
}}
|
|
1305
1459
|
>
|
|
1306
|
-
{item.icon ?? item.text}
|
|
1460
|
+
{item.icon ?? item.text ?? item.label}
|
|
1307
1461
|
{item.menu && (
|
|
1308
1462
|
<span
|
|
1309
1463
|
className="foli-toolbar-swatch"
|
|
@@ -1314,15 +1468,15 @@ export function FoliToolbar({
|
|
|
1314
1468
|
</button>
|
|
1315
1469
|
);
|
|
1316
1470
|
if (item.actions) {
|
|
1317
|
-
const actions =
|
|
1318
|
-
typeof item.actions === "function" ? item.actions(body) : item.actions;
|
|
1471
|
+
const actions = actionsOf(item, body);
|
|
1319
1472
|
return (
|
|
1320
1473
|
<span key={item.key} className="foli-toolbar-menu">
|
|
1321
1474
|
{button}
|
|
1322
|
-
{menu === item.key && !disabled && (
|
|
1475
|
+
{menu === item.key && !disabled && actions.length > 0 && (
|
|
1323
1476
|
<ActionMenu
|
|
1324
1477
|
label={item.label}
|
|
1325
1478
|
actions={actions}
|
|
1479
|
+
editor={editor}
|
|
1326
1480
|
state={editor.state}
|
|
1327
1481
|
onPick={(action, pointer) => {
|
|
1328
1482
|
setMenu(null);
|
|
@@ -1688,12 +1842,15 @@ function LinkDialog({
|
|
|
1688
1842
|
function ActionMenu({
|
|
1689
1843
|
label,
|
|
1690
1844
|
actions,
|
|
1845
|
+
editor,
|
|
1691
1846
|
state,
|
|
1692
1847
|
onPick,
|
|
1693
1848
|
onClose,
|
|
1694
1849
|
}: {
|
|
1695
1850
|
label: string;
|
|
1696
1851
|
actions: readonly Action[];
|
|
1852
|
+
/** For `checked`, with `state`. */
|
|
1853
|
+
editor: Editor;
|
|
1697
1854
|
state: EditorState;
|
|
1698
1855
|
onPick: (action: Action, pointer: boolean) => void;
|
|
1699
1856
|
onClose: (refocus: boolean) => void;
|
|
@@ -1756,7 +1913,7 @@ function ActionMenu({
|
|
|
1756
1913
|
key={action.key}
|
|
1757
1914
|
{...props}
|
|
1758
1915
|
role="menuitemcheckbox"
|
|
1759
|
-
aria-checked={action.checked(state)}
|
|
1916
|
+
aria-checked={action.checked(state, editor)}
|
|
1760
1917
|
>
|
|
1761
1918
|
{action.label}
|
|
1762
1919
|
</button>
|