@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,245 @@
1
+ import { TextBlock } from "../text-block.svelte.js";
2
+ import { LoroText } from "loro-crdt";
3
+ import CodeComponent from "./Code.svelte";
4
+ import { tick } from "svelte";
5
+ import { escapeHtml } from "../../utils/html.js";
6
+
7
+ /**
8
+ * @import { Nabu, NabuNode } from "../nabu.svelte.js";
9
+ */
10
+
11
+ /**
12
+ * @typedef {NabuNode<{type: "code", text: LoroText, language: string | null}>} CodeNode
13
+ */
14
+
15
+ /**
16
+ * Plain-text content of a PasteBlock (joins all string inserts, ignores embeds).
17
+ * @param {import('../../utils/extensions.js').PasteBlock} pb
18
+ * @returns {string}
19
+ */
20
+ function pbText(pb) {
21
+ return (pb.delta || []).map(op => typeof op.insert === 'string' ? op.insert : '').join('');
22
+ }
23
+
24
+ /** The opening fence of a code block: indent, fence characters, info string. */
25
+ const FENCE_OPEN = /^( {0,3})(`{3,}|~{3,})(.*)$/;
26
+
27
+ /**
28
+ * Whether `line` closes the fence `open` (same character, at least as long, only spaces after it).
29
+ * @param {string} line
30
+ * @param {string} open
31
+ */
32
+ function isClosingFence(line, open) {
33
+ const m = /^ {0,3}(`+|~+)[ \t]*$/.exec(line);
34
+ return !!m && m[1][0] === open[0] && m[1].length >= open.length;
35
+ }
36
+
37
+ // @ts-ignore — static-side variance on create() (same pattern as Paragraph/Heading)
38
+ /**
39
+ * A fenced code block as markdown. The fence is longer than any run of backticks in the code (a code block that holds a
40
+ * fence round-trips); the info string is the language, without whitespace or backtick (it could not be read back).
41
+ * @param {string | null | undefined} language
42
+ * @param {string} text
43
+ * @returns {string}
44
+ */
45
+ function codeToMarkdown(language, text) {
46
+ const longest = Math.max(0, ...(text.match(/`+/g) || []).map((run) => run.length));
47
+ const fence = '`'.repeat(Math.max(3, longest + 1));
48
+ return fence + String(language || '').replace(/[\s`]/g, '') + '\n' + text + '\n' + fence;
49
+ }
50
+
51
+ export class CodeBlock extends TextBlock {
52
+ /** @param {Nabu} nabu @param {CodeNode} node */
53
+ constructor(nabu, node) {
54
+ super(nabu, node);
55
+
56
+ this.language = node.data.get('language') || null;
57
+
58
+ this.serializers.set('markdown', () => codeToMarkdown(this.language, this.text));
59
+ this.serializers.set('json', () => ({
60
+ id: this.id,
61
+ type: 'code',
62
+ props: { language: this.language },
63
+ content: this.text,
64
+ }));
65
+ this.serializers.set('application/x-nabu+json', (ctx = {}) =>
66
+ this.behavior.toClipboardBlock({ ...ctx, props: { language: this.language } })
67
+ );
68
+
69
+ // Sync language from Loro data changes (mirrors Heading's level sync)
70
+ this.node.data.subscribe(() => {
71
+ this.language = this.node.data.get('language') || null;
72
+ });
73
+ }
74
+
75
+ // ── Capability flags ─────────────────────────────────────────────────────
76
+ /** @type {boolean} */
77
+ splitsOnEnter = false;
78
+ /** @type {boolean} */
79
+ transformsEnabled = false;
80
+ /** @type {boolean} */
81
+ marksEnabled = false;
82
+ /** @type {boolean} */
83
+ pastesAsPlainText = true;
84
+
85
+ /** @type {string | null} */
86
+ language = $state(null);
87
+
88
+ static placeholder = 'Code…';
89
+
90
+ component = $derived(this.nabu.components.get("code") || CodeComponent);
91
+
92
+ /**
93
+ * Set the code language and commit.
94
+ * @param {string | null} lang
95
+ */
96
+ setLanguage(lang) {
97
+ this.language = lang || null;
98
+ /** @type {any} */ (this.node.data).set('language', lang || null);
99
+ this.commit();
100
+ }
101
+
102
+ /**
103
+ * Handle special keydown events: Tab, Shift+Tab, ArrowDown.
104
+ * Returns true if handled (engine should preventDefault).
105
+ * @param {KeyboardEvent} event
106
+ * @returns {boolean}
107
+ */
108
+ keydown(event) {
109
+ const sel = this.behavior.selection;
110
+ if (!sel) return false;
111
+
112
+ // Tab without shift — insert 2 spaces
113
+ if (event.key === 'Tab' && !event.shiftKey) {
114
+ if (!sel.isCollapsed) this.behavior.delete({ index: sel.from, length: sel.to - sel.from });
115
+ this.behavior.insert(sel.from, ' ');
116
+ this.commit();
117
+ tick().then(() => this.nabu.selection.setCursor(this, sel.from + 2));
118
+ return true;
119
+ }
120
+
121
+ // Shift+Tab — outdent (remove up to 2 leading spaces on current line)
122
+ if (event.key === 'Tab' && event.shiftKey) {
123
+ const ls = this.text.lastIndexOf('\n', sel.from - 1) + 1;
124
+ let n = 0;
125
+ while (n < 2 && this.text[ls + n] === ' ') n++;
126
+ if (n > 0) {
127
+ this.behavior.delete({ index: ls, length: n });
128
+ this.commit();
129
+ tick().then(() => this.nabu.selection.setCursor(this, Math.max(ls, sel.from - n)));
130
+ }
131
+ return true;
132
+ }
133
+
134
+ // ArrowDown (no modifiers, collapsed) — escape code block by creating a paragraph after
135
+ if (event.key === 'ArrowDown' && !event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey && sel.isCollapsed) {
136
+ const onLastLine = this.text.indexOf('\n', sel.from) === -1;
137
+ const hasNextTextBlock = !!this.findForward(b => b.behaviors?.has('text'));
138
+ if (onLastLine && !hasNextTextBlock) {
139
+ const p = this.nabu.insert('paragraph', {}, this.node.parent()?.id.toString() || null, (this.node.index() ?? 0) + 1);
140
+ this.nabu.commit().then(() => {
141
+ const fresh = this.nabu.blocks.get(p.id);
142
+ if (fresh) this.nabu.selection.setCursor(fresh, 0);
143
+ });
144
+ return true;
145
+ }
146
+ return false;
147
+ }
148
+
149
+ return false;
150
+ }
151
+
152
+ // ── Statics ──────────────────────────────────────────────────────────────
153
+
154
+ /** @param {import('../../utils/extensions.js').PasteBlock} pb */
155
+ static toMarkdown(pb) {
156
+ return codeToMarkdown(pb.props?.language, pbText(pb));
157
+ }
158
+
159
+ /** @param {import('../../utils/extensions.js').PasteBlock} pb */
160
+ static toHtml(pb) {
161
+ return `<pre><code>${escapeHtml(pbText(pb))}</code></pre>`;
162
+ }
163
+
164
+ /**
165
+ * Fenced code (CommonMark): 3+ backticks or 3+ tildes (up to 3 spaces of indent), an optional info string (the first
166
+ * word is the language), closed by a fence of the SAME character at least as long. An unclosed fence runs to the end.
167
+ * The content is verbatim (no inline parsing, blank lines kept). An indented block (4 spaces) is NOT code.
168
+ */
169
+ static markdownRules = [
170
+ {
171
+ priority: 30,
172
+ interruptsParagraph: true,
173
+ // A backtick fence has no backtick in its info string (otherwise it is an inline code span: ```a```).
174
+ detect: /^ {0,3}(?:`{3,}[^`]*|~{3,}.*)$/,
175
+ /**
176
+ * Consume from the opening fence to the closing fence (or EOF).
177
+ * @param {string[]} lines
178
+ * @param {number} i
179
+ * @returns {number}
180
+ */
181
+ consume(lines, i) {
182
+ const open = FENCE_OPEN.exec(lines[i]);
183
+ if (!open) return 1;
184
+ let j = i + 1;
185
+ while (j < lines.length && !isClosingFence(lines[j], open[2])) j++;
186
+ return Math.min(lines.length, j + 1) - i;
187
+ },
188
+ }
189
+ ];
190
+
191
+ /**
192
+ * @param {string[]} lines
193
+ * @returns {import('../../utils/extensions.js').PasteBlock | null}
194
+ */
195
+ static fromMarkdown(lines) {
196
+ const open = FENCE_OPEN.exec(lines[0]);
197
+ if (!open) return null;
198
+ const language = open[3].trim().split(/\s+/)[0] || null;
199
+ let body = lines.slice(1);
200
+ // The closing fence is the last line, when it closes this fence (consume stops at the first closer).
201
+ if (body.length > 0 && isClosingFence(body[body.length - 1], open[2])) body = body.slice(0, -1);
202
+ // An indented opening fence removes up to that many spaces from the content lines.
203
+ const indent = open[1].length;
204
+ if (indent) body = body.map((line) => line.replace(new RegExp('^ {0,' + indent + '}'), ''));
205
+ const bodyText = body.join('\n');
206
+ // Empty body is still a valid code block
207
+ if (!bodyText && body.length === 0 && lines.length === 1) return null;
208
+ return {
209
+ type: 'code',
210
+ props: { language },
211
+ delta: [{ insert: bodyText }],
212
+ partial: false,
213
+ };
214
+ }
215
+
216
+ static htmlRules = [
217
+ { selector: 'pre', priority: 10 },
218
+ ];
219
+
220
+ /**
221
+ * @param {Element} el
222
+ * @returns {import('../../utils/extensions.js').PasteBlock | null}
223
+ */
224
+ static fromHTML(el) {
225
+ const text = el.textContent || '';
226
+ const language = el.querySelector('code')?.className.match(/language-(\w+)/)?.[1] || null;
227
+ return {
228
+ type: 'code',
229
+ props: { language },
230
+ delta: [{ insert: text }],
231
+ partial: false,
232
+ };
233
+ }
234
+
235
+ /** @param {Nabu} nabu @param {string} type @param {Record<string, any>} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
236
+ static create(nabu, type, props = {}, parentId = null, index = null) {
237
+ const node = nabu.tree.createNode(/** @type {import('loro-crdt').TreeID | undefined} */ (parentId || undefined), index ?? undefined);
238
+ /** @type {any} */ (node.data).set('type', 'code');
239
+ if (props.language) /** @type {any} */ (node.data).set('language', props.language);
240
+ const container = /** @type {any} */ (node.data).setContainer('text', new LoroText());
241
+ if (props.text) container.insert(0, props.text);
242
+ if (props.delta) container.applyDelta([...props.delta]);
243
+ return new CodeBlock(nabu, /** @type {CodeNode} */ (/** @type {unknown} */ (node)));
244
+ }
245
+ }
@@ -0,0 +1,3 @@
1
+ export { CodeBlock } from "./code.svelte.js";
2
+ export { CodeExtension } from "./code.extension.js";
3
+ export { default as CodeComponent } from "./Code.svelte";
@@ -0,0 +1,3 @@
1
+ export { CodeBlock } from './code.svelte.js';
2
+ export { CodeExtension } from './code.extension.js';
3
+ export { default as CodeComponent } from './Code.svelte';
@@ -1,3 +1,7 @@
1
+ /**
2
+ * @import { Block } from './block.svelte.js';
3
+ * @import { Nabu } from './nabu.svelte.js';
4
+ */
1
5
  /**
2
6
  * Walks up from `block` toward `container` and returns the direct child of
3
7
  * `container` that is (or contains) `block`.
@@ -38,18 +42,65 @@ export function findLCA(blockA: Block, blockB: Block): Block | null;
38
42
  */
39
43
  export function wrapOrphan(nabu: Nabu, block: Block): Block | undefined;
40
44
  /**
41
- * Shared beforeinput logic for block containers (Nabu root and MegaBlock).
45
+ * The text blocks of the document in document order (a block's own text before its children). With `visible`, the children of
46
+ * a block that hides them are skipped (they are not rendered: no caret, no DOM point).
47
+ * @param {Nabu} nabu
48
+ * @param {{ visible?: boolean }} [options]
49
+ * @returns {Block[]}
50
+ */
51
+ export function textBlocksInOrder(nabu: Nabu, { visible }?: {
52
+ visible?: boolean;
53
+ }): Block[];
54
+ /**
55
+ * The ISOLATION OWNER of a block: its nearest ancestor that isolates its text (`Block#isolatesSelection`), or null. Two text
56
+ * blocks with different owners (the body of a chapter and the body of a scene inside it) never merge by a range deletion.
57
+ * @param {Block} block
58
+ * @returns {Block | null}
59
+ */
60
+ export function isolationOwnerOf(block: Block): Block | null;
61
+ /**
62
+ * Splits a range into the RUNS that a deletion may act on (ADR 018 §3.9): maximal sequences of consecutive text blocks, in
63
+ * document order, that do not contain an isolated block and share one isolation owner. The text of an isolated block is
64
+ * outside every run (a range starting or ending in it starts after it / ends before it), and a run boundary is never merged
65
+ * across. A document without isolated blocks gives a single run, the range itself.
66
+ * @param {Nabu} nabu
67
+ * @param {DeletionRange} range
68
+ * @returns {DeletionRange[] | null} the runs in document order (possibly empty), or null when the range cannot be placed in the document
69
+ */
70
+ export function isolatedRuns(nabu: Nabu, range: DeletionRange): DeletionRange[] | null;
71
+ /**
72
+ * THE one implementation of "delete the selected range across blocks" (cut, Backspace / Delete, typing over a
73
+ * selection, paste over a selection, move by drag). Mutates the Loro tree and texts, never commits.
42
74
  *
43
- * Uses a "spine" approach: for each selection boundary, we walk up the parent
44
- * chain until we hit the container, collecting intermediate blocks to clean up
45
- * along the way. This correctly handles arbitrarily nested structures.
75
+ * A range that crosses ISOLATED text (`Block#isolatesSelection`, e.g. the title of a region) acts on its {@link isolatedRuns}
76
+ * only: the text of an isolated block is never touched, an isolated block and its container are never deleted, and the runs
77
+ * are never merged with one another (each one is deleted like a range of its own, the LAST run first so that the earlier
78
+ * ones are not disturbed). What is left is the first block of every run, emptied: that is where a caret still has a home.
79
+ * Without isolated block in the range it is exactly {@link deleteRangeRaw}.
46
80
  *
47
- * Spine termination differs between the two callers:
48
- * - MegaBlock: the container IS pushed into the spine (loop exits on === container),
49
- * so the direct child of container is at spine.at(-2).
50
- * - Nabu: root blocks have parent = null, so the loop breaks before Nabu is ever
51
- * pushed, and the direct child of container is at spine.at(-1).
52
- * Both cases are unified by the directChild() helper below.
81
+ * @param {Nabu} nabu
82
+ * @param {DeletionRange} range
83
+ * @returns {{ block: Block, offset: number } | null} the junction (where the caret belongs: the start of the first run), or null if the range is unusable or holds no deletable text
84
+ */
85
+ export function deleteRange(nabu: Nabu, range: DeletionRange): {
86
+ block: Block;
87
+ offset: number;
88
+ } | null;
89
+ /**
90
+ * The current selection as a {@link DeletionRange}, or null when there is none.
91
+ * A selection inside one block has `startBlock === endBlock`.
92
+ * @param {Nabu} nabu
93
+ * @returns {DeletionRange | null}
94
+ */
95
+ export function selectionRange(nabu: Nabu): DeletionRange | null;
96
+ /**
97
+ * Shared beforeinput logic for block containers (Nabu root and MegaBlock) when the selection spans SEVERAL
98
+ * blocks (a selection inside one block is that block's business). Backspace / Delete / word deletions delete the
99
+ * range; typing replaces it; Enter replaces it and splits at the junction. All of it is {@link deleteRange}
100
+ * followed by the plain single-block edit at the junction, then ONE commit (one undo step).
101
+ *
102
+ * The `container` (the lowest common ancestor the event was routed to) is not needed any more: the range is
103
+ * resolved from the selection alone, which also makes the same code correct for every depth.
53
104
  *
54
105
  * @param {{ children: Block[], commit: () => void }} container
55
106
  * @param {Nabu} nabu
@@ -60,11 +111,11 @@ export function handleContainerBeforeInput(container: {
60
111
  commit: () => void;
61
112
  }, nabu: Nabu, event: InputEvent): any;
62
113
  /**
63
- * Deletes the current selection content without committing.
64
- * Handles both single-block and multi-block selections.
114
+ * Deletes the current selection content without committing (cut, paste over a selection).
115
+ * Handles both single-block and multi-block selections — see {@link deleteRange}.
65
116
  * Returns the resulting insertion position {block, offset}, or null on failure.
66
117
  *
67
- * @param {{ children: import('./block.svelte.js').Block[] }} container
118
+ * @param {{ children: import('./block.svelte.js').Block[] }} container (unused: the range comes from the selection)
68
119
  * @param {import('./nabu.svelte.js').Nabu} nabu
69
120
  * @returns {{ block: import('./block.svelte.js').Block, offset: number } | null}
70
121
  */
@@ -74,5 +125,26 @@ export function deleteSelectionContent(container: {
74
125
  block: import("./block.svelte.js").Block;
75
126
  offset: number;
76
127
  } | null;
128
+ /**
129
+ * A selected range of the document, as model positions.
130
+ */
131
+ export type DeletionRange = {
132
+ /**
133
+ * Block holding the start of the range (earlier in document order).
134
+ */
135
+ startBlock: Block;
136
+ /**
137
+ * Text offset of the start inside `startBlock`.
138
+ */
139
+ startOffset: number;
140
+ /**
141
+ * Block holding the end of the range.
142
+ */
143
+ endBlock: Block;
144
+ /**
145
+ * Text offset of the end inside `endBlock`.
146
+ */
147
+ endOffset: number;
148
+ };
77
149
  import type { Block } from './block.svelte.js';
78
150
  import type { Nabu } from './nabu.svelte.js';