@sveltia/ui 0.87.3 → 0.88.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/text-editor/code-editor.svelte +0 -5
- package/dist/components/text-editor/core.d.ts +8 -1
- package/dist/components/text-editor/core.js +358 -26
- package/dist/components/text-editor/lexical-root.svelte +7 -3
- package/dist/components/text-editor/markdown.js +149 -8
- package/dist/components/text-editor/raw-markdown.d.ts +8 -0
- package/dist/components/text-editor/raw-markdown.js +85 -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 +3 -3
- package/dist/components/text-editor/text-editor.svelte +44 -6
- package/dist/components/text-editor/text-editor.svelte.d.ts +28 -0
- package/dist/components/text-editor/toolbar/insert-component.d.ts +1 -2
- package/dist/components/text-editor/toolbar/insert-component.js +0 -9
- package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte +1 -1
- package/dist/components/text-editor/transformers/block-escape.js +19 -24
- package/dist/components/text-editor/transformers/raw-syntax.d.ts +33 -0
- package/dist/components/text-editor/transformers/raw-syntax.js +85 -1
- package/dist/components/text-editor/transformers/table.js +8 -2
- package/dist/components/text-field/text-area.svelte +4 -4
- package/dist/shiki-engine.js +5 -5
- package/dist/typedefs.d.ts +5 -0
- package/dist/typedefs.js +2 -0
- package/package.json +8 -8
|
@@ -160,12 +160,7 @@
|
|
|
160
160
|
|
|
161
161
|
<style>.code-editor {
|
|
162
162
|
margin: var(--sui-focus-ring-width);
|
|
163
|
-
border-radius: var(--sui-textbox-border-radius);
|
|
164
163
|
width: calc(100% - var(--sui-focus-ring-width) * 2);
|
|
165
|
-
transition: all 200ms;
|
|
166
|
-
}
|
|
167
|
-
.code-editor:focus-within {
|
|
168
|
-
outline: var(--sui-focus-ring-width) solid var(--sui-focus-ring-color);
|
|
169
164
|
}
|
|
170
165
|
.code-editor.flex:not([hidden]) {
|
|
171
166
|
display: block;
|
|
@@ -5,7 +5,12 @@ export function getSelectionTypes(): TextEditorSelectionState;
|
|
|
5
5
|
export function exportMarkdown(enabledTransformers: Transformer[]): string;
|
|
6
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
|
|
8
|
+
export function isInlineComponent({ transformer }: TextEditorComponent): boolean;
|
|
9
|
+
export function limitToSingleLine(config: TextEditorConfig): TextEditorConfig;
|
|
10
|
+
export function $joinIntoSingleParagraph(root: RootNode): void;
|
|
11
|
+
export function $replaceLineBreak(node: LineBreakNode): void;
|
|
12
|
+
export function removePlaceholdersFromMatch(transformer: Transformer): Transformer;
|
|
13
|
+
export function initEditor(config: TextEditorConfig): InitEditorResult;
|
|
9
14
|
export function loadCodeHighlighter(lang: string): Promise<void>;
|
|
10
15
|
export function convertMarkdownToLexical(editor: LexicalEditor, value: string, enabledTransformers: Transformer[]): Promise<string | undefined>;
|
|
11
16
|
export function convertHtmlToLexical(editor: LexicalEditor, value: string): Promise<string | undefined>;
|
|
@@ -36,4 +41,6 @@ import type { Transformer } from '@lexical/markdown';
|
|
|
36
41
|
import type { TextEditorValueFormat } from '../../typedefs.js';
|
|
37
42
|
import type { TextEditorComponent } from '../../typedefs.js';
|
|
38
43
|
import type { TextEditorConfig } from '../../typedefs.js';
|
|
44
|
+
import { RootNode } from 'lexical';
|
|
45
|
+
import { LineBreakNode } from 'lexical';
|
|
39
46
|
import type { LinkAttributes } from '@lexical/link';
|
|
@@ -49,6 +49,7 @@ import {
|
|
|
49
49
|
import { sleep } from '@sveltia/utils/misc';
|
|
50
50
|
import { isURL } from '@sveltia/utils/string';
|
|
51
51
|
import {
|
|
52
|
+
COMMAND_PRIORITY_HIGH,
|
|
52
53
|
COMMAND_PRIORITY_LOW,
|
|
53
54
|
COMMAND_PRIORITY_NORMAL,
|
|
54
55
|
configExtension,
|
|
@@ -63,11 +64,14 @@ import {
|
|
|
63
64
|
$getSelection as getSelection,
|
|
64
65
|
HISTORY_MERGE_TAG,
|
|
65
66
|
INDENT_CONTENT_COMMAND,
|
|
67
|
+
INSERT_LINE_BREAK_COMMAND,
|
|
66
68
|
INSERT_PARAGRAPH_COMMAND,
|
|
67
69
|
KEY_ENTER_COMMAND,
|
|
70
|
+
LineBreakNode,
|
|
68
71
|
$insertNodes as insertNodes,
|
|
69
72
|
$isDecoratorNode as isDecoratorNode,
|
|
70
73
|
$isElementNode as isElementNode,
|
|
74
|
+
$isParagraphNode as isParagraphNode,
|
|
71
75
|
$isRangeSelection as isRangeSelection,
|
|
72
76
|
$isTextNode as isTextNode,
|
|
73
77
|
OUTDENT_CONTENT_COMMAND,
|
|
@@ -79,6 +83,7 @@ import {
|
|
|
79
83
|
DISABLED_MARKDOWN_TAGS,
|
|
80
84
|
EDITOR_THEME,
|
|
81
85
|
IMPORT_UPDATE_TAG,
|
|
86
|
+
INLINE_BUTTON_TYPES,
|
|
82
87
|
NODE_MAP,
|
|
83
88
|
TEXT_FORMAT_BUTTON_TYPES,
|
|
84
89
|
TRANSFORMER_MAP,
|
|
@@ -114,7 +119,13 @@ import { BLOCK_SYNTAX_ESCAPE } from './transformers/block-escape.js';
|
|
|
114
119
|
import { CODE } from './transformers/code.js';
|
|
115
120
|
import { addCodeSpanSplit } from './transformers/formatted-code.js';
|
|
116
121
|
import { HR } from './transformers/hr.js';
|
|
117
|
-
import {
|
|
122
|
+
import {
|
|
123
|
+
getImportedRawSyntax,
|
|
124
|
+
HTML_BLOCK_PLACEHOLDER,
|
|
125
|
+
INLINE_HTML_REGEX,
|
|
126
|
+
RAW_HTML,
|
|
127
|
+
RAW_SYNTAX,
|
|
128
|
+
} from './transformers/raw-syntax.js';
|
|
118
129
|
import { $getSelectedAlignment } from './alignment.js';
|
|
119
130
|
import {
|
|
120
131
|
$ensureParagraphAfterTable,
|
|
@@ -683,6 +694,157 @@ const registerCodeEditorCommands = (editor, defaultLanguage) =>
|
|
|
683
694
|
}),
|
|
684
695
|
);
|
|
685
696
|
|
|
697
|
+
/**
|
|
698
|
+
* Check if the given editor component is inline, which means its Markdown is matched within a line
|
|
699
|
+
* of text by a text match transformer. Only an inline component can be inserted into a Markdown
|
|
700
|
+
* table cell or a single-line editor.
|
|
701
|
+
* @param {TextEditorComponent} component Editor component.
|
|
702
|
+
* @returns {boolean} Result.
|
|
703
|
+
*/
|
|
704
|
+
export const isInlineComponent = ({ transformer }) => transformer.type === 'text-match';
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* Limit the given editor configuration to what a single-line editor can hold, if it’s one: only
|
|
708
|
+
* the inline buttons and components, and no plain text mode for HTML, whose source can’t be kept to
|
|
709
|
+
* a single paragraph by keeping it on a single line. Any other configuration is returned as is.
|
|
710
|
+
* @param {TextEditorConfig} config Editor configuration.
|
|
711
|
+
* @returns {TextEditorConfig} Limited configuration.
|
|
712
|
+
*/
|
|
713
|
+
export const limitToSingleLine = (config) => {
|
|
714
|
+
if (config.multiline !== false || config.isCodeEditor) {
|
|
715
|
+
return config;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
return {
|
|
719
|
+
...config,
|
|
720
|
+
modes: config.format === 'html' ? ['rich-text'] : config.modes,
|
|
721
|
+
enabledButtons: (config.enabledButtons ?? []).filter((button) =>
|
|
722
|
+
/** @type {TextEditorNodeType[]} */ (INLINE_BUTTON_TYPES).includes(button),
|
|
723
|
+
),
|
|
724
|
+
components: (config.components ?? []).filter(isInlineComponent),
|
|
725
|
+
};
|
|
726
|
+
};
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* Get the inline content of the given top-level node, looking into any block within it.
|
|
730
|
+
* @param {LexicalNode} node Node.
|
|
731
|
+
* @returns {LexicalNode[]} Inline nodes, or an empty array for a block that has no inline content,
|
|
732
|
+
* like a horizontal rule.
|
|
733
|
+
*/
|
|
734
|
+
const getInlineContent = (node) => {
|
|
735
|
+
if (isElementNode(node) && !node.isInline()) {
|
|
736
|
+
return node.getChildren().flatMap(getInlineContent);
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
return node.isInline() ? [node] : [];
|
|
740
|
+
};
|
|
741
|
+
|
|
742
|
+
/**
|
|
743
|
+
* Join the top-level nodes of the given root into a single paragraph, with a space between the
|
|
744
|
+
* content of each, for a single-line editor.
|
|
745
|
+
* @param {RootNode} root Root node.
|
|
746
|
+
*/
|
|
747
|
+
export const $joinIntoSingleParagraph = (root) => {
|
|
748
|
+
const children = root.getChildren();
|
|
749
|
+
const [firstChild] = children;
|
|
750
|
+
|
|
751
|
+
if (!firstChild || (children.length === 1 && isParagraphNode(firstChild))) {
|
|
752
|
+
return;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
const paragraph = isParagraphNode(firstChild) ? firstChild : createParagraphNode();
|
|
756
|
+
|
|
757
|
+
if (paragraph !== firstChild) {
|
|
758
|
+
firstChild.insertBefore(paragraph);
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
// Keep track of the last character rather than reading the growing text of the paragraph again
|
|
762
|
+
// for each block, which would take quadratic time on a long pasted document
|
|
763
|
+
let lastChar = paragraph.getTextContent().at(-1) ?? '';
|
|
764
|
+
|
|
765
|
+
children
|
|
766
|
+
.filter((child) => child !== paragraph)
|
|
767
|
+
.forEach((child) => {
|
|
768
|
+
const content = getInlineContent(child);
|
|
769
|
+
const text = content.map((node) => node.getTextContent()).join('');
|
|
770
|
+
|
|
771
|
+
// Add a space between the content of each block, unless there’s one already
|
|
772
|
+
if (content.length && paragraph.getChildrenSize() && !/\s/.test(lastChar + text.charAt(0))) {
|
|
773
|
+
paragraph.append(createTextNode(' '));
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
paragraph.append(...content);
|
|
777
|
+
lastChar = text.at(-1) ?? lastChar;
|
|
778
|
+
// Lexical moves the selection out of a removed node on its own
|
|
779
|
+
child.remove();
|
|
780
|
+
});
|
|
781
|
+
};
|
|
782
|
+
|
|
783
|
+
/**
|
|
784
|
+
* Replace the given line break with a space for a single-line editor, with the formats the text on
|
|
785
|
+
* both sides shares, so a line break within bold text doesn’t split it. With a space on either side
|
|
786
|
+
* already, or nothing at all, the line break is just removed.
|
|
787
|
+
* @param {LineBreakNode} node Line break.
|
|
788
|
+
*/
|
|
789
|
+
export const $replaceLineBreak = (node) => {
|
|
790
|
+
const previous = node.getPreviousSibling();
|
|
791
|
+
const next = node.getNextSibling();
|
|
792
|
+
|
|
793
|
+
// A line break at either end of the paragraph separates nothing, so it leaves no space
|
|
794
|
+
if (
|
|
795
|
+
!previous ||
|
|
796
|
+
!next ||
|
|
797
|
+
/\s$/.test(previous.getTextContent()) ||
|
|
798
|
+
/^\s/.test(next.getTextContent())
|
|
799
|
+
) {
|
|
800
|
+
node.remove();
|
|
801
|
+
|
|
802
|
+
return;
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
const space = createTextNode(' ');
|
|
806
|
+
|
|
807
|
+
if (isTextNode(previous) && isTextNode(next)) {
|
|
808
|
+
// Keep the formats both sides share, e.g. italic between italic and bold italic text
|
|
809
|
+
TEXT_FORMAT_BUTTON_TYPES.forEach((format) => {
|
|
810
|
+
if (previous.hasFormat(format) && next.hasFormat(format)) {
|
|
811
|
+
space.toggleFormat(format);
|
|
812
|
+
}
|
|
813
|
+
});
|
|
814
|
+
|
|
815
|
+
if (previous.getStyle() === next.getStyle()) {
|
|
816
|
+
space.setStyle(previous.getStyle());
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
node.replace(space);
|
|
821
|
+
};
|
|
822
|
+
|
|
823
|
+
/**
|
|
824
|
+
* Register the commands and transforms that keep a single-line editor on one line. Neither a new
|
|
825
|
+
* paragraph nor a line break can be inserted, and any content that brings them in anyway, like
|
|
826
|
+
* pasted text or an imported value, is joined into a single paragraph with spaces.
|
|
827
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
828
|
+
* @returns {() => void} Cleanup handler.
|
|
829
|
+
*/
|
|
830
|
+
const registerSingleLine = (editor) =>
|
|
831
|
+
mergeUnregisters(
|
|
832
|
+
// The emoji autocomplete, which selects an emoji with the Enter key, takes priority
|
|
833
|
+
editor.registerCommand(
|
|
834
|
+
KEY_ENTER_COMMAND,
|
|
835
|
+
(event) => {
|
|
836
|
+
event?.preventDefault();
|
|
837
|
+
|
|
838
|
+
return true;
|
|
839
|
+
},
|
|
840
|
+
COMMAND_PRIORITY_HIGH,
|
|
841
|
+
),
|
|
842
|
+
editor.registerCommand(INSERT_PARAGRAPH_COMMAND, () => true, COMMAND_PRIORITY_HIGH),
|
|
843
|
+
editor.registerCommand(INSERT_LINE_BREAK_COMMAND, () => true, COMMAND_PRIORITY_HIGH),
|
|
844
|
+
editor.registerNodeTransform(LineBreakNode, $replaceLineBreak),
|
|
845
|
+
editor.registerNodeTransform(RootNode, $joinIntoSingleParagraph),
|
|
846
|
+
);
|
|
847
|
+
|
|
686
848
|
/**
|
|
687
849
|
* Register the link command, the paste handler that turns a pasted URL into a link, and the
|
|
688
850
|
* transform that removes a link with an unsafe URL.
|
|
@@ -904,22 +1066,181 @@ const registerTabIndentation = (editor) =>
|
|
|
904
1066
|
};
|
|
905
1067
|
});
|
|
906
1068
|
|
|
1069
|
+
/**
|
|
1070
|
+
* Remove the placeholders added by `addImportPlaceholders()` from a regular expression match.
|
|
1071
|
+
* @param {RegExpMatchArray} match Match.
|
|
1072
|
+
* @returns {RegExpMatchArray} Match without the placeholders, with the same index and input.
|
|
1073
|
+
*/
|
|
1074
|
+
const cleanMatch = (match) => {
|
|
1075
|
+
/**
|
|
1076
|
+
* Remove the placeholders from a captured string, if any.
|
|
1077
|
+
* @param {string | undefined} value Captured string.
|
|
1078
|
+
* @returns {string | undefined} Cleaned string.
|
|
1079
|
+
*/
|
|
1080
|
+
const clean = (value) => (value === undefined ? value : removeImportPlaceholders(value));
|
|
1081
|
+
|
|
1082
|
+
return /** @type {RegExpMatchArray} */ (
|
|
1083
|
+
Object.assign(match.map(clean), {
|
|
1084
|
+
index: match.index,
|
|
1085
|
+
input: match.input,
|
|
1086
|
+
groups: match.groups
|
|
1087
|
+
? Object.fromEntries(
|
|
1088
|
+
Object.entries(match.groups).map(([key, value]) => [key, clean(value)]),
|
|
1089
|
+
)
|
|
1090
|
+
: undefined,
|
|
1091
|
+
})
|
|
1092
|
+
);
|
|
1093
|
+
};
|
|
1094
|
+
|
|
1095
|
+
/**
|
|
1096
|
+
* Make a regular expression match the text after the placeholder at the start of a line of an HTML
|
|
1097
|
+
* block, if any, so the pattern of an editor component, like `^<aside>`, still matches the line.
|
|
1098
|
+
* @param {RegExp} regExp Regular expression without the `g` flag.
|
|
1099
|
+
* @param {boolean} isLineStart Whether Lexical takes the match as the start of the line and removes
|
|
1100
|
+
* it by its length, like that of an element transformer, in which case the match includes the
|
|
1101
|
+
* placeholder. Otherwise its index is moved past the placeholder.
|
|
1102
|
+
* @returns {RegExp} Regular expression.
|
|
1103
|
+
*/
|
|
1104
|
+
const skipHtmlBlockPlaceholder = (regExp, isLineStart) =>
|
|
1105
|
+
// Lexical matches the text with `String.prototype.match()`, which calls this method
|
|
1106
|
+
Object.assign(new RegExp(regExp), {
|
|
1107
|
+
/**
|
|
1108
|
+
* Match the text after the placeholder at its start, if any.
|
|
1109
|
+
* @param {string} text Text.
|
|
1110
|
+
* @returns {RegExpMatchArray | null} Match.
|
|
1111
|
+
*/
|
|
1112
|
+
[Symbol.match]: (text) => {
|
|
1113
|
+
const offset = text.startsWith(HTML_BLOCK_PLACEHOLDER) ? 1 : 0;
|
|
1114
|
+
const match = text.slice(offset).match(regExp);
|
|
1115
|
+
|
|
1116
|
+
if (!match || !offset) {
|
|
1117
|
+
return match;
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
match.input = text;
|
|
1121
|
+
|
|
1122
|
+
if (isLineStart && match.index === 0) {
|
|
1123
|
+
match[0] = `${HTML_BLOCK_PLACEHOLDER}${match[0]}`;
|
|
1124
|
+
} else {
|
|
1125
|
+
// The pattern has no `g` flag, so the match has an index
|
|
1126
|
+
/** @type {number} */ (match.index) += offset;
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
return match;
|
|
1130
|
+
},
|
|
1131
|
+
});
|
|
1132
|
+
|
|
1133
|
+
/**
|
|
1134
|
+
* Remove the placeholders added by `addImportPlaceholders()` from an argument of a transformer’s
|
|
1135
|
+
* `replace()`: a regular expression match, or the lines of a multiline element.
|
|
1136
|
+
* @param {any} arg Argument.
|
|
1137
|
+
* @returns {any} Argument without the placeholders.
|
|
1138
|
+
*/
|
|
1139
|
+
const cleanReplaceArg = (arg) => {
|
|
1140
|
+
if (!Array.isArray(arg)) {
|
|
1141
|
+
return arg;
|
|
1142
|
+
}
|
|
1143
|
+
|
|
1144
|
+
if ('index' in arg) {
|
|
1145
|
+
return cleanMatch(/** @type {RegExpMatchArray} */ (arg));
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
return arg.every((item) => typeof item === 'string')
|
|
1149
|
+
? arg.map((line) => removeImportPlaceholders(line))
|
|
1150
|
+
: arg;
|
|
1151
|
+
};
|
|
1152
|
+
|
|
1153
|
+
/**
|
|
1154
|
+
* Make an editor component’s transformer import the Markdown without the placeholders added by
|
|
1155
|
+
* `addImportPlaceholders()`, which `removePlaceholders()` can’t remove from the properties of the
|
|
1156
|
+
* component node it creates, like the alt text of an image. The line of an HTML block starts with
|
|
1157
|
+
* a placeholder, so the patterns of a component, like `^<aside>`, are matched after it.
|
|
1158
|
+
* @param {Transformer} transformer Transformer.
|
|
1159
|
+
* @returns {Transformer} Transformer.
|
|
1160
|
+
*/
|
|
1161
|
+
export const removePlaceholdersFromMatch = (transformer) => {
|
|
1162
|
+
/** @type {any} */
|
|
1163
|
+
const {
|
|
1164
|
+
type,
|
|
1165
|
+
handleImportAfterStartMatch,
|
|
1166
|
+
importRegExp,
|
|
1167
|
+
regExp,
|
|
1168
|
+
regExpStart,
|
|
1169
|
+
regExpEnd,
|
|
1170
|
+
replace,
|
|
1171
|
+
} = transformer;
|
|
1172
|
+
|
|
1173
|
+
/** @type {Record<string, any>} */
|
|
1174
|
+
const overrides = {};
|
|
1175
|
+
|
|
1176
|
+
if (handleImportAfterStartMatch) {
|
|
1177
|
+
/**
|
|
1178
|
+
* Import the lines without the placeholders.
|
|
1179
|
+
* @param {{ lines: string[] }} args Arguments.
|
|
1180
|
+
* @returns {any} Result.
|
|
1181
|
+
*/
|
|
1182
|
+
overrides.handleImportAfterStartMatch = (args) =>
|
|
1183
|
+
handleImportAfterStartMatch({
|
|
1184
|
+
...args,
|
|
1185
|
+
lines: args.lines.map((line) => removeImportPlaceholders(line)),
|
|
1186
|
+
});
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
if (importRegExp) {
|
|
1190
|
+
overrides.importRegExp = skipHtmlBlockPlaceholder(importRegExp, false);
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
// The regular expression of an element is matched against the start of a line on import
|
|
1194
|
+
if (type === 'element' && regExp) {
|
|
1195
|
+
overrides.regExp = skipHtmlBlockPlaceholder(regExp, true);
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
if (regExpStart) {
|
|
1199
|
+
overrides.regExpStart = skipHtmlBlockPlaceholder(regExpStart, true);
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
if (regExpEnd instanceof RegExp) {
|
|
1203
|
+
overrides.regExpEnd = skipHtmlBlockPlaceholder(regExpEnd, true);
|
|
1204
|
+
} else if (regExpEnd?.regExp) {
|
|
1205
|
+
overrides.regExpEnd = {
|
|
1206
|
+
...regExpEnd,
|
|
1207
|
+
regExp: skipHtmlBlockPlaceholder(regExpEnd.regExp, true),
|
|
1208
|
+
};
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
if (replace) {
|
|
1212
|
+
/**
|
|
1213
|
+
* Import the match and the lines without the placeholders.
|
|
1214
|
+
* @param {...any} args Arguments.
|
|
1215
|
+
* @returns {any} Result.
|
|
1216
|
+
*/
|
|
1217
|
+
overrides.replace = (...args) => replace(...args.map(cleanReplaceArg));
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
return /** @type {Transformer} */ ({ ...transformer, ...overrides });
|
|
1221
|
+
};
|
|
1222
|
+
|
|
907
1223
|
/**
|
|
908
1224
|
* Initialize the Lexical editor.
|
|
909
1225
|
* @param {TextEditorConfig} config Editor configuration.
|
|
910
1226
|
* @returns {InitEditorResult} Editor instance and cleanup.
|
|
911
1227
|
*/
|
|
912
|
-
export const initEditor = ({
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
1228
|
+
export const initEditor = (config) => {
|
|
1229
|
+
const {
|
|
1230
|
+
enabledButtons = [],
|
|
1231
|
+
components = [],
|
|
1232
|
+
useMarkdownShortcuts,
|
|
1233
|
+
isCodeEditor = false,
|
|
1234
|
+
defaultLanguage = 'plain',
|
|
1235
|
+
format = 'markdown',
|
|
1236
|
+
multiline = true,
|
|
1237
|
+
} = limitToSingleLine(config);
|
|
1238
|
+
|
|
920
1239
|
const hasCodeBlock = enabledButtons.includes('code-block') || isCodeEditor;
|
|
921
1240
|
const hasTable = enabledButtons.includes('table') && !isCodeEditor;
|
|
922
1241
|
const hasHorizontalRule = enabledButtons.includes('horizontal-rule') && !isCodeEditor;
|
|
1242
|
+
/** Whether to support horizontal rules and tables in Markdown, even without their buttons. */
|
|
1243
|
+
const hasBlockSyntax = !isCodeEditor && multiline;
|
|
923
1244
|
|
|
924
1245
|
const editorExtension = defineExtension({
|
|
925
1246
|
name: '@sveltia/ui/editor',
|
|
@@ -954,11 +1275,11 @@ export const initEditor = ({
|
|
|
954
1275
|
.filter(([button]) => enabledButtons.includes(/** @type {TextEditorNodeType} */ (button)))
|
|
955
1276
|
.flatMap(([, nodes]) => nodes),
|
|
956
1277
|
),
|
|
957
|
-
...(isCodeEditor
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
1278
|
+
...(isCodeEditor ? [CodeNode, CodeHighlightNode] : []),
|
|
1279
|
+
// We haven’t implemented buttons for horizontal rules and tables yet, but we still want to
|
|
1280
|
+
// support them in Markdown, so always include them in the node list, unless the editor is
|
|
1281
|
+
// limited to a single line
|
|
1282
|
+
...(hasBlockSyntax ? [HorizontalRuleNode, TableNode, TableCellNode, TableRowNode] : []),
|
|
962
1283
|
],
|
|
963
1284
|
theme: EDITOR_THEME,
|
|
964
1285
|
// Export clean HTML without the attributes and elements only meant for the editor, and import
|
|
@@ -980,20 +1301,25 @@ export const initEditor = ({
|
|
|
980
1301
|
},
|
|
981
1302
|
});
|
|
982
1303
|
|
|
983
|
-
const componentTransformers = components.map(({ transformer }) =>
|
|
1304
|
+
const componentTransformers = components.map(({ transformer }) =>
|
|
1305
|
+
removePlaceholdersFromMatch(transformer),
|
|
1306
|
+
);
|
|
984
1307
|
|
|
985
1308
|
/** @type {Transformer[]} */
|
|
986
1309
|
const enabledTransformers = [
|
|
987
1310
|
...componentTransformers,
|
|
1311
|
+
// Raw HTML comes before the editor’s own syntax, so a line of an HTML block that starts like a
|
|
1312
|
+
// link, for example, is still raw HTML. Only an editor component takes precedence
|
|
1313
|
+
...(isCodeEditor ? [] : [RAW_HTML]),
|
|
988
1314
|
...new Set(
|
|
989
1315
|
Object.entries(TRANSFORMER_MAP)
|
|
990
1316
|
.filter(([button]) => enabledButtons.includes(/** @type {TextEditorNodeType} */ (button)))
|
|
991
1317
|
.flatMap(([, transformers]) => transformers),
|
|
992
1318
|
),
|
|
993
|
-
...(isCodeEditor
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
1319
|
+
...(isCodeEditor ? [CODE] : []),
|
|
1320
|
+
// See the comment above for why we always include horizontal rules and tables
|
|
1321
|
+
...(hasBlockSyntax
|
|
1322
|
+
? [
|
|
997
1323
|
HR,
|
|
998
1324
|
// A table cell can only have inline content, so only the components with a text match
|
|
999
1325
|
// transformer can be used there
|
|
@@ -1002,9 +1328,10 @@ export const initEditor = ({
|
|
|
1002
1328
|
componentTransformers.filter(({ type }) => type === 'text-match')
|
|
1003
1329
|
),
|
|
1004
1330
|
),
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1331
|
+
]
|
|
1332
|
+
: []),
|
|
1333
|
+
// Last, so the syntax of the editor’s own comes first
|
|
1334
|
+
...(isCodeEditor ? [] : [RAW_SYNTAX]),
|
|
1008
1335
|
];
|
|
1009
1336
|
|
|
1010
1337
|
const editor = buildEditorFromExtensions(editorExtension);
|
|
@@ -1026,6 +1353,7 @@ export const initEditor = ({
|
|
|
1026
1353
|
HISTORY_MAX_DEPTH,
|
|
1027
1354
|
),
|
|
1028
1355
|
isCodeEditor ? registerCodeEditorCommands(editor, defaultLanguage) : undefined,
|
|
1356
|
+
multiline || isCodeEditor ? undefined : registerSingleLine(editor),
|
|
1029
1357
|
useMarkdownShortcuts ? registerMarkdownShortcuts(editor, enabledTransformers) : undefined,
|
|
1030
1358
|
hasCodeBlock
|
|
1031
1359
|
? registerCodeHighlighting(editor, {
|
|
@@ -1162,14 +1490,16 @@ const importValue = async (editor, { languages, $import, $export, formatName })
|
|
|
1162
1490
|
|
|
1163
1491
|
/**
|
|
1164
1492
|
* Remove the placeholders added by `addImportPlaceholders()` from the imported nodes: the text, the
|
|
1165
|
-
* Markdown of raw syntax, and the URL and title of a link.
|
|
1493
|
+
* Markdown of raw syntax, and the URL and title of a link. The text of raw syntax is restored from
|
|
1494
|
+
* the Markdown it was imported from, as Lexical has removed its escapes, which are not escapes in
|
|
1495
|
+
* raw HTML; see `RAW_HTML`.
|
|
1166
1496
|
*/
|
|
1167
1497
|
const removePlaceholders = () => {
|
|
1168
1498
|
getRoot()
|
|
1169
1499
|
.getAllTextNodes()
|
|
1170
1500
|
.forEach((node) => {
|
|
1171
1501
|
const text = node.getTextContent();
|
|
1172
|
-
const newText = removeImportPlaceholders(text);
|
|
1502
|
+
const newText = removeImportPlaceholders(getImportedRawSyntax(node) ?? text);
|
|
1173
1503
|
const link = node.getParent();
|
|
1174
1504
|
|
|
1175
1505
|
if (newText !== text) {
|
|
@@ -1230,7 +1560,8 @@ export const convertMarkdownToLexical = async (editor, value, enabledTransformer
|
|
|
1230
1560
|
$import: () => {
|
|
1231
1561
|
// Split multiline formatting into separate lines to prevent Markdown parsing issues
|
|
1232
1562
|
const splitValue = splitMultilineFormatting(normalizedValue);
|
|
1233
|
-
// Add placeholders so Lexical reads the backslashes
|
|
1563
|
+
// Add placeholders so Lexical reads the backslashes, the tabs in code spans and raw HTML
|
|
1564
|
+
// right
|
|
1234
1565
|
const markedValue = addImportPlaceholders(splitValue);
|
|
1235
1566
|
|
|
1236
1567
|
convertFromMarkdownString(
|
|
@@ -1240,7 +1571,8 @@ export const convertMarkdownToLexical = async (editor, value, enabledTransformer
|
|
|
1240
1571
|
addCodeSpanSplit(enabledTransformers),
|
|
1241
1572
|
);
|
|
1242
1573
|
|
|
1243
|
-
|
|
1574
|
+
// The text of raw HTML is restored as well. The lines of an HTML block have a placeholder
|
|
1575
|
+
if (markedValue !== splitValue || INLINE_HTML_REGEX.test(markedValue)) {
|
|
1244
1576
|
removePlaceholders();
|
|
1245
1577
|
}
|
|
1246
1578
|
},
|
|
@@ -240,7 +240,7 @@
|
|
|
240
240
|
bind:this={lexicalRoot}
|
|
241
241
|
{...restProps}
|
|
242
242
|
role="textbox"
|
|
243
|
-
aria-multiline=
|
|
243
|
+
aria-multiline={editorStore.config.multiline !== false}
|
|
244
244
|
aria-hidden={hidden}
|
|
245
245
|
aria-disabled={disabled}
|
|
246
246
|
aria-readonly={readonly}
|
|
@@ -264,6 +264,9 @@
|
|
|
264
264
|
font-family: var(--sui-textbox-font-family);
|
|
265
265
|
font-size: var(--sui-textbox-font-size);
|
|
266
266
|
line-height: var(--sui-textbox-multiline-line-height);
|
|
267
|
+
transition-duration: 200ms;
|
|
268
|
+
transition-property: outline-color, border-color, color, background-color;
|
|
269
|
+
outline-offset: calc(var(--sui-focus-ring-width) * -1);
|
|
267
270
|
}
|
|
268
271
|
.lexical-root:not(:first-child) {
|
|
269
272
|
border-start-start-radius: 0 !important;
|
|
@@ -276,8 +279,9 @@
|
|
|
276
279
|
border-radius: 0 !important;
|
|
277
280
|
min-height: 120px;
|
|
278
281
|
}
|
|
279
|
-
.lexical-root:focus
|
|
280
|
-
|
|
282
|
+
.lexical-root:focus {
|
|
283
|
+
color: var(--sui-textbox-foreground-color-focus, var(--sui-textbox-foreground-color));
|
|
284
|
+
background-color: var(--sui-textbox-background-color-focus, var(--sui-textbox-background-color));
|
|
281
285
|
}
|
|
282
286
|
.lexical-root[aria-invalid=true] {
|
|
283
287
|
border-color: var(--sui-error-border-color);
|