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

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 (41) hide show
  1. package/README.md +15 -1
  2. package/dist/behaviors/text/rich-text.extension.js +87 -30
  3. package/dist/behaviors/text/text.behavior.svelte.js +14 -1
  4. package/dist/blocks/block.extension.d.ts +2 -0
  5. package/dist/blocks/block.extension.js +31 -0
  6. package/dist/blocks/block.svelte.d.ts +10 -2
  7. package/dist/blocks/block.svelte.js +12 -6
  8. package/dist/blocks/container.utils.d.ts +0 -4
  9. package/dist/blocks/container.utils.js +48 -1
  10. package/dist/blocks/dialogue/Dialogue.svelte +46 -0
  11. package/dist/blocks/dialogue/Dialogue.svelte.d.ts +11 -0
  12. package/dist/blocks/dialogue/dialogue.svelte.d.ts +41 -0
  13. package/dist/blocks/dialogue/dialogue.svelte.js +85 -0
  14. package/dist/blocks/dialogue/hooks/onBeforeInput.hook.d.ts +3 -0
  15. package/dist/blocks/dialogue/hooks/onBeforeInput.hook.js +52 -0
  16. package/dist/blocks/dialogue/index.d.ts +7 -0
  17. package/dist/blocks/dialogue/index.js +51 -0
  18. package/dist/blocks/heading/index.js +8 -0
  19. package/dist/blocks/index.d.ts +2 -0
  20. package/dist/blocks/index.js +2 -0
  21. package/dist/blocks/list/hooks/onBeforeInput.hook.d.ts +3 -0
  22. package/dist/blocks/list/hooks/onBeforeInput.hook.js +77 -0
  23. package/dist/blocks/list/index.js +15 -1
  24. package/dist/blocks/list/list-item.svelte.d.ts +24 -0
  25. package/dist/blocks/list/list-item.svelte.js +88 -83
  26. package/dist/blocks/list/list.svelte.js +1 -1
  27. package/dist/blocks/megablock.svelte.d.ts +1 -3
  28. package/dist/blocks/megablock.svelte.js +8 -14
  29. package/dist/blocks/nabu.svelte.d.ts +8 -0
  30. package/dist/blocks/nabu.svelte.js +25 -1
  31. package/dist/blocks/paragraph/index.js +3 -0
  32. package/dist/blocks/selection.svelte.js +6 -1
  33. package/dist/persistence/indexeddb.d.ts +19 -0
  34. package/dist/persistence/indexeddb.js +71 -0
  35. package/dist/persistence/persistence.d.ts +48 -0
  36. package/dist/persistence/persistence.js +80 -0
  37. package/dist/presets.d.ts +1 -0
  38. package/dist/presets.js +8 -1
  39. package/dist/utils/extensions.d.ts +4 -0
  40. package/dist/utils/extensions.js +3 -0
  41. package/package.json +2 -1
@@ -0,0 +1,51 @@
1
+ import { tick } from "svelte";
2
+ import { extension } from "../../utils/extensions";
3
+ import { Dialogue } from "./dialogue.svelte.js";
4
+ import DialogueComponent from "./Dialogue.svelte";
5
+ import { onBeforeInput } from "./hooks/onBeforeInput.hook.js";
6
+
7
+ /**
8
+ * @import { Nabu } from "../nabu.svelte.js";
9
+ */
10
+
11
+ const DialogueExtension = extension("dialogue", {
12
+ block: Dialogue,
13
+ component: DialogueComponent,
14
+ hooks: {
15
+ /** @param {Nabu} nabu @param {Dialogue} block @param {Event} event @param {{offset: number, delta: import('loro-crdt').Delta<string>}} data */
16
+ onSplit: (nabu, block, event, data) => {
17
+ const { offset, delta } = data;
18
+
19
+ // Bloc dialogue vide + Entrée → sortir du mode dialogue
20
+ if (block.text.length === 0) {
21
+ const blockId = block.node.id.toString();
22
+ block.transformTo("paragraph");
23
+ tick().then(() => {
24
+ const b = nabu.blocks.get(blockId);
25
+ if (b) b.focus({ start: 0, end: 0 });
26
+ });
27
+ return { block };
28
+ }
29
+
30
+ // Bloc non vide + Entrée → créer un nouveau bloc dialogue après
31
+ block.delete({ from: offset, to: -1 });
32
+
33
+ const currentIndex = block.node.index();
34
+ const parent = block.node.parent();
35
+ const parentId = parent?.id.toString() || null;
36
+
37
+ const newBlock = nabu.insert("dialogue", { delta }, parentId, currentIndex + 1);
38
+
39
+ block.commit();
40
+
41
+ setTimeout(() => {
42
+ nabu.selection.setCursor(newBlock, 0);
43
+ }, 0);
44
+
45
+ return { block: newBlock };
46
+ },
47
+ onBeforeInput,
48
+ }
49
+ });
50
+
51
+ export { DialogueExtension, DialogueComponent, Dialogue };
@@ -12,6 +12,14 @@ import { onBeforeInput } from "./hooks/onBeforeInput.hook";
12
12
  const HeadingExtension = extension("heading", {
13
13
  block: Heading,
14
14
  component: HeadingComponent,
15
+ actions: {
16
+ 'heading:1': (nabu) => nabu.exec('block:transform', { type: 'heading', props: { level: 1 } }),
17
+ 'heading:2': (nabu) => nabu.exec('block:transform', { type: 'heading', props: { level: 2 } }),
18
+ 'heading:3': (nabu) => nabu.exec('block:transform', { type: 'heading', props: { level: 3 } }),
19
+ 'heading:4': (nabu) => nabu.exec('block:transform', { type: 'heading', props: { level: 4 } }),
20
+ 'heading:5': (nabu) => nabu.exec('block:transform', { type: 'heading', props: { level: 5 } }),
21
+ 'heading:6': (nabu) => nabu.exec('block:transform', { type: 'heading', props: { level: 6 } }),
22
+ },
15
23
  hooks: {
16
24
  /** @param {Nabu} nabu @param {Heading} block @param {Event} event @param {{offset: number, delta: import('loro-crdt').Delta<string>}} data */
17
25
  onSplit: (nabu, block, event, data) => {
@@ -1,10 +1,12 @@
1
1
  export * from "./block.svelte.js";
2
+ export * from "./block.extension.js";
2
3
  export * from "./nabu.svelte.js";
3
4
  export * from "./megablock.svelte.js";
4
5
  export * from "./container.utils";
5
6
  export * from "./paragraph";
6
7
  export * from "./heading";
7
8
  export * from "./list";
9
+ export * from "./dialogue";
8
10
  export * from "./selection.svelte.js";
9
11
  export { default as NabuEditor } from "./Nabu.svelte";
10
12
  export { default as BlockComponent } from "./Block.svelte";
@@ -1,4 +1,5 @@
1
1
  export * from "./block.svelte.js";
2
+ export * from "./block.extension.js";
2
3
  export * from "./nabu.svelte.js";
3
4
  export * from "./megablock.svelte.js";
4
5
  export { default as NabuEditor } from "./Nabu.svelte";
@@ -8,5 +9,6 @@ export * from "./container.utils";
8
9
  export * from "./paragraph";
9
10
  export * from "./heading";
10
11
  export * from "./list";
12
+ export * from "./dialogue";
11
13
 
12
14
  export * from "./selection.svelte.js";
@@ -0,0 +1,3 @@
1
+ export function onBeforeInput(nabu: Nabu, event: InputEvent, block: Block): symbol;
2
+ import type { Nabu } from "../..";
3
+ import type { Block } from "../..";
@@ -0,0 +1,77 @@
1
+ import { tick } from "svelte";
2
+ import { TextBehavior } from "../../../behaviors/text";
3
+ import { List } from "../list.svelte.js";
4
+ import { ListItem } from "../list-item.svelte.js";
5
+
6
+ /**
7
+ * @import { Nabu, Block } from "../..";
8
+ */
9
+
10
+ /**
11
+ * Markdown shortcut: typing "- " or "1. " at the start of an empty/new paragraph
12
+ * converts it into a list item inside a new bullet/ordered list.
13
+ *
14
+ * @param {Nabu} nabu
15
+ * @param {InputEvent} event
16
+ * @param {Block} block
17
+ */
18
+ export const onBeforeInput = (nabu, event, block) => {
19
+ const { BREAK, CONTINUE } = nabu;
20
+
21
+ if (event.data !== " ") return CONTINUE;
22
+ if (block.type !== "paragraph") return CONTINUE;
23
+
24
+ const behavior = block.behaviors.get("text");
25
+ if (!behavior || !(behavior instanceof TextBehavior)) return CONTINUE;
26
+
27
+ const sel = behavior.selection;
28
+ if (!sel || !sel.isCollapsed) return CONTINUE;
29
+
30
+ const textBeforeCursor = behavior.text.slice(0, sel.from);
31
+
32
+ /** @type {"bullet" | "ordered" | null} */
33
+ let listType = null;
34
+ let prefixLength = 0;
35
+
36
+ if (textBeforeCursor.match(/^-$/)) {
37
+ listType = "bullet";
38
+ prefixLength = 1;
39
+ } else if (textBeforeCursor.match(/^\d+\.$/)) {
40
+ listType = "ordered";
41
+ prefixLength = textBeforeCursor.length;
42
+ }
43
+
44
+ if (!listType) return CONTINUE;
45
+
46
+ // Delete the prefix ("- " trigger: delete the "-"; "1. " trigger: delete the "1.")
47
+ behavior.delete({ index: 0, length: prefixLength });
48
+
49
+ // Capture remaining text delta (everything after the prefix)
50
+ const delta = behavior.container.toDelta();
51
+
52
+ // Get current tree position from live Loro state (pre-commit)
53
+ const parentNode = block.node.parent();
54
+ const parentId = parentNode?.id.toString() ?? null;
55
+ const index = block.node.index();
56
+
57
+ // Create the List at the same position as the paragraph
58
+ const list = List.create(nabu, "list", { listType }, parentId, index);
59
+
60
+ // Create the ListItem inside the list with the captured content
61
+ const listItem = ListItem.create(nabu, "list-item", { delta }, list.node.id.toString());
62
+
63
+ // Destroy the original paragraph (no commit)
64
+ block.destroy();
65
+
66
+ // Commit the whole operation as a single transaction
67
+ nabu.commit();
68
+
69
+ // Restore focus to the new list-item
70
+ const listItemId = listItem.node.id.toString();
71
+ tick().then(() => {
72
+ const updatedListItem = nabu.blocks.get(listItemId);
73
+ if (updatedListItem) updatedListItem.focus({ start: 0, end: 0 });
74
+ });
75
+
76
+ return BREAK;
77
+ };
@@ -1,9 +1,10 @@
1
1
  import { extension } from "../../utils/extensions";
2
2
  import { List } from "./list.svelte.js";
3
3
  import ListComponent from "./List.svelte";
4
- import { ListItem } from "./list-item.svelte.js";
4
+ import { ListItem, indentListItem, unindentListItem } from "./list-item.svelte.js";
5
5
  import ListItemComponent from "./ListItem.svelte";
6
6
  import { ListBehavior } from "./list.behavior.svelte.js";
7
+ import { onBeforeInput } from "./hooks/onBeforeInput.hook.js";
7
8
 
8
9
  /**
9
10
  * @import {SvelteSet} from "svelte/reactivity";
@@ -13,6 +14,7 @@ const ListExtension = extension("list", {
13
14
  block: List,
14
15
  component: ListComponent,
15
16
  hooks: {
17
+ onBeforeInput,
16
18
  onBeforeTransaction(nabu) {
17
19
  const lists = /** @type {SvelteSet<List>} */ (nabu.blocksByType.get("list"));
18
20
  lists?.forEach(list => {
@@ -36,6 +38,18 @@ const ListExtension = extension("list", {
36
38
  const ListItemExtension = extension("list-item", {
37
39
  block: ListItem,
38
40
  component: ListItemComponent,
41
+ actions: {
42
+ 'list:indent': (nabu) => {
43
+ const block = nabu.selection.anchorBlock;
44
+ if (block?.type !== 'list-item') return;
45
+ indentListItem(/** @type {ListItem} */ (block));
46
+ },
47
+ 'list:unindent': (nabu) => {
48
+ const block = nabu.selection.anchorBlock;
49
+ if (block?.type !== 'list-item') return;
50
+ unindentListItem(/** @type {ListItem} */ (block));
51
+ },
52
+ }
39
53
  });
40
54
 
41
55
  export { ListExtension, ListItemExtension, List, ListItem, ListComponent, ListItemComponent };
@@ -4,6 +4,18 @@
4
4
  /**
5
5
  * @typedef {NabuNode<{type: "list-item", text: LoroText}>} ListItemNode
6
6
  */
7
+ /**
8
+ * Indent a list item (Tab behavior): move into previous sibling's sublist, hoist own children.
9
+ * @param {ListItem} item
10
+ * @returns {boolean}
11
+ */
12
+ export function indentListItem(item: ListItem): boolean;
13
+ /**
14
+ * Unindent a list item (Shift+Tab behavior): move out of parent sublist, carry followers.
15
+ * @param {ListItem} item
16
+ * @returns {boolean}
17
+ */
18
+ export function unindentListItem(item: ListItem): boolean;
7
19
  export class ListItem extends MegaBlock {
8
20
  /** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
9
21
  static create(nabu: Nabu, type: string, props?: Object, parentId?: string | null, index?: number | null): ListItem;
@@ -24,6 +36,18 @@ export class ListItem extends MegaBlock {
24
36
  direction: "forward" | "backward" | "none";
25
37
  } | null;
26
38
  sublist: Block | undefined;
39
+ /**
40
+ * A ListItem must always live inside a List.
41
+ * wrapOrphan() uses this to auto-create a List wrapper when a ListItem
42
+ * is relocated to a context that doesn't have a List parent.
43
+ * props() reads this.parent (stale Svelte state) which still points to the
44
+ * original List at relocation time, giving us the correct listType.
45
+ * @returns {{ type: string, props: () => Record<string, any> }}
46
+ */
47
+ get requiredParent(): {
48
+ type: string;
49
+ props: () => Record<string, any>;
50
+ };
27
51
  /**
28
52
  * Retourne la sous-liste existante, ou en crée une nouvelle à la fin des enfants.
29
53
  * @param {"bullet" | "ordered"} [listType="bullet"]
@@ -11,6 +11,73 @@ import { TextBehavior } from "../../behaviors/text";
11
11
  * @typedef {NabuNode<{type: "list-item", text: LoroText}>} ListItemNode
12
12
  */
13
13
 
14
+ /**
15
+ * Indent a list item (Tab behavior): move into previous sibling's sublist, hoist own children.
16
+ * @param {ListItem} item
17
+ * @returns {boolean}
18
+ */
19
+ export function indentListItem(item) {
20
+ const currentIndex = item.node.index();
21
+ if (currentIndex === 0) return false;
22
+
23
+ const parentList = item.node.parent();
24
+ if (!parentList) return false;
25
+
26
+ const previousNode = parentList.children()?.[currentIndex - 1];
27
+ if (!previousNode) return false;
28
+ const previousItem = item.nabu.blocks.get(previousNode.id.toString());
29
+ if (!previousItem || previousItem.type !== 'list-item') return false;
30
+
31
+ // @ts-ignore
32
+ const targetSublist = previousItem.getOrCreateSublist();
33
+ const targetSublistId = targetSublist.node.id.toString();
34
+
35
+ item.nabu.tree.move(item.node.id.toString(), targetSublistId);
36
+
37
+ if (item.sublist) {
38
+ for (const childNode of item.sublist.node.children()) {
39
+ item.nabu.tree.move(childNode.id.toString(), targetSublistId);
40
+ }
41
+ }
42
+
43
+ item.commit();
44
+ setTimeout(() => item.behavior.block.focus(), 0);
45
+ return true;
46
+ }
47
+
48
+ /**
49
+ * Unindent a list item (Shift+Tab behavior): move out of parent sublist, carry followers.
50
+ * @param {ListItem} item
51
+ * @returns {boolean}
52
+ */
53
+ export function unindentListItem(item) {
54
+ const parentListNode = item.node.parent();
55
+ if (!parentListNode) return false;
56
+
57
+ const grandParentItemNode = parentListNode.parent();
58
+ if (!grandParentItemNode || grandParentItemNode.data.get('type') !== 'list-item') return false;
59
+
60
+ const myIndex = item.node.index();
61
+ const followers = parentListNode.children().slice(myIndex + 1);
62
+
63
+ if (followers.length > 0) {
64
+ const mySublist = item.getOrCreateSublist();
65
+ for (const follower of followers) {
66
+ item.nabu.tree.move(follower.id.toString(), mySublist.node.id.toString());
67
+ }
68
+ }
69
+
70
+ const targetParentListNode = grandParentItemNode.parent();
71
+ if (!targetParentListNode) return false;
72
+
73
+ const targetIndex = grandParentItemNode.index() + 1;
74
+ item.nabu.tree.move(item.node.id.toString(), targetParentListNode.id.toString(), targetIndex);
75
+
76
+ item.commit();
77
+ setTimeout(() => item.behavior.block.focus(), 0);
78
+ return true;
79
+ }
80
+
14
81
  export class ListItem extends MegaBlock {
15
82
  /** @param {Nabu} nabu @param {ListItemNode} node */
16
83
  constructor(nabu, node) {
@@ -62,6 +129,23 @@ export class ListItem extends MegaBlock {
62
129
 
63
130
  sublist = $derived(this.children.find(child => child.node.data.get("type") === "list"));
64
131
 
132
+ /**
133
+ * A ListItem must always live inside a List.
134
+ * wrapOrphan() uses this to auto-create a List wrapper when a ListItem
135
+ * is relocated to a context that doesn't have a List parent.
136
+ * props() reads this.parent (stale Svelte state) which still points to the
137
+ * original List at relocation time, giving us the correct listType.
138
+ * @returns {{ type: string, props: () => Record<string, any> }}
139
+ */
140
+ get requiredParent() {
141
+ return {
142
+ type: "list",
143
+ props: () => ({
144
+ listType: this.parent?.behaviors?.get("list")?.listType || "bullet"
145
+ })
146
+ };
147
+ }
148
+
65
149
  /**
66
150
  * Retourne la sous-liste existante, ou en crée une nouvelle à la fin des enfants.
67
151
  * @param {"bullet" | "ordered"} [listType="bullet"]
@@ -76,94 +160,15 @@ export class ListItem extends MegaBlock {
76
160
 
77
161
  /** @param {KeyboardEvent} event */
78
162
  keydown(event) {
79
- if (event.key === "Tab") {
80
- event.preventDefault(); // On bloque le comportement natif
81
-
163
+ if (event.key === 'Tab') {
164
+ event.preventDefault();
82
165
  if (event.shiftKey) {
83
- // --- Shift+Tab (Unindent with Carry) ---
84
-
85
- // 1. Notre parent immédiat (une List)
86
- const parentListNode = this.node.parent();
87
- if (!parentListNode) return true;
88
-
89
- // 2. Le ListItem qui contient cette liste (le "grand-parent" logique)
90
- const grandParentItemNode = parentListNode.parent();
91
-
92
- // Si on n'a pas de ListItem au-dessus (on est déjà au premier niveau)
93
- if (!grandParentItemNode || grandParentItemNode.data.get("type") !== "list-item") {
94
- return true;
95
- }
96
-
97
- // 3. Gérer le "Carry" : emporter les frères suivants avec nous
98
- const myIndex = this.node.index();
99
- const siblings = parentListNode.children();
100
- const followers = siblings.slice(myIndex + 1);
101
-
102
- if (followers.length > 0) {
103
- // On s'assure d'avoir notre propre sous-liste pour accueillir nos anciens frères
104
- const mySublist = this.getOrCreateSublist();
105
- for (const follower of followers) {
106
- this.nabu.tree.move(follower.id.toString(), mySublist.node.id.toString());
107
- }
108
- }
109
-
110
- // 4. Le parent cible est la List qui contient notre grand-parent
111
- const targetParentListNode = grandParentItemNode.parent();
112
- if (!targetParentListNode) return true;
113
-
114
- // 5. On se place juste après notre ancien grand-parent
115
- const targetIndex = grandParentItemNode.index() + 1;
116
-
117
- this.nabu.tree.move(this.node.id.toString(), targetParentListNode.id.toString(), targetIndex);
118
-
119
- this.commit();
120
-
121
- // 6. Restauration du focus
122
- setTimeout(() => this.behavior.block.focus(), 0);
166
+ unindentListItem(this);
123
167
  } else {
124
- // --- Tab (Indent with Hoist) ---
125
-
126
- // 1. On trouve notre index actuel
127
- const currentIndex = this.node.index();
128
- if (currentIndex === 0) return true; // Impossible d'indenter le premier item
129
-
130
- // 2. On trouve le frère précédent (qui va devenir notre parent "logique")
131
- const parentList = this.node.parent();
132
- if (!parentList) return true;
133
-
134
- const previousNode = parentList.children()?.[currentIndex - 1];
135
- if (!previousNode) return true;
136
- const previousItem = this.nabu.blocks.get(previousNode.id.toString());
137
-
138
- // Si le frère précédent n'est pas un ListItem
139
- if (!previousItem || previousItem.type !== "list-item") return true;
140
-
141
- // 3. On demande au frère précédent sa sous-liste (ou de la créer)
142
- // @ts-ignore : On sait que c'est un ListItem
143
- const targetSublist = previousItem.getOrCreateSublist();
144
- const targetSublistId = targetSublist.node.id.toString();
145
-
146
- // 4. On se déplace à la fin de cette sous-liste cible
147
- this.nabu.tree.move(this.node.id.toString(), targetSublistId);
148
-
149
- // 5. Si on avait des enfants (Hoist), on les remonte avec nous en tant que frères
150
- if (this.sublist) {
151
- const childrenNodes = this.sublist.node.children();
152
- for (const childNode of childrenNodes) {
153
- // Ils se placeront après nous dans la nouvelle liste
154
- this.nabu.tree.move(childNode.id.toString(), targetSublistId);
155
- }
156
- }
157
-
158
- this.commit();
159
-
160
- // 6. On restaure le curseur
161
- setTimeout(() => this.behavior.block.focus(), 0);
168
+ indentListItem(this);
162
169
  }
163
170
  return true;
164
171
  }
165
-
166
- // On délègue les flèches etc.
167
172
  return false;
168
173
  }
169
174
 
@@ -130,7 +130,7 @@ export class List extends MegaBlock {
130
130
 
131
131
  /** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
132
132
  static create(nabu, type, props = {}, parentId = null, index = null) {
133
- const node = nabu.tree.createNode(parentId || undefined, index || undefined);
133
+ const node = nabu.tree.createNode(parentId || undefined, index != null ? index : undefined);
134
134
  node.data.set("type", "list");
135
135
  node.data.set("listType", props.listType || "bullet");
136
136
  const block = new List(nabu, node);
@@ -6,8 +6,6 @@ export class MegaBlock extends Block {
6
6
  children: Block[];
7
7
  updateChildren(): void;
8
8
  /** @param {Block[]} children @param {number | null} [index] */
9
- adoptChildren(children: Block[], index?: number | null): {
10
- skipped: boolean | Block[];
11
- };
9
+ adoptChildren(children: Block[], index?: number | null): void;
12
10
  }
13
11
  import { Block } from "./block.svelte.js";
@@ -40,20 +40,14 @@ export class MegaBlock extends Block {
40
40
 
41
41
  /** @param {Block[]} children @param {number | null} [index] */
42
42
  adoptChildren(children, index = null) {
43
- /** @type {Block[]} */
44
- let skipped = [];
45
- children.reverse().forEach(child => {
46
- const childNode = child.node;
47
- if (!childNode) return;
48
- if (false) {
49
- //TODO: conditions de skip, ex: si le block est déjà dans la mega block, ou si c'est un block parent de la mega block, etc.
50
- this.nabu.warn("Skipping child with id", child.id, "because ...");
51
- return skipped.push(child);
52
- }
53
- this.nabu.tree.move(childNode.id, this.node.id, index);
54
- })
55
-
56
- return {skipped: skipped.length ? skipped.reverse() : false};
43
+ // Reverse so that when index is specified, children are inserted in original
44
+ // order (each insert at the same position pushes others down, so reverse of
45
+ // reverse = original). With index=null (append), order doesn't matter.
46
+ // Spread to avoid mutating the input array (which may be a reactive Svelte array).
47
+ [...children].reverse().forEach(child => {
48
+ if (!child.node) return;
49
+ this.nabu.tree.move(child.node.id, this.node.id, index);
50
+ });
57
51
  }
58
52
 
59
53
 
@@ -51,6 +51,13 @@ export class Nabu {
51
51
  init(): void;
52
52
  /** Rafraîchit les blocs racines de l'éditeur */
53
53
  updateRoots(): void;
54
+ /**
55
+ * Dispatch an action to all registered handlers for the given topic.
56
+ * @param {string} topic
57
+ * @param {any} [data]
58
+ * @returns {Promise<import('@aionbuilders/pulse').PulseEvent>}
59
+ */
60
+ exec(topic: string, data?: any): Promise<import("@aionbuilders/pulse").PulseEvent>;
54
61
  trigger(hookName: any, ...args: any[]): void;
55
62
  commit(): void;
56
63
  /**
@@ -102,6 +109,7 @@ export class Nabu {
102
109
  beforeinput(e: InputEvent): any;
103
110
  /** @param {...any} args */
104
111
  warn(...args: any[]): void;
112
+ #private;
105
113
  }
106
114
  export type NabuNode<T extends {
107
115
  [x: string]: any;
@@ -4,6 +4,7 @@ import { Block } from './block.svelte.js';
4
4
  import { NabuSelection } from './selection.svelte.js';
5
5
  import { handleContainerBeforeInput } from './container.utils.js';
6
6
  import { tick } from 'svelte';
7
+ import { Pulse } from '@aionbuilders/pulse';
7
8
 
8
9
 
9
10
  /**
@@ -24,6 +25,9 @@ import { tick } from 'svelte';
24
25
  */
25
26
 
26
27
  export class Nabu {
28
+ /** @type {Pulse} */
29
+ #pulse = new Pulse();
30
+
27
31
  /** @param {NabuInit} init */
28
32
  constructor(init = {}) {
29
33
  this.doc = new LoroDoc();
@@ -87,9 +91,19 @@ export class Nabu {
87
91
  }
88
92
  }
89
93
 
94
+ if (ext.actions) {
95
+ for (const [topic, handler] of Object.entries(ext.actions)) {
96
+ this.#pulse.on(topic, ({ event }) => handler(this, event.data, event.topic));
97
+ }
98
+ }
99
+
90
100
  }
91
101
  }
92
-
102
+
103
+ // Core actions — always available
104
+ this.#pulse.on('undo', () => this.undo());
105
+ this.#pulse.on('redo', () => this.redo());
106
+
93
107
  const roots = /** @type {NabuNode[]} */ (this.tree.roots());
94
108
  if (roots?.length) {
95
109
  for (const root of roots) {
@@ -222,6 +236,16 @@ export class Nabu {
222
236
  });
223
237
  }
224
238
 
239
+ /**
240
+ * Dispatch an action to all registered handlers for the given topic.
241
+ * @param {string} topic
242
+ * @param {any} [data]
243
+ * @returns {Promise<import('@aionbuilders/pulse').PulseEvent>}
244
+ */
245
+ exec(topic, data) {
246
+ return this.#pulse.emit(topic, data);
247
+ }
248
+
225
249
  trigger(hookName, ...args) {
226
250
  const hooks = this.hooks.get(hookName) || [];
227
251
  for (const hook of hooks) {
@@ -9,6 +9,9 @@ import ParagraphComponent from "./Paragraph.svelte";
9
9
  const ParagraphExtension = extension("paragraph", {
10
10
  block: Paragraph,
11
11
  component: ParagraphComponent,
12
+ actions: {
13
+ 'paragraph': (nabu) => nabu.exec('block:transform', { type: 'paragraph' }),
14
+ },
12
15
  hooks: {
13
16
  onInit: (nabu) => {
14
17
  if (nabu.children.length === 0) {
@@ -13,7 +13,8 @@ export class NabuSelection extends SvelteSelection {
13
13
  super();
14
14
  this.nabu = nabu;
15
15
 
16
- $effect(() => {
16
+ $effect.root(() => {
17
+ $effect(() => {
17
18
  if (this.blocks) {
18
19
  this.previous.forEach(block => block.clearSelection());
19
20
  let i = 0;
@@ -27,6 +28,10 @@ export class NabuSelection extends SvelteSelection {
27
28
  this.previous = new Set(this.blocks);
28
29
  }
29
30
  })
31
+
32
+ })
33
+
34
+
30
35
  }
31
36
 
32
37
  anchorBlock = $derived.by(() => {
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Load a snapshot from IndexedDB.
3
+ * @param {string} docId
4
+ * @returns {Promise<Uint8Array | null>}
5
+ */
6
+ export function loadSnapshot(docId: string): Promise<Uint8Array | null>;
7
+ /**
8
+ * Save a snapshot to IndexedDB.
9
+ * @param {string} docId
10
+ * @param {Uint8Array} snapshot
11
+ * @returns {Promise<void>}
12
+ */
13
+ export function saveSnapshot(docId: string, snapshot: Uint8Array): Promise<void>;
14
+ /**
15
+ * Delete a snapshot from IndexedDB.
16
+ * @param {string} docId
17
+ * @returns {Promise<void>}
18
+ */
19
+ export function deleteSnapshot(docId: string): Promise<void>;