@aionbuilders/nabu 0.1.0-alpha.9 → 0.1.0-beta.0
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 +216 -9
- package/dist/behaviors/text/RichText.svelte +4 -1
- package/dist/behaviors/text/index.d.ts +2 -0
- package/dist/behaviors/text/index.js +2 -0
- package/dist/behaviors/text/markdown-shortcuts.extension.d.ts +2 -0
- package/dist/behaviors/text/markdown-shortcuts.extension.js +54 -0
- package/dist/behaviors/text/rich-text.extension.js +47 -22
- package/dist/behaviors/text/text.behavior.svelte.d.ts +25 -1
- package/dist/behaviors/text/text.behavior.svelte.js +154 -18
- package/dist/behaviors/text/typography.extension.d.ts +6 -0
- package/dist/behaviors/text/typography.extension.js +24 -0
- package/dist/blocks/Block.svelte +5 -2
- package/dist/blocks/Nabu.svelte +22 -0
- package/dist/blocks/block.extension.js +10 -8
- package/dist/blocks/block.svelte.d.ts +24 -0
- package/dist/blocks/block.svelte.js +35 -0
- package/dist/blocks/blockquote/Blockquote.svelte +40 -0
- package/dist/blocks/blockquote/Blockquote.svelte.d.ts +11 -0
- package/dist/blocks/blockquote/blockquote.extension.d.ts +1 -0
- package/dist/blocks/blockquote/blockquote.extension.js +71 -0
- package/dist/blocks/blockquote/blockquote.svelte.d.ts +55 -0
- package/dist/blocks/blockquote/blockquote.svelte.js +91 -0
- package/dist/blocks/blockquote/index.d.ts +3 -0
- package/dist/blocks/blockquote/index.js +3 -0
- package/dist/blocks/callout/Callout.svelte +69 -0
- package/dist/blocks/callout/Callout.svelte.d.ts +11 -0
- package/dist/blocks/callout/callout.extension.d.ts +1 -0
- package/dist/blocks/callout/callout.extension.js +68 -0
- package/dist/blocks/callout/callout.svelte.d.ts +78 -0
- package/dist/blocks/callout/callout.svelte.js +184 -0
- package/dist/blocks/callout/index.d.ts +3 -0
- package/dist/blocks/callout/index.js +3 -0
- package/dist/blocks/code/Code.svelte +85 -0
- package/dist/blocks/code/Code.svelte.d.ts +11 -0
- package/dist/blocks/code/code.extension.d.ts +1 -0
- package/dist/blocks/code/code.extension.js +68 -0
- package/dist/blocks/code/code.svelte.d.ts +61 -0
- package/dist/blocks/code/code.svelte.js +221 -0
- package/dist/blocks/code/index.d.ts +3 -0
- package/dist/blocks/code/index.js +3 -0
- package/dist/blocks/dialogue/Dialogue.svelte +2 -0
- package/dist/blocks/dialogue/dialogue.svelte.d.ts +3 -21
- package/dist/blocks/dialogue/dialogue.svelte.js +2 -50
- package/dist/blocks/divider/Divider.svelte +28 -0
- package/dist/blocks/divider/Divider.svelte.d.ts +11 -0
- package/dist/blocks/divider/divider.extension.d.ts +1 -0
- package/dist/blocks/divider/divider.extension.js +87 -0
- package/dist/blocks/divider/divider.svelte.d.ts +49 -0
- package/dist/blocks/divider/divider.svelte.js +84 -0
- package/dist/blocks/divider/index.d.ts +3 -0
- package/dist/blocks/divider/index.js +3 -0
- package/dist/blocks/file/File.svelte +49 -0
- package/dist/blocks/file/File.svelte.d.ts +11 -0
- package/dist/blocks/file/file.extension.d.ts +1 -0
- package/dist/blocks/file/file.extension.js +19 -0
- package/dist/blocks/file/file.svelte.d.ts +29 -0
- package/dist/blocks/file/file.svelte.js +71 -0
- package/dist/blocks/file/index.d.ts +3 -0
- package/dist/blocks/file/index.js +3 -0
- package/dist/blocks/heading/Heading.svelte +3 -1
- package/dist/blocks/heading/heading.svelte.d.ts +4 -23
- package/dist/blocks/heading/heading.svelte.js +4 -47
- package/dist/blocks/image/Image.svelte +38 -0
- package/dist/blocks/image/Image.svelte.d.ts +11 -0
- package/dist/blocks/image/image.extension.d.ts +2 -0
- package/dist/blocks/image/image.extension.js +26 -0
- package/dist/blocks/image/image.svelte.d.ts +50 -0
- package/dist/blocks/image/image.svelte.js +92 -0
- package/dist/blocks/image/index.d.ts +3 -0
- package/dist/blocks/image/index.js +3 -0
- package/dist/blocks/index.d.ts +10 -0
- package/dist/blocks/index.js +10 -0
- package/dist/blocks/list/List.svelte +8 -2
- package/dist/blocks/list/ListItem.svelte +43 -7
- package/dist/blocks/list/hooks/onBeforeInput.hook.js +15 -2
- package/dist/blocks/list/index.d.ts +1 -0
- package/dist/blocks/list/index.js +1 -0
- package/dist/blocks/list/list-item.svelte.d.ts +14 -2
- package/dist/blocks/list/list-item.svelte.js +58 -7
- package/dist/blocks/list/list.behavior.svelte.d.ts +4 -4
- package/dist/blocks/list/list.behavior.svelte.js +5 -5
- package/dist/blocks/list/list.svelte.d.ts +13 -6
- package/dist/blocks/list/list.svelte.js +24 -6
- package/dist/blocks/list/todos.svelte.d.ts +35 -0
- package/dist/blocks/list/todos.svelte.js +76 -0
- package/dist/blocks/media/index.d.ts +2 -0
- package/dist/blocks/media/index.js +2 -0
- package/dist/blocks/media/media-block.svelte.d.ts +32 -0
- package/dist/blocks/media/media-block.svelte.js +69 -0
- package/dist/blocks/media/media.helpers.d.ts +19 -0
- package/dist/blocks/media/media.helpers.js +51 -0
- package/dist/blocks/megablock.svelte.js +1 -0
- package/dist/blocks/nabu.svelte.d.ts +94 -16
- package/dist/blocks/nabu.svelte.js +254 -3
- package/dist/blocks/paragraph/Paragraph.svelte +2 -0
- package/dist/blocks/paragraph/index.js +0 -3
- package/dist/blocks/paragraph/paragraph.svelte.d.ts +3 -21
- package/dist/blocks/paragraph/paragraph.svelte.js +4 -53
- package/dist/blocks/selection.svelte.js +11 -1
- package/dist/blocks/text-block.svelte.d.ts +49 -0
- package/dist/blocks/text-block.svelte.js +74 -0
- package/dist/blocks/toggle/Toggle.svelte +84 -0
- package/dist/blocks/toggle/Toggle.svelte.d.ts +11 -0
- package/dist/blocks/toggle/index.d.ts +3 -0
- package/dist/blocks/toggle/index.js +3 -0
- package/dist/blocks/toggle/toggle.extension.d.ts +1 -0
- package/dist/blocks/toggle/toggle.extension.js +12 -0
- package/dist/blocks/toggle/toggle.svelte.d.ts +88 -0
- package/dist/blocks/toggle/toggle.svelte.js +252 -0
- package/dist/blocks/variable.svelte.d.ts +47 -0
- package/dist/blocks/variable.svelte.js +51 -0
- package/dist/blocks/video/Video.svelte +39 -0
- package/dist/blocks/video/Video.svelte.d.ts +11 -0
- package/dist/blocks/video/index.d.ts +3 -0
- package/dist/blocks/video/index.js +3 -0
- package/dist/blocks/video/video.extension.d.ts +1 -0
- package/dist/blocks/video/video.extension.js +19 -0
- package/dist/blocks/video/video.svelte.d.ts +28 -0
- package/dist/blocks/video/video.svelte.js +71 -0
- package/dist/blocks/zone/TemplateEditor.svelte +308 -0
- package/dist/blocks/zone/TemplateEditor.svelte.d.ts +11 -0
- package/dist/blocks/zone/Zone.svelte +158 -144
- package/dist/blocks/zone/ZoneEditor.svelte +289 -0
- package/dist/blocks/zone/ZoneEditor.svelte.d.ts +11 -0
- package/dist/blocks/zone/ZoneTemplatesList.svelte +188 -0
- package/dist/blocks/zone/ZoneTemplatesList.svelte.d.ts +11 -0
- package/dist/blocks/zone/hooks/onBeforeInput.hook.d.ts +3 -0
- package/dist/blocks/zone/hooks/onBeforeInput.hook.js +30 -0
- package/dist/blocks/zone/zone.extension.js +22 -6
- package/dist/blocks/zone/zone.svelte.d.ts +111 -2
- package/dist/blocks/zone/zone.svelte.js +304 -17
- package/dist/files/files.svelte.d.ts +61 -0
- package/dist/files/files.svelte.js +93 -0
- package/dist/files/index.d.ts +1 -0
- package/dist/files/index.js +1 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +5 -2
- package/dist/nabu-theme.css +92 -0
- package/dist/overlays/context/Context.svelte +182 -0
- package/dist/overlays/context/Context.svelte.d.ts +17 -0
- package/dist/overlays/context/collect.d.ts +17 -0
- package/dist/overlays/context/collect.js +33 -0
- package/dist/overlays/context/context.extension.d.ts +2 -0
- package/dist/overlays/context/context.extension.js +8 -0
- package/dist/overlays/context/context.svelte.d.ts +82 -0
- package/dist/overlays/context/context.svelte.js +159 -0
- package/dist/overlays/context/index.d.ts +3 -0
- package/dist/overlays/context/index.js +3 -0
- package/dist/paste/html.paste.js +9 -1
- package/dist/presets.d.ts +3 -1
- package/dist/presets.js +21 -6
- package/dist/ui/DragHandle.svelte +141 -0
- package/dist/ui/DragHandle.svelte.d.ts +11 -0
- package/dist/ui/FloatingToolbar.svelte +117 -0
- package/dist/ui/FloatingToolbar.svelte.d.ts +17 -0
- package/dist/ui/SlashMenu.svelte +83 -0
- package/dist/ui/SlashMenu.svelte.d.ts +13 -0
- package/dist/ui/index.d.ts +4 -0
- package/dist/ui/index.js +4 -0
- package/dist/ui/slash-menu.svelte.d.ts +40 -0
- package/dist/ui/slash-menu.svelte.js +116 -0
- package/dist/utils/extensions.d.ts +54 -0
- package/dist/utils/extensions.js +40 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/placeholders.d.ts +10 -0
- package/dist/utils/placeholders.js +21 -0
- package/dist/vite.d.ts +16 -0
- package/dist/vite.js +23 -0
- package/package.json +10 -5
|
@@ -3,6 +3,7 @@ import { SvelteMap, SvelteSet } from 'svelte/reactivity';
|
|
|
3
3
|
import { Block } from './block.svelte.js';
|
|
4
4
|
import { NabuSelection } from './selection.svelte.js';
|
|
5
5
|
import { handleContainerBeforeInput, wrapOrphan, deleteSelectionContent, findDirectChildOf, findLCA } from './container.utils.js';
|
|
6
|
+
import { jsonToPasteBlocks } from '../utils/extensions.js';
|
|
6
7
|
import { tick } from 'svelte';
|
|
7
8
|
import { Pulse } from '@aionbuilders/pulse';
|
|
8
9
|
|
|
@@ -22,14 +23,45 @@ import { Pulse } from '@aionbuilders/pulse';
|
|
|
22
23
|
* @typedef {Object} NabuInit
|
|
23
24
|
* @property {Extension[]} [extensions]
|
|
24
25
|
* @property {Uint8Array<ArrayBufferLike>} [snapshot]
|
|
26
|
+
* @property {import('../files/files.svelte.js').FilesConfig} [files]
|
|
25
27
|
*/
|
|
26
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Whether the character immediately before `pos` in a text behavior carries `name`.
|
|
31
|
+
* Used to decide the toggle direction for stored marks.
|
|
32
|
+
* @param {any} behavior @param {number} pos @param {string} name
|
|
33
|
+
* @returns {boolean}
|
|
34
|
+
*/
|
|
35
|
+
function inheritedMarkAt(behavior, pos, name) {
|
|
36
|
+
if (pos <= 0) return false;
|
|
37
|
+
const delta = behavior.delta;
|
|
38
|
+
let offset = 0;
|
|
39
|
+
for (const op of delta) {
|
|
40
|
+
if (typeof op.insert !== 'string') continue;
|
|
41
|
+
const end = offset + op.insert.length;
|
|
42
|
+
if (pos <= end) return !!(op.attributes && op.attributes[name]);
|
|
43
|
+
offset = end;
|
|
44
|
+
}
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
|
|
27
48
|
export class Nabu {
|
|
28
49
|
/** @type {Pulse} */
|
|
29
50
|
#pulse = new Pulse();
|
|
30
51
|
/** @type {import('../utils/extensions.js').PasteInterpreter[]} */
|
|
31
52
|
#pasteInterpreters = [];
|
|
32
53
|
|
|
54
|
+
// ── IME composition state ────────────────────────────────────────────────
|
|
55
|
+
// During an active IME composition the browser mutates the DOM freely
|
|
56
|
+
// (insertCompositionText cannot be canceled). We switch to "DOM-first"
|
|
57
|
+
// for the duration of the composition and sync the result into Loro at
|
|
58
|
+
// compositionend. See issue #6.
|
|
59
|
+
#composing = false;
|
|
60
|
+
/** @type {number | null} offset modèle au début de la composition */
|
|
61
|
+
#compositionStartOffset = null;
|
|
62
|
+
/** @type {Block | null} bloc cible au début de la composition */
|
|
63
|
+
#compositionStartBlock = null;
|
|
64
|
+
|
|
33
65
|
/** @param {NabuInit} init */
|
|
34
66
|
constructor(init = {}) {
|
|
35
67
|
this.doc = new LoroDoc();
|
|
@@ -73,6 +105,7 @@ export class Nabu {
|
|
|
73
105
|
});
|
|
74
106
|
|
|
75
107
|
this.extensions = init.extensions || [];
|
|
108
|
+
this.fileConfig = init.files || {};
|
|
76
109
|
|
|
77
110
|
if (this.extensions?.length) {
|
|
78
111
|
for (const ext of this.extensions) {
|
|
@@ -105,6 +138,12 @@ export class Nabu {
|
|
|
105
138
|
}
|
|
106
139
|
}
|
|
107
140
|
|
|
141
|
+
if (ext.inputRules?.length) {
|
|
142
|
+
for (const rule of ext.inputRules) {
|
|
143
|
+
this.inputRules.push(rule);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
108
147
|
if (ext.loroTextStyles && Object.keys(ext.loroTextStyles).length) {
|
|
109
148
|
this.doc.configTextStyle(ext.loroTextStyles);
|
|
110
149
|
}
|
|
@@ -187,11 +226,33 @@ export class Nabu {
|
|
|
187
226
|
/** @type {SvelteMap<string, SvelteSet<Block>>} */
|
|
188
227
|
blocksByType = new SvelteMap();
|
|
189
228
|
|
|
190
|
-
/**
|
|
229
|
+
/**
|
|
230
|
+
* Registered subsystems. Each system should implement:
|
|
231
|
+
* - `start(): void` — called when the editor mounts
|
|
232
|
+
* - `stop(): void` — called when the editor unmounts
|
|
233
|
+
* @type {SvelteMap<string, { start?: () => void, stop?: () => void, [key: string]: any }>}
|
|
234
|
+
*/
|
|
191
235
|
systems = new SvelteMap();
|
|
192
236
|
|
|
193
237
|
hooks = new Map();
|
|
194
238
|
|
|
239
|
+
/**
|
|
240
|
+
* Declarative input rules collected from extensions, tested by TextBehavior
|
|
241
|
+
* against the text before the cursor on matching beforeinput events.
|
|
242
|
+
* @type {import('../utils/extensions.js').InputRule[]}
|
|
243
|
+
*/
|
|
244
|
+
inputRules = [];
|
|
245
|
+
|
|
246
|
+
/** @type {import('../files/files.svelte.js').FilesConfig} */
|
|
247
|
+
fileConfig = {};
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Pending inline formatting for the next typed character (active marks at a
|
|
251
|
+
* collapsed cursor). null = inherit from the span at the cursor.
|
|
252
|
+
* @type {Record<string, boolean> | null}
|
|
253
|
+
*/
|
|
254
|
+
storedMarks = $state(null);
|
|
255
|
+
|
|
195
256
|
debugging = $state(false);
|
|
196
257
|
|
|
197
258
|
/**
|
|
@@ -284,6 +345,42 @@ export class Nabu {
|
|
|
284
345
|
await tick();
|
|
285
346
|
}
|
|
286
347
|
|
|
348
|
+
/**
|
|
349
|
+
* Run Loro mutations as a single transaction, then optionally restore the cursor.
|
|
350
|
+
* Centralizes the "mutate → commit → setCursor" dance used by most actions.
|
|
351
|
+
* @param {() => void} mutateFn - Mutations Loro ici. Ne PAS appeler commit() dedans.
|
|
352
|
+
* @param {{ block: Block, offset: number } | null} [cursorIntent]
|
|
353
|
+
* @returns {Promise<void>}
|
|
354
|
+
*/
|
|
355
|
+
async transact(mutateFn, cursorIntent = null) {
|
|
356
|
+
mutateFn();
|
|
357
|
+
await this.commit();
|
|
358
|
+
if (cursorIntent) {
|
|
359
|
+
this.selection.setCursor(cursorIntent.block, cursorIntent.offset);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* True when every selected text run carries the given mark.
|
|
365
|
+
* Reactive: reads block.selected and the behaviors' selections/deltas,
|
|
366
|
+
* so it can be used directly in component templates (e.g. FloatingToolbar).
|
|
367
|
+
* @param {string} markName
|
|
368
|
+
* @returns {boolean}
|
|
369
|
+
*/
|
|
370
|
+
isMarkActive(markName) {
|
|
371
|
+
let found = false;
|
|
372
|
+
for (const [, block] of this.blocks) {
|
|
373
|
+
if (!block.selected) continue;
|
|
374
|
+
const behavior = block.behaviors?.get('text');
|
|
375
|
+
if (!behavior || typeof behavior.isMarkActive !== 'function') continue;
|
|
376
|
+
const sel = behavior.selection;
|
|
377
|
+
if (!sel || sel.isCollapsed) continue;
|
|
378
|
+
found = true;
|
|
379
|
+
if (!behavior.isMarkActive(markName, sel)) return false;
|
|
380
|
+
}
|
|
381
|
+
return found;
|
|
382
|
+
}
|
|
383
|
+
|
|
287
384
|
/**
|
|
288
385
|
* Undo the last operation
|
|
289
386
|
*/
|
|
@@ -302,6 +399,24 @@ export class Nabu {
|
|
|
302
399
|
}
|
|
303
400
|
}
|
|
304
401
|
|
|
402
|
+
/** Clear the pending stored marks. */
|
|
403
|
+
clearStoredMarks() { this.storedMarks = null; }
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Toggle a stored mark for the next typed char (Ctrl+B etc. at a collapsed cursor).
|
|
407
|
+
* @param {string} name
|
|
408
|
+
*/
|
|
409
|
+
toggleStoredMark(name) {
|
|
410
|
+
const anchor = this.selection.anchorBlock;
|
|
411
|
+
const beh = anchor?.behaviors?.get('text');
|
|
412
|
+
const sel = beh?.selection;
|
|
413
|
+
if (!beh || !sel) return;
|
|
414
|
+
let active;
|
|
415
|
+
if (this.storedMarks && name in this.storedMarks) active = this.storedMarks[name];
|
|
416
|
+
else active = inheritedMarkAt(beh, sel.from, name);
|
|
417
|
+
this.storedMarks = { ...(this.storedMarks || {}), [name]: !active };
|
|
418
|
+
}
|
|
419
|
+
|
|
305
420
|
|
|
306
421
|
|
|
307
422
|
/** @param {{from: {offset: number, block: Block}, to: {offset: number, block: Block}, offset?: number}} [options={}] */
|
|
@@ -317,12 +432,12 @@ export class Nabu {
|
|
|
317
432
|
{offset: options?.to?.offset ?? sel.endOffset ?? 0, block: options?.to?.block} :
|
|
318
433
|
this.resolveDocumentOffset(options?.offset ?? options?.to?.offset ?? sel.endOffset ?? 0) || {offset: sel.endOffset ?? 0, block: sel.focusBlock};
|
|
319
434
|
|
|
320
|
-
|
|
435
|
+
this.warn('Focusing from', fromBlock, 'offset', fromOffset, 'to', toBlock, 'offset', toOffset);
|
|
321
436
|
|
|
322
437
|
if (fromBlock && toBlock) {
|
|
323
438
|
const fromPoint = fromBlock.getDOMPoint(fromOffset);
|
|
324
439
|
const toPoint = toBlock.getDOMPoint(toOffset);
|
|
325
|
-
|
|
440
|
+
this.warn('Resolved DOM points:', fromPoint, toPoint);
|
|
326
441
|
if (fromPoint && toPoint) this.selection.setBaseAndExtent(fromPoint.node, fromPoint.offset, toPoint.node, toPoint.offset);
|
|
327
442
|
}
|
|
328
443
|
})
|
|
@@ -415,10 +530,78 @@ export class Nabu {
|
|
|
415
530
|
|
|
416
531
|
/** @param {InputEvent} e */
|
|
417
532
|
handleBeforeinput(e) {
|
|
533
|
+
// Ne pas interférer avec la composition IME en cours : le navigateur
|
|
534
|
+
// doit pouvoir muter librement le text node (insertCompositionText
|
|
535
|
+
// n'est de toute façon pas annulable). Sync dans Loro à compositionend.
|
|
536
|
+
if (this.#composing && (e.inputType === 'insertCompositionText' || e.inputType === 'deleteCompositionText')) {
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
418
539
|
e.preventDefault(); // Toujours bloquer les mutations natives
|
|
419
540
|
this.dispatchEventToSelection('beforeinput', e, 'onBeforeInput');
|
|
420
541
|
}
|
|
421
542
|
|
|
543
|
+
// ── IME composition (issue #6) ───────────────────────────────────────────
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Capture the model position at the start of an IME composition and
|
|
547
|
+
* switch to DOM-first mode for its duration.
|
|
548
|
+
* @param {CompositionEvent} e
|
|
549
|
+
*/
|
|
550
|
+
handleCompositionStart(e) {
|
|
551
|
+
this.#composing = true;
|
|
552
|
+
const anchor = this.selection.anchorBlock;
|
|
553
|
+
if (!anchor) {
|
|
554
|
+
this.#compositionStartBlock = null;
|
|
555
|
+
this.#compositionStartOffset = null;
|
|
556
|
+
return;
|
|
557
|
+
}
|
|
558
|
+
const behavior = anchor.behaviors?.get('text');
|
|
559
|
+
const sel = behavior?.selection;
|
|
560
|
+
this.#compositionStartBlock = anchor;
|
|
561
|
+
this.#compositionStartOffset = sel?.from ?? 0;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* No-op: the browser manages the DOM freely during composition.
|
|
566
|
+
* @param {CompositionEvent} e
|
|
567
|
+
*/
|
|
568
|
+
handleCompositionUpdate(e) {}
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* Sync the composed text into Loro. The browser already inserted `e.data`
|
|
572
|
+
* into the DOM; committing the same text to the model lets the next render
|
|
573
|
+
* converge without duplication.
|
|
574
|
+
* @param {CompositionEvent} e
|
|
575
|
+
*/
|
|
576
|
+
handleCompositionEnd(e) {
|
|
577
|
+
this.#composing = false;
|
|
578
|
+
const block = this.#compositionStartBlock;
|
|
579
|
+
const startOffset = this.#compositionStartOffset;
|
|
580
|
+
this.#compositionStartBlock = null;
|
|
581
|
+
this.#compositionStartOffset = null;
|
|
582
|
+
if (!block || startOffset === null) return;
|
|
583
|
+
|
|
584
|
+
// The block may have been destroyed/replaced during the composition
|
|
585
|
+
// (e.g. the user clicked elsewhere and a transform happened).
|
|
586
|
+
const fresh = this.blocks.get(block.id);
|
|
587
|
+
if (!fresh || fresh !== block) return;
|
|
588
|
+
// nabu.blocks can still hold a deleted block — check the Loro tree too.
|
|
589
|
+
if (block.node.isDeleted?.() || !this.tree.has(block.node.id)) return;
|
|
590
|
+
|
|
591
|
+
const behavior = block.behaviors?.get('text');
|
|
592
|
+
if (!behavior) return;
|
|
593
|
+
|
|
594
|
+
// Canceled composition (Escape) fires compositionend with e.data === ''.
|
|
595
|
+
const composedText = e.data || '';
|
|
596
|
+
if (!composedText) return;
|
|
597
|
+
|
|
598
|
+
behavior.insert(startOffset, composedText);
|
|
599
|
+
block.commit();
|
|
600
|
+
tick().then(() => {
|
|
601
|
+
this.selection.setCursor(block, startOffset + composedText.length);
|
|
602
|
+
});
|
|
603
|
+
}
|
|
604
|
+
|
|
422
605
|
/** @param {KeyboardEvent} e */
|
|
423
606
|
handleKeydown(e) {
|
|
424
607
|
// Undo: Ctrl+Z or Cmd+Z (without Shift)
|
|
@@ -750,6 +933,74 @@ export class Nabu {
|
|
|
750
933
|
}
|
|
751
934
|
}
|
|
752
935
|
|
|
936
|
+
// DOCUMENT IMPORT
|
|
937
|
+
|
|
938
|
+
/**
|
|
939
|
+
* Replace the entire document with the given PasteFragment.
|
|
940
|
+
* Unlike insertFragment(), this does not depend on a live DOM selection:
|
|
941
|
+
* blocks are created at the root through the same registry pipeline as paste.
|
|
942
|
+
* @param {import('../utils/extensions.js').PasteFragment} fragment
|
|
943
|
+
* @returns {Promise<void>}
|
|
944
|
+
*/
|
|
945
|
+
async importFragment(fragment) {
|
|
946
|
+
const { blocks } = fragment;
|
|
947
|
+
if (!blocks?.length) return;
|
|
948
|
+
|
|
949
|
+
[...this.tree.roots()].forEach(root => this.tree.delete(root.id));
|
|
950
|
+
|
|
951
|
+
for (const pb of blocks) {
|
|
952
|
+
const BlockClass = this.registry.get(pb.type) || this.registry.get('paragraph');
|
|
953
|
+
if (!BlockClass) continue;
|
|
954
|
+
const block = BlockClass.create(this, pb.type, pb.props || {});
|
|
955
|
+
const tb = block.behaviors?.get('text');
|
|
956
|
+
if (tb && pb.delta?.length) tb.applyDelta(pb.delta);
|
|
957
|
+
for (const child of pb.children || []) {
|
|
958
|
+
this.#createInContainer(child, block.node.id.toString());
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
await this.commit();
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
/**
|
|
966
|
+
* Replace the entire document with content parsed from a Markdown string.
|
|
967
|
+
* Requires MarkdownPasteExtension (or any text/plain interpreter) to be loaded.
|
|
968
|
+
* @param {string} markdown
|
|
969
|
+
* @returns {Promise<void>}
|
|
970
|
+
*/
|
|
971
|
+
async importMarkdown(markdown) {
|
|
972
|
+
const interpreter = [...this.#pasteInterpreters]
|
|
973
|
+
.sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0))
|
|
974
|
+
.find(i => i.format === 'text/plain');
|
|
975
|
+
if (!interpreter) {
|
|
976
|
+
this.warn('importMarkdown: MarkdownPasteExtension not loaded');
|
|
977
|
+
return;
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
const fragment = interpreter.interpret(markdown, this);
|
|
981
|
+
if (!fragment) {
|
|
982
|
+
this.warn('importMarkdown: nothing to import');
|
|
983
|
+
return;
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
await this.importFragment(fragment);
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
/**
|
|
990
|
+
* Replace the entire document with a `serialize('json')` export.
|
|
991
|
+
* @param {{version?: string, blocks: any[]} | string} json
|
|
992
|
+
* @returns {Promise<void>}
|
|
993
|
+
*/
|
|
994
|
+
async importJSON(json) {
|
|
995
|
+
const doc = typeof json === 'string' ? JSON.parse(json) : json;
|
|
996
|
+
const blocks = jsonToPasteBlocks(doc?.blocks || []);
|
|
997
|
+
if (!blocks.length) {
|
|
998
|
+
this.warn('importJSON: nothing to import');
|
|
999
|
+
return;
|
|
1000
|
+
}
|
|
1001
|
+
await this.importFragment({ blocks });
|
|
1002
|
+
}
|
|
1003
|
+
|
|
753
1004
|
/** @param {number} globalOffset */
|
|
754
1005
|
resolveDocumentOffset(globalOffset) {
|
|
755
1006
|
let remaining = globalOffset;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
<script>
|
|
2
2
|
import RichText from '../../behaviors/text/RichText.svelte';
|
|
3
|
+
import { getPlaceholder } from '../../utils/placeholders.js';
|
|
3
4
|
/** @type {{block: import('./paragraph.svelte.js').Paragraph}}*/
|
|
4
5
|
let {block} = $props();
|
|
5
6
|
</script>
|
|
@@ -8,6 +9,7 @@
|
|
|
8
9
|
bind:this={block.element}
|
|
9
10
|
data-block-id={block.id}
|
|
10
11
|
data-block-type="paragraph"
|
|
12
|
+
data-placeholder={block.text ? '' : getPlaceholder('paragraph')}
|
|
11
13
|
class="nabu-paragraph"
|
|
12
14
|
class:debug={block.nabu.debugging}
|
|
13
15
|
class:selected={block.selected}
|
|
@@ -34,14 +34,11 @@ const ParagraphExtension = extension("paragraph", {
|
|
|
34
34
|
const roots = nabu.tree.roots();
|
|
35
35
|
roots.forEach(root => {
|
|
36
36
|
const type = root.data.get("type");
|
|
37
|
-
console.log('Root block type:', type);
|
|
38
37
|
root.children()?.forEach(child => {
|
|
39
38
|
const childType = child.data.get("type");
|
|
40
|
-
console.log('Child block type:', childType);
|
|
41
39
|
});
|
|
42
40
|
})
|
|
43
41
|
if (roots.length === 0) {
|
|
44
|
-
console.log('Document is empty, inserting initial paragraph block.');
|
|
45
42
|
nabu.insert("paragraph", { text: "" });
|
|
46
43
|
}
|
|
47
44
|
},
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
/**
|
|
5
5
|
* @typedef {NabuNode<{type: "paragraph", text: LoroText}>} ParagraphNode
|
|
6
6
|
*/
|
|
7
|
-
export class Paragraph extends
|
|
7
|
+
export class Paragraph extends TextBlock {
|
|
8
8
|
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
9
9
|
static toMarkdown(pb: import("../../utils/extensions.js").PasteBlock): string;
|
|
10
10
|
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
@@ -37,33 +37,15 @@ export class Paragraph extends Block {
|
|
|
37
37
|
static create(nabu: Nabu, type: string, props?: Object, parentId?: string | null, index?: number | null): Paragraph;
|
|
38
38
|
/** @param {Nabu} nabu @param {ParagraphNode} node */
|
|
39
39
|
constructor(nabu: Nabu, node: ParagraphNode);
|
|
40
|
-
container: LoroText;
|
|
41
|
-
/** @type {TextBehavior} */
|
|
42
|
-
behavior: TextBehavior;
|
|
43
40
|
component: import("svelte").Component<any, any, string> | import("svelte").Component<{
|
|
44
41
|
block: import("./paragraph.svelte.js").Paragraph;
|
|
45
42
|
}, {}, "">;
|
|
46
|
-
get text(): string;
|
|
47
|
-
get delta(): import("loro-crdt").Delta<string>[];
|
|
48
|
-
selection: {
|
|
49
|
-
from: number;
|
|
50
|
-
to: number;
|
|
51
|
-
isCollapsed: boolean;
|
|
52
|
-
direction: "forward" | "backward" | "none";
|
|
53
|
-
} | null;
|
|
54
|
-
/** @param {Block} block */
|
|
55
|
-
absorbs(block: Block): boolean;
|
|
56
|
-
/** @param {import('loro-crdt').Delta<string>[]} data */
|
|
57
|
-
applyDelta(data?: import("loro-crdt").Delta<string>[]): void;
|
|
58
|
-
/** @param {Parameters<Block["split"]>[0]} [options] @returns {ReturnType<Block["split"]>} */
|
|
59
|
-
split(options?: Parameters<Block["split"]>[0]): ReturnType<Block["split"]>;
|
|
60
43
|
}
|
|
61
44
|
export type ParagraphNode = NabuNode<{
|
|
62
45
|
type: "paragraph";
|
|
63
46
|
text: LoroText;
|
|
64
47
|
}>;
|
|
65
|
-
import {
|
|
66
|
-
import { LoroText } from "loro-crdt";
|
|
67
|
-
import { TextBehavior } from "../../behaviors/text";
|
|
48
|
+
import { TextBlock } from "../text-block.svelte.js";
|
|
68
49
|
import type { Nabu } from "../nabu.svelte.js";
|
|
50
|
+
import { LoroText } from "loro-crdt";
|
|
69
51
|
import type { NabuNode } from "../nabu.svelte.js";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TextBlock } from "../text-block.svelte.js";
|
|
2
2
|
import { LoroText } from "loro-crdt";
|
|
3
3
|
import ParagraphComponent from "./Paragraph.svelte";
|
|
4
|
-
import {
|
|
4
|
+
import { deltaToHtml, deltaToMarkdown } from "../../behaviors/text";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* @import { Nabu, NabuNode } from "../nabu.svelte.js";
|
|
@@ -11,17 +11,10 @@ import { TextBehavior, deltaToHtml, deltaToMarkdown } from "../../behaviors/text
|
|
|
11
11
|
* @typedef {NabuNode<{type: "paragraph", text: LoroText}>} ParagraphNode
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
-
export class Paragraph extends
|
|
14
|
+
export class Paragraph extends TextBlock {
|
|
15
15
|
/** @param {Nabu} nabu @param {ParagraphNode} node */
|
|
16
16
|
constructor(nabu, node) {
|
|
17
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
18
|
|
|
26
19
|
this.serializers.set('markdown', () => this.behavior.toMarkdown());
|
|
27
20
|
this.serializers.set('json', () => ({
|
|
@@ -33,47 +26,6 @@ export class Paragraph extends Block {
|
|
|
33
26
|
}
|
|
34
27
|
|
|
35
28
|
component = $derived(this.nabu.components.get("paragraph") || ParagraphComponent);
|
|
36
|
-
|
|
37
|
-
get text() {
|
|
38
|
-
return this.behavior.text;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
get delta() {
|
|
42
|
-
return this.behavior.delta;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
selection = $derived(this.behavior.selection);
|
|
46
|
-
positions = $derived(this.behavior.positions);
|
|
47
|
-
|
|
48
|
-
/** @param {InputEvent} event */
|
|
49
|
-
beforeinput(event) {
|
|
50
|
-
return this.behavior.handleBeforeInput(event);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Retrouve le nœud texte et l'offset DOM pour un offset Modèle donné
|
|
55
|
-
* @param {number} targetOffset
|
|
56
|
-
* @returns {{node: Node, offset: number} | null}
|
|
57
|
-
*/
|
|
58
|
-
getDOMPoint(targetOffset) {
|
|
59
|
-
if (!this.element) return null;
|
|
60
|
-
return this.behavior.getDOMPoint(targetOffset);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/** @param {Block} block */
|
|
64
|
-
absorbs(block) { return this.behavior.absorbs(block); }
|
|
65
|
-
|
|
66
|
-
/** @param {number} index @param {string} text */
|
|
67
|
-
insert(index, text) { return this.behavior.insert(index, text); }
|
|
68
|
-
|
|
69
|
-
/** @param {Parameters<Block["delete"]>[0]} [deletion] */
|
|
70
|
-
delete(deletion) { return this.behavior.delete(deletion); }
|
|
71
|
-
|
|
72
|
-
/** @param {import('loro-crdt').Delta<string>[]} data */
|
|
73
|
-
applyDelta(data = []) { return this.behavior.applyDelta(data); }
|
|
74
|
-
|
|
75
|
-
/** @param {Parameters<Block["split"]>[0]} [options] @returns {ReturnType<Block["split"]>} */
|
|
76
|
-
split(options) { return this.behavior.split(options); }
|
|
77
29
|
|
|
78
30
|
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
79
31
|
static toMarkdown(pb) {
|
|
@@ -111,7 +63,6 @@ export class Paragraph extends Block {
|
|
|
111
63
|
static htmlRules = [
|
|
112
64
|
{ selector: 'p' },
|
|
113
65
|
{ selector: 'blockquote' },
|
|
114
|
-
{ selector: 'pre' },
|
|
115
66
|
];
|
|
116
67
|
|
|
117
68
|
/**
|
|
@@ -135,4 +86,4 @@ export class Paragraph extends Block {
|
|
|
135
86
|
const block = new Paragraph(nabu, node);
|
|
136
87
|
return block;
|
|
137
88
|
}
|
|
138
|
-
}
|
|
89
|
+
}
|
|
@@ -37,14 +37,24 @@ export class NabuSelection extends SvelteSelection {
|
|
|
37
37
|
const onKeydown = (e) => {
|
|
38
38
|
if (e.key === 'ArrowRight' || e.key === 'ArrowDown') {
|
|
39
39
|
this.#pendingDirection = 'forward';
|
|
40
|
+
this.nabu.storedMarks = null;
|
|
40
41
|
} else if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') {
|
|
41
42
|
this.#pendingDirection = 'backward';
|
|
43
|
+
this.nabu.storedMarks = null;
|
|
42
44
|
} else {
|
|
43
45
|
this.#pendingDirection = null;
|
|
46
|
+
if (e.key === 'Home' || e.key === 'End' || e.key === 'PageUp' || e.key === 'PageDown') {
|
|
47
|
+
this.nabu.storedMarks = null;
|
|
48
|
+
}
|
|
44
49
|
}
|
|
45
50
|
};
|
|
51
|
+
const onMousedown = () => { this.nabu.storedMarks = null; };
|
|
46
52
|
document.addEventListener('keydown', onKeydown, true);
|
|
47
|
-
|
|
53
|
+
document.addEventListener('mousedown', onMousedown, true);
|
|
54
|
+
return () => {
|
|
55
|
+
document.removeEventListener('keydown', onKeydown, true);
|
|
56
|
+
document.removeEventListener('mousedown', onMousedown, true);
|
|
57
|
+
};
|
|
48
58
|
});
|
|
49
59
|
|
|
50
60
|
// Normalize invalid caret/focus positions: inert elements and element-node
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { Nabu, NabuNode } from "./nabu.svelte.js";
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {NabuNode<{text: LoroText}>} TextBlockNode
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Base class for purely textual blocks (paragraph, heading, dialogue…).
|
|
9
|
+
* Owns a single TextBehavior and proxies the standard Block text API to it,
|
|
10
|
+
* removing the proxy boilerplate previously duplicated in every text block.
|
|
11
|
+
*
|
|
12
|
+
* Hybrid blocks (MegaBlock + TextBehavior, like ListItem) must NOT extend
|
|
13
|
+
* this class — their children/positions logic differs.
|
|
14
|
+
*/
|
|
15
|
+
export class TextBlock extends Block {
|
|
16
|
+
/**
|
|
17
|
+
* @param {Nabu} nabu
|
|
18
|
+
* @param {NabuNode} node
|
|
19
|
+
* @param {LoroText} [textContainer] - Existing LoroText container. Defaults to the node's "text" container (created if missing).
|
|
20
|
+
*/
|
|
21
|
+
constructor(nabu: Nabu, node: NabuNode, textContainer?: LoroText);
|
|
22
|
+
/** @type {LoroText} */
|
|
23
|
+
container: LoroText;
|
|
24
|
+
/** @returns {TextBehavior} */
|
|
25
|
+
get behavior(): TextBehavior;
|
|
26
|
+
get text(): string;
|
|
27
|
+
get delta(): import("loro-crdt").Delta<string>[];
|
|
28
|
+
selection: {
|
|
29
|
+
from: number;
|
|
30
|
+
to: number;
|
|
31
|
+
isCollapsed: boolean;
|
|
32
|
+
direction: "forward" | "backward" | "none";
|
|
33
|
+
} | null;
|
|
34
|
+
/** @param {Block} block */
|
|
35
|
+
absorbs(block: Block): boolean;
|
|
36
|
+
/** @param {import('loro-crdt').Delta<string>[]} data */
|
|
37
|
+
applyDelta(data?: import("loro-crdt").Delta<string>[]): void;
|
|
38
|
+
/** @param {Parameters<Block["split"]>[0]} [options] @returns {ReturnType<Block["split"]>} */
|
|
39
|
+
split(options?: Parameters<Block["split"]>[0]): ReturnType<Block["split"]>;
|
|
40
|
+
#private;
|
|
41
|
+
}
|
|
42
|
+
export type TextBlockNode = NabuNode<{
|
|
43
|
+
text: LoroText;
|
|
44
|
+
}>;
|
|
45
|
+
import { Block } from "./block.svelte.js";
|
|
46
|
+
import { LoroText } from "loro-crdt";
|
|
47
|
+
import { TextBehavior } from "../behaviors/text/text.behavior.svelte.js";
|
|
48
|
+
import type { Nabu } from "./nabu.svelte.js";
|
|
49
|
+
import type { NabuNode } from "./nabu.svelte.js";
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Block } from "./block.svelte.js";
|
|
2
|
+
import { LoroText } from "loro-crdt";
|
|
3
|
+
import { TextBehavior } from "../behaviors/text/text.behavior.svelte.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @import { Nabu, NabuNode } from "./nabu.svelte.js";
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {NabuNode<{text: LoroText}>} TextBlockNode
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Base class for purely textual blocks (paragraph, heading, dialogue…).
|
|
15
|
+
* Owns a single TextBehavior and proxies the standard Block text API to it,
|
|
16
|
+
* removing the proxy boilerplate previously duplicated in every text block.
|
|
17
|
+
*
|
|
18
|
+
* Hybrid blocks (MegaBlock + TextBehavior, like ListItem) must NOT extend
|
|
19
|
+
* this class — their children/positions logic differs.
|
|
20
|
+
*/
|
|
21
|
+
export class TextBlock extends Block {
|
|
22
|
+
/** @type {TextBehavior} */
|
|
23
|
+
#behavior;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {Nabu} nabu
|
|
27
|
+
* @param {NabuNode} node
|
|
28
|
+
* @param {LoroText} [textContainer] - Existing LoroText container. Defaults to the node's "text" container (created if missing).
|
|
29
|
+
*/
|
|
30
|
+
constructor(nabu, node, textContainer) {
|
|
31
|
+
super(nabu, node);
|
|
32
|
+
const data = /** @type {TextBlockNode} */ (node).data;
|
|
33
|
+
/** @type {LoroText} */
|
|
34
|
+
this.container = textContainer ?? data.get("text") ?? data.setContainer("text", new LoroText());
|
|
35
|
+
this.#behavior = new TextBehavior(/** @type {Block} */ (/** @type {unknown} */ (this)), this.container);
|
|
36
|
+
this.behaviors.set("text", this.#behavior);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** @returns {TextBehavior} */
|
|
40
|
+
get behavior() { return this.#behavior; }
|
|
41
|
+
|
|
42
|
+
get text() { return this.behavior.text; }
|
|
43
|
+
|
|
44
|
+
get delta() { return this.behavior.delta; }
|
|
45
|
+
|
|
46
|
+
selection = $derived(this.behavior.selection);
|
|
47
|
+
positions = $derived(this.behavior.positions);
|
|
48
|
+
wordCount = $derived(this.behavior.wordCount);
|
|
49
|
+
|
|
50
|
+
/** @param {InputEvent} event */
|
|
51
|
+
beforeinput(event) { return this.behavior.handleBeforeInput(event); }
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Retrouve le nœud texte et l'offset DOM pour un offset Modèle donné
|
|
55
|
+
* @param {number} targetOffset
|
|
56
|
+
* @returns {{node: Node, offset: number} | null}
|
|
57
|
+
*/
|
|
58
|
+
getDOMPoint(targetOffset) { return this.behavior.getDOMPoint(targetOffset); }
|
|
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
|
+
}
|