@sveltia/ui 0.90.0 → 0.91.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.
Files changed (75) hide show
  1. package/dist/components/text-editor/constants.d.ts +40 -6
  2. package/dist/components/text-editor/constants.js +81 -7
  3. package/dist/components/text-editor/conversion/html/components.d.ts +16 -0
  4. package/dist/components/text-editor/conversion/html/components.js +87 -0
  5. package/dist/components/text-editor/conversion/html/export.d.ts +10 -0
  6. package/dist/components/text-editor/conversion/html/export.js +360 -0
  7. package/dist/components/text-editor/conversion/html/import-rules.d.ts +12 -0
  8. package/dist/components/text-editor/conversion/html/import-rules.js +234 -0
  9. package/dist/components/text-editor/conversion/html/index.d.ts +10 -0
  10. package/dist/components/text-editor/conversion/html/index.js +33 -0
  11. package/dist/components/text-editor/conversion/html/paragraph.d.ts +1 -0
  12. package/dist/components/text-editor/conversion/html/paragraph.js +123 -0
  13. package/dist/components/text-editor/conversion/html/task-list.d.ts +13 -0
  14. package/dist/components/text-editor/conversion/html/task-list.js +82 -0
  15. package/dist/components/text-editor/conversion/html/unsupported.d.ts +2 -0
  16. package/dist/components/text-editor/conversion/html/unsupported.js +122 -0
  17. package/dist/components/text-editor/conversion/html/utils.d.ts +6 -0
  18. package/dist/components/text-editor/conversion/html/utils.js +34 -0
  19. package/dist/components/text-editor/conversion/import.js +1 -1
  20. package/dist/components/text-editor/conversion/transformers/task-list.d.ts +24 -0
  21. package/dist/components/text-editor/conversion/transformers/task-list.js +53 -0
  22. package/dist/components/text-editor/conversion/update.js +1 -1
  23. package/dist/components/text-editor/core.js +4 -12
  24. package/dist/components/text-editor/editing/commands.js +9 -18
  25. package/dist/components/text-editor/editing/guards.d.ts +7 -0
  26. package/dist/components/text-editor/editing/guards.js +123 -0
  27. package/dist/components/text-editor/editing/raw-markdown.d.ts +4 -2
  28. package/dist/components/text-editor/editing/raw-markdown.js +22 -6
  29. package/dist/components/text-editor/editing/selection.js +6 -2
  30. package/dist/components/text-editor/lexical-root.svelte +40 -0
  31. package/dist/components/text-editor/shiki/version.d.ts +1 -1
  32. package/dist/components/text-editor/shiki/version.js +1 -1
  33. package/dist/components/text-editor/text-editor.svelte +7 -4
  34. package/dist/components/text-editor/text-editor.svelte.d.ts +4 -2
  35. package/dist/components/text-editor/toolbar/format-text-button.svelte +7 -1
  36. package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte +19 -8
  37. package/dist/components/text-editor/toolbar/toggle-block-menu-item.svelte +7 -14
  38. package/dist/locales/ar.yaml +2 -0
  39. package/dist/locales/bg.yaml +2 -0
  40. package/dist/locales/ca.yaml +2 -0
  41. package/dist/locales/cs.yaml +2 -0
  42. package/dist/locales/da.yaml +2 -0
  43. package/dist/locales/de.yaml +2 -0
  44. package/dist/locales/el.yaml +2 -0
  45. package/dist/locales/en-CA.yaml +2 -0
  46. package/dist/locales/en-GB.yaml +2 -0
  47. package/dist/locales/en-US.yaml +2 -0
  48. package/dist/locales/en-ZA.yaml +2 -0
  49. package/dist/locales/es-CO.yaml +2 -0
  50. package/dist/locales/es-ES.yaml +2 -0
  51. package/dist/locales/fa.yaml +2 -0
  52. package/dist/locales/fi.yaml +2 -0
  53. package/dist/locales/fr.yaml +2 -0
  54. package/dist/locales/he.yaml +2 -0
  55. package/dist/locales/hr.yaml +2 -0
  56. package/dist/locales/it.yaml +2 -0
  57. package/dist/locales/ja.yaml +2 -0
  58. package/dist/locales/ko.yaml +2 -0
  59. package/dist/locales/nl.yaml +2 -0
  60. package/dist/locales/pl.yaml +2 -0
  61. package/dist/locales/pt-BR.yaml +2 -0
  62. package/dist/locales/pt-PT.yaml +2 -0
  63. package/dist/locales/ru.yaml +2 -0
  64. package/dist/locales/sk.yaml +2 -0
  65. package/dist/locales/sv.yaml +2 -0
  66. package/dist/locales/tr.yaml +2 -0
  67. package/dist/locales/uk.yaml +2 -0
  68. package/dist/locales/vi.yaml +2 -0
  69. package/dist/locales/zh-CN.yaml +2 -0
  70. package/dist/locales/zh-TW.yaml +2 -0
  71. package/dist/typedefs.d.ts +5 -2
  72. package/dist/typedefs.js +3 -2
  73. package/package.json +5 -5
  74. package/dist/components/text-editor/conversion/html.d.ts +0 -30
  75. package/dist/components/text-editor/conversion/html.js +0 -857
@@ -0,0 +1,360 @@
1
+ /**
2
+ * Export of the editor content to clean HTML, without the attributes and elements only meant for
3
+ * the editor.
4
+ */
5
+
6
+ import { CodeHighlightNode, CodeNode } from '@lexical/code-core';
7
+ import { HorizontalRuleNode } from '@lexical/extension';
8
+ import { $generateHtmlFromNodes as generateHtmlFromNodes } from '@lexical/html';
9
+ import { LinkNode } from '@lexical/link';
10
+ import { ListItemNode, ListNode } from '@lexical/list';
11
+ import { HeadingNode, QuoteNode } from '@lexical/rich-text';
12
+ import { TableCellNode, TableNode } from '@lexical/table';
13
+ import {
14
+ $isParagraphNode as isParagraphNode,
15
+ isHTMLElement,
16
+ ParagraphNode,
17
+ TabNode,
18
+ TextNode,
19
+ } from 'lexical';
20
+ import { isPlainLanguage } from '../../shiki/facade.js';
21
+ import { decorateTaskList, decorateTaskListItem } from './task-list.js';
22
+ import { splitParagraphAroundBlocks } from './paragraph.js';
23
+ import { TEXT_FORMAT_TAGS } from './utils.js';
24
+
25
+ /**
26
+ * @import {
27
+ * DOMExportOutput,
28
+ * DOMExportOutputMap,
29
+ * ElementNode,
30
+ * LexicalEditor,
31
+ * } from 'lexical';
32
+ */
33
+
34
+ /**
35
+ * Attributes to be kept on the exported elements, by tag name. Anything else Lexical adds, like
36
+ * `class`, `dir`, `style`, `value` or `spellcheck`, is only meant for the editor itself, except for
37
+ * the text alignment of a block; see {@link ALIGNABLE_TAGS}.
38
+ * @type {Record<string, string[]>}
39
+ */
40
+ const ALLOWED_ATTRIBUTES = {
41
+ a: ['href', 'rel', 'target', 'title'],
42
+ ol: ['start'],
43
+ td: ['colspan', 'rowspan'],
44
+ th: ['colspan', 'rowspan'],
45
+ };
46
+
47
+ /**
48
+ * Elements that keep their text alignment, which is the only style kept in the exported HTML.
49
+ * Lexical writes it as `style="text-align: …"` for the blocks it can be set on.
50
+ */
51
+ const ALIGNABLE_TAGS = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'li'];
52
+
53
+ /**
54
+ * Remove the attributes not listed in {@link ALLOWED_ATTRIBUTES} from the given element, as well as
55
+ * any style but the text alignment of a block listed in {@link ALIGNABLE_TAGS}.
56
+ * @param {Element} element Element.
57
+ */
58
+ const removeExtraAttributes = (element) => {
59
+ const allowedAttributes = ALLOWED_ATTRIBUTES[element.localName] ?? [];
60
+
61
+ const textAlign = ALIGNABLE_TAGS.includes(element.localName)
62
+ ? /** @type {HTMLElement} */ (element).style.textAlign
63
+ : '';
64
+
65
+ [...element.attributes].forEach(({ name }) => {
66
+ if (!allowedAttributes.includes(name)) {
67
+ element.removeAttribute(name);
68
+ }
69
+ });
70
+
71
+ if (textAlign) {
72
+ /** @type {HTMLElement} */ (element).style.textAlign = textAlign;
73
+ }
74
+ };
75
+
76
+ /**
77
+ * Export an element node with Lexical’s own implementation, then remove the extra attributes from
78
+ * the element, as well as the attribute Lexical adds to the `<br>` terminating a trailing line
79
+ * break. The element’s own `after` function, if any, still runs first, so the structure is kept,
80
+ * like a nested list moved into the previous list item.
81
+ * @param {LexicalEditor} editor Editor instance.
82
+ * @param {ElementNode} node Element node.
83
+ * @returns {DOMExportOutput} Export output.
84
+ */
85
+ const exportElementNode = (editor, node) => {
86
+ const { after, ...output } = node.exportDOM(editor);
87
+
88
+ if (isHTMLElement(output.element)) {
89
+ removeExtraAttributes(output.element);
90
+ }
91
+
92
+ return {
93
+ ...output,
94
+ /**
95
+ * Clean up the element once its children are in place.
96
+ * @param {HTMLElement | DocumentFragment | Text | null | undefined} element Exported element.
97
+ * @returns {HTMLElement | DocumentFragment | Text | null | undefined} New element to replace
98
+ * the exported one, if any.
99
+ */
100
+ after: (element) => {
101
+ const newElement = after ? after.call(node, element) : undefined;
102
+ const target = newElement ?? element;
103
+
104
+ if (isHTMLElement(target)) {
105
+ removeExtraAttributes(target);
106
+ target.querySelectorAll(':scope > br').forEach(removeExtraAttributes);
107
+ }
108
+
109
+ // Returning the same element would make Lexical replace it with itself
110
+ return newElement === element ? undefined : newElement;
111
+ },
112
+ };
113
+ };
114
+
115
+ /**
116
+ * Export a paragraph node. Empty paragraphs at the end are left out, as they have no content; one
117
+ * may be added just by clicking below a block decorator. A paragraph that is the only child of a
118
+ * table cell is exported without the `<p>` element, unless it’s aligned. A paragraph holding
119
+ * elements it can’t hold, like a `<figure>`, is split around them.
120
+ * @param {LexicalEditor} editor Editor instance.
121
+ * @param {ParagraphNode} node Paragraph node.
122
+ * @returns {DOMExportOutput} Export output.
123
+ */
124
+ const exportParagraphNode = (editor, node) => {
125
+ const parent = node.getParent();
126
+
127
+ // Keep the `<p>` of an aligned paragraph, which holds the alignment
128
+ if (parent instanceof TableCellNode && parent.getChildrenSize() === 1 && !node.getFormatType()) {
129
+ return { element: document.createDocumentFragment() };
130
+ }
131
+
132
+ if (
133
+ node.isEmpty() &&
134
+ node.getNextSiblings().every((sibling) => isParagraphNode(sibling) && sibling.isEmpty())
135
+ ) {
136
+ return { element: null };
137
+ }
138
+
139
+ const { after, ...output } = exportElementNode(editor, node);
140
+
141
+ return {
142
+ ...output,
143
+ /**
144
+ * Clean up the paragraph once its children are in place, and split it around the elements it
145
+ * can’t hold, if any.
146
+ * @param {HTMLElement | DocumentFragment | Text | null | undefined} element Exported element.
147
+ * @returns {HTMLElement | DocumentFragment | Text | null | undefined} New element to replace
148
+ * the exported one, if any.
149
+ */
150
+ after: (element) => {
151
+ const newElement = /** @type {NonNullable<DOMExportOutput['after']>} */ (after).call(
152
+ node,
153
+ element,
154
+ );
155
+
156
+ const target = newElement ?? element;
157
+
158
+ return (isHTMLElement(target) && splitParagraphAroundBlocks(target)) || newElement;
159
+ },
160
+ };
161
+ };
162
+
163
+ /**
164
+ * Export a text node as plain text wrapped with the elements for its formats, instead of the
165
+ * `<span>`, `<b>` and `<i>` elements with styles and classes Lexical uses.
166
+ * @param {LexicalEditor} _editor Editor instance.
167
+ * @param {TextNode} node Text node.
168
+ * @returns {DOMExportOutput} Export output.
169
+ */
170
+ const exportTextNode = (_editor, node) => {
171
+ /** @type {HTMLElement | Text} */
172
+ let element = document.createTextNode(node.getTextContent());
173
+
174
+ TEXT_FORMAT_TAGS.forEach(([format, tagName]) => {
175
+ if (node.hasFormat(format)) {
176
+ const wrapper = document.createElement(tagName);
177
+
178
+ wrapper.append(element);
179
+ element = wrapper;
180
+ }
181
+ });
182
+
183
+ return { element };
184
+ };
185
+
186
+ /**
187
+ * Export a code block node as `<pre><code>` with the common `language-*` class, instead of the
188
+ * highlighted tokens Lexical renders in the editor. The children are still exported as usual, so
189
+ * copying part of a code block only exports the selected text, then their text is moved into the
190
+ * `<code>` element.
191
+ * @param {LexicalEditor} _editor Editor instance.
192
+ * @param {CodeNode} node Code node.
193
+ * @returns {DOMExportOutput} Export output.
194
+ */
195
+ const exportCodeNode = (_editor, node) => ({
196
+ element: document.createElement('pre'),
197
+ /**
198
+ * Replace the exported children with a `<code>` element holding their text.
199
+ * @param {HTMLElement | DocumentFragment | Text | null | undefined} pre Exported element.
200
+ * @returns {undefined} Nothing, as the element is updated in place.
201
+ */
202
+ after: (pre) => {
203
+ /* v8 ignore next 3 */
204
+ if (!isHTMLElement(pre)) {
205
+ return undefined;
206
+ }
207
+
208
+ const code = document.createElement('code');
209
+ const language = node.getLanguage();
210
+
211
+ if (language && !isPlainLanguage(language)) {
212
+ code.className = `language-${language}`;
213
+ }
214
+
215
+ code.textContent = [...pre.childNodes]
216
+ .map((child) => {
217
+ if (child.nodeName !== 'BR') {
218
+ return child.textContent;
219
+ }
220
+
221
+ // Skip the `<br>` Lexical adds to terminate a trailing line break
222
+ return isHTMLElement(child) && child.hasAttribute('data-lexical-managed-linebreak')
223
+ ? ''
224
+ : '\n';
225
+ })
226
+ .join('');
227
+
228
+ pre.replaceChildren(code);
229
+
230
+ return undefined;
231
+ },
232
+ });
233
+
234
+ /**
235
+ * Selector for the structural elements of a table, excluding the cell content.
236
+ */
237
+ const TABLE_STRUCTURE_SELECTOR = [
238
+ ':scope > :is(thead, tbody, tfoot)',
239
+ ':scope > :is(thead, tbody, tfoot) > tr',
240
+ ':scope > :is(thead, tbody, tfoot) > tr > :is(th, td)',
241
+ ':scope > tr',
242
+ ':scope > tr > :is(th, td)',
243
+ ].join(', ');
244
+
245
+ /**
246
+ * Export a table node, then remove the column definitions and extra attributes Lexical adds to the
247
+ * table and its descendants. The cleanup has to wait until the table is complete, as Lexical uses
248
+ * temporary attributes on the cells to compute the columns.
249
+ * @param {LexicalEditor} editor Editor instance.
250
+ * @param {TableNode} node Table node.
251
+ * @returns {DOMExportOutput} Export output.
252
+ */
253
+ const exportTableNode = (editor, node) => {
254
+ const { after, ...output } = node.exportDOM(editor);
255
+
256
+ return {
257
+ ...output,
258
+ /**
259
+ * Clean up the table once Lexical has completed it.
260
+ * @param {HTMLElement | DocumentFragment | Text | null | undefined} element Exported element.
261
+ * @returns {HTMLElement | DocumentFragment | Text | null | undefined} New element to replace
262
+ * the exported one, if any.
263
+ */
264
+ after: (element) => {
265
+ const newElement = after ? after.call(node, element) : undefined;
266
+ const target = newElement ?? element;
267
+
268
+ if (isHTMLElement(target)) {
269
+ target.querySelectorAll(':scope > colgroup').forEach((colgroup) => colgroup.remove());
270
+ // Leave the cell content alone, which has been cleaned up by its own nodes, while the
271
+ // content of an editor component may need its attributes
272
+ [target, ...target.querySelectorAll(TABLE_STRUCTURE_SELECTOR)].forEach(
273
+ removeExtraAttributes,
274
+ );
275
+ }
276
+
277
+ // Returning the same element would make Lexical replace it with itself
278
+ return newElement === element ? undefined : newElement;
279
+ },
280
+ };
281
+ };
282
+
283
+ /**
284
+ * Export an element node with {@link exportElementNode}, then update the cleaned-up element with
285
+ * the given function, like adding the classes and check boxes of a task list.
286
+ * @param {LexicalEditor} editor Editor instance.
287
+ * @param {ElementNode} node Element node.
288
+ * @param {(element: HTMLElement) => void} decorate Function to update the exported element.
289
+ * @returns {DOMExportOutput} Export output.
290
+ */
291
+ const exportDecoratedElementNode = (editor, node, decorate) => {
292
+ const output = exportElementNode(editor, node);
293
+
294
+ return {
295
+ ...output,
296
+ /**
297
+ * Update the element once it’s been cleaned up.
298
+ * @param {HTMLElement | DocumentFragment | Text | null | undefined} element Exported element.
299
+ * @returns {HTMLElement | DocumentFragment | Text | null | undefined} New element, if any.
300
+ */
301
+ after: (element) => {
302
+ const newElement = output.after?.(element);
303
+ const target = newElement ?? element;
304
+
305
+ if (isHTMLElement(target)) {
306
+ decorate(target);
307
+ }
308
+
309
+ return newElement;
310
+ },
311
+ };
312
+ };
313
+
314
+ /**
315
+ * Export a list node, adding the class of a task list to a check list.
316
+ * @param {LexicalEditor} editor Editor instance.
317
+ * @param {ListNode} node List node.
318
+ * @returns {DOMExportOutput} Export output.
319
+ */
320
+ const exportListNode = (editor, node) =>
321
+ exportDecoratedElementNode(editor, node, (element) => decorateTaskList(node, element));
322
+
323
+ /**
324
+ * Export a list item node, adding the class and a disabled check box to an item of a check list.
325
+ * @param {LexicalEditor} editor Editor instance.
326
+ * @param {ListItemNode} node List item node.
327
+ * @returns {DOMExportOutput} Export output.
328
+ */
329
+ const exportListItemNode = (editor, node) =>
330
+ exportDecoratedElementNode(editor, node, (element) => decorateTaskListItem(node, element));
331
+
332
+ /**
333
+ * Map of Lexical node classes and functions to export them to clean HTML. A class only takes effect
334
+ * if the node is registered with the editor. Other nodes, including the ones of editor components,
335
+ * are exported with their own `exportDOM` method.
336
+ * @type {DOMExportOutputMap}
337
+ */
338
+ export const HTML_EXPORT_MAP = new Map(
339
+ /** @type {[any, (editor: LexicalEditor, node: any) => DOMExportOutput][]} */ ([
340
+ [TextNode, exportTextNode],
341
+ [TabNode, exportTextNode],
342
+ [CodeHighlightNode, exportTextNode],
343
+ [ParagraphNode, exportParagraphNode],
344
+ [HeadingNode, exportElementNode],
345
+ [QuoteNode, exportElementNode],
346
+ [ListNode, exportListNode],
347
+ [ListItemNode, exportListItemNode],
348
+ [LinkNode, exportElementNode],
349
+ [HorizontalRuleNode, exportElementNode],
350
+ [CodeNode, exportCodeNode],
351
+ [TableNode, exportTableNode],
352
+ ]),
353
+ );
354
+
355
+ /**
356
+ * Convert the editor content to HTML. Call this within an editor update or read.
357
+ * @param {LexicalEditor} editor Editor instance.
358
+ * @returns {string} HTML value.
359
+ */
360
+ export const exportHtml = (editor) => generateHtmlFromNodes(editor);
@@ -0,0 +1,12 @@
1
+ export function getCodeLanguage(code: Element): string | undefined;
2
+ /**
3
+ * Additional DOM conversions to import HTML. They take priority over Lexical’s own conversions.
4
+ * @type {DOMConversionMap}
5
+ */
6
+ export const HTML_IMPORT_MAP: DOMConversionMap;
7
+ /**
8
+ * Additional DOM conversions to import HTML with code blocks.
9
+ * @type {DOMConversionMap}
10
+ */
11
+ export const HTML_CODE_IMPORT_MAP: DOMConversionMap;
12
+ import type { DOMConversionMap } from 'lexical';
@@ -0,0 +1,234 @@
1
+ /**
2
+ * Rules to import HTML: DOM conversions that take priority over Lexical’s own.
3
+ */
4
+
5
+ import { $createCodeNode as createCodeNode } from '@lexical/code-core';
6
+ import {
7
+ $createTextNode as createTextNode,
8
+ isDOMTextNode,
9
+ isHTMLElement,
10
+ isInlineDomNode,
11
+ } from 'lexical';
12
+ import { isInlineComponentElement } from './components.js';
13
+ import { isPreformatted } from './utils.js';
14
+
15
+ /**
16
+ * @import {
17
+ * DOMConversion,
18
+ * DOMConversionMap,
19
+ * DOMConversionOutput,
20
+ * LexicalNode,
21
+ * } from 'lexical';
22
+ */
23
+
24
+ /**
25
+ * Get the programming language from the `language-*` or `lang-*` class of the given `<code>`
26
+ * element, which is common in HTML generated from Markdown.
27
+ * @param {Element} code `<code>` element.
28
+ * @returns {string | undefined} Language.
29
+ */
30
+ export const getCodeLanguage = (code) =>
31
+ code.className.match(/(?:^|\s)(?:lang|language)-(\S+)/)?.[1];
32
+
33
+ /**
34
+ * Get the text content of the given element, with `<br>` elements as line breaks.
35
+ * @param {Element} element Element.
36
+ * @returns {string} Text.
37
+ */
38
+ const getTextWithLineBreaks = (element) => {
39
+ const walker = document.createTreeWalker(element, NodeFilter.SHOW_ALL);
40
+ let text = '';
41
+
42
+ for (let node = walker.nextNode(); node; node = walker.nextNode()) {
43
+ if (node.nodeType === Node.TEXT_NODE) {
44
+ text += /** @type {Text} */ (node).data;
45
+ } else if (node.nodeName === 'BR') {
46
+ text += '\n';
47
+ }
48
+ }
49
+
50
+ return text;
51
+ };
52
+
53
+ /**
54
+ * Convert a `<pre>` element to a code block node, taking the language from the `language-*` or
55
+ * `lang-*` class of the `<code>` element within it, which is common in HTML generated from
56
+ * Markdown, as well as the `data-language` attribute Lexical uses. The content is imported as plain
57
+ * text, so any markup within it, like highlighted tokens, is dropped, while a `<br>` element
58
+ * becomes a line break.
59
+ * @param {HTMLElement} pre `<pre>` element.
60
+ * @returns {DOMConversionOutput} Conversion output.
61
+ */
62
+ const convertPreElement = (pre) => {
63
+ const code = pre.querySelector(':scope > code');
64
+ const language = (code ? getCodeLanguage(code) : undefined) ?? pre.getAttribute('data-language');
65
+ const node = createCodeNode(language);
66
+ // A code block generated from Markdown ends with a line break
67
+ const text = getTextWithLineBreaks(pre).replace(/\n$/, '');
68
+
69
+ if (text) {
70
+ node.append(createTextNode(text));
71
+ }
72
+
73
+ return {
74
+ node,
75
+ /**
76
+ * Drop the child nodes, which are already imported as the text content.
77
+ * @returns {LexicalNode[]} Nodes.
78
+ */
79
+ after: () => [],
80
+ };
81
+ };
82
+
83
+ /**
84
+ * Find the text or the inline editor component element next to the given text in the same line,
85
+ * like Lexical’s own `findTextInLine()` does for text only. Without the component, Lexical takes an
86
+ * element like an image for the end of the line, or skips it, and drops the space between it and
87
+ * the text, e.g. `a <img> b` becomes `a<img>b`.
88
+ * @param {Node} text Text node.
89
+ * @param {boolean} forward Whether to search forward.
90
+ * @returns {Text | Element | null} Text or element, or `null` if the line ends.
91
+ */
92
+ const findContentInLine = (text, forward) => {
93
+ let node = text;
94
+
95
+ for (;;) {
96
+ let sibling = forward ? node.nextSibling : node.previousSibling;
97
+
98
+ while (!sibling) {
99
+ const { parentElement } = node;
100
+
101
+ if (!parentElement) {
102
+ return null;
103
+ }
104
+
105
+ node = parentElement;
106
+ sibling = forward ? node.nextSibling : node.previousSibling;
107
+ }
108
+
109
+ node = sibling;
110
+
111
+ if (isHTMLElement(node)) {
112
+ if (isInlineComponentElement(node)) {
113
+ return node;
114
+ }
115
+
116
+ const { display } = node.style;
117
+
118
+ if (display ? !display.startsWith('inline') : !isInlineDomNode(node)) {
119
+ return null;
120
+ }
121
+ }
122
+
123
+ let descendant = forward ? node.firstChild : node.lastChild;
124
+
125
+ while (descendant) {
126
+ node = descendant;
127
+
128
+ if (isHTMLElement(node) && isInlineComponentElement(node)) {
129
+ return node;
130
+ }
131
+
132
+ descendant = forward ? node.firstChild : node.lastChild;
133
+ }
134
+
135
+ if (isDOMTextNode(node)) {
136
+ return node;
137
+ }
138
+
139
+ if (node.nodeName === 'BR') {
140
+ return null;
141
+ }
142
+ }
143
+ };
144
+
145
+ /**
146
+ * Convert a text node, collapsing whitespace like Lexical’s own conversion does, except that an
147
+ * inline editor component element counts as content. See {@link findContentInLine}.
148
+ * @param {Text} domNode Text node.
149
+ * @returns {DOMConversionOutput} Conversion output.
150
+ */
151
+ const convertTextNode = (domNode) => {
152
+ let textContent = domNode.data.replace(/\r/g, '').replace(/[ \t\n]+/g, ' ');
153
+
154
+ if (textContent.startsWith(' ')) {
155
+ let previous = findContentInLine(domNode, false);
156
+ // Drop the space at the start of a line, or after another space
157
+ let drop = true;
158
+
159
+ for (; previous; previous = findContentInLine(previous, false)) {
160
+ if (!isDOMTextNode(previous)) {
161
+ drop = false;
162
+ break;
163
+ }
164
+
165
+ const previousText = previous.data;
166
+
167
+ // Skip empty text
168
+ if (previousText) {
169
+ drop = /[ \t\n]$/.test(previousText);
170
+ break;
171
+ }
172
+ }
173
+
174
+ if (drop) {
175
+ textContent = textContent.slice(1);
176
+ }
177
+ }
178
+
179
+ if (textContent.endsWith(' ')) {
180
+ let next = findContentInLine(domNode, true);
181
+ let isEndOfLine = true;
182
+
183
+ // Skip any text that only has whitespace, which is dropped as well
184
+ for (; next; next = findContentInLine(next, true)) {
185
+ if (!isDOMTextNode(next) || next.data.replace(/^[ \t\n]+/, '')) {
186
+ isEndOfLine = false;
187
+ break;
188
+ }
189
+ }
190
+
191
+ if (isEndOfLine) {
192
+ textContent = textContent.slice(0, -1);
193
+ }
194
+ }
195
+
196
+ return { node: textContent ? createTextNode(textContent) : null };
197
+ };
198
+
199
+ /**
200
+ * Additional DOM conversions to import HTML. They take priority over Lexical’s own conversions.
201
+ * @type {DOMConversionMap}
202
+ */
203
+ export const HTML_IMPORT_MAP = {
204
+ /**
205
+ * Get the conversion for a text node, unless it’s preformatted, which Lexical’s own conversion
206
+ * handles.
207
+ * @param {Node} node Text node.
208
+ * @returns {DOMConversion | null} Conversion.
209
+ */
210
+ '#text': (node) =>
211
+ isPreformatted(node)
212
+ ? null
213
+ : {
214
+ /**
215
+ * Convert the text node.
216
+ * @param {Node} domNode Text node.
217
+ * @returns {DOMConversionOutput} Conversion output.
218
+ */
219
+ conversion: (domNode) => convertTextNode(/** @type {Text} */ (domNode)),
220
+ priority: 1,
221
+ },
222
+ };
223
+
224
+ /**
225
+ * Additional DOM conversions to import HTML with code blocks.
226
+ * @type {DOMConversionMap}
227
+ */
228
+ export const HTML_CODE_IMPORT_MAP = {
229
+ /**
230
+ * Get the conversion for a `<pre>` element.
231
+ * @returns {DOMConversion} Conversion.
232
+ */
233
+ pre: () => ({ conversion: convertPreElement, priority: 2 }),
234
+ };
@@ -0,0 +1,10 @@
1
+ export { editorComponents } from "./components.js";
2
+ export { findUnsupportedNode } from "./unsupported.js";
3
+ export function getHtmlConfig({ hasCodeBlock, hasTaskList }: {
4
+ hasCodeBlock: boolean;
5
+ hasTaskList: boolean;
6
+ }): HTMLConfig;
7
+ import { exportHtml } from './export.js';
8
+ import { getCodeLanguage } from './import-rules.js';
9
+ import type { HTMLConfig } from 'lexical';
10
+ export { exportHtml, getCodeLanguage };
@@ -0,0 +1,33 @@
1
+ /**
2
+ * HTML import and export of the rich text editor.
3
+ */
4
+
5
+ import { exportHtml, HTML_EXPORT_MAP } from './export.js';
6
+ import { getCodeLanguage, HTML_CODE_IMPORT_MAP, HTML_IMPORT_MAP } from './import-rules.js';
7
+ import { HTML_TASK_LIST_IMPORT_MAP } from './task-list.js';
8
+
9
+ /**
10
+ * @import { HTMLConfig } from 'lexical';
11
+ */
12
+
13
+ export { editorComponents } from './components.js';
14
+ export { findUnsupportedNode } from './unsupported.js';
15
+ export { exportHtml, getCodeLanguage };
16
+
17
+ /**
18
+ * Get the HTML configuration of an editor for the HTML format: the export map, which also applies
19
+ * to copying, to export clean HTML without the attributes and elements only meant for the editor,
20
+ * and the import rules for the enabled features, like code blocks with their language.
21
+ * @param {object} features Enabled features.
22
+ * @param {boolean} features.hasCodeBlock Whether code blocks are supported.
23
+ * @param {boolean} features.hasTaskList Whether task lists are supported.
24
+ * @returns {HTMLConfig} Configuration.
25
+ */
26
+ export const getHtmlConfig = ({ hasCodeBlock, hasTaskList }) => ({
27
+ export: HTML_EXPORT_MAP,
28
+ import: {
29
+ ...HTML_IMPORT_MAP,
30
+ ...(hasCodeBlock ? HTML_CODE_IMPORT_MAP : {}),
31
+ ...(hasTaskList ? HTML_TASK_LIST_IMPORT_MAP : {}),
32
+ },
33
+ });
@@ -0,0 +1 @@
1
+ export function splitParagraphAroundBlocks(paragraph: HTMLElement): DocumentFragment | undefined;