@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
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { extension } from "../../utils/extensions";
|
|
2
|
+
import { ToggleBlock } from "./toggle.svelte.js";
|
|
3
|
+
import ToggleComponent from "./Toggle.svelte";
|
|
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
|
+
*/
|
|
8
|
+
export const ToggleExtension = extension("toggle", {
|
|
9
|
+
// @ts-ignore — static-side variance on create()
|
|
10
|
+
block: ToggleBlock,
|
|
11
|
+
component: ToggleComponent,
|
|
12
|
+
slashCommands: [
|
|
13
|
+
{ title: 'Toggle', icon: '▸', aliases: ['toggle', 'collapse', 'details'], group: 'blocks', order: 30, run: (e) => e.exec('toggle') },
|
|
14
|
+
],
|
|
15
|
+
actions: {
|
|
16
|
+
'toggle': (nabu) => nabu.exec('block:transform', { type: 'toggle' }),
|
|
17
|
+
},
|
|
18
|
+
});
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { Nabu, NabuNode } from "../nabu.svelte.js";
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {NabuNode<{type: "toggle", text: LoroText, collapsed: boolean}>} ToggleNode
|
|
6
|
+
*/
|
|
7
|
+
export class ToggleBlock extends MegaBlock {
|
|
8
|
+
static placeholder: string;
|
|
9
|
+
/**
|
|
10
|
+
* `<details>` / `<details open>` + `<summary>` + the body as markdown + `</details>` (see markdown.js).
|
|
11
|
+
* @param {import('../../utils/extensions.js').PasteBlock} pb
|
|
12
|
+
* @param {{ recurse: (child: import('../../utils/extensions.js').PasteBlock, ctx?: object) => string, recurseAll?: (children: import('../../utils/extensions.js').PasteBlock[], ctx?: object) => string[] }} helpers
|
|
13
|
+
*/
|
|
14
|
+
static toMarkdown(pb: import("../../utils/extensions.js").PasteBlock, { recurse, recurseAll }: {
|
|
15
|
+
recurse: (child: import("../../utils/extensions.js").PasteBlock, ctx?: object) => string;
|
|
16
|
+
recurseAll?: (children: import("../../utils/extensions.js").PasteBlock[], ctx?: object) => string[];
|
|
17
|
+
}): string;
|
|
18
|
+
/**
|
|
19
|
+
* Markdown (paste AND `importMarkdown`): the exact `<details>` construct Nabu emits, nothing else (see markdown.js).
|
|
20
|
+
* An opening line without a matching `</details>`, with attributes or without a `<summary>` is not a toggle: it stays text.
|
|
21
|
+
*/
|
|
22
|
+
static markdownRules: {
|
|
23
|
+
priority: number;
|
|
24
|
+
interruptsParagraph: boolean;
|
|
25
|
+
detect: (line: string, lines: string[], i: number) => boolean;
|
|
26
|
+
consume: (lines: string[], i: number) => number;
|
|
27
|
+
}[];
|
|
28
|
+
/**
|
|
29
|
+
* The toggle (header = the summary, parsed as inline markdown; body = the lines between the summary and `</details>`,
|
|
30
|
+
* parsed as a document of their own, nested toggles included).
|
|
31
|
+
* @param {string[]} lines
|
|
32
|
+
* @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[], parseBlocks?: (lines: string[]) => import('../../utils/extensions.js').PasteBlock[] }} helpers
|
|
33
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
34
|
+
*/
|
|
35
|
+
static fromMarkdown(lines: string[], { parseInline, parseBlocks }: {
|
|
36
|
+
parseInline: (text: string) => import("loro-crdt").Delta<string>[];
|
|
37
|
+
parseBlocks?: (lines: string[]) => import("../../utils/extensions.js").PasteBlock[];
|
|
38
|
+
}): import("../../utils/extensions.js").PasteBlock | null;
|
|
39
|
+
/**
|
|
40
|
+
* @param {import('../../utils/extensions.js').PasteBlock} pb
|
|
41
|
+
* @param {{ recurse: (child: import('../../utils/extensions.js').PasteBlock) => string }} helpers
|
|
42
|
+
*/
|
|
43
|
+
static toHtml(pb: import("../../utils/extensions.js").PasteBlock, { recurse }: {
|
|
44
|
+
recurse: (child: import("../../utils/extensions.js").PasteBlock) => string;
|
|
45
|
+
}): string;
|
|
46
|
+
static htmlRules: {
|
|
47
|
+
selector: string;
|
|
48
|
+
}[];
|
|
49
|
+
/**
|
|
50
|
+
* @param {Element} el
|
|
51
|
+
* @param {{ parseInline: (el: Element) => import('loro-crdt').Delta<string>[], parseChildren?: (el: Element) => import('../../utils/extensions.js').PasteBlock[] }} helpers
|
|
52
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
53
|
+
*/
|
|
54
|
+
static fromHTML(el: Element, { parseInline, parseChildren }: {
|
|
55
|
+
parseInline: (el: Element) => import("loro-crdt").Delta<string>[];
|
|
56
|
+
parseChildren?: (el: Element) => import("../../utils/extensions.js").PasteBlock[];
|
|
57
|
+
}): import("../../utils/extensions.js").PasteBlock | null;
|
|
58
|
+
/**
|
|
59
|
+
* @param {Nabu} nabu
|
|
60
|
+
* @param {string} type
|
|
61
|
+
* @param {Record<string, any>} [props={}]
|
|
62
|
+
* @param {string|null} [parentId=null]
|
|
63
|
+
* @param {number|null} [index=null]
|
|
64
|
+
*/
|
|
65
|
+
static create(nabu: Nabu, type: string, props?: Record<string, any>, parentId?: string | null, index?: number | null): ToggleBlock;
|
|
66
|
+
/** @param {Nabu} nabu @param {ToggleNode} node */
|
|
67
|
+
constructor(nabu: Nabu, node: ToggleNode);
|
|
68
|
+
container: LoroText;
|
|
69
|
+
/** @type {TextBehavior} */
|
|
70
|
+
behavior: TextBehavior;
|
|
71
|
+
set collapsed(v: boolean);
|
|
72
|
+
/**
|
|
73
|
+
* Whether the body is hidden. In a read-only editor a reader may open or close a toggle to read it: that is VIEW STATE
|
|
74
|
+
* (kept in memory, not in the document: no Loro write, no commit, no undo step, nothing synced or persisted, serializers
|
|
75
|
+
* keep reporting the document's own state), and it is ignored again as soon as the editor is not read-only.
|
|
76
|
+
* @type {boolean}
|
|
77
|
+
*/
|
|
78
|
+
get collapsed(): boolean;
|
|
79
|
+
/** A collapsed toggle stays collapsed on the clipboard. */
|
|
80
|
+
get clipboardProps(): {
|
|
81
|
+
collapsed: boolean;
|
|
82
|
+
};
|
|
83
|
+
component: import("svelte").Component<any, any, string> | import("svelte").Component<{
|
|
84
|
+
block: import("./toggle.svelte.js").ToggleBlock;
|
|
85
|
+
}, {}, "">;
|
|
86
|
+
get text(): string;
|
|
87
|
+
get delta(): import("loro-crdt").Delta<string>[];
|
|
88
|
+
selection: {
|
|
89
|
+
from: number;
|
|
90
|
+
to: number;
|
|
91
|
+
isCollapsed: boolean;
|
|
92
|
+
direction: "forward" | "backward" | "none";
|
|
93
|
+
} | null;
|
|
94
|
+
/**
|
|
95
|
+
* Set the collapsed state and persist to Loro (in a read-only editor: only what the reader sees, see {@link ToggleBlock#collapsed}).
|
|
96
|
+
* @param {boolean} v
|
|
97
|
+
*/
|
|
98
|
+
setCollapsed(v: boolean): void;
|
|
99
|
+
/**
|
|
100
|
+
* Toggle the collapsed state.
|
|
101
|
+
*/
|
|
102
|
+
toggleCollapsed(): void;
|
|
103
|
+
/** @param {import('../block.svelte.js').Block} block */
|
|
104
|
+
absorbs(block: import("../block.svelte.js").Block): boolean;
|
|
105
|
+
/** @param {import('loro-crdt').Delta<string>[]} data */
|
|
106
|
+
applyDelta(data?: import("loro-crdt").Delta<string>[]): void;
|
|
107
|
+
/**
|
|
108
|
+
* Enter in a body child (called through `ascend("onSplit")`). Only the Enter that EXITS the sub-thread is
|
|
109
|
+
* taken here: on an EMPTY paragraph that is the LAST child, the paragraph moves right after the toggle and
|
|
110
|
+
* takes the caret. Anything else is declined (falsy) and goes through the regular split of the child's type.
|
|
111
|
+
* @param {Event | null} event
|
|
112
|
+
* @param {{ from: import('../block.svelte.js').Block }} data
|
|
113
|
+
*/
|
|
114
|
+
onSplit(event: Event | null, data: {
|
|
115
|
+
from: import("../block.svelte.js").Block;
|
|
116
|
+
}): false | {
|
|
117
|
+
block: import("../block.svelte.js").Block;
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* @param {InputEvent} event
|
|
121
|
+
* @returns {boolean}
|
|
122
|
+
*/
|
|
123
|
+
beforeinput(event: InputEvent): boolean;
|
|
124
|
+
#private;
|
|
125
|
+
}
|
|
126
|
+
export type ToggleNode = NabuNode<{
|
|
127
|
+
type: "toggle";
|
|
128
|
+
text: LoroText;
|
|
129
|
+
collapsed: boolean;
|
|
130
|
+
}>;
|
|
131
|
+
import { MegaBlock } from "../megablock.svelte.js";
|
|
132
|
+
import { LoroText } from "loro-crdt";
|
|
133
|
+
import { TextBehavior } from "../../behaviors/text";
|
|
134
|
+
import type { Nabu } from "../nabu.svelte.js";
|
|
135
|
+
import type { NabuNode } from "../nabu.svelte.js";
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
import { MegaBlock } from "../megablock.svelte.js";
|
|
2
|
+
import { TextBehavior, deltaToHtml, deltaToMarkdown } from "../../behaviors/text";
|
|
3
|
+
import { LoroText } from "loro-crdt";
|
|
4
|
+
import ToggleComponent from "./Toggle.svelte";
|
|
5
|
+
import { parseDetails, toggleMarkdown } from "./markdown.js";
|
|
6
|
+
import { isEmptyLastParagraph, exitSubThread, dissolveToParagraph, insertParagraphAfter, pasteIntoHeader } from "../subthread.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @import { Nabu, NabuNode } from "../nabu.svelte.js";
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {NabuNode<{type: "toggle", text: LoroText, collapsed: boolean}>} ToggleNode
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
// @ts-ignore — static-side variance on create()
|
|
17
|
+
export class ToggleBlock extends MegaBlock {
|
|
18
|
+
/** @param {Nabu} nabu @param {ToggleNode} node */
|
|
19
|
+
constructor(nabu, node) {
|
|
20
|
+
super(nabu, node);
|
|
21
|
+
|
|
22
|
+
const data = node.data;
|
|
23
|
+
this.container = data.get("text") ?? data.setContainer("text", new LoroText());
|
|
24
|
+
|
|
25
|
+
/** @type {TextBehavior} */
|
|
26
|
+
this.behavior = new TextBehavior(this, this.container);
|
|
27
|
+
this.behaviors.set("text", this.behavior);
|
|
28
|
+
|
|
29
|
+
// Initialize collapsed from Loro metadata
|
|
30
|
+
this.collapsed = !!data.get('collapsed');
|
|
31
|
+
// Subscribe to data changes to keep collapsed in sync (a change of the document wins over the read-only view state)
|
|
32
|
+
this.node.data.subscribe(() => {
|
|
33
|
+
this.#viewState = null;
|
|
34
|
+
this.collapsed = !!this.node.data.get('collapsed');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
this.serializers.set('markdown', () => toggleMarkdown(this.#stored, this.behavior.toMarkdown(), /** @type {any} */ (this.children.map(c => c.serialize('markdown')))));
|
|
38
|
+
this.serializers.set('json', () => ({
|
|
39
|
+
id: this.id,
|
|
40
|
+
type: 'toggle',
|
|
41
|
+
props: { collapsed: this.#stored },
|
|
42
|
+
content: this.behavior.toJSON(),
|
|
43
|
+
children: this.children.map(c => c.serialize('json')).filter(Boolean)
|
|
44
|
+
}));
|
|
45
|
+
this.serializers.set('application/x-nabu+json', () => ({
|
|
46
|
+
type: 'toggle',
|
|
47
|
+
props: { collapsed: this.#stored },
|
|
48
|
+
delta: this.container.sliceDelta(0, this.container.length),
|
|
49
|
+
children: this.children.map(c => c.serialize('application/x-nabu+json')).filter(Boolean)
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ── Capability flags ────────────────────────────────────────────────────
|
|
54
|
+
// A typed "# " must not transform the toggle header away
|
|
55
|
+
transformsEnabled = false;
|
|
56
|
+
// marksEnabled stays true (default) — bold/italic work inside the header
|
|
57
|
+
// splitsOnEnter is not set (defaults true) but Enter is overridden in beforeinput
|
|
58
|
+
|
|
59
|
+
/** The collapsed state of the DOCUMENT (the `collapsed` prop of the node). */
|
|
60
|
+
#stored = $state(false);
|
|
61
|
+
|
|
62
|
+
/** Read-only editors only: what the reader chose to see, never written to the document (`null`: nothing chosen). */
|
|
63
|
+
/** @type {boolean | null} */
|
|
64
|
+
#viewState = $state(null);
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Whether the body is hidden. In a read-only editor a reader may open or close a toggle to read it: that is VIEW STATE
|
|
68
|
+
* (kept in memory, not in the document: no Loro write, no commit, no undo step, nothing synced or persisted, serializers
|
|
69
|
+
* keep reporting the document's own state), and it is ignored again as soon as the editor is not read-only.
|
|
70
|
+
* @type {boolean}
|
|
71
|
+
*/
|
|
72
|
+
get collapsed() { return this.nabu.readonly && this.#viewState !== null ? this.#viewState : this.#stored; }
|
|
73
|
+
set collapsed(v) { this.#stored = !!v; }
|
|
74
|
+
|
|
75
|
+
/** A collapsed toggle does not render its body: navigation must never put the caret in it. */
|
|
76
|
+
get hidesChildren() { return this.collapsed; }
|
|
77
|
+
|
|
78
|
+
/** A collapsed toggle stays collapsed on the clipboard. */
|
|
79
|
+
get clipboardProps() { return { collapsed: this.collapsed }; }
|
|
80
|
+
|
|
81
|
+
static placeholder = 'Toggle…';
|
|
82
|
+
|
|
83
|
+
component = $derived(this.nabu.components.get("toggle") || ToggleComponent);
|
|
84
|
+
|
|
85
|
+
get text() { return this.behavior.text; }
|
|
86
|
+
get delta() { return this.behavior.delta; }
|
|
87
|
+
|
|
88
|
+
selection = $derived(this.behavior.selection);
|
|
89
|
+
|
|
90
|
+
// When collapsed, exclude children from positions so cursor cannot enter hidden body
|
|
91
|
+
// @ts-ignore — `super.positions`: Svelte compiles the parent's `$derived` field to a prototype accessor, which TS sees as a field
|
|
92
|
+
positions = $derived(this.behavior.positions + (this.collapsed ? 0 : super.positions));
|
|
93
|
+
|
|
94
|
+
// Count body words regardless of collapse state
|
|
95
|
+
wordCount = $derived((this.behavior?.wordCount ?? 0) + this.children.reduce((s, c) => s + (c.wordCount ?? 0), 0));
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Mixed block: own text comes first, then children.
|
|
99
|
+
* When collapsed, children are excluded from navigation.
|
|
100
|
+
* @param {number} localOffset
|
|
101
|
+
* @returns {{ block: import('../block.svelte.js').Block, offset: number } | null}
|
|
102
|
+
*/
|
|
103
|
+
resolveOffset(localOffset) {
|
|
104
|
+
const textPositions = this.behavior.positions; // text.length + 1
|
|
105
|
+
if (this.collapsed || localOffset < textPositions || !this.children.length) {
|
|
106
|
+
return { block: this, offset: Math.min(localOffset, this.behavior.text.length) };
|
|
107
|
+
}
|
|
108
|
+
let remaining = localOffset - textPositions;
|
|
109
|
+
for (const child of this.children) {
|
|
110
|
+
const cp = child.positions ?? 0;
|
|
111
|
+
if (remaining < cp) return child.resolveOffset(remaining);
|
|
112
|
+
remaining -= cp;
|
|
113
|
+
}
|
|
114
|
+
return { block: this, offset: this.behavior.text.length };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// ── Collapse control ────────────────────────────────────────────────────
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Set the collapsed state and persist to Loro (in a read-only editor: only what the reader sees, see {@link ToggleBlock#collapsed}).
|
|
121
|
+
* @param {boolean} v
|
|
122
|
+
*/
|
|
123
|
+
setCollapsed(v) {
|
|
124
|
+
if (this.nabu.readonly) { this.#viewState = !!v; return; } // view state only: the document is not touched
|
|
125
|
+
this.#viewState = null;
|
|
126
|
+
this.collapsed = !!v;
|
|
127
|
+
/** @type {any} */ (this.node.data).set('collapsed', !!v);
|
|
128
|
+
this.commit();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Toggle the collapsed state.
|
|
133
|
+
*/
|
|
134
|
+
toggleCollapsed() {
|
|
135
|
+
this.setCollapsed(!this.collapsed);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// ── Proxies to TextBehavior ─────────────────────────────────────────────
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* @param {number} targetOffset
|
|
142
|
+
* @returns {{node: Node, offset: number} | null}
|
|
143
|
+
*/
|
|
144
|
+
getDOMPoint(targetOffset) { return this.behavior.getDOMPoint(targetOffset); }
|
|
145
|
+
|
|
146
|
+
/** @param {import('../block.svelte.js').Block} block */
|
|
147
|
+
absorbs(block) { return this.behavior.absorbs(block); }
|
|
148
|
+
|
|
149
|
+
/** @param {number} index @param {string} text */
|
|
150
|
+
insert(index, text) { return this.behavior.insert(index, text); }
|
|
151
|
+
|
|
152
|
+
/** @param {Parameters<import('../block.svelte.js').Block["delete"]>[0]} [deletion] */
|
|
153
|
+
delete(deletion) { return this.behavior.delete(deletion); }
|
|
154
|
+
|
|
155
|
+
/** @param {import('loro-crdt').Delta<string>[]} data */
|
|
156
|
+
applyDelta(data = []) { return this.behavior.applyDelta(data); }
|
|
157
|
+
|
|
158
|
+
// ── Sub-thread rules (P-40) ─────────────────────────────────────────────
|
|
159
|
+
// Header = own text, body = children. The tree operations live in ../subthread.js, shared with any other
|
|
160
|
+
// container block (the container Callout will reuse them as is).
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Enter in a body child (called through `ascend("onSplit")`). Only the Enter that EXITS the sub-thread is
|
|
164
|
+
* taken here: on an EMPTY paragraph that is the LAST child, the paragraph moves right after the toggle and
|
|
165
|
+
* takes the caret. Anything else is declined (falsy) and goes through the regular split of the child's type.
|
|
166
|
+
* @param {Event | null} event
|
|
167
|
+
* @param {{ from: import('../block.svelte.js').Block }} data
|
|
168
|
+
*/
|
|
169
|
+
onSplit(event, data) {
|
|
170
|
+
const source = data.from;
|
|
171
|
+
if (!isEmptyLastParagraph(/** @type {any} */ (this), source)) return false;
|
|
172
|
+
const id = source.node.id.toString();
|
|
173
|
+
exitSubThread(source.node, this.node);
|
|
174
|
+
this.nabu.commit().then(() => {
|
|
175
|
+
const f = this.nabu.blocks.get(id);
|
|
176
|
+
if (f) this.nabu.selection.setCursor(f, 0);
|
|
177
|
+
});
|
|
178
|
+
return { block: source };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Children adopted by a merge (`Block#consume`) never vanish into a HIDDEN body: when collapsed they stay
|
|
183
|
+
* visible, in order, right after the toggle.
|
|
184
|
+
* @param {import('../block.svelte.js').Block[]} children
|
|
185
|
+
* @param {number | null} [index]
|
|
186
|
+
*/
|
|
187
|
+
adoptChildren(children, index = null) {
|
|
188
|
+
if (!this.collapsed) return super.adoptChildren(children, index);
|
|
189
|
+
[...children].reverse().forEach(child => { if (child.node) child.node.moveAfter(this.node); });
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** The body is free-form: an empty paragraph of the body takes the type of a whole block pasted into it. */
|
|
193
|
+
get adoptsPastedTypes() { return true; }
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Paste with the caret in the HEADER (text only). One pasted text block is the engine's inline merge
|
|
197
|
+
* (declined). Several blocks: the first completes the header at the caret, the others go to the TOP of the
|
|
198
|
+
* body (like Enter, which creates the first body child), the header text after the caret follows the last
|
|
199
|
+
* one (merged into it when it is text, else in a new paragraph). A COLLAPSED toggle is EXPANDED first: the
|
|
200
|
+
* pasted content would otherwise land hidden. (A paste with the caret in a body child never reaches this
|
|
201
|
+
* hook: the child's own rules apply, inside the body.)
|
|
202
|
+
* @param {import('../../utils/extensions.js').PasteBlock[]} blocks
|
|
203
|
+
* @param {{ offset: number }} ctx
|
|
204
|
+
*/
|
|
205
|
+
acceptPaste(blocks, ctx) {
|
|
206
|
+
if (!blocks?.length || (blocks.length === 1 && !blocks[0].children?.length)) return null;
|
|
207
|
+
const landing = pasteIntoHeader(this.nabu, /** @type {any} */ (this), ctx.offset, blocks);
|
|
208
|
+
if (landing && this.collapsed) /** @type {any} */ (this.node.data).set('collapsed', false);
|
|
209
|
+
return landing;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// ── Custom beforeinput (handles Enter and Backspace in the header) ──────
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* @param {InputEvent} event
|
|
216
|
+
* @returns {boolean}
|
|
217
|
+
*/
|
|
218
|
+
beforeinput(event) {
|
|
219
|
+
const sel = this.behavior.selection;
|
|
220
|
+
|
|
221
|
+
// Enter in the header → split at the caret.
|
|
222
|
+
// - COLLAPSED: the tail goes into a new paragraph AFTER the toggle (caret in it); the toggle stays collapsed
|
|
223
|
+
// and its hidden body is not touched.
|
|
224
|
+
// - EXPANDED: the tail goes into a new paragraph as the FIRST body child (caret in it).
|
|
225
|
+
if (event.inputType === 'insertParagraph' && sel) {
|
|
226
|
+
const from = sel.from;
|
|
227
|
+
const to = sel.isCollapsed ? this.behavior.text.length : sel.to;
|
|
228
|
+
const delta = this.container.sliceDelta(from, to);
|
|
229
|
+
if (this.behavior.text.length > from) {
|
|
230
|
+
this.behavior.delete({ index: from, length: this.behavior.text.length - from });
|
|
231
|
+
}
|
|
232
|
+
const p = this.collapsed
|
|
233
|
+
? insertParagraphAfter(this.nabu, this.node, delta)
|
|
234
|
+
: this.nabu.insert('paragraph', { delta }, this.node.id.toString(), 0);
|
|
235
|
+
this.nabu.commit().then(() => {
|
|
236
|
+
const f = this.nabu.blocks.get(p.id);
|
|
237
|
+
if (f) this.nabu.selection.setCursor(f, 0);
|
|
238
|
+
});
|
|
239
|
+
return true;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Backspace at offset 0 of the header (empty or not, with or without body) → the toggle becomes a
|
|
243
|
+
// paragraph with the header text; its body children follow it. One transaction = one undo step.
|
|
244
|
+
if (event.inputType === 'deleteContentBackward' && sel?.isCollapsed && sel.from === 0) {
|
|
245
|
+
const id = this.node.id.toString();
|
|
246
|
+
this.nabu.transact(() => dissolveToParagraph(this.node, ['collapsed'])).then(() => {
|
|
247
|
+
const f = this.nabu.blocks.get(id);
|
|
248
|
+
if (f) f.focus({ start: 0, end: 0 });
|
|
249
|
+
});
|
|
250
|
+
return true;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return this.behavior.handleBeforeInput(event);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// ── Statics ─────────────────────────────────────────────────────────────
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* `<details>` / `<details open>` + `<summary>` + the body as markdown + `</details>` (see markdown.js).
|
|
260
|
+
* @param {import('../../utils/extensions.js').PasteBlock} pb
|
|
261
|
+
* @param {{ recurse: (child: import('../../utils/extensions.js').PasteBlock, ctx?: object) => string, recurseAll?: (children: import('../../utils/extensions.js').PasteBlock[], ctx?: object) => string[] }} helpers
|
|
262
|
+
*/
|
|
263
|
+
static toMarkdown(pb, { recurse, recurseAll }) {
|
|
264
|
+
const children = pb.children || [];
|
|
265
|
+
return toggleMarkdown(!!pb.props?.collapsed, deltaToMarkdown(pb.delta || []), recurseAll ? recurseAll(children) : children.map(c => recurse(c)));
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Markdown (paste AND `importMarkdown`): the exact `<details>` construct Nabu emits, nothing else (see markdown.js).
|
|
270
|
+
* An opening line without a matching `</details>`, with attributes or without a `<summary>` is not a toggle: it stays text.
|
|
271
|
+
*/
|
|
272
|
+
static markdownRules = [
|
|
273
|
+
{
|
|
274
|
+
priority: 20,
|
|
275
|
+
interruptsParagraph: true,
|
|
276
|
+
detect: (/** @type {string} */ line, /** @type {string[]} */ lines, /** @type {number} */ i) => line.startsWith('<details') && !!parseDetails(lines, i),
|
|
277
|
+
consume: (/** @type {string[]} */ lines, /** @type {number} */ i) => (parseDetails(lines, i)?.end ?? i) - i + 1,
|
|
278
|
+
}
|
|
279
|
+
];
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* The toggle (header = the summary, parsed as inline markdown; body = the lines between the summary and `</details>`,
|
|
283
|
+
* parsed as a document of their own, nested toggles included).
|
|
284
|
+
* @param {string[]} lines
|
|
285
|
+
* @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[], parseBlocks?: (lines: string[]) => import('../../utils/extensions.js').PasteBlock[] }} helpers
|
|
286
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
287
|
+
*/
|
|
288
|
+
static fromMarkdown(lines, { parseInline, parseBlocks }) {
|
|
289
|
+
const details = parseDetails(lines, 0);
|
|
290
|
+
if (!details || !parseBlocks) return null;
|
|
291
|
+
const children = parseBlocks(lines.slice(details.bodyStart, details.end));
|
|
292
|
+
return { type: 'toggle', props: { collapsed: !details.open }, delta: parseInline(details.summary), partial: false, ...(children.length ? { children } : {}) };
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* @param {import('../../utils/extensions.js').PasteBlock} pb
|
|
297
|
+
* @param {{ recurse: (child: import('../../utils/extensions.js').PasteBlock) => string }} helpers
|
|
298
|
+
*/
|
|
299
|
+
static toHtml(pb, { recurse }) {
|
|
300
|
+
const open = pb.props?.collapsed ? '' : ' open';
|
|
301
|
+
const head = deltaToHtml(pb.delta || []);
|
|
302
|
+
const body = (pb.children || []).map(c => recurse(c)).join('');
|
|
303
|
+
return '<details' + open + '><summary>' + head + '</summary>' + body + '</details>';
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
static htmlRules = [{ selector: 'details' }];
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* @param {Element} el
|
|
310
|
+
* @param {{ parseInline: (el: Element) => import('loro-crdt').Delta<string>[], parseChildren?: (el: Element) => import('../../utils/extensions.js').PasteBlock[] }} helpers
|
|
311
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
312
|
+
*/
|
|
313
|
+
static fromHTML(el, { parseInline, parseChildren }) {
|
|
314
|
+
// Use 'summary' without :scope because happy-dom doesn't support :scope pseudo-class
|
|
315
|
+
const summary = /** @type {Element | null} */ ([...el.children].find(c => c.tagName.toLowerCase() === 'summary') || null);
|
|
316
|
+
const delta = summary ? parseInline(summary) : [];
|
|
317
|
+
const collapsed = !el.hasAttribute('open');
|
|
318
|
+
// Parse non-summary child elements as body blocks
|
|
319
|
+
const bodyEls = [...el.children].filter(c => c.tagName.toLowerCase() !== 'summary');
|
|
320
|
+
/** @type {import('../../utils/extensions.js').PasteBlock[]} */
|
|
321
|
+
let children = [];
|
|
322
|
+
if (parseChildren && bodyEls.length) {
|
|
323
|
+
// Create a temporary wrapper with only the non-summary children
|
|
324
|
+
const ownerDoc = el.ownerDocument;
|
|
325
|
+
if (ownerDoc) {
|
|
326
|
+
const wrapper = ownerDoc.createElement('div');
|
|
327
|
+
for (const bodyEl of bodyEls) {
|
|
328
|
+
wrapper.appendChild(bodyEl.cloneNode(true));
|
|
329
|
+
}
|
|
330
|
+
children = parseChildren(wrapper);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return { type: 'toggle', props: { collapsed }, delta, partial: false, ...(children.length ? { children } : {}) };
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* @param {Nabu} nabu
|
|
338
|
+
* @param {string} type
|
|
339
|
+
* @param {Record<string, any>} [props={}]
|
|
340
|
+
* @param {string|null} [parentId=null]
|
|
341
|
+
* @param {number|null} [index=null]
|
|
342
|
+
*/
|
|
343
|
+
static create(nabu, type, props = {}, parentId = null, index = null) {
|
|
344
|
+
const node = nabu.tree.createNode(/** @type {import('loro-crdt').TreeID | undefined} */ (parentId || undefined), index ?? undefined);
|
|
345
|
+
/** @type {any} */ (node.data).set('type', 'toggle');
|
|
346
|
+
/** @type {any} */ (node.data).set('collapsed', props.collapsed ?? false);
|
|
347
|
+
const container = /** @type {any} */ (node.data).setContainer('text', new LoroText());
|
|
348
|
+
if (props.text) container.insert(0, props.text);
|
|
349
|
+
if (props.delta) container.applyDelta([...props.delta]);
|
|
350
|
+
return new ToggleBlock(nabu, /** @type {ToggleNode} */ (/** @type {unknown} */ (node)));
|
|
351
|
+
}
|
|
352
|
+
}
|