@aionbuilders/nabu 0.1.0-alpha.3 → 0.1.0-alpha.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -1
- package/dist/behaviors/text/rich-text.extension.js +87 -30
- package/dist/behaviors/text/text.behavior.svelte.js +14 -1
- package/dist/blocks/block.extension.d.ts +2 -0
- package/dist/blocks/block.extension.js +31 -0
- package/dist/blocks/block.svelte.d.ts +10 -2
- package/dist/blocks/block.svelte.js +12 -6
- package/dist/blocks/container.utils.d.ts +0 -4
- package/dist/blocks/container.utils.js +48 -1
- package/dist/blocks/dialogue/Dialogue.svelte +46 -0
- package/dist/blocks/dialogue/Dialogue.svelte.d.ts +11 -0
- package/dist/blocks/dialogue/dialogue.svelte.d.ts +41 -0
- package/dist/blocks/dialogue/dialogue.svelte.js +85 -0
- package/dist/blocks/dialogue/hooks/onBeforeInput.hook.d.ts +3 -0
- package/dist/blocks/dialogue/hooks/onBeforeInput.hook.js +52 -0
- package/dist/blocks/dialogue/index.d.ts +7 -0
- package/dist/blocks/dialogue/index.js +51 -0
- package/dist/blocks/heading/index.js +8 -0
- package/dist/blocks/index.d.ts +2 -0
- package/dist/blocks/index.js +2 -0
- package/dist/blocks/list/hooks/onBeforeInput.hook.d.ts +3 -0
- package/dist/blocks/list/hooks/onBeforeInput.hook.js +77 -0
- package/dist/blocks/list/index.js +15 -1
- package/dist/blocks/list/list-item.svelte.d.ts +24 -0
- package/dist/blocks/list/list-item.svelte.js +88 -83
- package/dist/blocks/list/list.svelte.js +1 -1
- package/dist/blocks/megablock.svelte.d.ts +1 -3
- package/dist/blocks/megablock.svelte.js +8 -14
- package/dist/blocks/nabu.svelte.d.ts +8 -0
- package/dist/blocks/nabu.svelte.js +25 -1
- package/dist/blocks/paragraph/index.js +3 -0
- package/dist/blocks/selection.svelte.js +6 -1
- package/dist/persistence/indexeddb.d.ts +19 -0
- package/dist/persistence/indexeddb.js +71 -0
- package/dist/persistence/persistence.d.ts +48 -0
- package/dist/persistence/persistence.js +80 -0
- package/dist/presets.d.ts +1 -0
- package/dist/presets.js +8 -1
- package/dist/utils/extensions.d.ts +4 -0
- package/dist/utils/extensions.js +3 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -66,6 +66,7 @@ Without this setup, Nabu will fail to load.
|
|
|
66
66
|
HeadingExtension,
|
|
67
67
|
ListExtension,
|
|
68
68
|
ListItemExtension,
|
|
69
|
+
DialogueExtension,
|
|
69
70
|
RichTextExtension
|
|
70
71
|
} from '@aionbuilders/nabu';
|
|
71
72
|
|
|
@@ -75,6 +76,7 @@ Without this setup, Nabu will fail to load.
|
|
|
75
76
|
HeadingExtension,
|
|
76
77
|
ListExtension,
|
|
77
78
|
ListItemExtension,
|
|
79
|
+
DialogueExtension,
|
|
78
80
|
RichTextExtension
|
|
79
81
|
]
|
|
80
82
|
});
|
|
@@ -105,7 +107,7 @@ const engine = createEditor({
|
|
|
105
107
|
| `MinimalPreset` | Paragraph only |
|
|
106
108
|
| `TextPreset` | Paragraph + rich text marks |
|
|
107
109
|
| `DocumentPreset` | Paragraph + Headings + rich text |
|
|
108
|
-
| `FullPreset` | All built-in blocks + rich text |
|
|
110
|
+
| `FullPreset` | All built-in blocks + rich text (including Dialogue) |
|
|
109
111
|
|
|
110
112
|
### Factory functions
|
|
111
113
|
|
|
@@ -136,8 +138,17 @@ All block types and behaviors are provided as extensions. Pass them to the `Nabu
|
|
|
136
138
|
| `HeadingExtension` | `heading` block (h1–h6) | `# ` → h1, `## ` → h2, ... |
|
|
137
139
|
| `ListExtension` | `list` container (bullet/ordered) | — |
|
|
138
140
|
| `ListItemExtension` | `list-item` block | `Tab` indent, `Shift+Tab` unindent |
|
|
141
|
+
| `DialogueExtension` | `dialogue` block | `-- ` → dialogue, `Backspace` at start → paragraph |
|
|
139
142
|
| `RichTextExtension` | Inline marks (bold, italic...) | `Ctrl+B/I/U/E`, `Ctrl+Shift+X` |
|
|
140
143
|
|
|
144
|
+
### Typographic substitutions
|
|
145
|
+
|
|
146
|
+
When `RichTextExtension` or any text-bearing block is active, Nabu automatically applies typographic substitutions as you type:
|
|
147
|
+
|
|
148
|
+
| Input | Output | Description |
|
|
149
|
+
|---|---|---|
|
|
150
|
+
| `--` + space | `— ` | Em-dash (French dialogue convention) |
|
|
151
|
+
|
|
141
152
|
## Programmatic API
|
|
142
153
|
|
|
143
154
|
```js
|
|
@@ -145,6 +156,7 @@ All block types and behaviors are provided as extensions. Pass them to the `Nabu
|
|
|
145
156
|
engine.insert('paragraph', { text: 'Hello' });
|
|
146
157
|
engine.insert('heading', { level: 1, text: 'Title' });
|
|
147
158
|
engine.insert('list', { listType: 'bullet' });
|
|
159
|
+
engine.insert('dialogue', { text: 'Tu viens ce soir ?' });
|
|
148
160
|
|
|
149
161
|
// Undo / Redo
|
|
150
162
|
engine.undo();
|
|
@@ -152,6 +164,8 @@ engine.redo();
|
|
|
152
164
|
|
|
153
165
|
// Serialize
|
|
154
166
|
const markdown = engine.serialize('markdown');
|
|
167
|
+
// dialogue blocks serialize as: — Tu viens ce soir ?
|
|
168
|
+
|
|
155
169
|
const json = engine.serialize('json');
|
|
156
170
|
```
|
|
157
171
|
|
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
* @import { Nabu } from '../../blocks/nabu.svelte.js'
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import { tick } from 'svelte';
|
|
6
5
|
import { Extension } from '../../utils/extensions.js';
|
|
7
6
|
import { TextBehavior } from './text.behavior.svelte.js';
|
|
8
7
|
|
|
@@ -15,28 +14,11 @@ const MARK_SHORTCUTS = {
|
|
|
15
14
|
};
|
|
16
15
|
|
|
17
16
|
/**
|
|
17
|
+
* Collect all selected blocks with a TextBehavior and a non-collapsed selection.
|
|
18
18
|
* @param {Nabu} nabu
|
|
19
|
-
* @
|
|
19
|
+
* @returns {{ behavior: TextBehavior, sel: any }[]}
|
|
20
20
|
*/
|
|
21
|
-
function
|
|
22
|
-
const ctrl = event.ctrlKey || event.metaKey;
|
|
23
|
-
if (!ctrl) return;
|
|
24
|
-
|
|
25
|
-
const endBlock = nabu.selection.endBlock;
|
|
26
|
-
const startBlock = nabu.selection.startBlock;
|
|
27
|
-
const startFocusData = startBlock?.focus(null, true);
|
|
28
|
-
const endFocusData = endBlock?.focus(null, true);
|
|
29
|
-
|
|
30
|
-
let markName = null;
|
|
31
|
-
if (!event.shiftKey) {
|
|
32
|
-
markName = MARK_SHORTCUTS[event.key.toLowerCase()] ?? null;
|
|
33
|
-
} else if (event.key.toLowerCase() === 'x') {
|
|
34
|
-
markName = 'strikethrough';
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (!markName) return;
|
|
38
|
-
|
|
39
|
-
// Collect all targeted block/selection pairs
|
|
21
|
+
function collectMarkTargets(nabu) {
|
|
40
22
|
const targets = [];
|
|
41
23
|
for (const [, block] of nabu.blocks) {
|
|
42
24
|
if (!block.selected) continue;
|
|
@@ -46,10 +28,23 @@ function onKeyDown(nabu, event) {
|
|
|
46
28
|
if (!sel || sel.isCollapsed) continue;
|
|
47
29
|
targets.push({ behavior, sel });
|
|
48
30
|
}
|
|
31
|
+
return targets;
|
|
32
|
+
}
|
|
49
33
|
|
|
50
|
-
|
|
34
|
+
/**
|
|
35
|
+
* Toggle a mark on all selected text targets.
|
|
36
|
+
* @param {Nabu} nabu
|
|
37
|
+
* @param {string} markName
|
|
38
|
+
*/
|
|
39
|
+
function toggleMark(nabu, markName) {
|
|
40
|
+
const startBlock = nabu.selection.startBlock;
|
|
41
|
+
const endBlock = nabu.selection.endBlock;
|
|
42
|
+
const startFocusData = startBlock?.focus(null, true);
|
|
43
|
+
const endFocusData = endBlock?.focus(null, true);
|
|
44
|
+
|
|
45
|
+
const targets = collectMarkTargets(nabu);
|
|
46
|
+
if (!targets.length) return false;
|
|
51
47
|
|
|
52
|
-
// Classic editor behavior: remove if mark is fully active across all targets, apply otherwise
|
|
53
48
|
const isFullyActive = targets.every(({ behavior, sel }) => behavior.isMarkActive(markName, sel));
|
|
54
49
|
for (const { behavior, sel } of targets) {
|
|
55
50
|
if (isFullyActive) {
|
|
@@ -58,18 +53,80 @@ function onKeyDown(nabu, event) {
|
|
|
58
53
|
behavior.applyMark(markName, true, sel);
|
|
59
54
|
}
|
|
60
55
|
}
|
|
61
|
-
const applied = true;
|
|
62
56
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
57
|
+
nabu.commit();
|
|
58
|
+
if (startBlock && endBlock) {
|
|
59
|
+
nabu.focus({
|
|
60
|
+
from: { block: startBlock, offset: startFocusData?.start?.offset ?? 0 },
|
|
61
|
+
to: { block: endBlock, offset: endFocusData?.end?.offset ?? 0 }
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
68
66
|
|
|
69
|
-
|
|
67
|
+
/**
|
|
68
|
+
* Apply a mark on all selected text targets.
|
|
69
|
+
* @param {Nabu} nabu
|
|
70
|
+
* @param {string} markName
|
|
71
|
+
* @param {any} value
|
|
72
|
+
*/
|
|
73
|
+
function applyMark(nabu, markName, value = true) {
|
|
74
|
+
const targets = collectMarkTargets(nabu);
|
|
75
|
+
if (!targets.length) return;
|
|
76
|
+
for (const { behavior, sel } of targets) {
|
|
77
|
+
behavior.applyMark(markName, value, sel);
|
|
78
|
+
}
|
|
79
|
+
nabu.commit();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Remove a mark from all selected text targets.
|
|
84
|
+
* @param {Nabu} nabu
|
|
85
|
+
* @param {string} markName
|
|
86
|
+
*/
|
|
87
|
+
function removeMark(nabu, markName) {
|
|
88
|
+
const targets = collectMarkTargets(nabu);
|
|
89
|
+
if (!targets.length) return;
|
|
90
|
+
for (const { behavior, sel } of targets) {
|
|
91
|
+
behavior.removeMark(markName, sel);
|
|
70
92
|
}
|
|
93
|
+
nabu.commit();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @param {Nabu} nabu
|
|
98
|
+
* @param {KeyboardEvent} event
|
|
99
|
+
*/
|
|
100
|
+
function onKeyDown(nabu, event) {
|
|
101
|
+
const ctrl = event.ctrlKey || event.metaKey;
|
|
102
|
+
if (!ctrl) return;
|
|
103
|
+
|
|
104
|
+
let markName = null;
|
|
105
|
+
if (!event.shiftKey) {
|
|
106
|
+
markName = MARK_SHORTCUTS[event.key.toLowerCase()] ?? null;
|
|
107
|
+
} else if (event.key.toLowerCase() === 'x') {
|
|
108
|
+
markName = 'strikethrough';
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (!markName) return;
|
|
112
|
+
|
|
113
|
+
event.preventDefault();
|
|
114
|
+
nabu.exec('mark:toggle', { mark: markName });
|
|
115
|
+
return nabu.BREAK;
|
|
71
116
|
}
|
|
72
117
|
|
|
73
118
|
export const RichTextExtension = new Extension('rich-text', {
|
|
119
|
+
actions: {
|
|
120
|
+
// Full API
|
|
121
|
+
'mark:toggle': (nabu, data) => toggleMark(nabu, data?.mark),
|
|
122
|
+
'mark:apply': (nabu, data) => applyMark(nabu, data?.mark, data?.value),
|
|
123
|
+
'mark:remove': (nabu, data) => removeMark(nabu, data?.mark),
|
|
124
|
+
// Aliases
|
|
125
|
+
'bold': (nabu) => toggleMark(nabu, 'bold'),
|
|
126
|
+
'italic': (nabu) => toggleMark(nabu, 'italic'),
|
|
127
|
+
'underline': (nabu) => toggleMark(nabu, 'underline'),
|
|
128
|
+
'code': (nabu) => toggleMark(nabu, 'code'),
|
|
129
|
+
'strikethrough': (nabu) => toggleMark(nabu, 'strikethrough'),
|
|
130
|
+
},
|
|
74
131
|
hooks: { onKeyDown }
|
|
75
132
|
});
|
|
@@ -129,7 +129,20 @@ export class TextBehavior {
|
|
|
129
129
|
handleInsertText(event, selection = this.selection) {
|
|
130
130
|
if (!selection) return;
|
|
131
131
|
|
|
132
|
-
|
|
132
|
+
let textToInsert = event.data || "";
|
|
133
|
+
|
|
134
|
+
// Substitution typographique : `-- ` → `— ` (em-dash)
|
|
135
|
+
if (textToInsert === " " && selection.isCollapsed) {
|
|
136
|
+
const textBeforeCursor = this.text.slice(0, selection.from);
|
|
137
|
+
if (textBeforeCursor.endsWith("--")) {
|
|
138
|
+
this.delete({ index: selection.from - 2, length: 2 }, selection);
|
|
139
|
+
this.insert(selection.from - 2, "— ");
|
|
140
|
+
this.block.commit();
|
|
141
|
+
tick().then(() => this.nabu.selection.setCursor(this.block, selection.from - 2 + 2));
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
133
146
|
if (!selection.isCollapsed) this.delete({index: selection.from, length: selection.to - selection.from}, selection);
|
|
134
147
|
this.insert(selection.from, textToInsert);
|
|
135
148
|
this.block.commit();
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { Nabu } from './nabu.svelte.js'
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { tick } from 'svelte';
|
|
6
|
+
import { Extension } from '../utils/extensions.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Transform the anchor block to a new type, then restore cursor position.
|
|
10
|
+
* @param {Nabu} nabu
|
|
11
|
+
* @param {string} type
|
|
12
|
+
* @param {Object} [props={}]
|
|
13
|
+
*/
|
|
14
|
+
function transformBlock(nabu, type, props = {}) {
|
|
15
|
+
const block = nabu.selection.anchorBlock;
|
|
16
|
+
if (!block) return;
|
|
17
|
+
const blockId = block.id;
|
|
18
|
+
const offset = block.behaviors?.get('text')?.selection?.from ?? 0;
|
|
19
|
+
block.transformTo(type, props);
|
|
20
|
+
// After transformTo, updateRoots() replaces the block instance — fetch the new one.
|
|
21
|
+
tick().then(() => {
|
|
22
|
+
const newBlock = nabu.blocks.get(blockId);
|
|
23
|
+
if (newBlock) nabu.focus({ from: { block: newBlock, offset }, to: { block: newBlock, offset } });
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const BlockExtension = new Extension('block', {
|
|
28
|
+
actions: {
|
|
29
|
+
'block:transform': (nabu, data) => transformBlock(nabu, data?.type, data?.props ?? {}),
|
|
30
|
+
}
|
|
31
|
+
});
|
|
@@ -88,8 +88,16 @@ export class Block {
|
|
|
88
88
|
* @returns {Block} The surviving block
|
|
89
89
|
*/
|
|
90
90
|
consume(otherBlock: Block, direction?: "into" | "from"): Block;
|
|
91
|
-
/**
|
|
92
|
-
|
|
91
|
+
/**
|
|
92
|
+
* Declares a required parent type for structural integrity.
|
|
93
|
+
* Override in subclasses (e.g. ListItem requires a "list" parent).
|
|
94
|
+
* Used by wrapOrphan() after block relocations.
|
|
95
|
+
* @returns {{ type: string, props: () => Record<string, any> } | null}
|
|
96
|
+
*/
|
|
97
|
+
get requiredParent(): {
|
|
98
|
+
type: string;
|
|
99
|
+
props: () => Record<string, any>;
|
|
100
|
+
} | null;
|
|
93
101
|
/** @param {{from?: number, to?: number, index?: number, length?: number, offset?: number}} options @returns {{block: Block} | null} */
|
|
94
102
|
split(options: {
|
|
95
103
|
from?: number;
|
|
@@ -235,14 +235,17 @@ export class Block {
|
|
|
235
235
|
// Handle children relocation if victim has any
|
|
236
236
|
// @ts-ignore - MegaBlock has children
|
|
237
237
|
if (victim.children?.length) {
|
|
238
|
+
// @ts-ignore - adoptChildren only exists on MegaBlock, not on Block base class
|
|
238
239
|
if (absorbed && survivor.adoptChildren) {
|
|
239
240
|
// MegaBlock survivor adopts children
|
|
240
241
|
// @ts-ignore
|
|
241
242
|
survivor.adoptChildren(victim.children);
|
|
242
243
|
} else {
|
|
243
|
-
// Non-MegaBlock survivor: promote children as siblings
|
|
244
|
+
// Non-MegaBlock survivor: promote children as siblings after survivor.
|
|
245
|
+
// Reverse to preserve original order (each moveAfter inserts right after
|
|
246
|
+
// survivor, so forward iteration would reverse the result).
|
|
244
247
|
// @ts-ignore
|
|
245
|
-
victim.children.forEach(child => {
|
|
248
|
+
[...victim.children].reverse().forEach(child => {
|
|
246
249
|
child.node.moveAfter(survivor.node);
|
|
247
250
|
});
|
|
248
251
|
}
|
|
@@ -256,10 +259,13 @@ export class Block {
|
|
|
256
259
|
return survivor;
|
|
257
260
|
}
|
|
258
261
|
|
|
259
|
-
/**
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
262
|
+
/**
|
|
263
|
+
* Declares a required parent type for structural integrity.
|
|
264
|
+
* Override in subclasses (e.g. ListItem requires a "list" parent).
|
|
265
|
+
* Used by wrapOrphan() after block relocations.
|
|
266
|
+
* @returns {{ type: string, props: () => Record<string, any> } | null}
|
|
267
|
+
*/
|
|
268
|
+
get requiredParent() { return null; }
|
|
263
269
|
|
|
264
270
|
/** @param {{from?: number, to?: number, index?: number, length?: number, offset?: number}} options @returns {{block: Block} | null} */
|
|
265
271
|
split(options) {
|
|
@@ -3,6 +3,45 @@
|
|
|
3
3
|
* @import { Nabu } from './nabu.svelte.js';
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Ensures a block respects its structural parent constraint after being relocated.
|
|
8
|
+
* If the block's current Loro parent doesn't match its `requiredParent` type,
|
|
9
|
+
* creates a wrapper block at the same position WITHOUT committing.
|
|
10
|
+
*
|
|
11
|
+
* Uses BlockClass.create() instead of nabu.insert() to avoid premature commits.
|
|
12
|
+
* New wrapper blocks are registered in nabu.blocksByType immediately, so the
|
|
13
|
+
* onBeforeTransaction hook will see and merge adjacent wrappers of the same type.
|
|
14
|
+
*
|
|
15
|
+
* @param {Nabu} nabu
|
|
16
|
+
* @param {Block} block
|
|
17
|
+
*/
|
|
18
|
+
function wrapOrphan(nabu, block) {
|
|
19
|
+
const req = block.requiredParent;
|
|
20
|
+
if (!req) return;
|
|
21
|
+
|
|
22
|
+
// Read the CURRENT Loro parent (not the stale Svelte $state)
|
|
23
|
+
const currentLoroParent = block.node.parent();
|
|
24
|
+
const currentParentType = currentLoroParent?.data?.get("type") ?? null;
|
|
25
|
+
|
|
26
|
+
// Already in the correct parent context — nothing to do
|
|
27
|
+
if (currentParentType === req.type) return;
|
|
28
|
+
|
|
29
|
+
const WrapperClass = nabu.registry.get(req.type);
|
|
30
|
+
if (!WrapperClass) {
|
|
31
|
+
nabu.warn(`wrapOrphan: block type "${req.type}" is not registered`);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Create wrapper at the block's current position (no commit)
|
|
36
|
+
const loroParentId = currentLoroParent?.id?.toString() || null;
|
|
37
|
+
const index = block.node.index() ?? undefined; // null-guard for safety
|
|
38
|
+
|
|
39
|
+
const wrapper = WrapperClass.create(nabu, req.type, req.props?.() || {}, loroParentId, index);
|
|
40
|
+
|
|
41
|
+
// Move block into wrapper (no commit)
|
|
42
|
+
nabu.tree.move(block.node.id, wrapper.node.id);
|
|
43
|
+
}
|
|
44
|
+
|
|
6
45
|
/**
|
|
7
46
|
* Shared beforeinput logic for block containers (Nabu root and MegaBlock).
|
|
8
47
|
*
|
|
@@ -100,7 +139,15 @@ export function handleContainerBeforeInput(container, nabu, event) {
|
|
|
100
139
|
const index = brotherhood.indexOf(block);
|
|
101
140
|
if (index !== -1) {
|
|
102
141
|
const nextSiblings = brotherhood.slice(index + 1);
|
|
103
|
-
if (nextSiblings.length)
|
|
142
|
+
if (nextSiblings.length) {
|
|
143
|
+
// Reverse to preserve original order: moveAfter(focus) inserts right
|
|
144
|
+
// after focus each time, so forward iteration would reverse the result.
|
|
145
|
+
// wrapOrphan ensures structural integrity (e.g. ListItem → needs List parent).
|
|
146
|
+
[...nextSiblings].reverse().forEach(sibling => {
|
|
147
|
+
sibling.node.moveAfter(focusBlock.node);
|
|
148
|
+
wrapOrphan(nabu, sibling);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
104
151
|
}
|
|
105
152
|
});
|
|
106
153
|
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
import RichText from '../../behaviors/text/RichText.svelte';
|
|
3
|
+
/** @type {{block: import('./dialogue.svelte.js').Dialogue}}*/
|
|
4
|
+
let {block} = $props();
|
|
5
|
+
</script>
|
|
6
|
+
|
|
7
|
+
<div
|
|
8
|
+
bind:this={block.element}
|
|
9
|
+
data-block-id={block.id}
|
|
10
|
+
data-block-type="dialogue"
|
|
11
|
+
class="nabu-dialogue"
|
|
12
|
+
class:debug={block.nabu.debugging}
|
|
13
|
+
class:selected={block.selected}
|
|
14
|
+
class:first={block.isSelectionStart}
|
|
15
|
+
class:last={block.isSelectionEnd}
|
|
16
|
+
><RichText delta={block.delta} /></div>
|
|
17
|
+
|
|
18
|
+
<style>
|
|
19
|
+
.nabu-dialogue {
|
|
20
|
+
white-space: pre-wrap;
|
|
21
|
+
margin-bottom: 0.5em;
|
|
22
|
+
padding-left: 1em;
|
|
23
|
+
position: relative;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
.nabu-dialogue::before {
|
|
27
|
+
/* content: "—"; */
|
|
28
|
+
content: "–";
|
|
29
|
+
position: absolute;
|
|
30
|
+
left: 0;
|
|
31
|
+
user-select: none;
|
|
32
|
+
pointer-events: none;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
.nabu-dialogue.debug {
|
|
36
|
+
&.selected {
|
|
37
|
+
background-color: rgba(59, 130, 246, 0.25);
|
|
38
|
+
}
|
|
39
|
+
&.first {
|
|
40
|
+
border-top: 1px solid rgba(59, 130, 246, 0.5);
|
|
41
|
+
}
|
|
42
|
+
&.last {
|
|
43
|
+
border-bottom: 1px solid rgba(59, 130, 246, 0.5);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
</style>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export default Dialogue;
|
|
2
|
+
type Dialogue = {
|
|
3
|
+
$on?(type: string, callback: (e: any) => void): () => void;
|
|
4
|
+
$set?(props: Partial<$$ComponentProps>): void;
|
|
5
|
+
};
|
|
6
|
+
declare const Dialogue: import("svelte").Component<{
|
|
7
|
+
block: import("./dialogue.svelte.js").Dialogue;
|
|
8
|
+
}, {}, "">;
|
|
9
|
+
type $$ComponentProps = {
|
|
10
|
+
block: import("./dialogue.svelte.js").Dialogue;
|
|
11
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { Nabu, NabuNode } from "../nabu.svelte.js";
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {NabuNode<{type: "dialogue", text: LoroText}>} DialogueNode
|
|
6
|
+
*/
|
|
7
|
+
export class Dialogue extends Block {
|
|
8
|
+
/** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
9
|
+
static create(nabu: Nabu, type: string, props?: Object, parentId?: string | null, index?: number | null): Dialogue;
|
|
10
|
+
/** @param {Nabu} nabu @param {DialogueNode} node */
|
|
11
|
+
constructor(nabu: Nabu, node: DialogueNode);
|
|
12
|
+
container: LoroText;
|
|
13
|
+
/** @type {TextBehavior} */
|
|
14
|
+
behavior: TextBehavior;
|
|
15
|
+
component: import("svelte").Component<any, any, string> | import("svelte").Component<{
|
|
16
|
+
block: import("./dialogue.svelte.js").Dialogue;
|
|
17
|
+
}, {}, "">;
|
|
18
|
+
get text(): string;
|
|
19
|
+
get delta(): import("loro-crdt").Delta<string>[];
|
|
20
|
+
selection: {
|
|
21
|
+
from: number;
|
|
22
|
+
to: number;
|
|
23
|
+
isCollapsed: boolean;
|
|
24
|
+
direction: "forward" | "backward" | "none";
|
|
25
|
+
} | null;
|
|
26
|
+
/** @param {Block} block */
|
|
27
|
+
absorbs(block: Block): boolean;
|
|
28
|
+
/** @param {import('loro-crdt').Delta<string>[]} data */
|
|
29
|
+
applyDelta(data?: import("loro-crdt").Delta<string>[]): void;
|
|
30
|
+
/** @param {Parameters<Block["split"]>[0]} [options] @returns {ReturnType<Block["split"]>} */
|
|
31
|
+
split(options?: Parameters<Block["split"]>[0]): ReturnType<Block["split"]>;
|
|
32
|
+
}
|
|
33
|
+
export type DialogueNode = NabuNode<{
|
|
34
|
+
type: "dialogue";
|
|
35
|
+
text: LoroText;
|
|
36
|
+
}>;
|
|
37
|
+
import { Block } from "../block.svelte.js";
|
|
38
|
+
import { LoroText } from "loro-crdt";
|
|
39
|
+
import { TextBehavior } from "../../behaviors/text";
|
|
40
|
+
import type { Nabu } from "../nabu.svelte.js";
|
|
41
|
+
import type { NabuNode } from "../nabu.svelte.js";
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { Block } from "../block.svelte.js";
|
|
2
|
+
import { LoroText } from "loro-crdt";
|
|
3
|
+
import DialogueComponent from "./Dialogue.svelte";
|
|
4
|
+
import { TextBehavior } from "../../behaviors/text";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @import { Nabu, NabuNode } from "../nabu.svelte.js";
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {NabuNode<{type: "dialogue", text: LoroText}>} DialogueNode
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export class Dialogue extends Block {
|
|
15
|
+
/** @param {Nabu} nabu @param {DialogueNode} node */
|
|
16
|
+
constructor(nabu, node) {
|
|
17
|
+
super(nabu, node);
|
|
18
|
+
|
|
19
|
+
const data = node.data;
|
|
20
|
+
this.container = data.get("text") ?? data.setContainer("text", new LoroText());
|
|
21
|
+
|
|
22
|
+
/** @type {TextBehavior} */
|
|
23
|
+
this.behavior = new TextBehavior(this, this.container);
|
|
24
|
+
this.behaviors.set("text", this.behavior);
|
|
25
|
+
|
|
26
|
+
this.serializers.set('markdown', () => `— ${this.behavior.toMarkdown()}`);
|
|
27
|
+
this.serializers.set('json', () => ({
|
|
28
|
+
id: this.id,
|
|
29
|
+
type: 'dialogue',
|
|
30
|
+
content: this.behavior.toJSON()
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
component = $derived(this.nabu.components.get("dialogue") || DialogueComponent);
|
|
35
|
+
|
|
36
|
+
get text() {
|
|
37
|
+
return this.behavior.text;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
get delta() {
|
|
41
|
+
return this.behavior.delta;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
selection = $derived(this.behavior.selection);
|
|
45
|
+
|
|
46
|
+
/** @param {InputEvent} event */
|
|
47
|
+
beforeinput(event) {
|
|
48
|
+
return this.behavior.handleBeforeInput(event);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @param {number} targetOffset
|
|
53
|
+
* @returns {{node: Node, offset: number} | null}
|
|
54
|
+
*/
|
|
55
|
+
getDOMPoint(targetOffset) {
|
|
56
|
+
if (!this.element) return null;
|
|
57
|
+
return this.behavior.getDOMPoint(targetOffset);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** @param {Block} block */
|
|
61
|
+
absorbs(block) { return this.behavior.absorbs(block); }
|
|
62
|
+
|
|
63
|
+
/** @param {number} index @param {string} text */
|
|
64
|
+
insert(index, text) { return this.behavior.insert(index, text); }
|
|
65
|
+
|
|
66
|
+
/** @param {Parameters<Block["delete"]>[0]} [deletion] */
|
|
67
|
+
delete(deletion) { return this.behavior.delete(deletion); }
|
|
68
|
+
|
|
69
|
+
/** @param {import('loro-crdt').Delta<string>[]} data */
|
|
70
|
+
applyDelta(data = []) { return this.behavior.applyDelta(data); }
|
|
71
|
+
|
|
72
|
+
/** @param {Parameters<Block["split"]>[0]} [options] @returns {ReturnType<Block["split"]>} */
|
|
73
|
+
split(options) { return this.behavior.split(options); }
|
|
74
|
+
|
|
75
|
+
/** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
76
|
+
static create(nabu, type, props = {}, parentId = null, index = null) {
|
|
77
|
+
const node = nabu.tree.createNode(parentId || undefined, index || undefined);
|
|
78
|
+
node.data.set("type", "dialogue");
|
|
79
|
+
const container = node.data.setContainer("text", new LoroText());
|
|
80
|
+
if (props.text) container.insert(0, props.text);
|
|
81
|
+
if (props.delta) container.applyDelta([...props.delta]);
|
|
82
|
+
const block = new Dialogue(nabu, node);
|
|
83
|
+
return block;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { tick } from "svelte";
|
|
2
|
+
import { TextBehavior } from "../../../behaviors/text";
|
|
3
|
+
import { Dialogue } from "../dialogue.svelte.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @import { Nabu, Block } from "../../..";
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** @param {Nabu} nabu @param {InputEvent} event @param {Block} block */
|
|
10
|
+
export const onBeforeInput = (nabu, event, block) => {
|
|
11
|
+
const { BREAK, CONTINUE } = nabu;
|
|
12
|
+
|
|
13
|
+
// Backspace au début d'un bloc dialogue → retour en paragraphe
|
|
14
|
+
if (event.inputType === "deleteContentBackward" && block instanceof Dialogue) {
|
|
15
|
+
const behavior = block.behaviors.get("text");
|
|
16
|
+
if (!behavior || !(behavior instanceof TextBehavior)) return CONTINUE;
|
|
17
|
+
const sel = behavior.selection;
|
|
18
|
+
if (!sel || !sel.isCollapsed || sel.from !== 0) return CONTINUE;
|
|
19
|
+
const blockId = block.node.id.toString();
|
|
20
|
+
block.transformTo("paragraph");
|
|
21
|
+
tick().then(() => {
|
|
22
|
+
const b = nabu.blocks.get(blockId);
|
|
23
|
+
if (b) b.focus({ start: 0, end: 0 });
|
|
24
|
+
});
|
|
25
|
+
return BREAK;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// `-- ` en début de paragraphe → transforme en bloc dialogue
|
|
29
|
+
if (event.data !== " ") return CONTINUE;
|
|
30
|
+
if (block.type !== "paragraph") return CONTINUE;
|
|
31
|
+
|
|
32
|
+
const behavior = block.behaviors.get("text");
|
|
33
|
+
if (!behavior || !(behavior instanceof TextBehavior)) return CONTINUE;
|
|
34
|
+
|
|
35
|
+
const sel = behavior.selection;
|
|
36
|
+
if (!sel || !sel.isCollapsed) return CONTINUE;
|
|
37
|
+
|
|
38
|
+
const textBeforeCursor = behavior.text.slice(0, sel.from);
|
|
39
|
+
if (textBeforeCursor !== "--") return CONTINUE;
|
|
40
|
+
|
|
41
|
+
behavior.delete({ index: 0, length: 2 });
|
|
42
|
+
|
|
43
|
+
const blockId = block.node.id.toString();
|
|
44
|
+
block.transformTo("dialogue");
|
|
45
|
+
|
|
46
|
+
tick().then(() => {
|
|
47
|
+
const b = nabu.blocks.get(blockId);
|
|
48
|
+
if (b) b.focus({ start: 0, end: 0 });
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return BREAK;
|
|
52
|
+
};
|