foldkit-mixins-richtext 0.3.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +167 -10
- package/dist/index.d.mts +252 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +561 -27
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -6
package/dist/index.mjs
CHANGED
|
@@ -7,6 +7,213 @@ import * as Mount from "foldkit/mount";
|
|
|
7
7
|
import { RovingTabindex } from "foldkit-primitives/interaction";
|
|
8
8
|
import { renderDocument } from "foldkit-richtext-dom/view";
|
|
9
9
|
import { closeSource } from "foldkit-richtext-markdown";
|
|
10
|
+
//#region src/icons.ts
|
|
11
|
+
const box = "M5 3h14a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2z";
|
|
12
|
+
const dot = (x, y) => `M${x} ${y + 1}a1 1 0 1 0 0-2 1 1 0 0 0 0 2z`;
|
|
13
|
+
const paths = {
|
|
14
|
+
paragraph: [
|
|
15
|
+
"M13 4v16",
|
|
16
|
+
"M17 4v16",
|
|
17
|
+
"M19 4H9.5a4.5 4.5 0 0 0 0 9H13"
|
|
18
|
+
],
|
|
19
|
+
"heading-1": [
|
|
20
|
+
"M4 12h8",
|
|
21
|
+
"M4 18V6",
|
|
22
|
+
"M12 18V6",
|
|
23
|
+
"m17 12 3-2v8"
|
|
24
|
+
],
|
|
25
|
+
"heading-2": [
|
|
26
|
+
"M4 12h8",
|
|
27
|
+
"M4 18V6",
|
|
28
|
+
"M12 18V6",
|
|
29
|
+
"M21 18h-4c0-4 4-3 4-6 0-1.5-2-2.5-4-1"
|
|
30
|
+
],
|
|
31
|
+
"heading-3": [
|
|
32
|
+
"M4 12h8",
|
|
33
|
+
"M4 18V6",
|
|
34
|
+
"M12 18V6",
|
|
35
|
+
"M17.5 10.5c1.7-1 3.5 0 3.5 1.5a2 2 0 0 1-2 2",
|
|
36
|
+
"M17 17.5c2 1.5 4 .3 4-1.5a2 2 0 0 0-2-2"
|
|
37
|
+
],
|
|
38
|
+
quote: [
|
|
39
|
+
"M17 6H3",
|
|
40
|
+
"M21 12H8",
|
|
41
|
+
"M21 18H8",
|
|
42
|
+
"M3 12v6"
|
|
43
|
+
],
|
|
44
|
+
"bulleted-list": [
|
|
45
|
+
"M8 6h13",
|
|
46
|
+
"M8 12h13",
|
|
47
|
+
"M8 18h13",
|
|
48
|
+
"M3 6h.01",
|
|
49
|
+
"M3 12h.01",
|
|
50
|
+
"M3 18h.01"
|
|
51
|
+
],
|
|
52
|
+
"numbered-list": [
|
|
53
|
+
"M10 6h11",
|
|
54
|
+
"M10 12h11",
|
|
55
|
+
"M10 18h11",
|
|
56
|
+
"M4 6h1v4",
|
|
57
|
+
"M4 10h2",
|
|
58
|
+
"M6 18H4c0-1 2-2 2-3s-1-1.5-2-1"
|
|
59
|
+
],
|
|
60
|
+
checklist: [
|
|
61
|
+
"m3 17 2 2 4-4",
|
|
62
|
+
"m3 7 2 2 4-4",
|
|
63
|
+
"M13 6h8",
|
|
64
|
+
"M13 12h8",
|
|
65
|
+
"M13 18h8"
|
|
66
|
+
],
|
|
67
|
+
"code-block": [
|
|
68
|
+
box,
|
|
69
|
+
"m10 9-3 3 3 3",
|
|
70
|
+
"m14 15 3-3-3-3"
|
|
71
|
+
],
|
|
72
|
+
bold: ["M14 12a4 4 0 0 0 0-8H6v8", "M15 20a4 4 0 0 0 0-8H6v8Z"],
|
|
73
|
+
italic: [
|
|
74
|
+
"M19 4h-9",
|
|
75
|
+
"M14 20H5",
|
|
76
|
+
"M15 4 9 20"
|
|
77
|
+
],
|
|
78
|
+
underline: ["M6 4v6a6 6 0 0 0 12 0V4", "M4 20h16"],
|
|
79
|
+
strikethrough: [
|
|
80
|
+
"M16 4H9a3 3 0 0 0-2.83 4",
|
|
81
|
+
"M14 12a4 4 0 0 1 0 8H6",
|
|
82
|
+
"M4 12h16"
|
|
83
|
+
],
|
|
84
|
+
code: ["m16 18 6-6-6-6", "m8 6-6 6 6 6"],
|
|
85
|
+
highlight: ["m9 11-6 6v3h9l3-3", "m22 12-4.6 4.6a2 2 0 0 1-2.8 0l-5.2-5.2a2 2 0 0 1 0-2.8L14 4"],
|
|
86
|
+
kbd: [
|
|
87
|
+
"M4 4h16a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2z",
|
|
88
|
+
"M6 8h.01",
|
|
89
|
+
"M10 8h.01",
|
|
90
|
+
"M14 8h.01",
|
|
91
|
+
"M18 8h.01",
|
|
92
|
+
"M8 12h.01",
|
|
93
|
+
"M12 12h.01",
|
|
94
|
+
"M16 12h.01",
|
|
95
|
+
"M7 16h10"
|
|
96
|
+
],
|
|
97
|
+
link: ["M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71", "M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"],
|
|
98
|
+
comment: ["M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"],
|
|
99
|
+
table: [
|
|
100
|
+
box,
|
|
101
|
+
"M3 9h18",
|
|
102
|
+
"M3 15h18",
|
|
103
|
+
"M12 3v18"
|
|
104
|
+
],
|
|
105
|
+
image: [
|
|
106
|
+
box,
|
|
107
|
+
"M9 11a2 2 0 1 0 0-4 2 2 0 0 0 0 4z",
|
|
108
|
+
"M21 15l-5-5L5 21"
|
|
109
|
+
],
|
|
110
|
+
video: ["m16 13 5.223 3.482a.5.5 0 0 0 .777-.416V7.87a.5.5 0 0 0-.752-.432L16 10.5", "M4 6h10a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2z"],
|
|
111
|
+
audio: [
|
|
112
|
+
"M9 18V5l12-2v13",
|
|
113
|
+
"M6 21a3 3 0 1 0 0-6 3 3 0 0 0 0 6z",
|
|
114
|
+
"M18 19a3 3 0 1 0 0-6 3 3 0 0 0 0 6z"
|
|
115
|
+
],
|
|
116
|
+
file: ["m21.44 11.05-9.19 9.19a6 6 0 0 1-8.49-8.49l8.57-8.57A4 4 0 1 1 18 8.84l-8.59 8.57a2 2 0 0 1-2.83-2.83l8.49-8.48"],
|
|
117
|
+
callout: [
|
|
118
|
+
"M15 14c.2-1 .7-1.7 1.5-2.5 1-.9 1.5-2.2 1.5-3.5A6 6 0 0 0 6 8c0 1 .2 2.2 1.5 3.5.7.7 1.3 1.5 1.5 2.5",
|
|
119
|
+
"M9 18h6",
|
|
120
|
+
"M10 22h4"
|
|
121
|
+
],
|
|
122
|
+
toggle: [
|
|
123
|
+
"m3 10 2.5-2.5L3 5",
|
|
124
|
+
"m3 19 2.5-2.5L3 14",
|
|
125
|
+
"M10 6h11",
|
|
126
|
+
"M10 12h11",
|
|
127
|
+
"M10 18h11"
|
|
128
|
+
],
|
|
129
|
+
divider: ["M5 12h14"],
|
|
130
|
+
footnote: [
|
|
131
|
+
"m4 19 8-8",
|
|
132
|
+
"m12 19-8-8",
|
|
133
|
+
"M20 12h-4c0-1.5.442-2 1.5-2.5S20 8.334 20 7.002c0-.472-.17-.93-.484-1.29a2.105 2.105 0 0 0-2.617-.436c-.42.239-.738.614-.899 1.06"
|
|
134
|
+
],
|
|
135
|
+
undo: ["M9 14 4 9l5-5", "M4 9h11a5 5 0 0 1 0 10h-3"],
|
|
136
|
+
redo: ["M15 14l5-5-5-5", "M20 9H9a5 5 0 0 0 0 10h3"],
|
|
137
|
+
search: ["M11 19a8 8 0 1 0 0-16 8 8 0 0 0 0 16z", "M21 21l-4.35-4.35"],
|
|
138
|
+
replace: [
|
|
139
|
+
"M14 4a1 1 0 0 1 1-1",
|
|
140
|
+
"M15 10a1 1 0 0 1-1-1",
|
|
141
|
+
"M21 4a1 1 0 0 0-1-1",
|
|
142
|
+
"M21 9a1 1 0 0 1-1 1",
|
|
143
|
+
"m3 7 3 3 3-3",
|
|
144
|
+
"M6 10V5a2 2 0 0 1 2-2h2",
|
|
145
|
+
"M4 14h6a1 1 0 0 1 1 1v5a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1v-5a1 1 0 0 1 1-1z"
|
|
146
|
+
],
|
|
147
|
+
"match-case": [
|
|
148
|
+
"m3 15 4-8 4 8",
|
|
149
|
+
"M4 13h6",
|
|
150
|
+
"M18 15a3 3 0 1 0 0-6 3 3 0 0 0 0 6z",
|
|
151
|
+
"M21 9v6"
|
|
152
|
+
],
|
|
153
|
+
download: [
|
|
154
|
+
"M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4",
|
|
155
|
+
"M7 10l5 5 5-5",
|
|
156
|
+
"M12 15V3"
|
|
157
|
+
],
|
|
158
|
+
copy: ["M10 8h10a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2H10a2 2 0 0 1-2-2V10a2 2 0 0 1 2-2z", "M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"],
|
|
159
|
+
trash: [
|
|
160
|
+
"M3 6h18",
|
|
161
|
+
"M8 6V4h8v2",
|
|
162
|
+
"M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"
|
|
163
|
+
],
|
|
164
|
+
plus: ["M12 5v14", "M5 12h14"],
|
|
165
|
+
more: [
|
|
166
|
+
dot(5, 12),
|
|
167
|
+
dot(12, 12),
|
|
168
|
+
dot(19, 12)
|
|
169
|
+
],
|
|
170
|
+
grip: [
|
|
171
|
+
dot(9, 5),
|
|
172
|
+
dot(9, 12),
|
|
173
|
+
dot(9, 19),
|
|
174
|
+
dot(15, 5),
|
|
175
|
+
dot(15, 12),
|
|
176
|
+
dot(15, 19)
|
|
177
|
+
],
|
|
178
|
+
close: ["M18 6 6 18", "m6 6 12 12"],
|
|
179
|
+
check: ["M20 6 9 17l-5-5"],
|
|
180
|
+
"chevron-down": ["m6 9 6 6 6-6"],
|
|
181
|
+
"chevron-up": ["m18 15-6-6-6 6"],
|
|
182
|
+
"chevron-right": ["m9 18 6-6-6-6"],
|
|
183
|
+
back: ["m12 19-7-7 7-7", "M19 12H5"],
|
|
184
|
+
contents: [
|
|
185
|
+
"M21 12h-8",
|
|
186
|
+
"M21 6H8",
|
|
187
|
+
"M21 18h-8",
|
|
188
|
+
"M3 6v4c0 1.1.9 2 2 2h3",
|
|
189
|
+
"M3 10v6c0 1.1.9 2 2 2h3"
|
|
190
|
+
],
|
|
191
|
+
reset: ["M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8", "M3 3v5h5"]
|
|
192
|
+
};
|
|
193
|
+
/** Whether a name, such as a slash entry's id, has a drawing in the set. */
|
|
194
|
+
const isRichTextIcon = (name) => Object.hasOwn(paths, name);
|
|
195
|
+
/** An icon as inline SVG, `size` CSS pixels square, in the text's color. */
|
|
196
|
+
const richTextIcon = (h, name, { size = 16 } = {}) => h.svg([
|
|
197
|
+
h.ViewBox("0 0 24 24"),
|
|
198
|
+
h.Width(String(size)),
|
|
199
|
+
h.Height(String(size)),
|
|
200
|
+
h.Fill("none"),
|
|
201
|
+
h.Stroke("currentColor"),
|
|
202
|
+
h.StrokeWidth("2"),
|
|
203
|
+
h.StrokeLinecap("round"),
|
|
204
|
+
h.StrokeLinejoin("round"),
|
|
205
|
+
h.AriaHidden(true),
|
|
206
|
+
h.DataAttribute("icon", name)
|
|
207
|
+
], paths[name].map((d) => h.path([h.D(d)], [])));
|
|
208
|
+
/**
|
|
209
|
+
* An icon as a CSS `url(…)`, for markup that cannot hold an element of its own, such as a
|
|
210
|
+
* rendering's adornment: a stylesheet masks it over the text's color.
|
|
211
|
+
*/
|
|
212
|
+
const richTextIconUrl = (name) => {
|
|
213
|
+
const svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="black" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">${paths[name].map((d) => `<path d="${d}"/>`).join("")}</svg>`;
|
|
214
|
+
return `url("data:image/svg+xml,${encodeURIComponent(svg)}")`;
|
|
215
|
+
};
|
|
216
|
+
//#endregion
|
|
10
217
|
//#region src/style.ts
|
|
11
218
|
/**
|
|
12
219
|
* The block style picker (§124 §11, §134): a button per text style the caret's block can take —
|
|
@@ -47,6 +254,251 @@ const blockStyles = () => SlotView.forMessages().define(BlockStyleSlots, (input,
|
|
|
47
254
|
count: styles.length
|
|
48
255
|
}), [style.label])))]);
|
|
49
256
|
});
|
|
257
|
+
/**
|
|
258
|
+
* The elements the style menu publishes: its wrapper, the trigger that names the current style,
|
|
259
|
+
* the menu, and one item per style with its icon, label and the check on the current one.
|
|
260
|
+
*/
|
|
261
|
+
const BlockStyleMenuSlots = Slots.define({
|
|
262
|
+
root: Slot.make({ capability: Capability.Container }),
|
|
263
|
+
trigger: Slot.make({ capability: Capability.Interactive }),
|
|
264
|
+
menu: Slot.make({ capability: Capability.Collection }),
|
|
265
|
+
item: Slot.make({ capability: Capability.Interactive }),
|
|
266
|
+
icon: Slot.make({ capability: Capability.Container }),
|
|
267
|
+
label: Slot.make({ capability: Capability.Container }),
|
|
268
|
+
check: Slot.make({ capability: Capability.Container })
|
|
269
|
+
});
|
|
270
|
+
/**
|
|
271
|
+
* The same styles as `blockStyles`, in the space of one button: the trigger names the style the
|
|
272
|
+
* selection's block has ("Text style" where none applies, as in a code block) and opens a menu of
|
|
273
|
+
* every style, the current one checked. Choosing one sends its `RetypedBlock` through `wrap`, which
|
|
274
|
+
* is where a caller also closes the menu. The trigger is disabled without a text selection.
|
|
275
|
+
*/
|
|
276
|
+
const blockStyleMenu = () => SlotView.forMessages().define(BlockStyleMenuSlots, (input, slots, h) => {
|
|
277
|
+
const current = RichText.textBlockAt(input.document, input.selection);
|
|
278
|
+
const live = input.selection?.type === "Range";
|
|
279
|
+
const chosen = styles.find((style) => current !== void 0 && Equal.equals(style.message.block, current));
|
|
280
|
+
const icon = (name) => name !== void 0 && isRichTextIcon(name) ? [h.span(slots.icon.attrs(), [richTextIcon(h, name)])] : [];
|
|
281
|
+
return h.div(slots.root.attrs(), [h.button(slots.trigger.attrs([
|
|
282
|
+
h.Type("button"),
|
|
283
|
+
h.AriaHasPopup("menu"),
|
|
284
|
+
h.AriaExpanded(input.open && live),
|
|
285
|
+
h.AriaLabel(`Text style: ${chosen?.label ?? "none"}`),
|
|
286
|
+
h.Disabled(!live),
|
|
287
|
+
...live ? [h.OnClick(input.toggled)] : []
|
|
288
|
+
]), [
|
|
289
|
+
...icon(chosen?.icon),
|
|
290
|
+
h.span(slots.label.attrs(), [chosen?.label ?? "Text style"]),
|
|
291
|
+
richTextIcon(h, "chevron-down", { size: 14 })
|
|
292
|
+
]), ...input.open && live ? [h.div(slots.menu.attrs([h.Role("menu"), h.AriaLabel("Text style")]), styles.map((style, index) => h.button(slots.item.attrs([
|
|
293
|
+
h.Type("button"),
|
|
294
|
+
h.Role("menuitemradio"),
|
|
295
|
+
h.AriaChecked(style === chosen),
|
|
296
|
+
h.DataAttribute("style", style.id),
|
|
297
|
+
h.OnClick(input.wrap(style.message))
|
|
298
|
+
], {
|
|
299
|
+
index,
|
|
300
|
+
id: style.id,
|
|
301
|
+
count: styles.length
|
|
302
|
+
}), [
|
|
303
|
+
...icon(style.icon),
|
|
304
|
+
h.span(slots.label.attrs(), [style.label]),
|
|
305
|
+
h.span(slots.check.attrs(), style === chosen ? [richTextIcon(h, "check")] : [])
|
|
306
|
+
])))] : []]);
|
|
307
|
+
});
|
|
308
|
+
//#endregion
|
|
309
|
+
//#region src/find.ts
|
|
310
|
+
/**
|
|
311
|
+
* The find and replace bar, drawn through slots. Like the rest of the chrome it holds no state:
|
|
312
|
+
* the query, the replacement, which match is current and whether case counts are the caller's,
|
|
313
|
+
* the matches are a read of the document (`RichText.findMatches`), and replacing is the editor's
|
|
314
|
+
* own `ReplacedText`, so a replacement is an edit with an undo step like any other.
|
|
315
|
+
*/
|
|
316
|
+
/** The elements the bar publishes: its wrapper, its two fields, the count, and its buttons. */
|
|
317
|
+
const FindReplaceSlots = Slots.define({
|
|
318
|
+
root: Slot.make({ capability: Capability.Container }),
|
|
319
|
+
field: Slot.make({ capability: Capability.TextInput }),
|
|
320
|
+
count: Slot.make({ capability: Capability.Container }),
|
|
321
|
+
button: Slot.make({ capability: Capability.Interactive })
|
|
322
|
+
});
|
|
323
|
+
/**
|
|
324
|
+
* The index a step from `current` lands on among `count` matches, wrapping at either end, so
|
|
325
|
+
* Enter keeps cycling through them as a browser's find does.
|
|
326
|
+
*/
|
|
327
|
+
const stepMatch = (current, count, step) => count === 0 ? 0 : ((current + step) % count + count) % count;
|
|
328
|
+
/**
|
|
329
|
+
* The bar as a slot view. Enter in the find field goes to the next match and Shift+Enter to the
|
|
330
|
+
* previous one; Escape in either field closes it. Replace replaces the current match and Replace
|
|
331
|
+
* all every one; both are disabled with no match. Each button carries `data-find`.
|
|
332
|
+
*/
|
|
333
|
+
const findReplace = () => SlotView.forMessages().define(FindReplaceSlots, (input, slots, h) => {
|
|
334
|
+
const count = RichText.findMatches(input.document, input.query, input).length;
|
|
335
|
+
const current = Math.min(input.current, Math.max(0, count - 1));
|
|
336
|
+
const replaced = (at) => input.wrap(Message.ReplacedText({
|
|
337
|
+
query: input.query,
|
|
338
|
+
replacement: input.replacement,
|
|
339
|
+
caseSensitive: input.caseSensitive,
|
|
340
|
+
...at === void 0 ? {} : { at }
|
|
341
|
+
}));
|
|
342
|
+
const button = (id, label, message, { disabled = false, pressed, icon } = {}) => h.button(slots.button.attrs([
|
|
343
|
+
h.Type("button"),
|
|
344
|
+
h.DataAttribute("find", id),
|
|
345
|
+
h.AriaLabel(label),
|
|
346
|
+
...icon === void 0 ? [] : [h.Title(label)],
|
|
347
|
+
...pressed === void 0 ? [] : [h.AriaPressed(pressed ? "true" : "false")],
|
|
348
|
+
h.Disabled(disabled),
|
|
349
|
+
...disabled ? [] : [h.OnClick(message)]
|
|
350
|
+
]), [icon === void 0 ? label : richTextIcon(h, icon)]);
|
|
351
|
+
return h.div(slots.root.attrs([h.Role("search"), h.AriaLabel("Find and replace")]), [
|
|
352
|
+
h.input(slots.field.attrs([
|
|
353
|
+
h.Type("search"),
|
|
354
|
+
h.DataAttribute("find", "query"),
|
|
355
|
+
h.AriaLabel("Find"),
|
|
356
|
+
h.Placeholder("Find"),
|
|
357
|
+
h.Value(input.query),
|
|
358
|
+
h.OnInput(input.queried),
|
|
359
|
+
h.OnKeyDownPreventDefault((key, modifiers) => {
|
|
360
|
+
if (key === "Escape") return Option.some(input.closed);
|
|
361
|
+
if (key !== "Enter" || count === 0) return Option.none();
|
|
362
|
+
return Option.some(input.stepped(modifiers.shiftKey ? -1 : 1));
|
|
363
|
+
})
|
|
364
|
+
])),
|
|
365
|
+
h.span(slots.count.attrs([h.AriaLive("polite")]), [input.query === "" ? "" : count === 0 ? "No matches" : `${current + 1} of ${count}`]),
|
|
366
|
+
button("previous", "Previous match", input.stepped(-1), {
|
|
367
|
+
disabled: count === 0,
|
|
368
|
+
icon: "chevron-up"
|
|
369
|
+
}),
|
|
370
|
+
button("next", "Next match", input.stepped(1), {
|
|
371
|
+
disabled: count === 0,
|
|
372
|
+
icon: "chevron-down"
|
|
373
|
+
}),
|
|
374
|
+
button("case", "Match case", input.caseToggled, {
|
|
375
|
+
pressed: input.caseSensitive,
|
|
376
|
+
icon: "match-case"
|
|
377
|
+
}),
|
|
378
|
+
h.input(slots.field.attrs([
|
|
379
|
+
h.Type("text"),
|
|
380
|
+
h.DataAttribute("find", "replacement"),
|
|
381
|
+
h.AriaLabel("Replace with"),
|
|
382
|
+
h.Placeholder("Replace with"),
|
|
383
|
+
h.Value(input.replacement),
|
|
384
|
+
h.OnInput(input.replacementDrafted),
|
|
385
|
+
h.OnKeyDownPreventDefault((key) => key === "Escape" ? Option.some(input.closed) : Option.none())
|
|
386
|
+
])),
|
|
387
|
+
button("replace", "Replace", replaced(current), { disabled: count === 0 }),
|
|
388
|
+
button("replace-all", "Replace all", replaced(void 0), { disabled: count === 0 }),
|
|
389
|
+
button("close", "Close", input.closed, { icon: "close" })
|
|
390
|
+
]);
|
|
391
|
+
});
|
|
392
|
+
//#endregion
|
|
393
|
+
//#region src/table.ts
|
|
394
|
+
/** The elements the toolbar publishes: its wrapper, its row, and one button per change. */
|
|
395
|
+
const TableToolbarSlots = Slots.define({
|
|
396
|
+
root: Slot.make({ capability: Capability.Container }),
|
|
397
|
+
toolbar: Slot.make({ capability: Capability.Collection }),
|
|
398
|
+
button: Slot.make({ capability: Capability.Interactive })
|
|
399
|
+
});
|
|
400
|
+
/**
|
|
401
|
+
* The table toolbar as a slot view. Every button is disabled outside a table, so a caller can
|
|
402
|
+
* draw it always or only while `RichText.tableAt` finds the caret in one. Each button carries its
|
|
403
|
+
* change as `data-table` and as the slot item's `id`, so a Style or Behavior can single one out;
|
|
404
|
+
* the header button is pressed while the table's first row is its header.
|
|
405
|
+
*/
|
|
406
|
+
const tableToolbar = () => SlotView.forMessages().define(TableToolbarSlots, (input, slots, h) => {
|
|
407
|
+
const at = RichText.tableAt(input.document, input.selection);
|
|
408
|
+
const first = at?.rows[0];
|
|
409
|
+
const header = first?.props.header === true;
|
|
410
|
+
const changes = [
|
|
411
|
+
[
|
|
412
|
+
"row-above",
|
|
413
|
+
"Row above",
|
|
414
|
+
Message.InsertedRow({ where: "before" })
|
|
415
|
+
],
|
|
416
|
+
[
|
|
417
|
+
"row-below",
|
|
418
|
+
"Row below",
|
|
419
|
+
Message.InsertedRow({ where: "after" })
|
|
420
|
+
],
|
|
421
|
+
[
|
|
422
|
+
"column-left",
|
|
423
|
+
"Column left",
|
|
424
|
+
Message.InsertedColumn({ where: "before" })
|
|
425
|
+
],
|
|
426
|
+
[
|
|
427
|
+
"column-right",
|
|
428
|
+
"Column right",
|
|
429
|
+
Message.InsertedColumn({ where: "after" })
|
|
430
|
+
],
|
|
431
|
+
[
|
|
432
|
+
"delete-row",
|
|
433
|
+
"Delete row",
|
|
434
|
+
Message.DeletedRow()
|
|
435
|
+
],
|
|
436
|
+
[
|
|
437
|
+
"delete-column",
|
|
438
|
+
"Delete column",
|
|
439
|
+
Message.DeletedColumn()
|
|
440
|
+
],
|
|
441
|
+
[
|
|
442
|
+
"header",
|
|
443
|
+
"Header row",
|
|
444
|
+
first === void 0 ? void 0 : Message.ChangedProps({
|
|
445
|
+
node: first.id,
|
|
446
|
+
props: { header: !header }
|
|
447
|
+
}),
|
|
448
|
+
header
|
|
449
|
+
],
|
|
450
|
+
[
|
|
451
|
+
"delete-table",
|
|
452
|
+
"Delete table",
|
|
453
|
+
at === void 0 ? void 0 : Message.DeletedBlock({ node: at.table.id })
|
|
454
|
+
]
|
|
455
|
+
];
|
|
456
|
+
return h.div(slots.root.attrs(), [h.div(slots.toolbar.attrs([h.Role("toolbar"), h.AriaLabel("Table")]), changes.map(([id, label, message, pressed], index) => h.button(slots.button.attrs([
|
|
457
|
+
h.Type("button"),
|
|
458
|
+
h.DataAttribute("table", id),
|
|
459
|
+
...pressed === void 0 ? [] : [h.AriaPressed(pressed ? "true" : "false")],
|
|
460
|
+
h.Disabled(at === void 0 || message === void 0),
|
|
461
|
+
...at === void 0 || message === void 0 ? [] : [h.OnClick(input.wrap(message))]
|
|
462
|
+
], {
|
|
463
|
+
index,
|
|
464
|
+
id,
|
|
465
|
+
count: changes.length
|
|
466
|
+
}), [label])))]);
|
|
467
|
+
});
|
|
468
|
+
//#endregion
|
|
469
|
+
//#region src/code.ts
|
|
470
|
+
/** The elements the toolbar publishes: its wrapper, the language picker, and the Copy button. */
|
|
471
|
+
const CodeToolbarSlots = Slots.define({
|
|
472
|
+
root: Slot.make({ capability: Capability.Container }),
|
|
473
|
+
language: Slot.make({ capability: Capability.Interactive }),
|
|
474
|
+
copy: Slot.make({ capability: Capability.Interactive })
|
|
475
|
+
});
|
|
476
|
+
/**
|
|
477
|
+
* The code block toolbar as a slot view. The picker shows the block's language, and a language
|
|
478
|
+
* the list does not name is offered as itself, so choosing nothing never changes it. Nothing is
|
|
479
|
+
* drawn for a block that is not there.
|
|
480
|
+
*/
|
|
481
|
+
const codeToolbar = () => SlotView.forMessages().define(CodeToolbarSlots, (input, slots, h) => {
|
|
482
|
+
const block = RichText.locateBlock(input.document, input.node)?.block;
|
|
483
|
+
const current = block?.type === "Node" ? block.props.language : void 0;
|
|
484
|
+
const language = typeof current === "string" ? current : "";
|
|
485
|
+
const languages = input.languages.some((each) => each.id === language) ? input.languages : [...input.languages, {
|
|
486
|
+
id: language,
|
|
487
|
+
label: language
|
|
488
|
+
}];
|
|
489
|
+
return h.div(slots.root.attrs([h.Role("toolbar"), h.AriaLabel("Code")]), [h.select(slots.language.attrs([
|
|
490
|
+
h.AriaLabel("Language"),
|
|
491
|
+
h.Value(language),
|
|
492
|
+
h.OnChange((chosen) => input.wrap(Message.ChangedProps({
|
|
493
|
+
node: input.node,
|
|
494
|
+
props: { language: chosen }
|
|
495
|
+
})))
|
|
496
|
+
]), languages.map((each) => h.option([h.Value(each.id), h.Selected(each.id === language)], [each.label]))), h.button(slots.copy.attrs([
|
|
497
|
+
h.Type("button"),
|
|
498
|
+
h.DataAttribute("code", "copy"),
|
|
499
|
+
h.OnClick(input.copied)
|
|
500
|
+
]), ["Copy"])]);
|
|
501
|
+
});
|
|
50
502
|
//#endregion
|
|
51
503
|
//#region src/handle.ts
|
|
52
504
|
/** The elements the handle publishes: its wrapper, the grip it is dragged by, and its two moves. */
|
|
@@ -56,19 +508,6 @@ const BlockHandleSlots = Slots.define({
|
|
|
56
508
|
up: Slot.make({ capability: Capability.Interactive }),
|
|
57
509
|
down: Slot.make({ capability: Capability.Interactive })
|
|
58
510
|
});
|
|
59
|
-
/** The blocks either side of a block in its own container. */
|
|
60
|
-
const siblingsOf = (document, node) => {
|
|
61
|
-
const found = RichText.locateBlock(document, node);
|
|
62
|
-
if (found === void 0) return {};
|
|
63
|
-
const container = found.path.slice(0, -1);
|
|
64
|
-
const parent = container.length === 0 ? void 0 : RichText.blockAtPath(document, container);
|
|
65
|
-
const blocks = parent?.type === "Node" ? parent.blocks ?? [] : document.children;
|
|
66
|
-
const index = found.path[found.path.length - 1];
|
|
67
|
-
return {
|
|
68
|
-
previous: blocks[index - 1],
|
|
69
|
-
next: blocks[index + 1]
|
|
70
|
-
};
|
|
71
|
-
};
|
|
72
511
|
/** The drag Mount lifted into the caller's Messages, as the grip carries it. */
|
|
73
512
|
const dragMount = (hostId, node, wrap) => Mount.mapMessage(blockDrag({
|
|
74
513
|
hostId,
|
|
@@ -81,7 +520,7 @@ const dragMount = (hostId, node, wrap) => Mount.mapMessage(blockDrag({
|
|
|
81
520
|
* disabled at its end of the container, and both when the block is not there.
|
|
82
521
|
*/
|
|
83
522
|
const blockHandle = () => SlotView.forMessages().define(BlockHandleSlots, (input, slots, h) => {
|
|
84
|
-
const { previous, next } = siblingsOf(input.document, input.node);
|
|
523
|
+
const { previous, next } = RichText.siblingsOf(input.document, input.node);
|
|
85
524
|
const move = (to) => input.wrap(Message.MovedBlock({
|
|
86
525
|
node: input.node,
|
|
87
526
|
to
|
|
@@ -379,33 +818,50 @@ const LinkEditorSlots = Slots.define({
|
|
|
379
818
|
remove: Slot.make({ capability: Capability.Interactive })
|
|
380
819
|
});
|
|
381
820
|
/**
|
|
821
|
+
* The edit applying a draft makes: the selection linked, or the link around the caret changed, to
|
|
822
|
+
* `RichText.safeUrl(draft)`. None when the policy refuses the address, or when there is neither
|
|
823
|
+
* a range nor a caret inside a link to put it on. The view's button sends it; a caller handling
|
|
824
|
+
* `submitted` sends it from its own draft.
|
|
825
|
+
*/
|
|
826
|
+
const linkEdit = (document, selection, draft) => {
|
|
827
|
+
const href = RichText.safeUrl(draft);
|
|
828
|
+
if (href === void 0) return Option.none();
|
|
829
|
+
if (RichText.linkAt(document, selection) === void 0 && !RichText.coversText(selection)) return Option.none();
|
|
830
|
+
return Option.some(Message.AppliedMark({ mark: {
|
|
831
|
+
name: "Link",
|
|
832
|
+
props: { href }
|
|
833
|
+
} }));
|
|
834
|
+
};
|
|
835
|
+
/**
|
|
382
836
|
* The link editor as a slot view. Applying needs an address the URL policy accepts
|
|
383
837
|
* (`RichText.safeUrl`), which is what it sends, and something to link: a range, or a caret
|
|
384
838
|
* inside a link. Otherwise the apply button is disabled and Enter in the field does nothing.
|
|
385
|
-
* The remove button is drawn only inside a link.
|
|
839
|
+
* The remove button is drawn only inside a link. Escape in the field sends `closed`.
|
|
386
840
|
*/
|
|
387
841
|
const linkEditor = () => SlotView.forMessages().define(LinkEditorSlots, (input, slots, h) => {
|
|
388
842
|
const link = RichText.linkAt(input.document, input.selection);
|
|
389
|
-
const
|
|
390
|
-
const apply = href === void 0 || link === void 0 && !RichText.coversText(input.selection) ? void 0 : input.wrap(Message.AppliedMark({ mark: {
|
|
391
|
-
name: "Link",
|
|
392
|
-
props: { href }
|
|
393
|
-
} }));
|
|
843
|
+
const apply = Option.getOrUndefined(Option.map(linkEdit(input.document, input.selection, input.draft), input.wrap));
|
|
394
844
|
return h.div(slots.root.attrs([h.Role("group"), h.AriaLabel("Link")]), [
|
|
395
845
|
h.input(slots.input.attrs([
|
|
396
846
|
h.Type("url"),
|
|
397
847
|
h.DataAttribute("link", "address"),
|
|
398
848
|
h.AriaLabel("Link address"),
|
|
849
|
+
h.Placeholder("Paste or type a link"),
|
|
399
850
|
h.Value(input.draft),
|
|
400
851
|
h.OnInput(input.drafted),
|
|
401
|
-
h.OnKeyDownPreventDefault((key) =>
|
|
852
|
+
h.OnKeyDownPreventDefault((key) => {
|
|
853
|
+
if (key === "Escape" && input.closed !== void 0) return Option.some(input.closed);
|
|
854
|
+
if (key !== "Enter") return Option.none();
|
|
855
|
+
if (input.submitted !== void 0) return Option.some(input.submitted);
|
|
856
|
+
return apply === void 0 ? Option.none() : Option.some(apply);
|
|
857
|
+
})
|
|
402
858
|
])),
|
|
403
859
|
h.button(slots.apply.attrs([
|
|
404
860
|
h.Type("button"),
|
|
405
861
|
h.DataAttribute("link", "apply"),
|
|
406
862
|
h.Disabled(apply === void 0),
|
|
407
863
|
...apply === void 0 ? [] : [h.OnClick(apply)]
|
|
408
|
-
]), [link === void 0 ? "
|
|
864
|
+
]), [link === void 0 ? "Apply" : "Update"]),
|
|
409
865
|
...link === void 0 ? [] : [h.button(slots.remove.attrs([
|
|
410
866
|
h.Type("button"),
|
|
411
867
|
h.DataAttribute("link", "remove"),
|
|
@@ -415,17 +871,26 @@ const linkEditor = () => SlotView.forMessages().define(LinkEditorSlots, (input,
|
|
|
415
871
|
});
|
|
416
872
|
//#endregion
|
|
417
873
|
//#region src/menu.ts
|
|
418
|
-
/**
|
|
874
|
+
/**
|
|
875
|
+
* The elements the menu publishes: its wrapper, its list, a heading over each group, one item per
|
|
876
|
+
* match with its icon, label and description, and what shows when nothing matches.
|
|
877
|
+
*/
|
|
419
878
|
const SlashMenuSlots = Slots.define({
|
|
420
879
|
root: Slot.make({ capability: Capability.Container }),
|
|
421
880
|
list: Slot.make({ capability: Capability.Collection }),
|
|
422
|
-
|
|
881
|
+
heading: Slot.make({ capability: Capability.Container }),
|
|
882
|
+
item: Slot.make({ capability: Capability.Interactive }),
|
|
883
|
+
icon: Slot.make({ capability: Capability.Container }),
|
|
884
|
+
label: Slot.make({ capability: Capability.Container }),
|
|
885
|
+
description: Slot.make({ capability: Capability.Container }),
|
|
886
|
+
empty: Slot.make({ capability: Capability.Container })
|
|
423
887
|
});
|
|
424
888
|
const slashMenuView = () => SlotView.forMessages().define(SlashMenuSlots, (input, slots, h) => {
|
|
425
889
|
const menu = slashMenu(input.entries, input.textBefore, input.index);
|
|
426
890
|
const matches = menu?.matches ?? [];
|
|
427
891
|
const highlighted = matches.findIndex((entry) => entry === menu?.highlighted);
|
|
428
|
-
|
|
892
|
+
const headed = menu?.query === "";
|
|
893
|
+
const item = (entry, index) => h.button(slots.item.attrs([
|
|
429
894
|
h.Type("button"),
|
|
430
895
|
h.Role("menuitem"),
|
|
431
896
|
h.DataAttribute("entry", entry.id),
|
|
@@ -435,7 +900,76 @@ const slashMenuView = () => SlotView.forMessages().define(SlashMenuSlots, (input
|
|
|
435
900
|
index,
|
|
436
901
|
id: entry.id,
|
|
437
902
|
count: matches.length
|
|
438
|
-
}), [
|
|
903
|
+
}), [
|
|
904
|
+
...entry.icon !== void 0 && isRichTextIcon(entry.icon) ? [h.span(slots.icon.attrs(), [richTextIcon(h, entry.icon)])] : [],
|
|
905
|
+
h.span(slots.label.attrs(), [entry.label]),
|
|
906
|
+
...entry.description === void 0 ? [] : [h.span(slots.description.attrs(), [entry.description])]
|
|
907
|
+
]);
|
|
908
|
+
const rows = matches.flatMap((entry, index) => headed && entry.group !== void 0 && entry.group !== matches[index - 1]?.group ? [h.div(slots.heading.attrs([h.Role("presentation")]), [entry.group]), item(entry, index)] : [item(entry, index)]);
|
|
909
|
+
return h.div(slots.root.attrs(), [matches.length === 0 ? h.p(slots.empty.attrs(), ["No matches"]) : h.div(slots.list.attrs([h.Role("menu"), h.AriaLabel("Insert")]), rows)]);
|
|
910
|
+
});
|
|
911
|
+
//#endregion
|
|
912
|
+
//#region src/contents.ts
|
|
913
|
+
/**
|
|
914
|
+
* The elements the contents publish: the `nav`, its list, and one entry per heading. An entry
|
|
915
|
+
* carries its depth as `--fk-toc-level` (1 for the shallowest level the document uses) and
|
|
916
|
+
* `aria-current="location"` for the section the selection is in.
|
|
917
|
+
*/
|
|
918
|
+
const TableOfContentsSlots = Slots.define({
|
|
919
|
+
root: Slot.make({ capability: Capability.Container }),
|
|
920
|
+
list: Slot.make({ capability: Capability.Container }),
|
|
921
|
+
entry: Slot.make({ capability: Capability.Interactive })
|
|
922
|
+
});
|
|
923
|
+
/** The contents as a slot view; nothing is drawn for a document with no headings. */
|
|
924
|
+
const tableOfContents = () => SlotView.forMessages().define(TableOfContentsSlots, (input, slots, h) => {
|
|
925
|
+
const entries = RichText.outlineOf(input.document);
|
|
926
|
+
if (entries.length === 0) return h.empty;
|
|
927
|
+
const top = Math.min(...entries.map((entry) => entry.level));
|
|
928
|
+
const current = RichText.sectionAt(input.document, input.selection);
|
|
929
|
+
return h.nav(slots.root.attrs([h.AriaLabel("Contents")]), [h.ul(slots.list.attrs(), entries.map((entry) => h.li([h.Key(entry.node)], [h.button(slots.entry.attrs([
|
|
930
|
+
h.Type("button"),
|
|
931
|
+
h.Style({ "--fk-toc-level": String(entry.level - top + 1) }),
|
|
932
|
+
...entry.node === current ? [h.AriaCurrent("location")] : [],
|
|
933
|
+
h.OnClick(input.chosen(entry.node))
|
|
934
|
+
]), [entry.text])])))]);
|
|
935
|
+
});
|
|
936
|
+
//#endregion
|
|
937
|
+
//#region src/footnote.ts
|
|
938
|
+
/** The elements the preview publishes: its card, the label field, the note's text, and the button. */
|
|
939
|
+
const FootnotePreviewSlots = Slots.define({
|
|
940
|
+
root: Slot.make({ capability: Capability.Container }),
|
|
941
|
+
label: Slot.make({ capability: Capability.TextInput }),
|
|
942
|
+
text: Slot.make({ capability: Capability.Container }),
|
|
943
|
+
button: Slot.make({ capability: Capability.Interactive })
|
|
944
|
+
});
|
|
945
|
+
/**
|
|
946
|
+
* The preview as a slot view, drawn while the caret is in a footnote reference and nothing
|
|
947
|
+
* otherwise. The label field renames the footnote when it changes (`RenamedFootnote`, which the
|
|
948
|
+
* editor refuses for a label in use). A note with no text reads "No note yet"; with no definition
|
|
949
|
+
* at all, Go to note is disabled.
|
|
950
|
+
*/
|
|
951
|
+
const footnotePreview = () => SlotView.forMessages().define(FootnotePreviewSlots, (input, slots, h) => {
|
|
952
|
+
const label = RichText.footnoteAt(input.document, input.selection);
|
|
953
|
+
if (label === void 0) return h.empty;
|
|
954
|
+
const note = RichText.footnotesOf(input.document).find((each) => each.label === label);
|
|
955
|
+
const definition = note?.definition;
|
|
956
|
+
return h.div(slots.root.attrs([h.Role("group"), h.AriaLabel(`Footnote ${label}`)]), [
|
|
957
|
+
h.input(slots.label.attrs([
|
|
958
|
+
h.Type("text"),
|
|
959
|
+
h.AriaLabel("Footnote label"),
|
|
960
|
+
h.Value(label),
|
|
961
|
+
h.OnChange((to) => input.wrap(Message.RenamedFootnote({
|
|
962
|
+
from: label,
|
|
963
|
+
to
|
|
964
|
+
})))
|
|
965
|
+
])),
|
|
966
|
+
h.p(slots.text.attrs(), [note === void 0 || note.text === "" ? "No note yet" : note.text]),
|
|
967
|
+
h.button(slots.button.attrs([
|
|
968
|
+
h.Type("button"),
|
|
969
|
+
h.Disabled(definition === void 0),
|
|
970
|
+
...definition === void 0 ? [] : [h.OnClick(input.opened(definition))]
|
|
971
|
+
]), ["Go to note"])
|
|
972
|
+
]);
|
|
439
973
|
});
|
|
440
974
|
//#endregion
|
|
441
975
|
//#region src/index.ts
|
|
@@ -475,6 +1009,6 @@ const markToolbar = () => SlotView.forMessages().define(MarkToolbarSlots, (input
|
|
|
475
1009
|
}), [mark])))]);
|
|
476
1010
|
});
|
|
477
1011
|
//#endregion
|
|
478
|
-
export { BlockHandleSlots, BlockStyleSlots, CommandPaletteSlots, EditorStatusSlots, LinkEditorSlots, MarkToolbarSlots, SlashMenuSlots, SourceEditorSlots, SourcePreviewSlots, blockHandle, blockStyles, commandPalette, editorStatus, linkEditor, markToolbar, matchingEntries, slashEntries, slashMenu, slashMenuView, slashMove, slashQuery, sourceEditor, sourcePreview };
|
|
1012
|
+
export { BlockHandleSlots, BlockStyleMenuSlots, BlockStyleSlots, CodeToolbarSlots, CommandPaletteSlots, EditorStatusSlots, FindReplaceSlots, FootnotePreviewSlots, LinkEditorSlots, MarkToolbarSlots, SlashMenuSlots, SourceEditorSlots, SourcePreviewSlots, TableOfContentsSlots, TableToolbarSlots, blockHandle, blockStyleMenu, blockStyles, codeToolbar, commandPalette, editorStatus, findReplace, footnotePreview, isRichTextIcon, linkEdit, linkEditor, markToolbar, matchingEntries, richTextIcon, richTextIconUrl, slashEntries, slashMenu, slashMenuView, slashMove, slashQuery, sourceEditor, sourcePreview, stepMatch, tableOfContents, tableToolbar };
|
|
479
1013
|
|
|
480
1014
|
//# sourceMappingURL=index.mjs.map
|