@aionbuilders/nabu 0.1.0-beta.0 → 0.1.1

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 (308) hide show
  1. package/CHANGELOG.md +531 -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 +195 -55
  12. package/dist/behaviors/text/text.behavior.svelte.js +531 -103
  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 +32 -18
  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 +1753 -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 +35 -14
  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 +55 -8
  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/composition.d.ts +28 -0
  253. package/dist/utils/composition.js +51 -0
  254. package/dist/utils/dom-scope.d.ts +14 -0
  255. package/dist/utils/dom-scope.js +20 -0
  256. package/dist/utils/drop.d.ts +160 -0
  257. package/dist/utils/drop.js +305 -0
  258. package/dist/utils/extensions.d.ts +439 -45
  259. package/dist/utils/extensions.js +271 -29
  260. package/dist/utils/html.d.ts +44 -0
  261. package/dist/utils/html.js +183 -0
  262. package/dist/utils/index.d.ts +3 -0
  263. package/dist/utils/index.js +3 -0
  264. package/dist/utils/keys.d.ts +120 -0
  265. package/dist/utils/keys.js +248 -0
  266. package/dist/utils/links.d.ts +22 -0
  267. package/dist/utils/links.js +34 -0
  268. package/dist/utils/placeholders.d.ts +81 -3
  269. package/dist/utils/placeholders.js +73 -3
  270. package/dist/utils/selection.svelte.d.ts +48 -20
  271. package/dist/utils/selection.svelte.js +60 -10
  272. package/dist/utils/spellcheck.d.ts +9 -0
  273. package/dist/utils/spellcheck.js +17 -0
  274. package/dist/utils/tab.d.ts +31 -0
  275. package/dist/utils/tab.js +117 -0
  276. package/dist/utils/words.d.ts +42 -0
  277. package/dist/utils/words.js +147 -0
  278. package/dist/vite.d.ts +9 -3
  279. package/dist/vite.js +19 -8
  280. package/package.json +56 -20
  281. package/dist/blocks/variable.svelte.d.ts +0 -47
  282. package/dist/blocks/variable.svelte.js +0 -51
  283. package/dist/blocks/zone/TemplateEditor.svelte +0 -308
  284. package/dist/blocks/zone/TemplateEditor.svelte.d.ts +0 -11
  285. package/dist/blocks/zone/Zone.svelte +0 -272
  286. package/dist/blocks/zone/Zone.svelte.d.ts +0 -11
  287. package/dist/blocks/zone/ZoneEditor.svelte +0 -289
  288. package/dist/blocks/zone/ZoneEditor.svelte.d.ts +0 -11
  289. package/dist/blocks/zone/ZoneTemplatesList.svelte +0 -188
  290. package/dist/blocks/zone/ZoneTemplatesList.svelte.d.ts +0 -11
  291. package/dist/blocks/zone/hooks/onBeforeInput.hook.d.ts +0 -3
  292. package/dist/blocks/zone/hooks/onBeforeInput.hook.js +0 -30
  293. package/dist/blocks/zone/index.d.ts +0 -2
  294. package/dist/blocks/zone/index.js +0 -2
  295. package/dist/blocks/zone/zone.extension.d.ts +0 -2
  296. package/dist/blocks/zone/zone.extension.js +0 -237
  297. package/dist/blocks/zone/zone.svelte.d.ts +0 -221
  298. package/dist/blocks/zone/zone.svelte.js +0 -608
  299. package/dist/overlays/context/Context.svelte +0 -182
  300. package/dist/overlays/context/Context.svelte.d.ts +0 -17
  301. package/dist/overlays/context/collect.d.ts +0 -17
  302. package/dist/overlays/context/collect.js +0 -33
  303. package/dist/overlays/context/context.extension.d.ts +0 -2
  304. package/dist/overlays/context/context.extension.js +0 -8
  305. package/dist/overlays/context/context.svelte.d.ts +0 -82
  306. package/dist/overlays/context/context.svelte.js +0 -159
  307. package/dist/overlays/context/index.d.ts +0 -3
  308. package/dist/overlays/context/index.js +0 -3
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Takes a top-level item out of its list and retypes it as `type` (a paragraph for the step back, any
3
+ * block for a type change: heading, quote, code…). Moves only; no commit. The `checked` flag is dropped,
4
+ * `props` are written on the node (e.g. a heading `level`).
5
+ * @param {Nabu} nabu
6
+ * @param {NabuNode} item
7
+ * @param {string} [type]
8
+ * @param {Record<string, any>} [props]
9
+ * @param {boolean} [numbered=true] whether the item counts as a numbered item of an ORDERED list (the tail
10
+ * resumes after it). False for a pure placeholder (a paste splitting the list): the tail resumes right where
11
+ * the items before it stop.
12
+ * @returns {boolean} whether the item was lifted (false when it is nested / not in a list)
13
+ */
14
+ export function liftItemTo(nabu: Nabu, item: NabuNode, type?: string, props?: Record<string, any>, numbered?: boolean): boolean;
15
+ /**
16
+ * Step a list item back one level (see the file header). Resolves once committed and the caret is back.
17
+ * @param {Nabu} nabu
18
+ * @param {NabuNode} item a list-item node
19
+ * @returns {Promise<boolean>} whether the structure changed
20
+ */
21
+ export function stepBack(nabu: Nabu, item: NabuNode): Promise<boolean>;
22
+ export function liftItemToParagraph(nabu: Nabu, item: NabuNode): boolean;
23
+ import type { Nabu } from "../nabu.svelte.js";
24
+ import type { NabuNode } from "../nabu.svelte.js";
@@ -0,0 +1,87 @@
1
+ /**
2
+ * "Step back one level" for a list item: Enter on an EMPTY item, Backspace at offset 0 of an item.
3
+ *
4
+ * - a NESTED item is unindented one level (exactly Shift+Tab: its following siblings become its children);
5
+ * - a TOP-LEVEL item leaves the list and becomes a paragraph (same node, so its id, text, marks and caret
6
+ * survive). The list is split around it: the items before stay, the items after go into a new list
7
+ * placed after the paragraph. The item's own sublists (it may have several, of different types) are moved out right after the paragraph, in order (nothing is
8
+ * lost, the relative nesting is kept). An ORDERED tail list keeps counting where the list left off
9
+ * (`start`), so `1. a 2. b 3. c` with b removed reads `1. a`, `b`, `3. c`.
10
+ *
11
+ * Moves on tree NODES only, one transaction (= one undo step), then the caret goes to offset 0.
12
+ *
13
+ * @import { Nabu, NabuNode } from "../nabu.svelte.js";
14
+ */
15
+
16
+ import { List, listStart } from "./list.svelte.js";
17
+ import { typeOf, kids, sublistsOf, dropIfEmpty, moveItems } from "./indent.js";
18
+
19
+ /**
20
+ * Takes a top-level item out of its list and retypes it as `type` (a paragraph for the step back, any
21
+ * block for a type change: heading, quote, code…). Moves only; no commit. The `checked` flag is dropped,
22
+ * `props` are written on the node (e.g. a heading `level`).
23
+ * @param {Nabu} nabu
24
+ * @param {NabuNode} item
25
+ * @param {string} [type]
26
+ * @param {Record<string, any>} [props]
27
+ * @param {boolean} [numbered=true] whether the item counts as a numbered item of an ORDERED list (the tail
28
+ * resumes after it). False for a pure placeholder (a paste splitting the list): the tail resumes right where
29
+ * the items before it stop.
30
+ * @returns {boolean} whether the item was lifted (false when it is nested / not in a list)
31
+ */
32
+ export function liftItemTo(nabu, item, type = "paragraph", props = {}, numbered = true) {
33
+ const list = item.parent();
34
+ if (!list || typeOf(list) !== "list" || typeOf(list.parent()) === "list-item") return false;
35
+
36
+ const index = item.index() ?? 0;
37
+ const followers = kids(list).slice(index + 1);
38
+ const own = sublistsOf(item);
39
+ const listType = /** @type {any} */ (list.data.get("listType")) || "bullet";
40
+ // The number the first follower had: the list's own start + the items before it (item included).
41
+ const tailStart = (listStart(listType, list.data.get("start")) ?? 1) + index + (numbered ? 1 : 0);
42
+ const parentId = list.parent()?.id.toString();
43
+
44
+ item.moveAfter(list);
45
+ /** @type {NabuNode} */
46
+ let anchor = item;
47
+ for (const sublist of own) {
48
+ sublist.moveAfter(anchor);
49
+ anchor = sublist;
50
+ }
51
+ if (followers.length) {
52
+ // List.create, not nabu.insert(): that commits, and the empty tail would be normalized away.
53
+ const tail = List.create(nabu, "list", { listType, start: tailStart }, parentId).node;
54
+ tail.moveAfter(anchor);
55
+ for (const follower of followers) follower.move(tail, kids(tail).length);
56
+ }
57
+ dropIfEmpty(nabu, list);
58
+
59
+ if (/** @type {any} */ (item.data).get("checked") !== undefined) /** @type {any} */ (item.data).delete("checked");
60
+ item.data.set("type", type);
61
+ for (const [key, value] of Object.entries(props)) /** @type {any} */ (item.data).set(key, value);
62
+ return true;
63
+ }
64
+
65
+ /**
66
+ * Takes a top-level item out of its list and retypes it as a paragraph. Moves only; no commit.
67
+ * @param {Nabu} nabu
68
+ * @param {NabuNode} item
69
+ * @returns {boolean} whether the item was lifted (false when it is nested / not in a list)
70
+ */
71
+ export const liftItemToParagraph = (nabu, item) => liftItemTo(nabu, item, "paragraph");
72
+
73
+ /**
74
+ * Step a list item back one level (see the file header). Resolves once committed and the caret is back.
75
+ * @param {Nabu} nabu
76
+ * @param {NabuNode} item a list-item node
77
+ * @returns {Promise<boolean>} whether the structure changed
78
+ */
79
+ export async function stepBack(nabu, item) {
80
+ const id = item.id.toString();
81
+ if (typeOf(item.parent()?.parent()) === "list-item") return moveItems(nabu, [item], true);
82
+ if (!liftItemToParagraph(nabu, item)) return false;
83
+ await nabu.commit();
84
+ const block = nabu.blocks.get(id);
85
+ if (block) nabu.selection.setCursor(block, 0);
86
+ return true;
87
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @param {Nabu} nabu
3
+ * @param {"bullet" | "ordered" | "todo"} listType
4
+ * @returns {Promise<void>}
5
+ */
6
+ export function convertToList(nabu: Nabu, listType: "bullet" | "ordered" | "todo"): Promise<void>;
7
+ import type { Nabu } from "../nabu.svelte.js";
@@ -0,0 +1,44 @@
1
+ /**
2
+ * `list:convert` — turn the block holding the caret into a list item of the given type (slash menu
3
+ * "Bulleted / Numbered / To-do list"):
4
+ * - a list item changes list type on its own, in place (see convert.js);
5
+ * - any other text block is replaced by a new list (same flow as the "- " / "1. " markdown shortcuts).
6
+ *
7
+ * @import { Nabu } from "../nabu.svelte.js";
8
+ */
9
+
10
+ import { List } from "./list.svelte.js";
11
+ import { ListItem } from "./list-item.svelte.js";
12
+ import { convertItemToListType } from "./convert.js";
13
+
14
+ /**
15
+ * @param {Nabu} nabu
16
+ * @param {"bullet" | "ordered" | "todo"} listType
17
+ * @returns {Promise<void>}
18
+ */
19
+ export async function convertToList(nabu, listType) {
20
+ const block = nabu.selection.anchorBlock;
21
+ if (!block || !["bullet", "ordered", "todo"].includes(listType)) return;
22
+
23
+ if (block.type === "list-item") {
24
+ const id = block.id;
25
+ const offset = block.behaviors?.get("text")?.selection?.from ?? 0;
26
+ await nabu.transact(() => { convertItemToListType(nabu, /** @type {any} */ (block.node), listType); });
27
+ const fresh = nabu.blocks.get(id);
28
+ if (fresh) nabu.focus({ from: { block: fresh, offset }, to: { block: fresh, offset } });
29
+ return;
30
+ }
31
+
32
+ const beh = block.behaviors?.get("text");
33
+ const delta = beh ? beh.container.toDelta() : [];
34
+ const parentNode = block.node.parent();
35
+ const parentId = parentNode?.id.toString() ?? null;
36
+ const index = block.node.index();
37
+ const list = List.create(nabu, "list", { listType }, parentId, index);
38
+ const itemProps = listType === "todo" ? { delta, checked: false } : { delta };
39
+ const item = ListItem.create(nabu, "list-item", itemProps, list.node.id.toString());
40
+ block.destroy();
41
+ await nabu.commit();
42
+ const fresh = nabu.blocks.get(item.id);
43
+ if (fresh) fresh.focus({ start: 0, end: 0 });
44
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * The to-do items the selection touches (the anchor item for a caret).
3
+ * @param {Nabu} nabu
4
+ */
5
+ export function selectedTodoItems(nabu: Nabu): import("../nabu.svelte.js").NabuNode[];
6
+ /**
7
+ * Toggle the to-do items the selection touches (see the module doc). The `todo:toggle` command.
8
+ * @param {Nabu} nabu
9
+ * @returns {boolean} whether at least one to-do item was touched
10
+ */
11
+ export function toggleSelectedTodos(nabu: Nabu): boolean;
12
+ /**
13
+ * Key-event form of {@link toggleSelectedTodos}: Ctrl/Cmd+Enter only (the keyboard path is the `todo:toggle` command now;
14
+ * this stays for direct callers).
15
+ * @param {Nabu} nabu
16
+ * @param {KeyboardEvent} event
17
+ * @returns {boolean} whether the key was handled (= at least one to-do item was touched); false for any other key
18
+ */
19
+ export function toggleTodos(nabu: Nabu, event: KeyboardEvent): boolean;
20
+ import type { Nabu } from "../nabu.svelte.js";
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Ctrl/Cmd+Enter on a to-do item (the `todo:toggle` command, ADR 017) toggles its `checked`. With a selection spanning several to-do items they
3
+ * are toggled together: if ANY is unchecked every one gets checked, otherwise every one gets unchecked.
4
+ * One transaction (one undo step); the caret / selection is not touched.
5
+ *
6
+ * @import { Nabu } from "../nabu.svelte.js";
7
+ */
8
+
9
+ import { selectedItems } from "./indent.js";
10
+
11
+ /**
12
+ * The to-do items the selection touches (the anchor item for a caret).
13
+ * @param {Nabu} nabu
14
+ */
15
+ export function selectedTodoItems(nabu) {
16
+ return selectedItems(nabu).filter((node) => /** @type {any} */ (node.parent()?.data)?.get("listType") === "todo");
17
+ }
18
+
19
+ /**
20
+ * Toggle the to-do items the selection touches (see the module doc). The `todo:toggle` command.
21
+ * @param {Nabu} nabu
22
+ * @returns {boolean} whether at least one to-do item was touched
23
+ */
24
+ export function toggleSelectedTodos(nabu) {
25
+ const items = selectedTodoItems(nabu);
26
+ if (!items.length) return false;
27
+ const value = items.some((node) => !(/** @type {any} */ (node.data)).get("checked"));
28
+ for (const node of items) {
29
+ const block = /** @type {any} */ (nabu.blocks.get(node.id.toString()));
30
+ if (block) block.checked = value;
31
+ /** @type {any} */ (node.data).set("checked", value);
32
+ }
33
+ nabu.isolateUndoStep(); // a flag change is not structural: it would merge with the typing around it
34
+ nabu.commit();
35
+ return true;
36
+ }
37
+
38
+ /**
39
+ * Key-event form of {@link toggleSelectedTodos}: Ctrl/Cmd+Enter only (the keyboard path is the `todo:toggle` command now;
40
+ * this stays for direct callers).
41
+ * @param {Nabu} nabu
42
+ * @param {KeyboardEvent} event
43
+ * @returns {boolean} whether the key was handled (= at least one to-do item was touched); false for any other key
44
+ */
45
+ export function toggleTodos(nabu, event) {
46
+ if (event.key !== "Enter" || !(event.ctrlKey || event.metaKey) || event.shiftKey || event.altKey) return false;
47
+ return toggleSelectedTodos(nabu);
48
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @param {string} alt
3
+ * @param {string} ref
4
+ * @returns {string}
5
+ */
6
+ export function markdownImage(alt: string, ref: string): string;
7
+ /**
8
+ * @param {string} label
9
+ * @param {string} ref
10
+ * @returns {string}
11
+ */
12
+ export function markdownMediaLink(label: string, ref: string): string;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Markdown syntax of the media blocks: `![alt](ref)` for an image, `[label](ref)` for a video / file. The alt text and the
3
+ * label are escaped like any inline text and the destination is wrapped in `<…>` when it holds spaces or unbalanced
4
+ * parentheses, so that what the importer reads back is the same `alt` / `ref`.
5
+ */
6
+
7
+ import { markdownDestination } from "../../behaviors/text";
8
+ import { escapeMarkdownText } from "../../behaviors/text/markdown-syntax.js";
9
+
10
+ /**
11
+ * @param {string} alt
12
+ * @param {string} ref
13
+ * @returns {string}
14
+ */
15
+ export function markdownImage(alt, ref) {
16
+ return '![' + escapeMarkdownText(alt) + '](' + markdownDestination(ref) + ')';
17
+ }
18
+
19
+ /**
20
+ * @param {string} label
21
+ * @param {string} ref
22
+ * @returns {string}
23
+ */
24
+ export function markdownMediaLink(label, ref) {
25
+ return '[' + escapeMarkdownText(label) + '](' + markdownDestination(ref) + ')';
26
+ }
@@ -1,3 +1,4 @@
1
+ /** @import { FileReason, FileStatus } from "../../files/status.js"; */
1
2
  /** @import { Nabu, NabuNode } from "../nabu.svelte.js"; */
2
3
  /**
3
4
  * Base class for void media blocks (image / video / file). Shares void behavior,
@@ -7,7 +8,7 @@
7
8
  export class MediaBlock extends Block {
8
9
  /**
9
10
  * Shared node factory. Subclasses call this from their static create().
10
- * @param {Nabu} nabu @param {string} type @param {{ref?:string,alt?:string,status?:string,name?:string}} [props] @param {string|null} [parentId] @param {number|null} [index]
11
+ * @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]
11
12
  * @returns {any}
12
13
  */
13
14
  static createNode(nabu: Nabu, type: string, props?: {
@@ -15,18 +16,92 @@ export class MediaBlock extends Block {
15
16
  alt?: string;
16
17
  status?: string;
17
18
  name?: string;
19
+ reason?: FileReason;
18
20
  }, parentId?: string | null, index?: number | null): any;
19
21
  /** @type {string} */ ref: string;
20
22
  /** @type {string} */ alt: string;
21
- /** @type {string} */ status: string;
23
+ /**
24
+ * What the block stores (see `files/status.js`): `ready` | `pending` (bytes being stored / uploaded; `progress` follows the
25
+ * upload) | `error` (upload failed or cancelled; the local copy is kept, `retry()` sends it again) | `missing` (no source has
26
+ * the bytes) | `rejected` (refused, e.g. over `files.maxFileSize`; no ref). A `pending` block that no upload is running for and
27
+ * whose file is neither local nor at the host is SHOWN as missing, see `isMissing`.
28
+ * @type {string}
29
+ */ status: string;
22
30
  /** @type {string} */ name: string;
23
31
  /** Transient local objectURL while pending (not persisted). @type {string | null} */ previewUrl: string | null;
32
+ /**
33
+ * Whether the file cannot be shown: flagged `missing` (pasted without its bytes), or a `nabu-asset:` ref that neither the
34
+ * local store nor the document's index can provide (e.g. the document was opened on another device). Reactive.
35
+ * @returns {boolean}
36
+ */
37
+ get isMissing(): boolean;
38
+ /**
39
+ * The status to SHOW: `missing` when the file cannot be displayed (see `isMissing`: includes a `pending` block whose upload
40
+ * was interrupted and is not available on this device), else the stored `status`. Reactive.
41
+ * @returns {string}
42
+ */
43
+ get displayStatus(): string;
44
+ /**
45
+ * Why the block is `error`, `rejected` or `missing`: `{ code, message }` (a stable code and a readable English sentence), `null`
46
+ * otherwise. A `pending` block shown as missing has the code `interrupted`. Reactive.
47
+ * @returns {FileReason | null}
48
+ */
49
+ get reason(): FileReason | null;
50
+ /**
51
+ * Upload progress, `0..1` while the file is being sent to the host (`files.onBeforePersist`), `null` otherwise. Reactive.
52
+ * @returns {number | null}
53
+ */
54
+ get progress(): number | null;
55
+ /**
56
+ * Send the file to the host again from its local copy (after `error`, including a cancelled upload). Resolves `true` when
57
+ * the file ended up at the host, `false` when there is nothing to retry (no host, bytes gone, not a local file).
58
+ * @returns {Promise<boolean>}
59
+ */
60
+ retry(): Promise<boolean>;
61
+ /**
62
+ * Abort the running upload (the host's `signal` fires). The block becomes `error` with the reason `cancelled`, the local copy
63
+ * is kept. Uploads are per file: every block showing the same file is affected.
64
+ * @returns {boolean} whether an upload was running
65
+ */
66
+ cancel(): boolean;
67
+ /**
68
+ * For the component's effect: keeps the display URL of this block alive while it is mounted (an unused objectURL is
69
+ * revoked). Returns the release function, so it can be used as `$effect(() => block.holdSource())`.
70
+ * @returns {() => void}
71
+ */
72
+ holdSource(): () => void;
24
73
  /** @param {InputEvent} event */
25
74
  beforeinput(event: InputEvent): boolean;
26
75
  /** Resolved display URL. While pending → local preview; else resolve the ref. @returns {string} */
27
76
  get displaySrc(): string;
77
+ /**
78
+ * The props a DOCUMENT export (`serialize('json')` / `'markdown'`) writes: `ref`, `alt`, `name`, and `status` (+ `reason`)
79
+ * when it is not `ready`. A `pending` block whose bytes are still being stored has no durable ref yet: its transient
80
+ * `nabu-pending:` ref (or empty one) is NOT exported as if it were final, the block is exported as `missing` (label
81
+ * kept, no source) so the import shows the "Missing file" state instead of a block stuck pending forever. A `pending`
82
+ * block with a hash ref (being uploaded) exports as a normal file: the export carries its bytes.
83
+ * @returns {{ ref: string, alt: string, name: string, status?: string, reason?: FileReason }}
84
+ */
85
+ get exportProps(): {
86
+ ref: string;
87
+ alt: string;
88
+ name: string;
89
+ status?: string;
90
+ reason?: FileReason;
91
+ };
92
+ /**
93
+ * What a COPY (`application/x-nabu+json`) carries of the file state: nothing, except a `rejected` block (so the refusal and
94
+ * its reason travel with it). Upload state (`pending`, `error`) never travels: the receiver works it out.
95
+ * @returns {{ status?: string, reason?: FileReason }}
96
+ */
97
+ get clipboardState(): {
98
+ status?: string;
99
+ reason?: FileReason;
100
+ };
28
101
  /** @param {string} v */
29
102
  setAlt(v: string): void;
103
+ #private;
30
104
  }
31
105
  import { Block } from "../block.svelte.js";
106
+ import type { FileReason } from "../../files/status.js";
32
107
  import type { Nabu } from "../nabu.svelte.js";
@@ -1,5 +1,9 @@
1
1
  import { Block } from "../block.svelte.js";
2
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"; */
3
7
 
4
8
  /** @import { Nabu, NabuNode } from "../nabu.svelte.js"; */
5
9
 
@@ -17,28 +21,101 @@ export class MediaBlock extends Block {
17
21
  // so `displaySrc` — read inside a $derived during render — only READS it.
18
22
  // Lazily creating it in displaySrc would mutate nabu.systems (a SvelteMap)
19
23
  // during a derived → Svelte's state_unsafe_mutation error.
20
- Files.get(nabu);
21
- const data = node.data;
24
+ const files = Files.get(nabu);
25
+ const data = /** @type {any} */ (node.data);
22
26
  this.ref = data.get('ref') || '';
27
+ files.warm(this.ref); // small files: bytes ready for a (synchronous) copy / export
23
28
  this.alt = data.get('alt') || '';
24
29
  this.status = data.get('status') || 'ready';
30
+ this.#reason = normalizeReason(data.get('reason'));
25
31
  this.name = data.get('name') || '';
26
- node.data.subscribe(() => {
27
- this.ref = node.data.get('ref') || '';
28
- this.alt = node.data.get('alt') || '';
29
- this.status = node.data.get('status') || 'ready';
30
- this.name = node.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
+ }
31
44
  });
32
45
  }
33
46
 
34
47
  /** @type {string} */ ref = $state('');
35
48
  /** @type {string} */ alt = $state('');
36
- /** @type {string} */ status = $state('ready');
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);
37
57
  /** @type {string} */ name = $state('');
38
58
  /** Transient local objectURL while pending (not persisted). @type {string | null} */ previewUrl = $state(null);
39
59
 
40
60
  positions = $derived(1);
41
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
+
42
119
  /** @param {number} offset @returns {{node: Node, offset: number} | null} */
43
120
  getDOMPoint(offset) { return null; }
44
121
 
@@ -47,8 +124,41 @@ export class MediaBlock extends Block {
47
124
 
48
125
  /** Resolved display URL. While pending → local preview; else resolve the ref. @returns {string} */
49
126
  get displaySrc() {
50
- if (this.status === 'pending' && this.previewUrl) return this.previewUrl;
51
- return Files.get(this.nabu).resolve(this.ref);
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 } : {};
52
162
  }
53
163
 
54
164
  /** @param {string} v */
@@ -56,14 +166,16 @@ export class MediaBlock extends Block {
56
166
 
57
167
  /**
58
168
  * Shared node factory. Subclasses call this from their static create().
59
- * @param {Nabu} nabu @param {string} type @param {{ref?:string,alt?:string,status?:string,name?:string}} [props] @param {string|null} [parentId] @param {number|null} [index]
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]
60
170
  * @returns {any}
61
171
  */
62
172
  static createNode(nabu, type, props = {}, parentId = null, index = null) {
63
- const node = nabu.tree.createNode(/** @type {any} */ (parentId || undefined), index || undefined);
173
+ const node = nabu.tree.createNode(/** @type {import('loro-crdt').TreeID | undefined} */ (parentId || undefined), index ?? undefined);
64
174
  const d = /** @type {any} */ (node.data);
65
175
  d.set('type', type); d.set('ref', props.ref || ''); d.set('alt', props.alt || '');
66
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);
67
179
  return node;
68
180
  }
69
181
  }
@@ -1,5 +1,9 @@
1
1
  /**
2
- * Insert a media block of `type` from a local File: optimistic preview → persist → ref.
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`.
3
7
  * @param {import('../nabu.svelte.js').Nabu} nabu @param {string} type @param {File} file
4
8
  */
5
9
  export function insertMediaFromFile(nabu: import("../nabu.svelte.js").Nabu, type: string, file: File): Promise<void>;
@@ -17,3 +21,4 @@ export function insertMediaUrl(nabu: import("../nabu.svelte.js").Nabu, type: str
17
21
  alt?: string;
18
22
  name?: string;
19
23
  }): Promise<void> | undefined;
24
+ export function mediaFileHandler(type: string, accepts: (file: File) => boolean, priority?: number): import("../../utils/extensions.js").PasteFileHandler;
@@ -1,26 +1,49 @@
1
- import { Files } from "../../files/files.svelte.js";
1
+ import { Files, FileRejectedError } from "../../files/files.svelte.js";
2
+ import { reasonOf } from "../../files/status.js";
2
3
 
3
4
  /**
4
- * Insert a media block of `type` from a local File: optimistic preview → persist → ref.
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`.
5
10
  * @param {import('../nabu.svelte.js').Nabu} nabu @param {string} type @param {File} file
6
11
  */
7
12
  export async function insertMediaFromFile(nabu, type, file) {
8
13
  const BlockClass = /** @type {any} */ (nabu.registry.get(type));
9
14
  if (!BlockClass) return;
15
+ const files = Files.get(nabu);
10
16
  const anchor = nabu.selection.anchorBlock;
11
17
  const parentId = anchor?.node.parent()?.id.toString() ?? null;
12
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
+ }
13
26
  const block = BlockClass.create(nabu, type, { status: 'pending', name: file.name }, parentId, idx >= 0 ? idx : null);
14
27
  block.previewUrl = URL.createObjectURL(file);
15
28
  await nabu.commit();
16
29
  const id = block.id;
17
30
  try {
18
- const ref = await Files.get(nabu).persist(file);
31
+ // lot 3: drag and drop calls this same path.
32
+ const { id: hash, ref } = await files.ingest(file);
19
33
  const fresh = nabu.blocks.get(id);
20
- if (fresh) { const d = /** @type {any} */ (fresh.node.data); d.set('ref', ref); d.set('status', 'ready'); /** @type {any} */ (fresh).previewUrl = null; await nabu.commit(); }
21
- } catch {
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) {
22
41
  const fresh = nabu.blocks.get(id);
23
- if (fresh) { /** @type {any} */ (fresh.node.data).set('status', 'error'); await nabu.commit(); }
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
+ }
24
47
  }
25
48
  }
26
49
 
@@ -49,3 +72,17 @@ export function insertMediaUrl(nabu, type, data) {
49
72
  BlockClass.create(nabu, type, { ref: data.ref, alt: data.alt || '', name: data.name || '', status: 'ready' }, parentId, idx >= 0 ? idx : null);
50
73
  return nabu.commit();
51
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
+ });