@aionbuilders/nabu 0.1.0-alpha.8 → 0.1.0-beta.0

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 (177) hide show
  1. package/README.md +216 -9
  2. package/dist/behaviors/text/RichText.svelte +4 -1
  3. package/dist/behaviors/text/index.d.ts +2 -0
  4. package/dist/behaviors/text/index.js +2 -0
  5. package/dist/behaviors/text/markdown-shortcuts.extension.d.ts +2 -0
  6. package/dist/behaviors/text/markdown-shortcuts.extension.js +54 -0
  7. package/dist/behaviors/text/rich-text.extension.js +47 -22
  8. package/dist/behaviors/text/text.behavior.svelte.d.ts +25 -1
  9. package/dist/behaviors/text/text.behavior.svelte.js +154 -18
  10. package/dist/behaviors/text/typography.extension.d.ts +6 -0
  11. package/dist/behaviors/text/typography.extension.js +24 -0
  12. package/dist/blocks/Block.svelte +5 -2
  13. package/dist/blocks/BlockGap.svelte +55 -0
  14. package/dist/blocks/BlockGap.svelte.d.ts +11 -0
  15. package/dist/blocks/Nabu.svelte +22 -0
  16. package/dist/blocks/block.extension.js +10 -8
  17. package/dist/blocks/block.svelte.d.ts +24 -0
  18. package/dist/blocks/block.svelte.js +35 -0
  19. package/dist/blocks/blockquote/Blockquote.svelte +40 -0
  20. package/dist/blocks/blockquote/Blockquote.svelte.d.ts +11 -0
  21. package/dist/blocks/blockquote/blockquote.extension.d.ts +1 -0
  22. package/dist/blocks/blockquote/blockquote.extension.js +71 -0
  23. package/dist/blocks/blockquote/blockquote.svelte.d.ts +55 -0
  24. package/dist/blocks/blockquote/blockquote.svelte.js +91 -0
  25. package/dist/blocks/blockquote/index.d.ts +3 -0
  26. package/dist/blocks/blockquote/index.js +3 -0
  27. package/dist/blocks/callout/Callout.svelte +69 -0
  28. package/dist/blocks/callout/Callout.svelte.d.ts +11 -0
  29. package/dist/blocks/callout/callout.extension.d.ts +1 -0
  30. package/dist/blocks/callout/callout.extension.js +68 -0
  31. package/dist/blocks/callout/callout.svelte.d.ts +78 -0
  32. package/dist/blocks/callout/callout.svelte.js +184 -0
  33. package/dist/blocks/callout/index.d.ts +3 -0
  34. package/dist/blocks/callout/index.js +3 -0
  35. package/dist/blocks/code/Code.svelte +85 -0
  36. package/dist/blocks/code/Code.svelte.d.ts +11 -0
  37. package/dist/blocks/code/code.extension.d.ts +1 -0
  38. package/dist/blocks/code/code.extension.js +68 -0
  39. package/dist/blocks/code/code.svelte.d.ts +61 -0
  40. package/dist/blocks/code/code.svelte.js +221 -0
  41. package/dist/blocks/code/index.d.ts +3 -0
  42. package/dist/blocks/code/index.js +3 -0
  43. package/dist/blocks/dialogue/Dialogue.svelte +2 -0
  44. package/dist/blocks/dialogue/dialogue.svelte.d.ts +3 -21
  45. package/dist/blocks/dialogue/dialogue.svelte.js +2 -50
  46. package/dist/blocks/divider/Divider.svelte +28 -0
  47. package/dist/blocks/divider/Divider.svelte.d.ts +11 -0
  48. package/dist/blocks/divider/divider.extension.d.ts +1 -0
  49. package/dist/blocks/divider/divider.extension.js +87 -0
  50. package/dist/blocks/divider/divider.svelte.d.ts +49 -0
  51. package/dist/blocks/divider/divider.svelte.js +84 -0
  52. package/dist/blocks/divider/index.d.ts +3 -0
  53. package/dist/blocks/divider/index.js +3 -0
  54. package/dist/blocks/file/File.svelte +49 -0
  55. package/dist/blocks/file/File.svelte.d.ts +11 -0
  56. package/dist/blocks/file/file.extension.d.ts +1 -0
  57. package/dist/blocks/file/file.extension.js +19 -0
  58. package/dist/blocks/file/file.svelte.d.ts +29 -0
  59. package/dist/blocks/file/file.svelte.js +71 -0
  60. package/dist/blocks/file/index.d.ts +3 -0
  61. package/dist/blocks/file/index.js +3 -0
  62. package/dist/blocks/heading/Heading.svelte +3 -1
  63. package/dist/blocks/heading/heading.svelte.d.ts +4 -23
  64. package/dist/blocks/heading/heading.svelte.js +4 -47
  65. package/dist/blocks/image/Image.svelte +38 -0
  66. package/dist/blocks/image/Image.svelte.d.ts +11 -0
  67. package/dist/blocks/image/image.extension.d.ts +2 -0
  68. package/dist/blocks/image/image.extension.js +26 -0
  69. package/dist/blocks/image/image.svelte.d.ts +50 -0
  70. package/dist/blocks/image/image.svelte.js +92 -0
  71. package/dist/blocks/image/index.d.ts +3 -0
  72. package/dist/blocks/image/index.js +3 -0
  73. package/dist/blocks/index.d.ts +11 -0
  74. package/dist/blocks/index.js +11 -0
  75. package/dist/blocks/list/List.svelte +8 -2
  76. package/dist/blocks/list/ListItem.svelte +43 -7
  77. package/dist/blocks/list/hooks/onBeforeInput.hook.js +15 -2
  78. package/dist/blocks/list/index.d.ts +1 -0
  79. package/dist/blocks/list/index.js +1 -0
  80. package/dist/blocks/list/list-item.svelte.d.ts +14 -2
  81. package/dist/blocks/list/list-item.svelte.js +58 -7
  82. package/dist/blocks/list/list.behavior.svelte.d.ts +4 -4
  83. package/dist/blocks/list/list.behavior.svelte.js +5 -5
  84. package/dist/blocks/list/list.svelte.d.ts +13 -6
  85. package/dist/blocks/list/list.svelte.js +24 -6
  86. package/dist/blocks/list/todos.svelte.d.ts +35 -0
  87. package/dist/blocks/list/todos.svelte.js +76 -0
  88. package/dist/blocks/media/index.d.ts +2 -0
  89. package/dist/blocks/media/index.js +2 -0
  90. package/dist/blocks/media/media-block.svelte.d.ts +32 -0
  91. package/dist/blocks/media/media-block.svelte.js +69 -0
  92. package/dist/blocks/media/media.helpers.d.ts +19 -0
  93. package/dist/blocks/media/media.helpers.js +51 -0
  94. package/dist/blocks/megablock.svelte.js +1 -0
  95. package/dist/blocks/nabu.svelte.d.ts +94 -16
  96. package/dist/blocks/nabu.svelte.js +254 -3
  97. package/dist/blocks/paragraph/Paragraph.svelte +2 -0
  98. package/dist/blocks/paragraph/index.js +8 -3
  99. package/dist/blocks/paragraph/paragraph.svelte.d.ts +3 -21
  100. package/dist/blocks/paragraph/paragraph.svelte.js +4 -53
  101. package/dist/blocks/selection.svelte.d.ts +1 -0
  102. package/dist/blocks/selection.svelte.js +212 -16
  103. package/dist/blocks/text-block.svelte.d.ts +49 -0
  104. package/dist/blocks/text-block.svelte.js +74 -0
  105. package/dist/blocks/toggle/Toggle.svelte +84 -0
  106. package/dist/blocks/toggle/Toggle.svelte.d.ts +11 -0
  107. package/dist/blocks/toggle/index.d.ts +3 -0
  108. package/dist/blocks/toggle/index.js +3 -0
  109. package/dist/blocks/toggle/toggle.extension.d.ts +1 -0
  110. package/dist/blocks/toggle/toggle.extension.js +12 -0
  111. package/dist/blocks/toggle/toggle.svelte.d.ts +88 -0
  112. package/dist/blocks/toggle/toggle.svelte.js +252 -0
  113. package/dist/blocks/variable.svelte.d.ts +47 -0
  114. package/dist/blocks/variable.svelte.js +51 -0
  115. package/dist/blocks/video/Video.svelte +39 -0
  116. package/dist/blocks/video/Video.svelte.d.ts +11 -0
  117. package/dist/blocks/video/index.d.ts +3 -0
  118. package/dist/blocks/video/index.js +3 -0
  119. package/dist/blocks/video/video.extension.d.ts +1 -0
  120. package/dist/blocks/video/video.extension.js +19 -0
  121. package/dist/blocks/video/video.svelte.d.ts +28 -0
  122. package/dist/blocks/video/video.svelte.js +71 -0
  123. package/dist/blocks/zone/TemplateEditor.svelte +308 -0
  124. package/dist/blocks/zone/TemplateEditor.svelte.d.ts +11 -0
  125. package/dist/blocks/zone/Zone.svelte +272 -0
  126. package/dist/blocks/zone/Zone.svelte.d.ts +11 -0
  127. package/dist/blocks/zone/ZoneEditor.svelte +289 -0
  128. package/dist/blocks/zone/ZoneEditor.svelte.d.ts +11 -0
  129. package/dist/blocks/zone/ZoneTemplatesList.svelte +188 -0
  130. package/dist/blocks/zone/ZoneTemplatesList.svelte.d.ts +11 -0
  131. package/dist/blocks/zone/hooks/onBeforeInput.hook.d.ts +3 -0
  132. package/dist/blocks/zone/hooks/onBeforeInput.hook.js +30 -0
  133. package/dist/blocks/zone/index.d.ts +2 -0
  134. package/dist/blocks/zone/index.js +2 -0
  135. package/dist/blocks/zone/zone.extension.d.ts +2 -0
  136. package/dist/blocks/zone/zone.extension.js +237 -0
  137. package/dist/blocks/zone/zone.svelte.d.ts +221 -0
  138. package/dist/blocks/zone/zone.svelte.js +608 -0
  139. package/dist/files/files.svelte.d.ts +61 -0
  140. package/dist/files/files.svelte.js +93 -0
  141. package/dist/files/index.d.ts +1 -0
  142. package/dist/files/index.js +1 -0
  143. package/dist/index.d.ts +4 -1
  144. package/dist/index.js +5 -2
  145. package/dist/nabu-theme.css +92 -0
  146. package/dist/overlays/context/Context.svelte +182 -0
  147. package/dist/overlays/context/Context.svelte.d.ts +17 -0
  148. package/dist/overlays/context/collect.d.ts +17 -0
  149. package/dist/overlays/context/collect.js +33 -0
  150. package/dist/overlays/context/context.extension.d.ts +2 -0
  151. package/dist/overlays/context/context.extension.js +8 -0
  152. package/dist/overlays/context/context.svelte.d.ts +82 -0
  153. package/dist/overlays/context/context.svelte.js +159 -0
  154. package/dist/overlays/context/index.d.ts +3 -0
  155. package/dist/overlays/context/index.js +3 -0
  156. package/dist/paste/html.paste.js +9 -1
  157. package/dist/presets.d.ts +3 -1
  158. package/dist/presets.js +22 -6
  159. package/dist/ui/DragHandle.svelte +141 -0
  160. package/dist/ui/DragHandle.svelte.d.ts +11 -0
  161. package/dist/ui/FloatingToolbar.svelte +117 -0
  162. package/dist/ui/FloatingToolbar.svelte.d.ts +17 -0
  163. package/dist/ui/SlashMenu.svelte +83 -0
  164. package/dist/ui/SlashMenu.svelte.d.ts +13 -0
  165. package/dist/ui/index.d.ts +4 -0
  166. package/dist/ui/index.js +4 -0
  167. package/dist/ui/slash-menu.svelte.d.ts +40 -0
  168. package/dist/ui/slash-menu.svelte.js +116 -0
  169. package/dist/utils/extensions.d.ts +54 -0
  170. package/dist/utils/extensions.js +40 -1
  171. package/dist/utils/index.d.ts +1 -0
  172. package/dist/utils/index.js +1 -0
  173. package/dist/utils/placeholders.d.ts +10 -0
  174. package/dist/utils/placeholders.js +21 -0
  175. package/dist/vite.d.ts +16 -0
  176. package/dist/vite.js +23 -0
  177. package/package.json +10 -5
@@ -33,6 +33,7 @@ export function deltaToHtml(delta) {
33
33
  if (attrs.italic) text = `<em>${text}</em>`;
34
34
  if (attrs.underline) text = `<u>${text}</u>`;
35
35
  if (attrs.strikethrough) text = `<s>${text}</s>`;
36
+ if (attrs.link) text = `<a href="${escapeHtml(String(attrs.link))}">${text}</a>`;
36
37
  return text;
37
38
  }).join('');
38
39
  }
@@ -45,14 +46,15 @@ export function deltaToHtml(delta) {
45
46
  export function deltaToMarkdown(delta) {
46
47
  return delta.map(op => {
47
48
  if (typeof op.insert !== 'string') return '';
48
- const text = op.insert;
49
+ let text = op.insert;
49
50
  const attrs = op.attributes || {};
50
- if (attrs.code) return `\`${text}\``;
51
- if (attrs.bold && attrs.italic) return `***${text}***`;
52
- if (attrs.bold) return `**${text}**`;
53
- if (attrs.italic) return `*${text}*`;
54
- if (attrs.strikethrough) return `~~${text}~~`;
55
- if (attrs.underline) return `<u>${text}</u>`;
51
+ if (attrs.code) text = `\`${text}\``;
52
+ else if (attrs.bold && attrs.italic) text = `***${text}***`;
53
+ else if (attrs.bold) text = `**${text}**`;
54
+ else if (attrs.italic) text = `*${text}*`;
55
+ else if (attrs.strikethrough) text = `~~${text}~~`;
56
+ else if (attrs.underline) text = `<u>${text}</u>`;
57
+ if (attrs.link) text = `[${text}](${attrs.link})`;
56
58
  return text;
57
59
  }).join('');
58
60
  }
@@ -78,8 +80,16 @@ export class TextBehavior {
78
80
  }
79
81
 
80
82
  selection = $derived(this.block.element && this.block.nabu.selection && this.getSelection(this.block.element, this.block.nabu.selection));
81
-
83
+
82
84
  positions = $derived(this.text.length + 1);
85
+
86
+ /**
87
+ * Number of whitespace-separated words in this text.
88
+ * @returns {number}
89
+ */
90
+ get wordCount() {
91
+ return this.text.trim().split(/\s+/).filter(Boolean).length;
92
+ }
83
93
 
84
94
  /**
85
95
  * @param {HTMLElement} element
@@ -129,8 +139,16 @@ export class TextBehavior {
129
139
 
130
140
  /** @param {InputEvent} event @param {ReturnType<TextBehavior["getSelection"]>} [selection] */
131
141
  handleBeforeInput(event, selection = this.selection) {
132
- // Hook system (Markdown shortcuts, de-transformation, etc.)
133
- const hooks = this.nabu.hooks.get("onBeforeInput");
142
+ // Hook system (Markdown shortcuts, de-transformation, structural guards…).
143
+ // `transformsEnabled: false` (e.g. code blocks) only suppresses transforms,
144
+ // which fire on insertions — deletion hooks (block exit, zone guards) still run.
145
+ const isInsertion = event.inputType === "insertText"
146
+ || event.inputType === "insertParagraph"
147
+ || event.inputType === "insertLineBreak"
148
+ || event.inputType === "insertSoftLineBreak";
149
+ const hooks = (this.block.transformsEnabled === false && isInsertion)
150
+ ? null
151
+ : this.nabu.hooks.get("onBeforeInput");
134
152
  if (hooks) {
135
153
  for (const hook of hooks) {
136
154
  const result = hook(this.nabu, event, this.block);
@@ -151,8 +169,19 @@ export class TextBehavior {
151
169
  return this.handleDeleteContentBackward(event, selection);
152
170
  case "deleteContentForward":
153
171
  return this.handleDeleteContentForward(event, selection);
172
+ case "deleteWordBackward":
173
+ return this.handleDeleteWordBackward(event, selection);
174
+ case "deleteWordForward":
175
+ return this.handleDeleteWordForward(event, selection);
176
+ case "deleteSoftLineBackward":
177
+ return this.handleDeleteSoftLineBackward(event, selection);
154
178
  case "insertParagraph":
155
179
  return this.handleInsertParagraph(event, selection);
180
+ case "historyUndo":
181
+ case "historyRedo":
182
+ // Handled by the keydown handler (Ctrl+Z / Ctrl+Y) via UndoManager.
183
+ event.preventDefault();
184
+ return true;
156
185
  default:
157
186
  this.nabu.warn("Unhandled input type:", event.inputType);
158
187
  return;
@@ -165,20 +194,37 @@ export class TextBehavior {
165
194
 
166
195
  let textToInsert = event.data || "";
167
196
 
168
- // Substitution typographique : `-- ` → `— ` (em-dash)
169
- if (textToInsert === " " && selection.isCollapsed) {
197
+ // Declarative input rules (typography, markdown shortcuts…)
198
+ if (selection.isCollapsed && this.block.transformsEnabled !== false) {
170
199
  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;
200
+ for (const rule of this.nabu.inputRules) {
201
+ if ((rule.inputType ?? "insertText") !== event.inputType) continue;
202
+ const match = textBeforeCursor.match(rule.pattern);
203
+ if (!match) continue;
204
+ if (rule.handler(this.nabu, this.block, match, { ...selection, data: textToInsert }) === true) {
205
+ this.nabu.storedMarks = null;
206
+ this.block.commit();
207
+ // Clamp to the post-mutation text length: rules that shrink the text
208
+ // (e.g. markdown **bold** → bold) would otherwise restore the cursor
209
+ // past the end, hitting getDOMPoint's element fallback → the selection
210
+ // normalizer ejects the caret out of the block, losing all further input.
211
+ tick().then(() => this.nabu.selection.setCursor(this.block, Math.min(selection.from, this.text.length)));
212
+ return true;
213
+ }
177
214
  }
178
215
  }
179
216
 
180
217
  if (!selection.isCollapsed) this.delete({index: selection.from, length: selection.to - selection.from}, selection);
181
218
  this.insert(selection.from, textToInsert);
219
+ // Apply the cursor's pending stored marks to the inserted text.
220
+ const stored = this.nabu.storedMarks;
221
+ if (stored && textToInsert) {
222
+ const range = { from: selection.from, to: selection.from + textToInsert.length, isCollapsed: false };
223
+ for (const [name, on] of Object.entries(stored)) {
224
+ if (on) this.applyMark(name, true, range);
225
+ else this.removeMark(name, range);
226
+ }
227
+ }
182
228
  this.block.commit();
183
229
  tick().then(() => this.nabu.selection.setCursor(this.block, selection.from + textToInsert.length));
184
230
  }
@@ -251,9 +297,99 @@ export class TextBehavior {
251
297
  }
252
298
  }
253
299
 
300
+ /**
301
+ * Deletes the word before the cursor (Ctrl+Backspace on Win/Linux,
302
+ * Option+Delete on Mac), including the whitespace that precedes it
303
+ * so no orphan space is left behind (issue #4).
304
+ * @param {InputEvent} event @param {ReturnType<TextBehavior["getSelection"]>} [sel]
305
+ */
306
+ handleDeleteWordBackward(event, sel = this.selection) {
307
+ if (!sel) return;
308
+ if (!sel.isCollapsed) {
309
+ this.delete({ index: sel.from, length: sel.to - sel.from });
310
+ this.block.commit();
311
+ tick().then(() => this.nabu.selection.setCursor(this.block, sel.from));
312
+ return true;
313
+ }
314
+ // Prefer getTargetRanges() when available (more reliable cross-platform)
315
+ const ranges = event.getTargetRanges?.();
316
+ let deleteFrom;
317
+ if (ranges?.length) {
318
+ const r = ranges[0];
319
+ deleteFrom = this.calculateOffset(r.startContainer, r.startOffset);
320
+ } else {
321
+ // FIX issue #4 : inclure le whitespace AVANT le mot dans la suppression
322
+ const textBefore = this.text.slice(0, sel.from);
323
+ // \s* leading : l'espace qui précède le mot est supprimé aussi (comportement Mac)
324
+ const wordMatch = textBefore.match(/(\s*\S+\s*)$/);
325
+ deleteFrom = wordMatch ? sel.from - wordMatch[0].length : 0;
326
+ }
327
+ const len = sel.from - deleteFrom;
328
+ if (len > 0) this.delete({ index: deleteFrom, length: len });
329
+ this.block.commit();
330
+ tick().then(() => this.nabu.selection.setCursor(this.block, deleteFrom));
331
+ return true;
332
+ }
333
+
334
+ /**
335
+ * Deletes the word after the cursor (Ctrl+Delete / Option+Fn+Delete),
336
+ * including its trailing whitespace/punctuation.
337
+ * @param {InputEvent} event @param {ReturnType<TextBehavior["getSelection"]>} [sel]
338
+ */
339
+ handleDeleteWordForward(event, sel = this.selection) {
340
+ if (!sel) return;
341
+ if (!sel.isCollapsed) {
342
+ this.delete({ index: sel.from, length: sel.to - sel.from });
343
+ this.block.commit();
344
+ tick().then(() => this.nabu.selection.setCursor(this.block, sel.from));
345
+ return true;
346
+ }
347
+ const ranges = event.getTargetRanges?.();
348
+ let deleteTo;
349
+ if (ranges?.length) {
350
+ const r = ranges[0];
351
+ deleteTo = this.calculateOffset(r.endContainer, r.endOffset);
352
+ } else {
353
+ const textAfter = this.text.slice(sel.from);
354
+ // Inclure le whitespace leading + le mot + son trailing whitespace
355
+ const wordMatch = textAfter.match(/^(\s*\S+\s*)/);
356
+ deleteTo = sel.from + (wordMatch ? wordMatch[0].length : textAfter.length);
357
+ }
358
+ const len = deleteTo - sel.from;
359
+ if (len > 0) this.delete({ index: sel.from, length: len });
360
+ this.block.commit();
361
+ tick().then(() => this.nabu.selection.setCursor(this.block, sel.from));
362
+ return true;
363
+ }
364
+
365
+ /**
366
+ * Deletes from the start of the current soft line to the cursor
367
+ * (Cmd+Backspace on Mac).
368
+ * @param {InputEvent} event @param {ReturnType<TextBehavior["getSelection"]>} [sel]
369
+ */
370
+ handleDeleteSoftLineBackward(event, sel = this.selection) {
371
+ if (!sel) return;
372
+ const lastNL = this.text.lastIndexOf('\n', sel.from - 1);
373
+ const deleteFrom = lastNL === -1 ? 0 : lastNL + 1;
374
+ if (deleteFrom < sel.from) {
375
+ this.delete({ index: deleteFrom, length: sel.from - deleteFrom });
376
+ this.block.commit();
377
+ tick().then(() => this.nabu.selection.setCursor(this.block, deleteFrom));
378
+ }
379
+ return true;
380
+ }
381
+
254
382
  /** @param {InputEvent} event @param {ReturnType<TextBehavior["getSelection"]>} [sel] */
255
383
  handleInsertParagraph(event, sel = this.selection) {
256
384
  if (!sel) return;
385
+ // Blocks that don't split on Enter (e.g. code) insert a literal newline instead.
386
+ if (this.block.splitsOnEnter === false) {
387
+ if (!sel.isCollapsed) this.delete({ index: sel.from, length: sel.to - sel.from });
388
+ this.insert(sel.from, "\n");
389
+ this.block.commit();
390
+ tick().then(() => this.nabu.selection.setCursor(this.block, sel.from + 1));
391
+ return true;
392
+ }
257
393
  const from = sel.from;
258
394
  const to = sel.isCollapsed ? this.text.length : sel.to ;
259
395
  const newBlock = this.block.ascend("onSplit", event, { offset: from, delta: this.container.sliceDelta(from, to)});
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Smart typography input rules.
3
+ * Currently: `-- ` → `— ` (em-dash substitution while typing).
4
+ */
5
+ export const TypographyExtension: Extension;
6
+ import { Extension } from '../../utils/extensions.js';
@@ -0,0 +1,24 @@
1
+ import { Extension } from '../../utils/extensions.js';
2
+ import { TextBehavior } from './text.behavior.svelte.js';
3
+
4
+ /**
5
+ * Smart typography input rules.
6
+ * Currently: `-- ` → `— ` (em-dash substitution while typing).
7
+ */
8
+ export const TypographyExtension = new Extension('typography', {
9
+ inputRules: [
10
+ {
11
+ pattern: /--$/,
12
+ handler(nabu, block, match, sel) {
13
+ // Only convert when the trailing space is typed, so sequences
14
+ // like `--help` keep their double dash.
15
+ if (sel.data !== ' ') return;
16
+ const behavior = block.behaviors.get('text');
17
+ if (!(behavior instanceof TextBehavior)) return;
18
+ behavior.delete({ index: sel.from - 2, length: 2 });
19
+ behavior.insert(sel.from - 2, '— ');
20
+ return true;
21
+ }
22
+ }
23
+ ]
24
+ });
@@ -1,4 +1,9 @@
1
1
  <script>
2
+ // BlockGap (the hover "+" between blocks) is intentionally disabled — it
3
+ // injected an inert div between every block, causing cursor/selection edge
4
+ // cases. The component + its `gap:click` handler remain, dormant, for a
5
+ // possible future revisit. Re-enable by rendering <BlockGap {block} /> below.
6
+
2
7
  /** @type {{ block: import('./block.svelte.js').Block }} */
3
8
  let { block } = $props();
4
9
 
@@ -8,10 +13,8 @@
8
13
  </script>
9
14
 
10
15
  {#if Component}
11
- <!-- On passe l'instance du bloc au composant spécifique -->
12
16
  <Component {block} />
13
17
  {:else}
14
- <!-- Fallback pour les types inconnus (utile en dev) -->
15
18
  <div style="color: red; border: 1px dashed red; padding: 0.5rem; margin: 0.5rem 0;" contenteditable="false">
16
19
  ⚠️ Unknown block type: <strong>{block.type}</strong>
17
20
  </div>
@@ -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
@@ -17,6 +22,9 @@
17
22
  oncopy={(e) => engine.handleCopy(e)}
18
23
  oncut={(e) => engine.handleCut(e)}
19
24
  onpaste={(e) => engine.handlePaste(e)}
25
+ oncompositionstart={(e) => engine.handleCompositionStart(e)}
26
+ oncompositionupdate={(e) => engine.handleCompositionUpdate(e)}
27
+ oncompositionend={(e) => engine.handleCompositionEnd(e)}
20
28
  >
21
29
  {#each engine.children as block (block.id)}
22
30
  <Block {block} />
@@ -31,4 +39,18 @@
31
39
  white-space: pre-wrap; /* Important pour le comportement éditeur */
32
40
  word-break: break-word;
33
41
  }
42
+
43
+ /* Placeholders — blocks expose data-placeholder when empty.
44
+ Uses ::after (not ::before) so blocks that decorate themselves with
45
+ ::before (e.g. dialogue's dash) keep working. */
46
+ /* .nabu-editor :global([data-block-id][data-placeholder]) {
47
+ position: relative; /* nécessaire pour le ::after absolu */
48
+ /* } */
49
+ /* .nabu-editor :global([data-block-id][data-placeholder]:not([data-placeholder=''])::after) {
50
+ content: attr(data-placeholder);
51
+ color: var(--nb-text-placeholder, rgba(255, 255, 255, 0.25));
52
+ pointer-events: none;
53
+ position: absolute;
54
+ user-select: none;
55
+ } */
34
56
  </style>
@@ -2,7 +2,6 @@
2
2
  * @import { Nabu } from './nabu.svelte.js'
3
3
  */
4
4
 
5
- import { tick } from 'svelte';
6
5
  import { Extension } from '../utils/extensions.js';
7
6
 
8
7
  /**
@@ -11,18 +10,21 @@ import { Extension } from '../utils/extensions.js';
11
10
  * @param {string} type
12
11
  * @param {Object} [props={}]
13
12
  */
14
- function transformBlock(nabu, type, props = {}) {
15
- console.log(`Transforming block to type "${type}" with props`, props);
13
+ async function transformBlock(nabu, type, props = {}) {
16
14
  const block = nabu.selection.anchorBlock;
17
15
  if (!block) return;
18
16
  const blockId = block.id;
19
17
  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 } });
18
+ await nabu.transact(() => {
19
+ const data = /** @type {any} */ (block.node.data);
20
+ data.set("type", type);
21
+ for (const [key, value] of Object.entries(props)) {
22
+ data.set(key, value);
23
+ }
25
24
  });
25
+ // After the commit, updateRoots() replaces the block instance — fetch the new one.
26
+ const newBlock = nabu.blocks.get(blockId);
27
+ if (newBlock) nabu.focus({ from: { block: newBlock, offset }, to: { block: newBlock, offset } });
26
28
  }
27
29
 
28
30
  export const BlockExtension = new Extension('block', {
@@ -14,6 +14,15 @@ export class Block {
14
14
  /** @type {SvelteMap<string, function(any): void>} */
15
15
  serializers: SvelteMap<string, (arg0: any) => void>;
16
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>;
17
26
  selected: boolean;
18
27
  isSelectionStart: boolean;
19
28
  isSelectionEnd: boolean;
@@ -26,6 +35,19 @@ export class Block {
26
35
  direction: "forward" | "backward" | "none";
27
36
  } | null;
28
37
  positions: number;
38
+ /**
39
+ * Number of words contained in this block. Overridden by text blocks
40
+ * (own text) and containers (sum of children) so that parents can
41
+ * aggregate without re-traversing the whole subtree.
42
+ * @type {number}
43
+ */
44
+ wordCount: number;
45
+ /** When false, Enter inserts a literal "\n" in place instead of splitting the block. @type {boolean} */
46
+ splitsOnEnter: boolean;
47
+ /** When false, onBeforeInput markdown-shortcut hooks and inputRules are skipped in this block. @type {boolean} */
48
+ transformsEnabled: boolean;
49
+ /** When false, inline marks (bold/italic/...) are never applied to this block. @type {boolean} */
50
+ marksEnabled: boolean;
29
51
  /**
30
52
  * Resolve a document-local offset to { block, offset } within this block.
31
53
  * Leaf blocks with text return themselves directly.
@@ -162,3 +184,5 @@ import type { Nabu } from "./nabu.svelte.js";
162
184
  import type { NabuNode } from "./nabu.svelte.js";
163
185
  import type { MegaBlock } from "./megablock.svelte.js";
164
186
  import { SvelteMap } from "svelte/reactivity";
187
+ import { Variable } from "./variable.svelte.js";
188
+ import type { VariableInit } from "./variable.svelte.js";
@@ -1,10 +1,12 @@
1
1
  /**
2
2
  * @import { Nabu, NabuNode } from "./nabu.svelte.js";
3
3
  * @import { MegaBlock } from "./megablock.svelte.js";
4
+ * @import { VariableInit } from "./variable.svelte.js";
4
5
  */
5
6
 
6
7
  import { tick } from "svelte";
7
8
  import { SvelteMap, SvelteSet } from "svelte/reactivity";
9
+ import { Variable } from "./variable.svelte.js";
8
10
 
9
11
 
10
12
  export class Block {
@@ -31,6 +33,21 @@ export class Block {
31
33
  /** @type {SvelteMap<string, function(any): void>} */
32
34
  serializers = new SvelteMap();
33
35
  behaviors = new SvelteMap();
36
+ /** @type {SvelteMap<string, Variable<this>>} */
37
+ variables = new SvelteMap();
38
+
39
+ /**
40
+ * Declare a variable on this block, which can be used in serializers and behaviors.
41
+ * @param {string} name
42
+ * @param {VariableInit<this>} init
43
+ * @returns {Variable<this>}
44
+ */
45
+ var(name, init) {
46
+ const variable = new Variable(this, name, init);
47
+ return variable;
48
+ }
49
+
50
+
34
51
  selected = $state(false);
35
52
  isSelectionStart = $state(false);
36
53
  isSelectionEnd = $state(false);
@@ -47,6 +64,24 @@ export class Block {
47
64
 
48
65
  positions = $derived(0);
49
66
 
67
+ /**
68
+ * Number of words contained in this block. Overridden by text blocks
69
+ * (own text) and containers (sum of children) so that parents can
70
+ * aggregate without re-traversing the whole subtree.
71
+ * @type {number}
72
+ */
73
+ wordCount = $derived(0);
74
+
75
+ // ── Capability flags ─────────────────────────────────────────────────────
76
+ // Declarative opt-outs respected centrally by the engine. Default true;
77
+ // override to false in a subclass (e.g. CodeBlock) to disable a behavior.
78
+ /** When false, Enter inserts a literal "\n" in place instead of splitting the block. @type {boolean} */
79
+ splitsOnEnter = true;
80
+ /** When false, onBeforeInput markdown-shortcut hooks and inputRules are skipped in this block. @type {boolean} */
81
+ transformsEnabled = true;
82
+ /** When false, inline marks (bold/italic/...) are never applied to this block. @type {boolean} */
83
+ marksEnabled = true;
84
+
50
85
  /**
51
86
  * Resolve a document-local offset to { block, offset } within this block.
52
87
  * Leaf blocks with text return themselves directly.
@@ -0,0 +1,40 @@
1
+ <script>
2
+ import RichText from '../../behaviors/text/RichText.svelte';
3
+ import { getPlaceholder } from '../../utils/placeholders.js';
4
+ /** @type {{block: import('./blockquote.svelte.js').BlockquoteBlock}}*/
5
+ let {block} = $props();
6
+ </script>
7
+
8
+ <blockquote
9
+ bind:this={block.element}
10
+ data-block-id={block.id}
11
+ data-block-type="blockquote"
12
+ data-placeholder={block.text ? '' : getPlaceholder('blockquote')}
13
+ class="nabu-blockquote"
14
+ class:debug={block.nabu.debugging}
15
+ class:selected={block.selected}
16
+ class:first={block.isSelectionStart}
17
+ class:last={block.isSelectionEnd}
18
+ ><RichText delta={block.delta} /></blockquote>
19
+
20
+ <style>
21
+ .nabu-blockquote {
22
+ white-space: pre-wrap;
23
+ margin: 0 0 0.5em 0;
24
+ padding: 0.25em 0 0.25em 0.875em;
25
+ border-left: 3px solid var(--nb-border, rgba(255, 255, 255, 0.25));
26
+ color: var(--nb-text-secondary, inherit);
27
+
28
+ &.debug{
29
+ &.selected {
30
+ background-color: rgba(59, 130, 246, 0.25);
31
+ }
32
+ &.first {
33
+ border-top: 1px solid rgba(59, 130, 246, 0.5);
34
+ }
35
+ &.last {
36
+ border-bottom: 1px solid rgba(59, 130, 246, 0.5);
37
+ }
38
+ }
39
+ }
40
+ </style>
@@ -0,0 +1,11 @@
1
+ export default Blockquote;
2
+ type Blockquote = {
3
+ $on?(type: string, callback: (e: any) => void): () => void;
4
+ $set?(props: Partial<$$ComponentProps>): void;
5
+ };
6
+ declare const Blockquote: import("svelte").Component<{
7
+ block: import("./blockquote.svelte.js").BlockquoteBlock;
8
+ }, {}, "">;
9
+ type $$ComponentProps = {
10
+ block: import("./blockquote.svelte.js").BlockquoteBlock;
11
+ };
@@ -0,0 +1 @@
1
+ export const BlockquoteExtension: import("../..").Extension;
@@ -0,0 +1,71 @@
1
+ import { tick } from "svelte";
2
+ import { TextBehavior } from "../../behaviors/text";
3
+ import { extension } from "../../utils/extensions";
4
+ import { BlockquoteBlock } from "./blockquote.svelte.js";
5
+ import BlockquoteComponent from "./Blockquote.svelte";
6
+
7
+ /**
8
+ * @import { Nabu, Block } from "..";
9
+ */
10
+
11
+ /**
12
+ * Markdown shortcut + de-transformation hooks for blockquotes:
13
+ * - `> ` at the start of a block → transform into blockquote
14
+ * - Backspace at the start of a blockquote → transform back into paragraph
15
+ * - Enter in an empty blockquote → exit the quote (becomes a paragraph)
16
+ *
17
+ * @param {Nabu} nabu @param {InputEvent} event @param {Block} block
18
+ */
19
+ const onBeforeInput = (nabu, event, block) => {
20
+ const { BREAK, CONTINUE } = nabu;
21
+
22
+ const behavior = block.behaviors.get("text");
23
+ if (!behavior || !(behavior instanceof TextBehavior)) return CONTINUE;
24
+ const sel = behavior.selection;
25
+ if (!sel || !sel.isCollapsed) return CONTINUE;
26
+
27
+ /** @param {string} type */
28
+ const transformAndFocus = (type) => {
29
+ const blockId = block.node.id.toString();
30
+ block.transformTo(type);
31
+ tick().then(() => {
32
+ const fresh = nabu.blocks.get(blockId);
33
+ if (fresh) fresh.focus({ start: 0, end: 0 });
34
+ });
35
+ return BREAK;
36
+ };
37
+
38
+ if (block instanceof BlockquoteBlock) {
39
+ // Backspace at the very start → back to paragraph
40
+ if (event.inputType === "deleteContentBackward" && sel.from === 0) {
41
+ return transformAndFocus("paragraph");
42
+ }
43
+ // Enter in an empty blockquote → exit the quote
44
+ if (event.inputType === "insertParagraph" && behavior.text.length === 0) {
45
+ return transformAndFocus("paragraph");
46
+ }
47
+ return CONTINUE;
48
+ }
49
+
50
+ // `> ` markdown shortcut (typed space after a lone ">" at block start)
51
+ if (event.inputType === "insertText" && event.data === " ") {
52
+ const textBeforeCursor = behavior.text.slice(0, sel.from);
53
+ if (textBeforeCursor !== ">") return CONTINUE;
54
+ behavior.delete({ index: 0, length: 1 });
55
+ return transformAndFocus("blockquote");
56
+ }
57
+
58
+ return CONTINUE;
59
+ };
60
+
61
+ export const BlockquoteExtension = extension("blockquote", {
62
+ // @ts-ignore — static-side variance on create() (same pattern as Paragraph/Heading)
63
+ block: BlockquoteBlock,
64
+ component: BlockquoteComponent,
65
+ actions: {
66
+ 'blockquote': (nabu) => nabu.exec('block:transform', { type: 'blockquote' }),
67
+ },
68
+ hooks: {
69
+ onBeforeInput,
70
+ }
71
+ });