@aionbuilders/nabu 0.1.0-alpha.4 → 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 (46) hide show
  1. package/dist/behaviors/text/rich-text.extension.js +96 -30
  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.d.ts +2 -0
  6. package/dist/blocks/block.extension.js +32 -0
  7. package/dist/blocks/block.svelte.d.ts +17 -4
  8. package/dist/blocks/block.svelte.js +18 -4
  9. package/dist/blocks/container.utils.d.ts +54 -0
  10. package/dist/blocks/container.utils.js +165 -8
  11. package/dist/blocks/dialogue/dialogue.svelte.js +2 -0
  12. package/dist/blocks/dialogue/index.js +1 -5
  13. package/dist/blocks/heading/heading.svelte.d.ts +36 -0
  14. package/dist/blocks/heading/heading.svelte.js +58 -2
  15. package/dist/blocks/heading/index.js +9 -5
  16. package/dist/blocks/index.d.ts +1 -0
  17. package/dist/blocks/index.js +1 -0
  18. package/dist/blocks/list/index.js +13 -1
  19. package/dist/blocks/list/list-item.svelte.d.ts +41 -0
  20. package/dist/blocks/list/list-item.svelte.js +136 -85
  21. package/dist/blocks/list/list.svelte.d.ts +53 -0
  22. package/dist/blocks/list/list.svelte.js +112 -11
  23. package/dist/blocks/megablock.svelte.d.ts +27 -0
  24. package/dist/blocks/megablock.svelte.js +103 -1
  25. package/dist/blocks/nabu.svelte.d.ts +28 -2
  26. package/dist/blocks/nabu.svelte.js +383 -10
  27. package/dist/blocks/paragraph/index.js +21 -6
  28. package/dist/blocks/paragraph/paragraph.svelte.d.ts +28 -0
  29. package/dist/blocks/paragraph/paragraph.svelte.js +53 -1
  30. package/dist/index.d.ts +1 -0
  31. package/dist/index.js +2 -1
  32. package/dist/paste/html.paste.d.ts +12 -0
  33. package/dist/paste/html.paste.js +192 -0
  34. package/dist/paste/index.d.ts +4 -0
  35. package/dist/paste/index.js +4 -0
  36. package/dist/paste/markdown.paste.d.ts +12 -0
  37. package/dist/paste/markdown.paste.js +140 -0
  38. package/dist/paste/nabu.paste.d.ts +2 -0
  39. package/dist/paste/nabu.paste.js +20 -0
  40. package/dist/paste/plain-text.paste.d.ts +8 -0
  41. package/dist/paste/plain-text.paste.js +42 -0
  42. package/dist/presets.js +6 -4
  43. package/dist/utils/extensions.d.ts +63 -0
  44. package/dist/utils/extensions.js +33 -0
  45. package/dist/utils/selection.svelte.js +0 -5
  46. package/package.json +14 -3
@@ -2,7 +2,6 @@
2
2
  * @import { Nabu } from '../../blocks/nabu.svelte.js'
3
3
  */
4
4
 
5
- import { tick } from 'svelte';
6
5
  import { Extension } from '../../utils/extensions.js';
7
6
  import { TextBehavior } from './text.behavior.svelte.js';
8
7
 
@@ -15,28 +14,11 @@ const MARK_SHORTCUTS = {
15
14
  };
16
15
 
17
16
  /**
17
+ * Collect all selected blocks with a TextBehavior and a non-collapsed selection.
18
18
  * @param {Nabu} nabu
19
- * @param {KeyboardEvent} event
19
+ * @returns {{ behavior: TextBehavior, sel: any }[]}
20
20
  */
21
- function onKeyDown(nabu, event) {
22
- const ctrl = event.ctrlKey || event.metaKey;
23
- if (!ctrl) return;
24
-
25
- const endBlock = nabu.selection.endBlock;
26
- const startBlock = nabu.selection.startBlock;
27
- const startFocusData = startBlock?.focus(null, true);
28
- const endFocusData = endBlock?.focus(null, true);
29
-
30
- let markName = null;
31
- if (!event.shiftKey) {
32
- markName = MARK_SHORTCUTS[event.key.toLowerCase()] ?? null;
33
- } else if (event.key.toLowerCase() === 'x') {
34
- markName = 'strikethrough';
35
- }
36
-
37
- if (!markName) return;
38
-
39
- // Collect all targeted block/selection pairs
21
+ function collectMarkTargets(nabu) {
40
22
  const targets = [];
41
23
  for (const [, block] of nabu.blocks) {
42
24
  if (!block.selected) continue;
@@ -46,10 +28,23 @@ function onKeyDown(nabu, event) {
46
28
  if (!sel || sel.isCollapsed) continue;
47
29
  targets.push({ behavior, sel });
48
30
  }
31
+ return targets;
32
+ }
49
33
 
50
- if (!targets.length) return;
34
+ /**
35
+ * Toggle a mark on all selected text targets.
36
+ * @param {Nabu} nabu
37
+ * @param {string} markName
38
+ */
39
+ function toggleMark(nabu, markName) {
40
+ const startBlock = nabu.selection.startBlock;
41
+ const endBlock = nabu.selection.endBlock;
42
+ const startFocusData = startBlock?.focus(null, true);
43
+ const endFocusData = endBlock?.focus(null, true);
44
+
45
+ const targets = collectMarkTargets(nabu);
46
+ if (!targets.length) return false;
51
47
 
52
- // Classic editor behavior: remove if mark is fully active across all targets, apply otherwise
53
48
  const isFullyActive = targets.every(({ behavior, sel }) => behavior.isMarkActive(markName, sel));
54
49
  for (const { behavior, sel } of targets) {
55
50
  if (isFullyActive) {
@@ -58,18 +53,89 @@ function onKeyDown(nabu, event) {
58
53
  behavior.applyMark(markName, true, sel);
59
54
  }
60
55
  }
61
- const applied = true;
62
56
 
63
- if (applied) {
64
- nabu.commit();
65
- if (!startBlock || !endBlock) return;
66
- nabu.focus({ from: { block: startBlock, offset: startFocusData?.start?.offset }, to: { block: endBlock, offset: endFocusData?.end?.offset } });
67
-
57
+ nabu.commit();
58
+ if (startBlock && endBlock) {
59
+ nabu.focus({
60
+ from: { block: startBlock, offset: startFocusData?.start?.offset ?? 0 },
61
+ to: { block: endBlock, offset: endFocusData?.end?.offset ?? 0 }
62
+ });
63
+ }
64
+ return true;
65
+ }
68
66
 
69
- return nabu.BREAK;
67
+ /**
68
+ * Apply a mark on all selected text targets.
69
+ * @param {Nabu} nabu
70
+ * @param {string} markName
71
+ * @param {any} value
72
+ */
73
+ function applyMark(nabu, markName, value = true) {
74
+ const targets = collectMarkTargets(nabu);
75
+ if (!targets.length) return;
76
+ for (const { behavior, sel } of targets) {
77
+ behavior.applyMark(markName, value, sel);
78
+ }
79
+ nabu.commit();
80
+ }
81
+
82
+ /**
83
+ * Remove a mark from all selected text targets.
84
+ * @param {Nabu} nabu
85
+ * @param {string} markName
86
+ */
87
+ function removeMark(nabu, markName) {
88
+ const targets = collectMarkTargets(nabu);
89
+ if (!targets.length) return;
90
+ for (const { behavior, sel } of targets) {
91
+ behavior.removeMark(markName, sel);
70
92
  }
93
+ nabu.commit();
94
+ }
95
+
96
+ /**
97
+ * @param {Nabu} nabu
98
+ * @param {KeyboardEvent} event
99
+ */
100
+ function onKeyDown(nabu, event) {
101
+ const ctrl = event.ctrlKey || event.metaKey;
102
+ if (!ctrl) return;
103
+
104
+ let markName = null;
105
+ if (!event.shiftKey) {
106
+ markName = MARK_SHORTCUTS[event.key.toLowerCase()] ?? null;
107
+ } else if (event.key.toLowerCase() === 'x') {
108
+ markName = 'strikethrough';
109
+ }
110
+
111
+ if (!markName) return;
112
+
113
+ event.preventDefault();
114
+ nabu.exec('mark:toggle', { mark: markName });
115
+ return nabu.BREAK;
71
116
  }
72
117
 
73
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
+ },
128
+ actions: {
129
+ // Full API
130
+ 'mark:toggle': (nabu, data) => toggleMark(nabu, data?.mark),
131
+ 'mark:apply': (nabu, data) => applyMark(nabu, data?.mark, data?.value),
132
+ 'mark:remove': (nabu, data) => removeMark(nabu, data?.mark),
133
+ // Aliases
134
+ 'bold': (nabu) => toggleMark(nabu, 'bold'),
135
+ 'italic': (nabu) => toggleMark(nabu, 'italic'),
136
+ 'underline': (nabu) => toggleMark(nabu, 'underline'),
137
+ 'code': (nabu) => toggleMark(nabu, 'code'),
138
+ 'strikethrough': (nabu) => toggleMark(nabu, 'strikethrough'),
139
+ },
74
140
  hooks: { onKeyDown }
75
141
  });
@@ -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} />
@@ -0,0 +1,2 @@
1
+ export const BlockExtension: Extension;
2
+ import { Extension } from '../utils/extensions.js';
@@ -0,0 +1,32 @@
1
+ /**
2
+ * @import { Nabu } from './nabu.svelte.js'
3
+ */
4
+
5
+ import { tick } from 'svelte';
6
+ import { Extension } from '../utils/extensions.js';
7
+
8
+ /**
9
+ * Transform the anchor block to a new type, then restore cursor position.
10
+ * @param {Nabu} nabu
11
+ * @param {string} type
12
+ * @param {Object} [props={}]
13
+ */
14
+ function transformBlock(nabu, type, props = {}) {
15
+ console.log(`Transforming block to type "${type}" with props`, props);
16
+ const block = nabu.selection.anchorBlock;
17
+ if (!block) return;
18
+ const blockId = block.id;
19
+ const offset = block.behaviors?.get('text')?.selection?.from ?? 0;
20
+ block.transformTo(type, props);
21
+ // After transformTo, updateRoots() replaces the block instance — fetch the new one.
22
+ tick().then(() => {
23
+ const newBlock = nabu.blocks.get(blockId);
24
+ if (newBlock) nabu.focus({ from: { block: newBlock, offset }, to: { block: newBlock, offset } });
25
+ });
26
+ }
27
+
28
+ export const BlockExtension = new Extension('block', {
29
+ actions: {
30
+ 'block:transform': (nabu, data) => transformBlock(nabu, data?.type, data?.props ?? {}),
31
+ }
32
+ });
@@ -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';