@aionbuilders/nabu 0.1.0-alpha.0 → 0.1.0-alpha.10

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 (108) hide show
  1. package/README.md +70 -1
  2. package/dist/behaviors/text/index.d.ts +1 -1
  3. package/dist/behaviors/text/index.js +1 -1
  4. package/dist/behaviors/text/rich-text.extension.js +96 -30
  5. package/dist/behaviors/text/text.behavior.svelte.d.ts +19 -0
  6. package/dist/behaviors/text/text.behavior.svelte.js +64 -2
  7. package/dist/blocks/Block.svelte +3 -3
  8. package/dist/blocks/BlockGap.svelte +55 -0
  9. package/dist/blocks/BlockGap.svelte.d.ts +11 -0
  10. package/dist/blocks/Nabu.svelte +8 -0
  11. package/dist/blocks/block.extension.d.ts +2 -0
  12. package/dist/blocks/block.extension.js +32 -0
  13. package/dist/blocks/block.svelte.d.ts +41 -9
  14. package/dist/blocks/block.svelte.js +55 -18
  15. package/dist/blocks/container.utils.d.ts +54 -4
  16. package/dist/blocks/container.utils.js +211 -8
  17. package/dist/blocks/dialogue/Dialogue.svelte +46 -0
  18. package/dist/blocks/dialogue/Dialogue.svelte.d.ts +11 -0
  19. package/dist/blocks/dialogue/dialogue.svelte.d.ts +41 -0
  20. package/dist/blocks/dialogue/dialogue.svelte.js +87 -0
  21. package/dist/blocks/dialogue/hooks/onBeforeInput.hook.d.ts +3 -0
  22. package/dist/blocks/dialogue/hooks/onBeforeInput.hook.js +52 -0
  23. package/dist/blocks/dialogue/index.d.ts +7 -0
  24. package/dist/blocks/dialogue/index.js +47 -0
  25. package/dist/blocks/heading/Heading.svelte +13 -9
  26. package/dist/blocks/heading/heading.svelte.d.ts +40 -4
  27. package/dist/blocks/heading/heading.svelte.js +60 -4
  28. package/dist/blocks/heading/hooks/onBeforeInput.hook.js +1 -1
  29. package/dist/blocks/heading/index.d.ts +2 -2
  30. package/dist/blocks/heading/index.js +11 -7
  31. package/dist/blocks/index.d.ts +7 -4
  32. package/dist/blocks/index.js +7 -4
  33. package/dist/blocks/list/ListItem.svelte +6 -2
  34. package/dist/blocks/list/hooks/onBeforeInput.hook.d.ts +3 -0
  35. package/dist/blocks/list/hooks/onBeforeInput.hook.js +77 -0
  36. package/dist/blocks/list/index.d.ts +2 -2
  37. package/dist/blocks/list/index.js +17 -3
  38. package/dist/blocks/list/list-item.svelte.d.ts +59 -6
  39. package/dist/blocks/list/list-item.svelte.js +157 -89
  40. package/dist/blocks/list/list.behavior.svelte.d.ts +1 -0
  41. package/dist/blocks/list/list.behavior.svelte.js +5 -4
  42. package/dist/blocks/list/list.svelte.d.ts +60 -7
  43. package/dist/blocks/list/list.svelte.js +116 -15
  44. package/dist/blocks/megablock.svelte.d.ts +30 -5
  45. package/dist/blocks/megablock.svelte.js +113 -17
  46. package/dist/blocks/nabu.svelte.d.ts +44 -6
  47. package/dist/blocks/nabu.svelte.js +397 -15
  48. package/dist/blocks/paragraph/Paragraph.svelte +12 -16
  49. package/dist/blocks/paragraph/Paragraph.svelte.d.ts +2 -2
  50. package/dist/blocks/paragraph/index.d.ts +2 -2
  51. package/dist/blocks/paragraph/index.js +29 -8
  52. package/dist/blocks/paragraph/paragraph.svelte.d.ts +33 -5
  53. package/dist/blocks/paragraph/paragraph.svelte.js +55 -3
  54. package/dist/blocks/selection.svelte.d.ts +6 -5
  55. package/dist/blocks/selection.svelte.js +209 -18
  56. package/dist/blocks/variable.svelte.d.ts +47 -0
  57. package/dist/blocks/variable.svelte.js +51 -0
  58. package/dist/blocks/zone/TemplateEditor.svelte +308 -0
  59. package/dist/blocks/zone/TemplateEditor.svelte.d.ts +11 -0
  60. package/dist/blocks/zone/Zone.svelte +272 -0
  61. package/dist/blocks/zone/Zone.svelte.d.ts +11 -0
  62. package/dist/blocks/zone/ZoneEditor copy.svelte +118 -0
  63. package/dist/blocks/zone/ZoneEditor copy.svelte.d.ts +11 -0
  64. package/dist/blocks/zone/ZoneEditor.svelte +289 -0
  65. package/dist/blocks/zone/ZoneEditor.svelte.d.ts +11 -0
  66. package/dist/blocks/zone/ZoneTemplatesList.svelte +188 -0
  67. package/dist/blocks/zone/ZoneTemplatesList.svelte.d.ts +11 -0
  68. package/dist/blocks/zone/hooks/onBeforeInput.hook.d.ts +3 -0
  69. package/dist/blocks/zone/hooks/onBeforeInput.hook.js +30 -0
  70. package/dist/blocks/zone/index.d.ts +2 -0
  71. package/dist/blocks/zone/index.js +2 -0
  72. package/dist/blocks/zone/zone.extension.d.ts +2 -0
  73. package/dist/blocks/zone/zone.extension.js +227 -0
  74. package/dist/blocks/zone/zone.svelte.d.ts +222 -0
  75. package/dist/blocks/zone/zone.svelte.js +620 -0
  76. package/dist/index.d.ts +3 -0
  77. package/dist/index.js +4 -1
  78. package/dist/nabu-theme.css +92 -0
  79. package/dist/overlays/context/Context.svelte +179 -0
  80. package/dist/overlays/context/Context.svelte.d.ts +16 -0
  81. package/dist/overlays/context/collect.d.ts +17 -0
  82. package/dist/overlays/context/collect.js +33 -0
  83. package/dist/overlays/context/context.extension.d.ts +2 -0
  84. package/dist/overlays/context/context.extension.js +8 -0
  85. package/dist/overlays/context/context.svelte.d.ts +81 -0
  86. package/dist/overlays/context/context.svelte.js +159 -0
  87. package/dist/overlays/context/index.d.ts +3 -0
  88. package/dist/overlays/context/index.js +3 -0
  89. package/dist/paste/html.paste.d.ts +12 -0
  90. package/dist/paste/html.paste.js +192 -0
  91. package/dist/paste/index.d.ts +4 -0
  92. package/dist/paste/index.js +4 -0
  93. package/dist/paste/markdown.paste.d.ts +12 -0
  94. package/dist/paste/markdown.paste.js +140 -0
  95. package/dist/paste/nabu.paste.d.ts +2 -0
  96. package/dist/paste/nabu.paste.js +20 -0
  97. package/dist/paste/plain-text.paste.d.ts +8 -0
  98. package/dist/paste/plain-text.paste.js +42 -0
  99. package/dist/persistence/indexeddb.d.ts +19 -0
  100. package/dist/persistence/indexeddb.js +71 -0
  101. package/dist/persistence/persistence.d.ts +48 -0
  102. package/dist/persistence/persistence.js +80 -0
  103. package/dist/presets.d.ts +38 -0
  104. package/dist/presets.js +86 -0
  105. package/dist/utils/extensions.d.ts +63 -0
  106. package/dist/utils/extensions.js +33 -0
  107. package/dist/utils/selection.svelte.js +0 -5
  108. package/package.json +14 -3
@@ -1,10 +1,12 @@
1
1
  /**
2
- * @import { Nabu, NabuNode } from "./nabu.svelte";
3
- * @import { MegaBlock } from "./megablock.svelte";
2
+ * @import { Nabu, NabuNode } from "./nabu.svelte.js";
3
+ * @import { MegaBlock } from "./megablock.svelte.js";
4
+ * @import { VariableInit } from "./variable.svelte.js";
4
5
  */
5
6
 
6
7
  import { tick } from "svelte";
7
8
  import { SvelteMap, SvelteSet } from "svelte/reactivity";
9
+ import { Variable } from "./variable.svelte.js";
8
10
 
9
11
 
10
12
  export class Block {
@@ -28,8 +30,24 @@ export class Block {
28
30
  }
29
31
  }
30
32
 
33
+ /** @type {SvelteMap<string, function(any): void>} */
31
34
  serializers = new SvelteMap();
32
35
  behaviors = new SvelteMap();
36
+ /** @type {SvelteMap<string, Variable<this>>} */
37
+ variables = new SvelteMap();
38
+
39
+ /**
40
+ * Declare a variable on this block, which can be used in serializers and behaviors.
41
+ * @param {string} name
42
+ * @param {VariableInit<this>} init
43
+ * @returns {Variable<this>}
44
+ */
45
+ var(name, init) {
46
+ const variable = new Variable(this, name, init);
47
+ return variable;
48
+ }
49
+
50
+
33
51
  selected = $state(false);
34
52
  isSelectionStart = $state(false);
35
53
  isSelectionEnd = $state(false);
@@ -44,6 +62,19 @@ export class Block {
44
62
  /** @type {{from: number, to: number, direction: "forward" | "backward" | "none"} | null} */
45
63
  selection = $derived(null);
46
64
 
65
+ positions = $derived(0);
66
+
67
+ /**
68
+ * Resolve a document-local offset to { block, offset } within this block.
69
+ * Leaf blocks with text return themselves directly.
70
+ * Override in MegaBlock (pure containers) and mixed blocks (e.g. ListItem).
71
+ * @param {number} localOffset
72
+ * @returns {{ block: Block, offset: number } | null}
73
+ */
74
+ resolveOffset(localOffset) {
75
+ return { block: this, offset: localOffset };
76
+ }
77
+
47
78
  /** @type {MegaBlock | null} */
48
79
  parent = $state(null);
49
80
  index = $state(0);
@@ -194,17 +225,17 @@ export class Block {
194
225
 
195
226
  /** @param {number} index @param {string} text */
196
227
  insert(index, text) {
197
- console.warn("Not implemented: insert text", text, "at index", index, "in block", this.id);
228
+ this.nabu.warn("Not implemented: insert text", text, "at index", index, "in block", this.id);
198
229
  }
199
230
 
200
231
  /** @param {{from?: number, to?: number, index?: number, length?: number}} [deletion] */
201
232
  delete(deletion) {
202
- console.warn("Not implemented: delete block", this.id, "with deletion range", deletion);
233
+ this.nabu.warn("Not implemented: delete block", this.id, "with deletion range", deletion);
203
234
  }
204
235
 
205
236
  /** @param {Block} block @returns {any} */
206
237
  absorbs(block) {
207
- console.warn("Not implemented: check if block", this.id, "absorbs block", block.id);
238
+ this.nabu.warn("Not implemented: check if block", this.id, "absorbs block", block.id);
208
239
  return false;
209
240
  }
210
241
 
@@ -235,14 +266,17 @@ export class Block {
235
266
  // Handle children relocation if victim has any
236
267
  // @ts-ignore - MegaBlock has children
237
268
  if (victim.children?.length) {
269
+ // @ts-ignore - adoptChildren only exists on MegaBlock, not on Block base class
238
270
  if (absorbed && survivor.adoptChildren) {
239
271
  // MegaBlock survivor adopts children
240
272
  // @ts-ignore
241
273
  survivor.adoptChildren(victim.children);
242
274
  } else {
243
- // Non-MegaBlock survivor: promote children as siblings
275
+ // Non-MegaBlock survivor: promote children as siblings after survivor.
276
+ // Reverse to preserve original order (each moveAfter inserts right after
277
+ // survivor, so forward iteration would reverse the result).
244
278
  // @ts-ignore
245
- victim.children.forEach(child => {
279
+ [...victim.children].reverse().forEach(child => {
246
280
  child.node.moveAfter(survivor.node);
247
281
  });
248
282
  }
@@ -256,14 +290,17 @@ export class Block {
256
290
  return survivor;
257
291
  }
258
292
 
259
- /** @param {Block[]} children @param {number | null} [index] */
260
- adoptChildren(children, index = null) {
261
- console.warn("Not implemented: adopt children", children.map(c => c.id), "into block", this.id, "at index", index);
262
- }
293
+ /**
294
+ * Declares a required parent type for structural integrity.
295
+ * Override in subclasses (e.g. ListItem requires a "list" parent).
296
+ * Used by wrapOrphan() after block relocations.
297
+ * @returns {{ type: string, props: () => Record<string, any> } | null}
298
+ */
299
+ get requiredParent() { return null; }
263
300
 
264
301
  /** @param {{from?: number, to?: number, index?: number, length?: number, offset?: number}} options @returns {{block: Block} | null} */
265
302
  split(options) {
266
- console.warn("Not implemented: split block", this.id, "with options", options);
303
+ this.nabu.warn("Not implemented: split block", this.id, "with options", options);
267
304
  return null;
268
305
  }
269
306
 
@@ -287,7 +324,7 @@ export class Block {
287
324
  * @returns {{node: Node, offset: number} | null}
288
325
  */
289
326
  getDOMPoint(offset) {
290
- console.warn("getDOMPoint not implemented for block", this.type, this.id);
327
+ this.nabu.warn("getDOMPoint not implemented for block", this.type, this.id);
291
328
  return null;
292
329
  }
293
330
 
@@ -328,17 +365,17 @@ export class Block {
328
365
  // -- UTILS --
329
366
 
330
367
  commit() {
331
- this.nabu.commit();
368
+ return this.nabu.commit();
332
369
  }
333
370
 
334
371
 
335
- /** @param {string} format */
336
- serialize(format) {
372
+ /** @param {string} format @param {Record<string, any>} [context] */
373
+ serialize(format, context = {}) {
337
374
  const serializer = this.serializers.get(format);
338
375
  if (serializer) {
339
- return serializer(this);
376
+ return serializer(context);
340
377
  } else {
341
- console.warn(`No serializer found for format "${format}" on block type "${this.type}"`);
378
+ this.nabu.warn(`No serializer found for format "${format}" on block type "${this.type}"`);
342
379
  return null;
343
380
  }
344
381
  }
@@ -1,7 +1,42 @@
1
1
  /**
2
- * @import { Block } from './block.svelte';
3
- * @import { Nabu } from './nabu.svelte';
2
+ * Walks up from `block` toward `container` and returns the direct child of
3
+ * `container` that is (or contains) `block`.
4
+ *
5
+ * Works for both Nabu (root blocks have parent = null, loop breaks before
6
+ * reaching Nabu) and MegaBlock (loop stops when parent === container).
7
+ *
8
+ * @param {Block} block
9
+ * @param {Block | Nabu} container
10
+ * @returns {Block | null}
11
+ */
12
+ export function findDirectChildOf(block: Block, container: Block | Nabu): Block | null;
13
+ /**
14
+ * Finds the Lowest Common Ancestor (LCA) of two blocks in the block tree.
15
+ *
16
+ * Returns the LCA block, or null if the two blocks only share Nabu as ancestor
17
+ * (i.e. both ancestor chains terminate at a root block with parent = null).
18
+ *
19
+ * Examples:
20
+ * LCA(subA, subB) where both are children of subList → subList
21
+ * LCA(para1, para2) where both are root-level blocks → null
22
+ *
23
+ * @param {Block} blockA
24
+ * @param {Block} blockB
25
+ * @returns {Block | null}
4
26
  */
27
+ export function findLCA(blockA: Block, blockB: Block): Block | null;
28
+ /**
29
+ * Ensures a block respects its structural parent constraint after being relocated.
30
+ * If the block's current Loro parent doesn't match its `requiredParent` type,
31
+ * creates a wrapper block at the same position WITHOUT committing.
32
+ *
33
+ * Returns the wrapper block if one was created, otherwise undefined.
34
+ *
35
+ * @param {Nabu} nabu
36
+ * @param {Block} block
37
+ * @returns {Block | undefined}
38
+ */
39
+ export function wrapOrphan(nabu: Nabu, block: Block): Block | undefined;
5
40
  /**
6
41
  * Shared beforeinput logic for block containers (Nabu root and MegaBlock).
7
42
  *
@@ -24,5 +59,20 @@ export function handleContainerBeforeInput(container: {
24
59
  children: Block[];
25
60
  commit: () => void;
26
61
  }, nabu: Nabu, event: InputEvent): any;
27
- import type { Block } from './block.svelte';
28
- import type { Nabu } from './nabu.svelte';
62
+ /**
63
+ * Deletes the current selection content without committing.
64
+ * Handles both single-block and multi-block selections.
65
+ * Returns the resulting insertion position {block, offset}, or null on failure.
66
+ *
67
+ * @param {{ children: import('./block.svelte.js').Block[] }} container
68
+ * @param {import('./nabu.svelte.js').Nabu} nabu
69
+ * @returns {{ block: import('./block.svelte.js').Block, offset: number } | null}
70
+ */
71
+ export function deleteSelectionContent(container: {
72
+ children: import("./block.svelte.js").Block[];
73
+ }, nabu: import("./nabu.svelte.js").Nabu): {
74
+ block: import("./block.svelte.js").Block;
75
+ offset: number;
76
+ } | null;
77
+ import type { Block } from './block.svelte.js';
78
+ import type { Nabu } from './nabu.svelte.js';
@@ -1,8 +1,106 @@
1
1
  /**
2
- * @import { Block } from './block.svelte';
3
- * @import { Nabu } from './nabu.svelte';
2
+ * @import { Block } from './block.svelte.js';
3
+ * @import { Nabu } from './nabu.svelte.js';
4
4
  */
5
5
 
6
+ import { MegaBlock } from './megablock.svelte.js';
7
+
8
+ /**
9
+ * Walks up from `block` toward `container` and returns the direct child of
10
+ * `container` that is (or contains) `block`.
11
+ *
12
+ * Works for both Nabu (root blocks have parent = null, loop breaks before
13
+ * reaching Nabu) and MegaBlock (loop stops when parent === container).
14
+ *
15
+ * @param {Block} block
16
+ * @param {Block | Nabu} container
17
+ * @returns {Block | null}
18
+ */
19
+ export function findDirectChildOf(block, container) {
20
+ const spine = [block];
21
+ while (spine.at(-1) && spine.at(-1) !== container) {
22
+ const current = spine.at(-1);
23
+ const parent = current?.parent;
24
+ if (!current || !parent) break;
25
+ spine.push(parent);
26
+ }
27
+ return spine.at(-1) === container ? spine.at(-2) ?? null : spine.at(-1) ?? null;
28
+ }
29
+
30
+ /**
31
+ * Finds the Lowest Common Ancestor (LCA) of two blocks in the block tree.
32
+ *
33
+ * Returns the LCA block, or null if the two blocks only share Nabu as ancestor
34
+ * (i.e. both ancestor chains terminate at a root block with parent = null).
35
+ *
36
+ * Examples:
37
+ * LCA(subA, subB) where both are children of subList → subList
38
+ * LCA(para1, para2) where both are root-level blocks → null
39
+ *
40
+ * @param {Block} blockA
41
+ * @param {Block} blockB
42
+ * @returns {Block | null}
43
+ */
44
+ export function findLCA(blockA, blockB) {
45
+ if (blockA === blockB) return blockA.parent ?? null;
46
+
47
+ // Build the ancestor set for blockA (blockA itself included)
48
+ const ancestorsA = new Set();
49
+ let curr = /** @type {Block | null} */ (blockA);
50
+ while (curr) {
51
+ ancestorsA.add(curr);
52
+ curr = curr.parent ?? null;
53
+ }
54
+
55
+ // Walk from blockB up until we hit a node in blockA's ancestor set
56
+ let currB = /** @type {Block | null} */ (blockB);
57
+ while (currB) {
58
+ if (ancestorsA.has(currB)) return currB;
59
+ currB = currB.parent ?? null;
60
+ }
61
+
62
+ return null; // only Nabu is a common ancestor
63
+ }
64
+
65
+ /**
66
+ * Ensures a block respects its structural parent constraint after being relocated.
67
+ * If the block's current Loro parent doesn't match its `requiredParent` type,
68
+ * creates a wrapper block at the same position WITHOUT committing.
69
+ *
70
+ * Returns the wrapper block if one was created, otherwise undefined.
71
+ *
72
+ * @param {Nabu} nabu
73
+ * @param {Block} block
74
+ * @returns {Block | undefined}
75
+ */
76
+ export function wrapOrphan(nabu, block) {
77
+ const req = block.requiredParent;
78
+ if (!req) return;
79
+
80
+ // Read the CURRENT Loro parent (not the stale Svelte $state)
81
+ const currentLoroParent = block.node.parent();
82
+ const currentParentType = currentLoroParent?.data?.get("type") ?? null;
83
+
84
+ // Already in the correct parent context — nothing to do
85
+ if (currentParentType === req.type) return;
86
+
87
+ const WrapperClass = nabu.registry.get(req.type);
88
+ if (!WrapperClass) {
89
+ nabu.warn(`wrapOrphan: block type "${req.type}" is not registered`);
90
+ return;
91
+ }
92
+
93
+ // Create wrapper at the block's current position (no commit)
94
+ const loroParentId = currentLoroParent?.id?.toString() || null;
95
+ const index = block.node.index() ?? undefined; // null-guard for safety
96
+
97
+ const wrapper = WrapperClass.create(nabu, req.type, req.props?.() || {}, loroParentId, index);
98
+
99
+ // Move block into wrapper (no commit)
100
+ nabu.tree.move(block.node.id, wrapper.node.id);
101
+ return wrapper;
102
+ }
103
+
6
104
  /**
7
105
  * Shared beforeinput logic for block containers (Nabu root and MegaBlock).
8
106
  *
@@ -29,6 +127,8 @@ export function handleContainerBeforeInput(container, nabu, event) {
29
127
  const focusData = startBlock.focus(undefined, true);
30
128
  const inputType = event.inputType;
31
129
 
130
+ const toDestroy = new Set();
131
+
32
132
  // Build start spine: walk from startBlock up to (but not past) container,
33
133
  // pruning next-siblings of each intermediate node along the way.
34
134
  const startSpine = [startBlock];
@@ -39,7 +139,7 @@ export function handleContainerBeforeInput(container, nabu, event) {
39
139
  startSpine.push(parent);
40
140
  if (parent !== container) {
41
141
  const nextSiblings = parent.children.slice(current.index + 1);
42
- if (nextSiblings.length) nextSiblings.forEach(block => block.destroy());
142
+ if (nextSiblings.length) nextSiblings.forEach(block => toDestroy.add(block));
43
143
  }
44
144
  }
45
145
 
@@ -53,7 +153,7 @@ export function handleContainerBeforeInput(container, nabu, event) {
53
153
  endSpine.push(parent);
54
154
  if (parent !== container) {
55
155
  const previousSiblings = parent.children.slice(0, current.index);
56
- if (previousSiblings.length) previousSiblings.forEach(block => block.destroy());
156
+ if (previousSiblings.length) previousSiblings.forEach(block => toDestroy.add(block));
57
157
  }
58
158
  }
59
159
 
@@ -65,7 +165,7 @@ export function handleContainerBeforeInput(container, nabu, event) {
65
165
 
66
166
  // Delete intermediate direct children of container between the two spines.
67
167
  const intermediates = container.children.slice(startOfStartSpine.index + 1, startOfEndSpine.index);
68
- intermediates.forEach(block => block.destroy());
168
+ intermediates.forEach(block => toDestroy.add(block));
69
169
 
70
170
  // Single-block selection: delegate directly to that block.
71
171
  if (startBlock === endBlock) {
@@ -87,7 +187,7 @@ export function handleContainerBeforeInput(container, nabu, event) {
87
187
  const text = event.data || (inputType === 'insertLineBreak' ? '\n' : '');
88
188
  startBlock.insert(focusData.options.startOffset, text);
89
189
  } else {
90
- console.warn('Unhandled input type in container:', inputType);
190
+ nabu.warn('Unhandled input type in container:', inputType);
91
191
  return false;
92
192
  }
93
193
 
@@ -100,11 +200,32 @@ export function handleContainerBeforeInput(container, nabu, event) {
100
200
  const index = brotherhood.indexOf(block);
101
201
  if (index !== -1) {
102
202
  const nextSiblings = brotherhood.slice(index + 1);
103
- if (nextSiblings.length) nextSiblings.forEach(sibling => sibling.node.moveAfter(focusBlock.node));
203
+ if (nextSiblings.length) {
204
+ // Reverse to preserve original order: moveAfter(focus) inserts right
205
+ // after focus each time, so forward iteration would reverse the result.
206
+ // wrapOrphan ensures structural integrity (e.g. ListItem → needs List parent).
207
+ [...nextSiblings].reverse().forEach(sibling => {
208
+ sibling.node.moveAfter(focusBlock.node);
209
+ wrapOrphan(nabu, sibling);
210
+ });
211
+ }
104
212
  }
105
213
  });
106
214
 
107
- startOfEndSpine?.destroy();
215
+ if (startOfEndSpine === startOfStartSpine) {
216
+ if (startOfStartSpine instanceof MegaBlock) {
217
+ startOfStartSpine.children.filter(c => c.selected).forEach(c => toDestroy.add(c));
218
+ }
219
+ }
220
+
221
+ if (startOfEndSpine !== startOfStartSpine) {
222
+ startOfEndSpine?.destroy();
223
+ if (startOfStartSpine !== startBlock && startBlock instanceof MegaBlock) {
224
+ startBlock.children.forEach(child => child.destroy());
225
+ }
226
+ }
227
+
228
+ toDestroy.forEach(block => block.destroy());
108
229
 
109
230
  container.commit();
110
231
  startBlock.focus({
@@ -112,3 +233,85 @@ export function handleContainerBeforeInput(container, nabu, event) {
112
233
  });
113
234
  return true;
114
235
  }
236
+
237
+ /**
238
+ * Deletes the current selection content without committing.
239
+ * Handles both single-block and multi-block selections.
240
+ * Returns the resulting insertion position {block, offset}, or null on failure.
241
+ *
242
+ * @param {{ children: import('./block.svelte.js').Block[] }} container
243
+ * @param {import('./nabu.svelte.js').Nabu} nabu
244
+ * @returns {{ block: import('./block.svelte.js').Block, offset: number } | null}
245
+ */
246
+ export function deleteSelectionContent(container, nabu) {
247
+ const startBlock = /** @type {import('./block.svelte.js').Block} */ (nabu.selection.startBlock);
248
+ const endBlock = /** @type {import('./block.svelte.js').Block} */ (nabu.selection.endBlock);
249
+ if (!startBlock || !endBlock) return null;
250
+
251
+ // Single-block selection: delete the range only
252
+ if (startBlock === endBlock) {
253
+ const sel = startBlock.selection;
254
+ if (!sel || sel.from === sel.to) return { block: startBlock, offset: sel?.from ?? 0 };
255
+ const tb = startBlock.behaviors?.get('text');
256
+ if (tb) tb.delete({ from: sel.from, to: sel.to });
257
+ return { block: startBlock, offset: sel.from };
258
+ }
259
+
260
+ // Multi-block selection: same spine logic as handleContainerBeforeInput (no commit)
261
+ const startOffset = startBlock.focus(undefined, true).options.startOffset;
262
+
263
+ const startSpine = [startBlock];
264
+ while (startSpine.at(-1) && startSpine.at(-1) !== container) {
265
+ const current = startSpine.at(-1);
266
+ const parent = current?.parent;
267
+ if (!current || !parent) break;
268
+ startSpine.push(parent);
269
+ if (parent !== container) {
270
+ const nextSiblings = parent.children.slice(current.index + 1);
271
+ if (nextSiblings.length) nextSiblings.forEach(block => block.destroy());
272
+ }
273
+ }
274
+
275
+ const endSpine = [endBlock];
276
+ while (endSpine.at(-1) && endSpine.at(-1) !== container) {
277
+ const current = endSpine.at(-1);
278
+ const parent = current?.parent;
279
+ if (!current || !parent) break;
280
+ endSpine.push(parent);
281
+ if (parent !== container) {
282
+ const previousSiblings = parent.children.slice(0, current.index);
283
+ if (previousSiblings.length) previousSiblings.forEach(block => block.destroy());
284
+ }
285
+ }
286
+
287
+ const directChild = (spine) => spine.at(-1) === container ? spine.at(-2) : spine.at(-1);
288
+ const startOfStartSpine = directChild(startSpine);
289
+ const startOfEndSpine = directChild(endSpine);
290
+ if (!startOfStartSpine || !startOfEndSpine) return null;
291
+
292
+ const intermediates = container.children.slice(startOfStartSpine.index + 1, startOfEndSpine.index);
293
+ intermediates.forEach(block => block.destroy());
294
+
295
+ startBlock.delete();
296
+ endBlock.delete();
297
+ startBlock.consume(endBlock);
298
+
299
+ endSpine.forEach(block => {
300
+ if (block === container) return;
301
+ const brotherhood = block.parent?.children || [];
302
+ const index = brotherhood.indexOf(block);
303
+ if (index !== -1) {
304
+ const nextSiblings = brotherhood.slice(index + 1);
305
+ if (nextSiblings.length) {
306
+ [...nextSiblings].reverse().forEach(sibling => {
307
+ sibling.node.moveAfter(startBlock.node);
308
+ wrapOrphan(nabu, sibling);
309
+ });
310
+ }
311
+ }
312
+ });
313
+
314
+ startOfEndSpine?.destroy();
315
+
316
+ return { block: startBlock, offset: startOffset };
317
+ }
@@ -0,0 +1,46 @@
1
+ <script>
2
+ import RichText from '../../behaviors/text/RichText.svelte';
3
+ /** @type {{block: import('./dialogue.svelte.js').Dialogue}}*/
4
+ let {block} = $props();
5
+ </script>
6
+
7
+ <div
8
+ bind:this={block.element}
9
+ data-block-id={block.id}
10
+ data-block-type="dialogue"
11
+ class="nabu-dialogue"
12
+ class:debug={block.nabu.debugging}
13
+ class:selected={block.selected}
14
+ class:first={block.isSelectionStart}
15
+ class:last={block.isSelectionEnd}
16
+ ><RichText delta={block.delta} /></div>
17
+
18
+ <style>
19
+ .nabu-dialogue {
20
+ white-space: pre-wrap;
21
+ margin-bottom: 0.5em;
22
+ padding-left: 1em;
23
+ position: relative;
24
+ }
25
+
26
+ .nabu-dialogue::before {
27
+ /* content: "—"; */
28
+ content: "–";
29
+ position: absolute;
30
+ left: 0;
31
+ user-select: none;
32
+ pointer-events: none;
33
+ }
34
+
35
+ .nabu-dialogue.debug {
36
+ &.selected {
37
+ background-color: rgba(59, 130, 246, 0.25);
38
+ }
39
+ &.first {
40
+ border-top: 1px solid rgba(59, 130, 246, 0.5);
41
+ }
42
+ &.last {
43
+ border-bottom: 1px solid rgba(59, 130, 246, 0.5);
44
+ }
45
+ }
46
+ </style>
@@ -0,0 +1,11 @@
1
+ export default Dialogue;
2
+ type Dialogue = {
3
+ $on?(type: string, callback: (e: any) => void): () => void;
4
+ $set?(props: Partial<$$ComponentProps>): void;
5
+ };
6
+ declare const Dialogue: import("svelte").Component<{
7
+ block: import("./dialogue.svelte.js").Dialogue;
8
+ }, {}, "">;
9
+ type $$ComponentProps = {
10
+ block: import("./dialogue.svelte.js").Dialogue;
11
+ };
@@ -0,0 +1,41 @@
1
+ /**
2
+ * @import { Nabu, NabuNode } from "../nabu.svelte.js";
3
+ */
4
+ /**
5
+ * @typedef {NabuNode<{type: "dialogue", text: LoroText}>} DialogueNode
6
+ */
7
+ export class Dialogue extends Block {
8
+ /** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
9
+ static create(nabu: Nabu, type: string, props?: Object, parentId?: string | null, index?: number | null): Dialogue;
10
+ /** @param {Nabu} nabu @param {DialogueNode} node */
11
+ constructor(nabu: Nabu, node: DialogueNode);
12
+ container: LoroText;
13
+ /** @type {TextBehavior} */
14
+ behavior: TextBehavior;
15
+ component: import("svelte").Component<any, any, string> | import("svelte").Component<{
16
+ block: import("./dialogue.svelte.js").Dialogue;
17
+ }, {}, "">;
18
+ get text(): string;
19
+ get delta(): import("loro-crdt").Delta<string>[];
20
+ selection: {
21
+ from: number;
22
+ to: number;
23
+ isCollapsed: boolean;
24
+ direction: "forward" | "backward" | "none";
25
+ } | null;
26
+ /** @param {Block} block */
27
+ absorbs(block: Block): boolean;
28
+ /** @param {import('loro-crdt').Delta<string>[]} data */
29
+ applyDelta(data?: import("loro-crdt").Delta<string>[]): void;
30
+ /** @param {Parameters<Block["split"]>[0]} [options] @returns {ReturnType<Block["split"]>} */
31
+ split(options?: Parameters<Block["split"]>[0]): ReturnType<Block["split"]>;
32
+ }
33
+ export type DialogueNode = NabuNode<{
34
+ type: "dialogue";
35
+ text: LoroText;
36
+ }>;
37
+ import { Block } from "../block.svelte.js";
38
+ import { LoroText } from "loro-crdt";
39
+ import { TextBehavior } from "../../behaviors/text";
40
+ import type { Nabu } from "../nabu.svelte.js";
41
+ import type { NabuNode } from "../nabu.svelte.js";
@@ -0,0 +1,87 @@
1
+ import { Block } from "../block.svelte.js";
2
+ import { LoroText } from "loro-crdt";
3
+ import DialogueComponent from "./Dialogue.svelte";
4
+ import { TextBehavior } from "../../behaviors/text";
5
+
6
+ /**
7
+ * @import { Nabu, NabuNode } from "../nabu.svelte.js";
8
+ */
9
+
10
+ /**
11
+ * @typedef {NabuNode<{type: "dialogue", text: LoroText}>} DialogueNode
12
+ */
13
+
14
+ export class Dialogue extends Block {
15
+ /** @param {Nabu} nabu @param {DialogueNode} node */
16
+ constructor(nabu, node) {
17
+ super(nabu, node);
18
+
19
+ const data = node.data;
20
+ this.container = data.get("text") ?? data.setContainer("text", new LoroText());
21
+
22
+ /** @type {TextBehavior} */
23
+ this.behavior = new TextBehavior(this, this.container);
24
+ this.behaviors.set("text", this.behavior);
25
+
26
+ this.serializers.set('markdown', () => `— ${this.behavior.toMarkdown()}`);
27
+ this.serializers.set('json', () => ({
28
+ id: this.id,
29
+ type: 'dialogue',
30
+ content: this.behavior.toJSON()
31
+ }));
32
+ this.serializers.set('application/x-nabu+json', (ctx) => this.behavior.toClipboardBlock(ctx));
33
+ }
34
+
35
+ component = $derived(this.nabu.components.get("dialogue") || DialogueComponent);
36
+
37
+ get text() {
38
+ return this.behavior.text;
39
+ }
40
+
41
+ get delta() {
42
+ return this.behavior.delta;
43
+ }
44
+
45
+ selection = $derived(this.behavior.selection);
46
+ positions = $derived(this.behavior.positions);
47
+
48
+ /** @param {InputEvent} event */
49
+ beforeinput(event) {
50
+ return this.behavior.handleBeforeInput(event);
51
+ }
52
+
53
+ /**
54
+ * @param {number} targetOffset
55
+ * @returns {{node: Node, offset: number} | null}
56
+ */
57
+ getDOMPoint(targetOffset) {
58
+ if (!this.element) return null;
59
+ return this.behavior.getDOMPoint(targetOffset);
60
+ }
61
+
62
+ /** @param {Block} block */
63
+ absorbs(block) { return this.behavior.absorbs(block); }
64
+
65
+ /** @param {number} index @param {string} text */
66
+ insert(index, text) { return this.behavior.insert(index, text); }
67
+
68
+ /** @param {Parameters<Block["delete"]>[0]} [deletion] */
69
+ delete(deletion) { return this.behavior.delete(deletion); }
70
+
71
+ /** @param {import('loro-crdt').Delta<string>[]} data */
72
+ applyDelta(data = []) { return this.behavior.applyDelta(data); }
73
+
74
+ /** @param {Parameters<Block["split"]>[0]} [options] @returns {ReturnType<Block["split"]>} */
75
+ split(options) { return this.behavior.split(options); }
76
+
77
+ /** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
78
+ static create(nabu, type, props = {}, parentId = null, index = null) {
79
+ const node = nabu.tree.createNode(parentId || undefined, index || undefined);
80
+ node.data.set("type", "dialogue");
81
+ const container = node.data.setContainer("text", new LoroText());
82
+ if (props.text) container.insert(0, props.text);
83
+ if (props.delta) container.applyDelta([...props.delta]);
84
+ const block = new Dialogue(nabu, node);
85
+ return block;
86
+ }
87
+ }