@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
|
@@ -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 "..";
|
|
@@ -11,6 +11,71 @@ import { TextBehavior } from "../../behaviors/text";
|
|
|
11
11
|
* @typedef {NabuNode<{type: "list-item", text: LoroText}>} ListItemNode
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Indent a list item (Tab behavior): move into previous sibling's sublist, hoist own children.
|
|
16
|
+
* @param {ListItem} item
|
|
17
|
+
* @returns {boolean}
|
|
18
|
+
*/
|
|
19
|
+
export function indentListItem(item) {
|
|
20
|
+
const currentIndex = item.node.index();
|
|
21
|
+
if (currentIndex === 0) return false;
|
|
22
|
+
|
|
23
|
+
const parentList = item.node.parent();
|
|
24
|
+
if (!parentList) return false;
|
|
25
|
+
|
|
26
|
+
const previousNode = parentList.children()?.[currentIndex - 1];
|
|
27
|
+
if (!previousNode) return false;
|
|
28
|
+
const previousItem = item.nabu.blocks.get(previousNode.id.toString());
|
|
29
|
+
if (!previousItem || previousItem.type !== 'list-item') return false;
|
|
30
|
+
|
|
31
|
+
// @ts-ignore
|
|
32
|
+
const targetSublist = previousItem.getOrCreateSublist();
|
|
33
|
+
const targetSublistId = targetSublist.node.id.toString();
|
|
34
|
+
|
|
35
|
+
item.nabu.tree.move(item.node.id.toString(), targetSublistId);
|
|
36
|
+
|
|
37
|
+
if (item.sublist) {
|
|
38
|
+
for (const childNode of item.sublist.node.children()) {
|
|
39
|
+
item.nabu.tree.move(childNode.id.toString(), targetSublistId);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
item.commit().then(() => item.behavior.block.focus());
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Unindent a list item (Shift+Tab behavior): move out of parent sublist, carry followers.
|
|
49
|
+
* @param {ListItem} item
|
|
50
|
+
* @returns {boolean}
|
|
51
|
+
*/
|
|
52
|
+
export function unindentListItem(item) {
|
|
53
|
+
const parentListNode = item.node.parent();
|
|
54
|
+
if (!parentListNode) return false;
|
|
55
|
+
|
|
56
|
+
const grandParentItemNode = parentListNode.parent();
|
|
57
|
+
if (!grandParentItemNode || grandParentItemNode.data.get('type') !== 'list-item') return false;
|
|
58
|
+
|
|
59
|
+
const myIndex = item.node.index();
|
|
60
|
+
const followers = parentListNode.children().slice(myIndex + 1);
|
|
61
|
+
|
|
62
|
+
if (followers.length > 0) {
|
|
63
|
+
const mySublist = item.getOrCreateSublist();
|
|
64
|
+
for (const follower of followers) {
|
|
65
|
+
item.nabu.tree.move(follower.id.toString(), mySublist.node.id.toString());
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const targetParentListNode = grandParentItemNode.parent();
|
|
70
|
+
if (!targetParentListNode) return false;
|
|
71
|
+
|
|
72
|
+
const targetIndex = grandParentItemNode.index() + 1;
|
|
73
|
+
item.nabu.tree.move(item.node.id.toString(), targetParentListNode.id.toString(), targetIndex);
|
|
74
|
+
|
|
75
|
+
item.commit().then(() => item.behavior.block.focus());
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
|
|
14
79
|
export class ListItem extends MegaBlock {
|
|
15
80
|
/** @param {Nabu} nabu @param {ListItemNode} node */
|
|
16
81
|
constructor(nabu, node) {
|
|
@@ -24,7 +89,7 @@ export class ListItem extends MegaBlock {
|
|
|
24
89
|
this.behaviors.set("text", this.behavior);
|
|
25
90
|
|
|
26
91
|
this.serializers.set('markdown', () => {
|
|
27
|
-
const depth = this.parents.filter(p => p.type === '
|
|
92
|
+
const depth = this.parents.filter(p => p.type === 'list-item').length;
|
|
28
93
|
const indent = ' '.repeat(depth);
|
|
29
94
|
const listType = /** @type {any} */ (this.parent)?.listType ?? 'bullet';
|
|
30
95
|
const prefix = listType === 'ordered' ? `${this.index + 1}.` : '-';
|
|
@@ -46,6 +111,7 @@ export class ListItem extends MegaBlock {
|
|
|
46
111
|
if (childrenJson.length) result.children = childrenJson;
|
|
47
112
|
return result;
|
|
48
113
|
});
|
|
114
|
+
this.serializers.set('application/x-nabu+json', (ctx = {}) => this.behavior.toClipboardBlock(ctx));
|
|
49
115
|
}
|
|
50
116
|
|
|
51
117
|
component = $derived(this.nabu.components.get("list-item") || ListItemComponent);
|
|
@@ -60,6 +126,27 @@ export class ListItem extends MegaBlock {
|
|
|
60
126
|
|
|
61
127
|
selection = $derived(this.behavior.selection);
|
|
62
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
|
+
|
|
63
150
|
sublist = $derived(this.children.find(child => child.node.data.get("type") === "list"));
|
|
64
151
|
|
|
65
152
|
/**
|
|
@@ -93,94 +180,15 @@ export class ListItem extends MegaBlock {
|
|
|
93
180
|
|
|
94
181
|
/** @param {KeyboardEvent} event */
|
|
95
182
|
keydown(event) {
|
|
96
|
-
if (event.key ===
|
|
97
|
-
event.preventDefault();
|
|
98
|
-
|
|
183
|
+
if (event.key === 'Tab') {
|
|
184
|
+
event.preventDefault();
|
|
99
185
|
if (event.shiftKey) {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
// 1. Notre parent immédiat (une List)
|
|
103
|
-
const parentListNode = this.node.parent();
|
|
104
|
-
if (!parentListNode) return true;
|
|
105
|
-
|
|
106
|
-
// 2. Le ListItem qui contient cette liste (le "grand-parent" logique)
|
|
107
|
-
const grandParentItemNode = parentListNode.parent();
|
|
108
|
-
|
|
109
|
-
// Si on n'a pas de ListItem au-dessus (on est déjà au premier niveau)
|
|
110
|
-
if (!grandParentItemNode || grandParentItemNode.data.get("type") !== "list-item") {
|
|
111
|
-
return true;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// 3. Gérer le "Carry" : emporter les frères suivants avec nous
|
|
115
|
-
const myIndex = this.node.index();
|
|
116
|
-
const siblings = parentListNode.children();
|
|
117
|
-
const followers = siblings.slice(myIndex + 1);
|
|
118
|
-
|
|
119
|
-
if (followers.length > 0) {
|
|
120
|
-
// On s'assure d'avoir notre propre sous-liste pour accueillir nos anciens frères
|
|
121
|
-
const mySublist = this.getOrCreateSublist();
|
|
122
|
-
for (const follower of followers) {
|
|
123
|
-
this.nabu.tree.move(follower.id.toString(), mySublist.node.id.toString());
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// 4. Le parent cible est la List qui contient notre grand-parent
|
|
128
|
-
const targetParentListNode = grandParentItemNode.parent();
|
|
129
|
-
if (!targetParentListNode) return true;
|
|
130
|
-
|
|
131
|
-
// 5. On se place juste après notre ancien grand-parent
|
|
132
|
-
const targetIndex = grandParentItemNode.index() + 1;
|
|
133
|
-
|
|
134
|
-
this.nabu.tree.move(this.node.id.toString(), targetParentListNode.id.toString(), targetIndex);
|
|
135
|
-
|
|
136
|
-
this.commit();
|
|
137
|
-
|
|
138
|
-
// 6. Restauration du focus
|
|
139
|
-
setTimeout(() => this.behavior.block.focus(), 0);
|
|
186
|
+
unindentListItem(this);
|
|
140
187
|
} else {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
// 1. On trouve notre index actuel
|
|
144
|
-
const currentIndex = this.node.index();
|
|
145
|
-
if (currentIndex === 0) return true; // Impossible d'indenter le premier item
|
|
146
|
-
|
|
147
|
-
// 2. On trouve le frère précédent (qui va devenir notre parent "logique")
|
|
148
|
-
const parentList = this.node.parent();
|
|
149
|
-
if (!parentList) return true;
|
|
150
|
-
|
|
151
|
-
const previousNode = parentList.children()?.[currentIndex - 1];
|
|
152
|
-
if (!previousNode) return true;
|
|
153
|
-
const previousItem = this.nabu.blocks.get(previousNode.id.toString());
|
|
154
|
-
|
|
155
|
-
// Si le frère précédent n'est pas un ListItem
|
|
156
|
-
if (!previousItem || previousItem.type !== "list-item") return true;
|
|
157
|
-
|
|
158
|
-
// 3. On demande au frère précédent sa sous-liste (ou de la créer)
|
|
159
|
-
// @ts-ignore : On sait que c'est un ListItem
|
|
160
|
-
const targetSublist = previousItem.getOrCreateSublist();
|
|
161
|
-
const targetSublistId = targetSublist.node.id.toString();
|
|
162
|
-
|
|
163
|
-
// 4. On se déplace à la fin de cette sous-liste cible
|
|
164
|
-
this.nabu.tree.move(this.node.id.toString(), targetSublistId);
|
|
165
|
-
|
|
166
|
-
// 5. Si on avait des enfants (Hoist), on les remonte avec nous en tant que frères
|
|
167
|
-
if (this.sublist) {
|
|
168
|
-
const childrenNodes = this.sublist.node.children();
|
|
169
|
-
for (const childNode of childrenNodes) {
|
|
170
|
-
// Ils se placeront après nous dans la nouvelle liste
|
|
171
|
-
this.nabu.tree.move(childNode.id.toString(), targetSublistId);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
this.commit();
|
|
176
|
-
|
|
177
|
-
// 6. On restaure le curseur
|
|
178
|
-
setTimeout(() => this.behavior.block.focus(), 0);
|
|
188
|
+
indentListItem(this);
|
|
179
189
|
}
|
|
180
190
|
return true;
|
|
181
191
|
}
|
|
182
|
-
|
|
183
|
-
// On délègue les flèches etc.
|
|
184
192
|
return false;
|
|
185
193
|
}
|
|
186
194
|
|
|
@@ -217,6 +225,49 @@ export class ListItem extends MegaBlock {
|
|
|
217
225
|
});
|
|
218
226
|
}
|
|
219
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
|
+
|
|
220
271
|
/** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
221
272
|
static create(nabu, type, props = {}, parentId = null, index = null) {
|
|
222
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
|
}
|