@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
@@ -1,608 +0,0 @@
1
- import { untrack } from 'svelte';
2
- import { MegaBlock } from '../megablock.svelte.js';
3
- import { LoroText } from 'loro-crdt';
4
- import { SvelteMap, SvelteSet } from 'svelte/reactivity';
5
-
6
- /**
7
- * @import { Nabu, NabuNode } from '../nabu.svelte.js';
8
- */
9
-
10
- /**
11
- * @typedef {NabuNode<{type: "zone", synopsis: LoroText, status: string, wordCountTarget: number | null, title: string, template: string | null}>} ZoneNode
12
- */
13
-
14
- export class ZoneBlock extends MegaBlock {
15
- /** @param {Nabu} nabu @param {ZoneNode} node */
16
- constructor(nabu, node) {
17
- super(nabu, node);
18
-
19
- const data = node.data;
20
-
21
- // Synopsis LoroText — get existing or create on first construction
22
- this.synopsisContainer = /** @type {LoroText} */ (
23
- data.get('synopsis') ?? data.setContainer('synopsis', new LoroText())
24
- );
25
-
26
- // Initialize $state backing fields from Loro
27
- this._status = data.get('status') ?? 'draft';
28
- this._wordCountTarget = data.get('wordCountTarget') ?? null;
29
- this._title = data.get('title') ?? '';
30
- this._template = /** @type {string | null} */ (data.get('template') ?? null);
31
- this.synopsisText = this.synopsisContainer.toString();
32
-
33
- // Subscribe to map changes for reactive scalar fields
34
- data.subscribe(() => {
35
- this._status = data.get('status') ?? 'draft';
36
- this._wordCountTarget = data.get('wordCountTarget') ?? null;
37
- this._title = data.get('title') ?? '';
38
- this._template = /** @type {string | null} */ (data.get('template') ?? null);
39
- });
40
-
41
- // Subscribe to synopsis LoroText changes
42
- this.synopsisContainer.subscribe(() => {
43
- this.synopsisText = this.synopsisContainer.toString();
44
- });
45
-
46
- this.serializers.set('markdown', () => {
47
- const header = `<!-- zone: ${this._status} -->`;
48
- const body = this.children
49
- .map(child => child.serialize('markdown'))
50
- .filter(Boolean)
51
- .join('\n\n');
52
- return [header, body].filter(Boolean).join('\n');
53
- });
54
-
55
- this.serializers.set('json', () => ({
56
- id: this.id,
57
- type: 'zone',
58
- props: { status: this._status, wordCountTarget: this._wordCountTarget },
59
- children: this.children.map(child => child.serialize('json')).filter(Boolean),
60
- }));
61
-
62
- this.serializers.set('application/x-nabu+json', () => ({
63
- type: 'zone',
64
- props: this.clipboardProps,
65
- children: this.children
66
- .map(child => child.serialize('application/x-nabu+json'))
67
- .filter(Boolean),
68
- }));
69
-
70
- this.zones = Zones.get(nabu);
71
-
72
-
73
- $effect.root(() => {
74
- $effect(() => {
75
- const templateId = this._template;
76
- if (!templateId) {
77
- this.zones.counters.unsubscribe(this);
78
- this.counter = null;
79
- return;
80
- }
81
- const template = this.zones.templates.get(templateId) || this.zones.templates.new(templateId);
82
- const counter = template?.counter;
83
- if (!counter) {
84
- this.zones.counters.unsubscribe(this);
85
- this.counter = null;
86
- return;
87
- }
88
- this.counter = template.getCounter(this);
89
- });
90
- });
91
- }
92
-
93
-
94
-
95
-
96
- // zones = $derived.by(() => {
97
- // this.nabu.systems.has('zones');
98
- // return untrack(() => Zones.get(this.nabu));
99
- // });
100
-
101
- // ── Reactive backing fields ─────────────────────────────────────────────
102
-
103
- /** @type {string} */
104
- _status = $state('draft');
105
-
106
- /** @type {number | null} */
107
- _wordCountTarget = $state(null);
108
-
109
- /** @type {string} */
110
- _title = $state('');
111
-
112
- /** @type {string | null} */
113
- _template = $state(null);
114
-
115
- /** Incremented each time the title input should be focused. */
116
- _focusTitleTick = $state(0);
117
-
118
- /** @type {string} */
119
- synopsisText = $state('');
120
-
121
- // ── Derived ─────────────────────────────────────────────────────────────
122
-
123
- // Incremental: each child maintains its own wordCount, we just sum.
124
- // (Inherited MegaBlock.wordCount does the same, but Zone redeclares it
125
- // explicitly because the word count is part of its public contract.)
126
- wordCount = $derived(this.children.reduce((sum, c) => sum + (c.wordCount ?? 0), 0));
127
-
128
- // ── Getters / setters ───────────────────────────────────────────────────
129
-
130
- get status() { return this._status; }
131
- set status(value) {
132
- this._status = value;
133
- this.node.data.set('status', value);
134
- this.commit();
135
- }
136
-
137
- get wordCountTarget() { return this._wordCountTarget; }
138
- set wordCountTarget(value) {
139
- this._wordCountTarget = value ?? null;
140
- this.node.data.set('wordCountTarget', value ?? null);
141
- this.commit();
142
- }
143
-
144
- get title() { return this._title; }
145
- set title(value) {
146
- this._title = value ?? '';
147
- /** @type {any} */ (this.node.data).set('title', value ?? '');
148
- this.commit();
149
- }
150
-
151
- get template() { return this._template; }
152
- set template(value) {
153
- this._template = value ?? null;
154
- /** @type {any} */ (this.node.data).set('template', value ?? null);
155
- this.commit();
156
- }
157
-
158
- focusTitle() { this._focusTitleTick++; }
159
-
160
- /** Returns the LoroText container for direct CRDT mutation. */
161
- get synopsis() { return this.synopsisContainer; }
162
-
163
- parentZones = $derived(this.parents.filter(p => p instanceof ZoneBlock));
164
- parentZone = $derived(this.parentZones[0] ?? null);
165
-
166
- /** @type {Counter?} */
167
- counter = $state(null);
168
- count = $state(0);
169
-
170
-
171
- /**
172
- * Replace the synopsis text entirely.
173
- * @param {string} text
174
- */
175
- setSynopsis(text) {
176
- const len = this.synopsisContainer.length;
177
- if (len > 0) this.synopsisContainer.delete(0, len);
178
- if (text) this.synopsisContainer.insert(0, text);
179
- this.commit();
180
- }
181
-
182
- get clipboardProps() {
183
- return { status: this._status, wordCountTarget: this._wordCountTarget };
184
- }
185
-
186
- // ── Container split ─────────────────────────────────────────────────────
187
-
188
- /**
189
- * Called via ascend("onSplit") when a child block's Enter key fires.
190
- * Creates a new paragraph inside this Zone, never outside it.
191
- *
192
- * @param {Event | null} event
193
- * @param {{ from: import('../block.svelte.js').Block, offset: number, delta: import('loro-crdt').Delta<string>[] }} data
194
- */
195
- onSplit(event, data) {
196
- const { from: sourceBlock, offset, delta } = data;
197
-
198
- // Delete content from split point to end of source block
199
- sourceBlock.delete({ from: offset, to: -1 });
200
-
201
- const currentIndex = sourceBlock.node.index();
202
-
203
- // Insert new paragraph inside THIS zone (not the zone's parent)
204
- const newParagraph = this.nabu.insert(
205
- 'paragraph',
206
- { delta },
207
- this.node.id.toString(),
208
- currentIndex + 1
209
- );
210
-
211
- this.nabu.commit().then(() => this.nabu.selection.setCursor(newParagraph, 0));
212
- return { block: newParagraph };
213
- }
214
-
215
- // ── Static factory ──────────────────────────────────────────────────────
216
-
217
- /**
218
- * @param {Nabu} nabu
219
- * @param {string} type
220
- * @param {Object} [props]
221
- * @param {string | null} [parentId]
222
- * @param {number | null} [index]
223
- */
224
- static create(nabu, type, props = {}, parentId = null, index = null) {
225
- const node = nabu.tree.createNode(
226
- parentId || undefined,
227
- index != null ? index : undefined
228
- );
229
-
230
- node.data.set('type', 'zone');
231
- node.data.set('status', props.status ?? 'draft');
232
- node.data.set('wordCountTarget', props.wordCountTarget ?? null);
233
- /** @type {any} */ (node.data).set('template', /** @type {any} */ (props).template ?? null);
234
-
235
- const synopsisContainer = node.data.setContainer('synopsis', new LoroText());
236
- if (props.synopsis) synopsisContainer.insert(0, props.synopsis);
237
-
238
- return new ZoneBlock(nabu, node);
239
- }
240
- }
241
-
242
- export class Zones {
243
- /** @type {Nabu} */
244
- #nabu;
245
-
246
- /** @param {Nabu} nabu */
247
- constructor(nabu) {
248
- this.#nabu = nabu;
249
- this.templates = new Templates(this.#nabu, this);
250
- }
251
-
252
- /** @param {Nabu} nabu */
253
- static get(nabu) {
254
- if (!nabu.systems.has('zones')) {
255
- const zones = new Zones(nabu);
256
- nabu.systems.set('zones', zones);
257
- return zones;
258
- }
259
- return /** @type {Zones} */ (nabu.systems.get('zones'));
260
- }
261
-
262
- /**
263
- * Flat list of all ZoneBlocks in document order (DFS traversal).
264
- * @type {ZoneBlock[]}
265
- */
266
- all = $derived.by(() => {
267
- /** @type {ZoneBlock[]} */
268
- const result = [];
269
- /** @param {import('../block.svelte.js').Block[]} blocks */
270
- const traverse = (blocks) => {
271
- for (const block of blocks) {
272
- if (block.type === 'zone') result.push(/** @type {ZoneBlock} */ (block));
273
- const children = /** @type {any} */ (block).children;
274
- if (children?.length) traverse(children);
275
- }
276
- };
277
- traverse(this.#nabu.children);
278
- return result;
279
- });
280
-
281
- /**
282
- * Total number of zones.
283
- */
284
- count = $derived(this.all.length);
285
-
286
- /**
287
- * Hierarchical zone tree — zones nested inside other zones.
288
- * @type {Array<{zone: ZoneBlock, children: any[]}>}
289
- */
290
- tree = $derived.by(() => {
291
- /** @param {import('../block.svelte.js').Block[]} blocks */
292
- const buildTree = (blocks) => {
293
- /** @type {Array<{zone: ZoneBlock, children: any[]}>} */
294
- const result = [];
295
- for (const block of blocks) {
296
- const children = /** @type {any} */ (block).children ?? [];
297
- if (block.type === 'zone') {
298
- result.push({ zone: /** @type {ZoneBlock} */ (block), children: buildTree(children) });
299
- } else if (children.length) {
300
- result.push(...buildTree(children));
301
- }
302
- }
303
- return result;
304
- };
305
- return buildTree(this.#nabu.children);
306
- });
307
-
308
- /**
309
- * The innermost zone containing the current cursor, or null if outside any zone.
310
- * @type {ZoneBlock | null}
311
- */
312
- current = $derived.by(() => {
313
- const anchor = this.#nabu.selection.anchorBlock;
314
- if (!anchor) return null;
315
- let node = anchor.parent;
316
- while (node) {
317
- if (node.type === 'zone') return /** @type {ZoneBlock} */ (node);
318
- node = node.parent;
319
- }
320
- return null;
321
- });
322
-
323
- /**
324
- * Index of the current zone in `all` (-1 if cursor is outside any zone).
325
- */
326
- currentIndex = $derived.by(() => {
327
- const zone = this.current;
328
- if (!zone) return -1;
329
- return this.all.indexOf(zone);
330
- });
331
-
332
- /**
333
- * Navigate to the next zone (moves cursor to its first text position).
334
- */
335
- goToNext() {
336
- const idx = this.currentIndex;
337
- const next = idx === -1
338
- ? this.all[0]
339
- : this.all[idx + 1];
340
- if (next) this.#focusZone(next);
341
- }
342
-
343
- /**
344
- * Navigate to the previous zone.
345
- */
346
- goToPrevious() {
347
- const idx = this.currentIndex;
348
- if (idx <= 0) return;
349
- this.#focusZone(this.all[idx - 1]);
350
- }
351
-
352
- /** @param {ZoneBlock} zone */
353
- #focusZone(zone) {
354
- const firstChild = /** @type {any} */ (zone).children?.[0];
355
- if (firstChild) this.#nabu.selection.setCursor(firstChild, 0);
356
- }
357
-
358
- counters = new Counters();
359
-
360
- }
361
-
362
- /** @extends SvelteMap<string, Template> */
363
- export class Templates extends SvelteMap {
364
- /** @param {Nabu} nabu @param {Zones} zones */
365
- constructor(nabu, zones) {
366
- super();
367
- this.nabu = nabu;
368
- this.zones = zones;
369
-
370
-
371
- this.map = this.nabu.doc.getMap('zones::templates');
372
- Object.entries(this.map.toJSON()).forEach(([key, value]) => {
373
- this.set(key, new Template(value, this));
374
- });
375
- }
376
-
377
- /** @param {string} id @param {TemplateData} [data] */
378
- new(id, data) {
379
- const templateData = { id, label: id, ...data };
380
- const template = new Template(templateData, this);
381
- this.set(id, template);
382
- this.map.set(id, templateData);
383
- return template;
384
- }
385
- }
386
-
387
- /**
388
- * @typedef {Object} TemplateData
389
- * @property {string} id - Unique identifier for the template.
390
- * @property {string} label - The template's display name.
391
- * @property {{
392
- * scope: "root" | "parent",
393
- * } | boolean | null | undefined} [counter]
394
- */
395
-
396
- export class Template {
397
- /** @param {TemplateData} init @param {Templates} templates */
398
- constructor(init, templates) {
399
- this.templates = templates;
400
- this.id = init.id;
401
- this._label = init.label;
402
- this._counter = init.counter;
403
-
404
- }
405
-
406
- _label = $state('');
407
- get label() { return this._label; }
408
- set label(value) {
409
- this._label = value;
410
- this.update(data => ({ ...data, label: value }));
411
- }
412
-
413
- renderLabel(index, title = '') {
414
- return formatTemplateLabel(this.label, index, title);
415
- }
416
-
417
- /** @type {TemplateData['counter']} */
418
- _counter = $state(null);
419
- /**
420
- * @returns {{scope: "root" | "parent"} | null | undefined}
421
- */
422
- get counter() {
423
- if (this._counter === true) return { scope: 'root' };
424
- if (this._counter === false) return null;
425
- return this._counter;
426
- }
427
- /** @param {TemplateData['counter']} value */
428
- set counter(value) {
429
- this._counter = value;
430
- this.update(data => ({ ...data, counter: value }));
431
- }
432
-
433
-
434
- /** @param {ZoneBlock} zone */
435
- getCounter(zone) {
436
- if (this.counter?.scope === 'root') {
437
- return this.templates.zones.counters.subscribe(zone, 'root', this.id);
438
- } else if (this.counter?.scope === 'parent') {
439
- const parentZone = zone.parentZone;
440
- const parentZoneId = parentZone ? parentZone.id : null;
441
- if (!parentZoneId) return null;
442
- return this.templates.zones.counters.subscribe(zone, parentZoneId, this.id);
443
- }
444
- }
445
-
446
- /** @param {function(TemplateData): TemplateData} callback */
447
- update(callback) {
448
- const updatedData = callback({
449
- id: this.id,
450
- label: this._label,
451
- counter: this._counter,
452
- });
453
- this.templates.map.set(this.id, updatedData);
454
- }
455
- }
456
-
457
- export function formatTemplateLabel(formatString, index, title = '') {
458
- if (!formatString) return '';
459
- let text = formatString;
460
- if (text.includes('{{1}}')) text = text.replace(/\{\{1\}\}/g, String(index));
461
- if (text.includes('{{A}}')) text = text.replace(/\{\{A\}\}/g, toAlpha(index, true));
462
- if (text.includes('{{a}}')) text = text.replace(/\{\{a\}\}/g, toAlpha(index, false));
463
- if (text.includes('{{I}}')) text = text.replace(/\{\{I\}\}/g, toRoman(index, true));
464
- if (text.includes('{{i}}')) text = text.replace(/\{\{i\}\}/g, toRoman(index, false));
465
- if (text.includes('{{title}}')) text = text.replace(/\{\{title\}\}/g, title);
466
- return text;
467
- }
468
-
469
- function toAlpha(num, upper) {
470
- let result = '';
471
- while (num > 0) {
472
- num--;
473
- result = String.fromCharCode((upper ? 65 : 97) + (num % 26)) + result;
474
- num = Math.floor(num / 26);
475
- }
476
- return result || (upper ? 'A' : 'a');
477
- }
478
-
479
- function toRoman(num, upper) {
480
- if (num <= 0) return String(num);
481
- const roman = {M:1000, CM:900, D:500, CD:400, C:100, XC:90, L:50, XL:40, X:10, IX:9, V:5, IV:4, I:1};
482
- let str = '';
483
- for (let i of Object.keys(roman)) {
484
- let q = Math.floor(num / roman[i]);
485
- num -= q * roman[i];
486
- str += i.repeat(q);
487
- }
488
- return upper ? str : str.toLowerCase();
489
- }
490
-
491
- export class Counters {
492
- /** @type {SvelteMap<string, Counter>} */
493
- counters = new SvelteMap();
494
-
495
- /** @type {SvelteMap<ZoneBlock, string>} */
496
- index = new SvelteMap();
497
-
498
- /**
499
- * @param {ZoneBlock} zone
500
- */
501
- unsubscribe(zone) {
502
- if (this.index.has(zone)) {
503
- const oldName = this.index.get(zone);
504
- if (oldName) {
505
- const set = this.counters.get(oldName);
506
- if (set) {
507
- set.delete(zone);
508
- untrack(() => set.size === 0 && set.unwatch());
509
- }
510
- }
511
- this.index.delete(zone);
512
- }
513
- }
514
-
515
- /**
516
- * @param {ZoneBlock} zone
517
- * @param {"root"|string} scope
518
- * @param {string} name
519
- */
520
- subscribe(zone, scope, name) {
521
- const c = scope + ':' + name;
522
-
523
- if (this.index.has(zone)) {
524
- const oldName = this.index.get(zone);
525
- if (oldName) {
526
- if (oldName === c) return this.counters.get(c);
527
- this.unsubscribe(zone);
528
- }
529
- }
530
-
531
- this.index.set(zone, c);
532
-
533
- if (!this.counters.has(c)) {
534
- this.counters.set(c, new Counter(scope, name));
535
- }
536
- const counter = this.counters.get(c);
537
- counter?.add(zone);
538
- return counter;
539
- }
540
- }
541
-
542
- /**
543
- * @extends SvelteSet<ZoneBlock>
544
- */
545
- class Counter extends SvelteSet {
546
- /**
547
- * @param {"root"|string} scope
548
- * @param {string} name
549
- */
550
- constructor(scope, name) {
551
- super();
552
- this.scope = scope;
553
- this.name = name;
554
-
555
- // L'effet racine qui observe les changements dans l'ensemble (Set)
556
- // et déclenche la mise à jour des `.count` des zones.
557
- this.watcher = $effect.root(() => {
558
- $effect(() => {
559
- if (this.zones) {
560
- // console.log(`Counter "${this.name}" (${this.scope}) has ${this.size} zones.`);
561
- // On recalcule l'index pour chaque zone du compteur
562
- this.zones.forEach(zone => {
563
- // untrack n'est pas strictement nécessaire ici si count n'est pas lu
564
- // dans cet effet, mais c'est une bonne pratique pour une simple écriture.
565
- untrack(() => zone.count = this.getIndex(zone));
566
- });
567
- }
568
- })
569
- });
570
- }
571
-
572
- unwatch = () => {
573
- // console.log(`Unwatching counter "${this.name}" (${this.scope}).`);
574
- if (this.watcher) this.watcher();
575
- }
576
-
577
- // 1. Correction du Tri (Utiliser l'ordre du document plat) + Filtrage des fantômes
578
- zones = $derived.by(() => {
579
- const zonesInSet = Array.from(this);
580
- if (zonesInSet.length === 0) return [];
581
-
582
- // On récupère la liste maître aplatie via n'importe quelle zone de notre Set
583
- const allZonesInDoc = zonesInSet[0].zones.all;
584
-
585
- // On filtre les zones qui auraient été supprimées du document (fantômes)
586
- const activeZones = zonesInSet.filter(z => allZonesInDoc.includes(z));
587
-
588
- // On trie en utilisant la position globale dans le document
589
- activeZones.sort((a, b) => allZonesInDoc.indexOf(a) - allZonesInDoc.indexOf(b));
590
- return activeZones;
591
- });
592
-
593
- /**
594
- * @param {ZoneBlock} targetZone
595
- * @returns {number}
596
- */
597
- getIndex(targetZone) {
598
- const sortedZones = this.zones;
599
- const targetIndexInList = sortedZones.indexOf(targetZone);
600
-
601
- if (targetIndexInList === -1) return 1;
602
-
603
- // Grâce à l'architecture du Template qui crée des compteurs isolés (un par ID de parent),
604
- // le Set ne contient DEJA que les zones qui doivent être comptées ensemble !
605
- // L'index est donc tout simplement sa position dans ce sous-groupe.
606
- return targetIndexInList + 1;
607
- }
608
- }