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

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 +179 -0
  80. package/dist/overlays/context/Context.svelte.d.ts +16 -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 +81 -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
@@ -1,10 +1,38 @@
1
1
  /**
2
- * @import { Nabu, NabuNode } from "../nabu.svelte";
2
+ * @import { Nabu, NabuNode } from "../nabu.svelte.js";
3
3
  */
4
4
  /**
5
5
  * @typedef {NabuNode<{type: "paragraph", text: LoroText}>} ParagraphNode
6
6
  */
7
7
  export class Paragraph extends Block {
8
+ /** @param {import('../../utils/extensions.js').PasteBlock} pb */
9
+ static toMarkdown(pb: import("../../utils/extensions.js").PasteBlock): string;
10
+ /** @param {import('../../utils/extensions.js').PasteBlock} pb */
11
+ static toHtml(pb: import("../../utils/extensions.js").PasteBlock): string;
12
+ static markdownRules: {
13
+ priority: number;
14
+ detect: () => boolean;
15
+ consume(lines: string[], i: number): number;
16
+ }[];
17
+ /**
18
+ * @param {string[]} lines
19
+ * @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[] }} helpers
20
+ * @returns {import('../../utils/extensions.js').PasteBlock | null}
21
+ */
22
+ static fromMarkdown(lines: string[], { parseInline }: {
23
+ parseInline: (text: string) => import("loro-crdt").Delta<string>[];
24
+ }): import("../../utils/extensions.js").PasteBlock | null;
25
+ static htmlRules: {
26
+ selector: string;
27
+ }[];
28
+ /**
29
+ * @param {Element} el
30
+ * @param {{ parseInline: (el: Element) => import('loro-crdt').Delta<string>[] }} helpers
31
+ * @returns {import('../../utils/extensions.js').PasteBlock | null}
32
+ */
33
+ static fromHTML(el: Element, { parseInline }: {
34
+ parseInline: (el: Element) => import("loro-crdt").Delta<string>[];
35
+ }): import("../../utils/extensions.js").PasteBlock | null;
8
36
  /** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
9
37
  static create(nabu: Nabu, type: string, props?: Object, parentId?: string | null, index?: number | null): Paragraph;
10
38
  /** @param {Nabu} nabu @param {ParagraphNode} node */
@@ -13,7 +41,7 @@ export class Paragraph extends Block {
13
41
  /** @type {TextBehavior} */
14
42
  behavior: TextBehavior;
15
43
  component: import("svelte").Component<any, any, string> | import("svelte").Component<{
16
- block: import("./paragraph.svelte").Paragraph;
44
+ block: import("./paragraph.svelte.js").Paragraph;
17
45
  }, {}, "">;
18
46
  get text(): string;
19
47
  get delta(): import("loro-crdt").Delta<string>[];
@@ -34,8 +62,8 @@ export type ParagraphNode = NabuNode<{
34
62
  type: "paragraph";
35
63
  text: LoroText;
36
64
  }>;
37
- import { Block } from "../block.svelte";
65
+ import { Block } from "../block.svelte.js";
38
66
  import { LoroText } from "loro-crdt";
39
67
  import { TextBehavior } from "../../behaviors/text";
40
- import type { Nabu } from "../nabu.svelte";
41
- import type { NabuNode } from "../nabu.svelte";
68
+ import type { Nabu } from "../nabu.svelte.js";
69
+ import type { NabuNode } from "../nabu.svelte.js";
@@ -1,10 +1,10 @@
1
- import { Block } from "../block.svelte";
1
+ import { Block } from "../block.svelte.js";
2
2
  import { LoroText } from "loro-crdt";
3
3
  import ParagraphComponent from "./Paragraph.svelte";
4
- import { TextBehavior } from "../../behaviors/text";
4
+ import { TextBehavior, deltaToHtml, deltaToMarkdown } from "../../behaviors/text";
5
5
 
6
6
  /**
7
- * @import { Nabu, NabuNode } from "../nabu.svelte";
7
+ * @import { Nabu, NabuNode } from "../nabu.svelte.js";
8
8
  */
9
9
 
10
10
  /**
@@ -29,6 +29,7 @@ export class Paragraph extends Block {
29
29
  type: 'paragraph',
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("paragraph") || ParagraphComponent);
@@ -42,6 +43,7 @@ export class Paragraph 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) {
@@ -73,6 +75,56 @@ export class Paragraph extends Block {
73
75
  /** @param {Parameters<Block["split"]>[0]} [options] @returns {ReturnType<Block["split"]>} */
74
76
  split(options) { return this.behavior.split(options); }
75
77
 
78
+ /** @param {import('../../utils/extensions.js').PasteBlock} pb */
79
+ static toMarkdown(pb) {
80
+ return deltaToMarkdown(pb.delta || []);
81
+ }
82
+
83
+ /** @param {import('../../utils/extensions.js').PasteBlock} pb */
84
+ static toHtml(pb) {
85
+ return `<p>${deltaToHtml(pb.delta || [])}</p>`;
86
+ }
87
+
88
+ static markdownRules = [
89
+ {
90
+ priority: 0, // catch-all — must be lowest
91
+ detect: () => true,
92
+ consume(/** @type {string[]} */ lines, /** @type {number} */ i) {
93
+ let j = i;
94
+ while (j < lines.length && lines[j].trim()) j++;
95
+ return Math.max(1, j - i);
96
+ },
97
+ }
98
+ ];
99
+
100
+ /**
101
+ * @param {string[]} lines
102
+ * @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[] }} helpers
103
+ * @returns {import('../../utils/extensions.js').PasteBlock | null}
104
+ */
105
+ static fromMarkdown(lines, { parseInline }) {
106
+ const delta = parseInline(lines.join(' '));
107
+ if (!delta.length) return null;
108
+ return { type: 'paragraph', delta, partial: false };
109
+ }
110
+
111
+ static htmlRules = [
112
+ { selector: 'p' },
113
+ { selector: 'blockquote' },
114
+ { selector: 'pre' },
115
+ ];
116
+
117
+ /**
118
+ * @param {Element} el
119
+ * @param {{ parseInline: (el: Element) => import('loro-crdt').Delta<string>[] }} helpers
120
+ * @returns {import('../../utils/extensions.js').PasteBlock | null}
121
+ */
122
+ static fromHTML(el, { parseInline }) {
123
+ const delta = parseInline(el);
124
+ if (!delta.length) return null;
125
+ return { type: 'paragraph', delta, partial: false };
126
+ }
127
+
76
128
  /** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
77
129
  static create(nabu, type, props = {}, parentId = null, index = null) {
78
130
  const node = nabu.tree.createNode(parentId || undefined, index || undefined);
@@ -1,6 +1,6 @@
1
1
  /**
2
- * @import { Nabu } from "./nabu.svelte";
3
- * @import { Block } from "./block.svelte";
2
+ * @import { Nabu } from "./nabu.svelte.js";
3
+ * @import { Block } from "./block.svelte.js";
4
4
  * @import { LoroTree, LoroTreeNode } from "loro-crdt";
5
5
  */
6
6
  export class NabuSelection extends SvelteSelection {
@@ -32,7 +32,8 @@ export class NabuSelection extends SvelteSelection {
32
32
  */
33
33
  setCursor(block: Block, offset: number): void;
34
34
  blocks: Set<Block>;
35
+ #private;
35
36
  }
36
- import { SvelteSelection } from "../utils/selection.svelte";
37
- import type { Nabu } from "./nabu.svelte";
38
- import type { Block } from "./block.svelte";
37
+ import { SvelteSelection } from "../utils/selection.svelte.js";
38
+ import type { Nabu } from "./nabu.svelte.js";
39
+ import type { Block } from "./block.svelte.js";
@@ -1,9 +1,9 @@
1
1
 
2
- import { SvelteSelection } from "../utils/selection.svelte";
2
+ import { SvelteSelection } from "../utils/selection.svelte.js";
3
3
 
4
4
  /**
5
- * @import { Nabu } from "./nabu.svelte";
6
- * @import { Block } from "./block.svelte";
5
+ * @import { Nabu } from "./nabu.svelte.js";
6
+ * @import { Block } from "./block.svelte.js";
7
7
  * @import { LoroTree, LoroTreeNode } from "loro-crdt";
8
8
  */
9
9
 
@@ -13,22 +13,103 @@ export class NabuSelection extends SvelteSelection {
13
13
  super();
14
14
  this.nabu = nabu;
15
15
 
16
- $effect(() => {
17
- if (this.blocks) {
18
- this.previous.forEach(block => block.clearSelection());
19
- let i = 0;
20
- const lastIndex = this.blocks.size - 1;
21
- this.blocks.forEach((block) => {
22
- block.selected = true;
23
- block.isSelectionStart = (i === 0);
24
- block.isSelectionEnd = i === lastIndex;
25
- i++;
26
- });
27
- this.previous = new Set(this.blocks);
28
- }
29
- })
16
+ $effect.root(() => {
17
+ $effect(() => {
18
+ if (this.blocks) {
19
+ this.previous.forEach(block => block.clearSelection());
20
+ let i = 0;
21
+ const lastIndex = this.blocks.size - 1;
22
+ this.blocks.forEach((block) => {
23
+ block.selected = true;
24
+ block.isSelectionStart = (i === 0);
25
+ block.isSelectionEnd = i === lastIndex;
26
+ i++;
27
+ });
28
+ this.previous = new Set(this.blocks);
29
+ }
30
+ });
31
+
32
+ // Track the intended navigation direction from arrow key presses.
33
+ // This is the only reliable source of direction — DOM position comparisons
34
+ // break when anchorNode is an ancestor of the previous position.
35
+ $effect(() => {
36
+ /** @param {KeyboardEvent} e */
37
+ const onKeydown = (e) => {
38
+ if (e.key === 'ArrowRight' || e.key === 'ArrowDown') {
39
+ this.#pendingDirection = 'forward';
40
+ } else if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') {
41
+ this.#pendingDirection = 'backward';
42
+ } else {
43
+ this.#pendingDirection = null;
44
+ }
45
+ };
46
+ document.addEventListener('keydown', onKeydown, true);
47
+ return () => document.removeEventListener('keydown', onKeydown, true);
48
+ });
49
+
50
+ // Normalize invalid caret/focus positions: inert elements and element-node
51
+ // cursors (which happen in container padding / between block children).
52
+ // Works for both collapsed cursors and non-collapsed selections (shift+arrow):
53
+ // - anchor invalid → collapse to corrected anchor
54
+ // - only focus invalid → keep anchor, correct only focus (preserves the selection)
55
+ $effect(() => {
56
+ const activeEl = document.activeElement;
57
+ if (activeEl && activeEl.closest?.('[data-nabu-ui], input, textarea, select, button')) {
58
+ this.#pendingDirection = null;
59
+ return;
60
+ }
61
+
62
+ const anchor = this.anchorNode;
63
+ const anchorOffset = this.anchorOffset;
64
+ const focus = this.focusNode;
65
+ const focusOffset = this.focusOffset;
66
+ if (!anchor) return;
67
+
68
+ const anchorEl = /** @type {Element | null} */ (anchor.nodeType === Node.ELEMENT_NODE ? anchor : anchor.parentElement);
69
+ const focusEl = /** @type {Element | null} */ (focus?.nodeType === Node.ELEMENT_NODE ? focus : focus?.parentElement);
70
+
71
+ // Skip normalization when the selection touches a UI element (e.g. zone title input).
72
+ if (anchorEl?.closest('[data-nabu-ui]') || focusEl?.closest('[data-nabu-ui]')) {
73
+ this.#pendingDirection = null;
74
+ return;
75
+ }
76
+
77
+ const anchorInvalid = isInInert(anchor) || (
78
+ anchor.nodeType !== Node.TEXT_NODE &&
79
+ !!/** @type {Element} */ (anchor).closest?.('[data-block-id]')
80
+ );
81
+ const focusInvalid = !!focus && (isInInert(focus) || (
82
+ focus.nodeType !== Node.TEXT_NODE &&
83
+ !!/** @type {Element} */ (focus).closest?.('[data-block-id]')
84
+ ));
85
+
86
+ if (!anchorInvalid && !focusInvalid) {
87
+ this.#pendingDirection = null;
88
+ return;
89
+ }
90
+
91
+ if (anchorInvalid) {
92
+ const dir = this.#pendingDirection ?? offsetHeuristic(anchor, anchorOffset);
93
+ this.#pendingDirection = null;
94
+ const target = findNearestTextNode(anchor, anchorOffset, dir);
95
+ if (!target) return;
96
+ const offset = dir === 'forward' ? 0 : (target.textContent?.length ?? 0);
97
+ this.setBaseAndExtent(target, offset, target, offset);
98
+ } else if (focusInvalid && focus) {
99
+ const dir = this.#pendingDirection ?? offsetHeuristic(focus, focusOffset);
100
+ this.#pendingDirection = null;
101
+ const target = findNearestTextNode(focus, focusOffset, dir);
102
+ if (!target) return;
103
+ const offset = dir === 'forward' ? 0 : (target.textContent?.length ?? 0);
104
+ this.setBaseAndExtent(anchor, anchorOffset, target, offset);
105
+ }
106
+ });
107
+ });
30
108
  }
31
-
109
+
110
+ /** @type {'forward' | 'backward' | null} */
111
+ #pendingDirection = null;
112
+
32
113
  anchorBlock = $derived.by(() => {
33
114
  this.anchorNode;
34
115
  const id = this.anchorNode?.parentElement?.closest("[data-block-id]")?.getAttribute("data-block-id");
@@ -90,6 +171,116 @@ export class NabuSelection extends SvelteSelection {
90
171
 
91
172
 
92
173
 
174
+ /** @param {Node | null} node */
175
+ function isInInert(node) {
176
+ if (!node) return false;
177
+ const el = node.nodeType === Node.ELEMENT_NODE
178
+ ? /** @type {Element} */ (node)
179
+ : node.parentElement;
180
+ return !!el?.closest('[data-nabu-inert]');
181
+ }
182
+
183
+ /**
184
+ * Heuristic for mouse clicks: infer direction from where in the element the
185
+ * cursor landed. At/past the last child → user likely moving forward; otherwise
186
+ * backward.
187
+ * @param {Node} anchor
188
+ * @param {number} anchorOffset
189
+ * @returns {'forward' | 'backward'}
190
+ */
191
+ function offsetHeuristic(anchor, anchorOffset) {
192
+ if (anchor.nodeType !== Node.TEXT_NODE) {
193
+ return anchorOffset >= anchor.childNodes.length ? 'forward' : 'backward';
194
+ }
195
+ return 'backward';
196
+ }
197
+
198
+ /** @type {NodeFilter} */
199
+ const TEXT_FILTER = {
200
+ acceptNode(/** @type {Node} */ node) {
201
+ if (isInInert(node)) return NodeFilter.FILTER_REJECT;
202
+ if (!/** @type {Text} */ (node).parentElement?.closest('[data-block-id]')) return NodeFilter.FILTER_REJECT;
203
+ return NodeFilter.FILTER_ACCEPT;
204
+ }
205
+ };
206
+
207
+ /**
208
+ * Returns the deepest last descendant of a node (for positioning a TreeWalker
209
+ * so that nextNode() starts at the node immediately after the subtree).
210
+ * @param {Node} node
211
+ */
212
+ function getLastDescendant(node) {
213
+ let n = node;
214
+ while (n.lastChild) n = n.lastChild;
215
+ return n;
216
+ }
217
+
218
+ /**
219
+ * Find the nearest valid (non-inert, inside a block) text node from a given
220
+ * DOM position, respecting the intended navigation direction.
221
+ *
222
+ * For element-node cursors, (fromNode, fromOffset) means the caret sits between
223
+ * child[fromOffset-1] and child[fromOffset]. We use fromOffset to start the
224
+ * search at the right position — NOT from the beginning/end of the element —
225
+ * so we never jump back to a position the user is trying to leave.
226
+ *
227
+ * @param {Node} fromNode
228
+ * @param {number} fromOffset
229
+ * @param {'forward' | 'backward'} direction
230
+ * @returns {Text | null}
231
+ */
232
+ function findNearestTextNode(fromNode, fromOffset, direction) {
233
+ // @ts-ignore — TreeWalker filter shape is correct, TS typedef is overly strict
234
+ const bodyWalker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, TEXT_FILTER);
235
+
236
+ if (fromNode.nodeType === Node.TEXT_NODE) {
237
+ bodyWalker.currentNode = fromNode;
238
+ return /** @type {Text | null} */ (
239
+ direction === 'backward' ? bodyWalker.previousNode() : bodyWalker.nextNode()
240
+ );
241
+ }
242
+
243
+ // Element-node cursor: caret is between child[fromOffset-1] and child[fromOffset].
244
+ const childCount = fromNode.childNodes.length;
245
+
246
+ if (direction === 'backward') {
247
+ if (fromOffset === 0) {
248
+ // Before all children → escape before element
249
+ bodyWalker.currentNode = fromNode;
250
+ return /** @type {Text | null} */ (bodyWalker.previousNode());
251
+ }
252
+ if (fromOffset < childCount) {
253
+ // Between child[fromOffset-1] and child[fromOffset]:
254
+ // last valid text before child[fromOffset] (may be inside or before element)
255
+ bodyWalker.currentNode = fromNode.childNodes[fromOffset];
256
+ return /** @type {Text | null} */ (bodyWalker.previousNode());
257
+ }
258
+ // Past last child → last valid text inside the element (sub-walker), or before it
259
+ // @ts-ignore
260
+ const sub = document.createTreeWalker(fromNode, NodeFilter.SHOW_TEXT, TEXT_FILTER);
261
+ let last = null, n;
262
+ while ((n = sub.nextNode())) last = n;
263
+ if (last) return /** @type {Text} */ (last);
264
+ bodyWalker.currentNode = fromNode;
265
+ return /** @type {Text | null} */ (bodyWalker.previousNode());
266
+ } else {
267
+ if (fromOffset >= childCount) {
268
+ // Past last child → escape after element
269
+ bodyWalker.currentNode = getLastDescendant(fromNode);
270
+ return /** @type {Text | null} */ (bodyWalker.nextNode());
271
+ }
272
+ // Between child[fromOffset-1] and child[fromOffset]:
273
+ // first valid text at or after child[fromOffset] (may be inside or after element).
274
+ // Pivot to the last descendant of child[fromOffset-1] so nextNode() starts
275
+ // from child[fromOffset] onward — never re-entering already-visited content.
276
+ const pivot = fromOffset > 0
277
+ ? getLastDescendant(fromNode.childNodes[fromOffset - 1])
278
+ : fromNode;
279
+ bodyWalker.currentNode = pivot;
280
+ return /** @type {Text | null} */ (bodyWalker.nextNode());
281
+ }
282
+ }
283
+
93
284
  /**
94
285
  * Récupère tous les nœuds situés entre deux nœuds dans un arbre Loro (version aplatie).
95
286
  * L'ordre dans lequel on passe A et B n'a pas d'importance.
@@ -0,0 +1,47 @@
1
+ /**
2
+ * @import {Block} from "./block.svelte.js"
3
+ */
4
+ /**
5
+ * @template {Block} [TBlock=Block]
6
+ * @typedef {Object} VariableInit
7
+ * @property {"string"|"number"|"boolean"} type - The variable type (e.g., "string", "number", "boolean").
8
+ * @property {function(TBlock): any} get - A function that returns the current value of the variable.
9
+ * @property {function(TBlock, any): void} [set] - A function that updates the variable's value.
10
+ */
11
+ /**
12
+ * @template {Block} [TBlock=Block]
13
+ */
14
+ export class Variable<TBlock extends Block = Block> {
15
+ /**
16
+ * Creates a new Variable instance.
17
+ * @param {TBlock} block - The block that owns this variable.
18
+ * @param {string} name
19
+ * @param {VariableInit<TBlock>} init
20
+ */
21
+ constructor(block: TBlock, name: string, init: VariableInit<TBlock>);
22
+ block: TBlock;
23
+ name: string;
24
+ init: VariableInit<TBlock>;
25
+ type: "string" | "number" | "boolean";
26
+ get: (arg0: TBlock) => any;
27
+ set: ((arg0: TBlock, arg1: any) => void) | undefined;
28
+ writable: boolean;
29
+ set value(newValue: any);
30
+ get value(): any;
31
+ destroy(): void;
32
+ }
33
+ export type VariableInit<TBlock extends Block = Block> = {
34
+ /**
35
+ * - The variable type (e.g., "string", "number", "boolean").
36
+ */
37
+ type: "string" | "number" | "boolean";
38
+ /**
39
+ * - A function that returns the current value of the variable.
40
+ */
41
+ get: (arg0: TBlock) => any;
42
+ /**
43
+ * - A function that updates the variable's value.
44
+ */
45
+ set?: ((arg0: TBlock, arg1: any) => void) | undefined;
46
+ };
47
+ import type { Block } from "./block.svelte.js";
@@ -0,0 +1,51 @@
1
+ /**
2
+ * @import {Block} from "./block.svelte.js"
3
+ */
4
+
5
+
6
+ /**
7
+ * @template {Block} [TBlock=Block]
8
+ * @typedef {Object} VariableInit
9
+ * @property {"string"|"number"|"boolean"} type - The variable type (e.g., "string", "number", "boolean").
10
+ * @property {function(TBlock): any} get - A function that returns the current value of the variable.
11
+ * @property {function(TBlock, any): void} [set] - A function that updates the variable's value.
12
+ */
13
+
14
+ /**
15
+ * @template {Block} [TBlock=Block]
16
+ */
17
+ export class Variable {
18
+ /**
19
+ * Creates a new Variable instance.
20
+ * @param {TBlock} block - The block that owns this variable.
21
+ * @param {string} name
22
+ * @param {VariableInit<TBlock>} init
23
+ */
24
+ constructor(block, name, init) {
25
+ this.block = block;
26
+ this.name = name;
27
+ this.init = init;
28
+ this.type = init.type;
29
+ this.get = init.get;
30
+ this.set = init.set;
31
+
32
+ this.block.variables.set(name, this);
33
+ }
34
+
35
+ writable = $derived(!!this.set);
36
+
37
+ set value(newValue) {
38
+ if (!this.set) {
39
+ throw new Error(`Variable "${this.name}" is read-only.`);
40
+ }
41
+ this.set(this.block, newValue);
42
+ }
43
+
44
+ get value() {
45
+ return this.get(this.block);
46
+ }
47
+
48
+ destroy() {
49
+ this.block.variables.delete(this.name);
50
+ }
51
+ }