@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
@@ -7,7 +7,7 @@ import ListComponent from "./List.svelte";
7
7
  */
8
8
 
9
9
  /**
10
- * @typedef {NabuNode<{type: "list", listType: "bullet" | "ordered"}>} ListNode
10
+ * @typedef {NabuNode<{type: "list", listType: "bullet" | "ordered" | "todo"}>} ListNode
11
11
  */
12
12
 
13
13
 
@@ -138,7 +138,8 @@ export class List extends MegaBlock {
138
138
  */
139
139
  static toHtml(pb, { recurse }) {
140
140
  const tag = pb.props?.listType === 'ordered' ? 'ol' : 'ul';
141
- return `<${tag}>${(pb.children || []).map(recurse).join('')}</${tag}>`;
141
+ const cls = pb.props?.listType === 'todo' ? ' class="contains-task-list"' : '';
142
+ return `<${tag}${cls}>${(pb.children || []).map(recurse).join('')}</${tag}>`;
142
143
  }
143
144
 
144
145
  static markdownRules = [
@@ -163,13 +164,20 @@ export class List extends MegaBlock {
163
164
  * @returns {import('../../utils/extensions.js').PasteBlock | null}
164
165
  */
165
166
  static fromMarkdown(lines, { parseInline }) {
166
- /** @typedef {{ depth: number, ordered: boolean, text: string }} ListToken */
167
+ /** @typedef {{ depth: number, ordered: boolean, task: boolean, checked: boolean, text: string }} ListToken */
167
168
 
168
169
  /** @type {ListToken[]} */
169
170
  const tokens = lines.flatMap(line => {
170
171
  const m = line.match(/^([ \t]*)([-*+]|\d+[.)]) +(.*)/);
171
172
  if (!m) return [];
172
- return [{ depth: m[1].length, ordered: /^\d/.test(m[2]), text: m[3] }];
173
+ const rawText = m[3];
174
+ const taskMatch = rawText.match(/^\[( |x|X)?\]\s*(.*)/);
175
+ if (taskMatch) {
176
+ const inner = taskMatch[1] ?? '';
177
+ const rest = taskMatch[2];
178
+ return [{ depth: m[1].length, ordered: /^\d/.test(m[2]), task: true, checked: /[xX]/.test(inner), text: rest }];
179
+ }
180
+ return [{ depth: m[1].length, ordered: /^\d/.test(m[2]), task: false, checked: false, text: rawText }];
173
181
  });
174
182
 
175
183
  if (!tokens.length) return null;
@@ -182,7 +190,10 @@ export class List extends MegaBlock {
182
190
  */
183
191
  function buildList(toks) {
184
192
  const baseDepth = Math.min(...toks.map(t => t.depth));
185
- const listType = toks.find(t => t.depth === baseDepth)?.ordered ? 'ordered' : 'bullet';
193
+ const baseItems = toks.filter(t => t.depth === baseDepth);
194
+ // Task takes precedence: if ANY base-depth item is a task, list is todo
195
+ const hasTask = baseItems.some(t => t.task);
196
+ const listType = hasTask ? 'todo' : (baseItems.find(t => t.depth === baseDepth)?.ordered ? 'ordered' : 'bullet');
186
197
  /** @type {import('../../utils/extensions.js').PasteBlock[]} */
187
198
  const items = [];
188
199
  let i = 0;
@@ -200,6 +211,7 @@ export class List extends MegaBlock {
200
211
 
201
212
  /** @type {import('../../utils/extensions.js').PasteBlock} */
202
213
  const item = { type: 'list-item', delta: parseInline(toks[i].text), partial: false };
214
+ if (toks[i].task) item.props = { checked: toks[i].checked };
203
215
  if (subToks.length) item.children = [buildList(subToks)];
204
216
 
205
217
  items.push(item);
@@ -213,6 +225,7 @@ export class List extends MegaBlock {
213
225
  }
214
226
 
215
227
  static htmlRules = [
228
+ { selector: 'ul.contains-task-list', props: { listType: 'todo' }, priority: 10 },
216
229
  { selector: 'ul', props: { listType: 'bullet' } },
217
230
  { selector: 'ol', props: { listType: 'ordered' } },
218
231
  ];
@@ -226,7 +239,12 @@ export class List extends MegaBlock {
226
239
  static fromHTML(el, { parseChildren }, rule) {
227
240
  const children = parseChildren(el);
228
241
  if (!children.length) return null;
229
- return { type: 'list', props: { listType: rule?.props?.listType ?? 'bullet' }, children, partial: false };
242
+ // Determine listType: prefer rule, but promote to 'todo' if any child has checked prop
243
+ let listType = rule?.props?.listType ?? 'bullet';
244
+ if (listType !== 'todo' && children.some(c => c.props?.checked !== undefined)) {
245
+ listType = 'todo';
246
+ }
247
+ return { type: 'list', props: { listType }, children, partial: false };
230
248
  }
231
249
 
232
250
  /** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
@@ -0,0 +1,35 @@
1
+ /** @import { Nabu } from "../nabu.svelte.js"; */
2
+ /**
3
+ * Document-level reactive registry of all to-do items (checklist list-items).
4
+ * Pure derived view over the block tree — stores nothing in Loro.
5
+ * Lazy: obtain via Todos.get(nabu).
6
+ */
7
+ export class Todos {
8
+ /** @param {Nabu} nabu @returns {Todos} */
9
+ static get(nabu: Nabu): Todos;
10
+ /** @param {Nabu} nabu */
11
+ constructor(nabu: Nabu);
12
+ /**
13
+ * All to-do items in document order (DFS). A to-do is a list-item whose
14
+ * parent list is a todo list (ListItem.isTodo).
15
+ * @type {ListItem[]}
16
+ */
17
+ all: ListItem[];
18
+ count: number;
19
+ done: ListItem[];
20
+ doneCount: number;
21
+ remaining: ListItem[];
22
+ /** Fraction completed, 0..1 (0 when there are no to-dos). */
23
+ progress: number;
24
+ /** @param {ListItem} item */
25
+ toggle(item: ListItem): void;
26
+ /** Mark every to-do checked, in a single transaction. */
27
+ checkAll(): void;
28
+ /** Mark every to-do unchecked, in a single transaction. */
29
+ uncheckAll(): void;
30
+ /** Move the cursor to a to-do item. @param {ListItem} item */
31
+ goTo(item: ListItem): void;
32
+ #private;
33
+ }
34
+ import { ListItem } from "./list-item.svelte.js";
35
+ import type { Nabu } from "../nabu.svelte.js";
@@ -0,0 +1,76 @@
1
+ import { ListItem } from "./list-item.svelte.js";
2
+
3
+ /** @import { Nabu } from "../nabu.svelte.js"; */
4
+
5
+ /**
6
+ * Document-level reactive registry of all to-do items (checklist list-items).
7
+ * Pure derived view over the block tree — stores nothing in Loro.
8
+ * Lazy: obtain via Todos.get(nabu).
9
+ */
10
+ export class Todos {
11
+ /** @type {Nabu} */
12
+ #nabu;
13
+
14
+ /** @param {Nabu} nabu */
15
+ constructor(nabu) { this.#nabu = nabu; }
16
+
17
+ /** @param {Nabu} nabu @returns {Todos} */
18
+ static get(nabu) {
19
+ if (!nabu.systems.has('todos')) {
20
+ const todos = new Todos(nabu);
21
+ nabu.systems.set('todos', todos);
22
+ return todos;
23
+ }
24
+ return /** @type {Todos} */ (nabu.systems.get('todos'));
25
+ }
26
+
27
+ /**
28
+ * All to-do items in document order (DFS). A to-do is a list-item whose
29
+ * parent list is a todo list (ListItem.isTodo).
30
+ * @type {ListItem[]}
31
+ */
32
+ all = $derived.by(() => {
33
+ /** @type {ListItem[]} */
34
+ const result = [];
35
+ /** @param {import('../block.svelte.js').Block[]} blocks */
36
+ const traverse = (blocks) => {
37
+ for (const block of blocks) {
38
+ if (block instanceof ListItem && block.isTodo) result.push(block);
39
+ const children = /** @type {any} */ (block).children;
40
+ if (children?.length) traverse(children);
41
+ }
42
+ };
43
+ traverse(this.#nabu.children);
44
+ return result;
45
+ });
46
+
47
+ count = $derived(this.all.length);
48
+ done = $derived(this.all.filter(t => t.checked));
49
+ doneCount = $derived(this.done.length);
50
+ remaining = $derived(this.all.filter(t => !t.checked));
51
+ /** Fraction completed, 0..1 (0 when there are no to-dos). */
52
+ progress = $derived(this.count ? this.doneCount / this.count : 0);
53
+
54
+ /** @param {ListItem} item */
55
+ toggle(item) { item.toggleChecked(); }
56
+
57
+ /** Mark every to-do checked, in a single transaction. */
58
+ checkAll() { this.#setAll(true); }
59
+ /** Mark every to-do unchecked, in a single transaction. */
60
+ uncheckAll() { this.#setAll(false); }
61
+
62
+ /** @param {boolean} value */
63
+ #setAll(value) {
64
+ let changed = false;
65
+ for (const item of this.all) {
66
+ if (item.checked === value) continue;
67
+ item.checked = value;
68
+ /** @type {any} */ (item.node.data).set('checked', value);
69
+ changed = true;
70
+ }
71
+ if (changed) this.#nabu.commit();
72
+ }
73
+
74
+ /** Move the cursor to a to-do item. @param {ListItem} item */
75
+ goTo(item) { this.#nabu.selection.setCursor(item, 0); }
76
+ }
@@ -0,0 +1,2 @@
1
+ export { MediaBlock } from "./media-block.svelte.js";
2
+ export * from "./media.helpers.js";
@@ -0,0 +1,2 @@
1
+ export { MediaBlock } from './media-block.svelte.js';
2
+ export * from './media.helpers.js';
@@ -0,0 +1,32 @@
1
+ /** @import { Nabu, NabuNode } from "../nabu.svelte.js"; */
2
+ /**
3
+ * Base class for void media blocks (image / video / file). Shares void behavior,
4
+ * reactive {ref, alt, status, name} metadata, Files display resolution, and the
5
+ * optimistic preview. Subclasses provide component, serializers, and static rules.
6
+ */
7
+ export class MediaBlock extends Block {
8
+ /**
9
+ * Shared node factory. Subclasses call this from their static create().
10
+ * @param {Nabu} nabu @param {string} type @param {{ref?:string,alt?:string,status?:string,name?:string}} [props] @param {string|null} [parentId] @param {number|null} [index]
11
+ * @returns {any}
12
+ */
13
+ static createNode(nabu: Nabu, type: string, props?: {
14
+ ref?: string;
15
+ alt?: string;
16
+ status?: string;
17
+ name?: string;
18
+ }, parentId?: string | null, index?: number | null): any;
19
+ /** @type {string} */ ref: string;
20
+ /** @type {string} */ alt: string;
21
+ /** @type {string} */ status: string;
22
+ /** @type {string} */ name: string;
23
+ /** Transient local objectURL while pending (not persisted). @type {string | null} */ previewUrl: string | null;
24
+ /** @param {InputEvent} event */
25
+ beforeinput(event: InputEvent): boolean;
26
+ /** Resolved display URL. While pending → local preview; else resolve the ref. @returns {string} */
27
+ get displaySrc(): string;
28
+ /** @param {string} v */
29
+ setAlt(v: string): void;
30
+ }
31
+ import { Block } from "../block.svelte.js";
32
+ import type { Nabu } from "../nabu.svelte.js";
@@ -0,0 +1,69 @@
1
+ import { Block } from "../block.svelte.js";
2
+ import { Files } from "../../files/files.svelte.js";
3
+
4
+ /** @import { Nabu, NabuNode } from "../nabu.svelte.js"; */
5
+
6
+ /**
7
+ * Base class for void media blocks (image / video / file). Shares void behavior,
8
+ * reactive {ref, alt, status, name} metadata, Files display resolution, and the
9
+ * optimistic preview. Subclasses provide component, serializers, and static rules.
10
+ */
11
+ // @ts-ignore — static-side variance on create()
12
+ export class MediaBlock extends Block {
13
+ /** @param {Nabu} nabu @param {NabuNode} node */
14
+ constructor(nabu, node) {
15
+ super(nabu, node);
16
+ // Eagerly ensure the Files system exists here (a safe, non-reactive context)
17
+ // so `displaySrc` — read inside a $derived during render — only READS it.
18
+ // Lazily creating it in displaySrc would mutate nabu.systems (a SvelteMap)
19
+ // during a derived → Svelte's state_unsafe_mutation error.
20
+ Files.get(nabu);
21
+ const data = node.data;
22
+ this.ref = data.get('ref') || '';
23
+ this.alt = data.get('alt') || '';
24
+ this.status = data.get('status') || 'ready';
25
+ this.name = data.get('name') || '';
26
+ node.data.subscribe(() => {
27
+ this.ref = node.data.get('ref') || '';
28
+ this.alt = node.data.get('alt') || '';
29
+ this.status = node.data.get('status') || 'ready';
30
+ this.name = node.data.get('name') || '';
31
+ });
32
+ }
33
+
34
+ /** @type {string} */ ref = $state('');
35
+ /** @type {string} */ alt = $state('');
36
+ /** @type {string} */ status = $state('ready');
37
+ /** @type {string} */ name = $state('');
38
+ /** Transient local objectURL while pending (not persisted). @type {string | null} */ previewUrl = $state(null);
39
+
40
+ positions = $derived(1);
41
+
42
+ /** @param {number} offset @returns {{node: Node, offset: number} | null} */
43
+ getDOMPoint(offset) { return null; }
44
+
45
+ /** @param {InputEvent} event */
46
+ beforeinput(event) { return false; }
47
+
48
+ /** Resolved display URL. While pending → local preview; else resolve the ref. @returns {string} */
49
+ get displaySrc() {
50
+ if (this.status === 'pending' && this.previewUrl) return this.previewUrl;
51
+ return Files.get(this.nabu).resolve(this.ref);
52
+ }
53
+
54
+ /** @param {string} v */
55
+ setAlt(v) { this.alt = v; /** @type {any} */ (this.node.data).set('alt', v); this.commit(); }
56
+
57
+ /**
58
+ * Shared node factory. Subclasses call this from their static create().
59
+ * @param {Nabu} nabu @param {string} type @param {{ref?:string,alt?:string,status?:string,name?:string}} [props] @param {string|null} [parentId] @param {number|null} [index]
60
+ * @returns {any}
61
+ */
62
+ static createNode(nabu, type, props = {}, parentId = null, index = null) {
63
+ const node = nabu.tree.createNode(/** @type {any} */ (parentId || undefined), index || undefined);
64
+ const d = /** @type {any} */ (node.data);
65
+ d.set('type', type); d.set('ref', props.ref || ''); d.set('alt', props.alt || '');
66
+ d.set('status', props.status || 'ready'); d.set('name', props.name || '');
67
+ return node;
68
+ }
69
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Insert a media block of `type` from a local File: optimistic preview → persist → ref.
3
+ * @param {import('../nabu.svelte.js').Nabu} nabu @param {string} type @param {File} file
4
+ */
5
+ export function insertMediaFromFile(nabu: import("../nabu.svelte.js").Nabu, type: string, file: File): Promise<void>;
6
+ /**
7
+ * Open a file picker (accept filter) and insert the chosen media as `type`.
8
+ * @param {import('../nabu.svelte.js').Nabu} nabu @param {string} type @param {string} accept
9
+ */
10
+ export function pickMedia(nabu: import("../nabu.svelte.js").Nabu, type: string, accept: string): void;
11
+ /**
12
+ * Insert a media block of `type` from an external URL ref.
13
+ * @param {import('../nabu.svelte.js').Nabu} nabu @param {string} type @param {{ref:string, alt?:string, name?:string}} data
14
+ */
15
+ export function insertMediaUrl(nabu: import("../nabu.svelte.js").Nabu, type: string, data: {
16
+ ref: string;
17
+ alt?: string;
18
+ name?: string;
19
+ }): Promise<void> | undefined;
@@ -0,0 +1,51 @@
1
+ import { Files } from "../../files/files.svelte.js";
2
+
3
+ /**
4
+ * Insert a media block of `type` from a local File: optimistic preview → persist → ref.
5
+ * @param {import('../nabu.svelte.js').Nabu} nabu @param {string} type @param {File} file
6
+ */
7
+ export async function insertMediaFromFile(nabu, type, file) {
8
+ const BlockClass = /** @type {any} */ (nabu.registry.get(type));
9
+ if (!BlockClass) return;
10
+ const anchor = nabu.selection.anchorBlock;
11
+ const parentId = anchor?.node.parent()?.id.toString() ?? null;
12
+ const idx = (anchor?.node.index() ?? -1) + 1;
13
+ const block = BlockClass.create(nabu, type, { status: 'pending', name: file.name }, parentId, idx >= 0 ? idx : null);
14
+ block.previewUrl = URL.createObjectURL(file);
15
+ await nabu.commit();
16
+ const id = block.id;
17
+ try {
18
+ const ref = await Files.get(nabu).persist(file);
19
+ const fresh = nabu.blocks.get(id);
20
+ if (fresh) { const d = /** @type {any} */ (fresh.node.data); d.set('ref', ref); d.set('status', 'ready'); /** @type {any} */ (fresh).previewUrl = null; await nabu.commit(); }
21
+ } catch {
22
+ const fresh = nabu.blocks.get(id);
23
+ if (fresh) { /** @type {any} */ (fresh.node.data).set('status', 'error'); await nabu.commit(); }
24
+ }
25
+ }
26
+
27
+ /**
28
+ * Open a file picker (accept filter) and insert the chosen media as `type`.
29
+ * @param {import('../nabu.svelte.js').Nabu} nabu @param {string} type @param {string} accept
30
+ */
31
+ export function pickMedia(nabu, type, accept) {
32
+ const input = document.createElement('input');
33
+ input.type = 'file'; input.accept = accept;
34
+ input.onchange = () => { const f = input.files?.[0]; if (f) insertMediaFromFile(nabu, type, f); };
35
+ input.click();
36
+ }
37
+
38
+ /**
39
+ * Insert a media block of `type` from an external URL ref.
40
+ * @param {import('../nabu.svelte.js').Nabu} nabu @param {string} type @param {{ref:string, alt?:string, name?:string}} data
41
+ */
42
+ export function insertMediaUrl(nabu, type, data) {
43
+ if (!data?.ref) return;
44
+ const BlockClass = /** @type {any} */ (nabu.registry.get(type));
45
+ if (!BlockClass) return;
46
+ const anchor = nabu.selection.anchorBlock;
47
+ const parentId = anchor?.node.parent()?.id.toString() ?? null;
48
+ const idx = (anchor?.node.index() ?? -1) + 1;
49
+ BlockClass.create(nabu, type, { ref: data.ref, alt: data.alt || '', name: data.name || '', status: 'ready' }, parentId, idx >= 0 ? idx : null);
50
+ return nabu.commit();
51
+ }
@@ -16,6 +16,7 @@ export class MegaBlock extends Block {
16
16
  /** @type {Block[]} */
17
17
  children = $state([]);
18
18
  positions = $derived(this.children.reduce((sum, child) => sum + (child.positions ?? 0), 0));
19
+ wordCount = $derived(this.children.reduce((sum, c) => sum + (c.wordCount ?? 0), 0));
19
20
 
20
21
  /**
21
22
  * Pure container: no own text, delegate entirely to children.
@@ -1,17 +1,3 @@
1
- /**
2
- * @import {Component} from "svelte";
3
- * @import { LoroTreeNode, LoroTree } from "loro-crdt";
4
- * @import {Extension} from '../utils/extensions.js';
5
- */
6
- /**
7
- * @template {Object<string, any>} [T={}]
8
- * @typedef {LoroTreeNode<{type: string} & T>} NabuNode
9
- */
10
- /**
11
- * @typedef {Object} NabuInit
12
- * @property {Extension[]} [extensions]
13
- * @property {Uint8Array<ArrayBufferLike>} [snapshot]
14
- */
15
1
  export class Nabu {
16
2
  static BREAK: symbol;
17
3
  static CONTINUE: symbol;
@@ -24,6 +10,8 @@ export class Nabu {
24
10
  content: import("loro-crdt").LoroMap<Record<string, unknown>>;
25
11
  undoManager: UndoManager;
26
12
  extensions: Extension[];
13
+ /** @type {import('../files/files.svelte.js').FilesConfig} */
14
+ fileConfig: import("../files/files.svelte.js").FilesConfig;
27
15
  BREAK: symbol;
28
16
  CONTINUE: symbol;
29
17
  /** @type {SvelteMap<string, typeof Block>} */
@@ -34,9 +22,30 @@ export class Nabu {
34
22
  blocks: SvelteMap<string, Block>;
35
23
  /** @type {SvelteMap<string, SvelteSet<Block>>} */
36
24
  blocksByType: SvelteMap<string, SvelteSet<Block>>;
37
- /** @type {SvelteMap<string, any>} */
38
- systems: SvelteMap<string, any>;
25
+ /**
26
+ * Registered subsystems. Each system should implement:
27
+ * - `start(): void` — called when the editor mounts
28
+ * - `stop(): void` — called when the editor unmounts
29
+ * @type {SvelteMap<string, { start?: () => void, stop?: () => void, [key: string]: any }>}
30
+ */
31
+ systems: SvelteMap<string, {
32
+ start?: () => void;
33
+ stop?: () => void;
34
+ [key: string]: any;
35
+ }>;
39
36
  hooks: Map<any, any>;
37
+ /**
38
+ * Declarative input rules collected from extensions, tested by TextBehavior
39
+ * against the text before the cursor on matching beforeinput events.
40
+ * @type {import('../utils/extensions.js').InputRule[]}
41
+ */
42
+ inputRules: import("../utils/extensions.js").InputRule[];
43
+ /**
44
+ * Pending inline formatting for the next typed character (active marks at a
45
+ * collapsed cursor). null = inherit from the span at the cursor.
46
+ * @type {Record<string, boolean> | null}
47
+ */
48
+ storedMarks: Record<string, boolean> | null;
40
49
  debugging: boolean;
41
50
  /**
42
51
  * Root-level serializers. Each function receives the Nabu instance and returns the serialized document.
@@ -61,6 +70,25 @@ export class Nabu {
61
70
  exec(topic: string, data?: any): Promise<import("@aionbuilders/pulse").PulseEvent>;
62
71
  trigger(hookName: any, ...args: any[]): void;
63
72
  commit(): Promise<void>;
73
+ /**
74
+ * Run Loro mutations as a single transaction, then optionally restore the cursor.
75
+ * Centralizes the "mutate → commit → setCursor" dance used by most actions.
76
+ * @param {() => void} mutateFn - Mutations Loro ici. Ne PAS appeler commit() dedans.
77
+ * @param {{ block: Block, offset: number } | null} [cursorIntent]
78
+ * @returns {Promise<void>}
79
+ */
80
+ transact(mutateFn: () => void, cursorIntent?: {
81
+ block: Block;
82
+ offset: number;
83
+ } | null): Promise<void>;
84
+ /**
85
+ * True when every selected text run carries the given mark.
86
+ * Reactive: reads block.selected and the behaviors' selections/deltas,
87
+ * so it can be used directly in component templates (e.g. FloatingToolbar).
88
+ * @param {string} markName
89
+ * @returns {boolean}
90
+ */
91
+ isMarkActive(markName: string): boolean;
64
92
  /**
65
93
  * Undo the last operation
66
94
  */
@@ -69,6 +97,13 @@ export class Nabu {
69
97
  * Redo the last undone operation
70
98
  */
71
99
  redo(): void;
100
+ /** Clear the pending stored marks. */
101
+ clearStoredMarks(): void;
102
+ /**
103
+ * Toggle a stored mark for the next typed char (Ctrl+B etc. at a collapsed cursor).
104
+ * @param {string} name
105
+ */
106
+ toggleStoredMark(name: string): void;
72
107
  /** @param {{from: {offset: number, block: Block}, to: {offset: number, block: Block}, offset?: number}} [options={}] */
73
108
  focus(options?: {
74
109
  from: {
@@ -105,6 +140,24 @@ export class Nabu {
105
140
  dispatchEventToSelection(handlerName: string, e: Event, hookName?: string): void;
106
141
  /** @param {InputEvent} e */
107
142
  handleBeforeinput(e: InputEvent): void;
143
+ /**
144
+ * Capture the model position at the start of an IME composition and
145
+ * switch to DOM-first mode for its duration.
146
+ * @param {CompositionEvent} e
147
+ */
148
+ handleCompositionStart(e: CompositionEvent): void;
149
+ /**
150
+ * No-op: the browser manages the DOM freely during composition.
151
+ * @param {CompositionEvent} e
152
+ */
153
+ handleCompositionUpdate(e: CompositionEvent): void;
154
+ /**
155
+ * Sync the composed text into Loro. The browser already inserted `e.data`
156
+ * into the DOM; committing the same text to the model lets the next render
157
+ * converge without duplication.
158
+ * @param {CompositionEvent} e
159
+ */
160
+ handleCompositionEnd(e: CompositionEvent): void;
108
161
  /** @param {KeyboardEvent} e */
109
162
  handleKeydown(e: KeyboardEvent): void;
110
163
  /** @param {InputEvent} e */
@@ -120,6 +173,30 @@ export class Nabu {
120
173
  * @param {import('../utils/extensions.js').PasteFragment} fragment
121
174
  */
122
175
  insertFragment(fragment: import("../utils/extensions.js").PasteFragment): void;
176
+ /**
177
+ * Replace the entire document with the given PasteFragment.
178
+ * Unlike insertFragment(), this does not depend on a live DOM selection:
179
+ * blocks are created at the root through the same registry pipeline as paste.
180
+ * @param {import('../utils/extensions.js').PasteFragment} fragment
181
+ * @returns {Promise<void>}
182
+ */
183
+ importFragment(fragment: import("../utils/extensions.js").PasteFragment): Promise<void>;
184
+ /**
185
+ * Replace the entire document with content parsed from a Markdown string.
186
+ * Requires MarkdownPasteExtension (or any text/plain interpreter) to be loaded.
187
+ * @param {string} markdown
188
+ * @returns {Promise<void>}
189
+ */
190
+ importMarkdown(markdown: string): Promise<void>;
191
+ /**
192
+ * Replace the entire document with a `serialize('json')` export.
193
+ * @param {{version?: string, blocks: any[]} | string} json
194
+ * @returns {Promise<void>}
195
+ */
196
+ importJSON(json: {
197
+ version?: string;
198
+ blocks: any[];
199
+ } | string): Promise<void>;
123
200
  /** @param {number} globalOffset */
124
201
  resolveDocumentOffset(globalOffset: number): {
125
202
  block: Block;
@@ -137,6 +214,7 @@ export type NabuNode<T extends {
137
214
  export type NabuInit = {
138
215
  extensions?: Extension[] | undefined;
139
216
  snapshot?: Uint8Array<ArrayBufferLike> | undefined;
217
+ files?: import("../files/files.svelte.js").FilesConfig | undefined;
140
218
  };
141
219
  import { LoroDoc } from 'loro-crdt';
142
220
  import { NabuSelection } from './selection.svelte.js';