@sveltia/ui 0.89.2 → 0.90.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/checkbox/checkbox.svelte +5 -8
- package/dist/components/dialog/dialog.svelte +149 -9
- package/dist/components/emoji/emoji-suggestions.svelte +7 -7
- package/dist/components/grid/grid-col-header.svelte +4 -0
- package/dist/components/grid/grid-head.svelte +1 -1
- package/dist/components/grid/grid-head.svelte.d.ts +1 -1
- package/dist/components/radio/radio.svelte +9 -9
- package/dist/components/resizable-pane/resizable-pane-group.svelte +5 -20
- package/dist/components/resizable-pane/sizing.d.ts +6 -0
- package/dist/components/resizable-pane/sizing.js +31 -0
- package/dist/components/text-editor/code-editor.svelte +22 -39
- package/dist/components/text-editor/constants.js +3 -3
- package/dist/components/text-editor/conversion/editor-components.d.ts +5 -0
- package/dist/components/text-editor/conversion/editor-components.js +228 -0
- package/dist/components/text-editor/{html.d.ts → conversion/html.d.ts} +1 -1
- package/dist/components/text-editor/{html.js → conversion/html.js} +158 -20
- package/dist/components/text-editor/conversion/import.d.ts +12 -0
- package/dist/components/text-editor/conversion/import.js +327 -0
- package/dist/components/text-editor/conversion/paste.d.ts +4 -0
- package/dist/components/text-editor/conversion/paste.js +166 -0
- package/dist/components/text-editor/{transformers → conversion/transformers}/raw-syntax.js +4 -4
- package/dist/components/text-editor/conversion/transformers/table.js +342 -0
- package/dist/components/text-editor/conversion/update.d.ts +6 -0
- package/dist/components/text-editor/conversion/update.js +125 -0
- package/dist/components/text-editor/core.d.ts +50 -26
- package/dist/components/text-editor/core.js +133 -1694
- package/dist/components/text-editor/editing/caret.d.ts +4 -0
- package/dist/components/text-editor/editing/caret.js +248 -0
- package/dist/components/text-editor/editing/commands.d.ts +5 -0
- package/dist/components/text-editor/editing/commands.js +205 -0
- package/dist/components/text-editor/editing/link.d.ts +19 -0
- package/dist/components/text-editor/editing/link.js +222 -0
- package/dist/components/text-editor/{raw-markdown.d.ts → editing/raw-markdown.d.ts} +3 -3
- package/dist/components/text-editor/{raw-markdown.js → editing/raw-markdown.js} +2 -2
- package/dist/components/text-editor/editing/selection.d.ts +5 -0
- package/dist/components/text-editor/editing/selection.js +122 -0
- package/dist/components/text-editor/editing/single-line.d.ts +8 -0
- package/dist/components/text-editor/editing/single-line.js +190 -0
- package/dist/components/text-editor/{table.js → editing/table.js} +1 -1
- package/dist/components/text-editor/lexical-root.svelte +61 -10
- package/dist/components/text-editor/shiki/preload.d.ts +1 -0
- package/dist/components/text-editor/shiki/preload.js +30 -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.d.ts +5 -0
- package/dist/components/text-editor/store.svelte.js +55 -1
- package/dist/components/text-editor/text-editor.svelte +29 -41
- package/dist/components/text-editor/toolbar/align-menu-button.svelte +1 -1
- package/dist/components/text-editor/toolbar/code-language-switcher.svelte +2 -1
- package/dist/components/text-editor/toolbar/format-text-button.svelte +6 -2
- package/dist/components/text-editor/toolbar/insert-component.js +1 -1
- package/dist/components/text-editor/toolbar/insert-horizontal-rule-button.svelte +4 -4
- package/dist/components/text-editor/toolbar/insert-link-button.svelte +3 -2
- package/dist/components/text-editor/toolbar/insert-table-button.svelte +7 -3
- package/dist/components/text-editor/toolbar/table-action-menu.svelte +3 -3
- package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte +1 -1
- package/dist/components/text-editor/toolbar/toggle-block-menu-item.svelte +2 -2
- package/dist/components/text-editor/utils.d.ts +1 -0
- package/dist/components/text-editor/utils.js +17 -0
- package/dist/components/text-editor/{emoji-autocomplete.svelte → widgets/emoji-autocomplete.svelte} +3 -3
- package/dist/components/text-editor/{floating-link-editor.svelte → widgets/floating-link-editor.svelte} +36 -79
- package/dist/components/text-editor/{table-cell-menu-button.svelte → widgets/table-cell-menu-button.svelte} +15 -5
- package/dist/components/text-field/date-time-input.svelte +8 -8
- package/dist/components/text-field/text-area.svelte +10 -10
- package/dist/components/text-field/text-input.svelte +7 -7
- package/dist/components/util/modal.svelte +41 -12
- package/dist/components/util/popup.svelte +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/locales/es-ES.yaml +174 -0
- package/dist/services/label.d.ts +8 -0
- package/dist/services/label.js +15 -0
- package/dist/services/popup-position.d.ts +9 -0
- package/dist/services/popup-position.js +52 -55
- package/dist/styles/mixins.scss +69 -0
- package/dist/typedefs.d.ts +28 -1
- package/dist/typedefs.js +16 -1
- package/package.json +13 -11
- package/dist/components/text-editor/transformers/table.js +0 -296
- /package/dist/components/text-editor/{markdown.d.ts → conversion/markdown.d.ts} +0 -0
- /package/dist/components/text-editor/{markdown.js → conversion/markdown.js} +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/block-escape.d.ts +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/block-escape.js +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/code.d.ts +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/code.js +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/formatted-code.d.ts +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/formatted-code.js +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/hr.d.ts +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/hr.js +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/inline-code.d.ts +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/inline-code.js +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/link.d.ts +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/link.js +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/raw-syntax.d.ts +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/table.d.ts +0 -0
- /package/dist/components/text-editor/{alignment.d.ts → editing/alignment.d.ts} +0 -0
- /package/dist/components/text-editor/{alignment.js → editing/alignment.js} +0 -0
- /package/dist/components/text-editor/{block.d.ts → editing/block.d.ts} +0 -0
- /package/dist/components/text-editor/{block.js → editing/block.js} +0 -0
- /package/dist/components/text-editor/{table.d.ts → editing/table.d.ts} +0 -0
- /package/dist/components/text-editor/{emoji-autocomplete.svelte.d.ts → widgets/emoji-autocomplete.svelte.d.ts} +0 -0
- /package/dist/components/text-editor/{floating-link-editor.svelte.d.ts → widgets/floating-link-editor.svelte.d.ts} +0 -0
- /package/dist/components/text-editor/{table-cell-menu-button.svelte.d.ts → widgets/table-cell-menu-button.svelte.d.ts} +0 -0
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export function isStaticDecoratorContent(target: EventTarget | null): boolean;
|
|
2
|
+
export function handleEditorMouseDown(editor: LexicalEditor, event: MouseEvent): boolean;
|
|
3
|
+
export function focusEditor(editor: LexicalEditor): Promise<void>;
|
|
4
|
+
import type { LexicalEditor } from 'lexical';
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Placing the caret and the focus in the editor.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
$createParagraphNode as createParagraphNode,
|
|
7
|
+
$getNearestNodeFromDOMNode as getNearestNodeFromDOMNode,
|
|
8
|
+
$getRoot as getRoot,
|
|
9
|
+
$getSelection as getSelection,
|
|
10
|
+
$isDecoratorNode as isDecoratorNode,
|
|
11
|
+
$isElementNode as isElementNode,
|
|
12
|
+
$isRangeSelection as isRangeSelection,
|
|
13
|
+
} from 'lexical';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @import { LexicalEditor } from 'lexical';
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Selector for the elements in a decorator node, like an editor component, that handle a click on
|
|
21
|
+
* their own.
|
|
22
|
+
*/
|
|
23
|
+
const DECORATOR_INTERACTIVE_SELECTOR = [
|
|
24
|
+
'input',
|
|
25
|
+
'textarea',
|
|
26
|
+
'select',
|
|
27
|
+
'button',
|
|
28
|
+
'a[href]',
|
|
29
|
+
// A label moves the focus to its control on click
|
|
30
|
+
'label',
|
|
31
|
+
'summary',
|
|
32
|
+
'video[controls]',
|
|
33
|
+
'audio[controls]',
|
|
34
|
+
'[tabindex]',
|
|
35
|
+
'[draggable="true"]',
|
|
36
|
+
'[contenteditable]:not([contenteditable="false"])',
|
|
37
|
+
].join(', ');
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Whether a click on the given element would put the caret on the static content of a decorator
|
|
41
|
+
* node, like the label or padding of an editor component. The editor cannot map such a position to
|
|
42
|
+
* a node, so the caret appears there but typing does nothing.
|
|
43
|
+
* @param {EventTarget | null} target Event target.
|
|
44
|
+
* @returns {boolean} Result.
|
|
45
|
+
*/
|
|
46
|
+
export const isStaticDecoratorContent = (target) => {
|
|
47
|
+
if (!(target instanceof Element)) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const decorator = target.closest('[data-lexical-decorator]');
|
|
52
|
+
|
|
53
|
+
if (!decorator) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const interactive = target.closest(DECORATOR_INTERACTIVE_SELECTOR);
|
|
58
|
+
|
|
59
|
+
// An interactive element outside the decorator is the editor root or one of its ancestors
|
|
60
|
+
return !interactive || !decorator.contains(interactive);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Move a caret placed directly on the root node into a block: the adjacent paragraph or other
|
|
65
|
+
* block, or a new paragraph if the adjacent nodes are decorators, where nothing can be typed.
|
|
66
|
+
* @returns {boolean} Whether the selection has been moved.
|
|
67
|
+
*/
|
|
68
|
+
const $moveCaretIntoBlock = () => {
|
|
69
|
+
const selection = getSelection();
|
|
70
|
+
|
|
71
|
+
if (
|
|
72
|
+
!isRangeSelection(selection) ||
|
|
73
|
+
!selection.isCollapsed() ||
|
|
74
|
+
selection.anchor.type !== 'element'
|
|
75
|
+
) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const root = getRoot();
|
|
80
|
+
|
|
81
|
+
if (!selection.anchor.getNode().is(root)) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const { offset } = selection.anchor;
|
|
86
|
+
const before = root.getChildAtIndex(offset - 1);
|
|
87
|
+
const after = root.getChildAtIndex(offset);
|
|
88
|
+
|
|
89
|
+
if (isElementNode(after)) {
|
|
90
|
+
after.selectStart();
|
|
91
|
+
} else if (isElementNode(before)) {
|
|
92
|
+
before.selectEnd();
|
|
93
|
+
} else {
|
|
94
|
+
const paragraph = createParagraphNode();
|
|
95
|
+
|
|
96
|
+
if (before) {
|
|
97
|
+
before.insertAfter(paragraph);
|
|
98
|
+
} else if (after) {
|
|
99
|
+
after.insertBefore(paragraph);
|
|
100
|
+
} else {
|
|
101
|
+
root.append(paragraph);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
paragraph.select();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return true;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Move the focus to the editor, and run the given function with the node of the given element
|
|
112
|
+
* within an editor update.
|
|
113
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
114
|
+
* @param {Element} element Element of the node.
|
|
115
|
+
* @param {(node: ReturnType<typeof getNearestNodeFromDOMNode>) => void} callback Function to be
|
|
116
|
+
* called with the node.
|
|
117
|
+
*/
|
|
118
|
+
const updateNodeWithFocus = (editor, element, callback) => {
|
|
119
|
+
editor.getRootElement()?.focus({ preventScroll: true });
|
|
120
|
+
|
|
121
|
+
editor.update(() => {
|
|
122
|
+
callback(getNearestNodeFromDOMNode(element));
|
|
123
|
+
});
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Move the focus to the editor, and the caret to right after the given decorator node, like an
|
|
128
|
+
* editor component: the start of the next block, or a new paragraph if there’s none to type in.
|
|
129
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
130
|
+
* @param {Element} decorator Element of the decorator node.
|
|
131
|
+
*/
|
|
132
|
+
const placeCaretAfterDecorator = (editor, decorator) => {
|
|
133
|
+
updateNodeWithFocus(editor, decorator, (node) => {
|
|
134
|
+
// The element has been found by its `data-lexical-decorator` attribute
|
|
135
|
+
/* v8 ignore next 3 */
|
|
136
|
+
if (!isDecoratorNode(node)) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
node.selectNext();
|
|
141
|
+
$moveCaretIntoBlock();
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Move the focus to the editor, and the caret to the end of the given block.
|
|
147
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
148
|
+
* @param {Element} block Element of the block node.
|
|
149
|
+
*/
|
|
150
|
+
const placeCaretAtBlockEnd = (editor, block) => {
|
|
151
|
+
updateNodeWithFocus(editor, block, (node) => {
|
|
152
|
+
/* v8 ignore next 3 */
|
|
153
|
+
if (!isElementNode(node)) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
node.selectEnd();
|
|
158
|
+
});
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Handle a `mousedown` event on the editor, placing the caret where the user expects, rather than
|
|
163
|
+
* leaving it to the browser, when the pointer is not on any editable text. Browsers disagree on
|
|
164
|
+
* where the caret goes in these cases, and some put it within a decorator node, like an editor
|
|
165
|
+
* component, where the editor cannot map it to a node, so typing does nothing:
|
|
166
|
+
*
|
|
167
|
+
* - The static content of a decorator, like its label or padding, places the caret after it.
|
|
168
|
+
* - The empty area of the editor, outside any block, places the caret after the decorator above
|
|
169
|
+
* the pointer, or at the end of the block above the pointer.
|
|
170
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
171
|
+
* @param {MouseEvent} event `mousedown` event.
|
|
172
|
+
* @returns {boolean} Whether the event has been handled.
|
|
173
|
+
*/
|
|
174
|
+
export const handleEditorMouseDown = (editor, event) => {
|
|
175
|
+
const { target, button, shiftKey, clientY } = event;
|
|
176
|
+
const root = editor.getRootElement();
|
|
177
|
+
|
|
178
|
+
// Leave the primary button with Shift, which extends the selection, and other buttons alone.
|
|
179
|
+
// Leave a read-only editor alone as well, as a paragraph may be added, and an event already
|
|
180
|
+
// handled by a nested editor, like one in an editor component
|
|
181
|
+
if (
|
|
182
|
+
!root ||
|
|
183
|
+
!editor.isEditable() ||
|
|
184
|
+
event.defaultPrevented ||
|
|
185
|
+
button !== 0 ||
|
|
186
|
+
shiftKey ||
|
|
187
|
+
!(target instanceof Element)
|
|
188
|
+
) {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (isStaticDecoratorContent(target)) {
|
|
193
|
+
const decorator = /** @type {Element} */ (target.closest('[data-lexical-decorator]'));
|
|
194
|
+
|
|
195
|
+
// A decorator of a nested editor, like one in an editor component, is handled by that editor
|
|
196
|
+
if (decorator.parentElement?.closest('[data-lexical-editor]') !== root) {
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
event.preventDefault();
|
|
201
|
+
placeCaretAfterDecorator(editor, decorator);
|
|
202
|
+
|
|
203
|
+
return true;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (target !== root) {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Find the last block starting above the pointer. Lexical adds a hidden element to the root
|
|
211
|
+
// after a decorator, which has no size
|
|
212
|
+
const block = [...root.children]
|
|
213
|
+
.filter((element) => !element.hasAttribute('data-lexical-decorator-boundary'))
|
|
214
|
+
.findLast((element) => element.getBoundingClientRect().top <= clientY);
|
|
215
|
+
|
|
216
|
+
if (!block) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (block.hasAttribute('data-lexical-decorator')) {
|
|
221
|
+
event.preventDefault();
|
|
222
|
+
placeCaretAfterDecorator(editor, block);
|
|
223
|
+
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Beside a block, the browser can find the nearest text on its own
|
|
228
|
+
if (clientY <= block.getBoundingClientRect().bottom) {
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
event.preventDefault();
|
|
233
|
+
placeCaretAtBlockEnd(editor, block);
|
|
234
|
+
|
|
235
|
+
return true;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Move focus to the editor so the user can start editing immediately.
|
|
240
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
241
|
+
* @returns {Promise<void>} Nothing.
|
|
242
|
+
*/
|
|
243
|
+
export const focusEditor = async (editor) =>
|
|
244
|
+
new Promise((resolve) => {
|
|
245
|
+
editor.focus(() => {
|
|
246
|
+
resolve(undefined);
|
|
247
|
+
});
|
|
248
|
+
});
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export function registerCodeEditorCommands(editor: LexicalEditor, defaultLanguage: string): () => void;
|
|
2
|
+
export function registerListCommands(editor: LexicalEditor, enabledButtons?: TextEditorConfig["enabledButtons"]): () => void;
|
|
3
|
+
export function registerTabIndentation(editor: LexicalEditor): () => void;
|
|
4
|
+
import type { LexicalEditor } from 'lexical';
|
|
5
|
+
import type { TextEditorConfig } from '../../../typedefs.js';
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Commands of the code editor, lists and indentation, and the helper to combine the cleanup
|
|
3
|
+
* handlers of the registered listeners.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
CodeNode,
|
|
8
|
+
$createCodeNode as createCodeNode,
|
|
9
|
+
$isCodeNode as isCodeNode,
|
|
10
|
+
} from '@lexical/code-core';
|
|
11
|
+
import {
|
|
12
|
+
$handleListInsertParagraph as handleListInsertParagraph,
|
|
13
|
+
INSERT_ORDERED_LIST_COMMAND,
|
|
14
|
+
INSERT_UNORDERED_LIST_COMMAND,
|
|
15
|
+
$insertList as insertList,
|
|
16
|
+
$isListItemNode as isListItemNode,
|
|
17
|
+
} from '@lexical/list';
|
|
18
|
+
import { $getNearestNodeOfType as getNearestNodeOfType } from '@lexical/utils';
|
|
19
|
+
import {
|
|
20
|
+
COMMAND_PRIORITY_LOW,
|
|
21
|
+
COMMAND_PRIORITY_NORMAL,
|
|
22
|
+
DELETE_CHARACTER_COMMAND,
|
|
23
|
+
$getSelection as getSelection,
|
|
24
|
+
INDENT_CONTENT_COMMAND,
|
|
25
|
+
INSERT_PARAGRAPH_COMMAND,
|
|
26
|
+
KEY_ENTER_COMMAND,
|
|
27
|
+
$isElementNode as isElementNode,
|
|
28
|
+
$isRangeSelection as isRangeSelection,
|
|
29
|
+
OUTDENT_CONTENT_COMMAND,
|
|
30
|
+
RootNode,
|
|
31
|
+
} from 'lexical';
|
|
32
|
+
import { getNearestElementNode } from './selection.js';
|
|
33
|
+
import { mergeUnregisters } from '../utils.js';
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @import { LexicalEditor } from 'lexical';
|
|
37
|
+
* @import { ListType } from '@lexical/list';
|
|
38
|
+
* @import { TextEditorBlockType, TextEditorConfig } from '../../../typedefs.js';
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Register the commands and transform specific to the code editor, which only holds a single code
|
|
43
|
+
* block.
|
|
44
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
45
|
+
* @param {string} defaultLanguage Default language of the code block.
|
|
46
|
+
* @returns {() => void} Cleanup handler.
|
|
47
|
+
*/
|
|
48
|
+
export const registerCodeEditorCommands = (editor, defaultLanguage) =>
|
|
49
|
+
mergeUnregisters(
|
|
50
|
+
// Pressing Backspace at the very beginning of a code block converts it to a paragraph by
|
|
51
|
+
// default (`CodeNode.collapseAtStart()`), which makes no sense when the editor only holds a
|
|
52
|
+
// single code block, so ignore it
|
|
53
|
+
editor.registerCommand(
|
|
54
|
+
DELETE_CHARACTER_COMMAND,
|
|
55
|
+
(isBackward) => {
|
|
56
|
+
const selection = getSelection();
|
|
57
|
+
|
|
58
|
+
if (!isBackward || !isRangeSelection(selection) || !selection.isCollapsed()) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const { offset } = selection.anchor;
|
|
63
|
+
const node = selection.anchor.getNode();
|
|
64
|
+
const codeNode = isCodeNode(node) ? node : getNearestNodeOfType(node, CodeNode);
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
!!codeNode &&
|
|
68
|
+
offset === 0 &&
|
|
69
|
+
(node.is(codeNode) || !!codeNode.getFirstDescendant()?.is(node))
|
|
70
|
+
);
|
|
71
|
+
},
|
|
72
|
+
COMMAND_PRIORITY_LOW,
|
|
73
|
+
),
|
|
74
|
+
// Pressing Enter after two blank lines at the end of a code block exits it by default
|
|
75
|
+
// (`CodeExtension`), adding a paragraph after it, which would be left out of the code. Insert a
|
|
76
|
+
// new line instead, which `CodeNode.insertNewAfter()` does without exiting when the extension
|
|
77
|
+
// is in place. This has to take priority over the extension’s own handler
|
|
78
|
+
editor.registerCommand(
|
|
79
|
+
KEY_ENTER_COMMAND,
|
|
80
|
+
(event) => {
|
|
81
|
+
const selection = getSelection();
|
|
82
|
+
|
|
83
|
+
if (!isRangeSelection(selection)) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
event?.preventDefault();
|
|
88
|
+
|
|
89
|
+
return editor.dispatchCommand(INSERT_PARAGRAPH_COMMAND, undefined);
|
|
90
|
+
},
|
|
91
|
+
COMMAND_PRIORITY_NORMAL,
|
|
92
|
+
),
|
|
93
|
+
// Make sure the editor always has a single code block. This runs within the same update that
|
|
94
|
+
// made the change, so the user never gets a chance to type in anything else
|
|
95
|
+
editor.registerNodeTransform(RootNode, (root) => {
|
|
96
|
+
const children = root.getChildren();
|
|
97
|
+
|
|
98
|
+
if (children.length === 1 && isCodeNode(children[0])) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const [firstChild] = children;
|
|
103
|
+
|
|
104
|
+
if (children.length === 1 && isElementNode(firstChild)) {
|
|
105
|
+
const node = createCodeNode(defaultLanguage);
|
|
106
|
+
|
|
107
|
+
// Keep the content and the selection
|
|
108
|
+
firstChild.replace(node, true);
|
|
109
|
+
} else if (children.length === 0) {
|
|
110
|
+
const node = createCodeNode(defaultLanguage);
|
|
111
|
+
|
|
112
|
+
root.append(node);
|
|
113
|
+
node.selectStart();
|
|
114
|
+
}
|
|
115
|
+
}),
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* List buttons, along with the command to insert the list and its type.
|
|
120
|
+
* @type {[TextEditorBlockType, typeof INSERT_UNORDERED_LIST_COMMAND, ListType][]}
|
|
121
|
+
*/
|
|
122
|
+
const LIST_COMMANDS = [
|
|
123
|
+
['bulleted-list', INSERT_UNORDERED_LIST_COMMAND, 'bullet'],
|
|
124
|
+
['numbered-list', INSERT_ORDERED_LIST_COMMAND, 'number'],
|
|
125
|
+
];
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Register the commands for the enabled list buttons.
|
|
129
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
130
|
+
* @param {TextEditorConfig['enabledButtons']} enabledButtons Enabled buttons.
|
|
131
|
+
* @returns {() => void} Cleanup handler.
|
|
132
|
+
*/
|
|
133
|
+
export const registerListCommands = (editor, enabledButtons = []) => {
|
|
134
|
+
const enabledLists = LIST_COMMANDS.filter(([button]) => enabledButtons.includes(button));
|
|
135
|
+
|
|
136
|
+
return mergeUnregisters(
|
|
137
|
+
...enabledLists.map(([, command, listType]) =>
|
|
138
|
+
editor.registerCommand(
|
|
139
|
+
command,
|
|
140
|
+
() => {
|
|
141
|
+
insertList(listType);
|
|
142
|
+
|
|
143
|
+
return true;
|
|
144
|
+
},
|
|
145
|
+
COMMAND_PRIORITY_NORMAL,
|
|
146
|
+
),
|
|
147
|
+
),
|
|
148
|
+
// https://github.com/facebook/lexical/blob/main/packages/lexical-react/src/shared/useList.ts
|
|
149
|
+
enabledLists.length
|
|
150
|
+
? editor.registerCommand(
|
|
151
|
+
INSERT_PARAGRAPH_COMMAND,
|
|
152
|
+
() => handleListInsertParagraph(),
|
|
153
|
+
COMMAND_PRIORITY_NORMAL,
|
|
154
|
+
)
|
|
155
|
+
: undefined,
|
|
156
|
+
);
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Register a handler for the Tab key that indents a list item, or unindents it with Shift.
|
|
161
|
+
* `editor.registerCommand(KEY_TAB_COMMAND, listener, priority)` doesn’t work for some reason, so
|
|
162
|
+
* listen to the `keydown` event on the root element instead.
|
|
163
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
164
|
+
* @returns {() => void} Cleanup handler.
|
|
165
|
+
*/
|
|
166
|
+
export const registerTabIndentation = (editor) =>
|
|
167
|
+
editor.registerRootListener((root) => {
|
|
168
|
+
if (!root) {
|
|
169
|
+
return undefined;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Handle Tab indentation shortcuts.
|
|
174
|
+
* @param {KeyboardEvent} event Keydown event.
|
|
175
|
+
*/
|
|
176
|
+
const onKeydown = (event) => {
|
|
177
|
+
editor.update(() => {
|
|
178
|
+
if (event.key === 'Tab') {
|
|
179
|
+
const selection = getSelection();
|
|
180
|
+
|
|
181
|
+
if (!isRangeSelection(selection)) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const parent = getNearestElementNode(selection.anchor.getNode());
|
|
186
|
+
|
|
187
|
+
if (isListItemNode(parent) && parent.canIndent()) {
|
|
188
|
+
if (!event.shiftKey) {
|
|
189
|
+
event.preventDefault();
|
|
190
|
+
editor.dispatchCommand(INDENT_CONTENT_COMMAND, undefined);
|
|
191
|
+
} else if (parent.getIndent() > 0) {
|
|
192
|
+
event.preventDefault();
|
|
193
|
+
editor.dispatchCommand(OUTDENT_CONTENT_COMMAND, undefined);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
root.addEventListener('keydown', onKeydown);
|
|
201
|
+
|
|
202
|
+
return () => {
|
|
203
|
+
root.removeEventListener('keydown', onKeydown);
|
|
204
|
+
};
|
|
205
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function isSafeLinkURL(url: string): boolean;
|
|
2
|
+
export function registerLinkCommands(editor: LexicalEditor): () => void;
|
|
3
|
+
export function $findSelectedLink(): LinkNode | null;
|
|
4
|
+
export function getLinkAnchorRect(editor: LexicalEditor, target: {
|
|
5
|
+
selection: RangeSelection;
|
|
6
|
+
} | {
|
|
7
|
+
linkKey: string | undefined;
|
|
8
|
+
}): EmojiAnchorRect | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Payload of `TOGGLE_LINK_COMMAND` with the URL and the link attributes, like `target`.
|
|
11
|
+
*/
|
|
12
|
+
export type LinkAttributesWithURL = LinkAttributes & {
|
|
13
|
+
url: string;
|
|
14
|
+
};
|
|
15
|
+
import type { LexicalEditor } from 'lexical';
|
|
16
|
+
import { LinkNode } from '@lexical/link';
|
|
17
|
+
import type { RangeSelection } from 'lexical';
|
|
18
|
+
import type { EmojiAnchorRect } from '../../../typedefs.js';
|
|
19
|
+
import type { LinkAttributes } from '@lexical/link';
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Links: the URL check, the link commands, and the helpers of the floating link editor.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
$isLinkNode as isLinkNode,
|
|
7
|
+
LinkNode,
|
|
8
|
+
TOGGLE_LINK_COMMAND,
|
|
9
|
+
$toggleLink as toggleLink,
|
|
10
|
+
} from '@lexical/link';
|
|
11
|
+
import { createDOMRange } from '@lexical/selection';
|
|
12
|
+
import {
|
|
13
|
+
$findMatchingParent as findMatchingParent,
|
|
14
|
+
objectKlassEquals,
|
|
15
|
+
$unwrapNode as unwrapNode,
|
|
16
|
+
} from '@lexical/utils';
|
|
17
|
+
import { isURL } from '@sveltia/utils/string';
|
|
18
|
+
import {
|
|
19
|
+
COMMAND_PRIORITY_LOW,
|
|
20
|
+
COMMAND_PRIORITY_NORMAL,
|
|
21
|
+
$createTextNode as createTextNode,
|
|
22
|
+
$getSelection as getSelection,
|
|
23
|
+
$insertNodes as insertNodes,
|
|
24
|
+
$isElementNode as isElementNode,
|
|
25
|
+
$isRangeSelection as isRangeSelection,
|
|
26
|
+
$isTextNode as isTextNode,
|
|
27
|
+
PASTE_COMMAND,
|
|
28
|
+
} from 'lexical';
|
|
29
|
+
import { mergeUnregisters } from '../utils.js';
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @import { LexicalEditor, RangeSelection } from 'lexical';
|
|
33
|
+
* @import { LinkAttributes } from '@lexical/link';
|
|
34
|
+
* @import { EmojiAnchorRect } from '../../../typedefs.js';
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Payload of `TOGGLE_LINK_COMMAND` with the URL and the link attributes, like `target`.
|
|
39
|
+
* @typedef {LinkAttributes & { url: string }} LinkAttributesWithURL
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* URL schemes allowed in a link, the same as the ones Lexical renders as is.
|
|
44
|
+
*/
|
|
45
|
+
const SAFE_LINK_URL_SCHEMES = ['http', 'https', 'mailto', 'sms', 'tel'];
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Whether the given URL is safe to be used as a link, meaning it’s either relative or uses one of
|
|
49
|
+
* the {@link SAFE_LINK_URL_SCHEMES}. This keeps a URL like `javascript:alert(1)` out of the
|
|
50
|
+
* Markdown, where the site that renders it may not sanitize it.
|
|
51
|
+
* @param {string} url URL.
|
|
52
|
+
* @returns {boolean} Result.
|
|
53
|
+
*/
|
|
54
|
+
export const isSafeLinkURL = (url) => {
|
|
55
|
+
// Browsers ignore control characters and spaces around a URL as well as tabs and newlines within
|
|
56
|
+
// it, e.g. `java\tscript:`, so strip them all before looking for the scheme
|
|
57
|
+
// eslint-disable-next-line no-control-regex
|
|
58
|
+
const normalizedURL = url.replace(/[\u0000-\u0020]/g, '');
|
|
59
|
+
|
|
60
|
+
// Markdown decodes character references and backslash escapes in a link destination, so a scheme
|
|
61
|
+
// can be hidden with them, e.g. `javascript:`, `javascript:` or `javascript\:`. Reject
|
|
62
|
+
// any of them before the path, query or fragment.
|
|
63
|
+
if (/^[^/?#]*[&\\]/.test(normalizedURL)) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const scheme = /^([a-z][a-z\d+.-]*):/i.exec(normalizedURL)?.[1];
|
|
68
|
+
|
|
69
|
+
return !scheme || SAFE_LINK_URL_SCHEMES.includes(scheme.toLowerCase());
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Register the link command, the paste handler that turns a pasted URL into a link, and the
|
|
74
|
+
* transform that removes a link with an unsafe URL.
|
|
75
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
76
|
+
* @returns {() => void} Cleanup handler.
|
|
77
|
+
* @see https://github.com/facebook/lexical/blob/main/packages/lexical-link/src/LexicalLinkExtension.ts
|
|
78
|
+
*/
|
|
79
|
+
export const registerLinkCommands = (editor) =>
|
|
80
|
+
mergeUnregisters(
|
|
81
|
+
// A link can also be created by importing the value or pasting rich text, which bypass the
|
|
82
|
+
// command below. Unwrap a link with an unsafe URL, keeping its text, so the URL never reaches
|
|
83
|
+
// the Markdown or HTML value
|
|
84
|
+
editor.registerNodeTransform(LinkNode, (node) => {
|
|
85
|
+
if (!isSafeLinkURL(node.getURL())) {
|
|
86
|
+
unwrapNode(node);
|
|
87
|
+
}
|
|
88
|
+
}),
|
|
89
|
+
editor.registerCommand(
|
|
90
|
+
TOGGLE_LINK_COMMAND,
|
|
91
|
+
(payload) => {
|
|
92
|
+
// The payload is the URL, or an object with the URL and attributes like `target`
|
|
93
|
+
const url = typeof payload === 'string' ? payload : (payload?.url ?? null);
|
|
94
|
+
|
|
95
|
+
// Ignore an unsafe URL rather than linking to it
|
|
96
|
+
if (url !== null && !isSafeLinkURL(url)) {
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
toggleLink(url === null ? null : /** @type {string | LinkAttributesWithURL} */ (payload));
|
|
101
|
+
|
|
102
|
+
return true;
|
|
103
|
+
},
|
|
104
|
+
COMMAND_PRIORITY_NORMAL,
|
|
105
|
+
),
|
|
106
|
+
editor.registerCommand(
|
|
107
|
+
PASTE_COMMAND,
|
|
108
|
+
(event) => {
|
|
109
|
+
const selection = getSelection();
|
|
110
|
+
|
|
111
|
+
if (
|
|
112
|
+
!isRangeSelection(selection) ||
|
|
113
|
+
!objectKlassEquals(event, ClipboardEvent) ||
|
|
114
|
+
!event.clipboardData ||
|
|
115
|
+
/** @type {HTMLElement} */ (event.target).matches('input, textarea')
|
|
116
|
+
) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const clipboardText = event.clipboardData.getData('text').trim();
|
|
121
|
+
|
|
122
|
+
// Paste an unsafe URL as plain text rather than as a link
|
|
123
|
+
if (!isURL(clipboardText) || !isSafeLinkURL(clipboardText)) {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (selection.isCollapsed()) {
|
|
128
|
+
insertNodes([createTextNode(clipboardText)]);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (
|
|
132
|
+
!selection
|
|
133
|
+
.getNodes()
|
|
134
|
+
.some((node) => isElementNode(node) || (isTextNode(node) && !node.isSimpleText()))
|
|
135
|
+
) {
|
|
136
|
+
editor.dispatchCommand(TOGGLE_LINK_COMMAND, clipboardText);
|
|
137
|
+
event.preventDefault();
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return false;
|
|
142
|
+
},
|
|
143
|
+
COMMAND_PRIORITY_LOW,
|
|
144
|
+
),
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Find the link that contains the whole selection. This must be called within an editor state
|
|
149
|
+
* read.
|
|
150
|
+
* @returns {LinkNode | null} Link, if any.
|
|
151
|
+
*/
|
|
152
|
+
export const $findSelectedLink = () => {
|
|
153
|
+
const selection = getSelection();
|
|
154
|
+
|
|
155
|
+
if (!isRangeSelection(selection)) {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const link = findMatchingParent(selection.anchor.getNode(), isLinkNode);
|
|
160
|
+
|
|
161
|
+
return link && findMatchingParent(selection.focus.getNode(), isLinkNode) === link
|
|
162
|
+
? /** @type {LinkNode} */ (link)
|
|
163
|
+
: null;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Get the viewport-relative bounds of the given selection.
|
|
168
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
169
|
+
* @param {RangeSelection} selection Selection, which may come from an earlier editor state.
|
|
170
|
+
* @returns {DOMRect | undefined} Bounds, or `undefined` if the selected nodes are gone.
|
|
171
|
+
*/
|
|
172
|
+
const getSelectionRect = (editor, selection) => {
|
|
173
|
+
/** @type {DOMRect | undefined} */
|
|
174
|
+
let rect;
|
|
175
|
+
|
|
176
|
+
editor.getEditorState().read(() => {
|
|
177
|
+
const [start, end] = selection.isBackward()
|
|
178
|
+
? [selection.focus, selection.anchor]
|
|
179
|
+
: [selection.anchor, selection.focus];
|
|
180
|
+
|
|
181
|
+
// The selected nodes may be gone, e.g. after an undo
|
|
182
|
+
/* v8 ignore else */
|
|
183
|
+
if (start.getNode().isAttached() && end.getNode().isAttached()) {
|
|
184
|
+
rect = createDOMRange(
|
|
185
|
+
editor,
|
|
186
|
+
start.getNode(),
|
|
187
|
+
start.offset,
|
|
188
|
+
end.getNode(),
|
|
189
|
+
end.offset,
|
|
190
|
+
)?.getBoundingClientRect();
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
return rect;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Get the viewport-relative bounds of what the link editor is anchored to: the selected text to be
|
|
199
|
+
* linked, or a link.
|
|
200
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
201
|
+
* @param {{ selection: RangeSelection } | { linkKey: string | undefined }} target Selected text,
|
|
202
|
+
* or the key of the link.
|
|
203
|
+
* @returns {EmojiAnchorRect | undefined} Bounds, or `undefined` if they can’t be determined.
|
|
204
|
+
*/
|
|
205
|
+
export const getLinkAnchorRect = (editor, target) => {
|
|
206
|
+
const rect =
|
|
207
|
+
'selection' in target
|
|
208
|
+
? getSelectionRect(editor, target.selection)
|
|
209
|
+
: target.linkKey
|
|
210
|
+
? editor.getElementByKey(target.linkKey)?.getBoundingClientRect()
|
|
211
|
+
: undefined;
|
|
212
|
+
|
|
213
|
+
// The link or text may not be laid out, e.g. while the editor is hidden
|
|
214
|
+
/* v8 ignore next */
|
|
215
|
+
if (!rect || !(rect.top || rect.bottom || rect.left || rect.width || rect.height)) {
|
|
216
|
+
return undefined;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const { top, bottom, left, right } = rect;
|
|
220
|
+
|
|
221
|
+
return { top, bottom, left, right };
|
|
222
|
+
};
|