@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
@@ -0,0 +1,402 @@
1
+ /**
2
+ * The operations behind the region commands (ADR 018 lot A2): split, insert, merge, unwrap, exit, move, promote / demote,
3
+ * navigate. A region is a CONTAINER: every structural operation is a handful of tree moves (a block that survives is MOVED,
4
+ * never deleted and recreated, so its id, its text and the caret survive), committed ONCE = one undo step (the undo restores
5
+ * the selection, ADR 015: the selection captured before the operation is the one the step gives back). They put the caret
6
+ * back where it belongs. No key handling here (see commands.js); a function returns whether it changed something.
7
+ *
8
+ * Reads go through the registry and the blocks (the committed, rendered state); writes go through the Loro tree nodes. An
9
+ * operation reads EVERYTHING it needs before its first write.
10
+ *
11
+ * @import { Nabu, NabuNode } from "../nabu.svelte.js";
12
+ * @import { Block } from "../block.svelte.js";
13
+ * @import { Region } from "./regions.svelte.js";
14
+ */
15
+
16
+ import { tick } from "svelte";
17
+ import { Regions } from "./regions.svelte.js";
18
+ import { createRegion } from "./insert.js";
19
+ import { hasText, isBodyTextBlock, mergePlan, neighbourOf, siblingsOf } from "./facts.js";
20
+
21
+ /** @typedef {{ block: Block, offset: number }} Caret */
22
+
23
+ /** The children nodes of a node (a snapshot, in order). @param {NabuNode} node @returns {NabuNode[]} */
24
+ export const kids = (node) => /** @type {NabuNode[]} */ (node.children() ?? []);
25
+
26
+ /** The id of a node's parent, `null` at the root. @param {NabuNode} node @returns {string | null} */
27
+ export const parentIdOf = (node) => node.parent()?.id.toString() ?? null;
28
+
29
+ /** The region of a `Region`, of a region block, or of the id of a region block. @param {Nabu} nabu @param {unknown} target @returns {Region | null} */
30
+ export function toRegion(nabu, target) {
31
+ if (!target) return null;
32
+ const regions = Regions.get(nabu);
33
+ if (typeof target === 'string') return regions.of(target);
34
+ const block = /** @type {any} */ (target).block ?? /** @type {any} */ (target).marker ?? target;
35
+ return typeof block?.id === 'string' ? regions.of(block.id) : null;
36
+ }
37
+
38
+ /** The type of the blocks the editor creates by default (a paragraph). @param {Nabu} nabu @returns {string} */
39
+ export const defaultType = (nabu) => (nabu.defaultBlockType && nabu.registry.has(nabu.defaultBlockType) ? nabu.defaultBlockType : 'paragraph');
40
+
41
+ /**
42
+ * Creates a block of the default text type WITHOUT committing (the caller owns the transaction).
43
+ * @param {Nabu} nabu @param {Record<string, any>} props @param {string | null} parentId @param {number} index @returns {Block}
44
+ */
45
+ export function createDefaultBlock(nabu, props, parentId, index) {
46
+ const type = defaultType(nabu);
47
+ return /** @type {any} */ (nabu.registry.get(type)).create(nabu, type, props, parentId, index);
48
+ }
49
+
50
+ /** Puts the caret back after a commit (the DOM is re-rendered by then). @param {Nabu} nabu @param {Block | null | undefined} block @param {number} offset */
51
+ export async function restoreCaret(nabu, block, offset) {
52
+ if (!block) return;
53
+ await tick();
54
+ const fresh = nabu.blocks.get(block.id);
55
+ if (fresh) nabu.selection.setCursor(fresh, offset);
56
+ }
57
+
58
+ /** The first block holding text, in document order, among `blocks` and their descendants (a sub-region's title counts). @param {Block[]} blocks @returns {Block | null} */
59
+ export function firstTextBlock(blocks) {
60
+ for (const block of blocks) {
61
+ if (hasText(block)) return block;
62
+ const found = firstTextBlock(/** @type {any} */ (block).children ?? []);
63
+ if (found) return found;
64
+ }
65
+ return null;
66
+ }
67
+
68
+ /** The first declared type of a level (several types may share one level: the first declared wins). @param {Regions} regions @param {number} level */
69
+ const typeOfLevel = (regions, level) => [...regions.config.types.values()].find((def) => def.level === level);
70
+
71
+ /**
72
+ * "Split here" (ADR 018 §3.5): the region C whose body directly holds the caret block (none at the root) gets a new sibling N
73
+ * of the same type (the default type at the root) right after it. The caret block is cut like Enter does (a heading cut at its
74
+ * end gives a paragraph, mid-text both halves keep their type; at offset 0 there is no cut), then the tail and ALL the blocks
75
+ * that follow it move, in order, into the body of N. At the root N takes the tail and the root blocks that follow it up to
76
+ * (excluding) the next root region. The caret goes to the start of the tail.
77
+ * A block that does not split on Enter (a code block, where Mod-Enter is the user's own) is left alone. One undo step: the
78
+ * block split commits itself (twice), and the engine merges those commits with ours, which follows in the same synchronous turn.
79
+ * @param {Nabu} nabu
80
+ * @param {{ block: Block | null }} ctx
81
+ * @returns {Promise<boolean>} false when the caret is not in a splittable text block
82
+ */
83
+ export async function splitRegion(nabu, ctx) {
84
+ const block = ctx.block;
85
+ if (!block || !isBodyTextBlock(block) || block.splitsOnEnter === false) return false;
86
+ const sel = block.selection;
87
+ if (!sel) return false;
88
+ const regions = Regions.get(nabu);
89
+ const current = block.parent ? regions.of(block.parent) : null;
90
+ const type = current && regions.config.types.has(current.type) ? current.type : regions.config.defaultType;
91
+ const length = /** @type {any} */ (block.behaviors.get('text')).text.length;
92
+ const offset = Math.min(sel.from, length);
93
+
94
+ // The first block that moves: the caret block itself at offset 0, else the tail of the cut.
95
+ let first = block;
96
+ if (offset > 0) {
97
+ const cut = block.split({ offset, to: length });
98
+ if (!cut?.block) return false;
99
+ first = cut.block;
100
+ }
101
+
102
+ const node = first.node;
103
+ const siblings = node.parent() ? kids(/** @type {NabuNode} */ (node.parent())) : /** @type {NabuNode[]} */ (nabu.tree.roots());
104
+ const from = siblings.findIndex((n) => n.id === node.id);
105
+ let movers = siblings.slice(from);
106
+ if (!current) {
107
+ const stop = movers.findIndex((n) => n.data.get('type') === 'region');
108
+ if (stop >= 0) movers = movers.slice(0, stop);
109
+ }
110
+
111
+ // Right after the region (a sibling), or at the cut at the root.
112
+ const fresh = current
113
+ ? createRegion(nabu, { type, parentId: parentIdOf(current.block.node), index: (current.block.node.index() ?? 0) + 1 })
114
+ : createRegion(nabu, { type, parentId: null, index: from });
115
+ movers.forEach((n, i) => n.move(fresh.node, i));
116
+
117
+ await nabu.commit();
118
+ await restoreCaret(nabu, first, 0);
119
+ return true;
120
+ }
121
+
122
+ /**
123
+ * Inserts an empty region next to a region, or inside its body, as one undo step; the caret stays where it is.
124
+ * - `at: 'after'` (default): right after `region`, in its parent (a sibling; its whole body stays with it); `'before'`: right before it;
125
+ * - `at: 'inside'`: a SUB-region, in the body of `region`, at the end of the body or at `index` (clamped). The nesting rule
126
+ * holds: the type must be strictly deeper than `region`, and without `type` the shallowest declared deeper type is used (the
127
+ * first declared of its level). A warning and `false` when no type is deeper (a region of the deepest level) or `type` is
128
+ * not valid there; `region` is required;
129
+ * - without `region` (the caret is outside any region): at the caret's position among the root blocks (`from` = the caret
130
+ * block): after / before the root block that holds it.
131
+ * @param {Nabu} nabu
132
+ * @param {Region | null} region the region the position is relative to
133
+ * @param {{ type?: string, at?: 'after' | 'before' | 'inside', index?: number, title?: string, fields?: Record<string, any> }} [options]
134
+ * `type` defaults to the type of `region` (`'inside'`: see above), else the default (top-level) type. `index` only counts for `'inside'`.
135
+ * @param {Block | null} [from] the caret block, for the case without region
136
+ * @returns {Promise<boolean>}
137
+ */
138
+ export async function insertNearRegion(nabu, region, { type, at = 'after', index: bodyIndex, title, fields } = {}, from = null) {
139
+ const regions = Regions.get(nabu);
140
+ const { types, defaultType: topType } = regions.config;
141
+ if (at !== 'after' && at !== 'before' && at !== 'inside') { nabu.warn(`region:insert: "at" must be "after", "before" or "inside", got ${JSON.stringify(at)}`); return false; }
142
+
143
+ if (at === 'inside') {
144
+ if (!region) { nabu.warn('region:insert: at "inside" needs a region (none given, none holds the caret)'); return false; }
145
+ const deeper = [...types.values()].filter((def) => def.level > region.level);
146
+ if (!deeper.length) { nabu.warn(`region:insert: no region type is deeper than "${region.type}" (level ${region.level}): nothing can be nested in it`); return false; }
147
+ const def = type === undefined
148
+ ? deeper.reduce((best, d) => (d.level < best.level ? d : best))
149
+ : types.get(type);
150
+ if (!def) { nabu.warn(`region:insert: region type "${type}" is not declared`); return false; }
151
+ if (def.level <= region.level) { nabu.warn(`region:insert: "${def.id}" (level ${def.level}) cannot be nested in "${region.type}" (level ${region.level}): a sub-region must be deeper`); return false; }
152
+ createRegion(nabu, {
153
+ type: def.id, parentId: region.block.id, title, fields,
154
+ index: typeof bodyIndex === 'number' && Number.isFinite(bodyIndex) ? bodyIndex : null,
155
+ });
156
+ await nabu.commit();
157
+ return true;
158
+ }
159
+
160
+ const wanted = type ?? (region && types.has(region.type) ? region.type : topType);
161
+ if (!types.has(wanted)) { nabu.warn(`region:insert: region type "${wanted}" is not declared`); return false; }
162
+
163
+ /** @type {string | null} */
164
+ let parentId = null;
165
+ let index;
166
+ if (region) {
167
+ parentId = parentIdOf(region.block.node);
168
+ index = (region.block.node.index() ?? 0) + (at === 'before' ? 0 : 1);
169
+ } else {
170
+ /** @type {Block | null} */
171
+ let root = from;
172
+ while (root?.parent) root = /** @type {Block} */ (root.parent);
173
+ const position = root ? nabu.children.indexOf(root) : -1;
174
+ index = position < 0 ? (at === 'before' ? 0 : nabu.children.length) : position + (at === 'before' ? 0 : 1);
175
+ }
176
+ createRegion(nabu, { type: wanted, parentId, index, title, fields });
177
+ await nabu.commit();
178
+ return true;
179
+ }
180
+
181
+ /**
182
+ * Where the caret goes once `region`'s title is gone (merge, unwrap): the start of the first text block of its body, else null.
183
+ * @param {Region} region @returns {Caret | null}
184
+ */
185
+ function bodyLanding(region) {
186
+ const first = firstTextBlock(region.block.children);
187
+ return first ? { block: first, offset: 0 } : null;
188
+ }
189
+
190
+ /**
191
+ * Dissolves a region node in place: its children take its position in its parent, in order, and the node is deleted by the
192
+ * caller. Writes only.
193
+ * @param {NabuNode} node
194
+ */
195
+ function spill(node) {
196
+ /** @type {NabuNode} */
197
+ let anchor = node;
198
+ for (const child of kids(node)) {
199
+ child.moveAfter(anchor);
200
+ anchor = child;
201
+ }
202
+ }
203
+
204
+ /**
205
+ * Merges a region with what is before it (ADR 018 §3.5, see `mergePlan`): into its previous sibling when that one is a region
206
+ * of the same level (its body joins the end of that body), else the region is dissolved in place (its body takes its
207
+ * position in its parent). The title goes away in both cases (an undo gives it back). One undo step. The caret, if given,
208
+ * is put back; when it was in the title that went away it goes to the start of the body (else the end of the region joined).
209
+ * @param {Nabu} nabu
210
+ * @param {Region} region
211
+ * @param {Caret | null} [caret]
212
+ * @returns {Promise<boolean>} false when there is nothing to merge with (the first region of the document)
213
+ */
214
+ export async function mergeRegion(nabu, region, caret = null) {
215
+ const plan = mergePlan(nabu, region);
216
+ if (!plan) return false;
217
+ const landing = caret && caret.block !== region.block
218
+ ? caret
219
+ : (bodyLanding(region) ?? (plan.kind === 'merge' ? { block: plan.into.block, offset: plan.into.block.title.length } : null));
220
+
221
+ if (plan.kind === 'merge') {
222
+ const target = plan.into.block.node;
223
+ let at = kids(target).length;
224
+ for (const child of kids(region.block.node)) child.move(target, at++);
225
+ } else {
226
+ spill(region.block.node);
227
+ }
228
+ nabu.delete(region.block);
229
+ await nabu.commit();
230
+ if (landing) await restoreCaret(nabu, landing.block, landing.offset);
231
+ return true;
232
+ }
233
+
234
+ /**
235
+ * Dissolves a region (ADR 018 §3.5): its title, when not empty, becomes a default paragraph at the region's position (delta and
236
+ * marks kept), followed by the children of its body, in order; the region is deleted. One undo step. The caret goes to the
237
+ * same offset in the paragraph if it was in the title, else it is put back.
238
+ * @param {Nabu} nabu
239
+ * @param {Region} region
240
+ * @param {Caret | null} [caret]
241
+ * @returns {Promise<boolean>}
242
+ */
243
+ export async function unwrapRegion(nabu, region, caret = null) {
244
+ const node = region.block.node;
245
+ /** @type {Caret | null} */
246
+ let landing = caret && caret.block !== region.block ? caret : bodyLanding(region);
247
+
248
+ /** @type {NabuNode} */
249
+ let anchor = node;
250
+ if (region.block.title.length) {
251
+ const paragraph = createDefaultBlock(nabu, { delta: region.block.container.toDelta() }, parentIdOf(node), node.index() ?? 0);
252
+ anchor = paragraph.node;
253
+ if (caret && caret.block === region.block) landing = { block: paragraph, offset: caret.offset };
254
+ }
255
+ for (const child of kids(node)) {
256
+ child.moveAfter(anchor);
257
+ anchor = child;
258
+ }
259
+ nabu.delete(region.block);
260
+ await nabu.commit();
261
+ if (landing) await restoreCaret(nabu, landing.block, landing.offset);
262
+ return true;
263
+ }
264
+
265
+ /**
266
+ * Leaves a region (ADR 018 §3.5, `region:exit`): the caret ends up in an empty default paragraph right after the region, in the
267
+ * region's parent (one level up).
268
+ * - the caret block is an EMPTY default paragraph that is the LAST child of the region's body: that very paragraph moves out,
269
+ * right after the region (so repeating the command climbs scene, chapter, part, root);
270
+ * - otherwise a new empty paragraph is created there.
271
+ * One undo step.
272
+ * @param {Nabu} nabu
273
+ * @param {Region} region
274
+ * @param {Block | null} [from] the block holding the caret
275
+ * @returns {Promise<boolean>}
276
+ */
277
+ export async function exitRegion(nabu, region, from = null) {
278
+ const node = region.block.node;
279
+ const last = region.block.children[region.block.children.length - 1];
280
+ const movable = !!from && from === last && from.type === defaultType(nabu)
281
+ && /** @type {any} */ (from.behaviors.get('text'))?.text === '' && !(/** @type {any} */ (from).children?.length);
282
+
283
+ /** @type {Block} */
284
+ let landing;
285
+ if (movable && from) {
286
+ from.node.moveAfter(node);
287
+ landing = from;
288
+ } else {
289
+ landing = createDefaultBlock(nabu, {}, parentIdOf(node), (node.index() ?? 0) + 1);
290
+ }
291
+ await nabu.commit();
292
+ await restoreCaret(nabu, landing, 0);
293
+ return true;
294
+ }
295
+
296
+ /**
297
+ * Swaps a region with the previous / next sibling node that is a region of the SAME level, in the same parent: they trade
298
+ * places with their bodies (a region is a container, it moves with its subtree); the text siblings in between stay where
299
+ * they are. At an edge, or when the next sibling region has another level, nothing moves (no commit). One undo step.
300
+ * @param {Nabu} nabu
301
+ * @param {Region} region
302
+ * @param {'up' | 'down'} direction
303
+ * @param {Caret | null} [caret]
304
+ * @returns {Promise<boolean>}
305
+ */
306
+ export async function moveRegion(nabu, region, direction, caret = null) {
307
+ const siblings = siblingsOf(nabu, region.block);
308
+ const step = direction === 'up' ? -1 : 1;
309
+ let j = siblings.indexOf(region.block) + step;
310
+ while (siblings[j] && siblings[j].type !== 'region') j += step;
311
+ const other = siblings[j] ? Regions.get(nabu).of(siblings[j]) : null;
312
+ if (!other || other.level !== region.level) return false;
313
+
314
+ const [first, second] = direction === 'up' ? [other, region] : [region, other];
315
+ const beforeSecond = neighbourOf(nabu, second.block, -1);
316
+ second.block.node.moveBefore(first.block.node);
317
+ // Text between the two: the first one goes after it, i.e. to where the second one was.
318
+ if (beforeSecond && beforeSecond !== first.block) first.block.node.moveAfter(beforeSecond.node);
319
+
320
+ await nabu.commit();
321
+ if (caret) await restoreCaret(nabu, caret.block, caret.offset);
322
+ return true;
323
+ }
324
+
325
+ /**
326
+ * Demotes a region (ADR 018 §3.5), structurally like a list indent: it is retyped to the first type of the level below and
327
+ * becomes the LAST child of its previous sibling node, which must be a region shallower than the new type. The sub-regions of
328
+ * its body that are now too shallow for it (level <= its new level) are moved out, in order, right after it (inside the
329
+ * sibling). No-op (no commit) when there is no deeper type or no such sibling. One undo step.
330
+ * @param {Nabu} nabu
331
+ * @param {Region} region
332
+ * @param {Caret | null} [caret]
333
+ * @returns {Promise<boolean>}
334
+ */
335
+ export async function demoteRegion(nabu, region, caret = null) {
336
+ const regions = Regions.get(nabu);
337
+ const target = region.def && typeOfLevel(regions, region.level + 1);
338
+ if (!target) return false;
339
+ const previous = neighbourOf(nabu, region.block, -1);
340
+ const into = previous?.type === 'region' ? regions.of(previous) : null;
341
+ if (!into || into.level >= target.level) return false;
342
+ const out = region.children.filter((child) => child.def && child.level <= target.level);
343
+
344
+ const node = region.block.node;
345
+ region.block.setRegionType(target.id, { commit: false });
346
+ node.move(into.block.node, kids(into.block.node).length);
347
+ /** @type {NabuNode} */
348
+ let anchor = node;
349
+ for (const child of out) {
350
+ child.block.node.moveAfter(anchor);
351
+ anchor = child.block.node;
352
+ }
353
+
354
+ await nabu.commit();
355
+ if (caret) await restoreCaret(nabu, caret.block, caret.offset);
356
+ return true;
357
+ }
358
+
359
+ /**
360
+ * Promotes a region (ADR 018 §3.5, Q4), the inverse of a demotion: it is retyped to the first type of the level above and
361
+ * leaves its parent region P to sit right after it (in P's parent); every block that FOLLOWED it in P's body, text and
362
+ * regions, follows it into its body, in order, appended after its own. No-op (no commit) when it has no parent region or
363
+ * there is no type one level up. One undo step.
364
+ * @param {Nabu} nabu
365
+ * @param {Region} region
366
+ * @param {Caret | null} [caret]
367
+ * @returns {Promise<boolean>}
368
+ */
369
+ export async function promoteRegion(nabu, region, caret = null) {
370
+ const regions = Regions.get(nabu);
371
+ const parent = region.parent;
372
+ const target = region.def && parent && typeOfLevel(regions, region.level - 1);
373
+ if (!parent || !target) return false;
374
+ const siblings = parent.block.children;
375
+ const followers = siblings.slice(siblings.indexOf(region.block) + 1);
376
+
377
+ const node = region.block.node;
378
+ region.block.setRegionType(target.id, { commit: false });
379
+ node.moveAfter(parent.block.node);
380
+ let at = kids(node).length;
381
+ for (const follower of followers) follower.node.move(node, at++);
382
+
383
+ await nabu.commit();
384
+ if (caret) await restoreCaret(nabu, caret.block, caret.offset);
385
+ return true;
386
+ }
387
+
388
+ /**
389
+ * Puts the caret at the first text position of the BODY of `region` (the first text block after its title, inside it), else
390
+ * at the end of its title, and scrolls it into view.
391
+ * @param {Nabu} nabu
392
+ * @param {Region} region
393
+ * @returns {Promise<boolean>}
394
+ */
395
+ export async function goToRegion(nabu, region) {
396
+ await tick();
397
+ const first = firstTextBlock(region.block.children);
398
+ if (first) nabu.selection.setCursor(first, 0);
399
+ else nabu.selection.setCursor(region.block, region.block.title.length);
400
+ /** @type {any} */ ((first ?? region.block).element)?.scrollIntoView?.({ block: 'center' });
401
+ return true;
402
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * The regions that contain `block` when it is a closing mark: a default paragraph without children, a DIRECT child of a region
3
+ * body, whose text is `:::` (trailing spaces allowed) or empty. Innermost first. Empty when `block` is not such a mark.
4
+ * @param {Nabu} nabu @param {Block | null | undefined} block
5
+ * @returns {Region[]}
6
+ */
7
+ export function closeChain(nabu: Nabu, block: Block | null | undefined): Region[];
8
+ /**
9
+ * The options of the `region:close` chooser: one per region of `chain`, innermost first. `region` is what the command runs on.
10
+ * @param {Region[]} chain
11
+ * @param {ReturnType<typeof createMessages>} text the region messages (`createMessages`)
12
+ * @returns {Array<{ id: string, label: string, detail: string, region: Region }>}
13
+ */
14
+ export function closeOptions(chain: Region[], text: ReturnType<typeof createMessages>): Array<{
15
+ id: string;
16
+ label: string;
17
+ detail: string;
18
+ region: Region;
19
+ }>;
20
+ /**
21
+ * Closes `region` (an ancestor of the closing mark `mark`, see the module doc). Resolves to false (nothing changed) when `mark` is
22
+ * not a closing mark inside `region`.
23
+ * @param {Nabu} nabu @param {Region} region @param {Block} mark
24
+ * @returns {Promise<boolean>}
25
+ */
26
+ export function closeRegionAt(nabu: Nabu, region: Region, mark: Block): Promise<boolean>;
27
+ /**
28
+ * `Extension#hooks.onBeforeInput` of the `:::` + Enter trigger (see the module doc). Returns BREAK when it took the Enter.
29
+ * @param {Nabu} nabu @param {InputEvent} event @param {Block} block
30
+ */
31
+ export function closeOnEnter(nabu: Nabu, event: InputEvent, block: Block): symbol;
32
+ import type { Nabu } from "../nabu.svelte.js";
33
+ import type { Block } from "../block.svelte.js";
34
+ import type { Region } from "./regions.svelte.js";
35
+ import type { createMessages } from "./messages.js";
@@ -0,0 +1,130 @@
1
+ /**
2
+ * Closing a region from inside it (ADR 018 §3.5, "Fermer une région"): the operation behind `region:close` and the `:::` + Enter
3
+ * trigger (the counterpart of `:::` + Space, which creates one: see create.js).
4
+ *
5
+ * The TRIGGER is an Enter (`insertParagraph`) in a default paragraph that holds exactly `:::` (trailing spaces allowed), the
6
+ * collapsed caret at its end, and that is a DIRECT child of a region body. Anywhere else (outside any region, in a list, with other
7
+ * text) Enter is an ordinary Enter. The regions that CONTAIN the caret, innermost first, are the things that can be closed:
8
+ *
9
+ * - one region: it is closed at once;
10
+ * - several: the `choose` confirmation of `region:close` lists them ("Close Scene 2.2 — The ferryman", detail = the numbering
11
+ * prefix). While it is open the Enter is NOT applied: Escape (or a click away) leaves the paragraph with its `:::` as it was,
12
+ * and the user can press Enter again or type on.
13
+ *
14
+ * THE EFFECT of closing X (R0 the innermost region, p the `:::` paragraph, a child of R0; X = R_k): p is deleted; the blocks that
15
+ * FOLLOWED p in the body of R0, then those that followed R0 in the body of R1, … up to those that followed R_{k-1} in the body of X
16
+ * (document order) leave their bodies and are moved, in that order, right after X, in the parent of X. The caret goes to the first
17
+ * text position of the first block that moved (a sub-region: its title); when nothing followed, an empty default paragraph is created
18
+ * right after X for it. Moved sub-regions stay valid for the nesting rule: they are deeper than the region they were in, which was
19
+ * deeper than X, which is deeper than the parent of X. One undo step; undo gives back the typed `:::` (and the caret after it).
20
+ *
21
+ * At the end of a region this is the same as `region:exit` (Mod-Shift-Enter): nothing follows, a paragraph comes after the region.
22
+ *
23
+ * @import { Nabu } from "../nabu.svelte.js";
24
+ * @import { Block } from "../block.svelte.js";
25
+ * @import { Region } from "./regions.svelte.js";
26
+ * @import { createMessages } from "./messages.js";
27
+ */
28
+
29
+ import { tick } from "svelte";
30
+ import { Regions } from "./regions.svelte.js";
31
+ import { createDefaultBlock, defaultType, firstTextBlock, parentIdOf } from "./actions.js";
32
+ import { siblingsOf } from "./facts.js";
33
+
34
+ /** The text of a block, `null` when it holds none. @param {Block} block @returns {string | null} */
35
+ const textOf = (block) => /** @type {any} */ (block.behaviors.get('text'))?.text ?? null;
36
+
37
+ /**
38
+ * The regions that contain `block` when it is a closing mark: a default paragraph without children, a DIRECT child of a region
39
+ * body, whose text is `:::` (trailing spaces allowed) or empty. Innermost first. Empty when `block` is not such a mark.
40
+ * @param {Nabu} nabu @param {Block | null | undefined} block
41
+ * @returns {Region[]}
42
+ */
43
+ export function closeChain(nabu, block) {
44
+ if (!block || block.type !== defaultType(nabu) || /** @type {any} */ (block).children?.length) return [];
45
+ if (!block.parent || block.parent.type !== 'region') return [];
46
+ const text = textOf(block);
47
+ if (text === null || !/^(::: *)?$/.test(text)) return [];
48
+ /** @type {Region[]} */
49
+ const chain = [];
50
+ for (let r = Regions.get(nabu).of(block.parent); r; r = r.parent) chain.push(r);
51
+ return chain;
52
+ }
53
+
54
+ /**
55
+ * The options of the `region:close` chooser: one per region of `chain`, innermost first. `region` is what the command runs on.
56
+ * @param {Region[]} chain
57
+ * @param {ReturnType<typeof createMessages>} text the region messages (`createMessages`)
58
+ * @returns {Array<{ id: string, label: string, detail: string, region: Region }>}
59
+ */
60
+ export function closeOptions(chain, text) {
61
+ return chain.map((region) => ({
62
+ id: region.block.id,
63
+ label: text('closeOption', {
64
+ typeName: region.def?.name ?? region.type, name: region.prefix || region.title || region.def?.name || region.type,
65
+ prefix: region.prefix, title: region.title, label: region.label, type: region.type,
66
+ }),
67
+ detail: region.prefix,
68
+ region,
69
+ }));
70
+ }
71
+
72
+ /**
73
+ * Closes `region` (an ancestor of the closing mark `mark`, see the module doc). Resolves to false (nothing changed) when `mark` is
74
+ * not a closing mark inside `region`.
75
+ * @param {Nabu} nabu @param {Region} region @param {Block} mark
76
+ * @returns {Promise<boolean>}
77
+ */
78
+ export async function closeRegionAt(nabu, region, mark) {
79
+ const chain = closeChain(nabu, mark);
80
+ const k = chain.findIndex((r) => r.block.id === region.block.id);
81
+ if (k < 0) return false;
82
+
83
+ /** The blocks that follow `block` in its parent. @param {Block} block @returns {Block[]} */
84
+ const after = (block) => { const all = siblingsOf(nabu, block); return all.slice(all.indexOf(block) + 1); };
85
+ // Everything is read before the first write.
86
+ const moved = [...after(mark)];
87
+ for (let j = 0; j < k; j++) moved.push(...after(chain[j].block));
88
+ const target = chain[k].block.node;
89
+ const landing = firstTextBlock(moved);
90
+
91
+ /** @type {Block | null} */
92
+ let created = null;
93
+ const mutate = () => {
94
+ /** @type {import("../nabu.svelte.js").NabuNode} */
95
+ let anchor = target;
96
+ for (const block of moved) { block.node.moveAfter(anchor); anchor = block.node; }
97
+ if (!landing) created = createDefaultBlock(nabu, {}, parentIdOf(target), (target.index() ?? 0) + 1 + moved.length);
98
+ nabu.delete(mark);
99
+ };
100
+
101
+ // The typed `:::` is already in the document and the Enter was not applied: `shortcut` (no trigger text) makes the closing its
102
+ // own undo step, whose undo gives the `:::` back with the caret after it. Same technique as `createRegionHere`.
103
+ if (mark.transformsEnabled !== false && /** @type {any} */ (mark.behaviors.get('text'))?.selection) await nabu.shortcut(mark, '', mutate);
104
+ else { mutate(); await nabu.commit(); }
105
+
106
+ const caretBlock = landing ?? created;
107
+ const fresh = caretBlock && nabu.blocks.get(caretBlock.id);
108
+ if (fresh) { await tick(); nabu.selection.setCursor(fresh, 0); }
109
+ return true;
110
+ }
111
+
112
+ /**
113
+ * `Extension#hooks.onBeforeInput` of the `:::` + Enter trigger (see the module doc). Returns BREAK when it took the Enter.
114
+ * @param {Nabu} nabu @param {InputEvent} event @param {Block} block
115
+ */
116
+ export function closeOnEnter(nabu, event, block) {
117
+ const { BREAK, CONTINUE } = nabu;
118
+ if (event.inputType !== 'insertParagraph') return CONTINUE;
119
+ const behavior = /** @type {any} */ (block.behaviors.get('text'));
120
+ const sel = behavior?.selection;
121
+ if (!sel || !sel.isCollapsed || sel.from !== behavior.text.length || !/^::: *$/.test(behavior.text)) return CONTINUE;
122
+ const chain = closeChain(nabu, block);
123
+ if (!chain.length) return CONTINUE;
124
+ if (chain.length === 1) {
125
+ closeRegionAt(nabu, chain[0], block);
126
+ return BREAK;
127
+ }
128
+ nabu.selection.sync?.(); // the command reads selection.anchorBlock: don't wait for the async `selectionchange`
129
+ return nabu.commandConfirm.offer('region:close') ? BREAK : CONTINUE;
130
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * @param {import("./messages.js").RegionMessages | undefined} messages
3
+ * @returns {Record<string, CommandDef>}
4
+ */
5
+ export function createRegionCommands(messages: import("./messages.js").RegionMessages | undefined): Record<string, CommandDef>;
6
+ import type { CommandDef } from "../../utils/commands.js";