@sveltia/ui 0.89.2 → 0.90.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/checkbox/checkbox.svelte +5 -8
- package/dist/components/dialog/dialog.svelte +149 -9
- package/dist/components/emoji/emoji-suggestions.svelte +7 -7
- package/dist/components/grid/grid-col-header.svelte +4 -0
- package/dist/components/grid/grid-head.svelte +1 -1
- package/dist/components/grid/grid-head.svelte.d.ts +1 -1
- package/dist/components/radio/radio.svelte +9 -9
- package/dist/components/resizable-pane/resizable-pane-group.svelte +5 -20
- package/dist/components/resizable-pane/sizing.d.ts +6 -0
- package/dist/components/resizable-pane/sizing.js +31 -0
- package/dist/components/text-editor/code-editor.svelte +22 -39
- package/dist/components/text-editor/constants.js +3 -3
- package/dist/components/text-editor/conversion/editor-components.d.ts +5 -0
- package/dist/components/text-editor/conversion/editor-components.js +228 -0
- package/dist/components/text-editor/{html.d.ts → conversion/html.d.ts} +1 -1
- package/dist/components/text-editor/{html.js → conversion/html.js} +158 -20
- package/dist/components/text-editor/conversion/import.d.ts +12 -0
- package/dist/components/text-editor/conversion/import.js +327 -0
- package/dist/components/text-editor/conversion/paste.d.ts +4 -0
- package/dist/components/text-editor/conversion/paste.js +166 -0
- package/dist/components/text-editor/{transformers → conversion/transformers}/raw-syntax.js +4 -4
- package/dist/components/text-editor/conversion/transformers/table.js +342 -0
- package/dist/components/text-editor/conversion/update.d.ts +6 -0
- package/dist/components/text-editor/conversion/update.js +125 -0
- package/dist/components/text-editor/core.d.ts +50 -26
- package/dist/components/text-editor/core.js +133 -1694
- package/dist/components/text-editor/editing/caret.d.ts +4 -0
- package/dist/components/text-editor/editing/caret.js +248 -0
- package/dist/components/text-editor/editing/commands.d.ts +5 -0
- package/dist/components/text-editor/editing/commands.js +205 -0
- package/dist/components/text-editor/editing/link.d.ts +19 -0
- package/dist/components/text-editor/editing/link.js +222 -0
- package/dist/components/text-editor/{raw-markdown.d.ts → editing/raw-markdown.d.ts} +3 -3
- package/dist/components/text-editor/{raw-markdown.js → editing/raw-markdown.js} +2 -2
- package/dist/components/text-editor/editing/selection.d.ts +5 -0
- package/dist/components/text-editor/editing/selection.js +122 -0
- package/dist/components/text-editor/editing/single-line.d.ts +8 -0
- package/dist/components/text-editor/editing/single-line.js +190 -0
- package/dist/components/text-editor/{table.js → editing/table.js} +1 -1
- package/dist/components/text-editor/lexical-root.svelte +61 -10
- package/dist/components/text-editor/shiki/preload.d.ts +1 -0
- package/dist/components/text-editor/shiki/preload.js +30 -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.d.ts +5 -0
- package/dist/components/text-editor/store.svelte.js +55 -1
- package/dist/components/text-editor/text-editor.svelte +29 -41
- package/dist/components/text-editor/toolbar/align-menu-button.svelte +1 -1
- package/dist/components/text-editor/toolbar/code-language-switcher.svelte +2 -1
- package/dist/components/text-editor/toolbar/format-text-button.svelte +6 -2
- package/dist/components/text-editor/toolbar/insert-component.js +1 -1
- package/dist/components/text-editor/toolbar/insert-horizontal-rule-button.svelte +4 -4
- package/dist/components/text-editor/toolbar/insert-link-button.svelte +3 -2
- package/dist/components/text-editor/toolbar/insert-table-button.svelte +7 -3
- package/dist/components/text-editor/toolbar/table-action-menu.svelte +3 -3
- package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte +1 -1
- package/dist/components/text-editor/toolbar/toggle-block-menu-item.svelte +2 -2
- package/dist/components/text-editor/utils.d.ts +1 -0
- package/dist/components/text-editor/utils.js +17 -0
- package/dist/components/text-editor/{emoji-autocomplete.svelte → widgets/emoji-autocomplete.svelte} +3 -3
- package/dist/components/text-editor/{floating-link-editor.svelte → widgets/floating-link-editor.svelte} +36 -79
- package/dist/components/text-editor/{table-cell-menu-button.svelte → widgets/table-cell-menu-button.svelte} +15 -5
- package/dist/components/text-field/date-time-input.svelte +8 -8
- package/dist/components/text-field/text-area.svelte +10 -10
- package/dist/components/text-field/text-input.svelte +7 -7
- package/dist/components/util/modal.svelte +41 -12
- package/dist/components/util/popup.svelte +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/locales/es-ES.yaml +174 -0
- package/dist/services/label.d.ts +8 -0
- package/dist/services/label.js +15 -0
- package/dist/services/popup-position.d.ts +9 -0
- package/dist/services/popup-position.js +52 -55
- package/dist/styles/mixins.scss +69 -0
- package/dist/typedefs.d.ts +28 -1
- package/dist/typedefs.js +16 -1
- package/package.json +13 -11
- package/dist/components/text-editor/transformers/table.js +0 -296
- /package/dist/components/text-editor/{markdown.d.ts → conversion/markdown.d.ts} +0 -0
- /package/dist/components/text-editor/{markdown.js → conversion/markdown.js} +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/block-escape.d.ts +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/block-escape.js +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/code.d.ts +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/code.js +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/formatted-code.d.ts +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/formatted-code.js +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/hr.d.ts +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/hr.js +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/inline-code.d.ts +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/inline-code.js +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/link.d.ts +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/link.js +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/raw-syntax.d.ts +0 -0
- /package/dist/components/text-editor/{transformers → conversion/transformers}/table.d.ts +0 -0
- /package/dist/components/text-editor/{alignment.d.ts → editing/alignment.d.ts} +0 -0
- /package/dist/components/text-editor/{alignment.js → editing/alignment.js} +0 -0
- /package/dist/components/text-editor/{block.d.ts → editing/block.d.ts} +0 -0
- /package/dist/components/text-editor/{block.js → editing/block.js} +0 -0
- /package/dist/components/text-editor/{table.d.ts → editing/table.d.ts} +0 -0
- /package/dist/components/text-editor/{emoji-autocomplete.svelte.d.ts → widgets/emoji-autocomplete.svelte.d.ts} +0 -0
- /package/dist/components/text-editor/{floating-link-editor.svelte.d.ts → widgets/floating-link-editor.svelte.d.ts} +0 -0
- /package/dist/components/text-editor/{table-cell-menu-button.svelte.d.ts → widgets/table-cell-menu-button.svelte.d.ts} +0 -0
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Editor components: custom nodes with their own Markdown transformers.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
createEditor,
|
|
7
|
+
$createParagraphNode as createParagraphNode,
|
|
8
|
+
$getRoot as getRoot,
|
|
9
|
+
} from 'lexical';
|
|
10
|
+
import { removeImportPlaceholders } from './markdown.js';
|
|
11
|
+
import { HTML_BLOCK_PLACEHOLDER } from './transformers/raw-syntax.js';
|
|
12
|
+
import { onEditorUpdate } from './update.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @import { Transformer } from '@lexical/markdown';
|
|
16
|
+
* @import { TextEditorComponent } from '../../../typedefs.js';
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Get the Markdown of a new instance of the given editor component, to insert it in the plain text
|
|
21
|
+
* mode. The component’s own `createMarkdown` function is used if any. Otherwise, a new node is
|
|
22
|
+
* created in a temporary editor and exported with the component’s transformer, just like the rich
|
|
23
|
+
* text editor does.
|
|
24
|
+
* @param {TextEditorComponent} component Editor component.
|
|
25
|
+
* @returns {string} Markdown. It can be empty, for example if the component’s output depends on a
|
|
26
|
+
* field value that is not set yet.
|
|
27
|
+
*/
|
|
28
|
+
export const getComponentMarkdown = ({ node, createNode, transformer, createMarkdown }) => {
|
|
29
|
+
if (createMarkdown) {
|
|
30
|
+
return createMarkdown().trim();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const editor = createEditor({
|
|
34
|
+
namespace: 'component',
|
|
35
|
+
nodes: node ? [/** @type {any} */ (node)] : [],
|
|
36
|
+
/**
|
|
37
|
+
* Throw an error, so the export below fails as a whole.
|
|
38
|
+
* @param {Error} error Error.
|
|
39
|
+
* @throws {Error} Always.
|
|
40
|
+
*/
|
|
41
|
+
onError: (error) => {
|
|
42
|
+
throw error;
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
let markdown = '';
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
editor.update(
|
|
50
|
+
() => {
|
|
51
|
+
const newNode = createNode();
|
|
52
|
+
|
|
53
|
+
// An inline node needs a paragraph to live in
|
|
54
|
+
getRoot().append(newNode.isInline() ? createParagraphNode().append(newNode) : newNode);
|
|
55
|
+
|
|
56
|
+
markdown = onEditorUpdate(editor, [transformer]);
|
|
57
|
+
},
|
|
58
|
+
{ discrete: true },
|
|
59
|
+
);
|
|
60
|
+
} catch {
|
|
61
|
+
// The component cannot be created or exported outside the rich text editor
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return markdown.trim();
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Check if the given editor component is inline, which means its Markdown is matched within a line
|
|
69
|
+
* of text by a text match transformer. Only an inline component can be inserted into a Markdown
|
|
70
|
+
* table cell or a single-line editor.
|
|
71
|
+
* @param {TextEditorComponent} component Editor component.
|
|
72
|
+
* @returns {boolean} Result.
|
|
73
|
+
*/
|
|
74
|
+
export const isInlineComponent = ({ transformer }) => transformer.type === 'text-match';
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Remove the placeholders added by `addImportPlaceholders()` from a regular expression match.
|
|
78
|
+
* @param {RegExpMatchArray} match Match.
|
|
79
|
+
* @returns {RegExpMatchArray} Match without the placeholders, with the same index and input.
|
|
80
|
+
*/
|
|
81
|
+
const cleanMatch = (match) => {
|
|
82
|
+
/**
|
|
83
|
+
* Remove the placeholders from a captured string, if any.
|
|
84
|
+
* @param {string | undefined} value Captured string.
|
|
85
|
+
* @returns {string | undefined} Cleaned string.
|
|
86
|
+
*/
|
|
87
|
+
const clean = (value) => (value === undefined ? value : removeImportPlaceholders(value));
|
|
88
|
+
|
|
89
|
+
return /** @type {RegExpMatchArray} */ (
|
|
90
|
+
Object.assign(match.map(clean), {
|
|
91
|
+
index: match.index,
|
|
92
|
+
input: match.input,
|
|
93
|
+
groups: match.groups
|
|
94
|
+
? Object.fromEntries(
|
|
95
|
+
Object.entries(match.groups).map(([key, value]) => [key, clean(value)]),
|
|
96
|
+
)
|
|
97
|
+
: undefined,
|
|
98
|
+
})
|
|
99
|
+
);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Make a regular expression match the text after the placeholder at the start of a line of an HTML
|
|
104
|
+
* block, if any, so the pattern of an editor component, like `^<aside>`, still matches the line.
|
|
105
|
+
* @param {RegExp} regExp Regular expression without the `g` flag.
|
|
106
|
+
* @param {boolean} isLineStart Whether Lexical takes the match as the start of the line and removes
|
|
107
|
+
* it by its length, like that of an element transformer, in which case the match includes the
|
|
108
|
+
* placeholder. Otherwise its index is moved past the placeholder.
|
|
109
|
+
* @returns {RegExp} Regular expression.
|
|
110
|
+
*/
|
|
111
|
+
const skipHtmlBlockPlaceholder = (regExp, isLineStart) =>
|
|
112
|
+
// Lexical matches the text with `String.prototype.match()`, which calls this method
|
|
113
|
+
Object.assign(new RegExp(regExp), {
|
|
114
|
+
/**
|
|
115
|
+
* Match the text after the placeholder at its start, if any.
|
|
116
|
+
* @param {string} text Text.
|
|
117
|
+
* @returns {RegExpMatchArray | null} Match.
|
|
118
|
+
*/
|
|
119
|
+
[Symbol.match]: (text) => {
|
|
120
|
+
const offset = text.startsWith(HTML_BLOCK_PLACEHOLDER) ? 1 : 0;
|
|
121
|
+
const match = text.slice(offset).match(regExp);
|
|
122
|
+
|
|
123
|
+
if (!match || !offset) {
|
|
124
|
+
return match;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
match.input = text;
|
|
128
|
+
|
|
129
|
+
if (isLineStart && match.index === 0) {
|
|
130
|
+
match[0] = `${HTML_BLOCK_PLACEHOLDER}${match[0]}`;
|
|
131
|
+
} else {
|
|
132
|
+
// The pattern has no `g` flag, so the match has an index
|
|
133
|
+
/** @type {number} */ (match.index) += offset;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return match;
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Remove the placeholders added by `addImportPlaceholders()` from an argument of a transformer’s
|
|
142
|
+
* `replace()`: a regular expression match, or the lines of a multiline element.
|
|
143
|
+
* @param {any} arg Argument.
|
|
144
|
+
* @returns {any} Argument without the placeholders.
|
|
145
|
+
*/
|
|
146
|
+
const cleanReplaceArg = (arg) => {
|
|
147
|
+
if (!Array.isArray(arg)) {
|
|
148
|
+
return arg;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if ('index' in arg) {
|
|
152
|
+
return cleanMatch(/** @type {RegExpMatchArray} */ (arg));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return arg.every((item) => typeof item === 'string')
|
|
156
|
+
? arg.map((line) => removeImportPlaceholders(line))
|
|
157
|
+
: arg;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Make an editor component’s transformer import the Markdown without the placeholders added by
|
|
162
|
+
* `addImportPlaceholders()`, which `removePlaceholders()` can’t remove from the properties of the
|
|
163
|
+
* component node it creates, like the alt text of an image. The line of an HTML block starts with
|
|
164
|
+
* a placeholder, so the patterns of a component, like `^<aside>`, are matched after it.
|
|
165
|
+
* @param {Transformer} transformer Transformer.
|
|
166
|
+
* @returns {Transformer} Transformer.
|
|
167
|
+
*/
|
|
168
|
+
export const removePlaceholdersFromMatch = (transformer) => {
|
|
169
|
+
/** @type {any} */
|
|
170
|
+
const {
|
|
171
|
+
type,
|
|
172
|
+
handleImportAfterStartMatch,
|
|
173
|
+
importRegExp,
|
|
174
|
+
regExp,
|
|
175
|
+
regExpStart,
|
|
176
|
+
regExpEnd,
|
|
177
|
+
replace,
|
|
178
|
+
} = transformer;
|
|
179
|
+
|
|
180
|
+
/** @type {Record<string, any>} */
|
|
181
|
+
const overrides = {};
|
|
182
|
+
|
|
183
|
+
if (handleImportAfterStartMatch) {
|
|
184
|
+
/**
|
|
185
|
+
* Import the lines without the placeholders.
|
|
186
|
+
* @param {{ lines: string[] }} args Arguments.
|
|
187
|
+
* @returns {any} Result.
|
|
188
|
+
*/
|
|
189
|
+
overrides.handleImportAfterStartMatch = (args) =>
|
|
190
|
+
handleImportAfterStartMatch({
|
|
191
|
+
...args,
|
|
192
|
+
lines: args.lines.map((line) => removeImportPlaceholders(line)),
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (importRegExp) {
|
|
197
|
+
overrides.importRegExp = skipHtmlBlockPlaceholder(importRegExp, false);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// The regular expression of an element is matched against the start of a line on import
|
|
201
|
+
if (type === 'element' && regExp) {
|
|
202
|
+
overrides.regExp = skipHtmlBlockPlaceholder(regExp, true);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (regExpStart) {
|
|
206
|
+
overrides.regExpStart = skipHtmlBlockPlaceholder(regExpStart, true);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (regExpEnd instanceof RegExp) {
|
|
210
|
+
overrides.regExpEnd = skipHtmlBlockPlaceholder(regExpEnd, true);
|
|
211
|
+
} else if (regExpEnd?.regExp) {
|
|
212
|
+
overrides.regExpEnd = {
|
|
213
|
+
...regExpEnd,
|
|
214
|
+
regExp: skipHtmlBlockPlaceholder(regExpEnd.regExp, true),
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (replace) {
|
|
219
|
+
/**
|
|
220
|
+
* Import the match and the lines without the placeholders.
|
|
221
|
+
* @param {...any} args Arguments.
|
|
222
|
+
* @returns {any} Result.
|
|
223
|
+
*/
|
|
224
|
+
overrides.replace = (...args) => replace(...args.map(cleanReplaceArg));
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return /** @type {Transformer} */ ({ ...transformer, ...overrides });
|
|
228
|
+
};
|
|
@@ -26,5 +26,5 @@ export function findUnsupportedNode(editor: LexicalEditor, dom: Document): Eleme
|
|
|
26
26
|
export function exportHtml(editor: LexicalEditor): string;
|
|
27
27
|
import type { DOMExportOutputMap } from 'lexical';
|
|
28
28
|
import type { LexicalEditor } from 'lexical';
|
|
29
|
-
import type { TextEditorComponent } from '
|
|
29
|
+
import type { TextEditorComponent } from '../../../typedefs.js';
|
|
30
30
|
import type { DOMConversionMap } from 'lexical';
|
|
@@ -16,7 +16,7 @@ import {
|
|
|
16
16
|
TabNode,
|
|
17
17
|
TextNode,
|
|
18
18
|
} from 'lexical';
|
|
19
|
-
import { isPlainLanguage } from '
|
|
19
|
+
import { isPlainLanguage } from '../shiki/facade.js';
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* @import {
|
|
@@ -29,7 +29,7 @@ import { isPlainLanguage } from './shiki/facade.js';
|
|
|
29
29
|
* LexicalEditor,
|
|
30
30
|
* LexicalNode,
|
|
31
31
|
* } from 'lexical';
|
|
32
|
-
* @import { TextEditorComponent } from '
|
|
32
|
+
* @import { TextEditorComponent } from '../../../typedefs.js';
|
|
33
33
|
*/
|
|
34
34
|
|
|
35
35
|
/**
|
|
@@ -147,10 +147,143 @@ const exportElementNode = (editor, node) => {
|
|
|
147
147
|
};
|
|
148
148
|
};
|
|
149
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
|
+
|
|
150
282
|
/**
|
|
151
283
|
* Export a paragraph node. Empty paragraphs at the end are left out, as they have no content; one
|
|
152
284
|
* may be added just by clicking below a block decorator. A paragraph that is the only child of a
|
|
153
|
-
* table cell is exported without the `<p>` element, unless it’s aligned.
|
|
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.
|
|
154
287
|
* @param {LexicalEditor} editor Editor instance.
|
|
155
288
|
* @param {ParagraphNode} node Paragraph node.
|
|
156
289
|
* @returns {DOMExportOutput} Export output.
|
|
@@ -170,7 +303,28 @@ const exportParagraphNode = (editor, node) => {
|
|
|
170
303
|
return { element: null };
|
|
171
304
|
}
|
|
172
305
|
|
|
173
|
-
|
|
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
|
+
};
|
|
174
328
|
};
|
|
175
329
|
|
|
176
330
|
/**
|
|
@@ -505,22 +659,6 @@ const findContentInLine = (text, forward) => {
|
|
|
505
659
|
}
|
|
506
660
|
};
|
|
507
661
|
|
|
508
|
-
/**
|
|
509
|
-
* Check if the given node is within a `<pre>` element or an element with preformatted whitespace,
|
|
510
|
-
* whose text is imported by Lexical’s own conversion as is.
|
|
511
|
-
* @param {Node} node Node.
|
|
512
|
-
* @returns {boolean} Result.
|
|
513
|
-
*/
|
|
514
|
-
const isPreformatted = (node) => {
|
|
515
|
-
for (let element = node.parentElement; element; element = element.parentElement) {
|
|
516
|
-
if (element.localName === 'pre' || element.style.whiteSpace.startsWith('pre')) {
|
|
517
|
-
return true;
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
return false;
|
|
522
|
-
};
|
|
523
|
-
|
|
524
662
|
/**
|
|
525
663
|
* Convert a text node, collapsing whitespace like Lexical’s own conversion does, except that an
|
|
526
664
|
* inline editor component element counts as content. See {@link findContentInLine}.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function prepareMarkdown(value: string, enabledTransformers: Transformer[]): {
|
|
2
|
+
markdown: string;
|
|
3
|
+
languages: string[];
|
|
4
|
+
};
|
|
5
|
+
export function $importMarkdown(markdown: string, enabledTransformers: Transformer[], { toRoot }?: {
|
|
6
|
+
toRoot?: boolean | undefined;
|
|
7
|
+
}): LexicalNode[];
|
|
8
|
+
export function convertMarkdownToLexical(editor: LexicalEditor, value: string, enabledTransformers: Transformer[]): Promise<string | undefined>;
|
|
9
|
+
export function convertHtmlToLexical(editor: LexicalEditor, value: string): Promise<string | undefined>;
|
|
10
|
+
import type { Transformer } from '@lexical/markdown';
|
|
11
|
+
import type { LexicalNode } from 'lexical';
|
|
12
|
+
import type { LexicalEditor } from 'lexical';
|