@aionbuilders/nabu 0.1.0-beta.0 → 0.1.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 (306) hide show
  1. package/CHANGELOG.md +523 -0
  2. package/LICENSE +21 -0
  3. package/README.md +72 -341
  4. package/dist/behaviors/text/RichText.svelte +24 -4
  5. package/dist/behaviors/text/markdown-shortcuts.extension.d.ts +3 -0
  6. package/dist/behaviors/text/markdown-shortcuts.extension.js +35 -10
  7. package/dist/behaviors/text/markdown-syntax.d.ts +42 -0
  8. package/dist/behaviors/text/markdown-syntax.js +81 -0
  9. package/dist/behaviors/text/rich-text.extension.d.ts +3 -0
  10. package/dist/behaviors/text/rich-text.extension.js +95 -38
  11. package/dist/behaviors/text/text.behavior.svelte.d.ts +199 -48
  12. package/dist/behaviors/text/text.behavior.svelte.js +549 -83
  13. package/dist/behaviors/text/typography.extension.d.ts +2 -1
  14. package/dist/behaviors/text/typography.extension.js +3 -2
  15. package/dist/blocks/BlockList.svelte +48 -0
  16. package/dist/blocks/BlockList.svelte.d.ts +17 -0
  17. package/dist/blocks/Nabu.svelte +169 -25
  18. package/dist/blocks/Nabu.svelte.d.ts +13 -0
  19. package/dist/blocks/block.extension.d.ts +3 -0
  20. package/dist/blocks/block.extension.js +4 -7
  21. package/dist/blocks/block.svelte.d.ts +350 -86
  22. package/dist/blocks/block.svelte.js +370 -59
  23. package/dist/blocks/blockquote/Blockquote.svelte +1 -2
  24. package/dist/blocks/blockquote/blockquote.extension.d.ts +4 -1
  25. package/dist/blocks/blockquote/blockquote.extension.js +14 -5
  26. package/dist/blocks/blockquote/blockquote.svelte.d.ts +19 -11
  27. package/dist/blocks/blockquote/blockquote.svelte.js +58 -13
  28. package/dist/blocks/callout/Callout.svelte +6 -8
  29. package/dist/blocks/callout/callout.extension.d.ts +4 -1
  30. package/dist/blocks/callout/callout.extension.js +19 -3
  31. package/dist/blocks/callout/callout.svelte.d.ts +5 -3
  32. package/dist/blocks/callout/callout.svelte.js +11 -6
  33. package/dist/blocks/chunks.d.ts +49 -0
  34. package/dist/blocks/chunks.js +90 -0
  35. package/dist/blocks/code/Code.svelte +7 -8
  36. package/dist/blocks/code/code.extension.d.ts +4 -1
  37. package/dist/blocks/code/code.extension.js +22 -2
  38. package/dist/blocks/code/code.svelte.d.ts +9 -2
  39. package/dist/blocks/code/code.svelte.js +49 -25
  40. package/dist/blocks/container.utils.d.ts +85 -13
  41. package/dist/blocks/container.utils.js +338 -178
  42. package/dist/blocks/dialogue/Dialogue.svelte +8 -4
  43. package/dist/blocks/dialogue/dialogue.svelte.d.ts +33 -2
  44. package/dist/blocks/dialogue/dialogue.svelte.js +58 -4
  45. package/dist/blocks/dialogue/hooks/onBeforeInput.hook.d.ts +2 -2
  46. package/dist/blocks/dialogue/hooks/onBeforeInput.hook.js +6 -4
  47. package/dist/blocks/dialogue/index.d.ts +12 -1
  48. package/dist/blocks/dialogue/index.js +51 -35
  49. package/dist/blocks/divider/divider.extension.d.ts +23 -1
  50. package/dist/blocks/divider/divider.extension.js +85 -19
  51. package/dist/blocks/divider/divider.svelte.d.ts +7 -2
  52. package/dist/blocks/divider/divider.svelte.js +9 -4
  53. package/dist/blocks/file/File.svelte +13 -6
  54. package/dist/blocks/file/file.extension.d.ts +4 -1
  55. package/dist/blocks/file/file.extension.js +9 -1
  56. package/dist/blocks/file/file.svelte.d.ts +3 -3
  57. package/dist/blocks/file/file.svelte.js +20 -14
  58. package/dist/blocks/heading/Heading.svelte +1 -2
  59. package/dist/blocks/heading/heading.svelte.d.ts +23 -11
  60. package/dist/blocks/heading/heading.svelte.js +68 -13
  61. package/dist/blocks/heading/hooks/onBeforeInput.hook.js +7 -5
  62. package/dist/blocks/heading/index.d.ts +2 -2
  63. package/dist/blocks/heading/index.js +33 -8
  64. package/dist/blocks/image/Image.svelte +5 -2
  65. package/dist/blocks/image/image.extension.d.ts +4 -1
  66. package/dist/blocks/image/image.extension.js +9 -1
  67. package/dist/blocks/image/image.svelte.d.ts +17 -6
  68. package/dist/blocks/image/image.svelte.js +33 -11
  69. package/dist/blocks/index.d.ts +1 -1
  70. package/dist/blocks/index.js +1 -1
  71. package/dist/blocks/list/List.svelte +1 -0
  72. package/dist/blocks/list/ListItem.svelte +1 -0
  73. package/dist/blocks/list/convert.d.ts +20 -0
  74. package/dist/blocks/list/convert.js +92 -0
  75. package/dist/blocks/list/hooks/onBeforeInput.hook.js +49 -28
  76. package/dist/blocks/list/html-flat.d.ts +78 -0
  77. package/dist/blocks/list/html-flat.js +244 -0
  78. package/dist/blocks/list/indent.d.ts +86 -0
  79. package/dist/blocks/list/indent.js +205 -0
  80. package/dist/blocks/list/index.d.ts +6 -3
  81. package/dist/blocks/list/index.js +42 -31
  82. package/dist/blocks/list/list-item.svelte.d.ts +31 -17
  83. package/dist/blocks/list/list-item.svelte.js +99 -100
  84. package/dist/blocks/list/list.behavior.svelte.d.ts +4 -1
  85. package/dist/blocks/list/list.behavior.svelte.js +6 -1
  86. package/dist/blocks/list/list.svelte.d.ts +89 -18
  87. package/dist/blocks/list/list.svelte.js +129 -125
  88. package/dist/blocks/list/markdown.d.ts +149 -0
  89. package/dist/blocks/list/markdown.js +442 -0
  90. package/dist/blocks/list/normalize.d.ts +11 -0
  91. package/dist/blocks/list/normalize.js +396 -0
  92. package/dist/blocks/list/paste.d.ts +45 -0
  93. package/dist/blocks/list/paste.js +242 -0
  94. package/dist/blocks/list/step-back.d.ts +24 -0
  95. package/dist/blocks/list/step-back.js +87 -0
  96. package/dist/blocks/list/to-list.d.ts +7 -0
  97. package/dist/blocks/list/to-list.js +44 -0
  98. package/dist/blocks/list/todos-toggle.d.ts +20 -0
  99. package/dist/blocks/list/todos-toggle.js +48 -0
  100. package/dist/blocks/media/markdown.d.ts +12 -0
  101. package/dist/blocks/media/markdown.js +26 -0
  102. package/dist/blocks/media/media-block.svelte.d.ts +77 -2
  103. package/dist/blocks/media/media-block.svelte.js +124 -12
  104. package/dist/blocks/media/media.helpers.d.ts +6 -1
  105. package/dist/blocks/media/media.helpers.js +43 -6
  106. package/dist/blocks/megablock.svelte.d.ts +50 -19
  107. package/dist/blocks/megablock.svelte.js +114 -74
  108. package/dist/blocks/move.d.ts +23 -0
  109. package/dist/blocks/move.js +92 -0
  110. package/dist/blocks/nabu.svelte.d.ts +764 -71
  111. package/dist/blocks/nabu.svelte.js +1746 -189
  112. package/dist/blocks/paragraph/Paragraph.svelte +1 -2
  113. package/dist/blocks/paragraph/index.d.ts +2 -2
  114. package/dist/blocks/paragraph/index.js +35 -13
  115. package/dist/blocks/paragraph/paragraph.svelte.d.ts +46 -14
  116. package/dist/blocks/paragraph/paragraph.svelte.js +77 -16
  117. package/dist/blocks/paragraph/table.html.d.ts +9 -0
  118. package/dist/blocks/paragraph/table.html.js +111 -0
  119. package/dist/blocks/paragraph/table.markdown.d.ts +32 -0
  120. package/dist/blocks/paragraph/table.markdown.js +90 -0
  121. package/dist/blocks/region/Region.svelte +264 -0
  122. package/dist/blocks/region/Region.svelte.d.ts +11 -0
  123. package/dist/blocks/region/actions.d.ts +135 -0
  124. package/dist/blocks/region/actions.js +402 -0
  125. package/dist/blocks/region/close.d.ts +35 -0
  126. package/dist/blocks/region/close.js +130 -0
  127. package/dist/blocks/region/commands.d.ts +6 -0
  128. package/dist/blocks/region/commands.js +277 -0
  129. package/dist/blocks/region/create.d.ts +72 -0
  130. package/dist/blocks/region/create.js +208 -0
  131. package/dist/blocks/region/end-marker.d.ts +35 -0
  132. package/dist/blocks/region/end-marker.js +51 -0
  133. package/dist/blocks/region/facts.d.ts +69 -0
  134. package/dist/blocks/region/facts.js +152 -0
  135. package/dist/blocks/region/format.d.ts +91 -0
  136. package/dist/blocks/region/format.js +260 -0
  137. package/dist/blocks/region/index.d.ts +14 -0
  138. package/dist/blocks/region/index.js +14 -0
  139. package/dist/blocks/region/insert.d.ts +59 -0
  140. package/dist/blocks/region/insert.js +102 -0
  141. package/dist/blocks/region/messages.d.ts +254 -0
  142. package/dist/blocks/region/messages.js +131 -0
  143. package/dist/blocks/region/normalize.d.ts +7 -0
  144. package/dist/blocks/region/normalize.js +170 -0
  145. package/dist/blocks/region/region.extension.d.ts +58 -0
  146. package/dist/blocks/region/region.extension.js +119 -0
  147. package/dist/blocks/region/region.svelte.d.ts +221 -0
  148. package/dist/blocks/region/region.svelte.js +582 -0
  149. package/dist/blocks/region/regions.svelte.d.ts +141 -0
  150. package/dist/blocks/region/regions.svelte.js +276 -0
  151. package/dist/blocks/region/serialize.d.ts +57 -0
  152. package/dist/blocks/region/serialize.js +112 -0
  153. package/dist/blocks/region/types.d.ts +255 -0
  154. package/dist/blocks/region/types.js +204 -0
  155. package/dist/blocks/selection.svelte.d.ts +106 -8
  156. package/dist/blocks/selection.svelte.js +622 -74
  157. package/dist/blocks/subthread.d.ts +80 -0
  158. package/dist/blocks/subthread.js +167 -0
  159. package/dist/blocks/text-block.svelte.d.ts +34 -13
  160. package/dist/blocks/text-block.svelte.js +42 -8
  161. package/dist/blocks/toggle/Toggle.svelte +4 -15
  162. package/dist/blocks/toggle/markdown.d.ts +20 -0
  163. package/dist/blocks/toggle/markdown.js +112 -0
  164. package/dist/blocks/toggle/toggle.extension.d.ts +4 -1
  165. package/dist/blocks/toggle/toggle.extension.js +6 -0
  166. package/dist/blocks/toggle/toggle.svelte.d.ts +54 -7
  167. package/dist/blocks/toggle/toggle.svelte.js +134 -34
  168. package/dist/blocks/undo-selection.d.ts +175 -0
  169. package/dist/blocks/undo-selection.js +360 -0
  170. package/dist/blocks/video/Video.svelte +5 -2
  171. package/dist/blocks/video/video.extension.d.ts +4 -1
  172. package/dist/blocks/video/video.extension.js +9 -1
  173. package/dist/blocks/video/video.svelte.d.ts +6 -5
  174. package/dist/blocks/video/video.svelte.js +18 -9
  175. package/dist/extend.d.ts +55 -0
  176. package/dist/extend.js +88 -0
  177. package/dist/files/assets.clipboard.d.ts +77 -0
  178. package/dist/files/assets.clipboard.js +152 -0
  179. package/dist/files/files.svelte.d.ts +321 -19
  180. package/dist/files/files.svelte.js +847 -43
  181. package/dist/files/index.d.ts +3 -1
  182. package/dist/files/index.js +3 -1
  183. package/dist/files/outbox.d.ts +69 -0
  184. package/dist/files/outbox.js +48 -0
  185. package/dist/files/status.d.ts +65 -0
  186. package/dist/files/status.js +87 -0
  187. package/dist/files/store.d.ts +97 -0
  188. package/dist/files/store.js +65 -0
  189. package/dist/files/uploads.svelte.d.ts +174 -0
  190. package/dist/files/uploads.svelte.js +504 -0
  191. package/dist/index.d.ts +68 -9
  192. package/dist/index.js +117 -10
  193. package/dist/nabu-theme.css +21 -1
  194. package/dist/paste/html.paste.d.ts +74 -3
  195. package/dist/paste/html.paste.js +550 -86
  196. package/dist/paste/markdown-inline.d.ts +86 -0
  197. package/dist/paste/markdown-inline.js +528 -0
  198. package/dist/paste/markdown.paste.d.ts +2 -8
  199. package/dist/paste/markdown.paste.js +78 -94
  200. package/dist/paste/nabu.paste.d.ts +3 -0
  201. package/dist/paste/nabu.paste.js +29 -3
  202. package/dist/paste/plain-text.paste.d.ts +2 -2
  203. package/dist/paste/plain-text.paste.js +3 -3
  204. package/dist/persistence/asset-store.d.ts +51 -0
  205. package/dist/persistence/asset-store.js +103 -0
  206. package/dist/persistence/autosave.d.ts +53 -0
  207. package/dist/persistence/autosave.js +62 -0
  208. package/dist/persistence/compact.d.ts +162 -0
  209. package/dist/persistence/compact.js +141 -0
  210. package/dist/persistence/history.d.ts +77 -0
  211. package/dist/persistence/history.js +176 -0
  212. package/dist/persistence/indexeddb.d.ts +190 -4
  213. package/dist/persistence/indexeddb.js +333 -29
  214. package/dist/persistence/merge.d.ts +38 -0
  215. package/dist/persistence/merge.js +36 -0
  216. package/dist/persistence/outbox.d.ts +25 -0
  217. package/dist/persistence/outbox.js +40 -0
  218. package/dist/persistence/persistence.d.ts +70 -9
  219. package/dist/persistence/persistence.js +507 -35
  220. package/dist/persistence/recovery.d.ts +79 -0
  221. package/dist/persistence/recovery.js +79 -0
  222. package/dist/persistence/release.d.ts +34 -0
  223. package/dist/persistence/release.js +64 -0
  224. package/dist/persistence/state.svelte.d.ts +53 -0
  225. package/dist/persistence/state.svelte.js +48 -0
  226. package/dist/persistence/sync.d.ts +39 -0
  227. package/dist/persistence/sync.js +138 -0
  228. package/dist/persistence/tail.d.ts +42 -0
  229. package/dist/persistence/tail.js +103 -0
  230. package/dist/presets.d.ts +29 -26
  231. package/dist/presets.js +35 -13
  232. package/dist/themes/atelier.css +318 -0
  233. package/dist/ui/CommandChooser.svelte +153 -0
  234. package/dist/ui/CommandChooser.svelte.d.ts +13 -0
  235. package/dist/ui/CommandInvite.svelte +99 -0
  236. package/dist/ui/CommandInvite.svelte.d.ts +12 -0
  237. package/dist/ui/DragHandle.svelte +97 -21
  238. package/dist/ui/FloatingToolbar.svelte +75 -27
  239. package/dist/ui/FloatingToolbar.svelte.d.ts +10 -0
  240. package/dist/ui/SlashMenu.svelte +147 -15
  241. package/dist/ui/SlashMenu.svelte.d.ts +20 -0
  242. package/dist/ui/anchor.d.ts +112 -0
  243. package/dist/ui/anchor.js +136 -0
  244. package/dist/ui/index.d.ts +7 -1
  245. package/dist/ui/index.js +12 -1
  246. package/dist/ui/slash-menu.svelte.d.ts +79 -7
  247. package/dist/ui/slash-menu.svelte.js +85 -51
  248. package/dist/utils/command-confirm.svelte.d.ts +349 -0
  249. package/dist/utils/command-confirm.svelte.js +673 -0
  250. package/dist/utils/commands.d.ts +328 -0
  251. package/dist/utils/commands.js +416 -0
  252. package/dist/utils/dom-scope.d.ts +14 -0
  253. package/dist/utils/dom-scope.js +20 -0
  254. package/dist/utils/drop.d.ts +160 -0
  255. package/dist/utils/drop.js +305 -0
  256. package/dist/utils/extensions.d.ts +439 -45
  257. package/dist/utils/extensions.js +271 -29
  258. package/dist/utils/html.d.ts +44 -0
  259. package/dist/utils/html.js +183 -0
  260. package/dist/utils/index.d.ts +3 -0
  261. package/dist/utils/index.js +3 -0
  262. package/dist/utils/keys.d.ts +120 -0
  263. package/dist/utils/keys.js +248 -0
  264. package/dist/utils/links.d.ts +22 -0
  265. package/dist/utils/links.js +34 -0
  266. package/dist/utils/placeholders.d.ts +81 -3
  267. package/dist/utils/placeholders.js +73 -3
  268. package/dist/utils/selection.svelte.d.ts +47 -19
  269. package/dist/utils/selection.svelte.js +60 -10
  270. package/dist/utils/spellcheck.d.ts +9 -0
  271. package/dist/utils/spellcheck.js +17 -0
  272. package/dist/utils/tab.d.ts +31 -0
  273. package/dist/utils/tab.js +117 -0
  274. package/dist/utils/words.d.ts +7 -0
  275. package/dist/utils/words.js +62 -0
  276. package/dist/vite.d.ts +9 -3
  277. package/dist/vite.js +19 -8
  278. package/package.json +54 -20
  279. package/dist/blocks/variable.svelte.d.ts +0 -47
  280. package/dist/blocks/variable.svelte.js +0 -51
  281. package/dist/blocks/zone/TemplateEditor.svelte +0 -308
  282. package/dist/blocks/zone/TemplateEditor.svelte.d.ts +0 -11
  283. package/dist/blocks/zone/Zone.svelte +0 -272
  284. package/dist/blocks/zone/Zone.svelte.d.ts +0 -11
  285. package/dist/blocks/zone/ZoneEditor.svelte +0 -289
  286. package/dist/blocks/zone/ZoneEditor.svelte.d.ts +0 -11
  287. package/dist/blocks/zone/ZoneTemplatesList.svelte +0 -188
  288. package/dist/blocks/zone/ZoneTemplatesList.svelte.d.ts +0 -11
  289. package/dist/blocks/zone/hooks/onBeforeInput.hook.d.ts +0 -3
  290. package/dist/blocks/zone/hooks/onBeforeInput.hook.js +0 -30
  291. package/dist/blocks/zone/index.d.ts +0 -2
  292. package/dist/blocks/zone/index.js +0 -2
  293. package/dist/blocks/zone/zone.extension.d.ts +0 -2
  294. package/dist/blocks/zone/zone.extension.js +0 -237
  295. package/dist/blocks/zone/zone.svelte.d.ts +0 -221
  296. package/dist/blocks/zone/zone.svelte.js +0 -608
  297. package/dist/overlays/context/Context.svelte +0 -182
  298. package/dist/overlays/context/Context.svelte.d.ts +0 -17
  299. package/dist/overlays/context/collect.d.ts +0 -17
  300. package/dist/overlays/context/collect.js +0 -33
  301. package/dist/overlays/context/context.extension.d.ts +0 -2
  302. package/dist/overlays/context/context.extension.js +0 -8
  303. package/dist/overlays/context/context.svelte.d.ts +0 -82
  304. package/dist/overlays/context/context.svelte.js +0 -159
  305. package/dist/overlays/context/index.d.ts +0 -3
  306. package/dist/overlays/context/index.js +0 -3
@@ -1,17 +1,36 @@
1
1
  import { MegaBlock } from "../megablock.svelte.js";
2
2
  import { ListBehavior } from "./list.behavior.svelte.js";
3
3
  import ListComponent from "./List.svelte";
4
+ import { stepBack } from "./step-back.js";
5
+ import { hasLeveledItems, docsEntries, docsSiblings, isWordListParagraph, wordEntries, wordSiblings, nestEntries } from "./html-flat.js";
6
+ import { isListItem, interruptsParagraph, listLength, parseListLines, renderList } from "./markdown.js";
7
+ import { deltaToMarkdown } from "../../behaviors/text";
4
8
 
5
9
  /**
6
10
  * @import { Nabu, NabuNode } from "../nabu.svelte.js";
7
11
  */
8
12
 
9
13
  /**
10
- * @typedef {NabuNode<{type: "list", listType: "bullet" | "ordered" | "todo"}>} ListNode
14
+ * @typedef {NabuNode<{type: "list", listType: "bullet" | "ordered" | "todo", start?: number}>} ListNode
11
15
  */
12
16
 
17
+ /**
18
+ * Normalize a list's `start` number: only meaningful for ORDERED lists, and `1` is the default.
19
+ * @param {string | undefined} listType
20
+ * @param {any} start a number, a numeric string (markdown / `<ol start>`), or nothing
21
+ * @returns {number | null} the number to store / emit, or null = default (omit it)
22
+ */
23
+ export function listStart(listType, start) {
24
+ if (listType !== 'ordered' || start === undefined || start === null || start === '') return null;
25
+ const n = Number(start);
26
+ return Number.isInteger(n) && n >= 0 && n !== 1 ? n : null;
27
+ }
28
+
13
29
 
14
30
 
31
+ /** What the list renderers (markdown.js) need from outside: the inline serializer and the `start` normalizer. */
32
+ const markdownEnv = { toMarkdown: (/** @type {any[]} */ delta) => deltaToMarkdown(delta), listStart };
33
+
15
34
  export class List extends MegaBlock {
16
35
  /** @param {Nabu} nabu @param {ListNode} node */
17
36
  constructor(nabu, node) {
@@ -20,23 +39,59 @@ export class List extends MegaBlock {
20
39
  this.behavior = new ListBehavior(this);
21
40
  this.behaviors.set("list", this.behavior);
22
41
 
23
- this.serializers.set('markdown', () =>
24
- this.children
25
- .map(child => child.serialize('markdown'))
26
- .filter(Boolean)
27
- .join('\n')
28
- );
42
+ // Rendered from the PasteBlock tree, like the clipboard: ONE implementation of the list markdown (`renderList`).
43
+ this.serializers.set('markdown', () => renderList(this.toPasteBlock(), markdownEnv, { before: this.siblingsBefore() }));
29
44
  this.serializers.set('json', () => ({
30
45
  id: this.id,
31
46
  type: 'list',
32
- props: { listType: this.listType },
47
+ props: this.clipboardProps,
33
48
  children: this.children.map(child => child.serialize('json')).filter(Boolean)
34
49
  }));
35
50
  }
36
51
 
52
+ /**
53
+ * This list, its items and their sublists as PasteBlocks (the shape the static serializers read).
54
+ * @returns {import('../../utils/extensions.js').PasteBlock}
55
+ */
56
+ toPasteBlock() {
57
+ return {
58
+ type: 'list',
59
+ partial: false,
60
+ props: this.clipboardProps,
61
+ children: this.children.map(item => /** @type {any} */ (item).toPasteBlock()),
62
+ };
63
+ }
64
+
65
+ /**
66
+ * The siblings before this list (the lists right before it decide its marker, see `adjacentRun`).
67
+ * @returns {Array<{ type: string, props: { listType?: string } }>}
68
+ */
69
+ siblingsBefore() {
70
+ const siblings = /** @type {any[]} */ (this.parent?.children ?? this.nabu.children);
71
+ const at = siblings.indexOf(this);
72
+ return siblings.slice(0, at < 0 ? 0 : at).map(b => ({ type: b.type, props: { listType: b.listType } }));
73
+ }
74
+
37
75
  listType = $derived(this.behavior.listType);
38
76
 
39
- get clipboardProps() { return { listType: this.listType }; }
77
+ /** First number of an ORDERED list (`<ol start>`); null = 1, the default. @type {number | null} */
78
+ start = $derived(listStart(this.behavior.listType, this.behavior.start));
79
+
80
+ /** `start` is only emitted when it is not the default. */
81
+ get clipboardProps() {
82
+ return this.start === null ? { listType: this.listType } : { listType: this.listType, start: this.start };
83
+ }
84
+
85
+ /**
86
+ * Copying a slice that starts at item `firstChildIndex` of an ORDERED list: the copy is numbered from that item's
87
+ * visual number (`start + index`), not from the original list start.
88
+ * @param {number} firstChildIndex
89
+ */
90
+ clipboardPropsFrom(firstChildIndex) {
91
+ if (this.listType !== 'ordered' || !firstChildIndex) return this.clipboardProps;
92
+ const start = listStart('ordered', (this.start ?? 1) + firstChildIndex);
93
+ return start === null ? { listType: this.listType } : { listType: this.listType, start };
94
+ }
40
95
 
41
96
  component = $derived(this.nabu.components.get("list") || ListComponent);
42
97
 
@@ -55,39 +110,11 @@ export class List extends MegaBlock {
55
110
  onSplit(event, data) {
56
111
  const { from: sourceItem, offset, delta } = data;
57
112
 
58
- // --- CAS 1 : L'item est vide (Demande de sortie de liste) ---
113
+ // --- CAS 1 : empty item = step back one level (nested: unindent; top-level: leave the list as a paragraph) ---
59
114
  if (sourceItem.text.length === 0) {
60
- const currentIndex = sourceItem.node.index();
61
- const siblings = this.node.children();
62
- const followers = siblings.slice(currentIndex + 1);
63
-
64
- // 1. On identifie le point d'insertion (juste après la liste actuelle)
65
- const parentNode = this.node.parent();
66
- const grandParentId = parentNode?.id.toString() || null;
67
- const myIndexInGrandParent = this.node.index();
68
-
69
- // 2. On insère le paragraphe de sortie juste après cette liste
70
- const newParagraph = this.nabu.insert("paragraph", {}, grandParentId, myIndexInGrandParent + 1);
71
-
72
- // 3. Si on était au milieu de la liste, on crée une nouvelle liste après le paragraphe pour les "followers"
73
- if (followers.length > 0) {
74
- const newList = this.nabu.insert("list", { listType: this.listType }, grandParentId, myIndexInGrandParent + 2);
75
- for (const follower of followers) {
76
- this.nabu.tree.move(follower.id.toString(), newList.node.id.toString());
77
- }
78
- }
79
-
80
- // 4. On détruit l'item vide actuel
81
- sourceItem.destroy();
82
-
83
- // 5. Si la liste d'origine est devenue vide, on la supprime aussi
84
- if (currentIndex === 0 && followers.length === 0) {
85
- this.destroy();
86
- }
87
-
88
- this.nabu.commit().then(() => this.nabu.selection.setCursor(newParagraph, 0));
89
-
90
- return { block: newParagraph };
115
+ // Same node in both cases (a top-level item is retyped), so the returned block stays valid.
116
+ stepBack(this.nabu, /** @type {import('../nabu.svelte.js').NabuNode} */ (sourceItem.node));
117
+ return { block: sourceItem };
91
118
  }
92
119
 
93
120
  // --- CAS 2 : Comportement normal (Créer un nouvel item en dessous) ---
@@ -104,7 +131,7 @@ export class List extends MegaBlock {
104
131
  "list-item",
105
132
  { delta },
106
133
  this.node.id.toString(), // Le parent est la liste actuelle
107
- currentIndex + 1
134
+ /** @type {number} */ (currentIndex) + 1
108
135
  );
109
136
 
110
137
  // 3.1. On transfère TOUS les enfants du sourceItem vers le newItem
@@ -122,14 +149,12 @@ export class List extends MegaBlock {
122
149
  }
123
150
 
124
151
  /**
152
+ * Markdown of a list and its sublists. Adjacent lists alternate their marker (`-` / `*`, `.` / `)`), see `adjacentRun`.
125
153
  * @param {import('../../utils/extensions.js').PasteBlock} pb
126
- * @param {{ recurse: (child: import('../../utils/extensions.js').PasteBlock, ctx?: object) => string, depth?: number }} helpers
154
+ * @param {{ indent?: string, before?: import('../../utils/extensions.js').PasteBlock[] }} helpers `before`: the siblings before the list
127
155
  */
128
- static toMarkdown(pb, { recurse, depth = 0 }) {
129
- const listType = pb.props?.listType ?? 'bullet';
130
- return (pb.children || [])
131
- .map((child, i) => recurse(child, { listType, index: i, depth }))
132
- .join('\n');
156
+ static toMarkdown(pb, { indent = '', before = [] }) {
157
+ return renderList(pb, markdownEnv, { indent, before });
133
158
  }
134
159
 
135
160
  /**
@@ -139,104 +164,80 @@ export class List extends MegaBlock {
139
164
  static toHtml(pb, { recurse }) {
140
165
  const tag = pb.props?.listType === 'ordered' ? 'ol' : 'ul';
141
166
  const cls = pb.props?.listType === 'todo' ? ' class="contains-task-list"' : '';
142
- return `<${tag}${cls}>${(pb.children || []).map(recurse).join('')}</${tag}>`;
167
+ const start = listStart(pb.props?.listType, pb.props?.start);
168
+ return `<${tag}${cls}${start === null ? '' : ` start="${start}"`}>${(pb.children || []).map(recurse).join('')}</${tag}>`;
143
169
  }
144
170
 
171
+ /**
172
+ * Markdown lists, see markdown.js: markers `-` `*` `+` `1.` `1)`, nesting by indentation (2 / 4 spaces, TAB), tasks,
173
+ * loose lists, continuation paragraphs of an item.
174
+ */
145
175
  static markdownRules = [
146
176
  {
147
177
  priority: 10,
148
- detect: /^[ \t]*([-*+]|\d+[.)]) /,
149
- consume(/** @type {string[]} */ lines, /** @type {number} */ i) {
150
- const LIST_RE = /^[ \t]*([-*+]|\d+[.)]) /;
151
- let j = i;
152
- while (j < lines.length && lines[j].trim() && LIST_RE.test(lines[j])) j++;
153
- return Math.max(1, j - i);
154
- },
178
+ interruptsParagraph: (/** @type {string} */ line) => interruptsParagraph(line),
179
+ detect: (/** @type {string} */ line) => isListItem(line),
180
+ consume: (/** @type {string[]} */ lines, /** @type {number} */ i, /** @type {any} */ helpers) => listLength(lines, i, helpers),
155
181
  }
156
182
  ];
157
183
 
158
184
  /**
159
- * Parse markdown list lines into a nested List/ListItem PasteBlock tree.
160
- * Uses indent depth to reconstruct nesting recursively.
185
+ * Parse markdown list lines into a nested List/ListItem PasteBlock tree (an array when the marker type changes at
186
+ * the root: several lists).
161
187
  *
162
188
  * @param {string[]} lines
163
- * @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[] }} helpers
164
- * @returns {import('../../utils/extensions.js').PasteBlock | null}
189
+ * @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[], joinLines?: boolean, softBreak?: string }} helpers
190
+ * @returns {import('../../utils/extensions.js').PasteBlock | import('../../utils/extensions.js').PasteBlock[] | null}
165
191
  */
166
- static fromMarkdown(lines, { parseInline }) {
167
- /** @typedef {{ depth: number, ordered: boolean, task: boolean, checked: boolean, text: string }} ListToken */
168
-
169
- /** @type {ListToken[]} */
170
- const tokens = lines.flatMap(line => {
171
- const m = line.match(/^([ \t]*)([-*+]|\d+[.)]) +(.*)/);
172
- if (!m) return [];
173
- const rawText = m[3];
174
- const taskMatch = rawText.match(/^\[( |x|X)?\]\s*(.*)/);
175
- if (taskMatch) {
176
- const inner = taskMatch[1] ?? '';
177
- const rest = taskMatch[2];
178
- return [{ depth: m[1].length, ordered: /^\d/.test(m[2]), task: true, checked: /[xX]/.test(inner), text: rest }];
179
- }
180
- return [{ depth: m[1].length, ordered: /^\d/.test(m[2]), task: false, checked: false, text: rawText }];
181
- });
182
-
183
- if (!tokens.length) return null;
184
-
185
- /**
186
- * Recursively build a List PasteBlock from a flat token array.
187
- * Items at `baseDepth` become direct children; deeper tokens become sublists.
188
- * @param {ListToken[]} toks
189
- * @returns {import('../../utils/extensions.js').PasteBlock}
190
- */
191
- function buildList(toks) {
192
- const baseDepth = Math.min(...toks.map(t => t.depth));
193
- const baseItems = toks.filter(t => t.depth === baseDepth);
194
- // Task takes precedence: if ANY base-depth item is a task, list is todo
195
- const hasTask = baseItems.some(t => t.task);
196
- const listType = hasTask ? 'todo' : (baseItems.find(t => t.depth === baseDepth)?.ordered ? 'ordered' : 'bullet');
197
- /** @type {import('../../utils/extensions.js').PasteBlock[]} */
198
- const items = [];
199
- let i = 0;
200
-
201
- while (i < toks.length) {
202
- if (toks[i].depth !== baseDepth) { i++; continue; }
203
-
204
- // Collect all sub-tokens directly following this item (deeper indent)
205
- const subToks = /** @type {ListToken[]} */ ([]);
206
- let j = i + 1;
207
- while (j < toks.length && toks[j].depth > baseDepth) {
208
- subToks.push(toks[j]);
209
- j++;
210
- }
211
-
212
- /** @type {import('../../utils/extensions.js').PasteBlock} */
213
- const item = { type: 'list-item', delta: parseInline(toks[i].text), partial: false };
214
- if (toks[i].task) item.props = { checked: toks[i].checked };
215
- if (subToks.length) item.children = [buildList(subToks)];
216
-
217
- items.push(item);
218
- i = j;
219
- }
220
-
221
- return { type: 'list', props: { listType }, children: items, partial: false };
222
- }
223
-
224
- return buildList(tokens);
192
+ static fromMarkdown(lines, helpers) {
193
+ return parseListLines(lines, helpers, listStart);
225
194
  }
226
195
 
227
196
  static htmlRules = [
228
197
  { selector: 'ul.contains-task-list', props: { listType: 'todo' }, priority: 10 },
198
+ // Word writes a list as consecutive `<p style="mso-list:l0 level2 lfo1">` (list identity + level), see html-flat.js.
199
+ { selector: 'p', filter: isWordListParagraph, props: { listType: 'bullet' }, priority: 20 },
229
200
  { selector: 'ul', props: { listType: 'bullet' } },
230
201
  { selector: 'ol', props: { listType: 'ordered' } },
231
202
  ];
232
203
 
233
204
  /**
205
+ * The elements after `el` that continue its list (the `html` interpreter reads them as part of this list): the `<ul>`
206
+ * per nesting level of Google Docs, the following list paragraphs of Word.
234
207
  * @param {Element} el
235
- * @param {{ parseChildren: (el: Element) => import('../../utils/extensions.js').PasteBlock[] }} helpers
208
+ * @returns {Element[]}
209
+ */
210
+ static htmlSiblings(el) {
211
+ return el.localName === 'p' ? wordSiblings(el) : docsSiblings(el);
212
+ }
213
+
214
+ /**
215
+ * @param {Element} el
216
+ * @param {{ parseChildren: (el: Element) => import('../../utils/extensions.js').PasteBlock[], parseBlock: (el: Element) => import('../../utils/extensions.js').PasteBlock | null, parseInline: (el: Element) => import('loro-crdt').Delta<string>[] }} helpers
236
217
  * @param {{ props?: { listType?: string } }} [rule]
218
+ * @param {Element[]} [siblings] what `htmlSiblings` absorbed
237
219
  * @returns {import('../../utils/extensions.js').PasteBlock | null}
238
220
  */
239
- static fromHTML(el, { parseChildren }, rule) {
221
+ static fromHTML(el, helpers, rule, siblings = []) {
222
+ const { parseChildren } = helpers;
223
+ /** @param {string} listType @param {number | null} start */
224
+ const propsOf = (listType, start) => { const n = listStart(listType, start); return n === null ? { listType } : { listType, start: n }; };
225
+ // Word: a run of list paragraphs; Docs: leveled items. Either way the levels are rebuilt as real nested lists.
226
+ if (el.localName === 'p') {
227
+ const raw = wordEntries([el, ...siblings]);
228
+ return nestEntries(raw.flatMap((e) => {
229
+ const delta = helpers.parseInline(e.el);
230
+ return delta.some((op) => typeof op.insert === 'string' && /\S/.test(op.insert))
231
+ ? [{ ...e, item: /** @type {import('../../utils/extensions.js').PasteBlock} */ ({ type: 'list-item', delta, partial: false }) }] : [];
232
+ }), propsOf);
233
+ }
234
+ if (siblings.length || hasLeveledItems(el)) {
235
+ const raw = [el, ...siblings].flatMap((list) => docsEntries(list));
236
+ return nestEntries(raw.flatMap((e) => {
237
+ const item = helpers.parseBlock(e.el);
238
+ return item ? [{ ...e, item }] : [];
239
+ }), propsOf);
240
+ }
240
241
  const children = parseChildren(el);
241
242
  if (!children.length) return null;
242
243
  // Determine listType: prefer rule, but promote to 'todo' if any child has checked prop
@@ -244,15 +245,18 @@ export class List extends MegaBlock {
244
245
  if (listType !== 'todo' && children.some(c => c.props?.checked !== undefined)) {
245
246
  listType = 'todo';
246
247
  }
247
- return { type: 'list', props: { listType }, children, partial: false };
248
+ const start = listStart(listType, el.getAttribute?.('start'));
249
+ return { type: 'list', props: start === null ? { listType } : { listType, start }, children, partial: false };
248
250
  }
249
251
 
250
- /** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
252
+ /** @param {Nabu} nabu @param {string} type @param {Record<string, any>} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
251
253
  static create(nabu, type, props = {}, parentId = null, index = null) {
252
- const node = nabu.tree.createNode(parentId || undefined, index != null ? index : undefined);
254
+ const node = nabu.tree.createNode(/** @type {import('loro-crdt').TreeID | undefined} */ (parentId || undefined), index ?? undefined);
253
255
  node.data.set("type", "list");
254
256
  node.data.set("listType", props.listType || "bullet");
255
- const block = new List(nabu, node);
257
+ const start = listStart(props.listType, /** @type {any} */ (props).start);
258
+ if (start !== null) /** @type {any} */ (node.data).set("start", start);
259
+ const block = new List(nabu, /** @type {ListNode} */ (node));
256
260
  return block;
257
261
  }
258
262
  }
@@ -0,0 +1,149 @@
1
+ /** @param {string} line */
2
+ export function isListItem(line: string): boolean;
3
+ /**
4
+ * Whether a list may start in the middle of a paragraph (CommonMark): a bullet with text, or an ordered list that starts at 1.
5
+ * @param {string} line
6
+ */
7
+ export function interruptsParagraph(line: string): boolean;
8
+ /**
9
+ * Number of lines of the list that starts at line `i`.
10
+ * @param {string[]} lines
11
+ * @param {number} i
12
+ * @param {{ joinLines?: boolean, interrupts?: (lines: string[], k: number) => boolean }} [helpers]
13
+ * @returns {number}
14
+ */
15
+ export function listLength(lines: string[], i: number, helpers?: {
16
+ joinLines?: boolean;
17
+ interrupts?: (lines: string[], k: number) => boolean;
18
+ }): number;
19
+ /**
20
+ * @typedef {{ raw: boolean, s: string }} Chunk
21
+ * @typedef {{ indent: number, key: string, ordered: boolean, num: number, task: boolean, checked: boolean, chunks: Chunk[], previous: string, children: Item[] }} Item
22
+ */
23
+ /**
24
+ * Parse the lines of a list (see {@link listLength}) into List PasteBlocks: one, or several when the marker type changes at
25
+ * the root.
26
+ * @param {string[]} lines
27
+ * @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[], joinLines?: boolean, softBreak?: string }} helpers
28
+ * @param {(listType: string, start: unknown) => number | null} listStart normalises the `start` of an ordered list
29
+ * @returns {PasteBlock | PasteBlock[] | null}
30
+ */
31
+ export function parseListLines(lines: string[], { parseInline, joinLines, softBreak }: {
32
+ parseInline: (text: string) => import("loro-crdt").Delta<string>[];
33
+ joinLines?: boolean;
34
+ softBreak?: string;
35
+ }, listStart: (listType: string, start: unknown) => number | null): PasteBlock | PasteBlock[] | null;
36
+ /**
37
+ * Number of lists of the same family directly before a list: the position of the list in a run of ADJACENT lists, which
38
+ * picks its marker (`-` then `*` then `-`…, `.` then `)`). CommonMark ends a list at a change of marker, so this is what
39
+ * keeps two neighbours (a to-do list then a bullet list, two numbered lists…) from reading back as ONE list. A block
40
+ * that is not a list, or a list of the other family, ends the run (the marker type already differs).
41
+ * @param {string | undefined} listType
42
+ * @param {Array<{ type: string, props?: { listType?: string } }>} before the siblings before the list, in order
43
+ * @returns {number}
44
+ */
45
+ export function adjacentRun(listType: string | undefined, before: Array<{
46
+ type: string;
47
+ props?: {
48
+ listType?: string;
49
+ };
50
+ }>): number;
51
+ /**
52
+ * Columns taken by the marker of an item plus its space: the indentation of what belongs to the item (continuation
53
+ * lines, sublists) so that any CommonMark reader nests them under it. A to-do item counts the bullet only.
54
+ * @param {string | undefined} listType
55
+ * @param {number} number the number of the item (ordered lists)
56
+ * @returns {number}
57
+ */
58
+ export function markerWidth(listType: string | undefined, number: number): number;
59
+ /**
60
+ * The text of an item as markdown. An item holds text, not blocks, so a fenced code written in it (typed, or imported from a
61
+ * list) is TEXT with line breaks. Escaped like any text it would be unreadable (`\`\`\`js\`), so a well-formed fence (an
62
+ * opener at the start of a line of the text, not the first, and its closer) is written back as an indented fence under the
63
+ * item, which the list parser reads as the same text, verbatim (marks inside the fence are not exported). Any other
64
+ * text is the inline markdown of its delta.
65
+ * @param {Array<{ insert?: unknown, attributes?: Record<string, any> }>} delta
66
+ * @param {string} pad indentation of what belongs to the item
67
+ * @param {(delta: any[]) => string} toMarkdown inline serializer
68
+ * @returns {string} the first line (after the marker) and the following ones, already indented
69
+ */
70
+ export function itemMarkdown(delta: Array<{
71
+ insert?: unknown;
72
+ attributes?: Record<string, any>;
73
+ }>, pad: string, toMarkdown: (delta: any[]) => string): string;
74
+ /**
75
+ * Environment of the renderers below: the inline serializer and the normalizer of the `start` of an ordered list (both
76
+ * live outside this pure module).
77
+ * @typedef {{ toMarkdown: (delta: any[]) => string, listStart: (listType: string, start: unknown) => number | null }} RenderEnv
78
+ */
79
+ /**
80
+ * One list item (with its sublists) as markdown.
81
+ * - marker: `-` / `*` (alternated, see {@link adjacentRun}), `- [ ]` / `- [x]` for a to-do item, `1.` / `1)` (the number
82
+ * is `start + index`);
83
+ * - everything that belongs to the item (continuation lines, sublists) is indented by the width of the marker;
84
+ * - its sublists follow, in order (an item may hold several, of different types: P-28).
85
+ * @param {{ type?: string, delta?: any[], props?: { checked?: boolean }, children?: any[] }} pb
86
+ * @param {RenderEnv} env
87
+ * @param {{ listType?: string, index?: number, start?: number, run?: number, indent?: string }} [ctx]
88
+ * @returns {string}
89
+ */
90
+ export function renderItem(pb: {
91
+ type?: string;
92
+ delta?: any[];
93
+ props?: {
94
+ checked?: boolean;
95
+ };
96
+ children?: any[];
97
+ }, env: RenderEnv, { listType, index, start, run, indent }?: {
98
+ listType?: string;
99
+ index?: number;
100
+ start?: number;
101
+ run?: number;
102
+ indent?: string;
103
+ }): string;
104
+ /**
105
+ * A list (its items and, recursively, their sublists) as markdown.
106
+ * @param {{ props?: { listType?: string, start?: unknown }, children?: any[] }} pb
107
+ * @param {RenderEnv} env
108
+ * @param {{ indent?: string, before?: Array<{ type: string, props?: { listType?: string } }> }} [ctx] `before`: the siblings before the list
109
+ * @returns {string}
110
+ */
111
+ export function renderList(pb: {
112
+ props?: {
113
+ listType?: string;
114
+ start?: unknown;
115
+ };
116
+ children?: any[];
117
+ }, env: RenderEnv, { indent, before }?: {
118
+ indent?: string;
119
+ before?: Array<{
120
+ type: string;
121
+ props?: {
122
+ listType?: string;
123
+ };
124
+ }>;
125
+ }): string;
126
+ export type Chunk = {
127
+ raw: boolean;
128
+ s: string;
129
+ };
130
+ export type Item = {
131
+ indent: number;
132
+ key: string;
133
+ ordered: boolean;
134
+ num: number;
135
+ task: boolean;
136
+ checked: boolean;
137
+ chunks: Chunk[];
138
+ previous: string;
139
+ children: Item[];
140
+ };
141
+ /**
142
+ * Environment of the renderers below: the inline serializer and the normalizer of the `start` of an ordered list (both
143
+ * live outside this pure module).
144
+ */
145
+ export type RenderEnv = {
146
+ toMarkdown: (delta: any[]) => string;
147
+ listStart: (listType: string, start: unknown) => number | null;
148
+ };
149
+ import type { PasteBlock } from "../../utils/extensions.js";