@sveltia/ui 0.89.1 → 0.89.3
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 +2 -2
- 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} +2 -2
- 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/popup.svelte +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- 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/shiki-engine.js +1 -1
- package/dist/styles/mixins.scss +69 -0
- package/package.json +12 -10
- 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,327 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Importing a Markdown or HTML value to the editor.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { $generateNodesFromDOM as generateNodesFromDOM } from '@lexical/html';
|
|
6
|
+
import { $isLinkNode as isLinkNode } from '@lexical/link';
|
|
7
|
+
import {
|
|
8
|
+
HEADING,
|
|
9
|
+
$convertFromMarkdownString as convertFromMarkdownString,
|
|
10
|
+
$generateNodesFromMarkdownString as generateNodesFromMarkdownString,
|
|
11
|
+
} from '@lexical/markdown';
|
|
12
|
+
import {
|
|
13
|
+
$createParagraphNode as createParagraphNode,
|
|
14
|
+
$getRoot as getRoot,
|
|
15
|
+
$getSelection as getSelection,
|
|
16
|
+
HISTORY_MERGE_TAG,
|
|
17
|
+
$isDecoratorNode as isDecoratorNode,
|
|
18
|
+
$isElementNode as isElementNode,
|
|
19
|
+
$isTextNode as isTextNode,
|
|
20
|
+
} from 'lexical';
|
|
21
|
+
import { IMPORT_UPDATE_TAG } from '../constants.js';
|
|
22
|
+
import { exportHtml, findUnsupportedNode, getCodeLanguage } from './html.js';
|
|
23
|
+
import {
|
|
24
|
+
addImportPlaceholders,
|
|
25
|
+
convertSetextHeadings,
|
|
26
|
+
convertTildeFences,
|
|
27
|
+
padBlankBlockquoteLines,
|
|
28
|
+
removeImportPlaceholders,
|
|
29
|
+
splitMultilineFormatting,
|
|
30
|
+
} from './markdown.js';
|
|
31
|
+
import { loadCodeHighlighter } from '../shiki/preload.js';
|
|
32
|
+
import { CODE } from './transformers/code.js';
|
|
33
|
+
import { addCodeSpanSplit } from './transformers/formatted-code.js';
|
|
34
|
+
import { getImportedRawSyntax } from './transformers/raw-syntax.js';
|
|
35
|
+
import { exportMarkdown } from './update.js';
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @import { ElementNode, LexicalEditor, LexicalNode } from 'lexical';
|
|
39
|
+
* @import { Transformer } from '@lexical/markdown';
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Remove the placeholders added by `addImportPlaceholders()` from the imported nodes: the text, the
|
|
44
|
+
* Markdown of raw syntax, and the URL and title of a link. The text of raw syntax is restored from
|
|
45
|
+
* the Markdown it was imported from, as Lexical has removed its escapes, which are not escapes in
|
|
46
|
+
* raw HTML; see `RAW_HTML`.
|
|
47
|
+
* @param {LexicalNode[]} nodes Imported nodes.
|
|
48
|
+
*/
|
|
49
|
+
const removePlaceholders = (nodes) => {
|
|
50
|
+
nodes
|
|
51
|
+
.flatMap((node) => (isElementNode(node) ? node.getAllTextNodes() : node))
|
|
52
|
+
.filter((node) => isTextNode(node))
|
|
53
|
+
.forEach((node) => {
|
|
54
|
+
const text = node.getTextContent();
|
|
55
|
+
const newText = removeImportPlaceholders(getImportedRawSyntax(node) ?? text);
|
|
56
|
+
const link = node.getParent();
|
|
57
|
+
|
|
58
|
+
if (newText !== text) {
|
|
59
|
+
node.setTextContent(newText);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (isLinkNode(link)) {
|
|
63
|
+
const url = link.getURL();
|
|
64
|
+
const title = link.getTitle();
|
|
65
|
+
|
|
66
|
+
if (removeImportPlaceholders(url) !== url) {
|
|
67
|
+
link.setURL(removeImportPlaceholders(url));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (title && removeImportPlaceholders(title) !== title) {
|
|
71
|
+
link.setTitle(removeImportPlaceholders(title));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Prepare Markdown for the import: convert the syntax Lexical doesn’t support to the one it does,
|
|
79
|
+
* if the element is enabled, and list the programming languages used in the code blocks.
|
|
80
|
+
* @param {string} value Markdown.
|
|
81
|
+
* @param {Transformer[]} enabledTransformers List of enabled Markdown transformers.
|
|
82
|
+
* @returns {{ markdown: string, languages: string[] }} Normalized Markdown and the languages.
|
|
83
|
+
*/
|
|
84
|
+
export const prepareMarkdown = (value, enabledTransformers) => {
|
|
85
|
+
// Tilde code fences are converted to backtick ones, and setext headings to ATX ones. Otherwise
|
|
86
|
+
// the syntax is kept as text, as written. A transformer is found by its regular expression,
|
|
87
|
+
// because the transformers can come wrapped in a `$state` proxy, which leaves a regular
|
|
88
|
+
// expression as is but not the transformer object itself
|
|
89
|
+
const hasCodeBlocks = enabledTransformers.some(
|
|
90
|
+
(transformer) => 'regExpStart' in transformer && transformer.regExpStart === CODE.regExpStart,
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
const hasHeadings = enabledTransformers.some(
|
|
94
|
+
(transformer) => 'regExp' in transformer && transformer.regExp === HEADING.regExp,
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
const fencedValue = hasCodeBlocks ? convertTildeFences(value) : value;
|
|
98
|
+
const markdown = hasHeadings ? convertSetextHeadings(fencedValue) : fencedValue;
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
markdown,
|
|
102
|
+
// The language is the leading word of the info string, like `js` in ```` ``` js title=x ````;
|
|
103
|
+
// see `CODE`
|
|
104
|
+
languages: [...markdown.matchAll(/^`{3,}[ \t]*(?<lang>[\w-]+)/gm)].map(
|
|
105
|
+
({ groups: { lang = 'plain' } = {} }) => lang,
|
|
106
|
+
),
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Convert Markdown prepared with {@link prepareMarkdown} to Lexical nodes, either replacing the
|
|
112
|
+
* content of the root or returning the nodes to be inserted somewhere. Called within an editor
|
|
113
|
+
* update.
|
|
114
|
+
* @param {string} markdown Markdown.
|
|
115
|
+
* @param {Transformer[]} enabledTransformers List of enabled Markdown transformers.
|
|
116
|
+
* @param {object} [options] Options.
|
|
117
|
+
* @param {boolean} [options.toRoot] Whether to replace the content of the root.
|
|
118
|
+
* @returns {LexicalNode[]} Imported nodes.
|
|
119
|
+
*/
|
|
120
|
+
export const $importMarkdown = (markdown, enabledTransformers, { toRoot = false } = {}) => {
|
|
121
|
+
// Split multiline formatting into separate lines to prevent Markdown parsing issues
|
|
122
|
+
const splitValue = splitMultilineFormatting(markdown);
|
|
123
|
+
// Add placeholders so Lexical reads the backslashes, the tabs in code spans and raw HTML right
|
|
124
|
+
const markedValue = addImportPlaceholders(splitValue);
|
|
125
|
+
// Pad blank blockquote lines so they are not imported as literal `>` text
|
|
126
|
+
const paddedValue = padBlankBlockquoteLines(markedValue);
|
|
127
|
+
// Keep the formatting around a code span after other formatting
|
|
128
|
+
const transformers = addCodeSpanSplit(enabledTransformers);
|
|
129
|
+
/** @type {LexicalNode[]} */
|
|
130
|
+
let nodes;
|
|
131
|
+
|
|
132
|
+
if (toRoot) {
|
|
133
|
+
convertFromMarkdownString(paddedValue, transformers);
|
|
134
|
+
nodes = getRoot().getChildren();
|
|
135
|
+
} else {
|
|
136
|
+
nodes = generateNodesFromMarkdownString(paddedValue, transformers);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// The text of raw HTML and raw syntax is restored as well. The lines of an HTML block have a
|
|
140
|
+
// placeholder. Other raw syntax, like an autolink or a definition label, only differs from its
|
|
141
|
+
// text if Lexical has decoded an escape or a character reference in it
|
|
142
|
+
if (markedValue !== splitValue || /[\\&]/.test(markedValue)) {
|
|
143
|
+
removePlaceholders(nodes);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return nodes;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Number of the latest import started for each editor.
|
|
151
|
+
* @type {WeakMap<LexicalEditor, number>}
|
|
152
|
+
*/
|
|
153
|
+
const latestImports = new WeakMap();
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Import a value to the editor, replacing the current content.
|
|
157
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
158
|
+
* @param {object} args Arguments.
|
|
159
|
+
* @param {string[]} args.languages Programming languages used in the code blocks.
|
|
160
|
+
* @param {() => void} args.$import Function to convert the value to Lexical nodes, called within an
|
|
161
|
+
* editor update.
|
|
162
|
+
* @param {() => string} args.$export Function to convert the editor content back to the value.
|
|
163
|
+
* @param {string} args.formatName Name of the value format, used in the error message.
|
|
164
|
+
* @returns {Promise<string | undefined>} The value as the editor exports it once imported, which
|
|
165
|
+
* can differ in style from the given value. `undefined` if another import has been started for the
|
|
166
|
+
* same editor in the meantime, in which case this value is skipped.
|
|
167
|
+
* @throws {Error} Failed to convert the value to Lexical nodes.
|
|
168
|
+
*/
|
|
169
|
+
const importValue = async (editor, { languages, $import, $export, formatName }) => {
|
|
170
|
+
const importId = (latestImports.get(editor) ?? 0) + 1;
|
|
171
|
+
|
|
172
|
+
latestImports.set(editor, importId);
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Check if a newer import has been started while waiting for the highlighter. Imports can finish
|
|
176
|
+
* out of order, as only those with code blocks have to wait, and an older value must not
|
|
177
|
+
* overwrite a newer one.
|
|
178
|
+
* @returns {boolean} Result.
|
|
179
|
+
*/
|
|
180
|
+
const isSuperseded = () => latestImports.get(editor) !== importId;
|
|
181
|
+
|
|
182
|
+
try {
|
|
183
|
+
// Preload the highlighter for every language used in the document, so code blocks are
|
|
184
|
+
// highlighted as soon as they appear rather than a moment later
|
|
185
|
+
await Promise.all(languages.map(async (lang) => loadCodeHighlighter(lang)));
|
|
186
|
+
} catch (ex) {
|
|
187
|
+
if (isSuperseded()) {
|
|
188
|
+
return undefined;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
throw ex;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (isSuperseded()) {
|
|
195
|
+
return undefined;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Lexical commits an empty state when the root element is attached, and the history keeps it as
|
|
199
|
+
// the current entry. An empty state can’t be restored, so undoing to it throws an error. Merge
|
|
200
|
+
// the first import into that entry instead of pushing it onto the undo stack
|
|
201
|
+
const isEmpty = editor.getEditorState().isEmpty();
|
|
202
|
+
/** @type {unknown} */
|
|
203
|
+
let error;
|
|
204
|
+
|
|
205
|
+
editor.update(
|
|
206
|
+
() => {
|
|
207
|
+
try {
|
|
208
|
+
$import();
|
|
209
|
+
} catch (ex) {
|
|
210
|
+
error = ex;
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
// Tell the import from a change made by the user, and commit it right away, so the node
|
|
214
|
+
// transforms, e.g. the one giving a code block its default language, have run by the time the
|
|
215
|
+
// content is exported below
|
|
216
|
+
{ tag: isEmpty ? [IMPORT_UPDATE_TAG, HISTORY_MERGE_TAG] : IMPORT_UPDATE_TAG, discrete: true },
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
if (error) {
|
|
220
|
+
throw new Error(`Failed to convert ${formatName}`, { cause: error });
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return editor.read($export);
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Convert Markdown to Lexical nodes.
|
|
228
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
229
|
+
* @param {string} value Current Markdown value.
|
|
230
|
+
* @param {Transformer[]} enabledTransformers List of enabled Markdown transformers.
|
|
231
|
+
* @returns {Promise<string | undefined>} The value as the editor exports it once imported, which
|
|
232
|
+
* can differ in style from the given value, e.g. `_text_` for `*text*`. `undefined` if another
|
|
233
|
+
* import has been started for the same editor in the meantime, in which case this value is skipped.
|
|
234
|
+
* @throws {Error} Failed to convert the value to Lexical nodes.
|
|
235
|
+
*/
|
|
236
|
+
export const convertMarkdownToLexical = async (editor, value, enabledTransformers) => {
|
|
237
|
+
const { markdown, languages } = prepareMarkdown(value, enabledTransformers);
|
|
238
|
+
|
|
239
|
+
return importValue(editor, {
|
|
240
|
+
languages,
|
|
241
|
+
/**
|
|
242
|
+
* Import the Markdown.
|
|
243
|
+
*/
|
|
244
|
+
$import: () => {
|
|
245
|
+
$importMarkdown(markdown, enabledTransformers, { toRoot: true });
|
|
246
|
+
},
|
|
247
|
+
/**
|
|
248
|
+
* Export the content as Markdown.
|
|
249
|
+
* @returns {string} Markdown.
|
|
250
|
+
*/
|
|
251
|
+
$export: () => exportMarkdown(enabledTransformers),
|
|
252
|
+
formatName: 'Markdown',
|
|
253
|
+
});
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Convert HTML to Lexical nodes. Inline content outside any block, like bare text, is wrapped with
|
|
258
|
+
* a paragraph. An element that none of the registered nodes can import, like `<img>` or a heading
|
|
259
|
+
* with the heading buttons disabled, as well as a comment, causes an error instead of being
|
|
260
|
+
* silently dropped.
|
|
261
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
262
|
+
* @param {string} value Current HTML value.
|
|
263
|
+
* @returns {Promise<string | undefined>} The value as the editor exports it once imported, which
|
|
264
|
+
* can differ in style from the given value, e.g. `<strong>` for `<b>`. `undefined` if another
|
|
265
|
+
* import has been started for the same editor in the meantime, in which case this value is skipped.
|
|
266
|
+
* @throws {Error} Failed to convert the value to Lexical nodes.
|
|
267
|
+
*/
|
|
268
|
+
export const convertHtmlToLexical = async (editor, value) => {
|
|
269
|
+
const dom = new DOMParser().parseFromString(value, 'text/html');
|
|
270
|
+
|
|
271
|
+
return importValue(editor, {
|
|
272
|
+
languages: [...dom.querySelectorAll('pre > code')]
|
|
273
|
+
.map(getCodeLanguage)
|
|
274
|
+
.filter((lang) => lang !== undefined),
|
|
275
|
+
/**
|
|
276
|
+
* Import the HTML.
|
|
277
|
+
* @throws {Error} The HTML has an element the editor cannot handle.
|
|
278
|
+
*/
|
|
279
|
+
$import: () => {
|
|
280
|
+
const unsupportedNode = findUnsupportedNode(editor, dom);
|
|
281
|
+
|
|
282
|
+
if (unsupportedNode) {
|
|
283
|
+
throw new Error(
|
|
284
|
+
unsupportedNode.nodeType === Node.COMMENT_NODE
|
|
285
|
+
? 'Unsupported comment'
|
|
286
|
+
: `Unsupported element: <${/** @type {Element} */ (unsupportedNode).localName}>`,
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const root = getRoot();
|
|
291
|
+
/** @type {ElementNode | null} */
|
|
292
|
+
let paragraph = null;
|
|
293
|
+
|
|
294
|
+
root.clear();
|
|
295
|
+
|
|
296
|
+
generateNodesFromDOM(editor, dom).forEach((node) => {
|
|
297
|
+
if (isElementNode(node) ? !node.isInline() : isDecoratorNode(node) && !node.isInline()) {
|
|
298
|
+
paragraph = null;
|
|
299
|
+
root.append(node);
|
|
300
|
+
} else {
|
|
301
|
+
if (!paragraph) {
|
|
302
|
+
paragraph = createParagraphNode();
|
|
303
|
+
root.append(paragraph);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
paragraph.append(node);
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
// Like the Markdown import, there should be at least one paragraph
|
|
311
|
+
// @see https://github.com/facebook/lexical/issues/2308
|
|
312
|
+
if (root.isEmpty()) {
|
|
313
|
+
root.append(createParagraphNode());
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (getSelection() !== null) {
|
|
317
|
+
root.selectStart();
|
|
318
|
+
}
|
|
319
|
+
},
|
|
320
|
+
/**
|
|
321
|
+
* Export the content as HTML.
|
|
322
|
+
* @returns {string} HTML.
|
|
323
|
+
*/
|
|
324
|
+
$export: () => exportHtml(editor),
|
|
325
|
+
formatName: 'HTML',
|
|
326
|
+
});
|
|
327
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export function pasteMarkdown(editor: LexicalEditor, value: string, enabledTransformers: Transformer[]): Promise<void>;
|
|
2
|
+
export function registerPasteAsMarkdown(editor: LexicalEditor, enabledTransformers: Transformer[]): () => void;
|
|
3
|
+
import type { LexicalEditor } from 'lexical';
|
|
4
|
+
import type { Transformer } from '@lexical/markdown';
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pasting Markdown, either as is or with the Paste as Markdown shortcut.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { objectKlassEquals } from '@lexical/utils';
|
|
6
|
+
import { isURL } from '@sveltia/utils/string';
|
|
7
|
+
import { isMac, matchesShortcuts } from '@sveltia/utils/events';
|
|
8
|
+
import {
|
|
9
|
+
COMMAND_PRIORITY_NORMAL,
|
|
10
|
+
$getSelection as getSelection,
|
|
11
|
+
$setSelection as setSelection,
|
|
12
|
+
KEY_DOWN_COMMAND,
|
|
13
|
+
PASTE_COMMAND,
|
|
14
|
+
PASTE_TAG,
|
|
15
|
+
} from 'lexical';
|
|
16
|
+
import { loadCodeHighlighter } from '../shiki/preload.js';
|
|
17
|
+
import { $importMarkdown, prepareMarkdown } from './import.js';
|
|
18
|
+
import { mergeUnregisters } from '../utils.js';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @import { LexicalEditor, LexicalNode } from 'lexical';
|
|
22
|
+
* @import { Transformer } from '@lexical/markdown';
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Insert the given Markdown at the selection, converted to Lexical nodes, as if it were pasted. The
|
|
27
|
+
* content stays as it is if the editor is read-only or has no selection.
|
|
28
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
29
|
+
* @param {string} value Markdown.
|
|
30
|
+
* @param {Transformer[]} enabledTransformers List of enabled Markdown transformers.
|
|
31
|
+
* @returns {Promise<void>} Nothing.
|
|
32
|
+
*/
|
|
33
|
+
export const pasteMarkdown = async (editor, value, enabledTransformers) => {
|
|
34
|
+
const { markdown, languages } = prepareMarkdown(value, enabledTransformers);
|
|
35
|
+
|
|
36
|
+
// Preload the highlighter like the import does, but carry on without it if it fails to load: the
|
|
37
|
+
// code is highlighted once it’s available, if ever
|
|
38
|
+
await Promise.allSettled(languages.map((lang) => loadCodeHighlighter(lang)));
|
|
39
|
+
|
|
40
|
+
if (!editor.isEditable()) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
editor.update(
|
|
45
|
+
() => {
|
|
46
|
+
// The Markdown import moves the selection to the nodes it creates, so keep a copy to insert
|
|
47
|
+
// the nodes where the caret was
|
|
48
|
+
const selection = getSelection()?.clone();
|
|
49
|
+
|
|
50
|
+
if (!selection) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** @type {LexicalNode[] | undefined} */
|
|
55
|
+
let nodes;
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
nodes = $importMarkdown(markdown, enabledTransformers);
|
|
59
|
+
} catch (ex) {
|
|
60
|
+
// The browser paste has been canceled, so paste the text as is rather than nothing
|
|
61
|
+
// eslint-disable-next-line no-console
|
|
62
|
+
console.error(ex);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
setSelection(selection);
|
|
66
|
+
|
|
67
|
+
if (nodes) {
|
|
68
|
+
selection.insertNodes(nodes);
|
|
69
|
+
} else {
|
|
70
|
+
selection.insertRawText(value);
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
{ tag: PASTE_TAG },
|
|
74
|
+
);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* How long to wait for the `paste` event after a Paste as Markdown shortcut, in milliseconds.
|
|
79
|
+
*/
|
|
80
|
+
const PASTE_AS_MARKDOWN_TIMEOUT = 1000;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Keyboard shortcuts to paste the text on the clipboard as Markdown: the browser’s own shortcuts to
|
|
84
|
+
* paste plain text, Accel+Shift+V, plus Command+Option+Shift+V, the macOS Paste and Match Style.
|
|
85
|
+
* @returns {string} Shortcuts.
|
|
86
|
+
*/
|
|
87
|
+
const getPasteAsMarkdownShortcuts = () =>
|
|
88
|
+
isMac() ? 'Meta+Shift+V Meta+Alt+Shift+V' : 'Ctrl+Shift+V';
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Register the keyboard shortcuts to paste the text on the clipboard as Markdown. A regular paste
|
|
92
|
+
* prefers the HTML on the clipboard, so text copied from a code editor or a terminal ends up in a
|
|
93
|
+
* code block, while Markdown pasted as plain text is kept as literal text. The shortcuts make the
|
|
94
|
+
* browser paste plain text, which is then converted from Markdown. The browser does the paste,
|
|
95
|
+
* because a script can’t read the clipboard after a key combination in every browser: Firefox
|
|
96
|
+
* doesn’t allow it after a key pressed with Ctrl, Alt or Command.
|
|
97
|
+
* @param {LexicalEditor} editor Editor instance.
|
|
98
|
+
* @param {Transformer[]} enabledTransformers List of enabled Markdown transformers.
|
|
99
|
+
* @returns {() => void} Cleanup handler.
|
|
100
|
+
*/
|
|
101
|
+
export const registerPasteAsMarkdown = (editor, enabledTransformers) => {
|
|
102
|
+
const shortcuts = getPasteAsMarkdownShortcuts();
|
|
103
|
+
/** Whether the paste that follows was started with one of the shortcuts. */
|
|
104
|
+
let pasteAsMarkdown = false;
|
|
105
|
+
let timer = 0;
|
|
106
|
+
|
|
107
|
+
return mergeUnregisters(
|
|
108
|
+
() => {
|
|
109
|
+
clearTimeout(timer);
|
|
110
|
+
},
|
|
111
|
+
editor.registerCommand(
|
|
112
|
+
KEY_DOWN_COMMAND,
|
|
113
|
+
(event) => {
|
|
114
|
+
// Any other key cancels the shortcut. Chrome on macOS fires the `paste` event in a later
|
|
115
|
+
// task, so wait a moment for it, but not too long, in case the browser doesn’t paste with
|
|
116
|
+
// the shortcut, like Safari with Command+Shift+V
|
|
117
|
+
pasteAsMarkdown = matchesShortcuts(event, shortcuts);
|
|
118
|
+
clearTimeout(timer);
|
|
119
|
+
|
|
120
|
+
if (pasteAsMarkdown) {
|
|
121
|
+
timer = window.setTimeout(() => {
|
|
122
|
+
pasteAsMarkdown = false;
|
|
123
|
+
}, PASTE_AS_MARKDOWN_TIMEOUT);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Let the browser paste
|
|
127
|
+
return false;
|
|
128
|
+
},
|
|
129
|
+
COMMAND_PRIORITY_NORMAL,
|
|
130
|
+
),
|
|
131
|
+
editor.registerCommand(
|
|
132
|
+
PASTE_COMMAND,
|
|
133
|
+
(event) => {
|
|
134
|
+
if (!pasteAsMarkdown) {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
pasteAsMarkdown = false;
|
|
139
|
+
|
|
140
|
+
const isClipboardEvent = objectKlassEquals(event, ClipboardEvent);
|
|
141
|
+
|
|
142
|
+
// Safari pastes with an `insertFromPaste` input event instead of a `paste` event
|
|
143
|
+
const dataTransfer = isClipboardEvent
|
|
144
|
+
? /** @type {ClipboardEvent} */ (event).clipboardData
|
|
145
|
+
: objectKlassEquals(event, InputEvent)
|
|
146
|
+
? /** @type {InputEvent} */ (event).dataTransfer
|
|
147
|
+
: null;
|
|
148
|
+
|
|
149
|
+
const text = dataTransfer?.getData('text/plain');
|
|
150
|
+
|
|
151
|
+
// Leave a URL to the link paste handler, which links the selected text. It only handles a
|
|
152
|
+
// `paste` event, so Safari still pastes a URL as Markdown
|
|
153
|
+
if (!text || (isClipboardEvent && isURL(text.trim()))) {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
event.preventDefault();
|
|
158
|
+
pasteMarkdown(editor, text, enabledTransformers);
|
|
159
|
+
|
|
160
|
+
return true;
|
|
161
|
+
},
|
|
162
|
+
// Before the link paste handler and Lexical’s own one
|
|
163
|
+
COMMAND_PRIORITY_NORMAL,
|
|
164
|
+
),
|
|
165
|
+
);
|
|
166
|
+
};
|
|
@@ -192,10 +192,10 @@ export const RAW_SYNTAX = {
|
|
|
192
192
|
* it, inline HTML, like a tag, an autolink or a footnote reference, which would otherwise be
|
|
193
193
|
* escaped on export as text that only looks like one. The placeholder is left out of the match, so
|
|
194
194
|
* an editor component that matches the line after it, which comes first, still takes precedence;
|
|
195
|
-
* see `removePlaceholdersFromMatch()` in `
|
|
196
|
-
* formatting marker, though: Lexical prefers formatting to a text match that covers the
|
|
197
|
-
* so a line like `*foo*` would otherwise be imported as italic. A component pattern
|
|
198
|
-
* with one.
|
|
195
|
+
* see `removePlaceholdersFromMatch()` in `editor-components.js`. It’s included if the line starts
|
|
196
|
+
* with a formatting marker, though: Lexical prefers formatting to a text match that covers the
|
|
197
|
+
* same text, so a line like `*foo*` would otherwise be imported as italic. A component pattern
|
|
198
|
+
* doesn’t start with one.
|
|
199
199
|
*/
|
|
200
200
|
const RAW_HTML_REGEX = new RegExp(
|
|
201
201
|
[
|