@aionbuilders/nabu 0.1.0-alpha.4 → 0.1.0-alpha.6
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/dist/behaviors/text/rich-text.extension.js +96 -30
- package/dist/behaviors/text/text.behavior.svelte.d.ts +19 -0
- package/dist/behaviors/text/text.behavior.svelte.js +49 -0
- package/dist/blocks/Nabu.svelte +3 -0
- package/dist/blocks/block.extension.d.ts +2 -0
- package/dist/blocks/block.extension.js +32 -0
- package/dist/blocks/block.svelte.d.ts +17 -4
- package/dist/blocks/block.svelte.js +18 -4
- package/dist/blocks/container.utils.d.ts +54 -0
- package/dist/blocks/container.utils.js +165 -8
- package/dist/blocks/dialogue/dialogue.svelte.js +2 -0
- package/dist/blocks/dialogue/index.js +1 -5
- package/dist/blocks/heading/heading.svelte.d.ts +36 -0
- package/dist/blocks/heading/heading.svelte.js +58 -2
- package/dist/blocks/heading/index.js +9 -5
- package/dist/blocks/index.d.ts +1 -0
- package/dist/blocks/index.js +1 -0
- package/dist/blocks/list/index.js +13 -1
- package/dist/blocks/list/list-item.svelte.d.ts +41 -0
- package/dist/blocks/list/list-item.svelte.js +136 -85
- package/dist/blocks/list/list.svelte.d.ts +53 -0
- package/dist/blocks/list/list.svelte.js +112 -11
- package/dist/blocks/megablock.svelte.d.ts +27 -0
- package/dist/blocks/megablock.svelte.js +103 -1
- package/dist/blocks/nabu.svelte.d.ts +28 -2
- package/dist/blocks/nabu.svelte.js +383 -10
- package/dist/blocks/paragraph/index.js +21 -6
- package/dist/blocks/paragraph/paragraph.svelte.d.ts +28 -0
- package/dist/blocks/paragraph/paragraph.svelte.js +53 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -1
- package/dist/paste/html.paste.d.ts +12 -0
- package/dist/paste/html.paste.js +192 -0
- package/dist/paste/index.d.ts +4 -0
- package/dist/paste/index.js +4 -0
- package/dist/paste/markdown.paste.d.ts +12 -0
- package/dist/paste/markdown.paste.js +140 -0
- package/dist/paste/nabu.paste.d.ts +2 -0
- package/dist/paste/nabu.paste.js +20 -0
- package/dist/paste/plain-text.paste.d.ts +8 -0
- package/dist/paste/plain-text.paste.js +42 -0
- package/dist/presets.js +6 -4
- package/dist/utils/extensions.d.ts +63 -0
- package/dist/utils/extensions.js +33 -0
- package/dist/utils/selection.svelte.js +0 -5
- package/package.json +14 -3
|
@@ -3,19 +3,78 @@
|
|
|
3
3
|
* @import { Nabu } from './nabu.svelte.js';
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
import { start } from 'happy-dom/lib/PropertySymbol.js';
|
|
7
|
+
import { MegaBlock } from './megablock.svelte.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Walks up from `block` toward `container` and returns the direct child of
|
|
11
|
+
* `container` that is (or contains) `block`.
|
|
12
|
+
*
|
|
13
|
+
* Works for both Nabu (root blocks have parent = null, loop breaks before
|
|
14
|
+
* reaching Nabu) and MegaBlock (loop stops when parent === container).
|
|
15
|
+
*
|
|
16
|
+
* @param {Block} block
|
|
17
|
+
* @param {Block | Nabu} container
|
|
18
|
+
* @returns {Block | null}
|
|
19
|
+
*/
|
|
20
|
+
export function findDirectChildOf(block, container) {
|
|
21
|
+
const spine = [block];
|
|
22
|
+
while (spine.at(-1) && spine.at(-1) !== container) {
|
|
23
|
+
const current = spine.at(-1);
|
|
24
|
+
const parent = current?.parent;
|
|
25
|
+
if (!current || !parent) break;
|
|
26
|
+
spine.push(parent);
|
|
27
|
+
}
|
|
28
|
+
return spine.at(-1) === container ? spine.at(-2) ?? null : spine.at(-1) ?? null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Finds the Lowest Common Ancestor (LCA) of two blocks in the block tree.
|
|
33
|
+
*
|
|
34
|
+
* Returns the LCA block, or null if the two blocks only share Nabu as ancestor
|
|
35
|
+
* (i.e. both ancestor chains terminate at a root block with parent = null).
|
|
36
|
+
*
|
|
37
|
+
* Examples:
|
|
38
|
+
* LCA(subA, subB) where both are children of subList → subList
|
|
39
|
+
* LCA(para1, para2) where both are root-level blocks → null
|
|
40
|
+
*
|
|
41
|
+
* @param {Block} blockA
|
|
42
|
+
* @param {Block} blockB
|
|
43
|
+
* @returns {Block | null}
|
|
44
|
+
*/
|
|
45
|
+
export function findLCA(blockA, blockB) {
|
|
46
|
+
if (blockA === blockB) return blockA.parent ?? null;
|
|
47
|
+
|
|
48
|
+
// Build the ancestor set for blockA (blockA itself included)
|
|
49
|
+
const ancestorsA = new Set();
|
|
50
|
+
let curr = /** @type {Block | null} */ (blockA);
|
|
51
|
+
while (curr) {
|
|
52
|
+
ancestorsA.add(curr);
|
|
53
|
+
curr = curr.parent ?? null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Walk from blockB up until we hit a node in blockA's ancestor set
|
|
57
|
+
let currB = /** @type {Block | null} */ (blockB);
|
|
58
|
+
while (currB) {
|
|
59
|
+
if (ancestorsA.has(currB)) return currB;
|
|
60
|
+
currB = currB.parent ?? null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return null; // only Nabu is a common ancestor
|
|
64
|
+
}
|
|
65
|
+
|
|
6
66
|
/**
|
|
7
67
|
* Ensures a block respects its structural parent constraint after being relocated.
|
|
8
68
|
* If the block's current Loro parent doesn't match its `requiredParent` type,
|
|
9
69
|
* creates a wrapper block at the same position WITHOUT committing.
|
|
10
70
|
*
|
|
11
|
-
*
|
|
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.
|
|
71
|
+
* Returns the wrapper block if one was created, otherwise undefined.
|
|
14
72
|
*
|
|
15
73
|
* @param {Nabu} nabu
|
|
16
74
|
* @param {Block} block
|
|
75
|
+
* @returns {Block | undefined}
|
|
17
76
|
*/
|
|
18
|
-
function wrapOrphan(nabu, block) {
|
|
77
|
+
export function wrapOrphan(nabu, block) {
|
|
19
78
|
const req = block.requiredParent;
|
|
20
79
|
if (!req) return;
|
|
21
80
|
|
|
@@ -40,6 +99,7 @@ function wrapOrphan(nabu, block) {
|
|
|
40
99
|
|
|
41
100
|
// Move block into wrapper (no commit)
|
|
42
101
|
nabu.tree.move(block.node.id, wrapper.node.id);
|
|
102
|
+
return wrapper;
|
|
43
103
|
}
|
|
44
104
|
|
|
45
105
|
/**
|
|
@@ -68,6 +128,8 @@ export function handleContainerBeforeInput(container, nabu, event) {
|
|
|
68
128
|
const focusData = startBlock.focus(undefined, true);
|
|
69
129
|
const inputType = event.inputType;
|
|
70
130
|
|
|
131
|
+
const toDestroy = new Set();
|
|
132
|
+
|
|
71
133
|
// Build start spine: walk from startBlock up to (but not past) container,
|
|
72
134
|
// pruning next-siblings of each intermediate node along the way.
|
|
73
135
|
const startSpine = [startBlock];
|
|
@@ -78,7 +140,7 @@ export function handleContainerBeforeInput(container, nabu, event) {
|
|
|
78
140
|
startSpine.push(parent);
|
|
79
141
|
if (parent !== container) {
|
|
80
142
|
const nextSiblings = parent.children.slice(current.index + 1);
|
|
81
|
-
if (nextSiblings.length) nextSiblings.forEach(block =>
|
|
143
|
+
if (nextSiblings.length) nextSiblings.forEach(block => toDestroy.add(block));
|
|
82
144
|
}
|
|
83
145
|
}
|
|
84
146
|
|
|
@@ -92,7 +154,7 @@ export function handleContainerBeforeInput(container, nabu, event) {
|
|
|
92
154
|
endSpine.push(parent);
|
|
93
155
|
if (parent !== container) {
|
|
94
156
|
const previousSiblings = parent.children.slice(0, current.index);
|
|
95
|
-
if (previousSiblings.length) previousSiblings.forEach(block =>
|
|
157
|
+
if (previousSiblings.length) previousSiblings.forEach(block => toDestroy.add(block));
|
|
96
158
|
}
|
|
97
159
|
}
|
|
98
160
|
|
|
@@ -104,7 +166,7 @@ export function handleContainerBeforeInput(container, nabu, event) {
|
|
|
104
166
|
|
|
105
167
|
// Delete intermediate direct children of container between the two spines.
|
|
106
168
|
const intermediates = container.children.slice(startOfStartSpine.index + 1, startOfEndSpine.index);
|
|
107
|
-
intermediates.forEach(block =>
|
|
169
|
+
intermediates.forEach(block => toDestroy.add(block));
|
|
108
170
|
|
|
109
171
|
// Single-block selection: delegate directly to that block.
|
|
110
172
|
if (startBlock === endBlock) {
|
|
@@ -151,7 +213,20 @@ export function handleContainerBeforeInput(container, nabu, event) {
|
|
|
151
213
|
}
|
|
152
214
|
});
|
|
153
215
|
|
|
154
|
-
startOfEndSpine
|
|
216
|
+
if (startOfEndSpine === startOfStartSpine) {
|
|
217
|
+
if (startOfStartSpine instanceof MegaBlock) {
|
|
218
|
+
startOfStartSpine.children.filter(c => c.selected).forEach(c => toDestroy.add(c));
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (startOfEndSpine !== startOfStartSpine) {
|
|
223
|
+
startOfEndSpine?.destroy();
|
|
224
|
+
if (startOfStartSpine !== startBlock && startBlock instanceof MegaBlock) {
|
|
225
|
+
startBlock.children.forEach(child => child.destroy());
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
toDestroy.forEach(block => block.destroy());
|
|
155
230
|
|
|
156
231
|
container.commit();
|
|
157
232
|
startBlock.focus({
|
|
@@ -159,3 +234,85 @@ export function handleContainerBeforeInput(container, nabu, event) {
|
|
|
159
234
|
});
|
|
160
235
|
return true;
|
|
161
236
|
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Deletes the current selection content without committing.
|
|
240
|
+
* Handles both single-block and multi-block selections.
|
|
241
|
+
* Returns the resulting insertion position {block, offset}, or null on failure.
|
|
242
|
+
*
|
|
243
|
+
* @param {{ children: import('./block.svelte.js').Block[] }} container
|
|
244
|
+
* @param {import('./nabu.svelte.js').Nabu} nabu
|
|
245
|
+
* @returns {{ block: import('./block.svelte.js').Block, offset: number } | null}
|
|
246
|
+
*/
|
|
247
|
+
export function deleteSelectionContent(container, nabu) {
|
|
248
|
+
const startBlock = /** @type {import('./block.svelte.js').Block} */ (nabu.selection.startBlock);
|
|
249
|
+
const endBlock = /** @type {import('./block.svelte.js').Block} */ (nabu.selection.endBlock);
|
|
250
|
+
if (!startBlock || !endBlock) return null;
|
|
251
|
+
|
|
252
|
+
// Single-block selection: delete the range only
|
|
253
|
+
if (startBlock === endBlock) {
|
|
254
|
+
const sel = startBlock.selection;
|
|
255
|
+
if (!sel || sel.from === sel.to) return { block: startBlock, offset: sel?.from ?? 0 };
|
|
256
|
+
const tb = startBlock.behaviors?.get('text');
|
|
257
|
+
if (tb) tb.delete({ from: sel.from, to: sel.to });
|
|
258
|
+
return { block: startBlock, offset: sel.from };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Multi-block selection: same spine logic as handleContainerBeforeInput (no commit)
|
|
262
|
+
const startOffset = startBlock.focus(undefined, true).options.startOffset;
|
|
263
|
+
|
|
264
|
+
const startSpine = [startBlock];
|
|
265
|
+
while (startSpine.at(-1) && startSpine.at(-1) !== container) {
|
|
266
|
+
const current = startSpine.at(-1);
|
|
267
|
+
const parent = current?.parent;
|
|
268
|
+
if (!current || !parent) break;
|
|
269
|
+
startSpine.push(parent);
|
|
270
|
+
if (parent !== container) {
|
|
271
|
+
const nextSiblings = parent.children.slice(current.index + 1);
|
|
272
|
+
if (nextSiblings.length) nextSiblings.forEach(block => block.destroy());
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const endSpine = [endBlock];
|
|
277
|
+
while (endSpine.at(-1) && endSpine.at(-1) !== container) {
|
|
278
|
+
const current = endSpine.at(-1);
|
|
279
|
+
const parent = current?.parent;
|
|
280
|
+
if (!current || !parent) break;
|
|
281
|
+
endSpine.push(parent);
|
|
282
|
+
if (parent !== container) {
|
|
283
|
+
const previousSiblings = parent.children.slice(0, current.index);
|
|
284
|
+
if (previousSiblings.length) previousSiblings.forEach(block => block.destroy());
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const directChild = (spine) => spine.at(-1) === container ? spine.at(-2) : spine.at(-1);
|
|
289
|
+
const startOfStartSpine = directChild(startSpine);
|
|
290
|
+
const startOfEndSpine = directChild(endSpine);
|
|
291
|
+
if (!startOfStartSpine || !startOfEndSpine) return null;
|
|
292
|
+
|
|
293
|
+
const intermediates = container.children.slice(startOfStartSpine.index + 1, startOfEndSpine.index);
|
|
294
|
+
intermediates.forEach(block => block.destroy());
|
|
295
|
+
|
|
296
|
+
startBlock.delete();
|
|
297
|
+
endBlock.delete();
|
|
298
|
+
startBlock.consume(endBlock);
|
|
299
|
+
|
|
300
|
+
endSpine.forEach(block => {
|
|
301
|
+
if (block === container) return;
|
|
302
|
+
const brotherhood = block.parent?.children || [];
|
|
303
|
+
const index = brotherhood.indexOf(block);
|
|
304
|
+
if (index !== -1) {
|
|
305
|
+
const nextSiblings = brotherhood.slice(index + 1);
|
|
306
|
+
if (nextSiblings.length) {
|
|
307
|
+
[...nextSiblings].reverse().forEach(sibling => {
|
|
308
|
+
sibling.node.moveAfter(startBlock.node);
|
|
309
|
+
wrapOrphan(nabu, sibling);
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
startOfEndSpine?.destroy();
|
|
316
|
+
|
|
317
|
+
return { block: startBlock, offset: startOffset };
|
|
318
|
+
}
|
|
@@ -29,6 +29,7 @@ export class Dialogue extends Block {
|
|
|
29
29
|
type: 'dialogue',
|
|
30
30
|
content: this.behavior.toJSON()
|
|
31
31
|
}));
|
|
32
|
+
this.serializers.set('application/x-nabu+json', (ctx) => this.behavior.toClipboardBlock(ctx));
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
component = $derived(this.nabu.components.get("dialogue") || DialogueComponent);
|
|
@@ -42,6 +43,7 @@ export class Dialogue extends Block {
|
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
selection = $derived(this.behavior.selection);
|
|
46
|
+
positions = $derived(this.behavior.positions);
|
|
45
47
|
|
|
46
48
|
/** @param {InputEvent} event */
|
|
47
49
|
beforeinput(event) {
|
|
@@ -36,11 +36,7 @@ const DialogueExtension = extension("dialogue", {
|
|
|
36
36
|
|
|
37
37
|
const newBlock = nabu.insert("dialogue", { delta }, parentId, currentIndex + 1);
|
|
38
38
|
|
|
39
|
-
block.commit();
|
|
40
|
-
|
|
41
|
-
setTimeout(() => {
|
|
42
|
-
nabu.selection.setCursor(newBlock, 0);
|
|
43
|
-
}, 0);
|
|
39
|
+
block.commit().then(() => nabu.selection.setCursor(newBlock, 0));
|
|
44
40
|
|
|
45
41
|
return { block: newBlock };
|
|
46
42
|
},
|
|
@@ -6,6 +6,42 @@
|
|
|
6
6
|
* @typedef {NabuNode<{type: "heading", text: LoroText, level: number}>} HeadingNode
|
|
7
7
|
*/
|
|
8
8
|
export class Heading extends Block {
|
|
9
|
+
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
10
|
+
static toMarkdown(pb: import("../../utils/extensions.js").PasteBlock): string;
|
|
11
|
+
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
12
|
+
static toHtml(pb: import("../../utils/extensions.js").PasteBlock): string;
|
|
13
|
+
static markdownRules: {
|
|
14
|
+
priority: number;
|
|
15
|
+
detect: RegExp;
|
|
16
|
+
consume: () => number;
|
|
17
|
+
}[];
|
|
18
|
+
/**
|
|
19
|
+
* @param {string[]} lines
|
|
20
|
+
* @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[] }} helpers
|
|
21
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
22
|
+
*/
|
|
23
|
+
static fromMarkdown(lines: string[], { parseInline }: {
|
|
24
|
+
parseInline: (text: string) => import("loro-crdt").Delta<string>[];
|
|
25
|
+
}): import("../../utils/extensions.js").PasteBlock | null;
|
|
26
|
+
static htmlRules: {
|
|
27
|
+
selector: string;
|
|
28
|
+
props: {
|
|
29
|
+
level: number;
|
|
30
|
+
};
|
|
31
|
+
}[];
|
|
32
|
+
/**
|
|
33
|
+
* @param {Element} el
|
|
34
|
+
* @param {{ parseInline: (el: Element) => import('loro-crdt').Delta<string>[] }} helpers
|
|
35
|
+
* @param {{ props?: { level?: number } }} [rule]
|
|
36
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
37
|
+
*/
|
|
38
|
+
static fromHTML(el: Element, { parseInline }: {
|
|
39
|
+
parseInline: (el: Element) => import("loro-crdt").Delta<string>[];
|
|
40
|
+
}, rule?: {
|
|
41
|
+
props?: {
|
|
42
|
+
level?: number;
|
|
43
|
+
};
|
|
44
|
+
}): import("../../utils/extensions.js").PasteBlock | null;
|
|
9
45
|
/** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
10
46
|
static create(nabu: Nabu, type: string, props?: Object, parentId?: string | null, index?: number | null): Heading;
|
|
11
47
|
/** @param {Nabu} nabu @param {HeadingNode} node */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TextBehavior } from "../../behaviors/text";
|
|
1
|
+
import { TextBehavior, deltaToHtml, deltaToMarkdown } from "../../behaviors/text";
|
|
2
2
|
import HeadingComponent from "./Heading.svelte";
|
|
3
3
|
import { Block } from "../block.svelte.js";
|
|
4
4
|
import { LoroText } from "loro-crdt";
|
|
@@ -32,6 +32,7 @@ export class Heading extends Block {
|
|
|
32
32
|
props: { level: this.level },
|
|
33
33
|
content: this.behavior.toJSON()
|
|
34
34
|
}));
|
|
35
|
+
this.serializers.set('application/x-nabu+json', (ctx) => this.behavior.toClipboardBlock({ ...ctx, props: { level: this.level } }));
|
|
35
36
|
|
|
36
37
|
// Synchronisation du niveau
|
|
37
38
|
this.node.data.subscribe(() => {
|
|
@@ -53,7 +54,8 @@ export class Heading extends Block {
|
|
|
53
54
|
}
|
|
54
55
|
|
|
55
56
|
selection = $derived(this.behavior.selection);
|
|
56
|
-
|
|
57
|
+
positions = $derived(this.behavior.positions);
|
|
58
|
+
|
|
57
59
|
/** @param {InputEvent} event */
|
|
58
60
|
beforeinput(event) {
|
|
59
61
|
return this.behavior.handleBeforeInput(event);
|
|
@@ -80,6 +82,60 @@ export class Heading extends Block {
|
|
|
80
82
|
/** @param {Parameters<Block["split"]>[0]} [options] @returns {ReturnType<Block["split"]>} */
|
|
81
83
|
split(options) { return this.behavior.split(options); }
|
|
82
84
|
|
|
85
|
+
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
86
|
+
static toMarkdown(pb) {
|
|
87
|
+
const level = pb.props?.level ?? 1;
|
|
88
|
+
return `${'#'.repeat(level)} ${deltaToMarkdown(pb.delta || [])}`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
92
|
+
static toHtml(pb) {
|
|
93
|
+
const level = pb.props?.level ?? 1;
|
|
94
|
+
return `<h${level}>${deltaToHtml(pb.delta || [])}</h${level}>`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
static markdownRules = [
|
|
98
|
+
{
|
|
99
|
+
priority: 10,
|
|
100
|
+
detect: /^#{1,6} /,
|
|
101
|
+
consume: () => 1,
|
|
102
|
+
}
|
|
103
|
+
];
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @param {string[]} lines
|
|
107
|
+
* @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[] }} helpers
|
|
108
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
109
|
+
*/
|
|
110
|
+
static fromMarkdown(lines, { parseInline }) {
|
|
111
|
+
const m = lines[0].match(/^(#{1,6})\s+(.*)/);
|
|
112
|
+
if (!m) return null;
|
|
113
|
+
const delta = parseInline(m[2]);
|
|
114
|
+
if (!delta.length) return null;
|
|
115
|
+
return { type: 'heading', props: { level: m[1].length }, delta, partial: false };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
static htmlRules = [
|
|
119
|
+
{ selector: 'h1', props: { level: 1 } },
|
|
120
|
+
{ selector: 'h2', props: { level: 2 } },
|
|
121
|
+
{ selector: 'h3', props: { level: 3 } },
|
|
122
|
+
{ selector: 'h4', props: { level: 4 } },
|
|
123
|
+
{ selector: 'h5', props: { level: 5 } },
|
|
124
|
+
{ selector: 'h6', props: { level: 6 } },
|
|
125
|
+
];
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* @param {Element} el
|
|
129
|
+
* @param {{ parseInline: (el: Element) => import('loro-crdt').Delta<string>[] }} helpers
|
|
130
|
+
* @param {{ props?: { level?: number } }} [rule]
|
|
131
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
132
|
+
*/
|
|
133
|
+
static fromHTML(el, { parseInline }, rule) {
|
|
134
|
+
const delta = parseInline(el);
|
|
135
|
+
if (!delta.length) return null;
|
|
136
|
+
return { type: 'heading', props: { level: rule?.props?.level ?? 1 }, delta, partial: false };
|
|
137
|
+
}
|
|
138
|
+
|
|
83
139
|
/** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
84
140
|
static create(nabu, type, props = {}, parentId = null, index = null) {
|
|
85
141
|
const node = nabu.tree.createNode(parentId || undefined, index || undefined);
|
|
@@ -12,6 +12,14 @@ import { onBeforeInput } from "./hooks/onBeforeInput.hook";
|
|
|
12
12
|
const HeadingExtension = extension("heading", {
|
|
13
13
|
block: Heading,
|
|
14
14
|
component: HeadingComponent,
|
|
15
|
+
actions: {
|
|
16
|
+
'heading:1': (nabu) => nabu.exec('block:transform', { type: 'heading', props: { level: 1 } }),
|
|
17
|
+
'heading:2': (nabu) => nabu.exec('block:transform', { type: 'heading', props: { level: 2 } }),
|
|
18
|
+
'heading:3': (nabu) => nabu.exec('block:transform', { type: 'heading', props: { level: 3 } }),
|
|
19
|
+
'heading:4': (nabu) => nabu.exec('block:transform', { type: 'heading', props: { level: 4 } }),
|
|
20
|
+
'heading:5': (nabu) => nabu.exec('block:transform', { type: 'heading', props: { level: 5 } }),
|
|
21
|
+
'heading:6': (nabu) => nabu.exec('block:transform', { type: 'heading', props: { level: 6 } }),
|
|
22
|
+
},
|
|
15
23
|
hooks: {
|
|
16
24
|
/** @param {Nabu} nabu @param {Heading} block @param {Event} event @param {{offset: number, delta: import('loro-crdt').Delta<string>}} data */
|
|
17
25
|
onSplit: (nabu, block, event, data) => {
|
|
@@ -26,11 +34,7 @@ const HeadingExtension = extension("heading", {
|
|
|
26
34
|
// On split souvent un titre pour créer un paragraphe après
|
|
27
35
|
const newBlock = nabu.insert("paragraph", { delta }, parentId, currentIndex + 1);
|
|
28
36
|
|
|
29
|
-
block.commit();
|
|
30
|
-
|
|
31
|
-
setTimeout(() => {
|
|
32
|
-
nabu.selection.setCursor(newBlock, 0);
|
|
33
|
-
}, 0);
|
|
37
|
+
block.commit().then(() => nabu.selection.setCursor(newBlock, 0));
|
|
34
38
|
|
|
35
39
|
return { block: newBlock };
|
|
36
40
|
},
|
package/dist/blocks/index.d.ts
CHANGED
package/dist/blocks/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { extension } from "../../utils/extensions";
|
|
2
2
|
import { List } from "./list.svelte.js";
|
|
3
3
|
import ListComponent from "./List.svelte";
|
|
4
|
-
import { ListItem } from "./list-item.svelte.js";
|
|
4
|
+
import { ListItem, indentListItem, unindentListItem } from "./list-item.svelte.js";
|
|
5
5
|
import ListItemComponent from "./ListItem.svelte";
|
|
6
6
|
import { ListBehavior } from "./list.behavior.svelte.js";
|
|
7
7
|
import { onBeforeInput } from "./hooks/onBeforeInput.hook.js";
|
|
@@ -38,6 +38,18 @@ const ListExtension = extension("list", {
|
|
|
38
38
|
const ListItemExtension = extension("list-item", {
|
|
39
39
|
block: ListItem,
|
|
40
40
|
component: ListItemComponent,
|
|
41
|
+
actions: {
|
|
42
|
+
'list:indent': (nabu) => {
|
|
43
|
+
const block = nabu.selection.anchorBlock;
|
|
44
|
+
if (block?.type !== 'list-item') return;
|
|
45
|
+
indentListItem(/** @type {ListItem} */ (block));
|
|
46
|
+
},
|
|
47
|
+
'list:unindent': (nabu) => {
|
|
48
|
+
const block = nabu.selection.anchorBlock;
|
|
49
|
+
if (block?.type !== 'list-item') return;
|
|
50
|
+
unindentListItem(/** @type {ListItem} */ (block));
|
|
51
|
+
},
|
|
52
|
+
}
|
|
41
53
|
});
|
|
42
54
|
|
|
43
55
|
export { ListExtension, ListItemExtension, List, ListItem, ListComponent, ListItemComponent };
|
|
@@ -4,7 +4,48 @@
|
|
|
4
4
|
/**
|
|
5
5
|
* @typedef {NabuNode<{type: "list-item", text: LoroText}>} ListItemNode
|
|
6
6
|
*/
|
|
7
|
+
/**
|
|
8
|
+
* Indent a list item (Tab behavior): move into previous sibling's sublist, hoist own children.
|
|
9
|
+
* @param {ListItem} item
|
|
10
|
+
* @returns {boolean}
|
|
11
|
+
*/
|
|
12
|
+
export function indentListItem(item: ListItem): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Unindent a list item (Shift+Tab behavior): move out of parent sublist, carry followers.
|
|
15
|
+
* @param {ListItem} item
|
|
16
|
+
* @returns {boolean}
|
|
17
|
+
*/
|
|
18
|
+
export function unindentListItem(item: ListItem): boolean;
|
|
7
19
|
export class ListItem extends MegaBlock {
|
|
20
|
+
/**
|
|
21
|
+
* @param {import('../../utils/extensions.js').PasteBlock} pb
|
|
22
|
+
* @param {{ recurse: (child: import('../../utils/extensions.js').PasteBlock, ctx?: object) => string, listType?: string, index?: number, depth?: number }} helpers
|
|
23
|
+
*/
|
|
24
|
+
static toMarkdown(pb: import("../../utils/extensions.js").PasteBlock, { recurse, listType, index, depth }: {
|
|
25
|
+
recurse: (child: import("../../utils/extensions.js").PasteBlock, ctx?: object) => string;
|
|
26
|
+
listType?: string;
|
|
27
|
+
index?: number;
|
|
28
|
+
depth?: number;
|
|
29
|
+
}): string;
|
|
30
|
+
/**
|
|
31
|
+
* @param {import('../../utils/extensions.js').PasteBlock} pb
|
|
32
|
+
* @param {{ recurse: (child: import('../../utils/extensions.js').PasteBlock) => string }} helpers
|
|
33
|
+
*/
|
|
34
|
+
static toHtml(pb: import("../../utils/extensions.js").PasteBlock, { recurse }: {
|
|
35
|
+
recurse: (child: import("../../utils/extensions.js").PasteBlock) => string;
|
|
36
|
+
}): string;
|
|
37
|
+
static htmlRules: {
|
|
38
|
+
selector: string;
|
|
39
|
+
}[];
|
|
40
|
+
/**
|
|
41
|
+
* @param {Element} el
|
|
42
|
+
* @param {{ parseInline: (el: Element) => import('loro-crdt').Delta<string>[], parseBlock: (el: Element) => import('../../utils/extensions.js').PasteBlock | null }} helpers
|
|
43
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
44
|
+
*/
|
|
45
|
+
static fromHTML(el: Element, { parseInline, parseBlock }: {
|
|
46
|
+
parseInline: (el: Element) => import("loro-crdt").Delta<string>[];
|
|
47
|
+
parseBlock: (el: Element) => import("../../utils/extensions.js").PasteBlock | null;
|
|
48
|
+
}): import("../../utils/extensions.js").PasteBlock | null;
|
|
8
49
|
/** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
9
50
|
static create(nabu: Nabu, type: string, props?: Object, parentId?: string | null, index?: number | null): ListItem;
|
|
10
51
|
/** @param {Nabu} nabu @param {ListItemNode} node */
|