svelte-rtf-editor 1.15.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 +84 -0
- package/dist/components/ColorPicker.svelte +4 -2
- package/dist/components/ColorPicker.svelte.d.ts +2 -0
- package/dist/components/FontMenu.svelte +187 -0
- package/dist/components/FontMenu.svelte.d.ts +20 -0
- package/dist/components/InkEditor.svelte +129 -2
- package/dist/components/InkEditor.svelte.d.ts +120 -1
- package/dist/components/Toolbar.svelte +265 -131
- package/dist/components/Toolbar.svelte.d.ts +13 -0
- package/dist/components/ToolbarButton.svelte +4 -1
- package/dist/components/ToolbarButton.svelte.d.ts +2 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/toolbar-tools.d.ts +26 -0
- package/dist/toolbar-tools.js +50 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -144,6 +144,12 @@ Pass an HTML string as `content`. If the content is already RTF, convert it firs
|
|
|
144
144
|
| `fontSize` | `number` | `12` | Body text size in points, on screen and in the RTF |
|
|
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
|
+
| `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) |
|
|
147
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) |
|
|
148
154
|
| `keepClasses` | `string[]` | `[]` | Class names of your own that stay the same way |
|
|
149
155
|
|
|
@@ -154,6 +160,7 @@ Pass an HTML string as `content`. If the content is already RTF, convert it firs
|
|
|
154
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 |
|
|
155
161
|
| `onsave` | `{ html }` | Fired on `Ctrl+S`, and after each auto-save where `autosave` is on |
|
|
156
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 |
|
|
157
164
|
|
|
158
165
|
#### Methods (via `bind:this`)
|
|
159
166
|
|
|
@@ -281,6 +288,65 @@ The editing area grows with the document, and the page scrolls. The toolbar stay
|
|
|
281
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.
|
|
282
289
|
- `stickyToolbar={false}` lets the toolbar go with the page, as it did through 1.12.0.
|
|
283
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
|
+
|
|
284
350
|
### Opening a link
|
|
285
351
|
|
|
286
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.
|
|
@@ -362,6 +428,24 @@ The list is the limit. A font that is not on it is not offered, is taken off HTM
|
|
|
362
428
|
- Code stays in Courier New whatever font is around it. A monospace font you offer is written as that font, and read back as code, which is what a reader makes of text in a monospace font.
|
|
363
429
|
- [Clear formatting](#clear-formatting) takes a font off with the rest.
|
|
364
430
|
|
|
431
|
+
#### The font menu
|
|
432
|
+
|
|
433
|
+
The font list is in the toolbar. Set `fontMenu` and a right-click on the text opens the same fonts as a menu as well, where the pointer is:
|
|
434
|
+
|
|
435
|
+
```svelte
|
|
436
|
+
<InkEditor fonts={['Georgia', 'Calibri, Carlito, sans-serif', 'Arial']} fontMenu />
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
The menu shows the document font first, then yours, each in its own font, with the font of the selected text checked. Choosing one sets the font of the selection, exactly as the toolbar's list does, and Undo takes it off again. On a Mac a right-click selects the word it is on, so a right-click on a word and a choice from the menu are enough to set that word.
|
|
440
|
+
|
|
441
|
+
The menu is off by default because it takes the place of the browser's own menu, and with it the spelling suggestions, Copy and Paste. The browser's menu is still there when you need it:
|
|
442
|
+
|
|
443
|
+
- Shift + right-click opens the browser's menu. The foot of the font menu says so.
|
|
444
|
+
- A right-click on a link or a picture opens the browser's menu, which has what a link or a picture needs (Open link, Copy image).
|
|
445
|
+
- Without `fonts`, or with `readonly`, `fontMenu` changes nothing.
|
|
446
|
+
|
|
447
|
+
From the keyboard, the menu key or Shift+F10 opens the menu at the caret, where the browser sends the page the menu's event (Chromium does; check the others in your own setup). In the menu, the arrow keys, Home and End move, Enter or Space chooses, and Escape or Tab closes it and gives the text the keyboard back. A click anywhere else, a scroll, or the window losing the keyboard closes it too.
|
|
448
|
+
|
|
365
449
|
### Clear formatting
|
|
366
450
|
|
|
367
451
|
The toolbar's Clear formatting button, next to the text color, takes the formatting off the selected text. `editor.clearFormatting()` does the same from your own toolbar. With nothing selected it changes nothing in the document.
|
|
@@ -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>
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* The font menu a right-click on the text opens (InkEditor's `fontMenu`): the document's
|
|
4
|
+
* font and the fonts offered, each in itself, the font of the selected text checked. It
|
|
5
|
+
* takes the keyboard while it is open: the arrows, Home and End move in it, Enter or Space
|
|
6
|
+
* chooses, Escape or Tab closes it. A press anywhere else closes it, and so do a scroll
|
|
7
|
+
* and the window losing the keyboard.
|
|
8
|
+
*/
|
|
9
|
+
import { onMount, tick } from 'svelte';
|
|
10
|
+
|
|
11
|
+
interface Choice {
|
|
12
|
+
key: string;
|
|
13
|
+
name: string;
|
|
14
|
+
css: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface Props {
|
|
18
|
+
/** Where the menu opens, in the window's coordinates: its top left corner. */
|
|
19
|
+
x: number;
|
|
20
|
+
y: number;
|
|
21
|
+
/** The choices, the document's own font first, with the key ''. */
|
|
22
|
+
choices: Choice[];
|
|
23
|
+
/** The key of the font of the selected text; null where it is in more than one. */
|
|
24
|
+
current: string | null;
|
|
25
|
+
onpick: (key: string) => void;
|
|
26
|
+
/** `refocus`: the menu was left from the keyboard, and the text should have it back. */
|
|
27
|
+
onclose: (refocus: boolean) => void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let { x, y, choices, current, onpick, onclose }: Props = $props();
|
|
31
|
+
|
|
32
|
+
let menuEl: HTMLDivElement | null = $state(null);
|
|
33
|
+
/** Where the menu is shown: where it opens, moved in so that all of it is in the window. */
|
|
34
|
+
let left = $state(0);
|
|
35
|
+
let top = $state(0);
|
|
36
|
+
|
|
37
|
+
const MARGIN = 8;
|
|
38
|
+
|
|
39
|
+
function items(): HTMLElement[] {
|
|
40
|
+
return menuEl ? Array.from(menuEl.querySelectorAll<HTMLElement>('[role="menuitemradio"]')) : [];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function focusItem(index: number): void {
|
|
44
|
+
const all = items();
|
|
45
|
+
if (all.length === 0) return;
|
|
46
|
+
all[(index + all.length) % all.length].focus();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
onMount(() => {
|
|
50
|
+
left = x;
|
|
51
|
+
top = y;
|
|
52
|
+
void tick().then(() => {
|
|
53
|
+
if (!menuEl) return;
|
|
54
|
+
const box = menuEl.getBoundingClientRect();
|
|
55
|
+
left = Math.max(MARGIN, Math.min(x, window.innerWidth - box.width - MARGIN));
|
|
56
|
+
top = y + box.height + MARGIN > window.innerHeight ? Math.max(MARGIN, y - box.height) : y;
|
|
57
|
+
const at = choices.findIndex((choice) => choice.key === current);
|
|
58
|
+
focusItem(at < 0 ? 0 : at);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
function onkeydown(e: KeyboardEvent): void {
|
|
63
|
+
const all = items();
|
|
64
|
+
const at = all.indexOf(document.activeElement as HTMLElement);
|
|
65
|
+
if (e.key === 'ArrowDown') focusItem(at + 1);
|
|
66
|
+
else if (e.key === 'ArrowUp') focusItem(at < 0 ? -1 : at - 1);
|
|
67
|
+
else if (e.key === 'Home') focusItem(0);
|
|
68
|
+
else if (e.key === 'End') focusItem(-1);
|
|
69
|
+
else if (e.key === 'Escape' || e.key === 'Tab') onclose(true);
|
|
70
|
+
else return;
|
|
71
|
+
e.preventDefault();
|
|
72
|
+
e.stopPropagation();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** A press outside the menu closes it, and goes on to do what it does there. */
|
|
76
|
+
function pressedOutside(e: PointerEvent): void {
|
|
77
|
+
if (menuEl && !menuEl.contains(e.target as Node)) onclose(false);
|
|
78
|
+
}
|
|
79
|
+
</script>
|
|
80
|
+
|
|
81
|
+
<svelte:window
|
|
82
|
+
onpointerdowncapture={pressedOutside}
|
|
83
|
+
onscrollcapture={(e) => {
|
|
84
|
+
if (!menuEl?.contains(e.target as Node)) onclose(false);
|
|
85
|
+
}}
|
|
86
|
+
onresize={() => onclose(false)}
|
|
87
|
+
onblur={() => onclose(false)}
|
|
88
|
+
/>
|
|
89
|
+
|
|
90
|
+
<div
|
|
91
|
+
class="ink-font-menu"
|
|
92
|
+
role="menu"
|
|
93
|
+
aria-label="Font"
|
|
94
|
+
tabindex="-1"
|
|
95
|
+
style:left="{left}px"
|
|
96
|
+
style:top="{top}px"
|
|
97
|
+
bind:this={menuEl}
|
|
98
|
+
{onkeydown}
|
|
99
|
+
oncontextmenu={(e) => e.preventDefault()}
|
|
100
|
+
>
|
|
101
|
+
<div class="ink-font-menu-title" aria-hidden="true">Font</div>
|
|
102
|
+
{#each choices as choice (choice.key)}
|
|
103
|
+
<button
|
|
104
|
+
type="button"
|
|
105
|
+
role="menuitemradio"
|
|
106
|
+
aria-checked={choice.key === current}
|
|
107
|
+
tabindex="-1"
|
|
108
|
+
style:font-family={choice.css}
|
|
109
|
+
onmousedown={(e) => e.preventDefault()}
|
|
110
|
+
onclick={() => onpick(choice.key)}
|
|
111
|
+
>
|
|
112
|
+
<span class="ink-font-menu-check" aria-hidden="true">{choice.key === current ? '✓' : ''}</span>
|
|
113
|
+
{choice.name}
|
|
114
|
+
</button>
|
|
115
|
+
{/each}
|
|
116
|
+
<div class="ink-font-menu-note" aria-hidden="true">Shift + right-click: the browser's menu</div>
|
|
117
|
+
</div>
|
|
118
|
+
|
|
119
|
+
<style>
|
|
120
|
+
.ink-font-menu {
|
|
121
|
+
position: fixed;
|
|
122
|
+
z-index: 1000;
|
|
123
|
+
box-sizing: border-box;
|
|
124
|
+
min-width: 190px;
|
|
125
|
+
max-width: min(320px, calc(100vw - 16px));
|
|
126
|
+
max-height: calc(100vh - 16px);
|
|
127
|
+
overflow-y: auto;
|
|
128
|
+
padding: 4px;
|
|
129
|
+
border: 1px solid var(--border, #d9d4cc);
|
|
130
|
+
border-radius: 10px;
|
|
131
|
+
background: var(--bg, #fff);
|
|
132
|
+
color: var(--text, #1a1714);
|
|
133
|
+
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.16);
|
|
134
|
+
font-family: 'DM Sans', system-ui, sans-serif;
|
|
135
|
+
font-size: 14px;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
.ink-font-menu:focus {
|
|
139
|
+
outline: none;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
.ink-font-menu-title {
|
|
143
|
+
padding: 4px 10px 2px;
|
|
144
|
+
font-size: 11px;
|
|
145
|
+
font-weight: 600;
|
|
146
|
+
letter-spacing: 0.04em;
|
|
147
|
+
text-transform: uppercase;
|
|
148
|
+
color: var(--text-muted, #8a7e72);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
.ink-font-menu button {
|
|
152
|
+
display: flex;
|
|
153
|
+
align-items: center;
|
|
154
|
+
gap: 6px;
|
|
155
|
+
width: 100%;
|
|
156
|
+
padding: 6px 10px 6px 6px;
|
|
157
|
+
border: 0;
|
|
158
|
+
border-radius: 6px;
|
|
159
|
+
background: transparent;
|
|
160
|
+
color: inherit;
|
|
161
|
+
font-size: 15px;
|
|
162
|
+
text-align: left;
|
|
163
|
+
cursor: pointer;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
.ink-font-menu button:hover,
|
|
167
|
+
.ink-font-menu button:focus-visible,
|
|
168
|
+
.ink-font-menu button:focus {
|
|
169
|
+
outline: none;
|
|
170
|
+
background: var(--accent-soft, #ede9fe);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
.ink-font-menu-check {
|
|
174
|
+
display: inline-block;
|
|
175
|
+
width: 1.1em;
|
|
176
|
+
font-family: 'DM Sans', system-ui, sans-serif;
|
|
177
|
+
color: var(--accent, #6e56cf);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
.ink-font-menu-note {
|
|
181
|
+
margin-top: 4px;
|
|
182
|
+
padding: 6px 10px 4px;
|
|
183
|
+
border-top: 1px solid var(--border, #d9d4cc);
|
|
184
|
+
font-size: 11px;
|
|
185
|
+
color: var(--text-muted, #8a7e72);
|
|
186
|
+
}
|
|
187
|
+
</style>
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
interface Choice {
|
|
2
|
+
key: string;
|
|
3
|
+
name: string;
|
|
4
|
+
css: string;
|
|
5
|
+
}
|
|
6
|
+
interface Props {
|
|
7
|
+
/** Where the menu opens, in the window's coordinates: its top left corner. */
|
|
8
|
+
x: number;
|
|
9
|
+
y: number;
|
|
10
|
+
/** The choices, the document's own font first, with the key ''. */
|
|
11
|
+
choices: Choice[];
|
|
12
|
+
/** The key of the font of the selected text; null where it is in more than one. */
|
|
13
|
+
current: string | null;
|
|
14
|
+
onpick: (key: string) => void;
|
|
15
|
+
/** `refocus`: the menu was left from the keyboard, and the text should have it back. */
|
|
16
|
+
onclose: (refocus: boolean) => void;
|
|
17
|
+
}
|
|
18
|
+
declare const FontMenu: import("svelte").Component<Props, {}, "">;
|
|
19
|
+
type FontMenu = ReturnType<typeof FontMenu>;
|
|
20
|
+
export default FontMenu;
|
|
@@ -50,6 +50,24 @@
|
|
|
50
50
|
* any other font is taken off HTML as it comes in, set or pasted.
|
|
51
51
|
* Default: none: there is no font list, and no font but the
|
|
52
52
|
* document's is let in.
|
|
53
|
+
* fontMenu — If true, a right-click on the text opens the editor's own menu of
|
|
54
|
+
* `fonts` in place of the browser's, to set the font of the selected
|
|
55
|
+
* text. Shift with the right-click still opens the browser's menu (its
|
|
56
|
+
* spelling suggestions, Copy and Paste), and so does a right-click on a
|
|
57
|
+
* link or a picture. The menu key and Shift+F10 open it at the caret.
|
|
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).
|
|
53
71
|
* keepAttributes — `data-` attributes the application's documents carry, which
|
|
54
72
|
* the editor leaves on their elements: a mark on a paragraph that
|
|
55
73
|
* the application reads back (`data-section="2"`). Every other
|
|
@@ -65,6 +83,7 @@
|
|
|
65
83
|
* { html, text, wordCount, charCount, estimatedRtfBytes }.
|
|
66
84
|
* onsave — Fires on auto-save or Ctrl+S with { html }.
|
|
67
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.
|
|
68
87
|
*
|
|
69
88
|
* Bindable methods (via bind:this):
|
|
70
89
|
* getHTML() — Returns the current editor HTML string.
|
|
@@ -85,9 +104,11 @@
|
|
|
85
104
|
* the application's own marks stay.
|
|
86
105
|
*/
|
|
87
106
|
|
|
88
|
-
import { onMount } from 'svelte';
|
|
107
|
+
import { onMount, type Snippet } from 'svelte';
|
|
108
|
+
import { knownTools, TOOLBAR_TOOLS, type ToolbarTool } from '../toolbar-tools.js';
|
|
89
109
|
import Toolbar from './Toolbar.svelte';
|
|
90
110
|
import Modal from './Modal.svelte';
|
|
111
|
+
import FontMenu from './FontMenu.svelte';
|
|
91
112
|
import ImageOverlay, { type PicturePlace } from './ImageOverlay.svelte';
|
|
92
113
|
import { htmlToMarkdown, downloadFile } from '../utils.js';
|
|
93
114
|
import { keptMarks, pasteHasWords, pasteNotes, pasteWithoutFrames, removeWordHidden, sanitizedNodes, sanitizeTree, type PasteNotes } from '../sanitize.js';
|
|
@@ -160,6 +181,19 @@
|
|
|
160
181
|
fontSizes?: number[];
|
|
161
182
|
/** The fonts offered for text, beside the document's own; none unless given. */
|
|
162
183
|
fonts?: string[];
|
|
184
|
+
/** A right-click on the text opens a menu of `fonts`; the browser's menu is Shift + right-click. */
|
|
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;
|
|
163
197
|
/** `data-` attributes of the application's own, kept on HTML that is set or pasted. */
|
|
164
198
|
keepAttributes?: string[];
|
|
165
199
|
/** Class names of the application's own, kept the same way. */
|
|
@@ -182,6 +216,7 @@
|
|
|
182
216
|
/** The sizes, in points, the toolbar's size list offers unless `fontSizes` says otherwise. */
|
|
183
217
|
const DEFAULT_FONT_SIZES = [8, 9, 10, 11, 12, 14, 16, 18, 20, 24, 28, 36];
|
|
184
218
|
const NO_KEPT_ATTRIBUTES: string[] = [];
|
|
219
|
+
const NO_TOOLS: readonly ToolbarTool[] = [];
|
|
185
220
|
|
|
186
221
|
// ── Props ──
|
|
187
222
|
let {
|
|
@@ -203,6 +238,14 @@
|
|
|
203
238
|
fontSize = DEFAULT_FONT_SIZE,
|
|
204
239
|
fontSizes = DEFAULT_FONT_SIZES,
|
|
205
240
|
fonts = NO_KEPT_ATTRIBUTES,
|
|
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,
|
|
206
249
|
keepAttributes = NO_KEPT_ATTRIBUTES,
|
|
207
250
|
keepClasses = NO_KEPT_ATTRIBUTES,
|
|
208
251
|
onchange,
|
|
@@ -210,6 +253,14 @@
|
|
|
210
253
|
onimport
|
|
211
254
|
}: Props = $props();
|
|
212
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
|
+
|
|
213
264
|
const keptNames = $derived(keptMarks({ keepAttributes, keepClasses, fonts, fontFamily }));
|
|
214
265
|
|
|
215
266
|
// The same font and size go to the screen and to the RTF, so the editor
|
|
@@ -892,6 +943,53 @@
|
|
|
892
943
|
return '';
|
|
893
944
|
}
|
|
894
945
|
|
|
946
|
+
// ── The font menu (`fontMenu`) ──
|
|
947
|
+
// A right-click on the text opens it where the pointer is; the menu key, or Shift+F10, at the
|
|
948
|
+
// caret. It acts on the selection the right-click left: on a Mac the word it was on, elsewhere
|
|
949
|
+
// what was selected, or a caret where the pointer was.
|
|
950
|
+
|
|
951
|
+
/** Where the font menu is open, in the window's coordinates; null where it is not. */
|
|
952
|
+
let fontMenuAt: { x: number; y: number } | null = $state(null);
|
|
953
|
+
const fontMenuChoices = $derived([
|
|
954
|
+
{ key: '', name: fontChoices([fontFamily])[0]?.name ?? DEFAULT_FONT_FAMILY, css: screenFontFamily },
|
|
955
|
+
...fontList
|
|
956
|
+
]);
|
|
957
|
+
|
|
958
|
+
function openFontMenu(e: MouseEvent): void {
|
|
959
|
+
if (!fontMenu || readonly || fontList.length === 0 || !editorEl) return;
|
|
960
|
+
const target = e.target instanceof Element ? e.target : null;
|
|
961
|
+
// The browser's own menu: asked for with Shift, and the one with what a link or a picture needs.
|
|
962
|
+
if (e.shiftKey || target?.closest('a[href], img')) return;
|
|
963
|
+
e.preventDefault();
|
|
964
|
+
// (From the keyboard the event has no place of its own: the menu opens at the caret.)
|
|
965
|
+
let { clientX: x, clientY: y } = e;
|
|
966
|
+
const fromKeyboard = e.button !== 2 || (x === 0 && y === 0);
|
|
967
|
+
if (fromKeyboard) {
|
|
968
|
+
const selection = window.getSelection();
|
|
969
|
+
const range = selection && selection.rangeCount > 0 && selectionInEditor() ? selection.getRangeAt(0) : null;
|
|
970
|
+
const box = range?.getClientRects()[0] ?? range?.getBoundingClientRect() ?? editorEl.getBoundingClientRect();
|
|
971
|
+
x = box.left;
|
|
972
|
+
y = box.bottom + 4;
|
|
973
|
+
}
|
|
974
|
+
saveSelection();
|
|
975
|
+
currentFont = fontAtSelection();
|
|
976
|
+
fontMenuAt = { x, y };
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
function closeFontMenu(refocus: boolean): void {
|
|
980
|
+
if (!fontMenuAt) return;
|
|
981
|
+
fontMenuAt = null;
|
|
982
|
+
if (refocus && editorEl) {
|
|
983
|
+
restoreSelection();
|
|
984
|
+
editorEl.focus({ preventScroll: true });
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
function pickFont(key: string): void {
|
|
989
|
+
fontMenuAt = null;
|
|
990
|
+
setFont(key);
|
|
991
|
+
}
|
|
992
|
+
|
|
895
993
|
/** The font of the text at the selection, for the toolbar: null where the text selected is in more than one. */
|
|
896
994
|
function fontAtSelection(): string | null {
|
|
897
995
|
if (pendingFont !== null) return pendingFont;
|
|
@@ -4186,8 +4284,17 @@
|
|
|
4186
4284
|
|
|
4187
4285
|
|
|
4188
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
|
+
|
|
4189
4295
|
onMount(() => {
|
|
4190
4296
|
if (!editorEl) return;
|
|
4297
|
+
started = true;
|
|
4191
4298
|
document.addEventListener('selectionchange', trackSelection);
|
|
4192
4299
|
const stopTracking = () => {
|
|
4193
4300
|
document.removeEventListener('selectionchange', trackSelection);
|
|
@@ -4320,6 +4427,13 @@
|
|
|
4320
4427
|
spacing={currentSpacing}
|
|
4321
4428
|
copyingFormat={copyFormat !== 'off'}
|
|
4322
4429
|
oncopyformat={toggleCopyFormat}
|
|
4430
|
+
tools={toolSet}
|
|
4431
|
+
moreTools={moreToolSet}
|
|
4432
|
+
{moreOpen}
|
|
4433
|
+
onmoretoggle={toggleMore}
|
|
4434
|
+
{moreLabel}
|
|
4435
|
+
{lessLabel}
|
|
4436
|
+
end={toolbarEnd}
|
|
4323
4437
|
/>
|
|
4324
4438
|
{/if}
|
|
4325
4439
|
|
|
@@ -4349,12 +4463,13 @@
|
|
|
4349
4463
|
class:copy-format-pick={copyFormat === 'pick'}
|
|
4350
4464
|
class:copy-format-apply={copyFormat === 'apply'}
|
|
4351
4465
|
class:links-open={linkArmed && linkHint !== null}
|
|
4352
|
-
contenteditable={!readonly}
|
|
4466
|
+
contenteditable={started && !readonly}
|
|
4353
4467
|
role="textbox"
|
|
4354
4468
|
tabindex="0"
|
|
4355
4469
|
aria-multiline="true"
|
|
4356
4470
|
aria-label={ariaLabel}
|
|
4357
4471
|
aria-readonly={readonly ? 'true' : undefined}
|
|
4472
|
+
aria-busy={started ? undefined : 'true'}
|
|
4358
4473
|
spellcheck="true"
|
|
4359
4474
|
bind:this={editorEl}
|
|
4360
4475
|
onbeforeinput={handleBeforeInput}
|
|
@@ -4384,9 +4499,21 @@
|
|
|
4384
4499
|
}}
|
|
4385
4500
|
ondragover={handleDragOver}
|
|
4386
4501
|
ondrop={handleDrop}
|
|
4502
|
+
oncontextmenu={openFontMenu}
|
|
4387
4503
|
data-placeholder={placeholder}
|
|
4388
4504
|
></div>
|
|
4389
4505
|
|
|
4506
|
+
{#if fontMenuAt}
|
|
4507
|
+
<FontMenu
|
|
4508
|
+
x={fontMenuAt.x}
|
|
4509
|
+
y={fontMenuAt.y}
|
|
4510
|
+
choices={fontMenuChoices}
|
|
4511
|
+
current={currentFont}
|
|
4512
|
+
onpick={pickFont}
|
|
4513
|
+
onclose={closeFontMenu}
|
|
4514
|
+
/>
|
|
4515
|
+
{/if}
|
|
4516
|
+
|
|
4390
4517
|
{#if copySourceBox}
|
|
4391
4518
|
<div
|
|
4392
4519
|
class="ink-copy-format-source"
|