svelte-rtf-editor 1.16.0 → 1.17.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 CHANGED
@@ -145,6 +145,11 @@ Pass an HTML string as `content`. If the content is already RTF, convert it firs
145
145
  | `fontSizes` | `number[]` | `[8, 9, 10, 11, 12, 14, 16, 18, 20, 24, 28, 36]` | The sizes, in points, the toolbar's size list offers for selected text. The body size is always among them; `[]` takes the list off the toolbar. See [Size of selected text](#size-of-selected-text) |
146
146
  | `fonts` | `string[]` | `[]` | The fonts the toolbar's font list offers for selected text, beside the document's own. `[]` means no font list, and one font for the whole document. See [Fonts for selected text](#fonts-for-selected-text) |
147
147
  | `fontMenu` | `boolean` | `false` | A right-click on the text opens a menu of `fonts` in place of the browser's menu. Shift + right-click still opens the browser's. See [The font menu](#the-font-menu) |
148
+ | `tools` | `ToolbarTool[]` | all (`TOOLBAR_TOOLS`) | The toolbar's controls, by name. A control not named is not rendered. See [Choosing the toolbar's controls](#choosing-the-toolbars-controls) |
149
+ | `moreTools` | `ToolbarTool[]` | `[]` | Those of `tools` that wait behind a More button, in a second row. `[]`: no button |
150
+ | `moreOpen` | `boolean` | `false` | Bindable: whether that second row is open. The editor keeps it nowhere itself |
151
+ | `moreLabel`, `lessLabel` | `string` | `'More'`, `'Less'` | The button's words, closed and open |
152
+ | `toolbarEnd` | `Snippet` | none | Controls of your own, at the end of the toolbar's first row (before More) |
148
153
  | `keepAttributes` | `string[]` | `[]` | `data-` attributes of your own that stay on HTML you set or the user pastes. See [HTML from outside](#html-from-outside) |
149
154
  | `keepClasses` | `string[]` | `[]` | Class names of your own that stay the same way |
150
155
 
@@ -155,6 +160,7 @@ Pass an HTML string as `content`. If the content is already RTF, convert it firs
155
160
  | `onchange` | `{ html, text, wordCount, charCount, estimatedRtfBytes }` | Fired on every content change. `html`, `text` and `estimatedRtfBytes` are worked out when the handler reads them, so a handler that only wants the counts costs nothing on a long document. They are the document as it is when first read: read them in the handler. A payload kept and read after a later edit gives the document as it is by then |
156
161
  | `onsave` | `{ html }` | Fired on `Ctrl+S`, and after each auto-save where `autosave` is on |
157
162
  | `onimport` | `{ html }` | Fired after a successful RTF file import |
163
+ | `onmoretoggle` | `open: boolean` | Fired when the More button opens or closes the second row |
158
164
 
159
165
  #### Methods (via `bind:this`)
160
166
 
@@ -282,6 +288,65 @@ The editing area grows with the document, and the page scrolls. The toolbar stay
282
288
  - It cannot stay where an element around the editor has `overflow: hidden`, `auto` or `scroll` and is not itself what scrolls: that is how `position: sticky` works. Take the `overflow` off that element, or make it the one that scrolls.
283
289
  - `stickyToolbar={false}` lets the toolbar go with the page, as it did through 1.12.0.
284
290
 
291
+ ### Choosing the toolbar's controls
292
+
293
+ Every control of the toolbar has a name that is part of the API, and is on the element as `data-tool`. A name does not change when a tooltip is reworded or translated; a control added later gets a new name, and the release notes say so.
294
+
295
+ ```ts
296
+ import { TOOLBAR_TOOLS, type ToolbarTool } from 'svelte-rtf-editor';
297
+ // block, font, fontSize, bold, italic, underline, strikethrough, textColor, clearFormatting,
298
+ // bulletList, numberedList, alignLeft, alignCenter, alignRight, outdent, indent, hangingOutdent,
299
+ // hangingIndent, hangAtCaret, rightOutdent, rightIndent, spacing, copyFormat, link, image, table,
300
+ // horizontalRule, undo, redo
301
+ ```
302
+
303
+ **`tools`** says which controls the toolbar has. Leave out what the receiving system does not keep: a control that changes the screen and not the delivered document misleads whoever signs it.
304
+
305
+ ```svelte
306
+ <InkEditor tools={['bold', 'italic', 'underline', 'clearFormatting', 'bulletList', 'numberedList', 'undo', 'redo']} />
307
+ ```
308
+
309
+ - A control that is not named is not rendered at all. Its keyboard shortcut still works (Ctrl+B with `bold` left out).
310
+ - The order is the editor's own, whatever order you name them in. A group left empty is not rendered, and a divider stands only between two groups of controls.
311
+ - `fonts={[]}` and `fontSizes={[]}` still take those lists off; `tools` can only narrow further.
312
+ - A name that is no control is ignored, with a warning in the console.
313
+
314
+ **`moreTools`** puts some of those controls behind a More button, for what applies but is seldom wanted:
315
+
316
+ ```svelte
317
+ <script>
318
+ let moreOpen = $state(false); // keep it where you like: the editor stores nothing
319
+ </script>
320
+
321
+ <InkEditor
322
+ {fonts}
323
+ moreTools={['font', 'fontSize', 'strikethrough', 'alignLeft', 'alignCenter', 'alignRight',
324
+ 'hangingOutdent', 'hangingIndent', 'rightOutdent', 'rightIndent', 'copyFormat', 'link', 'image']}
325
+ bind:moreOpen
326
+ />
327
+ ```
328
+
329
+ - The first row has the other controls and, at its end, a button with a word on it: **More** when closed, **Less** when open (`moreLabel`, `lessLabel` to translate them). It has `aria-expanded`, and `aria-controls` names the second row.
330
+ - Open, the second row is under the first, in the editor's order with its dividers. Closed, it is `hidden`.
331
+ - Nothing in the first row moves when the second opens or closes: the button is one width for both of its words.
332
+ - Pressing it leaves the selection where it was, so words selected, then More, then a size, sizes those words. A font or size on the text stays when the row closes, and the lists show it when it opens again.
333
+ - Tab goes through the first row, the button, then the second row: the order they are seen in.
334
+ - The toolbar that stays in sight of a long document (`stickyToolbar`) is both rows.
335
+ - On a narrow page the first row wraps, with the button at the end of its last line; the second row starts on a line of its own.
336
+ - `onmoretoggle(open)` fires on each press.
337
+
338
+ **`toolbarEnd`** is a place for a control of your own, at the end of the first row and before More. It is inside the toolbar, in the Tab order, and styled by you:
339
+
340
+ ```svelte
341
+ <InkEditor>
342
+ {#snippet toolbarEnd()}
343
+ <button type="button" onclick={insertTemplate}>Template</button>
344
+ {/snippet}
345
+ </InkEditor>
346
+ ```
347
+
348
+ With none of these set, the toolbar is as it was in 1.16.0, apart from the `data-tool` names.
349
+
285
350
  ### Opening a link
286
351
 
287
352
  A click on a link in the editor puts the caret in it, as anywhere else in the text, so that its words can be changed. To open the link, click it with `Ctrl` held down (`Cmd` on a Mac), as in Word. The pointer over a link shows a note with the address and that instruction. The note is the editor's own: it is not in the HTML or the RTF.
@@ -2,10 +2,12 @@
2
2
  interface Props {
3
3
  value?: string;
4
4
  title?: string;
5
+ /** The control's name (see toolbar-tools.ts), as `data-tool`. */
6
+ tool?: string;
5
7
  onchange?: (color: string) => void;
6
8
  }
7
9
 
8
- let { value = $bindable('#2C2520'), title = 'Color', onchange }: Props = $props();
10
+ let { value = $bindable('#2C2520'), title = 'Color', tool, onchange }: Props = $props();
9
11
 
10
12
  function handleChange(e: Event) {
11
13
  value = (e.target as HTMLInputElement).value;
@@ -13,7 +15,7 @@
13
15
  }
14
16
  </script>
15
17
 
16
- <div class="color-picker-wrap">
18
+ <div class="color-picker-wrap" data-tool={tool}>
17
19
  <button class="color-swatch" {title} type="button">
18
20
  <div class="color-swatch-inner" style="background: {value};"></div>
19
21
  </button>
@@ -1,6 +1,8 @@
1
1
  interface Props {
2
2
  value?: string;
3
3
  title?: string;
4
+ /** The control's name (see toolbar-tools.ts), as `data-tool`. */
5
+ tool?: string;
4
6
  onchange?: (color: string) => void;
5
7
  }
6
8
  declare const ColorPicker: import("svelte").Component<Props, {}, "value">;
@@ -56,6 +56,18 @@
56
56
  * spelling suggestions, Copy and Paste), and so does a right-click on a
57
57
  * link or a picture. The menu key and Shift+F10 open it at the caret.
58
58
  * Nothing changes without `fonts`, or when readonly. Default: false.
59
+ * tools — The toolbar's controls, by name (TOOLBAR_TOOLS, toolbar-tools.ts). A
60
+ * control not named is not rendered; its keyboard shortcut still works.
61
+ * The order is the editor's own, and an empty group or a divider with
62
+ * nothing beside it is left out. An unknown name is ignored with a console
63
+ * warning. Default: all of them.
64
+ * moreTools — Those of `tools` that wait behind a More button at the end of the first
65
+ * row, in a second row under it when opened. Default: none, and no button.
66
+ * moreOpen — Bindable: whether that second row is open. The editor keeps it nowhere
67
+ * itself. Default: false.
68
+ * moreLabel, lessLabel — The button's words, closed and open. Default: 'More', 'Less'.
69
+ * toolbarEnd — A snippet of the application's own controls, at the end of the first
70
+ * row (before the More button).
59
71
  * keepAttributes — `data-` attributes the application's documents carry, which
60
72
  * the editor leaves on their elements: a mark on a paragraph that
61
73
  * the application reads back (`data-section="2"`). Every other
@@ -71,6 +83,7 @@
71
83
  * { html, text, wordCount, charCount, estimatedRtfBytes }.
72
84
  * onsave — Fires on auto-save or Ctrl+S with { html }.
73
85
  * onimport — Fires after a successful RTF import with { html }.
86
+ * onmoretoggle — Fires with the new state when the More button opens or closes its row.
74
87
  *
75
88
  * Bindable methods (via bind:this):
76
89
  * getHTML() — Returns the current editor HTML string.
@@ -91,7 +104,8 @@
91
104
  * the application's own marks stay.
92
105
  */
93
106
 
94
- import { onMount } from 'svelte';
107
+ import { onMount, type Snippet } from 'svelte';
108
+ import { knownTools, TOOLBAR_TOOLS, type ToolbarTool } from '../toolbar-tools.js';
95
109
  import Toolbar from './Toolbar.svelte';
96
110
  import Modal from './Modal.svelte';
97
111
  import FontMenu from './FontMenu.svelte';
@@ -169,6 +183,17 @@
169
183
  fonts?: string[];
170
184
  /** A right-click on the text opens a menu of `fonts`; the browser's menu is Shift + right-click. */
171
185
  fontMenu?: boolean;
186
+ /** The toolbar's controls, by name; all of them unless given. */
187
+ tools?: readonly ToolbarTool[];
188
+ /** Those of `tools` behind a More button, in a second row; none unless given. */
189
+ moreTools?: readonly ToolbarTool[];
190
+ /** Whether the second row is open (bindable). */
191
+ moreOpen?: boolean;
192
+ moreLabel?: string;
193
+ lessLabel?: string;
194
+ /** The application's own controls, at the end of the toolbar's first row. */
195
+ toolbarEnd?: Snippet;
196
+ onmoretoggle?: (open: boolean) => void;
172
197
  /** `data-` attributes of the application's own, kept on HTML that is set or pasted. */
173
198
  keepAttributes?: string[];
174
199
  /** Class names of the application's own, kept the same way. */
@@ -191,6 +216,7 @@
191
216
  /** The sizes, in points, the toolbar's size list offers unless `fontSizes` says otherwise. */
192
217
  const DEFAULT_FONT_SIZES = [8, 9, 10, 11, 12, 14, 16, 18, 20, 24, 28, 36];
193
218
  const NO_KEPT_ATTRIBUTES: string[] = [];
219
+ const NO_TOOLS: readonly ToolbarTool[] = [];
194
220
 
195
221
  // ── Props ──
196
222
  let {
@@ -213,6 +239,13 @@
213
239
  fontSizes = DEFAULT_FONT_SIZES,
214
240
  fonts = NO_KEPT_ATTRIBUTES,
215
241
  fontMenu = false,
242
+ tools = TOOLBAR_TOOLS,
243
+ moreTools = NO_TOOLS,
244
+ moreOpen = $bindable(false),
245
+ moreLabel = 'More',
246
+ lessLabel = 'Less',
247
+ toolbarEnd,
248
+ onmoretoggle,
216
249
  keepAttributes = NO_KEPT_ATTRIBUTES,
217
250
  keepClasses = NO_KEPT_ATTRIBUTES,
218
251
  onchange,
@@ -220,6 +253,14 @@
220
253
  onimport
221
254
  }: Props = $props();
222
255
 
256
+ const toolSet = $derived(knownTools(tools, 'tools'));
257
+ const moreToolSet = $derived(knownTools(moreTools, 'moreTools'));
258
+
259
+ function toggleMore(open: boolean): void {
260
+ moreOpen = open;
261
+ onmoretoggle?.(open);
262
+ }
263
+
223
264
  const keptNames = $derived(keptMarks({ keepAttributes, keepClasses, fonts, fontFamily }));
224
265
 
225
266
  // The same font and size go to the screen and to the RTF, so the editor
@@ -4243,8 +4284,17 @@
4243
4284
 
4244
4285
 
4245
4286
  // ── Lifecycle ──
4287
+ /**
4288
+ * Whether the editor has started: its document is in, and its handlers are listening. Until
4289
+ * then the text cannot be edited. (A page rendered on the server showed an editing area that
4290
+ * took typing before the editor's script had run; the editor then put its document in, and
4291
+ * what had been typed was gone.)
4292
+ */
4293
+ let started = $state(false);
4294
+
4246
4295
  onMount(() => {
4247
4296
  if (!editorEl) return;
4297
+ started = true;
4248
4298
  document.addEventListener('selectionchange', trackSelection);
4249
4299
  const stopTracking = () => {
4250
4300
  document.removeEventListener('selectionchange', trackSelection);
@@ -4377,6 +4427,13 @@
4377
4427
  spacing={currentSpacing}
4378
4428
  copyingFormat={copyFormat !== 'off'}
4379
4429
  oncopyformat={toggleCopyFormat}
4430
+ tools={toolSet}
4431
+ moreTools={moreToolSet}
4432
+ {moreOpen}
4433
+ onmoretoggle={toggleMore}
4434
+ {moreLabel}
4435
+ {lessLabel}
4436
+ end={toolbarEnd}
4380
4437
  />
4381
4438
  {/if}
4382
4439
 
@@ -4406,12 +4463,13 @@
4406
4463
  class:copy-format-pick={copyFormat === 'pick'}
4407
4464
  class:copy-format-apply={copyFormat === 'apply'}
4408
4465
  class:links-open={linkArmed && linkHint !== null}
4409
- contenteditable={!readonly}
4466
+ contenteditable={started && !readonly}
4410
4467
  role="textbox"
4411
4468
  tabindex="0"
4412
4469
  aria-multiline="true"
4413
4470
  aria-label={ariaLabel}
4414
4471
  aria-readonly={readonly ? 'true' : undefined}
4472
+ aria-busy={started ? undefined : 'true'}
4415
4473
  spellcheck="true"
4416
4474
  bind:this={editorEl}
4417
4475
  onbeforeinput={handleBeforeInput}
@@ -1,3 +1,109 @@
1
+ /**
2
+ * InkEditor — A reusable Svelte 5 Rich Text Editor component.
3
+ *
4
+ * Props:
5
+ * content — Initial HTML content (string). Default: an empty paragraph. It is made
6
+ * safe before it is shown (see sanitize.ts): scripts, event handlers and
7
+ * the like are taken out.
8
+ * placeholder — Placeholder text shown when editor is empty.
9
+ * autosave — Keep a draft in localStorage and restore it on mount, in place of
10
+ * `content`. Default: false. Give each document its own storageKey.
11
+ * storageKey — localStorage key for auto-save. Default: 'ink-editor-content'.
12
+ * showToolbar — Show the formatting toolbar. Default: true.
13
+ * stickyToolbar — Keep the toolbar in sight while a long document is scrolled: it
14
+ * stays at the top of the window, or of the scrolling element the
15
+ * editor is in. Default: true. Under a header of your own that stays
16
+ * too, set the CSS variable --ink-toolbar-top to its height.
17
+ * showStatusBar — Show the word/char count status bar. Default: true.
18
+ * minHeight — CSS min-height of the editing area. Default: '40vh'.
19
+ * readonly — If true, editor is not editable. Default: false.
20
+ * ariaLabel — The name of the editing area for a screen reader. Default: 'Document'.
21
+ * maxImageEdge — Longest edge (px) an inserted image is scaled down to.
22
+ * 0 keeps the original dimensions. Default: 1600.
23
+ * maxImageDisplayWidth
24
+ * — Width (px) a newly inserted image is displayed at, unless
25
+ * the editor column is narrower. Defaults to 624 — 6.5in of
26
+ * page at 96 dpi — so pictures arrive sized to the page
27
+ * rather than to the browser window. 0 fills the column.
28
+ * maxImageBytes — Encoded byte ceiling per inserted image. Pictures over it
29
+ * are re-encoded (and shrunk if needed) until they fit,
30
+ * which is what bounds the RTF payload — it costs two hex
31
+ * characters per byte. 0 disables. Default: 524288 (512 KB,
32
+ * about 1 MB of document per image).
33
+ * maxRtfBytes — The largest RTF file Import reads. Default: 52428800 (50 MB);
34
+ * 0 reads any size.
35
+ * fontFamily — Font for the document, both on screen and in the RTF.
36
+ * Given a CSS list ('Segoe UI, Selawik, sans-serif'), the first
37
+ * family is the font, and the rest is what a screen without it
38
+ * shows. Default: DEFAULT_FONT_FAMILY (rtf-writer.ts).
39
+ * fontSize — Body text size in points, both on screen and in the RTF.
40
+ * Headings scale from it. Default: 12.
41
+ * fontSizes — The sizes, in points, the toolbar's size list offers for the
42
+ * selected text. The body size is always among them. An empty
43
+ * list takes the size list off the toolbar.
44
+ * Default: 8, 9, 10, 11, 12, 14, 16, 18, 20, 24, 28, 36.
45
+ * fonts — The fonts the toolbar's font list offers for the selected text,
46
+ * beside the document's own: names ('Calibri'), or CSS lists whose
47
+ * first family is the name ('Calibri, Carlito, sans-serif'). Text
48
+ * in one of them is written in it to the RTF and read back in it;
49
+ * any other font is taken off HTML as it comes in, set or pasted.
50
+ * Default: none: there is no font list, and no font but the
51
+ * document's is let in.
52
+ * fontMenu — If true, a right-click on the text opens the editor's own menu of
53
+ * `fonts` in place of the browser's, to set the font of the selected
54
+ * text. Shift with the right-click still opens the browser's menu (its
55
+ * spelling suggestions, Copy and Paste), and so does a right-click on a
56
+ * link or a picture. The menu key and Shift+F10 open it at the caret.
57
+ * Nothing changes without `fonts`, or when readonly. Default: false.
58
+ * tools — The toolbar's controls, by name (TOOLBAR_TOOLS, toolbar-tools.ts). A
59
+ * control not named is not rendered; its keyboard shortcut still works.
60
+ * The order is the editor's own, and an empty group or a divider with
61
+ * nothing beside it is left out. An unknown name is ignored with a console
62
+ * warning. Default: all of them.
63
+ * moreTools — Those of `tools` that wait behind a More button at the end of the first
64
+ * row, in a second row under it when opened. Default: none, and no button.
65
+ * moreOpen — Bindable: whether that second row is open. The editor keeps it nowhere
66
+ * itself. Default: false.
67
+ * moreLabel, lessLabel — The button's words, closed and open. Default: 'More', 'Less'.
68
+ * toolbarEnd — A snippet of the application's own controls, at the end of the first
69
+ * row (before the More button).
70
+ * keepAttributes — `data-` attributes the application's documents carry, which
71
+ * the editor leaves on their elements: a mark on a paragraph that
72
+ * the application reads back (`data-section="2"`). Every other
73
+ * attribute the editor does not know is taken off HTML as it comes
74
+ * in, set or pasted. Names that do not start with `data-` are not
75
+ * kept. Default: none.
76
+ * keepClasses — Class names of the application's own that stay in an element's
77
+ * class list the same way (`<pre class="summary">`). Every other
78
+ * class is taken off. Default: none.
79
+ *
80
+ * Events (callback props):
81
+ * onchange — Fires on content change with
82
+ * { html, text, wordCount, charCount, estimatedRtfBytes }.
83
+ * onsave — Fires on auto-save or Ctrl+S with { html }.
84
+ * onimport — Fires after a successful RTF import with { html }.
85
+ * onmoretoggle — Fires with the new state when the More button opens or closes its row.
86
+ *
87
+ * Bindable methods (via bind:this):
88
+ * getHTML() — Returns the current editor HTML string.
89
+ * getText() — Returns plain text content.
90
+ * setHTML(html) — Replaces editor content with the given HTML.
91
+ * getMarkdown() — Returns Markdown conversion of current content.
92
+ * getRTF() — Returns RTF conversion of current content.
93
+ * getRtfSize() — Returns the exact byte size of that RTF.
94
+ * clear() — Clears the editor (no confirm dialog).
95
+ * focus() — Focuses the editor.
96
+ * exportFile(format) — Downloads as 'html', 'md', or 'rtf'.
97
+ * importRtf() — Opens the file picker to import an RTF file.
98
+ * setFontSize(points) — Sets the size of the selected text, or of what is typed next.
99
+ * setFont(name) — Sets the font of the selected text, or of what is typed next, to
100
+ * one of `fonts`; '' or the document font's name sets it back.
101
+ * clearFormatting() — Takes bold, italic, underline, strike, color, size and font off
102
+ * the selected text. Headings, lists, links, alignment, indents and
103
+ * the application's own marks stay.
104
+ */
105
+ import { type Snippet } from 'svelte';
106
+ import { type ToolbarTool } from '../toolbar-tools.js';
1
107
  interface ChangePayload {
2
108
  /**
3
109
  * `html`, `text` and `estimatedRtfBytes` are worked out when the handler reads them, and
@@ -38,6 +144,17 @@ interface Props {
38
144
  fonts?: string[];
39
145
  /** A right-click on the text opens a menu of `fonts`; the browser's menu is Shift + right-click. */
40
146
  fontMenu?: boolean;
147
+ /** The toolbar's controls, by name; all of them unless given. */
148
+ tools?: readonly ToolbarTool[];
149
+ /** Those of `tools` behind a More button, in a second row; none unless given. */
150
+ moreTools?: readonly ToolbarTool[];
151
+ /** Whether the second row is open (bindable). */
152
+ moreOpen?: boolean;
153
+ moreLabel?: string;
154
+ lessLabel?: string;
155
+ /** The application's own controls, at the end of the toolbar's first row. */
156
+ toolbarEnd?: Snippet;
157
+ onmoretoggle?: (open: boolean) => void;
41
158
  /** `data-` attributes of the application's own, kept on HTML that is set or pasted. */
42
159
  keepAttributes?: string[];
43
160
  /** Class names of the application's own, kept the same way. */
@@ -76,6 +193,6 @@ declare const InkEditor: import("svelte").Component<Props, {
76
193
  borders?: boolean;
77
194
  header?: boolean;
78
195
  }) => void;
79
- }, "">;
196
+ }, "moreOpen">;
80
197
  type InkEditor = ReturnType<typeof InkEditor>;
81
198
  export default InkEditor;
@@ -1,4 +1,11 @@
1
+ <script lang="ts" module>
2
+ /** Each toolbar's second row has an id of its own, for its More button to name. */
3
+ let rows = 0;
4
+ </script>
5
+
1
6
  <script lang="ts">
7
+ import type { Snippet } from 'svelte';
8
+ import { toolbarRow, TOOLBAR_TOOLS, type ToolbarTool } from '../toolbar-tools.js';
2
9
  import ToolbarButton from './ToolbarButton.svelte';
3
10
  import ColorPicker from './ColorPicker.svelte';
4
11
 
@@ -31,6 +38,17 @@
31
38
  /** The Copy format brush is in hand: the button shows as pressed. */
32
39
  copyingFormat?: boolean;
33
40
  oncopyformat?: () => void;
41
+ /** The controls the toolbar has. All of them unless given. */
42
+ tools?: ReadonlySet<ToolbarTool>;
43
+ /** Those of `tools` that wait behind the More button, in a second row. None, and there is no button. */
44
+ moreTools?: ReadonlySet<ToolbarTool>;
45
+ /** Whether the second row is open. */
46
+ moreOpen?: boolean;
47
+ onmoretoggle?: (open: boolean) => void;
48
+ moreLabel?: string;
49
+ lessLabel?: string;
50
+ /** The application's own controls, at the end of the first row (before the More button). */
51
+ end?: Snippet;
34
52
  }
35
53
 
36
54
  let {
@@ -50,9 +68,23 @@
50
68
  onfont,
51
69
  spacing = { line: null, before: 0, after: 0 },
52
70
  copyingFormat = false,
53
- oncopyformat
71
+ oncopyformat,
72
+ tools = new Set(TOOLBAR_TOOLS),
73
+ moreTools = new Set(),
74
+ moreOpen = false,
75
+ onmoretoggle,
76
+ moreLabel = 'More',
77
+ lessLabel = 'Less',
78
+ end
54
79
  }: Props = $props();
55
80
 
81
+ const moreId = `ink-toolbar-more-${++rows}`;
82
+
83
+ /** A control that has something to offer: a font list needs fonts, a size list sizes. */
84
+ const offers = (tool: ToolbarTool) => (tool === 'font' ? fonts.length > 0 : tool === 'fontSize' ? fontSizes.length > 0 : true);
85
+ const firstRow = $derived(toolbarRow((tool) => tools.has(tool) && !moreTools.has(tool) && offers(tool)));
86
+ const moreRow = $derived(toolbarRow((tool) => tools.has(tool) && moreTools.has(tool) && offers(tool)));
87
+
56
88
  // The size at the caret is shown even when it is not one of the sizes offered (13.5 pt in a
57
89
  // document from elsewhere), so the list never shows a size the text does not have.
58
90
  const sizeChoices = $derived(
@@ -82,10 +114,11 @@
82
114
  }
83
115
  </script>
84
116
 
85
- <div class="toolbar" role="toolbar" aria-label="Formatting">
86
- <!-- Block type -->
87
- <div class="toolbar-group">
117
+ <!-- One control of the toolbar, by its name. -->
118
+ {#snippet control(tool: ToolbarTool)}
119
+ {#if tool === 'block'}
88
120
  <select
121
+ data-tool="block"
89
122
  class="tb-select"
90
123
  title="Paragraph style"
91
124
  aria-label="Paragraph style"
@@ -99,142 +132,117 @@
99
132
  <option value="blockquote">Quote</option>
100
133
  <option value="pre">Code Block</option>
101
134
  </select>
102
- </div>
103
-
104
- {#if fonts.length > 0}
105
- <!-- Font -->
106
- <div class="toolbar-group">
107
- <select
108
- class="tb-select tb-font"
109
- title="Font"
110
- aria-label="Font"
111
- value={font === null ? ' ' : font}
112
- onchange={(e) => {
113
- const key = (e.target as HTMLSelectElement).value;
114
- if (key !== ' ') onfont?.(key);
115
- }}
116
- >
117
- {#if font === null}
118
- <option value=" "></option>
119
- {/if}
120
- <option value="">{documentFont}</option>
121
- {#each fonts as choice (choice.key)}
122
- <option value={choice.key} style:font-family={choice.css}>{choice.name}</option>
123
- {/each}
124
- </select>
125
- </div>
126
- {/if}
127
-
128
- {#if fontSizes.length > 0}
129
- <!-- Font size -->
130
- <div class="toolbar-group">
131
- <select
132
- class="tb-select tb-size"
133
- title="Font size"
134
- aria-label="Font size"
135
- value={fontSize === null ? '' : String(fontSize)}
136
- onchange={(e) => {
137
- const points = Number((e.target as HTMLSelectElement).value);
138
- if (points > 0) onfontsize?.(points);
139
- }}
140
- >
141
- {#if fontSize === null}
142
- <option value=""></option>
143
- {/if}
144
- {#each sizeChoices as size (size)}
145
- <option value={String(size)}>{size}</option>
146
- {/each}
147
- </select>
148
- </div>
149
- {/if}
150
-
151
- <div class="toolbar-divider"></div>
152
-
153
- <!-- Inline formatting -->
154
- <div class="toolbar-group">
155
- <ToolbarButton active={formatState.bold} title="Bold (Ctrl+B)" onclick={() => exec('bold')}>
135
+ {:else if tool === 'font'}
136
+ <select
137
+ data-tool="font"
138
+ class="tb-select tb-font"
139
+ title="Font"
140
+ aria-label="Font"
141
+ value={font === null ? ' ' : font}
142
+ onchange={(e) => {
143
+ const key = (e.target as HTMLSelectElement).value;
144
+ if (key !== ' ') onfont?.(key);
145
+ }}
146
+ >
147
+ {#if font === null}
148
+ <option value=" "></option>
149
+ {/if}
150
+ <option value="">{documentFont}</option>
151
+ {#each fonts as choice (choice.key)}
152
+ <option value={choice.key} style:font-family={choice.css}>{choice.name}</option>
153
+ {/each}
154
+ </select>
155
+ {:else if tool === 'fontSize'}
156
+ <select
157
+ data-tool="fontSize"
158
+ class="tb-select tb-size"
159
+ title="Font size"
160
+ aria-label="Font size"
161
+ value={fontSize === null ? '' : String(fontSize)}
162
+ onchange={(e) => {
163
+ const points = Number((e.target as HTMLSelectElement).value);
164
+ if (points > 0) onfontsize?.(points);
165
+ }}
166
+ >
167
+ {#if fontSize === null}
168
+ <option value=""></option>
169
+ {/if}
170
+ {#each sizeChoices as size (size)}
171
+ <option value={String(size)}>{size}</option>
172
+ {/each}
173
+ </select>
174
+ {:else if tool === 'bold'}
175
+ <ToolbarButton tool="bold" active={formatState.bold} title="Bold (Ctrl+B)" onclick={() => exec('bold')}>
156
176
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round"><path d="M6 4h8a4 4 0 0 1 4 4 4 4 0 0 1-4 4H6z"/><path d="M6 12h9a4 4 0 0 1 4 4 4 4 0 0 1-4 4H6z"/></svg>
157
177
  </ToolbarButton>
158
- <ToolbarButton active={formatState.italic} title="Italic (Ctrl+I)" onclick={() => exec('italic')}>
178
+ {:else if tool === 'italic'}
179
+ <ToolbarButton tool="italic" active={formatState.italic} title="Italic (Ctrl+I)" onclick={() => exec('italic')}>
159
180
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round"><line x1="19" y1="4" x2="10" y2="4"/><line x1="14" y1="20" x2="5" y2="20"/><line x1="15" y1="4" x2="9" y2="20"/></svg>
160
181
  </ToolbarButton>
161
- <ToolbarButton active={formatState.underline} title="Underline (Ctrl+U)" onclick={() => exec('underline')}>
182
+ {:else if tool === 'underline'}
183
+ <ToolbarButton tool="underline" active={formatState.underline} title="Underline (Ctrl+U)" onclick={() => exec('underline')}>
162
184
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round"><path d="M6 3v7a6 6 0 0 0 6 6 6 6 0 0 0 6-6V3"/><line x1="4" y1="21" x2="20" y2="21"/></svg>
163
185
  </ToolbarButton>
164
- <ToolbarButton active={formatState.strikeThrough} title="Strikethrough" onclick={() => exec('strikeThrough')}>
186
+ {:else if tool === 'strikethrough'}
187
+ <ToolbarButton tool="strikethrough" active={formatState.strikeThrough} title="Strikethrough" onclick={() => exec('strikeThrough')}>
165
188
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round"><path d="M16 4c-.5-1.5-2.5-3-5-3-3 0-5 2-5 4.5 0 2 1 3.5 5 5"/><line x1="2" y1="12" x2="22" y2="12"/><path d="M8 20c.5 1.5 2.5 3 5 3 3 0 5-2 5-4.5 0-1.5-.5-2.5-2-3.5"/></svg>
166
189
  </ToolbarButton>
167
- </div>
168
-
169
- <div class="toolbar-divider"></div>
170
-
171
- <!-- Color, and all of the above taken off again -->
172
- <div class="toolbar-group">
173
- <ColorPicker value="#2C2520" title="Text color" onchange={(c) => exec('foreColor', c)} />
174
- <ToolbarButton title="Clear formatting" onclick={() => exec('removeFormat')}>
190
+ {:else if tool === 'textColor'}
191
+ <ColorPicker tool="textColor" value="#2C2520" title="Text color" onchange={(c) => exec('foreColor', c)} />
192
+ {:else if tool === 'clearFormatting'}
193
+ <ToolbarButton tool="clearFormatting" title="Clear formatting" onclick={() => exec('removeFormat')}>
175
194
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M4 7V4h16v3"/><path d="M9 20h6"/><path d="M12 4v16"/><line x1="18" y1="4" x2="4" y2="20" stroke="var(--accent)" stroke-width="2"/></svg>
176
195
  </ToolbarButton>
177
- </div>
178
-
179
- <div class="toolbar-divider"></div>
180
-
181
- <!-- Lists -->
182
- <div class="toolbar-group">
183
- <ToolbarButton active={formatState.bulletList} title="Bullet list" onclick={() => exec('insertUnorderedList')}>
196
+ {:else if tool === 'bulletList'}
197
+ <ToolbarButton tool="bulletList" active={formatState.bulletList} title="Bullet list" onclick={() => exec('insertUnorderedList')}>
184
198
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line x1="9" y1="6" x2="20" y2="6"/><line x1="9" y1="12" x2="20" y2="12"/><line x1="9" y1="18" x2="20" y2="18"/><circle cx="5" cy="6" r="1.5" fill="currentColor"/><circle cx="5" cy="12" r="1.5" fill="currentColor"/><circle cx="5" cy="18" r="1.5" fill="currentColor"/></svg>
185
199
  </ToolbarButton>
186
- <ToolbarButton active={formatState.numberedList} title="Numbered list" onclick={() => exec('insertOrderedList')}>
200
+ {:else if tool === 'numberedList'}
201
+ <ToolbarButton tool="numberedList" active={formatState.numberedList} title="Numbered list" onclick={() => exec('insertOrderedList')}>
187
202
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line x1="10" y1="6" x2="20" y2="6"/><line x1="10" y1="12" x2="20" y2="12"/><line x1="10" y1="18" x2="20" y2="18"/><text x="3" y="8" font-size="8" fill="currentColor" stroke="none" font-family="sans-serif">1</text><text x="3" y="14" font-size="8" fill="currentColor" stroke="none" font-family="sans-serif">2</text><text x="3" y="20" font-size="8" fill="currentColor" stroke="none" font-family="sans-serif">3</text></svg>
188
203
  </ToolbarButton>
189
- </div>
190
-
191
- <div class="toolbar-divider"></div>
192
-
193
- <!-- Alignment -->
194
- <div class="toolbar-group">
195
- <ToolbarButton title="Align left" onclick={() => exec('justifyLeft')}>
204
+ {:else if tool === 'alignLeft'}
205
+ <ToolbarButton tool="alignLeft" title="Align left" onclick={() => exec('justifyLeft')}>
196
206
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line x1="3" y1="6" x2="21" y2="6"/><line x1="3" y1="12" x2="15" y2="12"/><line x1="3" y1="18" x2="18" y2="18"/></svg>
197
207
  </ToolbarButton>
198
- <ToolbarButton title="Align center" onclick={() => exec('justifyCenter')}>
208
+ {:else if tool === 'alignCenter'}
209
+ <ToolbarButton tool="alignCenter" title="Align center" onclick={() => exec('justifyCenter')}>
199
210
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line x1="3" y1="6" x2="21" y2="6"/><line x1="6" y1="12" x2="18" y2="12"/><line x1="4" y1="18" x2="20" y2="18"/></svg>
200
211
  </ToolbarButton>
201
- <ToolbarButton title="Align right" onclick={() => exec('justifyRight')}>
212
+ {:else if tool === 'alignRight'}
213
+ <ToolbarButton tool="alignRight" title="Align right" onclick={() => exec('justifyRight')}>
202
214
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line x1="3" y1="6" x2="21" y2="6"/><line x1="9" y1="12" x2="21" y2="12"/><line x1="6" y1="18" x2="21" y2="18"/></svg>
203
215
  </ToolbarButton>
204
- </div>
205
-
206
- <div class="toolbar-divider"></div>
207
-
208
- <!-- Indent -->
209
- <div class="toolbar-group">
210
- <ToolbarButton title="Decrease indent (Ctrl+[)" onclick={() => exec('outdent')}>
216
+ {:else if tool === 'outdent'}
217
+ <ToolbarButton tool="outdent" title="Decrease indent (Ctrl+[)" onclick={() => exec('outdent')}>
211
218
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="3" y1="5" x2="21" y2="5"/><line x1="11" y1="10" x2="21" y2="10"/><line x1="11" y1="14" x2="21" y2="14"/><line x1="3" y1="19" x2="21" y2="19"/><polyline points="7 9 4 12 7 15"/></svg>
212
219
  </ToolbarButton>
213
- <ToolbarButton title="Increase indent (Ctrl+])" onclick={() => exec('indent')}>
220
+ {:else if tool === 'indent'}
221
+ <ToolbarButton tool="indent" title="Increase indent (Ctrl+])" onclick={() => exec('indent')}>
214
222
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="3" y1="5" x2="21" y2="5"/><line x1="11" y1="10" x2="21" y2="10"/><line x1="11" y1="14" x2="21" y2="14"/><line x1="3" y1="19" x2="21" y2="19"/><polyline points="4 9 7 12 4 15"/></svg>
215
223
  </ToolbarButton>
216
- <ToolbarButton title="Decrease hanging indent" onclick={() => exec('hangingOutdent')}>
224
+ {:else if tool === 'hangingOutdent'}
225
+ <ToolbarButton tool="hangingOutdent" title="Decrease hanging indent" onclick={() => exec('hangingOutdent')}>
217
226
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="3" y1="5" x2="21" y2="5"/><line x1="11" y1="10" x2="21" y2="10"/><line x1="11" y1="14" x2="21" y2="14"/><line x1="11" y1="19" x2="21" y2="19"/><polyline points="7 11.5 4 14.5 7 17.5"/></svg>
218
227
  </ToolbarButton>
219
- <ToolbarButton title="Increase hanging indent" onclick={() => exec('hangingIndent')}>
228
+ {:else if tool === 'hangingIndent'}
229
+ <ToolbarButton tool="hangingIndent" title="Increase hanging indent" onclick={() => exec('hangingIndent')}>
220
230
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="3" y1="5" x2="21" y2="5"/><line x1="11" y1="10" x2="21" y2="10"/><line x1="11" y1="14" x2="21" y2="14"/><line x1="11" y1="19" x2="21" y2="19"/><polyline points="4 11.5 7 14.5 4 17.5"/></svg>
221
231
  </ToolbarButton>
222
- <ToolbarButton title="Hang at caret" onclick={() => exec('hangAtCaret')}>
232
+ {:else if tool === 'hangAtCaret'}
233
+ <ToolbarButton tool="hangAtCaret" title="Hang at caret" onclick={() => exec('hangAtCaret')}>
223
234
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="3" y1="5" x2="7" y2="5"/><line x1="14" y1="5" x2="21" y2="5"/><line x1="10.5" y1="2" x2="10.5" y2="8" stroke="var(--accent, currentColor)"/><line x1="10.5" y1="11" x2="21" y2="11"/><line x1="10.5" y1="15" x2="21" y2="15"/><line x1="10.5" y1="19" x2="21" y2="19"/></svg>
224
235
  </ToolbarButton>
225
- <ToolbarButton title="Decrease right indent" onclick={() => exec('rightOutdent')}>
236
+ {:else if tool === 'rightOutdent'}
237
+ <ToolbarButton tool="rightOutdent" title="Decrease right indent" onclick={() => exec('rightOutdent')}>
226
238
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="3" y1="5" x2="21" y2="5"/><line x1="3" y1="10" x2="13" y2="10"/><line x1="3" y1="14" x2="13" y2="14"/><line x1="3" y1="19" x2="21" y2="19"/><polyline points="17 9 20 12 17 15"/></svg>
227
239
  </ToolbarButton>
228
- <ToolbarButton title="Increase right indent" onclick={() => exec('rightIndent')}>
240
+ {:else if tool === 'rightIndent'}
241
+ <ToolbarButton tool="rightIndent" title="Increase right indent" onclick={() => exec('rightIndent')}>
229
242
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="3" y1="5" x2="21" y2="5"/><line x1="3" y1="10" x2="13" y2="10"/><line x1="3" y1="14" x2="13" y2="14"/><line x1="3" y1="19" x2="21" y2="19"/><polyline points="20 9 17 12 20 15"/></svg>
230
243
  </ToolbarButton>
231
- </div>
232
-
233
- <div class="toolbar-divider"></div>
234
-
235
- <!-- Spacing -->
236
- <div class="toolbar-group">
237
- <select class="tb-select tb-spacing" title="Line and paragraph spacing" aria-label="Line and paragraph spacing" value="" onchange={chooseSpacing}>
244
+ {:else if tool === 'spacing'}
245
+ <select class="tb-select tb-spacing" data-tool="spacing" title="Line and paragraph spacing" aria-label="Line and paragraph spacing" value="" onchange={chooseSpacing}>
238
246
  <option value="" hidden>Spacing</option>
239
247
  <optgroup label="Line spacing">
240
248
  {#each lineChoices as lines (lines)}
@@ -261,46 +269,98 @@
261
269
  </optgroup>
262
270
  {/if}
263
271
  </select>
264
- </div>
265
-
266
- <div class="toolbar-divider"></div>
267
-
268
- <!-- Copy format -->
269
- <div class="toolbar-group">
270
- <ToolbarButton active={copyingFormat} title="Copy format" onclick={oncopyformat}>
272
+ {:else if tool === 'copyFormat'}
273
+ <ToolbarButton tool="copyFormat" active={copyingFormat} title="Copy format" onclick={oncopyformat}>
271
274
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="14" height="6" rx="1.5"/><path d="M17 6h3v5h-9v3"/><rect x="9.5" y="14" width="3" height="7" rx="1"/></svg>
272
275
  </ToolbarButton>
273
- </div>
274
-
275
- <div class="toolbar-divider"></div>
276
-
277
- <!-- Insert -->
278
- <div class="toolbar-group">
279
- <ToolbarButton title="Insert link (Ctrl+K)" onclick={oninsertlink}>
276
+ {:else if tool === 'link'}
277
+ <ToolbarButton tool="link" title="Insert link (Ctrl+K)" onclick={oninsertlink}>
280
278
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"/><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"/></svg>
281
279
  </ToolbarButton>
282
- <ToolbarButton title="Insert image" onclick={oninsertimage}>
280
+ {:else if tool === 'image'}
281
+ <ToolbarButton tool="image" title="Insert image" onclick={oninsertimage}>
283
282
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="8.5" cy="8.5" r="1.5"/><path d="M21 15l-5-5L5 21"/></svg>
284
283
  </ToolbarButton>
285
- <ToolbarButton title="Insert table" onclick={oninserttable}>
284
+ {:else if tool === 'table'}
285
+ <ToolbarButton tool="table" title="Insert table" onclick={oninserttable}>
286
286
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="4" width="18" height="16" rx="2"/><line x1="3" y1="10" x2="21" y2="10"/><line x1="3" y1="15" x2="21" y2="15"/><line x1="9" y1="4" x2="9" y2="20"/><line x1="15" y1="4" x2="15" y2="20"/></svg>
287
287
  </ToolbarButton>
288
- <ToolbarButton title="Horizontal rule" onclick={() => exec('insertHorizontalRule')}>
288
+ {:else if tool === 'horizontalRule'}
289
+ <ToolbarButton tool="horizontalRule" title="Horizontal rule" onclick={() => exec('insertHorizontalRule')}>
289
290
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line x1="3" y1="12" x2="21" y2="12"/></svg>
290
291
  </ToolbarButton>
291
- </div>
292
-
293
- <div class="toolbar-divider"></div>
294
-
295
- <!-- History -->
296
- <div class="toolbar-group">
297
- <ToolbarButton title="Undo (Ctrl+Z)" onclick={() => exec('undo')}>
292
+ {:else if tool === 'undo'}
293
+ <ToolbarButton tool="undo" title="Undo (Ctrl+Z)" onclick={() => exec('undo')}>
298
294
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M3 7v6h6"/><path d="M3 13c0 0 2.03-7 9-7a9 9 0 0 1 9 9 9 9 0 0 1-9 9c-3.5 0-6-1.5-7.5-4"/></svg>
299
295
  </ToolbarButton>
300
- <ToolbarButton title="Redo" onclick={() => exec('redo')}>
296
+ {:else if tool === 'redo'}
297
+ <ToolbarButton tool="redo" title="Redo" onclick={() => exec('redo')}>
301
298
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M21 7v6h-6"/><path d="M21 13c0 0-2.03-7-9-7a9 9 0 0 0-9 9 9 9 0 0 0 9 9c3.5 0 6-1.5 7.5-4"/></svg>
302
299
  </ToolbarButton>
300
+ {/if}
301
+ {/snippet}
302
+
303
+ <!-- A row of the toolbar: its sections, a divider between two of them. `tail` goes after its
304
+ last group, held to it: a row that wraps has it at the end of its last line, not alone. -->
305
+ {#snippet row(sections: ToolbarTool[][][], tail?: Snippet)}
306
+ {#each sections as section, i}
307
+ {#if i > 0}
308
+ <div class="toolbar-divider"></div>
309
+ {/if}
310
+ {#each section as group, j}
311
+ {#if tail && i === sections.length - 1 && j === section.length - 1}
312
+ <div class="toolbar-tail">
313
+ {@render toolGroup(group)}
314
+ {@render tail()}
315
+ </div>
316
+ {:else}
317
+ {@render toolGroup(group)}
318
+ {/if}
319
+ {/each}
320
+ {/each}
321
+ {#if tail && sections.length === 0}
322
+ {@render tail()}
323
+ {/if}
324
+ {/snippet}
325
+
326
+ {#snippet toolGroup(group: ToolbarTool[])}
327
+ <div class="toolbar-group">
328
+ {#each group as tool (tool)}
329
+ {@render control(tool)}
330
+ {/each}
303
331
  </div>
332
+ {/snippet}
333
+
334
+ <!-- The end of the first row where there is a second: the application's own controls, then More. -->
335
+ {#snippet firstRowEnd()}
336
+ {@render end?.()}
337
+ <!-- (One width for both words: what is before it, and the row under it, stay where they are.) -->
338
+ <button
339
+ type="button"
340
+ class="tb-more"
341
+ data-tool-more
342
+ aria-expanded={moreOpen}
343
+ aria-controls={moreId}
344
+ onmousedown={(e) => e.preventDefault()}
345
+ onclick={() => onmoretoggle?.(!moreOpen)}
346
+ >
347
+ <span class:shown={!moreOpen}>{moreLabel}</span>
348
+ <span class:shown={moreOpen}>{lessLabel}</span>
349
+ </button>
350
+ {/snippet}
351
+
352
+ <div class="toolbar" class:two-rows={moreRow.length > 0} role="toolbar" aria-label="Formatting">
353
+ {#if moreRow.length > 0}
354
+ <div class="toolbar-row">
355
+ {@render row(firstRow, firstRowEnd)}
356
+ </div>
357
+ <div class="toolbar-row toolbar-more" id={moreId} hidden={!moreOpen}>
358
+ {@render row(moreRow)}
359
+ </div>
360
+ {:else}
361
+ {@render row(firstRow)}
362
+ {@render end?.()}
363
+ {/if}
304
364
  </div>
305
365
 
306
366
  <style>
@@ -318,6 +378,80 @@
318
378
  box-shadow: var(--shadow-sm);
319
379
  }
320
380
 
381
+ /* With a More button the toolbar is two rows, one under the other: the second does not
382
+ move the first, and the first wraps on a narrow page with the button at its end. */
383
+ .toolbar.two-rows {
384
+ flex-direction: column;
385
+ align-items: stretch;
386
+ gap: 4px;
387
+ }
388
+
389
+ .toolbar-row {
390
+ display: flex;
391
+ align-items: center;
392
+ gap: 2px;
393
+ flex-wrap: wrap;
394
+ }
395
+
396
+ .toolbar-row[hidden] {
397
+ display: none;
398
+ }
399
+
400
+ .toolbar-more {
401
+ padding-top: 4px;
402
+ border-top: 1px solid var(--border, #e5e2dc);
403
+ }
404
+
405
+ /* A word, not an icon; one width for both words (the two are laid over one another). */
406
+ .toolbar-tail {
407
+ display: flex;
408
+ align-items: center;
409
+ gap: 2px;
410
+ }
411
+
412
+ .tb-more {
413
+ display: inline-grid;
414
+ align-items: center;
415
+ justify-items: center;
416
+ height: 32px;
417
+ margin-left: 4px;
418
+ padding: 0 12px;
419
+ border: 1px solid var(--border, #d9d4cc);
420
+ border-radius: 8px;
421
+ background: transparent;
422
+ font-family: 'DM Sans', sans-serif;
423
+ font-size: 13px;
424
+ font-weight: 500;
425
+ color: var(--text-muted);
426
+ cursor: pointer;
427
+ }
428
+
429
+ .tb-more > span {
430
+ grid-area: 1 / 1;
431
+ visibility: hidden;
432
+ }
433
+
434
+ .tb-more > span.shown {
435
+ visibility: visible;
436
+ }
437
+
438
+ .tb-more:hover {
439
+ background: var(--surface, #f6f4f0);
440
+ border-color: var(--border-active, #b8ab98);
441
+ color: var(--text);
442
+ }
443
+
444
+ .tb-more[aria-expanded='true'] {
445
+ background: var(--accent-soft, #ede9fe);
446
+ color: var(--text);
447
+ }
448
+
449
+ .tb-more:focus-visible {
450
+ outline: none;
451
+ border-color: var(--accent, #6e56cf);
452
+ box-shadow: 0 0 0 3px var(--accent-soft, #ede9fe);
453
+ }
454
+
321
455
  .toolbar-group {
322
456
  display: flex;
323
457
  align-items: center;
@@ -1,3 +1,5 @@
1
+ import type { Snippet } from 'svelte';
2
+ import { type ToolbarTool } from '../toolbar-tools.js';
1
3
  interface Props {
2
4
  formatState?: Record<string, boolean>;
3
5
  blockType?: string;
@@ -36,6 +38,17 @@ interface Props {
36
38
  /** The Copy format brush is in hand: the button shows as pressed. */
37
39
  copyingFormat?: boolean;
38
40
  oncopyformat?: () => void;
41
+ /** The controls the toolbar has. All of them unless given. */
42
+ tools?: ReadonlySet<ToolbarTool>;
43
+ /** Those of `tools` that wait behind the More button, in a second row. None, and there is no button. */
44
+ moreTools?: ReadonlySet<ToolbarTool>;
45
+ /** Whether the second row is open. */
46
+ moreOpen?: boolean;
47
+ onmoretoggle?: (open: boolean) => void;
48
+ moreLabel?: string;
49
+ lessLabel?: string;
50
+ /** The application's own controls, at the end of the first row (before the More button). */
51
+ end?: Snippet;
39
52
  }
40
53
  declare const Toolbar: import("svelte").Component<Props, {}, "">;
41
54
  type Toolbar = ReturnType<typeof Toolbar>;
@@ -8,15 +8,18 @@
8
8
  */
9
9
  active?: boolean;
10
10
  title?: string;
11
+ /** The control's name (see toolbar-tools.ts), as `data-tool`. */
12
+ tool?: string;
11
13
  onclick?: () => void;
12
14
  children: Snippet;
13
15
  }
14
16
 
15
- let { active, title = '', onclick, children }: Props = $props();
17
+ let { active, title = '', tool, onclick, children }: Props = $props();
16
18
  </script>
17
19
 
18
20
  <button
19
21
  class="tb"
22
+ data-tool={tool}
20
23
  class:active={active === true}
21
24
  {title}
22
25
  aria-label={title || undefined}
@@ -6,6 +6,8 @@ interface Props {
6
6
  */
7
7
  active?: boolean;
8
8
  title?: string;
9
+ /** The control's name (see toolbar-tools.ts), as `data-tool`. */
10
+ tool?: string;
9
11
  onclick?: () => void;
10
12
  children: Snippet;
11
13
  }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { default as RtfViewer } from './RtfViewer.svelte';
2
2
  export { default as InkEditor } from './components/InkEditor.svelte';
3
+ export { TOOLBAR_TOOLS, type ToolbarTool } from './toolbar-tools.js';
3
4
  export { rtfToHtml, readRtfFile, DEFAULT_MAX_RTF_BYTES } from './rtf-parser.js';
4
5
  export { htmlToRtf, estimateRtfBytes, isSafeLinkUrl, DEFAULT_FONT_FAMILY, DEFAULT_FONT_SIZE } from './rtf-writer.js';
5
6
  export type { RtfWriteOptions } from './rtf-writer.js';
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  // Svelte components
2
2
  export { default as RtfViewer } from './RtfViewer.svelte';
3
3
  export { default as InkEditor } from './components/InkEditor.svelte';
4
+ export { TOOLBAR_TOOLS } from './toolbar-tools.js';
4
5
  // RTF utilities
5
6
  export { rtfToHtml, readRtfFile, DEFAULT_MAX_RTF_BYTES } from './rtf-parser.js';
6
7
  export { htmlToRtf, estimateRtfBytes, isSafeLinkUrl, DEFAULT_FONT_FAMILY, DEFAULT_FONT_SIZE } from './rtf-writer.js';
@@ -0,0 +1,26 @@
1
+ /**
2
+ * The toolbar's controls, by names that are part of the public API: an application chooses
3
+ * which the toolbar has (`tools`) and which wait behind its More button (`moreTools`) by these,
4
+ * and each control carries its name as `data-tool`. A name does not change when a control's
5
+ * tooltip does. A control added in a later version gets a name of its own, and the release
6
+ * notes say so.
7
+ */
8
+ /**
9
+ * The toolbar's layout: sections, set apart by dividers, of groups, of controls. A row of the
10
+ * toolbar shows each section that has a control in it, and a divider only between two of them.
11
+ */
12
+ export declare const TOOLBAR_SECTIONS: readonly [readonly [readonly ["block"], readonly ["font"], readonly ["fontSize"]], readonly [readonly ["bold", "italic", "underline", "strikethrough"]], readonly [readonly ["textColor", "clearFormatting"]], readonly [readonly ["bulletList", "numberedList"]], readonly [readonly ["alignLeft", "alignCenter", "alignRight"]], readonly [readonly ["outdent", "indent", "hangingOutdent", "hangingIndent", "hangAtCaret", "rightOutdent", "rightIndent"]], readonly [readonly ["spacing"]], readonly [readonly ["copyFormat"]], readonly [readonly ["link", "image", "table", "horizontalRule"]], readonly [readonly ["undo", "redo"]]];
13
+ /** The name of one of the toolbar's controls. */
14
+ export type ToolbarTool = (typeof TOOLBAR_SECTIONS)[number][number][number];
15
+ /** Every control of the toolbar, by name, in the toolbar's own order. */
16
+ export declare const TOOLBAR_TOOLS: readonly ToolbarTool[];
17
+ /**
18
+ * The known controls of a list an application gave, in the toolbar's own order. A name that
19
+ * is no control is left out, with a warning in the console (once for each name).
20
+ */
21
+ export declare function knownTools(given: readonly string[] | null | undefined, prop: string): Set<ToolbarTool>;
22
+ /**
23
+ * The sections of one row of the toolbar: those of its layout that keep a control, with the
24
+ * groups that keep one, with the controls `shown` has.
25
+ */
26
+ export declare function toolbarRow(shown: (tool: ToolbarTool) => boolean): ToolbarTool[][][];
@@ -0,0 +1,50 @@
1
+ /**
2
+ * The toolbar's controls, by names that are part of the public API: an application chooses
3
+ * which the toolbar has (`tools`) and which wait behind its More button (`moreTools`) by these,
4
+ * and each control carries its name as `data-tool`. A name does not change when a control's
5
+ * tooltip does. A control added in a later version gets a name of its own, and the release
6
+ * notes say so.
7
+ */
8
+ /**
9
+ * The toolbar's layout: sections, set apart by dividers, of groups, of controls. A row of the
10
+ * toolbar shows each section that has a control in it, and a divider only between two of them.
11
+ */
12
+ export const TOOLBAR_SECTIONS = [
13
+ [['block'], ['font'], ['fontSize']],
14
+ [['bold', 'italic', 'underline', 'strikethrough']],
15
+ [['textColor', 'clearFormatting']],
16
+ [['bulletList', 'numberedList']],
17
+ [['alignLeft', 'alignCenter', 'alignRight']],
18
+ [['outdent', 'indent', 'hangingOutdent', 'hangingIndent', 'hangAtCaret', 'rightOutdent', 'rightIndent']],
19
+ [['spacing']],
20
+ [['copyFormat']],
21
+ [['link', 'image', 'table', 'horizontalRule']],
22
+ [['undo', 'redo']]
23
+ ];
24
+ /** Every control of the toolbar, by name, in the toolbar's own order. */
25
+ export const TOOLBAR_TOOLS = Object.freeze(TOOLBAR_SECTIONS.flat(2));
26
+ const KNOWN = new Set(TOOLBAR_TOOLS);
27
+ const warned = new Set();
28
+ /**
29
+ * The known controls of a list an application gave, in the toolbar's own order. A name that
30
+ * is no control is left out, with a warning in the console (once for each name).
31
+ */
32
+ export function knownTools(given, prop) {
33
+ const tools = new Set();
34
+ for (const name of Array.isArray(given) ? given : []) {
35
+ if (KNOWN.has(name))
36
+ tools.add(name);
37
+ else if (!warned.has(`${prop}:${name}`)) {
38
+ warned.add(`${prop}:${name}`);
39
+ console.warn(`svelte-rtf-editor: \`${prop}\` names "${name}", which is no toolbar control. Known: ${TOOLBAR_TOOLS.join(', ')}.`);
40
+ }
41
+ }
42
+ return tools;
43
+ }
44
+ /**
45
+ * The sections of one row of the toolbar: those of its layout that keep a control, with the
46
+ * groups that keep one, with the controls `shown` has.
47
+ */
48
+ export function toolbarRow(shown) {
49
+ return TOOLBAR_SECTIONS.map((section) => section.map((group) => group.filter(shown)).filter((group) => group.length > 0)).filter((section) => section.length > 0);
50
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-rtf-editor",
3
- "version": "1.16.0",
3
+ "version": "1.17.0",
4
4
  "description": "RTF viewer and rich-text editor components for Svelte 5",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/jcelano/svelte-rtf-editor#readme",