@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,181 @@
1
+ import { Block } from "../block.svelte.js";
2
+ import { Files } from "../../files/files.svelte.js";
3
+ import { sanitizeUrl } from "../../utils/html.js";
4
+ import { normalizeReason, defaultReason, reasonOf } from "../../files/status.js";
5
+
6
+ /** @import { FileReason, FileStatus } from "../../files/status.js"; */
7
+
8
+ /** @import { Nabu, NabuNode } from "../nabu.svelte.js"; */
9
+
10
+ /**
11
+ * Base class for void media blocks (image / video / file). Shares void behavior,
12
+ * reactive {ref, alt, status, name} metadata, Files display resolution, and the
13
+ * optimistic preview. Subclasses provide component, serializers, and static rules.
14
+ */
15
+ // @ts-ignore — static-side variance on create()
16
+ export class MediaBlock extends Block {
17
+ /** @param {Nabu} nabu @param {NabuNode} node */
18
+ constructor(nabu, node) {
19
+ super(nabu, node);
20
+ // Eagerly ensure the Files system exists here (a safe, non-reactive context)
21
+ // so `displaySrc` — read inside a $derived during render — only READS it.
22
+ // Lazily creating it in displaySrc would mutate nabu.systems (a SvelteMap)
23
+ // during a derived → Svelte's state_unsafe_mutation error.
24
+ const files = Files.get(nabu);
25
+ const data = /** @type {any} */ (node.data);
26
+ this.ref = data.get('ref') || '';
27
+ files.warm(this.ref); // small files: bytes ready for a (synchronous) copy / export
28
+ this.alt = data.get('alt') || '';
29
+ this.status = data.get('status') || 'ready';
30
+ this.#reason = normalizeReason(data.get('reason'));
31
+ this.name = data.get('name') || '';
32
+ data.subscribe(() => {
33
+ this.ref = data.get('ref') || '';
34
+ this.alt = data.get('alt') || '';
35
+ this.status = data.get('status') || 'ready';
36
+ this.#reason = normalizeReason(data.get('reason'));
37
+ this.name = data.get('name') || '';
38
+ files.warm(this.ref);
39
+ // the optimistic preview is only for the time the file is being stored / sent
40
+ if (this.status !== 'pending' && this.previewUrl) {
41
+ if (this.previewUrl.startsWith('blob:')) URL.revokeObjectURL(this.previewUrl);
42
+ this.previewUrl = null;
43
+ }
44
+ });
45
+ }
46
+
47
+ /** @type {string} */ ref = $state('');
48
+ /** @type {string} */ alt = $state('');
49
+ /**
50
+ * What the block stores (see `files/status.js`): `ready` | `pending` (bytes being stored / uploaded; `progress` follows the
51
+ * upload) | `error` (upload failed or cancelled; the local copy is kept, `retry()` sends it again) | `missing` (no source has
52
+ * the bytes) | `rejected` (refused, e.g. over `files.maxFileSize`; no ref). A `pending` block that no upload is running for and
53
+ * whose file is neither local nor at the host is SHOWN as missing, see `isMissing`.
54
+ * @type {string}
55
+ */ status = $state('ready');
56
+ /** @type {FileReason | null} */ #reason = $state(null);
57
+ /** @type {string} */ name = $state('');
58
+ /** Transient local objectURL while pending (not persisted). @type {string | null} */ previewUrl = $state(null);
59
+
60
+ positions = $derived(1);
61
+
62
+ /**
63
+ * Whether the file cannot be shown: flagged `missing` (pasted without its bytes), or a `nabu-asset:` ref that neither the
64
+ * local store nor the document's index can provide (e.g. the document was opened on another device). Reactive.
65
+ * @returns {boolean}
66
+ */
67
+ get isMissing() {
68
+ if (this.status === 'missing') return true;
69
+ if (this.status !== 'ready' && this.status !== 'pending') return false;
70
+ const files = Files.get(this.nabu);
71
+ return files.state(this.ref) === 'missing' && !files.isUploading(this.ref);
72
+ }
73
+
74
+ /**
75
+ * The status to SHOW: `missing` when the file cannot be displayed (see `isMissing`: includes a `pending` block whose upload
76
+ * was interrupted and is not available on this device), else the stored `status`. Reactive.
77
+ * @returns {string}
78
+ */
79
+ get displayStatus() { return this.isMissing ? 'missing' : this.status; }
80
+
81
+ /**
82
+ * Why the block is `error`, `rejected` or `missing`: `{ code, message }` (a stable code and a readable English sentence), `null`
83
+ * otherwise. A `pending` block shown as missing has the code `interrupted`. Reactive.
84
+ * @returns {FileReason | null}
85
+ */
86
+ get reason() {
87
+ if (this.status === 'pending') return this.isMissing ? reasonOf('interrupted') : null;
88
+ if (this.status === 'ready') return this.isMissing ? reasonOf('unavailable') : null;
89
+ return this.#reason ?? defaultReason(this.status);
90
+ }
91
+
92
+ /**
93
+ * Upload progress, `0..1` while the file is being sent to the host (`files.onBeforePersist`), `null` otherwise. Reactive.
94
+ * @returns {number | null}
95
+ */
96
+ get progress() { return Files.get(this.nabu).progress(this.ref); }
97
+
98
+ /**
99
+ * Send the file to the host again from its local copy (after `error`, including a cancelled upload). Resolves `true` when
100
+ * the file ended up at the host, `false` when there is nothing to retry (no host, bytes gone, not a local file).
101
+ * @returns {Promise<boolean>}
102
+ */
103
+ retry() { return Files.get(this.nabu).retry(this.ref); }
104
+
105
+ /**
106
+ * Abort the running upload (the host's `signal` fires). The block becomes `error` with the reason `cancelled`, the local copy
107
+ * is kept. Uploads are per file: every block showing the same file is affected.
108
+ * @returns {boolean} whether an upload was running
109
+ */
110
+ cancel() { return Files.get(this.nabu).cancel(this.ref); }
111
+
112
+ /**
113
+ * For the component's effect: keeps the display URL of this block alive while it is mounted (an unused objectURL is
114
+ * revoked). Returns the release function, so it can be used as `$effect(() => block.holdSource())`.
115
+ * @returns {() => void}
116
+ */
117
+ holdSource() { return Files.get(this.nabu).hold(this.ref); }
118
+
119
+ /** @param {number} offset @returns {{node: Node, offset: number} | null} */
120
+ getDOMPoint(offset) { return null; }
121
+
122
+ /** @param {InputEvent} event */
123
+ beforeinput(event) { return false; }
124
+
125
+ /** Resolved display URL. While pending → local preview; else resolve the ref. @returns {string} */
126
+ get displaySrc() {
127
+ // Render guard: only a media-safe URL (http(s), blob:, nabu-asset:, data:image|video, relative) reaches
128
+ // a `src` / `href` — a document loaded with `javascript:` in `ref` renders as "no media".
129
+ // `resolve` is called first on purpose: it starts loading the stored file, so the switch from the local preview to the
130
+ // stored copy (when the block stops being pending) does not flash an empty image.
131
+ const resolved = Files.get(this.nabu).resolve(this.ref);
132
+ if (this.status === 'pending' && this.previewUrl) return sanitizeUrl(this.previewUrl, { kind: 'media' }) ?? '';
133
+ return sanitizeUrl(resolved, { kind: 'media' }) ?? '';
134
+ }
135
+
136
+ /**
137
+ * The props a DOCUMENT export (`serialize('json')` / `'markdown'`) writes: `ref`, `alt`, `name`, and `status` (+ `reason`)
138
+ * when it is not `ready`. A `pending` block whose bytes are still being stored has no durable ref yet: its transient
139
+ * `nabu-pending:` ref (or empty one) is NOT exported as if it were final, the block is exported as `missing` (label
140
+ * kept, no source) so the import shows the "Missing file" state instead of a block stuck pending forever. A `pending`
141
+ * block with a hash ref (being uploaded) exports as a normal file: the export carries its bytes.
142
+ * @returns {{ ref: string, alt: string, name: string, status?: string, reason?: FileReason }}
143
+ */
144
+ get exportProps() {
145
+ const props = { ref: this.ref, alt: this.alt, name: this.name };
146
+ if (this.status === 'pending') {
147
+ if (!this.ref || this.ref.startsWith('nabu-pending:')) return { ...props, ref: '', status: 'missing' };
148
+ return props;
149
+ }
150
+ if (this.status === 'ready') return props;
151
+ const reason = this.#reason;
152
+ return { ...props, status: this.status, ...(reason ? { reason } : {}) };
153
+ }
154
+
155
+ /**
156
+ * What a COPY (`application/x-nabu+json`) carries of the file state: nothing, except a `rejected` block (so the refusal and
157
+ * its reason travel with it). Upload state (`pending`, `error`) never travels: the receiver works it out.
158
+ * @returns {{ status?: string, reason?: FileReason }}
159
+ */
160
+ get clipboardState() {
161
+ return this.status === 'rejected' ? { status: 'rejected', reason: this.reason ?? undefined } : {};
162
+ }
163
+
164
+ /** @param {string} v */
165
+ setAlt(v) { this.alt = v; /** @type {any} */ (this.node.data).set('alt', v); this.commit(); }
166
+
167
+ /**
168
+ * Shared node factory. Subclasses call this from their static create().
169
+ * @param {Nabu} nabu @param {string} type @param {{ref?:string,alt?:string,status?:string,name?:string,reason?:FileReason}} [props] @param {string|null} [parentId] @param {number|null} [index]
170
+ * @returns {any}
171
+ */
172
+ static createNode(nabu, type, props = {}, parentId = null, index = null) {
173
+ const node = nabu.tree.createNode(/** @type {import('loro-crdt').TreeID | undefined} */ (parentId || undefined), index ?? undefined);
174
+ const d = /** @type {any} */ (node.data);
175
+ d.set('type', type); d.set('ref', props.ref || ''); d.set('alt', props.alt || '');
176
+ d.set('status', props.status || 'ready'); d.set('name', props.name || '');
177
+ const reason = normalizeReason(props.reason);
178
+ if (reason) d.set('reason', reason);
179
+ return node;
180
+ }
181
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Insert a media block of `type` from a local File. The block appears at once (`pending`, with a local preview), the bytes
3
+ * are hashed and written to the local store (ADR 014), then the block gets its `nabu-asset:sha256-…` ref: `ready`, or — with a
4
+ * host (`files.onBeforePersist`) — still `pending` while the background upload runs (`block.progress`, `block.retry()`,
5
+ * `block.cancel()`); the ref never changes afterwards. A file over `files.maxFileSize` is refused: the block is created
6
+ * `rejected`, with no ref and a readable `reason`.
7
+ * @param {import('../nabu.svelte.js').Nabu} nabu @param {string} type @param {File} file
8
+ */
9
+ export function insertMediaFromFile(nabu: import("../nabu.svelte.js").Nabu, type: string, file: File): Promise<void>;
10
+ /**
11
+ * Open a file picker (accept filter) and insert the chosen media as `type`.
12
+ * @param {import('../nabu.svelte.js').Nabu} nabu @param {string} type @param {string} accept
13
+ */
14
+ export function pickMedia(nabu: import("../nabu.svelte.js").Nabu, type: string, accept: string): void;
15
+ /**
16
+ * Insert a media block of `type` from an external URL ref.
17
+ * @param {import('../nabu.svelte.js').Nabu} nabu @param {string} type @param {{ref:string, alt?:string, name?:string}} data
18
+ */
19
+ export function insertMediaUrl(nabu: import("../nabu.svelte.js").Nabu, type: string, data: {
20
+ ref: string;
21
+ alt?: string;
22
+ name?: string;
23
+ }): Promise<void> | undefined;
24
+ export function mediaFileHandler(type: string, accepts: (file: File) => boolean, priority?: number): import("../../utils/extensions.js").PasteFileHandler;
@@ -0,0 +1,88 @@
1
+ import { Files, FileRejectedError } from "../../files/files.svelte.js";
2
+ import { reasonOf } from "../../files/status.js";
3
+
4
+ /**
5
+ * Insert a media block of `type` from a local File. The block appears at once (`pending`, with a local preview), the bytes
6
+ * are hashed and written to the local store (ADR 014), then the block gets its `nabu-asset:sha256-…` ref: `ready`, or — with a
7
+ * host (`files.onBeforePersist`) — still `pending` while the background upload runs (`block.progress`, `block.retry()`,
8
+ * `block.cancel()`); the ref never changes afterwards. A file over `files.maxFileSize` is refused: the block is created
9
+ * `rejected`, with no ref and a readable `reason`.
10
+ * @param {import('../nabu.svelte.js').Nabu} nabu @param {string} type @param {File} file
11
+ */
12
+ export async function insertMediaFromFile(nabu, type, file) {
13
+ const BlockClass = /** @type {any} */ (nabu.registry.get(type));
14
+ if (!BlockClass) return;
15
+ const files = Files.get(nabu);
16
+ const anchor = nabu.selection.anchorBlock;
17
+ const parentId = anchor?.node.parent()?.id.toString() ?? null;
18
+ const idx = (anchor?.node.index() ?? -1) + 1;
19
+ const rejection = files.checkSize(file.size);
20
+ if (rejection) {
21
+ nabu.warn(`file "${file.name}" refused: ${rejection.message}`);
22
+ BlockClass.create(nabu, type, { status: 'rejected', name: file.name, reason: rejection }, parentId, idx >= 0 ? idx : null);
23
+ await nabu.commit();
24
+ return;
25
+ }
26
+ const block = BlockClass.create(nabu, type, { status: 'pending', name: file.name }, parentId, idx >= 0 ? idx : null);
27
+ block.previewUrl = URL.createObjectURL(file);
28
+ await nabu.commit();
29
+ const id = block.id;
30
+ try {
31
+ // lot 3: drag and drop calls this same path.
32
+ const { id: hash, ref } = await files.ingest(file);
33
+ const fresh = nabu.blocks.get(id);
34
+ if (fresh) {
35
+ const d = /** @type {any} */ (fresh.node.data);
36
+ d.set('ref', ref);
37
+ d.set('status', files.initialStatus(hash)); // with a host: stays pending, the upload queue takes over
38
+ await nabu.commit();
39
+ }
40
+ } catch (error) {
41
+ const fresh = nabu.blocks.get(id);
42
+ if (fresh) {
43
+ const d = /** @type {any} */ (fresh.node.data);
44
+ if (error instanceof FileRejectedError) { d.set('status', 'rejected'); d.set('reason', error.reason); } else { d.set('status', 'error'); d.set('reason', reasonOf('upload-failed')); }
45
+ await nabu.commit();
46
+ }
47
+ }
48
+ }
49
+
50
+ /**
51
+ * Open a file picker (accept filter) and insert the chosen media as `type`.
52
+ * @param {import('../nabu.svelte.js').Nabu} nabu @param {string} type @param {string} accept
53
+ */
54
+ export function pickMedia(nabu, type, accept) {
55
+ const input = document.createElement('input');
56
+ input.type = 'file'; input.accept = accept;
57
+ input.onchange = () => { const f = input.files?.[0]; if (f) insertMediaFromFile(nabu, type, f); };
58
+ input.click();
59
+ }
60
+
61
+ /**
62
+ * Insert a media block of `type` from an external URL ref.
63
+ * @param {import('../nabu.svelte.js').Nabu} nabu @param {string} type @param {{ref:string, alt?:string, name?:string}} data
64
+ */
65
+ export function insertMediaUrl(nabu, type, data) {
66
+ if (!data?.ref) return;
67
+ const BlockClass = /** @type {any} */ (nabu.registry.get(type));
68
+ if (!BlockClass) return;
69
+ const anchor = nabu.selection.anchorBlock;
70
+ const parentId = anchor?.node.parent()?.id.toString() ?? null;
71
+ const idx = (anchor?.node.index() ?? -1) + 1;
72
+ BlockClass.create(nabu, type, { ref: data.ref, alt: data.alt || '', name: data.name || '', status: 'ready' }, parentId, idx >= 0 ? idx : null);
73
+ return nabu.commit();
74
+ }
75
+
76
+ /**
77
+ * The paste-file handler of a media block (`Extension.pasteFileHandlers`): a pasted file the block recognises becomes a
78
+ * block of `type` named after the file; the engine stores the bytes and sets the `ref`.
79
+ * @param {string} type block type
80
+ * @param {(file: File) => boolean} accepts
81
+ * @param {number} [priority] higher is asked first: the specific kinds (image, video) before the catch-all file
82
+ * @returns {import('../../utils/extensions.js').PasteFileHandler}
83
+ */
84
+ export const mediaFileHandler = (type, accepts, priority = 0) => ({
85
+ priority,
86
+ accepts: (file) => accepts(file),
87
+ block: (file) => ({ type, partial: false, props: { name: file.name || '', alt: '' } }),
88
+ });
@@ -1,38 +1,69 @@
1
1
  /**
2
2
  * @import { Nabu, NabuNode } from "./nabu.svelte.js";
3
3
  */
4
+ /**
5
+ * A block that holds child blocks (a container): list, list item, toggle, region. It keeps its `children` in sync with the tree and delegates positions, word count and event handling to them.
6
+ */
4
7
  export class MegaBlock extends Block {
5
- /** @type {Block[]} */
6
- children: Block[];
7
- updateChildren(): void;
8
- /** @param {Block[]} children @param {number | null} [index] */
9
- adoptChildren(children: Block[], index?: number | null): void;
8
+ /**
9
+ * @type {Block[]}
10
+ * @public
11
+ */
12
+ public children: Block[];
13
+ /**
14
+ * @param {Set<string>} [retyped] ids whose `type` just changed; without it, the type of every child is checked
15
+ * @public
16
+ */
17
+ public updateChildren(retyped?: Set<string>): void;
18
+ /**
19
+ * @param {Block[]} children
20
+ * @param {number | null} [index]
21
+ * @public
22
+ */
23
+ public adoptChildren(children: Block[], index?: number | null): void;
10
24
  /**
11
25
  * Props to include in the clipboard representation of this container block.
12
- * Override in subclasses that carry structural props (e.g. List → { listType }).
26
+ * Override in subclasses that carry structural props (e.g. List -> { listType }, ListItem -> { checked },
27
+ * Toggle -> { collapsed }).
28
+ * @returns {Record<string, any>}
29
+ * @public
30
+ */
31
+ public get clipboardProps(): Record<string, any>;
32
+ /**
33
+ * Clipboard props when the copied slice starts at `firstChildIndex` (the children before it are NOT copied).
34
+ * Containers whose props depend on their first copied child (an ordered list's `start`) override this.
35
+ * @param {number} firstChildIndex
13
36
  * @returns {Record<string, any>}
37
+ * @public
14
38
  */
15
- get clipboardProps(): Record<string, any>;
39
+ public clipboardPropsFrom(firstChildIndex: number): Record<string, any>;
16
40
  /**
17
- * Serializes the portion of this MegaBlock that falls within the given
18
- * selection boundaries, recursively.
41
+ * Serializes the portion of this MegaBlock that falls within the given selection boundaries, recursively.
19
42
  *
20
- * For hybrid blocks (MegaBlock + text behavior, like ListItem):
21
- * if startBlock or endBlock IS this block, serialize text directly.
22
- * if neither boundary is set, this block is fully selected — serialize all text.
43
+ * The context carries the selection boundary blocks that belong to THIS subtree (`startBlock` / `endBlock`, null =
44
+ * the selection extends past this block on that side, i.e. it is fully selected on that side). Document order of a
45
+ * block is: own text (hybrid blocks only), then its children. So:
46
+ * - own text is copied unless the selection starts inside a child; it is clipped to `startFrom` when this block IS
47
+ * the start boundary, and to `endTo` when it IS the end boundary;
48
+ * - children are copied unless the selection ends in the own text; only those between the child holding the start
49
+ * boundary and the child holding the end boundary (inclusive) are kept, each one recursively clipped.
50
+ * A pure container (no text) simply has no own-text part. Nothing here depends on a concrete block type.
23
51
  *
24
- * For pure containers (like List):
25
- * find direct children that contain/are the boundaries, slice children in range,
26
- * recurse into each with propagated context.
52
+ * With `textOnly` (the context of a TEXT selection: copy, cut, drag), a block that ISOLATES its text (`isolatesSelection`) is
53
+ * transparent to a selection that is not entirely inside it: its own text is not copied and its container is not created, its
54
+ * selected children are returned directly (an ARRAY, to be spliced in the list of its siblings). Without it (the structure of
55
+ * a whole block), the container is serialized as it is.
27
56
  *
28
- * @param {{ startBlock?: Block|null, startFrom?: number, endBlock?: Block|null, endTo?: number|null }} [ctx]
29
- * @returns {import('../utils/extensions.js').PasteBlock | null}
57
+ * @param {{ startBlock?: Block|null, startFrom?: number, endBlock?: Block|null, endTo?: number|null, textOnly?: boolean }} [ctx]
58
+ * @returns {import('../utils/extensions.js').PasteBlock | import('../utils/extensions.js').PasteBlock[] | null}
59
+ * @public
30
60
  */
31
- serializeForClipboard({ startBlock, startFrom, endBlock, endTo }?: {
61
+ public serializeForClipboard({ startBlock, startFrom, endBlock, endTo, textOnly }?: {
32
62
  startBlock?: Block | null;
33
63
  startFrom?: number;
34
64
  endBlock?: Block | null;
35
65
  endTo?: number | null;
36
- }): import("../utils/extensions.js").PasteBlock | null;
66
+ textOnly?: boolean;
67
+ }): import("../utils/extensions.js").PasteBlock | import("../utils/extensions.js").PasteBlock[] | null;
37
68
  }
38
69
  import { Block } from "./block.svelte.js";
@@ -5,6 +5,9 @@ import { handleContainerBeforeInput, findDirectChildOf } from "./container.utils
5
5
  * @import { Nabu, NabuNode } from "./nabu.svelte.js";
6
6
  */
7
7
 
8
+ /**
9
+ * A block that holds child blocks (a container): list, list item, toggle, region. It keeps its `children` in sync with the tree and delegates positions, word count and event handling to them.
10
+ */
8
11
  export class MegaBlock extends Block {
9
12
 
10
13
  /** @param {Nabu} nabu @param {NabuNode} node */
@@ -13,14 +16,21 @@ export class MegaBlock extends Block {
13
16
  this.updateChildren();
14
17
  }
15
18
 
16
- /** @type {Block[]} */
19
+ /**
20
+ * @type {Block[]}
21
+ * @public
22
+ */
17
23
  children = $state([]);
24
+ /** @public */
18
25
  positions = $derived(this.children.reduce((sum, child) => sum + (child.positions ?? 0), 0));
26
+ /** @public */
27
+ wordCount = $derived(this.children.reduce((sum, c) => sum + (c.wordCount ?? 0), 0));
19
28
 
20
29
  /**
21
30
  * Pure container: no own text, delegate entirely to children.
22
31
  * @param {number} localOffset
23
32
  * @returns {{ block: Block, offset: number } | null}
33
+ * @public
24
34
  */
25
35
  resolveOffset(localOffset) {
26
36
  let remaining = localOffset;
@@ -34,20 +44,26 @@ export class MegaBlock extends Block {
34
44
  return last ? last.resolveOffset(Math.max(0, (last.positions ?? 1) - 1)) : null;
35
45
  }
36
46
 
37
- updateChildren() {
47
+ /**
48
+ * @param {Set<string>} [retyped] ids whose `type` just changed; without it, the type of every child is checked
49
+ * @public
50
+ */
51
+ updateChildren(retyped) {
38
52
  const childrenNodes = this.node.children();
39
53
  if (!childrenNodes) return;
40
54
  this.children = childrenNodes.map((childNode, i) => {
41
55
  const id = childNode.id.toString();
42
56
  let block = this.nabu.blocks.get(id);
43
- const currentType = childNode.data.get("type");
44
57
 
45
- if (!block || block.type !== currentType) {
46
- if (block) {
47
- this.nabu.blocks.delete(id);
48
- this.nabu.blocksByType.get(block.type)?.delete(block);
58
+ if (!block || !retyped || retyped.has(id)) {
59
+ const currentType = childNode.data.get("type");
60
+ if (!block || block.type !== currentType) {
61
+ if (block) {
62
+ this.nabu.blocks.delete(id);
63
+ this.nabu.blocksByType.get(block.type)?.delete(block);
64
+ }
65
+ block = Block.load(this.nabu, childNode);
49
66
  }
50
- block = Block.load(this.nabu, childNode);
51
67
  }
52
68
  block.parent = this;
53
69
  block.index = i;
@@ -56,7 +72,11 @@ export class MegaBlock extends Block {
56
72
  }
57
73
 
58
74
 
59
- /** @param {Block[]} children @param {number | null} [index] */
75
+ /**
76
+ * @param {Block[]} children
77
+ * @param {number | null} [index]
78
+ * @public
79
+ */
60
80
  adoptChildren(children, index = null) {
61
81
  // Reverse so that when index is specified, children are inserted in original
62
82
  // order (each insert at the same position pushes others down, so reverse of
@@ -69,92 +89,113 @@ export class MegaBlock extends Block {
69
89
  }
70
90
 
71
91
 
72
- /** @param {InputEvent} event */
92
+ /**
93
+ * @param {InputEvent} event
94
+ * @public
95
+ */
73
96
  beforeinput(event) {
74
97
  return handleContainerBeforeInput(this, this.nabu, event);
75
98
  }
76
99
 
77
100
  /**
78
101
  * Props to include in the clipboard representation of this container block.
79
- * Override in subclasses that carry structural props (e.g. List → { listType }).
102
+ * Override in subclasses that carry structural props (e.g. List -> { listType }, ListItem -> { checked },
103
+ * Toggle -> { collapsed }).
80
104
  * @returns {Record<string, any>}
105
+ * @public
81
106
  */
82
107
  get clipboardProps() { return {}; }
83
108
 
84
109
  /**
85
- * Serializes the portion of this MegaBlock that falls within the given
86
- * selection boundaries, recursively.
110
+ * Clipboard props when the copied slice starts at `firstChildIndex` (the children before it are NOT copied).
111
+ * Containers whose props depend on their first copied child (an ordered list's `start`) override this.
112
+ * @param {number} firstChildIndex
113
+ * @returns {Record<string, any>}
114
+ * @public
115
+ */
116
+ clipboardPropsFrom(firstChildIndex) { return this.clipboardProps; }
117
+
118
+ /**
119
+ * Serializes the portion of this MegaBlock that falls within the given selection boundaries, recursively.
87
120
  *
88
- * For hybrid blocks (MegaBlock + text behavior, like ListItem):
89
- * if startBlock or endBlock IS this block, serialize text directly.
90
- * if neither boundary is set, this block is fully selected — serialize all text.
121
+ * The context carries the selection boundary blocks that belong to THIS subtree (`startBlock` / `endBlock`, null =
122
+ * the selection extends past this block on that side, i.e. it is fully selected on that side). Document order of a
123
+ * block is: own text (hybrid blocks only), then its children. So:
124
+ * - own text is copied unless the selection starts inside a child; it is clipped to `startFrom` when this block IS
125
+ * the start boundary, and to `endTo` when it IS the end boundary;
126
+ * - children are copied unless the selection ends in the own text; only those between the child holding the start
127
+ * boundary and the child holding the end boundary (inclusive) are kept, each one recursively clipped.
128
+ * A pure container (no text) simply has no own-text part. Nothing here depends on a concrete block type.
91
129
  *
92
- * For pure containers (like List):
93
- * find direct children that contain/are the boundaries, slice children in range,
94
- * recurse into each with propagated context.
130
+ * With `textOnly` (the context of a TEXT selection: copy, cut, drag), a block that ISOLATES its text (`isolatesSelection`) is
131
+ * transparent to a selection that is not entirely inside it: its own text is not copied and its container is not created, its
132
+ * selected children are returned directly (an ARRAY, to be spliced in the list of its siblings). Without it (the structure of
133
+ * a whole block), the container is serialized as it is.
95
134
  *
96
- * @param {{ startBlock?: Block|null, startFrom?: number, endBlock?: Block|null, endTo?: number|null }} [ctx]
97
- * @returns {import('../utils/extensions.js').PasteBlock | null}
135
+ * @param {{ startBlock?: Block|null, startFrom?: number, endBlock?: Block|null, endTo?: number|null, textOnly?: boolean }} [ctx]
136
+ * @returns {import('../utils/extensions.js').PasteBlock | import('../utils/extensions.js').PasteBlock[] | null}
137
+ * @public
98
138
  */
99
- serializeForClipboard({ startBlock = null, startFrom = 0, endBlock = null, endTo = null } = {}) {
139
+ serializeForClipboard({ startBlock = null, startFrom = 0, endBlock = null, endTo = null, textOnly = false } = {}) {
100
140
  const tb = this.behaviors?.get('text');
101
141
  const imStart = startBlock === this;
102
142
  const imEnd = endBlock === this;
103
-
104
- // ── Hybrid block (has text): this block IS a selection boundary or fully selected ──
105
- if (tb && (imStart || imEnd || (!startBlock && !endBlock))) {
143
+ const childOfThis = (/** @type {Block|null} */ block) => {
144
+ for (let cur = block; cur; cur = cur.parent ?? null) if (cur.parent === this) return cur;
145
+ return null;
146
+ };
147
+ const startChild = startBlock && !imStart ? childOfThis(startBlock) : null;
148
+ const endChild = endBlock && !imEnd ? childOfThis(endBlock) : null;
149
+
150
+ const isolated = textOnly && this.isolatesSelection && !(imStart && imEnd);
151
+ const textIncluded = !!tb && !startChild && !isolated;
152
+ const childrenIncluded = !imEnd && !!this.children?.length;
153
+ if (!textIncluded && imEnd) return null;
154
+
155
+ // ── Own text ──
156
+ /** @type {import('../utils/extensions.js').PasteBlock | null} */
157
+ let result = null;
158
+ if (tb && textIncluded) {
159
+ const length = tb.container.length;
106
160
  const from = imStart ? startFrom : 0;
107
- const to = imEnd ? endTo : tb.container.length;
161
+ const to = imEnd ? (endTo ?? length) : length;
108
162
  const partial = imStart && imEnd ? 'both' : imStart ? 'start' : imEnd ? 'end' : false;
109
- const delta = tb.container.sliceDelta(from, to ?? tb.container.length);
110
- const result = /** @type {import('../utils/extensions.js').PasteBlock} */ (
111
- { type: this.type, props: this.clipboardProps, partial, delta }
163
+ result = /** @type {import('../utils/extensions.js').PasteBlock} */ (
164
+ { type: this.type, props: this.clipboardProps, partial, delta: tb.container.sliceDelta(from, to) }
112
165
  );
113
- // If fully selected and has children (e.g. ListItem with sublist), include them too
114
- if (!imStart && !imEnd && this.children?.length) {
115
- const childrenSerialized = this.children.map(child =>
116
- typeof child.serializeForClipboard === 'function'
117
- ? child.serializeForClipboard({})
118
- : child.serialize('application/x-nabu+json', { partial: false })
119
- ).filter(Boolean);
120
- if (childrenSerialized.length) result.children = childrenSerialized;
121
- }
122
- return result;
123
166
  }
124
167
 
125
- // ── Pure container: find relevant children by walking up from boundaries ──
126
- if (!this.children?.length) return null;
127
-
128
- const sc = (startBlock ? findDirectChildOf(startBlock, this) : null) ?? this.children[0];
129
- const ec = (endBlock ? findDirectChildOf(endBlock, this) : null) ?? this.children.at(-1);
130
- if (!sc || !ec) return null;
131
-
132
- const si = this.children.indexOf(sc);
133
- const ei = this.children.indexOf(ec);
134
- if (si === -1 || ei === -1) return null;
135
-
136
- const relevant = this.children.slice(si, ei + 1);
137
- const lastI = relevant.length - 1;
138
-
139
- const children = relevant.map((child, i) => {
140
- const isFirst = i === 0;
141
- const isLast = i === lastI;
142
- const childCtx = {
143
- startBlock: isFirst ? startBlock : null,
144
- startFrom: isFirst ? startFrom : 0,
145
- endBlock: isLast ? endBlock : null,
146
- endTo: isLast ? endTo : null,
147
- };
148
- if (typeof child.serializeForClipboard === 'function') {
149
- return child.serializeForClipboard(childCtx);
150
- }
151
- return child.serialize('application/x-nabu+json', {
152
- from: isFirst && startBlock ? startFrom : 0,
153
- to: isLast && endBlock ? endTo : null,
154
- partial: isFirst && isLast ? 'both' : isFirst ? 'start' : isLast ? 'end' : false,
155
- });
156
- }).filter(Boolean);
157
-
158
- return children.length ? { type: this.type, props: this.clipboardProps, children } : null;
168
+ // ── Children inside the selection ──
169
+ /** @type {any[]} */
170
+ let kids = [];
171
+ let firstIndex = 0;
172
+ if (childrenIncluded) {
173
+ const si = startChild ? this.children.indexOf(/** @type {Block} */ (startChild)) : 0;
174
+ const ei = endChild ? this.children.indexOf(/** @type {Block} */ (endChild)) : this.children.length - 1;
175
+ if (si === -1 || ei === -1 || si > ei) return result;
176
+ firstIndex = si;
177
+ const relevant = this.children.slice(si, ei + 1);
178
+ const lastI = relevant.length - 1;
179
+ kids = relevant.map((child, i) => {
180
+ const cs = i === 0 && startChild ? startBlock : null;
181
+ const ce = i === lastI && endChild ? endBlock : null;
182
+ const mb = /** @type {any} */ (child);
183
+ if (typeof mb.serializeForClipboard === 'function') {
184
+ return mb.serializeForClipboard({ startBlock: cs, startFrom: cs ? startFrom : 0, endBlock: ce, endTo: ce ? endTo : null, textOnly });
185
+ }
186
+ return child.serialize('application/x-nabu+json', {
187
+ from: cs ? startFrom : 0,
188
+ to: ce ? endTo : null,
189
+ partial: cs && ce ? 'both' : cs ? 'start' : ce ? 'end' : false,
190
+ });
191
+ }).flat().filter(Boolean);
192
+ }
193
+
194
+ if (result) {
195
+ if (kids.length) result.children = kids;
196
+ return result;
197
+ }
198
+ if (isolated) return /** @type {import('../utils/extensions.js').PasteBlock[]} */ (kids);
199
+ return kids.length ? { type: this.type, props: this.clipboardPropsFrom(firstIndex), children: kids } : null;
159
200
  }
160
- }
201
+ }