@sveltia/ui 0.91.0 → 0.92.1

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 (226) hide show
  1. package/dist/components/bottom-navigation/bottom-navigation.svelte +3 -9
  2. package/dist/components/bottom-navigation/bottom-navigation.svelte.d.ts +4 -39
  3. package/dist/components/button/button-group.svelte +2 -4
  4. package/dist/components/button/button-group.svelte.d.ts +3 -19
  5. package/dist/components/button/select-button-group.svelte +2 -13
  6. package/dist/components/button/select-button-group.svelte.d.ts +5 -67
  7. package/dist/components/calendar/calendar.d.ts +9 -0
  8. package/dist/components/calendar/calendar.js +43 -0
  9. package/dist/components/calendar/calendar.svelte +26 -27
  10. package/dist/components/checkbox/checkbox-group.svelte +2 -8
  11. package/dist/components/checkbox/checkbox-group.svelte.d.ts +4 -39
  12. package/dist/components/dialog/alert-dialog.svelte.d.ts +2 -3
  13. package/dist/components/dialog/confirmation-dialog.svelte.d.ts +2 -3
  14. package/dist/components/dialog/dialog.svelte +6 -7
  15. package/dist/components/dialog/dialog.svelte.d.ts +2 -3
  16. package/dist/components/dialog/prompt-dialog.svelte +2 -2
  17. package/dist/components/dialog/prompt-dialog.svelte.d.ts +2 -3
  18. package/dist/components/disclosure/disclosure.svelte +14 -16
  19. package/dist/components/disclosure/disclosure.svelte.d.ts +4 -38
  20. package/dist/components/drawer/drawer.svelte +22 -39
  21. package/dist/components/drawer/drawer.svelte.d.ts +3 -24
  22. package/dist/components/grid/grid-body.svelte +2 -3
  23. package/dist/components/grid/grid-body.svelte.d.ts +3 -18
  24. package/dist/components/grid/grid-cell.svelte +2 -8
  25. package/dist/components/grid/grid-cell.svelte.d.ts +3 -22
  26. package/dist/components/grid/grid-col-header.svelte +2 -4
  27. package/dist/components/grid/grid-col-header.svelte.d.ts +3 -19
  28. package/dist/components/grid/grid-foot.svelte +4 -9
  29. package/dist/components/grid/grid-foot.svelte.d.ts +3 -22
  30. package/dist/components/grid/grid-head.svelte +4 -9
  31. package/dist/components/grid/grid-head.svelte.d.ts +3 -22
  32. package/dist/components/grid/grid-row-header.svelte +2 -8
  33. package/dist/components/grid/grid-row-header.svelte.d.ts +3 -22
  34. package/dist/components/grid/grid-row.svelte +4 -5
  35. package/dist/components/grid/grid-row.svelte.d.ts +3 -19
  36. package/dist/components/grid/grid.d.ts +10 -0
  37. package/dist/components/grid/grid.js +11 -0
  38. package/dist/components/grid/grid.svelte +6 -7
  39. package/dist/components/grid/grid.svelte.d.ts +5 -21
  40. package/dist/components/listbox/listbox.svelte +34 -15
  41. package/dist/components/listbox/listbox.svelte.d.ts +5 -69
  42. package/dist/components/listbox/option-group.svelte +2 -8
  43. package/dist/components/listbox/option-group.svelte.d.ts +4 -39
  44. package/dist/components/listbox/option.svelte +2 -1
  45. package/dist/components/menu/menu-item-group.svelte +2 -6
  46. package/dist/components/menu/menu-item-group.svelte.d.ts +3 -22
  47. package/dist/components/menu/menu-item.svelte +5 -1
  48. package/dist/components/menu/menu.svelte +2 -8
  49. package/dist/components/menu/menu.svelte.d.ts +4 -39
  50. package/dist/components/radio/radio-group.svelte +2 -17
  51. package/dist/components/radio/radio-group.svelte.d.ts +5 -67
  52. package/dist/components/radio/radio.svelte +19 -22
  53. package/dist/components/radio/radio.svelte.d.ts +10 -40
  54. package/dist/components/resizable-pane/resizable-handle.svelte +42 -49
  55. package/dist/components/resizable-pane/resizable-pane-group.svelte +44 -58
  56. package/dist/components/resizable-pane/resizable-pane-group.svelte.d.ts +3 -19
  57. package/dist/components/resizable-pane/resizable-pane.svelte +2 -5
  58. package/dist/components/resizable-pane/resizable-pane.svelte.d.ts +3 -19
  59. package/dist/components/resizable-pane/sizing.d.ts +24 -0
  60. package/dist/components/resizable-pane/sizing.js +83 -0
  61. package/dist/components/select/combobox.svelte +35 -39
  62. package/dist/components/select/select-tags.svelte +3 -13
  63. package/dist/components/select/select-tags.svelte.d.ts +5 -67
  64. package/dist/components/slider/slider.d.ts +5 -4
  65. package/dist/components/slider/slider.js +28 -23
  66. package/dist/components/slider/slider.svelte +64 -62
  67. package/dist/components/slider/slider.svelte.d.ts +4 -37
  68. package/dist/components/switch/switch.svelte +2 -13
  69. package/dist/components/switch/switch.svelte.d.ts +5 -67
  70. package/dist/components/table/table-body.svelte +2 -4
  71. package/dist/components/table/table-body.svelte.d.ts +3 -19
  72. package/dist/components/table/table-cell.svelte +2 -8
  73. package/dist/components/table/table-cell.svelte.d.ts +3 -22
  74. package/dist/components/table/table-col-header.svelte +2 -8
  75. package/dist/components/table/table-col-header.svelte.d.ts +3 -22
  76. package/dist/components/table/table-foot.svelte +2 -8
  77. package/dist/components/table/table-foot.svelte.d.ts +3 -22
  78. package/dist/components/table/table-head.svelte +2 -8
  79. package/dist/components/table/table-head.svelte.d.ts +3 -22
  80. package/dist/components/table/table-part.svelte +2 -4
  81. package/dist/components/table/table-part.svelte.d.ts +3 -19
  82. package/dist/components/table/table-row-header.svelte +2 -8
  83. package/dist/components/table/table-row-header.svelte.d.ts +3 -22
  84. package/dist/components/table/table-row.svelte +2 -8
  85. package/dist/components/table/table-row.svelte.d.ts +3 -22
  86. package/dist/components/table/table.svelte +2 -4
  87. package/dist/components/table/table.svelte.d.ts +3 -19
  88. package/dist/components/tabs/tab-box.svelte +2 -4
  89. package/dist/components/tabs/tab-box.svelte.d.ts +3 -19
  90. package/dist/components/tabs/tab-list.svelte +2 -7
  91. package/dist/components/tabs/tab-list.svelte.d.ts +4 -37
  92. package/dist/components/tabs/tab-panel.svelte +2 -8
  93. package/dist/components/tabs/tab-panel.svelte.d.ts +3 -22
  94. package/dist/components/tabs/tab-panels.svelte +2 -8
  95. package/dist/components/tabs/tab-panels.svelte.d.ts +3 -22
  96. package/dist/components/text-editor/code-editor.js +14 -7
  97. package/dist/components/text-editor/code-editor.svelte +14 -22
  98. package/dist/components/text-editor/code-editor.svelte.d.ts +5 -103
  99. package/dist/components/text-editor/config.d.ts +73 -0
  100. package/dist/components/text-editor/config.js +110 -0
  101. package/dist/components/text-editor/conversion/editor-components.js +4 -4
  102. package/dist/components/text-editor/conversion/html/export.js +1 -1
  103. package/dist/components/text-editor/conversion/import.js +32 -26
  104. package/dist/components/text-editor/conversion/markdown/block-conversion.d.ts +4 -0
  105. package/dist/components/text-editor/conversion/markdown/block-conversion.js +154 -0
  106. package/dist/components/text-editor/conversion/markdown/block-structure.d.ts +16 -0
  107. package/dist/components/text-editor/conversion/markdown/block-structure.js +254 -0
  108. package/dist/components/text-editor/conversion/markdown/code-blocks.d.ts +57 -0
  109. package/dist/components/text-editor/conversion/markdown/code-blocks.js +279 -0
  110. package/dist/components/text-editor/conversion/markdown/code-spans.d.ts +19 -0
  111. package/dist/components/text-editor/conversion/markdown/code-spans.js +71 -0
  112. package/dist/components/text-editor/conversion/markdown/formatting.d.ts +22 -0
  113. package/dist/components/text-editor/conversion/markdown/formatting.js +451 -0
  114. package/dist/components/text-editor/conversion/markdown/index.d.ts +10 -0
  115. package/dist/components/text-editor/conversion/markdown/index.js +55 -0
  116. package/dist/components/text-editor/conversion/markdown/placeholders.d.ts +2 -0
  117. package/dist/components/text-editor/conversion/markdown/placeholders.js +229 -0
  118. package/dist/components/text-editor/conversion/markdown/syntax.d.ts +99 -0
  119. package/dist/components/text-editor/conversion/markdown/syntax.js +206 -0
  120. package/dist/components/text-editor/conversion/placeholder-chars.d.ts +43 -0
  121. package/dist/components/text-editor/conversion/placeholder-chars.js +51 -0
  122. package/dist/components/text-editor/conversion/transformers/block-escape.d.ts +1 -0
  123. package/dist/components/text-editor/conversion/transformers/block-escape.js +53 -26
  124. package/dist/components/text-editor/conversion/transformers/code.d.ts +3 -14
  125. package/dist/components/text-editor/conversion/transformers/code.js +78 -16
  126. package/dist/components/text-editor/conversion/transformers/formatted-code.js +3 -18
  127. package/dist/components/text-editor/conversion/transformers/inline-code.js +3 -1
  128. package/dist/components/text-editor/conversion/transformers/link.js +3 -1
  129. package/dist/components/text-editor/conversion/transformers/raw-syntax.d.ts +0 -19
  130. package/dist/components/text-editor/conversion/transformers/raw-syntax.js +22 -93
  131. package/dist/components/text-editor/conversion/transformers/table.js +57 -16
  132. package/dist/components/text-editor/conversion/update.d.ts +19 -2
  133. package/dist/components/text-editor/conversion/update.js +63 -39
  134. package/dist/components/text-editor/core.d.ts +12 -51
  135. package/dist/components/text-editor/core.js +44 -66
  136. package/dist/components/text-editor/editing/raw-markdown.d.ts +3 -0
  137. package/dist/components/text-editor/editing/raw-markdown.js +46 -2
  138. package/dist/components/text-editor/lexical-root.svelte +49 -47
  139. package/dist/components/text-editor/lexical-root.svelte.d.ts +4 -67
  140. package/dist/components/text-editor/shiki/{facade.d.ts → engine.d.ts} +8 -8
  141. package/dist/components/text-editor/shiki/{facade.js → engine.js} +53 -156
  142. package/dist/components/text-editor/shiki/highlighter.js +5 -5
  143. package/dist/components/text-editor/shiki/lexical.d.ts +6 -0
  144. package/dist/components/text-editor/shiki/lexical.js +165 -0
  145. package/dist/components/text-editor/shiki/preload.js +1 -1
  146. package/dist/components/text-editor/shiki/source-highlight.svelte.d.ts +15 -0
  147. package/dist/components/text-editor/shiki/source-highlight.svelte.js +120 -0
  148. package/dist/components/text-editor/shiki/version.d.ts +1 -1
  149. package/dist/components/text-editor/shiki/version.js +1 -1
  150. package/dist/components/text-editor/store.svelte.js +65 -16
  151. package/dist/components/text-editor/text-editor.svelte +53 -145
  152. package/dist/components/text-editor/text-editor.svelte.d.ts +5 -103
  153. package/dist/components/text-editor/toolbar/actions.d.ts +25 -0
  154. package/dist/components/text-editor/toolbar/actions.js +107 -0
  155. package/dist/components/text-editor/toolbar/align-menu-button.svelte +16 -14
  156. package/dist/components/text-editor/toolbar/format-text-button.svelte +26 -24
  157. package/dist/components/text-editor/toolbar/insert-horizontal-rule-button.svelte +23 -23
  158. package/dist/components/text-editor/toolbar/insert-item-button.svelte +1 -1
  159. package/dist/components/text-editor/toolbar/insert-link-button.svelte +47 -51
  160. package/dist/components/text-editor/toolbar/insert-menu-button.svelte +1 -1
  161. package/dist/components/text-editor/toolbar/insert-table-button.svelte +35 -52
  162. package/dist/components/text-editor/toolbar/table-action-menu.svelte +19 -17
  163. package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte +16 -4
  164. package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte.d.ts +0 -8
  165. package/dist/components/text-editor/toolbar/toggle-block-menu-item.svelte +34 -33
  166. package/dist/components/text-editor/widgets/emoji-autocomplete.svelte +7 -6
  167. package/dist/components/text-editor/widgets/floating-link-editor.svelte +19 -32
  168. package/dist/components/text-editor/widgets/table-cell-menu-button.svelte +28 -32
  169. package/dist/components/text-field/date-time-input.svelte +7 -8
  170. package/dist/components/text-field/date-time-input.svelte.d.ts +3 -32
  171. package/dist/components/text-field/number-input.d.ts +0 -2
  172. package/dist/components/text-field/number-input.js +25 -25
  173. package/dist/components/text-field/number-input.svelte +18 -9
  174. package/dist/components/text-field/number-input.svelte.d.ts +1 -1
  175. package/dist/components/text-field/text-area.d.ts +2 -0
  176. package/dist/components/text-field/text-area.js +31 -0
  177. package/dist/components/text-field/text-area.svelte +12 -25
  178. package/dist/components/text-field/text-area.svelte.d.ts +5 -67
  179. package/dist/components/toast/toast.svelte +31 -8
  180. package/dist/components/toast/toast.svelte.d.ts +6 -6
  181. package/dist/components/toolbar/toolbar.svelte +2 -7
  182. package/dist/components/toolbar/toolbar.svelte.d.ts +4 -37
  183. package/dist/components/tree/tree-item.svelte +2 -6
  184. package/dist/components/tree/tree-item.svelte.d.ts +3 -22
  185. package/dist/components/tree/tree.svelte +3 -10
  186. package/dist/components/tree/tree.svelte.d.ts +4 -39
  187. package/dist/components/util/group.svelte +2 -7
  188. package/dist/components/util/group.svelte.d.ts +4 -37
  189. package/dist/components/util/modal.d.ts +9 -0
  190. package/dist/components/util/modal.js +30 -0
  191. package/dist/components/util/modal.svelte +33 -14
  192. package/dist/index.d.ts +1 -1
  193. package/dist/index.js +1 -1
  194. package/dist/locales/hu.yaml +176 -0
  195. package/dist/services/composite-widget.svelte.d.ts +76 -0
  196. package/dist/services/composite-widget.svelte.js +157 -0
  197. package/dist/services/focus.d.ts +2 -0
  198. package/dist/services/focus.js +68 -0
  199. package/dist/services/group.svelte.d.ts +17 -69
  200. package/dist/services/group.svelte.js +128 -391
  201. package/dist/services/label.d.ts +9 -0
  202. package/dist/services/label.js +22 -0
  203. package/dist/services/menu.d.ts +8 -0
  204. package/dist/services/menu.js +237 -0
  205. package/dist/services/navigation.d.ts +9 -0
  206. package/dist/services/navigation.js +13 -0
  207. package/dist/services/number.d.ts +2 -0
  208. package/dist/services/number.js +27 -0
  209. package/dist/services/pointer-drag.d.ts +14 -0
  210. package/dist/services/pointer-drag.js +89 -0
  211. package/dist/services/popup.svelte.js +123 -103
  212. package/dist/services/text.d.ts +3 -0
  213. package/dist/services/text.js +17 -0
  214. package/dist/services/tree.svelte.d.ts +29 -43
  215. package/dist/services/tree.svelte.js +58 -105
  216. package/dist/services/type-ahead.d.ts +10 -1
  217. package/dist/services/type-ahead.js +56 -34
  218. package/dist/typedefs.d.ts +103 -4
  219. package/dist/typedefs.js +60 -4
  220. package/package.json +2 -2
  221. package/dist/components/text-editor/conversion/markdown.d.ts +0 -56
  222. package/dist/components/text-editor/conversion/markdown.js +0 -949
  223. package/dist/components/text-editor/toolbar/code-editor-toolbar.svelte +0 -28
  224. package/dist/components/text-editor/toolbar/code-editor-toolbar.svelte.d.ts +0 -37
  225. package/dist/components/text-editor/toolbar/insert-component.d.ts +0 -3
  226. package/dist/components/text-editor/toolbar/insert-component.js +0 -37
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Syntax highlighting of the Markdown or HTML source shown in the plain text mode of the text
3
+ * editor.
4
+ */
5
+
6
+ import { unique } from '@sveltia/utils/array';
7
+ import { untrack } from 'svelte';
8
+ import { parseOpeningFence } from '../conversion/markdown/code-blocks.js';
9
+ import { highlightCodeToTokens } from './engine.js';
10
+ import { loadCodeHighlighter } from './preload.js';
11
+ import { getCodeTheme, onCodeThemeChange } from './theme.js';
12
+
13
+ /**
14
+ * @import { HighlightedToken } from '../../../typedefs.js';
15
+ */
16
+
17
+ /**
18
+ * Maximum length of the Markdown or HTML source to highlight in the plain text mode. The whole
19
+ * source is tokenized on every keystroke, so highlighting a longer one would make typing sluggish.
20
+ */
21
+ export const MAX_HIGHLIGHT_LENGTH = 20_000;
22
+
23
+ /**
24
+ * Get the languages used in the fenced code blocks of the given Markdown source, read from the
25
+ * opening fences the same way the rich text mode reads them; see `parseOpeningFence()`. Fences
26
+ * within a code block are taken in as well, which only means loading a grammar that isn’t needed.
27
+ * @param {string} source Markdown source.
28
+ * @returns {string[]} Unique languages, in the order they first appear.
29
+ */
30
+ export const getCodeFenceLanguages = (source) =>
31
+ unique(
32
+ source
33
+ .split('\n')
34
+ .map((line) => parseOpeningFence(line)?.language)
35
+ .filter((language) => language !== undefined),
36
+ );
37
+
38
+ /**
39
+ * Create a function that highlights the Markdown or HTML source in the plain text mode, loading the
40
+ * highlighter, as well as the languages of the fenced code blocks in Markdown, while the mode is
41
+ * shown, and following the app’s appearance. This must be called during the component
42
+ * initialization, as it sets up effects.
43
+ * @param {object} args Arguments.
44
+ * @param {'markdown' | 'html'} args.language Language of the source.
45
+ * @param {() => boolean} args.isActive Whether the plain text mode is shown. Nothing is loaded or
46
+ * highlighted otherwise.
47
+ * @param {() => string} args.getSource Get the current source, to find the languages of its fenced
48
+ * code blocks.
49
+ * @returns {(source: string) => HighlightedToken[][] | undefined} Function to highlight the given
50
+ * source, which returns `undefined` while the highlighter is still loading, the plain text mode
51
+ * isn’t shown, or the source is too long. It’s reactive: it gives new tokens once something has
52
+ * been loaded, or the theme has changed.
53
+ */
54
+ export const createSourceHighlighter = ({ language, isActive, getSource }) => {
55
+ /** Syntax highlighting theme matching the app’s appearance. */
56
+ let codeTheme = $state(getCodeTheme());
57
+ /** Incremented whenever the highlighter has loaded something, to highlight the text again. */
58
+ let loadCount = $state(0);
59
+
60
+ /**
61
+ * Comma-separated languages used in the fenced code blocks of the Markdown source, which are
62
+ * highlighted as well. It’s a string rather than an array, so the highlighter isn’t reloaded on
63
+ * every keystroke.
64
+ */
65
+ const codeLanguages = $derived.by(() => {
66
+ if (!isActive() || language !== 'markdown' || getSource().length > MAX_HIGHLIGHT_LENGTH) {
67
+ return '';
68
+ }
69
+
70
+ return getCodeFenceLanguages(getSource()).join(',');
71
+ });
72
+
73
+ $effect(() =>
74
+ onCodeThemeChange(async () => {
75
+ const theme = getCodeTheme();
76
+
77
+ // Keep the current highlighting until the new theme is ready, so the text doesn’t flash
78
+ if (isActive()) {
79
+ await loadCodeHighlighter(language);
80
+ }
81
+
82
+ // Ignore an outdated change, in case the appearance has changed again while loading
83
+ if (theme === getCodeTheme()) {
84
+ codeTheme = theme;
85
+ }
86
+ }),
87
+ );
88
+
89
+ $effect(() => {
90
+ // Load the highlighter only when the plain text mode is actually shown
91
+ if (!isActive()) {
92
+ return;
93
+ }
94
+
95
+ const languages = codeLanguages ? codeLanguages.split(',') : [];
96
+
97
+ void codeTheme;
98
+
99
+ untrack(() => {
100
+ // The Markdown or HTML grammar is loaded first, because the engine has to be in place before
101
+ // anything else can be loaded
102
+ loadCodeHighlighter(language).then(async () => {
103
+ loadCount += 1;
104
+ await Promise.all(languages.map((lang) => loadCodeHighlighter(lang)));
105
+ loadCount += 1;
106
+ });
107
+ });
108
+ });
109
+
110
+ return (source) => {
111
+ void loadCount;
112
+
113
+ // Don’t waste time on tokenizing the source while the rich text mode is shown
114
+ if (!isActive() || source.length > MAX_HIGHLIGHT_LENGTH) {
115
+ return undefined;
116
+ }
117
+
118
+ return highlightCodeToTokens(source, language, { theme: codeTheme });
119
+ };
120
+ };
@@ -1,4 +1,4 @@
1
1
  /**
2
2
  * Version of this package, used to resolve the prebuilt Shiki engine chunk from a CDN.
3
3
  */
4
- export const UI_VERSION: "0.91.0";
4
+ export const UI_VERSION: "0.92.1";
@@ -3,4 +3,4 @@
3
3
  /**
4
4
  * Version of this package, used to resolve the prebuilt Shiki engine chunk from a CDN.
5
5
  */
6
- export const UI_VERSION = '0.91.0';
6
+ export const UI_VERSION = '0.92.1';
@@ -1,7 +1,7 @@
1
1
  import { generateElementId } from '@sveltia/utils/element';
2
2
  import { untrack } from 'svelte';
3
3
  import { convertHtmlToLexical, convertMarkdownToLexical } from './conversion/import.js';
4
- import { limitToSingleLine } from './editing/single-line.js';
4
+ import { normalizeConfig } from './config.js';
5
5
 
6
6
  /**
7
7
  * @import { LexicalEditor } from 'lexical';
@@ -9,6 +9,13 @@ import { limitToSingleLine } from './editing/single-line.js';
9
9
  * @import { TextEditorConfig, TextEditorSelectionState, TextEditorStore } from '../../typedefs.js';
10
10
  */
11
11
 
12
+ /**
13
+ * Function to call with the new input value whenever it changes, for each editor store; see
14
+ * {@link syncEditorProps}.
15
+ * @type {WeakMap<TextEditorStore, (inputValue: string) => void>}
16
+ */
17
+ const inputValueListeners = new WeakMap();
18
+
12
19
  /**
13
20
  * Create an editor editor store that contains all the states and configuration.
14
21
  * @returns {TextEditorStore} Store.
@@ -22,17 +29,8 @@ export const createEditorStore = () => {
22
29
  let editor = $state();
23
30
  /** @type {Transformer[]} */
24
31
  let enabledTransformers = $state([]);
25
-
26
32
  /** @type {TextEditorConfig} */
27
- let config = $state({
28
- modes: [],
29
- enabledButtons: [],
30
- components: [],
31
- useMarkdownShortcuts: true,
32
- isCodeEditor: false,
33
- useEmojiAutocomplete: false,
34
- });
35
-
33
+ let config = $state(normalizeConfig({ useMarkdownShortcuts: true }));
36
34
  /** @type {string} */
37
35
  let inputValue = $state('');
38
36
  /** @type {TextEditorSelectionState} */
@@ -130,7 +128,8 @@ export const createEditorStore = () => {
130
128
  }
131
129
  };
132
130
 
133
- return {
131
+ /** @type {TextEditorStore} */
132
+ const store = {
134
133
  /* eslint-disable jsdoc/require-jsdoc */
135
134
  get editor() {
136
135
  return editor;
@@ -154,7 +153,7 @@ export const createEditorStore = () => {
154
153
  return config;
155
154
  },
156
155
  set config(newValue) {
157
- config = limitToSingleLine(newValue);
156
+ config = normalizeConfig(newValue);
158
157
  useRichText = config.modes[0] === 'rich-text' || config.isCodeEditor;
159
158
  },
160
159
  get inputValue() {
@@ -163,9 +162,7 @@ export const createEditorStore = () => {
163
162
  set inputValue(newValue) {
164
163
  const hasChange = inputValue !== newValue;
165
164
 
166
- if (hasChange) {
167
- inputValue = newValue;
168
- }
165
+ store.setExportedValue(newValue);
169
166
 
170
167
  if (useRichText && (hasChange || editor?.getEditorState().isEmpty())) {
171
168
  convertToLexical();
@@ -255,8 +252,26 @@ export const createEditorStore = () => {
255
252
  * @returns {string | undefined} Imported value, or `undefined` if the content has changed.
256
253
  */
257
254
  getImportedValue: (value) => (lastImport?.exported === value ? lastImport.source : undefined),
255
+ /**
256
+ * Set the input value to the one exported from the Lexical editor, without importing it back
257
+ * into the editor, which would reset its nodes and selection.
258
+ * @param {string} value Exported value.
259
+ */
260
+ setExportedValue: (value) => {
261
+ if (inputValue !== value) {
262
+ inputValue = value;
263
+ inputValueListeners.get(store)?.(value);
264
+ }
265
+ },
266
+ /**
267
+ * Export the content of the Lexical editor right away, if it’s scheduled to be exported. The
268
+ * editor root sets this while the editor is there.
269
+ */
270
+ flushExport: () => {},
258
271
  /* eslint-enable jsdoc/require-jsdoc */
259
272
  };
273
+
274
+ return store;
260
275
  };
261
276
 
262
277
  /**
@@ -272,6 +287,17 @@ export const createEditorStore = () => {
272
287
  * @param {(pending: boolean) => void} args.setPending Update the `pending` prop.
273
288
  */
274
289
  export const syncEditorProps = (editorStore, { getInputValue, setValue, setPending }) => {
290
+ /**
291
+ * Input value last passed on to the props.
292
+ * @type {string | undefined}
293
+ */
294
+ let appliedValue = undefined;
295
+ /**
296
+ * Latest input value, which may not have been passed on to the props yet.
297
+ * @type {string | undefined}
298
+ */
299
+ let latestValue = undefined;
300
+
275
301
  $effect(() => {
276
302
  setPending(editorStore.pending);
277
303
  });
@@ -305,8 +331,31 @@ export const syncEditorProps = (editorStore, { getInputValue, setValue, setPendi
305
331
 
306
332
  const newValue = editorStore.inputValue;
307
333
 
334
+ appliedValue = newValue;
335
+
308
336
  untrack(() => {
309
337
  setValue(newValue);
310
338
  });
311
339
  });
340
+
341
+ // An editor being removed passes on what was just typed as it goes, either as the focus leaves it
342
+ // or as `<LexicalRoot>` is destroyed, but the effect above is gone by then. Remember the change,
343
+ // and pass it on once the removal is done: a teardown function only sees the state as it was
344
+ // before, so it can’t tell whether the props have been changed from outside in the meantime,
345
+ // e.g. in the same tick as the removal, in which case they win
346
+ inputValueListeners.set(editorStore, (newValue) => {
347
+ latestValue = newValue;
348
+ });
349
+
350
+ $effect(() => () => {
351
+ queueMicrotask(() => {
352
+ if (
353
+ latestValue !== undefined &&
354
+ latestValue !== appliedValue &&
355
+ untrack(getInputValue) === appliedValue
356
+ ) {
357
+ setValue(latestValue);
358
+ }
359
+ });
360
+ });
312
361
  };
@@ -8,31 +8,28 @@
8
8
  import Alert from '../alert/alert.svelte';
9
9
  import TextArea from '../text-field/text-area.svelte';
10
10
  import Toast from '../toast/toast.svelte';
11
+ import { getEditorSetup } from './config.js';
11
12
  import { DEFAULT_BUTTON_TYPES } from './constants.js';
12
- import { loadCodeHighlighter } from './shiki/preload.js';
13
13
  import EmojiAutocomplete from './widgets/emoji-autocomplete.svelte';
14
14
  import FloatingLinkEditor from './widgets/floating-link-editor.svelte';
15
15
  import LexicalRoot from './lexical-root.svelte';
16
16
  import TableCellMenuButton from './widgets/table-cell-menu-button.svelte';
17
- import {
18
- getBlockType,
19
- getRawTextState,
20
- registerRawTextSingleLine,
21
- } from './editing/raw-markdown.js';
22
- import { highlightCodeToTokens } from './shiki/facade.js';
23
- import { getCodeTheme, onCodeThemeChange } from './shiki/theme.js';
17
+ import { registerRawTextSelection, registerRawTextSingleLine } from './editing/raw-markdown.js';
18
+ import { createSourceHighlighter } from './shiki/source-highlight.svelte.js';
24
19
  import { createEditorStore, syncEditorProps } from './store.svelte.js';
25
20
  import TextEditorToolbar from './toolbar/text-editor-toolbar.svelte';
26
21
 
27
22
  /**
28
- * @import { Snippet } from 'svelte';
29
23
  * @import {
24
+ * EditorCommonProps,
25
+ * FormFieldStateProps,
30
26
  * TextEditorComponent,
31
27
  * TextEditorMode,
32
28
  * TextEditorNodeType,
33
29
  * TextEditorValueFormat,
30
+ * WidgetStateProps,
34
31
  * } from '../../typedefs.js';
35
- * @import { HighlightedToken } from '../../typedefs.js';
32
+ * @import { NormalizedEditorConfig } from './config.js';
36
33
  */
37
34
 
38
35
  /**
@@ -48,7 +45,6 @@
48
45
  * @property {boolean} [pending] Whether the user has changed the rich text content, and the
49
46
  * editor has yet to update {@link value}, which it does a moment later. Bind it to wait for the
50
47
  * change before reading the value, for example to save it. Read-only.
51
- * @property {boolean} [flex] Make the text input container flexible.
52
48
  * @property {'ltr' | 'rtl' | 'auto'} [dir] The `dir` attribute on the editable text box, both
53
49
  * the rich text editor and the `<textarea>` element of the plain text mode.
54
50
  * @property {TextEditorMode[]} [modes] Enabled modes.
@@ -67,26 +63,11 @@
67
63
  * rich text editor.
68
64
  * @property {boolean} [useEmojiAutocomplete] Whether to autocomplete emojis when the user
69
65
  * types a shortcode, like `:smi`.
70
- * @property {string} [class] The `class` attribute on the wrapper element.
71
- * @property {boolean} [hidden] Whether to hide the widget.
72
- * @property {boolean} [disabled] Whether to disable the widget. An alias of the `aria-disabled`
73
- * attribute.
74
- * @property {boolean} [readonly] Whether to make the widget read-only. An alias of the
75
- * `aria-readonly` attribute.
76
- * @property {boolean} [required] Whether to mark the widget required. An alias of the
77
- * `aria-required` attribute.
78
- * @property {boolean} [invalid] Whether to mark the widget invalid. An alias of the
79
- * `aria-invalid` attribute.
80
- * @property {string} [ariaLabel] The `aria-label` attribute on the editable text box. Either
81
- * this or `ariaLabelledby` is required for the editor to have an accessible name.
82
- * @property {string} [ariaLabelledby] The `aria-labelledby` attribute on the editable text box.
83
- * @property {string} [ariaDescribedby] The `aria-describedby` attribute on the editable text
84
- * box.
85
- * @property {Snippet} [children] Primary slot content.
86
66
  */
87
67
 
88
68
  /**
89
- * @type {Props & Record<string, any>}
69
+ * @type {EditorCommonProps & WidgetStateProps & FormFieldStateProps & Props &
70
+ * Record<string, any>}
90
71
  */
91
72
  let {
92
73
  /* eslint-disable prefer-const */
@@ -113,7 +94,8 @@
113
94
  'aria-label': ariaLabelAttr = undefined,
114
95
  'aria-labelledby': ariaLabelledbyAttr = undefined,
115
96
  'aria-describedby': ariaDescribedbyAttr = undefined,
116
- children,
97
+ // Not rendered, but kept out of `restProps`, which go on the wrapper element
98
+ children: _children,
117
99
  ...restProps
118
100
  /* eslint-enable prefer-const */
119
101
  } = $props();
@@ -128,6 +110,9 @@
128
110
  'aria-describedby': ariaDescribedby ?? ariaDescribedbyAttr,
129
111
  });
130
112
 
113
+ /** Whether the user can change the content, which the widgets below are only shown for. */
114
+ const editable = $derived(!disabled && !readonly);
115
+
131
116
  const editorStore = createEditorStore();
132
117
 
133
118
  // svelte-ignore state_referenced_locally
@@ -144,21 +129,10 @@
144
129
 
145
130
  setContext('editorStore', editorStore);
146
131
 
147
- /** Language of the source shown in the plain text mode. */
148
- // svelte-ignore state_referenced_locally
149
- const sourceLanguage = format === 'html' ? 'html' : 'markdown';
150
-
151
- /**
152
- * Maximum length of the Markdown or HTML source to highlight in the plain text mode. The whole
153
- * source is tokenized on every keystroke, so highlighting a longer one would make typing
154
- * sluggish.
155
- */
156
- const MAX_HIGHLIGHT_LENGTH = 20_000;
157
-
158
- /** Syntax highlighting theme matching the app’s appearance. */
159
- let codeTheme = $state(getCodeTheme());
160
- /** Incremented whenever the highlighter has loaded something, to highlight the text again. */
161
- let highlighterLoadCount = $state(0);
132
+ /** Features derived from the editor configuration, which the store holds normalized. */
133
+ const setup = $derived(
134
+ getEditorSetup(/** @type {NormalizedEditorConfig} */ (editorStore.config)),
135
+ );
162
136
 
163
137
  const usePlainText = $derived(!editorStore.useRichText && !hidden);
164
138
  /**
@@ -168,81 +142,24 @@
168
142
  * document slow. As nobody sees the `<textarea>` meanwhile, it only gets `auto` once it’s shown.
169
143
  */
170
144
  const textAreaDir = $derived(dir === 'auto' && editorStore.useRichText ? undefined : dir);
171
- /**
172
- * Comma-separated languages used in the fenced code blocks of the Markdown source, which are
173
- * highlighted as well. It’s a string rather than an array, so the highlighter isn’t reloaded on
174
- * every keystroke.
175
- */
176
- const codeLanguages = $derived.by(() => {
177
- if (
178
- !usePlainText ||
179
- sourceLanguage !== 'markdown' ||
180
- editorStore.inputValue.length > MAX_HIGHLIGHT_LENGTH
181
- ) {
182
- return '';
183
- }
184
-
185
- return [
186
- ...new Set(
187
- [...editorStore.inputValue.matchAll(/^[ \t]*(?:```|~~~)[ \t]*(?<lang>[\w+#-]+)/gm)].map(
188
- ({ groups }) => groups?.lang,
189
- ),
190
- ),
191
- ].join(',');
192
- });
193
-
194
- /**
195
- * Highlight the Markdown or HTML source in the plain text mode.
196
- * @param {string} source Markdown or HTML source.
197
- * @returns {HighlightedToken[][] | undefined} Tokens, or `undefined` while the highlighter is
198
- * still loading.
199
- */
200
- const highlightSource = (source) => {
201
- void highlighterLoadCount;
202
-
203
- // Don’t waste time on tokenizing the source while the rich text mode is shown
204
- if (!usePlainText || source.length > MAX_HIGHLIGHT_LENGTH) {
205
- return undefined;
206
- }
207
-
208
- return highlightCodeToTokens(source, sourceLanguage, { theme: codeTheme });
209
- };
210
145
 
211
- $effect(() =>
212
- onCodeThemeChange(async () => {
213
- const theme = getCodeTheme();
214
-
215
- // Keep the current highlighting until the new theme is ready, so the text doesn’t flash
216
- if (usePlainText) {
217
- await loadCodeHighlighter(sourceLanguage);
218
- }
219
-
220
- // Ignore an outdated change, in case the appearance has changed again while loading
221
- if (theme === getCodeTheme()) {
222
- codeTheme = theme;
223
- }
224
- }),
225
- );
226
-
227
- $effect(() => {
228
- // Load the highlighter only when the plain text mode is actually shown
229
- if (!usePlainText) {
230
- return;
231
- }
232
-
233
- const languages = codeLanguages ? codeLanguages.split(',') : [];
234
-
235
- void codeTheme;
146
+ /** Language of the source shown in the plain text mode. */
147
+ // svelte-ignore state_referenced_locally
148
+ const sourceLanguage = format === 'html' ? 'html' : 'markdown';
236
149
 
237
- untrack(() => {
238
- // The Markdown or HTML grammar is loaded first, because the engine has to be in place before
239
- // anything else can be loaded
240
- loadCodeHighlighter(sourceLanguage).then(async () => {
241
- highlighterLoadCount += 1;
242
- await Promise.all(languages.map((lang) => loadCodeHighlighter(lang)));
243
- highlighterLoadCount += 1;
244
- });
245
- });
150
+ /** Highlighter of the Markdown or HTML source shown in the plain text mode. */
151
+ const highlightSource = createSourceHighlighter({
152
+ language: sourceLanguage,
153
+ /**
154
+ * Check if the plain text mode is shown.
155
+ * @returns {boolean} Result.
156
+ */
157
+ isActive: () => usePlainText,
158
+ /**
159
+ * Get the Markdown or HTML source.
160
+ * @returns {string} Source.
161
+ */
162
+ getSource: () => editorStore.inputValue,
246
163
  });
247
164
 
248
165
  $effect(() => {
@@ -254,31 +171,22 @@
254
171
  return undefined;
255
172
  }
256
173
 
257
- /**
258
- * Update the selection state. Until the `<textarea>` gets the focus, its caret is at the end of
259
- * the value rather than where the user would expect, so the block type is not detected yet.
260
- */
261
- const update = () => {
262
- editorStore.selection = {
263
- blockNodeKey: null,
264
- blockType:
265
- document.activeElement === textArea
266
- ? getBlockType(getRawTextState(textArea), {
267
- hasTaskList: editorStore.config.enabledButtons.includes('task-list'),
268
- })
269
- : 'paragraph',
270
- inlineTypes: [],
271
- };
272
- };
273
-
274
- const events = ['selectionchange', 'select', 'input', 'keyup', 'mouseup', 'focus'];
275
-
276
- untrack(update);
277
- events.forEach((type) => textArea.addEventListener(type, update));
278
-
279
- return () => {
280
- events.forEach((type) => textArea.removeEventListener(type, update));
281
- };
174
+ return untrack(() =>
175
+ registerRawTextSelection(
176
+ textArea,
177
+ (blockType) => {
178
+ editorStore.selection = { blockNodeKey: null, blockType, inlineTypes: [] };
179
+ },
180
+ {
181
+ /**
182
+ * Tell whether the task list button is enabled. It’s read on every change rather than
183
+ * once, so a change to the buttons is followed.
184
+ * @returns {boolean} Result.
185
+ */
186
+ hasTaskList: () => setup.hasTaskList,
187
+ },
188
+ ),
189
+ );
282
190
  });
283
191
 
284
192
  $effect(() => {
@@ -354,13 +262,13 @@
354
262
  {required}
355
263
  {invalid}
356
264
  />
357
- {#if editorStore.config.useEmojiAutocomplete && !disabled && !readonly}
265
+ {#if editorStore.config.useEmojiAutocomplete && editable}
358
266
  <EmojiAutocomplete />
359
267
  {/if}
360
- {#if editorStore.config.enabledButtons.includes('link') && !disabled && !readonly}
268
+ {#if setup.hasLink && editable}
361
269
  <FloatingLinkEditor />
362
270
  {/if}
363
- {#if editorStore.config.enabledButtons.includes('table') && !disabled && !readonly}
271
+ {#if setup.hasTable && editable}
364
272
  <TableCellMenuButton />
365
273
  {/if}
366
274
  </div>