foldkit-mixins-richtext 0.9.0 → 0.11.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 +211 -6
- package/dist/code.d.ts.map +1 -1
- package/dist/code.js +6 -1
- package/dist/code.js.map +1 -1
- package/dist/formatting.d.ts +178 -0
- package/dist/formatting.d.ts.map +1 -0
- package/dist/formatting.js +480 -0
- package/dist/formatting.js.map +1 -0
- package/dist/handle.js +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/link.d.ts.map +1 -1
- package/dist/link.js +6 -1
- package/dist/link.js.map +1 -1
- package/dist/menu.d.ts.map +1 -1
- package/dist/menu.js +2 -1
- package/dist/menu.js.map +1 -1
- package/dist/palette.js +1 -1
- package/dist/style.d.ts +10 -1
- package/dist/style.d.ts.map +1 -1
- package/dist/style.js +4 -3
- package/dist/style.js.map +1 -1
- package/dist/table.d.ts.map +1 -1
- package/dist/table.js +2 -1
- package/dist/table.js.map +1 -1
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -6,11 +6,12 @@ every element the way it does any other view.
|
|
|
6
6
|
|
|
7
7
|
## Who owns what
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
- `foldkit-richtext-dom` owns the editor: its host element, the editable subtree and
|
|
10
|
+
every edit.
|
|
11
|
+
- Your application owns every piece of state the chrome shows, and where it is
|
|
12
|
+
drawn.
|
|
13
|
+
- `foldkit-mixins-richtext` (this package) owns nothing: each view is a function of
|
|
14
|
+
what it is given.
|
|
14
15
|
|
|
15
16
|
The editor renders its host and nothing else; the chrome beside
|
|
16
17
|
it is the application's. `foldkit-richtext-dom/toolbar` ships plain versions of some of it,
|
|
@@ -53,9 +54,24 @@ const Message = defineMessageUnion({
|
|
|
53
54
|
DraftedSource: { draft: Schema.String },
|
|
54
55
|
MovedInSource: { caret: Schema.Number },
|
|
55
56
|
LeftSource: {},
|
|
57
|
+
ToggledMenu: { menu: Schema.Literals(['style', 'insert', 'link', 'more']), open: Schema.Boolean },
|
|
58
|
+
FocusedControl: { id: Schema.String },
|
|
59
|
+
ToggledFind: {},
|
|
60
|
+
PickedFile: {},
|
|
61
|
+
ReturnedToText: {},
|
|
56
62
|
})
|
|
57
63
|
type Message = typeof Message.Type
|
|
58
|
-
const {
|
|
64
|
+
const {
|
|
65
|
+
DraftedLink,
|
|
66
|
+
DraftedSource,
|
|
67
|
+
MovedInSource,
|
|
68
|
+
LeftSource,
|
|
69
|
+
ToggledMenu,
|
|
70
|
+
FocusedControl,
|
|
71
|
+
ToggledFind,
|
|
72
|
+
PickedFile,
|
|
73
|
+
ReturnedToText,
|
|
74
|
+
} = Message
|
|
59
75
|
const edited = (message: EditorEvent): Message => Message.GotEditor({ message })
|
|
60
76
|
declare const model: {
|
|
61
77
|
readonly document: RichText.Document
|
|
@@ -63,6 +79,8 @@ declare const model: {
|
|
|
63
79
|
readonly linkDraft: string
|
|
64
80
|
readonly palette: { readonly query: string; readonly index: number } | null
|
|
65
81
|
readonly source: SourceSession | null
|
|
82
|
+
readonly menu: 'style' | 'insert' | 'link' | undefined
|
|
83
|
+
readonly tabStop: string | null
|
|
66
84
|
}
|
|
67
85
|
declare const h: HtmlBuilder<Message>
|
|
68
86
|
declare const document: RichText.Document
|
|
@@ -160,6 +178,193 @@ menu. The trigger is disabled without a text selection, and the menu draws nothi
|
|
|
160
178
|
| `label` | Container | a style's label |
|
|
161
179
|
| `check` | Container | the check beside the current style |
|
|
162
180
|
|
|
181
|
+
## The formatting toolbar
|
|
182
|
+
|
|
183
|
+
The standard bar: undo and redo, the text style menu, marks, link, lists, quote and an insert
|
|
184
|
+
menu, in one `role="toolbar"` with one tab stop. What is pressed or unavailable is a read of the
|
|
185
|
+
document and selection, as everywhere here. Two things are the caller's, because only the caller
|
|
186
|
+
knows them: which menu is open, and where the tab stop is.
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
import { formattingToolbar } from 'foldkit-mixins-richtext'
|
|
190
|
+
|
|
191
|
+
formattingToolbar<Message>()(
|
|
192
|
+
{
|
|
193
|
+
id: 'body-bar',
|
|
194
|
+
document: model.document,
|
|
195
|
+
selection: model.editor.selection,
|
|
196
|
+
storedMarks: model.editor.storedMarks,
|
|
197
|
+
wrap: edited,
|
|
198
|
+
menu: model.menu,
|
|
199
|
+
toggled: (menu, open) => ToggledMenu({ menu, open }),
|
|
200
|
+
roving: { current: model.tabStop },
|
|
201
|
+
onFocus: id => FocusedControl({ id }),
|
|
202
|
+
},
|
|
203
|
+
h,
|
|
204
|
+
)
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
`id` prefixes the ids of the controls, which the arrow keys move focus by, and is unique on the
|
|
208
|
+
page. `toggled` is what a menu's trigger sends, to open or close that menu; `menu` is the one
|
|
209
|
+
the application says is open. A choice in the style or insert menu goes through `wrap`, which is
|
|
210
|
+
where the caller closes the menu. `link` is a trigger only: the popup is `linkEditor`, which the
|
|
211
|
+
caller places when `menu` is `'link'`. `roving` and `onFocus` are the tab stop, the same slice
|
|
212
|
+
`foldkit-mixins-ui`'s Toolbar asks for: store the id `onFocus` sends and give it back, so that Tab
|
|
213
|
+
returns to the control last used. The first control holds the stop until one has had focus.
|
|
214
|
+
|
|
215
|
+
Choose controls with `items`, in order. Each is `'undo'`, `'redo'`, `'style'`, `'link'`,
|
|
216
|
+
`'bulleted-list'`, `'numbered-list'`, `'checklist'`, `'quote'`, `'insert'`, `'separator'`, or
|
|
217
|
+
`{ mark: 'Underline' }` for a mark of the document's vocabulary, or `{ control }` for one of the
|
|
218
|
+
application's own (below); the default is
|
|
219
|
+
`standardFormattingItems`, which lists the marks `foldkit-richtext` ships. A control left out is
|
|
220
|
+
not drawn, so it is not a tab stop: an application that drops the less used ones on a phone
|
|
221
|
+
passes a shorter list there. `inserts` is the insert menu's entries in one list the caller
|
|
222
|
+
orders: `InsertEntry.Editor({ entry })` for an entry of the slash menu's catalogue (sent through
|
|
223
|
+
`wrap`, so the two menus never disagree) and `InsertEntry.App({ id, label, description?, icon?,
|
|
224
|
+
message })` for the application's own, whose Message is sent as it is. The default is the editor's
|
|
225
|
+
blocks. `history` says whether undo and redo apply:
|
|
226
|
+
the editor cannot know what the application undoes, so a page that undoes only its own steps
|
|
227
|
+
passes `{ undo: false, redo: true }`. `words` overrides the toolbar's name and its controls'.
|
|
228
|
+
`shortcuts` adds `foldkit-primitives` patterns (`'mod+u'`) by control id (`mark-Underline`) beside
|
|
229
|
+
the ones the editor itself binds (undo, redo, Bold, Italic, Code, Strikethrough), and each shows as
|
|
230
|
+
the title and `aria-keyshortcuts`.
|
|
231
|
+
|
|
232
|
+
The keys are the APG toolbar's: ArrowLeft and ArrowRight (mirrored with `direction: 'rtl'`), Home
|
|
233
|
+
and End move between controls and wrap, and Tab leaves. ArrowDown on a closed menu's trigger opens
|
|
234
|
+
it; a second ArrowDown, once its items are drawn, enters the menu, where the arrows, Home and End
|
|
235
|
+
walk the items and Escape closes it and returns to the trigger. A control that is on is
|
|
236
|
+
`aria-pressed` (a mark the selection carries, a link around the caret); a menu's trigger is
|
|
237
|
+
`aria-expanded`, and the style trigger names the current style. An unavailable control (a mark or
|
|
238
|
+
a wrap without a selection, the style menu without a text selection, link where the selection
|
|
239
|
+
covers no text, undo or redo as `history` says) is `aria-disabled`, stays focusable so the arrows
|
|
240
|
+
still reach it, and sends nothing. It is never natively `disabled`, which would take it out of
|
|
241
|
+
the tab order. The package ships no styles: the application styles the slots, and a bar that
|
|
242
|
+
should fit a phone wraps (`flex-wrap`) or scrolls in its own `root`.
|
|
243
|
+
|
|
244
|
+
### The application's own controls and entries
|
|
245
|
+
|
|
246
|
+
An item `{ control: { id, label, icon?, shortcut?, pressed?, disabled?, message, popup? } }` joins
|
|
247
|
+
the same tab stop and arrow order, where `items` puts it; its element and `data-item` are
|
|
248
|
+
`app-<id>`. A press sends `message`. While `disabled` it is `aria-disabled`, stays focusable and
|
|
249
|
+
sends nothing, but the view cannot make a Message unsendable from elsewhere: the application's
|
|
250
|
+
`update` must refuse it in that state too. `popup: { kind, open }` makes it a trigger for something
|
|
251
|
+
the application draws, as `link` is: `aria-haspopup` says which kind, a `menu` is `aria-expanded`
|
|
252
|
+
while `open`, and `message` toggles it. ArrowDown on a closed menu's trigger, and Escape on an open
|
|
253
|
+
popup's, send `message`.
|
|
254
|
+
|
|
255
|
+
```ts
|
|
256
|
+
import { formattingToolbar, InsertEntry } from 'foldkit-mixins-richtext'
|
|
257
|
+
|
|
258
|
+
formattingToolbar<Message>()(
|
|
259
|
+
{
|
|
260
|
+
id: 'body-bar',
|
|
261
|
+
document: model.document,
|
|
262
|
+
selection: model.editor.selection,
|
|
263
|
+
wrap: edited,
|
|
264
|
+
toggled: (menu, open) => ToggledMenu({ menu, open }),
|
|
265
|
+
roving: { current: model.tabStop },
|
|
266
|
+
onFocus: id => FocusedControl({ id }),
|
|
267
|
+
items: [
|
|
268
|
+
'undo',
|
|
269
|
+
'redo',
|
|
270
|
+
'separator',
|
|
271
|
+
{ mark: 'Bold' },
|
|
272
|
+
{ control: { id: 'find', label: 'Find', icon: 'search', message: ToggledFind() } },
|
|
273
|
+
'insert',
|
|
274
|
+
],
|
|
275
|
+
inserts: [InsertEntry.App({ id: 'file', label: 'Image, video or file', message: PickedFile() })],
|
|
276
|
+
returned: ReturnedToText(),
|
|
277
|
+
},
|
|
278
|
+
h,
|
|
279
|
+
)
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
### Narrow screens
|
|
283
|
+
|
|
284
|
+
Wrap an item as `{ low: item }` to say it gives way: with `compact: true` it is not drawn in the
|
|
285
|
+
bar, and a separator a hidden group leaves at an end or beside another is dropped. The bar ends
|
|
286
|
+
with a **More** button instead, whose menu lists the low controls in order with their names. Each
|
|
287
|
+
row sends the control's own Message; a toggle (Code, Strikethrough, an application control with
|
|
288
|
+
`pressed`) is a `menuitemcheckbox` that says whether it is on, and a control that does not apply
|
|
289
|
+
(undo with nothing to undo) is an `aria-disabled` row that sends nothing. The button is part of the
|
|
290
|
+
bar's one tab stop and its arrow keys; ArrowDown opens the menu and a second enters it, and Escape
|
|
291
|
+
returns to the button. The application keeps the open menu as it does for the others (`menu:
|
|
292
|
+
'more'`, `toggled('more', open)`), and a press of a row goes through `wrap`, where it closes the
|
|
293
|
+
menu. A low control that opens a popup of its own (`style`, `insert`, `link`, an application
|
|
294
|
+
control with a `popup`) has no form in that menu, so a compact bar leaves it out: do not mark one
|
|
295
|
+
`low` unless the narrow screen can do without it. `words.more` names the button and its menu. If
|
|
296
|
+
the control the tab stop names is the one that goes, the stop falls to the nearest control still
|
|
297
|
+
drawn (the next one, else the one before).
|
|
298
|
+
|
|
299
|
+
The bar knows no width: the application keeps `compact` in its Model. A window's media query is one
|
|
300
|
+
signal (`foldkit-primitives/media`'s `MediaQuery.with({ query: '(max-width: 40rem)' })`, a Bundle
|
|
301
|
+
holding `matches` that a `matchMedia` Subscription keeps current: pass `compact:
|
|
302
|
+
model.narrow.matches`). Where the bar sits in a column narrower than the window (a panel beside
|
|
303
|
+
it), measure the bar's own width instead: a `Resize` mount from `foldkit-primitives/observers` on
|
|
304
|
+
the element around it, kept in the Model as a number, and `compact` is that number under what the
|
|
305
|
+
bar needs (the CMS example does this).
|
|
306
|
+
|
|
307
|
+
```ts
|
|
308
|
+
formattingToolbar<Message>()(
|
|
309
|
+
{
|
|
310
|
+
id: 'body-bar',
|
|
311
|
+
document: model.document,
|
|
312
|
+
selection: model.editor.selection,
|
|
313
|
+
wrap: edited,
|
|
314
|
+
toggled: (menu, open) => ToggledMenu({ menu, open }),
|
|
315
|
+
roving: { current: model.tabStop },
|
|
316
|
+
onFocus: id => FocusedControl({ id }),
|
|
317
|
+
items: ['undo', 'redo', 'separator', { mark: 'Bold' }, { low: { mark: 'Code' } }, { low: 'quote' }],
|
|
318
|
+
compact: true,
|
|
319
|
+
// The Code and Quote controls are in the More menu, which `menu: 'more'` opens.
|
|
320
|
+
},
|
|
321
|
+
h,
|
|
322
|
+
)
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
### Where the focus goes
|
|
326
|
+
|
|
327
|
+
**A pointer press leaves the focus in the text.** The bar's root carries `data-editor-controls`
|
|
328
|
+
(`CONTROLS_ATTRIBUTE` from `foldkit-richtext-dom/toolbar`), and while the focus is in the text the
|
|
329
|
+
editor holds back the focus change of a press inside such an element. The click still happens. So
|
|
330
|
+
after pressing Bold the same text is still selected in the browser, pressing Italic marks it too,
|
|
331
|
+
and typing replaces it. A menu opened by a press is chosen from by a press, and its entry lands on
|
|
332
|
+
the selection or caret that is still in the text. No control took the focus, so the tab stop does
|
|
333
|
+
not move. A press made while a bar control or another field has the focus moves the focus to the
|
|
334
|
+
pressed control, as any button does.
|
|
335
|
+
|
|
336
|
+
**A control reached by key holds the focus.** After Tab or Shift+Tab to the bar and the arrows, a
|
|
337
|
+
command (Enter or Space on Bold, a list wrap, undo) leaves the focus on its button, so the next
|
|
338
|
+
control is an arrow away. Every command reads the selection the editor keeps in the Model, which
|
|
339
|
+
the focus leaving does not change, so two commands in a row act on the same text. The browser
|
|
340
|
+
cannot show a selection in the text while a button has the focus, so the editor draws it as a
|
|
341
|
+
decoration, `span[data-decoration="selection"]`, which the application's stylesheet paints as
|
|
342
|
+
selected text. Typing then is not typing in the text: a letter reaches the button, and Space
|
|
343
|
+
presses it again.
|
|
344
|
+
|
|
345
|
+
To go back to the text, press Escape on a control. It sends `returned`, usually
|
|
346
|
+
a Message whose `update` runs `foldkit-richtext-dom/editor-bundle`'s `focus(hostId, selection)`;
|
|
347
|
+
without `returned` Escape does nothing there. It closes an open popup first, and then returns.
|
|
348
|
+
Tab into the text returns too. Either way the decoration goes and the selection is the browser's
|
|
349
|
+
again, so typing replaces it. To come back to the bar, Tab or Shift+Tab reaches the control last
|
|
350
|
+
focused.
|
|
351
|
+
|
|
352
|
+
`markToolbar`, `blockStyles`, `blockStyleMenu`, `tableToolbar`, `codeToolbar`, `slashMenuView` and
|
|
353
|
+
`linkEditor` mark their roots the same way. A field in a marked element (the link editor's
|
|
354
|
+
address) takes the focus as usual. Mark your own chrome that acts on the selection, such as a
|
|
355
|
+
block menu, with `h.Attribute(CONTROLS_ATTRIBUTE, '')`.
|
|
356
|
+
|
|
357
|
+
| Slot | Capability | Renders |
|
|
358
|
+
| --- | --- | --- |
|
|
359
|
+
| `root` | Container | the bar, `role="toolbar"`, `data-editor-controls`; owns `keydown` |
|
|
360
|
+
| `button` | Interactive | one control, `data-item` its id, `aria-pressed` or `aria-expanded` |
|
|
361
|
+
| `separator` | Container | a rule between sets |
|
|
362
|
+
| `anchor` | Container | a menu's trigger and its open panel, to anchor the panel to |
|
|
363
|
+
| `label` | Container | a trigger's text |
|
|
364
|
+
| `menu` | Collection | the open panel, `role="menu"` |
|
|
365
|
+
| `item` | Interactive | one entry, `data-style` or `data-insert` its id |
|
|
366
|
+
| `itemLabel`, `itemDescription` | Container | an entry's label and description |
|
|
367
|
+
|
|
163
368
|
## The link editor
|
|
164
369
|
|
|
165
370
|
An address field with two buttons: link the selection (or change the link around the caret),
|
package/dist/code.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"code.d.ts","names":[],"sources":["../src/code.ts"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"code.d.ts","names":[],"sources":["../src/code.ts"],"mappings":";;;;qBAaa,kBAAgB,MAAA;;;;;;iBAOZ;WACN;WACA;;iBAGM,iBAAiB;WACvB,UAAU,SAAS;;WAEnB,MAAM,SAAS;;WAEf,WAAW,cAAc;;WAEzB,OAAO,SAAS,gBAAgB;;WAEhC,QAAQ;;;;;;;qBAQN,cAAe,cAAY,SAAS,gBACxC,kBACP,iBAAiB,UACjB"}
|
package/dist/code.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Capability, Slot, SlotView, Slots } from "foldkit-mixins";
|
|
2
2
|
import * as RichText from "foldkit-richtext";
|
|
3
3
|
import { Message } from "foldkit-richtext-dom/editor";
|
|
4
|
+
import { CONTROLS_ATTRIBUTE } from "foldkit-richtext-dom/toolbar";
|
|
4
5
|
// src/code.ts
|
|
5
6
|
/** The elements the toolbar publishes: its wrapper, the language picker, and the Copy button. */
|
|
6
7
|
const CodeToolbarSlots = Slots.define({
|
|
@@ -21,7 +22,11 @@ const codeToolbar = () => SlotView.forMessages().define(CodeToolbarSlots, (input
|
|
|
21
22
|
id: language,
|
|
22
23
|
label: language
|
|
23
24
|
}];
|
|
24
|
-
return h.div(slots.root.attrs([
|
|
25
|
+
return h.div(slots.root.attrs([
|
|
26
|
+
h.Role("toolbar"),
|
|
27
|
+
h.AriaLabel("Code"),
|
|
28
|
+
h.Attribute(CONTROLS_ATTRIBUTE, "")
|
|
29
|
+
]), [h.select(slots.language.attrs([
|
|
25
30
|
h.AriaLabel("Language"),
|
|
26
31
|
h.Value(language),
|
|
27
32
|
h.OnChange((chosen) => input.wrap(Message.ChangedProps({
|
package/dist/code.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"code.js","names":["EditorMessage"],"sources":["../src/code.ts"],"sourcesContent":["/**\n * The code block toolbar: the block's language, chosen from the caller's list, and a Copy\n * button, drawn through slots. The language is the block's `language` prop, set through the\n * editor's `ChangedProps`, so it is saved and undone with the text; copying is the caller's\n * own Message, since writing to the clipboard is a Command the editor does not own.\n */\nimport type { Html } from 'foldkit/html'\nimport { Capability, Slot, Slots, SlotView } from 'foldkit-mixins'\nimport * as RichText from 'foldkit-richtext'\nimport { Message as EditorMessage, type EditorEvent } from 'foldkit-richtext-dom/editor'\n\n/** The elements the toolbar publishes: its wrapper, the language picker, and the Copy button. */\nexport const CodeToolbarSlots = Slots.define({\n root: Slot.make({ capability: Capability.Container }),\n language: Slot.make({ capability: Capability.Interactive }),\n copy: Slot.make({ capability: Capability.Interactive }),\n})\n\n/** A language the picker offers: the `language` prop's value, and what the picker shows. */\nexport interface CodeLanguage {\n readonly id: string\n readonly label: string\n}\n\nexport interface CodeToolbarInput<Message> {\n readonly document: RichText.Document\n /** The code block the toolbar is for. */\n readonly node: RichText.NodeId\n /** What the picker offers, in order; `''` is code with no language. */\n readonly languages: ReadonlyArray<CodeLanguage>\n /** An editor Message as this caller's, usually its `edited(...)` wrapper. */\n readonly wrap: (message: EditorEvent) => Message\n /** What Copy sends: the caller copies the block's text (`RichText.toText([block])`). */\n readonly copied: Message\n}\n\n/**\n * The code block toolbar as a slot view. The picker shows the block's language, and a language\n * the list does not name is offered as itself, so choosing nothing never changes it. Nothing is\n * drawn for a block that is not there.\n */\nexport const codeToolbar = <Message>(): SlotView.SlotView<\n typeof CodeToolbarSlots,\n CodeToolbarInput<Message>,\n Message\n> =>\n SlotView.forMessages<Message>().define(CodeToolbarSlots, (input, slots, h): Html => {\n const block = RichText.locateBlock(input.document, input.node)?.block\n const current = block?.type === 'Node' ? block.props.language : undefined\n const language = typeof current === 'string' ? current : ''\n const languages = input.languages.some(each => each.id === language)\n ? input.languages\n : [...input.languages, { id: language, label: language }]\n return h.div(slots.root.attrs([h.Role('toolbar')
|
|
1
|
+
{"version":3,"file":"code.js","names":["EditorMessage"],"sources":["../src/code.ts"],"sourcesContent":["/**\n * The code block toolbar: the block's language, chosen from the caller's list, and a Copy\n * button, drawn through slots. The language is the block's `language` prop, set through the\n * editor's `ChangedProps`, so it is saved and undone with the text; copying is the caller's\n * own Message, since writing to the clipboard is a Command the editor does not own.\n */\nimport type { Html } from 'foldkit/html'\nimport { Capability, Slot, Slots, SlotView } from 'foldkit-mixins'\nimport * as RichText from 'foldkit-richtext'\nimport { Message as EditorMessage, type EditorEvent } from 'foldkit-richtext-dom/editor'\nimport { CONTROLS_ATTRIBUTE } from 'foldkit-richtext-dom/toolbar'\n\n/** The elements the toolbar publishes: its wrapper, the language picker, and the Copy button. */\nexport const CodeToolbarSlots = Slots.define({\n root: Slot.make({ capability: Capability.Container }),\n language: Slot.make({ capability: Capability.Interactive }),\n copy: Slot.make({ capability: Capability.Interactive }),\n})\n\n/** A language the picker offers: the `language` prop's value, and what the picker shows. */\nexport interface CodeLanguage {\n readonly id: string\n readonly label: string\n}\n\nexport interface CodeToolbarInput<Message> {\n readonly document: RichText.Document\n /** The code block the toolbar is for. */\n readonly node: RichText.NodeId\n /** What the picker offers, in order; `''` is code with no language. */\n readonly languages: ReadonlyArray<CodeLanguage>\n /** An editor Message as this caller's, usually its `edited(...)` wrapper. */\n readonly wrap: (message: EditorEvent) => Message\n /** What Copy sends: the caller copies the block's text (`RichText.toText([block])`). */\n readonly copied: Message\n}\n\n/**\n * The code block toolbar as a slot view. The picker shows the block's language, and a language\n * the list does not name is offered as itself, so choosing nothing never changes it. Nothing is\n * drawn for a block that is not there.\n */\nexport const codeToolbar = <Message>(): SlotView.SlotView<\n typeof CodeToolbarSlots,\n CodeToolbarInput<Message>,\n Message\n> =>\n SlotView.forMessages<Message>().define(CodeToolbarSlots, (input, slots, h): Html => {\n const block = RichText.locateBlock(input.document, input.node)?.block\n const current = block?.type === 'Node' ? block.props.language : undefined\n const language = typeof current === 'string' ? current : ''\n const languages = input.languages.some(each => each.id === language)\n ? input.languages\n : [...input.languages, { id: language, label: language }]\n return h.div(\n slots.root.attrs([\n h.Role('toolbar'),\n h.AriaLabel('Code'),\n h.Attribute(CONTROLS_ATTRIBUTE, ''),\n ]),\n [\n h.select(\n slots.language.attrs([\n h.AriaLabel('Language'),\n h.Value(language),\n h.OnChange(chosen =>\n input.wrap(\n EditorMessage.ChangedProps({ node: input.node, props: { language: chosen } }),\n ),\n ),\n ]),\n languages.map(each =>\n h.option([h.Value(each.id), h.Selected(each.id === language)], [each.label]),\n ),\n ),\n h.button(\n slots.copy.attrs([\n h.Type('button'),\n h.DataAttribute('code', 'copy'),\n h.OnClick(input.copied),\n ]),\n ['Copy'],\n ),\n ],\n )\n })\n"],"mappings":";;;;;;AAaA,MAAa,mBAAmB,MAAM,OAAO;CAC3C,MAAM,KAAK,KAAK,EAAE,YAAY,WAAW,UAAU,CAAC;CACpD,UAAU,KAAK,KAAK,EAAE,YAAY,WAAW,YAAY,CAAC;CAC1D,MAAM,KAAK,KAAK,EAAE,YAAY,WAAW,YAAY,CAAC;AACxD,CAAC;;;;;;AAyBD,MAAa,oBAKX,SAAS,YAAqB,CAAC,CAAC,OAAO,mBAAmB,OAAO,OAAO,MAAY;CAClF,MAAM,QAAQ,SAAS,YAAY,MAAM,UAAU,MAAM,IAAI,CAAC,EAAE;CAChE,MAAM,UAAU,OAAO,SAAS,SAAS,MAAM,MAAM,WAAW,KAAA;CAChE,MAAM,WAAW,OAAO,YAAY,WAAW,UAAU;CACzD,MAAM,YAAY,MAAM,UAAU,MAAK,SAAQ,KAAK,OAAO,QAAQ,IAC/D,MAAM,YACN,CAAC,GAAG,MAAM,WAAW;EAAE,IAAI;EAAU,OAAO;CAAS,CAAC;CAC1D,OAAO,EAAE,IACP,MAAM,KAAK,MAAM;EACf,EAAE,KAAK,SAAS;EAChB,EAAE,UAAU,MAAM;EAClB,EAAE,UAAU,oBAAoB,EAAE;CACpC,CAAC,GACD,CACE,EAAE,OACA,MAAM,SAAS,MAAM;EACnB,EAAE,UAAU,UAAU;EACtB,EAAE,MAAM,QAAQ;EAChB,EAAE,UAAS,WACT,MAAM,KACJA,QAAc,aAAa;GAAE,MAAM,MAAM;GAAM,OAAO,EAAE,UAAU,OAAO;EAAE,CAAC,CAC9E,CACF;CACF,CAAC,GACD,UAAU,KAAI,SACZ,EAAE,OAAO,CAAC,EAAE,MAAM,KAAK,EAAE,GAAG,EAAE,SAAS,KAAK,OAAO,QAAQ,CAAC,GAAG,CAAC,KAAK,KAAK,CAAC,CAC7E,CACF,GACA,EAAE,OACA,MAAM,KAAK,MAAM;EACf,EAAE,KAAK,QAAQ;EACf,EAAE,cAAc,QAAQ,MAAM;EAC9B,EAAE,QAAQ,MAAM,MAAM;CACxB,CAAC,GACD,CAAC,MAAM,CACT,CACF,CACF;AACF,CAAC"}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { Capability, Event, Slot, SlotView, Slots } from "foldkit-mixins";
|
|
2
|
+
import * as RichText from "foldkit-richtext";
|
|
3
|
+
import { EditorEvent, SlashEntry } from "foldkit-richtext-dom/editor";
|
|
4
|
+
import { Data } from "effect";
|
|
5
|
+
import { RichTextIconName } from "./icons.js";
|
|
6
|
+
import { RovingTabindex } from "foldkit-primitives/interaction";
|
|
7
|
+
/** The elements the toolbar publishes. `anchor` holds a menu's trigger and its panel. */
|
|
8
|
+
export declare const FormattingToolbarSlots: Slots.Contract<{
|
|
9
|
+
readonly root: Slot.UnnamedSlot<Capability.Capability<"Container", readonly [Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>]>, readonly [Event.EventToken<"keydown">], readonly Slot.AttributeName[], readonly Slot.RequirementName[], boolean>;
|
|
10
|
+
readonly button: Slot.UnnamedSlot<Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>, readonly Slot.EventName[], readonly Slot.AttributeName[], readonly Slot.RequirementName[], boolean>;
|
|
11
|
+
readonly separator: Slot.UnnamedSlot<Capability.Capability<"Container", readonly [Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>]>, readonly Slot.EventName[], readonly Slot.AttributeName[], readonly Slot.RequirementName[], boolean>;
|
|
12
|
+
readonly anchor: Slot.UnnamedSlot<Capability.Capability<"Container", readonly [Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>]>, readonly Slot.EventName[], readonly Slot.AttributeName[], readonly Slot.RequirementName[], boolean>;
|
|
13
|
+
readonly label: Slot.UnnamedSlot<Capability.Capability<"Container", readonly [Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>]>, readonly Slot.EventName[], readonly Slot.AttributeName[], readonly Slot.RequirementName[], boolean>;
|
|
14
|
+
readonly menu: Slot.UnnamedSlot<Capability.Capability<"Collection", readonly [Capability.Capability<"Base", readonly []>]>, readonly Slot.EventName[], readonly Slot.AttributeName[], readonly Slot.RequirementName[], boolean>;
|
|
15
|
+
readonly item: Slot.UnnamedSlot<Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>, readonly Slot.EventName[], readonly Slot.AttributeName[], readonly Slot.RequirementName[], boolean>;
|
|
16
|
+
readonly itemLabel: Slot.UnnamedSlot<Capability.Capability<"Container", readonly [Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>]>, readonly Slot.EventName[], readonly Slot.AttributeName[], readonly Slot.RequirementName[], boolean>;
|
|
17
|
+
readonly itemDescription: Slot.UnnamedSlot<Capability.Capability<"Container", readonly [Capability.Capability<"Interactive", readonly [Capability.Capability<"Base", readonly []>]>]>, readonly Slot.EventName[], readonly Slot.AttributeName[], readonly Slot.RequirementName[], boolean>;
|
|
18
|
+
}>;
|
|
19
|
+
/** The popups a control opens; the caller keeps which one is open, and draws `link`'s own editor. */
|
|
20
|
+
export type FormattingMenu = 'style' | 'insert' | 'link' | 'more';
|
|
21
|
+
/**
|
|
22
|
+
* A control of the application's own in the bar: it joins the same tab stop and the same arrow
|
|
23
|
+
* keys, in the place `items` gives it. `message` is what a press sends. A press of a `disabled`
|
|
24
|
+
* control sends nothing (the control is `aria-disabled` and stays focusable), and the
|
|
25
|
+
* application's `update` must refuse the Message in that state too, since a Message can arrive
|
|
26
|
+
* from elsewhere.
|
|
27
|
+
*/
|
|
28
|
+
export interface ApplicationControl<Message> {
|
|
29
|
+
/** Unique among the application's controls; the element's id and `data-item` are `app-<id>`. */
|
|
30
|
+
readonly id: string;
|
|
31
|
+
/** The control's accessible name, and its visible text where it has no `icon`. */
|
|
32
|
+
readonly label: string;
|
|
33
|
+
readonly icon?: RichTextIconName | undefined;
|
|
34
|
+
/** A `foldkit-primitives` pattern (`'mod+k'`), shown in the title and as `aria-keyshortcuts`. */
|
|
35
|
+
readonly shortcut?: string | undefined;
|
|
36
|
+
/** `aria-pressed`, for a control that is on; absent for one that is not a toggle. */
|
|
37
|
+
readonly pressed?: boolean | undefined;
|
|
38
|
+
readonly disabled?: boolean | undefined;
|
|
39
|
+
readonly message: Message;
|
|
40
|
+
/**
|
|
41
|
+
* A control that opens something the application draws, as `link` opens the link editor:
|
|
42
|
+
* `aria-haspopup` says which kind and, for a `menu`, `aria-expanded` says whether it is `open`.
|
|
43
|
+
* `message` then toggles it. ArrowDown on a closed menu's trigger and Escape on an open popup's
|
|
44
|
+
* trigger send `message`, as for the built-in menus.
|
|
45
|
+
*/
|
|
46
|
+
readonly popup?: {
|
|
47
|
+
readonly kind: 'menu' | 'dialog';
|
|
48
|
+
readonly open: boolean;
|
|
49
|
+
} | undefined;
|
|
50
|
+
}
|
|
51
|
+
type PlacedItem<Message> = 'undo' | 'redo' | 'style' | 'link' | 'bulleted-list' | 'numbered-list' | 'checklist' | 'quote' | 'insert' | 'separator' | 'more' | {
|
|
52
|
+
readonly mark: string;
|
|
53
|
+
} | {
|
|
54
|
+
readonly control: ApplicationControl<Message>;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* One control, in the order drawn. A `mark` is a mark of the document's vocabulary, by name: the
|
|
58
|
+
* toolbar cannot know which marks an application declared, so it lists none by itself beyond
|
|
59
|
+
* `standardFormattingItems`'. A `control` is the application's own. `{ low: item }` is a control
|
|
60
|
+
* that gives way when the bar is `compact`: it is not drawn in the bar then, and is listed in the
|
|
61
|
+
* "More" menu the bar ends with instead, in its order, with its name, pressed and unavailable
|
|
62
|
+
* states. A `low` control that opens a popup of its own (`style`, `insert`, `link`, or an
|
|
63
|
+
* application control with a `popup`) has no form in that menu, so a compact bar leaves it out.
|
|
64
|
+
*/
|
|
65
|
+
export type FormattingItem<Message = never> = Exclude<PlacedItem<Message>, 'more'> | {
|
|
66
|
+
readonly low: Exclude<PlacedItem<Message>, 'separator' | 'more'>;
|
|
67
|
+
};
|
|
68
|
+
type InsertEntryUnion<Message> = Data.TaggedEnum<{
|
|
69
|
+
/** An entry of the slash menu's catalogue: its Message is the editor's, sent through `wrap`. */
|
|
70
|
+
Editor: {
|
|
71
|
+
readonly entry: SlashEntry<EditorEvent>;
|
|
72
|
+
};
|
|
73
|
+
/** An entry of the application's own, such as a file dialog: its Message is the application's. */
|
|
74
|
+
App: {
|
|
75
|
+
readonly id: string;
|
|
76
|
+
readonly label: string;
|
|
77
|
+
readonly description?: string | undefined;
|
|
78
|
+
readonly icon?: string | undefined;
|
|
79
|
+
readonly message: Message;
|
|
80
|
+
};
|
|
81
|
+
}>;
|
|
82
|
+
interface InsertEntries extends Data.TaggedEnum.WithGenerics<1> {
|
|
83
|
+
readonly taggedEnum: InsertEntryUnion<this['A']>;
|
|
84
|
+
}
|
|
85
|
+
/** One entry of the insert menu. A caller lists editor and application entries in one order. */
|
|
86
|
+
export type InsertEntry<Message> = Data.TaggedEnum.Kind<InsertEntries, Message>;
|
|
87
|
+
/** Builds an insert entry: `InsertEntry.Editor({ entry })` or `InsertEntry.App({ id, label, message })`. */
|
|
88
|
+
export declare const InsertEntry: Data.TaggedEnum.GenericConstructor<InsertEntries>;
|
|
89
|
+
/** What a plain bar offers: the marks `foldkit-richtext` ships, the lists and the quote. */
|
|
90
|
+
export declare const standardFormattingItems: ReadonlyArray<FormattingItem>;
|
|
91
|
+
/** The words the toolbar draws and names its controls by; a caller overrides any of them. */
|
|
92
|
+
export interface FormattingWords {
|
|
93
|
+
readonly toolbar: string;
|
|
94
|
+
readonly undo: string;
|
|
95
|
+
readonly redo: string;
|
|
96
|
+
readonly textStyle: string;
|
|
97
|
+
readonly link: string;
|
|
98
|
+
readonly bulletedList: string;
|
|
99
|
+
readonly numberedList: string;
|
|
100
|
+
readonly checklist: string;
|
|
101
|
+
readonly quote: string;
|
|
102
|
+
readonly insert: string;
|
|
103
|
+
/** The overflow menu's button and its menu's name, on a compact bar. */
|
|
104
|
+
readonly more: string;
|
|
105
|
+
}
|
|
106
|
+
export interface FormattingToolbarInput<Message> {
|
|
107
|
+
/** Prefixes the ids the controls draw, which the arrows move focus by; unique on the page. */
|
|
108
|
+
readonly id: string;
|
|
109
|
+
readonly document: RichText.Document;
|
|
110
|
+
readonly selection: RichText.Selection | null;
|
|
111
|
+
/** The caret's pending format, which wins over the run under it (`ToolbarState`). */
|
|
112
|
+
readonly storedMarks?: ReadonlyArray<string> | null | undefined;
|
|
113
|
+
/** An editor Message as this caller's, usually its `edited(...)` wrapper. */
|
|
114
|
+
readonly wrap: (message: EditorEvent) => Message;
|
|
115
|
+
/** The controls and their order; default `standardFormattingItems`. Leave a control out to hide it. */
|
|
116
|
+
readonly items?: ReadonlyArray<FormattingItem<NoInfer<Message>>> | undefined;
|
|
117
|
+
/**
|
|
118
|
+
* Draw only the controls not marked `{ low }`, and the rest in a "More" menu: the application's
|
|
119
|
+
* answer to a narrow screen, from the width signal it keeps in its Model (a media-query
|
|
120
|
+
* Subscription, or a `Resize` mount on the bar's own column). Default `false`.
|
|
121
|
+
*/
|
|
122
|
+
readonly compact?: boolean | undefined;
|
|
123
|
+
/**
|
|
124
|
+
* Whether undo and redo apply. The editor cannot know what the application undoes (a page may
|
|
125
|
+
* undo only this tab's own steps), so the caller says. Default: both available.
|
|
126
|
+
*/
|
|
127
|
+
readonly history?: {
|
|
128
|
+
readonly undo: boolean;
|
|
129
|
+
readonly redo: boolean;
|
|
130
|
+
} | undefined;
|
|
131
|
+
/** The menu that is open, if any. */
|
|
132
|
+
readonly menu?: FormattingMenu | undefined;
|
|
133
|
+
/** What a menu's trigger sends: open (`true`) or close (`false`) that menu. Also what Escape sends. */
|
|
134
|
+
readonly toggled: (menu: FormattingMenu, open: boolean) => NoInfer<Message>;
|
|
135
|
+
/**
|
|
136
|
+
* What the insert menu lists, in order: the slash menu's own entries (`InsertEntry.Editor`, sent
|
|
137
|
+
* through `wrap`, so the two never disagree) and the application's (`InsertEntry.App`, sent as
|
|
138
|
+
* they are). Default: the editor's own blocks (`group: 'Blocks'`).
|
|
139
|
+
*/
|
|
140
|
+
readonly inserts?: ReadonlyArray<InsertEntry<NoInfer<Message>>> | undefined;
|
|
141
|
+
/**
|
|
142
|
+
* What Escape sends from a control when it closes no popup: usually a Message whose `update` runs
|
|
143
|
+
* the editor's `focus` Command, so Escape returns to the text, as in a conventional toolbar. Without
|
|
144
|
+
* it Escape does nothing there.
|
|
145
|
+
*/
|
|
146
|
+
readonly returned?: NoInfer<Message> | undefined;
|
|
147
|
+
/** Where the one tab stop is: the caller's `RovingTabindex.Model`, as for `foldkit-mixins-ui`'s Toolbar. */
|
|
148
|
+
readonly roving: RovingTabindex.Model;
|
|
149
|
+
/** Sent with a control's id when it takes focus, by a key or a press; answer it by storing the id as `roving.current`. */
|
|
150
|
+
readonly onFocus: (id: string) => NoInfer<Message>;
|
|
151
|
+
/** Pattern (`mod+u`) by control id (`mark-Underline`, `link`) for chords the application's keymap binds. */
|
|
152
|
+
readonly shortcuts?: Readonly<Record<string, string>> | undefined;
|
|
153
|
+
readonly words?: Partial<FormattingWords> | undefined;
|
|
154
|
+
/** Default `ltr`; `rtl` swaps ArrowLeft and ArrowRight. */
|
|
155
|
+
readonly direction?: 'ltr' | 'rtl' | undefined;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* The toolbar as a slot view, with the APG toolbar's keys: one tab stop (the control `roving`
|
|
159
|
+
* names, else the first), ArrowLeft and ArrowRight (mirrored under `rtl`), Home and End moving
|
|
160
|
+
* focus between controls, wrapping. Controls are never skipped, since an unavailable one stays
|
|
161
|
+
* focusable. ArrowDown on a closed menu's trigger opens it; on an open one it moves into the menu,
|
|
162
|
+
* whose items the arrows, Home and End then walk and whose Escape closes it and returns to the
|
|
163
|
+
* trigger. Escape on a trigger closes its open menu. Opening by Down does not enter the menu, since
|
|
164
|
+
* its items are not drawn yet: a second Down does.
|
|
165
|
+
*
|
|
166
|
+
* A control that is on is `aria-pressed` (a mark the selection carries, a link, an open menu is
|
|
167
|
+
* `aria-expanded`); the text style trigger names the current style. Marks and wraps need a
|
|
168
|
+
* selection, the text style menu a text selection, and link a selection that covers text. Lists
|
|
169
|
+
* and quote are not toggles here: wrapping again wraps deeper, as in the slash menu.
|
|
170
|
+
*
|
|
171
|
+
* The root is marked as the editor's controls (`CONTROLS_ATTRIBUTE`), so a pointer press on a
|
|
172
|
+
* control or a menu entry leaves the focus, and the browser's selection, in the text. A control
|
|
173
|
+
* reached by key does hold the focus: every command then reads the selection the editor keeps in
|
|
174
|
+
* the Model, and the editor draws that selection as a decoration until the focus returns.
|
|
175
|
+
*/
|
|
176
|
+
export declare const formattingToolbar: <Message>() => SlotView.SlotView<typeof FormattingToolbarSlots, FormattingToolbarInput<Message>, Message>;
|
|
177
|
+
export {};
|
|
178
|
+
//# sourceMappingURL=formatting.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatting.d.ts","names":[],"sources":["../src/formatting.ts"],"mappings":";;;;;;;qBAiCa,wBAAsB,MAAA;;;;;;;;;;;;YAavB;;;;;;;;iBASK,mBAAmB;;WAEzB;;WAEA;WACA,OAAO;;WAEP;;WAEA;WACA;WACA,SAAS;;;;;;;WAOT;aAAmB;aAAkC;;;KAG3D,WAAW;WAYD;;WACA,SAAS,mBAAmB;;;;;;;;;;;YAW/B,eAAe,mBACvB,QAAQ,WAAW;WACR,KAAK,QAAQ,WAAW;;KAElC,iBAAiB,WAAW,KAAK;;EAEpC;aAAmB,OAAO,WAAW;;;EAErC;aACW;aACA;aACA;aACA;aACA,SAAS;;;UAIZ,sBAAsB,KAAK,WAAW;WACrC,YAAY;;;YAIX,YAAY,WAAW,KAAK,WAAW,KAAK,eAAe;;qBAG1D,aAAW,KAAA,WAAA,mBAAA;;qBAGX,yBAAyB,cAAc;;iBAiBnC;WACN;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;WACA;;WAEA;;iBA8BM,uBAAuB;;WAE7B;WACA,UAAU,SAAS;WACnB,WAAW,SAAS;;WAEpB,cAAc;;WAEd,OAAO,SAAS,gBAAgB;;WAEhC,QAAQ,cAAc,eAAe,QAAQ;;;;;;WAM7C;;;;;WAKA;aAAqB;aAAwB;;;WAE7C,OAAO;;WAEP,UAAU,MAAM,gBAAgB,kBAAkB,QAAQ;;;;;;WAM1D,UAAU,cAAc,YAAY,QAAQ;;;;;;WAM5C,WAAW,QAAQ;;WAEnB,QAAQ,eAAe;;WAEvB,UAAU,eAAe,QAAQ;;WAEjC,YAAY,SAAS;WACrB,QAAQ,QAAQ;;WAEhB;;;;;;;;;;;;;;;;;;;;;qBA2LE,oBAAqB,cAAY,SAAS,gBAC9C,wBACP,uBAAuB,UACvB"}
|