@aionbuilders/nabu 0.1.0-alpha.9 → 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 (170) 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/Nabu.svelte +22 -0
  14. package/dist/blocks/block.extension.js +10 -8
  15. package/dist/blocks/block.svelte.d.ts +24 -0
  16. package/dist/blocks/block.svelte.js +35 -0
  17. package/dist/blocks/blockquote/Blockquote.svelte +40 -0
  18. package/dist/blocks/blockquote/Blockquote.svelte.d.ts +11 -0
  19. package/dist/blocks/blockquote/blockquote.extension.d.ts +1 -0
  20. package/dist/blocks/blockquote/blockquote.extension.js +71 -0
  21. package/dist/blocks/blockquote/blockquote.svelte.d.ts +55 -0
  22. package/dist/blocks/blockquote/blockquote.svelte.js +91 -0
  23. package/dist/blocks/blockquote/index.d.ts +3 -0
  24. package/dist/blocks/blockquote/index.js +3 -0
  25. package/dist/blocks/callout/Callout.svelte +69 -0
  26. package/dist/blocks/callout/Callout.svelte.d.ts +11 -0
  27. package/dist/blocks/callout/callout.extension.d.ts +1 -0
  28. package/dist/blocks/callout/callout.extension.js +68 -0
  29. package/dist/blocks/callout/callout.svelte.d.ts +78 -0
  30. package/dist/blocks/callout/callout.svelte.js +184 -0
  31. package/dist/blocks/callout/index.d.ts +3 -0
  32. package/dist/blocks/callout/index.js +3 -0
  33. package/dist/blocks/code/Code.svelte +85 -0
  34. package/dist/blocks/code/Code.svelte.d.ts +11 -0
  35. package/dist/blocks/code/code.extension.d.ts +1 -0
  36. package/dist/blocks/code/code.extension.js +68 -0
  37. package/dist/blocks/code/code.svelte.d.ts +61 -0
  38. package/dist/blocks/code/code.svelte.js +221 -0
  39. package/dist/blocks/code/index.d.ts +3 -0
  40. package/dist/blocks/code/index.js +3 -0
  41. package/dist/blocks/dialogue/Dialogue.svelte +2 -0
  42. package/dist/blocks/dialogue/dialogue.svelte.d.ts +3 -21
  43. package/dist/blocks/dialogue/dialogue.svelte.js +2 -50
  44. package/dist/blocks/divider/Divider.svelte +28 -0
  45. package/dist/blocks/divider/Divider.svelte.d.ts +11 -0
  46. package/dist/blocks/divider/divider.extension.d.ts +1 -0
  47. package/dist/blocks/divider/divider.extension.js +87 -0
  48. package/dist/blocks/divider/divider.svelte.d.ts +49 -0
  49. package/dist/blocks/divider/divider.svelte.js +84 -0
  50. package/dist/blocks/divider/index.d.ts +3 -0
  51. package/dist/blocks/divider/index.js +3 -0
  52. package/dist/blocks/file/File.svelte +49 -0
  53. package/dist/blocks/file/File.svelte.d.ts +11 -0
  54. package/dist/blocks/file/file.extension.d.ts +1 -0
  55. package/dist/blocks/file/file.extension.js +19 -0
  56. package/dist/blocks/file/file.svelte.d.ts +29 -0
  57. package/dist/blocks/file/file.svelte.js +71 -0
  58. package/dist/blocks/file/index.d.ts +3 -0
  59. package/dist/blocks/file/index.js +3 -0
  60. package/dist/blocks/heading/Heading.svelte +3 -1
  61. package/dist/blocks/heading/heading.svelte.d.ts +4 -23
  62. package/dist/blocks/heading/heading.svelte.js +4 -47
  63. package/dist/blocks/image/Image.svelte +38 -0
  64. package/dist/blocks/image/Image.svelte.d.ts +11 -0
  65. package/dist/blocks/image/image.extension.d.ts +2 -0
  66. package/dist/blocks/image/image.extension.js +26 -0
  67. package/dist/blocks/image/image.svelte.d.ts +50 -0
  68. package/dist/blocks/image/image.svelte.js +92 -0
  69. package/dist/blocks/image/index.d.ts +3 -0
  70. package/dist/blocks/image/index.js +3 -0
  71. package/dist/blocks/index.d.ts +10 -0
  72. package/dist/blocks/index.js +10 -0
  73. package/dist/blocks/list/List.svelte +8 -2
  74. package/dist/blocks/list/ListItem.svelte +43 -7
  75. package/dist/blocks/list/hooks/onBeforeInput.hook.js +15 -2
  76. package/dist/blocks/list/index.d.ts +1 -0
  77. package/dist/blocks/list/index.js +1 -0
  78. package/dist/blocks/list/list-item.svelte.d.ts +14 -2
  79. package/dist/blocks/list/list-item.svelte.js +58 -7
  80. package/dist/blocks/list/list.behavior.svelte.d.ts +4 -4
  81. package/dist/blocks/list/list.behavior.svelte.js +5 -5
  82. package/dist/blocks/list/list.svelte.d.ts +13 -6
  83. package/dist/blocks/list/list.svelte.js +24 -6
  84. package/dist/blocks/list/todos.svelte.d.ts +35 -0
  85. package/dist/blocks/list/todos.svelte.js +76 -0
  86. package/dist/blocks/media/index.d.ts +2 -0
  87. package/dist/blocks/media/index.js +2 -0
  88. package/dist/blocks/media/media-block.svelte.d.ts +32 -0
  89. package/dist/blocks/media/media-block.svelte.js +69 -0
  90. package/dist/blocks/media/media.helpers.d.ts +19 -0
  91. package/dist/blocks/media/media.helpers.js +51 -0
  92. package/dist/blocks/megablock.svelte.js +1 -0
  93. package/dist/blocks/nabu.svelte.d.ts +94 -16
  94. package/dist/blocks/nabu.svelte.js +254 -3
  95. package/dist/blocks/paragraph/Paragraph.svelte +2 -0
  96. package/dist/blocks/paragraph/index.js +0 -3
  97. package/dist/blocks/paragraph/paragraph.svelte.d.ts +3 -21
  98. package/dist/blocks/paragraph/paragraph.svelte.js +4 -53
  99. package/dist/blocks/selection.svelte.js +11 -1
  100. package/dist/blocks/text-block.svelte.d.ts +49 -0
  101. package/dist/blocks/text-block.svelte.js +74 -0
  102. package/dist/blocks/toggle/Toggle.svelte +84 -0
  103. package/dist/blocks/toggle/Toggle.svelte.d.ts +11 -0
  104. package/dist/blocks/toggle/index.d.ts +3 -0
  105. package/dist/blocks/toggle/index.js +3 -0
  106. package/dist/blocks/toggle/toggle.extension.d.ts +1 -0
  107. package/dist/blocks/toggle/toggle.extension.js +12 -0
  108. package/dist/blocks/toggle/toggle.svelte.d.ts +88 -0
  109. package/dist/blocks/toggle/toggle.svelte.js +252 -0
  110. package/dist/blocks/variable.svelte.d.ts +47 -0
  111. package/dist/blocks/variable.svelte.js +51 -0
  112. package/dist/blocks/video/Video.svelte +39 -0
  113. package/dist/blocks/video/Video.svelte.d.ts +11 -0
  114. package/dist/blocks/video/index.d.ts +3 -0
  115. package/dist/blocks/video/index.js +3 -0
  116. package/dist/blocks/video/video.extension.d.ts +1 -0
  117. package/dist/blocks/video/video.extension.js +19 -0
  118. package/dist/blocks/video/video.svelte.d.ts +28 -0
  119. package/dist/blocks/video/video.svelte.js +71 -0
  120. package/dist/blocks/zone/TemplateEditor.svelte +308 -0
  121. package/dist/blocks/zone/TemplateEditor.svelte.d.ts +11 -0
  122. package/dist/blocks/zone/Zone.svelte +158 -144
  123. package/dist/blocks/zone/ZoneEditor.svelte +289 -0
  124. package/dist/blocks/zone/ZoneEditor.svelte.d.ts +11 -0
  125. package/dist/blocks/zone/ZoneTemplatesList.svelte +188 -0
  126. package/dist/blocks/zone/ZoneTemplatesList.svelte.d.ts +11 -0
  127. package/dist/blocks/zone/hooks/onBeforeInput.hook.d.ts +3 -0
  128. package/dist/blocks/zone/hooks/onBeforeInput.hook.js +30 -0
  129. package/dist/blocks/zone/zone.extension.js +22 -6
  130. package/dist/blocks/zone/zone.svelte.d.ts +111 -2
  131. package/dist/blocks/zone/zone.svelte.js +304 -17
  132. package/dist/files/files.svelte.d.ts +61 -0
  133. package/dist/files/files.svelte.js +93 -0
  134. package/dist/files/index.d.ts +1 -0
  135. package/dist/files/index.js +1 -0
  136. package/dist/index.d.ts +4 -1
  137. package/dist/index.js +5 -2
  138. package/dist/nabu-theme.css +92 -0
  139. package/dist/overlays/context/Context.svelte +182 -0
  140. package/dist/overlays/context/Context.svelte.d.ts +17 -0
  141. package/dist/overlays/context/collect.d.ts +17 -0
  142. package/dist/overlays/context/collect.js +33 -0
  143. package/dist/overlays/context/context.extension.d.ts +2 -0
  144. package/dist/overlays/context/context.extension.js +8 -0
  145. package/dist/overlays/context/context.svelte.d.ts +82 -0
  146. package/dist/overlays/context/context.svelte.js +159 -0
  147. package/dist/overlays/context/index.d.ts +3 -0
  148. package/dist/overlays/context/index.js +3 -0
  149. package/dist/paste/html.paste.js +9 -1
  150. package/dist/presets.d.ts +3 -1
  151. package/dist/presets.js +21 -6
  152. package/dist/ui/DragHandle.svelte +141 -0
  153. package/dist/ui/DragHandle.svelte.d.ts +11 -0
  154. package/dist/ui/FloatingToolbar.svelte +117 -0
  155. package/dist/ui/FloatingToolbar.svelte.d.ts +17 -0
  156. package/dist/ui/SlashMenu.svelte +83 -0
  157. package/dist/ui/SlashMenu.svelte.d.ts +13 -0
  158. package/dist/ui/index.d.ts +4 -0
  159. package/dist/ui/index.js +4 -0
  160. package/dist/ui/slash-menu.svelte.d.ts +40 -0
  161. package/dist/ui/slash-menu.svelte.js +116 -0
  162. package/dist/utils/extensions.d.ts +54 -0
  163. package/dist/utils/extensions.js +40 -1
  164. package/dist/utils/index.d.ts +1 -0
  165. package/dist/utils/index.js +1 -0
  166. package/dist/utils/placeholders.d.ts +10 -0
  167. package/dist/utils/placeholders.js +21 -0
  168. package/dist/vite.d.ts +16 -0
  169. package/dist/vite.js +23 -0
  170. package/package.json +10 -5
@@ -0,0 +1,141 @@
1
+ <script>
2
+ import { wrapOrphan } from '../blocks/container.utils.js';
3
+ /** @type {{ engine: import('../blocks/nabu.svelte.js').Nabu }} */
4
+ let { engine } = $props();
5
+
6
+ let handle = $state({ visible: false, top: 0, left: 0, blockId: /** @type {string|null} */ (null) });
7
+ let dragging = $state(false);
8
+ let indicator = $state({ visible: false, top: 0, left: 0, width: 0 });
9
+ /** @type {{ blockId: string, position: 'before'|'after' } | null} */
10
+ let dropTarget = null;
11
+ let draggedId = /** @type {string|null} */ (null);
12
+
13
+ const HANDLE_W = 24;
14
+
15
+ /**
16
+ * Find the nearest block element under a point (skipping the dragged one + UI islands).
17
+ * @param {number} x
18
+ * @param {number} y
19
+ * @param {string|null} excludeId
20
+ * @returns {Element|null}
21
+ */
22
+ function blockElAt(x, y, excludeId) {
23
+ let el = /** @type {Element|null} */ (document.elementFromPoint(x, y));
24
+ const root = el?.closest?.('.nabu-editor');
25
+ if (!root) return null;
26
+ const b = el?.closest?.('[data-block-id]');
27
+ if (!b) return null;
28
+ if (excludeId && b.getAttribute('data-block-id') === excludeId) return null;
29
+ // don't target a UI island
30
+ if (b.closest('[data-nabu-ui]')) return null;
31
+ return b;
32
+ }
33
+
34
+ /** @param {PointerEvent} e */
35
+ function onPointerMove(e) {
36
+ if (dragging) { onDragMove(e); return; }
37
+ // The handle lives OUTSIDE the editor; while the pointer is over the handle
38
+ // itself, keep it visible — else moving onto it to grab it would hide it.
39
+ const over = document.elementFromPoint(e.clientX, e.clientY);
40
+ if (over?.closest?.('.nabu-drag-handle')) return;
41
+ const b = blockElAt(e.clientX, e.clientY, null);
42
+ if (!b) { handle.visible = false; return; }
43
+ const r = b.getBoundingClientRect();
44
+ // Flush against the block's left edge — no dead gap to cross when grabbing.
45
+ handle = { visible: true, top: r.top + 2, left: r.left - HANDLE_W, blockId: b.getAttribute('data-block-id') };
46
+ }
47
+
48
+ /** @param {PointerEvent} e */
49
+ function onHandleDown(e) {
50
+ e.preventDefault();
51
+ e.stopPropagation();
52
+ draggedId = handle.blockId;
53
+ if (!draggedId) return;
54
+ dragging = true;
55
+ // dim the dragged block
56
+ const el = document.querySelector(`[data-block-id="${cssEscape(draggedId)}"]`);
57
+ el?.classList.add('nabu-dragging');
58
+ try { /** @type {any} */ (e.target).setPointerCapture?.(e.pointerId); } catch {}
59
+ }
60
+
61
+ /** @param {PointerEvent} e */
62
+ function onDragMove(e) {
63
+ handle.visible = false;
64
+ const b = blockElAt(e.clientX, e.clientY, draggedId);
65
+ if (!b) { indicator.visible = false; dropTarget = null; return; }
66
+ const r = b.getBoundingClientRect();
67
+ const after = e.clientY > r.top + r.height / 2;
68
+ dropTarget = { blockId: /** @type {string} */ (b.getAttribute('data-block-id')), position: after ? 'after' : 'before' };
69
+ indicator = { visible: true, top: after ? r.bottom : r.top, left: r.left, width: r.width };
70
+ }
71
+
72
+ function onPointerUp() {
73
+ if (dragging) {
74
+ const el = draggedId ? document.querySelector(`[data-block-id="${cssEscape(draggedId)}"]`) : null;
75
+ el?.classList.remove('nabu-dragging');
76
+ performDrop();
77
+ }
78
+ dragging = false; draggedId = null; indicator.visible = false; dropTarget = null;
79
+ }
80
+
81
+ function performDrop() {
82
+ const id = draggedId;
83
+ if (!dropTarget || !id) return;
84
+ const dragged = engine.blocks.get(id);
85
+ const target = engine.blocks.get(dropTarget.blockId);
86
+ if (!dragged || !target || dragged === target) return;
87
+ // never drop a block into its own subtree (cycle)
88
+ if (target.parents?.some((p) => p.id === dragged.id)) return;
89
+ if (dropTarget.position === 'after') dragged.node.moveAfter(target.node);
90
+ else dragged.node.moveBefore(target.node);
91
+ // repair structural integrity (e.g. a list-item dropped at root → wrap in a list).
92
+ // No-op for blocks without a requiredParent (paragraph, heading, …).
93
+ const orphan = /** @type {any} */ (dragged);
94
+ try { wrapOrphan(engine, orphan); } catch {}
95
+ engine.commit().then(() => {
96
+ // keep the moved block focusable: move the cursor into it if it has text
97
+ const fresh = engine.blocks.get(id);
98
+ if (fresh?.behaviors?.has('text')) engine.selection.setCursor(fresh, 0);
99
+ });
100
+ }
101
+
102
+ /** Minimal CSS.escape fallback for the attribute selector. @param {string} s */
103
+ function cssEscape(s) { return (window.CSS && CSS.escape) ? CSS.escape(s) : String(s).replace(/["\\\]]/g, '\\$&'); }
104
+ </script>
105
+
106
+ <svelte:window onpointermove={onPointerMove} onpointerup={onPointerUp} />
107
+
108
+ {#if handle.visible && !dragging}
109
+ <button
110
+ class="nabu-drag-handle"
111
+ data-nabu-ui
112
+ contenteditable="false"
113
+ type="button"
114
+ aria-label="Drag to reorder"
115
+ title="Drag to reorder"
116
+ style="top: {handle.top}px; left: {handle.left}px;"
117
+ onpointerdown={onHandleDown}
118
+ >⠿</button>
119
+ {/if}
120
+
121
+ {#if indicator.visible}
122
+ <div class="nabu-drop-indicator" style="top: {indicator.top}px; left: {indicator.left}px; width: {indicator.width}px;"></div>
123
+ {/if}
124
+
125
+ <style>
126
+ .nabu-drag-handle {
127
+ position: fixed; z-index: 150; width: 24px; height: 22px;
128
+ display: flex; align-items: center; justify-content: center;
129
+ border: none; background: transparent; cursor: grab;
130
+ color: var(--nb-text-muted, rgba(255,255,255,0.35)); font-size: 14px;
131
+ border-radius: 4px; user-select: none; padding: 0; line-height: 1;
132
+ }
133
+ .nabu-drag-handle:hover { background: var(--nb-bg-surface-hover, rgba(255,255,255,0.08)); color: var(--nb-text-primary, rgba(255,255,255,0.8)); }
134
+ .nabu-drag-handle:active { cursor: grabbing; }
135
+ .nabu-drop-indicator {
136
+ position: fixed; z-index: 150; height: 2px; pointer-events: none;
137
+ background: var(--nb-accent, #4d9fff); border-radius: 2px;
138
+ box-shadow: 0 0 4px var(--nb-accent, #4d9fff);
139
+ }
140
+ :global(.nabu-dragging) { opacity: 0.4; }
141
+ </style>
@@ -0,0 +1,11 @@
1
+ export default DragHandle;
2
+ type DragHandle = {
3
+ $on?(type: string, callback: (e: any) => void): () => void;
4
+ $set?(props: Partial<$$ComponentProps>): void;
5
+ };
6
+ declare const DragHandle: import("svelte").Component<{
7
+ engine: import("../blocks/nabu.svelte.js").Nabu;
8
+ }, {}, "">;
9
+ type $$ComponentProps = {
10
+ engine: import("../blocks/nabu.svelte.js").Nabu;
11
+ };
@@ -0,0 +1,117 @@
1
+ <script>
2
+ /**
3
+ * Floating mark toolbar. Appears above the current (non-collapsed) selection.
4
+ * Positioned with `position: fixed` in viewport coordinates, so it works
5
+ * inside any layout (scrollable containers, overflow: hidden ancestors…)
6
+ * without a positioning wrapper.
7
+ *
8
+ * @type {{ engine: import('../blocks/nabu.svelte.js').Nabu }}
9
+ */
10
+ let { engine } = $props();
11
+
12
+ /** @type {HTMLElement | null} */
13
+ let toolbarEl = $state(null);
14
+ let visible = $state(false);
15
+ let position = $state({ top: 0, left: 0 });
16
+
17
+ $effect(() => {
18
+ const sel = engine.selection;
19
+ if (!sel || sel.isCollapsed) {
20
+ visible = false;
21
+ return;
22
+ }
23
+ // Position above the selection via Range.getBoundingClientRect()
24
+ const range = sel.firstRange;
25
+ if (!range) {
26
+ visible = false;
27
+ return;
28
+ }
29
+ const rect = range.getBoundingClientRect();
30
+ // Collapsed-looking rect (e.g. selection cleared mid-update) → hide
31
+ if (rect.width === 0 && rect.height === 0) {
32
+ visible = false;
33
+ return;
34
+ }
35
+ visible = true;
36
+ // Viewport coordinates — the toolbar is position: fixed
37
+ position = {
38
+ top: Math.max(8, rect.top - (toolbarEl?.offsetHeight ?? 40) - 8),
39
+ left: rect.left + rect.width / 2,
40
+ };
41
+ });
42
+
43
+ const marks = [
44
+ { key: 'bold', label: 'B', title: 'Bold (Ctrl+B)' },
45
+ { key: 'italic', label: 'I', title: 'Italic (Ctrl+I)' },
46
+ { key: 'underline', label: 'U', title: 'Underline (Ctrl+U)' },
47
+ { key: 'code', label: '<>', title: 'Code (Ctrl+E)' },
48
+ { key: 'strikethrough', label: 'S', title: 'Strikethrough (Ctrl+Shift+X)' },
49
+ ];
50
+
51
+ function toggleLink() {
52
+ if (engine.isMarkActive('link')) {
53
+ engine.exec('link:remove');
54
+ return;
55
+ }
56
+ const href = window.prompt('Link URL');
57
+ if (href) engine.exec('link:set', { href });
58
+ }
59
+ </script>
60
+
61
+ {#if visible}
62
+ <div
63
+ bind:this={toolbarEl}
64
+ class="nabu-toolbar"
65
+ style="top: {position.top}px; left: {position.left}px;"
66
+ contenteditable="false"
67
+ data-nabu-ui
68
+ >
69
+ {#each marks as mark}
70
+ <button
71
+ class="nabu-toolbar-btn"
72
+ class:active={engine.isMarkActive(mark.key)}
73
+ title={mark.title}
74
+ onmousedown={(e) => { e.preventDefault(); engine.exec('mark:toggle', { mark: mark.key }); }}
75
+ >
76
+ {mark.label}
77
+ </button>
78
+ {/each}
79
+ <button
80
+ class="nabu-toolbar-btn"
81
+ class:active={engine.isMarkActive('link')}
82
+ title="Link"
83
+ onmousedown={(e) => { e.preventDefault(); toggleLink(); }}
84
+ >
85
+ 🔗
86
+ </button>
87
+ </div>
88
+ {/if}
89
+
90
+ <style>
91
+ .nabu-toolbar {
92
+ position: fixed;
93
+ transform: translateX(-50%);
94
+ display: flex;
95
+ gap: 2px;
96
+ padding: 4px;
97
+ background: var(--nb-bg-surface, #1a1a1a);
98
+ border: 1px solid var(--nb-border, rgba(255,255,255,0.1));
99
+ border-radius: 8px;
100
+ box-shadow: 0 4px 16px rgba(0,0,0,0.3);
101
+ z-index: 100;
102
+ pointer-events: all;
103
+ }
104
+ .nabu-toolbar-btn {
105
+ padding: 4px 8px;
106
+ border: none;
107
+ background: transparent;
108
+ color: var(--nb-text-primary, rgba(255,255,255,0.85));
109
+ border-radius: 4px;
110
+ cursor: pointer;
111
+ font-size: 13px;
112
+ font-weight: 600;
113
+ transition: background 0.1s;
114
+ }
115
+ .nabu-toolbar-btn:hover { background: var(--nb-bg-surface-hover, rgba(255,255,255,0.08)); }
116
+ .nabu-toolbar-btn.active { background: var(--nb-bg-surface-active, rgba(255,255,255,0.15)); }
117
+ </style>
@@ -0,0 +1,17 @@
1
+ export default FloatingToolbar;
2
+ type FloatingToolbar = {
3
+ $on?(type: string, callback: (e: any) => void): () => void;
4
+ $set?(props: Partial<$$ComponentProps>): void;
5
+ };
6
+ declare const FloatingToolbar: import("svelte").Component<{
7
+ engine: import("../blocks/nabu.svelte.js").Nabu;
8
+ }, {}, "">;
9
+ /**
10
+ * Floating mark toolbar. Appears above the current (non-collapsed) selection.
11
+ * Positioned with `position: fixed` in viewport coordinates, so it works
12
+ * inside any layout (scrollable containers, overflow: hidden ancestors…)
13
+ * without a positioning wrapper.
14
+ */
15
+ type $$ComponentProps = {
16
+ engine: import("../blocks/nabu.svelte.js").Nabu;
17
+ };
@@ -0,0 +1,83 @@
1
+ <script>
2
+ import { SlashMenuController, DEFAULT_SLASH_COMMANDS } from './slash-menu.svelte.js';
3
+ /** @type {{ engine: import('../blocks/nabu.svelte.js').Nabu, commands?: import('./slash-menu.svelte.js').SlashCommand[] }} */
4
+ let { engine, commands = DEFAULT_SLASH_COMMANDS } = $props();
5
+ const ctrl = new SlashMenuController(engine, commands);
6
+ let pos = $state({ top: 0, left: 0 });
7
+
8
+ // Reset the highlighted item whenever the query changes.
9
+ $effect(() => { ctrl.query; ctrl.selectedIndex = 0; });
10
+
11
+ // Position the popup just below the caret when open.
12
+ $effect(() => {
13
+ if (!ctrl.open) return;
14
+ const range = engine.selection?.firstRange;
15
+ if (!range) return;
16
+ const r = range.getBoundingClientRect();
17
+ pos = { top: r.bottom + window.scrollY + 4, left: r.left + window.scrollX };
18
+ });
19
+
20
+ // Capture keyboard navigation ONLY while the menu is open (capture phase so it
21
+ // beats the editor's own keydown handler).
22
+ $effect(() => {
23
+ if (!ctrl.open) return;
24
+ /** @param {KeyboardEvent} ev */
25
+ const onKey = (ev) => {
26
+ if (ev.key === 'ArrowDown') { ev.preventDefault(); ev.stopImmediatePropagation(); ctrl.moveSelection(1); }
27
+ else if (ev.key === 'ArrowUp') { ev.preventDefault(); ev.stopImmediatePropagation(); ctrl.moveSelection(-1); }
28
+ else if (ev.key === 'Enter' || ev.key === 'Tab') { ev.preventDefault(); ev.stopImmediatePropagation(); ctrl.select(); }
29
+ else if (ev.key === 'Escape') { ev.preventDefault(); ev.stopImmediatePropagation(); ctrl.close(); }
30
+ };
31
+ document.addEventListener('keydown', onKey, true);
32
+ return () => document.removeEventListener('keydown', onKey, true);
33
+ });
34
+ </script>
35
+
36
+ {#if ctrl.open}
37
+ <div class="nabu-slash" data-nabu-ui contenteditable="false" style="top: {pos.top}px; left: {pos.left}px;">
38
+ {#each ctrl.filteredItems as item, i (item.title)}
39
+ <button
40
+ class="nabu-slash-item"
41
+ class:active={i === ctrl.selectedIndex}
42
+ onmousedown={(e) => { e.preventDefault(); ctrl.select(i); }}
43
+ onmouseenter={() => ctrl.selectedIndex = i}
44
+ >
45
+ <span class="nabu-slash-icon">{item.icon ?? ''}</span>
46
+ <span class="nabu-slash-title">{item.title}</span>
47
+ {#if item.subtitle}<span class="nabu-slash-sub">{item.subtitle}</span>{/if}
48
+ </button>
49
+ {/each}
50
+ </div>
51
+ {/if}
52
+
53
+ <style>
54
+ .nabu-slash {
55
+ position: fixed;
56
+ z-index: 200;
57
+ min-width: 220px;
58
+ max-height: 320px;
59
+ overflow-y: auto;
60
+ padding: 4px;
61
+ background: var(--nb-bg-surface, #1a1a1a);
62
+ border: 1px solid var(--nb-border, rgba(255,255,255,0.12));
63
+ border-radius: 8px;
64
+ box-shadow: 0 8px 24px rgba(0,0,0,0.35);
65
+ pointer-events: all;
66
+ }
67
+ .nabu-slash-item {
68
+ display: flex; align-items: center; gap: 8px;
69
+ width: 100%; padding: 6px 8px; border: none; background: transparent;
70
+ color: var(--nb-text-primary, rgba(255,255,255,0.85));
71
+ border-radius: 5px; cursor: pointer; text-align: left; font-size: 13px;
72
+ }
73
+ .nabu-slash-item.active { background: var(--nb-bg-surface-active, rgba(255,255,255,0.1)); }
74
+ .nabu-slash-icon {
75
+ flex-shrink: 0; width: 22px; height: 22px;
76
+ display: inline-flex; align-items: center; justify-content: center;
77
+ font-size: 12px; font-weight: 600;
78
+ color: var(--nb-text-secondary, rgba(255,255,255,0.5));
79
+ border: 1px solid var(--nb-border, rgba(255,255,255,0.1)); border-radius: 4px;
80
+ }
81
+ .nabu-slash-title { flex: 1; }
82
+ .nabu-slash-sub { color: var(--nb-text-secondary, rgba(255,255,255,0.4)); font-size: 11px; }
83
+ </style>
@@ -0,0 +1,13 @@
1
+ export default SlashMenu;
2
+ type SlashMenu = {
3
+ $on?(type: string, callback: (e: any) => void): () => void;
4
+ $set?(props: Partial<$$ComponentProps>): void;
5
+ };
6
+ declare const SlashMenu: import("svelte").Component<{
7
+ engine: import("../blocks/nabu.svelte.js").Nabu;
8
+ commands?: import("./slash-menu.svelte.js").SlashCommand[];
9
+ }, {}, "">;
10
+ type $$ComponentProps = {
11
+ engine: import("../blocks/nabu.svelte.js").Nabu;
12
+ commands?: import("./slash-menu.svelte.js").SlashCommand[];
13
+ };
@@ -0,0 +1,4 @@
1
+ export { default as FloatingToolbar } from "./FloatingToolbar.svelte";
2
+ export { default as SlashMenu } from "./SlashMenu.svelte";
3
+ export { default as DragHandle } from "./DragHandle.svelte";
4
+ export { SlashMenuController, DEFAULT_SLASH_COMMANDS } from "./slash-menu.svelte.js";
@@ -0,0 +1,4 @@
1
+ export { default as FloatingToolbar } from './FloatingToolbar.svelte';
2
+ export { default as SlashMenu } from './SlashMenu.svelte';
3
+ export { default as DragHandle } from './DragHandle.svelte';
4
+ export { SlashMenuController, DEFAULT_SLASH_COMMANDS } from './slash-menu.svelte.js';
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @typedef {Object} SlashCommand
3
+ * @property {string} title
4
+ * @property {string} [subtitle]
5
+ * @property {string} [icon]
6
+ * @property {string[]} [aliases]
7
+ * @property {(engine: import('../blocks/nabu.svelte.js').Nabu) => void} run
8
+ */
9
+ /** @type {SlashCommand[]} */
10
+ export const DEFAULT_SLASH_COMMANDS: SlashCommand[];
11
+ export class SlashMenuController {
12
+ /** @param {import('../blocks/nabu.svelte.js').Nabu} engine @param {SlashCommand[]} commands */
13
+ constructor(engine: import("../blocks/nabu.svelte.js").Nabu, commands: SlashCommand[]);
14
+ commands: SlashCommand[];
15
+ /** @type {{ block: any, slashStart: number, cursor: number, query: string } | null} */
16
+ match: {
17
+ block: any;
18
+ slashStart: number;
19
+ cursor: number;
20
+ query: string;
21
+ } | null;
22
+ query: string;
23
+ filteredItems: SlashCommand[];
24
+ open: boolean;
25
+ selectedIndex: number;
26
+ /** @param {number} delta */
27
+ moveSelection(delta: number): void;
28
+ /** Run the command at the given index (default: the selected one). @param {number} [index] */
29
+ select(index?: number): Promise<void>;
30
+ /** Dismiss the menu for the current trigger position (Escape). */
31
+ close(): void;
32
+ #private;
33
+ }
34
+ export type SlashCommand = {
35
+ title: string;
36
+ subtitle?: string | undefined;
37
+ icon?: string | undefined;
38
+ aliases?: string[] | undefined;
39
+ run: (engine: import("../blocks/nabu.svelte.js").Nabu) => void;
40
+ };
@@ -0,0 +1,116 @@
1
+ import { List } from '../blocks/list/list.svelte.js';
2
+ import { ListItem } from '../blocks/list/list-item.svelte.js';
3
+
4
+ /**
5
+ * Transform the current anchor block (a paragraph) into a list of the given type.
6
+ * Mirrors the "- "/"1. " creation flow. Isolated here so the slash menu doesn't
7
+ * depend on internal list actions.
8
+ * @param {import('../blocks/nabu.svelte.js').Nabu} nabu
9
+ * @param {"bullet"|"ordered"|"todo"} listType
10
+ */
11
+ function createListInBlock(nabu, listType) {
12
+ const block = nabu.selection.anchorBlock;
13
+ if (!block) return;
14
+ const beh = block.behaviors?.get('text');
15
+ const delta = beh ? beh.container.toDelta() : [];
16
+ const parentNode = block.node.parent();
17
+ const parentId = parentNode?.id.toString() ?? null;
18
+ const index = block.node.index();
19
+ const list = List.create(nabu, 'list', { listType }, parentId, index);
20
+ const itemProps = listType === 'todo' ? { delta, checked: false } : { delta };
21
+ const item = ListItem.create(nabu, 'list-item', itemProps, list.node.id.toString());
22
+ block.destroy();
23
+ nabu.commit().then(() => { const f = nabu.blocks.get(item.id); if (f) f.focus({ start: 0, end: 0 }); });
24
+ }
25
+
26
+ /**
27
+ * @typedef {Object} SlashCommand
28
+ * @property {string} title
29
+ * @property {string} [subtitle]
30
+ * @property {string} [icon]
31
+ * @property {string[]} [aliases]
32
+ * @property {(engine: import('../blocks/nabu.svelte.js').Nabu) => void} run
33
+ */
34
+
35
+ /** @type {SlashCommand[]} */
36
+ export const DEFAULT_SLASH_COMMANDS = [
37
+ { title: 'Text', icon: '¶', aliases: ['paragraph','p','plain'], run: (e) => e.exec('paragraph') },
38
+ { title: 'Heading 1', icon: 'H1', aliases: ['h1','title','heading'], run: (e) => e.exec('heading:1') },
39
+ { title: 'Heading 2', icon: 'H2', aliases: ['h2','subtitle'], run: (e) => e.exec('heading:2') },
40
+ { title: 'Heading 3', icon: 'H3', aliases: ['h3'], run: (e) => e.exec('heading:3') },
41
+ { title: 'Bulleted list', icon: '•', aliases: ['bullet','ul','list'], run: (e) => createListInBlock(e, 'bullet') },
42
+ { title: 'Numbered list', icon: '1.', aliases: ['numbered','ol','ordered'], run: (e) => createListInBlock(e, 'ordered') },
43
+ { title: 'To-do list', icon: '☑', aliases: ['todo','task','checkbox','checklist'], run: (e) => createListInBlock(e, 'todo') },
44
+ { title: 'Code', icon: '<>', aliases: ['code','snippet'], run: (e) => e.exec('code') },
45
+ { title: 'Callout', icon: '💡', aliases: ['callout','note','info','warning'], run: (e) => e.exec('callout') },
46
+ { title: 'Quote', icon: '"', aliases: ['quote','blockquote'], run: (e) => e.exec('blockquote') },
47
+ { title: 'Toggle', icon: '▸', aliases: ['toggle','collapse','details'], run: (e) => e.exec('toggle') },
48
+ { title: 'Divider', icon: '—', aliases: ['divider','hr','separator','rule'], run: (e) => e.exec('divider:insert') },
49
+ { title: 'Image', icon: '🖼', aliases: ['image','img','photo','picture'], run: (e) => e.exec('image:pick') },
50
+ { title: 'Video', icon: '🎬', aliases: ['video','movie','mp4'], run: (e) => e.exec('video:pick') },
51
+ { title: 'File', icon: '📎', aliases: ['file','attachment','upload','document'], run: (e) => e.exec('file:pick') },
52
+ ];
53
+
54
+ export class SlashMenuController {
55
+ /** @type {import('../blocks/nabu.svelte.js').Nabu} */
56
+ #engine;
57
+ /** @param {import('../blocks/nabu.svelte.js').Nabu} engine @param {SlashCommand[]} commands */
58
+ constructor(engine, commands) { this.#engine = engine; this.commands = commands ?? DEFAULT_SLASH_COMMANDS; }
59
+
60
+ /** @type {{ block: any, slashStart: number, cursor: number, query: string } | null} */
61
+ match = $derived.by(() => {
62
+ const sel = this.#engine.selection;
63
+ const block = sel?.anchorBlock;
64
+ if (!block) return null;
65
+ const beh = block.behaviors?.get('text');
66
+ const bsel = beh?.selection;
67
+ if (!beh || !bsel || !bsel.isCollapsed) return null;
68
+ const before = beh.text.slice(0, bsel.from);
69
+ // "/" at block start OR after whitespace, then non-space chars = the query
70
+ const m = before.match(/(?:^|\s)\/(\S*)$/);
71
+ if (!m) return null;
72
+ const query = m[1];
73
+ return { block, slashStart: bsel.from - query.length - 1, cursor: bsel.from, query };
74
+ });
75
+
76
+ query = $derived(this.match?.query ?? '');
77
+
78
+ filteredItems = $derived.by(() => {
79
+ if (!this.match) return [];
80
+ const q = this.query.toLowerCase();
81
+ if (!q) return this.commands;
82
+ return this.commands.filter(c =>
83
+ c.title.toLowerCase().includes(q) ||
84
+ (c.aliases || []).some(a => a.toLowerCase().includes(q)));
85
+ });
86
+
87
+ /** @type {number | null} slashStart of a dismissed (Escape) trigger */
88
+ #dismissedKey = $state(null);
89
+
90
+ open = $derived(!!this.match && this.filteredItems.length > 0 && this.match.slashStart !== this.#dismissedKey);
91
+
92
+ selectedIndex = $state(0);
93
+
94
+ /** @param {number} delta */
95
+ moveSelection(delta) {
96
+ const n = this.filteredItems.length;
97
+ if (!n) return;
98
+ this.selectedIndex = (this.selectedIndex + delta + n) % n;
99
+ }
100
+
101
+ /** Run the command at the given index (default: the selected one). @param {number} [index] */
102
+ async select(index) {
103
+ const item = this.filteredItems[index ?? this.selectedIndex];
104
+ const m = this.match;
105
+ if (!item || !m) return;
106
+ const beh = m.block.behaviors?.get('text');
107
+ if (beh) beh.delete({ index: m.slashStart, length: m.cursor - m.slashStart });
108
+ await this.#engine.commit();
109
+ this.#engine.selection.setCursor(m.block, m.slashStart);
110
+ this.close();
111
+ item.run(this.#engine);
112
+ }
113
+
114
+ /** Dismiss the menu for the current trigger position (Escape). */
115
+ close() { this.#dismissedKey = this.match?.slashStart ?? null; }
116
+ }
@@ -1,3 +1,19 @@
1
+ /**
2
+ * Convert a `serialize('json')` document export back into PasteBlocks.
3
+ * The json format stores text as Slate-like runs (`{text, ...marks}`);
4
+ * PasteBlocks store Loro deltas (`{insert, attributes}`).
5
+ * @param {Array<{type: string, props?: Record<string, any>, content?: Array<{text: string, [mark: string]: any}>, children?: any[]}>} blocks
6
+ * @returns {PasteBlock[]}
7
+ */
8
+ export function jsonToPasteBlocks(blocks: Array<{
9
+ type: string;
10
+ props?: Record<string, any>;
11
+ content?: Array<{
12
+ text: string;
13
+ [mark: string]: any;
14
+ }>;
15
+ children?: any[];
16
+ }>): PasteBlock[];
1
17
  /**
2
18
  * @import { Nabu, Block } from '../blocks'
3
19
  */
@@ -19,6 +35,14 @@
19
35
  * @property {number} [priority] - Plus haut = appelé en premier (default: 0)
20
36
  * @property {(raw: string, nabu: Nabu) => PasteFragment | null} interpret
21
37
  */
38
+ /**
39
+ * @typedef {Object} InputRule
40
+ * @property {RegExp} pattern - Testé sur le texte AVANT le curseur (avec l'inputType "insertText")
41
+ * @property {string} [inputType] - Défaut: "insertText"
42
+ * @property {(nabu: Nabu, block: Block, match: RegExpMatchArray, sel: {from: number, to: number, isCollapsed: boolean, data: string}) => boolean | void} handler
43
+ * - `sel.data` est le texte en cours d'insertion (event.data).
44
+ * - Retourne true pour BREAK. NE PAS appeler commit() dans le handler.
45
+ */
22
46
  /**
23
47
  * @typedef {{
24
48
  * onInit: function(Nabu): void,
@@ -41,6 +65,9 @@
41
65
  * @property {Record<string, (nabu: Nabu) => any>} [serializers]
42
66
  * @property {Record<string, (nabu: Nabu, data: any, topic: string) => any>} [actions]
43
67
  * @property {PasteInterpreter[]} [pasteInterpreters]
68
+ * @property {InputRule[]} [inputRules]
69
+ * Declarative text-replacement rules applied while typing (e.g. markdown
70
+ * shortcuts, smart typography). Tested against the text before the cursor.
44
71
  * @property {Record<string, { expand: 'after' | 'before' | 'both' | 'none' }>} [loroTextStyles]
45
72
  * Loro text style configuration required by this extension.
46
73
  * Passed to `LoroDoc.configTextStyle()` during Nabu initialization so that
@@ -60,6 +87,8 @@ export class Extension {
60
87
  actions: Record<string, (nabu: Nabu, data: any, topic: string) => any>;
61
88
  /** @type {PasteInterpreter[]} */
62
89
  pasteInterpreters: PasteInterpreter[];
90
+ /** @type {InputRule[]} */
91
+ inputRules: InputRule[];
63
92
  /** @type {Record<string, { expand: 'after' | 'before' | 'both' | 'none' }>} */
64
93
  loroTextStyles: Record<string, {
65
94
  expand: "after" | "before" | "both" | "none";
@@ -87,6 +116,26 @@ export type PasteInterpreter = {
87
116
  priority?: number | undefined;
88
117
  interpret: (raw: string, nabu: Nabu) => PasteFragment | null;
89
118
  };
119
+ export type InputRule = {
120
+ /**
121
+ * - Testé sur le texte AVANT le curseur (avec l'inputType "insertText")
122
+ */
123
+ pattern: RegExp;
124
+ /**
125
+ * - Défaut: "insertText"
126
+ */
127
+ inputType?: string | undefined;
128
+ /**
129
+ * - `sel.data` est le texte en cours d'insertion (event.data).
130
+ * - Retourne true pour BREAK. NE PAS appeler commit() dans le handler.
131
+ */
132
+ handler: (nabu: Nabu, block: Block, match: RegExpMatchArray, sel: {
133
+ from: number;
134
+ to: number;
135
+ isCollapsed: boolean;
136
+ data: string;
137
+ }) => boolean | void;
138
+ };
90
139
  export type ExtensionHooks = {
91
140
  onInit: (arg0: Nabu) => void;
92
141
  onBlockCreate: (arg0: Nabu, arg1: Block) => void;
@@ -119,6 +168,11 @@ export type ExtensionInit = {
119
168
  serializers?: Record<string, (nabu: Nabu) => any> | undefined;
120
169
  actions?: Record<string, (nabu: Nabu, data: any, topic: string) => any> | undefined;
121
170
  pasteInterpreters?: PasteInterpreter[] | undefined;
171
+ /**
172
+ * Declarative text-replacement rules applied while typing (e.g. markdown
173
+ * shortcuts, smart typography). Tested against the text before the cursor.
174
+ */
175
+ inputRules?: InputRule[] | undefined;
122
176
  /**
123
177
  * Loro text style configuration required by this extension.
124
178
  * Passed to `LoroDoc.configTextStyle()` during Nabu initialization so that