@sveltia/ui 0.90.0 → 0.92.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (284) 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/constants.d.ts +40 -6
  102. package/dist/components/text-editor/constants.js +81 -7
  103. package/dist/components/text-editor/conversion/editor-components.js +4 -4
  104. package/dist/components/text-editor/conversion/html/components.d.ts +16 -0
  105. package/dist/components/text-editor/conversion/html/components.js +87 -0
  106. package/dist/components/text-editor/conversion/html/export.d.ts +10 -0
  107. package/dist/components/text-editor/conversion/html/export.js +360 -0
  108. package/dist/components/text-editor/conversion/html/import-rules.d.ts +12 -0
  109. package/dist/components/text-editor/conversion/html/import-rules.js +234 -0
  110. package/dist/components/text-editor/conversion/html/index.d.ts +10 -0
  111. package/dist/components/text-editor/conversion/html/index.js +33 -0
  112. package/dist/components/text-editor/conversion/html/paragraph.d.ts +1 -0
  113. package/dist/components/text-editor/conversion/html/paragraph.js +123 -0
  114. package/dist/components/text-editor/conversion/html/task-list.d.ts +13 -0
  115. package/dist/components/text-editor/conversion/html/task-list.js +82 -0
  116. package/dist/components/text-editor/conversion/html/unsupported.d.ts +2 -0
  117. package/dist/components/text-editor/conversion/html/unsupported.js +122 -0
  118. package/dist/components/text-editor/conversion/html/utils.d.ts +6 -0
  119. package/dist/components/text-editor/conversion/html/utils.js +34 -0
  120. package/dist/components/text-editor/conversion/import.js +33 -27
  121. package/dist/components/text-editor/conversion/markdown/block-conversion.d.ts +4 -0
  122. package/dist/components/text-editor/conversion/markdown/block-conversion.js +154 -0
  123. package/dist/components/text-editor/conversion/markdown/block-structure.d.ts +16 -0
  124. package/dist/components/text-editor/conversion/markdown/block-structure.js +254 -0
  125. package/dist/components/text-editor/conversion/markdown/code-blocks.d.ts +57 -0
  126. package/dist/components/text-editor/conversion/markdown/code-blocks.js +279 -0
  127. package/dist/components/text-editor/conversion/markdown/code-spans.d.ts +19 -0
  128. package/dist/components/text-editor/conversion/markdown/code-spans.js +71 -0
  129. package/dist/components/text-editor/conversion/markdown/formatting.d.ts +22 -0
  130. package/dist/components/text-editor/conversion/markdown/formatting.js +451 -0
  131. package/dist/components/text-editor/conversion/markdown/index.d.ts +10 -0
  132. package/dist/components/text-editor/conversion/markdown/index.js +55 -0
  133. package/dist/components/text-editor/conversion/markdown/placeholders.d.ts +2 -0
  134. package/dist/components/text-editor/conversion/markdown/placeholders.js +229 -0
  135. package/dist/components/text-editor/conversion/markdown/syntax.d.ts +99 -0
  136. package/dist/components/text-editor/conversion/markdown/syntax.js +206 -0
  137. package/dist/components/text-editor/conversion/placeholder-chars.d.ts +43 -0
  138. package/dist/components/text-editor/conversion/placeholder-chars.js +51 -0
  139. package/dist/components/text-editor/conversion/transformers/block-escape.d.ts +1 -0
  140. package/dist/components/text-editor/conversion/transformers/block-escape.js +53 -26
  141. package/dist/components/text-editor/conversion/transformers/code.d.ts +3 -14
  142. package/dist/components/text-editor/conversion/transformers/code.js +78 -16
  143. package/dist/components/text-editor/conversion/transformers/formatted-code.js +3 -18
  144. package/dist/components/text-editor/conversion/transformers/inline-code.js +3 -1
  145. package/dist/components/text-editor/conversion/transformers/link.js +3 -1
  146. package/dist/components/text-editor/conversion/transformers/raw-syntax.d.ts +0 -19
  147. package/dist/components/text-editor/conversion/transformers/raw-syntax.js +22 -93
  148. package/dist/components/text-editor/conversion/transformers/table.js +57 -16
  149. package/dist/components/text-editor/conversion/transformers/task-list.d.ts +24 -0
  150. package/dist/components/text-editor/conversion/transformers/task-list.js +53 -0
  151. package/dist/components/text-editor/conversion/update.d.ts +19 -2
  152. package/dist/components/text-editor/conversion/update.js +64 -40
  153. package/dist/components/text-editor/core.d.ts +12 -51
  154. package/dist/components/text-editor/core.js +47 -77
  155. package/dist/components/text-editor/editing/commands.js +9 -18
  156. package/dist/components/text-editor/editing/guards.d.ts +7 -0
  157. package/dist/components/text-editor/editing/guards.js +123 -0
  158. package/dist/components/text-editor/editing/raw-markdown.d.ts +7 -2
  159. package/dist/components/text-editor/editing/raw-markdown.js +68 -8
  160. package/dist/components/text-editor/editing/selection.js +6 -2
  161. package/dist/components/text-editor/lexical-root.svelte +89 -47
  162. package/dist/components/text-editor/lexical-root.svelte.d.ts +4 -67
  163. package/dist/components/text-editor/shiki/{facade.d.ts → engine.d.ts} +8 -8
  164. package/dist/components/text-editor/shiki/{facade.js → engine.js} +53 -156
  165. package/dist/components/text-editor/shiki/highlighter.js +5 -5
  166. package/dist/components/text-editor/shiki/lexical.d.ts +6 -0
  167. package/dist/components/text-editor/shiki/lexical.js +165 -0
  168. package/dist/components/text-editor/shiki/preload.js +1 -1
  169. package/dist/components/text-editor/shiki/source-highlight.svelte.d.ts +15 -0
  170. package/dist/components/text-editor/shiki/source-highlight.svelte.js +120 -0
  171. package/dist/components/text-editor/shiki/version.d.ts +1 -1
  172. package/dist/components/text-editor/shiki/version.js +1 -1
  173. package/dist/components/text-editor/store.svelte.js +65 -16
  174. package/dist/components/text-editor/text-editor.svelte +57 -146
  175. package/dist/components/text-editor/text-editor.svelte.d.ts +9 -105
  176. package/dist/components/text-editor/toolbar/actions.d.ts +25 -0
  177. package/dist/components/text-editor/toolbar/actions.js +107 -0
  178. package/dist/components/text-editor/toolbar/align-menu-button.svelte +16 -14
  179. package/dist/components/text-editor/toolbar/format-text-button.svelte +27 -19
  180. package/dist/components/text-editor/toolbar/insert-horizontal-rule-button.svelte +23 -23
  181. package/dist/components/text-editor/toolbar/insert-item-button.svelte +1 -1
  182. package/dist/components/text-editor/toolbar/insert-link-button.svelte +47 -51
  183. package/dist/components/text-editor/toolbar/insert-menu-button.svelte +1 -1
  184. package/dist/components/text-editor/toolbar/insert-table-button.svelte +35 -52
  185. package/dist/components/text-editor/toolbar/table-action-menu.svelte +19 -17
  186. package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte +34 -11
  187. package/dist/components/text-editor/toolbar/text-editor-toolbar.svelte.d.ts +0 -8
  188. package/dist/components/text-editor/toolbar/toggle-block-menu-item.svelte +35 -41
  189. package/dist/components/text-editor/widgets/emoji-autocomplete.svelte +7 -6
  190. package/dist/components/text-editor/widgets/floating-link-editor.svelte +19 -32
  191. package/dist/components/text-editor/widgets/table-cell-menu-button.svelte +28 -32
  192. package/dist/components/text-field/date-time-input.svelte +7 -8
  193. package/dist/components/text-field/date-time-input.svelte.d.ts +3 -32
  194. package/dist/components/text-field/number-input.d.ts +0 -2
  195. package/dist/components/text-field/number-input.js +25 -25
  196. package/dist/components/text-field/number-input.svelte +18 -9
  197. package/dist/components/text-field/number-input.svelte.d.ts +1 -1
  198. package/dist/components/text-field/text-area.d.ts +2 -0
  199. package/dist/components/text-field/text-area.js +31 -0
  200. package/dist/components/text-field/text-area.svelte +12 -25
  201. package/dist/components/text-field/text-area.svelte.d.ts +5 -67
  202. package/dist/components/toast/toast.svelte +31 -8
  203. package/dist/components/toast/toast.svelte.d.ts +6 -6
  204. package/dist/components/toolbar/toolbar.svelte +2 -7
  205. package/dist/components/toolbar/toolbar.svelte.d.ts +4 -37
  206. package/dist/components/tree/tree-item.svelte +2 -6
  207. package/dist/components/tree/tree-item.svelte.d.ts +3 -22
  208. package/dist/components/tree/tree.svelte +3 -10
  209. package/dist/components/tree/tree.svelte.d.ts +4 -39
  210. package/dist/components/util/group.svelte +2 -7
  211. package/dist/components/util/group.svelte.d.ts +4 -37
  212. package/dist/components/util/modal.d.ts +9 -0
  213. package/dist/components/util/modal.js +30 -0
  214. package/dist/components/util/modal.svelte +16 -12
  215. package/dist/index.d.ts +1 -1
  216. package/dist/index.js +1 -1
  217. package/dist/locales/ar.yaml +2 -0
  218. package/dist/locales/bg.yaml +2 -0
  219. package/dist/locales/ca.yaml +2 -0
  220. package/dist/locales/cs.yaml +2 -0
  221. package/dist/locales/da.yaml +2 -0
  222. package/dist/locales/de.yaml +2 -0
  223. package/dist/locales/el.yaml +2 -0
  224. package/dist/locales/en-CA.yaml +2 -0
  225. package/dist/locales/en-GB.yaml +2 -0
  226. package/dist/locales/en-US.yaml +2 -0
  227. package/dist/locales/en-ZA.yaml +2 -0
  228. package/dist/locales/es-CO.yaml +2 -0
  229. package/dist/locales/es-ES.yaml +2 -0
  230. package/dist/locales/fa.yaml +2 -0
  231. package/dist/locales/fi.yaml +2 -0
  232. package/dist/locales/fr.yaml +2 -0
  233. package/dist/locales/he.yaml +2 -0
  234. package/dist/locales/hr.yaml +2 -0
  235. package/dist/locales/hu.yaml +176 -0
  236. package/dist/locales/it.yaml +2 -0
  237. package/dist/locales/ja.yaml +2 -0
  238. package/dist/locales/ko.yaml +2 -0
  239. package/dist/locales/nl.yaml +2 -0
  240. package/dist/locales/pl.yaml +2 -0
  241. package/dist/locales/pt-BR.yaml +2 -0
  242. package/dist/locales/pt-PT.yaml +2 -0
  243. package/dist/locales/ru.yaml +2 -0
  244. package/dist/locales/sk.yaml +2 -0
  245. package/dist/locales/sv.yaml +2 -0
  246. package/dist/locales/tr.yaml +2 -0
  247. package/dist/locales/uk.yaml +2 -0
  248. package/dist/locales/vi.yaml +2 -0
  249. package/dist/locales/zh-CN.yaml +2 -0
  250. package/dist/locales/zh-TW.yaml +2 -0
  251. package/dist/services/composite-widget.svelte.d.ts +76 -0
  252. package/dist/services/composite-widget.svelte.js +157 -0
  253. package/dist/services/focus.d.ts +2 -0
  254. package/dist/services/focus.js +68 -0
  255. package/dist/services/group.svelte.d.ts +17 -69
  256. package/dist/services/group.svelte.js +128 -391
  257. package/dist/services/label.d.ts +9 -0
  258. package/dist/services/label.js +22 -0
  259. package/dist/services/menu.d.ts +8 -0
  260. package/dist/services/menu.js +237 -0
  261. package/dist/services/navigation.d.ts +9 -0
  262. package/dist/services/navigation.js +13 -0
  263. package/dist/services/number.d.ts +2 -0
  264. package/dist/services/number.js +27 -0
  265. package/dist/services/pointer-drag.d.ts +14 -0
  266. package/dist/services/pointer-drag.js +89 -0
  267. package/dist/services/popup.svelte.js +123 -103
  268. package/dist/services/text.d.ts +3 -0
  269. package/dist/services/text.js +17 -0
  270. package/dist/services/tree.svelte.d.ts +29 -43
  271. package/dist/services/tree.svelte.js +58 -105
  272. package/dist/services/type-ahead.d.ts +10 -1
  273. package/dist/services/type-ahead.js +56 -34
  274. package/dist/typedefs.d.ts +108 -6
  275. package/dist/typedefs.js +63 -6
  276. package/package.json +5 -5
  277. package/dist/components/text-editor/conversion/html.d.ts +0 -30
  278. package/dist/components/text-editor/conversion/html.js +0 -857
  279. package/dist/components/text-editor/conversion/markdown.d.ts +0 -56
  280. package/dist/components/text-editor/conversion/markdown.js +0 -949
  281. package/dist/components/text-editor/toolbar/code-editor-toolbar.svelte +0 -28
  282. package/dist/components/text-editor/toolbar/code-editor-toolbar.svelte.d.ts +0 -37
  283. package/dist/components/text-editor/toolbar/insert-component.d.ts +0 -3
  284. package/dist/components/text-editor/toolbar/insert-component.js +0 -37
@@ -1,5 +1,6 @@
1
1
  /**
2
- * Thin wrapper around a Shiki highlighter instance.
2
+ * Thin wrapper around a Shiki highlighter instance, independent of any editor. The Lexical glue
3
+ * that turns tokens into nodes and refreshes code blocks lives in `lexical.js`.
3
4
  *
4
5
  * Adapted from Lexical’s `FacadeShiki.ts` (MIT licensed). Two things differ from upstream, both to
5
6
  * keep Shiki out of the consumer’s bundle. First, the engine is loaded on demand rather than
@@ -10,26 +11,12 @@
10
11
  * @see https://github.com/sveltia/sveltia-cms/issues/587
11
12
  */
12
13
 
13
- import {
14
- $isCodeNode as isCodeNode,
15
- $createCodeHighlightNode as createCodeHighlightNode,
16
- } from '@lexical/code-core';
17
- import {
18
- $createLineBreakNode as createLineBreakNode,
19
- $createTabNode as createTabNode,
20
- $getNodeByKey as getNodeByKey,
21
- HISTORY_MERGE_TAG,
22
- tokenizeRawText,
23
- } from 'lexical';
24
- import { getBackgroundUpdateTags } from '../background-update.js';
25
14
  import { cachePayload, getCachedPayload } from './cache.js';
26
15
  import { LANGUAGES, THEMES } from './generated.js';
27
16
  import { getCodeHighlighterLoaders } from './loader.js';
28
17
  import { getCodeTheme } from './theme.js';
29
18
 
30
19
  /**
31
- * @import { CodeNode } from '@lexical/code-core';
32
- * @import { LexicalEditor, LexicalNode, NodeKey } from 'lexical';
33
20
  * @import { HighlightedToken } from '../../../typedefs.js';
34
21
  */
35
22
 
@@ -102,57 +89,32 @@ export const isCodeLanguageLoaded = (language) => {
102
89
  return highlighter.getLoadedLanguages().includes(id);
103
90
  };
104
91
 
105
- /**
106
- * Mark a code node dirty so the highlighting transform runs again once an async load has completed.
107
- * @param {LexicalEditor} editor Editor instance.
108
- * @param {NodeKey} codeNodeKey Key of the code node to refresh.
109
- */
110
- const refreshCodeNode = (editor, codeNodeKey) => {
111
- editor.update(
112
- () => {
113
- const codeNode = getNodeByKey(codeNodeKey);
114
-
115
- if (!isCodeNode(codeNode)) {
116
- return;
117
- }
118
-
119
- const language = codeNode.getLanguage();
120
-
121
- if (language && isCodeLanguageLoaded(language) && !codeNode.getIsSyntaxHighlightSupported()) {
122
- codeNode.setIsSyntaxHighlightSupported(true);
123
- }
124
-
125
- codeNode.markDirty();
126
- },
127
- { tag: getBackgroundUpdateTags(editor, [HISTORY_MERGE_TAG]) },
128
- );
129
- };
130
-
131
92
  /**
132
93
  * In-flight loads, keyed by what is being loaded.
133
- * @type {Map<string, { promise: Promise<void>, targets: Map<NodeKey, LexicalEditor> }>}
94
+ * @type {Map<string, { promise: Promise<void>, callbacks: Set<() => void> }>}
134
95
  */
135
96
  const pendingLoads = new Map();
136
97
 
137
98
  /**
138
- * Run an asynchronous load once, however many callers ask for it while it is in flight, and refresh
139
- * each waiting code node exactly once when it completes.
99
+ * Run an asynchronous load once, however many callers ask for it while it is in flight, and call
100
+ * each distinct callback exactly once when it completes.
140
101
  *
141
- * The transform fires on every keystroke, so without this a burst of typing during a load would
142
- * start a download per keystroke and then flood the editor with one update each. That storm is
143
- * enough to starve the final re-highlight, leaving a block tokenized as of an earlier keystroke.
102
+ * The editor transform fires on every keystroke, so without this a burst of typing during a load
103
+ * would start a download per keystroke and then flood the editor with one update each. That storm
104
+ * is enough to starve the final re-highlight, leaving a block tokenized as of an earlier keystroke.
105
+ * Callers that want a single notification for repeated requests should therefore pass the same
106
+ * callback each time.
144
107
  * @param {string} key Cache key identifying the load.
145
108
  * @param {() => Promise<void>} run Work to perform on the first call.
146
- * @param {LexicalEditor} [editor] Editor instance to refresh once the load completes.
147
- * @param {NodeKey} [codeNodeKey] Key of the code node to refresh.
109
+ * @param {() => void} [onLoaded] Function to call once the load completes.
148
110
  * @returns {Promise<void>} Promise resolving when the load has completed.
149
111
  */
150
- const loadOnce = (key, run, editor, codeNodeKey) => {
112
+ const loadOnce = (key, run, onLoaded) => {
151
113
  let entry = pendingLoads.get(key);
152
114
 
153
115
  if (!entry) {
154
- /** @type {Map<NodeKey, LexicalEditor>} */
155
- const targets = new Map();
116
+ /** @type {Set<() => void>} */
117
+ const callbacks = new Set();
156
118
 
157
119
  const promise = (async () => {
158
120
  try {
@@ -162,32 +124,32 @@ const loadOnce = (key, run, editor, codeNodeKey) => {
162
124
  }
163
125
 
164
126
  pendingLoads.delete(key);
165
- targets.forEach((target, nodeKey) => refreshCodeNode(target, nodeKey));
127
+ callbacks.forEach((callback) => callback());
166
128
  })();
167
129
 
168
- entry = { promise, targets };
130
+ entry = { promise, callbacks };
169
131
  pendingLoads.set(key, entry);
170
132
  }
171
133
 
172
- if (editor && codeNodeKey) {
173
- entry.targets.set(codeNodeKey, editor);
134
+ if (onLoaded) {
135
+ entry.callbacks.add(onLoaded);
174
136
  }
175
137
 
176
138
  return entry.promise;
177
139
  };
178
140
 
179
141
  /**
180
- * Load the syntax highlighting engine, then refresh the given code node.
142
+ * Load the syntax highlighting engine, then call the given function.
181
143
  *
182
144
  * Tokenizing is synchronous because it runs inside a Lexical node transform, so the engine has to
183
145
  * be resident before any highlighting can happen. The transform bails out while this is in flight
184
- * and is re-run by `refreshCodeNode()`, the same mechanism upstream already uses for grammars.
185
- * @param {LexicalEditor} [editor] Editor instance to refresh once the engine is ready.
186
- * @param {NodeKey} [codeNodeKey] Key of the code node to refresh.
146
+ * and is re-run by the callback, the same mechanism upstream already uses for grammars.
147
+ * @param {() => void} [onLoaded] Function to call once the load has completed, whether or not it
148
+ * succeeded.
187
149
  * @returns {Promise<void> | undefined} Promise resolving when the engine is ready, or `undefined`
188
150
  * when it is already loaded or known to be unavailable.
189
151
  */
190
- export const loadEngine = (editor, codeNodeKey) => {
152
+ export const loadEngine = (onLoaded) => {
191
153
  if (highlighter || engineUnavailable) {
192
154
  return undefined;
193
155
  }
@@ -207,8 +169,7 @@ export const loadEngine = (editor, codeNodeKey) => {
207
169
  engineUnavailable = true;
208
170
  }
209
171
  },
210
- editor,
211
- codeNodeKey,
172
+ onLoaded,
212
173
  );
213
174
  };
214
175
 
@@ -244,15 +205,14 @@ const resolvePayload = async (kind, id, load) => {
244
205
  };
245
206
 
246
207
  /**
247
- * Load a grammar or theme into the highlighter once, then refresh the given code node. The
248
- * configured loader and the highlighter share the method name for each kind.
208
+ * Load a grammar or theme into the highlighter once, then call the given function. The configured
209
+ * loader and the highlighter share the method name for each kind.
249
210
  * @param {'lang' | 'theme'} kind Payload kind.
250
211
  * @param {string} id Language or theme ID.
251
- * @param {LexicalEditor} [editor] Editor instance to refresh once the payload is loaded.
252
- * @param {NodeKey} [codeNodeKey] Key of the code node to refresh.
212
+ * @param {() => void} [onLoaded] Function to call once the load has completed.
253
213
  * @returns {Promise<void>} Promise resolving when the payload is loaded.
254
214
  */
255
- const loadAsset = (kind, id, editor, codeNodeKey) => {
215
+ const loadAsset = (kind, id, onLoaded) => {
256
216
  const method = kind === 'lang' ? 'loadLanguage' : 'loadTheme';
257
217
 
258
218
  return loadOnce(
@@ -264,8 +224,7 @@ const loadAsset = (kind, id, editor, codeNodeKey) => {
264
224
  await highlighter[method](payload);
265
225
  }
266
226
  },
267
- editor,
268
- codeNodeKey,
227
+ onLoaded,
269
228
  );
270
229
  };
271
230
 
@@ -287,14 +246,13 @@ const findLanguage = (language) =>
287
246
  export const normalizeCodeLanguage = (language) => findLanguage(language)?.id ?? language;
288
247
 
289
248
  /**
290
- * Load the grammar for the given language, then refresh the given code node.
249
+ * Load the grammar for the given language, then call the given function.
291
250
  * @param {string} language Language identifier, like `scss` or `diff-js`.
292
- * @param {LexicalEditor} [editor] Editor instance to refresh once the grammar is ready.
293
- * @param {NodeKey} [codeNodeKey] Key of the code node to refresh.
251
+ * @param {() => void} [onLoaded] Function to call once the load has completed.
294
252
  * @returns {Promise<void> | undefined} Promise resolving when the grammar is ready, or `undefined`
295
253
  * when it is already loaded or the language is not supported.
296
254
  */
297
- export const loadCodeLanguage = (language, editor, codeNodeKey) => {
255
+ export const loadCodeLanguage = (language, onLoaded) => {
298
256
  const id = getDiffedLanguage(language) ?? language;
299
257
 
300
258
  if (!highlighter || isCodeLanguageLoaded(id)) {
@@ -307,7 +265,7 @@ export const loadCodeLanguage = (language, editor, codeNodeKey) => {
307
265
  return undefined;
308
266
  }
309
267
 
310
- return loadAsset('lang', info.id, editor, codeNodeKey);
268
+ return loadAsset('lang', info.id, onLoaded);
311
269
  };
312
270
 
313
271
  /**
@@ -324,14 +282,13 @@ export const isCodeThemeLoaded = (theme) => {
324
282
  };
325
283
 
326
284
  /**
327
- * Load the given theme, then refresh the given code node.
285
+ * Load the given theme, then call the given function.
328
286
  * @param {string} theme Theme identifier, like `github-light`.
329
- * @param {LexicalEditor} [editor] Editor instance to refresh once the theme is ready.
330
- * @param {NodeKey} [codeNodeKey] Key of the code node to refresh.
287
+ * @param {() => void} [onLoaded] Function to call once the load has completed.
331
288
  * @returns {Promise<void> | undefined} Promise resolving when the theme is ready, or `undefined`
332
289
  * when it is already loaded or unknown.
333
290
  */
334
- export const loadCodeTheme = (theme, editor, codeNodeKey) => {
291
+ export const loadCodeTheme = (theme, onLoaded) => {
335
292
  if (!highlighter || isCodeThemeLoaded(theme)) {
336
293
  return undefined;
337
294
  }
@@ -340,92 +297,32 @@ export const loadCodeTheme = (theme, editor, codeNodeKey) => {
340
297
  return undefined;
341
298
  }
342
299
 
343
- return loadAsset('theme', theme, editor, codeNodeKey);
300
+ return loadAsset('theme', theme, onLoaded);
344
301
  };
345
302
 
346
303
  /**
347
- * Convert Shiki tokens to Lexical nodes.
348
- * @param {any[][]} tokens Tokens, one array per line.
349
- * @param {boolean} diff Whether the language is a `diff-*` language.
350
- * @returns {LexicalNode[]} Lexical nodes.
304
+ * Tokenize a snippet of code with the raw Shiki tokens, for building editor nodes. The engine, the
305
+ * grammar and the theme must all be loaded.
306
+ * @param {string} code Code to tokenize.
307
+ * @param {string} language Language identifier, like `scss` or `diff-js`.
308
+ * @param {string} [theme] Shiki theme ID.
309
+ * @returns {{ tokens: any[][], diff: boolean }} Tokens, one array per line, and whether the
310
+ * language is a `diff-*` language, in which case the code is tokenized with the language it wraps.
351
311
  */
352
- const mapTokensToLexicalStructure = (tokens, diff) => {
353
- /** @type {LexicalNode[]} */
354
- const nodes = [];
355
-
356
- tokens.forEach((line, lineIndex) => {
357
- if (lineIndex) {
358
- nodes.push(createLineBreakNode());
359
- }
360
-
361
- line.forEach((token, tokenIndex) => {
362
- let { content: text } = token;
363
-
364
- // Implement `diff-*` languages
365
- if (diff && tokenIndex === 0 && text.length > 0) {
366
- const prefixes = ['+', '-', '>', '<', ' '];
367
- const prefixTypes = ['inserted', 'deleted', 'inserted', 'deleted', 'unchanged'];
368
- const prefixIndex = prefixes.indexOf(text[0]);
369
-
370
- if (prefixIndex !== -1) {
371
- nodes.push(createCodeHighlightNode(prefixes[prefixIndex], prefixTypes[prefixIndex]));
372
- text = text.slice(1);
373
- }
374
- }
312
+ export const tokenizeCode = (code, language, theme) => {
313
+ const diffedLanguage = getDiffedLanguage(language);
314
+ const { tokens } = highlighter.codeToTokens(code, { lang: diffedLanguage ?? language, theme });
375
315
 
376
- const style = engine.stringifyTokenStyle(
377
- token.htmlStyle || engine.getTokenStyleObject(token),
378
- );
379
-
380
- tokenizeRawText(text, {
381
- /**
382
- * Append a line break node.
383
- * @returns {number} New node count.
384
- */
385
- linebreak: () => nodes.push(createLineBreakNode()),
386
- /**
387
- * Append a tab node.
388
- * @returns {number} New node count.
389
- */
390
- tab: () => nodes.push(createTabNode()),
391
- /**
392
- * Create a highlight node carrying the token’s colour.
393
- * @param {string} part Text fragment.
394
- */
395
- text: (part) => {
396
- const node = createCodeHighlightNode(part);
397
-
398
- node.setStyle(style);
399
- nodes.push(node);
400
- },
401
- });
402
- });
403
- });
404
-
405
- return nodes;
316
+ return { tokens, diff: !!diffedLanguage };
406
317
  };
407
318
 
408
319
  /**
409
- * Tokenize a code node’s content into Lexical nodes.
410
- *
411
- * Unlike upstream, the theme’s background and foreground colours are not copied onto the code node.
412
- * The block’s own colours come from `--sui-code-background-color` so that plain and highlighted
413
- * blocks look alike and follow the app’s theme rather than Shiki’s; only the per-token colours are
414
- * taken from the theme.
415
- * @param {CodeNode} codeNode Code node to tokenize.
416
- * @param {string} language Language identifier.
417
- * @returns {LexicalNode[]} Lexical nodes.
320
+ * Get the inline style of a Shiki token, preferring its own inline style over the derived one.
321
+ * @param {any} token Token returned by {@link tokenizeCode}.
322
+ * @returns {string} CSS declarations.
418
323
  */
419
- export const getHighlightNodes = (codeNode, language) => {
420
- const diffedLanguage = getDiffedLanguage(language);
421
-
422
- const { tokens } = highlighter.codeToTokens(codeNode.getTextContent(), {
423
- lang: diffedLanguage ?? language,
424
- theme: codeNode.getTheme(),
425
- });
426
-
427
- return mapTokensToLexicalStructure(tokens, !!diffedLanguage);
428
- };
324
+ export const getTokenStyle = (token) =>
325
+ engine.stringifyTokenStyle(token.htmlStyle || engine.getTokenStyleObject(token));
429
326
 
430
327
  /**
431
328
  * Get the theme to highlight a snippet of code with, if it can be highlighted synchronously: the
@@ -35,7 +35,6 @@ import {
35
35
  } from 'lexical';
36
36
  import { getBackgroundUpdateTags } from '../background-update.js';
37
37
  import {
38
- getHighlightNodes,
39
38
  isCodeLanguageLoaded,
40
39
  isCodeThemeLoaded,
41
40
  isEngineLoaded,
@@ -44,7 +43,8 @@ import {
44
43
  loadCodeLanguage,
45
44
  loadCodeTheme,
46
45
  loadEngine,
47
- } from './facade.js';
46
+ } from './engine.js';
47
+ import { getCodeNodeRefresher, getHighlightNodes } from './lexical.js';
48
48
  import { CODE_THEME_LIGHT } from './theme.js';
49
49
 
50
50
  /**
@@ -321,12 +321,12 @@ const codeNodeTransform = (editor, tokenizer, transformState, node) => {
321
321
  }
322
322
  } else if (!isEngineLoaded()) {
323
323
  // Dynamic load of the engine itself
324
- loadEngine(editor, nodeKey);
324
+ loadEngine(getCodeNodeRefresher(editor, nodeKey));
325
325
  inFlight = true;
326
326
  } else {
327
327
  // Dynamic load of themes
328
328
  if (!isCodeThemeLoaded(theme)) {
329
- loadCodeTheme(theme, editor, nodeKey);
329
+ loadCodeTheme(theme, getCodeNodeRefresher(editor, nodeKey));
330
330
  inFlight = true;
331
331
  }
332
332
 
@@ -336,7 +336,7 @@ const codeNodeTransform = (editor, tokenizer, transformState, node) => {
336
336
  node.setIsSyntaxHighlightSupported(true);
337
337
  }
338
338
  } else {
339
- const loadingTask = loadCodeLanguage(language, editor, nodeKey);
339
+ const loadingTask = loadCodeLanguage(language, getCodeNodeRefresher(editor, nodeKey));
340
340
 
341
341
  // If the language is not supported, no download will occur
342
342
  if (!loadingTask && node.getIsSyntaxHighlightSupported()) {
@@ -0,0 +1,6 @@
1
+ export function getCodeNodeRefresher(editor: LexicalEditor, codeNodeKey: NodeKey): () => void;
2
+ export function getHighlightNodes(codeNode: CodeNode, language: string): LexicalNode[];
3
+ import type { LexicalEditor } from 'lexical';
4
+ import type { NodeKey } from 'lexical';
5
+ import type { CodeNode } from '@lexical/code-core';
6
+ import type { LexicalNode } from 'lexical';
@@ -0,0 +1,165 @@
1
+ /**
2
+ * Lexical glue for the Shiki engine: turning tokens into code highlight nodes, and refreshing code
3
+ * nodes once an async load has completed.
4
+ *
5
+ * Adapted from Lexical’s `FacadeShiki.ts` (MIT licensed).
6
+ * @see https://github.com/facebook/lexical/tree/v0.50.0/packages/lexical-code-shiki
7
+ */
8
+
9
+ import {
10
+ $isCodeNode as isCodeNode,
11
+ $createCodeHighlightNode as createCodeHighlightNode,
12
+ } from '@lexical/code-core';
13
+ import {
14
+ $createLineBreakNode as createLineBreakNode,
15
+ $createTabNode as createTabNode,
16
+ $getNodeByKey as getNodeByKey,
17
+ HISTORY_MERGE_TAG,
18
+ tokenizeRawText,
19
+ } from 'lexical';
20
+ import { getBackgroundUpdateTags } from '../background-update.js';
21
+ import { getTokenStyle, isCodeLanguageLoaded, tokenizeCode } from './engine.js';
22
+
23
+ /**
24
+ * @import { CodeNode } from '@lexical/code-core';
25
+ * @import { LexicalEditor, LexicalNode, NodeKey } from 'lexical';
26
+ */
27
+
28
+ /**
29
+ * Mark a code node dirty so the highlighting transform runs again once an async load has completed.
30
+ * @param {LexicalEditor} editor Editor instance.
31
+ * @param {NodeKey} codeNodeKey Key of the code node to refresh.
32
+ */
33
+ const refreshCodeNode = (editor, codeNodeKey) => {
34
+ editor.update(
35
+ () => {
36
+ const codeNode = getNodeByKey(codeNodeKey);
37
+
38
+ if (!isCodeNode(codeNode)) {
39
+ return;
40
+ }
41
+
42
+ const language = codeNode.getLanguage();
43
+
44
+ if (language && isCodeLanguageLoaded(language) && !codeNode.getIsSyntaxHighlightSupported()) {
45
+ codeNode.setIsSyntaxHighlightSupported(true);
46
+ }
47
+
48
+ codeNode.markDirty();
49
+ },
50
+ { tag: getBackgroundUpdateTags(editor, [HISTORY_MERGE_TAG]) },
51
+ );
52
+ };
53
+
54
+ /**
55
+ * Refresh callbacks created so far, per editor and code node.
56
+ * @type {WeakMap<LexicalEditor, Map<NodeKey, () => void>>}
57
+ */
58
+ const refreshers = new WeakMap();
59
+
60
+ /**
61
+ * Get a function that refreshes the given code node, to pass to the engine’s loaders as the
62
+ * `onLoaded` callback. The same function is returned for the same node, so the engine calls it once
63
+ * per load, however many keystrokes have asked for that load while it was in flight.
64
+ * @param {LexicalEditor} editor Editor instance.
65
+ * @param {NodeKey} codeNodeKey Key of the code node to refresh.
66
+ * @returns {() => void} Refresh function.
67
+ */
68
+ export const getCodeNodeRefresher = (editor, codeNodeKey) => {
69
+ let map = refreshers.get(editor);
70
+
71
+ if (!map) {
72
+ map = new Map();
73
+ refreshers.set(editor, map);
74
+ }
75
+
76
+ let refresher = map.get(codeNodeKey);
77
+
78
+ if (!refresher) {
79
+ /** Refresh the code node. */
80
+ refresher = () => {
81
+ refreshCodeNode(editor, codeNodeKey);
82
+ };
83
+
84
+ map.set(codeNodeKey, refresher);
85
+ }
86
+
87
+ return refresher;
88
+ };
89
+
90
+ /**
91
+ * Convert Shiki tokens to Lexical nodes.
92
+ * @param {any[][]} tokens Tokens, one array per line.
93
+ * @param {boolean} diff Whether the language is a `diff-*` language.
94
+ * @returns {LexicalNode[]} Lexical nodes.
95
+ */
96
+ const mapTokensToLexicalStructure = (tokens, diff) => {
97
+ /** @type {LexicalNode[]} */
98
+ const nodes = [];
99
+
100
+ tokens.forEach((line, lineIndex) => {
101
+ if (lineIndex) {
102
+ nodes.push(createLineBreakNode());
103
+ }
104
+
105
+ line.forEach((token, tokenIndex) => {
106
+ let { content: text } = token;
107
+
108
+ // Implement `diff-*` languages
109
+ if (diff && tokenIndex === 0 && text.length > 0) {
110
+ const prefixes = ['+', '-', '>', '<', ' '];
111
+ const prefixTypes = ['inserted', 'deleted', 'inserted', 'deleted', 'unchanged'];
112
+ const prefixIndex = prefixes.indexOf(text[0]);
113
+
114
+ if (prefixIndex !== -1) {
115
+ nodes.push(createCodeHighlightNode(prefixes[prefixIndex], prefixTypes[prefixIndex]));
116
+ text = text.slice(1);
117
+ }
118
+ }
119
+
120
+ const style = getTokenStyle(token);
121
+
122
+ tokenizeRawText(text, {
123
+ /**
124
+ * Append a line break node.
125
+ * @returns {number} New node count.
126
+ */
127
+ linebreak: () => nodes.push(createLineBreakNode()),
128
+ /**
129
+ * Append a tab node.
130
+ * @returns {number} New node count.
131
+ */
132
+ tab: () => nodes.push(createTabNode()),
133
+ /**
134
+ * Create a highlight node carrying the token’s colour.
135
+ * @param {string} part Text fragment.
136
+ */
137
+ text: (part) => {
138
+ const node = createCodeHighlightNode(part);
139
+
140
+ node.setStyle(style);
141
+ nodes.push(node);
142
+ },
143
+ });
144
+ });
145
+ });
146
+
147
+ return nodes;
148
+ };
149
+
150
+ /**
151
+ * Tokenize a code node’s content into Lexical nodes.
152
+ *
153
+ * Unlike upstream, the theme’s background and foreground colours are not copied onto the code node.
154
+ * The block’s own colours come from `--sui-code-background-color` so that plain and highlighted
155
+ * blocks look alike and follow the app’s theme rather than Shiki’s; only the per-token colours are
156
+ * taken from the theme.
157
+ * @param {CodeNode} codeNode Code node to tokenize.
158
+ * @param {string} language Language identifier.
159
+ * @returns {LexicalNode[]} Lexical nodes.
160
+ */
161
+ export const getHighlightNodes = (codeNode, language) => {
162
+ const { tokens, diff } = tokenizeCode(codeNode.getTextContent(), language, codeNode.getTheme());
163
+
164
+ return mapTokensToLexicalStructure(tokens, diff);
165
+ };
@@ -8,7 +8,7 @@ import {
8
8
  loadCodeTheme,
9
9
  loadEngine,
10
10
  normalizeCodeLanguage,
11
- } from './facade.js';
11
+ } from './engine.js';
12
12
  import { getCodeTheme } from './theme.js';
13
13
 
14
14
  /**
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @import { HighlightedToken } from '../../../typedefs.js';
3
+ */
4
+ /**
5
+ * Maximum length of the Markdown or HTML source to highlight in the plain text mode. The whole
6
+ * source is tokenized on every keystroke, so highlighting a longer one would make typing sluggish.
7
+ */
8
+ export const MAX_HIGHLIGHT_LENGTH: 20000;
9
+ export function getCodeFenceLanguages(source: string): string[];
10
+ export function createSourceHighlighter({ language, isActive, getSource }: {
11
+ language: "markdown" | "html";
12
+ isActive: () => boolean;
13
+ getSource: () => string;
14
+ }): (source: string) => HighlightedToken[][] | undefined;
15
+ import type { HighlightedToken } from '../../../typedefs.js';