@aionbuilders/nabu 0.1.0-beta.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +523 -0
- package/LICENSE +21 -0
- package/README.md +72 -341
- package/dist/behaviors/text/RichText.svelte +24 -4
- package/dist/behaviors/text/markdown-shortcuts.extension.d.ts +3 -0
- package/dist/behaviors/text/markdown-shortcuts.extension.js +35 -10
- package/dist/behaviors/text/markdown-syntax.d.ts +42 -0
- package/dist/behaviors/text/markdown-syntax.js +81 -0
- package/dist/behaviors/text/rich-text.extension.d.ts +3 -0
- package/dist/behaviors/text/rich-text.extension.js +95 -38
- package/dist/behaviors/text/text.behavior.svelte.d.ts +199 -48
- package/dist/behaviors/text/text.behavior.svelte.js +549 -83
- package/dist/behaviors/text/typography.extension.d.ts +2 -1
- package/dist/behaviors/text/typography.extension.js +3 -2
- package/dist/blocks/BlockList.svelte +48 -0
- package/dist/blocks/BlockList.svelte.d.ts +17 -0
- package/dist/blocks/Nabu.svelte +169 -25
- package/dist/blocks/Nabu.svelte.d.ts +13 -0
- package/dist/blocks/block.extension.d.ts +3 -0
- package/dist/blocks/block.extension.js +4 -7
- package/dist/blocks/block.svelte.d.ts +350 -86
- package/dist/blocks/block.svelte.js +370 -59
- package/dist/blocks/blockquote/Blockquote.svelte +1 -2
- package/dist/blocks/blockquote/blockquote.extension.d.ts +4 -1
- package/dist/blocks/blockquote/blockquote.extension.js +14 -5
- package/dist/blocks/blockquote/blockquote.svelte.d.ts +19 -11
- package/dist/blocks/blockquote/blockquote.svelte.js +58 -13
- package/dist/blocks/callout/Callout.svelte +6 -8
- package/dist/blocks/callout/callout.extension.d.ts +4 -1
- package/dist/blocks/callout/callout.extension.js +19 -3
- package/dist/blocks/callout/callout.svelte.d.ts +5 -3
- package/dist/blocks/callout/callout.svelte.js +11 -6
- package/dist/blocks/chunks.d.ts +49 -0
- package/dist/blocks/chunks.js +90 -0
- package/dist/blocks/code/Code.svelte +7 -8
- package/dist/blocks/code/code.extension.d.ts +4 -1
- package/dist/blocks/code/code.extension.js +22 -2
- package/dist/blocks/code/code.svelte.d.ts +9 -2
- package/dist/blocks/code/code.svelte.js +49 -25
- package/dist/blocks/container.utils.d.ts +85 -13
- package/dist/blocks/container.utils.js +338 -178
- package/dist/blocks/dialogue/Dialogue.svelte +8 -4
- package/dist/blocks/dialogue/dialogue.svelte.d.ts +33 -2
- package/dist/blocks/dialogue/dialogue.svelte.js +58 -4
- package/dist/blocks/dialogue/hooks/onBeforeInput.hook.d.ts +2 -2
- package/dist/blocks/dialogue/hooks/onBeforeInput.hook.js +6 -4
- package/dist/blocks/dialogue/index.d.ts +12 -1
- package/dist/blocks/dialogue/index.js +51 -35
- package/dist/blocks/divider/divider.extension.d.ts +23 -1
- package/dist/blocks/divider/divider.extension.js +85 -19
- package/dist/blocks/divider/divider.svelte.d.ts +7 -2
- package/dist/blocks/divider/divider.svelte.js +9 -4
- package/dist/blocks/file/File.svelte +13 -6
- package/dist/blocks/file/file.extension.d.ts +4 -1
- package/dist/blocks/file/file.extension.js +9 -1
- package/dist/blocks/file/file.svelte.d.ts +3 -3
- package/dist/blocks/file/file.svelte.js +20 -14
- package/dist/blocks/heading/Heading.svelte +1 -2
- package/dist/blocks/heading/heading.svelte.d.ts +23 -11
- package/dist/blocks/heading/heading.svelte.js +68 -13
- package/dist/blocks/heading/hooks/onBeforeInput.hook.js +7 -5
- package/dist/blocks/heading/index.d.ts +2 -2
- package/dist/blocks/heading/index.js +33 -8
- package/dist/blocks/image/Image.svelte +5 -2
- package/dist/blocks/image/image.extension.d.ts +4 -1
- package/dist/blocks/image/image.extension.js +9 -1
- package/dist/blocks/image/image.svelte.d.ts +17 -6
- package/dist/blocks/image/image.svelte.js +33 -11
- package/dist/blocks/index.d.ts +1 -1
- package/dist/blocks/index.js +1 -1
- package/dist/blocks/list/List.svelte +1 -0
- package/dist/blocks/list/ListItem.svelte +1 -0
- package/dist/blocks/list/convert.d.ts +20 -0
- package/dist/blocks/list/convert.js +92 -0
- package/dist/blocks/list/hooks/onBeforeInput.hook.js +49 -28
- package/dist/blocks/list/html-flat.d.ts +78 -0
- package/dist/blocks/list/html-flat.js +244 -0
- package/dist/blocks/list/indent.d.ts +86 -0
- package/dist/blocks/list/indent.js +205 -0
- package/dist/blocks/list/index.d.ts +6 -3
- package/dist/blocks/list/index.js +42 -31
- package/dist/blocks/list/list-item.svelte.d.ts +31 -17
- package/dist/blocks/list/list-item.svelte.js +99 -100
- package/dist/blocks/list/list.behavior.svelte.d.ts +4 -1
- package/dist/blocks/list/list.behavior.svelte.js +6 -1
- package/dist/blocks/list/list.svelte.d.ts +89 -18
- package/dist/blocks/list/list.svelte.js +129 -125
- package/dist/blocks/list/markdown.d.ts +149 -0
- package/dist/blocks/list/markdown.js +442 -0
- package/dist/blocks/list/normalize.d.ts +11 -0
- package/dist/blocks/list/normalize.js +396 -0
- package/dist/blocks/list/paste.d.ts +45 -0
- package/dist/blocks/list/paste.js +242 -0
- package/dist/blocks/list/step-back.d.ts +24 -0
- package/dist/blocks/list/step-back.js +87 -0
- package/dist/blocks/list/to-list.d.ts +7 -0
- package/dist/blocks/list/to-list.js +44 -0
- package/dist/blocks/list/todos-toggle.d.ts +20 -0
- package/dist/blocks/list/todos-toggle.js +48 -0
- package/dist/blocks/media/markdown.d.ts +12 -0
- package/dist/blocks/media/markdown.js +26 -0
- package/dist/blocks/media/media-block.svelte.d.ts +77 -2
- package/dist/blocks/media/media-block.svelte.js +124 -12
- package/dist/blocks/media/media.helpers.d.ts +6 -1
- package/dist/blocks/media/media.helpers.js +43 -6
- package/dist/blocks/megablock.svelte.d.ts +50 -19
- package/dist/blocks/megablock.svelte.js +114 -74
- package/dist/blocks/move.d.ts +23 -0
- package/dist/blocks/move.js +92 -0
- package/dist/blocks/nabu.svelte.d.ts +764 -71
- package/dist/blocks/nabu.svelte.js +1746 -189
- package/dist/blocks/paragraph/Paragraph.svelte +1 -2
- package/dist/blocks/paragraph/index.d.ts +2 -2
- package/dist/blocks/paragraph/index.js +35 -13
- package/dist/blocks/paragraph/paragraph.svelte.d.ts +46 -14
- package/dist/blocks/paragraph/paragraph.svelte.js +77 -16
- package/dist/blocks/paragraph/table.html.d.ts +9 -0
- package/dist/blocks/paragraph/table.html.js +111 -0
- package/dist/blocks/paragraph/table.markdown.d.ts +32 -0
- package/dist/blocks/paragraph/table.markdown.js +90 -0
- package/dist/blocks/region/Region.svelte +264 -0
- package/dist/blocks/region/Region.svelte.d.ts +11 -0
- package/dist/blocks/region/actions.d.ts +135 -0
- package/dist/blocks/region/actions.js +402 -0
- package/dist/blocks/region/close.d.ts +35 -0
- package/dist/blocks/region/close.js +130 -0
- package/dist/blocks/region/commands.d.ts +6 -0
- package/dist/blocks/region/commands.js +277 -0
- package/dist/blocks/region/create.d.ts +72 -0
- package/dist/blocks/region/create.js +208 -0
- package/dist/blocks/region/end-marker.d.ts +35 -0
- package/dist/blocks/region/end-marker.js +51 -0
- package/dist/blocks/region/facts.d.ts +69 -0
- package/dist/blocks/region/facts.js +152 -0
- package/dist/blocks/region/format.d.ts +91 -0
- package/dist/blocks/region/format.js +260 -0
- package/dist/blocks/region/index.d.ts +14 -0
- package/dist/blocks/region/index.js +14 -0
- package/dist/blocks/region/insert.d.ts +59 -0
- package/dist/blocks/region/insert.js +102 -0
- package/dist/blocks/region/messages.d.ts +254 -0
- package/dist/blocks/region/messages.js +131 -0
- package/dist/blocks/region/normalize.d.ts +7 -0
- package/dist/blocks/region/normalize.js +170 -0
- package/dist/blocks/region/region.extension.d.ts +58 -0
- package/dist/blocks/region/region.extension.js +119 -0
- package/dist/blocks/region/region.svelte.d.ts +221 -0
- package/dist/blocks/region/region.svelte.js +582 -0
- package/dist/blocks/region/regions.svelte.d.ts +141 -0
- package/dist/blocks/region/regions.svelte.js +276 -0
- package/dist/blocks/region/serialize.d.ts +57 -0
- package/dist/blocks/region/serialize.js +112 -0
- package/dist/blocks/region/types.d.ts +255 -0
- package/dist/blocks/region/types.js +204 -0
- package/dist/blocks/selection.svelte.d.ts +106 -8
- package/dist/blocks/selection.svelte.js +622 -74
- package/dist/blocks/subthread.d.ts +80 -0
- package/dist/blocks/subthread.js +167 -0
- package/dist/blocks/text-block.svelte.d.ts +34 -13
- package/dist/blocks/text-block.svelte.js +42 -8
- package/dist/blocks/toggle/Toggle.svelte +4 -15
- package/dist/blocks/toggle/markdown.d.ts +20 -0
- package/dist/blocks/toggle/markdown.js +112 -0
- package/dist/blocks/toggle/toggle.extension.d.ts +4 -1
- package/dist/blocks/toggle/toggle.extension.js +6 -0
- package/dist/blocks/toggle/toggle.svelte.d.ts +54 -7
- package/dist/blocks/toggle/toggle.svelte.js +134 -34
- package/dist/blocks/undo-selection.d.ts +175 -0
- package/dist/blocks/undo-selection.js +360 -0
- package/dist/blocks/video/Video.svelte +5 -2
- package/dist/blocks/video/video.extension.d.ts +4 -1
- package/dist/blocks/video/video.extension.js +9 -1
- package/dist/blocks/video/video.svelte.d.ts +6 -5
- package/dist/blocks/video/video.svelte.js +18 -9
- package/dist/extend.d.ts +55 -0
- package/dist/extend.js +88 -0
- package/dist/files/assets.clipboard.d.ts +77 -0
- package/dist/files/assets.clipboard.js +152 -0
- package/dist/files/files.svelte.d.ts +321 -19
- package/dist/files/files.svelte.js +847 -43
- package/dist/files/index.d.ts +3 -1
- package/dist/files/index.js +3 -1
- package/dist/files/outbox.d.ts +69 -0
- package/dist/files/outbox.js +48 -0
- package/dist/files/status.d.ts +65 -0
- package/dist/files/status.js +87 -0
- package/dist/files/store.d.ts +97 -0
- package/dist/files/store.js +65 -0
- package/dist/files/uploads.svelte.d.ts +174 -0
- package/dist/files/uploads.svelte.js +504 -0
- package/dist/index.d.ts +68 -9
- package/dist/index.js +117 -10
- package/dist/nabu-theme.css +21 -1
- package/dist/paste/html.paste.d.ts +74 -3
- package/dist/paste/html.paste.js +550 -86
- package/dist/paste/markdown-inline.d.ts +86 -0
- package/dist/paste/markdown-inline.js +528 -0
- package/dist/paste/markdown.paste.d.ts +2 -8
- package/dist/paste/markdown.paste.js +78 -94
- package/dist/paste/nabu.paste.d.ts +3 -0
- package/dist/paste/nabu.paste.js +29 -3
- package/dist/paste/plain-text.paste.d.ts +2 -2
- package/dist/paste/plain-text.paste.js +3 -3
- package/dist/persistence/asset-store.d.ts +51 -0
- package/dist/persistence/asset-store.js +103 -0
- package/dist/persistence/autosave.d.ts +53 -0
- package/dist/persistence/autosave.js +62 -0
- package/dist/persistence/compact.d.ts +162 -0
- package/dist/persistence/compact.js +141 -0
- package/dist/persistence/history.d.ts +77 -0
- package/dist/persistence/history.js +176 -0
- package/dist/persistence/indexeddb.d.ts +190 -4
- package/dist/persistence/indexeddb.js +333 -29
- package/dist/persistence/merge.d.ts +38 -0
- package/dist/persistence/merge.js +36 -0
- package/dist/persistence/outbox.d.ts +25 -0
- package/dist/persistence/outbox.js +40 -0
- package/dist/persistence/persistence.d.ts +70 -9
- package/dist/persistence/persistence.js +507 -35
- package/dist/persistence/recovery.d.ts +79 -0
- package/dist/persistence/recovery.js +79 -0
- package/dist/persistence/release.d.ts +34 -0
- package/dist/persistence/release.js +64 -0
- package/dist/persistence/state.svelte.d.ts +53 -0
- package/dist/persistence/state.svelte.js +48 -0
- package/dist/persistence/sync.d.ts +39 -0
- package/dist/persistence/sync.js +138 -0
- package/dist/persistence/tail.d.ts +42 -0
- package/dist/persistence/tail.js +103 -0
- package/dist/presets.d.ts +29 -26
- package/dist/presets.js +35 -13
- package/dist/themes/atelier.css +318 -0
- package/dist/ui/CommandChooser.svelte +153 -0
- package/dist/ui/CommandChooser.svelte.d.ts +13 -0
- package/dist/ui/CommandInvite.svelte +99 -0
- package/dist/ui/CommandInvite.svelte.d.ts +12 -0
- package/dist/ui/DragHandle.svelte +97 -21
- package/dist/ui/FloatingToolbar.svelte +75 -27
- package/dist/ui/FloatingToolbar.svelte.d.ts +10 -0
- package/dist/ui/SlashMenu.svelte +147 -15
- package/dist/ui/SlashMenu.svelte.d.ts +20 -0
- package/dist/ui/anchor.d.ts +112 -0
- package/dist/ui/anchor.js +136 -0
- package/dist/ui/index.d.ts +7 -1
- package/dist/ui/index.js +12 -1
- package/dist/ui/slash-menu.svelte.d.ts +79 -7
- package/dist/ui/slash-menu.svelte.js +85 -51
- package/dist/utils/command-confirm.svelte.d.ts +349 -0
- package/dist/utils/command-confirm.svelte.js +673 -0
- package/dist/utils/commands.d.ts +328 -0
- package/dist/utils/commands.js +416 -0
- package/dist/utils/dom-scope.d.ts +14 -0
- package/dist/utils/dom-scope.js +20 -0
- package/dist/utils/drop.d.ts +160 -0
- package/dist/utils/drop.js +305 -0
- package/dist/utils/extensions.d.ts +439 -45
- package/dist/utils/extensions.js +271 -29
- package/dist/utils/html.d.ts +44 -0
- package/dist/utils/html.js +183 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +3 -0
- package/dist/utils/keys.d.ts +120 -0
- package/dist/utils/keys.js +248 -0
- package/dist/utils/links.d.ts +22 -0
- package/dist/utils/links.js +34 -0
- package/dist/utils/placeholders.d.ts +81 -3
- package/dist/utils/placeholders.js +73 -3
- package/dist/utils/selection.svelte.d.ts +47 -19
- package/dist/utils/selection.svelte.js +60 -10
- package/dist/utils/spellcheck.d.ts +9 -0
- package/dist/utils/spellcheck.js +17 -0
- package/dist/utils/tab.d.ts +31 -0
- package/dist/utils/tab.js +117 -0
- package/dist/utils/words.d.ts +7 -0
- package/dist/utils/words.js +62 -0
- package/dist/vite.d.ts +9 -3
- package/dist/vite.js +19 -8
- package/package.json +54 -20
- package/dist/blocks/variable.svelte.d.ts +0 -47
- package/dist/blocks/variable.svelte.js +0 -51
- package/dist/blocks/zone/TemplateEditor.svelte +0 -308
- package/dist/blocks/zone/TemplateEditor.svelte.d.ts +0 -11
- package/dist/blocks/zone/Zone.svelte +0 -272
- package/dist/blocks/zone/Zone.svelte.d.ts +0 -11
- package/dist/blocks/zone/ZoneEditor.svelte +0 -289
- package/dist/blocks/zone/ZoneEditor.svelte.d.ts +0 -11
- package/dist/blocks/zone/ZoneTemplatesList.svelte +0 -188
- package/dist/blocks/zone/ZoneTemplatesList.svelte.d.ts +0 -11
- package/dist/blocks/zone/hooks/onBeforeInput.hook.d.ts +0 -3
- package/dist/blocks/zone/hooks/onBeforeInput.hook.js +0 -30
- package/dist/blocks/zone/index.d.ts +0 -2
- package/dist/blocks/zone/index.js +0 -2
- package/dist/blocks/zone/zone.extension.d.ts +0 -2
- package/dist/blocks/zone/zone.extension.js +0 -237
- package/dist/blocks/zone/zone.svelte.d.ts +0 -221
- package/dist/blocks/zone/zone.svelte.js +0 -608
- package/dist/overlays/context/Context.svelte +0 -182
- package/dist/overlays/context/Context.svelte.d.ts +0 -17
- package/dist/overlays/context/collect.d.ts +0 -17
- package/dist/overlays/context/collect.js +0 -33
- package/dist/overlays/context/context.extension.d.ts +0 -2
- package/dist/overlays/context/context.extension.js +0 -8
- package/dist/overlays/context/context.svelte.d.ts +0 -82
- package/dist/overlays/context/context.svelte.js +0 -159
- package/dist/overlays/context/index.d.ts +0 -3
- package/dist/overlays/context/index.js +0 -3
|
@@ -10,7 +10,8 @@ import CodeComponent from "./Code.svelte";
|
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* onBeforeInput hook for CodeBlock:
|
|
13
|
-
* - Inside a code block: Backspace on empty → transform back to paragraph
|
|
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).
|
|
14
15
|
* - Inside a non-code text block: ``` + Enter → transform to code block.
|
|
15
16
|
*
|
|
16
17
|
* @param {Nabu} nabu @param {InputEvent} event @param {Block} block
|
|
@@ -34,6 +35,18 @@ const onBeforeInput = (nabu, event, block) => {
|
|
|
34
35
|
});
|
|
35
36
|
return BREAK;
|
|
36
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
|
+
}
|
|
37
50
|
return CONTINUE;
|
|
38
51
|
}
|
|
39
52
|
|
|
@@ -55,12 +68,19 @@ const onBeforeInput = (nabu, event, block) => {
|
|
|
55
68
|
return CONTINUE;
|
|
56
69
|
};
|
|
57
70
|
|
|
71
|
+
/**
|
|
72
|
+
* The code block: plain monospace text with a language, the ``` shortcut, the `code:block` action and the "Code" slash entry.
|
|
73
|
+
*/
|
|
58
74
|
export const CodeExtension = extension("code", {
|
|
59
75
|
// @ts-ignore — static-side variance on create() (same pattern as Paragraph/Heading)
|
|
60
76
|
block: CodeBlock,
|
|
61
77
|
component: CodeComponent,
|
|
78
|
+
slashCommands: [
|
|
79
|
+
{ title: 'Code', icon: '<>', aliases: ['code', 'snippet'], group: 'blocks', order: 0, run: (e) => e.exec('code:block') },
|
|
80
|
+
],
|
|
62
81
|
actions: {
|
|
63
|
-
|
|
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' }),
|
|
64
84
|
},
|
|
65
85
|
hooks: {
|
|
66
86
|
onBeforeInput,
|
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
export class CodeBlock extends TextBlock {
|
|
2
|
+
static placeholder: string;
|
|
2
3
|
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
3
4
|
static toMarkdown(pb: import("../../utils/extensions.js").PasteBlock): string;
|
|
4
5
|
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
5
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
|
+
*/
|
|
6
12
|
static markdownRules: {
|
|
7
13
|
priority: number;
|
|
14
|
+
interruptsParagraph: boolean;
|
|
8
15
|
detect: RegExp;
|
|
9
16
|
/**
|
|
10
17
|
* Consume from the opening fence to the closing fence (or EOF).
|
|
@@ -28,8 +35,8 @@ export class CodeBlock extends TextBlock {
|
|
|
28
35
|
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
29
36
|
*/
|
|
30
37
|
static fromHTML(el: Element): import("../../utils/extensions.js").PasteBlock | null;
|
|
31
|
-
/** @param {Nabu} nabu @param {string} type @param {
|
|
32
|
-
static create(nabu: Nabu, type: string, props?:
|
|
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;
|
|
33
40
|
/** @param {Nabu} nabu @param {CodeNode} node */
|
|
34
41
|
constructor(nabu: Nabu, node: CodeNode);
|
|
35
42
|
/** @type {string | null} */
|
|
@@ -2,6 +2,7 @@ import { TextBlock } from "../text-block.svelte.js";
|
|
|
2
2
|
import { LoroText } from "loro-crdt";
|
|
3
3
|
import CodeComponent from "./Code.svelte";
|
|
4
4
|
import { tick } from "svelte";
|
|
5
|
+
import { escapeHtml } from "../../utils/html.js";
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* @import { Nabu, NabuNode } from "../nabu.svelte.js";
|
|
@@ -20,19 +21,33 @@ function pbText(pb) {
|
|
|
20
21
|
return (pb.delta || []).map(op => typeof op.insert === 'string' ? op.insert : '').join('');
|
|
21
22
|
}
|
|
22
23
|
|
|
24
|
+
/** The opening fence of a code block: indent, fence characters, info string. */
|
|
25
|
+
const FENCE_OPEN = /^( {0,3})(`{3,}|~{3,})(.*)$/;
|
|
26
|
+
|
|
23
27
|
/**
|
|
24
|
-
*
|
|
28
|
+
* Whether `line` closes the fence `open` (same character, at least as long, only spaces after it).
|
|
29
|
+
* @param {string} line
|
|
30
|
+
* @param {string} open
|
|
31
|
+
*/
|
|
32
|
+
function isClosingFence(line, open) {
|
|
33
|
+
const m = /^ {0,3}(`+|~+)[ \t]*$/.exec(line);
|
|
34
|
+
return !!m && m[1][0] === open[0] && m[1].length >= open.length;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// @ts-ignore — static-side variance on create() (same pattern as Paragraph/Heading)
|
|
38
|
+
/**
|
|
39
|
+
* A fenced code block as markdown. The fence is longer than any run of backticks in the code (a code block that holds a
|
|
40
|
+
* fence round-trips); the info string is the language, without whitespace or backtick (it could not be read back).
|
|
41
|
+
* @param {string | null | undefined} language
|
|
25
42
|
* @param {string} text
|
|
26
43
|
* @returns {string}
|
|
27
44
|
*/
|
|
28
|
-
function
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
.replace(/>/g, '>');
|
|
45
|
+
function codeToMarkdown(language, text) {
|
|
46
|
+
const longest = Math.max(0, ...(text.match(/`+/g) || []).map((run) => run.length));
|
|
47
|
+
const fence = '`'.repeat(Math.max(3, longest + 1));
|
|
48
|
+
return fence + String(language || '').replace(/[\s`]/g, '') + '\n' + text + '\n' + fence;
|
|
33
49
|
}
|
|
34
50
|
|
|
35
|
-
// @ts-ignore — static-side variance on create() (same pattern as Paragraph/Heading)
|
|
36
51
|
export class CodeBlock extends TextBlock {
|
|
37
52
|
/** @param {Nabu} nabu @param {CodeNode} node */
|
|
38
53
|
constructor(nabu, node) {
|
|
@@ -40,10 +55,7 @@ export class CodeBlock extends TextBlock {
|
|
|
40
55
|
|
|
41
56
|
this.language = node.data.get('language') || null;
|
|
42
57
|
|
|
43
|
-
this.serializers.set('markdown', () =>
|
|
44
|
-
const lang = this.language || '';
|
|
45
|
-
return '```' + lang + '\n' + this.text + '\n```';
|
|
46
|
-
});
|
|
58
|
+
this.serializers.set('markdown', () => codeToMarkdown(this.language, this.text));
|
|
47
59
|
this.serializers.set('json', () => ({
|
|
48
60
|
id: this.id,
|
|
49
61
|
type: 'code',
|
|
@@ -67,10 +79,14 @@ export class CodeBlock extends TextBlock {
|
|
|
67
79
|
transformsEnabled = false;
|
|
68
80
|
/** @type {boolean} */
|
|
69
81
|
marksEnabled = false;
|
|
82
|
+
/** @type {boolean} */
|
|
83
|
+
pastesAsPlainText = true;
|
|
70
84
|
|
|
71
85
|
/** @type {string | null} */
|
|
72
86
|
language = $state(null);
|
|
73
87
|
|
|
88
|
+
static placeholder = 'Code…';
|
|
89
|
+
|
|
74
90
|
component = $derived(this.nabu.components.get("code") || CodeComponent);
|
|
75
91
|
|
|
76
92
|
/**
|
|
@@ -137,8 +153,7 @@ export class CodeBlock extends TextBlock {
|
|
|
137
153
|
|
|
138
154
|
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
139
155
|
static toMarkdown(pb) {
|
|
140
|
-
|
|
141
|
-
return '```' + lang + '\n' + pbText(pb) + '\n```';
|
|
156
|
+
return codeToMarkdown(pb.props?.language, pbText(pb));
|
|
142
157
|
}
|
|
143
158
|
|
|
144
159
|
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
@@ -146,10 +161,17 @@ export class CodeBlock extends TextBlock {
|
|
|
146
161
|
return `<pre><code>${escapeHtml(pbText(pb))}</code></pre>`;
|
|
147
162
|
}
|
|
148
163
|
|
|
164
|
+
/**
|
|
165
|
+
* Fenced code (CommonMark): 3+ backticks or 3+ tildes (up to 3 spaces of indent), an optional info string (the first
|
|
166
|
+
* word is the language), closed by a fence of the SAME character at least as long. An unclosed fence runs to the end.
|
|
167
|
+
* The content is verbatim (no inline parsing, blank lines kept). An indented block (4 spaces) is NOT code.
|
|
168
|
+
*/
|
|
149
169
|
static markdownRules = [
|
|
150
170
|
{
|
|
151
171
|
priority: 30,
|
|
152
|
-
|
|
172
|
+
interruptsParagraph: true,
|
|
173
|
+
// A backtick fence has no backtick in its info string (otherwise it is an inline code span: ```a```).
|
|
174
|
+
detect: /^ {0,3}(?:`{3,}[^`]*|~{3,}.*)$/,
|
|
153
175
|
/**
|
|
154
176
|
* Consume from the opening fence to the closing fence (or EOF).
|
|
155
177
|
* @param {string[]} lines
|
|
@@ -157,8 +179,10 @@ export class CodeBlock extends TextBlock {
|
|
|
157
179
|
* @returns {number}
|
|
158
180
|
*/
|
|
159
181
|
consume(lines, i) {
|
|
182
|
+
const open = FENCE_OPEN.exec(lines[i]);
|
|
183
|
+
if (!open) return 1;
|
|
160
184
|
let j = i + 1;
|
|
161
|
-
while (j < lines.length &&
|
|
185
|
+
while (j < lines.length && !isClosingFence(lines[j], open[2])) j++;
|
|
162
186
|
return Math.min(lines.length, j + 1) - i;
|
|
163
187
|
},
|
|
164
188
|
}
|
|
@@ -169,15 +193,15 @@ export class CodeBlock extends TextBlock {
|
|
|
169
193
|
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
170
194
|
*/
|
|
171
195
|
static fromMarkdown(lines) {
|
|
172
|
-
const
|
|
173
|
-
if (!
|
|
174
|
-
const language =
|
|
175
|
-
// Body is everything between first line and the trailing ``` line (if present)
|
|
196
|
+
const open = FENCE_OPEN.exec(lines[0]);
|
|
197
|
+
if (!open) return null;
|
|
198
|
+
const language = open[3].trim().split(/\s+/)[0] || null;
|
|
176
199
|
let body = lines.slice(1);
|
|
177
|
-
//
|
|
178
|
-
if (body.length > 0 &&
|
|
179
|
-
|
|
180
|
-
|
|
200
|
+
// The closing fence is the last line, when it closes this fence (consume stops at the first closer).
|
|
201
|
+
if (body.length > 0 && isClosingFence(body[body.length - 1], open[2])) body = body.slice(0, -1);
|
|
202
|
+
// An indented opening fence removes up to that many spaces from the content lines.
|
|
203
|
+
const indent = open[1].length;
|
|
204
|
+
if (indent) body = body.map((line) => line.replace(new RegExp('^ {0,' + indent + '}'), ''));
|
|
181
205
|
const bodyText = body.join('\n');
|
|
182
206
|
// Empty body is still a valid code block
|
|
183
207
|
if (!bodyText && body.length === 0 && lines.length === 1) return null;
|
|
@@ -208,9 +232,9 @@ export class CodeBlock extends TextBlock {
|
|
|
208
232
|
};
|
|
209
233
|
}
|
|
210
234
|
|
|
211
|
-
/** @param {Nabu} nabu @param {string} type @param {
|
|
235
|
+
/** @param {Nabu} nabu @param {string} type @param {Record<string, any>} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
212
236
|
static create(nabu, type, props = {}, parentId = null, index = null) {
|
|
213
|
-
const node = nabu.tree.createNode(/** @type {
|
|
237
|
+
const node = nabu.tree.createNode(/** @type {import('loro-crdt').TreeID | undefined} */ (parentId || undefined), index ?? undefined);
|
|
214
238
|
/** @type {any} */ (node.data).set('type', 'code');
|
|
215
239
|
if (props.language) /** @type {any} */ (node.data).set('language', props.language);
|
|
216
240
|
const container = /** @type {any} */ (node.data).setContainer('text', new LoroText());
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { Block } from './block.svelte.js';
|
|
3
|
+
* @import { Nabu } from './nabu.svelte.js';
|
|
4
|
+
*/
|
|
1
5
|
/**
|
|
2
6
|
* Walks up from `block` toward `container` and returns the direct child of
|
|
3
7
|
* `container` that is (or contains) `block`.
|
|
@@ -38,18 +42,65 @@ export function findLCA(blockA: Block, blockB: Block): Block | null;
|
|
|
38
42
|
*/
|
|
39
43
|
export function wrapOrphan(nabu: Nabu, block: Block): Block | undefined;
|
|
40
44
|
/**
|
|
41
|
-
*
|
|
45
|
+
* The text blocks of the document in document order (a block's own text before its children). With `visible`, the children of
|
|
46
|
+
* a block that hides them are skipped (they are not rendered: no caret, no DOM point).
|
|
47
|
+
* @param {Nabu} nabu
|
|
48
|
+
* @param {{ visible?: boolean }} [options]
|
|
49
|
+
* @returns {Block[]}
|
|
50
|
+
*/
|
|
51
|
+
export function textBlocksInOrder(nabu: Nabu, { visible }?: {
|
|
52
|
+
visible?: boolean;
|
|
53
|
+
}): Block[];
|
|
54
|
+
/**
|
|
55
|
+
* The ISOLATION OWNER of a block: its nearest ancestor that isolates its text (`Block#isolatesSelection`), or null. Two text
|
|
56
|
+
* blocks with different owners (the body of a chapter and the body of a scene inside it) never merge by a range deletion.
|
|
57
|
+
* @param {Block} block
|
|
58
|
+
* @returns {Block | null}
|
|
59
|
+
*/
|
|
60
|
+
export function isolationOwnerOf(block: Block): Block | null;
|
|
61
|
+
/**
|
|
62
|
+
* Splits a range into the RUNS that a deletion may act on (ADR 018 §3.9): maximal sequences of consecutive text blocks, in
|
|
63
|
+
* document order, that do not contain an isolated block and share one isolation owner. The text of an isolated block is
|
|
64
|
+
* outside every run (a range starting or ending in it starts after it / ends before it), and a run boundary is never merged
|
|
65
|
+
* across. A document without isolated blocks gives a single run, the range itself.
|
|
66
|
+
* @param {Nabu} nabu
|
|
67
|
+
* @param {DeletionRange} range
|
|
68
|
+
* @returns {DeletionRange[] | null} the runs in document order (possibly empty), or null when the range cannot be placed in the document
|
|
69
|
+
*/
|
|
70
|
+
export function isolatedRuns(nabu: Nabu, range: DeletionRange): DeletionRange[] | null;
|
|
71
|
+
/**
|
|
72
|
+
* THE one implementation of "delete the selected range across blocks" (cut, Backspace / Delete, typing over a
|
|
73
|
+
* selection, paste over a selection, move by drag). Mutates the Loro tree and texts, never commits.
|
|
42
74
|
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
75
|
+
* A range that crosses ISOLATED text (`Block#isolatesSelection`, e.g. the title of a region) acts on its {@link isolatedRuns}
|
|
76
|
+
* only: the text of an isolated block is never touched, an isolated block and its container are never deleted, and the runs
|
|
77
|
+
* are never merged with one another (each one is deleted like a range of its own, the LAST run first so that the earlier
|
|
78
|
+
* ones are not disturbed). What is left is the first block of every run, emptied: that is where a caret still has a home.
|
|
79
|
+
* Without isolated block in the range it is exactly {@link deleteRangeRaw}.
|
|
46
80
|
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
81
|
+
* @param {Nabu} nabu
|
|
82
|
+
* @param {DeletionRange} range
|
|
83
|
+
* @returns {{ block: Block, offset: number } | null} the junction (where the caret belongs: the start of the first run), or null if the range is unusable or holds no deletable text
|
|
84
|
+
*/
|
|
85
|
+
export function deleteRange(nabu: Nabu, range: DeletionRange): {
|
|
86
|
+
block: Block;
|
|
87
|
+
offset: number;
|
|
88
|
+
} | null;
|
|
89
|
+
/**
|
|
90
|
+
* The current selection as a {@link DeletionRange}, or null when there is none.
|
|
91
|
+
* A selection inside one block has `startBlock === endBlock`.
|
|
92
|
+
* @param {Nabu} nabu
|
|
93
|
+
* @returns {DeletionRange | null}
|
|
94
|
+
*/
|
|
95
|
+
export function selectionRange(nabu: Nabu): DeletionRange | null;
|
|
96
|
+
/**
|
|
97
|
+
* Shared beforeinput logic for block containers (Nabu root and MegaBlock) when the selection spans SEVERAL
|
|
98
|
+
* blocks (a selection inside one block is that block's business). Backspace / Delete / word deletions delete the
|
|
99
|
+
* range; typing replaces it; Enter replaces it and splits at the junction. All of it is {@link deleteRange}
|
|
100
|
+
* followed by the plain single-block edit at the junction, then ONE commit (one undo step).
|
|
101
|
+
*
|
|
102
|
+
* The `container` (the lowest common ancestor the event was routed to) is not needed any more: the range is
|
|
103
|
+
* resolved from the selection alone, which also makes the same code correct for every depth.
|
|
53
104
|
*
|
|
54
105
|
* @param {{ children: Block[], commit: () => void }} container
|
|
55
106
|
* @param {Nabu} nabu
|
|
@@ -60,11 +111,11 @@ export function handleContainerBeforeInput(container: {
|
|
|
60
111
|
commit: () => void;
|
|
61
112
|
}, nabu: Nabu, event: InputEvent): any;
|
|
62
113
|
/**
|
|
63
|
-
* Deletes the current selection content without committing.
|
|
64
|
-
* Handles both single-block and multi-block selections.
|
|
114
|
+
* Deletes the current selection content without committing (cut, paste over a selection).
|
|
115
|
+
* Handles both single-block and multi-block selections — see {@link deleteRange}.
|
|
65
116
|
* Returns the resulting insertion position {block, offset}, or null on failure.
|
|
66
117
|
*
|
|
67
|
-
* @param {{ children: import('./block.svelte.js').Block[] }} container
|
|
118
|
+
* @param {{ children: import('./block.svelte.js').Block[] }} container (unused: the range comes from the selection)
|
|
68
119
|
* @param {import('./nabu.svelte.js').Nabu} nabu
|
|
69
120
|
* @returns {{ block: import('./block.svelte.js').Block, offset: number } | null}
|
|
70
121
|
*/
|
|
@@ -74,5 +125,26 @@ export function deleteSelectionContent(container: {
|
|
|
74
125
|
block: import("./block.svelte.js").Block;
|
|
75
126
|
offset: number;
|
|
76
127
|
} | null;
|
|
128
|
+
/**
|
|
129
|
+
* A selected range of the document, as model positions.
|
|
130
|
+
*/
|
|
131
|
+
export type DeletionRange = {
|
|
132
|
+
/**
|
|
133
|
+
* Block holding the start of the range (earlier in document order).
|
|
134
|
+
*/
|
|
135
|
+
startBlock: Block;
|
|
136
|
+
/**
|
|
137
|
+
* Text offset of the start inside `startBlock`.
|
|
138
|
+
*/
|
|
139
|
+
startOffset: number;
|
|
140
|
+
/**
|
|
141
|
+
* Block holding the end of the range.
|
|
142
|
+
*/
|
|
143
|
+
endBlock: Block;
|
|
144
|
+
/**
|
|
145
|
+
* Text offset of the end inside `endBlock`.
|
|
146
|
+
*/
|
|
147
|
+
endOffset: number;
|
|
148
|
+
};
|
|
77
149
|
import type { Block } from './block.svelte.js';
|
|
78
150
|
import type { Nabu } from './nabu.svelte.js';
|