@aionbuilders/nabu 0.1.0-alpha.9 → 0.1.0-beta.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 (170) hide show
  1. package/README.md +216 -9
  2. package/dist/behaviors/text/RichText.svelte +4 -1
  3. package/dist/behaviors/text/index.d.ts +2 -0
  4. package/dist/behaviors/text/index.js +2 -0
  5. package/dist/behaviors/text/markdown-shortcuts.extension.d.ts +2 -0
  6. package/dist/behaviors/text/markdown-shortcuts.extension.js +54 -0
  7. package/dist/behaviors/text/rich-text.extension.js +47 -22
  8. package/dist/behaviors/text/text.behavior.svelte.d.ts +25 -1
  9. package/dist/behaviors/text/text.behavior.svelte.js +154 -18
  10. package/dist/behaviors/text/typography.extension.d.ts +6 -0
  11. package/dist/behaviors/text/typography.extension.js +24 -0
  12. package/dist/blocks/Block.svelte +5 -2
  13. package/dist/blocks/Nabu.svelte +22 -0
  14. package/dist/blocks/block.extension.js +10 -8
  15. package/dist/blocks/block.svelte.d.ts +24 -0
  16. package/dist/blocks/block.svelte.js +35 -0
  17. package/dist/blocks/blockquote/Blockquote.svelte +40 -0
  18. package/dist/blocks/blockquote/Blockquote.svelte.d.ts +11 -0
  19. package/dist/blocks/blockquote/blockquote.extension.d.ts +1 -0
  20. package/dist/blocks/blockquote/blockquote.extension.js +71 -0
  21. package/dist/blocks/blockquote/blockquote.svelte.d.ts +55 -0
  22. package/dist/blocks/blockquote/blockquote.svelte.js +91 -0
  23. package/dist/blocks/blockquote/index.d.ts +3 -0
  24. package/dist/blocks/blockquote/index.js +3 -0
  25. package/dist/blocks/callout/Callout.svelte +69 -0
  26. package/dist/blocks/callout/Callout.svelte.d.ts +11 -0
  27. package/dist/blocks/callout/callout.extension.d.ts +1 -0
  28. package/dist/blocks/callout/callout.extension.js +68 -0
  29. package/dist/blocks/callout/callout.svelte.d.ts +78 -0
  30. package/dist/blocks/callout/callout.svelte.js +184 -0
  31. package/dist/blocks/callout/index.d.ts +3 -0
  32. package/dist/blocks/callout/index.js +3 -0
  33. package/dist/blocks/code/Code.svelte +85 -0
  34. package/dist/blocks/code/Code.svelte.d.ts +11 -0
  35. package/dist/blocks/code/code.extension.d.ts +1 -0
  36. package/dist/blocks/code/code.extension.js +68 -0
  37. package/dist/blocks/code/code.svelte.d.ts +61 -0
  38. package/dist/blocks/code/code.svelte.js +221 -0
  39. package/dist/blocks/code/index.d.ts +3 -0
  40. package/dist/blocks/code/index.js +3 -0
  41. package/dist/blocks/dialogue/Dialogue.svelte +2 -0
  42. package/dist/blocks/dialogue/dialogue.svelte.d.ts +3 -21
  43. package/dist/blocks/dialogue/dialogue.svelte.js +2 -50
  44. package/dist/blocks/divider/Divider.svelte +28 -0
  45. package/dist/blocks/divider/Divider.svelte.d.ts +11 -0
  46. package/dist/blocks/divider/divider.extension.d.ts +1 -0
  47. package/dist/blocks/divider/divider.extension.js +87 -0
  48. package/dist/blocks/divider/divider.svelte.d.ts +49 -0
  49. package/dist/blocks/divider/divider.svelte.js +84 -0
  50. package/dist/blocks/divider/index.d.ts +3 -0
  51. package/dist/blocks/divider/index.js +3 -0
  52. package/dist/blocks/file/File.svelte +49 -0
  53. package/dist/blocks/file/File.svelte.d.ts +11 -0
  54. package/dist/blocks/file/file.extension.d.ts +1 -0
  55. package/dist/blocks/file/file.extension.js +19 -0
  56. package/dist/blocks/file/file.svelte.d.ts +29 -0
  57. package/dist/blocks/file/file.svelte.js +71 -0
  58. package/dist/blocks/file/index.d.ts +3 -0
  59. package/dist/blocks/file/index.js +3 -0
  60. package/dist/blocks/heading/Heading.svelte +3 -1
  61. package/dist/blocks/heading/heading.svelte.d.ts +4 -23
  62. package/dist/blocks/heading/heading.svelte.js +4 -47
  63. package/dist/blocks/image/Image.svelte +38 -0
  64. package/dist/blocks/image/Image.svelte.d.ts +11 -0
  65. package/dist/blocks/image/image.extension.d.ts +2 -0
  66. package/dist/blocks/image/image.extension.js +26 -0
  67. package/dist/blocks/image/image.svelte.d.ts +50 -0
  68. package/dist/blocks/image/image.svelte.js +92 -0
  69. package/dist/blocks/image/index.d.ts +3 -0
  70. package/dist/blocks/image/index.js +3 -0
  71. package/dist/blocks/index.d.ts +10 -0
  72. package/dist/blocks/index.js +10 -0
  73. package/dist/blocks/list/List.svelte +8 -2
  74. package/dist/blocks/list/ListItem.svelte +43 -7
  75. package/dist/blocks/list/hooks/onBeforeInput.hook.js +15 -2
  76. package/dist/blocks/list/index.d.ts +1 -0
  77. package/dist/blocks/list/index.js +1 -0
  78. package/dist/blocks/list/list-item.svelte.d.ts +14 -2
  79. package/dist/blocks/list/list-item.svelte.js +58 -7
  80. package/dist/blocks/list/list.behavior.svelte.d.ts +4 -4
  81. package/dist/blocks/list/list.behavior.svelte.js +5 -5
  82. package/dist/blocks/list/list.svelte.d.ts +13 -6
  83. package/dist/blocks/list/list.svelte.js +24 -6
  84. package/dist/blocks/list/todos.svelte.d.ts +35 -0
  85. package/dist/blocks/list/todos.svelte.js +76 -0
  86. package/dist/blocks/media/index.d.ts +2 -0
  87. package/dist/blocks/media/index.js +2 -0
  88. package/dist/blocks/media/media-block.svelte.d.ts +32 -0
  89. package/dist/blocks/media/media-block.svelte.js +69 -0
  90. package/dist/blocks/media/media.helpers.d.ts +19 -0
  91. package/dist/blocks/media/media.helpers.js +51 -0
  92. package/dist/blocks/megablock.svelte.js +1 -0
  93. package/dist/blocks/nabu.svelte.d.ts +94 -16
  94. package/dist/blocks/nabu.svelte.js +254 -3
  95. package/dist/blocks/paragraph/Paragraph.svelte +2 -0
  96. package/dist/blocks/paragraph/index.js +0 -3
  97. package/dist/blocks/paragraph/paragraph.svelte.d.ts +3 -21
  98. package/dist/blocks/paragraph/paragraph.svelte.js +4 -53
  99. package/dist/blocks/selection.svelte.js +11 -1
  100. package/dist/blocks/text-block.svelte.d.ts +49 -0
  101. package/dist/blocks/text-block.svelte.js +74 -0
  102. package/dist/blocks/toggle/Toggle.svelte +84 -0
  103. package/dist/blocks/toggle/Toggle.svelte.d.ts +11 -0
  104. package/dist/blocks/toggle/index.d.ts +3 -0
  105. package/dist/blocks/toggle/index.js +3 -0
  106. package/dist/blocks/toggle/toggle.extension.d.ts +1 -0
  107. package/dist/blocks/toggle/toggle.extension.js +12 -0
  108. package/dist/blocks/toggle/toggle.svelte.d.ts +88 -0
  109. package/dist/blocks/toggle/toggle.svelte.js +252 -0
  110. package/dist/blocks/variable.svelte.d.ts +47 -0
  111. package/dist/blocks/variable.svelte.js +51 -0
  112. package/dist/blocks/video/Video.svelte +39 -0
  113. package/dist/blocks/video/Video.svelte.d.ts +11 -0
  114. package/dist/blocks/video/index.d.ts +3 -0
  115. package/dist/blocks/video/index.js +3 -0
  116. package/dist/blocks/video/video.extension.d.ts +1 -0
  117. package/dist/blocks/video/video.extension.js +19 -0
  118. package/dist/blocks/video/video.svelte.d.ts +28 -0
  119. package/dist/blocks/video/video.svelte.js +71 -0
  120. package/dist/blocks/zone/TemplateEditor.svelte +308 -0
  121. package/dist/blocks/zone/TemplateEditor.svelte.d.ts +11 -0
  122. package/dist/blocks/zone/Zone.svelte +158 -144
  123. package/dist/blocks/zone/ZoneEditor.svelte +289 -0
  124. package/dist/blocks/zone/ZoneEditor.svelte.d.ts +11 -0
  125. package/dist/blocks/zone/ZoneTemplatesList.svelte +188 -0
  126. package/dist/blocks/zone/ZoneTemplatesList.svelte.d.ts +11 -0
  127. package/dist/blocks/zone/hooks/onBeforeInput.hook.d.ts +3 -0
  128. package/dist/blocks/zone/hooks/onBeforeInput.hook.js +30 -0
  129. package/dist/blocks/zone/zone.extension.js +22 -6
  130. package/dist/blocks/zone/zone.svelte.d.ts +111 -2
  131. package/dist/blocks/zone/zone.svelte.js +304 -17
  132. package/dist/files/files.svelte.d.ts +61 -0
  133. package/dist/files/files.svelte.js +93 -0
  134. package/dist/files/index.d.ts +1 -0
  135. package/dist/files/index.js +1 -0
  136. package/dist/index.d.ts +4 -1
  137. package/dist/index.js +5 -2
  138. package/dist/nabu-theme.css +92 -0
  139. package/dist/overlays/context/Context.svelte +182 -0
  140. package/dist/overlays/context/Context.svelte.d.ts +17 -0
  141. package/dist/overlays/context/collect.d.ts +17 -0
  142. package/dist/overlays/context/collect.js +33 -0
  143. package/dist/overlays/context/context.extension.d.ts +2 -0
  144. package/dist/overlays/context/context.extension.js +8 -0
  145. package/dist/overlays/context/context.svelte.d.ts +82 -0
  146. package/dist/overlays/context/context.svelte.js +159 -0
  147. package/dist/overlays/context/index.d.ts +3 -0
  148. package/dist/overlays/context/index.js +3 -0
  149. package/dist/paste/html.paste.js +9 -1
  150. package/dist/presets.d.ts +3 -1
  151. package/dist/presets.js +21 -6
  152. package/dist/ui/DragHandle.svelte +141 -0
  153. package/dist/ui/DragHandle.svelte.d.ts +11 -0
  154. package/dist/ui/FloatingToolbar.svelte +117 -0
  155. package/dist/ui/FloatingToolbar.svelte.d.ts +17 -0
  156. package/dist/ui/SlashMenu.svelte +83 -0
  157. package/dist/ui/SlashMenu.svelte.d.ts +13 -0
  158. package/dist/ui/index.d.ts +4 -0
  159. package/dist/ui/index.js +4 -0
  160. package/dist/ui/slash-menu.svelte.d.ts +40 -0
  161. package/dist/ui/slash-menu.svelte.js +116 -0
  162. package/dist/utils/extensions.d.ts +54 -0
  163. package/dist/utils/extensions.js +40 -1
  164. package/dist/utils/index.d.ts +1 -0
  165. package/dist/utils/index.js +1 -0
  166. package/dist/utils/placeholders.d.ts +10 -0
  167. package/dist/utils/placeholders.js +21 -0
  168. package/dist/vite.d.ts +16 -0
  169. package/dist/vite.js +23 -0
  170. package/package.json +10 -5
@@ -1,13 +1,14 @@
1
1
  import { untrack } from 'svelte';
2
2
  import { MegaBlock } from '../megablock.svelte.js';
3
3
  import { LoroText } from 'loro-crdt';
4
+ import { SvelteMap, SvelteSet } from 'svelte/reactivity';
4
5
 
5
6
  /**
6
7
  * @import { Nabu, NabuNode } from '../nabu.svelte.js';
7
8
  */
8
9
 
9
10
  /**
10
- * @typedef {NabuNode<{type: "zone", synopsis: LoroText, status: string, wordCountTarget: number | null, title: string}>} ZoneNode
11
+ * @typedef {NabuNode<{type: "zone", synopsis: LoroText, status: string, wordCountTarget: number | null, title: string, template: string | null}>} ZoneNode
11
12
  */
12
13
 
13
14
  export class ZoneBlock extends MegaBlock {
@@ -26,6 +27,7 @@ export class ZoneBlock extends MegaBlock {
26
27
  this._status = data.get('status') ?? 'draft';
27
28
  this._wordCountTarget = data.get('wordCountTarget') ?? null;
28
29
  this._title = data.get('title') ?? '';
30
+ this._template = /** @type {string | null} */ (data.get('template') ?? null);
29
31
  this.synopsisText = this.synopsisContainer.toString();
30
32
 
31
33
  // Subscribe to map changes for reactive scalar fields
@@ -33,6 +35,7 @@ export class ZoneBlock extends MegaBlock {
33
35
  this._status = data.get('status') ?? 'draft';
34
36
  this._wordCountTarget = data.get('wordCountTarget') ?? null;
35
37
  this._title = data.get('title') ?? '';
38
+ this._template = /** @type {string | null} */ (data.get('template') ?? null);
36
39
  });
37
40
 
38
41
  // Subscribe to synopsis LoroText changes
@@ -65,6 +68,26 @@ export class ZoneBlock extends MegaBlock {
65
68
  }));
66
69
 
67
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
+ });
68
91
  }
69
92
 
70
93
 
@@ -86,27 +109,21 @@ export class ZoneBlock extends MegaBlock {
86
109
  /** @type {string} */
87
110
  _title = $state('');
88
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
+
89
118
  /** @type {string} */
90
119
  synopsisText = $state('');
91
120
 
92
121
  // ── Derived ─────────────────────────────────────────────────────────────
93
122
 
94
- wordCount = $derived.by(() => {
95
- let count = 0;
96
- /** @param {import('../block.svelte.js').Block[]} blocks */
97
- const traverse = (blocks) => {
98
- for (const block of blocks) {
99
- const tb = block.behaviors?.get('text');
100
- if (tb && typeof tb.text === 'string') {
101
- count += tb.text.trim().split(/\s+/).filter(Boolean).length;
102
- }
103
- // @ts-ignore — children exists on MegaBlock
104
- if (block.children?.length) traverse(block.children);
105
- }
106
- };
107
- traverse(this.children);
108
- return count;
109
- });
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));
110
127
 
111
128
  // ── Getters / setters ───────────────────────────────────────────────────
112
129
 
@@ -131,9 +148,26 @@ export class ZoneBlock extends MegaBlock {
131
148
  this.commit();
132
149
  }
133
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
+
134
160
  /** Returns the LoroText container for direct CRDT mutation. */
135
161
  get synopsis() { return this.synopsisContainer; }
136
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
+
137
171
  /**
138
172
  * Replace the synopsis text entirely.
139
173
  * @param {string} text
@@ -196,6 +230,7 @@ export class ZoneBlock extends MegaBlock {
196
230
  node.data.set('type', 'zone');
197
231
  node.data.set('status', props.status ?? 'draft');
198
232
  node.data.set('wordCountTarget', props.wordCountTarget ?? null);
233
+ /** @type {any} */ (node.data).set('template', /** @type {any} */ (props).template ?? null);
199
234
 
200
235
  const synopsisContainer = node.data.setContainer('synopsis', new LoroText());
201
236
  if (props.synopsis) synopsisContainer.insert(0, props.synopsis);
@@ -211,6 +246,7 @@ export class Zones {
211
246
  /** @param {Nabu} nabu */
212
247
  constructor(nabu) {
213
248
  this.#nabu = nabu;
249
+ this.templates = new Templates(this.#nabu, this);
214
250
  }
215
251
 
216
252
  /** @param {Nabu} nabu */
@@ -318,4 +354,255 @@ export class Zones {
318
354
  const firstChild = /** @type {any} */ (zone).children?.[0];
319
355
  if (firstChild) this.#nabu.selection.setCursor(firstChild, 0);
320
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
+ }
321
608
  }
@@ -0,0 +1,61 @@
1
+ /**
2
+ * @typedef {Object} FilesConfig
3
+ * @property {(file: File, ctx: { signal?: AbortSignal, onProgress?: (p: number) => void }) => Promise<{ ref: string } | void>} [onBeforePersist]
4
+ * Intercept before default persistence. Return `{ ref }` to redirect to your own
5
+ * storage (the bytes are NOT stored in Loro). Return nothing to fall back to default.
6
+ * @property {(ref: string) => string} [resolve] Turn a stored ref into a display URL.
7
+ * @property {number} [maxInlineSize] Byte threshold above which the default (Loro) mode warns. Default 2MB.
8
+ */
9
+ /**
10
+ * Document-level file/asset manager. Default mode stores bytes in a Loro `assets`
11
+ * map (source of truth stays in Loro). Lazy: Files.get(nabu).
12
+ */
13
+ export class Files {
14
+ /** @param {import('../blocks/nabu.svelte.js').Nabu} nabu @returns {Files} */
15
+ static get(nabu: import("../blocks/nabu.svelte.js").Nabu): Files;
16
+ /** @param {import('../blocks/nabu.svelte.js').Nabu} nabu */
17
+ constructor(nabu: import("../blocks/nabu.svelte.js").Nabu);
18
+ assets: LoroMap<Record<string, unknown>>;
19
+ /** @returns {FilesConfig} */
20
+ get config(): FilesConfig;
21
+ get maxInlineSize(): number;
22
+ /**
23
+ * Persist a File → durable ref. Uses onBeforePersist if configured, else stores
24
+ * the bytes in the Loro `assets` map.
25
+ * @param {File} file
26
+ * @param {{ signal?: AbortSignal, onProgress?: (p:number)=>void }} [ctx]
27
+ * @returns {Promise<string>}
28
+ */
29
+ persist(file: File, ctx?: {
30
+ signal?: AbortSignal;
31
+ onProgress?: (p: number) => void;
32
+ }): Promise<string>;
33
+ /**
34
+ * Resolve a ref to a displayable URL. nabu-asset: refs → objectURL from Loro
35
+ * bytes (cached); external URLs → config.resolve or identity.
36
+ * @param {string} ref @returns {string}
37
+ */
38
+ resolve(ref: string): string;
39
+ #private;
40
+ }
41
+ export type FilesConfig = {
42
+ /**
43
+ * Intercept before default persistence. Return `{ ref }` to redirect to your own
44
+ * storage (the bytes are NOT stored in Loro). Return nothing to fall back to default.
45
+ */
46
+ onBeforePersist?: ((file: File, ctx: {
47
+ signal?: AbortSignal;
48
+ onProgress?: (p: number) => void;
49
+ }) => Promise<{
50
+ ref: string;
51
+ } | void>) | undefined;
52
+ /**
53
+ * Turn a stored ref into a display URL.
54
+ */
55
+ resolve?: ((ref: string) => string) | undefined;
56
+ /**
57
+ * Byte threshold above which the default (Loro) mode warns. Default 2MB.
58
+ */
59
+ maxInlineSize?: number | undefined;
60
+ };
61
+ import { LoroMap } from 'loro-crdt';
@@ -0,0 +1,93 @@
1
+ import { LoroMap } from 'loro-crdt';
2
+
3
+ /**
4
+ * @typedef {Object} FilesConfig
5
+ * @property {(file: File, ctx: { signal?: AbortSignal, onProgress?: (p: number) => void }) => Promise<{ ref: string } | void>} [onBeforePersist]
6
+ * Intercept before default persistence. Return `{ ref }` to redirect to your own
7
+ * storage (the bytes are NOT stored in Loro). Return nothing to fall back to default.
8
+ * @property {(ref: string) => string} [resolve] Turn a stored ref into a display URL.
9
+ * @property {number} [maxInlineSize] Byte threshold above which the default (Loro) mode warns. Default 2MB.
10
+ */
11
+
12
+ /**
13
+ * Document-level file/asset manager. Default mode stores bytes in a Loro `assets`
14
+ * map (source of truth stays in Loro). Lazy: Files.get(nabu).
15
+ */
16
+ export class Files {
17
+ /** @type {import('../blocks/nabu.svelte.js').Nabu} */
18
+ #nabu;
19
+ /** @type {Map<string,string>} ref → objectURL cache for nabu-asset: refs */
20
+ #urlCache = new Map();
21
+
22
+ /** @param {import('../blocks/nabu.svelte.js').Nabu} nabu */
23
+ constructor(nabu) {
24
+ this.#nabu = nabu;
25
+ this.assets = nabu.doc.getMap('assets');
26
+ }
27
+
28
+ /** @param {import('../blocks/nabu.svelte.js').Nabu} nabu @returns {Files} */
29
+ static get(nabu) {
30
+ if (!nabu.systems.has('files')) {
31
+ const f = new Files(nabu);
32
+ nabu.systems.set('files', f);
33
+ return f;
34
+ }
35
+ return /** @type {Files} */ (nabu.systems.get('files'));
36
+ }
37
+
38
+ /** @returns {FilesConfig} */
39
+ get config() { return this.#nabu.fileConfig || {}; }
40
+
41
+ get maxInlineSize() { return this.config.maxInlineSize ?? 2 * 1024 * 1024; }
42
+
43
+ /**
44
+ * Persist a File → durable ref. Uses onBeforePersist if configured, else stores
45
+ * the bytes in the Loro `assets` map.
46
+ * @param {File} file
47
+ * @param {{ signal?: AbortSignal, onProgress?: (p:number)=>void }} [ctx]
48
+ * @returns {Promise<string>}
49
+ */
50
+ async persist(file, ctx = {}) {
51
+ const hook = this.config.onBeforePersist;
52
+ if (hook) {
53
+ const result = await hook(file, ctx);
54
+ if (result && result.ref) return result.ref;
55
+ // fell through → default
56
+ }
57
+ if (file.size > this.maxInlineSize) {
58
+ console.warn(`[Nabu] File "${file.name}" is ${(file.size / 1048576).toFixed(1)}MB — stored inline in the Loro document (default mode). For large media, configure files.onBeforePersist to upload to your own storage.`);
59
+ }
60
+ const buf = new Uint8Array(await file.arrayBuffer());
61
+ const id = (globalThis.crypto?.randomUUID?.() ?? String(Date.now()) + Math.random().toString(36).slice(2));
62
+ const sub = this.assets.setContainer(id, new LoroMap());
63
+ sub.set('data', buf);
64
+ sub.set('type', file.type || 'application/octet-stream');
65
+ sub.set('name', file.name || '');
66
+ this.#nabu.doc.commit();
67
+ return 'nabu-asset:' + id;
68
+ }
69
+
70
+ /**
71
+ * Resolve a ref to a displayable URL. nabu-asset: refs → objectURL from Loro
72
+ * bytes (cached); external URLs → config.resolve or identity.
73
+ * @param {string} ref @returns {string}
74
+ */
75
+ resolve(ref) {
76
+ if (!ref) return '';
77
+ if (ref.startsWith('nabu-asset:')) {
78
+ const cached = this.#urlCache.get(ref);
79
+ if (cached) return cached;
80
+ const id = ref.slice('nabu-asset:'.length);
81
+ const sub = /** @type {any} */ (this.assets.get(id));
82
+ if (!sub || typeof sub.get !== 'function') return '';
83
+ const data = sub.get('data');
84
+ const type = sub.get('type') || 'application/octet-stream';
85
+ if (!data) return '';
86
+ const url = URL.createObjectURL(new Blob([data], { type }));
87
+ this.#urlCache.set(ref, url);
88
+ return url;
89
+ }
90
+ if (this.config.resolve) return this.config.resolve(ref);
91
+ return ref;
92
+ }
93
+ }
@@ -0,0 +1 @@
1
+ export { Files } from "./files.svelte.js";
@@ -0,0 +1 @@
1
+ export { Files } from './files.svelte.js';
package/dist/index.d.ts CHANGED
@@ -3,4 +3,7 @@ export * from "./utils";
3
3
  export * from "./behaviors";
4
4
  export * from "./presets";
5
5
  export * from "./paste";
6
- export { RichTextExtension, RichText } from "./behaviors/text";
6
+ export * from "./overlays/context";
7
+ export * from "./ui";
8
+ export * from "./files";
9
+ export { RichTextExtension, TypographyExtension, MarkdownShortcutsExtension, RichText } from "./behaviors/text";
package/dist/index.js CHANGED
@@ -1,7 +1,10 @@
1
1
  // Reexport your entry components here
2
- export { RichTextExtension, RichText } from './behaviors/text';
2
+ export { RichTextExtension, TypographyExtension, MarkdownShortcutsExtension, RichText } from './behaviors/text';
3
3
  export * from './blocks';
4
4
  export * from "./utils";
5
5
  export * from './behaviors';
6
6
  export * from './presets';
7
- export * from './paste';
7
+ export * from './paste';
8
+ export * from './overlays/context';
9
+ export * from './ui';
10
+ export * from './files';
@@ -0,0 +1,92 @@
1
+ /*
2
+ * Nabu Theme Configuration
3
+ * State-of-the-art Semantic Design Tokens for UI Modularity
4
+ */
5
+
6
+ :root, [data-nabu-theme="light"] {
7
+ /* UI Backgrounds */
8
+ --nb-bg-surface: hsla(0, 0%, 100%, 0.85); /* Light glass */
9
+ --nb-bg-surface-sunken: hsla(0, 0%, 0%, 0.04);
10
+ --nb-bg-surface-hover: hsla(0, 0%, 0%, 0.04);
11
+ --nb-bg-surface-active: hsla(0, 0%, 0%, 0.08);
12
+
13
+ /* UI Borders & Lines */
14
+ --nb-border: hsla(0, 0%, 0%, 0.1);
15
+ --nb-border-subtle: hsla(0, 0%, 0%, 0.05);
16
+ --nb-border-focus: hsla(0, 0%, 0%, 0.15);
17
+
18
+ /* Text Colors */
19
+ --nb-text-primary: hsla(0, 0%, 0%, 0.9);
20
+ --nb-text-secondary: hsla(0, 0%, 0%, 0.6);
21
+ --nb-text-muted: hsla(0, 0%, 0%, 0.4);
22
+
23
+ /* Accent & Status Colors */
24
+ --nb-accent: hsla(210, 100%, 50%, 0.2);
25
+ --nb-success: hsla(150, 100%, 35%, 0.9);
26
+ --nb-success-bg: hsla(150, 100%, 40%, 0.15);
27
+ --nb-warning: rgba(245, 158, 11, 0.4);
28
+ --nb-danger: rgba(239, 68, 68, 0.4);
29
+
30
+ /* Shadows */
31
+ --nb-shadow-lg: 0 16px 32px -12px rgba(0,0,0,0.15);
32
+ --nb-shadow-inset: inset 0 2px 4px rgba(0,0,0,0.03);
33
+ --nb-shadow-glow: 0 0 0 2px hsla(0, 0%, 0%, 0.05);
34
+
35
+ /* Backdrop Filter for Glassmorphism */
36
+ --nb-glass-blur: blur(24px) saturate(180%);
37
+ --nb-glass-blur-sm: blur(12px) saturate(140%);
38
+ }
39
+
40
+ @media (prefers-color-scheme: dark) {
41
+ :root:not([data-nabu-theme="light"]) {
42
+ /* Dark glass overrides natively */
43
+ --nb-bg-surface: hsla(0, 0%, 12%, 0.75);
44
+ --nb-bg-surface-sunken: hsla(0, 0%, 0%, 0.2);
45
+ --nb-bg-surface-hover: hsla(0, 0%, 100%, 0.08);
46
+ --nb-bg-surface-active: hsla(0, 0%, 100%, 0.15);
47
+
48
+ --nb-border: hsla(0, 0%, 100%, 0.1);
49
+ --nb-border-subtle: hsla(0, 0%, 100%, 0.05);
50
+ --nb-border-focus: hsla(0, 0%, 100%, 0.2);
51
+
52
+ --nb-text-primary: hsla(0, 0%, 100%, 0.9);
53
+ --nb-text-secondary: hsla(0, 0%, 100%, 0.6);
54
+ --nb-text-muted: hsla(0, 0%, 100%, 0.4);
55
+
56
+ --nb-accent: hsla(210, 100%, 60%, 0.25);
57
+ --nb-success: hsla(150, 100%, 60%, 0.9);
58
+ --nb-success-bg: hsla(150, 100%, 60%, 0.15);
59
+ --nb-warning: rgba(245, 158, 11, 0.4);
60
+ --nb-danger: rgba(239, 68, 68, 0.4);
61
+
62
+ --nb-shadow-lg: 0 16px 32px -12px rgba(0,0,0,0.5);
63
+ --nb-shadow-inset: inset 0 2px 4px rgba(0,0,0,0.2);
64
+ --nb-shadow-glow: 0 0 0 2px hsla(0, 0%, 100%, 0.05);
65
+ }
66
+ }
67
+
68
+ [data-nabu-theme="dark"] {
69
+ /* Dark glass explicit override */
70
+ --nb-bg-surface: hsla(0, 0%, 12%, 0.75);
71
+ --nb-bg-surface-sunken: hsla(0, 0%, 0%, 0.2);
72
+ --nb-bg-surface-hover: hsla(0, 0%, 100%, 0.08);
73
+ --nb-bg-surface-active: hsla(0, 0%, 100%, 0.15);
74
+
75
+ --nb-border: hsla(0, 0%, 100%, 0.1);
76
+ --nb-border-subtle: hsla(0, 0%, 100%, 0.05);
77
+ --nb-border-focus: hsla(0, 0%, 100%, 0.2);
78
+
79
+ --nb-text-primary: hsla(0, 0%, 100%, 0.9);
80
+ --nb-text-secondary: hsla(0, 0%, 100%, 0.6);
81
+ --nb-text-muted: hsla(0, 0%, 100%, 0.4);
82
+
83
+ --nb-accent: hsla(210, 100%, 60%, 0.25);
84
+ --nb-success: hsla(150, 100%, 60%, 0.9);
85
+ --nb-success-bg: hsla(150, 100%, 60%, 0.15);
86
+ --nb-warning: rgba(245, 158, 11, 0.4);
87
+ --nb-danger: rgba(239, 68, 68, 0.4);
88
+
89
+ --nb-shadow-lg: 0 16px 32px -12px rgba(0,0,0,0.5);
90
+ --nb-shadow-inset: inset 0 2px 4px rgba(0,0,0,0.2);
91
+ --nb-shadow-glow: 0 0 0 2px hsla(0, 0%, 100%, 0.05);
92
+ }