@aionbuilders/nabu 0.1.0-beta.0 → 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 +72 -341
- package/dist/behaviors/text/RichText.svelte +24 -4
- package/dist/behaviors/text/markdown-shortcuts.extension.d.ts +3 -0
- package/dist/behaviors/text/markdown-shortcuts.extension.js +35 -10
- 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 +95 -38
- package/dist/behaviors/text/text.behavior.svelte.d.ts +199 -48
- package/dist/behaviors/text/text.behavior.svelte.js +549 -83
- package/dist/behaviors/text/typography.extension.d.ts +2 -1
- package/dist/behaviors/text/typography.extension.js +3 -2
- package/dist/blocks/BlockList.svelte +48 -0
- package/dist/blocks/BlockList.svelte.d.ts +17 -0
- package/dist/blocks/Nabu.svelte +169 -25
- package/dist/blocks/Nabu.svelte.d.ts +13 -0
- package/dist/blocks/block.extension.d.ts +3 -0
- package/dist/blocks/block.extension.js +4 -7
- package/dist/blocks/block.svelte.d.ts +350 -86
- package/dist/blocks/block.svelte.js +370 -59
- package/dist/blocks/blockquote/Blockquote.svelte +1 -2
- package/dist/blocks/blockquote/blockquote.extension.d.ts +4 -1
- package/dist/blocks/blockquote/blockquote.extension.js +14 -5
- package/dist/blocks/blockquote/blockquote.svelte.d.ts +19 -11
- package/dist/blocks/blockquote/blockquote.svelte.js +58 -13
- package/dist/blocks/callout/Callout.svelte +6 -8
- package/dist/blocks/callout/callout.extension.d.ts +4 -1
- package/dist/blocks/callout/callout.extension.js +19 -3
- package/dist/blocks/callout/callout.svelte.d.ts +5 -3
- package/dist/blocks/callout/callout.svelte.js +11 -6
- package/dist/blocks/chunks.d.ts +49 -0
- package/dist/blocks/chunks.js +90 -0
- package/dist/blocks/code/Code.svelte +7 -8
- package/dist/blocks/code/code.extension.d.ts +4 -1
- package/dist/blocks/code/code.extension.js +22 -2
- package/dist/blocks/code/code.svelte.d.ts +9 -2
- package/dist/blocks/code/code.svelte.js +49 -25
- package/dist/blocks/container.utils.d.ts +85 -13
- package/dist/blocks/container.utils.js +338 -178
- package/dist/blocks/dialogue/Dialogue.svelte +8 -4
- package/dist/blocks/dialogue/dialogue.svelte.d.ts +33 -2
- package/dist/blocks/dialogue/dialogue.svelte.js +58 -4
- 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.extension.d.ts +23 -1
- package/dist/blocks/divider/divider.extension.js +85 -19
- package/dist/blocks/divider/divider.svelte.d.ts +7 -2
- package/dist/blocks/divider/divider.svelte.js +9 -4
- package/dist/blocks/file/File.svelte +13 -6
- package/dist/blocks/file/file.extension.d.ts +4 -1
- package/dist/blocks/file/file.extension.js +9 -1
- package/dist/blocks/file/file.svelte.d.ts +3 -3
- package/dist/blocks/file/file.svelte.js +20 -14
- package/dist/blocks/heading/Heading.svelte +1 -2
- package/dist/blocks/heading/heading.svelte.d.ts +23 -11
- package/dist/blocks/heading/heading.svelte.js +68 -13
- 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 +5 -2
- package/dist/blocks/image/image.extension.d.ts +4 -1
- package/dist/blocks/image/image.extension.js +9 -1
- package/dist/blocks/image/image.svelte.d.ts +17 -6
- package/dist/blocks/image/image.svelte.js +33 -11
- package/dist/blocks/index.d.ts +1 -1
- package/dist/blocks/index.js +1 -1
- package/dist/blocks/list/List.svelte +1 -0
- package/dist/blocks/list/ListItem.svelte +1 -0
- 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 +49 -28
- 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 +6 -3
- package/dist/blocks/list/index.js +42 -31
- package/dist/blocks/list/list-item.svelte.d.ts +31 -17
- package/dist/blocks/list/list-item.svelte.js +99 -100
- package/dist/blocks/list/list.behavior.svelte.d.ts +4 -1
- package/dist/blocks/list/list.behavior.svelte.js +6 -1
- package/dist/blocks/list/list.svelte.d.ts +89 -18
- package/dist/blocks/list/list.svelte.js +129 -125
- 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/media/markdown.d.ts +12 -0
- package/dist/blocks/media/markdown.js +26 -0
- package/dist/blocks/media/media-block.svelte.d.ts +77 -2
- package/dist/blocks/media/media-block.svelte.js +124 -12
- package/dist/blocks/media/media.helpers.d.ts +6 -1
- package/dist/blocks/media/media.helpers.js +43 -6
- package/dist/blocks/megablock.svelte.d.ts +50 -19
- package/dist/blocks/megablock.svelte.js +114 -74
- package/dist/blocks/move.d.ts +23 -0
- package/dist/blocks/move.js +92 -0
- package/dist/blocks/nabu.svelte.d.ts +764 -71
- package/dist/blocks/nabu.svelte.js +1746 -189
- package/dist/blocks/paragraph/Paragraph.svelte +1 -2
- package/dist/blocks/paragraph/index.d.ts +2 -2
- package/dist/blocks/paragraph/index.js +35 -13
- package/dist/blocks/paragraph/paragraph.svelte.d.ts +46 -14
- package/dist/blocks/paragraph/paragraph.svelte.js +77 -16
- 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 +622 -74
- package/dist/blocks/subthread.d.ts +80 -0
- package/dist/blocks/subthread.js +167 -0
- package/dist/blocks/text-block.svelte.d.ts +34 -13
- package/dist/blocks/text-block.svelte.js +42 -8
- package/dist/blocks/toggle/Toggle.svelte +4 -15
- 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 -1
- package/dist/blocks/toggle/toggle.extension.js +6 -0
- package/dist/blocks/toggle/toggle.svelte.d.ts +54 -7
- package/dist/blocks/toggle/toggle.svelte.js +134 -34
- package/dist/blocks/undo-selection.d.ts +175 -0
- package/dist/blocks/undo-selection.js +360 -0
- package/dist/blocks/video/Video.svelte +5 -2
- package/dist/blocks/video/video.extension.d.ts +4 -1
- package/dist/blocks/video/video.extension.js +9 -1
- package/dist/blocks/video/video.svelte.d.ts +6 -5
- package/dist/blocks/video/video.svelte.js +18 -9
- 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 +321 -19
- package/dist/files/files.svelte.js +847 -43
- package/dist/files/index.d.ts +3 -1
- package/dist/files/index.js +3 -1
- 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 -9
- package/dist/index.js +117 -10
- package/dist/nabu-theme.css +21 -1
- package/dist/paste/html.paste.d.ts +74 -3
- package/dist/paste/html.paste.js +550 -86
- 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 -26
- package/dist/presets.js +35 -13
- 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 +97 -21
- package/dist/ui/FloatingToolbar.svelte +75 -27
- package/dist/ui/FloatingToolbar.svelte.d.ts +10 -0
- package/dist/ui/SlashMenu.svelte +147 -15
- package/dist/ui/SlashMenu.svelte.d.ts +20 -0
- package/dist/ui/anchor.d.ts +112 -0
- package/dist/ui/anchor.js +136 -0
- package/dist/ui/index.d.ts +7 -1
- package/dist/ui/index.js +12 -1
- package/dist/ui/slash-menu.svelte.d.ts +79 -7
- package/dist/ui/slash-menu.svelte.js +85 -51
- 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 +439 -45
- package/dist/utils/extensions.js +271 -29
- package/dist/utils/html.d.ts +44 -0
- package/dist/utils/html.js +183 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +3 -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 +81 -3
- package/dist/utils/placeholders.js +73 -3
- 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 +9 -3
- package/dist/vite.js +19 -8
- package/package.json +54 -20
- package/dist/blocks/variable.svelte.d.ts +0 -47
- package/dist/blocks/variable.svelte.js +0 -51
- package/dist/blocks/zone/TemplateEditor.svelte +0 -308
- package/dist/blocks/zone/TemplateEditor.svelte.d.ts +0 -11
- package/dist/blocks/zone/Zone.svelte +0 -272
- package/dist/blocks/zone/Zone.svelte.d.ts +0 -11
- package/dist/blocks/zone/ZoneEditor.svelte +0 -289
- package/dist/blocks/zone/ZoneEditor.svelte.d.ts +0 -11
- package/dist/blocks/zone/ZoneTemplatesList.svelte +0 -188
- package/dist/blocks/zone/ZoneTemplatesList.svelte.d.ts +0 -11
- package/dist/blocks/zone/hooks/onBeforeInput.hook.d.ts +0 -3
- package/dist/blocks/zone/hooks/onBeforeInput.hook.js +0 -30
- 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 -237
- package/dist/blocks/zone/zone.svelte.d.ts +0 -221
- package/dist/blocks/zone/zone.svelte.js +0 -608
- package/dist/overlays/context/Context.svelte +0 -182
- package/dist/overlays/context/Context.svelte.d.ts +0 -17
- package/dist/overlays/context/collect.d.ts +0 -17
- package/dist/overlays/context/collect.js +0 -33
- package/dist/overlays/context/context.extension.d.ts +0 -2
- package/dist/overlays/context/context.extension.js +0 -8
- package/dist/overlays/context/context.svelte.d.ts +0 -82
- package/dist/overlays/context/context.svelte.js +0 -159
- package/dist/overlays/context/index.d.ts +0 -3
- package/dist/overlays/context/index.js +0 -3
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sub-thread helpers: the keyboard rules of a CONTAINER block made of a header (its own text) and a body
|
|
3
|
+
* (child blocks), e.g. the Toggle, and later a container Callout. They are plain tree operations on nodes
|
|
4
|
+
* (no concrete container type in here): a container block composes them from its own `onSplit` /
|
|
5
|
+
* `beforeinput`, so each rule is one line to reuse.
|
|
6
|
+
*
|
|
7
|
+
* - Enter on an EMPTY paragraph that is the LAST child of the body exits the sub-thread:
|
|
8
|
+
* `isEmptyLastParagraph` + `exitSubThread`.
|
|
9
|
+
* - Backspace at offset 0 of the header turns the container into a paragraph, the body goes right after it:
|
|
10
|
+
* `dissolveToParagraph`.
|
|
11
|
+
* - Enter in the header of a container whose body is hidden creates the paragraph after the container:
|
|
12
|
+
* `insertParagraphAfter`.
|
|
13
|
+
* - A multi-block paste with the caret in the HEADER: the first block completes the header, the others go to
|
|
14
|
+
* the top of the body, the header text after the caret follows the last one: `pasteIntoHeader`.
|
|
15
|
+
*
|
|
16
|
+
* Every helper mutates the tree only (no commit): the caller owns the transaction, so that one user action stays
|
|
17
|
+
* one undo step.
|
|
18
|
+
*
|
|
19
|
+
* @import { Nabu, NabuNode } from "./nabu.svelte.js";
|
|
20
|
+
* @import { Block } from "./block.svelte.js";
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Whether `source` is an EMPTY paragraph that is the last child of `container`'s body.
|
|
24
|
+
* @param {{ children: Block[] }} container a block with children (a MegaBlock)
|
|
25
|
+
* @param {Block} source
|
|
26
|
+
* @returns {boolean}
|
|
27
|
+
*/
|
|
28
|
+
export function isEmptyLastParagraph(container: {
|
|
29
|
+
children: Block[];
|
|
30
|
+
}, source: Block): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Exits the sub-thread: moves `child` right after `container`, in the container's own parent. Nothing else is
|
|
33
|
+
* touched (the other children stay in the body, which may become empty).
|
|
34
|
+
* @param {NabuNode} child a node of the container's body
|
|
35
|
+
* @param {NabuNode} container
|
|
36
|
+
*/
|
|
37
|
+
export function exitSubThread(child: NabuNode, container: NabuNode): void;
|
|
38
|
+
/**
|
|
39
|
+
* Turns `container` into a paragraph (same node: its id, header text, marks and position survive) and moves its
|
|
40
|
+
* body right after it, in order, in the same parent. Nothing is lost. The props listed in `drop` are removed
|
|
41
|
+
* from the node (container-specific flags, e.g. `collapsed`).
|
|
42
|
+
* @param {NabuNode} container
|
|
43
|
+
* @param {string[]} [drop]
|
|
44
|
+
*/
|
|
45
|
+
export function dissolveToParagraph(container: NabuNode, drop?: string[]): void;
|
|
46
|
+
/**
|
|
47
|
+
* Inserts a paragraph holding `delta` right after `container`, in its parent. The insert commits.
|
|
48
|
+
* @param {Nabu} nabu
|
|
49
|
+
* @param {NabuNode} container
|
|
50
|
+
* @param {import('loro-crdt').Delta<string>[]} delta
|
|
51
|
+
* @returns {Block}
|
|
52
|
+
*/
|
|
53
|
+
export function insertParagraphAfter(nabu: Nabu, container: NabuNode, delta: import("loro-crdt").Delta<string>[]): Block;
|
|
54
|
+
/**
|
|
55
|
+
* Multi-block paste with the caret in the HEADER of a container (the header is text only, the body holds the
|
|
56
|
+
* blocks). Consistent with "Enter in the header creates the first body child":
|
|
57
|
+
* - the FIRST pasted block, when it is a text leaf, completes the header at the caret (the header keeps its
|
|
58
|
+
* type); a first block that is not text (container, image…) leaves the header as it is;
|
|
59
|
+
* - the other blocks go to the TOP of the body, in order, above the existing children;
|
|
60
|
+
* - the header text AFTER the caret follows the pasted blocks: it merges into the LAST pasted block when that
|
|
61
|
+
* one is text (cut at the edge of a partial copy, or a default text block), otherwise it becomes a new default
|
|
62
|
+
* text block right after it;
|
|
63
|
+
* - the caret ends at the junction (end of the pasted text), or at the start of the tail block.
|
|
64
|
+
* A single pasted text block is not for this helper (the engine's inline merge handles it): returns null and
|
|
65
|
+
* touches nothing, like `Block#acceptPaste`. Does NOT expand a collapsed container (its caller does: the body
|
|
66
|
+
* must be visible for the pasted content to be). Tree operations only, no commit.
|
|
67
|
+
*
|
|
68
|
+
* @param {Nabu} nabu
|
|
69
|
+
* @param {Block} container the block holding the caret in its header
|
|
70
|
+
* @param {number} offset the caret offset in the header text
|
|
71
|
+
* @param {import("../utils/extensions.js").PasteBlock[]} blocks
|
|
72
|
+
* @returns {{ block: Block | null, offset: number } | null}
|
|
73
|
+
*/
|
|
74
|
+
export function pasteIntoHeader(nabu: Nabu, container: Block, offset: number, blocks: import("../utils/extensions.js").PasteBlock[]): {
|
|
75
|
+
block: Block | null;
|
|
76
|
+
offset: number;
|
|
77
|
+
} | null;
|
|
78
|
+
import type { Block } from "./block.svelte.js";
|
|
79
|
+
import type { NabuNode } from "./nabu.svelte.js";
|
|
80
|
+
import type { Nabu } from "./nabu.svelte.js";
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sub-thread helpers: the keyboard rules of a CONTAINER block made of a header (its own text) and a body
|
|
3
|
+
* (child blocks), e.g. the Toggle, and later a container Callout. They are plain tree operations on nodes
|
|
4
|
+
* (no concrete container type in here): a container block composes them from its own `onSplit` /
|
|
5
|
+
* `beforeinput`, so each rule is one line to reuse.
|
|
6
|
+
*
|
|
7
|
+
* - Enter on an EMPTY paragraph that is the LAST child of the body exits the sub-thread:
|
|
8
|
+
* `isEmptyLastParagraph` + `exitSubThread`.
|
|
9
|
+
* - Backspace at offset 0 of the header turns the container into a paragraph, the body goes right after it:
|
|
10
|
+
* `dissolveToParagraph`.
|
|
11
|
+
* - Enter in the header of a container whose body is hidden creates the paragraph after the container:
|
|
12
|
+
* `insertParagraphAfter`.
|
|
13
|
+
* - A multi-block paste with the caret in the HEADER: the first block completes the header, the others go to
|
|
14
|
+
* the top of the body, the header text after the caret follows the last one: `pasteIntoHeader`.
|
|
15
|
+
*
|
|
16
|
+
* Every helper mutates the tree only (no commit): the caller owns the transaction, so that one user action stays
|
|
17
|
+
* one undo step.
|
|
18
|
+
*
|
|
19
|
+
* @import { Nabu, NabuNode } from "./nabu.svelte.js";
|
|
20
|
+
* @import { Block } from "./block.svelte.js";
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Whether `source` is an EMPTY paragraph that is the last child of `container`'s body.
|
|
25
|
+
* @param {{ children: Block[] }} container a block with children (a MegaBlock)
|
|
26
|
+
* @param {Block} source
|
|
27
|
+
* @returns {boolean}
|
|
28
|
+
*/
|
|
29
|
+
export function isEmptyLastParagraph(container, source) {
|
|
30
|
+
const kids = container.children;
|
|
31
|
+
return source.type === 'paragraph'
|
|
32
|
+
&& /** @type {any} */ (source.parent) === container
|
|
33
|
+
&& kids[kids.length - 1] === source
|
|
34
|
+
&& !(/** @type {any} */ (source).text?.length);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Exits the sub-thread: moves `child` right after `container`, in the container's own parent. Nothing else is
|
|
39
|
+
* touched (the other children stay in the body, which may become empty).
|
|
40
|
+
* @param {NabuNode} child a node of the container's body
|
|
41
|
+
* @param {NabuNode} container
|
|
42
|
+
*/
|
|
43
|
+
export function exitSubThread(child, container) {
|
|
44
|
+
child.moveAfter(container);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Turns `container` into a paragraph (same node: its id, header text, marks and position survive) and moves its
|
|
49
|
+
* body right after it, in order, in the same parent. Nothing is lost. The props listed in `drop` are removed
|
|
50
|
+
* from the node (container-specific flags, e.g. `collapsed`).
|
|
51
|
+
* @param {NabuNode} container
|
|
52
|
+
* @param {string[]} [drop]
|
|
53
|
+
*/
|
|
54
|
+
export function dissolveToParagraph(container, drop = []) {
|
|
55
|
+
const kids = container.children() ?? [];
|
|
56
|
+
for (const kid of [...kids].reverse()) kid.moveAfter(container);
|
|
57
|
+
for (const key of drop) if (/** @type {any} */ (container.data).get(key) !== undefined) /** @type {any} */ (container.data).delete(key);
|
|
58
|
+
container.data.set('type', 'paragraph');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Inserts a paragraph holding `delta` right after `container`, in its parent. The insert commits.
|
|
63
|
+
* @param {Nabu} nabu
|
|
64
|
+
* @param {NabuNode} container
|
|
65
|
+
* @param {import('loro-crdt').Delta<string>[]} delta
|
|
66
|
+
* @returns {Block}
|
|
67
|
+
*/
|
|
68
|
+
export function insertParagraphAfter(nabu, container, delta) {
|
|
69
|
+
const parentId = container.parent()?.id.toString() ?? null;
|
|
70
|
+
return nabu.insert('paragraph', { delta }, parentId, (container.index() ?? 0) + 1);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Multi-block paste with the caret in the HEADER of a container (the header is text only, the body holds the
|
|
75
|
+
* blocks). Consistent with "Enter in the header creates the first body child":
|
|
76
|
+
* - the FIRST pasted block, when it is a text leaf, completes the header at the caret (the header keeps its
|
|
77
|
+
* type); a first block that is not text (container, image…) leaves the header as it is;
|
|
78
|
+
* - the other blocks go to the TOP of the body, in order, above the existing children;
|
|
79
|
+
* - the header text AFTER the caret follows the pasted blocks: it merges into the LAST pasted block when that
|
|
80
|
+
* one is text (cut at the edge of a partial copy, or a default text block), otherwise it becomes a new default
|
|
81
|
+
* text block right after it;
|
|
82
|
+
* - the caret ends at the junction (end of the pasted text), or at the start of the tail block.
|
|
83
|
+
* A single pasted text block is not for this helper (the engine's inline merge handles it): returns null and
|
|
84
|
+
* touches nothing, like `Block#acceptPaste`. Does NOT expand a collapsed container (its caller does: the body
|
|
85
|
+
* must be visible for the pasted content to be). Tree operations only, no commit.
|
|
86
|
+
*
|
|
87
|
+
* @param {Nabu} nabu
|
|
88
|
+
* @param {Block} container the block holding the caret in its header
|
|
89
|
+
* @param {number} offset the caret offset in the header text
|
|
90
|
+
* @param {import("../utils/extensions.js").PasteBlock[]} blocks
|
|
91
|
+
* @returns {{ block: Block | null, offset: number } | null}
|
|
92
|
+
*/
|
|
93
|
+
export function pasteIntoHeader(nabu, container, offset, blocks) {
|
|
94
|
+
if (!blocks?.length || (blocks.length === 1 && !blocks[0].children?.length)) return null;
|
|
95
|
+
const text = /** @type {any} */ (container.behaviors?.get('text'));
|
|
96
|
+
if (!text) return null;
|
|
97
|
+
const defaultType = nabu.defaultBlockType && nabu.registry.has(nabu.defaultBlockType) ? nabu.defaultBlockType : 'paragraph';
|
|
98
|
+
const DefaultClass = nabu.registry.get(defaultType);
|
|
99
|
+
if (!DefaultClass) return null;
|
|
100
|
+
|
|
101
|
+
/** @param {any[]} delta */
|
|
102
|
+
const deltaLen = (delta) => delta.reduce((n, op) => n + (typeof op.insert === 'string' ? op.insert.length : 0), 0);
|
|
103
|
+
|
|
104
|
+
// ── 1. Cut the header at the caret ──
|
|
105
|
+
const tailDelta = text.container.sliceDelta(offset, text.container.length);
|
|
106
|
+
if (text.container.length > offset) text.delete({ index: offset, length: text.container.length - offset });
|
|
107
|
+
|
|
108
|
+
// ── 2. The first block completes the header when it is a text leaf ──
|
|
109
|
+
const first = blocks[0];
|
|
110
|
+
const completesHeader = !first.children?.length && Array.isArray(first.delta);
|
|
111
|
+
/** @type {{ block: Block | null, offset: number }} */
|
|
112
|
+
let landing = { block: container, offset };
|
|
113
|
+
if (completesHeader) {
|
|
114
|
+
const delta = first.delta ?? [];
|
|
115
|
+
if (delta.length) text.applyDelta([{ retain: offset }, ...delta]);
|
|
116
|
+
landing = { block: container, offset: offset + deltaLen(delta) };
|
|
117
|
+
}
|
|
118
|
+
const body = completesHeader ? blocks.slice(1) : blocks;
|
|
119
|
+
const last = body[body.length - 1];
|
|
120
|
+
const hasTail = deltaLen(tailDelta) > 0;
|
|
121
|
+
const lastIsText = !!last && !last.children?.length
|
|
122
|
+
&& (last.partial === 'end' || last.partial === 'both' || last.type === nabu.defaultBlockType);
|
|
123
|
+
const mergesTail = hasTail && lastIsText;
|
|
124
|
+
|
|
125
|
+
// ── 3. The body blocks, at the TOP of the body ──
|
|
126
|
+
// A placeholder at index 0 anchors the chain (insertPasteBlock inserts AFTER a node); it is removed at the end.
|
|
127
|
+
// (`Class.create`, not `nabu.insert`: the latter commits, and the whole paste is ONE transaction.)
|
|
128
|
+
const placeholder = DefaultClass.create(nabu, defaultType, {}, container.node.id.toString(), 0);
|
|
129
|
+
/** @type {NabuNode} */
|
|
130
|
+
let previous = placeholder.node;
|
|
131
|
+
/** @type {NabuNode | null} */
|
|
132
|
+
let lastNode = null;
|
|
133
|
+
body.forEach((pb, i) => {
|
|
134
|
+
if (mergesTail && i === body.length - 1) {
|
|
135
|
+
// The tail joins the last pasted text, in a default text block (the container's own type is not
|
|
136
|
+
// allowed in its body).
|
|
137
|
+
const delta = [...(pb.delta ?? []), ...tailDelta];
|
|
138
|
+
previous = nabu.insertPasteBlock({ type: defaultType, partial: false, delta }, previous);
|
|
139
|
+
landing = { block: nabu.blocks.get(previous.id.toString()) ?? null, offset: deltaLen(pb.delta ?? []) };
|
|
140
|
+
} else {
|
|
141
|
+
previous = nabu.insertPasteBlock(pb, previous);
|
|
142
|
+
}
|
|
143
|
+
lastNode = previous;
|
|
144
|
+
});
|
|
145
|
+
if (hasTail && !mergesTail) {
|
|
146
|
+
previous = nabu.insertPasteBlock({ type: defaultType, partial: false, delta: tailDelta }, previous);
|
|
147
|
+
landing = { block: nabu.blocks.get(previous.id.toString()) ?? null, offset: 0 };
|
|
148
|
+
} else if (!hasTail && lastNode) {
|
|
149
|
+
const end = textEnd(lastNode);
|
|
150
|
+
if (end) landing = { block: nabu.blocks.get(end.node.id.toString()) ?? null, offset: end.offset };
|
|
151
|
+
}
|
|
152
|
+
nabu.tree.delete(placeholder.node.id);
|
|
153
|
+
return landing;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The end of the last text under `node` (the node itself when it has no children), or null when that last leaf
|
|
158
|
+
* holds no text (an image…).
|
|
159
|
+
* @param {NabuNode} node
|
|
160
|
+
* @returns {{ node: NabuNode, offset: number } | null}
|
|
161
|
+
*/
|
|
162
|
+
function textEnd(node) {
|
|
163
|
+
let current = node;
|
|
164
|
+
for (let kids = current.children() ?? []; kids.length; kids = current.children() ?? []) current = kids[kids.length - 1];
|
|
165
|
+
const text = /** @type {any} */ (current.data).get('text');
|
|
166
|
+
return text ? { node: current, offset: text.length } : null;
|
|
167
|
+
}
|
|
@@ -19,24 +19,45 @@ export class TextBlock extends Block {
|
|
|
19
19
|
* @param {LoroText} [textContainer] - Existing LoroText container. Defaults to the node's "text" container (created if missing).
|
|
20
20
|
*/
|
|
21
21
|
constructor(nabu: Nabu, node: NabuNode, textContainer?: LoroText);
|
|
22
|
-
/**
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
22
|
+
/**
|
|
23
|
+
* The Loro text of the block: the source of its `text` and `delta`.
|
|
24
|
+
* @type {LoroText}
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
public container: LoroText;
|
|
28
|
+
/**
|
|
29
|
+
* The text behavior of the block (`behaviors.get('text')`): reads and edits the text, selection, marks and clipboard form.
|
|
30
|
+
* @returns {TextBehavior}
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
33
|
+
public get behavior(): TextBehavior;
|
|
34
|
+
/** The plain text of the block (reactive). @public */
|
|
35
|
+
public get text(): string;
|
|
36
|
+
/** The text with its marks, as a Loro delta (reactive). @public */
|
|
37
|
+
public get delta(): import("loro-crdt").Delta<string>[];
|
|
38
|
+
/** The selection inside this block, `{ from, to, direction }` in text offsets, or `null` (reactive). @public */
|
|
39
|
+
public selection: {
|
|
29
40
|
from: number;
|
|
30
41
|
to: number;
|
|
31
42
|
isCollapsed: boolean;
|
|
32
43
|
direction: "forward" | "backward" | "none";
|
|
33
44
|
} | null;
|
|
34
|
-
/**
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
45
|
+
/**
|
|
46
|
+
* @param {Block} block
|
|
47
|
+
* @public
|
|
48
|
+
*/
|
|
49
|
+
public absorbs(block: Block): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* @param {import('loro-crdt').Delta<string>[]} data
|
|
52
|
+
* @public
|
|
53
|
+
*/
|
|
54
|
+
public applyDelta(data?: import("loro-crdt").Delta<string>[]): void;
|
|
55
|
+
/**
|
|
56
|
+
* @param {Parameters<Block["split"]>[0]} [options]
|
|
57
|
+
* @returns {ReturnType<Block["split"]>}
|
|
58
|
+
* @public
|
|
59
|
+
*/
|
|
60
|
+
public split(options?: Parameters<Block["split"]>[0]): ReturnType<Block["split"]>;
|
|
40
61
|
#private;
|
|
41
62
|
}
|
|
42
63
|
export type TextBlockNode = NabuNode<{
|
|
@@ -30,45 +30,79 @@ export class TextBlock extends Block {
|
|
|
30
30
|
constructor(nabu, node, textContainer) {
|
|
31
31
|
super(nabu, node);
|
|
32
32
|
const data = /** @type {TextBlockNode} */ (node).data;
|
|
33
|
-
/**
|
|
33
|
+
/**
|
|
34
|
+
* The Loro text of the block: the source of its `text` and `delta`.
|
|
35
|
+
* @type {LoroText}
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
34
38
|
this.container = textContainer ?? data.get("text") ?? data.setContainer("text", new LoroText());
|
|
35
39
|
this.#behavior = new TextBehavior(/** @type {Block} */ (/** @type {unknown} */ (this)), this.container);
|
|
36
40
|
this.behaviors.set("text", this.#behavior);
|
|
37
41
|
}
|
|
38
42
|
|
|
39
|
-
/**
|
|
43
|
+
/**
|
|
44
|
+
* The text behavior of the block (`behaviors.get('text')`): reads and edits the text, selection, marks and clipboard form.
|
|
45
|
+
* @returns {TextBehavior}
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
40
48
|
get behavior() { return this.#behavior; }
|
|
41
49
|
|
|
50
|
+
/** The plain text of the block (reactive). @public */
|
|
42
51
|
get text() { return this.behavior.text; }
|
|
43
52
|
|
|
53
|
+
/** The text with its marks, as a Loro delta (reactive). @public */
|
|
44
54
|
get delta() { return this.behavior.delta; }
|
|
45
55
|
|
|
56
|
+
/** The selection inside this block, `{ from, to, direction }` in text offsets, or `null` (reactive). @public */
|
|
46
57
|
selection = $derived(this.behavior.selection);
|
|
58
|
+
/** Caret positions of the block: its text length plus one (reactive). @public */
|
|
47
59
|
positions = $derived(this.behavior.positions);
|
|
60
|
+
/** Number of words in the text (reactive). @public */
|
|
48
61
|
wordCount = $derived(this.behavior.wordCount);
|
|
49
62
|
|
|
50
|
-
/**
|
|
63
|
+
/**
|
|
64
|
+
* @param {InputEvent} event
|
|
65
|
+
* @public
|
|
66
|
+
*/
|
|
51
67
|
beforeinput(event) { return this.behavior.handleBeforeInput(event); }
|
|
52
68
|
|
|
53
69
|
/**
|
|
54
70
|
* Retrouve le nœud texte et l'offset DOM pour un offset Modèle donné
|
|
55
71
|
* @param {number} targetOffset
|
|
56
72
|
* @returns {{node: Node, offset: number} | null}
|
|
73
|
+
* @public
|
|
57
74
|
*/
|
|
58
75
|
getDOMPoint(targetOffset) { return this.behavior.getDOMPoint(targetOffset); }
|
|
59
76
|
|
|
60
|
-
/**
|
|
77
|
+
/**
|
|
78
|
+
* @param {Block} block
|
|
79
|
+
* @public
|
|
80
|
+
*/
|
|
61
81
|
absorbs(block) { return this.behavior.absorbs(block); }
|
|
62
82
|
|
|
63
|
-
/**
|
|
83
|
+
/**
|
|
84
|
+
* @param {number} index
|
|
85
|
+
* @param {string} text
|
|
86
|
+
* @public
|
|
87
|
+
*/
|
|
64
88
|
insert(index, text) { return this.behavior.insert(index, text); }
|
|
65
89
|
|
|
66
|
-
/**
|
|
90
|
+
/**
|
|
91
|
+
* @param {Parameters<Block["delete"]>[0]} [deletion]
|
|
92
|
+
* @public
|
|
93
|
+
*/
|
|
67
94
|
delete(deletion) { return this.behavior.delete(deletion); }
|
|
68
95
|
|
|
69
|
-
/**
|
|
96
|
+
/**
|
|
97
|
+
* @param {import('loro-crdt').Delta<string>[]} data
|
|
98
|
+
* @public
|
|
99
|
+
*/
|
|
70
100
|
applyDelta(data = []) { return this.behavior.applyDelta(data); }
|
|
71
101
|
|
|
72
|
-
/**
|
|
102
|
+
/**
|
|
103
|
+
* @param {Parameters<Block["split"]>[0]} [options]
|
|
104
|
+
* @returns {ReturnType<Block["split"]>}
|
|
105
|
+
* @public
|
|
106
|
+
*/
|
|
73
107
|
split(options) { return this.behavior.split(options); }
|
|
74
108
|
}
|
|
@@ -1,35 +1,24 @@
|
|
|
1
1
|
<script>
|
|
2
2
|
import Block from "../Block.svelte";
|
|
3
3
|
import RichText from '../../behaviors/text/RichText.svelte';
|
|
4
|
-
import { getPlaceholder } from '../../utils/placeholders.js';
|
|
5
4
|
/** @type {{ block: import('./toggle.svelte.js').ToggleBlock }} */
|
|
6
5
|
let { block } = $props();
|
|
7
6
|
</script>
|
|
8
7
|
|
|
9
|
-
<div class="nabu-toggle" class:collapsed={block.collapsed}
|
|
10
|
-
<div class="nabu-toggle-head">
|
|
11
|
-
<button
|
|
8
|
+
<div class="nabu-toggle" class:collapsed={block.collapsed}><div class="nabu-toggle-head"><button
|
|
12
9
|
class="nabu-toggle-arrow"
|
|
13
10
|
data-nabu-ui
|
|
14
11
|
contenteditable="false"
|
|
15
12
|
type="button"
|
|
16
13
|
aria-expanded={!block.collapsed}
|
|
17
14
|
onmousedown={(e) => { e.preventDefault(); block.toggleCollapsed(); }}
|
|
18
|
-
>{block.collapsed ? '▸' : '▾'}</button
|
|
19
|
-
<div
|
|
15
|
+
>{block.collapsed ? '▸' : '▾'}</button><div
|
|
20
16
|
bind:this={block.element}
|
|
21
17
|
class="nabu-toggle-header"
|
|
22
18
|
data-block-id={block.id}
|
|
23
19
|
data-block-type="toggle"
|
|
24
|
-
data-placeholder={block.
|
|
25
|
-
><RichText delta={block.delta} /></div>
|
|
26
|
-
</div>
|
|
27
|
-
{#if !block.collapsed}
|
|
28
|
-
<div class="nabu-toggle-body">
|
|
29
|
-
{#each block.children as child (child.id)}<Block block={child} />{/each}
|
|
30
|
-
</div>
|
|
31
|
-
{/if}
|
|
32
|
-
</div>
|
|
20
|
+
data-placeholder={block.placeholder}
|
|
21
|
+
><RichText delta={block.delta} /></div></div>{#if !block.collapsed}<div class="nabu-toggle-body">{#each block.children as child (child.id)}<Block block={child} />{/each}</div>{/if}</div>
|
|
33
22
|
|
|
34
23
|
<style>
|
|
35
24
|
.nabu-toggle {
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `<details>` construct that starts at line `at`, if it is the exact one (see the header), else `null`.
|
|
3
|
+
* @param {string[]} lines
|
|
4
|
+
* @param {number} at
|
|
5
|
+
* @returns {{ open: boolean, summary: string, bodyStart: number, end: number } | null}
|
|
6
|
+
*/
|
|
7
|
+
export function parseDetails(lines: string[], at: number): {
|
|
8
|
+
open: boolean;
|
|
9
|
+
summary: string;
|
|
10
|
+
bodyStart: number;
|
|
11
|
+
end: number;
|
|
12
|
+
} | null;
|
|
13
|
+
/**
|
|
14
|
+
* The toggle as markdown. `head` is the inline markdown of the header, `body` the markdown of each body block.
|
|
15
|
+
* @param {boolean} collapsed
|
|
16
|
+
* @param {string} head
|
|
17
|
+
* @param {string[]} body
|
|
18
|
+
* @returns {string}
|
|
19
|
+
*/
|
|
20
|
+
export function toggleMarkdown(collapsed: boolean, head: string, body: string[]): string;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown for the Toggle block: the `<details>` construct, and ONLY that one.
|
|
3
|
+
*
|
|
4
|
+
* Export (what Nabu emits):
|
|
5
|
+
*
|
|
6
|
+
* <details> (or `<details open>` when expanded)
|
|
7
|
+
* <summary>header, inline markdown</summary>
|
|
8
|
+
*
|
|
9
|
+
* body, block markdown
|
|
10
|
+
*
|
|
11
|
+
* </details>
|
|
12
|
+
*
|
|
13
|
+
* Import / paste recognises exactly this construct: an opening line that is `<details>` or `<details open>` (nothing else
|
|
14
|
+
* on it, no attribute, lower case), a `<summary>…</summary>` that follows it (on the same line, as the first Nabu exports
|
|
15
|
+
* wrote it, or on the next one; the summary text may span lines), the body as markdown, and a closing line that is
|
|
16
|
+
* `</details>`; nesting is allowed (a `<details>` of the body is matched with its own closing line, fenced code is
|
|
17
|
+
* skipped). Everything else stays literal text: any other tag, an attribute (`<details class=x>`, `<details open=…>`,
|
|
18
|
+
* `<details onclick=…>`), no summary, no closing line. No attribute or tag is ever passed through to the document: the
|
|
19
|
+
* result is a Toggle block (`collapsed` = not `open`) with a header and a body of Nabu blocks.
|
|
20
|
+
*
|
|
21
|
+
* Pure functions, no DOM, no runes.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
const OPEN_RE = /^<details( open)?>[ \t]*(.*)$/;
|
|
25
|
+
const CLOSE_RE = /^<\/details>[ \t]*$/;
|
|
26
|
+
const SUMMARY_OPEN = '<summary>';
|
|
27
|
+
/** A `</summary>` ending a line, unless it is escaped (an odd number of backslashes before the `<`: `\</summary>` is text). */
|
|
28
|
+
const SUMMARY_CLOSE_RE = /(^|[^\\])((?:\\\\)*)<\/summary>[ \t]*$/;
|
|
29
|
+
const FENCE_RE = /^ {0,3}(`{3,}[^`]*|~{3,}.*)$/;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Index of the line that closes the fence opened on line `at`, or -1 (an unclosed fence runs to the end of the text: it is
|
|
33
|
+
* not skipped, the `</details>` after it still closes the toggle).
|
|
34
|
+
* @param {string[]} lines
|
|
35
|
+
* @param {number} at
|
|
36
|
+
* @returns {number}
|
|
37
|
+
*/
|
|
38
|
+
function fenceEnd(lines, at) {
|
|
39
|
+
const open = /^ {0,3}(`{3,}|~{3,})/.exec(lines[at]);
|
|
40
|
+
if (!open) return -1;
|
|
41
|
+
for (let k = at + 1; k < lines.length; k++) {
|
|
42
|
+
const close = /^ {0,3}(`+|~+)[ \t]*$/.exec(lines[k]);
|
|
43
|
+
if (close && close[1][0] === open[1][0] && close[1].length >= open[1].length) return k;
|
|
44
|
+
}
|
|
45
|
+
return -1;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Index of the `</details>` line that closes a body starting at line `from`, or -1.
|
|
50
|
+
* @param {string[]} lines
|
|
51
|
+
* @param {number} from
|
|
52
|
+
* @returns {number}
|
|
53
|
+
*/
|
|
54
|
+
function findClose(lines, from) {
|
|
55
|
+
for (let j = from; j < lines.length; j++) {
|
|
56
|
+
const line = lines[j];
|
|
57
|
+
if (CLOSE_RE.test(line)) return j;
|
|
58
|
+
if (FENCE_RE.test(line)) {
|
|
59
|
+
const end = fenceEnd(lines, j);
|
|
60
|
+
if (end >= 0) { j = end; continue; }
|
|
61
|
+
}
|
|
62
|
+
if (OPEN_RE.test(line)) {
|
|
63
|
+
const inner = parseDetails(lines, j);
|
|
64
|
+
if (inner) j = inner.end;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return -1;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The `<details>` construct that starts at line `at`, if it is the exact one (see the header), else `null`.
|
|
72
|
+
* @param {string[]} lines
|
|
73
|
+
* @param {number} at
|
|
74
|
+
* @returns {{ open: boolean, summary: string, bodyStart: number, end: number } | null}
|
|
75
|
+
*/
|
|
76
|
+
export function parseDetails(lines, at) {
|
|
77
|
+
const head = OPEN_RE.exec(lines[at] ?? '');
|
|
78
|
+
if (!head) return null;
|
|
79
|
+
// The summary starts on the same line as the opener, or on the next one.
|
|
80
|
+
let s = at;
|
|
81
|
+
let first = head[2];
|
|
82
|
+
if (!first) { s = at + 1; first = lines[s] ?? ''; }
|
|
83
|
+
if (!first.startsWith(SUMMARY_OPEN)) return null;
|
|
84
|
+
first = first.slice(SUMMARY_OPEN.length);
|
|
85
|
+
|
|
86
|
+
const parts = [];
|
|
87
|
+
let k = s;
|
|
88
|
+
let current = first;
|
|
89
|
+
for (;;) {
|
|
90
|
+
const close = SUMMARY_CLOSE_RE.exec(current);
|
|
91
|
+
if (close) { parts.push(current.slice(0, close.index + close[1].length + close[2].length)); break; }
|
|
92
|
+
parts.push(current);
|
|
93
|
+
if (++k >= lines.length || !lines[k].trim()) return null; // no `</summary>` before a blank line / the end
|
|
94
|
+
current = lines[k];
|
|
95
|
+
}
|
|
96
|
+
const bodyStart = k + 1;
|
|
97
|
+
const end = findClose(lines, bodyStart);
|
|
98
|
+
if (end < 0) return null;
|
|
99
|
+
return { open: !!head[1], summary: parts.join('\n'), bodyStart, end };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The toggle as markdown. `head` is the inline markdown of the header, `body` the markdown of each body block.
|
|
104
|
+
* @param {boolean} collapsed
|
|
105
|
+
* @param {string} head
|
|
106
|
+
* @param {string[]} body
|
|
107
|
+
* @returns {string}
|
|
108
|
+
*/
|
|
109
|
+
export function toggleMarkdown(collapsed, head, body) {
|
|
110
|
+
const content = body.filter(Boolean).join('\n\n');
|
|
111
|
+
return `${collapsed ? '<details>' : '<details open>'}\n<summary>${head}</summary>\n\n${content ? content + '\n\n' : ''}</details>`;
|
|
112
|
+
}
|
|
@@ -1 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* The toggle block: a collapsible container with its own header text and a body of child blocks; the `toggle` action and the "Toggle" slash entry.
|
|
3
|
+
*/
|
|
4
|
+
export const ToggleExtension: import("../../extend.js").Extension;
|
|
@@ -2,10 +2,16 @@ import { extension } from "../../utils/extensions";
|
|
|
2
2
|
import { ToggleBlock } from "./toggle.svelte.js";
|
|
3
3
|
import ToggleComponent from "./Toggle.svelte";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* The toggle block: a collapsible container with its own header text and a body of child blocks; the `toggle` action and the "Toggle" slash entry.
|
|
7
|
+
*/
|
|
5
8
|
export const ToggleExtension = extension("toggle", {
|
|
6
9
|
// @ts-ignore — static-side variance on create()
|
|
7
10
|
block: ToggleBlock,
|
|
8
11
|
component: ToggleComponent,
|
|
12
|
+
slashCommands: [
|
|
13
|
+
{ title: 'Toggle', icon: '▸', aliases: ['toggle', 'collapse', 'details'], group: 'blocks', order: 30, run: (e) => e.exec('toggle') },
|
|
14
|
+
],
|
|
9
15
|
actions: {
|
|
10
16
|
'toggle': (nabu) => nabu.exec('block:transform', { type: 'toggle' }),
|
|
11
17
|
},
|