svelte-rtf-editor 1.15.0 → 1.16.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
|
@@ -144,6 +144,7 @@ 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) |
|
|
147
148
|
| `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
149
|
| `keepClasses` | `string[]` | `[]` | Class names of your own that stay the same way |
|
|
149
150
|
|
|
@@ -362,6 +363,24 @@ The list is the limit. A font that is not on it is not offered, is taken off HTM
|
|
|
362
363
|
- 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
364
|
- [Clear formatting](#clear-formatting) takes a font off with the rest.
|
|
364
365
|
|
|
366
|
+
#### The font menu
|
|
367
|
+
|
|
368
|
+
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:
|
|
369
|
+
|
|
370
|
+
```svelte
|
|
371
|
+
<InkEditor fonts={['Georgia', 'Calibri, Carlito, sans-serif', 'Arial']} fontMenu />
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
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.
|
|
375
|
+
|
|
376
|
+
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:
|
|
377
|
+
|
|
378
|
+
- Shift + right-click opens the browser's menu. The foot of the font menu says so.
|
|
379
|
+
- 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).
|
|
380
|
+
- Without `fonts`, or with `readonly`, `fontMenu` changes nothing.
|
|
381
|
+
|
|
382
|
+
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.
|
|
383
|
+
|
|
365
384
|
### Clear formatting
|
|
366
385
|
|
|
367
386
|
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.
|
|
@@ -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,12 @@
|
|
|
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.
|
|
53
59
|
* keepAttributes — `data-` attributes the application's documents carry, which
|
|
54
60
|
* the editor leaves on their elements: a mark on a paragraph that
|
|
55
61
|
* the application reads back (`data-section="2"`). Every other
|
|
@@ -88,6 +94,7 @@
|
|
|
88
94
|
import { onMount } from 'svelte';
|
|
89
95
|
import Toolbar from './Toolbar.svelte';
|
|
90
96
|
import Modal from './Modal.svelte';
|
|
97
|
+
import FontMenu from './FontMenu.svelte';
|
|
91
98
|
import ImageOverlay, { type PicturePlace } from './ImageOverlay.svelte';
|
|
92
99
|
import { htmlToMarkdown, downloadFile } from '../utils.js';
|
|
93
100
|
import { keptMarks, pasteHasWords, pasteNotes, pasteWithoutFrames, removeWordHidden, sanitizedNodes, sanitizeTree, type PasteNotes } from '../sanitize.js';
|
|
@@ -160,6 +167,8 @@
|
|
|
160
167
|
fontSizes?: number[];
|
|
161
168
|
/** The fonts offered for text, beside the document's own; none unless given. */
|
|
162
169
|
fonts?: string[];
|
|
170
|
+
/** A right-click on the text opens a menu of `fonts`; the browser's menu is Shift + right-click. */
|
|
171
|
+
fontMenu?: boolean;
|
|
163
172
|
/** `data-` attributes of the application's own, kept on HTML that is set or pasted. */
|
|
164
173
|
keepAttributes?: string[];
|
|
165
174
|
/** Class names of the application's own, kept the same way. */
|
|
@@ -203,6 +212,7 @@
|
|
|
203
212
|
fontSize = DEFAULT_FONT_SIZE,
|
|
204
213
|
fontSizes = DEFAULT_FONT_SIZES,
|
|
205
214
|
fonts = NO_KEPT_ATTRIBUTES,
|
|
215
|
+
fontMenu = false,
|
|
206
216
|
keepAttributes = NO_KEPT_ATTRIBUTES,
|
|
207
217
|
keepClasses = NO_KEPT_ATTRIBUTES,
|
|
208
218
|
onchange,
|
|
@@ -892,6 +902,53 @@
|
|
|
892
902
|
return '';
|
|
893
903
|
}
|
|
894
904
|
|
|
905
|
+
// ── The font menu (`fontMenu`) ──
|
|
906
|
+
// A right-click on the text opens it where the pointer is; the menu key, or Shift+F10, at the
|
|
907
|
+
// caret. It acts on the selection the right-click left: on a Mac the word it was on, elsewhere
|
|
908
|
+
// what was selected, or a caret where the pointer was.
|
|
909
|
+
|
|
910
|
+
/** Where the font menu is open, in the window's coordinates; null where it is not. */
|
|
911
|
+
let fontMenuAt: { x: number; y: number } | null = $state(null);
|
|
912
|
+
const fontMenuChoices = $derived([
|
|
913
|
+
{ key: '', name: fontChoices([fontFamily])[0]?.name ?? DEFAULT_FONT_FAMILY, css: screenFontFamily },
|
|
914
|
+
...fontList
|
|
915
|
+
]);
|
|
916
|
+
|
|
917
|
+
function openFontMenu(e: MouseEvent): void {
|
|
918
|
+
if (!fontMenu || readonly || fontList.length === 0 || !editorEl) return;
|
|
919
|
+
const target = e.target instanceof Element ? e.target : null;
|
|
920
|
+
// The browser's own menu: asked for with Shift, and the one with what a link or a picture needs.
|
|
921
|
+
if (e.shiftKey || target?.closest('a[href], img')) return;
|
|
922
|
+
e.preventDefault();
|
|
923
|
+
// (From the keyboard the event has no place of its own: the menu opens at the caret.)
|
|
924
|
+
let { clientX: x, clientY: y } = e;
|
|
925
|
+
const fromKeyboard = e.button !== 2 || (x === 0 && y === 0);
|
|
926
|
+
if (fromKeyboard) {
|
|
927
|
+
const selection = window.getSelection();
|
|
928
|
+
const range = selection && selection.rangeCount > 0 && selectionInEditor() ? selection.getRangeAt(0) : null;
|
|
929
|
+
const box = range?.getClientRects()[0] ?? range?.getBoundingClientRect() ?? editorEl.getBoundingClientRect();
|
|
930
|
+
x = box.left;
|
|
931
|
+
y = box.bottom + 4;
|
|
932
|
+
}
|
|
933
|
+
saveSelection();
|
|
934
|
+
currentFont = fontAtSelection();
|
|
935
|
+
fontMenuAt = { x, y };
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
function closeFontMenu(refocus: boolean): void {
|
|
939
|
+
if (!fontMenuAt) return;
|
|
940
|
+
fontMenuAt = null;
|
|
941
|
+
if (refocus && editorEl) {
|
|
942
|
+
restoreSelection();
|
|
943
|
+
editorEl.focus({ preventScroll: true });
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
function pickFont(key: string): void {
|
|
948
|
+
fontMenuAt = null;
|
|
949
|
+
setFont(key);
|
|
950
|
+
}
|
|
951
|
+
|
|
895
952
|
/** The font of the text at the selection, for the toolbar: null where the text selected is in more than one. */
|
|
896
953
|
function fontAtSelection(): string | null {
|
|
897
954
|
if (pendingFont !== null) return pendingFont;
|
|
@@ -4384,9 +4441,21 @@
|
|
|
4384
4441
|
}}
|
|
4385
4442
|
ondragover={handleDragOver}
|
|
4386
4443
|
ondrop={handleDrop}
|
|
4444
|
+
oncontextmenu={openFontMenu}
|
|
4387
4445
|
data-placeholder={placeholder}
|
|
4388
4446
|
></div>
|
|
4389
4447
|
|
|
4448
|
+
{#if fontMenuAt}
|
|
4449
|
+
<FontMenu
|
|
4450
|
+
x={fontMenuAt.x}
|
|
4451
|
+
y={fontMenuAt.y}
|
|
4452
|
+
choices={fontMenuChoices}
|
|
4453
|
+
current={currentFont}
|
|
4454
|
+
onpick={pickFont}
|
|
4455
|
+
onclose={closeFontMenu}
|
|
4456
|
+
/>
|
|
4457
|
+
{/if}
|
|
4458
|
+
|
|
4390
4459
|
{#if copySourceBox}
|
|
4391
4460
|
<div
|
|
4392
4461
|
class="ink-copy-format-source"
|
|
@@ -36,6 +36,8 @@ interface Props {
|
|
|
36
36
|
fontSizes?: number[];
|
|
37
37
|
/** The fonts offered for text, beside the document's own; none unless given. */
|
|
38
38
|
fonts?: string[];
|
|
39
|
+
/** A right-click on the text opens a menu of `fonts`; the browser's menu is Shift + right-click. */
|
|
40
|
+
fontMenu?: boolean;
|
|
39
41
|
/** `data-` attributes of the application's own, kept on HTML that is set or pasted. */
|
|
40
42
|
keepAttributes?: string[];
|
|
41
43
|
/** Class names of the application's own, kept the same way. */
|
package/package.json
CHANGED