@aionbuilders/nabu 0.1.0-alpha.9 → 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 (319) hide show
  1. package/CHANGELOG.md +523 -0
  2. package/LICENSE +21 -0
  3. package/README.md +82 -144
  4. package/dist/behaviors/text/RichText.svelte +25 -2
  5. package/dist/behaviors/text/index.d.ts +2 -0
  6. package/dist/behaviors/text/index.js +2 -0
  7. package/dist/behaviors/text/markdown-shortcuts.extension.d.ts +5 -0
  8. package/dist/behaviors/text/markdown-shortcuts.extension.js +79 -0
  9. package/dist/behaviors/text/markdown-syntax.d.ts +42 -0
  10. package/dist/behaviors/text/markdown-syntax.js +81 -0
  11. package/dist/behaviors/text/rich-text.extension.d.ts +3 -0
  12. package/dist/behaviors/text/rich-text.extension.js +135 -53
  13. package/dist/behaviors/text/text.behavior.svelte.d.ts +217 -42
  14. package/dist/behaviors/text/text.behavior.svelte.js +657 -55
  15. package/dist/behaviors/text/typography.extension.d.ts +7 -0
  16. package/dist/behaviors/text/typography.extension.js +25 -0
  17. package/dist/blocks/Block.svelte +5 -2
  18. package/dist/blocks/BlockList.svelte +48 -0
  19. package/dist/blocks/BlockList.svelte.d.ts +17 -0
  20. package/dist/blocks/Nabu.svelte +180 -14
  21. package/dist/blocks/Nabu.svelte.d.ts +13 -0
  22. package/dist/blocks/block.extension.d.ts +3 -0
  23. package/dist/blocks/block.extension.js +8 -9
  24. package/dist/blocks/block.svelte.d.ts +356 -68
  25. package/dist/blocks/block.svelte.js +388 -42
  26. package/dist/blocks/blockquote/Blockquote.svelte +39 -0
  27. package/dist/blocks/blockquote/Blockquote.svelte.d.ts +11 -0
  28. package/dist/blocks/blockquote/blockquote.extension.d.ts +4 -0
  29. package/dist/blocks/blockquote/blockquote.extension.js +80 -0
  30. package/dist/blocks/blockquote/blockquote.svelte.d.ts +63 -0
  31. package/dist/blocks/blockquote/blockquote.svelte.js +136 -0
  32. package/dist/blocks/blockquote/index.d.ts +3 -0
  33. package/dist/blocks/blockquote/index.js +3 -0
  34. package/dist/blocks/callout/Callout.svelte +67 -0
  35. package/dist/blocks/callout/Callout.svelte.d.ts +11 -0
  36. package/dist/blocks/callout/callout.extension.d.ts +4 -0
  37. package/dist/blocks/callout/callout.extension.js +84 -0
  38. package/dist/blocks/callout/callout.svelte.d.ts +80 -0
  39. package/dist/blocks/callout/callout.svelte.js +189 -0
  40. package/dist/blocks/callout/index.d.ts +3 -0
  41. package/dist/blocks/callout/index.js +3 -0
  42. package/dist/blocks/chunks.d.ts +49 -0
  43. package/dist/blocks/chunks.js +90 -0
  44. package/dist/blocks/code/Code.svelte +84 -0
  45. package/dist/blocks/code/Code.svelte.d.ts +11 -0
  46. package/dist/blocks/code/code.extension.d.ts +4 -0
  47. package/dist/blocks/code/code.extension.js +88 -0
  48. package/dist/blocks/code/code.svelte.d.ts +68 -0
  49. package/dist/blocks/code/code.svelte.js +245 -0
  50. package/dist/blocks/code/index.d.ts +3 -0
  51. package/dist/blocks/code/index.js +3 -0
  52. package/dist/blocks/container.utils.d.ts +85 -13
  53. package/dist/blocks/container.utils.js +338 -178
  54. package/dist/blocks/dialogue/Dialogue.svelte +8 -2
  55. package/dist/blocks/dialogue/dialogue.svelte.d.ts +36 -23
  56. package/dist/blocks/dialogue/dialogue.svelte.js +51 -45
  57. package/dist/blocks/dialogue/hooks/onBeforeInput.hook.d.ts +2 -2
  58. package/dist/blocks/dialogue/hooks/onBeforeInput.hook.js +6 -4
  59. package/dist/blocks/dialogue/index.d.ts +12 -1
  60. package/dist/blocks/dialogue/index.js +51 -35
  61. package/dist/blocks/divider/Divider.svelte +28 -0
  62. package/dist/blocks/divider/Divider.svelte.d.ts +11 -0
  63. package/dist/blocks/divider/divider.extension.d.ts +23 -0
  64. package/dist/blocks/divider/divider.extension.js +153 -0
  65. package/dist/blocks/divider/divider.svelte.d.ts +54 -0
  66. package/dist/blocks/divider/divider.svelte.js +89 -0
  67. package/dist/blocks/divider/index.d.ts +3 -0
  68. package/dist/blocks/divider/index.js +3 -0
  69. package/dist/blocks/file/File.svelte +56 -0
  70. package/dist/blocks/file/File.svelte.d.ts +11 -0
  71. package/dist/blocks/file/file.extension.d.ts +4 -0
  72. package/dist/blocks/file/file.extension.js +27 -0
  73. package/dist/blocks/file/file.svelte.d.ts +29 -0
  74. package/dist/blocks/file/file.svelte.js +77 -0
  75. package/dist/blocks/file/index.d.ts +3 -0
  76. package/dist/blocks/file/index.js +3 -0
  77. package/dist/blocks/heading/Heading.svelte +2 -1
  78. package/dist/blocks/heading/heading.svelte.d.ts +27 -34
  79. package/dist/blocks/heading/heading.svelte.js +72 -60
  80. package/dist/blocks/heading/hooks/onBeforeInput.hook.js +7 -5
  81. package/dist/blocks/heading/index.d.ts +2 -2
  82. package/dist/blocks/heading/index.js +33 -8
  83. package/dist/blocks/image/Image.svelte +41 -0
  84. package/dist/blocks/image/Image.svelte.d.ts +11 -0
  85. package/dist/blocks/image/image.extension.d.ts +5 -0
  86. package/dist/blocks/image/image.extension.js +34 -0
  87. package/dist/blocks/image/image.svelte.d.ts +61 -0
  88. package/dist/blocks/image/image.svelte.js +114 -0
  89. package/dist/blocks/image/index.d.ts +3 -0
  90. package/dist/blocks/image/index.js +3 -0
  91. package/dist/blocks/index.d.ts +11 -1
  92. package/dist/blocks/index.js +11 -1
  93. package/dist/blocks/list/List.svelte +9 -2
  94. package/dist/blocks/list/ListItem.svelte +44 -7
  95. package/dist/blocks/list/convert.d.ts +20 -0
  96. package/dist/blocks/list/convert.js +92 -0
  97. package/dist/blocks/list/hooks/onBeforeInput.hook.js +60 -26
  98. package/dist/blocks/list/html-flat.d.ts +78 -0
  99. package/dist/blocks/list/html-flat.js +244 -0
  100. package/dist/blocks/list/indent.d.ts +86 -0
  101. package/dist/blocks/list/indent.js +205 -0
  102. package/dist/blocks/list/index.d.ts +7 -3
  103. package/dist/blocks/list/index.js +43 -31
  104. package/dist/blocks/list/list-item.svelte.d.ts +43 -17
  105. package/dist/blocks/list/list-item.svelte.js +143 -93
  106. package/dist/blocks/list/list.behavior.svelte.d.ts +7 -4
  107. package/dist/blocks/list/list.behavior.svelte.js +10 -5
  108. package/dist/blocks/list/list.svelte.d.ts +101 -23
  109. package/dist/blocks/list/list.svelte.js +136 -114
  110. package/dist/blocks/list/markdown.d.ts +149 -0
  111. package/dist/blocks/list/markdown.js +442 -0
  112. package/dist/blocks/list/normalize.d.ts +11 -0
  113. package/dist/blocks/list/normalize.js +396 -0
  114. package/dist/blocks/list/paste.d.ts +45 -0
  115. package/dist/blocks/list/paste.js +242 -0
  116. package/dist/blocks/list/step-back.d.ts +24 -0
  117. package/dist/blocks/list/step-back.js +87 -0
  118. package/dist/blocks/list/to-list.d.ts +7 -0
  119. package/dist/blocks/list/to-list.js +44 -0
  120. package/dist/blocks/list/todos-toggle.d.ts +20 -0
  121. package/dist/blocks/list/todos-toggle.js +48 -0
  122. package/dist/blocks/list/todos.svelte.d.ts +35 -0
  123. package/dist/blocks/list/todos.svelte.js +76 -0
  124. package/dist/blocks/media/index.d.ts +2 -0
  125. package/dist/blocks/media/index.js +2 -0
  126. package/dist/blocks/media/markdown.d.ts +12 -0
  127. package/dist/blocks/media/markdown.js +26 -0
  128. package/dist/blocks/media/media-block.svelte.d.ts +107 -0
  129. package/dist/blocks/media/media-block.svelte.js +181 -0
  130. package/dist/blocks/media/media.helpers.d.ts +24 -0
  131. package/dist/blocks/media/media.helpers.js +88 -0
  132. package/dist/blocks/megablock.svelte.d.ts +50 -19
  133. package/dist/blocks/megablock.svelte.js +115 -74
  134. package/dist/blocks/move.d.ts +23 -0
  135. package/dist/blocks/move.js +92 -0
  136. package/dist/blocks/nabu.svelte.d.ts +846 -75
  137. package/dist/blocks/nabu.svelte.js +1990 -182
  138. package/dist/blocks/paragraph/Paragraph.svelte +1 -0
  139. package/dist/blocks/paragraph/index.d.ts +2 -2
  140. package/dist/blocks/paragraph/index.js +35 -16
  141. package/dist/blocks/paragraph/paragraph.svelte.d.ts +49 -35
  142. package/dist/blocks/paragraph/paragraph.svelte.js +81 -69
  143. package/dist/blocks/paragraph/table.html.d.ts +9 -0
  144. package/dist/blocks/paragraph/table.html.js +111 -0
  145. package/dist/blocks/paragraph/table.markdown.d.ts +32 -0
  146. package/dist/blocks/paragraph/table.markdown.js +90 -0
  147. package/dist/blocks/region/Region.svelte +264 -0
  148. package/dist/blocks/region/Region.svelte.d.ts +11 -0
  149. package/dist/blocks/region/actions.d.ts +135 -0
  150. package/dist/blocks/region/actions.js +402 -0
  151. package/dist/blocks/region/close.d.ts +35 -0
  152. package/dist/blocks/region/close.js +130 -0
  153. package/dist/blocks/region/commands.d.ts +6 -0
  154. package/dist/blocks/region/commands.js +277 -0
  155. package/dist/blocks/region/create.d.ts +72 -0
  156. package/dist/blocks/region/create.js +208 -0
  157. package/dist/blocks/region/end-marker.d.ts +35 -0
  158. package/dist/blocks/region/end-marker.js +51 -0
  159. package/dist/blocks/region/facts.d.ts +69 -0
  160. package/dist/blocks/region/facts.js +152 -0
  161. package/dist/blocks/region/format.d.ts +91 -0
  162. package/dist/blocks/region/format.js +260 -0
  163. package/dist/blocks/region/index.d.ts +14 -0
  164. package/dist/blocks/region/index.js +14 -0
  165. package/dist/blocks/region/insert.d.ts +59 -0
  166. package/dist/blocks/region/insert.js +102 -0
  167. package/dist/blocks/region/messages.d.ts +254 -0
  168. package/dist/blocks/region/messages.js +131 -0
  169. package/dist/blocks/region/normalize.d.ts +7 -0
  170. package/dist/blocks/region/normalize.js +170 -0
  171. package/dist/blocks/region/region.extension.d.ts +58 -0
  172. package/dist/blocks/region/region.extension.js +119 -0
  173. package/dist/blocks/region/region.svelte.d.ts +221 -0
  174. package/dist/blocks/region/region.svelte.js +582 -0
  175. package/dist/blocks/region/regions.svelte.d.ts +141 -0
  176. package/dist/blocks/region/regions.svelte.js +276 -0
  177. package/dist/blocks/region/serialize.d.ts +57 -0
  178. package/dist/blocks/region/serialize.js +112 -0
  179. package/dist/blocks/region/types.d.ts +255 -0
  180. package/dist/blocks/region/types.js +204 -0
  181. package/dist/blocks/selection.svelte.d.ts +106 -8
  182. package/dist/blocks/selection.svelte.js +632 -74
  183. package/dist/blocks/subthread.d.ts +80 -0
  184. package/dist/blocks/subthread.js +167 -0
  185. package/dist/blocks/text-block.svelte.d.ts +70 -0
  186. package/dist/blocks/text-block.svelte.js +108 -0
  187. package/dist/blocks/toggle/Toggle.svelte +73 -0
  188. package/dist/blocks/toggle/Toggle.svelte.d.ts +11 -0
  189. package/dist/blocks/toggle/index.d.ts +3 -0
  190. package/dist/blocks/toggle/index.js +3 -0
  191. package/dist/blocks/toggle/markdown.d.ts +20 -0
  192. package/dist/blocks/toggle/markdown.js +112 -0
  193. package/dist/blocks/toggle/toggle.extension.d.ts +4 -0
  194. package/dist/blocks/toggle/toggle.extension.js +18 -0
  195. package/dist/blocks/toggle/toggle.svelte.d.ts +135 -0
  196. package/dist/blocks/toggle/toggle.svelte.js +352 -0
  197. package/dist/blocks/undo-selection.d.ts +175 -0
  198. package/dist/blocks/undo-selection.js +360 -0
  199. package/dist/blocks/video/Video.svelte +42 -0
  200. package/dist/blocks/video/Video.svelte.d.ts +11 -0
  201. package/dist/blocks/video/index.d.ts +3 -0
  202. package/dist/blocks/video/index.js +3 -0
  203. package/dist/blocks/video/video.extension.d.ts +4 -0
  204. package/dist/blocks/video/video.extension.js +27 -0
  205. package/dist/blocks/video/video.svelte.d.ts +29 -0
  206. package/dist/blocks/video/video.svelte.js +80 -0
  207. package/dist/extend.d.ts +55 -0
  208. package/dist/extend.js +88 -0
  209. package/dist/files/assets.clipboard.d.ts +77 -0
  210. package/dist/files/assets.clipboard.js +152 -0
  211. package/dist/files/files.svelte.d.ts +363 -0
  212. package/dist/files/files.svelte.js +897 -0
  213. package/dist/files/index.d.ts +3 -0
  214. package/dist/files/index.js +3 -0
  215. package/dist/files/outbox.d.ts +69 -0
  216. package/dist/files/outbox.js +48 -0
  217. package/dist/files/status.d.ts +65 -0
  218. package/dist/files/status.js +87 -0
  219. package/dist/files/store.d.ts +97 -0
  220. package/dist/files/store.js +65 -0
  221. package/dist/files/uploads.svelte.d.ts +174 -0
  222. package/dist/files/uploads.svelte.js +504 -0
  223. package/dist/index.d.ts +68 -6
  224. package/dist/index.js +117 -7
  225. package/dist/nabu-theme.css +112 -0
  226. package/dist/paste/html.paste.d.ts +74 -3
  227. package/dist/paste/html.paste.js +555 -83
  228. package/dist/paste/markdown-inline.d.ts +86 -0
  229. package/dist/paste/markdown-inline.js +528 -0
  230. package/dist/paste/markdown.paste.d.ts +2 -8
  231. package/dist/paste/markdown.paste.js +78 -94
  232. package/dist/paste/nabu.paste.d.ts +3 -0
  233. package/dist/paste/nabu.paste.js +29 -3
  234. package/dist/paste/plain-text.paste.d.ts +2 -2
  235. package/dist/paste/plain-text.paste.js +3 -3
  236. package/dist/persistence/asset-store.d.ts +51 -0
  237. package/dist/persistence/asset-store.js +103 -0
  238. package/dist/persistence/autosave.d.ts +53 -0
  239. package/dist/persistence/autosave.js +62 -0
  240. package/dist/persistence/compact.d.ts +162 -0
  241. package/dist/persistence/compact.js +141 -0
  242. package/dist/persistence/history.d.ts +77 -0
  243. package/dist/persistence/history.js +176 -0
  244. package/dist/persistence/indexeddb.d.ts +190 -4
  245. package/dist/persistence/indexeddb.js +333 -29
  246. package/dist/persistence/merge.d.ts +38 -0
  247. package/dist/persistence/merge.js +36 -0
  248. package/dist/persistence/outbox.d.ts +25 -0
  249. package/dist/persistence/outbox.js +40 -0
  250. package/dist/persistence/persistence.d.ts +70 -9
  251. package/dist/persistence/persistence.js +507 -35
  252. package/dist/persistence/recovery.d.ts +79 -0
  253. package/dist/persistence/recovery.js +79 -0
  254. package/dist/persistence/release.d.ts +34 -0
  255. package/dist/persistence/release.js +64 -0
  256. package/dist/persistence/state.svelte.d.ts +53 -0
  257. package/dist/persistence/state.svelte.js +48 -0
  258. package/dist/persistence/sync.d.ts +39 -0
  259. package/dist/persistence/sync.js +138 -0
  260. package/dist/persistence/tail.d.ts +42 -0
  261. package/dist/persistence/tail.js +103 -0
  262. package/dist/presets.d.ts +29 -24
  263. package/dist/presets.js +51 -14
  264. package/dist/themes/atelier.css +318 -0
  265. package/dist/ui/CommandChooser.svelte +153 -0
  266. package/dist/ui/CommandChooser.svelte.d.ts +13 -0
  267. package/dist/ui/CommandInvite.svelte +99 -0
  268. package/dist/ui/CommandInvite.svelte.d.ts +12 -0
  269. package/dist/ui/DragHandle.svelte +217 -0
  270. package/dist/ui/DragHandle.svelte.d.ts +11 -0
  271. package/dist/ui/FloatingToolbar.svelte +165 -0
  272. package/dist/ui/FloatingToolbar.svelte.d.ts +27 -0
  273. package/dist/ui/SlashMenu.svelte +215 -0
  274. package/dist/ui/SlashMenu.svelte.d.ts +33 -0
  275. package/dist/ui/anchor.d.ts +112 -0
  276. package/dist/ui/anchor.js +136 -0
  277. package/dist/ui/index.d.ts +10 -0
  278. package/dist/ui/index.js +15 -0
  279. package/dist/ui/slash-menu.svelte.d.ts +112 -0
  280. package/dist/ui/slash-menu.svelte.js +150 -0
  281. package/dist/utils/command-confirm.svelte.d.ts +349 -0
  282. package/dist/utils/command-confirm.svelte.js +673 -0
  283. package/dist/utils/commands.d.ts +328 -0
  284. package/dist/utils/commands.js +416 -0
  285. package/dist/utils/dom-scope.d.ts +14 -0
  286. package/dist/utils/dom-scope.js +20 -0
  287. package/dist/utils/drop.d.ts +160 -0
  288. package/dist/utils/drop.js +305 -0
  289. package/dist/utils/extensions.d.ts +488 -40
  290. package/dist/utils/extensions.js +300 -19
  291. package/dist/utils/html.d.ts +44 -0
  292. package/dist/utils/html.js +183 -0
  293. package/dist/utils/index.d.ts +4 -0
  294. package/dist/utils/index.js +4 -0
  295. package/dist/utils/keys.d.ts +120 -0
  296. package/dist/utils/keys.js +248 -0
  297. package/dist/utils/links.d.ts +22 -0
  298. package/dist/utils/links.js +34 -0
  299. package/dist/utils/placeholders.d.ts +88 -0
  300. package/dist/utils/placeholders.js +91 -0
  301. package/dist/utils/selection.svelte.d.ts +47 -19
  302. package/dist/utils/selection.svelte.js +60 -10
  303. package/dist/utils/spellcheck.d.ts +9 -0
  304. package/dist/utils/spellcheck.js +17 -0
  305. package/dist/utils/tab.d.ts +31 -0
  306. package/dist/utils/tab.js +117 -0
  307. package/dist/utils/words.d.ts +7 -0
  308. package/dist/utils/words.js +62 -0
  309. package/dist/vite.d.ts +22 -0
  310. package/dist/vite.js +34 -0
  311. package/package.json +61 -22
  312. package/dist/blocks/zone/Zone.svelte +0 -258
  313. package/dist/blocks/zone/Zone.svelte.d.ts +0 -11
  314. package/dist/blocks/zone/index.d.ts +0 -2
  315. package/dist/blocks/zone/index.js +0 -2
  316. package/dist/blocks/zone/zone.extension.d.ts +0 -2
  317. package/dist/blocks/zone/zone.extension.js +0 -221
  318. package/dist/blocks/zone/zone.svelte.d.ts +0 -112
  319. package/dist/blocks/zone/zone.svelte.js +0 -321
@@ -0,0 +1,277 @@
1
+ /**
2
+ * The commands of the regions extension (ADR 016 §3.4, ADR 018 §3.5 / §3.9; ADR 017). Each is also an action of the bus:
3
+ * `nabu.exec('region:split')`. Default keys are few on purpose, the arrow-key chords being mostly taken by the OS / browser:
4
+ *
5
+ * | id | default keys | confirmation | what |
6
+ * |-------------------------|--------------------------------|--------------|---------------------------------------------------------------|
7
+ * | `region:split` | `Mod-Enter` | none | cut here: a new region of the same type takes the rest |
8
+ * | `region:merge-previous` | `Backspace`, `Shift-Backspace` | hold | at the start of a region: join the previous one / dissolve |
9
+ * | `region:merge-next` | `Delete` | hold | at the end of a region: the same for the NEXT region |
10
+ * | `region:exit` | `Mod-Shift-Enter` | none | a paragraph right after the region, one level up |
11
+ * | `region:demote` | `Tab` (in a title only) | none | becomes the last sub-region of the previous sibling |
12
+ * | `region:promote` | `Shift-Tab` (in a title only) | none | leaves its parent; the following siblings follow it |
13
+ * | `region:unwrap` | - | - | the title becomes a paragraph, the body stays |
14
+ * | `region:insert` | - | - | `{ type?, at?: 'after' \| 'before' \| 'inside', index?, title?, fields? }` |
15
+ * | `region:create` | - (`:::` + Space) | choose | `{ type? }`: a region where the caret is (see create.js) |
16
+ * | `region:close` | - (`:::` + Enter) | choose | `{ region? }`: end the region here (see close.js) |
17
+ * | `region:move` | - | - | `{ direction: 'up' \| 'down' }` |
18
+ * | `region:move-up`/`move-down` | - | - | the same, direction built in (what a `keymap` binds) |
19
+ * | `region:next`/`prev`/`parent` | - | - | caret to the first text position of the target region's body |
20
+ *
21
+ * `exec` runs a command directly (no confirmation: the hold is a KEYBOARD concern, ADR 017 §3.4a). `exec` data can carry
22
+ * `{ region }` (a `Region`, its block or its id) to act on that region instead of the one holding the caret. For
23
+ * `region:merge-next` it is the region that absorbs: its NEXT SIBLING region is merged (or dissolved) into it.
24
+ * Integrators bind the keyless commands with the `keymap` option of the editor.
25
+ *
26
+ * @import { Nabu } from "../nabu.svelte.js";
27
+ * @import { CommandDef, CommandContext } from "../../utils/commands.js";
28
+ * @import { Region } from "./regions.svelte.js";
29
+ */
30
+
31
+ import { Regions } from "./regions.svelte.js";
32
+ import { createMessages } from "./messages.js";
33
+ import { mergePlan, neighbourOf } from "./facts.js";
34
+ import { createOptions, createRegionHere, placeOf } from "./create.js";
35
+ import { closeChain, closeOptions, closeRegionAt } from "./close.js";
36
+ import { demoteRegion, exitRegion, goToRegion, insertNearRegion, mergeRegion, moveRegion, promoteRegion, splitRegion, toRegion, unwrapRegion } from "./actions.js";
37
+
38
+ /** @param {Region | null | undefined} region @returns {string} the title, else the label, else the type: never empty */
39
+ const nameOf = (region) => region?.title || region?.label || region?.type || '';
40
+
41
+ /** Whether `data` names a region to act on. @param {any} data */
42
+ const givesRegion = (data) => !!data && typeof data === 'object' && 'region' in data;
43
+
44
+ /**
45
+ * The region a command acts on: `data.region` when given, else the one holding the caret.
46
+ * @param {CommandContext & Record<string, any>} ctx @param {any} data
47
+ * @returns {Region | null}
48
+ */
49
+ function targetRegion(ctx, data) {
50
+ return givesRegion(data) ? toRegion(ctx.nabu, data.region) : (ctx.region ?? null);
51
+ }
52
+
53
+ /**
54
+ * The region `region:merge-next` merges: with `data.region` the next SIBLING region of that one; else the region whose title
55
+ * is right after the caret (the `nextBoundaryRegion` fact), else the next sibling region of the caret's region.
56
+ * @param {CommandContext & Record<string, any>} ctx @param {any} data
57
+ * @returns {Region | null}
58
+ */
59
+ function nextMergeTarget(ctx, data) {
60
+ const base = givesRegion(data) ? toRegion(ctx.nabu, data.region) : null;
61
+ if (givesRegion(data) && !base) return null;
62
+ if (!base && ctx.nextBoundaryRegion) return ctx.nextBoundaryRegion;
63
+ const from = base ?? ctx.region;
64
+ const sibling = from ? neighbourOf(ctx.nabu, from.block, 1) : null;
65
+ return sibling?.type === 'region' ? Regions.get(ctx.nabu).of(sibling) : null;
66
+ }
67
+
68
+ /**
69
+ * The caret to put back after a structural command (null when there is no text caret).
70
+ * @param {CommandContext} ctx
71
+ * @returns {{ block: import("../block.svelte.js").Block, offset: number } | null}
72
+ */
73
+ function caretOf(ctx) {
74
+ const block = ctx.block;
75
+ const sel = block?.selection;
76
+ return block && sel && sel.from === sel.to ? { block, offset: sel.to } : null;
77
+ }
78
+
79
+ /**
80
+ * @param {import("./messages.js").RegionMessages | undefined} messages
81
+ * @returns {Record<string, CommandDef>}
82
+ */
83
+ export function createRegionCommands(messages) {
84
+ const text = createMessages(messages);
85
+ /** @param {Region | null | undefined} r */
86
+ const params = (r) => ({ title: nameOf(r), label: r?.label ?? '', type: r?.type ?? '' });
87
+
88
+ /**
89
+ * The label of a protected merge of `region`: it names the region it joins, or the region that is dissolved.
90
+ * @param {Nabu} nabu @param {Region | null | undefined} region @param {'mergePrevious' | 'mergeNext'} joined
91
+ */
92
+ const mergeLabel = (nabu, region, joined) => {
93
+ const plan = region ? mergePlan(nabu, region) : null;
94
+ // No caret (`commands.list()` without a context), or nothing to merge with: no name to give, a generic label.
95
+ if (!region || !plan) return text(joined === 'mergePrevious' ? 'mergePreviousGeneric' : 'mergeNextGeneric');
96
+ if (plan.kind === 'merge') return joined === 'mergePrevious' ? text('mergePrevious', params(plan.into)) : text('mergeNext', params(region));
97
+ return text('dissolve', params(region));
98
+ };
99
+
100
+ return {
101
+ 'region:split': {
102
+ label: () => text('split'),
103
+ keys: ['Mod-Enter'],
104
+ when: (/** @type {any} */ ctx) => ctx.isCollapsed && ctx.inBodyTextBlock && ctx.block.splitsOnEnter !== false,
105
+ run: (nabu, ctx) => splitRegion(nabu, ctx),
106
+ },
107
+
108
+ 'region:insert': {
109
+ label: (/** @type {any} */ ctx) => text('insert', { type: ctx.region?.type ?? '' }),
110
+ run: (nabu, ctx, data) => insertNearRegion(nabu, targetRegion(/** @type {any} */ (ctx), data), data ?? {}, ctx.block),
111
+ },
112
+
113
+ // Typing `:::` + Space in an empty paragraph asks for the type through the `choose` strategy (the hook of the extension offers
114
+ // it: a typed trigger, no key to bind). `exec` runs it directly with `{ type }`, without asking.
115
+ 'region:create': {
116
+ label: () => text('create'),
117
+ when: (/** @type {any} */ ctx) => !!placeOf(ctx.nabu, ctx.block),
118
+ confirm: {
119
+ strategy: 'choose',
120
+ options: (/** @type {any} */ ctx) => {
121
+ const place = placeOf(ctx.nabu, ctx.block);
122
+ if (!place) return [];
123
+ return createOptions(ctx.nabu, ctx.block, place.parent, text);
124
+ },
125
+ footer: () => text('chooserFooter'),
126
+ messages: { choose: (/** @type {{ options: unknown[] }} */ p) => text('chooser', { count: p.options.length }) },
127
+ },
128
+ run: (nabu, ctx, data) => createRegionHere(nabu, ctx.block, typeof data?.type === 'string' ? data.type : undefined),
129
+ },
130
+
131
+ // Typing `:::` + Enter in a paragraph of a region body closes a region (the hook of the extension runs it at once when only one
132
+ // region holds the caret, else offers the chooser; a typed trigger, no key to bind). `exec` runs it directly: `{ region }` (a
133
+ // region holding the caret block) closes that one, without it the innermost.
134
+ 'region:close': {
135
+ label: () => text('closeCommand'),
136
+ when: (/** @type {any} */ ctx) => closeChain(ctx.nabu, ctx.block).length > 0,
137
+ confirm: {
138
+ strategy: 'choose',
139
+ options: (/** @type {any} */ ctx) => closeOptions(closeChain(ctx.nabu, ctx.block), text),
140
+ footer: () => text('closeFooter'),
141
+ messages: { choose: (/** @type {{ options: unknown[] }} */ p) => text('closeChooser', { count: p.options.length }) },
142
+ },
143
+ run: (nabu, ctx, data) => {
144
+ const chain = closeChain(nabu, ctx.block);
145
+ const wanted = givesRegion(data) ? toRegion(nabu, data.region) : chain[0];
146
+ if (!ctx.block || !wanted) return false;
147
+ return closeRegionAt(nabu, wanted, ctx.block);
148
+ },
149
+ },
150
+
151
+ 'region:merge-previous': {
152
+ label: (/** @type {any} */ ctx) => mergeLabel(ctx.nabu, ctx.region, 'mergePrevious'),
153
+ keys: ['Backspace', 'Shift-Backspace'],
154
+ when: (/** @type {any} */ ctx) => ctx.isCollapsed && ctx.atStartOfRegion && !!ctx.region && !!mergePlan(ctx.nabu, ctx.region),
155
+ confirm: {
156
+ strategy: 'hold',
157
+ target: (/** @type {any} */ ctx) => ctx.region?.block,
158
+ messages: { hold: (/** @type {{ keys: string, label: string }} */ p) => text('hold', p) },
159
+ },
160
+ run: (nabu, ctx, data) => {
161
+ const region = targetRegion(/** @type {any} */ (ctx), data);
162
+ return region ? mergeRegion(nabu, region, caretOf(ctx)) : false;
163
+ },
164
+ },
165
+
166
+ 'region:merge-next': {
167
+ label: (/** @type {any} */ ctx) => mergeLabel(ctx.nabu, ctx.nextBoundaryRegion, 'mergeNext'),
168
+ keys: ['Delete'],
169
+ when: (/** @type {any} */ ctx) => ctx.isCollapsed && ctx.atEndOfRegion && !!ctx.nextBoundaryRegion && !!mergePlan(ctx.nabu, ctx.nextBoundaryRegion),
170
+ confirm: {
171
+ strategy: 'hold',
172
+ target: (/** @type {any} */ ctx) => ctx.nextBoundaryRegion?.block,
173
+ messages: { hold: (/** @type {{ keys: string, label: string }} */ p) => text('hold', p) },
174
+ },
175
+ run: (nabu, ctx, data) => {
176
+ const next = nextMergeTarget(/** @type {any} */ (ctx), data);
177
+ return next ? mergeRegion(nabu, next, caretOf(ctx)) : false;
178
+ },
179
+ },
180
+
181
+ 'region:exit': {
182
+ label: () => text('exit'),
183
+ keys: ['Mod-Shift-Enter'],
184
+ when: (/** @type {any} */ ctx) => ctx.isCollapsed && !!ctx.region,
185
+ run: (nabu, ctx, data) => {
186
+ const region = targetRegion(/** @type {any} */ (ctx), data);
187
+ return region ? exitRegion(nabu, region, ctx.block) : false;
188
+ },
189
+ },
190
+
191
+ 'region:demote': {
192
+ label: () => text('demote'),
193
+ keys: ['Tab'],
194
+ when: (/** @type {any} */ ctx) => ctx.inTitle,
195
+ run: (nabu, ctx, data) => {
196
+ const region = targetRegion(/** @type {any} */ (ctx), data);
197
+ return region ? demoteRegion(nabu, region, caretOf(ctx)) : false;
198
+ },
199
+ },
200
+
201
+ 'region:promote': {
202
+ label: () => text('promote'),
203
+ keys: ['Shift-Tab'],
204
+ when: (/** @type {any} */ ctx) => ctx.inTitle,
205
+ run: (nabu, ctx, data) => {
206
+ const region = targetRegion(/** @type {any} */ (ctx), data);
207
+ return region ? promoteRegion(nabu, region, caretOf(ctx)) : false;
208
+ },
209
+ },
210
+
211
+ 'region:unwrap': {
212
+ label: () => text('unwrap'),
213
+ when: (/** @type {any} */ ctx) => !!ctx.region,
214
+ run: (nabu, ctx, data) => {
215
+ const region = targetRegion(/** @type {any} */ (ctx), data);
216
+ return region ? unwrapRegion(nabu, region, caretOf(ctx)) : false;
217
+ },
218
+ },
219
+
220
+ 'region:move': {
221
+ label: () => text('move'),
222
+ when: (/** @type {any} */ ctx) => !!ctx.region,
223
+ run: (nabu, ctx, data) => {
224
+ const direction = data?.direction;
225
+ if (direction !== 'up' && direction !== 'down') { nabu.warn('region:move: data.direction must be "up" or "down"'); return false; }
226
+ const region = targetRegion(/** @type {any} */ (ctx), data);
227
+ return region ? moveRegion(nabu, region, direction, caretOf(ctx)) : false;
228
+ },
229
+ },
230
+
231
+ // The same move with the direction built in: a key cannot carry `data`, so these are what a `keymap` binds.
232
+ 'region:move-up': {
233
+ label: () => text('moveUp'),
234
+ when: (/** @type {any} */ ctx) => !!ctx.region,
235
+ run: (nabu, ctx, data) => {
236
+ const region = targetRegion(/** @type {any} */ (ctx), data);
237
+ return region ? moveRegion(nabu, region, 'up', caretOf(ctx)) : false;
238
+ },
239
+ },
240
+
241
+ 'region:move-down': {
242
+ label: () => text('moveDown'),
243
+ when: (/** @type {any} */ ctx) => !!ctx.region,
244
+ run: (nabu, ctx, data) => {
245
+ const region = targetRegion(/** @type {any} */ (ctx), data);
246
+ return region ? moveRegion(nabu, region, 'down', caretOf(ctx)) : false;
247
+ },
248
+ },
249
+
250
+ 'region:next': {
251
+ label: () => text('next'),
252
+ run: (nabu, ctx, data) => {
253
+ const regions = Regions.get(nabu);
254
+ const base = givesRegion(data) ? toRegion(nabu, data.region) : null;
255
+ const target = givesRegion(data) ? (base ? regions.next(base) : null) : /** @type {any} */ (ctx).nextRegion;
256
+ return target ? goToRegion(nabu, target) : false;
257
+ },
258
+ },
259
+
260
+ 'region:prev': {
261
+ label: () => text('prev'),
262
+ run: (nabu, ctx, data) => {
263
+ const base = targetRegion(/** @type {any} */ (ctx), data);
264
+ const target = base ? Regions.get(nabu).prev(base) : null;
265
+ return target ? goToRegion(nabu, target) : false;
266
+ },
267
+ },
268
+
269
+ 'region:parent': {
270
+ label: () => text('parent'),
271
+ run: (nabu, ctx, data) => {
272
+ const parent = targetRegion(/** @type {any} */ (ctx), data)?.parent;
273
+ return parent ? goToRegion(nabu, parent) : false;
274
+ },
275
+ },
276
+ };
277
+ }
@@ -0,0 +1,72 @@
1
+ /**
2
+ * Where a region could be created from `block`: `{ parent }` (the region whose body holds it, `null` for the root), or `null`
3
+ * when it is nowhere (no block, a title, a block nested in a list / toggle / callout…).
4
+ * @param {Nabu} nabu @param {Block | null | undefined} block
5
+ * @returns {{ parent: Region | null } | null}
6
+ */
7
+ export function placeOf(nabu: Nabu, block: Block | null | undefined): {
8
+ parent: Region | null;
9
+ } | null;
10
+ /**
11
+ * The declared types by what they can do at a place (see the module doc).
12
+ * @param {Nabu} nabu @param {Region | null} parent the region whose body is the place, `null` for the root
13
+ * @returns {{ here: RegionTypeDef[], next: RegionTypeDef[] }}
14
+ */
15
+ export function regionChoices(nabu: Nabu, parent: Region | null): {
16
+ here: RegionTypeDef[];
17
+ next: RegionTypeDef[];
18
+ };
19
+ /**
20
+ * The type `region:create` uses without `type`: at the root the default (top-level) type; in a region the shallowest type that
21
+ * fits inside (first declared of its level), else, in a region of the deepest level, the type of the region itself (a sibling).
22
+ * @param {Nabu} nabu @param {Region | null} parent
23
+ * @returns {string}
24
+ */
25
+ export function defaultTypeAt(nabu: Nabu, parent: Region | null): string;
26
+ /**
27
+ * The region after which a `next` type (not nestable in `parent`) is created as a sibling: the nearest ancestor region (`parent`
28
+ * included) whose own parent accepts the type (the root, or a region of a shallower level).
29
+ * @param {Region} parent @param {RegionTypeDef} def
30
+ * @returns {Region}
31
+ */
32
+ export function siblingAnchor(parent: Region, def: RegionTypeDef): Region;
33
+ /**
34
+ * The numbering prefix a region of type `def` would get if it were created from `block` inside `parent` (`null` = the root): the
35
+ * label template of the type applied with an empty title, with the index the registry's numbering rule would give it ("2.1", "3.",
36
+ * "Chapter 4"). Empty when the type has nothing to show (unnumbered, label made of the title). Pure: reads the tree, changes nothing.
37
+ *
38
+ * A nestable type goes into the body of `parent`, at the caret block; any other becomes the sibling of {@link siblingAnchor}, and
39
+ * takes everything that follows the caret, so in both cases it starts at the caret in document order.
40
+ * @param {Nabu} nabu @param {RegionTypeDef} def @param {Block} block the caret block @param {Region | null} parent
41
+ * @returns {string}
42
+ */
43
+ export function previewPrefix(nabu: Nabu, def: RegionTypeDef, block: Block, parent: Region | null): string;
44
+ /**
45
+ * The options of the `region:create` chooser at a place, in two groups. "Here" holds the types created inside the body (every type
46
+ * at the root); "Next" the sibling entries, the current region's own type first (the commonest intent). Each option carries the
47
+ * prefix the new region would get as its `detail`. A group with no entry is absent (so there is no "Next" at the root).
48
+ * @param {Nabu} nabu @param {Block} block the caret block @param {Region | null} parent
49
+ * @param {ReturnType<typeof createMessages>} text the region messages (`createMessages`)
50
+ * @returns {Array<{ id: string, type: string, label: string, group: string, detail: string }>}
51
+ */
52
+ export function createOptions(nabu: Nabu, block: Block, parent: Region | null, text: ReturnType<typeof createMessages>): Array<{
53
+ id: string;
54
+ type: string;
55
+ label: string;
56
+ group: string;
57
+ detail: string;
58
+ }>;
59
+ /**
60
+ * Creates a region of `type` at the caret block (see the module doc). Resolves to false (nothing changed) when there is no
61
+ * place for a region or the type is not declared.
62
+ * @param {Nabu} nabu
63
+ * @param {Block | null | undefined} block the block holding the caret
64
+ * @param {string} [type] default: {@link defaultTypeAt}
65
+ * @returns {Promise<boolean>}
66
+ */
67
+ export function createRegionHere(nabu: Nabu, block: Block | null | undefined, type?: string): Promise<boolean>;
68
+ import type { Nabu } from "../nabu.svelte.js";
69
+ import type { Block } from "../block.svelte.js";
70
+ import type { Region } from "./regions.svelte.js";
71
+ import type { RegionTypeDef } from "./types.js";
72
+ import type { createMessages } from "./messages.js";
@@ -0,0 +1,208 @@
1
+ /**
2
+ * Creating a region where the caret is (ADR 018 §3.5, "Créer une région"): the operation behind `region:create`, the `:::` +
3
+ * Space chooser and the slash-menu entries.
4
+ *
5
+ * WHERE a region may be created. The caret block must be a direct child of the root or of a region body (not in a list, a
6
+ * toggle, a callout, not a title). The place is the BODY of its parent region (`null` = the root). The nesting rule (a
7
+ * declared sub-region is strictly deeper than its region parent) splits the declared types in two, each in declaration order:
8
+ *
9
+ * - `here` the types that fit INSIDE this body: every type at the root, else the types of a level strictly deeper than the
10
+ * region (so a region of the deepest level offers none);
11
+ * - `next` the other types (a level equal to or higher than the region, root excluded): a new region of such a type cannot
12
+ * be a child of the current one. It is created as a SIBLING after the nearest ancestor region (the current one
13
+ * included) whose parent accepts it (the root, or a shallower region). Like `region:split`, it takes what follows
14
+ * the caret in document order inside that ancestor, so nothing is reordered: the following siblings of the caret
15
+ * block, then those of each region up to (excluding) that ancestor.
16
+ *
17
+ * `region:create { type }` accepts any declared type: a `here` type nests, a `next` type is the sibling. The caret block is
18
+ * REPLACED by the region when it is an empty default paragraph (also `:::` / `::: `, the typed trigger); any other block stays
19
+ * and the region follows it. A region that takes nothing gets one empty default paragraph. One undo step; the caret lands in
20
+ * the title.
21
+ *
22
+ * The chooser (`createOptions`) shows the two lists as two GROUPS ("Here", "Next"), each option with the numbering prefix the new
23
+ * region would get (`previewPrefix`, computed from the registry's numbering rules as if it were created there).
24
+ *
25
+ * @import { Nabu } from "../nabu.svelte.js";
26
+ * @import { Block } from "../block.svelte.js";
27
+ * @import { Region } from "./regions.svelte.js";
28
+ * @import { RegionTypeDef } from "./types.js";
29
+ * @import { createMessages } from "./messages.js";
30
+ */
31
+
32
+ import { tick } from "svelte";
33
+ import { Regions } from "./regions.svelte.js";
34
+ import { createRegion } from "./insert.js";
35
+ import { createDefaultBlock, defaultType, kids, parentIdOf } from "./actions.js";
36
+ import { siblingsOf } from "./facts.js";
37
+ import { formatLabel } from "./types.js";
38
+
39
+ /**
40
+ * Where a region could be created from `block`: `{ parent }` (the region whose body holds it, `null` for the root), or `null`
41
+ * when it is nowhere (no block, a title, a block nested in a list / toggle / callout…).
42
+ * @param {Nabu} nabu @param {Block | null | undefined} block
43
+ * @returns {{ parent: Region | null } | null}
44
+ */
45
+ export function placeOf(nabu, block) {
46
+ if (!block || block.type === 'region') return null;
47
+ if (!block.parent) return { parent: null };
48
+ if (block.parent.type !== 'region') return null;
49
+ const parent = Regions.get(nabu).of(block.parent);
50
+ return parent ? { parent } : null;
51
+ }
52
+
53
+ /**
54
+ * The declared types by what they can do at a place (see the module doc).
55
+ * @param {Nabu} nabu @param {Region | null} parent the region whose body is the place, `null` for the root
56
+ * @returns {{ here: RegionTypeDef[], next: RegionTypeDef[] }}
57
+ */
58
+ export function regionChoices(nabu, parent) {
59
+ const types = [...Regions.get(nabu).config.types.values()];
60
+ if (!parent) return { here: types, next: [] };
61
+ return { here: types.filter((def) => def.level > parent.level), next: types.filter((def) => def.level <= parent.level) };
62
+ }
63
+
64
+ /**
65
+ * The type `region:create` uses without `type`: at the root the default (top-level) type; in a region the shallowest type that
66
+ * fits inside (first declared of its level), else, in a region of the deepest level, the type of the region itself (a sibling).
67
+ * @param {Nabu} nabu @param {Region | null} parent
68
+ * @returns {string}
69
+ */
70
+ export function defaultTypeAt(nabu, parent) {
71
+ const regions = Regions.get(nabu);
72
+ if (!parent) return regions.config.defaultType;
73
+ const { here, next } = regionChoices(nabu, parent);
74
+ const pick = (/** @type {RegionTypeDef[]} */ list, /** @type {1 | -1} */ sign) => list.reduce((/** @type {RegionTypeDef | null} */ best, def) => (!best || sign * def.level < sign * best.level ? def : best), null);
75
+ return (pick(here, 1) ?? pick(next, -1))?.id ?? regions.config.defaultType;
76
+ }
77
+
78
+ /**
79
+ * The region after which a `next` type (not nestable in `parent`) is created as a sibling: the nearest ancestor region (`parent`
80
+ * included) whose own parent accepts the type (the root, or a region of a shallower level).
81
+ * @param {Region} parent @param {RegionTypeDef} def
82
+ * @returns {Region}
83
+ */
84
+ export function siblingAnchor(parent, def) {
85
+ let ancestor = parent;
86
+ while (ancestor.parent && ancestor.parent.level >= def.level) ancestor = ancestor.parent;
87
+ return ancestor;
88
+ }
89
+
90
+ /** The position of a block in the document, as the indexes from the root down (ancestors are prefixes). @param {Nabu} nabu @param {Block} block @returns {number[]} */
91
+ function docPath(nabu, block) {
92
+ /** @type {number[]} */ const out = [];
93
+ /** @type {Block | null} */ let b = block;
94
+ while (b) { out.unshift(siblingsOf(nabu, b).indexOf(b)); b = /** @type {Block | null} */ (b.parent); }
95
+ return out;
96
+ }
97
+
98
+ /** Whether `a` is before `b` in document order, an ancestor of `b` included. @param {number[]} a @param {number[]} b */
99
+ function startsBefore(a, b) {
100
+ for (let i = 0; i < Math.min(a.length, b.length); i++) if (a[i] !== b[i]) return a[i] < b[i];
101
+ return a.length <= b.length;
102
+ }
103
+
104
+ /**
105
+ * The numbering prefix a region of type `def` would get if it were created from `block` inside `parent` (`null` = the root): the
106
+ * label template of the type applied with an empty title, with the index the registry's numbering rule would give it ("2.1", "3.",
107
+ * "Chapter 4"). Empty when the type has nothing to show (unnumbered, label made of the title). Pure: reads the tree, changes nothing.
108
+ *
109
+ * A nestable type goes into the body of `parent`, at the caret block; any other becomes the sibling of {@link siblingAnchor}, and
110
+ * takes everything that follows the caret, so in both cases it starts at the caret in document order.
111
+ * @param {Nabu} nabu @param {RegionTypeDef} def @param {Block} block the caret block @param {Region | null} parent
112
+ * @returns {string}
113
+ */
114
+ export function previewPrefix(nabu, def, block, parent) {
115
+ const nested = !parent || def.level > parent.level;
116
+ const anchor = nested ? block : siblingAnchor(/** @type {Region} */ (parent), def).block;
117
+ const container = nested ? parent : siblingAnchor(/** @type {Region} */ (parent), def).parent;
118
+ /** @type {number | null} */
119
+ let index = null;
120
+ if (def.numbering === 'document') {
121
+ const at = docPath(nabu, block);
122
+ index = 1 + Regions.get(nabu).list.filter((r) => r.type === def.id && startsBefore(docPath(nabu, r.block), at)).length;
123
+ } else if (def.numbering === 'parent') {
124
+ const siblings = siblingsOf(nabu, anchor);
125
+ index = 1 + siblings.slice(0, siblings.indexOf(anchor) + 1).filter((b) => b.type === 'region' && /** @type {any} */ (b).regionType === def.id).length;
126
+ }
127
+ const path = [...(container ? container.path : []), ...(index == null ? [] : [index])].join('.');
128
+ return formatLabel(def.label, { index, path, title: '', type: def.id, level: def.level });
129
+ }
130
+
131
+ /**
132
+ * The options of the `region:create` chooser at a place, in two groups. "Here" holds the types created inside the body (every type
133
+ * at the root); "Next" the sibling entries, the current region's own type first (the commonest intent). Each option carries the
134
+ * prefix the new region would get as its `detail`. A group with no entry is absent (so there is no "Next" at the root).
135
+ * @param {Nabu} nabu @param {Block} block the caret block @param {Region | null} parent
136
+ * @param {ReturnType<typeof createMessages>} text the region messages (`createMessages`)
137
+ * @returns {Array<{ id: string, type: string, label: string, group: string, detail: string }>}
138
+ */
139
+ export function createOptions(nabu, block, parent, text) {
140
+ const { here, next } = regionChoices(nabu, parent);
141
+ const own = parent?.type;
142
+ const ordered = [...next.filter((def) => def.id === own), ...next.filter((def) => def.id !== own)];
143
+ /** @param {RegionTypeDef[]} defs @param {'createOption' | 'createNext'} message @param {string} group */
144
+ const list = (defs, message, group) => defs.map((def) => ({
145
+ id: def.id,
146
+ type: def.id,
147
+ label: text(message, { name: def.name, type: def.id }),
148
+ group,
149
+ detail: previewPrefix(nabu, def, block, parent),
150
+ }));
151
+ return [...list(here, 'createOption', text('groupHere')), ...list(ordered, 'createNext', text('groupNext'))];
152
+ }
153
+
154
+ /** Whether the creation REPLACES `block`: an empty default paragraph, or one holding only the typed `:::` trigger. @param {Nabu} nabu @param {Block} block */
155
+ function isReplaceable(nabu, block) {
156
+ if (block.type !== defaultType(nabu) || /** @type {any} */ (block).children?.length) return false;
157
+ const text = /** @type {any} */ (block.behaviors.get('text'))?.text;
158
+ return text === '' || text === ':::' || text === '::: ';
159
+ }
160
+
161
+ /**
162
+ * Creates a region of `type` at the caret block (see the module doc). Resolves to false (nothing changed) when there is no
163
+ * place for a region or the type is not declared.
164
+ * @param {Nabu} nabu
165
+ * @param {Block | null | undefined} block the block holding the caret
166
+ * @param {string} [type] default: {@link defaultTypeAt}
167
+ * @returns {Promise<boolean>}
168
+ */
169
+ export async function createRegionHere(nabu, block, type) {
170
+ const place = placeOf(nabu, block);
171
+ if (!block || !place) return false;
172
+ const { parent } = place;
173
+ const id = type ?? defaultTypeAt(nabu, parent);
174
+ const def = Regions.get(nabu).config.types.get(id);
175
+ if (!def) { nabu.warn(`region:create: region type "${id}" is not declared`); return false; }
176
+
177
+ const replace = isReplaceable(nabu, block);
178
+ const nested = !parent || def.level > parent.level;
179
+ /** @type {import("./region.svelte.js").RegionBlock | null} */
180
+ let created = null;
181
+
182
+ const mutate = () => {
183
+ if (nested) {
184
+ const index = (block.node.index() ?? 0) + (replace ? 0 : 1);
185
+ created = createRegion(nabu, { type: id, parentId: parent?.block.id ?? null, index });
186
+ createDefaultBlock(nabu, {}, created.id, 0);
187
+ } else {
188
+ const ancestor = siblingAnchor(/** @type {Region} */ (parent), def);
189
+ /** The siblings that follow `node` in its parent. @param {import("../nabu.svelte.js").NabuNode} node */
190
+ const after = (node) => { const all = kids(/** @type {any} */ (node.parent())); return all.slice(all.findIndex((n) => n.id === node.id) + 1); };
191
+ const followers = after(block.node);
192
+ for (let r = /** @type {Region} */ (parent); r !== ancestor; r = /** @type {Region} */ (r.parent)) followers.push(...after(r.block.node));
193
+ created = createRegion(nabu, { type: id, parentId: parentIdOf(ancestor.block.node), index: (ancestor.block.node.index() ?? 0) + 1 });
194
+ if (followers.length) followers.forEach((n, i) => n.move(/** @type {any} */ (created).node, i));
195
+ else createDefaultBlock(nabu, {}, created.id, 0);
196
+ }
197
+ if (replace) nabu.delete(block);
198
+ };
199
+
200
+ // The typed trigger (`::: `) is already in the document: `shortcut` makes the conversion its own undo step, whose undo
201
+ // gives the trigger text back with the caret after it.
202
+ if (block.transformsEnabled !== false && /** @type {any} */ (block.behaviors.get('text'))?.selection) await nabu.shortcut(block, '', mutate);
203
+ else { mutate(); await nabu.commit(); }
204
+
205
+ const fresh = created && nabu.blocks.get(/** @type {any} */ (created).id);
206
+ if (fresh) { await tick(); nabu.selection.setCursor(fresh, 0); }
207
+ return true;
208
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * The end marker of a region (ADR 018 §3.3 "repère de fin", board 9 option B: "only when needed"). When text of the PARENT
3
+ * follows a region (a block that is not a region comes right after it, in the same body or at the root), the interface draws a
4
+ * discreet line after the region's body so that its end is readable: "End of 1.2 · back to 1. The ferryman", or "… · outside
5
+ * regions" at the root. Nothing is drawn when the next sibling is a region (its seam already says so) or when the region is the
6
+ * last child (the parent's own end follows).
7
+ *
8
+ * Pure functions, so that the condition and the text are testable on their own. `Region.svelte` draws the result.
9
+ *
10
+ * @import { Block } from "../block.svelte.js";
11
+ * @import { Region } from "./regions.svelte.js";
12
+ * @import { createMessages } from "./messages.js";
13
+ */
14
+ /**
15
+ * Whether a region block is followed, among `siblings` (the children of its parent, or the root blocks), by something that is NOT a
16
+ * region.
17
+ * @param {ReadonlyArray<{ id: string, type: string }>} siblings
18
+ * @param {{ id: string }} block the region block
19
+ * @returns {boolean}
20
+ */
21
+ export function needsEndMarker(siblings: ReadonlyArray<{
22
+ id: string;
23
+ type: string;
24
+ }>, block: {
25
+ id: string;
26
+ }): boolean;
27
+ /**
28
+ * The text of the end marker of `region`, from the messages (`endOf`, then `backTo` with the parent or `outside`), joined by " · ".
29
+ * @param {ReturnType<typeof createMessages>} text the region messages (`createMessages`)
30
+ * @param {Region} region
31
+ * @returns {string}
32
+ */
33
+ export function endMarkerText(text: ReturnType<typeof createMessages>, region: Region): string;
34
+ import type { createMessages } from "./messages.js";
35
+ import type { Region } from "./regions.svelte.js";
@@ -0,0 +1,51 @@
1
+ /**
2
+ * The end marker of a region (ADR 018 §3.3 "repère de fin", board 9 option B: "only when needed"). When text of the PARENT
3
+ * follows a region (a block that is not a region comes right after it, in the same body or at the root), the interface draws a
4
+ * discreet line after the region's body so that its end is readable: "End of 1.2 · back to 1. The ferryman", or "… · outside
5
+ * regions" at the root. Nothing is drawn when the next sibling is a region (its seam already says so) or when the region is the
6
+ * last child (the parent's own end follows).
7
+ *
8
+ * Pure functions, so that the condition and the text are testable on their own. `Region.svelte` draws the result.
9
+ *
10
+ * @import { Block } from "../block.svelte.js";
11
+ * @import { Region } from "./regions.svelte.js";
12
+ * @import { createMessages } from "./messages.js";
13
+ */
14
+
15
+ /**
16
+ * Whether a region block is followed, among `siblings` (the children of its parent, or the root blocks), by something that is NOT a
17
+ * region.
18
+ * @param {ReadonlyArray<{ id: string, type: string }>} siblings
19
+ * @param {{ id: string }} block the region block
20
+ * @returns {boolean}
21
+ */
22
+ export function needsEndMarker(siblings, block) {
23
+ const index = siblings.findIndex((b) => b.id === block.id);
24
+ const next = index < 0 ? undefined : siblings[index + 1];
25
+ return !!next && next.type !== 'region';
26
+ }
27
+
28
+ /** @param {Pick<Region, 'prefix' | 'title' | 'label' | 'type' | 'def'>} region */
29
+ function params(region) {
30
+ return {
31
+ name: region.prefix || region.title || region.def?.name || region.type,
32
+ prefix: region.prefix,
33
+ title: region.title,
34
+ label: region.label,
35
+ type: region.type,
36
+ };
37
+ }
38
+
39
+ /**
40
+ * The text of the end marker of `region`, from the messages (`endOf`, then `backTo` with the parent or `outside`), joined by " · ".
41
+ * @param {ReturnType<typeof createMessages>} text the region messages (`createMessages`)
42
+ * @param {Region} region
43
+ * @returns {string}
44
+ */
45
+ export function endMarkerText(text, region) {
46
+ const parent = region.parent;
47
+ const second = parent
48
+ ? text('backTo', { ...params(parent), name: parent.label || params(parent).name })
49
+ : text('outside');
50
+ return `${text('endOf', params(region))} · ${second}`;
51
+ }