@aionbuilders/nabu 0.1.0-alpha.5 → 0.1.0-alpha.6

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 (42) hide show
  1. package/dist/behaviors/text/rich-text.extension.js +9 -0
  2. package/dist/behaviors/text/text.behavior.svelte.d.ts +19 -0
  3. package/dist/behaviors/text/text.behavior.svelte.js +49 -0
  4. package/dist/blocks/Nabu.svelte +3 -0
  5. package/dist/blocks/block.extension.js +1 -0
  6. package/dist/blocks/block.svelte.d.ts +17 -4
  7. package/dist/blocks/block.svelte.js +18 -4
  8. package/dist/blocks/container.utils.d.ts +54 -0
  9. package/dist/blocks/container.utils.js +165 -8
  10. package/dist/blocks/dialogue/dialogue.svelte.js +2 -0
  11. package/dist/blocks/dialogue/index.js +1 -5
  12. package/dist/blocks/heading/heading.svelte.d.ts +36 -0
  13. package/dist/blocks/heading/heading.svelte.js +58 -2
  14. package/dist/blocks/heading/index.js +1 -5
  15. package/dist/blocks/list/list-item.svelte.d.ts +29 -0
  16. package/dist/blocks/list/list-item.svelte.js +69 -6
  17. package/dist/blocks/list/list.svelte.d.ts +53 -0
  18. package/dist/blocks/list/list.svelte.js +112 -11
  19. package/dist/blocks/megablock.svelte.d.ts +27 -0
  20. package/dist/blocks/megablock.svelte.js +103 -1
  21. package/dist/blocks/nabu.svelte.d.ts +20 -2
  22. package/dist/blocks/nabu.svelte.js +358 -9
  23. package/dist/blocks/paragraph/index.js +18 -6
  24. package/dist/blocks/paragraph/paragraph.svelte.d.ts +28 -0
  25. package/dist/blocks/paragraph/paragraph.svelte.js +53 -1
  26. package/dist/index.d.ts +1 -0
  27. package/dist/index.js +2 -1
  28. package/dist/paste/html.paste.d.ts +12 -0
  29. package/dist/paste/html.paste.js +192 -0
  30. package/dist/paste/index.d.ts +4 -0
  31. package/dist/paste/index.js +4 -0
  32. package/dist/paste/markdown.paste.d.ts +12 -0
  33. package/dist/paste/markdown.paste.js +140 -0
  34. package/dist/paste/nabu.paste.d.ts +2 -0
  35. package/dist/paste/nabu.paste.js +20 -0
  36. package/dist/paste/plain-text.paste.d.ts +8 -0
  37. package/dist/paste/plain-text.paste.js +42 -0
  38. package/dist/presets.js +6 -4
  39. package/dist/utils/extensions.d.ts +59 -0
  40. package/dist/utils/extensions.js +30 -0
  41. package/dist/utils/selection.svelte.js +0 -5
  42. package/package.json +13 -3
@@ -116,6 +116,15 @@ function onKeyDown(nabu, event) {
116
116
  }
117
117
 
118
118
  export const RichTextExtension = new Extension('rich-text', {
119
+ // Declare all mark attributes so Loro's applyDelta can handle them correctly.
120
+ // 'expand: after' means the style extends when typing at the end of a marked span.
121
+ loroTextStyles: {
122
+ bold: { expand: 'after' },
123
+ italic: { expand: 'after' },
124
+ underline: { expand: 'after' },
125
+ code: { expand: 'none' }, // code spans don't expand on insert
126
+ strikethrough: { expand: 'after' },
127
+ },
119
128
  actions: {
120
129
  // Full API
121
130
  'mark:toggle': (nabu, data) => toggleMark(nabu, data?.mark),
@@ -1,3 +1,10 @@
1
+ /**
2
+ * Converts a Loro text delta to inline HTML.
3
+ * Marks are rendered as semantic HTML tags.
4
+ * @param {import('loro-crdt').Delta<string>[]} delta
5
+ * @returns {string}
6
+ */
7
+ export function deltaToHtml(delta: import("loro-crdt").Delta<string>[]): string;
1
8
  /**
2
9
  * Converts a Loro text delta to inline Markdown.
3
10
  * @param {import('loro-crdt').Delta<string>[]} delta
@@ -22,6 +29,7 @@ export class TextBehavior {
22
29
  isCollapsed: boolean;
23
30
  direction: "forward" | "backward" | "none";
24
31
  } | null;
32
+ positions: number;
25
33
  /**
26
34
  * @param {HTMLElement} element
27
35
  * @param {NabuSelection} globalSelection
@@ -90,6 +98,17 @@ export class TextBehavior {
90
98
  text: string;
91
99
  [mark: string]: any;
92
100
  }[];
101
+ /**
102
+ * Serializes a (partial) range of this block's text as a PasteBlock.
103
+ * @param {{ from?: number, to?: number, partial?: 'start'|'end'|'both'|false, props?: Record<string,any> }} [context]
104
+ * @returns {import('../utils/extensions.js').PasteBlock}
105
+ */
106
+ toClipboardBlock({ from, to, partial, props }?: {
107
+ from?: number;
108
+ to?: number;
109
+ partial?: "start" | "end" | "both" | false;
110
+ props?: Record<string, any>;
111
+ }): any;
93
112
  /** @param {Block} other */
94
113
  absorbs(other: Block): boolean;
95
114
  }
@@ -5,6 +5,38 @@
5
5
  import { LoroText } from "loro-crdt";
6
6
  import { tick } from "svelte";
7
7
 
8
+ /**
9
+ * Escape special HTML characters in plain text.
10
+ * @param {string} text
11
+ * @returns {string}
12
+ */
13
+ function escapeHtml(text) {
14
+ return text
15
+ .replace(/&/g, '&amp;')
16
+ .replace(/</g, '&lt;')
17
+ .replace(/>/g, '&gt;');
18
+ }
19
+
20
+ /**
21
+ * Converts a Loro text delta to inline HTML.
22
+ * Marks are rendered as semantic HTML tags.
23
+ * @param {import('loro-crdt').Delta<string>[]} delta
24
+ * @returns {string}
25
+ */
26
+ export function deltaToHtml(delta) {
27
+ return delta.map(op => {
28
+ if (typeof op.insert !== 'string') return '';
29
+ let text = escapeHtml(op.insert);
30
+ const attrs = op.attributes || {};
31
+ if (attrs.code) text = `<code>${text}</code>`;
32
+ if (attrs.bold) text = `<strong>${text}</strong>`;
33
+ if (attrs.italic) text = `<em>${text}</em>`;
34
+ if (attrs.underline) text = `<u>${text}</u>`;
35
+ if (attrs.strikethrough) text = `<s>${text}</s>`;
36
+ return text;
37
+ }).join('');
38
+ }
39
+
8
40
  /**
9
41
  * Converts a Loro text delta to inline Markdown.
10
42
  * @param {import('loro-crdt').Delta<string>[]} delta
@@ -47,6 +79,8 @@ export class TextBehavior {
47
79
 
48
80
  selection = $derived(this.block.element && this.block.nabu.selection && this.getSelection(this.block.element, this.block.nabu.selection));
49
81
 
82
+ positions = $derived(this.text.length + 1);
83
+
50
84
  /**
51
85
  * @param {HTMLElement} element
52
86
  * @param {NabuSelection} globalSelection
@@ -344,6 +378,21 @@ export class TextBehavior {
344
378
  });
345
379
  }
346
380
 
381
+ /**
382
+ * Serializes a (partial) range of this block's text as a PasteBlock.
383
+ * @param {{ from?: number, to?: number, partial?: 'start'|'end'|'both'|false, props?: Record<string,any> }} [context]
384
+ * @returns {import('../utils/extensions.js').PasteBlock}
385
+ */
386
+ toClipboardBlock({ from = 0, to = null, partial = false, props = {} } = {}) {
387
+ const end = to ?? this.container.length;
388
+ return {
389
+ type: this.block.type,
390
+ partial,
391
+ delta: this.container.sliceDelta(from, end),
392
+ props
393
+ };
394
+ }
395
+
347
396
  /** @param {Block} other */
348
397
  absorbs(other) {
349
398
  const otherBehavior = other.behaviors.get("text");
@@ -14,6 +14,9 @@
14
14
  data-nabu-root="true"
15
15
  onbeforeinput={(e) => engine.handleBeforeinput(e)}
16
16
  onkeydown={(e) => engine.handleKeydown(e)}
17
+ oncopy={(e) => engine.handleCopy(e)}
18
+ oncut={(e) => engine.handleCut(e)}
19
+ onpaste={(e) => engine.handlePaste(e)}
17
20
  >
18
21
  {#each engine.children as block (block.id)}
19
22
  <Block {block} />
@@ -12,6 +12,7 @@ import { Extension } from '../utils/extensions.js';
12
12
  * @param {Object} [props={}]
13
13
  */
14
14
  function transformBlock(nabu, type, props = {}) {
15
+ console.log(`Transforming block to type "${type}" with props`, props);
15
16
  const block = nabu.selection.anchorBlock;
16
17
  if (!block) return;
17
18
  const blockId = block.id;
@@ -11,7 +11,8 @@ export class Block {
11
11
  type: string;
12
12
  /** @type {MegaBlock | null} */
13
13
  parent: MegaBlock | null;
14
- serializers: SvelteMap<any, any>;
14
+ /** @type {SvelteMap<string, function(any): void>} */
15
+ serializers: SvelteMap<string, (arg0: any) => void>;
15
16
  behaviors: SvelteMap<any, any>;
16
17
  selected: boolean;
17
18
  isSelectionStart: boolean;
@@ -24,6 +25,18 @@ export class Block {
24
25
  to: number;
25
26
  direction: "forward" | "backward" | "none";
26
27
  } | null;
28
+ positions: number;
29
+ /**
30
+ * Resolve a document-local offset to { block, offset } within this block.
31
+ * Leaf blocks with text return themselves directly.
32
+ * Override in MegaBlock (pure containers) and mixed blocks (e.g. ListItem).
33
+ * @param {number} localOffset
34
+ * @returns {{ block: Block, offset: number } | null}
35
+ */
36
+ resolveOffset(localOffset: number): {
37
+ block: Block;
38
+ offset: number;
39
+ } | null;
27
40
  index: number;
28
41
  /** @type {Block?} */
29
42
  previous: Block | null;
@@ -141,9 +154,9 @@ export class Block {
141
154
  beforeinput(event: InputEvent): any;
142
155
  /** @param {KeyboardEvent} event @returns {any} */
143
156
  keydown(event: KeyboardEvent): any;
144
- commit(): void;
145
- /** @param {string} format */
146
- serialize(format: string): any;
157
+ commit(): Promise<void>;
158
+ /** @param {string} format @param {Record<string, any>} [context] */
159
+ serialize(format: string, context?: Record<string, any>): void | null;
147
160
  }
148
161
  import type { Nabu } from "./nabu.svelte.js";
149
162
  import type { NabuNode } from "./nabu.svelte.js";
@@ -28,6 +28,7 @@ export class Block {
28
28
  }
29
29
  }
30
30
 
31
+ /** @type {SvelteMap<string, function(any): void>} */
31
32
  serializers = new SvelteMap();
32
33
  behaviors = new SvelteMap();
33
34
  selected = $state(false);
@@ -44,6 +45,19 @@ export class Block {
44
45
  /** @type {{from: number, to: number, direction: "forward" | "backward" | "none"} | null} */
45
46
  selection = $derived(null);
46
47
 
48
+ positions = $derived(0);
49
+
50
+ /**
51
+ * Resolve a document-local offset to { block, offset } within this block.
52
+ * Leaf blocks with text return themselves directly.
53
+ * Override in MegaBlock (pure containers) and mixed blocks (e.g. ListItem).
54
+ * @param {number} localOffset
55
+ * @returns {{ block: Block, offset: number } | null}
56
+ */
57
+ resolveOffset(localOffset) {
58
+ return { block: this, offset: localOffset };
59
+ }
60
+
47
61
  /** @type {MegaBlock | null} */
48
62
  parent = $state(null);
49
63
  index = $state(0);
@@ -334,15 +348,15 @@ export class Block {
334
348
  // -- UTILS --
335
349
 
336
350
  commit() {
337
- this.nabu.commit();
351
+ return this.nabu.commit();
338
352
  }
339
353
 
340
354
 
341
- /** @param {string} format */
342
- serialize(format) {
355
+ /** @param {string} format @param {Record<string, any>} [context] */
356
+ serialize(format, context = {}) {
343
357
  const serializer = this.serializers.get(format);
344
358
  if (serializer) {
345
- return serializer(this);
359
+ return serializer(context);
346
360
  } else {
347
361
  this.nabu.warn(`No serializer found for format "${format}" on block type "${this.type}"`);
348
362
  return null;
@@ -1,3 +1,42 @@
1
+ /**
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}
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;
1
40
  /**
2
41
  * Shared beforeinput logic for block containers (Nabu root and MegaBlock).
3
42
  *
@@ -20,5 +59,20 @@ export function handleContainerBeforeInput(container: {
20
59
  children: Block[];
21
60
  commit: () => void;
22
61
  }, nabu: Nabu, event: InputEvent): any;
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;
23
77
  import type { Block } from './block.svelte.js';
24
78
  import type { Nabu } from './nabu.svelte.js';
@@ -3,19 +3,78 @@
3
3
  * @import { Nabu } from './nabu.svelte.js';
4
4
  */
5
5
 
6
+ import { start } from 'happy-dom/lib/PropertySymbol.js';
7
+ import { MegaBlock } from './megablock.svelte.js';
8
+
9
+ /**
10
+ * Walks up from `block` toward `container` and returns the direct child of
11
+ * `container` that is (or contains) `block`.
12
+ *
13
+ * Works for both Nabu (root blocks have parent = null, loop breaks before
14
+ * reaching Nabu) and MegaBlock (loop stops when parent === container).
15
+ *
16
+ * @param {Block} block
17
+ * @param {Block | Nabu} container
18
+ * @returns {Block | null}
19
+ */
20
+ export function findDirectChildOf(block, container) {
21
+ const spine = [block];
22
+ while (spine.at(-1) && spine.at(-1) !== container) {
23
+ const current = spine.at(-1);
24
+ const parent = current?.parent;
25
+ if (!current || !parent) break;
26
+ spine.push(parent);
27
+ }
28
+ return spine.at(-1) === container ? spine.at(-2) ?? null : spine.at(-1) ?? null;
29
+ }
30
+
31
+ /**
32
+ * Finds the Lowest Common Ancestor (LCA) of two blocks in the block tree.
33
+ *
34
+ * Returns the LCA block, or null if the two blocks only share Nabu as ancestor
35
+ * (i.e. both ancestor chains terminate at a root block with parent = null).
36
+ *
37
+ * Examples:
38
+ * LCA(subA, subB) where both are children of subList → subList
39
+ * LCA(para1, para2) where both are root-level blocks → null
40
+ *
41
+ * @param {Block} blockA
42
+ * @param {Block} blockB
43
+ * @returns {Block | null}
44
+ */
45
+ export function findLCA(blockA, blockB) {
46
+ if (blockA === blockB) return blockA.parent ?? null;
47
+
48
+ // Build the ancestor set for blockA (blockA itself included)
49
+ const ancestorsA = new Set();
50
+ let curr = /** @type {Block | null} */ (blockA);
51
+ while (curr) {
52
+ ancestorsA.add(curr);
53
+ curr = curr.parent ?? null;
54
+ }
55
+
56
+ // Walk from blockB up until we hit a node in blockA's ancestor set
57
+ let currB = /** @type {Block | null} */ (blockB);
58
+ while (currB) {
59
+ if (ancestorsA.has(currB)) return currB;
60
+ currB = currB.parent ?? null;
61
+ }
62
+
63
+ return null; // only Nabu is a common ancestor
64
+ }
65
+
6
66
  /**
7
67
  * Ensures a block respects its structural parent constraint after being relocated.
8
68
  * If the block's current Loro parent doesn't match its `requiredParent` type,
9
69
  * creates a wrapper block at the same position WITHOUT committing.
10
70
  *
11
- * Uses BlockClass.create() instead of nabu.insert() to avoid premature commits.
12
- * New wrapper blocks are registered in nabu.blocksByType immediately, so the
13
- * onBeforeTransaction hook will see and merge adjacent wrappers of the same type.
71
+ * Returns the wrapper block if one was created, otherwise undefined.
14
72
  *
15
73
  * @param {Nabu} nabu
16
74
  * @param {Block} block
75
+ * @returns {Block | undefined}
17
76
  */
18
- function wrapOrphan(nabu, block) {
77
+ export function wrapOrphan(nabu, block) {
19
78
  const req = block.requiredParent;
20
79
  if (!req) return;
21
80
 
@@ -40,6 +99,7 @@ function wrapOrphan(nabu, block) {
40
99
 
41
100
  // Move block into wrapper (no commit)
42
101
  nabu.tree.move(block.node.id, wrapper.node.id);
102
+ return wrapper;
43
103
  }
44
104
 
45
105
  /**
@@ -68,6 +128,8 @@ export function handleContainerBeforeInput(container, nabu, event) {
68
128
  const focusData = startBlock.focus(undefined, true);
69
129
  const inputType = event.inputType;
70
130
 
131
+ const toDestroy = new Set();
132
+
71
133
  // Build start spine: walk from startBlock up to (but not past) container,
72
134
  // pruning next-siblings of each intermediate node along the way.
73
135
  const startSpine = [startBlock];
@@ -78,7 +140,7 @@ export function handleContainerBeforeInput(container, nabu, event) {
78
140
  startSpine.push(parent);
79
141
  if (parent !== container) {
80
142
  const nextSiblings = parent.children.slice(current.index + 1);
81
- if (nextSiblings.length) nextSiblings.forEach(block => block.destroy());
143
+ if (nextSiblings.length) nextSiblings.forEach(block => toDestroy.add(block));
82
144
  }
83
145
  }
84
146
 
@@ -92,7 +154,7 @@ export function handleContainerBeforeInput(container, nabu, event) {
92
154
  endSpine.push(parent);
93
155
  if (parent !== container) {
94
156
  const previousSiblings = parent.children.slice(0, current.index);
95
- if (previousSiblings.length) previousSiblings.forEach(block => block.destroy());
157
+ if (previousSiblings.length) previousSiblings.forEach(block => toDestroy.add(block));
96
158
  }
97
159
  }
98
160
 
@@ -104,7 +166,7 @@ export function handleContainerBeforeInput(container, nabu, event) {
104
166
 
105
167
  // Delete intermediate direct children of container between the two spines.
106
168
  const intermediates = container.children.slice(startOfStartSpine.index + 1, startOfEndSpine.index);
107
- intermediates.forEach(block => block.destroy());
169
+ intermediates.forEach(block => toDestroy.add(block));
108
170
 
109
171
  // Single-block selection: delegate directly to that block.
110
172
  if (startBlock === endBlock) {
@@ -151,7 +213,20 @@ export function handleContainerBeforeInput(container, nabu, event) {
151
213
  }
152
214
  });
153
215
 
154
- startOfEndSpine?.destroy();
216
+ if (startOfEndSpine === startOfStartSpine) {
217
+ if (startOfStartSpine instanceof MegaBlock) {
218
+ startOfStartSpine.children.filter(c => c.selected).forEach(c => toDestroy.add(c));
219
+ }
220
+ }
221
+
222
+ if (startOfEndSpine !== startOfStartSpine) {
223
+ startOfEndSpine?.destroy();
224
+ if (startOfStartSpine !== startBlock && startBlock instanceof MegaBlock) {
225
+ startBlock.children.forEach(child => child.destroy());
226
+ }
227
+ }
228
+
229
+ toDestroy.forEach(block => block.destroy());
155
230
 
156
231
  container.commit();
157
232
  startBlock.focus({
@@ -159,3 +234,85 @@ export function handleContainerBeforeInput(container, nabu, event) {
159
234
  });
160
235
  return true;
161
236
  }
237
+
238
+ /**
239
+ * Deletes the current selection content without committing.
240
+ * Handles both single-block and multi-block selections.
241
+ * Returns the resulting insertion position {block, offset}, or null on failure.
242
+ *
243
+ * @param {{ children: import('./block.svelte.js').Block[] }} container
244
+ * @param {import('./nabu.svelte.js').Nabu} nabu
245
+ * @returns {{ block: import('./block.svelte.js').Block, offset: number } | null}
246
+ */
247
+ export function deleteSelectionContent(container, nabu) {
248
+ const startBlock = /** @type {import('./block.svelte.js').Block} */ (nabu.selection.startBlock);
249
+ const endBlock = /** @type {import('./block.svelte.js').Block} */ (nabu.selection.endBlock);
250
+ if (!startBlock || !endBlock) return null;
251
+
252
+ // Single-block selection: delete the range only
253
+ if (startBlock === endBlock) {
254
+ const sel = startBlock.selection;
255
+ if (!sel || sel.from === sel.to) return { block: startBlock, offset: sel?.from ?? 0 };
256
+ const tb = startBlock.behaviors?.get('text');
257
+ if (tb) tb.delete({ from: sel.from, to: sel.to });
258
+ return { block: startBlock, offset: sel.from };
259
+ }
260
+
261
+ // Multi-block selection: same spine logic as handleContainerBeforeInput (no commit)
262
+ const startOffset = startBlock.focus(undefined, true).options.startOffset;
263
+
264
+ const startSpine = [startBlock];
265
+ while (startSpine.at(-1) && startSpine.at(-1) !== container) {
266
+ const current = startSpine.at(-1);
267
+ const parent = current?.parent;
268
+ if (!current || !parent) break;
269
+ startSpine.push(parent);
270
+ if (parent !== container) {
271
+ const nextSiblings = parent.children.slice(current.index + 1);
272
+ if (nextSiblings.length) nextSiblings.forEach(block => block.destroy());
273
+ }
274
+ }
275
+
276
+ const endSpine = [endBlock];
277
+ while (endSpine.at(-1) && endSpine.at(-1) !== container) {
278
+ const current = endSpine.at(-1);
279
+ const parent = current?.parent;
280
+ if (!current || !parent) break;
281
+ endSpine.push(parent);
282
+ if (parent !== container) {
283
+ const previousSiblings = parent.children.slice(0, current.index);
284
+ if (previousSiblings.length) previousSiblings.forEach(block => block.destroy());
285
+ }
286
+ }
287
+
288
+ const directChild = (spine) => spine.at(-1) === container ? spine.at(-2) : spine.at(-1);
289
+ const startOfStartSpine = directChild(startSpine);
290
+ const startOfEndSpine = directChild(endSpine);
291
+ if (!startOfStartSpine || !startOfEndSpine) return null;
292
+
293
+ const intermediates = container.children.slice(startOfStartSpine.index + 1, startOfEndSpine.index);
294
+ intermediates.forEach(block => block.destroy());
295
+
296
+ startBlock.delete();
297
+ endBlock.delete();
298
+ startBlock.consume(endBlock);
299
+
300
+ endSpine.forEach(block => {
301
+ if (block === container) return;
302
+ const brotherhood = block.parent?.children || [];
303
+ const index = brotherhood.indexOf(block);
304
+ if (index !== -1) {
305
+ const nextSiblings = brotherhood.slice(index + 1);
306
+ if (nextSiblings.length) {
307
+ [...nextSiblings].reverse().forEach(sibling => {
308
+ sibling.node.moveAfter(startBlock.node);
309
+ wrapOrphan(nabu, sibling);
310
+ });
311
+ }
312
+ }
313
+ });
314
+
315
+ startOfEndSpine?.destroy();
316
+
317
+ return { block: startBlock, offset: startOffset };
318
+ }
@@ -29,6 +29,7 @@ export class Dialogue extends Block {
29
29
  type: 'dialogue',
30
30
  content: this.behavior.toJSON()
31
31
  }));
32
+ this.serializers.set('application/x-nabu+json', (ctx) => this.behavior.toClipboardBlock(ctx));
32
33
  }
33
34
 
34
35
  component = $derived(this.nabu.components.get("dialogue") || DialogueComponent);
@@ -42,6 +43,7 @@ export class Dialogue extends Block {
42
43
  }
43
44
 
44
45
  selection = $derived(this.behavior.selection);
46
+ positions = $derived(this.behavior.positions);
45
47
 
46
48
  /** @param {InputEvent} event */
47
49
  beforeinput(event) {
@@ -36,11 +36,7 @@ const DialogueExtension = extension("dialogue", {
36
36
 
37
37
  const newBlock = nabu.insert("dialogue", { delta }, parentId, currentIndex + 1);
38
38
 
39
- block.commit();
40
-
41
- setTimeout(() => {
42
- nabu.selection.setCursor(newBlock, 0);
43
- }, 0);
39
+ block.commit().then(() => nabu.selection.setCursor(newBlock, 0));
44
40
 
45
41
  return { block: newBlock };
46
42
  },
@@ -6,6 +6,42 @@
6
6
  * @typedef {NabuNode<{type: "heading", text: LoroText, level: number}>} HeadingNode
7
7
  */
8
8
  export class Heading extends Block {
9
+ /** @param {import('../../utils/extensions.js').PasteBlock} pb */
10
+ static toMarkdown(pb: import("../../utils/extensions.js").PasteBlock): string;
11
+ /** @param {import('../../utils/extensions.js').PasteBlock} pb */
12
+ static toHtml(pb: import("../../utils/extensions.js").PasteBlock): string;
13
+ static markdownRules: {
14
+ priority: number;
15
+ detect: RegExp;
16
+ consume: () => number;
17
+ }[];
18
+ /**
19
+ * @param {string[]} lines
20
+ * @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[] }} helpers
21
+ * @returns {import('../../utils/extensions.js').PasteBlock | null}
22
+ */
23
+ static fromMarkdown(lines: string[], { parseInline }: {
24
+ parseInline: (text: string) => import("loro-crdt").Delta<string>[];
25
+ }): import("../../utils/extensions.js").PasteBlock | null;
26
+ static htmlRules: {
27
+ selector: string;
28
+ props: {
29
+ level: number;
30
+ };
31
+ }[];
32
+ /**
33
+ * @param {Element} el
34
+ * @param {{ parseInline: (el: Element) => import('loro-crdt').Delta<string>[] }} helpers
35
+ * @param {{ props?: { level?: number } }} [rule]
36
+ * @returns {import('../../utils/extensions.js').PasteBlock | null}
37
+ */
38
+ static fromHTML(el: Element, { parseInline }: {
39
+ parseInline: (el: Element) => import("loro-crdt").Delta<string>[];
40
+ }, rule?: {
41
+ props?: {
42
+ level?: number;
43
+ };
44
+ }): import("../../utils/extensions.js").PasteBlock | null;
9
45
  /** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
10
46
  static create(nabu: Nabu, type: string, props?: Object, parentId?: string | null, index?: number | null): Heading;
11
47
  /** @param {Nabu} nabu @param {HeadingNode} node */