@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
@@ -24,6 +24,8 @@ import {
24
24
  } from '@lexical/table';
25
25
  import { $isParagraphNode as isParagraphNode, $isTextNode as isTextNode } from 'lexical';
26
26
 
27
+ import { mapCodeSpans } from '../markdown/code-spans.js';
28
+ import { UNESCAPED_LOOKBEHIND } from '../markdown/syntax.js';
27
29
  import { BLOCK_SYNTAX_ESCAPE } from './block-escape.js';
28
30
  import { LEADING_INLINE_CODE } from './inline-code.js';
29
31
  import { LINK } from './link.js';
@@ -60,6 +62,17 @@ const getColumnAlignment = (delimiter) => {
60
62
  return left && right ? 'center' : left ? 'left' : right ? 'right' : '';
61
63
  };
62
64
 
65
+ /**
66
+ * Regular expression to match `\n` in a cell, which stands for a line break unless its backslash is
67
+ * escaped, like `\\n`. The lookahead comes first, so the lookbehind is only tried before a `\n`.
68
+ */
69
+ const CELL_LINE_BREAK_REG_EXP = new RegExp(String.raw`(?=\\n)${UNESCAPED_LOOKBEHIND}\\n`, 'g');
70
+ /**
71
+ * Regular expression to match a pipe that isn’t escaped, which separates the cells of a row. The
72
+ * lookahead comes first, so the lookbehind is only tried before a pipe.
73
+ */
74
+ const UNESCAPED_PIPE_REG_EXP = new RegExp(String.raw`(?=\|)${UNESCAPED_LOOKBEHIND}\|`);
75
+
63
76
  /**
64
77
  * Returns the number of columns in the table.
65
78
  * @internal
@@ -79,9 +92,12 @@ export const getTableColumnsSize = (table) => {
79
92
  * @returns {TableCellNode}
80
93
  */
81
94
  const createTableCell = (textContent, transformers) => {
82
- // An escaped pipe is part of the cell, even within inline code, like GitHub Flavored Markdown.
83
- // The spaces around the content only separate it from the pipes
84
- textContent = textContent.trim().replace(/\\n/g, '\n').replace(/\\\|/g, '|');
95
+ // `\n` is a line break in the cell, except within inline code, where it’s literal, or after an
96
+ // escaped backslash. An escaped pipe is part of the cell, even within inline code, like GitHub
97
+ // Flavored Markdown. The spaces around the content only separate it from the pipes
98
+ textContent = mapCodeSpans(textContent.trim(), {
99
+ outside: (text) => text.replace(CELL_LINE_BREAK_REG_EXP, '\n'),
100
+ }).replace(/\\\|/g, '|');
85
101
 
86
102
  const cell = createTableCellNode(TableCellHeaderStates.NO_STATUS);
87
103
 
@@ -105,8 +121,8 @@ export const mapToTableCells = (textContent, transformers) => {
105
121
  }
106
122
 
107
123
  // Split the row at the pipes that are not escaped, which are preceded by an even number of
108
- // backslashes
109
- return match.split(/(?<=(?<!\\)(?:\\\\)*)\|/).map((text) => createTableCell(text, transformers));
124
+ // backslashes, if any
125
+ return match.split(UNESCAPED_PIPE_REG_EXP).map((text) => createTableCell(text, transformers));
110
126
  };
111
127
 
112
128
  /**
@@ -126,7 +142,12 @@ const exportTableRow = (row, transformers) => {
126
142
  // It’s TableCellNode so it’s just to make flow happy
127
143
  if (isTableCellNode(cell)) {
128
144
  rowOutput.push(
129
- convertToMarkdownString(transformers, cell)
145
+ // A backslash followed by `n` in text, like that in `C:\new`, is escaped, so it’s not read
146
+ // as a line break; it means the same in Markdown. Inline code is left as is, as `\n` is
147
+ // literal there
148
+ mapCodeSpans(convertToMarkdownString(transformers, cell), {
149
+ outside: (text) => text.replace(CELL_LINE_BREAK_REG_EXP, '\\\\n'),
150
+ })
130
151
  // Escape a pipe, which would otherwise end the cell, even within inline code
131
152
  .replace(/\|/g, '\\|')
132
153
  .replace(/\n/g, '\\n')
@@ -235,10 +256,15 @@ const $takePrecedingRows = (parentNode, transformers) => {
235
256
  * Create a table from the given rows, padding the shorter ones with empty cells.
236
257
  * @param {TableCellNode[][]} rows Cells of each row.
237
258
  * @param {Transformer[]} transformers Transformers to import the empty cell content with.
259
+ * @param {number} [columnsSize] Number of columns. The cells beyond it are dropped. The default is
260
+ * the number of cells in the longest row.
238
261
  * @returns {TableNode} Table.
239
262
  */
240
- const $createTable = (rows, transformers) => {
241
- const maxCells = Math.max(...rows.map((cells) => cells.length));
263
+ const $createTable = (
264
+ rows,
265
+ transformers,
266
+ columnsSize = Math.max(...rows.map((cells) => cells.length)),
267
+ ) => {
242
268
  const table = createTableNode();
243
269
 
244
270
  rows.forEach((cells) => {
@@ -246,7 +272,7 @@ const $createTable = (rows, transformers) => {
246
272
 
247
273
  table.append(tableRow);
248
274
 
249
- for (let i = 0; i < maxCells; i += 1) {
275
+ for (let i = 0; i < columnsSize; i += 1) {
250
276
  tableRow.append(i < cells.length ? cells[i] : createTableCell('', transformers));
251
277
  }
252
278
  });
@@ -255,19 +281,33 @@ const $createTable = (rows, transformers) => {
255
281
  };
256
282
 
257
283
  /**
258
- * Put a new table in place of the given paragraph, or add its rows to the table right before it if
259
- * that has the same number of columns. The rows then take the column alignment from its header.
284
+ * Put a new table with the given rows in place of the given paragraph, or add the rows to the table
285
+ * right before it if that has a header row or the same number of columns. Like GitHub Flavored
286
+ * Markdown, a row with fewer cells than the header row is padded with empty cells, and the cells
287
+ * beyond it are dropped. The rows then take the column alignment from the header.
260
288
  * @param {ElementNode} parentNode Paragraph to replace.
261
- * @param {TableNode} table New table.
289
+ * @param {TableCellNode[][]} rows Cells of each row.
290
+ * @param {Transformer[]} transformers Transformers to import the empty cell content with.
291
+ * @see https://github.github.com/gfm/#tables-extension-
262
292
  */
263
- const $insertImportedTable = (parentNode, table) => {
293
+ const $insertImportedTable = (parentNode, rows, transformers) => {
264
294
  const previousSibling = parentNode.getPreviousSibling();
295
+ const headerRow = isTableNode(previousSibling) ? previousSibling.getFirstChild() : null;
296
+ const headerCells = isTableRowNode(headerRow) ? headerRow.getChildren() : [];
297
+
298
+ const hasHeaderRow = headerCells.some(
299
+ (cell) => isTableCellNode(cell) && cell.hasHeaderState(TableCellHeaderStates.ROW),
300
+ );
301
+
302
+ /** @type {TableNode} */
303
+ let table;
265
304
 
266
305
  if (
267
306
  isTableNode(previousSibling) &&
268
- getTableColumnsSize(previousSibling) === getTableColumnsSize(table)
307
+ (hasHeaderRow ||
308
+ getTableColumnsSize(previousSibling) === Math.max(...rows.map((cells) => cells.length)))
269
309
  ) {
270
- const headerCells = /** @type {TableRowNode} */ (previousSibling.getFirstChild()).getChildren();
310
+ table = $createTable(rows, transformers, headerCells.length);
271
311
 
272
312
  table.getChildren().forEach((row) => {
273
313
  /** @type {TableRowNode} */ (row).getChildren().forEach((cell, index) => {
@@ -280,6 +320,7 @@ const $insertImportedTable = (parentNode, table) => {
280
320
  previousSibling.append(...table.getChildren());
281
321
  parentNode.remove();
282
322
  } else {
323
+ table = $createTable(rows, transformers);
283
324
  parentNode.replace(table);
284
325
  }
285
326
 
@@ -335,7 +376,7 @@ export const createTableTransformer = (cellTransformers = []) => {
335
376
 
336
377
  const rows = [...$takePrecedingRows(parentNode, transformers), matchCells];
337
378
 
338
- $insertImportedTable(parentNode, $createTable(rows, transformers));
379
+ $insertImportedTable(parentNode, rows, transformers);
339
380
  },
340
381
  type: 'element',
341
382
  };
@@ -0,0 +1,24 @@
1
+ /**
2
+ * @import { ElementTransformer } from '@lexical/markdown';
3
+ */
4
+ /**
5
+ * Start of a task list item as GitHub Flavored Markdown defines it: a bullet list marker followed
6
+ * by a check box holding a space or `x`, like `- [ ] item`. Like Lexical’s `CHECK_LIST`, it allows
7
+ * one or two spaces between them; with more, the line is a bulleted list item starting with `[ ]`.
8
+ * Like the other block checks in the plain text mode, it doesn’t allow any indentation, which a
9
+ * nested item has.
10
+ * @see https://github.github.com/gfm/#task-list-items-extension-
11
+ */
12
+ export const TASK_LIST_ITEM_REGEX: RegExp;
13
+ /**
14
+ * Transformer for task list items, based on Lexical’s `CHECK_LIST`, which also takes a line without
15
+ * a list marker or with an empty check box, like `[x] Agreed` or `[] Empty`, for a task list item.
16
+ * That suits a Markdown shortcut, like typing `[ ] ` at the start of a paragraph, but would turn
17
+ * such a paragraph into a task list when Markdown is imported, so an import only takes a line that
18
+ * matches {@link TASK_LIST_ITEM_REGEX}, leaving any other line to the other transformers. Lexical
19
+ * has already removed the matched text from the line by then, and doesn’t put it back when a
20
+ * transformer leaves the line, so it’s restored here.
21
+ * @type {ElementTransformer}
22
+ */
23
+ export const TASK_LIST: ElementTransformer;
24
+ import type { ElementTransformer } from '@lexical/markdown';
@@ -0,0 +1,53 @@
1
+ import { CHECK_LIST } from '@lexical/markdown';
2
+ import { $isTextNode as isTextNode } from 'lexical';
3
+
4
+ /**
5
+ * @import { ElementTransformer } from '@lexical/markdown';
6
+ */
7
+
8
+ /**
9
+ * Start of a task list item as GitHub Flavored Markdown defines it: a bullet list marker followed
10
+ * by a check box holding a space or `x`, like `- [ ] item`. Like Lexical’s `CHECK_LIST`, it allows
11
+ * one or two spaces between them; with more, the line is a bulleted list item starting with `[ ]`.
12
+ * Like the other block checks in the plain text mode, it doesn’t allow any indentation, which a
13
+ * nested item has.
14
+ * @see https://github.github.com/gfm/#task-list-items-extension-
15
+ */
16
+ export const TASK_LIST_ITEM_REGEX = /^[-*+][ \t]{1,2}\[[ xX]\][ \t]/;
17
+
18
+ /**
19
+ * Transformer for task list items, based on Lexical’s `CHECK_LIST`, which also takes a line without
20
+ * a list marker or with an empty check box, like `[x] Agreed` or `[] Empty`, for a task list item.
21
+ * That suits a Markdown shortcut, like typing `[ ] ` at the start of a paragraph, but would turn
22
+ * such a paragraph into a task list when Markdown is imported, so an import only takes a line that
23
+ * matches {@link TASK_LIST_ITEM_REGEX}, leaving any other line to the other transformers. Lexical
24
+ * has already removed the matched text from the line by then, and doesn’t put it back when a
25
+ * transformer leaves the line, so it’s restored here.
26
+ * @type {ElementTransformer}
27
+ */
28
+ export const TASK_LIST = {
29
+ ...CHECK_LIST,
30
+ /**
31
+ * Replace the matched line with a task list item, unless it’s imported and isn’t one.
32
+ * @param {Parameters<ElementTransformer['replace']>} args Arguments.
33
+ * @returns {boolean | void} `false` if the line isn’t a task list item.
34
+ */
35
+ replace: (...args) => {
36
+ const [, children, match, isImport] = args;
37
+
38
+ // The match includes the indentation of a nested item
39
+ if (isImport && !TASK_LIST_ITEM_REGEX.test(match[0].trimStart())) {
40
+ const [textNode] = children;
41
+
42
+ if (isTextNode(textNode)) {
43
+ textNode.setTextContent(
44
+ /** @type {string} */ (/** @type {RegExpMatchArray} */ (match).input),
45
+ );
46
+ }
47
+
48
+ return false;
49
+ }
50
+
51
+ return CHECK_LIST.replace(...args);
52
+ },
53
+ };
@@ -1,6 +1,23 @@
1
1
  export function exportMarkdown(enabledTransformers: Transformer[]): string;
2
- export function onEditorUpdate(editor: LexicalEditor, enabledTransformers: Transformer[], cachedValue?: string, format?: TextEditorValueFormat): string;
3
- export function registerUpdateEvent(editor: LexicalEditor, enabledTransformers: Transformer[], format: TextEditorValueFormat): () => void;
2
+ export function exportValue(editor: LexicalEditor, enabledTransformers: Transformer[], format?: TextEditorValueFormat): string;
3
+ export function registerValueExport(editor: LexicalEditor, enabledTransformers: Transformer[], format: TextEditorValueFormat, onExport: (detail: ExportDetail) => void): {
4
+ flush: () => void;
5
+ unregister: () => void;
6
+ };
7
+ /**
8
+ * Value exported from the editor, and the selection state at the time.
9
+ */
10
+ export type ExportDetail = {
11
+ /**
12
+ * Markdown or HTML value.
13
+ */
14
+ value: string;
15
+ /**
16
+ * Selection state.
17
+ */
18
+ selection: TextEditorSelectionState;
19
+ };
4
20
  import type { Transformer } from '@lexical/markdown';
5
21
  import type { LexicalEditor } from 'lexical';
6
22
  import type { TextEditorValueFormat } from '../../../typedefs.js';
23
+ import type { TextEditorSelectionState } from '../../../typedefs.js';
@@ -1,12 +1,12 @@
1
1
  /**
2
- * Exporting the editor content, and the Update event fired on changes.
2
+ * Exporting the editor content, and passing it on as it changes.
3
3
  */
4
4
 
5
5
  import { $convertToMarkdownString as convertToMarkdownString } from '@lexical/markdown';
6
6
  import { sleep } from '@sveltia/utils/misc';
7
7
  import { DISABLED_MARKDOWN_TAGS } from '../constants.js';
8
- import { exportHtml } from './html.js';
9
- import { trimBlankBlockquoteLines } from './markdown.js';
8
+ import { exportHtml } from './html/index.js';
9
+ import { trimBlankBlockquoteLines } from './markdown/index.js';
10
10
  import { BLOCK_SYNTAX_ESCAPE } from './transformers/block-escape.js';
11
11
  import { getSelectionTypes } from '../editing/selection.js';
12
12
  import { mergeUnregisters } from '../utils.js';
@@ -14,7 +14,14 @@ import { mergeUnregisters } from '../utils.js';
14
14
  /**
15
15
  * @import { LexicalEditor } from 'lexical';
16
16
  * @import { Transformer } from '@lexical/markdown';
17
- * @import { TextEditorValueFormat } from '../../../typedefs.js';
17
+ * @import { TextEditorSelectionState, TextEditorValueFormat } from '../../../typedefs.js';
18
+ */
19
+
20
+ /**
21
+ * Value exported from the editor, and the selection state at the time.
22
+ * @typedef {object} ExportDetail
23
+ * @property {string} value Markdown or HTML value.
24
+ * @property {TextEditorSelectionState} selection Selection state.
18
25
  */
19
26
 
20
27
  /**
@@ -42,49 +49,70 @@ export const exportMarkdown = (enabledTransformers) => {
42
49
  };
43
50
 
44
51
  /**
45
- * Listen to changes made on the editor and trigger the Update event.
52
+ * Export the editor content as Markdown or HTML. Call this within an editor update or read.
46
53
  * @internal
47
54
  * @param {LexicalEditor} editor Editor instance.
48
55
  * @param {Transformer[]} enabledTransformers Enabled Markdown transformers.
49
- * @param {string} [cachedValue] Value from a previous call, to be reused instead of converting the
50
- * whole document again when only the selection has changed.
51
56
  * @param {TextEditorValueFormat} [format] Value format.
52
57
  * @returns {string} Markdown or HTML value.
53
58
  */
54
- export const onEditorUpdate = (editor, enabledTransformers, cachedValue, format = 'markdown') => {
55
- const value =
56
- cachedValue ?? (format === 'html' ? exportHtml(editor) : exportMarkdown(enabledTransformers));
57
-
58
- editor.getRootElement()?.dispatchEvent(
59
- new CustomEvent('Update', {
60
- detail: {
61
- value,
62
- selection: getSelectionTypes(),
63
- },
64
- }),
65
- );
66
-
67
- return value;
68
- };
59
+ export const exportValue = (editor, enabledTransformers, format = 'markdown') =>
60
+ format === 'html' ? exportHtml(editor) : exportMarkdown(enabledTransformers);
69
61
 
70
62
  /**
71
- * Register an update listener that triggers the Update event with the Markdown or HTML value,
72
- * debounced so only the latest update in a burst triggers it.
63
+ * Register an update listener that exports the editor content as Markdown or HTML, and passes it
64
+ * on along with the selection state, debounced so only the latest update in a burst is exported.
73
65
  * @param {LexicalEditor} editor Editor instance.
74
66
  * @param {Transformer[]} enabledTransformers Enabled Markdown transformers.
75
67
  * @param {TextEditorValueFormat} format Value format.
76
- * @returns {() => void} Cleanup handler.
68
+ * @param {(detail: ExportDetail) => void} onExport Function to call with the exported value. It’s
69
+ * called within an editor update.
70
+ * @returns {{ flush: () => void, unregister: () => void }} `flush` exports the content scheduled
71
+ * to be exported right away, instead of after the delay, so the value reflects what was just
72
+ * typed. Call it before acting on the value, like when the focus leaves the editor or before
73
+ * switching to the Markdown or HTML source. It does nothing if nothing is scheduled. `unregister`
74
+ * removes the listener.
77
75
  */
78
- export const registerUpdateEvent = (editor, enabledTransformers, format) => {
79
- /** Incremented on every update, so only the latest one in a burst triggers the Update event. */
76
+ export const registerValueExport = (editor, enabledTransformers, format, onExport) => {
77
+ /** Incremented on every update, so only the latest one in a burst is exported. */
80
78
  let updateCount = 0;
81
- /** Whether the content has changed since the Update event was last triggered. */
79
+ /** Whether the content has changed since it was last exported. */
82
80
  let contentChanged = true;
83
81
  /** @type {string | undefined} */
84
82
  let lastValue = undefined;
85
83
  let disposed = false;
84
+ /** Whether an export is scheduled, and hasn’t been made yet. */
85
+ let scheduled = false;
86
+
87
+ /**
88
+ * Export the current value, and pass it on.
89
+ */
90
+ const triggerExport = () => {
91
+ scheduled = false;
92
+
93
+ editor.update(() => {
94
+ // Reuse the value when only the selection has changed, instead of converting the whole
95
+ // document again
96
+ const value =
97
+ (contentChanged ? undefined : lastValue) ??
98
+ exportValue(editor, enabledTransformers, format);
86
99
 
87
- return mergeUnregisters(
100
+ onExport({ value, selection: getSelectionTypes() });
101
+ lastValue = value;
102
+ contentChanged = false;
103
+ });
104
+ };
105
+
106
+ /**
107
+ * Export the scheduled value right away.
108
+ */
109
+ const flush = () => {
110
+ if (scheduled && !disposed && !editor.isComposing()) {
111
+ triggerExport();
112
+ }
113
+ };
114
+
115
+ const unregister = mergeUnregisters(
88
116
  () => {
89
117
  disposed = true;
90
118
  },
@@ -99,27 +127,23 @@ export const registerUpdateEvent = (editor, enabledTransformers, format) => {
99
127
  }
100
128
 
101
129
  updateCount += 1;
130
+ scheduled = true;
102
131
 
103
132
  const currentCount = updateCount;
104
133
 
105
134
  (async () => {
106
135
  await sleep(100);
107
136
 
108
- // Skip if a newer update has been made in the meantime, or the editor has been disposed
109
- if (currentCount !== updateCount || disposed) {
137
+ // Skip if a newer update has been made in the meantime, the value has been exported
138
+ // already with `flush()`, or the editor has been disposed
139
+ if (currentCount !== updateCount || !scheduled || disposed) {
110
140
  return;
111
141
  }
112
142
 
113
- editor.update(() => {
114
- lastValue = onEditorUpdate(
115
- editor,
116
- enabledTransformers,
117
- contentChanged ? undefined : lastValue,
118
- format,
119
- );
120
- contentChanged = false;
121
- });
143
+ triggerExport();
122
144
  })();
123
145
  }),
124
146
  );
147
+
148
+ return { flush, unregister };
125
149
  };
@@ -1,4 +1,4 @@
1
- export function initEditor(config: TextEditorConfig): InitEditorResult;
1
+ export function initEditor(config: Partial<TextEditorConfig>, { onExport }?: InitEditorOptions): InitEditorResult;
2
2
  export type InitEditorResult = {
3
3
  /**
4
4
  * Editor instance.
@@ -8,64 +8,25 @@ export type InitEditorResult = {
8
8
  * List of enabled Markdown transformers.
9
9
  */
10
10
  enabledTransformers: Transformer[];
11
+ /**
12
+ * Export the content scheduled to be exported right away, instead
13
+ * of after the delay, so the value reflects what was just typed. Does nothing if nothing is
14
+ * scheduled.
15
+ */
16
+ flush: () => void;
11
17
  /**
12
18
  * Remove all registered Lexical listeners.
13
19
  */
14
20
  dispose: () => void;
15
21
  };
16
- /**
17
- * Editor configuration with the defaults applied, and the features derived from it.
18
- */
19
- export type EditorSetup = {
20
- /**
21
- * Enabled buttons.
22
- */
23
- enabledButtons: TextEditorNodeType[];
24
- /**
25
- * Editor components.
26
- */
27
- components: TextEditorComponent[];
28
- /**
29
- * Whether to enable Markdown shortcuts.
30
- */
31
- useMarkdownShortcuts: boolean | undefined;
32
- /**
33
- * Whether the editor is a code editor.
34
- */
35
- isCodeEditor: boolean;
36
- /**
37
- * Default code block language.
38
- */
39
- defaultLanguage: string;
40
- /**
41
- * Value format.
42
- */
43
- format: TextEditorValueFormat;
44
- /**
45
- * Whether the editor accepts multiple lines.
46
- */
47
- multiline: boolean;
48
- /**
49
- * Whether to support code blocks.
50
- */
51
- hasCodeBlock: boolean;
52
- /**
53
- * Whether to support table editing.
54
- */
55
- hasTable: boolean;
56
- /**
57
- * Whether to support selecting a horizontal rule.
58
- */
59
- hasHorizontalRule: boolean;
22
+ export type InitEditorOptions = {
60
23
  /**
61
- * Whether to support horizontal rules and tables in Markdown,
62
- * even without their buttons.
24
+ * Function to call with the Markdown or HTML
25
+ * value and the selection state, a moment after the editor content or selection has changed.
63
26
  */
64
- hasBlockSyntax: boolean;
27
+ onExport?: ((detail: ExportDetail) => void) | undefined;
65
28
  };
66
29
  import type { TextEditorConfig } from '../../typedefs.js';
67
30
  import type { LexicalEditor } from 'lexical';
68
31
  import type { Transformer } from '@lexical/markdown';
69
- import type { TextEditorNodeType } from '../../typedefs.js';
70
- import type { TextEditorComponent } from '../../typedefs.js';
71
- import type { TextEditorValueFormat } from '../../typedefs.js';
32
+ import type { ExportDetail } from './conversion/update.js';