@aionbuilders/nabu 0.1.0-alpha.3 → 0.1.0-alpha.4
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/text.behavior.svelte.js +14 -1
- 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/index.d.ts +1 -0
- package/dist/blocks/index.js +1 -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 +2 -0
- package/dist/blocks/list/list-item.svelte.d.ts +12 -0
- package/dist/blocks/list/list-item.svelte.js +17 -0
- 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/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/package.json +1 -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
|
|
|
@@ -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();
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { tick } from "svelte";
|
|
2
|
+
import { extension } from "../../utils/extensions";
|
|
3
|
+
import { Dialogue } from "./dialogue.svelte.js";
|
|
4
|
+
import DialogueComponent from "./Dialogue.svelte";
|
|
5
|
+
import { onBeforeInput } from "./hooks/onBeforeInput.hook.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @import { Nabu } from "../nabu.svelte.js";
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const DialogueExtension = extension("dialogue", {
|
|
12
|
+
block: Dialogue,
|
|
13
|
+
component: DialogueComponent,
|
|
14
|
+
hooks: {
|
|
15
|
+
/** @param {Nabu} nabu @param {Dialogue} block @param {Event} event @param {{offset: number, delta: import('loro-crdt').Delta<string>}} data */
|
|
16
|
+
onSplit: (nabu, block, event, data) => {
|
|
17
|
+
const { offset, delta } = data;
|
|
18
|
+
|
|
19
|
+
// Bloc dialogue vide + Entrée → sortir du mode dialogue
|
|
20
|
+
if (block.text.length === 0) {
|
|
21
|
+
const blockId = block.node.id.toString();
|
|
22
|
+
block.transformTo("paragraph");
|
|
23
|
+
tick().then(() => {
|
|
24
|
+
const b = nabu.blocks.get(blockId);
|
|
25
|
+
if (b) b.focus({ start: 0, end: 0 });
|
|
26
|
+
});
|
|
27
|
+
return { block };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Bloc non vide + Entrée → créer un nouveau bloc dialogue après
|
|
31
|
+
block.delete({ from: offset, to: -1 });
|
|
32
|
+
|
|
33
|
+
const currentIndex = block.node.index();
|
|
34
|
+
const parent = block.node.parent();
|
|
35
|
+
const parentId = parent?.id.toString() || null;
|
|
36
|
+
|
|
37
|
+
const newBlock = nabu.insert("dialogue", { delta }, parentId, currentIndex + 1);
|
|
38
|
+
|
|
39
|
+
block.commit();
|
|
40
|
+
|
|
41
|
+
setTimeout(() => {
|
|
42
|
+
nabu.selection.setCursor(newBlock, 0);
|
|
43
|
+
}, 0);
|
|
44
|
+
|
|
45
|
+
return { block: newBlock };
|
|
46
|
+
},
|
|
47
|
+
onBeforeInput,
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
export { DialogueExtension, DialogueComponent, Dialogue };
|
package/dist/blocks/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export * from "./container.utils";
|
|
|
5
5
|
export * from "./paragraph";
|
|
6
6
|
export * from "./heading";
|
|
7
7
|
export * from "./list";
|
|
8
|
+
export * from "./dialogue";
|
|
8
9
|
export * from "./selection.svelte.js";
|
|
9
10
|
export { default as NabuEditor } from "./Nabu.svelte";
|
|
10
11
|
export { default as BlockComponent } from "./Block.svelte";
|
package/dist/blocks/index.js
CHANGED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { tick } from "svelte";
|
|
2
|
+
import { TextBehavior } from "../../../behaviors/text";
|
|
3
|
+
import { List } from "../list.svelte.js";
|
|
4
|
+
import { ListItem } from "../list-item.svelte.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @import { Nabu, Block } from "../..";
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Markdown shortcut: typing "- " or "1. " at the start of an empty/new paragraph
|
|
12
|
+
* converts it into a list item inside a new bullet/ordered list.
|
|
13
|
+
*
|
|
14
|
+
* @param {Nabu} nabu
|
|
15
|
+
* @param {InputEvent} event
|
|
16
|
+
* @param {Block} block
|
|
17
|
+
*/
|
|
18
|
+
export const onBeforeInput = (nabu, event, block) => {
|
|
19
|
+
const { BREAK, CONTINUE } = nabu;
|
|
20
|
+
|
|
21
|
+
if (event.data !== " ") return CONTINUE;
|
|
22
|
+
if (block.type !== "paragraph") return CONTINUE;
|
|
23
|
+
|
|
24
|
+
const behavior = block.behaviors.get("text");
|
|
25
|
+
if (!behavior || !(behavior instanceof TextBehavior)) return CONTINUE;
|
|
26
|
+
|
|
27
|
+
const sel = behavior.selection;
|
|
28
|
+
if (!sel || !sel.isCollapsed) return CONTINUE;
|
|
29
|
+
|
|
30
|
+
const textBeforeCursor = behavior.text.slice(0, sel.from);
|
|
31
|
+
|
|
32
|
+
/** @type {"bullet" | "ordered" | null} */
|
|
33
|
+
let listType = null;
|
|
34
|
+
let prefixLength = 0;
|
|
35
|
+
|
|
36
|
+
if (textBeforeCursor.match(/^-$/)) {
|
|
37
|
+
listType = "bullet";
|
|
38
|
+
prefixLength = 1;
|
|
39
|
+
} else if (textBeforeCursor.match(/^\d+\.$/)) {
|
|
40
|
+
listType = "ordered";
|
|
41
|
+
prefixLength = textBeforeCursor.length;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!listType) return CONTINUE;
|
|
45
|
+
|
|
46
|
+
// Delete the prefix ("- " trigger: delete the "-"; "1. " trigger: delete the "1.")
|
|
47
|
+
behavior.delete({ index: 0, length: prefixLength });
|
|
48
|
+
|
|
49
|
+
// Capture remaining text delta (everything after the prefix)
|
|
50
|
+
const delta = behavior.container.toDelta();
|
|
51
|
+
|
|
52
|
+
// Get current tree position from live Loro state (pre-commit)
|
|
53
|
+
const parentNode = block.node.parent();
|
|
54
|
+
const parentId = parentNode?.id.toString() ?? null;
|
|
55
|
+
const index = block.node.index();
|
|
56
|
+
|
|
57
|
+
// Create the List at the same position as the paragraph
|
|
58
|
+
const list = List.create(nabu, "list", { listType }, parentId, index);
|
|
59
|
+
|
|
60
|
+
// Create the ListItem inside the list with the captured content
|
|
61
|
+
const listItem = ListItem.create(nabu, "list-item", { delta }, list.node.id.toString());
|
|
62
|
+
|
|
63
|
+
// Destroy the original paragraph (no commit)
|
|
64
|
+
block.destroy();
|
|
65
|
+
|
|
66
|
+
// Commit the whole operation as a single transaction
|
|
67
|
+
nabu.commit();
|
|
68
|
+
|
|
69
|
+
// Restore focus to the new list-item
|
|
70
|
+
const listItemId = listItem.node.id.toString();
|
|
71
|
+
tick().then(() => {
|
|
72
|
+
const updatedListItem = nabu.blocks.get(listItemId);
|
|
73
|
+
if (updatedListItem) updatedListItem.focus({ start: 0, end: 0 });
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return BREAK;
|
|
77
|
+
};
|
|
@@ -4,6 +4,7 @@ import ListComponent from "./List.svelte";
|
|
|
4
4
|
import { ListItem } from "./list-item.svelte.js";
|
|
5
5
|
import ListItemComponent from "./ListItem.svelte";
|
|
6
6
|
import { ListBehavior } from "./list.behavior.svelte.js";
|
|
7
|
+
import { onBeforeInput } from "./hooks/onBeforeInput.hook.js";
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* @import {SvelteSet} from "svelte/reactivity";
|
|
@@ -13,6 +14,7 @@ const ListExtension = extension("list", {
|
|
|
13
14
|
block: List,
|
|
14
15
|
component: ListComponent,
|
|
15
16
|
hooks: {
|
|
17
|
+
onBeforeInput,
|
|
16
18
|
onBeforeTransaction(nabu) {
|
|
17
19
|
const lists = /** @type {SvelteSet<List>} */ (nabu.blocksByType.get("list"));
|
|
18
20
|
lists?.forEach(list => {
|
|
@@ -24,6 +24,18 @@ export class ListItem extends MegaBlock {
|
|
|
24
24
|
direction: "forward" | "backward" | "none";
|
|
25
25
|
} | null;
|
|
26
26
|
sublist: Block | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* A ListItem must always live inside a List.
|
|
29
|
+
* wrapOrphan() uses this to auto-create a List wrapper when a ListItem
|
|
30
|
+
* is relocated to a context that doesn't have a List parent.
|
|
31
|
+
* props() reads this.parent (stale Svelte state) which still points to the
|
|
32
|
+
* original List at relocation time, giving us the correct listType.
|
|
33
|
+
* @returns {{ type: string, props: () => Record<string, any> }}
|
|
34
|
+
*/
|
|
35
|
+
get requiredParent(): {
|
|
36
|
+
type: string;
|
|
37
|
+
props: () => Record<string, any>;
|
|
38
|
+
};
|
|
27
39
|
/**
|
|
28
40
|
* Retourne la sous-liste existante, ou en crée une nouvelle à la fin des enfants.
|
|
29
41
|
* @param {"bullet" | "ordered"} [listType="bullet"]
|
|
@@ -62,6 +62,23 @@ export class ListItem extends MegaBlock {
|
|
|
62
62
|
|
|
63
63
|
sublist = $derived(this.children.find(child => child.node.data.get("type") === "list"));
|
|
64
64
|
|
|
65
|
+
/**
|
|
66
|
+
* A ListItem must always live inside a List.
|
|
67
|
+
* wrapOrphan() uses this to auto-create a List wrapper when a ListItem
|
|
68
|
+
* is relocated to a context that doesn't have a List parent.
|
|
69
|
+
* props() reads this.parent (stale Svelte state) which still points to the
|
|
70
|
+
* original List at relocation time, giving us the correct listType.
|
|
71
|
+
* @returns {{ type: string, props: () => Record<string, any> }}
|
|
72
|
+
*/
|
|
73
|
+
get requiredParent() {
|
|
74
|
+
return {
|
|
75
|
+
type: "list",
|
|
76
|
+
props: () => ({
|
|
77
|
+
listType: this.parent?.behaviors?.get("list")?.listType || "bullet"
|
|
78
|
+
})
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
65
82
|
/**
|
|
66
83
|
* Retourne la sous-liste existante, ou en crée une nouvelle à la fin des enfants.
|
|
67
84
|
* @param {"bullet" | "ordered"} [listType="bullet"]
|
|
@@ -130,7 +130,7 @@ export class List extends MegaBlock {
|
|
|
130
130
|
|
|
131
131
|
/** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
132
132
|
static create(nabu, type, props = {}, parentId = null, index = null) {
|
|
133
|
-
const node = nabu.tree.createNode(parentId || undefined, index
|
|
133
|
+
const node = nabu.tree.createNode(parentId || undefined, index != null ? index : undefined);
|
|
134
134
|
node.data.set("type", "list");
|
|
135
135
|
node.data.set("listType", props.listType || "bullet");
|
|
136
136
|
const block = new List(nabu, node);
|
|
@@ -6,8 +6,6 @@ export class MegaBlock extends Block {
|
|
|
6
6
|
children: Block[];
|
|
7
7
|
updateChildren(): void;
|
|
8
8
|
/** @param {Block[]} children @param {number | null} [index] */
|
|
9
|
-
adoptChildren(children: Block[], index?: number | null):
|
|
10
|
-
skipped: boolean | Block[];
|
|
11
|
-
};
|
|
9
|
+
adoptChildren(children: Block[], index?: number | null): void;
|
|
12
10
|
}
|
|
13
11
|
import { Block } from "./block.svelte.js";
|
|
@@ -40,20 +40,14 @@ export class MegaBlock extends Block {
|
|
|
40
40
|
|
|
41
41
|
/** @param {Block[]} children @param {number | null} [index] */
|
|
42
42
|
adoptChildren(children, index = null) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
return skipped.push(child);
|
|
52
|
-
}
|
|
53
|
-
this.nabu.tree.move(childNode.id, this.node.id, index);
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
return {skipped: skipped.length ? skipped.reverse() : false};
|
|
43
|
+
// Reverse so that when index is specified, children are inserted in original
|
|
44
|
+
// order (each insert at the same position pushes others down, so reverse of
|
|
45
|
+
// reverse = original). With index=null (append), order doesn't matter.
|
|
46
|
+
// Spread to avoid mutating the input array (which may be a reactive Svelte array).
|
|
47
|
+
[...children].reverse().forEach(child => {
|
|
48
|
+
if (!child.node) return;
|
|
49
|
+
this.nabu.tree.move(child.node.id, this.node.id, index);
|
|
50
|
+
});
|
|
57
51
|
}
|
|
58
52
|
|
|
59
53
|
|
|
@@ -13,7 +13,8 @@ export class NabuSelection extends SvelteSelection {
|
|
|
13
13
|
super();
|
|
14
14
|
this.nabu = nabu;
|
|
15
15
|
|
|
16
|
-
$effect(() => {
|
|
16
|
+
$effect.root(() => {
|
|
17
|
+
$effect(() => {
|
|
17
18
|
if (this.blocks) {
|
|
18
19
|
this.previous.forEach(block => block.clearSelection());
|
|
19
20
|
let i = 0;
|
|
@@ -27,6 +28,10 @@ export class NabuSelection extends SvelteSelection {
|
|
|
27
28
|
this.previous = new Set(this.blocks);
|
|
28
29
|
}
|
|
29
30
|
})
|
|
31
|
+
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
|
|
30
35
|
}
|
|
31
36
|
|
|
32
37
|
anchorBlock = $derived.by(() => {
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Load a snapshot from IndexedDB.
|
|
3
|
+
* @param {string} docId
|
|
4
|
+
* @returns {Promise<Uint8Array | null>}
|
|
5
|
+
*/
|
|
6
|
+
export function loadSnapshot(docId: string): Promise<Uint8Array | null>;
|
|
7
|
+
/**
|
|
8
|
+
* Save a snapshot to IndexedDB.
|
|
9
|
+
* @param {string} docId
|
|
10
|
+
* @param {Uint8Array} snapshot
|
|
11
|
+
* @returns {Promise<void>}
|
|
12
|
+
*/
|
|
13
|
+
export function saveSnapshot(docId: string, snapshot: Uint8Array): Promise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* Delete a snapshot from IndexedDB.
|
|
16
|
+
* @param {string} docId
|
|
17
|
+
* @returns {Promise<void>}
|
|
18
|
+
*/
|
|
19
|
+
export function deleteSnapshot(docId: string): Promise<void>;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
const DB_NAME = 'nabu';
|
|
2
|
+
const STORE = 'snapshots';
|
|
3
|
+
const DB_VERSION = 1;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @returns {Promise<IDBDatabase>}
|
|
7
|
+
*/
|
|
8
|
+
function openDB() {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
const request = indexedDB.open(DB_NAME, DB_VERSION);
|
|
11
|
+
|
|
12
|
+
request.onupgradeneeded = (event) => {
|
|
13
|
+
const db = /** @type {IDBOpenDBRequest} */ (event.target).result;
|
|
14
|
+
if (!db.objectStoreNames.contains(STORE)) {
|
|
15
|
+
db.createObjectStore(STORE, { keyPath: 'docId' });
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
request.onsuccess = (event) => resolve(/** @type {IDBOpenDBRequest} */ (event.target).result);
|
|
20
|
+
request.onerror = (event) => reject(/** @type {IDBOpenDBRequest} */ (event.target).error);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Load a snapshot from IndexedDB.
|
|
26
|
+
* @param {string} docId
|
|
27
|
+
* @returns {Promise<Uint8Array | null>}
|
|
28
|
+
*/
|
|
29
|
+
export async function loadSnapshot(docId) {
|
|
30
|
+
const db = await openDB();
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
const tx = db.transaction(STORE, 'readonly');
|
|
33
|
+
const request = tx.objectStore(STORE).get(docId);
|
|
34
|
+
request.onsuccess = (event) => {
|
|
35
|
+
const record = /** @type {IDBRequest} */ (event.target).result;
|
|
36
|
+
resolve(record ? record.snapshot : null);
|
|
37
|
+
};
|
|
38
|
+
request.onerror = (event) => reject(/** @type {IDBRequest} */ (event.target).error);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Save a snapshot to IndexedDB.
|
|
44
|
+
* @param {string} docId
|
|
45
|
+
* @param {Uint8Array} snapshot
|
|
46
|
+
* @returns {Promise<void>}
|
|
47
|
+
*/
|
|
48
|
+
export async function saveSnapshot(docId, snapshot) {
|
|
49
|
+
const db = await openDB();
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
const tx = db.transaction(STORE, 'readwrite');
|
|
52
|
+
const request = tx.objectStore(STORE).put({ docId, snapshot, savedAt: Date.now() });
|
|
53
|
+
request.onsuccess = () => resolve();
|
|
54
|
+
request.onerror = (event) => reject(/** @type {IDBRequest} */ (event.target).error);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Delete a snapshot from IndexedDB.
|
|
60
|
+
* @param {string} docId
|
|
61
|
+
* @returns {Promise<void>}
|
|
62
|
+
*/
|
|
63
|
+
export async function deleteSnapshot(docId) {
|
|
64
|
+
const db = await openDB();
|
|
65
|
+
return new Promise((resolve, reject) => {
|
|
66
|
+
const tx = db.transaction(STORE, 'readwrite');
|
|
67
|
+
const request = tx.objectStore(STORE).delete(docId);
|
|
68
|
+
request.onsuccess = () => resolve();
|
|
69
|
+
request.onerror = (event) => reject(/** @type {IDBRequest} */ (event.target).error);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { Extension } from '../utils/extensions.js';
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Create a Nabu editor instance with automatic IndexedDB persistence.
|
|
6
|
+
*
|
|
7
|
+
* Async counterpart of `createEditor()` — same `{ preset, extensions }` signature,
|
|
8
|
+
* extended with a required `docId` for storage.
|
|
9
|
+
*
|
|
10
|
+
* On first call, the document is empty (only the default paragraph from ParagraphExtension).
|
|
11
|
+
* On subsequent calls, the document state is restored from the last saved snapshot.
|
|
12
|
+
*
|
|
13
|
+
* The instance gains three extra methods:
|
|
14
|
+
* - `nabu.saveNow()` — force an immediate save (returns a Promise)
|
|
15
|
+
* - `nabu.clearPersistence()` — delete the stored snapshot (returns a Promise)
|
|
16
|
+
* - `nabu.isNew` — `true` if no previous snapshot was found (fresh document)
|
|
17
|
+
*
|
|
18
|
+
* @param {object} options
|
|
19
|
+
* @param {string} options.docId - Unique identifier for this document in IndexedDB
|
|
20
|
+
* @param {Extension[]} [options.preset] - Extension preset (default: FullPreset)
|
|
21
|
+
* @param {Extension[]} [options.extensions] - Additional extensions to append
|
|
22
|
+
* @param {number} [options.debounce] - Auto-save debounce delay in ms (default: 2000)
|
|
23
|
+
* @returns {Promise<Nabu>}
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* // Fresh or restored editor, full preset
|
|
27
|
+
* const engine = await createPersistedEditor({ docId: 'my-note' });
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* // Document preset with custom extension
|
|
31
|
+
* const engine = await createPersistedEditor({
|
|
32
|
+
* docId: 'article-42',
|
|
33
|
+
* preset: DocumentPreset,
|
|
34
|
+
* extensions: [MyCustomExtension],
|
|
35
|
+
* });
|
|
36
|
+
*/
|
|
37
|
+
export function createPersistedEditor({ docId, preset, extensions, debounce }?: {
|
|
38
|
+
docId: string;
|
|
39
|
+
preset?: Extension[] | undefined;
|
|
40
|
+
extensions?: Extension[] | undefined;
|
|
41
|
+
debounce?: number | undefined;
|
|
42
|
+
}): Promise<Nabu>;
|
|
43
|
+
import type { Extension } from '../utils/extensions.js';
|
|
44
|
+
import { Nabu } from '../blocks/nabu.svelte.js';
|
|
45
|
+
import { loadSnapshot } from './indexeddb.js';
|
|
46
|
+
import { saveSnapshot } from './indexeddb.js';
|
|
47
|
+
import { deleteSnapshot } from './indexeddb.js';
|
|
48
|
+
export { loadSnapshot, saveSnapshot, deleteSnapshot };
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Nabu } from '../blocks/nabu.svelte.js';
|
|
2
|
+
import { FullPreset } from '../presets.js';
|
|
3
|
+
import { loadSnapshot, saveSnapshot, deleteSnapshot } from './indexeddb.js';
|
|
4
|
+
|
|
5
|
+
export { loadSnapshot, saveSnapshot, deleteSnapshot };
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @import { Extension } from '../utils/extensions.js';
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Create a Nabu editor instance with automatic IndexedDB persistence.
|
|
13
|
+
*
|
|
14
|
+
* Async counterpart of `createEditor()` — same `{ preset, extensions }` signature,
|
|
15
|
+
* extended with a required `docId` for storage.
|
|
16
|
+
*
|
|
17
|
+
* On first call, the document is empty (only the default paragraph from ParagraphExtension).
|
|
18
|
+
* On subsequent calls, the document state is restored from the last saved snapshot.
|
|
19
|
+
*
|
|
20
|
+
* The instance gains three extra methods:
|
|
21
|
+
* - `nabu.saveNow()` — force an immediate save (returns a Promise)
|
|
22
|
+
* - `nabu.clearPersistence()` — delete the stored snapshot (returns a Promise)
|
|
23
|
+
* - `nabu.isNew` — `true` if no previous snapshot was found (fresh document)
|
|
24
|
+
*
|
|
25
|
+
* @param {object} options
|
|
26
|
+
* @param {string} options.docId - Unique identifier for this document in IndexedDB
|
|
27
|
+
* @param {Extension[]} [options.preset] - Extension preset (default: FullPreset)
|
|
28
|
+
* @param {Extension[]} [options.extensions] - Additional extensions to append
|
|
29
|
+
* @param {number} [options.debounce] - Auto-save debounce delay in ms (default: 2000)
|
|
30
|
+
* @returns {Promise<Nabu>}
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* // Fresh or restored editor, full preset
|
|
34
|
+
* const engine = await createPersistedEditor({ docId: 'my-note' });
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* // Document preset with custom extension
|
|
38
|
+
* const engine = await createPersistedEditor({
|
|
39
|
+
* docId: 'article-42',
|
|
40
|
+
* preset: DocumentPreset,
|
|
41
|
+
* extensions: [MyCustomExtension],
|
|
42
|
+
* });
|
|
43
|
+
*/
|
|
44
|
+
export async function createPersistedEditor({
|
|
45
|
+
docId,
|
|
46
|
+
preset = FullPreset,
|
|
47
|
+
extensions = [],
|
|
48
|
+
debounce = 2000
|
|
49
|
+
} = {}) {
|
|
50
|
+
const snapshot = await loadSnapshot(docId);
|
|
51
|
+
|
|
52
|
+
const nabu = new Nabu({
|
|
53
|
+
extensions: [...preset, ...extensions],
|
|
54
|
+
...(snapshot ? { snapshot } : {})
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Auto-save: Loro fires doc.subscribe() after every committed change
|
|
58
|
+
let saveTimer;
|
|
59
|
+
nabu.doc.subscribe(() => {
|
|
60
|
+
clearTimeout(saveTimer);
|
|
61
|
+
saveTimer = setTimeout(async () => {
|
|
62
|
+
const snap = nabu.doc.export({ mode: 'snapshot' });
|
|
63
|
+
await saveSnapshot(docId, snap);
|
|
64
|
+
}, debounce);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
/** Whether this instance was created from a fresh document (no prior snapshot). */
|
|
68
|
+
nabu.isNew = !snapshot;
|
|
69
|
+
|
|
70
|
+
/** Force an immediate save of the current document state. @returns {Promise<void>} */
|
|
71
|
+
nabu.saveNow = async () => {
|
|
72
|
+
const snap = nabu.doc.export({ mode: 'snapshot' });
|
|
73
|
+
await saveSnapshot(docId, snap);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/** Delete the persisted snapshot for this document. @returns {Promise<void>} */
|
|
77
|
+
nabu.clearPersistence = () => deleteSnapshot(docId);
|
|
78
|
+
|
|
79
|
+
return nabu;
|
|
80
|
+
}
|
package/dist/presets.d.ts
CHANGED
|
@@ -33,5 +33,6 @@ export function createDocumentEditor(options?: {
|
|
|
33
33
|
export function createFullEditor(options?: {
|
|
34
34
|
extensions?: (typeof ParagraphExtension)[];
|
|
35
35
|
}): Nabu;
|
|
36
|
+
export { createPersistedEditor } from "./persistence/persistence.js";
|
|
36
37
|
import { Nabu } from './blocks/nabu.svelte.js';
|
|
37
38
|
import { ParagraphExtension } from './blocks/paragraph/index.js';
|
package/dist/presets.js
CHANGED
|
@@ -2,6 +2,7 @@ import { Nabu } from './blocks/nabu.svelte.js';
|
|
|
2
2
|
import { ParagraphExtension } from './blocks/paragraph/index.js';
|
|
3
3
|
import { HeadingExtension } from './blocks/heading/index.js';
|
|
4
4
|
import { ListExtension, ListItemExtension } from './blocks/list/index.js';
|
|
5
|
+
import { DialogueExtension } from './blocks/dialogue/index.js';
|
|
5
6
|
import { RichTextExtension } from './behaviors/text/index.js';
|
|
6
7
|
|
|
7
8
|
// ---------------------------------------------------------------------------
|
|
@@ -18,7 +19,7 @@ export const TextPreset = [ParagraphExtension, RichTextExtension];
|
|
|
18
19
|
export const DocumentPreset = [ParagraphExtension, HeadingExtension, RichTextExtension];
|
|
19
20
|
|
|
20
21
|
/** All built-in block types + rich text. */
|
|
21
|
-
export const FullPreset = [ParagraphExtension, HeadingExtension, ListExtension, ListItemExtension, RichTextExtension];
|
|
22
|
+
export const FullPreset = [ParagraphExtension, HeadingExtension, ListExtension, ListItemExtension, DialogueExtension, RichTextExtension];
|
|
22
23
|
|
|
23
24
|
// ---------------------------------------------------------------------------
|
|
24
25
|
// Factory — generic createEditor
|
|
@@ -70,3 +71,9 @@ export const createDocumentEditor = (options = {}) =>
|
|
|
70
71
|
*/
|
|
71
72
|
export const createFullEditor = (options = {}) =>
|
|
72
73
|
createEditor({ preset: FullPreset, ...options });
|
|
74
|
+
|
|
75
|
+
// ---------------------------------------------------------------------------
|
|
76
|
+
// Persisted factory
|
|
77
|
+
// ---------------------------------------------------------------------------
|
|
78
|
+
|
|
79
|
+
export { createPersistedEditor } from './persistence/persistence.js';
|