@aionbuilders/nabu 0.1.0-beta.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (308) hide show
  1. package/CHANGELOG.md +531 -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 +195 -55
  12. package/dist/behaviors/text/text.behavior.svelte.js +531 -103
  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 +32 -18
  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 +1753 -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 +35 -14
  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 +55 -8
  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/composition.d.ts +28 -0
  253. package/dist/utils/composition.js +51 -0
  254. package/dist/utils/dom-scope.d.ts +14 -0
  255. package/dist/utils/dom-scope.js +20 -0
  256. package/dist/utils/drop.d.ts +160 -0
  257. package/dist/utils/drop.js +305 -0
  258. package/dist/utils/extensions.d.ts +439 -45
  259. package/dist/utils/extensions.js +271 -29
  260. package/dist/utils/html.d.ts +44 -0
  261. package/dist/utils/html.js +183 -0
  262. package/dist/utils/index.d.ts +3 -0
  263. package/dist/utils/index.js +3 -0
  264. package/dist/utils/keys.d.ts +120 -0
  265. package/dist/utils/keys.js +248 -0
  266. package/dist/utils/links.d.ts +22 -0
  267. package/dist/utils/links.js +34 -0
  268. package/dist/utils/placeholders.d.ts +81 -3
  269. package/dist/utils/placeholders.js +73 -3
  270. package/dist/utils/selection.svelte.d.ts +48 -20
  271. package/dist/utils/selection.svelte.js +60 -10
  272. package/dist/utils/spellcheck.d.ts +9 -0
  273. package/dist/utils/spellcheck.js +17 -0
  274. package/dist/utils/tab.d.ts +31 -0
  275. package/dist/utils/tab.js +117 -0
  276. package/dist/utils/words.d.ts +42 -0
  277. package/dist/utils/words.js +147 -0
  278. package/dist/vite.d.ts +9 -3
  279. package/dist/vite.js +19 -8
  280. package/package.json +56 -20
  281. package/dist/blocks/variable.svelte.d.ts +0 -47
  282. package/dist/blocks/variable.svelte.js +0 -51
  283. package/dist/blocks/zone/TemplateEditor.svelte +0 -308
  284. package/dist/blocks/zone/TemplateEditor.svelte.d.ts +0 -11
  285. package/dist/blocks/zone/Zone.svelte +0 -272
  286. package/dist/blocks/zone/Zone.svelte.d.ts +0 -11
  287. package/dist/blocks/zone/ZoneEditor.svelte +0 -289
  288. package/dist/blocks/zone/ZoneEditor.svelte.d.ts +0 -11
  289. package/dist/blocks/zone/ZoneTemplatesList.svelte +0 -188
  290. package/dist/blocks/zone/ZoneTemplatesList.svelte.d.ts +0 -11
  291. package/dist/blocks/zone/hooks/onBeforeInput.hook.d.ts +0 -3
  292. package/dist/blocks/zone/hooks/onBeforeInput.hook.js +0 -30
  293. package/dist/blocks/zone/index.d.ts +0 -2
  294. package/dist/blocks/zone/index.js +0 -2
  295. package/dist/blocks/zone/zone.extension.d.ts +0 -2
  296. package/dist/blocks/zone/zone.extension.js +0 -237
  297. package/dist/blocks/zone/zone.svelte.d.ts +0 -221
  298. package/dist/blocks/zone/zone.svelte.js +0 -608
  299. package/dist/overlays/context/Context.svelte +0 -182
  300. package/dist/overlays/context/Context.svelte.d.ts +0 -17
  301. package/dist/overlays/context/collect.d.ts +0 -17
  302. package/dist/overlays/context/collect.js +0 -33
  303. package/dist/overlays/context/context.extension.d.ts +0 -2
  304. package/dist/overlays/context/context.extension.js +0 -8
  305. package/dist/overlays/context/context.svelte.d.ts +0 -82
  306. package/dist/overlays/context/context.svelte.js +0 -159
  307. package/dist/overlays/context/index.d.ts +0 -3
  308. package/dist/overlays/context/index.js +0 -3
@@ -0,0 +1,205 @@
1
+ /**
2
+ * Indent / unindent of list items (Tab / Shift+Tab), for one item or for every item a selection touches.
3
+ *
4
+ * Everything works on Loro tree NODES, never on the Block instances: inside one batch of moves the
5
+ * Block state (`children`, `sublists`, `parent`) is stale until the commit, and the later moves of the
6
+ * batch depend on the earlier ones. All the moves belong to ONE transaction (one undo step); the
7
+ * selection is captured before and restored after.
8
+ *
9
+ * @import { Nabu, NabuNode } from "../nabu.svelte.js";
10
+ * @import { Block } from "../block.svelte.js";
11
+ */
12
+
13
+ import { List } from "./list.svelte.js";
14
+
15
+ /** @param {NabuNode | null | undefined} node */
16
+ export const typeOf = (node) => /** @type {any} */ (node?.data.get("type"));
17
+
18
+ /** @param {NabuNode} node @returns {NabuNode[]} */
19
+ export const kids = (node) => /** @type {NabuNode[]} */ (node.children() ?? []);
20
+
21
+ /** `listType` of a list node (`bullet` when unset). @param {NabuNode | null | undefined} list */
22
+ export const listTypeOf = (list) => /** @type {any} */ (list?.data)?.get("listType") || "bullet";
23
+
24
+ /**
25
+ * Sublists of an item, in order, read from the tree (not from the possibly stale Block). An item may hold
26
+ * SEVERAL, of different types (P-28): `a > [ul[s1], ol[s2], ul[s3]]`.
27
+ * @param {NabuNode} item
28
+ * @returns {NabuNode[]}
29
+ */
30
+ export const sublistsOf = (item) => kids(item).filter((child) => typeOf(child) === "list");
31
+
32
+ /**
33
+ * The sublist of `item` that receives a child of type `listType`: its LAST sublist when that one has the
34
+ * same type, otherwise a new sublist of that type appended after the existing ones. No `nabu.insert()`: it
35
+ * commits, and the normalizer would delete the still-empty list before the caller fills it.
36
+ * @param {Nabu} nabu
37
+ * @param {NabuNode} item
38
+ * @param {string} listType
39
+ * @returns {NabuNode}
40
+ */
41
+ function sublistForType(nabu, item, listType) {
42
+ const last = kids(item).at(-1);
43
+ if (last && typeOf(last) === "list" && listTypeOf(last) === listType) return last;
44
+ return List.create(nabu, "list", { listType }, item.id.toString()).node;
45
+ }
46
+
47
+ /**
48
+ * Deletes a list this batch just emptied. The normalizer would do it too, but it spares a list created by
49
+ * the same transaction (it may still be "under construction"), and a batch of moves creates then empties
50
+ * such lists (an item unindented, then its adopted follower unindented in turn).
51
+ * @param {Nabu} nabu
52
+ * @param {NabuNode | null | undefined} list
53
+ */
54
+ export function dropIfEmpty(nabu, list) {
55
+ if (list && typeOf(list) === "list" && !kids(list).length) nabu.tree.delete(list.id);
56
+ }
57
+
58
+ /**
59
+ * Indent one item: it goes under its previous sibling, at the end of that sibling's LAST sublist when that
60
+ * sublist has the same type as the item's current list, otherwise in a new sublist of that type appended
61
+ * after the sibling's sublists. Its whole subtree moves along (its own sublists stay attached to it). Moves
62
+ * only; no commit.
63
+ * @param {Nabu} nabu
64
+ * @param {NabuNode} item
65
+ * @returns {boolean} whether the item moved
66
+ */
67
+ export function indentNode(nabu, item) {
68
+ const index = item.index();
69
+ if (!index) return false;
70
+ const parentList = item.parent();
71
+ const previous = parentList && kids(parentList)[index - 1];
72
+ if (!previous || typeOf(previous) !== "list-item") return false;
73
+
74
+ nabu.tree.move(item.id, sublistForType(nabu, previous, listTypeOf(parentList)).id);
75
+ return true;
76
+ }
77
+
78
+ /**
79
+ * Unindent one item: it goes right after its parent item, and the siblings that followed it become its
80
+ * children, in a sublist of THEIR list type appended after the sublists it already has (its own subtree
81
+ * stays attached). To keep the document order, the sublists of the parent item that came after the item's
82
+ * list follow them. Moves only; no commit.
83
+ * @param {Nabu} nabu
84
+ * @param {NabuNode} item
85
+ * @returns {boolean} whether the item moved
86
+ */
87
+ export function unindentNode(nabu, item) {
88
+ const parentList = item.parent();
89
+ const parentItem = parentList?.parent();
90
+ if (!parentList || !parentItem || typeOf(parentItem) !== "list-item") return false;
91
+ const targetList = parentItem.parent();
92
+ if (!targetList) return false;
93
+
94
+ const followers = kids(parentList).slice((item.index() ?? 0) + 1);
95
+ const laterSublists = kids(parentItem).slice((parentList.index() ?? 0) + 1);
96
+ if (followers.length) {
97
+ const sublistId = sublistForType(nabu, item, listTypeOf(parentList)).id;
98
+ for (const follower of followers) nabu.tree.move(follower.id, sublistId);
99
+ }
100
+ for (const later of laterSublists) nabu.tree.move(later.id, item.id);
101
+ nabu.tree.move(item.id, targetList.id, (parentItem.index() ?? 0) + 1);
102
+ dropIfEmpty(nabu, parentList);
103
+ return true;
104
+ }
105
+
106
+ /**
107
+ * @typedef {{ block: Block, offset: number }} CaretPoint
108
+ * @typedef {{ anchor: CaretPoint, focus: CaretPoint }} SelectionSnapshot
109
+ */
110
+
111
+ /**
112
+ * Model position (block + text offset) of the live selection's anchor and focus, or null when one of
113
+ * them is not in a text block.
114
+ * @param {Nabu} nabu
115
+ * @returns {SelectionSnapshot | null}
116
+ */
117
+ export function captureSelection(nabu) {
118
+ const sel = nabu.selection;
119
+ /** @param {Node | null} node @param {number} offset @param {Block | null | undefined} block @returns {CaretPoint | null} */
120
+ const point = (node, offset, block) => {
121
+ const text = /** @type {any} */ (block?.behaviors?.get("text"));
122
+ if (!node || !block?.element || !text) return null;
123
+ return { block, offset: text.calculateOffset(node, offset, block.element) };
124
+ };
125
+ const anchor = point(sel.anchorNode, sel.anchorOffset, sel.anchorBlock);
126
+ const focus = point(sel.focusNode, sel.focusOffset, sel.focusBlock);
127
+ return anchor && focus ? { anchor, focus } : null;
128
+ }
129
+
130
+ /**
131
+ * Put the selection back after a re-render: same blocks (looked up again by id), same offsets, so a
132
+ * collapsed caret stays collapsed and a range stays a range.
133
+ * @param {Nabu} nabu
134
+ * @param {SelectionSnapshot | null} snapshot
135
+ * @returns {Promise<void>}
136
+ */
137
+ export async function restoreSelection(nabu, snapshot) {
138
+ if (!snapshot) return;
139
+ const anchor = nabu.blocks.get(snapshot.anchor.block.id);
140
+ const focus = nabu.blocks.get(snapshot.focus.block.id);
141
+ if (!anchor || !focus) return;
142
+ await nabu.focus({
143
+ from: { block: anchor, offset: snapshot.anchor.offset },
144
+ to: { block: focus, offset: snapshot.focus.offset },
145
+ });
146
+ }
147
+
148
+ /**
149
+ * Indent (`unindent = false`) or unindent every given list item, in document order, as ONE transaction.
150
+ * Items that cannot move (e.g. the first item of a list on indent) stay. The selection is restored.
151
+ * @param {Nabu} nabu
152
+ * @param {NabuNode[]} items list-item nodes, in document order
153
+ * @param {boolean} unindent
154
+ * @returns {Promise<boolean>} whether at least one item moved
155
+ */
156
+ export async function moveItems(nabu, items, unindent) {
157
+ const snapshot = captureSelection(nabu);
158
+ // Ancestors read BEFORE any move of the batch: an item whose original ancestor is selected AND moved
159
+ // goes along with it as part of the subtree, so it must not be moved a second time. (Later moves
160
+ // re-parent items: s2 adopted by an unindented s1 is still processed, it was no descendant of s1.)
161
+ const ancestors = items.map((item) => {
162
+ /** @type {string[]} */
163
+ const ids = [];
164
+ for (let a = item.parent(); a; a = a.parent()) ids.push(a.id.toString());
165
+ return ids;
166
+ });
167
+ /** @type {Set<string>} */
168
+ const moved = new Set();
169
+ items.forEach((item, i) => {
170
+ if (ancestors[i].some((id) => moved.has(id))) return;
171
+ if (unindent ? unindentNode(nabu, item) : indentNode(nabu, item)) moved.add(item.id.toString());
172
+ });
173
+ if (!moved.size) return false;
174
+ await nabu.commit();
175
+ await restoreSelection(nabu, snapshot);
176
+ return true;
177
+ }
178
+
179
+ /**
180
+ * The list items a selection touches, in document order: the anchor block alone when the selection is
181
+ * inside one block, otherwise every list item between the start and the end block.
182
+ * @param {Nabu} nabu
183
+ * @returns {NabuNode[]}
184
+ */
185
+ export function selectedItems(nabu) {
186
+ const sel = nabu.selection;
187
+ /** @type {Block[]} */
188
+ const blocks = sel.anchorBlock && sel.anchorBlock === sel.focusBlock ? [sel.anchorBlock] : [...sel.blocks];
189
+ return blocks.filter((block) => block.type === "list-item").map((block) => /** @type {NabuNode} */ (block.node));
190
+ }
191
+
192
+ /**
193
+ * Tab / Shift+Tab on the current selection. Returns true (= the key is handled, the default prevented)
194
+ * whenever the selection touches list items, even when none of them could move: focus never leaves the editor.
195
+ * @param {Nabu} nabu
196
+ * @param {KeyboardEvent} event
197
+ * @returns {boolean}
198
+ */
199
+ export function handleTab(nabu, event) {
200
+ if (event.key !== "Tab" || event.ctrlKey || event.altKey || event.metaKey) return false;
201
+ const items = selectedItems(nabu);
202
+ if (!items.length) return false;
203
+ moveItems(nabu, items, event.shiftKey);
204
+ return true;
205
+ }
@@ -1,9 +1,12 @@
1
1
  export { Todos } from "./todos.svelte.js";
2
2
  /**
3
- * @import {SvelteSet} from "svelte/reactivity";
3
+ * The list container (bulleted, numbered or to-do): the `list:*` actions, Tab and Shift+Tab indentation over a selection, and the list slash entries. Always loaded together with `ListItemExtension`.
4
4
  */
5
- export const ListExtension: import("../..").Extension;
6
- export const ListItemExtension: import("../..").Extension;
5
+ export const ListExtension: import("../../extend.js").Extension;
6
+ /**
7
+ * The list item block: Enter, Backspace and Tab behaviour inside lists, the to-do checkbox and `todo:toggle` (`Mod-Enter`). Always loaded together with `ListExtension`.
8
+ */
9
+ export const ListItemExtension: import("../../extend.js").Extension;
7
10
  import { List } from "./list.svelte.js";
8
11
  import { ListItem } from "./list-item.svelte.js";
9
12
  import ListComponent from "./List.svelte";
@@ -1,54 +1,65 @@
1
1
  import { extension } from "../../utils/extensions";
2
2
  import { List } from "./list.svelte.js";
3
3
  import ListComponent from "./List.svelte";
4
- import { ListItem, indentListItem, unindentListItem } from "./list-item.svelte.js";
4
+ import { ListItem } from "./list-item.svelte.js";
5
5
  import ListItemComponent from "./ListItem.svelte";
6
- import { ListBehavior } from "./list.behavior.svelte.js";
6
+ import { normalizeLists } from "./normalize.js";
7
+ import { moveItems, selectedItems, handleTab } from "./indent.js";
8
+ import { convertToList } from "./to-list.js";
9
+ import { toggleSelectedTodos, selectedTodoItems } from "./todos-toggle.js";
7
10
  import { onBeforeInput } from "./hooks/onBeforeInput.hook.js";
8
11
 
9
12
  /**
10
- * @import {SvelteSet} from "svelte/reactivity";
13
+ * The list container (bulleted, numbered or to-do): the `list:*` actions, Tab and Shift+Tab indentation over a selection, and the list slash entries. Always loaded together with `ListItemExtension`.
11
14
  */
12
-
13
15
  const ListExtension = extension("list", {
14
16
  block: List,
15
17
  component: ListComponent,
18
+ slashCommands: [
19
+ { title: 'Bulleted list', icon: '•', aliases: ['bullet', 'ul', 'list'], group: 'lists', order: 0, run: (e) => e.exec('list:convert', { listType: 'bullet' }) },
20
+ { title: 'Numbered list', icon: '1.', aliases: ['numbered', 'ol', 'ordered'], group: 'lists', order: 10, run: (e) => e.exec('list:convert', { listType: 'ordered' }) },
21
+ { title: 'To-do list', icon: '✓', aliases: ['todo', 'task', 'checkbox', 'checklist'], group: 'lists', order: 20, run: (e) => e.exec('list:convert', { listType: 'todo' }) },
22
+ ],
16
23
  hooks: {
17
24
  onBeforeInput,
18
- onBeforeTransaction(nabu) {
19
- const lists = /** @type {SvelteSet<List>} */ (nabu.blocksByType.get("list"));
20
- lists?.forEach(list => {
21
- const behavior = list.behaviors.get("list");
22
- if (!behavior || !(behavior instanceof ListBehavior)) return;
23
- const sibblings = list.getAdjacentSiblings();
24
- const previous = sibblings.previous;
25
- if (!previous) return;
26
- const previousBehavior = previous.behaviors.get("list");
27
- if (!previousBehavior || !(previousBehavior instanceof ListBehavior)) return;
28
-
29
- previousBehavior.absorbs(list);
30
- list.destroy();
31
-
32
- })
33
-
34
- }
25
+ // Tab / Shift+Tab with a selection spanning several blocks: the keydown would otherwise resolve to
26
+ // the lowest common ancestor (a List, a container, the root), which does not know about indenting.
27
+ // A selection inside one block is handled by ListItem.keydown.
28
+ onKeyDown: (nabu, event) => {
29
+ const sel = nabu.selection;
30
+ if (sel.anchorBlock === sel.focusBlock) return nabu.CONTINUE;
31
+ return handleTab(nabu, event) ? nabu.BREAK : nabu.CONTINUE;
32
+ },
33
+ // Runs before doc.commit(): the fixes belong to the same transaction (same undo step) as the
34
+ // operation that broke the structure. See normalize.js for the invariants.
35
+ onBeforeTransaction: normalizeLists,
35
36
  }
36
37
  });
37
38
 
39
+ /**
40
+ * The list item block: Enter, Backspace and Tab behaviour inside lists, the to-do checkbox and `todo:toggle` (`Mod-Enter`). Always loaded together with `ListExtension`.
41
+ */
38
42
  const ListItemExtension = extension("list-item", {
39
43
  block: ListItem,
40
44
  component: ListItemComponent,
41
- actions: {
42
- 'list:indent': (nabu) => {
43
- const block = nabu.selection.anchorBlock;
44
- if (block?.type !== 'list-item') return;
45
- indentListItem(/** @type {ListItem} */ (block));
46
- },
47
- 'list:unindent': (nabu) => {
48
- const block = nabu.selection.anchorBlock;
49
- if (block?.type !== 'list-item') return;
50
- unindentListItem(/** @type {ListItem} */ (block));
45
+ commands: {
46
+ // Mod-Enter on a to-do item: toggle the to-do item(s) the selection touches. `when` limits it to to-do items and
47
+ // `scope` makes it win over a generic Mod-Enter command (e.g. the regions' "split here") with the caret in an item.
48
+ 'todo:toggle': {
49
+ label: 'Toggle to-do',
50
+ keys: ['Mod-Enter'],
51
+ scope: 'list-item',
52
+ when: (ctx) => selectedTodoItems(ctx.nabu).length > 0,
53
+ run: (nabu) => toggleSelectedTodos(nabu),
51
54
  },
55
+ },
56
+ actions: {
57
+ // Every list item the selection touches (the anchor item for a caret).
58
+ 'list:indent': (nabu) => { moveItems(nabu, selectedItems(nabu), false); },
59
+ 'list:unindent': (nabu) => { moveItems(nabu, selectedItems(nabu), true); },
60
+ // Make the block holding the caret a list of that type: a list item changes type ON ITS OWN (the
61
+ // surrounding list is split around it), any other text block is replaced by a new list.
62
+ 'list:convert': (nabu, data) => convertToList(nabu, data?.listType),
52
63
  }
53
64
  });
54
65
 
@@ -1,17 +1,13 @@
1
1
  /**
2
- * @import { Nabu, NabuNode, Block } from "..";
3
- */
4
- /**
5
- * @typedef {NabuNode<{type: "list-item", text: LoroText}>} ListItemNode
6
- */
7
- /**
8
- * Indent a list item (Tab behavior): move into previous sibling's sublist, hoist own children.
2
+ * Indent a list item (Tab behavior): move under the previous sibling (into its last sublist when of the same type, else a new one).
3
+ * The selection (collapsed caret or range) is restored afterwards. See indent.js.
9
4
  * @param {ListItem} item
10
5
  * @returns {boolean}
11
6
  */
12
7
  export function indentListItem(item: ListItem): boolean;
13
8
  /**
14
9
  * Unindent a list item (Shift+Tab behavior): move out of parent sublist, carry followers.
10
+ * The selection (collapsed caret or range) is restored afterwards. See indent.js.
15
11
  * @param {ListItem} item
16
12
  * @returns {boolean}
17
13
  */
@@ -19,13 +15,14 @@ export function unindentListItem(item: ListItem): boolean;
19
15
  export class ListItem extends MegaBlock {
20
16
  /**
21
17
  * @param {import('../../utils/extensions.js').PasteBlock} pb
22
- * @param {{ recurse: (child: import('../../utils/extensions.js').PasteBlock, ctx?: object) => string, listType?: string, index?: number, depth?: number }} helpers
18
+ * @param {Partial<import('../../utils/extensions.js').SerializeContext> & { listType?: string, index?: number, start?: number, run?: number, indent?: string }} helpers `run`: position of the list among adjacent lists (marker alternation), `indent`: what the item is nested under
23
19
  */
24
- static toMarkdown(pb: import("../../utils/extensions.js").PasteBlock, { recurse, listType, index, depth }: {
25
- recurse: (child: import("../../utils/extensions.js").PasteBlock, ctx?: object) => string;
20
+ static toMarkdown(pb: import("../../utils/extensions.js").PasteBlock, { listType, index, start, run, indent }: Partial<import("../../utils/extensions.js").SerializeContext> & {
26
21
  listType?: string;
27
22
  index?: number;
28
- depth?: number;
23
+ start?: number;
24
+ run?: number;
25
+ indent?: string;
29
26
  }): string;
30
27
  /**
31
28
  * @param {import('../../utils/extensions.js').PasteBlock} pb
@@ -46,8 +43,8 @@ export class ListItem extends MegaBlock {
46
43
  parseInline: (el: Element) => import("loro-crdt").Delta<string>[];
47
44
  parseBlock: (el: Element) => import("../../utils/extensions.js").PasteBlock | null;
48
45
  }): import("../../utils/extensions.js").PasteBlock | null;
49
- /** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
50
- static create(nabu: Nabu, type: string, props?: Object, parentId?: string | null, index?: number | null): ListItem;
46
+ /** @param {Nabu} nabu @param {string} type @param {Record<string, any>} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
47
+ static create(nabu: Nabu, type: string, props?: Record<string, any>, parentId?: string | null, index?: number | null): ListItem;
51
48
  /** @param {Nabu} nabu @param {ListItemNode} node */
52
49
  constructor(nabu: Nabu, node: ListItemNode);
53
50
  container: LoroText;
@@ -59,13 +56,24 @@ export class ListItem extends MegaBlock {
59
56
  block: import("./list-item.svelte.js").ListItem;
60
57
  }, {}, "">;
61
58
  isTodo: boolean;
59
+ /**
60
+ * This item and its sublists as PasteBlocks (the shape the static serializers read).
61
+ * @returns {import('../../utils/extensions.js').PasteBlock}
62
+ */
63
+ toPasteBlock(): import("../../utils/extensions.js").PasteBlock;
64
+ /** A to-do item carries its `checked` state onto the clipboard. */
65
+ get clipboardProps(): {
66
+ checked: boolean;
67
+ } | {
68
+ checked?: undefined;
69
+ };
62
70
  /**
63
71
  * Set the checked state and persist to Loro.
64
72
  * @param {boolean} v
65
73
  */
66
74
  setChecked(v: boolean): void;
67
75
  /**
68
- * Toggle the checked state.
76
+ * Toggle the checked state (the user's gesture: a click on the box, the key). Does nothing in a read-only editor.
69
77
  */
70
78
  toggleChecked(): void;
71
79
  get text(): string;
@@ -74,9 +82,12 @@ export class ListItem extends MegaBlock {
74
82
  from: number;
75
83
  to: number;
76
84
  isCollapsed: boolean;
77
- direction: "forward" | "backward" | "none";
85
+ direction: "backward" | "forward" | "none";
78
86
  } | null;
79
- sublist: Block | undefined;
87
+ /** Every sublist, in order: an item may hold several, of different types (`a > [ul[…], ol[…], ul[…]]`). */
88
+ sublists: Block[];
89
+ /** The first sublist (see `sublists`). */
90
+ sublist: Block;
80
91
  /**
81
92
  * A ListItem must always live inside a List.
82
93
  * wrapOrphan() uses this to auto-create a List wrapper when a ListItem
@@ -90,10 +101,11 @@ export class ListItem extends MegaBlock {
90
101
  props: () => Record<string, any>;
91
102
  };
92
103
  /**
93
- * Retourne la sous-liste existante, ou en crée une nouvelle à la fin des enfants.
104
+ * The sublist that receives a child of type `listType`: the LAST sublist when it has that type, otherwise
105
+ * a new one appended after the existing sublists (an item may hold several sublists of different types).
94
106
  * @param {"bullet" | "ordered" | "todo"} [listType]
95
107
  */
96
- getOrCreateSublist(listType?: "bullet" | "ordered" | "todo"): Block;
108
+ getOrCreateSublist(listType?: "bullet" | "ordered" | "todo"): Block | List;
97
109
  /** @param {KeyboardEvent} event */
98
110
  keydown(event: KeyboardEvent): boolean;
99
111
  /** @param {import('../block.svelte.js').Block} block */
@@ -106,10 +118,12 @@ export class ListItem extends MegaBlock {
106
118
  export type ListItemNode = NabuNode<{
107
119
  type: "list-item";
108
120
  text: LoroText;
121
+ checked?: boolean;
109
122
  }>;
110
123
  import { MegaBlock } from "../megablock.svelte.js";
111
124
  import { LoroText } from "loro-crdt";
112
125
  import { TextBehavior } from "../../behaviors/text";
113
126
  import type { Block } from "..";
127
+ import { List } from "./list.svelte.js";
114
128
  import type { Nabu } from "..";
115
129
  import type { NabuNode } from "..";