@aionbuilders/nabu 0.1.0-alpha.1 → 0.1.0-alpha.11

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 +15 -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 +2 -0
  77. package/dist/index.js +3 -1
  78. package/dist/nabu-theme.css +92 -0
  79. package/dist/overlays/context/Context.svelte +182 -0
  80. package/dist/overlays/context/Context.svelte.d.ts +17 -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 +82 -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 +1 -0
  104. package/dist/presets.js +18 -4
  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
package/README.md CHANGED
@@ -66,6 +66,7 @@ Without this setup, Nabu will fail to load.
66
66
  HeadingExtension,
67
67
  ListExtension,
68
68
  ListItemExtension,
69
+ DialogueExtension,
69
70
  RichTextExtension
70
71
  } from '@aionbuilders/nabu';
71
72
 
@@ -75,6 +76,7 @@ Without this setup, Nabu will fail to load.
75
76
  HeadingExtension,
76
77
  ListExtension,
77
78
  ListItemExtension,
79
+ DialogueExtension,
78
80
  RichTextExtension
79
81
  ]
80
82
  });
@@ -105,7 +107,7 @@ const engine = createEditor({
105
107
  | `MinimalPreset` | Paragraph only |
106
108
  | `TextPreset` | Paragraph + rich text marks |
107
109
  | `DocumentPreset` | Paragraph + Headings + rich text |
108
- | `FullPreset` | All built-in blocks + rich text |
110
+ | `FullPreset` | All built-in blocks + rich text (including Dialogue) |
109
111
 
110
112
  ### Factory functions
111
113
 
@@ -136,8 +138,17 @@ All block types and behaviors are provided as extensions. Pass them to the `Nabu
136
138
  | `HeadingExtension` | `heading` block (h1–h6) | `# ` → h1, `## ` → h2, ... |
137
139
  | `ListExtension` | `list` container (bullet/ordered) | — |
138
140
  | `ListItemExtension` | `list-item` block | `Tab` indent, `Shift+Tab` unindent |
141
+ | `DialogueExtension` | `dialogue` block | `-- ` → dialogue, `Backspace` at start → paragraph |
139
142
  | `RichTextExtension` | Inline marks (bold, italic...) | `Ctrl+B/I/U/E`, `Ctrl+Shift+X` |
140
143
 
144
+ ### Typographic substitutions
145
+
146
+ When `RichTextExtension` or any text-bearing block is active, Nabu automatically applies typographic substitutions as you type:
147
+
148
+ | Input | Output | Description |
149
+ |---|---|---|
150
+ | `--` + space | `— ` | Em-dash (French dialogue convention) |
151
+
141
152
  ## Programmatic API
142
153
 
143
154
  ```js
@@ -145,6 +156,7 @@ All block types and behaviors are provided as extensions. Pass them to the `Nabu
145
156
  engine.insert('paragraph', { text: 'Hello' });
146
157
  engine.insert('heading', { level: 1, text: 'Title' });
147
158
  engine.insert('list', { listType: 'bullet' });
159
+ engine.insert('dialogue', { text: 'Tu viens ce soir ?' });
148
160
 
149
161
  // Undo / Redo
150
162
  engine.undo();
@@ -152,6 +164,8 @@ engine.redo();
152
164
 
153
165
  // Serialize
154
166
  const markdown = engine.serialize('markdown');
167
+ // dialogue blocks serialize as: — Tu viens ce soir ?
168
+
155
169
  const json = engine.serialize('json');
156
170
  ```
157
171
 
@@ -1,3 +1,3 @@
1
- export * from "./text.behavior.svelte";
1
+ export * from "./text.behavior.svelte.js";
2
2
  export { RichTextExtension } from "./rich-text.extension.js";
3
3
  export { default as RichText } from "./RichText.svelte";
@@ -1,3 +1,3 @@
1
- export * from './text.behavior.svelte';
1
+ export * from './text.behavior.svelte.js';
2
2
  export { RichTextExtension } from './rich-text.extension.js';
3
3
  export { default as RichText } from './RichText.svelte';
@@ -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
@@ -120,7 +154,7 @@ export class TextBehavior {
120
154
  case "insertParagraph":
121
155
  return this.handleInsertParagraph(event, selection);
122
156
  default:
123
- console.warn("Unhandled input type:", event.inputType);
157
+ this.nabu.warn("Unhandled input type:", event.inputType);
124
158
  return;
125
159
  }
126
160
  }
@@ -129,7 +163,20 @@ export class TextBehavior {
129
163
  handleInsertText(event, selection = this.selection) {
130
164
  if (!selection) return;
131
165
 
132
- const textToInsert = event.data || "";
166
+ let textToInsert = event.data || "";
167
+
168
+ // Substitution typographique : `-- ` → `— ` (em-dash)
169
+ if (textToInsert === " " && selection.isCollapsed) {
170
+ const textBeforeCursor = this.text.slice(0, selection.from);
171
+ if (textBeforeCursor.endsWith("--")) {
172
+ this.delete({ index: selection.from - 2, length: 2 }, selection);
173
+ this.insert(selection.from - 2, "— ");
174
+ this.block.commit();
175
+ tick().then(() => this.nabu.selection.setCursor(this.block, selection.from - 2 + 2));
176
+ return;
177
+ }
178
+ }
179
+
133
180
  if (!selection.isCollapsed) this.delete({index: selection.from, length: selection.to - selection.from}, selection);
134
181
  this.insert(selection.from, textToInsert);
135
182
  this.block.commit();
@@ -331,6 +378,21 @@ export class TextBehavior {
331
378
  });
332
379
  }
333
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
+
334
396
  /** @param {Block} other */
335
397
  absorbs(other) {
336
398
  const otherBehavior = other.behaviors.get("text");
@@ -1,4 +1,6 @@
1
1
  <script>
2
+ import BlockGap from './BlockGap.svelte';
3
+
2
4
  /** @type {{ block: import('./block.svelte.js').Block }} */
3
5
  let { block } = $props();
4
6
 
@@ -8,11 +10,9 @@
8
10
  </script>
9
11
 
10
12
  {#if Component}
11
- <!-- On passe l'instance du bloc au composant spécifique -->
12
13
  <Component {block} />
13
14
  {:else}
14
- <!-- Fallback pour les types inconnus (utile en dev) -->
15
15
  <div style="color: red; border: 1px dashed red; padding: 0.5rem; margin: 0.5rem 0;" contenteditable="false">
16
16
  ⚠️ Unknown block type: <strong>{block.type}</strong>
17
17
  </div>
18
- {/if}
18
+ {/if}<BlockGap {block} />
@@ -0,0 +1,55 @@
1
+ <script>
2
+ /** @type {{ block: import('./block.svelte.js').Block }} */
3
+ let { block } = $props();
4
+
5
+ function onclick() {
6
+ const parentId = block.parent?.node.id.toString() ?? null;
7
+ const insertIndex = block.index + 1;
8
+ block.nabu.exec('gap:click', { parentId, insertIndex });
9
+ }
10
+ </script>
11
+
12
+ <!-- svelte-ignore a11y_click_events_have_key_events a11y_no_static_element_interactions -->
13
+ <div
14
+ class="nabu-block-gap"
15
+ contenteditable="false"
16
+ data-nabu-inert
17
+ aria-hidden="true"
18
+ {onclick}
19
+ >
20
+ <div class="nabu-block-gap-line"></div>
21
+ </div>
22
+
23
+ <style>
24
+ .nabu-block-gap {
25
+ position: relative;
26
+ height: 0.75rem;
27
+ cursor: pointer;
28
+ user-select: none;
29
+ }
30
+
31
+ .nabu-block-gap-line {
32
+ position: absolute;
33
+ inset: 50% 0 auto 0;
34
+ transform: translateY(-50%);
35
+ height: 1px;
36
+ background: color-mix(in srgb, currentColor 20%, transparent);
37
+ display: flex;
38
+ align-items: center;
39
+ justify-content: center;
40
+ opacity: 0;
41
+ transition: opacity 0.15s;
42
+ }
43
+
44
+ .nabu-block-gap:hover .nabu-block-gap-line {
45
+ opacity: 1;
46
+ }
47
+
48
+ .nabu-block-gap-plus {
49
+ font-size: 0.6rem;
50
+ line-height: 1;
51
+ background: white;
52
+ padding: 0 0.25rem;
53
+ color: #aaa;
54
+ }
55
+ </style>
@@ -0,0 +1,11 @@
1
+ export default BlockGap;
2
+ type BlockGap = {
3
+ $on?(type: string, callback: (e: any) => void): () => void;
4
+ $set?(props: Partial<$$ComponentProps>): void;
5
+ };
6
+ declare const BlockGap: import("svelte").Component<{
7
+ block: import("./block.svelte.js").Block;
8
+ }, {}, "">;
9
+ type $$ComponentProps = {
10
+ block: import("./block.svelte.js").Block;
11
+ };
@@ -4,6 +4,11 @@
4
4
 
5
5
  /** @type {{ engine: Nabu }} */
6
6
  let { engine = new Nabu() } = $props();
7
+
8
+ $effect(() => {
9
+ engine.systems.forEach(system => system.start?.());
10
+ return () => engine.systems.forEach(system => system.stop?.());
11
+ });
7
12
  </script>
8
13
 
9
14
  <div
@@ -14,6 +19,9 @@
14
19
  data-nabu-root="true"
15
20
  onbeforeinput={(e) => engine.handleBeforeinput(e)}
16
21
  onkeydown={(e) => engine.handleKeydown(e)}
22
+ oncopy={(e) => engine.handleCopy(e)}
23
+ oncut={(e) => engine.handleCut(e)}
24
+ onpaste={(e) => engine.handlePaste(e)}
17
25
  >
18
26
  {#each engine.children as block (block.id)}
19
27
  <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,8 +11,18 @@ 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>;
17
+ /** @type {SvelteMap<string, Variable<this>>} */
18
+ variables: SvelteMap<string, Variable<this>>;
19
+ /**
20
+ * Declare a variable on this block, which can be used in serializers and behaviors.
21
+ * @param {string} name
22
+ * @param {VariableInit<this>} init
23
+ * @returns {Variable<this>}
24
+ */
25
+ var(name: string, init: VariableInit<this>): Variable<this>;
16
26
  selected: boolean;
17
27
  isSelectionStart: boolean;
18
28
  isSelectionEnd: boolean;
@@ -24,6 +34,18 @@ export class Block {
24
34
  to: number;
25
35
  direction: "forward" | "backward" | "none";
26
36
  } | null;
37
+ positions: number;
38
+ /**
39
+ * Resolve a document-local offset to { block, offset } within this block.
40
+ * Leaf blocks with text return themselves directly.
41
+ * Override in MegaBlock (pure containers) and mixed blocks (e.g. ListItem).
42
+ * @param {number} localOffset
43
+ * @returns {{ block: Block, offset: number } | null}
44
+ */
45
+ resolveOffset(localOffset: number): {
46
+ block: Block;
47
+ offset: number;
48
+ } | null;
27
49
  index: number;
28
50
  /** @type {Block?} */
29
51
  previous: Block | null;
@@ -88,8 +110,16 @@ export class Block {
88
110
  * @returns {Block} The surviving block
89
111
  */
90
112
  consume(otherBlock: Block, direction?: "into" | "from"): Block;
91
- /** @param {Block[]} children @param {number | null} [index] */
92
- adoptChildren(children: Block[], index?: number | null): void;
113
+ /**
114
+ * Declares a required parent type for structural integrity.
115
+ * Override in subclasses (e.g. ListItem requires a "list" parent).
116
+ * Used by wrapOrphan() after block relocations.
117
+ * @returns {{ type: string, props: () => Record<string, any> } | null}
118
+ */
119
+ get requiredParent(): {
120
+ type: string;
121
+ props: () => Record<string, any>;
122
+ } | null;
93
123
  /** @param {{from?: number, to?: number, index?: number, length?: number, offset?: number}} options @returns {{block: Block} | null} */
94
124
  split(options: {
95
125
  from?: number;
@@ -133,11 +163,13 @@ export class Block {
133
163
  beforeinput(event: InputEvent): any;
134
164
  /** @param {KeyboardEvent} event @returns {any} */
135
165
  keydown(event: KeyboardEvent): any;
136
- commit(): void;
137
- /** @param {string} format */
138
- serialize(format: string): any;
166
+ commit(): Promise<void>;
167
+ /** @param {string} format @param {Record<string, any>} [context] */
168
+ serialize(format: string, context?: Record<string, any>): void | null;
139
169
  }
140
- import type { Nabu } from "./nabu.svelte";
141
- import type { NabuNode } from "./nabu.svelte";
142
- import type { MegaBlock } from "./megablock.svelte";
170
+ import type { Nabu } from "./nabu.svelte.js";
171
+ import type { NabuNode } from "./nabu.svelte.js";
172
+ import type { MegaBlock } from "./megablock.svelte.js";
143
173
  import { SvelteMap } from "svelte/reactivity";
174
+ import { Variable } from "./variable.svelte.js";
175
+ import type { VariableInit } from "./variable.svelte.js";