@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,69 @@
1
+ /**
2
+ * A text block without children directly in the body of a region or at the root: where `region:split` applies.
3
+ * @param {Block | null | undefined} block
4
+ * @returns {boolean}
5
+ */
6
+ export function isBodyTextBlock(block: Block | null | undefined): boolean;
7
+ /** The blocks that share a parent with `block` (the root blocks of the document for a root block). @param {Nabu} nabu @param {Block} block @returns {Block[]} */
8
+ export function siblingsOf(nabu: Nabu, block: Block): Block[];
9
+ /** The sibling node right before / after `block` (any type of block), or null. @param {Nabu} nabu @param {Block} block @param {-1 | 1} [step] */
10
+ export function neighbourOf(nabu: Nabu, block: Block, step?: -1 | 1): Block;
11
+ /**
12
+ * What a protected merge of `region` does (ADR 018 §3.5): the region MERGES INTO its previous sibling node when that one is a
13
+ * region of the SAME level (its body joins the end of that one's body, the title goes away); otherwise it is DISSOLVED in
14
+ * place (its body takes its position in its parent, the title goes away). `null` when there is nothing before it and no
15
+ * parent region (the first region of the document: nothing to merge with).
16
+ * @param {Nabu} nabu @param {Region} region
17
+ * @returns {{ kind: 'merge', into: Region } | { kind: 'dissolve' } | null}
18
+ */
19
+ export function mergePlan(nabu: Nabu, region: Region): {
20
+ kind: "merge";
21
+ into: Region;
22
+ } | {
23
+ kind: "dissolve";
24
+ } | null;
25
+ /**
26
+ * @typedef {Object} RegionFacts
27
+ * @property {Region | null} region
28
+ * @property {Region | null} previousRegion
29
+ * @property {Region | null} nextRegion
30
+ * @property {boolean} inTitle
31
+ * @property {boolean} inBodyTextBlock
32
+ * @property {boolean} atStartOfRegion
33
+ * @property {boolean} atEndOfRegion
34
+ * @property {Region | null} nextBoundaryRegion
35
+ */
36
+ /**
37
+ * `Extension#commandContext` of the regions extension.
38
+ * @param {Nabu} nabu
39
+ * @param {{ block: Block | null, isCollapsed: boolean, inRoot: boolean }} ctx
40
+ * @returns {RegionFacts}
41
+ */
42
+ export function regionFacts(nabu: Nabu, ctx: {
43
+ block: Block | null;
44
+ isCollapsed: boolean;
45
+ inRoot: boolean;
46
+ }): RegionFacts;
47
+ /**
48
+ * Whether a Backspace / Delete merge of two text blocks must be refused (ADR 018 §3.9): one of them is a region block (its
49
+ * TITLE: nothing merges into or out of a title), or they are not in the same region (the merge would cross a seam). This is
50
+ * the `onBeforeMerge` veto: it refuses ANY silent merge through a seam, whatever started it (a key not bound to a command,
51
+ * a virtual keyboard, a block type that handles Backspace itself…).
52
+ * @param {Nabu} nabu @param {Block} survivor @param {Block} victim
53
+ * @returns {boolean}
54
+ */
55
+ export function mergeCrossesRegion(nabu: Nabu, survivor: Block, victim: Block): boolean;
56
+ export function hasText(b: Block | null | undefined): boolean;
57
+ export type RegionFacts = {
58
+ region: Region | null;
59
+ previousRegion: Region | null;
60
+ nextRegion: Region | null;
61
+ inTitle: boolean;
62
+ inBodyTextBlock: boolean;
63
+ atStartOfRegion: boolean;
64
+ atEndOfRegion: boolean;
65
+ nextBoundaryRegion: Region | null;
66
+ };
67
+ import type { Block } from "../block.svelte.js";
68
+ import type { Nabu } from "../nabu.svelte.js";
69
+ import type { Region } from "./regions.svelte.js";
@@ -0,0 +1,152 @@
1
+ /**
2
+ * The FACTS the regions extension adds to the command context (ADR 017 `commandContext`, ADR 018 §3.5). The core builds the
3
+ * context at each key press that has a command bound to it; these facts let the commands' `when` / `label` / `confirm.target`
4
+ * speak about regions without the engine knowing what a region is.
5
+ *
6
+ * - `region` the deepest region holding the caret block (a region block, i.e. a title, is its own region);
7
+ * `null` at the root outside any region / without caret
8
+ * - `previousRegion` the region before `region` in document order (`null` for the first one or at the root)
9
+ * - `nextRegion` the first region whose title lies AFTER the caret block in document order (a sub-region of the
10
+ * caret's own region included)
11
+ * - `inTitle` the caret block IS a region block: the caret is in a title
12
+ * - `inBodyTextBlock` the caret is in a text block that holds no children (a paragraph, a heading, a quote…, not a list item,
13
+ * a toggle, a callout…) and whose parent is a region (its body) or the root of the document
14
+ * - `atStartOfRegion` collapsed caret at offset 0 of the TITLE of `region`, or of the first text-holding block after the
15
+ * title (whatever its type, nested or not: the first item of a list counts) that still belongs to it:
16
+ * no text block lies between the title and the caret. Blocks without text (an image) do not count.
17
+ * - `atEndOfRegion` collapsed caret at the end of its text, and the next text block in document order is the TITLE of a
18
+ * region N whose previous sibling node (same parent) contains the caret block, or IS the caret block:
19
+ * a seam lies right ahead and a Delete would cross it. Examples: the last paragraph of a chapter
20
+ * followed by the next chapter; the last paragraph of a scene that closes a chapter (N = the NEXT
21
+ * chapter, the scene's parent being the previous sibling); a root paragraph right before a region; a
22
+ * paragraph that precedes a sub-region of its own region (N = that sub-region); the end of the title
23
+ * of an empty region (N = the region after it).
24
+ * - `nextBoundaryRegion` that region N (`null` when `atEndOfRegion` is false)
25
+ *
26
+ * @import { Nabu } from "../nabu.svelte.js";
27
+ * @import { Block } from "../block.svelte.js";
28
+ * @import { Region } from "./regions.svelte.js";
29
+ */
30
+
31
+ import { Regions } from "./regions.svelte.js";
32
+
33
+ /** The block can hold a caret in a text (same test the engine's Backspace / Delete use to find their merge partner). @param {Block | null | undefined} b */
34
+ export const hasText = (b) => !!b?.behaviors?.has('text');
35
+
36
+ /** The block holds children (a container, a hybrid such as a list item, a toggle or a region). @param {Block} b */
37
+ const isContainer = (b) => 'children' in b;
38
+
39
+ /**
40
+ * A text block without children directly in the body of a region or at the root: where `region:split` applies.
41
+ * @param {Block | null | undefined} block
42
+ * @returns {boolean}
43
+ */
44
+ export function isBodyTextBlock(block) {
45
+ return !!block && hasText(block) && !isContainer(block) && (!block.parent || block.parent.type === 'region');
46
+ }
47
+
48
+ /** The blocks that share a parent with `block` (the root blocks of the document for a root block). @param {Nabu} nabu @param {Block} block @returns {Block[]} */
49
+ export function siblingsOf(nabu, block) {
50
+ return /** @type {Block[]} */ (block.parent ? /** @type {any} */ (block.parent).children : nabu.children);
51
+ }
52
+
53
+ /** The sibling node right before / after `block` (any type of block), or null. @param {Nabu} nabu @param {Block} block @param {-1 | 1} [step] */
54
+ export function neighbourOf(nabu, block, step = -1) {
55
+ const siblings = siblingsOf(nabu, block);
56
+ return siblings[siblings.indexOf(block) + step] ?? null;
57
+ }
58
+
59
+ /** Whether `ancestor` is `block` or one of its ancestors. @param {Block} ancestor @param {Block} block */
60
+ function holds(ancestor, block) {
61
+ /** @type {Block | null} */
62
+ let current = block;
63
+ while (current) {
64
+ if (current === ancestor) return true;
65
+ current = /** @type {Block | null} */ (current.parent);
66
+ }
67
+ return false;
68
+ }
69
+
70
+ /**
71
+ * What a protected merge of `region` does (ADR 018 §3.5): the region MERGES INTO its previous sibling node when that one is a
72
+ * region of the SAME level (its body joins the end of that one's body, the title goes away); otherwise it is DISSOLVED in
73
+ * place (its body takes its position in its parent, the title goes away). `null` when there is nothing before it and no
74
+ * parent region (the first region of the document: nothing to merge with).
75
+ * @param {Nabu} nabu @param {Region} region
76
+ * @returns {{ kind: 'merge', into: Region } | { kind: 'dissolve' } | null}
77
+ */
78
+ export function mergePlan(nabu, region) {
79
+ const regions = Regions.get(nabu);
80
+ const previous = neighbourOf(nabu, region.block, -1);
81
+ const into = previous?.type === 'region' ? regions.of(previous) : null;
82
+ if (into && into.level === region.level) return { kind: 'merge', into };
83
+ return previous || region.parent ? { kind: 'dissolve' } : null;
84
+ }
85
+
86
+ /**
87
+ * @typedef {Object} RegionFacts
88
+ * @property {Region | null} region
89
+ * @property {Region | null} previousRegion
90
+ * @property {Region | null} nextRegion
91
+ * @property {boolean} inTitle
92
+ * @property {boolean} inBodyTextBlock
93
+ * @property {boolean} atStartOfRegion
94
+ * @property {boolean} atEndOfRegion
95
+ * @property {Region | null} nextBoundaryRegion
96
+ */
97
+
98
+ /**
99
+ * `Extension#commandContext` of the regions extension.
100
+ * @param {Nabu} nabu
101
+ * @param {{ block: Block | null, isCollapsed: boolean, inRoot: boolean }} ctx
102
+ * @returns {RegionFacts}
103
+ */
104
+ export function regionFacts(nabu, ctx) {
105
+ /** @type {RegionFacts} */
106
+ const facts = { region: null, previousRegion: null, nextRegion: null, inTitle: false, inBodyTextBlock: false, atStartOfRegion: false, atEndOfRegion: false, nextBoundaryRegion: null };
107
+ const block = ctx.block;
108
+ if (!block) return facts;
109
+
110
+ const regions = Regions.get(nabu);
111
+ const region = regions.at(block);
112
+ facts.region = region;
113
+ facts.previousRegion = region ? regions.prev(region) : null;
114
+ const ahead = block.findForward((b) => b.type === 'region');
115
+ facts.nextRegion = ahead ? regions.of(ahead) : null;
116
+ facts.inTitle = block.type === 'region';
117
+ facts.inBodyTextBlock = isBodyTextBlock(block);
118
+
119
+ if (!ctx.isCollapsed || !hasText(block)) return facts;
120
+ const sel = block.selection;
121
+ if (!sel || sel.from !== sel.to) return facts;
122
+ const length = /** @type {any} */ (block.behaviors.get('text')).text?.length ?? 0;
123
+
124
+ if (sel.from === 0 && region) {
125
+ // The title itself, or: no text block between the title and here (the title is the previous text in document order).
126
+ facts.atStartOfRegion = facts.inTitle || block.findBackward(hasText) === region.block;
127
+ }
128
+ if (sel.from === length) {
129
+ const partner = block.findForward(hasText);
130
+ const boundary = partner?.type === 'region' ? regions.of(partner) : null;
131
+ const before = boundary && neighbourOf(nabu, boundary.block, -1);
132
+ if (boundary && before && holds(before, block)) {
133
+ facts.atEndOfRegion = true;
134
+ facts.nextBoundaryRegion = boundary;
135
+ }
136
+ }
137
+ return facts;
138
+ }
139
+
140
+ /**
141
+ * Whether a Backspace / Delete merge of two text blocks must be refused (ADR 018 §3.9): one of them is a region block (its
142
+ * TITLE: nothing merges into or out of a title), or they are not in the same region (the merge would cross a seam). This is
143
+ * the `onBeforeMerge` veto: it refuses ANY silent merge through a seam, whatever started it (a key not bound to a command,
144
+ * a virtual keyboard, a block type that handles Backspace itself…).
145
+ * @param {Nabu} nabu @param {Block} survivor @param {Block} victim
146
+ * @returns {boolean}
147
+ */
148
+ export function mergeCrossesRegion(nabu, survivor, victim) {
149
+ if (survivor.type === 'region' || victim.type === 'region') return true;
150
+ const regions = Regions.get(nabu);
151
+ return regions.at(survivor) !== regions.at(victim);
152
+ }
@@ -0,0 +1,91 @@
1
+ /** The heading level of a region level: 1..6 (never an arbitrary value in a tag name). @param {unknown} level @returns {number} */
2
+ export function headingLevel(level: unknown): number;
3
+ /** A region type id as it is written after `region:` (characters outside `[A-Za-z0-9_-]` become `%XX`). @param {string} id @returns {string} */
4
+ export function encodeTypeId(id: string): string;
5
+ /**
6
+ * Minimal JSON for a comment / attribute: no whitespace, keys sorted at every level, and every character that could end
7
+ * the comment (`>`), open markup (`<`, `&`) or split a line (U+2028 / U+2029) written as a `\uXXXX` escape (still valid JSON).
8
+ * @param {unknown} value
9
+ * @returns {string}
10
+ */
11
+ export function safeJson(value: unknown): string;
12
+ /**
13
+ * The fields of a marker in their TEXT form: richtext fields (declared as such, held as a Loro delta) become inline
14
+ * markdown strings; everything else is kept; empty richtext fields and `undefined`s are left out (minimal).
15
+ * @param {RegionsConfig} config @param {string} regionType @param {Record<string, any> | undefined} fields
16
+ * @returns {Record<string, any>}
17
+ */
18
+ export function fieldsToText(config: RegionsConfig, regionType: string, fields: Record<string, any> | undefined): Record<string, any>;
19
+ /** Drops the link marks `sanitizeUrl` refuses (text kept). @param {Array<{ insert: string, attributes?: Record<string, any> }>} delta */
20
+ export function sanitizeFieldDelta(delta: Array<{
21
+ insert: string;
22
+ attributes?: Record<string, any>;
23
+ }>): {
24
+ insert: string;
25
+ attributes?: Record<string, any>;
26
+ }[];
27
+ /**
28
+ * The inverse of {@link fieldsToText}: richtext fields (declared as such, held as a markdown string) are parsed back to a
29
+ * delta; the other values are kept as they are (the block validates the declared ones when it is created: a wrong value is
30
+ * dropped there, with a warning). `__proto__` is never copied.
31
+ * @param {RegionsConfig} config @param {string} regionType @param {Record<string, any>} fields
32
+ * @returns {Record<string, any>}
33
+ */
34
+ export function fieldsFromText(config: RegionsConfig, regionType: string, fields: Record<string, any>): Record<string, any>;
35
+ /**
36
+ * The level a region type has WHEN IT IS READ BACK: its declared level; an undeclared type is read as the default type
37
+ * (see {@link parseRegionComment}). This is the level the end-of-region inference compares.
38
+ * @param {RegionsConfig} config @param {string} regionType @returns {number}
39
+ */
40
+ export function levelOfType(config: RegionsConfig, regionType: string): number;
41
+ /**
42
+ * Reads ONE line as the data comment: `{ regionType, fields }`, or `null` when it is not EXACTLY the construct (wrong shape,
43
+ * invalid JSON, JSON that is not an object): it stays literal text. An undeclared type falls back to the default type (the
44
+ * fields are kept).
45
+ * @param {RegionsConfig} config @param {string | undefined} line
46
+ * @returns {{ regionType: string, fields: Record<string, any> } | null}
47
+ */
48
+ export function parseRegionComment(config: RegionsConfig, line: string | undefined): {
49
+ regionType: string;
50
+ fields: Record<string, any>;
51
+ } | null;
52
+ /**
53
+ * The region that opens on line `at` of `lines`, with its extent (markdown import, paste AND `importMarkdown`):
54
+ *
55
+ * - the OPENING is the data comment ({@link parseRegionComment}); the line RIGHT AFTER it is the title when it is an ATX
56
+ * heading (`title` = its inline markdown, `bodyStart` = the line after it), else the title is empty and the body starts
57
+ * on the line after the comment;
58
+ * - the END is, whichever comes first (comments inside fenced code are not constructs):
59
+ * 1. its explicit `<!-- /region -->` (consumed with the region). A closing comment closes the INNERMOST region still
60
+ * open, so the one of a sub-region is not taken for this region's;
61
+ * 2. the next opening comment of a region whose level is <= this region's level (it closes this region and every
62
+ * sub-region still open; a DEEPER one nests: it starts a sub-region, which the body parse reads in turn);
63
+ * 3. the end of the lines (the end of the enclosing region, which hands its body lines over, or of the document).
64
+ *
65
+ * @param {RegionsConfig} config @param {string[]} lines @param {number} at
66
+ * @returns {{ regionType: string, fields: Record<string, any>, title: string, bodyStart: number, bodyEnd: number, end: number } | null}
67
+ * `lines.slice(bodyStart, bodyEnd)` is the body; `end` is the first line NOT consumed (after the closing comment, if any).
68
+ */
69
+ export function scanRegion(config: RegionsConfig, lines: string[], at: number): {
70
+ regionType: string;
71
+ fields: Record<string, any>;
72
+ title: string;
73
+ bodyStart: number;
74
+ bodyEnd: number;
75
+ end: number;
76
+ } | null;
77
+ /**
78
+ * Reads an element carrying `data-nabu-region`: `{ regionType, fields }`, or `null` when the type is not a plausible id.
79
+ * A fields attribute that is missing, too long, not JSON or not an object gives no fields (the region is still a region).
80
+ * An undeclared type falls back to the default type.
81
+ * @param {RegionsConfig} config @param {Element} el
82
+ * @returns {{ regionType: string, fields: Record<string, any> } | null}
83
+ */
84
+ export function parseRegionHtml(config: RegionsConfig, el: Element): {
85
+ regionType: string;
86
+ fields: Record<string, any>;
87
+ } | null;
88
+ /** The closing comment written when the end of a region cannot be deduced. */
89
+ export const CLOSE_COMMENT: "<!-- /region -->";
90
+ export function isCloseComment(line: string): boolean;
91
+ import type { RegionsConfig } from "./types.js";
@@ -0,0 +1,260 @@
1
+ /**
2
+ * Text forms of a region (ADR 018 §3.7, container model): the helpers shared by the markdown / HTML export (serialize.js)
3
+ * and by the import (the block statics). Pure functions, no runes, no DOM, no engine.
4
+ *
5
+ * MARKDOWN, the opening of a region is the DATA comment, optionally followed by the TITLE as an ATX heading:
6
+ *
7
+ * <!-- region:chapter {"pov":"Anna"} -->
8
+ * ## La forêt
9
+ *
10
+ * - line 1, the DATA comment, is the source of truth for the type and the fields: `region:<type>` then the fields as
11
+ * minimal JSON (keys sorted, `<` / `>` / `&` and the line separators written as `\uXXXX`, so the JSON can never contain
12
+ * `-->` or markup). The title is NOT among the fields. RICHTEXT fields are written as inline MARKDOWN strings (a Loro
13
+ * delta is unreadable) and parsed back with the inline parser: marks and links survive, whitespace at the edge of a mark
14
+ * does not (the same limits as a paragraph).
15
+ * - line 2, the heading, IS the title (inline markdown); its level is ignored on import (the type decides). It is
16
+ * optional: a comment followed by anything else (a blank line, a paragraph) is a region with an EMPTY title. A bare
17
+ * `##` is an empty heading, also an empty title (written only when the body starts with a heading of its own, which
18
+ * would otherwise be read as the title).
19
+ * - the END of a region is not written when it can be deduced: see {@link scanRegion} for the rules, and serialize.js for
20
+ * when the closing comment `<!-- /region -->` is written.
21
+ *
22
+ * HTML: one element, `<section data-nabu-region="chapter" data-nabu-fields='{…}'><h2>title</h2>…body…</section>`.
23
+ *
24
+ * Only these exact constructs are recognised on the way in. Anything else stays what it was: a heading without the comment
25
+ * is a heading, any other HTML comment is text, a comment with invalid JSON is text, a plain `<h2>` / `<section>` is itself.
26
+ */
27
+
28
+ import { sanitizeUrl } from '../../utils/html.js';
29
+ import { deltaToMarkdown } from '../../behaviors/text/text.behavior.svelte.js';
30
+ import { parseMarkdownInline } from '../../paste/markdown-inline.js';
31
+ import { fieldSpec } from './types.js';
32
+
33
+ /**
34
+ * @import { RegionsConfig } from "./types.js";
35
+ */
36
+
37
+ /** The data comment, whole line: `<!-- region:<type> {json} -->`. The type is percent-encoded when it has other characters. */
38
+ const COMMENT_RE = /^<!-- region:([A-Za-z0-9_%-]+) (\{.*\}) -->[ \t]*$/;
39
+ /** The title line that may follow it: an ATX heading, possibly empty (`##`). Group 2 is the inline markdown of the title. */
40
+ const TITLE_RE = /^ {0,3}(#{1,6})(?:[ \t]+(.*))?$/;
41
+ /** The closing comment, whole line. */
42
+ const CLOSE_RE = /^<!-- \/region -->[ \t]*$/;
43
+ /** A fence opening / closing (the same shapes as the toggle's: a comment inside a code block is not a construct). */
44
+ const FENCE_OPEN_RE = /^ {0,3}(`{3,}|~{3,})/;
45
+ /** Upper bound of the JSON a marker comment / attribute is allowed to carry (a hand-made payload is not trusted). */
46
+ const MAX_JSON = 1_000_000;
47
+
48
+ /** The heading level of a region level: 1..6 (never an arbitrary value in a tag name). @param {unknown} level @returns {number} */
49
+ export function headingLevel(level) {
50
+ const n = Math.trunc(Number(level));
51
+ return n >= 1 && n <= 6 ? n : n > 6 ? 6 : 1;
52
+ }
53
+
54
+ /** Whether a string is a plain object (what a `fields` JSON must be). @param {unknown} v @returns {v is Record<string, any>} */
55
+ const isPlainObject = (v) => !!v && typeof v === 'object' && !Array.isArray(v);
56
+
57
+ /** @param {unknown} v @returns {v is Array<{ insert: string, attributes?: Record<string, any> }>} */
58
+ const isDelta = (v) => Array.isArray(v) && v.every((op) => op && typeof op === 'object' && typeof (/** @type {any} */ (op)).insert === 'string');
59
+
60
+ /** A region type id as it is written after `region:` (characters outside `[A-Za-z0-9_-]` become `%XX`). @param {string} id @returns {string} */
61
+ export function encodeTypeId(id) {
62
+ return id.replace(/[^A-Za-z0-9_-]/gu, (c) => {
63
+ try { return encodeURIComponent(c).replace(/[^%A-Za-z0-9]/g, (x) => '%' + x.charCodeAt(0).toString(16).toUpperCase().padStart(2, '0')); } catch { return '_'; }
64
+ });
65
+ }
66
+
67
+ /** @param {string} encoded @returns {string | null} */
68
+ function decodeTypeId(encoded) {
69
+ try { return decodeURIComponent(encoded); } catch { return null; }
70
+ }
71
+
72
+ /**
73
+ * Minimal JSON for a comment / attribute: no whitespace, keys sorted at every level, and every character that could end
74
+ * the comment (`>`), open markup (`<`, `&`) or split a line (U+2028 / U+2029) written as a `\uXXXX` escape (still valid JSON).
75
+ * @param {unknown} value
76
+ * @returns {string}
77
+ */
78
+ export function safeJson(value) {
79
+ /** @param {any} v @returns {any} */
80
+ const sorted = (v) => Array.isArray(v) ? v.map(sorted) : isPlainObject(v) ? Object.fromEntries(Object.keys(v).sort().map((k) => [k, sorted(v[k])])) : v;
81
+ return JSON.stringify(sorted(value)).replace(/[<>&\u2028\u2029]/g, (c) => '\\u' + c.charCodeAt(0).toString(16).padStart(4, '0'));
82
+ }
83
+
84
+ /**
85
+ * The fields of a marker in their TEXT form: richtext fields (declared as such, held as a Loro delta) become inline
86
+ * markdown strings; everything else is kept; empty richtext fields and `undefined`s are left out (minimal).
87
+ * @param {RegionsConfig} config @param {string} regionType @param {Record<string, any> | undefined} fields
88
+ * @returns {Record<string, any>}
89
+ */
90
+ export function fieldsToText(config, regionType, fields) {
91
+ const def = config.types.get(regionType);
92
+ /** @type {Record<string, any>} */
93
+ const out = {};
94
+ for (const [name, value] of Object.entries(fields ?? {})) {
95
+ if (value === undefined) continue;
96
+ if (fieldSpec(def, name)?.kind === 'richtext' && isDelta(value)) {
97
+ const md = deltaToMarkdown(value);
98
+ if (md) out[name] = md;
99
+ continue;
100
+ }
101
+ out[name] = value;
102
+ }
103
+ return out;
104
+ }
105
+
106
+ /** Drops the link marks `sanitizeUrl` refuses (text kept). @param {Array<{ insert: string, attributes?: Record<string, any> }>} delta */
107
+ export function sanitizeFieldDelta(delta) {
108
+ return delta.map((op) => {
109
+ const link = op.attributes?.link;
110
+ if (link === undefined || link === null || link === false) return op;
111
+ if (typeof link === 'string' && sanitizeUrl(link, { kind: 'link' }) !== null) return op;
112
+ const { link: _drop, ...attributes } = /** @type {Record<string, any>} */ (op.attributes);
113
+ return Object.keys(attributes).length ? { ...op, attributes } : { insert: op.insert };
114
+ });
115
+ }
116
+
117
+ /**
118
+ * The inverse of {@link fieldsToText}: richtext fields (declared as such, held as a markdown string) are parsed back to a
119
+ * delta; the other values are kept as they are (the block validates the declared ones when it is created: a wrong value is
120
+ * dropped there, with a warning). `__proto__` is never copied.
121
+ * @param {RegionsConfig} config @param {string} regionType @param {Record<string, any>} fields
122
+ * @returns {Record<string, any>}
123
+ */
124
+ export function fieldsFromText(config, regionType, fields) {
125
+ const def = config.types.get(regionType);
126
+ /** @type {Record<string, any>} */
127
+ const out = {};
128
+ for (const [name, value] of Object.entries(fields)) {
129
+ if (name === '__proto__') continue;
130
+ if (fieldSpec(def, name)?.kind === 'richtext' && typeof value === 'string') {
131
+ const delta = sanitizeFieldDelta(/** @type {any} */ (parseMarkdownInline(value)));
132
+ if (delta.length) out[name] = delta;
133
+ continue;
134
+ }
135
+ out[name] = value;
136
+ }
137
+ return out;
138
+ }
139
+
140
+ /** The closing comment written when the end of a region cannot be deduced. */
141
+ export const CLOSE_COMMENT = '<!-- /region -->';
142
+
143
+ /** Whether a markdown line is exactly the closing comment. @param {string} line @returns {boolean} */
144
+ export const isCloseComment = (line) => CLOSE_RE.test(line);
145
+
146
+ /**
147
+ * The level a region type has WHEN IT IS READ BACK: its declared level; an undeclared type is read as the default type
148
+ * (see {@link parseRegionComment}). This is the level the end-of-region inference compares.
149
+ * @param {RegionsConfig} config @param {string} regionType @returns {number}
150
+ */
151
+ export function levelOfType(config, regionType) {
152
+ return (config.types.get(regionType) ?? config.types.get(config.defaultType))?.level ?? config.maxLevel;
153
+ }
154
+
155
+ /**
156
+ * Reads ONE line as the data comment: `{ regionType, fields }`, or `null` when it is not EXACTLY the construct (wrong shape,
157
+ * invalid JSON, JSON that is not an object): it stays literal text. An undeclared type falls back to the default type (the
158
+ * fields are kept).
159
+ * @param {RegionsConfig} config @param {string | undefined} line
160
+ * @returns {{ regionType: string, fields: Record<string, any> } | null}
161
+ */
162
+ export function parseRegionComment(config, line) {
163
+ if (!line || !line.startsWith('<!-- region:')) return null;
164
+ const m = COMMENT_RE.exec(line);
165
+ if (!m || m[2].length > MAX_JSON) return null;
166
+ const id = decodeTypeId(m[1]);
167
+ if (id === null) return null;
168
+ /** @type {unknown} */
169
+ let json;
170
+ try { json = JSON.parse(m[2]); } catch { return null; }
171
+ if (!isPlainObject(json)) return null;
172
+ const regionType = config.types.has(id) ? id : config.defaultType;
173
+ return { regionType, fields: fieldsFromText(config, regionType, json) };
174
+ }
175
+
176
+ /**
177
+ * Index of the line that closes the fence opened on line `at`, or -1 when `at` is not a fence opening. An UNCLOSED fence runs
178
+ * to the end of the text (that is what the code block rule does with it): `lines.length` is returned.
179
+ * @param {string[]} lines @param {number} at
180
+ * @returns {number}
181
+ */
182
+ function fenceEnd(lines, at) {
183
+ const open = FENCE_OPEN_RE.exec(lines[at]);
184
+ if (!open) return -1;
185
+ for (let k = at + 1; k < lines.length; k++) {
186
+ const close = /^ {0,3}(`+|~+)[ \t]*$/.exec(lines[k]);
187
+ if (close && close[1][0] === open[1][0] && close[1].length >= open[1].length) return k;
188
+ }
189
+ return lines.length;
190
+ }
191
+
192
+ /**
193
+ * The region that opens on line `at` of `lines`, with its extent (markdown import, paste AND `importMarkdown`):
194
+ *
195
+ * - the OPENING is the data comment ({@link parseRegionComment}); the line RIGHT AFTER it is the title when it is an ATX
196
+ * heading (`title` = its inline markdown, `bodyStart` = the line after it), else the title is empty and the body starts
197
+ * on the line after the comment;
198
+ * - the END is, whichever comes first (comments inside fenced code are not constructs):
199
+ * 1. its explicit `<!-- /region -->` (consumed with the region). A closing comment closes the INNERMOST region still
200
+ * open, so the one of a sub-region is not taken for this region's;
201
+ * 2. the next opening comment of a region whose level is <= this region's level (it closes this region and every
202
+ * sub-region still open; a DEEPER one nests: it starts a sub-region, which the body parse reads in turn);
203
+ * 3. the end of the lines (the end of the enclosing region, which hands its body lines over, or of the document).
204
+ *
205
+ * @param {RegionsConfig} config @param {string[]} lines @param {number} at
206
+ * @returns {{ regionType: string, fields: Record<string, any>, title: string, bodyStart: number, bodyEnd: number, end: number } | null}
207
+ * `lines.slice(bodyStart, bodyEnd)` is the body; `end` is the first line NOT consumed (after the closing comment, if any).
208
+ */
209
+ export function scanRegion(config, lines, at) {
210
+ const opening = parseRegionComment(config, lines[at]);
211
+ if (!opening) return null;
212
+ const level = levelOfType(config, opening.regionType);
213
+ const heading = TITLE_RE.exec(lines[at + 1] ?? '');
214
+ const title = heading?.[2] ?? '';
215
+ const bodyStart = at + (heading ? 2 : 1);
216
+ /** Levels of the sub-regions opened and not closed yet. @type {number[]} */
217
+ const open = [];
218
+ let bodyEnd = lines.length;
219
+ let end = lines.length;
220
+ for (let j = bodyStart; j < lines.length; j++) {
221
+ const line = lines[j];
222
+ const fence = fenceEnd(lines, j);
223
+ if (fence >= 0) { j = fence; continue; }
224
+ if (CLOSE_RE.test(line)) {
225
+ if (open.length) { open.pop(); continue; }
226
+ bodyEnd = j; end = j + 1;
227
+ break;
228
+ }
229
+ const next = parseRegionComment(config, line);
230
+ if (!next) continue;
231
+ const nextLevel = levelOfType(config, next.regionType);
232
+ while (open.length && open[open.length - 1] >= nextLevel) open.pop();
233
+ if (nextLevel <= level) { bodyEnd = end = j; break; }
234
+ open.push(nextLevel);
235
+ }
236
+ return { ...opening, title: title.trim() ? title : '', bodyStart, bodyEnd, end };
237
+ }
238
+
239
+ /**
240
+ * Reads an element carrying `data-nabu-region`: `{ regionType, fields }`, or `null` when the type is not a plausible id.
241
+ * A fields attribute that is missing, too long, not JSON or not an object gives no fields (the region is still a region).
242
+ * An undeclared type falls back to the default type.
243
+ * @param {RegionsConfig} config @param {Element} el
244
+ * @returns {{ regionType: string, fields: Record<string, any> } | null}
245
+ */
246
+ export function parseRegionHtml(config, el) {
247
+ const id = el.getAttribute('data-nabu-region')?.trim() ?? '';
248
+ if (!id || id.length > 200) return null;
249
+ const regionType = config.types.has(id) ? id : config.defaultType;
250
+ /** @type {Record<string, any>} */
251
+ let fields = {};
252
+ const raw = el.getAttribute('data-nabu-fields');
253
+ if (raw && raw.length <= MAX_JSON) {
254
+ try {
255
+ const json = JSON.parse(raw);
256
+ if (isPlainObject(json)) fields = fieldsFromText(config, regionType, json);
257
+ } catch { /* invalid JSON: a marker without fields */ }
258
+ }
259
+ return { regionType, fields };
260
+ }
@@ -0,0 +1,14 @@
1
+ export { RegionBlock } from "./region.svelte.js";
2
+ export { default as RegionComponent } from "./Region.svelte";
3
+ export { normalizeRegions } from "./normalize.js";
4
+ export { createRegionCommands } from "./commands.js";
5
+ export { DEFAULT_REGION_MESSAGES } from "./messages.js";
6
+ export { regionFacts } from "./facts.js";
7
+ export { RegionsExtension, createRegionsExtension } from "./region.extension.js";
8
+ export { Regions, Region, Preamble } from "./regions.svelte.js";
9
+ export { insertRegion, createRegion, createMarker } from "./insert.js";
10
+ export { resolveConfig as resolveRegionsConfig, validateField as validateRegionField, DEFAULT_TYPES as DEFAULT_REGION_TYPES } from "./types.js";
11
+ export { splitRegion, insertNearRegion, mergeRegion, unwrapRegion, exitRegion, moveRegion, demoteRegion, promoteRegion, goToRegion } from "./actions.js";
12
+ export { createOptions, previewPrefix } from "./create.js";
13
+ export { needsEndMarker, endMarkerText } from "./end-marker.js";
14
+ export { closeChain, closeOptions, closeRegionAt } from "./close.js";
@@ -0,0 +1,14 @@
1
+ export { RegionBlock } from './region.svelte.js';
2
+ export { RegionsExtension, createRegionsExtension } from './region.extension.js';
3
+ export { default as RegionComponent } from './Region.svelte';
4
+ export { Regions, Region, Preamble } from './regions.svelte.js';
5
+ export { insertRegion, createRegion, createMarker } from './insert.js';
6
+ export { normalizeRegions } from './normalize.js';
7
+ export { resolveConfig as resolveRegionsConfig, validateField as validateRegionField, DEFAULT_TYPES as DEFAULT_REGION_TYPES } from './types.js';
8
+ export { createRegionCommands } from './commands.js';
9
+ export { DEFAULT_REGION_MESSAGES } from './messages.js';
10
+ export { regionFacts } from './facts.js';
11
+ export { splitRegion, insertNearRegion, mergeRegion, unwrapRegion, exitRegion, moveRegion, demoteRegion, promoteRegion, goToRegion } from './actions.js';
12
+ export { createOptions, previewPrefix } from './create.js';
13
+ export { needsEndMarker, endMarkerText } from './end-marker.js';
14
+ export { closeChain, closeOptions, closeRegionAt } from './close.js';
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Creates a region WITHOUT committing: the caller owns the transaction (an action that splits a block and creates a region is
3
+ * one undo step). `parentId` is the id of a region block (its body), `null` for the root; `index` is clamped. The title is
4
+ * the header text. Throws on an unknown type or when the extension is not installed.
5
+ * @param {Nabu} nabu
6
+ * @param {{ type?: string, parentId?: string | null, index?: number | null, title?: string, fields?: Record<string, any> }} [options]
7
+ * `type` defaults to the lowest-level declared type; `index` defaults to the end of the body.
8
+ * @returns {import("./region.svelte.js").RegionBlock}
9
+ */
10
+ export function createRegion(nabu: Nabu, { type, parentId, index, title, fields }?: {
11
+ type?: string;
12
+ parentId?: string | null;
13
+ index?: number | null;
14
+ title?: string;
15
+ fields?: Record<string, any>;
16
+ }): import("./region.svelte.js").RegionBlock;
17
+ /**
18
+ * Inserts a region as one undo step.
19
+ * @param {Nabu} nabu
20
+ * @param {{ type?: string, at?: InsertRegionAt, title?: string, fields?: Record<string, any>, parent?: Block | null }} [options]
21
+ * `type` defaults to the lowest-level declared type; `at` defaults to `'end'`; `parent` is the region whose body receives
22
+ * the new one (the root of the document by default).
23
+ * @returns {Promise<import("./region.svelte.js").RegionBlock>}
24
+ */
25
+ export function insertRegion(nabu: Nabu, { type, at, title, fields, parent }?: {
26
+ type?: string;
27
+ at?: InsertRegionAt;
28
+ title?: string;
29
+ fields?: Record<string, any>;
30
+ parent?: Block | null;
31
+ }): Promise<import("./region.svelte.js").RegionBlock>;
32
+ /** Root index of the top-level block holding `block` (the end of the document when it is not found). @param {Nabu} nabu @param {Block} block @returns {number} */
33
+ export function rootIndexOf(nabu: Nabu, block: Block): number;
34
+ /**
35
+ * Creates a region at a ROOT index WITHOUT committing.
36
+ * @param {Nabu} nabu
37
+ * @param {{ type?: string, index: number, title?: string, fields?: Record<string, any> }} options
38
+ * @returns {import("./region.svelte.js").RegionBlock}
39
+ */
40
+ export function createMarker(nabu: Nabu, { type, index, title, fields }: {
41
+ type?: string;
42
+ index: number;
43
+ title?: string;
44
+ fields?: Record<string, any>;
45
+ }): import("./region.svelte.js").RegionBlock;
46
+ /**
47
+ * Where to insert, relative to the BODY of the parent (the root of the document by default):
48
+ * - `'start'` | `'end'` first / last position;
49
+ * - a number that index (clamped);
50
+ * - `{ after: Block }` right after that block (a child of the parent; a deeper block counts as its ancestor in the parent);
51
+ * - `{ before: Block }` right before it.
52
+ */
53
+ export type InsertRegionAt = "start" | "end" | number | {
54
+ after: Block;
55
+ } | {
56
+ before: Block;
57
+ };
58
+ import type { Nabu } from "../nabu.svelte.js";
59
+ import type { Block } from "../block.svelte.js";