@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,18 @@
1
+ import { extension } from "../../utils/extensions";
2
+ import { ToggleBlock } from "./toggle.svelte.js";
3
+ import ToggleComponent from "./Toggle.svelte";
4
+
5
+ /**
6
+ * The toggle block: a collapsible container with its own header text and a body of child blocks; the `toggle` action and the "Toggle" slash entry.
7
+ */
8
+ export const ToggleExtension = extension("toggle", {
9
+ // @ts-ignore — static-side variance on create()
10
+ block: ToggleBlock,
11
+ component: ToggleComponent,
12
+ slashCommands: [
13
+ { title: 'Toggle', icon: '▸', aliases: ['toggle', 'collapse', 'details'], group: 'blocks', order: 30, run: (e) => e.exec('toggle') },
14
+ ],
15
+ actions: {
16
+ 'toggle': (nabu) => nabu.exec('block:transform', { type: 'toggle' }),
17
+ },
18
+ });
@@ -0,0 +1,135 @@
1
+ /**
2
+ * @import { Nabu, NabuNode } from "../nabu.svelte.js";
3
+ */
4
+ /**
5
+ * @typedef {NabuNode<{type: "toggle", text: LoroText, collapsed: boolean}>} ToggleNode
6
+ */
7
+ export class ToggleBlock extends MegaBlock {
8
+ static placeholder: string;
9
+ /**
10
+ * `<details>` / `<details open>` + `<summary>` + the body as markdown + `</details>` (see markdown.js).
11
+ * @param {import('../../utils/extensions.js').PasteBlock} pb
12
+ * @param {{ recurse: (child: import('../../utils/extensions.js').PasteBlock, ctx?: object) => string, recurseAll?: (children: import('../../utils/extensions.js').PasteBlock[], ctx?: object) => string[] }} helpers
13
+ */
14
+ static toMarkdown(pb: import("../../utils/extensions.js").PasteBlock, { recurse, recurseAll }: {
15
+ recurse: (child: import("../../utils/extensions.js").PasteBlock, ctx?: object) => string;
16
+ recurseAll?: (children: import("../../utils/extensions.js").PasteBlock[], ctx?: object) => string[];
17
+ }): string;
18
+ /**
19
+ * Markdown (paste AND `importMarkdown`): the exact `<details>` construct Nabu emits, nothing else (see markdown.js).
20
+ * An opening line without a matching `</details>`, with attributes or without a `<summary>` is not a toggle: it stays text.
21
+ */
22
+ static markdownRules: {
23
+ priority: number;
24
+ interruptsParagraph: boolean;
25
+ detect: (line: string, lines: string[], i: number) => boolean;
26
+ consume: (lines: string[], i: number) => number;
27
+ }[];
28
+ /**
29
+ * The toggle (header = the summary, parsed as inline markdown; body = the lines between the summary and `</details>`,
30
+ * parsed as a document of their own, nested toggles included).
31
+ * @param {string[]} lines
32
+ * @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[], parseBlocks?: (lines: string[]) => import('../../utils/extensions.js').PasteBlock[] }} helpers
33
+ * @returns {import('../../utils/extensions.js').PasteBlock | null}
34
+ */
35
+ static fromMarkdown(lines: string[], { parseInline, parseBlocks }: {
36
+ parseInline: (text: string) => import("loro-crdt").Delta<string>[];
37
+ parseBlocks?: (lines: string[]) => import("../../utils/extensions.js").PasteBlock[];
38
+ }): import("../../utils/extensions.js").PasteBlock | null;
39
+ /**
40
+ * @param {import('../../utils/extensions.js').PasteBlock} pb
41
+ * @param {{ recurse: (child: import('../../utils/extensions.js').PasteBlock) => string }} helpers
42
+ */
43
+ static toHtml(pb: import("../../utils/extensions.js").PasteBlock, { recurse }: {
44
+ recurse: (child: import("../../utils/extensions.js").PasteBlock) => string;
45
+ }): string;
46
+ static htmlRules: {
47
+ selector: string;
48
+ }[];
49
+ /**
50
+ * @param {Element} el
51
+ * @param {{ parseInline: (el: Element) => import('loro-crdt').Delta<string>[], parseChildren?: (el: Element) => import('../../utils/extensions.js').PasteBlock[] }} helpers
52
+ * @returns {import('../../utils/extensions.js').PasteBlock | null}
53
+ */
54
+ static fromHTML(el: Element, { parseInline, parseChildren }: {
55
+ parseInline: (el: Element) => import("loro-crdt").Delta<string>[];
56
+ parseChildren?: (el: Element) => import("../../utils/extensions.js").PasteBlock[];
57
+ }): import("../../utils/extensions.js").PasteBlock | null;
58
+ /**
59
+ * @param {Nabu} nabu
60
+ * @param {string} type
61
+ * @param {Record<string, any>} [props={}]
62
+ * @param {string|null} [parentId=null]
63
+ * @param {number|null} [index=null]
64
+ */
65
+ static create(nabu: Nabu, type: string, props?: Record<string, any>, parentId?: string | null, index?: number | null): ToggleBlock;
66
+ /** @param {Nabu} nabu @param {ToggleNode} node */
67
+ constructor(nabu: Nabu, node: ToggleNode);
68
+ container: LoroText;
69
+ /** @type {TextBehavior} */
70
+ behavior: TextBehavior;
71
+ set collapsed(v: boolean);
72
+ /**
73
+ * Whether the body is hidden. In a read-only editor a reader may open or close a toggle to read it: that is VIEW STATE
74
+ * (kept in memory, not in the document: no Loro write, no commit, no undo step, nothing synced or persisted, serializers
75
+ * keep reporting the document's own state), and it is ignored again as soon as the editor is not read-only.
76
+ * @type {boolean}
77
+ */
78
+ get collapsed(): boolean;
79
+ /** A collapsed toggle stays collapsed on the clipboard. */
80
+ get clipboardProps(): {
81
+ collapsed: boolean;
82
+ };
83
+ component: import("svelte").Component<any, any, string> | import("svelte").Component<{
84
+ block: import("./toggle.svelte.js").ToggleBlock;
85
+ }, {}, "">;
86
+ get text(): string;
87
+ get delta(): import("loro-crdt").Delta<string>[];
88
+ selection: {
89
+ from: number;
90
+ to: number;
91
+ isCollapsed: boolean;
92
+ direction: "forward" | "backward" | "none";
93
+ } | null;
94
+ /**
95
+ * Set the collapsed state and persist to Loro (in a read-only editor: only what the reader sees, see {@link ToggleBlock#collapsed}).
96
+ * @param {boolean} v
97
+ */
98
+ setCollapsed(v: boolean): void;
99
+ /**
100
+ * Toggle the collapsed state.
101
+ */
102
+ toggleCollapsed(): void;
103
+ /** @param {import('../block.svelte.js').Block} block */
104
+ absorbs(block: import("../block.svelte.js").Block): boolean;
105
+ /** @param {import('loro-crdt').Delta<string>[]} data */
106
+ applyDelta(data?: import("loro-crdt").Delta<string>[]): void;
107
+ /**
108
+ * Enter in a body child (called through `ascend("onSplit")`). Only the Enter that EXITS the sub-thread is
109
+ * taken here: on an EMPTY paragraph that is the LAST child, the paragraph moves right after the toggle and
110
+ * takes the caret. Anything else is declined (falsy) and goes through the regular split of the child's type.
111
+ * @param {Event | null} event
112
+ * @param {{ from: import('../block.svelte.js').Block }} data
113
+ */
114
+ onSplit(event: Event | null, data: {
115
+ from: import("../block.svelte.js").Block;
116
+ }): false | {
117
+ block: import("../block.svelte.js").Block;
118
+ };
119
+ /**
120
+ * @param {InputEvent} event
121
+ * @returns {boolean}
122
+ */
123
+ beforeinput(event: InputEvent): boolean;
124
+ #private;
125
+ }
126
+ export type ToggleNode = NabuNode<{
127
+ type: "toggle";
128
+ text: LoroText;
129
+ collapsed: boolean;
130
+ }>;
131
+ import { MegaBlock } from "../megablock.svelte.js";
132
+ import { LoroText } from "loro-crdt";
133
+ import { TextBehavior } from "../../behaviors/text";
134
+ import type { Nabu } from "../nabu.svelte.js";
135
+ import type { NabuNode } from "../nabu.svelte.js";
@@ -0,0 +1,352 @@
1
+ import { MegaBlock } from "../megablock.svelte.js";
2
+ import { TextBehavior, deltaToHtml, deltaToMarkdown } from "../../behaviors/text";
3
+ import { LoroText } from "loro-crdt";
4
+ import ToggleComponent from "./Toggle.svelte";
5
+ import { parseDetails, toggleMarkdown } from "./markdown.js";
6
+ import { isEmptyLastParagraph, exitSubThread, dissolveToParagraph, insertParagraphAfter, pasteIntoHeader } from "../subthread.js";
7
+
8
+ /**
9
+ * @import { Nabu, NabuNode } from "../nabu.svelte.js";
10
+ */
11
+
12
+ /**
13
+ * @typedef {NabuNode<{type: "toggle", text: LoroText, collapsed: boolean}>} ToggleNode
14
+ */
15
+
16
+ // @ts-ignore — static-side variance on create()
17
+ export class ToggleBlock extends MegaBlock {
18
+ /** @param {Nabu} nabu @param {ToggleNode} node */
19
+ constructor(nabu, node) {
20
+ super(nabu, node);
21
+
22
+ const data = node.data;
23
+ this.container = data.get("text") ?? data.setContainer("text", new LoroText());
24
+
25
+ /** @type {TextBehavior} */
26
+ this.behavior = new TextBehavior(this, this.container);
27
+ this.behaviors.set("text", this.behavior);
28
+
29
+ // Initialize collapsed from Loro metadata
30
+ this.collapsed = !!data.get('collapsed');
31
+ // Subscribe to data changes to keep collapsed in sync (a change of the document wins over the read-only view state)
32
+ this.node.data.subscribe(() => {
33
+ this.#viewState = null;
34
+ this.collapsed = !!this.node.data.get('collapsed');
35
+ });
36
+
37
+ this.serializers.set('markdown', () => toggleMarkdown(this.#stored, this.behavior.toMarkdown(), /** @type {any} */ (this.children.map(c => c.serialize('markdown')))));
38
+ this.serializers.set('json', () => ({
39
+ id: this.id,
40
+ type: 'toggle',
41
+ props: { collapsed: this.#stored },
42
+ content: this.behavior.toJSON(),
43
+ children: this.children.map(c => c.serialize('json')).filter(Boolean)
44
+ }));
45
+ this.serializers.set('application/x-nabu+json', () => ({
46
+ type: 'toggle',
47
+ props: { collapsed: this.#stored },
48
+ delta: this.container.sliceDelta(0, this.container.length),
49
+ children: this.children.map(c => c.serialize('application/x-nabu+json')).filter(Boolean)
50
+ }));
51
+ }
52
+
53
+ // ── Capability flags ────────────────────────────────────────────────────
54
+ // A typed "# " must not transform the toggle header away
55
+ transformsEnabled = false;
56
+ // marksEnabled stays true (default) — bold/italic work inside the header
57
+ // splitsOnEnter is not set (defaults true) but Enter is overridden in beforeinput
58
+
59
+ /** The collapsed state of the DOCUMENT (the `collapsed` prop of the node). */
60
+ #stored = $state(false);
61
+
62
+ /** Read-only editors only: what the reader chose to see, never written to the document (`null`: nothing chosen). */
63
+ /** @type {boolean | null} */
64
+ #viewState = $state(null);
65
+
66
+ /**
67
+ * Whether the body is hidden. In a read-only editor a reader may open or close a toggle to read it: that is VIEW STATE
68
+ * (kept in memory, not in the document: no Loro write, no commit, no undo step, nothing synced or persisted, serializers
69
+ * keep reporting the document's own state), and it is ignored again as soon as the editor is not read-only.
70
+ * @type {boolean}
71
+ */
72
+ get collapsed() { return this.nabu.readonly && this.#viewState !== null ? this.#viewState : this.#stored; }
73
+ set collapsed(v) { this.#stored = !!v; }
74
+
75
+ /** A collapsed toggle does not render its body: navigation must never put the caret in it. */
76
+ get hidesChildren() { return this.collapsed; }
77
+
78
+ /** A collapsed toggle stays collapsed on the clipboard. */
79
+ get clipboardProps() { return { collapsed: this.collapsed }; }
80
+
81
+ static placeholder = 'Toggle…';
82
+
83
+ component = $derived(this.nabu.components.get("toggle") || ToggleComponent);
84
+
85
+ get text() { return this.behavior.text; }
86
+ get delta() { return this.behavior.delta; }
87
+
88
+ selection = $derived(this.behavior.selection);
89
+
90
+ // When collapsed, exclude children from positions so cursor cannot enter hidden body
91
+ // @ts-ignore — `super.positions`: Svelte compiles the parent's `$derived` field to a prototype accessor, which TS sees as a field
92
+ positions = $derived(this.behavior.positions + (this.collapsed ? 0 : super.positions));
93
+
94
+ // Count body words regardless of collapse state
95
+ wordCount = $derived((this.behavior?.wordCount ?? 0) + this.children.reduce((s, c) => s + (c.wordCount ?? 0), 0));
96
+
97
+ /**
98
+ * Mixed block: own text comes first, then children.
99
+ * When collapsed, children are excluded from navigation.
100
+ * @param {number} localOffset
101
+ * @returns {{ block: import('../block.svelte.js').Block, offset: number } | null}
102
+ */
103
+ resolveOffset(localOffset) {
104
+ const textPositions = this.behavior.positions; // text.length + 1
105
+ if (this.collapsed || localOffset < textPositions || !this.children.length) {
106
+ return { block: this, offset: Math.min(localOffset, this.behavior.text.length) };
107
+ }
108
+ let remaining = localOffset - textPositions;
109
+ for (const child of this.children) {
110
+ const cp = child.positions ?? 0;
111
+ if (remaining < cp) return child.resolveOffset(remaining);
112
+ remaining -= cp;
113
+ }
114
+ return { block: this, offset: this.behavior.text.length };
115
+ }
116
+
117
+ // ── Collapse control ────────────────────────────────────────────────────
118
+
119
+ /**
120
+ * Set the collapsed state and persist to Loro (in a read-only editor: only what the reader sees, see {@link ToggleBlock#collapsed}).
121
+ * @param {boolean} v
122
+ */
123
+ setCollapsed(v) {
124
+ if (this.nabu.readonly) { this.#viewState = !!v; return; } // view state only: the document is not touched
125
+ this.#viewState = null;
126
+ this.collapsed = !!v;
127
+ /** @type {any} */ (this.node.data).set('collapsed', !!v);
128
+ this.commit();
129
+ }
130
+
131
+ /**
132
+ * Toggle the collapsed state.
133
+ */
134
+ toggleCollapsed() {
135
+ this.setCollapsed(!this.collapsed);
136
+ }
137
+
138
+ // ── Proxies to TextBehavior ─────────────────────────────────────────────
139
+
140
+ /**
141
+ * @param {number} targetOffset
142
+ * @returns {{node: Node, offset: number} | null}
143
+ */
144
+ getDOMPoint(targetOffset) { return this.behavior.getDOMPoint(targetOffset); }
145
+
146
+ /** @param {import('../block.svelte.js').Block} block */
147
+ absorbs(block) { return this.behavior.absorbs(block); }
148
+
149
+ /** @param {number} index @param {string} text */
150
+ insert(index, text) { return this.behavior.insert(index, text); }
151
+
152
+ /** @param {Parameters<import('../block.svelte.js').Block["delete"]>[0]} [deletion] */
153
+ delete(deletion) { return this.behavior.delete(deletion); }
154
+
155
+ /** @param {import('loro-crdt').Delta<string>[]} data */
156
+ applyDelta(data = []) { return this.behavior.applyDelta(data); }
157
+
158
+ // ── Sub-thread rules (P-40) ─────────────────────────────────────────────
159
+ // Header = own text, body = children. The tree operations live in ../subthread.js, shared with any other
160
+ // container block (the container Callout will reuse them as is).
161
+
162
+ /**
163
+ * Enter in a body child (called through `ascend("onSplit")`). Only the Enter that EXITS the sub-thread is
164
+ * taken here: on an EMPTY paragraph that is the LAST child, the paragraph moves right after the toggle and
165
+ * takes the caret. Anything else is declined (falsy) and goes through the regular split of the child's type.
166
+ * @param {Event | null} event
167
+ * @param {{ from: import('../block.svelte.js').Block }} data
168
+ */
169
+ onSplit(event, data) {
170
+ const source = data.from;
171
+ if (!isEmptyLastParagraph(/** @type {any} */ (this), source)) return false;
172
+ const id = source.node.id.toString();
173
+ exitSubThread(source.node, this.node);
174
+ this.nabu.commit().then(() => {
175
+ const f = this.nabu.blocks.get(id);
176
+ if (f) this.nabu.selection.setCursor(f, 0);
177
+ });
178
+ return { block: source };
179
+ }
180
+
181
+ /**
182
+ * Children adopted by a merge (`Block#consume`) never vanish into a HIDDEN body: when collapsed they stay
183
+ * visible, in order, right after the toggle.
184
+ * @param {import('../block.svelte.js').Block[]} children
185
+ * @param {number | null} [index]
186
+ */
187
+ adoptChildren(children, index = null) {
188
+ if (!this.collapsed) return super.adoptChildren(children, index);
189
+ [...children].reverse().forEach(child => { if (child.node) child.node.moveAfter(this.node); });
190
+ }
191
+
192
+ /** The body is free-form: an empty paragraph of the body takes the type of a whole block pasted into it. */
193
+ get adoptsPastedTypes() { return true; }
194
+
195
+ /**
196
+ * Paste with the caret in the HEADER (text only). One pasted text block is the engine's inline merge
197
+ * (declined). Several blocks: the first completes the header at the caret, the others go to the TOP of the
198
+ * body (like Enter, which creates the first body child), the header text after the caret follows the last
199
+ * one (merged into it when it is text, else in a new paragraph). A COLLAPSED toggle is EXPANDED first: the
200
+ * pasted content would otherwise land hidden. (A paste with the caret in a body child never reaches this
201
+ * hook: the child's own rules apply, inside the body.)
202
+ * @param {import('../../utils/extensions.js').PasteBlock[]} blocks
203
+ * @param {{ offset: number }} ctx
204
+ */
205
+ acceptPaste(blocks, ctx) {
206
+ if (!blocks?.length || (blocks.length === 1 && !blocks[0].children?.length)) return null;
207
+ const landing = pasteIntoHeader(this.nabu, /** @type {any} */ (this), ctx.offset, blocks);
208
+ if (landing && this.collapsed) /** @type {any} */ (this.node.data).set('collapsed', false);
209
+ return landing;
210
+ }
211
+
212
+ // ── Custom beforeinput (handles Enter and Backspace in the header) ──────
213
+
214
+ /**
215
+ * @param {InputEvent} event
216
+ * @returns {boolean}
217
+ */
218
+ beforeinput(event) {
219
+ const sel = this.behavior.selection;
220
+
221
+ // Enter in the header → split at the caret.
222
+ // - COLLAPSED: the tail goes into a new paragraph AFTER the toggle (caret in it); the toggle stays collapsed
223
+ // and its hidden body is not touched.
224
+ // - EXPANDED: the tail goes into a new paragraph as the FIRST body child (caret in it).
225
+ if (event.inputType === 'insertParagraph' && sel) {
226
+ const from = sel.from;
227
+ const to = sel.isCollapsed ? this.behavior.text.length : sel.to;
228
+ const delta = this.container.sliceDelta(from, to);
229
+ if (this.behavior.text.length > from) {
230
+ this.behavior.delete({ index: from, length: this.behavior.text.length - from });
231
+ }
232
+ const p = this.collapsed
233
+ ? insertParagraphAfter(this.nabu, this.node, delta)
234
+ : this.nabu.insert('paragraph', { delta }, this.node.id.toString(), 0);
235
+ this.nabu.commit().then(() => {
236
+ const f = this.nabu.blocks.get(p.id);
237
+ if (f) this.nabu.selection.setCursor(f, 0);
238
+ });
239
+ return true;
240
+ }
241
+
242
+ // Backspace at offset 0 of the header (empty or not, with or without body) → the toggle becomes a
243
+ // paragraph with the header text; its body children follow it. One transaction = one undo step.
244
+ if (event.inputType === 'deleteContentBackward' && sel?.isCollapsed && sel.from === 0) {
245
+ const id = this.node.id.toString();
246
+ this.nabu.transact(() => dissolveToParagraph(this.node, ['collapsed'])).then(() => {
247
+ const f = this.nabu.blocks.get(id);
248
+ if (f) f.focus({ start: 0, end: 0 });
249
+ });
250
+ return true;
251
+ }
252
+
253
+ return this.behavior.handleBeforeInput(event);
254
+ }
255
+
256
+ // ── Statics ─────────────────────────────────────────────────────────────
257
+
258
+ /**
259
+ * `<details>` / `<details open>` + `<summary>` + the body as markdown + `</details>` (see markdown.js).
260
+ * @param {import('../../utils/extensions.js').PasteBlock} pb
261
+ * @param {{ recurse: (child: import('../../utils/extensions.js').PasteBlock, ctx?: object) => string, recurseAll?: (children: import('../../utils/extensions.js').PasteBlock[], ctx?: object) => string[] }} helpers
262
+ */
263
+ static toMarkdown(pb, { recurse, recurseAll }) {
264
+ const children = pb.children || [];
265
+ return toggleMarkdown(!!pb.props?.collapsed, deltaToMarkdown(pb.delta || []), recurseAll ? recurseAll(children) : children.map(c => recurse(c)));
266
+ }
267
+
268
+ /**
269
+ * Markdown (paste AND `importMarkdown`): the exact `<details>` construct Nabu emits, nothing else (see markdown.js).
270
+ * An opening line without a matching `</details>`, with attributes or without a `<summary>` is not a toggle: it stays text.
271
+ */
272
+ static markdownRules = [
273
+ {
274
+ priority: 20,
275
+ interruptsParagraph: true,
276
+ detect: (/** @type {string} */ line, /** @type {string[]} */ lines, /** @type {number} */ i) => line.startsWith('<details') && !!parseDetails(lines, i),
277
+ consume: (/** @type {string[]} */ lines, /** @type {number} */ i) => (parseDetails(lines, i)?.end ?? i) - i + 1,
278
+ }
279
+ ];
280
+
281
+ /**
282
+ * The toggle (header = the summary, parsed as inline markdown; body = the lines between the summary and `</details>`,
283
+ * parsed as a document of their own, nested toggles included).
284
+ * @param {string[]} lines
285
+ * @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[], parseBlocks?: (lines: string[]) => import('../../utils/extensions.js').PasteBlock[] }} helpers
286
+ * @returns {import('../../utils/extensions.js').PasteBlock | null}
287
+ */
288
+ static fromMarkdown(lines, { parseInline, parseBlocks }) {
289
+ const details = parseDetails(lines, 0);
290
+ if (!details || !parseBlocks) return null;
291
+ const children = parseBlocks(lines.slice(details.bodyStart, details.end));
292
+ return { type: 'toggle', props: { collapsed: !details.open }, delta: parseInline(details.summary), partial: false, ...(children.length ? { children } : {}) };
293
+ }
294
+
295
+ /**
296
+ * @param {import('../../utils/extensions.js').PasteBlock} pb
297
+ * @param {{ recurse: (child: import('../../utils/extensions.js').PasteBlock) => string }} helpers
298
+ */
299
+ static toHtml(pb, { recurse }) {
300
+ const open = pb.props?.collapsed ? '' : ' open';
301
+ const head = deltaToHtml(pb.delta || []);
302
+ const body = (pb.children || []).map(c => recurse(c)).join('');
303
+ return '<details' + open + '><summary>' + head + '</summary>' + body + '</details>';
304
+ }
305
+
306
+ static htmlRules = [{ selector: 'details' }];
307
+
308
+ /**
309
+ * @param {Element} el
310
+ * @param {{ parseInline: (el: Element) => import('loro-crdt').Delta<string>[], parseChildren?: (el: Element) => import('../../utils/extensions.js').PasteBlock[] }} helpers
311
+ * @returns {import('../../utils/extensions.js').PasteBlock | null}
312
+ */
313
+ static fromHTML(el, { parseInline, parseChildren }) {
314
+ // Use 'summary' without :scope because happy-dom doesn't support :scope pseudo-class
315
+ const summary = /** @type {Element | null} */ ([...el.children].find(c => c.tagName.toLowerCase() === 'summary') || null);
316
+ const delta = summary ? parseInline(summary) : [];
317
+ const collapsed = !el.hasAttribute('open');
318
+ // Parse non-summary child elements as body blocks
319
+ const bodyEls = [...el.children].filter(c => c.tagName.toLowerCase() !== 'summary');
320
+ /** @type {import('../../utils/extensions.js').PasteBlock[]} */
321
+ let children = [];
322
+ if (parseChildren && bodyEls.length) {
323
+ // Create a temporary wrapper with only the non-summary children
324
+ const ownerDoc = el.ownerDocument;
325
+ if (ownerDoc) {
326
+ const wrapper = ownerDoc.createElement('div');
327
+ for (const bodyEl of bodyEls) {
328
+ wrapper.appendChild(bodyEl.cloneNode(true));
329
+ }
330
+ children = parseChildren(wrapper);
331
+ }
332
+ }
333
+ return { type: 'toggle', props: { collapsed }, delta, partial: false, ...(children.length ? { children } : {}) };
334
+ }
335
+
336
+ /**
337
+ * @param {Nabu} nabu
338
+ * @param {string} type
339
+ * @param {Record<string, any>} [props={}]
340
+ * @param {string|null} [parentId=null]
341
+ * @param {number|null} [index=null]
342
+ */
343
+ static create(nabu, type, props = {}, parentId = null, index = null) {
344
+ const node = nabu.tree.createNode(/** @type {import('loro-crdt').TreeID | undefined} */ (parentId || undefined), index ?? undefined);
345
+ /** @type {any} */ (node.data).set('type', 'toggle');
346
+ /** @type {any} */ (node.data).set('collapsed', props.collapsed ?? false);
347
+ const container = /** @type {any} */ (node.data).setContainer('text', new LoroText());
348
+ if (props.text) container.insert(0, props.text);
349
+ if (props.delta) container.applyDelta([...props.delta]);
350
+ return new ToggleBlock(nabu, /** @type {ToggleNode} */ (/** @type {unknown} */ (node)));
351
+ }
352
+ }