@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.
Files changed (100) hide show
  1. package/dist/components/checkbox/checkbox.svelte +5 -8
  2. package/dist/components/dialog/dialog.svelte +2 -2
  3. package/dist/components/emoji/emoji-suggestions.svelte +7 -7
  4. package/dist/components/grid/grid-col-header.svelte +4 -0
  5. package/dist/components/grid/grid-head.svelte +1 -1
  6. package/dist/components/grid/grid-head.svelte.d.ts +1 -1
  7. package/dist/components/radio/radio.svelte +9 -9
  8. package/dist/components/resizable-pane/resizable-pane-group.svelte +5 -20
  9. package/dist/components/resizable-pane/sizing.d.ts +6 -0
  10. package/dist/components/resizable-pane/sizing.js +31 -0
  11. package/dist/components/text-editor/code-editor.svelte +22 -39
  12. package/dist/components/text-editor/constants.js +3 -3
  13. package/dist/components/text-editor/conversion/editor-components.d.ts +5 -0
  14. package/dist/components/text-editor/conversion/editor-components.js +228 -0
  15. package/dist/components/text-editor/{html.d.ts → conversion/html.d.ts} +1 -1
  16. package/dist/components/text-editor/{html.js → conversion/html.js} +2 -2
  17. package/dist/components/text-editor/conversion/import.d.ts +12 -0
  18. package/dist/components/text-editor/conversion/import.js +327 -0
  19. package/dist/components/text-editor/conversion/paste.d.ts +4 -0
  20. package/dist/components/text-editor/conversion/paste.js +166 -0
  21. package/dist/components/text-editor/{transformers → conversion/transformers}/raw-syntax.js +4 -4
  22. package/dist/components/text-editor/conversion/transformers/table.js +342 -0
  23. package/dist/components/text-editor/conversion/update.d.ts +6 -0
  24. package/dist/components/text-editor/conversion/update.js +125 -0
  25. package/dist/components/text-editor/core.d.ts +50 -26
  26. package/dist/components/text-editor/core.js +133 -1694
  27. package/dist/components/text-editor/editing/caret.d.ts +4 -0
  28. package/dist/components/text-editor/editing/caret.js +248 -0
  29. package/dist/components/text-editor/editing/commands.d.ts +5 -0
  30. package/dist/components/text-editor/editing/commands.js +205 -0
  31. package/dist/components/text-editor/editing/link.d.ts +19 -0
  32. package/dist/components/text-editor/editing/link.js +222 -0
  33. package/dist/components/text-editor/{raw-markdown.d.ts → editing/raw-markdown.d.ts} +3 -3
  34. package/dist/components/text-editor/{raw-markdown.js → editing/raw-markdown.js} +2 -2
  35. package/dist/components/text-editor/editing/selection.d.ts +5 -0
  36. package/dist/components/text-editor/editing/selection.js +122 -0
  37. package/dist/components/text-editor/editing/single-line.d.ts +8 -0
  38. package/dist/components/text-editor/editing/single-line.js +190 -0
  39. package/dist/components/text-editor/{table.js → editing/table.js} +1 -1
  40. package/dist/components/text-editor/lexical-root.svelte +61 -10
  41. package/dist/components/text-editor/shiki/preload.d.ts +1 -0
  42. package/dist/components/text-editor/shiki/preload.js +30 -0
  43. package/dist/components/text-editor/shiki/version.d.ts +1 -1
  44. package/dist/components/text-editor/shiki/version.js +1 -1
  45. package/dist/components/text-editor/store.svelte.d.ts +5 -0
  46. package/dist/components/text-editor/store.svelte.js +55 -1
  47. package/dist/components/text-editor/text-editor.svelte +29 -41
  48. package/dist/components/text-editor/toolbar/align-menu-button.svelte +1 -1
  49. package/dist/components/text-editor/toolbar/code-language-switcher.svelte +2 -1
  50. package/dist/components/text-editor/toolbar/format-text-button.svelte +6 -2
  51. package/dist/components/text-editor/toolbar/insert-component.js +1 -1
  52. package/dist/components/text-editor/toolbar/insert-horizontal-rule-button.svelte +4 -4
  53. package/dist/components/text-editor/toolbar/insert-link-button.svelte +3 -2
  54. package/dist/components/text-editor/toolbar/insert-table-button.svelte +7 -3
  55. package/dist/components/text-editor/toolbar/table-action-menu.svelte +3 -3
  56. package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte +1 -1
  57. package/dist/components/text-editor/toolbar/toggle-block-menu-item.svelte +2 -2
  58. package/dist/components/text-editor/utils.d.ts +1 -0
  59. package/dist/components/text-editor/utils.js +17 -0
  60. package/dist/components/text-editor/{emoji-autocomplete.svelte → widgets/emoji-autocomplete.svelte} +3 -3
  61. package/dist/components/text-editor/{floating-link-editor.svelte → widgets/floating-link-editor.svelte} +36 -79
  62. package/dist/components/text-editor/{table-cell-menu-button.svelte → widgets/table-cell-menu-button.svelte} +15 -5
  63. package/dist/components/text-field/date-time-input.svelte +8 -8
  64. package/dist/components/text-field/text-area.svelte +10 -10
  65. package/dist/components/text-field/text-input.svelte +7 -7
  66. package/dist/components/util/popup.svelte +1 -1
  67. package/dist/index.d.ts +1 -1
  68. package/dist/index.js +1 -1
  69. package/dist/services/label.d.ts +8 -0
  70. package/dist/services/label.js +15 -0
  71. package/dist/services/popup-position.d.ts +9 -0
  72. package/dist/services/popup-position.js +52 -55
  73. package/dist/shiki-engine.js +1 -1
  74. package/dist/styles/mixins.scss +69 -0
  75. package/package.json +12 -10
  76. package/dist/components/text-editor/transformers/table.js +0 -296
  77. /package/dist/components/text-editor/{markdown.d.ts → conversion/markdown.d.ts} +0 -0
  78. /package/dist/components/text-editor/{markdown.js → conversion/markdown.js} +0 -0
  79. /package/dist/components/text-editor/{transformers → conversion/transformers}/block-escape.d.ts +0 -0
  80. /package/dist/components/text-editor/{transformers → conversion/transformers}/block-escape.js +0 -0
  81. /package/dist/components/text-editor/{transformers → conversion/transformers}/code.d.ts +0 -0
  82. /package/dist/components/text-editor/{transformers → conversion/transformers}/code.js +0 -0
  83. /package/dist/components/text-editor/{transformers → conversion/transformers}/formatted-code.d.ts +0 -0
  84. /package/dist/components/text-editor/{transformers → conversion/transformers}/formatted-code.js +0 -0
  85. /package/dist/components/text-editor/{transformers → conversion/transformers}/hr.d.ts +0 -0
  86. /package/dist/components/text-editor/{transformers → conversion/transformers}/hr.js +0 -0
  87. /package/dist/components/text-editor/{transformers → conversion/transformers}/inline-code.d.ts +0 -0
  88. /package/dist/components/text-editor/{transformers → conversion/transformers}/inline-code.js +0 -0
  89. /package/dist/components/text-editor/{transformers → conversion/transformers}/link.d.ts +0 -0
  90. /package/dist/components/text-editor/{transformers → conversion/transformers}/link.js +0 -0
  91. /package/dist/components/text-editor/{transformers → conversion/transformers}/raw-syntax.d.ts +0 -0
  92. /package/dist/components/text-editor/{transformers → conversion/transformers}/table.d.ts +0 -0
  93. /package/dist/components/text-editor/{alignment.d.ts → editing/alignment.d.ts} +0 -0
  94. /package/dist/components/text-editor/{alignment.js → editing/alignment.js} +0 -0
  95. /package/dist/components/text-editor/{block.d.ts → editing/block.d.ts} +0 -0
  96. /package/dist/components/text-editor/{block.js → editing/block.js} +0 -0
  97. /package/dist/components/text-editor/{table.d.ts → editing/table.d.ts} +0 -0
  98. /package/dist/components/text-editor/{emoji-autocomplete.svelte.d.ts → widgets/emoji-autocomplete.svelte.d.ts} +0 -0
  99. /package/dist/components/text-editor/{floating-link-editor.svelte.d.ts → widgets/floating-link-editor.svelte.d.ts} +0 -0
  100. /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,342 @@
1
+ // Adopted from https://github.com/facebook/lexical/blob/main/packages/lexical-playground/src/plugins/MarkdownTransformers/index.ts
2
+
3
+ /* eslint-disable jsdoc/require-jsdoc */
4
+ /* eslint-disable jsdoc/require-returns-description */
5
+ /* eslint-disable jsdoc/require-param-description */
6
+
7
+ import {
8
+ $convertFromMarkdownString as convertFromMarkdownString,
9
+ $convertToMarkdownString as convertToMarkdownString,
10
+ LINK as LEXICAL_LINK,
11
+ TRANSFORMERS,
12
+ } from '@lexical/markdown';
13
+ import {
14
+ $createTableCellNode as createTableCellNode,
15
+ $createTableNode as createTableNode,
16
+ $createTableRowNode as createTableRowNode,
17
+ $isTableCellNode as isTableCellNode,
18
+ $isTableNode as isTableNode,
19
+ $isTableRowNode as isTableRowNode,
20
+ TableCellHeaderStates,
21
+ TableCellNode,
22
+ TableNode,
23
+ TableRowNode,
24
+ } from '@lexical/table';
25
+ import { $isParagraphNode as isParagraphNode, $isTextNode as isTextNode } from 'lexical';
26
+
27
+ import { BLOCK_SYNTAX_ESCAPE } from './block-escape.js';
28
+ import { LEADING_INLINE_CODE } from './inline-code.js';
29
+ import { LINK } from './link.js';
30
+ import { RAW_HTML, RAW_SYNTAX } from './raw-syntax.js';
31
+
32
+ /**
33
+ * @import { ElementTransformer, TextMatchTransformer, Transformer } from '@lexical/markdown';
34
+ * @import { ElementFormatType, ElementNode } from 'lexical';
35
+ */
36
+
37
+ const TABLE_ROW_REG_EXP = /^(?:\|)(.+)(?:\|)\s?$/;
38
+ /**
39
+ * Regular expression to match a delimiter row, like `| --- | :-: |`. Each cell needs a hyphen, so a
40
+ * row of empty cells, like `| | |`, is an empty header row, not a delimiter row.
41
+ * @see https://github.github.com/gfm/#delimiter-row
42
+ */
43
+ const TABLE_ROW_DIVIDER_REG_EXP = /^(?:\|[ \t]*:?-+:?[ \t]*)+\|\s?$/;
44
+ /**
45
+ * Delimiter row cell for each column alignment, which is kept as the format of the column’s cells.
46
+ * @type {Record<string, string>}
47
+ */
48
+ const ALIGNMENT_DELIMITERS = { left: ':---', center: ':---:', right: '---:' };
49
+
50
+ /**
51
+ * Get the alignment of a column from its delimiter row cell, like `:-:`.
52
+ * @param {string} delimiter Delimiter row cell.
53
+ * @returns {ElementFormatType} Alignment, or an empty string for the default.
54
+ */
55
+ const getColumnAlignment = (delimiter) => {
56
+ const text = delimiter.trim();
57
+ const left = text.startsWith(':');
58
+ const right = text.endsWith(':');
59
+
60
+ return left && right ? 'center' : left ? 'left' : right ? 'right' : '';
61
+ };
62
+
63
+ /**
64
+ * Returns the number of columns in the table.
65
+ * @internal
66
+ * @param {TableNode} table
67
+ * @returns {number}
68
+ */
69
+ export const getTableColumnsSize = (table) => {
70
+ const row = table.getFirstChild();
71
+
72
+ return isTableRowNode(row) ? row.getChildrenSize() : 0;
73
+ };
74
+
75
+ /**
76
+ * Creates a table cell with the given text content.
77
+ * @param {string} textContent
78
+ * @param {Transformer[]} transformers Transformers to import the cell content with.
79
+ * @returns {TableCellNode}
80
+ */
81
+ const createTableCell = (textContent, transformers) => {
82
+ // An escaped pipe is part of the cell, even within inline code, like GitHub Flavored Markdown.
83
+ // The spaces around the content only separate it from the pipes
84
+ textContent = textContent.trim().replace(/\\n/g, '\n').replace(/\\\|/g, '|');
85
+
86
+ const cell = createTableCellNode(TableCellHeaderStates.NO_STATUS);
87
+
88
+ convertFromMarkdownString(textContent, transformers, cell);
89
+
90
+ return cell;
91
+ };
92
+
93
+ /**
94
+ * Maps the given text content to an array of table cells.
95
+ * @internal
96
+ * @param {string} textContent
97
+ * @param {Transformer[]} transformers Transformers to import the cell content with.
98
+ * @returns {TableCellNode[] | null}
99
+ */
100
+ export const mapToTableCells = (textContent, transformers) => {
101
+ const [, match] = textContent.match(TABLE_ROW_REG_EXP) ?? [];
102
+
103
+ if (!match) {
104
+ return null;
105
+ }
106
+
107
+ // Split the row at the pipes that are not escaped, which are preceded by an even number of
108
+ // backslashes
109
+ return match.split(/(?<=(?<!\\)(?:\\\\)*)\|/).map((text) => createTableCell(text, transformers));
110
+ };
111
+
112
+ /**
113
+ * Export a table row to Markdown, followed by a delimiter row if it’s a header row.
114
+ * @param {TableRowNode} row Table row.
115
+ * @param {Transformer[]} transformers Transformers to export the cell content with.
116
+ * @returns {string[]} Lines.
117
+ */
118
+ const exportTableRow = (row, transformers) => {
119
+ /** @type {string[]} */
120
+ const rowOutput = [];
121
+ /** @type {TableCellNode[]} */
122
+ const cells = [];
123
+ let isHeaderRow = false;
124
+
125
+ row.getChildren().forEach((cell) => {
126
+ // It’s TableCellNode so it’s just to make flow happy
127
+ if (isTableCellNode(cell)) {
128
+ rowOutput.push(
129
+ convertToMarkdownString(transformers, cell)
130
+ // Escape a pipe, which would otherwise end the cell, even within inline code
131
+ .replace(/\|/g, '\\|')
132
+ .replace(/\n/g, '\\n')
133
+ .trim(),
134
+ );
135
+ cells.push(cell);
136
+
137
+ if (cell.__headerState === TableCellHeaderStates.ROW) {
138
+ isHeaderRow = true;
139
+ }
140
+ }
141
+ });
142
+
143
+ const lines = [`| ${rowOutput.join(' | ')} |`];
144
+
145
+ if (isHeaderRow) {
146
+ lines.push(
147
+ `| ${cells.map((cell) => ALIGNMENT_DELIMITERS[cell.getFormatType()] ?? '---').join(' | ')} |`,
148
+ );
149
+ }
150
+
151
+ return lines;
152
+ };
153
+
154
+ /**
155
+ * Turn the last row of the table before a delimiter row into the header row, and remove the
156
+ * delimiter row. Nothing happens if there is no such table.
157
+ * @param {ElementNode} parentNode Paragraph holding the delimiter row.
158
+ * @param {string} textContent Delimiter row, like `| --- | :-: |`.
159
+ */
160
+ const $applyHeaderRow = (parentNode, textContent) => {
161
+ const table = parentNode.getPreviousSibling();
162
+
163
+ if (!table || !isTableNode(table)) {
164
+ return;
165
+ }
166
+
167
+ const rows = table.getChildren();
168
+ const lastRow = rows[rows.length - 1];
169
+
170
+ if (!lastRow || !isTableRowNode(lastRow)) {
171
+ return;
172
+ }
173
+
174
+ const alignments = textContent.trim().slice(1, -1).split('|').map(getColumnAlignment);
175
+
176
+ // Add header state to row cells, and the column alignment, which the rows that follow take from
177
+ // the header row
178
+ lastRow.getChildren().forEach((cell, index) => {
179
+ if (!isTableCellNode(cell)) {
180
+ return;
181
+ }
182
+
183
+ cell.setHeaderStyles(TableCellHeaderStates.ROW, TableCellHeaderStates.ROW);
184
+ cell.setFormat(alignments[index] ?? '');
185
+ });
186
+
187
+ // Remove line
188
+ parentNode.remove();
189
+ };
190
+
191
+ /**
192
+ * Take the table rows from the paragraphs right before the given one, removing those paragraphs.
193
+ * @param {ElementNode} parentNode Paragraph holding the current table row.
194
+ * @param {Transformer[]} transformers Transformers to import the cell content with.
195
+ * @returns {TableCellNode[][]} Cells of each row, in the document order.
196
+ */
197
+ const $takePrecedingRows = (parentNode, transformers) => {
198
+ /** @type {TableCellNode[][]} */
199
+ const rows = [];
200
+ let sibling = parentNode.getPreviousSibling();
201
+
202
+ while (sibling) {
203
+ if (!isParagraphNode(sibling)) {
204
+ break;
205
+ }
206
+
207
+ if (sibling.getChildrenSize() !== 1) {
208
+ break;
209
+ }
210
+
211
+ const firstChild = sibling.getFirstChild();
212
+
213
+ if (!isTextNode(firstChild)) {
214
+ break;
215
+ }
216
+
217
+ const cells = mapToTableCells(firstChild.getTextContent(), transformers);
218
+
219
+ if (!cells) {
220
+ break;
221
+ }
222
+
223
+ rows.unshift(cells);
224
+
225
+ const previousSibling = sibling.getPreviousSibling();
226
+
227
+ sibling.remove();
228
+ sibling = previousSibling;
229
+ }
230
+
231
+ return rows;
232
+ };
233
+
234
+ /**
235
+ * Create a table from the given rows, padding the shorter ones with empty cells.
236
+ * @param {TableCellNode[][]} rows Cells of each row.
237
+ * @param {Transformer[]} transformers Transformers to import the empty cell content with.
238
+ * @returns {TableNode} Table.
239
+ */
240
+ const $createTable = (rows, transformers) => {
241
+ const maxCells = Math.max(...rows.map((cells) => cells.length));
242
+ const table = createTableNode();
243
+
244
+ rows.forEach((cells) => {
245
+ const tableRow = createTableRowNode();
246
+
247
+ table.append(tableRow);
248
+
249
+ for (let i = 0; i < maxCells; i += 1) {
250
+ tableRow.append(i < cells.length ? cells[i] : createTableCell('', transformers));
251
+ }
252
+ });
253
+
254
+ return table;
255
+ };
256
+
257
+ /**
258
+ * Put a new table in place of the given paragraph, or add its rows to the table right before it if
259
+ * that has the same number of columns. The rows then take the column alignment from its header.
260
+ * @param {ElementNode} parentNode Paragraph to replace.
261
+ * @param {TableNode} table New table.
262
+ */
263
+ const $insertImportedTable = (parentNode, table) => {
264
+ const previousSibling = parentNode.getPreviousSibling();
265
+
266
+ if (
267
+ isTableNode(previousSibling) &&
268
+ getTableColumnsSize(previousSibling) === getTableColumnsSize(table)
269
+ ) {
270
+ const headerCells = /** @type {TableRowNode} */ (previousSibling.getFirstChild()).getChildren();
271
+
272
+ table.getChildren().forEach((row) => {
273
+ /** @type {TableRowNode} */ (row).getChildren().forEach((cell, index) => {
274
+ /** @type {TableCellNode} */ (cell).setFormat(
275
+ /** @type {TableCellNode} */ (headerCells[index]).getFormatType(),
276
+ );
277
+ });
278
+ });
279
+
280
+ previousSibling.append(...table.getChildren());
281
+ parentNode.remove();
282
+ } else {
283
+ parentNode.replace(table);
284
+ }
285
+
286
+ table.selectEnd();
287
+ };
288
+
289
+ /**
290
+ * Create a table transformer. A cell can only have inline content, so it’s converted with the
291
+ * standard Markdown transformers and the given text match transformers, like those of the inline
292
+ * editor components, which come first to take priority, as they do for the rest of the document.
293
+ * @param {TextMatchTransformer[]} [cellTransformers] Additional transformers for the cell content.
294
+ * @returns {ElementTransformer}
295
+ */
296
+ export const createTableTransformer = (cellTransformers = []) => {
297
+ /** @type {Transformer[]} */
298
+ const transformers = [
299
+ ...cellTransformers,
300
+ ...TRANSFORMERS.map((transformer) => (transformer === LEXICAL_LINK ? LINK : transformer)),
301
+ LEADING_INLINE_CODE,
302
+ RAW_HTML,
303
+ RAW_SYNTAX,
304
+ ];
305
+
306
+ /** @type {Transformer[]} */
307
+ const exportTransformers = [...transformers, BLOCK_SYNTAX_ESCAPE];
308
+
309
+ return {
310
+ dependencies: [TableNode, TableRowNode, TableCellNode],
311
+ export: (node) => {
312
+ if (!isTableNode(node)) {
313
+ return null;
314
+ }
315
+
316
+ return node
317
+ .getChildren()
318
+ .flatMap((row) => (isTableRowNode(row) ? exportTableRow(row, exportTransformers) : []))
319
+ .join('\n');
320
+ },
321
+ regExp: TABLE_ROW_REG_EXP,
322
+ replace: (parentNode, _children, [textContent]) => {
323
+ // Header row
324
+ if (TABLE_ROW_DIVIDER_REG_EXP.test(textContent)) {
325
+ $applyHeaderRow(parentNode, textContent);
326
+
327
+ return;
328
+ }
329
+
330
+ const matchCells = mapToTableCells(textContent, transformers);
331
+
332
+ if (!matchCells) {
333
+ return;
334
+ }
335
+
336
+ const rows = [...$takePrecedingRows(parentNode, transformers), matchCells];
337
+
338
+ $insertImportedTable(parentNode, $createTable(rows, transformers));
339
+ },
340
+ type: 'element',
341
+ };
342
+ };
@@ -0,0 +1,6 @@
1
+ export function exportMarkdown(enabledTransformers: Transformer[]): string;
2
+ export function onEditorUpdate(editor: LexicalEditor, enabledTransformers: Transformer[], cachedValue?: string, format?: TextEditorValueFormat): string;
3
+ export function registerUpdateEvent(editor: LexicalEditor, enabledTransformers: Transformer[], format: TextEditorValueFormat): () => void;
4
+ import type { Transformer } from '@lexical/markdown';
5
+ import type { LexicalEditor } from 'lexical';
6
+ import type { TextEditorValueFormat } from '../../../typedefs.js';
@@ -0,0 +1,125 @@
1
+ /**
2
+ * Exporting the editor content, and the Update event fired on changes.
3
+ */
4
+
5
+ import { $convertToMarkdownString as convertToMarkdownString } from '@lexical/markdown';
6
+ import { sleep } from '@sveltia/utils/misc';
7
+ import { DISABLED_MARKDOWN_TAGS } from '../constants.js';
8
+ import { exportHtml } from './html.js';
9
+ import { trimBlankBlockquoteLines } from './markdown.js';
10
+ import { BLOCK_SYNTAX_ESCAPE } from './transformers/block-escape.js';
11
+ import { getSelectionTypes } from '../editing/selection.js';
12
+ import { mergeUnregisters } from '../utils.js';
13
+
14
+ /**
15
+ * @import { LexicalEditor } from 'lexical';
16
+ * @import { Transformer } from '@lexical/markdown';
17
+ * @import { TextEditorValueFormat } from '../../../typedefs.js';
18
+ */
19
+
20
+ /**
21
+ * Convert the editor content to Markdown. Call this within an editor update or read.
22
+ * @internal
23
+ * @param {Transformer[]} enabledTransformers Enabled Markdown transformers.
24
+ * @returns {string} Markdown value.
25
+ */
26
+ export const exportMarkdown = (enabledTransformers) => {
27
+ const transformers = [
28
+ ...enabledTransformers.filter(
29
+ (/** @type {any} */ { tag }) => !DISABLED_MARKDOWN_TAGS.includes(tag),
30
+ ),
31
+ // Tidy up the escapes in text and escape the block syntax at the start of a line. Last, so a
32
+ // transformer of the editor’s own, such as a link, comes first
33
+ BLOCK_SYNTAX_ESCAPE,
34
+ ];
35
+
36
+ return trimBlankBlockquoteLines(
37
+ convertToMarkdownString(transformers)
38
+ // Remove the line breaks left by empty paragraphs at the end, which have no content. One
39
+ // may be added just by clicking below a block decorator; see `$moveCaretIntoBlock()`
40
+ .replace(/\n+$/, ''),
41
+ );
42
+ };
43
+
44
+ /**
45
+ * Listen to changes made on the editor and trigger the Update event.
46
+ * @internal
47
+ * @param {LexicalEditor} editor Editor instance.
48
+ * @param {Transformer[]} enabledTransformers Enabled Markdown transformers.
49
+ * @param {string} [cachedValue] Value from a previous call, to be reused instead of converting the
50
+ * whole document again when only the selection has changed.
51
+ * @param {TextEditorValueFormat} [format] Value format.
52
+ * @returns {string} Markdown or HTML value.
53
+ */
54
+ export const onEditorUpdate = (editor, enabledTransformers, cachedValue, format = 'markdown') => {
55
+ const value =
56
+ cachedValue ?? (format === 'html' ? exportHtml(editor) : exportMarkdown(enabledTransformers));
57
+
58
+ editor.getRootElement()?.dispatchEvent(
59
+ new CustomEvent('Update', {
60
+ detail: {
61
+ value,
62
+ selection: getSelectionTypes(),
63
+ },
64
+ }),
65
+ );
66
+
67
+ return value;
68
+ };
69
+
70
+ /**
71
+ * Register an update listener that triggers the Update event with the Markdown or HTML value,
72
+ * debounced so only the latest update in a burst triggers it.
73
+ * @param {LexicalEditor} editor Editor instance.
74
+ * @param {Transformer[]} enabledTransformers Enabled Markdown transformers.
75
+ * @param {TextEditorValueFormat} format Value format.
76
+ * @returns {() => void} Cleanup handler.
77
+ */
78
+ export const registerUpdateEvent = (editor, enabledTransformers, format) => {
79
+ /** Incremented on every update, so only the latest one in a burst triggers the Update event. */
80
+ let updateCount = 0;
81
+ /** Whether the content has changed since the Update event was last triggered. */
82
+ let contentChanged = true;
83
+ /** @type {string | undefined} */
84
+ let lastValue = undefined;
85
+ let disposed = false;
86
+
87
+ return mergeUnregisters(
88
+ () => {
89
+ disposed = true;
90
+ },
91
+ editor.registerUpdateListener(({ dirtyElements, dirtyLeaves } = /** @type {any} */ ({})) => {
92
+ // An update that only moves the selection leaves no dirty nodes. Remember whether any update
93
+ // in the burst changed the content, so the document is only converted again when needed.
94
+ // This includes the updates made while composing, which are otherwise skipped.
95
+ contentChanged ||= !dirtyElements || dirtyElements.size > 0 || dirtyLeaves.size > 0;
96
+
97
+ if (editor?.isComposing()) {
98
+ return;
99
+ }
100
+
101
+ updateCount += 1;
102
+
103
+ const currentCount = updateCount;
104
+
105
+ (async () => {
106
+ await sleep(100);
107
+
108
+ // Skip if a newer update has been made in the meantime, or the editor has been disposed
109
+ if (currentCount !== updateCount || disposed) {
110
+ return;
111
+ }
112
+
113
+ editor.update(() => {
114
+ lastValue = onEditorUpdate(
115
+ editor,
116
+ enabledTransformers,
117
+ contentChanged ? undefined : lastValue,
118
+ format,
119
+ );
120
+ contentChanged = false;
121
+ });
122
+ })();
123
+ }),
124
+ );
125
+ };
@@ -1,21 +1,4 @@
1
- export function isStaticDecoratorContent(target: EventTarget | null): boolean;
2
- export function handleEditorMouseDown(editor: LexicalEditor, event: MouseEvent): boolean;
3
- export function isSafeLinkURL(url: string): boolean;
4
- export function getSelectionTypes(): TextEditorSelectionState;
5
- export function exportMarkdown(enabledTransformers: Transformer[]): string;
6
- export function onEditorUpdate(editor: LexicalEditor, enabledTransformers: Transformer[], cachedValue?: string, format?: TextEditorValueFormat): string;
7
- export function getComponentMarkdown({ node, createNode, transformer, createMarkdown }: TextEditorComponent): string;
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 loadCodeHighlighter(lang: string): Promise<void>;
14
- export function pasteMarkdown(editor: LexicalEditor, value: string, enabledTransformers: Transformer[]): Promise<void>;
15
1
  export function initEditor(config: TextEditorConfig): InitEditorResult;
16
- export function convertMarkdownToLexical(editor: LexicalEditor, value: string, enabledTransformers: Transformer[]): Promise<string | undefined>;
17
- export function convertHtmlToLexical(editor: LexicalEditor, value: string): Promise<string | undefined>;
18
- export function focusEditor(editor: LexicalEditor): Promise<void>;
19
2
  export type InitEditorResult = {
20
3
  /**
21
4
  * Editor instance.
@@ -31,17 +14,58 @@ export type InitEditorResult = {
31
14
  dispose: () => void;
32
15
  };
33
16
  /**
34
- * Payload of `TOGGLE_LINK_COMMAND` with the URL and the link attributes, like `target`.
17
+ * Editor configuration with the defaults applied, and the features derived from it.
35
18
  */
36
- export type LinkAttributesWithURL = LinkAttributes & {
37
- url: string;
19
+ export type EditorSetup = {
20
+ /**
21
+ * Enabled buttons.
22
+ */
23
+ enabledButtons: TextEditorNodeType[];
24
+ /**
25
+ * Editor components.
26
+ */
27
+ components: TextEditorComponent[];
28
+ /**
29
+ * Whether to enable Markdown shortcuts.
30
+ */
31
+ useMarkdownShortcuts: boolean | undefined;
32
+ /**
33
+ * Whether the editor is a code editor.
34
+ */
35
+ isCodeEditor: boolean;
36
+ /**
37
+ * Default code block language.
38
+ */
39
+ defaultLanguage: string;
40
+ /**
41
+ * Value format.
42
+ */
43
+ format: TextEditorValueFormat;
44
+ /**
45
+ * Whether the editor accepts multiple lines.
46
+ */
47
+ multiline: boolean;
48
+ /**
49
+ * Whether to support code blocks.
50
+ */
51
+ hasCodeBlock: boolean;
52
+ /**
53
+ * Whether to support table editing.
54
+ */
55
+ hasTable: boolean;
56
+ /**
57
+ * Whether to support selecting a horizontal rule.
58
+ */
59
+ hasHorizontalRule: boolean;
60
+ /**
61
+ * Whether to support horizontal rules and tables in Markdown,
62
+ * even without their buttons.
63
+ */
64
+ hasBlockSyntax: boolean;
38
65
  };
66
+ import type { TextEditorConfig } from '../../typedefs.js';
39
67
  import type { LexicalEditor } from 'lexical';
40
- import type { TextEditorSelectionState } from '../../typedefs.js';
41
68
  import type { Transformer } from '@lexical/markdown';
42
- import type { TextEditorValueFormat } from '../../typedefs.js';
69
+ import type { TextEditorNodeType } from '../../typedefs.js';
43
70
  import type { TextEditorComponent } from '../../typedefs.js';
44
- import type { TextEditorConfig } from '../../typedefs.js';
45
- import { RootNode } from 'lexical';
46
- import { LineBreakNode } from 'lexical';
47
- import type { LinkAttributes } from '@lexical/link';
71
+ import type { TextEditorValueFormat } from '../../typedefs.js';