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

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 +164 -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,77 @@
3
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
+
6
65
  /**
7
66
  * Ensures a block respects its structural parent constraint after being relocated.
8
67
  * If the block's current Loro parent doesn't match its `requiredParent` type,
9
68
  * creates a wrapper block at the same position WITHOUT committing.
10
69
  *
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.
70
+ * Returns the wrapper block if one was created, otherwise undefined.
14
71
  *
15
72
  * @param {Nabu} nabu
16
73
  * @param {Block} block
74
+ * @returns {Block | undefined}
17
75
  */
18
- function wrapOrphan(nabu, block) {
76
+ export function wrapOrphan(nabu, block) {
19
77
  const req = block.requiredParent;
20
78
  if (!req) return;
21
79
 
@@ -40,6 +98,7 @@ function wrapOrphan(nabu, block) {
40
98
 
41
99
  // Move block into wrapper (no commit)
42
100
  nabu.tree.move(block.node.id, wrapper.node.id);
101
+ return wrapper;
43
102
  }
44
103
 
45
104
  /**
@@ -68,6 +127,8 @@ export function handleContainerBeforeInput(container, nabu, event) {
68
127
  const focusData = startBlock.focus(undefined, true);
69
128
  const inputType = event.inputType;
70
129
 
130
+ const toDestroy = new Set();
131
+
71
132
  // Build start spine: walk from startBlock up to (but not past) container,
72
133
  // pruning next-siblings of each intermediate node along the way.
73
134
  const startSpine = [startBlock];
@@ -78,7 +139,7 @@ export function handleContainerBeforeInput(container, nabu, event) {
78
139
  startSpine.push(parent);
79
140
  if (parent !== container) {
80
141
  const nextSiblings = parent.children.slice(current.index + 1);
81
- if (nextSiblings.length) nextSiblings.forEach(block => block.destroy());
142
+ if (nextSiblings.length) nextSiblings.forEach(block => toDestroy.add(block));
82
143
  }
83
144
  }
84
145
 
@@ -92,7 +153,7 @@ export function handleContainerBeforeInput(container, nabu, event) {
92
153
  endSpine.push(parent);
93
154
  if (parent !== container) {
94
155
  const previousSiblings = parent.children.slice(0, current.index);
95
- if (previousSiblings.length) previousSiblings.forEach(block => block.destroy());
156
+ if (previousSiblings.length) previousSiblings.forEach(block => toDestroy.add(block));
96
157
  }
97
158
  }
98
159
 
@@ -104,7 +165,7 @@ export function handleContainerBeforeInput(container, nabu, event) {
104
165
 
105
166
  // Delete intermediate direct children of container between the two spines.
106
167
  const intermediates = container.children.slice(startOfStartSpine.index + 1, startOfEndSpine.index);
107
- intermediates.forEach(block => block.destroy());
168
+ intermediates.forEach(block => toDestroy.add(block));
108
169
 
109
170
  // Single-block selection: delegate directly to that block.
110
171
  if (startBlock === endBlock) {
@@ -151,7 +212,20 @@ export function handleContainerBeforeInput(container, nabu, event) {
151
212
  }
152
213
  });
153
214
 
154
- 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());
155
229
 
156
230
  container.commit();
157
231
  startBlock.focus({
@@ -159,3 +233,85 @@ export function handleContainerBeforeInput(container, nabu, event) {
159
233
  });
160
234
  return true;
161
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
+ }
@@ -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 */