@sveltia/ui 0.90.0 → 0.92.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/bottom-navigation/bottom-navigation.svelte +3 -9
- package/dist/components/bottom-navigation/bottom-navigation.svelte.d.ts +4 -39
- package/dist/components/button/button-group.svelte +2 -4
- package/dist/components/button/button-group.svelte.d.ts +3 -19
- package/dist/components/button/select-button-group.svelte +2 -13
- package/dist/components/button/select-button-group.svelte.d.ts +5 -67
- package/dist/components/calendar/calendar.d.ts +9 -0
- package/dist/components/calendar/calendar.js +43 -0
- package/dist/components/calendar/calendar.svelte +26 -27
- package/dist/components/checkbox/checkbox-group.svelte +2 -8
- package/dist/components/checkbox/checkbox-group.svelte.d.ts +4 -39
- package/dist/components/dialog/alert-dialog.svelte.d.ts +2 -3
- package/dist/components/dialog/confirmation-dialog.svelte.d.ts +2 -3
- package/dist/components/dialog/dialog.svelte +6 -7
- package/dist/components/dialog/dialog.svelte.d.ts +2 -3
- package/dist/components/dialog/prompt-dialog.svelte +2 -2
- package/dist/components/dialog/prompt-dialog.svelte.d.ts +2 -3
- package/dist/components/disclosure/disclosure.svelte +14 -16
- package/dist/components/disclosure/disclosure.svelte.d.ts +4 -38
- package/dist/components/drawer/drawer.svelte +22 -39
- package/dist/components/drawer/drawer.svelte.d.ts +3 -24
- package/dist/components/grid/grid-body.svelte +2 -3
- package/dist/components/grid/grid-body.svelte.d.ts +3 -18
- package/dist/components/grid/grid-cell.svelte +2 -8
- package/dist/components/grid/grid-cell.svelte.d.ts +3 -22
- package/dist/components/grid/grid-col-header.svelte +2 -4
- package/dist/components/grid/grid-col-header.svelte.d.ts +3 -19
- package/dist/components/grid/grid-foot.svelte +4 -9
- package/dist/components/grid/grid-foot.svelte.d.ts +3 -22
- package/dist/components/grid/grid-head.svelte +4 -9
- package/dist/components/grid/grid-head.svelte.d.ts +3 -22
- package/dist/components/grid/grid-row-header.svelte +2 -8
- package/dist/components/grid/grid-row-header.svelte.d.ts +3 -22
- package/dist/components/grid/grid-row.svelte +4 -5
- package/dist/components/grid/grid-row.svelte.d.ts +3 -19
- package/dist/components/grid/grid.d.ts +10 -0
- package/dist/components/grid/grid.js +11 -0
- package/dist/components/grid/grid.svelte +6 -7
- package/dist/components/grid/grid.svelte.d.ts +5 -21
- package/dist/components/listbox/listbox.svelte +34 -15
- package/dist/components/listbox/listbox.svelte.d.ts +5 -69
- package/dist/components/listbox/option-group.svelte +2 -8
- package/dist/components/listbox/option-group.svelte.d.ts +4 -39
- package/dist/components/listbox/option.svelte +2 -1
- package/dist/components/menu/menu-item-group.svelte +2 -6
- package/dist/components/menu/menu-item-group.svelte.d.ts +3 -22
- package/dist/components/menu/menu-item.svelte +5 -1
- package/dist/components/menu/menu.svelte +2 -8
- package/dist/components/menu/menu.svelte.d.ts +4 -39
- package/dist/components/radio/radio-group.svelte +2 -17
- package/dist/components/radio/radio-group.svelte.d.ts +5 -67
- package/dist/components/radio/radio.svelte +19 -22
- package/dist/components/radio/radio.svelte.d.ts +10 -40
- package/dist/components/resizable-pane/resizable-handle.svelte +42 -49
- package/dist/components/resizable-pane/resizable-pane-group.svelte +44 -58
- package/dist/components/resizable-pane/resizable-pane-group.svelte.d.ts +3 -19
- package/dist/components/resizable-pane/resizable-pane.svelte +2 -5
- package/dist/components/resizable-pane/resizable-pane.svelte.d.ts +3 -19
- package/dist/components/resizable-pane/sizing.d.ts +24 -0
- package/dist/components/resizable-pane/sizing.js +83 -0
- package/dist/components/select/combobox.svelte +35 -39
- package/dist/components/select/select-tags.svelte +3 -13
- package/dist/components/select/select-tags.svelte.d.ts +5 -67
- package/dist/components/slider/slider.d.ts +5 -4
- package/dist/components/slider/slider.js +28 -23
- package/dist/components/slider/slider.svelte +64 -62
- package/dist/components/slider/slider.svelte.d.ts +4 -37
- package/dist/components/switch/switch.svelte +2 -13
- package/dist/components/switch/switch.svelte.d.ts +5 -67
- package/dist/components/table/table-body.svelte +2 -4
- package/dist/components/table/table-body.svelte.d.ts +3 -19
- package/dist/components/table/table-cell.svelte +2 -8
- package/dist/components/table/table-cell.svelte.d.ts +3 -22
- package/dist/components/table/table-col-header.svelte +2 -8
- package/dist/components/table/table-col-header.svelte.d.ts +3 -22
- package/dist/components/table/table-foot.svelte +2 -8
- package/dist/components/table/table-foot.svelte.d.ts +3 -22
- package/dist/components/table/table-head.svelte +2 -8
- package/dist/components/table/table-head.svelte.d.ts +3 -22
- package/dist/components/table/table-part.svelte +2 -4
- package/dist/components/table/table-part.svelte.d.ts +3 -19
- package/dist/components/table/table-row-header.svelte +2 -8
- package/dist/components/table/table-row-header.svelte.d.ts +3 -22
- package/dist/components/table/table-row.svelte +2 -8
- package/dist/components/table/table-row.svelte.d.ts +3 -22
- package/dist/components/table/table.svelte +2 -4
- package/dist/components/table/table.svelte.d.ts +3 -19
- package/dist/components/tabs/tab-box.svelte +2 -4
- package/dist/components/tabs/tab-box.svelte.d.ts +3 -19
- package/dist/components/tabs/tab-list.svelte +2 -7
- package/dist/components/tabs/tab-list.svelte.d.ts +4 -37
- package/dist/components/tabs/tab-panel.svelte +2 -8
- package/dist/components/tabs/tab-panel.svelte.d.ts +3 -22
- package/dist/components/tabs/tab-panels.svelte +2 -8
- package/dist/components/tabs/tab-panels.svelte.d.ts +3 -22
- package/dist/components/text-editor/code-editor.js +14 -7
- package/dist/components/text-editor/code-editor.svelte +14 -22
- package/dist/components/text-editor/code-editor.svelte.d.ts +5 -103
- package/dist/components/text-editor/config.d.ts +73 -0
- package/dist/components/text-editor/config.js +110 -0
- package/dist/components/text-editor/constants.d.ts +40 -6
- package/dist/components/text-editor/constants.js +81 -7
- package/dist/components/text-editor/conversion/editor-components.js +4 -4
- package/dist/components/text-editor/conversion/html/components.d.ts +16 -0
- package/dist/components/text-editor/conversion/html/components.js +87 -0
- package/dist/components/text-editor/conversion/html/export.d.ts +10 -0
- package/dist/components/text-editor/conversion/html/export.js +360 -0
- package/dist/components/text-editor/conversion/html/import-rules.d.ts +12 -0
- package/dist/components/text-editor/conversion/html/import-rules.js +234 -0
- package/dist/components/text-editor/conversion/html/index.d.ts +10 -0
- package/dist/components/text-editor/conversion/html/index.js +33 -0
- package/dist/components/text-editor/conversion/html/paragraph.d.ts +1 -0
- package/dist/components/text-editor/conversion/html/paragraph.js +123 -0
- package/dist/components/text-editor/conversion/html/task-list.d.ts +13 -0
- package/dist/components/text-editor/conversion/html/task-list.js +82 -0
- package/dist/components/text-editor/conversion/html/unsupported.d.ts +2 -0
- package/dist/components/text-editor/conversion/html/unsupported.js +122 -0
- package/dist/components/text-editor/conversion/html/utils.d.ts +6 -0
- package/dist/components/text-editor/conversion/html/utils.js +34 -0
- package/dist/components/text-editor/conversion/import.js +33 -27
- package/dist/components/text-editor/conversion/markdown/block-conversion.d.ts +4 -0
- package/dist/components/text-editor/conversion/markdown/block-conversion.js +154 -0
- package/dist/components/text-editor/conversion/markdown/block-structure.d.ts +16 -0
- package/dist/components/text-editor/conversion/markdown/block-structure.js +254 -0
- package/dist/components/text-editor/conversion/markdown/code-blocks.d.ts +57 -0
- package/dist/components/text-editor/conversion/markdown/code-blocks.js +279 -0
- package/dist/components/text-editor/conversion/markdown/code-spans.d.ts +19 -0
- package/dist/components/text-editor/conversion/markdown/code-spans.js +71 -0
- package/dist/components/text-editor/conversion/markdown/formatting.d.ts +22 -0
- package/dist/components/text-editor/conversion/markdown/formatting.js +451 -0
- package/dist/components/text-editor/conversion/markdown/index.d.ts +10 -0
- package/dist/components/text-editor/conversion/markdown/index.js +55 -0
- package/dist/components/text-editor/conversion/markdown/placeholders.d.ts +2 -0
- package/dist/components/text-editor/conversion/markdown/placeholders.js +229 -0
- package/dist/components/text-editor/conversion/markdown/syntax.d.ts +99 -0
- package/dist/components/text-editor/conversion/markdown/syntax.js +206 -0
- package/dist/components/text-editor/conversion/placeholder-chars.d.ts +43 -0
- package/dist/components/text-editor/conversion/placeholder-chars.js +51 -0
- package/dist/components/text-editor/conversion/transformers/block-escape.d.ts +1 -0
- package/dist/components/text-editor/conversion/transformers/block-escape.js +53 -26
- package/dist/components/text-editor/conversion/transformers/code.d.ts +3 -14
- package/dist/components/text-editor/conversion/transformers/code.js +78 -16
- package/dist/components/text-editor/conversion/transformers/formatted-code.js +3 -18
- package/dist/components/text-editor/conversion/transformers/inline-code.js +3 -1
- package/dist/components/text-editor/conversion/transformers/link.js +3 -1
- package/dist/components/text-editor/conversion/transformers/raw-syntax.d.ts +0 -19
- package/dist/components/text-editor/conversion/transformers/raw-syntax.js +22 -93
- package/dist/components/text-editor/conversion/transformers/table.js +57 -16
- package/dist/components/text-editor/conversion/transformers/task-list.d.ts +24 -0
- package/dist/components/text-editor/conversion/transformers/task-list.js +53 -0
- package/dist/components/text-editor/conversion/update.d.ts +19 -2
- package/dist/components/text-editor/conversion/update.js +64 -40
- package/dist/components/text-editor/core.d.ts +12 -51
- package/dist/components/text-editor/core.js +47 -77
- package/dist/components/text-editor/editing/commands.js +9 -18
- package/dist/components/text-editor/editing/guards.d.ts +7 -0
- package/dist/components/text-editor/editing/guards.js +123 -0
- package/dist/components/text-editor/editing/raw-markdown.d.ts +7 -2
- package/dist/components/text-editor/editing/raw-markdown.js +68 -8
- package/dist/components/text-editor/editing/selection.js +6 -2
- package/dist/components/text-editor/lexical-root.svelte +89 -47
- package/dist/components/text-editor/lexical-root.svelte.d.ts +4 -67
- package/dist/components/text-editor/shiki/{facade.d.ts → engine.d.ts} +8 -8
- package/dist/components/text-editor/shiki/{facade.js → engine.js} +53 -156
- package/dist/components/text-editor/shiki/highlighter.js +5 -5
- package/dist/components/text-editor/shiki/lexical.d.ts +6 -0
- package/dist/components/text-editor/shiki/lexical.js +165 -0
- package/dist/components/text-editor/shiki/preload.js +1 -1
- package/dist/components/text-editor/shiki/source-highlight.svelte.d.ts +15 -0
- package/dist/components/text-editor/shiki/source-highlight.svelte.js +120 -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 +65 -16
- package/dist/components/text-editor/text-editor.svelte +57 -146
- package/dist/components/text-editor/text-editor.svelte.d.ts +9 -105
- package/dist/components/text-editor/toolbar/actions.d.ts +25 -0
- package/dist/components/text-editor/toolbar/actions.js +107 -0
- package/dist/components/text-editor/toolbar/align-menu-button.svelte +16 -14
- package/dist/components/text-editor/toolbar/format-text-button.svelte +27 -19
- package/dist/components/text-editor/toolbar/insert-horizontal-rule-button.svelte +23 -23
- package/dist/components/text-editor/toolbar/insert-item-button.svelte +1 -1
- package/dist/components/text-editor/toolbar/insert-link-button.svelte +47 -51
- package/dist/components/text-editor/toolbar/insert-menu-button.svelte +1 -1
- package/dist/components/text-editor/toolbar/insert-table-button.svelte +35 -52
- package/dist/components/text-editor/toolbar/table-action-menu.svelte +19 -17
- package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte +34 -11
- package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte.d.ts +0 -8
- package/dist/components/text-editor/toolbar/toggle-block-menu-item.svelte +35 -41
- package/dist/components/text-editor/widgets/emoji-autocomplete.svelte +7 -6
- package/dist/components/text-editor/widgets/floating-link-editor.svelte +19 -32
- package/dist/components/text-editor/widgets/table-cell-menu-button.svelte +28 -32
- package/dist/components/text-field/date-time-input.svelte +7 -8
- package/dist/components/text-field/date-time-input.svelte.d.ts +3 -32
- package/dist/components/text-field/number-input.d.ts +0 -2
- package/dist/components/text-field/number-input.js +25 -25
- package/dist/components/text-field/number-input.svelte +18 -9
- package/dist/components/text-field/number-input.svelte.d.ts +1 -1
- package/dist/components/text-field/text-area.d.ts +2 -0
- package/dist/components/text-field/text-area.js +31 -0
- package/dist/components/text-field/text-area.svelte +12 -25
- package/dist/components/text-field/text-area.svelte.d.ts +5 -67
- package/dist/components/toast/toast.svelte +31 -8
- package/dist/components/toast/toast.svelte.d.ts +6 -6
- package/dist/components/toolbar/toolbar.svelte +2 -7
- package/dist/components/toolbar/toolbar.svelte.d.ts +4 -37
- package/dist/components/tree/tree-item.svelte +2 -6
- package/dist/components/tree/tree-item.svelte.d.ts +3 -22
- package/dist/components/tree/tree.svelte +3 -10
- package/dist/components/tree/tree.svelte.d.ts +4 -39
- package/dist/components/util/group.svelte +2 -7
- package/dist/components/util/group.svelte.d.ts +4 -37
- package/dist/components/util/modal.d.ts +9 -0
- package/dist/components/util/modal.js +30 -0
- package/dist/components/util/modal.svelte +16 -12
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/locales/ar.yaml +2 -0
- package/dist/locales/bg.yaml +2 -0
- package/dist/locales/ca.yaml +2 -0
- package/dist/locales/cs.yaml +2 -0
- package/dist/locales/da.yaml +2 -0
- package/dist/locales/de.yaml +2 -0
- package/dist/locales/el.yaml +2 -0
- package/dist/locales/en-CA.yaml +2 -0
- package/dist/locales/en-GB.yaml +2 -0
- package/dist/locales/en-US.yaml +2 -0
- package/dist/locales/en-ZA.yaml +2 -0
- package/dist/locales/es-CO.yaml +2 -0
- package/dist/locales/es-ES.yaml +2 -0
- package/dist/locales/fa.yaml +2 -0
- package/dist/locales/fi.yaml +2 -0
- package/dist/locales/fr.yaml +2 -0
- package/dist/locales/he.yaml +2 -0
- package/dist/locales/hr.yaml +2 -0
- package/dist/locales/hu.yaml +176 -0
- package/dist/locales/it.yaml +2 -0
- package/dist/locales/ja.yaml +2 -0
- package/dist/locales/ko.yaml +2 -0
- package/dist/locales/nl.yaml +2 -0
- package/dist/locales/pl.yaml +2 -0
- package/dist/locales/pt-BR.yaml +2 -0
- package/dist/locales/pt-PT.yaml +2 -0
- package/dist/locales/ru.yaml +2 -0
- package/dist/locales/sk.yaml +2 -0
- package/dist/locales/sv.yaml +2 -0
- package/dist/locales/tr.yaml +2 -0
- package/dist/locales/uk.yaml +2 -0
- package/dist/locales/vi.yaml +2 -0
- package/dist/locales/zh-CN.yaml +2 -0
- package/dist/locales/zh-TW.yaml +2 -0
- package/dist/services/composite-widget.svelte.d.ts +76 -0
- package/dist/services/composite-widget.svelte.js +157 -0
- package/dist/services/focus.d.ts +2 -0
- package/dist/services/focus.js +68 -0
- package/dist/services/group.svelte.d.ts +17 -69
- package/dist/services/group.svelte.js +128 -391
- package/dist/services/label.d.ts +9 -0
- package/dist/services/label.js +22 -0
- package/dist/services/menu.d.ts +8 -0
- package/dist/services/menu.js +237 -0
- package/dist/services/navigation.d.ts +9 -0
- package/dist/services/navigation.js +13 -0
- package/dist/services/number.d.ts +2 -0
- package/dist/services/number.js +27 -0
- package/dist/services/pointer-drag.d.ts +14 -0
- package/dist/services/pointer-drag.js +89 -0
- package/dist/services/popup.svelte.js +123 -103
- package/dist/services/text.d.ts +3 -0
- package/dist/services/text.js +17 -0
- package/dist/services/tree.svelte.d.ts +29 -43
- package/dist/services/tree.svelte.js +58 -105
- package/dist/services/type-ahead.d.ts +10 -1
- package/dist/services/type-ahead.js +56 -34
- package/dist/typedefs.d.ts +108 -6
- package/dist/typedefs.js +63 -6
- package/package.json +5 -5
- package/dist/components/text-editor/conversion/html.d.ts +0 -30
- package/dist/components/text-editor/conversion/html.js +0 -857
- package/dist/components/text-editor/conversion/markdown.d.ts +0 -56
- package/dist/components/text-editor/conversion/markdown.js +0 -949
- package/dist/components/text-editor/toolbar/code-editor-toolbar.svelte +0 -28
- package/dist/components/text-editor/toolbar/code-editor-toolbar.svelte.d.ts +0 -37
- package/dist/components/text-editor/toolbar/insert-component.d.ts +0 -3
- package/dist/components/text-editor/toolbar/insert-component.js +0 -37
|
@@ -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;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Splitting of an exported paragraph around the elements a paragraph can’t hold, like a figure,
|
|
3
|
+
* which the HTML parser would otherwise move out of it.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { isDOMTextNode, isHTMLElement } from 'lexical';
|
|
7
|
+
import { isPreformatted, TEXT_FORMAT_TAGS } from './utils.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Elements a paragraph can’t hold: the HTML parser ends an open `<p>` before any of them.
|
|
11
|
+
* @see https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inbody
|
|
12
|
+
*/
|
|
13
|
+
const PARAGRAPH_ENDING_TAGS = [
|
|
14
|
+
...['address', 'article', 'aside', 'blockquote', 'center', 'dd', 'details', 'dialog', 'dir'],
|
|
15
|
+
...['div', 'dl', 'dt', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3'],
|
|
16
|
+
...['h4', 'h5', 'h6', 'header', 'hgroup', 'hr', 'li', 'listing', 'main', 'menu', 'nav', 'ol'],
|
|
17
|
+
...['p', 'plaintext', 'pre', 'search', 'section', 'summary', 'table', 'ul', 'xmp'],
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Check whether the given node is an element a paragraph can’t hold, like a `<figure>`.
|
|
22
|
+
* @param {Node} node Node.
|
|
23
|
+
* @returns {boolean} Result.
|
|
24
|
+
*/
|
|
25
|
+
const isParagraphEndingElement = (node) =>
|
|
26
|
+
isHTMLElement(node) && PARAGRAPH_ENDING_TAGS.includes(node.localName);
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Find the first or last text node within the given node, or the node itself if it’s a text node.
|
|
30
|
+
* @param {Node | undefined} node Node.
|
|
31
|
+
* @param {boolean} forward Whether to look for the first text node rather than the last.
|
|
32
|
+
* @returns {Text | undefined} Text node, if any.
|
|
33
|
+
*/
|
|
34
|
+
const findEdgeTextNode = (node, forward) => {
|
|
35
|
+
let current = node ?? null;
|
|
36
|
+
|
|
37
|
+
while (current && !isDOMTextNode(current)) {
|
|
38
|
+
current = forward ? current.firstChild : current.lastChild;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return current ?? undefined;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Names of the elements of the text formats, which hold nothing but text. See
|
|
46
|
+
* {@link TEXT_FORMAT_TAGS}.
|
|
47
|
+
*/
|
|
48
|
+
const TEXT_FORMAT_TAG_NAMES = TEXT_FORMAT_TAGS.map(([, tagName]) => tagName);
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Check whether the given node is blank: text with nothing but whitespace, or the element of a text
|
|
52
|
+
* format, like `<strong>`, holding only such text. Any other element, like an image, a line break
|
|
53
|
+
* or a link, is content, even without any text.
|
|
54
|
+
* @param {Node} node Node.
|
|
55
|
+
* @returns {boolean} Result.
|
|
56
|
+
*/
|
|
57
|
+
const isBlank = (node) =>
|
|
58
|
+
isDOMTextNode(node)
|
|
59
|
+
? !node.data.trim()
|
|
60
|
+
: isHTMLElement(node) &&
|
|
61
|
+
TEXT_FORMAT_TAG_NAMES.includes(node.localName) &&
|
|
62
|
+
[...node.childNodes].every(isBlank);
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Split the given exported paragraph around the elements it can’t hold, like the `<figure>` an
|
|
66
|
+
* inline editor component exports for an image with a caption: the HTML parser would end the `<p>`
|
|
67
|
+
* before the first one, leaving the content after it in an empty paragraph. The rest of the content
|
|
68
|
+
* stays in copies of the paragraph, which keep its alignment, while the whitespace next to such an
|
|
69
|
+
* element is dropped, as the parser collapses it at the start and end of a paragraph.
|
|
70
|
+
* @param {HTMLElement} paragraph Exported paragraph element.
|
|
71
|
+
* @returns {DocumentFragment | undefined} Paragraphs and elements to replace the paragraph with,
|
|
72
|
+
* or `undefined` if it holds no such element.
|
|
73
|
+
*/
|
|
74
|
+
export const splitParagraphAroundBlocks = (paragraph) => {
|
|
75
|
+
const children = [...paragraph.childNodes];
|
|
76
|
+
|
|
77
|
+
if (!children.some(isParagraphEndingElement)) {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const fragment = document.createDocumentFragment();
|
|
82
|
+
/** @type {ChildNode[]} */
|
|
83
|
+
let run = [];
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Add the content collected since the last block, if any, to the fragment in a new paragraph.
|
|
87
|
+
*/
|
|
88
|
+
const flush = () => {
|
|
89
|
+
const first = findEdgeTextNode(run[0], true);
|
|
90
|
+
const last = findEdgeTextNode(run.at(-1), false);
|
|
91
|
+
|
|
92
|
+
// Preformatted whitespace is imported as is, so it’s kept as well
|
|
93
|
+
if (first && !isPreformatted(first)) {
|
|
94
|
+
first.data = first.data.trimStart();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (last && !isPreformatted(last)) {
|
|
98
|
+
last.data = last.data.trimEnd();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!run.every(isBlank)) {
|
|
102
|
+
const newParagraph = /** @type {HTMLElement} */ (paragraph.cloneNode(false));
|
|
103
|
+
|
|
104
|
+
newParagraph.append(...run);
|
|
105
|
+
fragment.append(newParagraph);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
run = [];
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
children.forEach((child) => {
|
|
112
|
+
if (isParagraphEndingElement(child)) {
|
|
113
|
+
flush();
|
|
114
|
+
fragment.append(child);
|
|
115
|
+
} else {
|
|
116
|
+
run.push(child);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
flush();
|
|
121
|
+
|
|
122
|
+
return fragment;
|
|
123
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function decorateTaskList(node: ListNode, element: HTMLElement): void;
|
|
2
|
+
export function decorateTaskListItem(node: ListItemNode, element: HTMLElement): void;
|
|
3
|
+
export function isTaskListCheckbox(element: Element): boolean;
|
|
4
|
+
/**
|
|
5
|
+
* Additional DOM conversions to import HTML with task lists. The check box of a task list item is
|
|
6
|
+
* dropped, as Lexical has already taken its state for the item. Any other `<input>` is still
|
|
7
|
+
* reported by `findUnsupportedNode()` in `unsupported.js`.
|
|
8
|
+
* @type {DOMConversionMap}
|
|
9
|
+
*/
|
|
10
|
+
export const HTML_TASK_LIST_IMPORT_MAP: DOMConversionMap;
|
|
11
|
+
import type { ListNode } from '@lexical/list';
|
|
12
|
+
import type { ListItemNode } from '@lexical/list';
|
|
13
|
+
import type { DOMConversionMap } from 'lexical';
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML of GitHub Flavored Markdown task lists, which Lexical calls check lists. They’re exported
|
|
3
|
+
* the way GitHub and markdown-it render them, a `<ul class="contains-task-list">` holding
|
|
4
|
+
* `<li class="task-list-item">` items that start with a disabled `<input type="checkbox">`, which
|
|
5
|
+
* Lexical imports back as a check list. The Markdown transformer is in
|
|
6
|
+
* `../transformers/task-list.js`.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { $isListNode as isListNode } from '@lexical/list';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @import { ListItemNode, ListNode } from '@lexical/list';
|
|
13
|
+
* @import { DOMConversion, DOMConversionMap, DOMConversionOutput } from 'lexical';
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Add the class of a task list to the exported element of a check list.
|
|
18
|
+
* @param {ListNode} node List node.
|
|
19
|
+
* @param {HTMLElement} element Exported element.
|
|
20
|
+
*/
|
|
21
|
+
export const decorateTaskList = (node, element) => {
|
|
22
|
+
if (node.getListType() === 'check') {
|
|
23
|
+
element.classList.add('contains-task-list');
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Add the class and a disabled check box to the exported element of a check list item. An item
|
|
29
|
+
* that only holds a nested list has neither, as it’s merged into the previous item.
|
|
30
|
+
* @param {ListItemNode} node List item node.
|
|
31
|
+
* @param {HTMLElement} element Exported element.
|
|
32
|
+
*/
|
|
33
|
+
export const decorateTaskListItem = (node, element) => {
|
|
34
|
+
const parent = node.getParent();
|
|
35
|
+
|
|
36
|
+
if (!isListNode(parent) || parent.getListType() !== 'check' || isListNode(node.getFirstChild())) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const checkbox = element.ownerDocument.createElement('input');
|
|
41
|
+
|
|
42
|
+
checkbox.type = 'checkbox';
|
|
43
|
+
checkbox.disabled = true;
|
|
44
|
+
checkbox.toggleAttribute('checked', !!node.getChecked());
|
|
45
|
+
element.classList.add('task-list-item');
|
|
46
|
+
element.prepend(checkbox, ' ');
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Check whether the given element is the check box of a task list item, like the `<input>` in
|
|
51
|
+
* `<li class="task-list-item"><input type="checkbox" disabled> Item</li>`. Lexical imports the
|
|
52
|
+
* item as a check list item, taking its state from the first `<input>` in it.
|
|
53
|
+
* @param {Element} element Element.
|
|
54
|
+
* @returns {boolean} Result.
|
|
55
|
+
*/
|
|
56
|
+
export const isTaskListCheckbox = (element) =>
|
|
57
|
+
element.localName === 'input' &&
|
|
58
|
+
element.getAttribute('type')?.toLowerCase() === 'checkbox' &&
|
|
59
|
+
!!element.parentElement?.matches('li.task-list-item') &&
|
|
60
|
+
element.parentElement.querySelector(':scope > input') === element;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Drop an element without importing it.
|
|
64
|
+
* @returns {DOMConversionOutput} Conversion output.
|
|
65
|
+
*/
|
|
66
|
+
const dropElement = () => ({ node: null });
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Additional DOM conversions to import HTML with task lists. The check box of a task list item is
|
|
70
|
+
* dropped, as Lexical has already taken its state for the item. Any other `<input>` is still
|
|
71
|
+
* reported by `findUnsupportedNode()` in `unsupported.js`.
|
|
72
|
+
* @type {DOMConversionMap}
|
|
73
|
+
*/
|
|
74
|
+
export const HTML_TASK_LIST_IMPORT_MAP = {
|
|
75
|
+
/**
|
|
76
|
+
* Get the conversion for an `<input>` element.
|
|
77
|
+
* @param {HTMLElement} element Element.
|
|
78
|
+
* @returns {DOMConversion | null} Conversion.
|
|
79
|
+
*/
|
|
80
|
+
input: (element) =>
|
|
81
|
+
isTaskListCheckbox(element) ? { conversion: dropElement, priority: 1 } : null,
|
|
82
|
+
};
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Detection of HTML the rich text editor can’t import, which can then only be edited as source.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { isTaskListCheckbox } from './task-list.js';
|
|
6
|
+
import { editorComponents, getComponentConversionMap, getElementComponent } from './components.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @import { LexicalEditor } from 'lexical';
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Table elements that have no Lexical node of their own. The rows are imported as is, while the
|
|
14
|
+
* column definitions are only used for the column widths.
|
|
15
|
+
*/
|
|
16
|
+
const PASSTHROUGH_TAGS = ['tbody', 'thead', 'tfoot', 'colgroup', 'col'];
|
|
17
|
+
/**
|
|
18
|
+
* Elements the HTML parser always creates, which hold the content rather than being part of it.
|
|
19
|
+
*/
|
|
20
|
+
const DOCUMENT_TAGS = ['html', 'head', 'body'];
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Get the names of the elements only editor components import, which no other node handles.
|
|
24
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
25
|
+
* @returns {Set<string>} Tag names.
|
|
26
|
+
*/
|
|
27
|
+
const getComponentOnlyTagNames = (editor) => {
|
|
28
|
+
const components = editorComponents.get(editor) ?? [];
|
|
29
|
+
// The `node` is actually a node class
|
|
30
|
+
const componentNodes = new Set(components.map(({ node }) => /** @type {any} */ (node)));
|
|
31
|
+
|
|
32
|
+
const otherTagNames = new Set(
|
|
33
|
+
[...editor._nodes.values()]
|
|
34
|
+
.filter(({ klass }) => !componentNodes.has(klass))
|
|
35
|
+
.flatMap(({ klass }) => Object.keys(klass.importDOM?.() ?? {})),
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
return new Set(
|
|
39
|
+
components
|
|
40
|
+
.flatMap(({ node }) => Object.keys(getComponentConversionMap(node)))
|
|
41
|
+
.filter((tagName) => !otherTagNames.has(tagName)),
|
|
42
|
+
);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Get the first node in the given document that the editor cannot import, which would be silently
|
|
47
|
+
* dropped, possibly along with its content:
|
|
48
|
+
*
|
|
49
|
+
* - An element none of the registered nodes, including the ones of editor components, handle. This
|
|
50
|
+
* includes an element the parser moves to `<head>`, like a `<script>` at the beginning, and an
|
|
51
|
+
* element only editor components handle, but none of them imports, like a `<figure>` that isn’t an
|
|
52
|
+
* instance of a component for `<figure class="photo">`, which would be unwrapped.
|
|
53
|
+
* - A comment, like `<!-- more -->`.
|
|
54
|
+
*
|
|
55
|
+
* The content of an element an editor component imports is part of the component, which is
|
|
56
|
+
* expected to drop the children, and the elements within a `<pre>` element are imported as plain
|
|
57
|
+
* text, so they’re ignored.
|
|
58
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
59
|
+
* @param {Document} dom Parsed document.
|
|
60
|
+
* @returns {Element | Comment | undefined} Unsupported node.
|
|
61
|
+
*/
|
|
62
|
+
export const findUnsupportedNode = (editor, dom) => {
|
|
63
|
+
const componentOnlyTagNames = getComponentOnlyTagNames(editor);
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Find an unsupported node among the descendants of the given node.
|
|
67
|
+
* @param {Node} parent Parent node.
|
|
68
|
+
* @returns {Element | Comment | undefined} Unsupported node.
|
|
69
|
+
*/
|
|
70
|
+
const findWithin = (parent) => {
|
|
71
|
+
/** @type {Element | Comment | undefined} */
|
|
72
|
+
let found;
|
|
73
|
+
|
|
74
|
+
[...parent.childNodes].some((node) => {
|
|
75
|
+
if (node.nodeType === Node.COMMENT_NODE) {
|
|
76
|
+
found = /** @type {Comment} */ (node);
|
|
77
|
+
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (node.nodeType !== Node.ELEMENT_NODE) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const element = /** @type {Element} */ (node);
|
|
86
|
+
const tagName = element.localName;
|
|
87
|
+
|
|
88
|
+
if (getElementComponent(editor, element)) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// `_htmlConversions` holds the conversions of all the registered nodes, along with the ones
|
|
93
|
+
// in the `html.import` editor config. A conversion may still skip an element on purpose,
|
|
94
|
+
// like a `<br>` at the end of a paragraph, which browsers don’t render
|
|
95
|
+
if (
|
|
96
|
+
!DOCUMENT_TAGS.includes(tagName) &&
|
|
97
|
+
!PASSTHROUGH_TAGS.includes(tagName) &&
|
|
98
|
+
(!editor._htmlConversions.has(tagName) ||
|
|
99
|
+
componentOnlyTagNames.has(tagName) ||
|
|
100
|
+
// Only the check box of a task list item can be imported
|
|
101
|
+
(tagName === 'input' && !isTaskListCheckbox(element)))
|
|
102
|
+
) {
|
|
103
|
+
found = element;
|
|
104
|
+
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
found =
|
|
109
|
+
tagName === 'pre'
|
|
110
|
+
? /** @type {Comment | undefined} */ (
|
|
111
|
+
dom.createTreeWalker(element, NodeFilter.SHOW_COMMENT).nextNode() ?? undefined
|
|
112
|
+
)
|
|
113
|
+
: findWithin(element);
|
|
114
|
+
|
|
115
|
+
return !!found;
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
return found;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
return findWithin(dom);
|
|
122
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export function isPreformatted(node: Node): boolean;
|
|
2
|
+
/**
|
|
3
|
+
* Text formats and their HTML tags, from the innermost to the outermost.
|
|
4
|
+
* @type {[import('lexical').TextFormatType, string][]}
|
|
5
|
+
*/
|
|
6
|
+
export const TEXT_FORMAT_TAGS: [import("lexical").TextFormatType, string][];
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helpers and constants shared by the HTML import and export.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Check if the given node is within a `<pre>` element or an element with preformatted whitespace,
|
|
7
|
+
* whose text is imported by Lexical’s own conversion as is.
|
|
8
|
+
* @param {Node} node Node.
|
|
9
|
+
* @returns {boolean} Result.
|
|
10
|
+
*/
|
|
11
|
+
export const isPreformatted = (node) => {
|
|
12
|
+
for (let element = node.parentElement; element; element = element.parentElement) {
|
|
13
|
+
if (element.localName === 'pre' || element.style.whiteSpace.startsWith('pre')) {
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return false;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Text formats and their HTML tags, from the innermost to the outermost.
|
|
23
|
+
* @type {[import('lexical').TextFormatType, string][]}
|
|
24
|
+
*/
|
|
25
|
+
export const TEXT_FORMAT_TAGS = [
|
|
26
|
+
['code', 'code'],
|
|
27
|
+
['subscript', 'sub'],
|
|
28
|
+
['superscript', 'sup'],
|
|
29
|
+
['highlight', 'mark'],
|
|
30
|
+
['underline', 'u'],
|
|
31
|
+
['strikethrough', 's'],
|
|
32
|
+
['italic', 'em'],
|
|
33
|
+
['bold', 'strong'],
|
|
34
|
+
];
|