@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,89 @@
|
|
|
1
|
+
import { Block } from "../block.svelte.js";
|
|
2
|
+
import DividerComponent from "./Divider.svelte";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @import { Nabu, NabuNode } from "../nabu.svelte.js";
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {NabuNode<{type: "divider"}>} DividerNode
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Horizontal rule. First non-textual block: no TextBehavior, renders an
|
|
14
|
+
* inert <hr> and exposes a single cursor position so document-offset
|
|
15
|
+
* resolution can step over it.
|
|
16
|
+
*/
|
|
17
|
+
// @ts-ignore — static-side variance on create() (same pattern as Paragraph/Heading)
|
|
18
|
+
export class DividerBlock extends Block {
|
|
19
|
+
/** @param {Nabu} nabu @param {DividerNode} node */
|
|
20
|
+
constructor(nabu, node) {
|
|
21
|
+
super(nabu, node);
|
|
22
|
+
|
|
23
|
+
this.serializers.set('markdown', () => '---');
|
|
24
|
+
this.serializers.set('json', () => ({ id: this.id, type: 'divider' }));
|
|
25
|
+
this.serializers.set('application/x-nabu+json', () => ({ type: 'divider', partial: false }));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
component = $derived(this.nabu.components.get("divider") || DividerComponent);
|
|
29
|
+
|
|
30
|
+
// Un seul point de curseur — avant le bloc
|
|
31
|
+
positions = $derived(1);
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* No editable text — callers (focus, setCursor) handle null gracefully.
|
|
35
|
+
* @param {number} offset
|
|
36
|
+
* @returns {{node: Node, offset: number} | null}
|
|
37
|
+
*/
|
|
38
|
+
getDOMPoint(offset) { return null; }
|
|
39
|
+
|
|
40
|
+
/** @param {InputEvent} event */
|
|
41
|
+
beforeinput(event) { return false; }
|
|
42
|
+
|
|
43
|
+
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
44
|
+
static toMarkdown(pb) { return '---'; }
|
|
45
|
+
|
|
46
|
+
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
47
|
+
static toHtml(pb) { return '<hr>'; }
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Thematic break (CommonMark): 3+ `-`, `*` or `_` (the same one), spaces allowed between (`* * *`), up to 3 spaces of
|
|
51
|
+
* indent. A `---` right under a line of text is a setext heading underline, not a divider (see the heading block).
|
|
52
|
+
*/
|
|
53
|
+
static markdownRules = [
|
|
54
|
+
{
|
|
55
|
+
priority: 20,
|
|
56
|
+
interruptsParagraph: true,
|
|
57
|
+
detect: /^ {0,3}([-*_])(?:[ \t]*\1){2,}[ \t]*$/,
|
|
58
|
+
consume: () => 1,
|
|
59
|
+
}
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @param {string[]} lines
|
|
64
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
65
|
+
*/
|
|
66
|
+
static fromMarkdown(lines) {
|
|
67
|
+
if (!/^ {0,3}([-*_])(?:[ \t]*\1){2,}[ \t]*$/.test(lines[0])) return null;
|
|
68
|
+
return { type: 'divider', partial: false };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
static htmlRules = [
|
|
72
|
+
{ selector: 'hr' },
|
|
73
|
+
];
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @returns {import('../../utils/extensions.js').PasteBlock}
|
|
77
|
+
*/
|
|
78
|
+
static fromHTML() {
|
|
79
|
+
return { type: 'divider', partial: false };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** @param {Nabu} nabu @param {string} type @param {Record<string, any>} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
83
|
+
static create(nabu, type, props = {}, parentId = null, index = null) {
|
|
84
|
+
const node = nabu.tree.createNode(/** @type {import('loro-crdt').TreeID | undefined} */ (parentId || undefined), index ?? undefined);
|
|
85
|
+
/** @type {any} */ (node.data).set("type", "divider");
|
|
86
|
+
const block = new DividerBlock(nabu, /** @type {DividerNode} */ (/** @type {unknown} */ (node)));
|
|
87
|
+
return block;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
/** @type {{ block: import('./file.svelte.js').FileBlock }} */
|
|
3
|
+
let { block } = $props();
|
|
4
|
+
$effect(() => block.holdSource());
|
|
5
|
+
</script>
|
|
6
|
+
|
|
7
|
+
<div
|
|
8
|
+
bind:this={block.element}
|
|
9
|
+
data-block-id={block.id}
|
|
10
|
+
data-block-type="file"
|
|
11
|
+
data-nabu-inert
|
|
12
|
+
contenteditable="false"
|
|
13
|
+
class="nabu-file"
|
|
14
|
+
class:selected={block.selected}
|
|
15
|
+
class:pending={block.status === 'pending'}
|
|
16
|
+
class:error={block.status === 'error'}
|
|
17
|
+
class:missing={block.isMissing}
|
|
18
|
+
>
|
|
19
|
+
{#if block.status === 'error'}
|
|
20
|
+
<div class="nabu-file-chip nabu-file-chip--error">⚠️ Upload failed</div>
|
|
21
|
+
{:else if block.isMissing}
|
|
22
|
+
<div class="nabu-file-chip nabu-file-chip--error nabu-file-chip--missing" data-nabu-missing>
|
|
23
|
+
<span>📎</span><span>{block.name || 'file'}</span><span class="nabu-file-chip-status">Missing file</span>
|
|
24
|
+
</div>
|
|
25
|
+
{:else if block.status === 'pending'}
|
|
26
|
+
<div class="nabu-file-chip nabu-file-chip--pending">
|
|
27
|
+
<span>📎</span><span>{block.name || 'file'}</span><span class="nabu-file-chip-status">Uploading…</span>
|
|
28
|
+
</div>
|
|
29
|
+
{:else if block.displaySrc}
|
|
30
|
+
<a href={block.displaySrc} download={block.name} class="nabu-file-chip">
|
|
31
|
+
<span>📎</span><span>{block.name || 'file'}</span>
|
|
32
|
+
</a>
|
|
33
|
+
{:else}
|
|
34
|
+
<div class="nabu-file-chip">
|
|
35
|
+
<span>📎</span><span>{block.name || 'file'}</span>
|
|
36
|
+
</div>
|
|
37
|
+
{/if}
|
|
38
|
+
</div>
|
|
39
|
+
|
|
40
|
+
<style>
|
|
41
|
+
.nabu-file { margin: 0.25em 0; user-select: none; display: block; }
|
|
42
|
+
.nabu-file-chip {
|
|
43
|
+
display: inline-flex; align-items: center; gap: 0.4em;
|
|
44
|
+
padding: 0.3em 0.75em; border-radius: 999px;
|
|
45
|
+
border: 1px solid var(--nb-border, rgba(255,255,255,0.2));
|
|
46
|
+
background: var(--nb-bg-surface, rgba(255,255,255,0.06));
|
|
47
|
+
color: var(--nb-text-primary, inherit);
|
|
48
|
+
font-size: 0.875em; text-decoration: none; cursor: pointer;
|
|
49
|
+
transition: background 0.15s;
|
|
50
|
+
}
|
|
51
|
+
.nabu-file-chip:hover { background: var(--nb-bg-hover, rgba(255,255,255,0.12)); }
|
|
52
|
+
.nabu-file.selected .nabu-file-chip { outline: 2px solid var(--nb-accent, #4d9fff); }
|
|
53
|
+
.nabu-file-chip--pending { opacity: 0.6; cursor: default; }
|
|
54
|
+
.nabu-file-chip--error { border-color: var(--nb-error, #f44); color: var(--nb-error, #f44); cursor: default; }
|
|
55
|
+
.nabu-file-chip-status { font-size: 0.8em; opacity: 0.7; }
|
|
56
|
+
</style>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export default File;
|
|
2
|
+
type File = {
|
|
3
|
+
$on?(type: string, callback: (e: any) => void): () => void;
|
|
4
|
+
$set?(props: Partial<$$ComponentProps>): void;
|
|
5
|
+
};
|
|
6
|
+
declare const File: import("svelte").Component<{
|
|
7
|
+
block: import("./file.svelte.js").FileBlock;
|
|
8
|
+
}, {}, "">;
|
|
9
|
+
type $$ComponentProps = {
|
|
10
|
+
block: import("./file.svelte.js").FileBlock;
|
|
11
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { extension } from "../../utils/extensions";
|
|
2
|
+
import { FileBlock } from "./file.svelte.js";
|
|
3
|
+
import FileComponent from "./File.svelte";
|
|
4
|
+
import { pickMedia, insertMediaUrl, mediaFileHandler } from "../media/media.helpers.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The file block: an attachment (name, size, download link) stored through the `files` option; the `file:*` actions, file paste for any file type and the "File" slash entry.
|
|
8
|
+
*/
|
|
9
|
+
export const FileExtension = extension("file", {
|
|
10
|
+
// @ts-ignore — static-side variance on create()
|
|
11
|
+
block: FileBlock,
|
|
12
|
+
component: FileComponent,
|
|
13
|
+
// A file pasted from the clipboard becomes this block.
|
|
14
|
+
pasteFileHandlers: [mediaFileHandler('file', (file) => file.size > 0 || !!file.type)],
|
|
15
|
+
slashCommands: [
|
|
16
|
+
{ title: 'File', icon: '⎘', aliases: ['file', 'attachment', 'upload', 'document'], group: 'media', order: 20, run: (e) => e.exec('file:pick') },
|
|
17
|
+
],
|
|
18
|
+
actions: {
|
|
19
|
+
'file:pick': (nabu) => pickMedia(nabu, 'file', '*/*'),
|
|
20
|
+
/**
|
|
21
|
+
* Insert from a URL/ref. data: { ref, name? }
|
|
22
|
+
* @param {import('../nabu.svelte.js').Nabu} nabu
|
|
23
|
+
* @param {{ ref: string, name?: string }} data
|
|
24
|
+
*/
|
|
25
|
+
'file:insert': (nabu, data) => insertMediaUrl(nabu, 'file', data),
|
|
26
|
+
},
|
|
27
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { Nabu, NabuNode } from "../nabu.svelte.js";
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Void file (attachment) block. Extends MediaBlock for shared void/metadata behavior.
|
|
6
|
+
* Renders as a downloadable chip — not an inline media preview.
|
|
7
|
+
*/
|
|
8
|
+
export class FileBlock extends MediaBlock {
|
|
9
|
+
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
10
|
+
static toMarkdown(pb: import("../../utils/extensions.js").PasteBlock): any;
|
|
11
|
+
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
12
|
+
static toHtml(pb: import("../../utils/extensions.js").PasteBlock): string;
|
|
13
|
+
static htmlRules: {
|
|
14
|
+
selector: string;
|
|
15
|
+
priority: number;
|
|
16
|
+
}[];
|
|
17
|
+
/**
|
|
18
|
+
* @param {Element} el
|
|
19
|
+
* @returns {import('../../utils/extensions.js').PasteBlock}
|
|
20
|
+
*/
|
|
21
|
+
static fromHTML(el: Element): import("../../utils/extensions.js").PasteBlock;
|
|
22
|
+
/** @param {Nabu} nabu @param {string} type @param {Record<string, any>} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
23
|
+
static create(nabu: Nabu, type: string, props?: Record<string, any>, parentId?: string | null, index?: number | null): FileBlock;
|
|
24
|
+
component: import("svelte").Component<any, any, string> | import("svelte").Component<{
|
|
25
|
+
block: import("./file.svelte.js").FileBlock;
|
|
26
|
+
}, {}, "">;
|
|
27
|
+
}
|
|
28
|
+
import { MediaBlock } from "../media/media-block.svelte.js";
|
|
29
|
+
import type { Nabu } from "../nabu.svelte.js";
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// @ts-ignore
|
|
2
|
+
import { MediaBlock } from "../media/media-block.svelte.js";
|
|
3
|
+
import { markdownMediaLink } from "../media/markdown.js";
|
|
4
|
+
import FileComponent from "./File.svelte";
|
|
5
|
+
import { escapeHtml, escapeAttr, sanitizeUrl } from "../../utils/html.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @import { Nabu, NabuNode } from "../nabu.svelte.js";
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Void file (attachment) block. Extends MediaBlock for shared void/metadata behavior.
|
|
13
|
+
* Renders as a downloadable chip — not an inline media preview.
|
|
14
|
+
*/
|
|
15
|
+
// @ts-ignore — static-side variance on create()
|
|
16
|
+
export class FileBlock extends MediaBlock {
|
|
17
|
+
/** @param {Nabu} nabu @param {NabuNode} node */
|
|
18
|
+
constructor(nabu, node) {
|
|
19
|
+
super(nabu, node);
|
|
20
|
+
|
|
21
|
+
this.serializers.set('markdown', () => FileBlock.toMarkdown({ type: 'file', partial: false, props: this.exportProps }));
|
|
22
|
+
this.serializers.set('json', () => ({
|
|
23
|
+
id: this.id,
|
|
24
|
+
type: 'file',
|
|
25
|
+
props: this.exportProps,
|
|
26
|
+
}));
|
|
27
|
+
this.serializers.set('application/x-nabu+json', () => ({
|
|
28
|
+
type: 'file',
|
|
29
|
+
props: { ref: this.ref, name: this.name, ...this.clipboardState },
|
|
30
|
+
partial: false,
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
component = $derived(this.nabu.components.get("file") || FileComponent);
|
|
35
|
+
|
|
36
|
+
// ── Statics ──────────────────────────────────────────────────────────────
|
|
37
|
+
|
|
38
|
+
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
39
|
+
static toMarkdown(pb) {
|
|
40
|
+
if (!pb.props?.ref) return pb.props?.name || 'file'; // no source: the file name only
|
|
41
|
+
return markdownMediaLink(pb.props?.name || 'file', pb.props.ref);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
45
|
+
static toHtml(pb) {
|
|
46
|
+
const ref = pb.props?.ref || '';
|
|
47
|
+
const href = sanitizeUrl(ref, { kind: 'media' }); // null for an empty ref too
|
|
48
|
+
const name = pb.props?.name || '';
|
|
49
|
+
if (href === null) return '<p>' + escapeHtml(name || 'file') + '</p>';
|
|
50
|
+
return '<a href="' + escapeAttr(href) + '" download="' + escapeAttr(name) + '">' + escapeHtml(name || 'file') + '</a>';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
static htmlRules = [
|
|
54
|
+
{ selector: 'a[download]', priority: 10 },
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @param {Element} el
|
|
59
|
+
* @returns {import('../../utils/extensions.js').PasteBlock}
|
|
60
|
+
*/
|
|
61
|
+
static fromHTML(el) {
|
|
62
|
+
const href = el.getAttribute('href') || '';
|
|
63
|
+
const name = el.getAttribute('download') || el.textContent || '';
|
|
64
|
+
// An unsafe href never becomes a file block: keep the label as plain text.
|
|
65
|
+
if (href && sanitizeUrl(href, { kind: 'media' }) === null) {
|
|
66
|
+
return { type: 'paragraph', delta: name ? [{ insert: name }] : [], partial: false };
|
|
67
|
+
}
|
|
68
|
+
return { type: 'file', props: { ref: href, name }, partial: false };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** @param {Nabu} nabu @param {string} type @param {Record<string, any>} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
72
|
+
static create(nabu, type, props = {}, parentId = null, index = null) {
|
|
73
|
+
const node = MediaBlock.createNode(nabu, 'file', props, parentId, index);
|
|
74
|
+
const block = new FileBlock(nabu, /** @type {any} */ (node));
|
|
75
|
+
return block;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
bind:this={block.element}
|
|
10
10
|
data-block-id={block.id}
|
|
11
11
|
data-block-type="heading"
|
|
12
|
+
data-placeholder={block.placeholder}
|
|
12
13
|
class="nabu-heading"
|
|
13
14
|
class:debug={block.nabu.debugging}
|
|
14
15
|
class:selected={block.selected}
|
|
@@ -18,7 +19,7 @@
|
|
|
18
19
|
|
|
19
20
|
<style>
|
|
20
21
|
.nabu-heading {
|
|
21
|
-
margin-top:
|
|
22
|
+
margin-top: 0rem;
|
|
22
23
|
margin-bottom: 0.5rem;
|
|
23
24
|
white-space: pre-wrap;
|
|
24
25
|
outline: none;
|
|
@@ -1,28 +1,39 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* @typedef {NabuNode<{type: "heading", text: LoroText, level: number}>} HeadingNode
|
|
7
|
-
*/
|
|
8
|
-
export class Heading extends Block {
|
|
1
|
+
export class Heading extends TextBlock {
|
|
2
|
+
/** @param {Heading} block */
|
|
3
|
+
static placeholder: (block: Heading) => string;
|
|
9
4
|
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
10
5
|
static toMarkdown(pb: import("../../utils/extensions.js").PasteBlock): string;
|
|
11
6
|
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
12
7
|
static toHtml(pb: import("../../utils/extensions.js").PasteBlock): string;
|
|
13
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Two rules: the ATX heading (`## Title`, up to 3 spaces of indent) and the setext heading (`Title` then `===` for a
|
|
10
|
+
* level 1, `---` for a level 2). The setext rule sits just ABOVE the paragraph catch-all and BELOW every block rule: a
|
|
11
|
+
* line that starts a list, a quote, a fence, a divider… never becomes a heading text, and a `---` after a blank line
|
|
12
|
+
* (nothing to underline) stays a divider. In a paste a heading text is ONE line (one line = one block); on import
|
|
13
|
+
* (`joinLines`) it is the whole paragraph above the underline (CommonMark).
|
|
14
|
+
*/
|
|
15
|
+
static markdownRules: ({
|
|
14
16
|
priority: number;
|
|
17
|
+
interruptsParagraph: boolean;
|
|
15
18
|
detect: RegExp;
|
|
16
19
|
consume: () => number;
|
|
17
|
-
|
|
20
|
+
setext?: undefined;
|
|
21
|
+
} | {
|
|
22
|
+
priority: number;
|
|
23
|
+
setext: boolean;
|
|
24
|
+
detect(line: string, lines: string[], i: number, helpers: any): boolean;
|
|
25
|
+
consume(lines: string[], i: number, helpers: any): number;
|
|
26
|
+
interruptsParagraph?: undefined;
|
|
27
|
+
})[];
|
|
18
28
|
/**
|
|
19
29
|
* @param {string[]} lines
|
|
20
30
|
* @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[] }} helpers
|
|
31
|
+
* @param {import('../../utils/extensions.js').MarkdownRule} [rule]
|
|
21
32
|
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
22
33
|
*/
|
|
23
34
|
static fromMarkdown(lines: string[], { parseInline }: {
|
|
24
35
|
parseInline: (text: string) => import("loro-crdt").Delta<string>[];
|
|
25
|
-
}): import("../../utils/extensions.js").PasteBlock | null;
|
|
36
|
+
}, rule?: import("../../utils/extensions.js").MarkdownRule): import("../../utils/extensions.js").PasteBlock | null;
|
|
26
37
|
static htmlRules: {
|
|
27
38
|
selector: string;
|
|
28
39
|
props: {
|
|
@@ -42,40 +53,22 @@ export class Heading extends Block {
|
|
|
42
53
|
level?: number;
|
|
43
54
|
};
|
|
44
55
|
}): import("../../utils/extensions.js").PasteBlock | null;
|
|
45
|
-
/** @param {Nabu} nabu @param {string} type @param {
|
|
46
|
-
static create(nabu: Nabu, type: string, props?:
|
|
56
|
+
/** @param {Nabu} nabu @param {string} type @param {Record<string, any>} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
57
|
+
static create(nabu: Nabu, type: string, props?: Record<string, any>, parentId?: string | null, index?: number | null): Heading;
|
|
47
58
|
/** @param {Nabu} nabu @param {HeadingNode} node */
|
|
48
59
|
constructor(nabu: Nabu, node: HeadingNode);
|
|
49
|
-
container: LoroText;
|
|
50
|
-
/** @type {TextBehavior} */
|
|
51
|
-
behavior: TextBehavior;
|
|
52
60
|
/** @type {number} */
|
|
53
61
|
level: number;
|
|
54
62
|
component: import("svelte").Component<any, any, string> | import("svelte").Component<{
|
|
55
|
-
block: import("./heading.svelte").Heading;
|
|
63
|
+
block: import("./heading.svelte.js").Heading;
|
|
56
64
|
}, {}, "">;
|
|
57
|
-
get text(): string;
|
|
58
|
-
get delta(): import("loro-crdt").Delta<string>[];
|
|
59
|
-
selection: {
|
|
60
|
-
from: number;
|
|
61
|
-
to: number;
|
|
62
|
-
isCollapsed: boolean;
|
|
63
|
-
direction: "forward" | "backward" | "none";
|
|
64
|
-
} | null;
|
|
65
|
-
/** @param {Block} block */
|
|
66
|
-
absorbs(block: Block): boolean;
|
|
67
|
-
/** @param {import('loro-crdt').Delta<string>[]} data */
|
|
68
|
-
applyDelta(data?: import("loro-crdt").Delta<string>[]): void;
|
|
69
|
-
/** @param {Parameters<Block["split"]>[0]} [options] @returns {ReturnType<Block["split"]>} */
|
|
70
|
-
split(options?: Parameters<Block["split"]>[0]): ReturnType<Block["split"]>;
|
|
71
65
|
}
|
|
72
66
|
export type HeadingNode = NabuNode<{
|
|
73
67
|
type: "heading";
|
|
74
68
|
text: LoroText;
|
|
75
69
|
level: number;
|
|
76
70
|
}>;
|
|
77
|
-
import {
|
|
78
|
-
import { LoroText } from "loro-crdt";
|
|
79
|
-
import { TextBehavior } from "../../behaviors/text";
|
|
71
|
+
import { TextBlock } from "../text-block.svelte.js";
|
|
80
72
|
import type { Nabu } from "../nabu.svelte.js";
|
|
73
|
+
import { LoroText } from "loro-crdt";
|
|
81
74
|
import type { NabuNode } from "../nabu.svelte.js";
|
|
@@ -1,31 +1,45 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TextBlock } from "../text-block.svelte.js";
|
|
2
|
+
import { deltaToHtml, deltaToMarkdown } from "../../behaviors/text";
|
|
2
3
|
import HeadingComponent from "./Heading.svelte";
|
|
3
|
-
import { Block } from "../block.svelte.js";
|
|
4
4
|
import { LoroText } from "loro-crdt";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* @import { Nabu, NabuNode } from "../nabu.svelte.js";
|
|
8
|
-
* @import { TextNode } from "../../behaviors/text";
|
|
9
8
|
*/
|
|
10
9
|
|
|
11
10
|
/**
|
|
12
11
|
* @typedef {NabuNode<{type: "heading", text: LoroText, level: number}>} HeadingNode
|
|
13
12
|
*/
|
|
14
13
|
|
|
15
|
-
|
|
14
|
+
const SETEXT_UNDERLINE = /^ {0,3}(?:=+|-+)[ \t]*$/;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Index of the setext underline (`===` / `---`) that turns the text starting at line `i` into a heading, or -1. A paste
|
|
18
|
+
* heading text is the single line `i`; on import (`joinLines`) it is every line up to the underline, as long as none of
|
|
19
|
+
* them starts a block of its own.
|
|
20
|
+
* @param {string[]} lines
|
|
21
|
+
* @param {number} i
|
|
22
|
+
* @param {{ joinLines?: boolean, interrupts?: (lines: string[], k: number) => boolean }} [helpers]
|
|
23
|
+
* @returns {number}
|
|
24
|
+
*/
|
|
25
|
+
function setextEnd(lines, i, helpers) {
|
|
26
|
+
const last = helpers?.joinLines ? lines.length - 1 : Math.min(i + 1, lines.length - 1);
|
|
27
|
+
for (let j = i + 1; j <= last; j++) {
|
|
28
|
+
if (SETEXT_UNDERLINE.test(lines[j])) return j;
|
|
29
|
+
if (!lines[j].trim() || helpers?.interrupts?.(lines, j)) return -1;
|
|
30
|
+
}
|
|
31
|
+
return -1;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export class Heading extends TextBlock {
|
|
16
35
|
/** @param {Nabu} nabu @param {HeadingNode} node */
|
|
17
36
|
constructor(nabu, node) {
|
|
18
37
|
super(nabu, node);
|
|
19
38
|
const data = node.data;
|
|
20
|
-
this.container = data.get("text") ?? data.setContainer("text", new LoroText());
|
|
21
|
-
|
|
22
|
-
/** @type {TextBehavior} */
|
|
23
|
-
this.behavior = new TextBehavior(this, this.container);
|
|
24
|
-
this.behaviors.set("text", this.behavior);
|
|
25
39
|
|
|
26
40
|
this.level = data.get("level") || 1;
|
|
27
41
|
|
|
28
|
-
this.serializers.set('markdown', () => `${'#'.repeat(this.level)} ${
|
|
42
|
+
this.serializers.set('markdown', () => { const md = this.behavior.toMarkdown(); return md ? `${'#'.repeat(this.level)} ${md}` : ''; }); // an empty heading has no markdown (`## ` would read back as text)
|
|
29
43
|
this.serializers.set('json', () => ({
|
|
30
44
|
id: this.id,
|
|
31
45
|
type: 'heading',
|
|
@@ -33,7 +47,7 @@ export class Heading extends Block {
|
|
|
33
47
|
content: this.behavior.toJSON()
|
|
34
48
|
}));
|
|
35
49
|
this.serializers.set('application/x-nabu+json', (ctx) => this.behavior.toClipboardBlock({ ...ctx, props: { level: this.level } }));
|
|
36
|
-
|
|
50
|
+
|
|
37
51
|
// Synchronisation du niveau
|
|
38
52
|
this.node.data.subscribe(() => {
|
|
39
53
|
this.level = this.node.data.get("level") || 1;
|
|
@@ -43,76 +57,74 @@ export class Heading extends Block {
|
|
|
43
57
|
/** @type {number} */
|
|
44
58
|
level = $state(1);
|
|
45
59
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
get text() {
|
|
49
|
-
return this.behavior.text;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
get delta() {
|
|
53
|
-
return this.behavior.delta;
|
|
54
|
-
}
|
|
60
|
+
/** @param {Heading} block */
|
|
61
|
+
static placeholder = (block) => `Heading ${block.level}`;
|
|
55
62
|
|
|
56
|
-
|
|
57
|
-
positions = $derived(this.behavior.positions);
|
|
58
|
-
|
|
59
|
-
/** @param {InputEvent} event */
|
|
60
|
-
beforeinput(event) {
|
|
61
|
-
return this.behavior.handleBeforeInput(event);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* @param {number} targetOffset
|
|
66
|
-
* @returns {{node: Node, offset: number} | null}
|
|
67
|
-
*/
|
|
68
|
-
getDOMPoint(targetOffset) { return this.behavior.getDOMPoint(targetOffset); }
|
|
69
|
-
|
|
70
|
-
/** @param {Block} block */
|
|
71
|
-
absorbs(block) { return this.behavior.absorbs(block); }
|
|
72
|
-
|
|
73
|
-
/** @param {number} index @param {string} text */
|
|
74
|
-
insert(index, text) { return this.behavior.insert(index, text); }
|
|
75
|
-
|
|
76
|
-
/** @param {Parameters<Block["delete"]>[0]} [deletion] */
|
|
77
|
-
delete(deletion) { return this.behavior.delete(deletion); }
|
|
78
|
-
|
|
79
|
-
/** @param {import('loro-crdt').Delta<string>[]} data */
|
|
80
|
-
applyDelta(data = []) { return this.behavior.applyDelta(data); }
|
|
81
|
-
|
|
82
|
-
/** @param {Parameters<Block["split"]>[0]} [options] @returns {ReturnType<Block["split"]>} */
|
|
83
|
-
split(options) { return this.behavior.split(options); }
|
|
63
|
+
component = $derived(this.nabu.components.get("heading") || HeadingComponent);
|
|
84
64
|
|
|
85
65
|
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
86
66
|
static toMarkdown(pb) {
|
|
87
|
-
const
|
|
88
|
-
|
|
67
|
+
const n = Math.trunc(Number(pb.props?.level));
|
|
68
|
+
const level = n >= 1 && n <= 6 ? n : 1; // never interpolate an arbitrary value into a tag name
|
|
69
|
+
const md = deltaToMarkdown(pb.delta || []);
|
|
70
|
+
return md ? `${'#'.repeat(level)} ${md}` : ''; // an empty heading has no markdown (`## ` would read back as text)
|
|
89
71
|
}
|
|
90
72
|
|
|
91
73
|
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
92
74
|
static toHtml(pb) {
|
|
93
|
-
const
|
|
75
|
+
const n = Math.trunc(Number(pb.props?.level));
|
|
76
|
+
const level = n >= 1 && n <= 6 ? n : 1; // never interpolate an arbitrary value into a tag name
|
|
94
77
|
return `<h${level}>${deltaToHtml(pb.delta || [])}</h${level}>`;
|
|
95
78
|
}
|
|
96
79
|
|
|
80
|
+
/**
|
|
81
|
+
* Two rules: the ATX heading (`## Title`, up to 3 spaces of indent) and the setext heading (`Title` then `===` for a
|
|
82
|
+
* level 1, `---` for a level 2). The setext rule sits just ABOVE the paragraph catch-all and BELOW every block rule: a
|
|
83
|
+
* line that starts a list, a quote, a fence, a divider… never becomes a heading text, and a `---` after a blank line
|
|
84
|
+
* (nothing to underline) stays a divider. In a paste a heading text is ONE line (one line = one block); on import
|
|
85
|
+
* (`joinLines`) it is the whole paragraph above the underline (CommonMark).
|
|
86
|
+
*/
|
|
97
87
|
static markdownRules = [
|
|
98
88
|
{
|
|
99
89
|
priority: 10,
|
|
100
|
-
|
|
90
|
+
interruptsParagraph: true,
|
|
91
|
+
detect: /^ {0,3}#{1,6}[ \t]/,
|
|
101
92
|
consume: () => 1,
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
priority: 5,
|
|
96
|
+
setext: true,
|
|
97
|
+
detect(/** @type {string} */ line, /** @type {string[]} */ lines, /** @type {number} */ i, /** @type {any} */ helpers) {
|
|
98
|
+
return setextEnd(lines, i, helpers) > i;
|
|
99
|
+
},
|
|
100
|
+
consume(/** @type {string[]} */ lines, /** @type {number} */ i, /** @type {any} */ helpers) {
|
|
101
|
+
return setextEnd(lines, i, helpers) - i + 1;
|
|
102
|
+
},
|
|
102
103
|
}
|
|
103
104
|
];
|
|
104
105
|
|
|
105
106
|
/**
|
|
106
107
|
* @param {string[]} lines
|
|
107
108
|
* @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[] }} helpers
|
|
109
|
+
* @param {import('../../utils/extensions.js').MarkdownRule} [rule]
|
|
108
110
|
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
109
111
|
*/
|
|
110
|
-
static fromMarkdown(lines, { parseInline }) {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
112
|
+
static fromMarkdown(lines, { parseInline }, rule) {
|
|
113
|
+
let level;
|
|
114
|
+
let text;
|
|
115
|
+
if (rule?.setext) {
|
|
116
|
+
const underline = lines[lines.length - 1].trim();
|
|
117
|
+
level = underline[0] === '=' ? 1 : 2;
|
|
118
|
+
text = lines.slice(0, -1).map((line) => line.trim().replace(/\\$/, '')).join(' ');
|
|
119
|
+
} else {
|
|
120
|
+
const m = lines[0].match(/^ {0,3}(#{1,6})[ \t]+(.*)/);
|
|
121
|
+
if (!m) return null;
|
|
122
|
+
level = m[1].length;
|
|
123
|
+
text = m[2];
|
|
124
|
+
}
|
|
125
|
+
const delta = parseInline(text);
|
|
114
126
|
if (!delta.length) return null;
|
|
115
|
-
return { type: 'heading', props: { level
|
|
127
|
+
return { type: 'heading', props: { level }, delta, partial: false };
|
|
116
128
|
}
|
|
117
129
|
|
|
118
130
|
static htmlRules = [
|
|
@@ -136,15 +148,15 @@ export class Heading extends Block {
|
|
|
136
148
|
return { type: 'heading', props: { level: rule?.props?.level ?? 1 }, delta, partial: false };
|
|
137
149
|
}
|
|
138
150
|
|
|
139
|
-
/** @param {Nabu} nabu @param {string} type @param {
|
|
151
|
+
/** @param {Nabu} nabu @param {string} type @param {Record<string, any>} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
140
152
|
static create(nabu, type, props = {}, parentId = null, index = null) {
|
|
141
|
-
const node = nabu.tree.createNode(parentId || undefined, index
|
|
153
|
+
const node = nabu.tree.createNode(/** @type {import('loro-crdt').TreeID | undefined} */ (parentId || undefined), index ?? undefined);
|
|
142
154
|
node.data.set("type", "heading");
|
|
143
155
|
node.data.set("level", props.level || 1);
|
|
144
156
|
const container = node.data.setContainer("text", new LoroText());
|
|
145
157
|
if (props.text) container.insert(0, props.text);
|
|
146
158
|
if (props.delta) container.applyDelta([...props.delta]);
|
|
147
|
-
const block = new Heading(nabu, node);
|
|
159
|
+
const block = new Heading(nabu, /** @type {HeadingNode} */ (node));
|
|
148
160
|
return block;
|
|
149
161
|
}
|
|
150
162
|
}
|
|
@@ -40,13 +40,15 @@ export const onBeforeInput = (nabu, event, block) => {
|
|
|
40
40
|
|
|
41
41
|
const level = match[1].length;
|
|
42
42
|
|
|
43
|
-
if (block.type === "heading" && block.level === level) return CONTINUE;
|
|
44
|
-
|
|
45
|
-
behavior.delete({index: 0, length: level});
|
|
43
|
+
if (block.type === "heading" && /** @type {Heading} */ (block).level === level) return CONTINUE;
|
|
46
44
|
|
|
47
45
|
const blockId = block.node.id.toString();
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
|
|
47
|
+
// Undo gives back "# " (trigger space included): see Nabu.shortcut()
|
|
48
|
+
nabu.shortcut(block, " ", (b) => {
|
|
49
|
+
b.delete({index: 0, length: level + 1}); // the hashes + the trigger space
|
|
50
|
+
block.transformTo("heading", { level });
|
|
51
|
+
});
|
|
50
52
|
|
|
51
53
|
tick().then(() => {
|
|
52
54
|
const block = nabu.blocks.get(blockId);
|