@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
@@ -6,6 +6,7 @@
6
6
  @see https://www.w3.org/WAI/ARIA/apg/patterns/checkbox/
7
7
  -->
8
8
  <script>
9
+ import { getToggleLabelAttrs } from '../../services/label.js';
9
10
  import Button from '../button/button.svelte';
10
11
  import Icon from '../icon/icon.svelte';
11
12
 
@@ -63,13 +64,10 @@
63
64
  */
64
65
  let buttonElement = $state();
65
66
 
66
- /**
67
- * Accessible name given as a prop or as a plain `aria-label` attribute. It takes the place of the
68
- * visible label, which the button otherwise points at — but only when there is one to point at.
69
- * @type {string | undefined}
70
- */
71
- const accessibleLabel = $derived(ariaLabel ?? restProps['aria-label']);
72
67
  const hasVisibleLabel = $derived(!!(children || label));
68
+ const labelAttrs = $derived(
69
+ getToggleLabelAttrs({ id, ariaLabel: ariaLabel ?? restProps['aria-label'], hasVisibleLabel }),
70
+ );
73
71
  const indeterminate = $derived(checked === 'mixed');
74
72
 
75
73
  // Sync `checked` with `group` and `value`
@@ -119,8 +117,7 @@
119
117
  aria-required={required}
120
118
  aria-invalid={invalid}
121
119
  aria-checked={checked}
122
- aria-label={accessibleLabel || undefined}
123
- aria-labelledby={!accessibleLabel && hasVisibleLabel ? `${id}-label` : undefined}
120
+ {...labelAttrs}
124
121
  onclick={(event) => {
125
122
  event.preventDefault();
126
123
  event.stopPropagation();
@@ -289,12 +289,12 @@
289
289
 
290
290
  .body {
291
291
  overflow: auto;
292
- margin: var(--sui-dialog-body-margin, 24px 24px);
292
+ padding: var(--sui-dialog-body-padding, var(--sui-dialog-body-margin, 24px 24px));
293
293
  white-space: normal;
294
294
  line-height: var(--sui-line-height-compact);
295
295
  }
296
296
  @media (width < 768px) {
297
297
  .body {
298
- margin: var(--sui-dialog-body-margin, 16px 16px);
298
+ padding: var(--sui-dialog-body-padding, var(--sui-dialog-body-margin, 16px 16px));
299
299
  }
300
300
  }</style>
@@ -410,22 +410,22 @@
410
410
  flex-direction: column;
411
411
  overflow-y: auto;
412
412
  margin: 0;
413
+ padding: var(--sui-listbox-padding, 4px);
414
+ width: 280px;
415
+ font-family: var(--sui-control-font-family);
416
+ font-size: var(--sui-control-font-size);
417
+ line-height: var(--sui-control-line-height);
418
+ -webkit-user-select: none;
419
+ user-select: none;
413
420
  border-width: var(--sui-listbox-border-width, 1px);
414
421
  border-style: var(--sui-listbox-border-style, solid);
415
422
  border-color: var(--sui-listbox-border-color, var(--sui-secondary-border-color));
416
423
  border-radius: var(--sui-listbox-border-radius, 4px);
417
- padding: var(--sui-listbox-padding, 4px);
418
- width: 280px;
419
424
  color: var(--sui-primary-foreground-color);
420
425
  background-color: var(--sui-secondary-background-color-translucent);
421
426
  box-shadow: 0 8px 16px var(--sui-popup-shadow-color);
422
427
  -webkit-backdrop-filter: blur(16px);
423
428
  backdrop-filter: blur(16px);
424
- font-family: var(--sui-control-font-family);
425
- font-size: var(--sui-control-font-size);
426
- line-height: var(--sui-control-line-height);
427
- -webkit-user-select: none;
428
- user-select: none;
429
429
  }
430
430
 
431
431
  .option {
@@ -47,6 +47,7 @@
47
47
  /**
48
48
  * Keep the sort button’s clicks and activation keys from the `<Grid>`, which would otherwise
49
49
  * select or focus the header row, or prevent the key’s default action, and call `onSort` instead.
50
+ * A click also focuses the button, as it does in browsers other than Safari.
50
51
  * Native listeners are used because the grid listens on its own element, which a delegated Svelte
51
52
  * event handler would only reach after the grid has handled the event. The arrow keys are left
52
53
  * alone, so they still move from the header row to the data rows.
@@ -59,6 +60,9 @@
59
60
  */
60
61
  const onClick = (event) => {
61
62
  event.stopPropagation();
63
+ // Safari doesn’t focus a button on click, but moves the focus to the closest focusable
64
+ // ancestor instead, which is the header row
65
+ /** @type {HTMLElement} */ (element).focus();
62
66
  onSort?.(
63
67
  new CustomEvent('Sort', {
64
68
  detail: { sortOrder: sortOrder === 'ascending' ? 'descending' : 'ascending' },
@@ -1,6 +1,6 @@
1
1
  <!--
2
2
  @component
3
- The interactive version of `<TableThead>`.
3
+ The interactive version of `<TableHead>`.
4
4
  @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/thead
5
5
  @see https://w3c.github.io/aria/#rowgroup
6
6
  -->
@@ -4,7 +4,7 @@ type GridHead = {
4
4
  $set?(props: Partial<Props & Record<string, any>>): void;
5
5
  };
6
6
  /**
7
- * The interactive version of `<TableThead>`.
7
+ * The interactive version of `<TableHead>`.
8
8
  * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/thead
9
9
  * @see https://w3c.github.io/aria/#rowgroup
10
10
  */
@@ -6,6 +6,7 @@
6
6
  @see https://www.w3.org/WAI/ARIA/apg/patterns/radio/
7
7
  -->
8
8
  <script>
9
+ import { getToggleLabelAttrs } from '../../services/label.js';
9
10
  import Button from '../button/button.svelte';
10
11
 
11
12
  /**
@@ -66,13 +67,13 @@
66
67
  */
67
68
  let buttonElement = $state();
68
69
 
69
- /**
70
- * Accessible name given as a prop or as a plain `aria-label` attribute. It takes the place of the
71
- * visible label, which the button otherwise points at — but only when there is one to point at.
72
- * @type {string | undefined}
73
- */
74
- const accessibleLabel = $derived(ariaLabel ?? restProps['aria-label']);
75
- const hasVisibleLabel = $derived(!!(children || label));
70
+ const labelAttrs = $derived(
71
+ getToggleLabelAttrs({
72
+ id,
73
+ ariaLabel: ariaLabel ?? restProps['aria-label'],
74
+ hasVisibleLabel: !!(children || label),
75
+ }),
76
+ );
76
77
 
77
78
  /**
78
79
  * Check if the radio button belongs to a read-only `<RadioGroup>`.
@@ -121,8 +122,7 @@
121
122
  {value}
122
123
  {valueType}
123
124
  aria-checked={checked}
124
- aria-label={accessibleLabel || undefined}
125
- aria-labelledby={!accessibleLabel && hasVisibleLabel ? `${id}-label` : undefined}
125
+ {...labelAttrs}
126
126
  onclick={(event) => {
127
127
  event.preventDefault();
128
128
 
@@ -11,6 +11,7 @@
11
11
  getInitialSizes,
12
12
  resolvePaneConstraints,
13
13
  resolveToPercent,
14
+ shareSpaceAmongPanes,
14
15
  } from './sizing.js';
15
16
 
16
17
  /**
@@ -216,31 +217,15 @@
216
217
  };
217
218
 
218
219
  /**
219
- * Give the given space to the given panes, the first one first, then to the others, as far as
220
- * their maximum sizes allow, or take it from them with a negative amount, as far as their minimum
221
- * sizes allow.
220
+ * Give the given space to the given panes, then to the others, or take it from them with a
221
+ * negative amount; see {@link shareSpaceAmongPanes}.
222
222
  * @param {number[]} neighbors Indexes of the panes to give the space to first.
223
223
  * @param {number} amount Space in percentage points.
224
224
  * @param {number} [exclude] Index of a pane to leave alone.
225
225
  * @returns {number} Space that couldn’t be given or taken.
226
226
  */
227
- const shareSpace = (neighbors, amount, exclude) => {
228
- let rest = amount;
229
-
230
- [...neighbors, ...sizes.map((_size, index) => index)].forEach((neighbor) => {
231
- if (Math.abs(rest) < 0.001 || neighbor === exclude || sizes[neighbor] === undefined) {
232
- return;
233
- }
234
-
235
- const { minSize, maxSize } = getPaneConstraints(neighbor);
236
- const size = Math.min(Math.max(sizes[neighbor] + rest, minSize), maxSize);
237
-
238
- rest -= size - sizes[neighbor];
239
- sizes[neighbor] = size;
240
- });
241
-
242
- return rest;
243
- };
227
+ const shareSpace = (neighbors, amount, exclude) =>
228
+ shareSpaceAmongPanes(sizes, { neighbors, amount, exclude, getConstraints: getPaneConstraints });
244
229
 
245
230
  /**
246
231
  * Put the panes and handles in the order they are in the group, and size a pane shown after the
@@ -10,6 +10,12 @@ export function clampResizeDelta(deltaPercent: number, { sizeBefore, sizeAfter,
10
10
  constraintsBefore: PaneConstraints;
11
11
  constraintsAfter: PaneConstraints;
12
12
  }): number;
13
+ export function shareSpaceAmongPanes(sizes: number[], { neighbors, amount, exclude, getConstraints }: {
14
+ neighbors: number[];
15
+ amount: number;
16
+ exclude?: number | undefined;
17
+ getConstraints: (index: number) => PaneConstraints;
18
+ }): number;
13
19
  export function getHandleKeyAction({ key, shiftKey, isHorizontal, rtl }: {
14
20
  key: string;
15
21
  shiftKey: boolean;
@@ -207,6 +207,37 @@ export const clampResizeDelta = (
207
207
  return deltaPercent > 0 ? Math.min(deltaPercent, canGrow) : -Math.min(-deltaPercent, canShrink);
208
208
  };
209
209
 
210
+ /**
211
+ * Give the given space to the given panes, the first one first, then to the others, as far as
212
+ * their maximum sizes allow, or take it from them with a negative amount, as far as their minimum
213
+ * sizes allow. The sizes are updated in place.
214
+ * @param {number[]} sizes Pane sizes in percentages.
215
+ * @param {object} args Arguments.
216
+ * @param {number[]} args.neighbors Indexes of the panes to give the space to first.
217
+ * @param {number} args.amount Space in percentage points.
218
+ * @param {number} [args.exclude] Index of a pane to leave alone.
219
+ * @param {(index: number) => PaneConstraints} args.getConstraints Function to get the constraints
220
+ * of a pane.
221
+ * @returns {number} Space that couldn’t be given or taken.
222
+ */
223
+ export const shareSpaceAmongPanes = (sizes, { neighbors, amount, exclude, getConstraints }) => {
224
+ let rest = amount;
225
+
226
+ [...neighbors, ...sizes.map((_size, index) => index)].forEach((neighbor) => {
227
+ if (Math.abs(rest) < 0.001 || neighbor === exclude || sizes[neighbor] === undefined) {
228
+ return;
229
+ }
230
+
231
+ const { minSize, maxSize } = getConstraints(neighbor);
232
+ const size = Math.min(Math.max(sizes[neighbor] + rest, minSize), maxSize);
233
+
234
+ rest -= size - sizes[neighbor];
235
+ sizes[neighbor] = size;
236
+ });
237
+
238
+ return rest;
239
+ };
240
+
210
241
  /**
211
242
  * Interpret a key pressed on a resize handle. Arrow keys move the handle by 1%, or 10% with Shift.
212
243
  * Enter collapses or restores the pane before the handle. Home and End move it to its minimum and
@@ -4,12 +4,12 @@
4
4
  -->
5
5
  <script>
6
6
  import { _ } from '@sveltia/i18n';
7
- import { setContext, untrack } from 'svelte';
7
+ import { setContext } from 'svelte';
8
8
  import Alert from '../alert/alert.svelte';
9
9
  import Toast from '../toast/toast.svelte';
10
10
  import { parseCodeBlock, toCodeBlock } from './code-editor.js';
11
11
  import LexicalRoot from './lexical-root.svelte';
12
- import { createEditorStore } from './store.svelte.js';
12
+ import { createEditorStore, syncEditorProps } from './store.svelte.js';
13
13
  import CodeEditorToolbar from './toolbar/code-editor-toolbar.svelte';
14
14
 
15
15
  /**
@@ -90,42 +90,18 @@
90
90
 
91
91
  setContext('editorStore', editorStore);
92
92
 
93
- $effect(() => {
94
- pending = editorStore.pending;
95
- });
96
-
97
- // An editor removed right after a change never converts it, so don’t leave a bound `pending` set
98
- // for good, which would hold up anything waiting for the change
99
- $effect(() => () => {
100
- pending = false;
101
- });
102
-
103
- $effect(() => {
104
- // The root initializes the editor before these effects first run, and stays initialized
105
- /* v8 ignore next */
106
- if (!editorStore.initialized) {
107
- return;
108
- }
109
-
110
- void code;
111
- void lang;
112
-
113
- untrack(() => {
114
- editorStore.inputValue = toCodeBlock(lang, code);
115
- });
116
- });
117
-
118
- $effect(() => {
119
- // The root initializes the editor before these effects first run, and stays initialized
120
- /* v8 ignore next */
121
- if (!editorStore.initialized) {
122
- return;
123
- }
124
-
125
- void editorStore.inputValue;
126
-
127
- untrack(() => {
128
- const { lang: _lang, code: _code } = parseCodeBlock(editorStore.inputValue);
93
+ syncEditorProps(editorStore, {
94
+ /**
95
+ * Wrap the code in a fenced code block for the editor.
96
+ * @returns {string} Code block.
97
+ */
98
+ getInputValue: () => toCodeBlock(lang, code),
99
+ /**
100
+ * Take the editor’s code block apart into the language and the code.
101
+ * @param {string} inputValue Code block.
102
+ */
103
+ setValue: (inputValue) => {
104
+ const { lang: _lang, code: _code } = parseCodeBlock(inputValue);
129
105
 
130
106
  if (lang !== _lang) {
131
107
  lang = _lang;
@@ -134,7 +110,14 @@
134
110
  if (code !== _code) {
135
111
  code = _code;
136
112
  }
137
- });
113
+ },
114
+ /**
115
+ * Update the `pending` prop.
116
+ * @param {boolean} newValue New value.
117
+ */
118
+ setPending: (newValue) => {
119
+ pending = newValue;
120
+ },
138
121
  });
139
122
  </script>
140
123
 
@@ -18,9 +18,9 @@ import {
18
18
  import { HeadingNode, QuoteNode } from '@lexical/rich-text';
19
19
  import { createCommand } from 'lexical';
20
20
 
21
- import { CODE } from './transformers/code.js';
22
- import { LEADING_INLINE_CODE } from './transformers/inline-code.js';
23
- import { LINK } from './transformers/link.js';
21
+ import { CODE } from './conversion/transformers/code.js';
22
+ import { LEADING_INLINE_CODE } from './conversion/transformers/inline-code.js';
23
+ import { LINK } from './conversion/transformers/link.js';
24
24
 
25
25
  /**
26
26
  * @import { EditorThemeClasses, LexicalCommand } from 'lexical';
@@ -0,0 +1,5 @@
1
+ export function getComponentMarkdown({ node, createNode, transformer, createMarkdown }: TextEditorComponent): string;
2
+ export function isInlineComponent({ transformer }: TextEditorComponent): boolean;
3
+ export function removePlaceholdersFromMatch(transformer: Transformer): Transformer;
4
+ import type { TextEditorComponent } from '../../../typedefs.js';
5
+ import type { Transformer } from '@lexical/markdown';
@@ -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 '../../typedefs.js';
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 './shiki/facade.js';
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 '../../typedefs.js';
32
+ * @import { TextEditorComponent } from '../../../typedefs.js';
33
33
  */
34
34
 
35
35
  /**
@@ -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';