@sveltia/ui 0.79.4 → 0.80.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/dist/components/emoji/position.d.ts +2 -1
- package/dist/components/emoji/position.js +11 -3
- package/dist/components/text-editor/constants.d.ts +9 -1
- package/dist/components/text-editor/constants.js +10 -1
- package/dist/components/text-editor/core.d.ts +4 -2
- package/dist/components/text-editor/core.js +152 -33
- package/dist/components/text-editor/floating-link-editor.svelte +685 -0
- package/dist/components/text-editor/floating-link-editor.svelte.d.ts +17 -0
- package/dist/components/text-editor/html.d.ts +18 -0
- package/dist/components/text-editor/html.js +407 -0
- package/dist/components/text-editor/shiki/version.d.ts +1 -1
- package/dist/components/text-editor/shiki/version.js +1 -1
- package/dist/components/text-editor/store.svelte.js +12 -13
- package/dist/components/text-editor/text-editor.svelte +43 -17
- package/dist/components/text-editor/text-editor.svelte.d.ts +23 -6
- package/dist/components/text-editor/toolbar/insert-link-button.svelte +14 -71
- package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte +12 -5
- package/dist/locales/ar.yaml +5 -1
- package/dist/locales/bg.yaml +5 -1
- package/dist/locales/ca.yaml +5 -1
- package/dist/locales/cs.yaml +5 -1
- package/dist/locales/da.yaml +5 -1
- package/dist/locales/de.yaml +5 -1
- package/dist/locales/el.yaml +5 -1
- package/dist/locales/en-CA.yaml +5 -1
- package/dist/locales/en-GB.yaml +5 -1
- package/dist/locales/en-US.yaml +5 -1
- package/dist/locales/es-CO.yaml +5 -1
- package/dist/locales/fa.yaml +5 -1
- package/dist/locales/fi.yaml +5 -1
- package/dist/locales/fr.yaml +5 -1
- package/dist/locales/he.yaml +5 -1
- package/dist/locales/hr.yaml +5 -1
- package/dist/locales/it.yaml +5 -1
- package/dist/locales/ja.yaml +5 -1
- package/dist/locales/ko.yaml +5 -1
- package/dist/locales/nl.yaml +5 -1
- package/dist/locales/pl.yaml +5 -1
- package/dist/locales/pt-BR.yaml +5 -1
- package/dist/locales/pt-PT.yaml +5 -1
- package/dist/locales/ru.yaml +5 -1
- package/dist/locales/sk.yaml +5 -1
- package/dist/locales/sv.yaml +5 -1
- package/dist/locales/tr.yaml +5 -1
- package/dist/locales/uk.yaml +5 -1
- package/dist/locales/vi.yaml +5 -1
- package/dist/locales/zh-CN.yaml +5 -1
- package/dist/locales/zh-TW.yaml +5 -1
- package/dist/typedefs.d.ts +13 -7
- package/dist/typedefs.js +12 -6
- package/package.json +5 -4
|
@@ -0,0 +1,685 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component
|
|
3
|
+
Floating link editor for the rich text editor. While the caret is on a link, a small popup below
|
|
4
|
+
it shows the URL along with buttons to edit and remove the link, and the URL can be edited right
|
|
5
|
+
there, the same way it works in the Lexical Playground and Google Docs. The Link button and the
|
|
6
|
+
Accel+K shortcut open the popup in the edit mode, also to link the selected text.
|
|
7
|
+
|
|
8
|
+
The popup is rendered in the top layer with the Popover API, so it’s never clipped by whatever
|
|
9
|
+
contains the editor.
|
|
10
|
+
@see https://github.com/sveltia/sveltia-cms/issues/164
|
|
11
|
+
@see https://developer.mozilla.org/en-US/docs/Web/API/Popover_API
|
|
12
|
+
-->
|
|
13
|
+
<script>
|
|
14
|
+
import { $isLinkNode as isLinkNode, TOGGLE_LINK_COMMAND } from '@lexical/link';
|
|
15
|
+
import { createDOMRange } from '@lexical/selection';
|
|
16
|
+
import { $findMatchingParent as findMatchingParent } from '@lexical/utils';
|
|
17
|
+
import { _, isRTL } from '@sveltia/i18n';
|
|
18
|
+
import { matchesShortcuts } from '@sveltia/utils/events';
|
|
19
|
+
import { isURL } from '@sveltia/utils/string';
|
|
20
|
+
import {
|
|
21
|
+
COMMAND_PRIORITY_HIGH,
|
|
22
|
+
COMMAND_PRIORITY_NORMAL,
|
|
23
|
+
KEY_ESCAPE_COMMAND,
|
|
24
|
+
$getNodeByKey as getNodeByKey,
|
|
25
|
+
$getPreviousSelection as getPreviousSelection,
|
|
26
|
+
$getSelection as getSelection,
|
|
27
|
+
$isRangeSelection as isRangeSelection,
|
|
28
|
+
$setSelection as setSelection,
|
|
29
|
+
} from 'lexical';
|
|
30
|
+
import { getContext, onMount, tick, untrack } from 'svelte';
|
|
31
|
+
import Button from '../button/button.svelte';
|
|
32
|
+
import { getSuggestionListPosition } from '../emoji/position.js';
|
|
33
|
+
import Icon from '../icon/icon.svelte';
|
|
34
|
+
import TextInput from '../text-field/text-input.svelte';
|
|
35
|
+
import { OPEN_LINK_EDITOR_COMMAND } from './constants.js';
|
|
36
|
+
import { focusEditor, isSafeLinkURL } from './core.js';
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @import { LexicalEditor, RangeSelection } from 'lexical';
|
|
40
|
+
* @import { EmojiAnchorRect, TextEditorStore } from '../../typedefs.js';
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* What the edit mode is editing: an existing link, or the selected text to be linked.
|
|
45
|
+
* @typedef {{ linkKey: string } | { selection: RangeSelection }} LinkEditTarget
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Maximum width of the popup, also enforced in the stylesheet.
|
|
50
|
+
*/
|
|
51
|
+
const MAX_WIDTH = 400;
|
|
52
|
+
/**
|
|
53
|
+
* Height to assume until the popup has been measured, so the first open is positioned sensibly.
|
|
54
|
+
*/
|
|
55
|
+
const FALLBACK_HEIGHT = 48;
|
|
56
|
+
/**
|
|
57
|
+
* Margin to the viewport edges, matching the emoji suggestions.
|
|
58
|
+
*/
|
|
59
|
+
const VIEWPORT_MARGIN = 8;
|
|
60
|
+
|
|
61
|
+
const id = $props.id();
|
|
62
|
+
|
|
63
|
+
/** @type {TextEditorStore} */
|
|
64
|
+
const editorStore = getContext('editorStore');
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The link at the selection, if any.
|
|
68
|
+
* @type {{ key: string, url: string } | undefined}
|
|
69
|
+
*/
|
|
70
|
+
let currentLink = $state();
|
|
71
|
+
/**
|
|
72
|
+
* Whether the focus is within the editor or the popup. The popup is hidden otherwise, so it
|
|
73
|
+
* doesn’t linger once the user has moved on.
|
|
74
|
+
*/
|
|
75
|
+
let hasFocus = $state(false);
|
|
76
|
+
/**
|
|
77
|
+
* Key of the link the user has dismissed the popup for with the Escape key, so it doesn’t come
|
|
78
|
+
* back while the caret stays on the link.
|
|
79
|
+
* @type {string | undefined}
|
|
80
|
+
*/
|
|
81
|
+
let dismissedLinkKey = $state();
|
|
82
|
+
/**
|
|
83
|
+
* What is being edited, while the popup is in the edit mode.
|
|
84
|
+
* @type {LinkEditTarget | undefined}
|
|
85
|
+
*/
|
|
86
|
+
let editTarget = $state.raw();
|
|
87
|
+
/** URL entered in the edit mode. */
|
|
88
|
+
let url = $state('');
|
|
89
|
+
/**
|
|
90
|
+
* Viewport-relative bounds of the link, or the selected text, the popup is anchored to.
|
|
91
|
+
* @type {EmojiAnchorRect | undefined}
|
|
92
|
+
*/
|
|
93
|
+
let anchorRect = $state.raw();
|
|
94
|
+
/**
|
|
95
|
+
* Measured height of the popup.
|
|
96
|
+
* @type {number}
|
|
97
|
+
*/
|
|
98
|
+
let popupHeight = $state(0);
|
|
99
|
+
/**
|
|
100
|
+
* A reference to the popup element, which is only mounted while it’s shown.
|
|
101
|
+
* @type {HTMLElement | undefined}
|
|
102
|
+
*/
|
|
103
|
+
let popupElement = $state();
|
|
104
|
+
/**
|
|
105
|
+
* A reference to the URL field, which is only mounted in the edit mode.
|
|
106
|
+
* @type {HTMLInputElement | undefined}
|
|
107
|
+
*/
|
|
108
|
+
let inputElement = $state();
|
|
109
|
+
|
|
110
|
+
const editing = $derived(!!editTarget);
|
|
111
|
+
const isValidURL = $derived(!!url.trim() && isSafeLinkURL(url));
|
|
112
|
+
const open = $derived(
|
|
113
|
+
editing || (!!currentLink && hasFocus && dismissedLinkKey !== currentLink.key),
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Position of the popup, flipped above the link and clamped to the viewport as needed.
|
|
118
|
+
*/
|
|
119
|
+
const position = $derived(
|
|
120
|
+
anchorRect
|
|
121
|
+
? getSuggestionListPosition({
|
|
122
|
+
anchorRect,
|
|
123
|
+
listMaxHeight: popupHeight || FALLBACK_HEIGHT,
|
|
124
|
+
viewport: { width: window.innerWidth, height: window.innerHeight },
|
|
125
|
+
rtl: isRTL(),
|
|
126
|
+
listWidth: Math.min(MAX_WIDTH, window.innerWidth - VIEWPORT_MARGIN * 2),
|
|
127
|
+
})
|
|
128
|
+
: undefined,
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Find the link that contains the whole selection. This must be called within an editor state
|
|
133
|
+
* read.
|
|
134
|
+
* @returns {import('@lexical/link').LinkNode | null} Link, if any.
|
|
135
|
+
*/
|
|
136
|
+
const findLink = () => {
|
|
137
|
+
const selection = getSelection();
|
|
138
|
+
|
|
139
|
+
if (!isRangeSelection(selection)) {
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const link = findMatchingParent(selection.anchor.getNode(), isLinkNode);
|
|
144
|
+
|
|
145
|
+
return link && findMatchingParent(selection.focus.getNode(), isLinkNode) === link
|
|
146
|
+
? /** @type {import('@lexical/link').LinkNode} */ (link)
|
|
147
|
+
: null;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Get the viewport-relative bounds of what the popup is anchored to: the text being linked in the
|
|
152
|
+
* edit mode for a new link, or the link otherwise.
|
|
153
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
154
|
+
* @returns {EmojiAnchorRect | undefined} Bounds, or `undefined` if they can’t be determined.
|
|
155
|
+
*/
|
|
156
|
+
const getAnchorRect = (editor) => {
|
|
157
|
+
/** @type {DOMRect | undefined} */
|
|
158
|
+
let rect;
|
|
159
|
+
|
|
160
|
+
if (editTarget && 'selection' in editTarget) {
|
|
161
|
+
const { selection } = editTarget;
|
|
162
|
+
|
|
163
|
+
editor.getEditorState().read(() => {
|
|
164
|
+
const [start, end] = selection.isBackward()
|
|
165
|
+
? [selection.focus, selection.anchor]
|
|
166
|
+
: [selection.anchor, selection.focus];
|
|
167
|
+
|
|
168
|
+
// The selected nodes may be gone, e.g. after an undo
|
|
169
|
+
/* v8 ignore else */
|
|
170
|
+
if (start.getNode().isAttached() && end.getNode().isAttached()) {
|
|
171
|
+
rect = createDOMRange(
|
|
172
|
+
editor,
|
|
173
|
+
start.getNode(),
|
|
174
|
+
start.offset,
|
|
175
|
+
end.getNode(),
|
|
176
|
+
end.offset,
|
|
177
|
+
)?.getBoundingClientRect();
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
} else {
|
|
181
|
+
const key = editTarget && 'linkKey' in editTarget ? editTarget.linkKey : currentLink?.key;
|
|
182
|
+
|
|
183
|
+
rect = key ? editor.getElementByKey(key)?.getBoundingClientRect() : undefined;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// The link or text may not be laid out, e.g. while the editor is hidden
|
|
187
|
+
/* v8 ignore next */
|
|
188
|
+
if (!rect || !(rect.top || rect.bottom || rect.left || rect.width || rect.height)) {
|
|
189
|
+
return undefined;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const { top, bottom, left, right } = rect;
|
|
193
|
+
|
|
194
|
+
return { top, bottom, left, right };
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Follow the link when the page or an ancestor is scrolled, the window is resized, or the
|
|
199
|
+
* content has changed.
|
|
200
|
+
*/
|
|
201
|
+
const reposition = () => {
|
|
202
|
+
const { editor } = editorStore;
|
|
203
|
+
|
|
204
|
+
anchorRect = editor && open ? getAnchorRect(editor) : undefined;
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Switch the popup to the edit mode, and move the focus to the URL field.
|
|
209
|
+
* @param {LinkEditTarget} target What to edit.
|
|
210
|
+
* @param {string} initialURL URL to fill the field with.
|
|
211
|
+
*/
|
|
212
|
+
const startEditing = async (target, initialURL) => {
|
|
213
|
+
editTarget = target;
|
|
214
|
+
url = initialURL;
|
|
215
|
+
dismissedLinkKey = undefined;
|
|
216
|
+
await tick();
|
|
217
|
+
reposition();
|
|
218
|
+
await tick();
|
|
219
|
+
inputElement?.focus();
|
|
220
|
+
inputElement?.select();
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Leave the edit mode without applying anything.
|
|
225
|
+
*/
|
|
226
|
+
const stopEditing = () => {
|
|
227
|
+
editTarget = undefined;
|
|
228
|
+
url = '';
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Move the focus back to the editor, where the selection is restored, after the popup is done.
|
|
233
|
+
*/
|
|
234
|
+
const refocusEditor = async () => {
|
|
235
|
+
// The editor is there for as long as the popup can be shown
|
|
236
|
+
/* v8 ignore else */
|
|
237
|
+
if (editorStore.editor) {
|
|
238
|
+
await focusEditor(editorStore.editor);
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Start editing the link at the selection, or start linking the selected text. This is the
|
|
244
|
+
* handler for {@link OPEN_LINK_EDITOR_COMMAND}.
|
|
245
|
+
* @returns {boolean} Whether there is anything to edit. If not, the Link button opens the dialog
|
|
246
|
+
* to insert a link along with its text.
|
|
247
|
+
*/
|
|
248
|
+
const onOpenCommand = () => {
|
|
249
|
+
/** @type {LinkEditTarget | undefined} */
|
|
250
|
+
let target;
|
|
251
|
+
let initialURL = '';
|
|
252
|
+
|
|
253
|
+
editorStore.editor?.getEditorState().read(() => {
|
|
254
|
+
const link = findLink();
|
|
255
|
+
|
|
256
|
+
if (link) {
|
|
257
|
+
target = { linkKey: link.getKey() };
|
|
258
|
+
initialURL = link.getURL();
|
|
259
|
+
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// The selection is kept while the focus is on the toolbar
|
|
264
|
+
const selection = getSelection() ?? getPreviousSelection();
|
|
265
|
+
|
|
266
|
+
if (isRangeSelection(selection) && !selection.isCollapsed()) {
|
|
267
|
+
const text = selection.getTextContent().trim();
|
|
268
|
+
|
|
269
|
+
if (text) {
|
|
270
|
+
target = { selection: selection.clone() };
|
|
271
|
+
// Prefill the URL field with the selected text only if it’s a URL that can be linked to.
|
|
272
|
+
// Otherwise, it’s just the link text, like a word or phrase.
|
|
273
|
+
initialURL = isURL(text) && isSafeLinkURL(text) ? text : '';
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
if (!target) {
|
|
279
|
+
return false;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
startEditing(target, initialURL);
|
|
283
|
+
|
|
284
|
+
return true;
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Apply the URL entered in the edit mode: update the link being edited, or link the selected
|
|
289
|
+
* text.
|
|
290
|
+
*/
|
|
291
|
+
const applyURL = async () => {
|
|
292
|
+
const { editor } = editorStore;
|
|
293
|
+
const target = editTarget;
|
|
294
|
+
const newURL = url.trim();
|
|
295
|
+
|
|
296
|
+
// The Apply button is disabled and the Enter key is ignored otherwise
|
|
297
|
+
/* v8 ignore next */
|
|
298
|
+
if (!editor || !target || !isValidURL) {
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
stopEditing();
|
|
303
|
+
|
|
304
|
+
if ('linkKey' in target) {
|
|
305
|
+
editor.update(() => {
|
|
306
|
+
const node = getNodeByKey(target.linkKey);
|
|
307
|
+
|
|
308
|
+
// The edit mode ends once the link is gone; see the update listener below
|
|
309
|
+
/* v8 ignore else */
|
|
310
|
+
if (isLinkNode(node)) {
|
|
311
|
+
node.setURL(newURL);
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
} else {
|
|
315
|
+
editor.update(() => {
|
|
316
|
+
setSelection(target.selection.clone());
|
|
317
|
+
});
|
|
318
|
+
editor.dispatchCommand(TOGGLE_LINK_COMMAND, newURL);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
await refocusEditor();
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Cancel the edit mode, and bring the focus back to the editor.
|
|
326
|
+
*/
|
|
327
|
+
const cancelEditing = async () => {
|
|
328
|
+
stopEditing();
|
|
329
|
+
await refocusEditor();
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Remove the link at the selection, keeping its text.
|
|
334
|
+
*/
|
|
335
|
+
const removeLink = async () => {
|
|
336
|
+
const key = currentLink?.key;
|
|
337
|
+
|
|
338
|
+
// The Remove button is only shown for a link
|
|
339
|
+
/* v8 ignore next */
|
|
340
|
+
if (!key) {
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
editorStore.editor?.update(() => {
|
|
345
|
+
const node = getNodeByKey(key);
|
|
346
|
+
|
|
347
|
+
// The popup only shows a link that is there
|
|
348
|
+
/* v8 ignore else */
|
|
349
|
+
if (isLinkNode(node)) {
|
|
350
|
+
node.getChildren().forEach((child) => {
|
|
351
|
+
node.insertBefore(child);
|
|
352
|
+
});
|
|
353
|
+
node.remove();
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
await refocusEditor();
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Handle `keydown` event fired on the URL field.
|
|
362
|
+
* @param {KeyboardEvent} event `keydown` event.
|
|
363
|
+
*/
|
|
364
|
+
const onInputKeyDown = (event) => {
|
|
365
|
+
// Leave the keys to an input method editor while it’s composing text, where Enter commits the
|
|
366
|
+
// text and Escape cancels the composition
|
|
367
|
+
if (event.isComposing) {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
if (matchesShortcuts(event, 'Enter')) {
|
|
372
|
+
event.preventDefault();
|
|
373
|
+
|
|
374
|
+
if (isValidURL) {
|
|
375
|
+
applyURL();
|
|
376
|
+
}
|
|
377
|
+
} else if (matchesShortcuts(event, 'Escape')) {
|
|
378
|
+
// Don’t let the Escape key also close a dialog the editor is in
|
|
379
|
+
event.preventDefault();
|
|
380
|
+
event.stopPropagation();
|
|
381
|
+
cancelEditing();
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
$effect(() => {
|
|
386
|
+
const { editor } = editorStore;
|
|
387
|
+
|
|
388
|
+
// The root initializes the editor before this first runs
|
|
389
|
+
/* v8 ignore next */
|
|
390
|
+
if (!editor) {
|
|
391
|
+
return undefined;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const unregisterCommand = editor.registerCommand(
|
|
395
|
+
OPEN_LINK_EDITOR_COMMAND,
|
|
396
|
+
onOpenCommand,
|
|
397
|
+
COMMAND_PRIORITY_NORMAL,
|
|
398
|
+
);
|
|
399
|
+
|
|
400
|
+
// Dismiss the popup with the Escape key while the caret is on a link
|
|
401
|
+
const unregisterEscape = editor.registerCommand(
|
|
402
|
+
KEY_ESCAPE_COMMAND,
|
|
403
|
+
(event) => {
|
|
404
|
+
if (!open || editing || !currentLink) {
|
|
405
|
+
return false;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
event?.preventDefault();
|
|
409
|
+
dismissedLinkKey = currentLink.key;
|
|
410
|
+
|
|
411
|
+
return true;
|
|
412
|
+
},
|
|
413
|
+
COMMAND_PRIORITY_HIGH,
|
|
414
|
+
);
|
|
415
|
+
|
|
416
|
+
const unregisterUpdateListener = editor.registerUpdateListener(({ editorState }) => {
|
|
417
|
+
editorState.read(() => {
|
|
418
|
+
const link = findLink();
|
|
419
|
+
|
|
420
|
+
currentLink = link ? { key: link.getKey(), url: link.getURL() } : undefined;
|
|
421
|
+
|
|
422
|
+
// What is being edited may be gone, e.g. when the value is replaced from outside, in which
|
|
423
|
+
// case there is nothing to apply the URL to anymore
|
|
424
|
+
if (
|
|
425
|
+
editTarget &&
|
|
426
|
+
('linkKey' in editTarget
|
|
427
|
+
? !isLinkNode(getNodeByKey(editTarget.linkKey))
|
|
428
|
+
: [editTarget.selection.anchor, editTarget.selection.focus].some(
|
|
429
|
+
({ key }) => !getNodeByKey(key)?.isAttached(),
|
|
430
|
+
))
|
|
431
|
+
) {
|
|
432
|
+
stopEditing();
|
|
433
|
+
}
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
if (dismissedLinkKey && currentLink?.key !== dismissedLinkKey) {
|
|
437
|
+
dismissedLinkKey = undefined;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
reposition();
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
const unregisterEditableListener = editor.registerEditableListener((editable) => {
|
|
444
|
+
if (!editable) {
|
|
445
|
+
stopEditing();
|
|
446
|
+
}
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
return () => {
|
|
450
|
+
unregisterCommand();
|
|
451
|
+
unregisterEscape();
|
|
452
|
+
unregisterUpdateListener();
|
|
453
|
+
unregisterEditableListener();
|
|
454
|
+
};
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
// The rich text editor can be swapped for the plain text one at any time, in which case there is
|
|
458
|
+
// no link to anchor the popup to anymore
|
|
459
|
+
$effect(() => {
|
|
460
|
+
if (!editorStore.useRichText) {
|
|
461
|
+
stopEditing();
|
|
462
|
+
currentLink = undefined;
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
$effect(() => {
|
|
467
|
+
void open;
|
|
468
|
+
reposition();
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
// Move the popup to the top layer, so it’s not clipped by anything around the editor, then
|
|
472
|
+
// measure it. The measurement has to happen after the popover is shown, because until then the
|
|
473
|
+
// element isn’t rendered at all and everything measures zero.
|
|
474
|
+
$effect(() => {
|
|
475
|
+
void editing;
|
|
476
|
+
|
|
477
|
+
if (!popupElement) {
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
if (!popupElement.matches(':popover-open')) {
|
|
482
|
+
popupElement.showPopover?.();
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
popupHeight = popupElement.getBoundingClientRect().height;
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
onMount(() => {
|
|
489
|
+
/**
|
|
490
|
+
* Check if the given node is within the editor or the popup.
|
|
491
|
+
* @param {EventTarget | null} target Node.
|
|
492
|
+
* @returns {boolean} Result.
|
|
493
|
+
*/
|
|
494
|
+
const isWithin = (target) => {
|
|
495
|
+
const node = /** @type {Node | null} */ (target);
|
|
496
|
+
const root = editorStore.editor?.getRootElement();
|
|
497
|
+
|
|
498
|
+
return !!node && (!!root?.contains(node) || !!popupElement?.contains(node));
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
// The state is updated within `untrack()` below, because the browser fires these events
|
|
502
|
+
// synchronously while it removes a focused element from the DOM, e.g. the URL field when the
|
|
503
|
+
// edit mode ends — which is to say, in the middle of Svelte rendering, where a plain assignment
|
|
504
|
+
// would be reported as an unsafe mutation. See also `close()` in `<EmojiSuggestions>`.
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* Track the focus. Moving it anywhere but the popup, including back to the text, leaves the
|
|
508
|
+
* edit mode without applying anything, like clicking away from a menu.
|
|
509
|
+
* @param {FocusEvent} event `focusin` event.
|
|
510
|
+
*/
|
|
511
|
+
const onFocusIn = ({ target }) => {
|
|
512
|
+
untrack(() => {
|
|
513
|
+
hasFocus = isWithin(target);
|
|
514
|
+
|
|
515
|
+
if (editing && !popupElement?.contains(/** @type {Node} */ (target))) {
|
|
516
|
+
stopEditing();
|
|
517
|
+
}
|
|
518
|
+
});
|
|
519
|
+
};
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* Track the focus leaving the page or a removed element, which doesn’t fire `focusin`.
|
|
523
|
+
* @param {FocusEvent} event `focusout` event.
|
|
524
|
+
*/
|
|
525
|
+
const onFocusOut = ({ relatedTarget }) => {
|
|
526
|
+
if (!relatedTarget) {
|
|
527
|
+
untrack(() => {
|
|
528
|
+
hasFocus = false;
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
};
|
|
532
|
+
|
|
533
|
+
window.addEventListener('scroll', reposition, { capture: true, passive: true });
|
|
534
|
+
window.addEventListener('resize', reposition, { passive: true });
|
|
535
|
+
document.addEventListener('focusin', onFocusIn);
|
|
536
|
+
document.addEventListener('focusout', onFocusOut);
|
|
537
|
+
|
|
538
|
+
return () => {
|
|
539
|
+
window.removeEventListener('scroll', reposition, { capture: true });
|
|
540
|
+
window.removeEventListener('resize', reposition);
|
|
541
|
+
document.removeEventListener('focusin', onFocusIn);
|
|
542
|
+
document.removeEventListener('focusout', onFocusOut);
|
|
543
|
+
};
|
|
544
|
+
});
|
|
545
|
+
</script>
|
|
546
|
+
|
|
547
|
+
{#if open && position}
|
|
548
|
+
<div
|
|
549
|
+
bind:this={popupElement}
|
|
550
|
+
role="group"
|
|
551
|
+
class="sui floating-link-editor"
|
|
552
|
+
aria-label={_('_sui.text_editor.link')}
|
|
553
|
+
popover="manual"
|
|
554
|
+
style:top={position.top}
|
|
555
|
+
style:bottom={position.bottom}
|
|
556
|
+
style:left={position.left}
|
|
557
|
+
onmousedown={(event) => {
|
|
558
|
+
// Keep the focus where it is when the popup’s background is pressed, which would otherwise
|
|
559
|
+
// move it to the page, hiding the popup or blurring the URL field
|
|
560
|
+
if (event.target === event.currentTarget) {
|
|
561
|
+
event.preventDefault();
|
|
562
|
+
}
|
|
563
|
+
}}
|
|
564
|
+
>
|
|
565
|
+
{#if editing}
|
|
566
|
+
<TextInput
|
|
567
|
+
bind:element={inputElement}
|
|
568
|
+
dir="ltr"
|
|
569
|
+
id="{id}-url"
|
|
570
|
+
bind:value={url}
|
|
571
|
+
flex
|
|
572
|
+
invalid={!!url.trim() && !isSafeLinkURL(url)}
|
|
573
|
+
aria-label={_('_sui.text_editor.url')}
|
|
574
|
+
onkeydown={(event) => {
|
|
575
|
+
onInputKeyDown(event);
|
|
576
|
+
}}
|
|
577
|
+
/>
|
|
578
|
+
<Button
|
|
579
|
+
iconic
|
|
580
|
+
variant="ghost"
|
|
581
|
+
disabled={!isValidURL}
|
|
582
|
+
aria-label={editTarget && 'linkKey' in editTarget ? _('_sui.update') : _('_sui.insert')}
|
|
583
|
+
onclick={() => {
|
|
584
|
+
applyURL();
|
|
585
|
+
}}
|
|
586
|
+
>
|
|
587
|
+
{#snippet startIcon()}
|
|
588
|
+
<Icon name="check" />
|
|
589
|
+
{/snippet}
|
|
590
|
+
</Button>
|
|
591
|
+
<Button
|
|
592
|
+
iconic
|
|
593
|
+
variant="ghost"
|
|
594
|
+
aria-label={_('_sui.cancel')}
|
|
595
|
+
onclick={() => {
|
|
596
|
+
cancelEditing();
|
|
597
|
+
}}
|
|
598
|
+
>
|
|
599
|
+
{#snippet startIcon()}
|
|
600
|
+
<Icon name="close" />
|
|
601
|
+
{/snippet}
|
|
602
|
+
</Button>
|
|
603
|
+
{:else if currentLink}
|
|
604
|
+
<a
|
|
605
|
+
class="url"
|
|
606
|
+
href={isSafeLinkURL(currentLink.url) ? currentLink.url : undefined}
|
|
607
|
+
target="_blank"
|
|
608
|
+
rel="noopener noreferrer"
|
|
609
|
+
dir="ltr"
|
|
610
|
+
>
|
|
611
|
+
{currentLink.url}
|
|
612
|
+
</a>
|
|
613
|
+
<Button
|
|
614
|
+
iconic
|
|
615
|
+
variant="ghost"
|
|
616
|
+
aria-label={_('_sui.text_editor.edit_link')}
|
|
617
|
+
onclick={() => {
|
|
618
|
+
// The link is still at the selection, since the focus is within the popup
|
|
619
|
+
/* v8 ignore else */
|
|
620
|
+
if (currentLink) {
|
|
621
|
+
startEditing({ linkKey: currentLink.key }, currentLink.url);
|
|
622
|
+
}
|
|
623
|
+
}}
|
|
624
|
+
>
|
|
625
|
+
{#snippet startIcon()}
|
|
626
|
+
<Icon name="edit" />
|
|
627
|
+
{/snippet}
|
|
628
|
+
</Button>
|
|
629
|
+
<Button
|
|
630
|
+
iconic
|
|
631
|
+
variant="ghost"
|
|
632
|
+
aria-label={_('_sui.text_editor.remove_link')}
|
|
633
|
+
onclick={() => {
|
|
634
|
+
removeLink();
|
|
635
|
+
}}
|
|
636
|
+
>
|
|
637
|
+
{#snippet startIcon()}
|
|
638
|
+
<Icon name="link_off" />
|
|
639
|
+
{/snippet}
|
|
640
|
+
</Button>
|
|
641
|
+
{/if}
|
|
642
|
+
</div>
|
|
643
|
+
{/if}
|
|
644
|
+
|
|
645
|
+
<style>.floating-link-editor {
|
|
646
|
+
position: fixed;
|
|
647
|
+
inset: auto;
|
|
648
|
+
z-index: 1000;
|
|
649
|
+
display: flex;
|
|
650
|
+
align-items: center;
|
|
651
|
+
gap: 4px;
|
|
652
|
+
margin: 0;
|
|
653
|
+
border-width: var(--sui-listbox-border-width, 1px);
|
|
654
|
+
border-style: var(--sui-listbox-border-style, solid);
|
|
655
|
+
border-color: var(--sui-listbox-border-color, var(--sui-secondary-border-color));
|
|
656
|
+
border-radius: var(--sui-listbox-border-radius, 4px);
|
|
657
|
+
padding: 4px;
|
|
658
|
+
width: min(400px, 100vw - 16px);
|
|
659
|
+
color: var(--sui-primary-foreground-color);
|
|
660
|
+
background-color: var(--sui-secondary-background-color-translucent);
|
|
661
|
+
box-shadow: 0 8px 16px var(--sui-popup-shadow-color);
|
|
662
|
+
-webkit-backdrop-filter: blur(16px);
|
|
663
|
+
backdrop-filter: blur(16px);
|
|
664
|
+
font-family: var(--sui-control-font-family);
|
|
665
|
+
font-size: var(--sui-control-font-size);
|
|
666
|
+
line-height: var(--sui-control-line-height);
|
|
667
|
+
}
|
|
668
|
+
.floating-link-editor :global(.sui.text-input) {
|
|
669
|
+
flex: auto;
|
|
670
|
+
margin: 0 !important;
|
|
671
|
+
min-width: 0;
|
|
672
|
+
}
|
|
673
|
+
.floating-link-editor :global(.sui.button) {
|
|
674
|
+
flex: none;
|
|
675
|
+
margin: 0 !important;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
.url {
|
|
679
|
+
flex: auto;
|
|
680
|
+
overflow: hidden;
|
|
681
|
+
padding: 0 8px;
|
|
682
|
+
min-width: 0;
|
|
683
|
+
white-space: nowrap;
|
|
684
|
+
text-overflow: ellipsis;
|
|
685
|
+
}</style>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export default FloatingLinkEditor;
|
|
2
|
+
type FloatingLinkEditor = {
|
|
3
|
+
$on?(type: string, callback: (e: any) => void): () => void;
|
|
4
|
+
$set?(props: Partial<Record<string, never>>): void;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Floating link editor for the rich text editor. While the caret is on a link, a small popup below
|
|
8
|
+
* it shows the URL along with buttons to edit and remove the link, and the URL can be edited right
|
|
9
|
+
* there, the same way it works in the Lexical Playground and Google Docs. The Link button and the
|
|
10
|
+
* Accel+K shortcut open the popup in the edit mode, also to link the selected text.
|
|
11
|
+
*
|
|
12
|
+
* The popup is rendered in the top layer with the Popover API, so it’s never clipped by whatever
|
|
13
|
+
* contains the editor.
|
|
14
|
+
* @see https://github.com/sveltia/sveltia-cms/issues/164
|
|
15
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Popover_API
|
|
16
|
+
*/
|
|
17
|
+
declare const FloatingLinkEditor: import("svelte").Component<Record<string, never>, {}, "">;
|