@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,189 @@
|
|
|
1
|
+
import { TextBlock } from "../text-block.svelte.js";
|
|
2
|
+
import { LoroText } from "loro-crdt";
|
|
3
|
+
import CalloutComponent from "./Callout.svelte";
|
|
4
|
+
import { tick } from "svelte";
|
|
5
|
+
import { escapeAttr } from "../../utils/html.js";
|
|
6
|
+
import { deltaToHtml, deltaToMarkdown } from "../../behaviors/text";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @import { Nabu, NabuNode } from "../nabu.svelte.js";
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {NabuNode<{type: "callout", text: LoroText, variant: string}>} CalloutNode
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export const CALLOUT_VARIANTS = ['note', 'tip', 'important', 'warning', 'caution'];
|
|
17
|
+
|
|
18
|
+
// @ts-ignore — static-side variance on create()
|
|
19
|
+
export class CalloutBlock extends TextBlock {
|
|
20
|
+
/** @param {Nabu} nabu @param {CalloutNode} node */
|
|
21
|
+
constructor(nabu, node) {
|
|
22
|
+
super(nabu, node);
|
|
23
|
+
|
|
24
|
+
this.variant = node.data.get('variant') || 'note';
|
|
25
|
+
|
|
26
|
+
this.serializers.set('markdown', () => {
|
|
27
|
+
const header = '> [!' + this.variant.toUpperCase() + ']';
|
|
28
|
+
const body = this.behavior.toMarkdown({ rawBreaks: true });
|
|
29
|
+
if (!body) return header;
|
|
30
|
+
return header + '\n' + body.split('\n').map(l => '> ' + l).join('\n');
|
|
31
|
+
});
|
|
32
|
+
this.serializers.set('json', () => ({
|
|
33
|
+
id: this.id,
|
|
34
|
+
type: 'callout',
|
|
35
|
+
props: { variant: this.variant },
|
|
36
|
+
content: this.behavior.toJSON()
|
|
37
|
+
}));
|
|
38
|
+
this.serializers.set('application/x-nabu+json', (ctx = {}) =>
|
|
39
|
+
this.behavior.toClipboardBlock({ ...ctx, props: { variant: this.variant } })
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
// Sync variant from Loro data changes (mirrors Heading's level sync)
|
|
43
|
+
this.node.data.subscribe(() => {
|
|
44
|
+
this.variant = this.node.data.get('variant') || 'note';
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ── Capability flags ─────────────────────────────────────────────────────
|
|
49
|
+
/** Enter inserts a literal \n (multi-line cohesive note) */
|
|
50
|
+
splitsOnEnter = false;
|
|
51
|
+
/** a typed "# " or "> " must NOT transform the callout away */
|
|
52
|
+
transformsEnabled = false;
|
|
53
|
+
// marksEnabled stays true (default) — bold/italic work inside callouts
|
|
54
|
+
|
|
55
|
+
/** @type {string} */
|
|
56
|
+
variant = $state('note');
|
|
57
|
+
|
|
58
|
+
static placeholder = 'Callout…';
|
|
59
|
+
|
|
60
|
+
component = $derived(this.nabu.components.get("callout") || CalloutComponent);
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Set the callout variant and commit.
|
|
64
|
+
* @param {string} v
|
|
65
|
+
*/
|
|
66
|
+
setVariant(v) {
|
|
67
|
+
if (!CALLOUT_VARIANTS.includes(v)) return;
|
|
68
|
+
this.variant = v;
|
|
69
|
+
/** @type {any} */ (this.node.data).set('variant', v);
|
|
70
|
+
this.commit();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Cycle to the next callout variant (the user's gesture: a click on the icon). Does nothing in a read-only editor.
|
|
75
|
+
*/
|
|
76
|
+
cycleVariant() {
|
|
77
|
+
if (this.nabu.readonly) return;
|
|
78
|
+
const i = CALLOUT_VARIANTS.indexOf(this.variant);
|
|
79
|
+
this.setVariant(CALLOUT_VARIANTS[(i + 1) % CALLOUT_VARIANTS.length]);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Handle special keydown events: ArrowDown escape.
|
|
84
|
+
* Returns true if handled (engine should preventDefault).
|
|
85
|
+
* @param {KeyboardEvent} event
|
|
86
|
+
* @returns {boolean}
|
|
87
|
+
*/
|
|
88
|
+
keydown(event) {
|
|
89
|
+
const sel = this.behavior.selection;
|
|
90
|
+
if (!sel) return false;
|
|
91
|
+
|
|
92
|
+
// ArrowDown (no modifiers, collapsed) — escape callout by creating a paragraph after
|
|
93
|
+
if (event.key === 'ArrowDown' && !event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey && sel.isCollapsed) {
|
|
94
|
+
const onLastLine = this.text.indexOf('\n', sel.from) === -1;
|
|
95
|
+
const hasNextTextBlock = !!this.findForward(b => b.behaviors?.has('text'));
|
|
96
|
+
if (onLastLine && !hasNextTextBlock) {
|
|
97
|
+
const p = this.nabu.insert('paragraph', {}, this.node.parent()?.id.toString() || null, (this.node.index() ?? 0) + 1);
|
|
98
|
+
this.nabu.commit().then(() => {
|
|
99
|
+
const fresh = this.nabu.blocks.get(p.id);
|
|
100
|
+
if (fresh) this.nabu.selection.setCursor(fresh, 0);
|
|
101
|
+
});
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// ── Statics ──────────────────────────────────────────────────────────────
|
|
111
|
+
|
|
112
|
+
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
113
|
+
static toMarkdown(pb) {
|
|
114
|
+
const v = (pb.props?.variant || 'note').toUpperCase();
|
|
115
|
+
const header = '> [!' + v + ']';
|
|
116
|
+
const body = deltaToMarkdown(pb.delta || [], { rawBreaks: true }); // the callout rule re-reads its own lines
|
|
117
|
+
if (!body) return header;
|
|
118
|
+
return header + '\n' + body.split('\n').map(l => '> ' + l).join('\n');
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
122
|
+
static toHtml(pb) {
|
|
123
|
+
const v = escapeAttr(pb.props?.variant || 'note');
|
|
124
|
+
return '<aside class="callout callout-' + v + '" data-callout="' + v + '">' + deltaToHtml(pb.delta || []) + '</aside>';
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
static markdownRules = [
|
|
128
|
+
{
|
|
129
|
+
priority: 20,
|
|
130
|
+
interruptsParagraph: true,
|
|
131
|
+
detect: /^> \[!(note|tip|important|warning|caution)\]/i,
|
|
132
|
+
/**
|
|
133
|
+
* Consume the callout header line plus all subsequent ">" prefixed lines.
|
|
134
|
+
* @param {string[]} lines
|
|
135
|
+
* @param {number} i
|
|
136
|
+
* @returns {number}
|
|
137
|
+
*/
|
|
138
|
+
consume(lines, i) {
|
|
139
|
+
let j = i + 1;
|
|
140
|
+
while (j < lines.length && /^>\s?/.test(lines[j])) j++;
|
|
141
|
+
return j - i;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
];
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* @param {string[]} lines
|
|
148
|
+
* @param {{ parseInline: (text: string) => import('loro-crdt').Delta<string>[] }} helpers
|
|
149
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
150
|
+
*/
|
|
151
|
+
static fromMarkdown(lines, { parseInline }) {
|
|
152
|
+
const m = lines[0].match(/^> \[!(\w+)\]/i);
|
|
153
|
+
if (!m) return null;
|
|
154
|
+
let variant = m[1].toLowerCase();
|
|
155
|
+
if (!CALLOUT_VARIANTS.includes(variant)) variant = 'note';
|
|
156
|
+
const body = lines.slice(1).map(l => l.replace(/^>\s?/, '')).join('\n');
|
|
157
|
+
const delta = parseInline(body);
|
|
158
|
+
return { type: 'callout', props: { variant }, delta, partial: false };
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
static htmlRules = [
|
|
162
|
+
{ selector: '[data-callout]', priority: 20 },
|
|
163
|
+
{ selector: 'aside.callout', priority: 20 },
|
|
164
|
+
];
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* @param {Element} el
|
|
168
|
+
* @param {{ parseInline: (el: Element) => import('loro-crdt').Delta<string>[] }} helpers
|
|
169
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
170
|
+
*/
|
|
171
|
+
static fromHTML(el, { parseInline }) {
|
|
172
|
+
let variant = el.getAttribute('data-callout') || (el.className.match(/callout-(\w+)/)?.[1]) || 'note';
|
|
173
|
+
if (!CALLOUT_VARIANTS.includes(variant)) variant = 'note';
|
|
174
|
+
const delta = parseInline(el);
|
|
175
|
+
return { type: 'callout', props: { variant }, delta, partial: false };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** @param {Nabu} nabu @param {string} type @param {Record<string, any>} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
179
|
+
static create(nabu, type, props = {}, parentId = null, index = null) {
|
|
180
|
+
const node = nabu.tree.createNode(/** @type {import('loro-crdt').TreeID | undefined} */ (parentId || undefined), index ?? undefined);
|
|
181
|
+
/** @type {any} */ (node.data).set('type', 'callout');
|
|
182
|
+
const variant = CALLOUT_VARIANTS.includes(props.variant) ? props.variant : 'note';
|
|
183
|
+
/** @type {any} */ (node.data).set('variant', variant);
|
|
184
|
+
const container = /** @type {any} */ (node.data).setContainer('text', new LoroText());
|
|
185
|
+
if (props.text) container.insert(0, props.text);
|
|
186
|
+
if (props.delta) container.applyDelta([...props.delta]);
|
|
187
|
+
return new CalloutBlock(nabu, /** @type {CalloutNode} */ (/** @type {unknown} */ (node)));
|
|
188
|
+
}
|
|
189
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** Whether the browser can skip off-screen boxes (`content-visibility: auto`). Without it chunking would only add wrappers, so it is not done. */
|
|
2
|
+
export function supportsChunking(): boolean;
|
|
3
|
+
/** Whether a chunk starts at the block with this id. @param {string} id */
|
|
4
|
+
export function startsChunk(id: string): boolean;
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {Object} Chunk
|
|
7
|
+
* @property {string} key the id of its first block (stable while that block exists)
|
|
8
|
+
* @property {import('./block.svelte.js').Block[]} blocks
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Cut `blocks` into chunks. A chunk starts at the first block and at every block for which {@link startsChunk} is true.
|
|
12
|
+
* A chunk of `previous` (a Map by key) with the same blocks in the same order is returned as is, so that a keyed render sees an
|
|
13
|
+
* unchanged object and does not look at it again.
|
|
14
|
+
* @param {import('./block.svelte.js').Block[]} blocks
|
|
15
|
+
* @param {Map<string, Chunk>} [previous]
|
|
16
|
+
* @returns {Chunk[]}
|
|
17
|
+
*/
|
|
18
|
+
export function chunkBlocks(blocks: import("./block.svelte.js").Block[], previous?: Map<string, Chunk>): Chunk[];
|
|
19
|
+
/**
|
|
20
|
+
* Large documents: how a long list of sibling blocks is cut into CHUNKS.
|
|
21
|
+
*
|
|
22
|
+
* WHY. A keystroke costs the page more than the engine does: the browser lays out, paints and syncs the selection of the WHOLE
|
|
23
|
+
* contenteditable on every edit, and that work grows with the number of elements it has to look at (measured: 1 ms of script and no DOM
|
|
24
|
+
* node created per key at 10,000 blocks, but ~90 ms from key to next paint, almost all of it inside Chromium). `content-visibility: auto`
|
|
25
|
+
* lets the browser skip the layout, paint and selection bookkeeping of everything that is off screen, but it is only cheap on a FEW
|
|
26
|
+
* large boxes: one per block costs as much as it saves. So a long list of siblings is rendered as a few hundred chunk wrappers of
|
|
27
|
+
* ~64 blocks, each one `content-visibility: auto` (see BlockList.svelte).
|
|
28
|
+
*
|
|
29
|
+
* HOW the cuts are chosen. By CONTENT, not by position: a block starts a chunk when a hash of its id says so (one in `CHUNK_AVG`
|
|
30
|
+
* on average), whatever its index. Inserting or deleting a block then moves at most the one cut that block is, so a keyed render only
|
|
31
|
+
* ever touches the chunk (or the two chunks) around the edit; cutting every 64 blocks by index would shift every chunk after an Enter.
|
|
32
|
+
*
|
|
33
|
+
* Nothing here knows a block type: any list of blocks can be chunked.
|
|
34
|
+
*/
|
|
35
|
+
/** A list of at least this many sibling blocks is rendered in chunks... */
|
|
36
|
+
export const CHUNK_ON: 500;
|
|
37
|
+
/** ...until it shrinks under this many (hysteresis: a list hovering around the threshold does not flip its markup on every key). */
|
|
38
|
+
export const CHUNK_OFF: 250;
|
|
39
|
+
/** Average number of blocks in a chunk (a cut every `CHUNK_AVG` blocks). */
|
|
40
|
+
export const CHUNK_AVG: 64;
|
|
41
|
+
/** A document of at least this many blocks (all levels) is "large" (`data-nabu-large` on the editor root): containers may skip their off-screen bodies. */
|
|
42
|
+
export const LARGE_DOC: 800;
|
|
43
|
+
export type Chunk = {
|
|
44
|
+
/**
|
|
45
|
+
* the id of its first block (stable while that block exists)
|
|
46
|
+
*/
|
|
47
|
+
key: string;
|
|
48
|
+
blocks: import("./block.svelte.js").Block[];
|
|
49
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Large documents: how a long list of sibling blocks is cut into CHUNKS.
|
|
3
|
+
*
|
|
4
|
+
* WHY. A keystroke costs the page more than the engine does: the browser lays out, paints and syncs the selection of the WHOLE
|
|
5
|
+
* contenteditable on every edit, and that work grows with the number of elements it has to look at (measured: 1 ms of script and no DOM
|
|
6
|
+
* node created per key at 10,000 blocks, but ~90 ms from key to next paint, almost all of it inside Chromium). `content-visibility: auto`
|
|
7
|
+
* lets the browser skip the layout, paint and selection bookkeeping of everything that is off screen, but it is only cheap on a FEW
|
|
8
|
+
* large boxes: one per block costs as much as it saves. So a long list of siblings is rendered as a few hundred chunk wrappers of
|
|
9
|
+
* ~64 blocks, each one `content-visibility: auto` (see BlockList.svelte).
|
|
10
|
+
*
|
|
11
|
+
* HOW the cuts are chosen. By CONTENT, not by position: a block starts a chunk when a hash of its id says so (one in `CHUNK_AVG`
|
|
12
|
+
* on average), whatever its index. Inserting or deleting a block then moves at most the one cut that block is, so a keyed render only
|
|
13
|
+
* ever touches the chunk (or the two chunks) around the edit; cutting every 64 blocks by index would shift every chunk after an Enter.
|
|
14
|
+
*
|
|
15
|
+
* Nothing here knows a block type: any list of blocks can be chunked.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** A list of at least this many sibling blocks is rendered in chunks... */
|
|
19
|
+
export const CHUNK_ON = 500;
|
|
20
|
+
/** ...until it shrinks under this many (hysteresis: a list hovering around the threshold does not flip its markup on every key). */
|
|
21
|
+
export const CHUNK_OFF = 250;
|
|
22
|
+
/** Average number of blocks in a chunk (a cut every `CHUNK_AVG` blocks). */
|
|
23
|
+
export const CHUNK_AVG = 64;
|
|
24
|
+
/** A document of at least this many blocks (all levels) is "large" (`data-nabu-large` on the editor root): containers may skip their off-screen bodies. */
|
|
25
|
+
export const LARGE_DOC = 800;
|
|
26
|
+
|
|
27
|
+
/** @type {boolean | undefined} */
|
|
28
|
+
let supported;
|
|
29
|
+
/** Whether the browser can skip off-screen boxes (`content-visibility: auto`). Without it chunking would only add wrappers, so it is not done. */
|
|
30
|
+
export function supportsChunking() {
|
|
31
|
+
if (supported === undefined) supported = typeof CSS !== 'undefined' && typeof CSS.supports === 'function' && CSS.supports('content-visibility', 'auto');
|
|
32
|
+
return supported;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** FNV-1a (32 bit) of a string. @param {string} s */
|
|
36
|
+
function fnv1a(s) {
|
|
37
|
+
let h = 0x811c9dc5;
|
|
38
|
+
for (let i = 0; i < s.length; i++) {
|
|
39
|
+
h ^= s.charCodeAt(i);
|
|
40
|
+
h = Math.imul(h, 0x01000193);
|
|
41
|
+
}
|
|
42
|
+
return h >>> 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Whether a chunk starts at the block with this id. @param {string} id */
|
|
46
|
+
export function startsChunk(id) {
|
|
47
|
+
return fnv1a(id) % CHUNK_AVG === 0;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @typedef {Object} Chunk
|
|
52
|
+
* @property {string} key the id of its first block (stable while that block exists)
|
|
53
|
+
* @property {import('./block.svelte.js').Block[]} blocks
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Cut `blocks` into chunks. A chunk starts at the first block and at every block for which {@link startsChunk} is true.
|
|
58
|
+
* A chunk of `previous` (a Map by key) with the same blocks in the same order is returned as is, so that a keyed render sees an
|
|
59
|
+
* unchanged object and does not look at it again.
|
|
60
|
+
* @param {import('./block.svelte.js').Block[]} blocks
|
|
61
|
+
* @param {Map<string, Chunk>} [previous]
|
|
62
|
+
* @returns {Chunk[]}
|
|
63
|
+
*/
|
|
64
|
+
export function chunkBlocks(blocks, previous) {
|
|
65
|
+
/** @type {Chunk[]} */
|
|
66
|
+
const out = [];
|
|
67
|
+
/** @type {import('./block.svelte.js').Block[]} */
|
|
68
|
+
let current = [];
|
|
69
|
+
const flush = () => {
|
|
70
|
+
if (!current.length) return;
|
|
71
|
+
const key = current[0].id;
|
|
72
|
+
const old = previous?.get(key);
|
|
73
|
+
out.push(old && sameBlocks(old.blocks, current) ? old : { key, blocks: current });
|
|
74
|
+
current = [];
|
|
75
|
+
};
|
|
76
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
77
|
+
const block = blocks[i];
|
|
78
|
+
if (current.length && startsChunk(block.id)) flush();
|
|
79
|
+
current.push(block);
|
|
80
|
+
}
|
|
81
|
+
flush();
|
|
82
|
+
return out;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** @param {unknown[]} a @param {unknown[]} b */
|
|
86
|
+
function sameBlocks(a, b) {
|
|
87
|
+
if (a.length !== b.length) return false;
|
|
88
|
+
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
/** @type {{ block: import('./code.svelte.js').CodeBlock }} */
|
|
3
|
+
let { block } = $props();
|
|
4
|
+
const LANGS = ['plaintext', 'javascript', 'typescript', 'html', 'css', 'json', 'python', 'rust', 'bash', 'markdown', 'sql', 'go'];
|
|
5
|
+
</script>
|
|
6
|
+
|
|
7
|
+
<div class="nabu-code-wrapper"><select
|
|
8
|
+
class="nabu-code-lang"
|
|
9
|
+
data-nabu-ui
|
|
10
|
+
contenteditable="false"
|
|
11
|
+
value={block.language ?? 'plaintext'}
|
|
12
|
+
disabled={block.nabu.readonly}
|
|
13
|
+
aria-label="Code language"
|
|
14
|
+
onchange={(e) => block.setLanguage(e.currentTarget.value === 'plaintext' ? null : e.currentTarget.value)}
|
|
15
|
+
>
|
|
16
|
+
{#each LANGS as lang}<option value={lang}>{lang}</option>{/each}
|
|
17
|
+
</select><pre
|
|
18
|
+
bind:this={block.element}
|
|
19
|
+
data-block-id={block.id}
|
|
20
|
+
data-block-type="code"
|
|
21
|
+
spellcheck="false"
|
|
22
|
+
data-placeholder={block.placeholder}
|
|
23
|
+
class="nabu-code"
|
|
24
|
+
class:debug={block.nabu.debugging}
|
|
25
|
+
class:selected={block.selected}
|
|
26
|
+
><code>{block.text || '\n'}{#if block.text.endsWith('\n')}<br data-nabu-filler>{/if}</code></pre></div>
|
|
27
|
+
|
|
28
|
+
<style>
|
|
29
|
+
.nabu-code-wrapper {
|
|
30
|
+
position: relative;
|
|
31
|
+
margin: 0.5em 0;
|
|
32
|
+
border-radius: 6px;
|
|
33
|
+
border: 1px solid var(--nb-border, rgba(255, 255, 255, 0.12));
|
|
34
|
+
background: var(--nb-bg-surface-sunken, rgba(0, 0, 0, 0.25));
|
|
35
|
+
overflow: hidden;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
.nabu-code-lang {
|
|
39
|
+
position: absolute;
|
|
40
|
+
top: 0.4em;
|
|
41
|
+
right: 0.5em;
|
|
42
|
+
font-size: 10px;
|
|
43
|
+
font-family: inherit;
|
|
44
|
+
color: var(--nb-text-muted, rgba(255, 255, 255, 0.4));
|
|
45
|
+
background: var(--nb-bg-surface, rgba(255, 255, 255, 0.05));
|
|
46
|
+
border: 1px solid var(--nb-border-subtle, rgba(255, 255, 255, 0.08));
|
|
47
|
+
border-radius: 4px;
|
|
48
|
+
padding: 1px 4px;
|
|
49
|
+
cursor: pointer;
|
|
50
|
+
user-select: none;
|
|
51
|
+
outline: none;
|
|
52
|
+
z-index: 1;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.nabu-code-lang:focus {
|
|
56
|
+
border-color: var(--nb-border, rgba(255, 255, 255, 0.2));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
.nabu-code {
|
|
60
|
+
display: block;
|
|
61
|
+
white-space: pre;
|
|
62
|
+
font-family: var(--nb-font-mono, 'Menlo', 'Monaco', 'Consolas', monospace);
|
|
63
|
+
font-size: 0.85em;
|
|
64
|
+
line-height: 1.6;
|
|
65
|
+
padding: 0.75em 1em;
|
|
66
|
+
margin: 0;
|
|
67
|
+
overflow-x: auto;
|
|
68
|
+
color: var(--nb-text-primary, rgba(255, 255, 255, 0.85));
|
|
69
|
+
outline: none;
|
|
70
|
+
min-height: 1.6em;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
.nabu-code.debug.selected {
|
|
74
|
+
background-color: rgba(59, 130, 246, 0.1);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
.nabu-code code {
|
|
78
|
+
font-family: inherit;
|
|
79
|
+
font-size: inherit;
|
|
80
|
+
background: none;
|
|
81
|
+
padding: 0;
|
|
82
|
+
white-space: inherit;
|
|
83
|
+
}
|
|
84
|
+
</style>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export default Code;
|
|
2
|
+
type Code = {
|
|
3
|
+
$on?(type: string, callback: (e: any) => void): () => void;
|
|
4
|
+
$set?(props: Partial<$$ComponentProps>): void;
|
|
5
|
+
};
|
|
6
|
+
declare const Code: import("svelte").Component<{
|
|
7
|
+
block: import("./code.svelte.js").CodeBlock;
|
|
8
|
+
}, {}, "">;
|
|
9
|
+
type $$ComponentProps = {
|
|
10
|
+
block: import("./code.svelte.js").CodeBlock;
|
|
11
|
+
};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { tick } from "svelte";
|
|
2
|
+
import { TextBehavior } from "../../behaviors/text";
|
|
3
|
+
import { extension } from "../../utils/extensions";
|
|
4
|
+
import { CodeBlock } from "./code.svelte.js";
|
|
5
|
+
import CodeComponent from "./Code.svelte";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @import { Nabu, Block } from "..";
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* onBeforeInput hook for CodeBlock:
|
|
13
|
+
* - Inside a code block: Backspace on empty → transform back to paragraph; Backspace at offset 0 of a
|
|
14
|
+
* non-empty one → paragraph too (same text, language dropped, caret stays at 0).
|
|
15
|
+
* - Inside a non-code text block: ``` + Enter → transform to code block.
|
|
16
|
+
*
|
|
17
|
+
* @param {Nabu} nabu @param {InputEvent} event @param {Block} block
|
|
18
|
+
*/
|
|
19
|
+
const onBeforeInput = (nabu, event, block) => {
|
|
20
|
+
const { BREAK, CONTINUE } = nabu;
|
|
21
|
+
|
|
22
|
+
const behavior = block.behaviors.get('text');
|
|
23
|
+
if (!(behavior instanceof TextBehavior)) return CONTINUE;
|
|
24
|
+
const sel = behavior.selection;
|
|
25
|
+
if (!sel || !sel.isCollapsed) return CONTINUE;
|
|
26
|
+
|
|
27
|
+
if (block instanceof CodeBlock) {
|
|
28
|
+
// Backspace at position 0 in an empty code block → back to paragraph
|
|
29
|
+
if (event.inputType === 'deleteContentBackward' && sel.from === 0 && behavior.text.length === 0) {
|
|
30
|
+
const blockId = block.node.id.toString();
|
|
31
|
+
block.transformTo('paragraph');
|
|
32
|
+
tick().then(() => {
|
|
33
|
+
const fresh = nabu.blocks.get(blockId);
|
|
34
|
+
if (fresh) fresh.focus({ start: 0, end: 0 });
|
|
35
|
+
});
|
|
36
|
+
return BREAK;
|
|
37
|
+
}
|
|
38
|
+
// Backspace at position 0 in a NON-empty code block (collapsed caret) → it becomes a paragraph with the
|
|
39
|
+
// same text (the language is dropped), caret stays at 0. A second Backspace then merges normally.
|
|
40
|
+
if (event.inputType === 'deleteContentBackward' && sel.from === 0 && behavior.text.length > 0) {
|
|
41
|
+
const blockId = block.node.id.toString();
|
|
42
|
+
/** @type {any} */ (block.node.data).delete('language');
|
|
43
|
+
block.transformTo('paragraph');
|
|
44
|
+
tick().then(() => {
|
|
45
|
+
const fresh = nabu.blocks.get(blockId);
|
|
46
|
+
if (fresh) fresh.focus({ start: 0, end: 0 });
|
|
47
|
+
});
|
|
48
|
+
return BREAK;
|
|
49
|
+
}
|
|
50
|
+
return CONTINUE;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ``` + Enter in a non-code text block → transform to code
|
|
54
|
+
if (event.inputType === 'insertParagraph') {
|
|
55
|
+
const m = behavior.text.match(/^```(\w*)$/);
|
|
56
|
+
if (!m) return CONTINUE;
|
|
57
|
+
const language = m[1] || null;
|
|
58
|
+
behavior.delete({ index: 0, length: behavior.text.length });
|
|
59
|
+
const blockId = block.node.id.toString();
|
|
60
|
+
block.transformTo('code', language ? { language } : {});
|
|
61
|
+
tick().then(() => {
|
|
62
|
+
const fresh = nabu.blocks.get(blockId);
|
|
63
|
+
if (fresh) fresh.focus({ start: 0, end: 0 });
|
|
64
|
+
});
|
|
65
|
+
return BREAK;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return CONTINUE;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The code block: plain monospace text with a language, the ``` shortcut, the `code:block` action and the "Code" slash entry.
|
|
73
|
+
*/
|
|
74
|
+
export const CodeExtension = extension("code", {
|
|
75
|
+
// @ts-ignore — static-side variance on create() (same pattern as Paragraph/Heading)
|
|
76
|
+
block: CodeBlock,
|
|
77
|
+
component: CodeComponent,
|
|
78
|
+
slashCommands: [
|
|
79
|
+
{ title: 'Code', icon: '<>', aliases: ['code', 'snippet'], group: 'blocks', order: 0, run: (e) => e.exec('code:block') },
|
|
80
|
+
],
|
|
81
|
+
actions: {
|
|
82
|
+
// `code` alone is the inline mark of RichTextExtension (like `bold`, `italic`...): the block is `code:block`.
|
|
83
|
+
'code:block': (nabu) => nabu.exec('block:transform', { type: 'code' }),
|
|
84
|
+
},
|
|
85
|
+
hooks: {
|
|
86
|
+
onBeforeInput,
|
|
87
|
+
}
|
|
88
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
export class CodeBlock extends TextBlock {
|
|
2
|
+
static placeholder: string;
|
|
3
|
+
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
4
|
+
static toMarkdown(pb: import("../../utils/extensions.js").PasteBlock): string;
|
|
5
|
+
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
6
|
+
static toHtml(pb: import("../../utils/extensions.js").PasteBlock): string;
|
|
7
|
+
/**
|
|
8
|
+
* Fenced code (CommonMark): 3+ backticks or 3+ tildes (up to 3 spaces of indent), an optional info string (the first
|
|
9
|
+
* word is the language), closed by a fence of the SAME character at least as long. An unclosed fence runs to the end.
|
|
10
|
+
* The content is verbatim (no inline parsing, blank lines kept). An indented block (4 spaces) is NOT code.
|
|
11
|
+
*/
|
|
12
|
+
static markdownRules: {
|
|
13
|
+
priority: number;
|
|
14
|
+
interruptsParagraph: boolean;
|
|
15
|
+
detect: RegExp;
|
|
16
|
+
/**
|
|
17
|
+
* Consume from the opening fence to the closing fence (or EOF).
|
|
18
|
+
* @param {string[]} lines
|
|
19
|
+
* @param {number} i
|
|
20
|
+
* @returns {number}
|
|
21
|
+
*/
|
|
22
|
+
consume(lines: string[], i: number): number;
|
|
23
|
+
}[];
|
|
24
|
+
/**
|
|
25
|
+
* @param {string[]} lines
|
|
26
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
27
|
+
*/
|
|
28
|
+
static fromMarkdown(lines: string[]): import("../../utils/extensions.js").PasteBlock | null;
|
|
29
|
+
static htmlRules: {
|
|
30
|
+
selector: string;
|
|
31
|
+
priority: number;
|
|
32
|
+
}[];
|
|
33
|
+
/**
|
|
34
|
+
* @param {Element} el
|
|
35
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
36
|
+
*/
|
|
37
|
+
static fromHTML(el: Element): import("../../utils/extensions.js").PasteBlock | null;
|
|
38
|
+
/** @param {Nabu} nabu @param {string} type @param {Record<string, any>} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
39
|
+
static create(nabu: Nabu, type: string, props?: Record<string, any>, parentId?: string | null, index?: number | null): CodeBlock;
|
|
40
|
+
/** @param {Nabu} nabu @param {CodeNode} node */
|
|
41
|
+
constructor(nabu: Nabu, node: CodeNode);
|
|
42
|
+
/** @type {string | null} */
|
|
43
|
+
language: string | null;
|
|
44
|
+
component: import("svelte").Component<any, any, string> | import("svelte").Component<{
|
|
45
|
+
block: import("./code.svelte.js").CodeBlock;
|
|
46
|
+
}, {}, "">;
|
|
47
|
+
/**
|
|
48
|
+
* Set the code language and commit.
|
|
49
|
+
* @param {string | null} lang
|
|
50
|
+
*/
|
|
51
|
+
setLanguage(lang: string | null): void;
|
|
52
|
+
/**
|
|
53
|
+
* Handle special keydown events: Tab, Shift+Tab, ArrowDown.
|
|
54
|
+
* Returns true if handled (engine should preventDefault).
|
|
55
|
+
* @param {KeyboardEvent} event
|
|
56
|
+
* @returns {boolean}
|
|
57
|
+
*/
|
|
58
|
+
keydown(event: KeyboardEvent): boolean;
|
|
59
|
+
}
|
|
60
|
+
export type CodeNode = NabuNode<{
|
|
61
|
+
type: "code";
|
|
62
|
+
text: LoroText;
|
|
63
|
+
language: string | null;
|
|
64
|
+
}>;
|
|
65
|
+
import { TextBlock } from "../text-block.svelte.js";
|
|
66
|
+
import type { Nabu } from "../nabu.svelte.js";
|
|
67
|
+
import { LoroText } from "loro-crdt";
|
|
68
|
+
import type { NabuNode } from "../nabu.svelte.js";
|