@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
|
@@ -1,857 +0,0 @@
|
|
|
1
|
-
import { CodeHighlightNode, CodeNode, $createCodeNode as createCodeNode } from '@lexical/code-core';
|
|
2
|
-
import { HorizontalRuleNode } from '@lexical/extension';
|
|
3
|
-
import { $generateHtmlFromNodes as generateHtmlFromNodes } from '@lexical/html';
|
|
4
|
-
import { LinkNode } from '@lexical/link';
|
|
5
|
-
import { ListItemNode, ListNode } from '@lexical/list';
|
|
6
|
-
import { HeadingNode, QuoteNode } from '@lexical/rich-text';
|
|
7
|
-
import { TableCellNode, TableNode } from '@lexical/table';
|
|
8
|
-
import {
|
|
9
|
-
$createTextNode as createTextNode,
|
|
10
|
-
$getEditor as getEditor,
|
|
11
|
-
$isParagraphNode as isParagraphNode,
|
|
12
|
-
isDOMTextNode,
|
|
13
|
-
isHTMLElement,
|
|
14
|
-
isInlineDomNode,
|
|
15
|
-
ParagraphNode,
|
|
16
|
-
TabNode,
|
|
17
|
-
TextNode,
|
|
18
|
-
} from 'lexical';
|
|
19
|
-
import { isPlainLanguage } from '../shiki/facade.js';
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @import {
|
|
23
|
-
* DOMConversion,
|
|
24
|
-
* DOMConversionMap,
|
|
25
|
-
* DOMConversionOutput,
|
|
26
|
-
* DOMExportOutput,
|
|
27
|
-
* DOMExportOutputMap,
|
|
28
|
-
* ElementNode,
|
|
29
|
-
* LexicalEditor,
|
|
30
|
-
* LexicalNode,
|
|
31
|
-
* } from 'lexical';
|
|
32
|
-
* @import { TextEditorComponent } from '../../../typedefs.js';
|
|
33
|
-
*/
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Attributes to be kept on the exported elements, by tag name. Anything else Lexical adds, like
|
|
37
|
-
* `class`, `dir`, `style`, `value` or `spellcheck`, is only meant for the editor itself, except for
|
|
38
|
-
* the text alignment of a block; see {@link ALIGNABLE_TAGS}.
|
|
39
|
-
* @type {Record<string, string[]>}
|
|
40
|
-
*/
|
|
41
|
-
const ALLOWED_ATTRIBUTES = {
|
|
42
|
-
a: ['href', 'rel', 'target', 'title'],
|
|
43
|
-
ol: ['start'],
|
|
44
|
-
td: ['colspan', 'rowspan'],
|
|
45
|
-
th: ['colspan', 'rowspan'],
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Text formats and their HTML tags, from the innermost to the outermost.
|
|
50
|
-
* @type {[import('lexical').TextFormatType, string][]}
|
|
51
|
-
*/
|
|
52
|
-
const TEXT_FORMAT_TAGS = [
|
|
53
|
-
['code', 'code'],
|
|
54
|
-
['subscript', 'sub'],
|
|
55
|
-
['superscript', 'sup'],
|
|
56
|
-
['highlight', 'mark'],
|
|
57
|
-
['underline', 'u'],
|
|
58
|
-
['strikethrough', 's'],
|
|
59
|
-
['italic', 'em'],
|
|
60
|
-
['bold', 'strong'],
|
|
61
|
-
];
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Table elements that have no Lexical node of their own. The rows are imported as is, while the
|
|
65
|
-
* column definitions are only used for the column widths.
|
|
66
|
-
*/
|
|
67
|
-
const PASSTHROUGH_TAGS = ['tbody', 'thead', 'tfoot', 'colgroup', 'col'];
|
|
68
|
-
/**
|
|
69
|
-
* Elements the HTML parser always creates, which hold the content rather than being part of it.
|
|
70
|
-
*/
|
|
71
|
-
const DOCUMENT_TAGS = ['html', 'head', 'body'];
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Get the programming language from the `language-*` or `lang-*` class of the given `<code>`
|
|
75
|
-
* element, which is common in HTML generated from Markdown.
|
|
76
|
-
* @param {Element} code `<code>` element.
|
|
77
|
-
* @returns {string | undefined} Language.
|
|
78
|
-
*/
|
|
79
|
-
export const getCodeLanguage = (code) =>
|
|
80
|
-
code.className.match(/(?:^|\s)(?:lang|language)-(\S+)/)?.[1];
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Elements that keep their text alignment, which is the only style kept in the exported HTML.
|
|
84
|
-
* Lexical writes it as `style="text-align: …"` for the blocks it can be set on.
|
|
85
|
-
*/
|
|
86
|
-
const ALIGNABLE_TAGS = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'li'];
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Remove the attributes not listed in {@link ALLOWED_ATTRIBUTES} from the given element, as well as
|
|
90
|
-
* any style but the text alignment of a block listed in {@link ALIGNABLE_TAGS}.
|
|
91
|
-
* @param {Element} element Element.
|
|
92
|
-
*/
|
|
93
|
-
const removeExtraAttributes = (element) => {
|
|
94
|
-
const allowedAttributes = ALLOWED_ATTRIBUTES[element.localName] ?? [];
|
|
95
|
-
|
|
96
|
-
const textAlign = ALIGNABLE_TAGS.includes(element.localName)
|
|
97
|
-
? /** @type {HTMLElement} */ (element).style.textAlign
|
|
98
|
-
: '';
|
|
99
|
-
|
|
100
|
-
[...element.attributes].forEach(({ name }) => {
|
|
101
|
-
if (!allowedAttributes.includes(name)) {
|
|
102
|
-
element.removeAttribute(name);
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
if (textAlign) {
|
|
107
|
-
/** @type {HTMLElement} */ (element).style.textAlign = textAlign;
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Export an element node with Lexical’s own implementation, then remove the extra attributes from
|
|
113
|
-
* the element, as well as the attribute Lexical adds to the `<br>` terminating a trailing line
|
|
114
|
-
* break. The element’s own `after` function, if any, still runs first, so the structure is kept,
|
|
115
|
-
* like a nested list moved into the previous list item.
|
|
116
|
-
* @param {LexicalEditor} editor Editor instance.
|
|
117
|
-
* @param {ElementNode} node Element node.
|
|
118
|
-
* @returns {DOMExportOutput} Export output.
|
|
119
|
-
*/
|
|
120
|
-
const exportElementNode = (editor, node) => {
|
|
121
|
-
const { after, ...output } = node.exportDOM(editor);
|
|
122
|
-
|
|
123
|
-
if (isHTMLElement(output.element)) {
|
|
124
|
-
removeExtraAttributes(output.element);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
return {
|
|
128
|
-
...output,
|
|
129
|
-
/**
|
|
130
|
-
* Clean up the element once its children are in place.
|
|
131
|
-
* @param {HTMLElement | DocumentFragment | Text | null | undefined} element Exported element.
|
|
132
|
-
* @returns {HTMLElement | DocumentFragment | Text | null | undefined} New element to replace
|
|
133
|
-
* the exported one, if any.
|
|
134
|
-
*/
|
|
135
|
-
after: (element) => {
|
|
136
|
-
const newElement = after ? after.call(node, element) : undefined;
|
|
137
|
-
const target = newElement ?? element;
|
|
138
|
-
|
|
139
|
-
if (isHTMLElement(target)) {
|
|
140
|
-
removeExtraAttributes(target);
|
|
141
|
-
target.querySelectorAll(':scope > br').forEach(removeExtraAttributes);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// Returning the same element would make Lexical replace it with itself
|
|
145
|
-
return newElement === element ? undefined : newElement;
|
|
146
|
-
},
|
|
147
|
-
};
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Elements a paragraph can’t hold: the HTML parser ends an open `<p>` before any of them.
|
|
152
|
-
* @see https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inbody
|
|
153
|
-
*/
|
|
154
|
-
const PARAGRAPH_ENDING_TAGS = [
|
|
155
|
-
...['address', 'article', 'aside', 'blockquote', 'center', 'dd', 'details', 'dialog', 'dir'],
|
|
156
|
-
...['div', 'dl', 'dt', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'h1', 'h2', 'h3'],
|
|
157
|
-
...['h4', 'h5', 'h6', 'header', 'hgroup', 'hr', 'li', 'listing', 'main', 'menu', 'nav', 'ol'],
|
|
158
|
-
...['p', 'plaintext', 'pre', 'search', 'section', 'summary', 'table', 'ul', 'xmp'],
|
|
159
|
-
];
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Check whether the given node is an element a paragraph can’t hold, like a `<figure>`.
|
|
163
|
-
* @param {Node} node Node.
|
|
164
|
-
* @returns {boolean} Result.
|
|
165
|
-
*/
|
|
166
|
-
const isParagraphEndingElement = (node) =>
|
|
167
|
-
isHTMLElement(node) && PARAGRAPH_ENDING_TAGS.includes(node.localName);
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Check if the given node is within a `<pre>` element or an element with preformatted whitespace,
|
|
171
|
-
* whose text is imported by Lexical’s own conversion as is.
|
|
172
|
-
* @param {Node} node Node.
|
|
173
|
-
* @returns {boolean} Result.
|
|
174
|
-
*/
|
|
175
|
-
const isPreformatted = (node) => {
|
|
176
|
-
for (let element = node.parentElement; element; element = element.parentElement) {
|
|
177
|
-
if (element.localName === 'pre' || element.style.whiteSpace.startsWith('pre')) {
|
|
178
|
-
return true;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return false;
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Find the first or last text node within the given node, or the node itself if it’s a text node.
|
|
187
|
-
* @param {Node | undefined} node Node.
|
|
188
|
-
* @param {boolean} forward Whether to look for the first text node rather than the last.
|
|
189
|
-
* @returns {Text | undefined} Text node, if any.
|
|
190
|
-
*/
|
|
191
|
-
const findEdgeTextNode = (node, forward) => {
|
|
192
|
-
let current = node ?? null;
|
|
193
|
-
|
|
194
|
-
while (current && !isDOMTextNode(current)) {
|
|
195
|
-
current = forward ? current.firstChild : current.lastChild;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
return current ?? undefined;
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Names of the elements of the text formats, which hold nothing but text. See
|
|
203
|
-
* {@link TEXT_FORMAT_TAGS}.
|
|
204
|
-
*/
|
|
205
|
-
const TEXT_FORMAT_TAG_NAMES = TEXT_FORMAT_TAGS.map(([, tagName]) => tagName);
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Check whether the given node is blank: text with nothing but whitespace, or the element of a text
|
|
209
|
-
* format, like `<strong>`, holding only such text. Any other element, like an image, a line break
|
|
210
|
-
* or a link, is content, even without any text.
|
|
211
|
-
* @param {Node} node Node.
|
|
212
|
-
* @returns {boolean} Result.
|
|
213
|
-
*/
|
|
214
|
-
const isBlank = (node) =>
|
|
215
|
-
isDOMTextNode(node)
|
|
216
|
-
? !node.data.trim()
|
|
217
|
-
: isHTMLElement(node) &&
|
|
218
|
-
TEXT_FORMAT_TAG_NAMES.includes(node.localName) &&
|
|
219
|
-
[...node.childNodes].every(isBlank);
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Split the given exported paragraph around the elements it can’t hold, like the `<figure>` an
|
|
223
|
-
* inline editor component exports for an image with a caption: the HTML parser would end the `<p>`
|
|
224
|
-
* before the first one, leaving the content after it in an empty paragraph. The rest of the content
|
|
225
|
-
* stays in copies of the paragraph, which keep its alignment, while the whitespace next to such an
|
|
226
|
-
* element is dropped, as the parser collapses it at the start and end of a paragraph.
|
|
227
|
-
* @param {HTMLElement} paragraph Exported paragraph element.
|
|
228
|
-
* @returns {DocumentFragment | undefined} Paragraphs and elements to replace the paragraph with,
|
|
229
|
-
* or `undefined` if it holds no such element.
|
|
230
|
-
*/
|
|
231
|
-
const splitParagraphAroundBlocks = (paragraph) => {
|
|
232
|
-
const children = [...paragraph.childNodes];
|
|
233
|
-
|
|
234
|
-
if (!children.some(isParagraphEndingElement)) {
|
|
235
|
-
return undefined;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
const fragment = document.createDocumentFragment();
|
|
239
|
-
/** @type {ChildNode[]} */
|
|
240
|
-
let run = [];
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* Add the content collected since the last block, if any, to the fragment in a new paragraph.
|
|
244
|
-
*/
|
|
245
|
-
const flush = () => {
|
|
246
|
-
const first = findEdgeTextNode(run[0], true);
|
|
247
|
-
const last = findEdgeTextNode(run.at(-1), false);
|
|
248
|
-
|
|
249
|
-
// Preformatted whitespace is imported as is, so it’s kept as well
|
|
250
|
-
if (first && !isPreformatted(first)) {
|
|
251
|
-
first.data = first.data.trimStart();
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
if (last && !isPreformatted(last)) {
|
|
255
|
-
last.data = last.data.trimEnd();
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
if (!run.every(isBlank)) {
|
|
259
|
-
const newParagraph = /** @type {HTMLElement} */ (paragraph.cloneNode(false));
|
|
260
|
-
|
|
261
|
-
newParagraph.append(...run);
|
|
262
|
-
fragment.append(newParagraph);
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
run = [];
|
|
266
|
-
};
|
|
267
|
-
|
|
268
|
-
children.forEach((child) => {
|
|
269
|
-
if (isParagraphEndingElement(child)) {
|
|
270
|
-
flush();
|
|
271
|
-
fragment.append(child);
|
|
272
|
-
} else {
|
|
273
|
-
run.push(child);
|
|
274
|
-
}
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
flush();
|
|
278
|
-
|
|
279
|
-
return fragment;
|
|
280
|
-
};
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* Export a paragraph node. Empty paragraphs at the end are left out, as they have no content; one
|
|
284
|
-
* may be added just by clicking below a block decorator. A paragraph that is the only child of a
|
|
285
|
-
* table cell is exported without the `<p>` element, unless it’s aligned. A paragraph holding
|
|
286
|
-
* elements it can’t hold, like a `<figure>`, is split around them.
|
|
287
|
-
* @param {LexicalEditor} editor Editor instance.
|
|
288
|
-
* @param {ParagraphNode} node Paragraph node.
|
|
289
|
-
* @returns {DOMExportOutput} Export output.
|
|
290
|
-
*/
|
|
291
|
-
const exportParagraphNode = (editor, node) => {
|
|
292
|
-
const parent = node.getParent();
|
|
293
|
-
|
|
294
|
-
// Keep the `<p>` of an aligned paragraph, which holds the alignment
|
|
295
|
-
if (parent instanceof TableCellNode && parent.getChildrenSize() === 1 && !node.getFormatType()) {
|
|
296
|
-
return { element: document.createDocumentFragment() };
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
if (
|
|
300
|
-
node.isEmpty() &&
|
|
301
|
-
node.getNextSiblings().every((sibling) => isParagraphNode(sibling) && sibling.isEmpty())
|
|
302
|
-
) {
|
|
303
|
-
return { element: null };
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
const { after, ...output } = exportElementNode(editor, node);
|
|
307
|
-
|
|
308
|
-
return {
|
|
309
|
-
...output,
|
|
310
|
-
/**
|
|
311
|
-
* Clean up the paragraph once its children are in place, and split it around the elements it
|
|
312
|
-
* can’t hold, if any.
|
|
313
|
-
* @param {HTMLElement | DocumentFragment | Text | null | undefined} element Exported element.
|
|
314
|
-
* @returns {HTMLElement | DocumentFragment | Text | null | undefined} New element to replace
|
|
315
|
-
* the exported one, if any.
|
|
316
|
-
*/
|
|
317
|
-
after: (element) => {
|
|
318
|
-
const newElement = /** @type {NonNullable<DOMExportOutput['after']>} */ (after).call(
|
|
319
|
-
node,
|
|
320
|
-
element,
|
|
321
|
-
);
|
|
322
|
-
|
|
323
|
-
const target = newElement ?? element;
|
|
324
|
-
|
|
325
|
-
return (isHTMLElement(target) && splitParagraphAroundBlocks(target)) || newElement;
|
|
326
|
-
},
|
|
327
|
-
};
|
|
328
|
-
};
|
|
329
|
-
|
|
330
|
-
/**
|
|
331
|
-
* Export a text node as plain text wrapped with the elements for its formats, instead of the
|
|
332
|
-
* `<span>`, `<b>` and `<i>` elements with styles and classes Lexical uses.
|
|
333
|
-
* @param {LexicalEditor} _editor Editor instance.
|
|
334
|
-
* @param {TextNode} node Text node.
|
|
335
|
-
* @returns {DOMExportOutput} Export output.
|
|
336
|
-
*/
|
|
337
|
-
const exportTextNode = (_editor, node) => {
|
|
338
|
-
/** @type {HTMLElement | Text} */
|
|
339
|
-
let element = document.createTextNode(node.getTextContent());
|
|
340
|
-
|
|
341
|
-
TEXT_FORMAT_TAGS.forEach(([format, tagName]) => {
|
|
342
|
-
if (node.hasFormat(format)) {
|
|
343
|
-
const wrapper = document.createElement(tagName);
|
|
344
|
-
|
|
345
|
-
wrapper.append(element);
|
|
346
|
-
element = wrapper;
|
|
347
|
-
}
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
return { element };
|
|
351
|
-
};
|
|
352
|
-
|
|
353
|
-
/**
|
|
354
|
-
* Export a code block node as `<pre><code>` with the common `language-*` class, instead of the
|
|
355
|
-
* highlighted tokens Lexical renders in the editor. The children are still exported as usual, so
|
|
356
|
-
* copying part of a code block only exports the selected text, then their text is moved into the
|
|
357
|
-
* `<code>` element.
|
|
358
|
-
* @param {LexicalEditor} _editor Editor instance.
|
|
359
|
-
* @param {CodeNode} node Code node.
|
|
360
|
-
* @returns {DOMExportOutput} Export output.
|
|
361
|
-
*/
|
|
362
|
-
const exportCodeNode = (_editor, node) => ({
|
|
363
|
-
element: document.createElement('pre'),
|
|
364
|
-
/**
|
|
365
|
-
* Replace the exported children with a `<code>` element holding their text.
|
|
366
|
-
* @param {HTMLElement | DocumentFragment | Text | null | undefined} pre Exported element.
|
|
367
|
-
* @returns {undefined} Nothing, as the element is updated in place.
|
|
368
|
-
*/
|
|
369
|
-
after: (pre) => {
|
|
370
|
-
/* v8 ignore next 3 */
|
|
371
|
-
if (!isHTMLElement(pre)) {
|
|
372
|
-
return undefined;
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
const code = document.createElement('code');
|
|
376
|
-
const language = node.getLanguage();
|
|
377
|
-
|
|
378
|
-
if (language && !isPlainLanguage(language)) {
|
|
379
|
-
code.className = `language-${language}`;
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
code.textContent = [...pre.childNodes]
|
|
383
|
-
.map((child) => {
|
|
384
|
-
if (child.nodeName !== 'BR') {
|
|
385
|
-
return child.textContent;
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
// Skip the `<br>` Lexical adds to terminate a trailing line break
|
|
389
|
-
return isHTMLElement(child) && child.hasAttribute('data-lexical-managed-linebreak')
|
|
390
|
-
? ''
|
|
391
|
-
: '\n';
|
|
392
|
-
})
|
|
393
|
-
.join('');
|
|
394
|
-
|
|
395
|
-
pre.replaceChildren(code);
|
|
396
|
-
|
|
397
|
-
return undefined;
|
|
398
|
-
},
|
|
399
|
-
});
|
|
400
|
-
|
|
401
|
-
/**
|
|
402
|
-
* Selector for the structural elements of a table, excluding the cell content.
|
|
403
|
-
*/
|
|
404
|
-
const TABLE_STRUCTURE_SELECTOR = [
|
|
405
|
-
':scope > :is(thead, tbody, tfoot)',
|
|
406
|
-
':scope > :is(thead, tbody, tfoot) > tr',
|
|
407
|
-
':scope > :is(thead, tbody, tfoot) > tr > :is(th, td)',
|
|
408
|
-
':scope > tr',
|
|
409
|
-
':scope > tr > :is(th, td)',
|
|
410
|
-
].join(', ');
|
|
411
|
-
|
|
412
|
-
/**
|
|
413
|
-
* Export a table node, then remove the column definitions and extra attributes Lexical adds to the
|
|
414
|
-
* table and its descendants. The cleanup has to wait until the table is complete, as Lexical uses
|
|
415
|
-
* temporary attributes on the cells to compute the columns.
|
|
416
|
-
* @param {LexicalEditor} editor Editor instance.
|
|
417
|
-
* @param {TableNode} node Table node.
|
|
418
|
-
* @returns {DOMExportOutput} Export output.
|
|
419
|
-
*/
|
|
420
|
-
const exportTableNode = (editor, node) => {
|
|
421
|
-
const { after, ...output } = node.exportDOM(editor);
|
|
422
|
-
|
|
423
|
-
return {
|
|
424
|
-
...output,
|
|
425
|
-
/**
|
|
426
|
-
* Clean up the table once Lexical has completed it.
|
|
427
|
-
* @param {HTMLElement | DocumentFragment | Text | null | undefined} element Exported element.
|
|
428
|
-
* @returns {HTMLElement | DocumentFragment | Text | null | undefined} New element to replace
|
|
429
|
-
* the exported one, if any.
|
|
430
|
-
*/
|
|
431
|
-
after: (element) => {
|
|
432
|
-
const newElement = after ? after.call(node, element) : undefined;
|
|
433
|
-
const target = newElement ?? element;
|
|
434
|
-
|
|
435
|
-
if (isHTMLElement(target)) {
|
|
436
|
-
target.querySelectorAll(':scope > colgroup').forEach((colgroup) => colgroup.remove());
|
|
437
|
-
// Leave the cell content alone, which has been cleaned up by its own nodes, while the
|
|
438
|
-
// content of an editor component may need its attributes
|
|
439
|
-
[target, ...target.querySelectorAll(TABLE_STRUCTURE_SELECTOR)].forEach(
|
|
440
|
-
removeExtraAttributes,
|
|
441
|
-
);
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
// Returning the same element would make Lexical replace it with itself
|
|
445
|
-
return newElement === element ? undefined : newElement;
|
|
446
|
-
},
|
|
447
|
-
};
|
|
448
|
-
};
|
|
449
|
-
|
|
450
|
-
/**
|
|
451
|
-
* Map of Lexical node classes and functions to export them to clean HTML. A class only takes effect
|
|
452
|
-
* if the node is registered with the editor. Other nodes, including the ones of editor components,
|
|
453
|
-
* are exported with their own `exportDOM` method.
|
|
454
|
-
* @type {DOMExportOutputMap}
|
|
455
|
-
*/
|
|
456
|
-
export const HTML_EXPORT_MAP = new Map(
|
|
457
|
-
/** @type {[any, (editor: LexicalEditor, node: any) => DOMExportOutput][]} */ ([
|
|
458
|
-
[TextNode, exportTextNode],
|
|
459
|
-
[TabNode, exportTextNode],
|
|
460
|
-
[CodeHighlightNode, exportTextNode],
|
|
461
|
-
[ParagraphNode, exportParagraphNode],
|
|
462
|
-
[HeadingNode, exportElementNode],
|
|
463
|
-
[QuoteNode, exportElementNode],
|
|
464
|
-
[ListNode, exportElementNode],
|
|
465
|
-
[ListItemNode, exportElementNode],
|
|
466
|
-
[LinkNode, exportElementNode],
|
|
467
|
-
[HorizontalRuleNode, exportElementNode],
|
|
468
|
-
[CodeNode, exportCodeNode],
|
|
469
|
-
[TableNode, exportTableNode],
|
|
470
|
-
]),
|
|
471
|
-
);
|
|
472
|
-
|
|
473
|
-
/**
|
|
474
|
-
* Get the text content of the given element, with `<br>` elements as line breaks.
|
|
475
|
-
* @param {Element} element Element.
|
|
476
|
-
* @returns {string} Text.
|
|
477
|
-
*/
|
|
478
|
-
const getTextWithLineBreaks = (element) => {
|
|
479
|
-
const walker = document.createTreeWalker(element, NodeFilter.SHOW_ALL);
|
|
480
|
-
let text = '';
|
|
481
|
-
|
|
482
|
-
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
483
|
-
if (node.nodeType === Node.TEXT_NODE) {
|
|
484
|
-
text += /** @type {Text} */ (node).data;
|
|
485
|
-
} else if (node.nodeName === 'BR') {
|
|
486
|
-
text += '\n';
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
return text;
|
|
491
|
-
};
|
|
492
|
-
|
|
493
|
-
/**
|
|
494
|
-
* Convert a `<pre>` element to a code block node, taking the language from the `language-*` or
|
|
495
|
-
* `lang-*` class of the `<code>` element within it, which is common in HTML generated from
|
|
496
|
-
* Markdown, as well as the `data-language` attribute Lexical uses. The content is imported as plain
|
|
497
|
-
* text, so any markup within it, like highlighted tokens, is dropped, while a `<br>` element
|
|
498
|
-
* becomes a line break.
|
|
499
|
-
* @param {HTMLElement} pre `<pre>` element.
|
|
500
|
-
* @returns {DOMConversionOutput} Conversion output.
|
|
501
|
-
*/
|
|
502
|
-
const convertPreElement = (pre) => {
|
|
503
|
-
const code = pre.querySelector(':scope > code');
|
|
504
|
-
const language = (code ? getCodeLanguage(code) : undefined) ?? pre.getAttribute('data-language');
|
|
505
|
-
const node = createCodeNode(language);
|
|
506
|
-
// A code block generated from Markdown ends with a line break
|
|
507
|
-
const text = getTextWithLineBreaks(pre).replace(/\n$/, '');
|
|
508
|
-
|
|
509
|
-
if (text) {
|
|
510
|
-
node.append(createTextNode(text));
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
return {
|
|
514
|
-
node,
|
|
515
|
-
/**
|
|
516
|
-
* Drop the child nodes, which are already imported as the text content.
|
|
517
|
-
* @returns {LexicalNode[]} Nodes.
|
|
518
|
-
*/
|
|
519
|
-
after: () => [],
|
|
520
|
-
};
|
|
521
|
-
};
|
|
522
|
-
|
|
523
|
-
/**
|
|
524
|
-
* Editor components of each editor, used to tell the elements they import, which come with their
|
|
525
|
-
* content, when HTML is imported.
|
|
526
|
-
* @type {WeakMap<LexicalEditor, TextEditorComponent[]>}
|
|
527
|
-
*/
|
|
528
|
-
export const editorComponents = new WeakMap();
|
|
529
|
-
|
|
530
|
-
/**
|
|
531
|
-
* Conversion maps of the editor component nodes, cached as each `importDOM()` call can create a new
|
|
532
|
-
* map.
|
|
533
|
-
* @type {WeakMap<any, DOMConversionMap>}
|
|
534
|
-
*/
|
|
535
|
-
const componentConversionMaps = new WeakMap();
|
|
536
|
-
|
|
537
|
-
/**
|
|
538
|
-
* Get the conversion map of the given editor component node.
|
|
539
|
-
* @param {any} node Lexical node class.
|
|
540
|
-
* @returns {DOMConversionMap} Conversion map.
|
|
541
|
-
*/
|
|
542
|
-
const getComponentConversionMap = (node) => {
|
|
543
|
-
let map = componentConversionMaps.get(node);
|
|
544
|
-
|
|
545
|
-
if (!map) {
|
|
546
|
-
map = node.importDOM?.() ?? {};
|
|
547
|
-
componentConversionMaps.set(node, /** @type {DOMConversionMap} */ (map));
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
return /** @type {DOMConversionMap} */ (map);
|
|
551
|
-
};
|
|
552
|
-
|
|
553
|
-
/**
|
|
554
|
-
* Editor component that imports each element, or `null` if none does, cached as the same element is
|
|
555
|
-
* checked many times during an import, and a conversion can run developer code. An element is only
|
|
556
|
-
* imported to one editor, as each import parses the HTML anew.
|
|
557
|
-
* @type {WeakMap<Element, TextEditorComponent | null>}
|
|
558
|
-
*/
|
|
559
|
-
const elementComponents = new WeakMap();
|
|
560
|
-
|
|
561
|
-
/**
|
|
562
|
-
* Get the editor component that imports the given element, if any. The element’s content is then
|
|
563
|
-
* part of the component.
|
|
564
|
-
* @param {LexicalEditor} editor Editor instance.
|
|
565
|
-
* @param {Element} element Element.
|
|
566
|
-
* @returns {TextEditorComponent | undefined} Component.
|
|
567
|
-
*/
|
|
568
|
-
const getElementComponent = (editor, element) => {
|
|
569
|
-
let component = elementComponents.get(element);
|
|
570
|
-
|
|
571
|
-
if (component === undefined) {
|
|
572
|
-
component =
|
|
573
|
-
editorComponents
|
|
574
|
-
.get(editor)
|
|
575
|
-
?.find(
|
|
576
|
-
({ node }) =>
|
|
577
|
-
!!getComponentConversionMap(node)[element.localName]?.(
|
|
578
|
-
/** @type {HTMLElement} */ (element),
|
|
579
|
-
),
|
|
580
|
-
) ?? null;
|
|
581
|
-
elementComponents.set(element, component);
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
return component ?? undefined;
|
|
585
|
-
};
|
|
586
|
-
|
|
587
|
-
/**
|
|
588
|
-
* Check if the given element is imported by an inline editor component, like an image, which is
|
|
589
|
-
* content in the line just like text.
|
|
590
|
-
* @param {Element} element Element.
|
|
591
|
-
* @returns {boolean} Result.
|
|
592
|
-
*/
|
|
593
|
-
const isInlineComponentElement = (element) => {
|
|
594
|
-
const component = getElementComponent(getEditor(), element);
|
|
595
|
-
|
|
596
|
-
// The `node` is actually a node class
|
|
597
|
-
return !!component && !!(/** @type {any} */ (component.node).prototype.isInline?.());
|
|
598
|
-
};
|
|
599
|
-
|
|
600
|
-
/**
|
|
601
|
-
* Find the text or the inline editor component element next to the given text in the same line,
|
|
602
|
-
* like Lexical’s own `findTextInLine()` does for text only. Without the component, Lexical takes an
|
|
603
|
-
* element like an image for the end of the line, or skips it, and drops the space between it and
|
|
604
|
-
* the text, e.g. `a <img> b` becomes `a<img>b`.
|
|
605
|
-
* @param {Node} text Text node.
|
|
606
|
-
* @param {boolean} forward Whether to search forward.
|
|
607
|
-
* @returns {Text | Element | null} Text or element, or `null` if the line ends.
|
|
608
|
-
*/
|
|
609
|
-
const findContentInLine = (text, forward) => {
|
|
610
|
-
let node = text;
|
|
611
|
-
|
|
612
|
-
for (;;) {
|
|
613
|
-
let sibling = forward ? node.nextSibling : node.previousSibling;
|
|
614
|
-
|
|
615
|
-
while (!sibling) {
|
|
616
|
-
const { parentElement } = node;
|
|
617
|
-
|
|
618
|
-
if (!parentElement) {
|
|
619
|
-
return null;
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
node = parentElement;
|
|
623
|
-
sibling = forward ? node.nextSibling : node.previousSibling;
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
node = sibling;
|
|
627
|
-
|
|
628
|
-
if (isHTMLElement(node)) {
|
|
629
|
-
if (isInlineComponentElement(node)) {
|
|
630
|
-
return node;
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
const { display } = node.style;
|
|
634
|
-
|
|
635
|
-
if (display ? !display.startsWith('inline') : !isInlineDomNode(node)) {
|
|
636
|
-
return null;
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
let descendant = forward ? node.firstChild : node.lastChild;
|
|
641
|
-
|
|
642
|
-
while (descendant) {
|
|
643
|
-
node = descendant;
|
|
644
|
-
|
|
645
|
-
if (isHTMLElement(node) && isInlineComponentElement(node)) {
|
|
646
|
-
return node;
|
|
647
|
-
}
|
|
648
|
-
|
|
649
|
-
descendant = forward ? node.firstChild : node.lastChild;
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
if (isDOMTextNode(node)) {
|
|
653
|
-
return node;
|
|
654
|
-
}
|
|
655
|
-
|
|
656
|
-
if (node.nodeName === 'BR') {
|
|
657
|
-
return null;
|
|
658
|
-
}
|
|
659
|
-
}
|
|
660
|
-
};
|
|
661
|
-
|
|
662
|
-
/**
|
|
663
|
-
* Convert a text node, collapsing whitespace like Lexical’s own conversion does, except that an
|
|
664
|
-
* inline editor component element counts as content. See {@link findContentInLine}.
|
|
665
|
-
* @param {Text} domNode Text node.
|
|
666
|
-
* @returns {DOMConversionOutput} Conversion output.
|
|
667
|
-
*/
|
|
668
|
-
const convertTextNode = (domNode) => {
|
|
669
|
-
let textContent = domNode.data.replace(/\r/g, '').replace(/[ \t\n]+/g, ' ');
|
|
670
|
-
|
|
671
|
-
if (textContent.startsWith(' ')) {
|
|
672
|
-
let previous = findContentInLine(domNode, false);
|
|
673
|
-
// Drop the space at the start of a line, or after another space
|
|
674
|
-
let drop = true;
|
|
675
|
-
|
|
676
|
-
for (; previous; previous = findContentInLine(previous, false)) {
|
|
677
|
-
if (!isDOMTextNode(previous)) {
|
|
678
|
-
drop = false;
|
|
679
|
-
break;
|
|
680
|
-
}
|
|
681
|
-
|
|
682
|
-
const previousText = previous.data;
|
|
683
|
-
|
|
684
|
-
// Skip empty text
|
|
685
|
-
if (previousText) {
|
|
686
|
-
drop = /[ \t\n]$/.test(previousText);
|
|
687
|
-
break;
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
|
|
691
|
-
if (drop) {
|
|
692
|
-
textContent = textContent.slice(1);
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
if (textContent.endsWith(' ')) {
|
|
697
|
-
let next = findContentInLine(domNode, true);
|
|
698
|
-
let isEndOfLine = true;
|
|
699
|
-
|
|
700
|
-
// Skip any text that only has whitespace, which is dropped as well
|
|
701
|
-
for (; next; next = findContentInLine(next, true)) {
|
|
702
|
-
if (!isDOMTextNode(next) || next.data.replace(/^[ \t\n]+/, '')) {
|
|
703
|
-
isEndOfLine = false;
|
|
704
|
-
break;
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
if (isEndOfLine) {
|
|
709
|
-
textContent = textContent.slice(0, -1);
|
|
710
|
-
}
|
|
711
|
-
}
|
|
712
|
-
|
|
713
|
-
return { node: textContent ? createTextNode(textContent) : null };
|
|
714
|
-
};
|
|
715
|
-
|
|
716
|
-
/**
|
|
717
|
-
* Additional DOM conversions to import HTML. They take priority over Lexical’s own conversions.
|
|
718
|
-
* @type {DOMConversionMap}
|
|
719
|
-
*/
|
|
720
|
-
export const HTML_IMPORT_MAP = {
|
|
721
|
-
/**
|
|
722
|
-
* Get the conversion for a text node, unless it’s preformatted, which Lexical’s own conversion
|
|
723
|
-
* handles.
|
|
724
|
-
* @param {Node} node Text node.
|
|
725
|
-
* @returns {DOMConversion | null} Conversion.
|
|
726
|
-
*/
|
|
727
|
-
'#text': (node) =>
|
|
728
|
-
isPreformatted(node)
|
|
729
|
-
? null
|
|
730
|
-
: {
|
|
731
|
-
/**
|
|
732
|
-
* Convert the text node.
|
|
733
|
-
* @param {Node} domNode Text node.
|
|
734
|
-
* @returns {DOMConversionOutput} Conversion output.
|
|
735
|
-
*/
|
|
736
|
-
conversion: (domNode) => convertTextNode(/** @type {Text} */ (domNode)),
|
|
737
|
-
priority: 1,
|
|
738
|
-
},
|
|
739
|
-
};
|
|
740
|
-
|
|
741
|
-
/**
|
|
742
|
-
* Additional DOM conversions to import HTML with code blocks.
|
|
743
|
-
* @type {DOMConversionMap}
|
|
744
|
-
*/
|
|
745
|
-
export const HTML_CODE_IMPORT_MAP = {
|
|
746
|
-
/**
|
|
747
|
-
* Get the conversion for a `<pre>` element.
|
|
748
|
-
* @returns {DOMConversion} Conversion.
|
|
749
|
-
*/
|
|
750
|
-
pre: () => ({ conversion: convertPreElement, priority: 2 }),
|
|
751
|
-
};
|
|
752
|
-
|
|
753
|
-
/**
|
|
754
|
-
* Get the names of the elements only editor components import, which no other node handles.
|
|
755
|
-
* @param {LexicalEditor} editor Editor instance.
|
|
756
|
-
* @returns {Set<string>} Tag names.
|
|
757
|
-
*/
|
|
758
|
-
const getComponentOnlyTagNames = (editor) => {
|
|
759
|
-
const components = editorComponents.get(editor) ?? [];
|
|
760
|
-
// The `node` is actually a node class
|
|
761
|
-
const componentNodes = new Set(components.map(({ node }) => /** @type {any} */ (node)));
|
|
762
|
-
|
|
763
|
-
const otherTagNames = new Set(
|
|
764
|
-
[...editor._nodes.values()]
|
|
765
|
-
.filter(({ klass }) => !componentNodes.has(klass))
|
|
766
|
-
.flatMap(({ klass }) => Object.keys(klass.importDOM?.() ?? {})),
|
|
767
|
-
);
|
|
768
|
-
|
|
769
|
-
return new Set(
|
|
770
|
-
components
|
|
771
|
-
.flatMap(({ node }) => Object.keys(getComponentConversionMap(node)))
|
|
772
|
-
.filter((tagName) => !otherTagNames.has(tagName)),
|
|
773
|
-
);
|
|
774
|
-
};
|
|
775
|
-
|
|
776
|
-
/**
|
|
777
|
-
* Get the first node in the given document that the editor cannot import, which would be silently
|
|
778
|
-
* dropped, possibly along with its content:
|
|
779
|
-
*
|
|
780
|
-
* - An element none of the registered nodes, including the ones of editor components, handle. This
|
|
781
|
-
* includes an element the parser moves to `<head>`, like a `<script>` at the beginning, and an
|
|
782
|
-
* element only editor components handle, but none of them imports, like a `<figure>` that isn’t an
|
|
783
|
-
* instance of a component for `<figure class="photo">`, which would be unwrapped.
|
|
784
|
-
* - A comment, like `<!-- more -->`.
|
|
785
|
-
*
|
|
786
|
-
* The content of an element an editor component imports is part of the component, which is
|
|
787
|
-
* expected to drop the children, and the elements within a `<pre>` element are imported as plain
|
|
788
|
-
* text, so they’re ignored.
|
|
789
|
-
* @param {LexicalEditor} editor Editor instance.
|
|
790
|
-
* @param {Document} dom Parsed document.
|
|
791
|
-
* @returns {Element | Comment | undefined} Unsupported node.
|
|
792
|
-
*/
|
|
793
|
-
export const findUnsupportedNode = (editor, dom) => {
|
|
794
|
-
const componentOnlyTagNames = getComponentOnlyTagNames(editor);
|
|
795
|
-
|
|
796
|
-
/**
|
|
797
|
-
* Find an unsupported node among the descendants of the given node.
|
|
798
|
-
* @param {Node} parent Parent node.
|
|
799
|
-
* @returns {Element | Comment | undefined} Unsupported node.
|
|
800
|
-
*/
|
|
801
|
-
const findWithin = (parent) => {
|
|
802
|
-
/** @type {Element | Comment | undefined} */
|
|
803
|
-
let found;
|
|
804
|
-
|
|
805
|
-
[...parent.childNodes].some((node) => {
|
|
806
|
-
if (node.nodeType === Node.COMMENT_NODE) {
|
|
807
|
-
found = /** @type {Comment} */ (node);
|
|
808
|
-
|
|
809
|
-
return true;
|
|
810
|
-
}
|
|
811
|
-
|
|
812
|
-
if (node.nodeType !== Node.ELEMENT_NODE) {
|
|
813
|
-
return false;
|
|
814
|
-
}
|
|
815
|
-
|
|
816
|
-
const element = /** @type {Element} */ (node);
|
|
817
|
-
const tagName = element.localName;
|
|
818
|
-
|
|
819
|
-
if (getElementComponent(editor, element)) {
|
|
820
|
-
return false;
|
|
821
|
-
}
|
|
822
|
-
|
|
823
|
-
// `_htmlConversions` holds the conversions of all the registered nodes, along with the ones
|
|
824
|
-
// in the `html.import` editor config. A conversion may still skip an element on purpose,
|
|
825
|
-
// like a `<br>` at the end of a paragraph, which browsers don’t render
|
|
826
|
-
if (
|
|
827
|
-
!DOCUMENT_TAGS.includes(tagName) &&
|
|
828
|
-
!PASSTHROUGH_TAGS.includes(tagName) &&
|
|
829
|
-
(!editor._htmlConversions.has(tagName) || componentOnlyTagNames.has(tagName))
|
|
830
|
-
) {
|
|
831
|
-
found = element;
|
|
832
|
-
|
|
833
|
-
return true;
|
|
834
|
-
}
|
|
835
|
-
|
|
836
|
-
found =
|
|
837
|
-
tagName === 'pre'
|
|
838
|
-
? /** @type {Comment | undefined} */ (
|
|
839
|
-
dom.createTreeWalker(element, NodeFilter.SHOW_COMMENT).nextNode() ?? undefined
|
|
840
|
-
)
|
|
841
|
-
: findWithin(element);
|
|
842
|
-
|
|
843
|
-
return !!found;
|
|
844
|
-
});
|
|
845
|
-
|
|
846
|
-
return found;
|
|
847
|
-
};
|
|
848
|
-
|
|
849
|
-
return findWithin(dom);
|
|
850
|
-
};
|
|
851
|
-
|
|
852
|
-
/**
|
|
853
|
-
* Convert the editor content to HTML. Call this within an editor update or read.
|
|
854
|
-
* @param {LexicalEditor} editor Editor instance.
|
|
855
|
-
* @returns {string} HTML value.
|
|
856
|
-
*/
|
|
857
|
-
export const exportHtml = (editor) => generateHtmlFromNodes(editor);
|