@sveltia/ui 0.79.4 → 0.80.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/emoji/position.d.ts +2 -1
- package/dist/components/emoji/position.js +11 -3
- package/dist/components/text-editor/constants.d.ts +9 -1
- package/dist/components/text-editor/constants.js +10 -1
- package/dist/components/text-editor/core.d.ts +4 -2
- package/dist/components/text-editor/core.js +152 -33
- package/dist/components/text-editor/floating-link-editor.svelte +685 -0
- package/dist/components/text-editor/floating-link-editor.svelte.d.ts +17 -0
- package/dist/components/text-editor/html.d.ts +18 -0
- package/dist/components/text-editor/html.js +407 -0
- package/dist/components/text-editor/shiki/version.d.ts +1 -1
- package/dist/components/text-editor/shiki/version.js +1 -1
- package/dist/components/text-editor/store.svelte.js +12 -13
- package/dist/components/text-editor/text-editor.svelte +43 -17
- package/dist/components/text-editor/text-editor.svelte.d.ts +23 -6
- package/dist/components/text-editor/toolbar/insert-link-button.svelte +14 -71
- package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte +12 -5
- package/dist/locales/ar.yaml +5 -1
- package/dist/locales/bg.yaml +5 -1
- package/dist/locales/ca.yaml +5 -1
- package/dist/locales/cs.yaml +5 -1
- package/dist/locales/da.yaml +5 -1
- package/dist/locales/de.yaml +5 -1
- package/dist/locales/el.yaml +5 -1
- package/dist/locales/en-CA.yaml +5 -1
- package/dist/locales/en-GB.yaml +5 -1
- package/dist/locales/en-US.yaml +5 -1
- package/dist/locales/es-CO.yaml +5 -1
- package/dist/locales/fa.yaml +5 -1
- package/dist/locales/fi.yaml +5 -1
- package/dist/locales/fr.yaml +5 -1
- package/dist/locales/he.yaml +5 -1
- package/dist/locales/hr.yaml +5 -1
- package/dist/locales/it.yaml +5 -1
- package/dist/locales/ja.yaml +5 -1
- package/dist/locales/ko.yaml +5 -1
- package/dist/locales/nl.yaml +5 -1
- package/dist/locales/pl.yaml +5 -1
- package/dist/locales/pt-BR.yaml +5 -1
- package/dist/locales/pt-PT.yaml +5 -1
- package/dist/locales/ru.yaml +5 -1
- package/dist/locales/sk.yaml +5 -1
- package/dist/locales/sv.yaml +5 -1
- package/dist/locales/tr.yaml +5 -1
- package/dist/locales/uk.yaml +5 -1
- package/dist/locales/vi.yaml +5 -1
- package/dist/locales/zh-CN.yaml +5 -1
- package/dist/locales/zh-TW.yaml +5 -1
- package/dist/typedefs.d.ts +13 -7
- package/dist/typedefs.js +12 -6
- package/package.json +5 -4
|
@@ -14,7 +14,7 @@ export const LIST_WIDTH: 280;
|
|
|
14
14
|
*/
|
|
15
15
|
export const LIST_OFFSET: 4;
|
|
16
16
|
export const VIEWPORT_MARGIN: 8;
|
|
17
|
-
export function getSuggestionListPosition({ anchorRect, listMaxHeight, viewport, rtl }: {
|
|
17
|
+
export function getSuggestionListPosition({ anchorRect, listMaxHeight, viewport, rtl, listWidth, }: {
|
|
18
18
|
anchorRect: EmojiAnchorRect;
|
|
19
19
|
listMaxHeight: number;
|
|
20
20
|
viewport: {
|
|
@@ -22,6 +22,7 @@ export function getSuggestionListPosition({ anchorRect, listMaxHeight, viewport,
|
|
|
22
22
|
height: number;
|
|
23
23
|
};
|
|
24
24
|
rtl: boolean;
|
|
25
|
+
listWidth?: number | undefined;
|
|
25
26
|
}): SuggestionListPosition;
|
|
26
27
|
export type SuggestionListPosition = {
|
|
27
28
|
/**
|
|
@@ -38,20 +38,28 @@ export const VIEWPORT_MARGIN = 8;
|
|
|
38
38
|
* @param {number} options.listMaxHeight The height the list would like to have, in pixels.
|
|
39
39
|
* @param {{ width: number, height: number }} options.viewport Size of the viewport in pixels.
|
|
40
40
|
* @param {boolean} options.rtl Whether the layout is right-to-left.
|
|
41
|
+
* @param {number} [options.listWidth] Width of the dropdown in pixels, for another popup placed
|
|
42
|
+
* the same way.
|
|
41
43
|
* @returns {SuggestionListPosition} Position.
|
|
42
44
|
*/
|
|
43
|
-
export const getSuggestionListPosition = ({
|
|
45
|
+
export const getSuggestionListPosition = ({
|
|
46
|
+
anchorRect,
|
|
47
|
+
listMaxHeight,
|
|
48
|
+
viewport,
|
|
49
|
+
rtl,
|
|
50
|
+
listWidth = LIST_WIDTH,
|
|
51
|
+
}) => {
|
|
44
52
|
const { width: innerWidth, height: innerHeight } = viewport;
|
|
45
53
|
const spaceBelow = innerHeight - anchorRect.bottom;
|
|
46
54
|
const spaceAbove = anchorRect.top;
|
|
47
55
|
const flipped = spaceBelow < listMaxHeight + VIEWPORT_MARGIN && spaceAbove > spaceBelow;
|
|
48
|
-
const anchorLeft = rtl ? anchorRect.right -
|
|
56
|
+
const anchorLeft = rtl ? anchorRect.right - listWidth : anchorRect.left;
|
|
49
57
|
|
|
50
58
|
return {
|
|
51
59
|
top: flipped ? undefined : `${Math.round(anchorRect.bottom + LIST_OFFSET)}px`,
|
|
52
60
|
bottom: flipped ? `${Math.round(innerHeight - anchorRect.top + LIST_OFFSET)}px` : undefined,
|
|
53
61
|
left: `${Math.round(
|
|
54
|
-
Math.max(VIEWPORT_MARGIN, Math.min(anchorLeft, innerWidth -
|
|
62
|
+
Math.max(VIEWPORT_MARGIN, Math.min(anchorLeft, innerWidth - listWidth - VIEWPORT_MARGIN)),
|
|
55
63
|
)}px`,
|
|
56
64
|
maxHeight: `${Math.round(
|
|
57
65
|
Math.min(listMaxHeight, (flipped ? spaceAbove : spaceBelow) - LIST_OFFSET - VIEWPORT_MARGIN),
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @import { EditorThemeClasses } from 'lexical';
|
|
2
|
+
* @import { EditorThemeClasses, LexicalCommand } from 'lexical';
|
|
3
3
|
* @import { Transformer } from '@lexical/markdown';
|
|
4
4
|
* @import {
|
|
5
5
|
* TextEditorBlockType,
|
|
@@ -61,9 +61,17 @@ export const DISABLED_MARKDOWN_TAGS: string[];
|
|
|
61
61
|
* change made by the user.
|
|
62
62
|
*/
|
|
63
63
|
export const IMPORT_UPDATE_TAG: "sui-import";
|
|
64
|
+
/**
|
|
65
|
+
* Command to show the floating link editor for the link at the selection, or for the selected text
|
|
66
|
+
* to be linked. A listener returns `false` when there is no link or text to edit, in which case the
|
|
67
|
+
* Link button falls back to the dialog.
|
|
68
|
+
* @type {LexicalCommand<void>}
|
|
69
|
+
*/
|
|
70
|
+
export const OPEN_LINK_EDITOR_COMMAND: LexicalCommand<void>;
|
|
64
71
|
import type { EditorThemeClasses } from 'lexical';
|
|
65
72
|
import type { TextEditorFormatType } from '../../typedefs.js';
|
|
66
73
|
import type { TextEditorInlineType } from '../../typedefs.js';
|
|
67
74
|
import type { TextEditorBlockType } from '../../typedefs.js';
|
|
68
75
|
import type { TextEditorNodeType } from '../../typedefs.js';
|
|
69
76
|
import type { Transformer } from '@lexical/markdown';
|
|
77
|
+
import type { LexicalCommand } from 'lexical';
|
|
@@ -18,9 +18,10 @@ import {
|
|
|
18
18
|
UNORDERED_LIST,
|
|
19
19
|
} from '@lexical/markdown';
|
|
20
20
|
import { HeadingNode, QuoteNode } from '@lexical/rich-text';
|
|
21
|
+
import { createCommand } from 'lexical';
|
|
21
22
|
|
|
22
23
|
/**
|
|
23
|
-
* @import { EditorThemeClasses } from 'lexical';
|
|
24
|
+
* @import { EditorThemeClasses, LexicalCommand } from 'lexical';
|
|
24
25
|
* @import { Transformer } from '@lexical/markdown';
|
|
25
26
|
* @import {
|
|
26
27
|
* TextEditorBlockType,
|
|
@@ -237,3 +238,11 @@ export const DISABLED_MARKDOWN_TAGS = ['*', '__', '***', '___'];
|
|
|
237
238
|
* change made by the user.
|
|
238
239
|
*/
|
|
239
240
|
export const IMPORT_UPDATE_TAG = 'sui-import';
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Command to show the floating link editor for the link at the selection, or for the selected text
|
|
244
|
+
* to be linked. A listener returns `false` when there is no link or text to edit, in which case the
|
|
245
|
+
* Link button falls back to the dialog.
|
|
246
|
+
* @type {LexicalCommand<void>}
|
|
247
|
+
*/
|
|
248
|
+
export const OPEN_LINK_EDITOR_COMMAND = createCommand('OPEN_LINK_EDITOR_COMMAND');
|
|
@@ -3,11 +3,12 @@ export function handleEditorMouseDown(editor: LexicalEditor, event: MouseEvent):
|
|
|
3
3
|
export function isSafeLinkURL(url: string): boolean;
|
|
4
4
|
export function getSelectionTypes(): TextEditorSelectionState;
|
|
5
5
|
export function exportMarkdown(enabledTransformers: Transformer[]): string;
|
|
6
|
-
export function onEditorUpdate(editor: LexicalEditor, enabledTransformers: Transformer[], cachedValue?: string): string;
|
|
6
|
+
export function onEditorUpdate(editor: LexicalEditor, enabledTransformers: Transformer[], cachedValue?: string, format?: TextEditorValueFormat): string;
|
|
7
7
|
export function getComponentMarkdown({ node, createNode, transformer, createMarkdown }: TextEditorComponent): string;
|
|
8
|
-
export function initEditor({ enabledButtons, components, useMarkdownShortcuts, isCodeEditor, defaultLanguage, }: TextEditorConfig): InitEditorResult;
|
|
8
|
+
export function initEditor({ enabledButtons, components, useMarkdownShortcuts, isCodeEditor, defaultLanguage, format, }: TextEditorConfig): InitEditorResult;
|
|
9
9
|
export function loadCodeHighlighter(lang: string): Promise<void>;
|
|
10
10
|
export function convertMarkdownToLexical(editor: LexicalEditor, value: string, enabledTransformers: Transformer[]): Promise<string | undefined>;
|
|
11
|
+
export function convertHtmlToLexical(editor: LexicalEditor, value: string): Promise<string | undefined>;
|
|
11
12
|
export function focusEditor(editor: LexicalEditor): Promise<void>;
|
|
12
13
|
export type InitEditorResult = {
|
|
13
14
|
/**
|
|
@@ -26,5 +27,6 @@ export type InitEditorResult = {
|
|
|
26
27
|
import type { LexicalEditor } from 'lexical';
|
|
27
28
|
import type { TextEditorSelectionState } from '../../typedefs.js';
|
|
28
29
|
import type { Transformer } from '@lexical/markdown';
|
|
30
|
+
import type { TextEditorValueFormat } from '../../typedefs.js';
|
|
29
31
|
import type { TextEditorComponent } from '../../typedefs.js';
|
|
30
32
|
import type { TextEditorConfig } from '../../typedefs.js';
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
import { registerDragonSupport } from '@lexical/dragon';
|
|
10
10
|
import { buildEditorFromExtensions, HorizontalRuleNode } from '@lexical/extension';
|
|
11
11
|
import { createEmptyHistoryState, registerHistory } from '@lexical/history';
|
|
12
|
+
import { $generateNodesFromDOM as generateNodesFromDOM } from '@lexical/html';
|
|
12
13
|
import {
|
|
13
14
|
$isLinkNode as isLinkNode,
|
|
14
15
|
TOGGLE_LINK_COMMAND,
|
|
@@ -72,6 +73,13 @@ import {
|
|
|
72
73
|
TEXT_FORMAT_BUTTON_TYPES,
|
|
73
74
|
TRANSFORMER_MAP,
|
|
74
75
|
} from './constants.js';
|
|
76
|
+
import {
|
|
77
|
+
exportHtml,
|
|
78
|
+
findUnsupportedNode,
|
|
79
|
+
getCodeLanguage,
|
|
80
|
+
HTML_EXPORT_MAP,
|
|
81
|
+
HTML_IMPORT_MAP,
|
|
82
|
+
} from './html.js';
|
|
75
83
|
import {
|
|
76
84
|
increaseListIndentation,
|
|
77
85
|
padBlankBlockquoteLines,
|
|
@@ -101,6 +109,7 @@ import { TABLE } from './transformers/table.js';
|
|
|
101
109
|
* TextEditorConfig,
|
|
102
110
|
* TextEditorInlineType,
|
|
103
111
|
* TextEditorNodeType,
|
|
112
|
+
* TextEditorValueFormat,
|
|
104
113
|
* TextEditorSelectionState,
|
|
105
114
|
* } from '../../typedefs.js';
|
|
106
115
|
*/
|
|
@@ -487,12 +496,14 @@ export const exportMarkdown = (enabledTransformers) => {
|
|
|
487
496
|
* @internal
|
|
488
497
|
* @param {LexicalEditor} editor Editor instance.
|
|
489
498
|
* @param {Transformer[]} enabledTransformers Enabled Markdown transformers.
|
|
490
|
-
* @param {string} [cachedValue]
|
|
491
|
-
*
|
|
492
|
-
* @
|
|
499
|
+
* @param {string} [cachedValue] Value from a previous call, to be reused instead of converting the
|
|
500
|
+
* whole document again when only the selection has changed.
|
|
501
|
+
* @param {TextEditorValueFormat} [format] Value format.
|
|
502
|
+
* @returns {string} Markdown or HTML value.
|
|
493
503
|
*/
|
|
494
|
-
export const onEditorUpdate = (editor, enabledTransformers, cachedValue) => {
|
|
495
|
-
const value =
|
|
504
|
+
export const onEditorUpdate = (editor, enabledTransformers, cachedValue, format = 'markdown') => {
|
|
505
|
+
const value =
|
|
506
|
+
cachedValue ?? (format === 'html' ? exportHtml(editor) : exportMarkdown(enabledTransformers));
|
|
496
507
|
|
|
497
508
|
editor.getRootElement()?.dispatchEvent(
|
|
498
509
|
new CustomEvent('Update', {
|
|
@@ -750,13 +761,14 @@ const registerListCommands = (editor, enabledButtons = []) => {
|
|
|
750
761
|
};
|
|
751
762
|
|
|
752
763
|
/**
|
|
753
|
-
* Register an update listener that triggers the Update event with the Markdown value,
|
|
754
|
-
* only the latest update in a burst triggers it.
|
|
764
|
+
* Register an update listener that triggers the Update event with the Markdown or HTML value,
|
|
765
|
+
* debounced so only the latest update in a burst triggers it.
|
|
755
766
|
* @param {LexicalEditor} editor Editor instance.
|
|
756
767
|
* @param {Transformer[]} enabledTransformers Enabled Markdown transformers.
|
|
768
|
+
* @param {TextEditorValueFormat} format Value format.
|
|
757
769
|
* @returns {() => void} Cleanup handler.
|
|
758
770
|
*/
|
|
759
|
-
const registerUpdateEvent = (editor, enabledTransformers) => {
|
|
771
|
+
const registerUpdateEvent = (editor, enabledTransformers, format) => {
|
|
760
772
|
/** Incremented on every update, so only the latest one in a burst triggers the Update event. */
|
|
761
773
|
let updateCount = 0;
|
|
762
774
|
/** Whether the content has changed since the Update event was last triggered. */
|
|
@@ -796,6 +808,7 @@ const registerUpdateEvent = (editor, enabledTransformers) => {
|
|
|
796
808
|
editor,
|
|
797
809
|
enabledTransformers,
|
|
798
810
|
contentChanged ? undefined : lastValue,
|
|
811
|
+
format,
|
|
799
812
|
);
|
|
800
813
|
contentChanged = false;
|
|
801
814
|
});
|
|
@@ -863,6 +876,7 @@ export const initEditor = ({
|
|
|
863
876
|
useMarkdownShortcuts,
|
|
864
877
|
isCodeEditor = false,
|
|
865
878
|
defaultLanguage = 'plain',
|
|
879
|
+
format = 'markdown',
|
|
866
880
|
}) => {
|
|
867
881
|
const hasCodeBlock = enabledButtons.includes('code-block') || isCodeEditor;
|
|
868
882
|
|
|
@@ -890,6 +904,12 @@ export const initEditor = ({
|
|
|
890
904
|
[HorizontalRuleNode, TableNode, TableCellNode, TableRowNode]),
|
|
891
905
|
],
|
|
892
906
|
theme: EDITOR_THEME,
|
|
907
|
+
// Export clean HTML without the attributes and elements only meant for the editor, and import
|
|
908
|
+
// code blocks with their language. The export map also applies to copying
|
|
909
|
+
html:
|
|
910
|
+
format === 'html'
|
|
911
|
+
? { export: HTML_EXPORT_MAP, import: hasCodeBlock ? HTML_IMPORT_MAP : {} }
|
|
912
|
+
: undefined,
|
|
893
913
|
/**
|
|
894
914
|
* Log an error like `createEditor` does by default, instead of throwing it.
|
|
895
915
|
* @param {Error} error Error.
|
|
@@ -941,7 +961,7 @@ export const initEditor = ({
|
|
|
941
961
|
hasCodeBlock ? observeCodeTheme(editor) : undefined,
|
|
942
962
|
enabledButtons.includes('link') ? registerLinkCommands(editor) : undefined,
|
|
943
963
|
registerListCommands(editor, enabledButtons),
|
|
944
|
-
registerUpdateEvent(editor, enabledTransformers),
|
|
964
|
+
registerUpdateEvent(editor, enabledTransformers, format),
|
|
945
965
|
registerTabIndentation(editor),
|
|
946
966
|
);
|
|
947
967
|
|
|
@@ -977,22 +997,26 @@ export const loadCodeHighlighter = async (lang) => {
|
|
|
977
997
|
};
|
|
978
998
|
|
|
979
999
|
/**
|
|
980
|
-
* Number of the latest
|
|
1000
|
+
* Number of the latest import started for each editor.
|
|
981
1001
|
* @type {WeakMap<LexicalEditor, number>}
|
|
982
1002
|
*/
|
|
983
1003
|
const latestImports = new WeakMap();
|
|
984
1004
|
|
|
985
1005
|
/**
|
|
986
|
-
*
|
|
1006
|
+
* Import a value to the editor, replacing the current content.
|
|
987
1007
|
* @param {LexicalEditor} editor Editor instance.
|
|
988
|
-
* @param {
|
|
989
|
-
* @param {
|
|
1008
|
+
* @param {object} args Arguments.
|
|
1009
|
+
* @param {string[]} args.languages Programming languages used in the code blocks.
|
|
1010
|
+
* @param {() => void} args.$import Function to convert the value to Lexical nodes, called within an
|
|
1011
|
+
* editor update.
|
|
1012
|
+
* @param {() => string} args.$export Function to convert the editor content back to the value.
|
|
1013
|
+
* @param {string} args.formatName Name of the value format, used in the error message.
|
|
990
1014
|
* @returns {Promise<string | undefined>} The value as the editor exports it once imported, which
|
|
991
|
-
* can differ in style from the given value
|
|
992
|
-
*
|
|
1015
|
+
* can differ in style from the given value. `undefined` if another import has been started for the
|
|
1016
|
+
* same editor in the meantime, in which case this value is skipped.
|
|
993
1017
|
* @throws {Error} Failed to convert the value to Lexical nodes.
|
|
994
1018
|
*/
|
|
995
|
-
|
|
1019
|
+
const importValue = async (editor, { languages, $import, $export, formatName }) => {
|
|
996
1020
|
const importId = (latestImports.get(editor) ?? 0) + 1;
|
|
997
1021
|
|
|
998
1022
|
latestImports.set(editor, importId);
|
|
@@ -1008,11 +1032,7 @@ export const convertMarkdownToLexical = async (editor, value, enabledTransformer
|
|
|
1008
1032
|
try {
|
|
1009
1033
|
// Preload the highlighter for every language used in the document, so code blocks are
|
|
1010
1034
|
// highlighted as soon as they appear rather than a moment later
|
|
1011
|
-
await Promise.all(
|
|
1012
|
-
[...value.matchAll(/^```(?<lang>.+?)\n/gm)].map(async ({ groups: { lang = 'plain' } = {} }) =>
|
|
1013
|
-
loadCodeHighlighter(lang),
|
|
1014
|
-
),
|
|
1015
|
-
);
|
|
1035
|
+
await Promise.all(languages.map(async (lang) => loadCodeHighlighter(lang)));
|
|
1016
1036
|
} catch (ex) {
|
|
1017
1037
|
if (isSuperseded()) {
|
|
1018
1038
|
return undefined;
|
|
@@ -1025,15 +1045,6 @@ export const convertMarkdownToLexical = async (editor, value, enabledTransformer
|
|
|
1025
1045
|
return undefined;
|
|
1026
1046
|
}
|
|
1027
1047
|
|
|
1028
|
-
// Split multiline formatting into separate lines to prevent Markdown parsing issues
|
|
1029
|
-
value = splitMultilineFormatting(value);
|
|
1030
|
-
|
|
1031
|
-
// Increase list indentation levels to prevent Markdown parsing issues
|
|
1032
|
-
value = increaseListIndentation(value);
|
|
1033
|
-
|
|
1034
|
-
// Pad blank blockquote lines so they are not imported as literal `>` text
|
|
1035
|
-
value = padBlankBlockquoteLines(value);
|
|
1036
|
-
|
|
1037
1048
|
// Lexical commits an empty state when the root element is attached, and the history keeps it as
|
|
1038
1049
|
// the current entry. An empty state can’t be restored, so undoing to it throws an error. Merge
|
|
1039
1050
|
// the first import into that entry instead of pushing it onto the undo stack
|
|
@@ -1044,7 +1055,7 @@ export const convertMarkdownToLexical = async (editor, value, enabledTransformer
|
|
|
1044
1055
|
editor.update(
|
|
1045
1056
|
() => {
|
|
1046
1057
|
try {
|
|
1047
|
-
|
|
1058
|
+
$import();
|
|
1048
1059
|
} catch (ex) {
|
|
1049
1060
|
error = ex;
|
|
1050
1061
|
}
|
|
@@ -1056,10 +1067,118 @@ export const convertMarkdownToLexical = async (editor, value, enabledTransformer
|
|
|
1056
1067
|
);
|
|
1057
1068
|
|
|
1058
1069
|
if (error) {
|
|
1059
|
-
throw new Error(
|
|
1070
|
+
throw new Error(`Failed to convert ${formatName}`, { cause: error });
|
|
1060
1071
|
}
|
|
1061
1072
|
|
|
1062
|
-
return editor.read(
|
|
1073
|
+
return editor.read($export);
|
|
1074
|
+
};
|
|
1075
|
+
|
|
1076
|
+
/**
|
|
1077
|
+
* Convert Markdown to Lexical nodes.
|
|
1078
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
1079
|
+
* @param {string} value Current Markdown value.
|
|
1080
|
+
* @param {Transformer[]} enabledTransformers List of enabled Markdown transformers.
|
|
1081
|
+
* @returns {Promise<string | undefined>} The value as the editor exports it once imported, which
|
|
1082
|
+
* can differ in style from the given value, e.g. `_text_` for `*text*`. `undefined` if another
|
|
1083
|
+
* import has been started for the same editor in the meantime, in which case this value is skipped.
|
|
1084
|
+
* @throws {Error} Failed to convert the value to Lexical nodes.
|
|
1085
|
+
*/
|
|
1086
|
+
export const convertMarkdownToLexical = async (editor, value, enabledTransformers) =>
|
|
1087
|
+
importValue(editor, {
|
|
1088
|
+
languages: [...value.matchAll(/^```(?<lang>.+?)\n/gm)].map(
|
|
1089
|
+
({ groups: { lang = 'plain' } = {} }) => lang,
|
|
1090
|
+
),
|
|
1091
|
+
/**
|
|
1092
|
+
* Import the Markdown.
|
|
1093
|
+
*/
|
|
1094
|
+
$import: () => {
|
|
1095
|
+
convertFromMarkdownString(
|
|
1096
|
+
// Split multiline formatting into separate lines to prevent Markdown parsing issues, then
|
|
1097
|
+
// increase list indentation levels to prevent Markdown parsing issues, and pad blank
|
|
1098
|
+
// blockquote lines so they are not imported as literal `>` text
|
|
1099
|
+
padBlankBlockquoteLines(increaseListIndentation(splitMultilineFormatting(value))),
|
|
1100
|
+
enabledTransformers,
|
|
1101
|
+
);
|
|
1102
|
+
},
|
|
1103
|
+
/**
|
|
1104
|
+
* Export the content as Markdown.
|
|
1105
|
+
* @returns {string} Markdown.
|
|
1106
|
+
*/
|
|
1107
|
+
$export: () => exportMarkdown(enabledTransformers),
|
|
1108
|
+
formatName: 'Markdown',
|
|
1109
|
+
});
|
|
1110
|
+
|
|
1111
|
+
/**
|
|
1112
|
+
* Convert HTML to Lexical nodes. Inline content outside any block, like bare text, is wrapped with
|
|
1113
|
+
* a paragraph. An element that none of the registered nodes can import, like `<img>` or a heading
|
|
1114
|
+
* with the heading buttons disabled, as well as a comment, causes an error instead of being
|
|
1115
|
+
* silently dropped.
|
|
1116
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
1117
|
+
* @param {string} value Current HTML value.
|
|
1118
|
+
* @returns {Promise<string | undefined>} The value as the editor exports it once imported, which
|
|
1119
|
+
* can differ in style from the given value, e.g. `<strong>` for `<b>`. `undefined` if another
|
|
1120
|
+
* import has been started for the same editor in the meantime, in which case this value is skipped.
|
|
1121
|
+
* @throws {Error} Failed to convert the value to Lexical nodes.
|
|
1122
|
+
*/
|
|
1123
|
+
export const convertHtmlToLexical = async (editor, value) => {
|
|
1124
|
+
const dom = new DOMParser().parseFromString(value, 'text/html');
|
|
1125
|
+
|
|
1126
|
+
return importValue(editor, {
|
|
1127
|
+
languages: [...dom.querySelectorAll('pre > code')]
|
|
1128
|
+
.map(getCodeLanguage)
|
|
1129
|
+
.filter((lang) => lang !== undefined),
|
|
1130
|
+
/**
|
|
1131
|
+
* Import the HTML.
|
|
1132
|
+
* @throws {Error} The HTML has an element the editor cannot handle.
|
|
1133
|
+
*/
|
|
1134
|
+
$import: () => {
|
|
1135
|
+
const unsupportedNode = findUnsupportedNode(editor, dom);
|
|
1136
|
+
|
|
1137
|
+
if (unsupportedNode) {
|
|
1138
|
+
throw new Error(
|
|
1139
|
+
unsupportedNode.nodeType === Node.COMMENT_NODE
|
|
1140
|
+
? 'Unsupported comment'
|
|
1141
|
+
: `Unsupported element: <${/** @type {Element} */ (unsupportedNode).localName}>`,
|
|
1142
|
+
);
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
const root = getRoot();
|
|
1146
|
+
/** @type {ElementNode | null} */
|
|
1147
|
+
let paragraph = null;
|
|
1148
|
+
|
|
1149
|
+
root.clear();
|
|
1150
|
+
|
|
1151
|
+
generateNodesFromDOM(editor, dom).forEach((node) => {
|
|
1152
|
+
if (isElementNode(node) ? !node.isInline() : isDecoratorNode(node) && !node.isInline()) {
|
|
1153
|
+
paragraph = null;
|
|
1154
|
+
root.append(node);
|
|
1155
|
+
} else {
|
|
1156
|
+
if (!paragraph) {
|
|
1157
|
+
paragraph = createParagraphNode();
|
|
1158
|
+
root.append(paragraph);
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
paragraph.append(node);
|
|
1162
|
+
}
|
|
1163
|
+
});
|
|
1164
|
+
|
|
1165
|
+
// Like the Markdown import, there should be at least one paragraph
|
|
1166
|
+
// @see https://github.com/facebook/lexical/issues/2308
|
|
1167
|
+
if (root.isEmpty()) {
|
|
1168
|
+
root.append(createParagraphNode());
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
if (getSelection() !== null) {
|
|
1172
|
+
root.selectStart();
|
|
1173
|
+
}
|
|
1174
|
+
},
|
|
1175
|
+
/**
|
|
1176
|
+
* Export the content as HTML.
|
|
1177
|
+
* @returns {string} HTML.
|
|
1178
|
+
*/
|
|
1179
|
+
$export: () => exportHtml(editor),
|
|
1180
|
+
formatName: 'HTML',
|
|
1181
|
+
});
|
|
1063
1182
|
};
|
|
1064
1183
|
|
|
1065
1184
|
/**
|