@aionbuilders/nabu 0.1.0-alpha.5 → 0.1.0-alpha.7
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 +9 -0
- 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.js +1 -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 +164 -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 +1 -5
- package/dist/blocks/list/list-item.svelte.d.ts +29 -0
- package/dist/blocks/list/list-item.svelte.js +69 -6
- 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 +20 -2
- package/dist/blocks/nabu.svelte.js +358 -9
- package/dist/blocks/paragraph/index.js +18 -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 +59 -0
- package/dist/utils/extensions.js +30 -0
- package/dist/utils/selection.svelte.js +0 -5
- package/package.json +13 -3
|
@@ -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);
|
|
@@ -34,11 +34,7 @@ const HeadingExtension = extension("heading", {
|
|
|
34
34
|
// On split souvent un titre pour créer un paragraphe après
|
|
35
35
|
const newBlock = nabu.insert("paragraph", { delta }, parentId, currentIndex + 1);
|
|
36
36
|
|
|
37
|
-
block.commit();
|
|
38
|
-
|
|
39
|
-
setTimeout(() => {
|
|
40
|
-
nabu.selection.setCursor(newBlock, 0);
|
|
41
|
-
}, 0);
|
|
37
|
+
block.commit().then(() => nabu.selection.setCursor(newBlock, 0));
|
|
42
38
|
|
|
43
39
|
return { block: newBlock };
|
|
44
40
|
},
|
|
@@ -17,6 +17,35 @@ export function indentListItem(item: ListItem): boolean;
|
|
|
17
17
|
*/
|
|
18
18
|
export function unindentListItem(item: ListItem): boolean;
|
|
19
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;
|
|
20
49
|
/** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
21
50
|
static create(nabu: Nabu, type: string, props?: Object, parentId?: string | null, index?: number | null): ListItem;
|
|
22
51
|
/** @param {Nabu} nabu @param {ListItemNode} node */
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { MegaBlock } from "../megablock.svelte.js";
|
|
2
2
|
import ListItemComponent from "./ListItem.svelte";
|
|
3
3
|
import { LoroText } from "loro-crdt";
|
|
4
|
-
import { TextBehavior } from "../../behaviors/text";
|
|
4
|
+
import { TextBehavior, deltaToHtml, deltaToMarkdown } from "../../behaviors/text";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* @import { Nabu, NabuNode, Block } from "..";
|
|
@@ -40,8 +40,7 @@ export function indentListItem(item) {
|
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
item.commit();
|
|
44
|
-
setTimeout(() => item.behavior.block.focus(), 0);
|
|
43
|
+
item.commit().then(() => item.behavior.block.focus());
|
|
45
44
|
return true;
|
|
46
45
|
}
|
|
47
46
|
|
|
@@ -73,8 +72,7 @@ export function unindentListItem(item) {
|
|
|
73
72
|
const targetIndex = grandParentItemNode.index() + 1;
|
|
74
73
|
item.nabu.tree.move(item.node.id.toString(), targetParentListNode.id.toString(), targetIndex);
|
|
75
74
|
|
|
76
|
-
item.commit();
|
|
77
|
-
setTimeout(() => item.behavior.block.focus(), 0);
|
|
75
|
+
item.commit().then(() => item.behavior.block.focus());
|
|
78
76
|
return true;
|
|
79
77
|
}
|
|
80
78
|
|
|
@@ -91,7 +89,7 @@ export class ListItem extends MegaBlock {
|
|
|
91
89
|
this.behaviors.set("text", this.behavior);
|
|
92
90
|
|
|
93
91
|
this.serializers.set('markdown', () => {
|
|
94
|
-
const depth = this.parents.filter(p => p.type === '
|
|
92
|
+
const depth = this.parents.filter(p => p.type === 'list-item').length;
|
|
95
93
|
const indent = ' '.repeat(depth);
|
|
96
94
|
const listType = /** @type {any} */ (this.parent)?.listType ?? 'bullet';
|
|
97
95
|
const prefix = listType === 'ordered' ? `${this.index + 1}.` : '-';
|
|
@@ -113,6 +111,7 @@ export class ListItem extends MegaBlock {
|
|
|
113
111
|
if (childrenJson.length) result.children = childrenJson;
|
|
114
112
|
return result;
|
|
115
113
|
});
|
|
114
|
+
this.serializers.set('application/x-nabu+json', (ctx = {}) => this.behavior.toClipboardBlock(ctx));
|
|
116
115
|
}
|
|
117
116
|
|
|
118
117
|
component = $derived(this.nabu.components.get("list-item") || ListItemComponent);
|
|
@@ -127,6 +126,27 @@ export class ListItem extends MegaBlock {
|
|
|
127
126
|
|
|
128
127
|
selection = $derived(this.behavior.selection);
|
|
129
128
|
|
|
129
|
+
positions = $derived(this.behavior.positions + super.positions);
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Mixed block: own text comes first, then children (sublists).
|
|
133
|
+
* @param {number} localOffset
|
|
134
|
+
* @returns {{ block: import('../block.svelte.js').Block, offset: number } | null}
|
|
135
|
+
*/
|
|
136
|
+
resolveOffset(localOffset) {
|
|
137
|
+
const textPositions = this.behavior.positions; // text.length + 1
|
|
138
|
+
if (localOffset < textPositions || !this.children.length) {
|
|
139
|
+
return { block: this, offset: Math.min(localOffset, this.behavior.text.length) };
|
|
140
|
+
}
|
|
141
|
+
let remaining = localOffset - textPositions;
|
|
142
|
+
for (const child of this.children) {
|
|
143
|
+
const childPositions = child.positions ?? 0;
|
|
144
|
+
if (remaining < childPositions) return child.resolveOffset(remaining);
|
|
145
|
+
remaining -= childPositions;
|
|
146
|
+
}
|
|
147
|
+
return { block: this, offset: this.behavior.text.length };
|
|
148
|
+
}
|
|
149
|
+
|
|
130
150
|
sublist = $derived(this.children.find(child => child.node.data.get("type") === "list"));
|
|
131
151
|
|
|
132
152
|
/**
|
|
@@ -205,6 +225,49 @@ export class ListItem extends MegaBlock {
|
|
|
205
225
|
});
|
|
206
226
|
}
|
|
207
227
|
|
|
228
|
+
/**
|
|
229
|
+
* @param {import('../../utils/extensions.js').PasteBlock} pb
|
|
230
|
+
* @param {{ recurse: (child: import('../../utils/extensions.js').PasteBlock, ctx?: object) => string, listType?: string, index?: number, depth?: number }} helpers
|
|
231
|
+
*/
|
|
232
|
+
static toMarkdown(pb, { recurse, listType = 'bullet', index = 0, depth = 0 }) {
|
|
233
|
+
const indent = ' '.repeat(depth);
|
|
234
|
+
const prefix = listType === 'ordered' ? `${index + 1}.` : '-';
|
|
235
|
+
const text = deltaToMarkdown(pb.delta || []);
|
|
236
|
+
const sublists = (pb.children || []).map(sub => recurse(sub, { depth: depth + 1 })).join('\n');
|
|
237
|
+
return `${indent}${prefix} ${text}${sublists ? '\n' + sublists : ''}`;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* @param {import('../../utils/extensions.js').PasteBlock} pb
|
|
242
|
+
* @param {{ recurse: (child: import('../../utils/extensions.js').PasteBlock) => string }} helpers
|
|
243
|
+
*/
|
|
244
|
+
static toHtml(pb, { recurse }) {
|
|
245
|
+
const text = deltaToHtml(pb.delta || []);
|
|
246
|
+
const sublists = (pb.children || []).map(recurse).join('');
|
|
247
|
+
return `<li>${text}${sublists}</li>`;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
static htmlRules = [{ selector: 'li' }];
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* @param {Element} el
|
|
254
|
+
* @param {{ parseInline: (el: Element) => import('loro-crdt').Delta<string>[], parseBlock: (el: Element) => import('../../utils/extensions.js').PasteBlock | null }} helpers
|
|
255
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
256
|
+
*/
|
|
257
|
+
static fromHTML(el, { parseInline, parseBlock }) {
|
|
258
|
+
const delta = parseInline(el);
|
|
259
|
+
const childLists = /** @type {import('../../utils/extensions.js').PasteBlock[]} */ (
|
|
260
|
+
[...el.children]
|
|
261
|
+
.filter(child => child.matches('ul, ol'))
|
|
262
|
+
.map(parseBlock)
|
|
263
|
+
.filter(Boolean)
|
|
264
|
+
);
|
|
265
|
+
/** @type {import('../../utils/extensions.js').PasteBlock} */
|
|
266
|
+
const result = { type: 'list-item', delta, partial: false };
|
|
267
|
+
if (childLists.length) result.children = childLists;
|
|
268
|
+
return result;
|
|
269
|
+
}
|
|
270
|
+
|
|
208
271
|
/** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
209
272
|
static create(nabu, type, props = {}, parentId = null, index = null) {
|
|
210
273
|
const node = nabu.tree.createNode(parentId || undefined, index || undefined);
|
|
@@ -5,12 +5,65 @@
|
|
|
5
5
|
* @typedef {NabuNode<{type: "list", listType: "bullet" | "ordered"}>} ListNode
|
|
6
6
|
*/
|
|
7
7
|
export class List extends MegaBlock {
|
|
8
|
+
/**
|
|
9
|
+
* @param {import('../../utils/extensions.js').PasteBlock} pb
|
|
10
|
+
* @param {{ recurse: (child: import('../../utils/extensions.js').PasteBlock, ctx?: object) => string, depth?: number }} helpers
|
|
11
|
+
*/
|
|
12
|
+
static toMarkdown(pb: import("../../utils/extensions.js").PasteBlock, { recurse, depth }: {
|
|
13
|
+
recurse: (child: import("../../utils/extensions.js").PasteBlock, ctx?: object) => string;
|
|
14
|
+
depth?: number;
|
|
15
|
+
}): string;
|
|
16
|
+
/**
|
|
17
|
+
* @param {import('../../utils/extensions.js').PasteBlock} pb
|
|
18
|
+
* @param {{ recurse: (child: import('../../utils/extensions.js').PasteBlock) => string }} helpers
|
|
19
|
+
*/
|
|
20
|
+
static toHtml(pb: import("../../utils/extensions.js").PasteBlock, { recurse }: {
|
|
21
|
+
recurse: (child: import("../../utils/extensions.js").PasteBlock) => string;
|
|
22
|
+
}): string;
|
|
23
|
+
static markdownRules: {
|
|
24
|
+
priority: number;
|
|
25
|
+
detect: RegExp;
|
|
26
|
+
consume(lines: string[], i: number): number;
|
|
27
|
+
}[];
|
|
28
|
+
/**
|
|
29
|
+
* Parse markdown list lines into a nested List/ListItem PasteBlock tree.
|
|
30
|
+
* Uses indent depth to reconstruct nesting recursively.
|
|
31
|
+
*
|
|
32
|
+
* @param {string[]} lines
|
|
33
|
+
* @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[] }} helpers
|
|
34
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
35
|
+
*/
|
|
36
|
+
static fromMarkdown(lines: string[], { parseInline }: {
|
|
37
|
+
parseInline: (text: string) => import("loro-crdt").Delta<string>[];
|
|
38
|
+
}): import("../../utils/extensions.js").PasteBlock | null;
|
|
39
|
+
static htmlRules: {
|
|
40
|
+
selector: string;
|
|
41
|
+
props: {
|
|
42
|
+
listType: string;
|
|
43
|
+
};
|
|
44
|
+
}[];
|
|
45
|
+
/**
|
|
46
|
+
* @param {Element} el
|
|
47
|
+
* @param {{ parseChildren: (el: Element) => import('../../utils/extensions.js').PasteBlock[] }} helpers
|
|
48
|
+
* @param {{ props?: { listType?: string } }} [rule]
|
|
49
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
50
|
+
*/
|
|
51
|
+
static fromHTML(el: Element, { parseChildren }: {
|
|
52
|
+
parseChildren: (el: Element) => import("../../utils/extensions.js").PasteBlock[];
|
|
53
|
+
}, rule?: {
|
|
54
|
+
props?: {
|
|
55
|
+
listType?: string;
|
|
56
|
+
};
|
|
57
|
+
}): import("../../utils/extensions.js").PasteBlock | null;
|
|
8
58
|
/** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
9
59
|
static create(nabu: Nabu, type: string, props?: Object, parentId?: string | null, index?: number | null): List;
|
|
10
60
|
/** @param {Nabu} nabu @param {ListNode} node */
|
|
11
61
|
constructor(nabu: Nabu, node: ListNode);
|
|
12
62
|
behavior: ListBehavior;
|
|
13
63
|
listType: "bullet" | "ordered";
|
|
64
|
+
get clipboardProps(): {
|
|
65
|
+
listType: "bullet" | "ordered";
|
|
66
|
+
};
|
|
14
67
|
component: import("svelte").Component<any, any, string> | import("svelte").Component<{
|
|
15
68
|
block: import("./list.svelte.js").List;
|
|
16
69
|
}, {}, "">;
|
|
@@ -36,6 +36,8 @@ export class List extends MegaBlock {
|
|
|
36
36
|
|
|
37
37
|
listType = $derived(this.behavior.listType);
|
|
38
38
|
|
|
39
|
+
get clipboardProps() { return { listType: this.listType }; }
|
|
40
|
+
|
|
39
41
|
component = $derived(this.nabu.components.get("list") || ListComponent);
|
|
40
42
|
|
|
41
43
|
root = $derived(!this.parent);
|
|
@@ -83,11 +85,7 @@ export class List extends MegaBlock {
|
|
|
83
85
|
this.destroy();
|
|
84
86
|
}
|
|
85
87
|
|
|
86
|
-
this.nabu.commit();
|
|
87
|
-
|
|
88
|
-
setTimeout(() => {
|
|
89
|
-
this.nabu.selection.setCursor(newParagraph, 0);
|
|
90
|
-
}, 0);
|
|
88
|
+
this.nabu.commit().then(() => this.nabu.selection.setCursor(newParagraph, 0));
|
|
91
89
|
|
|
92
90
|
return { block: newParagraph };
|
|
93
91
|
}
|
|
@@ -118,16 +116,119 @@ export class List extends MegaBlock {
|
|
|
118
116
|
}
|
|
119
117
|
}
|
|
120
118
|
|
|
121
|
-
this.nabu.commit();
|
|
122
|
-
|
|
123
|
-
// 4. On replace le curseur au début du nouvel item
|
|
124
|
-
setTimeout(() => {
|
|
125
|
-
this.nabu.selection.setCursor(newItem, 0);
|
|
126
|
-
}, 0);
|
|
119
|
+
this.nabu.commit().then(() => this.nabu.selection.setCursor(newItem, 0));
|
|
127
120
|
|
|
128
121
|
return { block: newItem };
|
|
129
122
|
}
|
|
130
123
|
|
|
124
|
+
/**
|
|
125
|
+
* @param {import('../../utils/extensions.js').PasteBlock} pb
|
|
126
|
+
* @param {{ recurse: (child: import('../../utils/extensions.js').PasteBlock, ctx?: object) => string, depth?: number }} helpers
|
|
127
|
+
*/
|
|
128
|
+
static toMarkdown(pb, { recurse, depth = 0 }) {
|
|
129
|
+
const listType = pb.props?.listType ?? 'bullet';
|
|
130
|
+
return (pb.children || [])
|
|
131
|
+
.map((child, i) => recurse(child, { listType, index: i, depth }))
|
|
132
|
+
.join('\n');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* @param {import('../../utils/extensions.js').PasteBlock} pb
|
|
137
|
+
* @param {{ recurse: (child: import('../../utils/extensions.js').PasteBlock) => string }} helpers
|
|
138
|
+
*/
|
|
139
|
+
static toHtml(pb, { recurse }) {
|
|
140
|
+
const tag = pb.props?.listType === 'ordered' ? 'ol' : 'ul';
|
|
141
|
+
return `<${tag}>${(pb.children || []).map(recurse).join('')}</${tag}>`;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
static markdownRules = [
|
|
145
|
+
{
|
|
146
|
+
priority: 10,
|
|
147
|
+
detect: /^[ \t]*([-*+]|\d+[.)]) /,
|
|
148
|
+
consume(/** @type {string[]} */ lines, /** @type {number} */ i) {
|
|
149
|
+
const LIST_RE = /^[ \t]*([-*+]|\d+[.)]) /;
|
|
150
|
+
let j = i;
|
|
151
|
+
while (j < lines.length && lines[j].trim() && LIST_RE.test(lines[j])) j++;
|
|
152
|
+
return Math.max(1, j - i);
|
|
153
|
+
},
|
|
154
|
+
}
|
|
155
|
+
];
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Parse markdown list lines into a nested List/ListItem PasteBlock tree.
|
|
159
|
+
* Uses indent depth to reconstruct nesting recursively.
|
|
160
|
+
*
|
|
161
|
+
* @param {string[]} lines
|
|
162
|
+
* @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[] }} helpers
|
|
163
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
164
|
+
*/
|
|
165
|
+
static fromMarkdown(lines, { parseInline }) {
|
|
166
|
+
/** @typedef {{ depth: number, ordered: boolean, text: string }} ListToken */
|
|
167
|
+
|
|
168
|
+
/** @type {ListToken[]} */
|
|
169
|
+
const tokens = lines.flatMap(line => {
|
|
170
|
+
const m = line.match(/^([ \t]*)([-*+]|\d+[.)]) +(.*)/);
|
|
171
|
+
if (!m) return [];
|
|
172
|
+
return [{ depth: m[1].length, ordered: /^\d/.test(m[2]), text: m[3] }];
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
if (!tokens.length) return null;
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Recursively build a List PasteBlock from a flat token array.
|
|
179
|
+
* Items at `baseDepth` become direct children; deeper tokens become sublists.
|
|
180
|
+
* @param {ListToken[]} toks
|
|
181
|
+
* @returns {import('../../utils/extensions.js').PasteBlock}
|
|
182
|
+
*/
|
|
183
|
+
function buildList(toks) {
|
|
184
|
+
const baseDepth = Math.min(...toks.map(t => t.depth));
|
|
185
|
+
const listType = toks.find(t => t.depth === baseDepth)?.ordered ? 'ordered' : 'bullet';
|
|
186
|
+
/** @type {import('../../utils/extensions.js').PasteBlock[]} */
|
|
187
|
+
const items = [];
|
|
188
|
+
let i = 0;
|
|
189
|
+
|
|
190
|
+
while (i < toks.length) {
|
|
191
|
+
if (toks[i].depth !== baseDepth) { i++; continue; }
|
|
192
|
+
|
|
193
|
+
// Collect all sub-tokens directly following this item (deeper indent)
|
|
194
|
+
const subToks = /** @type {ListToken[]} */ ([]);
|
|
195
|
+
let j = i + 1;
|
|
196
|
+
while (j < toks.length && toks[j].depth > baseDepth) {
|
|
197
|
+
subToks.push(toks[j]);
|
|
198
|
+
j++;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** @type {import('../../utils/extensions.js').PasteBlock} */
|
|
202
|
+
const item = { type: 'list-item', delta: parseInline(toks[i].text), partial: false };
|
|
203
|
+
if (subToks.length) item.children = [buildList(subToks)];
|
|
204
|
+
|
|
205
|
+
items.push(item);
|
|
206
|
+
i = j;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return { type: 'list', props: { listType }, children: items, partial: false };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return buildList(tokens);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
static htmlRules = [
|
|
216
|
+
{ selector: 'ul', props: { listType: 'bullet' } },
|
|
217
|
+
{ selector: 'ol', props: { listType: 'ordered' } },
|
|
218
|
+
];
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* @param {Element} el
|
|
222
|
+
* @param {{ parseChildren: (el: Element) => import('../../utils/extensions.js').PasteBlock[] }} helpers
|
|
223
|
+
* @param {{ props?: { listType?: string } }} [rule]
|
|
224
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
225
|
+
*/
|
|
226
|
+
static fromHTML(el, { parseChildren }, rule) {
|
|
227
|
+
const children = parseChildren(el);
|
|
228
|
+
if (!children.length) return null;
|
|
229
|
+
return { type: 'list', props: { listType: rule?.props?.listType ?? 'bullet' }, children, partial: false };
|
|
230
|
+
}
|
|
231
|
+
|
|
131
232
|
/** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
132
233
|
static create(nabu, type, props = {}, parentId = null, index = null) {
|
|
133
234
|
const node = nabu.tree.createNode(parentId || undefined, index != null ? index : undefined);
|
|
@@ -7,5 +7,32 @@ export class MegaBlock extends Block {
|
|
|
7
7
|
updateChildren(): void;
|
|
8
8
|
/** @param {Block[]} children @param {number | null} [index] */
|
|
9
9
|
adoptChildren(children: Block[], index?: number | null): void;
|
|
10
|
+
/**
|
|
11
|
+
* Props to include in the clipboard representation of this container block.
|
|
12
|
+
* Override in subclasses that carry structural props (e.g. List → { listType }).
|
|
13
|
+
* @returns {Record<string, any>}
|
|
14
|
+
*/
|
|
15
|
+
get clipboardProps(): Record<string, any>;
|
|
16
|
+
/**
|
|
17
|
+
* Serializes the portion of this MegaBlock that falls within the given
|
|
18
|
+
* selection boundaries, recursively.
|
|
19
|
+
*
|
|
20
|
+
* For hybrid blocks (MegaBlock + text behavior, like ListItem):
|
|
21
|
+
* if startBlock or endBlock IS this block, serialize text directly.
|
|
22
|
+
* if neither boundary is set, this block is fully selected — serialize all text.
|
|
23
|
+
*
|
|
24
|
+
* For pure containers (like List):
|
|
25
|
+
* find direct children that contain/are the boundaries, slice children in range,
|
|
26
|
+
* recurse into each with propagated context.
|
|
27
|
+
*
|
|
28
|
+
* @param {{ startBlock?: Block|null, startFrom?: number, endBlock?: Block|null, endTo?: number|null }} [ctx]
|
|
29
|
+
* @returns {import('../utils/extensions.js').PasteBlock | null}
|
|
30
|
+
*/
|
|
31
|
+
serializeForClipboard({ startBlock, startFrom, endBlock, endTo }?: {
|
|
32
|
+
startBlock?: Block | null;
|
|
33
|
+
startFrom?: number;
|
|
34
|
+
endBlock?: Block | null;
|
|
35
|
+
endTo?: number | null;
|
|
36
|
+
}): import("../utils/extensions.js").PasteBlock | null;
|
|
10
37
|
}
|
|
11
38
|
import { Block } from "./block.svelte.js";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Block } from "./block.svelte.js";
|
|
2
|
-
import { handleContainerBeforeInput } from "./container.utils.js";
|
|
2
|
+
import { handleContainerBeforeInput, findDirectChildOf } from "./container.utils.js";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @import { Nabu, NabuNode } from "./nabu.svelte.js";
|
|
@@ -15,6 +15,24 @@ export class MegaBlock extends Block {
|
|
|
15
15
|
|
|
16
16
|
/** @type {Block[]} */
|
|
17
17
|
children = $state([]);
|
|
18
|
+
positions = $derived(this.children.reduce((sum, child) => sum + (child.positions ?? 0), 0));
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Pure container: no own text, delegate entirely to children.
|
|
22
|
+
* @param {number} localOffset
|
|
23
|
+
* @returns {{ block: Block, offset: number } | null}
|
|
24
|
+
*/
|
|
25
|
+
resolveOffset(localOffset) {
|
|
26
|
+
let remaining = localOffset;
|
|
27
|
+
for (const child of this.children) {
|
|
28
|
+
const childPositions = child.positions ?? 0;
|
|
29
|
+
if (remaining < childPositions) return child.resolveOffset(remaining);
|
|
30
|
+
remaining -= childPositions;
|
|
31
|
+
}
|
|
32
|
+
// Fallback: clamp to end of last child
|
|
33
|
+
const last = this.children[this.children.length - 1];
|
|
34
|
+
return last ? last.resolveOffset(Math.max(0, (last.positions ?? 1) - 1)) : null;
|
|
35
|
+
}
|
|
18
36
|
|
|
19
37
|
updateChildren() {
|
|
20
38
|
const childrenNodes = this.node.children();
|
|
@@ -55,4 +73,88 @@ export class MegaBlock extends Block {
|
|
|
55
73
|
beforeinput(event) {
|
|
56
74
|
return handleContainerBeforeInput(this, this.nabu, event);
|
|
57
75
|
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Props to include in the clipboard representation of this container block.
|
|
79
|
+
* Override in subclasses that carry structural props (e.g. List → { listType }).
|
|
80
|
+
* @returns {Record<string, any>}
|
|
81
|
+
*/
|
|
82
|
+
get clipboardProps() { return {}; }
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Serializes the portion of this MegaBlock that falls within the given
|
|
86
|
+
* selection boundaries, recursively.
|
|
87
|
+
*
|
|
88
|
+
* For hybrid blocks (MegaBlock + text behavior, like ListItem):
|
|
89
|
+
* if startBlock or endBlock IS this block, serialize text directly.
|
|
90
|
+
* if neither boundary is set, this block is fully selected — serialize all text.
|
|
91
|
+
*
|
|
92
|
+
* For pure containers (like List):
|
|
93
|
+
* find direct children that contain/are the boundaries, slice children in range,
|
|
94
|
+
* recurse into each with propagated context.
|
|
95
|
+
*
|
|
96
|
+
* @param {{ startBlock?: Block|null, startFrom?: number, endBlock?: Block|null, endTo?: number|null }} [ctx]
|
|
97
|
+
* @returns {import('../utils/extensions.js').PasteBlock | null}
|
|
98
|
+
*/
|
|
99
|
+
serializeForClipboard({ startBlock = null, startFrom = 0, endBlock = null, endTo = null } = {}) {
|
|
100
|
+
const tb = this.behaviors?.get('text');
|
|
101
|
+
const imStart = startBlock === this;
|
|
102
|
+
const imEnd = endBlock === this;
|
|
103
|
+
|
|
104
|
+
// ── Hybrid block (has text): this block IS a selection boundary or fully selected ──
|
|
105
|
+
if (tb && (imStart || imEnd || (!startBlock && !endBlock))) {
|
|
106
|
+
const from = imStart ? startFrom : 0;
|
|
107
|
+
const to = imEnd ? endTo : tb.container.length;
|
|
108
|
+
const partial = imStart && imEnd ? 'both' : imStart ? 'start' : imEnd ? 'end' : false;
|
|
109
|
+
const delta = tb.container.sliceDelta(from, to ?? tb.container.length);
|
|
110
|
+
const result = /** @type {import('../utils/extensions.js').PasteBlock} */ (
|
|
111
|
+
{ type: this.type, props: this.clipboardProps, partial, delta }
|
|
112
|
+
);
|
|
113
|
+
// If fully selected and has children (e.g. ListItem with sublist), include them too
|
|
114
|
+
if (!imStart && !imEnd && this.children?.length) {
|
|
115
|
+
const childrenSerialized = this.children.map(child =>
|
|
116
|
+
typeof child.serializeForClipboard === 'function'
|
|
117
|
+
? child.serializeForClipboard({})
|
|
118
|
+
: child.serialize('application/x-nabu+json', { partial: false })
|
|
119
|
+
).filter(Boolean);
|
|
120
|
+
if (childrenSerialized.length) result.children = childrenSerialized;
|
|
121
|
+
}
|
|
122
|
+
return result;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ── Pure container: find relevant children by walking up from boundaries ──
|
|
126
|
+
if (!this.children?.length) return null;
|
|
127
|
+
|
|
128
|
+
const sc = (startBlock ? findDirectChildOf(startBlock, this) : null) ?? this.children[0];
|
|
129
|
+
const ec = (endBlock ? findDirectChildOf(endBlock, this) : null) ?? this.children.at(-1);
|
|
130
|
+
if (!sc || !ec) return null;
|
|
131
|
+
|
|
132
|
+
const si = this.children.indexOf(sc);
|
|
133
|
+
const ei = this.children.indexOf(ec);
|
|
134
|
+
if (si === -1 || ei === -1) return null;
|
|
135
|
+
|
|
136
|
+
const relevant = this.children.slice(si, ei + 1);
|
|
137
|
+
const lastI = relevant.length - 1;
|
|
138
|
+
|
|
139
|
+
const children = relevant.map((child, i) => {
|
|
140
|
+
const isFirst = i === 0;
|
|
141
|
+
const isLast = i === lastI;
|
|
142
|
+
const childCtx = {
|
|
143
|
+
startBlock: isFirst ? startBlock : null,
|
|
144
|
+
startFrom: isFirst ? startFrom : 0,
|
|
145
|
+
endBlock: isLast ? endBlock : null,
|
|
146
|
+
endTo: isLast ? endTo : null,
|
|
147
|
+
};
|
|
148
|
+
if (typeof child.serializeForClipboard === 'function') {
|
|
149
|
+
return child.serializeForClipboard(childCtx);
|
|
150
|
+
}
|
|
151
|
+
return child.serialize('application/x-nabu+json', {
|
|
152
|
+
from: isFirst && startBlock ? startFrom : 0,
|
|
153
|
+
to: isLast && endBlock ? endTo : null,
|
|
154
|
+
partial: isFirst && isLast ? 'both' : isFirst ? 'start' : isLast ? 'end' : false,
|
|
155
|
+
});
|
|
156
|
+
}).filter(Boolean);
|
|
157
|
+
|
|
158
|
+
return children.length ? { type: this.type, props: this.clipboardProps, children } : null;
|
|
159
|
+
}
|
|
58
160
|
}
|