@aionbuilders/nabu 0.1.0-alpha.9 → 0.1.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/CHANGELOG.md +523 -0
- package/LICENSE +21 -0
- package/README.md +82 -144
- package/dist/behaviors/text/RichText.svelte +25 -2
- 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 +5 -0
- package/dist/behaviors/text/markdown-shortcuts.extension.js +79 -0
- package/dist/behaviors/text/markdown-syntax.d.ts +42 -0
- package/dist/behaviors/text/markdown-syntax.js +81 -0
- package/dist/behaviors/text/rich-text.extension.d.ts +3 -0
- package/dist/behaviors/text/rich-text.extension.js +135 -53
- package/dist/behaviors/text/text.behavior.svelte.d.ts +217 -42
- package/dist/behaviors/text/text.behavior.svelte.js +657 -55
- package/dist/behaviors/text/typography.extension.d.ts +7 -0
- package/dist/behaviors/text/typography.extension.js +25 -0
- package/dist/blocks/Block.svelte +5 -2
- package/dist/blocks/BlockList.svelte +48 -0
- package/dist/blocks/BlockList.svelte.d.ts +17 -0
- package/dist/blocks/Nabu.svelte +180 -14
- package/dist/blocks/Nabu.svelte.d.ts +13 -0
- package/dist/blocks/block.extension.d.ts +3 -0
- package/dist/blocks/block.extension.js +8 -9
- package/dist/blocks/block.svelte.d.ts +356 -68
- package/dist/blocks/block.svelte.js +388 -42
- package/dist/blocks/blockquote/Blockquote.svelte +39 -0
- package/dist/blocks/blockquote/Blockquote.svelte.d.ts +11 -0
- package/dist/blocks/blockquote/blockquote.extension.d.ts +4 -0
- package/dist/blocks/blockquote/blockquote.extension.js +80 -0
- package/dist/blocks/blockquote/blockquote.svelte.d.ts +63 -0
- package/dist/blocks/blockquote/blockquote.svelte.js +136 -0
- package/dist/blocks/blockquote/index.d.ts +3 -0
- package/dist/blocks/blockquote/index.js +3 -0
- package/dist/blocks/callout/Callout.svelte +67 -0
- package/dist/blocks/callout/Callout.svelte.d.ts +11 -0
- package/dist/blocks/callout/callout.extension.d.ts +4 -0
- package/dist/blocks/callout/callout.extension.js +84 -0
- package/dist/blocks/callout/callout.svelte.d.ts +80 -0
- package/dist/blocks/callout/callout.svelte.js +189 -0
- package/dist/blocks/callout/index.d.ts +3 -0
- package/dist/blocks/callout/index.js +3 -0
- package/dist/blocks/chunks.d.ts +49 -0
- package/dist/blocks/chunks.js +90 -0
- package/dist/blocks/code/Code.svelte +84 -0
- package/dist/blocks/code/Code.svelte.d.ts +11 -0
- package/dist/blocks/code/code.extension.d.ts +4 -0
- package/dist/blocks/code/code.extension.js +88 -0
- package/dist/blocks/code/code.svelte.d.ts +68 -0
- package/dist/blocks/code/code.svelte.js +245 -0
- package/dist/blocks/code/index.d.ts +3 -0
- package/dist/blocks/code/index.js +3 -0
- package/dist/blocks/container.utils.d.ts +85 -13
- package/dist/blocks/container.utils.js +338 -178
- package/dist/blocks/dialogue/Dialogue.svelte +8 -2
- package/dist/blocks/dialogue/dialogue.svelte.d.ts +36 -23
- package/dist/blocks/dialogue/dialogue.svelte.js +51 -45
- package/dist/blocks/dialogue/hooks/onBeforeInput.hook.d.ts +2 -2
- package/dist/blocks/dialogue/hooks/onBeforeInput.hook.js +6 -4
- package/dist/blocks/dialogue/index.d.ts +12 -1
- package/dist/blocks/dialogue/index.js +51 -35
- 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 +23 -0
- package/dist/blocks/divider/divider.extension.js +153 -0
- package/dist/blocks/divider/divider.svelte.d.ts +54 -0
- package/dist/blocks/divider/divider.svelte.js +89 -0
- package/dist/blocks/divider/index.d.ts +3 -0
- package/dist/blocks/divider/index.js +3 -0
- package/dist/blocks/file/File.svelte +56 -0
- package/dist/blocks/file/File.svelte.d.ts +11 -0
- package/dist/blocks/file/file.extension.d.ts +4 -0
- package/dist/blocks/file/file.extension.js +27 -0
- package/dist/blocks/file/file.svelte.d.ts +29 -0
- package/dist/blocks/file/file.svelte.js +77 -0
- package/dist/blocks/file/index.d.ts +3 -0
- package/dist/blocks/file/index.js +3 -0
- package/dist/blocks/heading/Heading.svelte +2 -1
- package/dist/blocks/heading/heading.svelte.d.ts +27 -34
- package/dist/blocks/heading/heading.svelte.js +72 -60
- package/dist/blocks/heading/hooks/onBeforeInput.hook.js +7 -5
- package/dist/blocks/heading/index.d.ts +2 -2
- package/dist/blocks/heading/index.js +33 -8
- package/dist/blocks/image/Image.svelte +41 -0
- package/dist/blocks/image/Image.svelte.d.ts +11 -0
- package/dist/blocks/image/image.extension.d.ts +5 -0
- package/dist/blocks/image/image.extension.js +34 -0
- package/dist/blocks/image/image.svelte.d.ts +61 -0
- package/dist/blocks/image/image.svelte.js +114 -0
- package/dist/blocks/image/index.d.ts +3 -0
- package/dist/blocks/image/index.js +3 -0
- package/dist/blocks/index.d.ts +11 -1
- package/dist/blocks/index.js +11 -1
- package/dist/blocks/list/List.svelte +9 -2
- package/dist/blocks/list/ListItem.svelte +44 -7
- package/dist/blocks/list/convert.d.ts +20 -0
- package/dist/blocks/list/convert.js +92 -0
- package/dist/blocks/list/hooks/onBeforeInput.hook.js +60 -26
- package/dist/blocks/list/html-flat.d.ts +78 -0
- package/dist/blocks/list/html-flat.js +244 -0
- package/dist/blocks/list/indent.d.ts +86 -0
- package/dist/blocks/list/indent.js +205 -0
- package/dist/blocks/list/index.d.ts +7 -3
- package/dist/blocks/list/index.js +43 -31
- package/dist/blocks/list/list-item.svelte.d.ts +43 -17
- package/dist/blocks/list/list-item.svelte.js +143 -93
- package/dist/blocks/list/list.behavior.svelte.d.ts +7 -4
- package/dist/blocks/list/list.behavior.svelte.js +10 -5
- package/dist/blocks/list/list.svelte.d.ts +101 -23
- package/dist/blocks/list/list.svelte.js +136 -114
- package/dist/blocks/list/markdown.d.ts +149 -0
- package/dist/blocks/list/markdown.js +442 -0
- package/dist/blocks/list/normalize.d.ts +11 -0
- package/dist/blocks/list/normalize.js +396 -0
- package/dist/blocks/list/paste.d.ts +45 -0
- package/dist/blocks/list/paste.js +242 -0
- package/dist/blocks/list/step-back.d.ts +24 -0
- package/dist/blocks/list/step-back.js +87 -0
- package/dist/blocks/list/to-list.d.ts +7 -0
- package/dist/blocks/list/to-list.js +44 -0
- package/dist/blocks/list/todos-toggle.d.ts +20 -0
- package/dist/blocks/list/todos-toggle.js +48 -0
- 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/markdown.d.ts +12 -0
- package/dist/blocks/media/markdown.js +26 -0
- package/dist/blocks/media/media-block.svelte.d.ts +107 -0
- package/dist/blocks/media/media-block.svelte.js +181 -0
- package/dist/blocks/media/media.helpers.d.ts +24 -0
- package/dist/blocks/media/media.helpers.js +88 -0
- package/dist/blocks/megablock.svelte.d.ts +50 -19
- package/dist/blocks/megablock.svelte.js +115 -74
- package/dist/blocks/move.d.ts +23 -0
- package/dist/blocks/move.js +92 -0
- package/dist/blocks/nabu.svelte.d.ts +846 -75
- package/dist/blocks/nabu.svelte.js +1990 -182
- package/dist/blocks/paragraph/Paragraph.svelte +1 -0
- package/dist/blocks/paragraph/index.d.ts +2 -2
- package/dist/blocks/paragraph/index.js +35 -16
- package/dist/blocks/paragraph/paragraph.svelte.d.ts +49 -35
- package/dist/blocks/paragraph/paragraph.svelte.js +81 -69
- package/dist/blocks/paragraph/table.html.d.ts +9 -0
- package/dist/blocks/paragraph/table.html.js +111 -0
- package/dist/blocks/paragraph/table.markdown.d.ts +32 -0
- package/dist/blocks/paragraph/table.markdown.js +90 -0
- package/dist/blocks/region/Region.svelte +264 -0
- package/dist/blocks/region/Region.svelte.d.ts +11 -0
- package/dist/blocks/region/actions.d.ts +135 -0
- package/dist/blocks/region/actions.js +402 -0
- package/dist/blocks/region/close.d.ts +35 -0
- package/dist/blocks/region/close.js +130 -0
- package/dist/blocks/region/commands.d.ts +6 -0
- package/dist/blocks/region/commands.js +277 -0
- package/dist/blocks/region/create.d.ts +72 -0
- package/dist/blocks/region/create.js +208 -0
- package/dist/blocks/region/end-marker.d.ts +35 -0
- package/dist/blocks/region/end-marker.js +51 -0
- package/dist/blocks/region/facts.d.ts +69 -0
- package/dist/blocks/region/facts.js +152 -0
- package/dist/blocks/region/format.d.ts +91 -0
- package/dist/blocks/region/format.js +260 -0
- package/dist/blocks/region/index.d.ts +14 -0
- package/dist/blocks/region/index.js +14 -0
- package/dist/blocks/region/insert.d.ts +59 -0
- package/dist/blocks/region/insert.js +102 -0
- package/dist/blocks/region/messages.d.ts +254 -0
- package/dist/blocks/region/messages.js +131 -0
- package/dist/blocks/region/normalize.d.ts +7 -0
- package/dist/blocks/region/normalize.js +170 -0
- package/dist/blocks/region/region.extension.d.ts +58 -0
- package/dist/blocks/region/region.extension.js +119 -0
- package/dist/blocks/region/region.svelte.d.ts +221 -0
- package/dist/blocks/region/region.svelte.js +582 -0
- package/dist/blocks/region/regions.svelte.d.ts +141 -0
- package/dist/blocks/region/regions.svelte.js +276 -0
- package/dist/blocks/region/serialize.d.ts +57 -0
- package/dist/blocks/region/serialize.js +112 -0
- package/dist/blocks/region/types.d.ts +255 -0
- package/dist/blocks/region/types.js +204 -0
- package/dist/blocks/selection.svelte.d.ts +106 -8
- package/dist/blocks/selection.svelte.js +632 -74
- package/dist/blocks/subthread.d.ts +80 -0
- package/dist/blocks/subthread.js +167 -0
- package/dist/blocks/text-block.svelte.d.ts +70 -0
- package/dist/blocks/text-block.svelte.js +108 -0
- package/dist/blocks/toggle/Toggle.svelte +73 -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/markdown.d.ts +20 -0
- package/dist/blocks/toggle/markdown.js +112 -0
- package/dist/blocks/toggle/toggle.extension.d.ts +4 -0
- package/dist/blocks/toggle/toggle.extension.js +18 -0
- package/dist/blocks/toggle/toggle.svelte.d.ts +135 -0
- package/dist/blocks/toggle/toggle.svelte.js +352 -0
- package/dist/blocks/undo-selection.d.ts +175 -0
- package/dist/blocks/undo-selection.js +360 -0
- package/dist/blocks/video/Video.svelte +42 -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 +4 -0
- package/dist/blocks/video/video.extension.js +27 -0
- package/dist/blocks/video/video.svelte.d.ts +29 -0
- package/dist/blocks/video/video.svelte.js +80 -0
- package/dist/extend.d.ts +55 -0
- package/dist/extend.js +88 -0
- package/dist/files/assets.clipboard.d.ts +77 -0
- package/dist/files/assets.clipboard.js +152 -0
- package/dist/files/files.svelte.d.ts +363 -0
- package/dist/files/files.svelte.js +897 -0
- package/dist/files/index.d.ts +3 -0
- package/dist/files/index.js +3 -0
- package/dist/files/outbox.d.ts +69 -0
- package/dist/files/outbox.js +48 -0
- package/dist/files/status.d.ts +65 -0
- package/dist/files/status.js +87 -0
- package/dist/files/store.d.ts +97 -0
- package/dist/files/store.js +65 -0
- package/dist/files/uploads.svelte.d.ts +174 -0
- package/dist/files/uploads.svelte.js +504 -0
- package/dist/index.d.ts +68 -6
- package/dist/index.js +117 -7
- package/dist/nabu-theme.css +112 -0
- package/dist/paste/html.paste.d.ts +74 -3
- package/dist/paste/html.paste.js +555 -83
- package/dist/paste/markdown-inline.d.ts +86 -0
- package/dist/paste/markdown-inline.js +528 -0
- package/dist/paste/markdown.paste.d.ts +2 -8
- package/dist/paste/markdown.paste.js +78 -94
- package/dist/paste/nabu.paste.d.ts +3 -0
- package/dist/paste/nabu.paste.js +29 -3
- package/dist/paste/plain-text.paste.d.ts +2 -2
- package/dist/paste/plain-text.paste.js +3 -3
- package/dist/persistence/asset-store.d.ts +51 -0
- package/dist/persistence/asset-store.js +103 -0
- package/dist/persistence/autosave.d.ts +53 -0
- package/dist/persistence/autosave.js +62 -0
- package/dist/persistence/compact.d.ts +162 -0
- package/dist/persistence/compact.js +141 -0
- package/dist/persistence/history.d.ts +77 -0
- package/dist/persistence/history.js +176 -0
- package/dist/persistence/indexeddb.d.ts +190 -4
- package/dist/persistence/indexeddb.js +333 -29
- package/dist/persistence/merge.d.ts +38 -0
- package/dist/persistence/merge.js +36 -0
- package/dist/persistence/outbox.d.ts +25 -0
- package/dist/persistence/outbox.js +40 -0
- package/dist/persistence/persistence.d.ts +70 -9
- package/dist/persistence/persistence.js +507 -35
- package/dist/persistence/recovery.d.ts +79 -0
- package/dist/persistence/recovery.js +79 -0
- package/dist/persistence/release.d.ts +34 -0
- package/dist/persistence/release.js +64 -0
- package/dist/persistence/state.svelte.d.ts +53 -0
- package/dist/persistence/state.svelte.js +48 -0
- package/dist/persistence/sync.d.ts +39 -0
- package/dist/persistence/sync.js +138 -0
- package/dist/persistence/tail.d.ts +42 -0
- package/dist/persistence/tail.js +103 -0
- package/dist/presets.d.ts +29 -24
- package/dist/presets.js +51 -14
- package/dist/themes/atelier.css +318 -0
- package/dist/ui/CommandChooser.svelte +153 -0
- package/dist/ui/CommandChooser.svelte.d.ts +13 -0
- package/dist/ui/CommandInvite.svelte +99 -0
- package/dist/ui/CommandInvite.svelte.d.ts +12 -0
- package/dist/ui/DragHandle.svelte +217 -0
- package/dist/ui/DragHandle.svelte.d.ts +11 -0
- package/dist/ui/FloatingToolbar.svelte +165 -0
- package/dist/ui/FloatingToolbar.svelte.d.ts +27 -0
- package/dist/ui/SlashMenu.svelte +215 -0
- package/dist/ui/SlashMenu.svelte.d.ts +33 -0
- package/dist/ui/anchor.d.ts +112 -0
- package/dist/ui/anchor.js +136 -0
- package/dist/ui/index.d.ts +10 -0
- package/dist/ui/index.js +15 -0
- package/dist/ui/slash-menu.svelte.d.ts +112 -0
- package/dist/ui/slash-menu.svelte.js +150 -0
- package/dist/utils/command-confirm.svelte.d.ts +349 -0
- package/dist/utils/command-confirm.svelte.js +673 -0
- package/dist/utils/commands.d.ts +328 -0
- package/dist/utils/commands.js +416 -0
- package/dist/utils/dom-scope.d.ts +14 -0
- package/dist/utils/dom-scope.js +20 -0
- package/dist/utils/drop.d.ts +160 -0
- package/dist/utils/drop.js +305 -0
- package/dist/utils/extensions.d.ts +488 -40
- package/dist/utils/extensions.js +300 -19
- package/dist/utils/html.d.ts +44 -0
- package/dist/utils/html.js +183 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.js +4 -0
- package/dist/utils/keys.d.ts +120 -0
- package/dist/utils/keys.js +248 -0
- package/dist/utils/links.d.ts +22 -0
- package/dist/utils/links.js +34 -0
- package/dist/utils/placeholders.d.ts +88 -0
- package/dist/utils/placeholders.js +91 -0
- package/dist/utils/selection.svelte.d.ts +47 -19
- package/dist/utils/selection.svelte.js +60 -10
- package/dist/utils/spellcheck.d.ts +9 -0
- package/dist/utils/spellcheck.js +17 -0
- package/dist/utils/tab.d.ts +31 -0
- package/dist/utils/tab.js +117 -0
- package/dist/utils/words.d.ts +7 -0
- package/dist/utils/words.js +62 -0
- package/dist/vite.d.ts +22 -0
- package/dist/vite.js +34 -0
- package/package.json +61 -22
- package/dist/blocks/zone/Zone.svelte +0 -258
- package/dist/blocks/zone/Zone.svelte.d.ts +0 -11
- package/dist/blocks/zone/index.d.ts +0 -2
- package/dist/blocks/zone/index.js +0 -2
- package/dist/blocks/zone/zone.extension.d.ts +0 -2
- package/dist/blocks/zone/zone.extension.js +0 -221
- package/dist/blocks/zone/zone.svelte.d.ts +0 -112
- package/dist/blocks/zone/zone.svelte.js +0 -321
|
@@ -5,15 +5,57 @@
|
|
|
5
5
|
|
|
6
6
|
import { tick } from "svelte";
|
|
7
7
|
import { SvelteMap, SvelteSet } from "svelte/reactivity";
|
|
8
|
+
import { resolvePlaceholderText } from "../utils/placeholders.js";
|
|
8
9
|
|
|
9
10
|
|
|
11
|
+
/**
|
|
12
|
+
* The first descendant of `block`, depth first in document order, that satisfies `predicate` (the block itself is not
|
|
13
|
+
* tested). Does not enter a block that hides its children. Each node is visited once.
|
|
14
|
+
* @param {Block} block @param {(block: Block) => boolean} predicate @returns {Block | null}
|
|
15
|
+
*/
|
|
16
|
+
function findInDescendants(block, predicate) {
|
|
17
|
+
// @ts-ignore
|
|
18
|
+
const children = block.children;
|
|
19
|
+
if (!children?.length || block.hidesChildren) return null;
|
|
20
|
+
for (const child of children) {
|
|
21
|
+
if (predicate(child)) return child;
|
|
22
|
+
const found = findInDescendants(child, predicate);
|
|
23
|
+
if (found) return found;
|
|
24
|
+
}
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The base class of every block. A block wraps a node of the document tree (Loro) and holds the behaviour of one block type: the capability flags, the tree accessors, and the handlers for typing, keys, splitting, merging, pasting and serialization that a subclass overrides.
|
|
31
|
+
*/
|
|
10
32
|
export class Block {
|
|
11
33
|
/** @param {Nabu} nabu @param {NabuNode} node */
|
|
12
34
|
constructor(nabu, node) {
|
|
35
|
+
/**
|
|
36
|
+
* The editor this block belongs to.
|
|
37
|
+
* @type {Nabu}
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
13
40
|
this.nabu = nabu;
|
|
41
|
+
/**
|
|
42
|
+
* The block's node in the document tree (Loro): `node.data` holds its `type` and props, `node.parent()` / `node.children()` the structure.
|
|
43
|
+
* @type {NabuNode}
|
|
44
|
+
* @public
|
|
45
|
+
*/
|
|
14
46
|
this.node = node;
|
|
15
47
|
const metadata = node.data;
|
|
16
|
-
|
|
48
|
+
/**
|
|
49
|
+
* The Loro tree id of the node (also the key in `nabu.blocks`).
|
|
50
|
+
* @type {import('loro-crdt').TreeID}
|
|
51
|
+
* @public
|
|
52
|
+
*/
|
|
53
|
+
this.id = /** @type {import('loro-crdt').TreeID} */ (node.id.toString());
|
|
54
|
+
/**
|
|
55
|
+
* The block type, as registered by its extension (`'paragraph'`, `'callout'`...). Read from the node when the block is built; `retype()` changes the node, and the engine builds a new block.
|
|
56
|
+
* @type {string}
|
|
57
|
+
* @public
|
|
58
|
+
*/
|
|
17
59
|
this.type = metadata.get("type") || "block";
|
|
18
60
|
this.nabu.blocks.set(this.id, this);
|
|
19
61
|
const blocksOfType = this.nabu.blocksByType.get(this.type) || new SvelteSet();
|
|
@@ -24,48 +66,141 @@ export class Block {
|
|
|
24
66
|
|
|
25
67
|
const parent = node.parent();
|
|
26
68
|
if (parent) {
|
|
27
|
-
this.parent = this.nabu.blocks.get(parent.id.toString()) || null;
|
|
69
|
+
this.parent = /** @type {MegaBlock | null} */ (this.nabu.blocks.get(parent.id.toString()) || null);
|
|
28
70
|
}
|
|
29
71
|
}
|
|
30
72
|
|
|
31
|
-
/**
|
|
73
|
+
/**
|
|
74
|
+
* Serializers of this block by format: `'markdown'`, `'json'`, `'application/x-nabu+json'` (the clipboard) or one of your own.
|
|
75
|
+
* Each receives the context of the call (`{ from, to, partial }` for the clipboard) and returns the serialized block.
|
|
76
|
+
* @type {SvelteMap<string, function(any): any>}
|
|
77
|
+
* @public
|
|
78
|
+
*/
|
|
32
79
|
serializers = new SvelteMap();
|
|
80
|
+
/**
|
|
81
|
+
* The behaviors of the block by name; a text block has `'text'` (a `TextBehavior`).
|
|
82
|
+
* @type {SvelteMap<string, any>}
|
|
83
|
+
* @public
|
|
84
|
+
*/
|
|
33
85
|
behaviors = new SvelteMap();
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
/** The selection covers this block (reactive). @public */
|
|
34
89
|
selected = $state(false);
|
|
90
|
+
/** The selection starts in this block (reactive). @public */
|
|
35
91
|
isSelectionStart = $state(false);
|
|
92
|
+
/** The selection ends in this block (reactive). @public */
|
|
36
93
|
isSelectionEnd = $state(false);
|
|
94
|
+
/** The selection covers this block and neither starts nor ends in it (reactive). @public */
|
|
37
95
|
isIntermediate = $derived(this.selected && !this.isSelectionStart && !this.isSelectionEnd);
|
|
38
96
|
|
|
97
|
+
/** @public */
|
|
39
98
|
clearSelection() {
|
|
40
99
|
this.selected = false;
|
|
41
100
|
this.isSelectionStart = false;
|
|
42
101
|
this.isSelectionEnd = false;
|
|
43
102
|
}
|
|
44
103
|
|
|
45
|
-
/**
|
|
104
|
+
/**
|
|
105
|
+
* @type {{from: number, to: number, direction: "forward" | "backward" | "none"} | null}
|
|
106
|
+
* @public
|
|
107
|
+
*/
|
|
46
108
|
selection = $derived(null);
|
|
47
109
|
|
|
110
|
+
/** @public */
|
|
48
111
|
positions = $derived(0);
|
|
49
112
|
|
|
113
|
+
/**
|
|
114
|
+
* Number of words contained in this block. Overridden by text blocks
|
|
115
|
+
* (own text) and containers (sum of children) so that parents can
|
|
116
|
+
* aggregate without re-traversing the whole subtree.
|
|
117
|
+
* @type {number}
|
|
118
|
+
* @public
|
|
119
|
+
*/
|
|
120
|
+
wordCount = $derived(0);
|
|
121
|
+
|
|
122
|
+
// ── Capability flags ─────────────────────────────────────────────────────
|
|
123
|
+
// Declarative opt-outs respected centrally by the engine. Default true;
|
|
124
|
+
// override to false in a subclass (e.g. CodeBlock) to disable a behavior.
|
|
125
|
+
/**
|
|
126
|
+
* When false, Enter inserts a literal "\n" in place instead of splitting the block.
|
|
127
|
+
* @type {boolean}
|
|
128
|
+
* @public
|
|
129
|
+
*/
|
|
130
|
+
splitsOnEnter = true;
|
|
131
|
+
/**
|
|
132
|
+
* When false, onBeforeInput markdown-shortcut hooks and inputRules are skipped in this block.
|
|
133
|
+
* @type {boolean}
|
|
134
|
+
* @public
|
|
135
|
+
*/
|
|
136
|
+
transformsEnabled = true;
|
|
137
|
+
/**
|
|
138
|
+
* When false, inline marks (bold/italic/...) are never applied to this block.
|
|
139
|
+
* @type {boolean}
|
|
140
|
+
* @public
|
|
141
|
+
*/
|
|
142
|
+
marksEnabled = true;
|
|
143
|
+
/**
|
|
144
|
+
* True when the block currently hides its children (they are not rendered, e.g. a collapsed
|
|
145
|
+
* foldable block): navigation (`findForward` / `findBackward` / `findLastDescendant`, caret
|
|
146
|
+
* normalisation) never enters them. Override with a getter in a subclass.
|
|
147
|
+
* @type {boolean}
|
|
148
|
+
* @public
|
|
149
|
+
*/
|
|
150
|
+
get hidesChildren() { return false; }
|
|
151
|
+
/**
|
|
152
|
+
* When true, the text of this block is an ISOLATED FIELD (like a form input embedded in the document): a text selection
|
|
153
|
+
* that does not lie entirely inside this block never touches its text. For such a selection (the engine's rules, see
|
|
154
|
+
* `container.utils.js`, `selection.svelte.js`):
|
|
155
|
+
* - range deletion, typing over it, cut and the marks skip the text of this block, and delete neither this block nor its
|
|
156
|
+
* container; the clipboard gets the TEXT blocks only (the block's own text and its container are not copied);
|
|
157
|
+
* - two text blocks on either side of an isolated block, or in different "isolation owners" (the nearest ancestor that
|
|
158
|
+
* isolates), never merge, so a deletion across them clears both sides without joining them;
|
|
159
|
+
* - the block is not highlighted (`data-nabu-isolated` + the root's `data-nabu-range`), and a selection end that would
|
|
160
|
+
* land in it is moved past it, in the direction of travel; a selection that STARTS in it stays bounded to it;
|
|
161
|
+
* - the caret enters it at its start going down / right, at its end going up / left; Select All inside it selects its text.
|
|
162
|
+
* @type {boolean}
|
|
163
|
+
* @public
|
|
164
|
+
*/
|
|
165
|
+
isolatesSelection = false;
|
|
166
|
+
/**
|
|
167
|
+
* When true, pasting inside this block inserts the clipboard's text/plain verbatim (replacing the selection), skipping every paste interpreter: no blocks created, no markdown/HTML interpretation.
|
|
168
|
+
* @type {boolean}
|
|
169
|
+
* @public
|
|
170
|
+
*/
|
|
171
|
+
pastesAsPlainText = false;
|
|
172
|
+
|
|
50
173
|
/**
|
|
51
174
|
* Resolve a document-local offset to { block, offset } within this block.
|
|
52
175
|
* Leaf blocks with text return themselves directly.
|
|
53
176
|
* Override in MegaBlock (pure containers) and mixed blocks (e.g. ListItem).
|
|
54
177
|
* @param {number} localOffset
|
|
55
178
|
* @returns {{ block: Block, offset: number } | null}
|
|
179
|
+
* @public
|
|
56
180
|
*/
|
|
57
181
|
resolveOffset(localOffset) {
|
|
58
182
|
return { block: this, offset: localOffset };
|
|
59
183
|
}
|
|
60
184
|
|
|
61
|
-
/**
|
|
185
|
+
/**
|
|
186
|
+
* @type {MegaBlock | null}
|
|
187
|
+
* @public
|
|
188
|
+
*/
|
|
62
189
|
parent = $state(null);
|
|
190
|
+
/** @public */
|
|
63
191
|
index = $state(0);
|
|
64
|
-
/**
|
|
192
|
+
/**
|
|
193
|
+
* @type {Block?}
|
|
194
|
+
* @public
|
|
195
|
+
*/
|
|
65
196
|
previous = $derived(this.index > 0 && this.parent ? this.parent.children[this.index - 1] : null);
|
|
66
|
-
/**
|
|
197
|
+
/**
|
|
198
|
+
* @type {Block?}
|
|
199
|
+
* @public
|
|
200
|
+
*/
|
|
67
201
|
next = $derived(this.parent && this.index < this.parent.children.length - 1 ? this.parent.children[this.index + 1] : null);
|
|
68
202
|
|
|
203
|
+
/** @public */
|
|
69
204
|
parents = $derived.by(() => {
|
|
70
205
|
const parents = [];
|
|
71
206
|
let current = this.parent;
|
|
@@ -76,25 +211,57 @@ export class Block {
|
|
|
76
211
|
return parents;
|
|
77
212
|
});
|
|
78
213
|
|
|
214
|
+
/** @public */
|
|
79
215
|
component = $derived(this.nabu.components.get(this.type) || null);
|
|
80
216
|
|
|
81
|
-
/**
|
|
217
|
+
/**
|
|
218
|
+
* Resolved placeholder to render in `data-placeholder` ('' = none). Combines the
|
|
219
|
+
* Nabu `placeholders` option, the block's emptiness and — with `show: 'focus'`
|
|
220
|
+
* (default) — whether it holds the collapsed cursor.
|
|
221
|
+
*
|
|
222
|
+
* The default text is declared by each block class as `static placeholder`
|
|
223
|
+
* (string, `false`, or `(block) => string | false`), overridable per type through
|
|
224
|
+
* the `placeholders.text` option. Blocks without a text behavior never show one.
|
|
225
|
+
* (No `static placeholder` is declared here: Svelte forbids a static and a
|
|
226
|
+
* reactive field with the same name in one class.)
|
|
227
|
+
* @type {string}
|
|
228
|
+
* @public
|
|
229
|
+
*/
|
|
230
|
+
placeholder = $derived.by(() => {
|
|
231
|
+
const config = this.nabu.placeholders;
|
|
232
|
+
if (!config.enabled) return '';
|
|
233
|
+
const text = this.behaviors.get("text");
|
|
234
|
+
if (!text || text.text) return '';
|
|
235
|
+
// The composed text is on screen (the model only catches up at compositionend): a placeholder would overlap it.
|
|
236
|
+
if (this.nabu.composingBlock === this) return '';
|
|
237
|
+
if (config.show === 'focus') {
|
|
238
|
+
const sel = this.nabu.selection;
|
|
239
|
+
if (!sel.isCollapsed || sel.anchorBlock !== this) return '';
|
|
240
|
+
}
|
|
241
|
+
return resolvePlaceholderText(/** @type {any} */ (this), config.text);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* @type {HTMLElement | null}
|
|
246
|
+
* @public
|
|
247
|
+
*/
|
|
82
248
|
element = $state(null);
|
|
83
249
|
|
|
84
|
-
/**
|
|
250
|
+
/**
|
|
251
|
+
* The next block in document order (own descendants first, then the following siblings with their descendants, then
|
|
252
|
+
* those of each ancestor) that satisfies `predicate`, or null. A block that hides its children is not entered.
|
|
253
|
+
* LINEAR: every block is visited at most once (the following siblings are only DESCENDED into, never searched
|
|
254
|
+
* "forward" again, which would revisit everything after them).
|
|
255
|
+
* @param {(block: Block) => boolean} predicate @returns {Block | null}
|
|
256
|
+
* @public
|
|
257
|
+
*/
|
|
85
258
|
findForward(predicate) {
|
|
86
|
-
// 1.
|
|
87
|
-
|
|
88
|
-
if (
|
|
89
|
-
// @ts-ignore
|
|
90
|
-
for (const child of this.children) {
|
|
91
|
-
if (predicate(child)) return child;
|
|
92
|
-
const found = child.findForward(predicate);
|
|
93
|
-
if (found) return found;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
259
|
+
// 1. Descendre dans nos propres enfants
|
|
260
|
+
const inside = findInDescendants(this, predicate);
|
|
261
|
+
if (inside) return inside;
|
|
96
262
|
|
|
97
|
-
// 2.
|
|
263
|
+
// 2. Frères suivants (eux et leurs descendants), puis remonter d'un niveau
|
|
264
|
+
/** @type {Block | null} */
|
|
98
265
|
let current = this;
|
|
99
266
|
while (current) {
|
|
100
267
|
const parent = current.parent || current.nabu;
|
|
@@ -103,7 +270,7 @@ export class Block {
|
|
|
103
270
|
for (let j = i + 1; j < parent.children.length; j++) {
|
|
104
271
|
const sibling = parent.children[j];
|
|
105
272
|
if (predicate(sibling)) return sibling;
|
|
106
|
-
const found = sibling
|
|
273
|
+
const found = findInDescendants(sibling, predicate);
|
|
107
274
|
if (found) return found;
|
|
108
275
|
}
|
|
109
276
|
}
|
|
@@ -112,8 +279,13 @@ export class Block {
|
|
|
112
279
|
return null;
|
|
113
280
|
}
|
|
114
281
|
|
|
115
|
-
/**
|
|
282
|
+
/**
|
|
283
|
+
* @param {(block: Block) => boolean} predicate
|
|
284
|
+
* @returns {Block | null}
|
|
285
|
+
* @public
|
|
286
|
+
*/
|
|
116
287
|
findBackward(predicate) {
|
|
288
|
+
/** @type {Block | null} */
|
|
117
289
|
let current = this;
|
|
118
290
|
while (current) {
|
|
119
291
|
const parent = current.parent || current.nabu;
|
|
@@ -126,7 +298,7 @@ export class Block {
|
|
|
126
298
|
|
|
127
299
|
// Si le frère a des enfants, le "précédent" est le DERNIER de ses descendants
|
|
128
300
|
// @ts-ignore
|
|
129
|
-
if (sibling.children?.length) {
|
|
301
|
+
if (sibling.children?.length && !sibling.hidesChildren) {
|
|
130
302
|
// @ts-ignore
|
|
131
303
|
const lastDescendant = sibling.findLastDescendant(predicate);
|
|
132
304
|
if (lastDescendant) return lastDescendant;
|
|
@@ -149,6 +321,7 @@ export class Block {
|
|
|
149
321
|
|
|
150
322
|
/**
|
|
151
323
|
* Returns real, uncommitted siblings, which is useful for extensions that want to check the document structure before the transaction is committed.
|
|
324
|
+
* @public
|
|
152
325
|
*/
|
|
153
326
|
getAdjacentSiblings() {
|
|
154
327
|
const parent = this.node.parent();
|
|
@@ -175,10 +348,11 @@ export class Block {
|
|
|
175
348
|
* Helper pour trouver le dernier descendant profond qui matche
|
|
176
349
|
* @param {(block: Block) => boolean} predicate
|
|
177
350
|
* @returns {Block | null}
|
|
351
|
+
* @public
|
|
178
352
|
*/
|
|
179
353
|
findLastDescendant(predicate) {
|
|
180
354
|
// @ts-ignore
|
|
181
|
-
const children = this.children || [];
|
|
355
|
+
const children = this.hidesChildren ? [] : (this.children || []);
|
|
182
356
|
for (let i = children.length - 1; i >= 0; i--) {
|
|
183
357
|
const child = children[i];
|
|
184
358
|
const found = child.findLastDescendant(predicate);
|
|
@@ -188,6 +362,7 @@ export class Block {
|
|
|
188
362
|
return null;
|
|
189
363
|
}
|
|
190
364
|
|
|
365
|
+
/** @public */
|
|
191
366
|
destroy() {
|
|
192
367
|
this.nabu.delete(this);
|
|
193
368
|
}
|
|
@@ -195,35 +370,64 @@ export class Block {
|
|
|
195
370
|
/**
|
|
196
371
|
* Transforme ce bloc en un autre type de bloc.
|
|
197
372
|
* @param {string} newType
|
|
198
|
-
* @param {
|
|
373
|
+
* @param {Record<string, any>} [props={}]
|
|
374
|
+
* @public
|
|
199
375
|
*/
|
|
200
376
|
transformTo(newType, props = {}) {
|
|
377
|
+
this.retype(newType, props);
|
|
378
|
+
this.commit();
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* The mutations of a type change, without the commit: the caller owns the transaction
|
|
383
|
+
* (`transformTo`, the `block:transform` action…). By default the block is retyped IN PLACE (same node:
|
|
384
|
+
* its id, text and position survive). Override it when a block cannot simply change type where it
|
|
385
|
+
* stands (e.g. because its parent only accepts some children): do the same job with Loro mutations only.
|
|
386
|
+
* @param {string} newType
|
|
387
|
+
* @param {Record<string, any>} [props={}]
|
|
388
|
+
* @public
|
|
389
|
+
*/
|
|
390
|
+
retype(newType, props = {}) {
|
|
201
391
|
const data = this.node.data;
|
|
202
392
|
data.set("type", newType);
|
|
203
393
|
for (const [key, value] of Object.entries(props)) {
|
|
204
394
|
data.set(key, value);
|
|
205
395
|
}
|
|
206
|
-
this.commit();
|
|
207
396
|
}
|
|
208
397
|
|
|
209
|
-
/**
|
|
398
|
+
/**
|
|
399
|
+
* @param {number} index
|
|
400
|
+
* @param {string} text
|
|
401
|
+
* @public
|
|
402
|
+
*/
|
|
210
403
|
insert(index, text) {
|
|
211
404
|
this.nabu.warn("Not implemented: insert text", text, "at index", index, "in block", this.id);
|
|
212
405
|
}
|
|
213
406
|
|
|
214
|
-
/**
|
|
407
|
+
/**
|
|
408
|
+
* @param {{from?: number, to?: number, index?: number, length?: number}} [deletion]
|
|
409
|
+
* @public
|
|
410
|
+
*/
|
|
215
411
|
delete(deletion) {
|
|
216
412
|
this.nabu.warn("Not implemented: delete block", this.id, "with deletion range", deletion);
|
|
217
413
|
}
|
|
218
414
|
|
|
219
|
-
/**
|
|
415
|
+
/**
|
|
416
|
+
* @param {Block} block
|
|
417
|
+
* @returns {any}
|
|
418
|
+
* @public
|
|
419
|
+
*/
|
|
220
420
|
absorbs(block) {
|
|
221
421
|
this.nabu.warn("Not implemented: check if block", this.id, "absorbs block", block.id);
|
|
222
422
|
return false;
|
|
223
423
|
}
|
|
224
424
|
|
|
225
425
|
|
|
226
|
-
/**
|
|
426
|
+
/**
|
|
427
|
+
* @param {Block} block
|
|
428
|
+
* @returns {any}
|
|
429
|
+
* @public
|
|
430
|
+
*/
|
|
227
431
|
mergeWith(block) {
|
|
228
432
|
const success = this.absorbs(block);
|
|
229
433
|
if (success) {
|
|
@@ -238,6 +442,7 @@ export class Block {
|
|
|
238
442
|
* @param {Block} otherBlock - The block to consume or be consumed by
|
|
239
443
|
* @param {'into' | 'from'} direction - 'into' = this merges into other, 'from' = other merges into this
|
|
240
444
|
* @returns {Block} The surviving block
|
|
445
|
+
* @public
|
|
241
446
|
*/
|
|
242
447
|
consume(otherBlock, direction = 'from') {
|
|
243
448
|
const [survivor, victim] = direction === 'into'
|
|
@@ -278,16 +483,61 @@ export class Block {
|
|
|
278
483
|
* Override in subclasses (e.g. ListItem requires a "list" parent).
|
|
279
484
|
* Used by wrapOrphan() after block relocations.
|
|
280
485
|
* @returns {{ type: string, props: () => Record<string, any> } | null}
|
|
486
|
+
* @public
|
|
281
487
|
*/
|
|
282
488
|
get requiredParent() { return null; }
|
|
283
489
|
|
|
284
|
-
/**
|
|
490
|
+
/**
|
|
491
|
+
* Whether an EMPTY child of this block takes the type of a whole block pasted into it (a `# Title` pasted
|
|
492
|
+
* into an empty paragraph makes it a heading). Always true for root blocks; a container with structural
|
|
493
|
+
* rules about its children (a list) keeps it `false`, a container with a free-form body (a toggle) opts in.
|
|
494
|
+
* @returns {boolean}
|
|
495
|
+
* @public
|
|
496
|
+
*/
|
|
497
|
+
get adoptsPastedTypes() { return false; }
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Paste extension point: lets the block that holds the caret decide how a pasted fragment lands, instead
|
|
501
|
+
* of the engine's default (first block merged into the caret block, the rest inserted after it as
|
|
502
|
+
* siblings). A block that lives in a container with its own rules about what it can hold overrides it
|
|
503
|
+
* (e.g. a list item turns pasted paragraphs into siblings items and splits its list around a block it
|
|
504
|
+
* cannot hold). The engine has already removed the selection; it commits the whole paste as ONE
|
|
505
|
+
* transaction (one undo step) and puts the caret at the returned point.
|
|
506
|
+
*
|
|
507
|
+
* Contract: return null to DECLINE, and then touch nothing (the default insertion runs). Otherwise
|
|
508
|
+
* apply the whole paste on the tree (the blocks are created through `nabu.createPasteBlock` /
|
|
509
|
+
* `nabu.insertPasteBlock`), do NOT commit, and return where the caret goes.
|
|
510
|
+
* @param {import('../utils/extensions.js').PasteBlock[]} blocks the sanitized fragment
|
|
511
|
+
* @param {{ offset: number }} ctx the caret offset in this block's text
|
|
512
|
+
* @returns {{ block: Block | null, offset: number } | null} `block: null` leaves the caret to the engine
|
|
513
|
+
* @public
|
|
514
|
+
*/
|
|
515
|
+
acceptPaste(blocks, ctx) { return null; }
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* The blocks that travel together when THIS block is dragged by its handle (P-81): this block and anything the drag must carry
|
|
519
|
+
* with it, in document order, all in the same parent. The default is the block alone (a container carries its own children:
|
|
520
|
+
* they are its tree children). A block whose "extent" is not its subtree overrides it (an extension may widen the range). The drop rules are the same for any range: it cannot land inside itself.
|
|
521
|
+
* @returns {Block[]}
|
|
522
|
+
* @public
|
|
523
|
+
*/
|
|
524
|
+
dragRange() { return [this]; }
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* @param {{from?: number, to?: number, index?: number, length?: number, offset?: number}} options
|
|
528
|
+
* @returns {{block: Block} | null}
|
|
529
|
+
* @public
|
|
530
|
+
*/
|
|
285
531
|
split(options) {
|
|
286
532
|
this.nabu.warn("Not implemented: split block", this.id, "with options", options);
|
|
287
533
|
return null;
|
|
288
534
|
}
|
|
289
535
|
|
|
290
|
-
/**
|
|
536
|
+
/**
|
|
537
|
+
* @param {{start?: number, end?: number, offset?: number} | null } options
|
|
538
|
+
* @param {boolean} [passive=false]
|
|
539
|
+
* @public
|
|
540
|
+
*/
|
|
291
541
|
focus(options = {}, passive = false) {
|
|
292
542
|
let start = options?.start ?? options?.offset ?? this.selection?.from ?? 0;
|
|
293
543
|
let end = options?.end ?? options?.offset ?? this.selection?.to ?? 0;
|
|
@@ -305,6 +555,7 @@ export class Block {
|
|
|
305
555
|
/**
|
|
306
556
|
* @param {number} offset
|
|
307
557
|
* @returns {{node: Node, offset: number} | null}
|
|
558
|
+
* @public
|
|
308
559
|
*/
|
|
309
560
|
getDOMPoint(offset) {
|
|
310
561
|
this.nabu.warn("getDOMPoint not implemented for block", this.type, this.id);
|
|
@@ -313,17 +564,34 @@ export class Block {
|
|
|
313
564
|
|
|
314
565
|
// -- Event Handling --
|
|
315
566
|
|
|
316
|
-
/**
|
|
567
|
+
/**
|
|
568
|
+
* @param {string} eventName
|
|
569
|
+
* @param {Event | null} event
|
|
570
|
+
* @param {Object} [data={}]
|
|
571
|
+
* @public
|
|
572
|
+
*/
|
|
317
573
|
ascend(eventName, event, data = {}) {
|
|
318
574
|
//@ts-ignore
|
|
319
575
|
if (this.parent && typeof this.parent[eventName] === "function") {
|
|
576
|
+
// A container handles the event for its children; a falsy result DECLINES it (e.g. a toggle only
|
|
577
|
+
// takes the Enter that exits its body), and the registered hooks below get their turn.
|
|
320
578
|
//@ts-ignore
|
|
321
|
-
|
|
579
|
+
const handled = this.parent[eventName](event, { ...data, from: this });
|
|
580
|
+
if (handled) return handled;
|
|
322
581
|
}
|
|
323
582
|
|
|
324
583
|
// 2. On essaye les hooks d'extensions enregistrés dans Nabu
|
|
325
|
-
const
|
|
326
|
-
if (
|
|
584
|
+
const registered = /** @type {Function[] | undefined} */ (this.nabu.hooks.get(eventName));
|
|
585
|
+
if (registered) {
|
|
586
|
+
// A hook registered by the extension that owns this block's type runs BEFORE the generic ones
|
|
587
|
+
// (extension order must not let a catch-all hook shadow a block-specific handler).
|
|
588
|
+
const owners = this.nabu.hookOwners;
|
|
589
|
+
const hooks = owners
|
|
590
|
+
? [
|
|
591
|
+
...registered.filter(h => owners.get(h) === this.type),
|
|
592
|
+
...registered.filter(h => owners.get(h) !== this.type),
|
|
593
|
+
]
|
|
594
|
+
: registered;
|
|
327
595
|
for (const hook of hooks) {
|
|
328
596
|
|
|
329
597
|
// Si un hook retourne 'true', on considère l'événement géré
|
|
@@ -335,24 +603,37 @@ export class Block {
|
|
|
335
603
|
return false;
|
|
336
604
|
}
|
|
337
605
|
|
|
338
|
-
/**
|
|
606
|
+
/**
|
|
607
|
+
* @param {InputEvent} event
|
|
608
|
+
* @returns {any}
|
|
609
|
+
* @public
|
|
610
|
+
*/
|
|
339
611
|
beforeinput(event) {
|
|
340
612
|
// On peut intercepter les événements d'input ici pour faire des choses comme :
|
|
341
613
|
}
|
|
342
614
|
|
|
343
|
-
/**
|
|
615
|
+
/**
|
|
616
|
+
* @param {KeyboardEvent} event
|
|
617
|
+
* @returns {any}
|
|
618
|
+
* @public
|
|
619
|
+
*/
|
|
344
620
|
keydown(event) {
|
|
345
621
|
// Intercepter les touches spéciales (Tab, Enter, flèches)
|
|
346
622
|
}
|
|
347
623
|
|
|
348
624
|
// -- UTILS --
|
|
349
625
|
|
|
626
|
+
/** @public */
|
|
350
627
|
commit() {
|
|
351
628
|
return this.nabu.commit();
|
|
352
629
|
}
|
|
353
630
|
|
|
354
631
|
|
|
355
|
-
/**
|
|
632
|
+
/**
|
|
633
|
+
* @param {string} format
|
|
634
|
+
* @param {Record<string, any>} [context]
|
|
635
|
+
* @public
|
|
636
|
+
*/
|
|
356
637
|
serialize(format, context = {}) {
|
|
357
638
|
const serializer = this.serializers.get(format);
|
|
358
639
|
if (serializer) {
|
|
@@ -364,7 +645,11 @@ export class Block {
|
|
|
364
645
|
}
|
|
365
646
|
|
|
366
647
|
|
|
367
|
-
/**
|
|
648
|
+
/**
|
|
649
|
+
* @param {Nabu} nabu
|
|
650
|
+
* @param {NabuNode} node
|
|
651
|
+
* @public
|
|
652
|
+
*/
|
|
368
653
|
static load(nabu, node) {
|
|
369
654
|
const metadata = node.data;
|
|
370
655
|
const type = metadata.get("type") || "block";
|
|
@@ -373,12 +658,73 @@ export class Block {
|
|
|
373
658
|
return block;
|
|
374
659
|
}
|
|
375
660
|
|
|
376
|
-
/**
|
|
661
|
+
/**
|
|
662
|
+
* @param {Nabu} nabu
|
|
663
|
+
* @param {string} type
|
|
664
|
+
* @param {Record<string, any>} [props={}]
|
|
665
|
+
* @param {string|null} [parentId=null]
|
|
666
|
+
* @param {number|null} [index=null]
|
|
667
|
+
* @public
|
|
668
|
+
*/
|
|
377
669
|
static create(nabu, type, props = {}, parentId = null, index = null) {
|
|
378
|
-
const node = nabu.tree.createNode(parentId || undefined, index
|
|
670
|
+
const node = nabu.tree.createNode(/** @type {import('loro-crdt').TreeID | undefined} */ (parentId || undefined), index ?? undefined);
|
|
379
671
|
node.data.set("type", type);
|
|
380
672
|
const block = Block.load(nabu, node);
|
|
381
673
|
return block;
|
|
382
674
|
}
|
|
383
675
|
|
|
676
|
+
// ── Static contract: clipboard, markdown and HTML ────────────────────────────────────────────────────────────────
|
|
677
|
+
// Optional members a block class declares to take part in paste, `importMarkdown` and the clipboard / `serialize` output. Not
|
|
678
|
+
// defined here (they default to `undefined`); the engine looks them up on the registered class. The built-ins (`Blockquote`,
|
|
679
|
+
// `Divider`, `Heading`...) are the model. They work on `PasteBlock`s (plain data), never on live blocks.
|
|
680
|
+
|
|
681
|
+
/**
|
|
682
|
+
* Which HTML elements this block reads on paste (see {@link HtmlRule}). Declare it together with `fromHTML`.
|
|
683
|
+
* @type {import('../utils/extensions.js').HtmlRule[] | undefined}
|
|
684
|
+
* @public
|
|
685
|
+
*/
|
|
686
|
+
static htmlRules = undefined;
|
|
687
|
+
|
|
688
|
+
/**
|
|
689
|
+
* Make the block(s) of an element matched by one of `htmlRules`; `null` / `[]` declines. `siblings` are the following
|
|
690
|
+
* elements `htmlSiblings` claimed.
|
|
691
|
+
* @type {((el: Element, helpers: import('../utils/extensions.js').HtmlHelpers, rule: import('../utils/extensions.js').HtmlRule, siblings: Element[]) => import('../utils/extensions.js').PasteBlock | import('../utils/extensions.js').PasteBlock[] | null) | undefined}
|
|
692
|
+
* @public
|
|
693
|
+
*/
|
|
694
|
+
static fromHTML = undefined;
|
|
695
|
+
|
|
696
|
+
/**
|
|
697
|
+
* Ask to absorb the elements that follow `el` (a structure that spans several elements, a list in Google Docs); returns them in order.
|
|
698
|
+
* @type {((el: Element, rule: import('../utils/extensions.js').HtmlRule, helpers: import('../utils/extensions.js').HtmlHelpers) => Element[]) | undefined}
|
|
699
|
+
* @public
|
|
700
|
+
*/
|
|
701
|
+
static htmlSiblings = undefined;
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* How this block starts and how many lines it takes in markdown (see {@link MarkdownRule}). Declare it together with `fromMarkdown`.
|
|
705
|
+
* @type {import('../utils/extensions.js').MarkdownRule[] | undefined}
|
|
706
|
+
* @public
|
|
707
|
+
*/
|
|
708
|
+
static markdownRules = undefined;
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* Make the block(s) of the lines a `markdownRules` rule consumed; `null` declines (the next rule is tried).
|
|
712
|
+
* @type {((lines: string[], helpers: import('../utils/extensions.js').MarkdownHelpers, rule: import('../utils/extensions.js').MarkdownRule) => import('../utils/extensions.js').PasteBlock | import('../utils/extensions.js').PasteBlock[] | null) | undefined}
|
|
713
|
+
* @public
|
|
714
|
+
*/
|
|
715
|
+
static fromMarkdown = undefined;
|
|
716
|
+
|
|
717
|
+
/**
|
|
718
|
+
* The markdown of a block of this type, for the clipboard's `text/plain` and for containers that serialize their children.
|
|
719
|
+
* @type {((pb: import('../utils/extensions.js').PasteBlock, ctx: import('../utils/extensions.js').SerializeContext) => string) | undefined}
|
|
720
|
+
* @public
|
|
721
|
+
*/
|
|
722
|
+
static toMarkdown = undefined;
|
|
723
|
+
|
|
724
|
+
/**
|
|
725
|
+
* The HTML of a block of this type, for the clipboard's `text/html`.
|
|
726
|
+
* @type {((pb: import('../utils/extensions.js').PasteBlock, ctx: import('../utils/extensions.js').SerializeContext) => string) | undefined}
|
|
727
|
+
* @public
|
|
728
|
+
*/
|
|
729
|
+
static toHtml = undefined;
|
|
384
730
|
}
|