@aionbuilders/nabu 0.1.0-alpha.8 → 0.1.0-beta.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/README.md +216 -9
- package/dist/behaviors/text/RichText.svelte +4 -1
- 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 +2 -0
- package/dist/behaviors/text/markdown-shortcuts.extension.js +54 -0
- package/dist/behaviors/text/rich-text.extension.js +47 -22
- package/dist/behaviors/text/text.behavior.svelte.d.ts +25 -1
- package/dist/behaviors/text/text.behavior.svelte.js +154 -18
- package/dist/behaviors/text/typography.extension.d.ts +6 -0
- package/dist/behaviors/text/typography.extension.js +24 -0
- package/dist/blocks/Block.svelte +5 -2
- package/dist/blocks/BlockGap.svelte +55 -0
- package/dist/blocks/BlockGap.svelte.d.ts +11 -0
- package/dist/blocks/Nabu.svelte +22 -0
- package/dist/blocks/block.extension.js +10 -8
- package/dist/blocks/block.svelte.d.ts +24 -0
- package/dist/blocks/block.svelte.js +35 -0
- package/dist/blocks/blockquote/Blockquote.svelte +40 -0
- package/dist/blocks/blockquote/Blockquote.svelte.d.ts +11 -0
- package/dist/blocks/blockquote/blockquote.extension.d.ts +1 -0
- package/dist/blocks/blockquote/blockquote.extension.js +71 -0
- package/dist/blocks/blockquote/blockquote.svelte.d.ts +55 -0
- package/dist/blocks/blockquote/blockquote.svelte.js +91 -0
- package/dist/blocks/blockquote/index.d.ts +3 -0
- package/dist/blocks/blockquote/index.js +3 -0
- package/dist/blocks/callout/Callout.svelte +69 -0
- package/dist/blocks/callout/Callout.svelte.d.ts +11 -0
- package/dist/blocks/callout/callout.extension.d.ts +1 -0
- package/dist/blocks/callout/callout.extension.js +68 -0
- package/dist/blocks/callout/callout.svelte.d.ts +78 -0
- package/dist/blocks/callout/callout.svelte.js +184 -0
- package/dist/blocks/callout/index.d.ts +3 -0
- package/dist/blocks/callout/index.js +3 -0
- package/dist/blocks/code/Code.svelte +85 -0
- package/dist/blocks/code/Code.svelte.d.ts +11 -0
- package/dist/blocks/code/code.extension.d.ts +1 -0
- package/dist/blocks/code/code.extension.js +68 -0
- package/dist/blocks/code/code.svelte.d.ts +61 -0
- package/dist/blocks/code/code.svelte.js +221 -0
- package/dist/blocks/code/index.d.ts +3 -0
- package/dist/blocks/code/index.js +3 -0
- package/dist/blocks/dialogue/Dialogue.svelte +2 -0
- package/dist/blocks/dialogue/dialogue.svelte.d.ts +3 -21
- package/dist/blocks/dialogue/dialogue.svelte.js +2 -50
- 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 +1 -0
- package/dist/blocks/divider/divider.extension.js +87 -0
- package/dist/blocks/divider/divider.svelte.d.ts +49 -0
- package/dist/blocks/divider/divider.svelte.js +84 -0
- package/dist/blocks/divider/index.d.ts +3 -0
- package/dist/blocks/divider/index.js +3 -0
- package/dist/blocks/file/File.svelte +49 -0
- package/dist/blocks/file/File.svelte.d.ts +11 -0
- package/dist/blocks/file/file.extension.d.ts +1 -0
- package/dist/blocks/file/file.extension.js +19 -0
- package/dist/blocks/file/file.svelte.d.ts +29 -0
- package/dist/blocks/file/file.svelte.js +71 -0
- package/dist/blocks/file/index.d.ts +3 -0
- package/dist/blocks/file/index.js +3 -0
- package/dist/blocks/heading/Heading.svelte +3 -1
- package/dist/blocks/heading/heading.svelte.d.ts +4 -23
- package/dist/blocks/heading/heading.svelte.js +4 -47
- package/dist/blocks/image/Image.svelte +38 -0
- package/dist/blocks/image/Image.svelte.d.ts +11 -0
- package/dist/blocks/image/image.extension.d.ts +2 -0
- package/dist/blocks/image/image.extension.js +26 -0
- package/dist/blocks/image/image.svelte.d.ts +50 -0
- package/dist/blocks/image/image.svelte.js +92 -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 -0
- package/dist/blocks/index.js +11 -0
- package/dist/blocks/list/List.svelte +8 -2
- package/dist/blocks/list/ListItem.svelte +43 -7
- package/dist/blocks/list/hooks/onBeforeInput.hook.js +15 -2
- package/dist/blocks/list/index.d.ts +1 -0
- package/dist/blocks/list/index.js +1 -0
- package/dist/blocks/list/list-item.svelte.d.ts +14 -2
- package/dist/blocks/list/list-item.svelte.js +58 -7
- package/dist/blocks/list/list.behavior.svelte.d.ts +4 -4
- package/dist/blocks/list/list.behavior.svelte.js +5 -5
- package/dist/blocks/list/list.svelte.d.ts +13 -6
- package/dist/blocks/list/list.svelte.js +24 -6
- 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/media-block.svelte.d.ts +32 -0
- package/dist/blocks/media/media-block.svelte.js +69 -0
- package/dist/blocks/media/media.helpers.d.ts +19 -0
- package/dist/blocks/media/media.helpers.js +51 -0
- package/dist/blocks/megablock.svelte.js +1 -0
- package/dist/blocks/nabu.svelte.d.ts +94 -16
- package/dist/blocks/nabu.svelte.js +254 -3
- package/dist/blocks/paragraph/Paragraph.svelte +2 -0
- package/dist/blocks/paragraph/index.js +8 -3
- package/dist/blocks/paragraph/paragraph.svelte.d.ts +3 -21
- package/dist/blocks/paragraph/paragraph.svelte.js +4 -53
- package/dist/blocks/selection.svelte.d.ts +1 -0
- package/dist/blocks/selection.svelte.js +212 -16
- package/dist/blocks/text-block.svelte.d.ts +49 -0
- package/dist/blocks/text-block.svelte.js +74 -0
- package/dist/blocks/toggle/Toggle.svelte +84 -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/toggle.extension.d.ts +1 -0
- package/dist/blocks/toggle/toggle.extension.js +12 -0
- package/dist/blocks/toggle/toggle.svelte.d.ts +88 -0
- package/dist/blocks/toggle/toggle.svelte.js +252 -0
- package/dist/blocks/variable.svelte.d.ts +47 -0
- package/dist/blocks/variable.svelte.js +51 -0
- package/dist/blocks/video/Video.svelte +39 -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 +1 -0
- package/dist/blocks/video/video.extension.js +19 -0
- package/dist/blocks/video/video.svelte.d.ts +28 -0
- package/dist/blocks/video/video.svelte.js +71 -0
- package/dist/blocks/zone/TemplateEditor.svelte +308 -0
- package/dist/blocks/zone/TemplateEditor.svelte.d.ts +11 -0
- package/dist/blocks/zone/Zone.svelte +272 -0
- package/dist/blocks/zone/Zone.svelte.d.ts +11 -0
- package/dist/blocks/zone/ZoneEditor.svelte +289 -0
- package/dist/blocks/zone/ZoneEditor.svelte.d.ts +11 -0
- package/dist/blocks/zone/ZoneTemplatesList.svelte +188 -0
- package/dist/blocks/zone/ZoneTemplatesList.svelte.d.ts +11 -0
- package/dist/blocks/zone/hooks/onBeforeInput.hook.d.ts +3 -0
- package/dist/blocks/zone/hooks/onBeforeInput.hook.js +30 -0
- package/dist/blocks/zone/index.d.ts +2 -0
- package/dist/blocks/zone/index.js +2 -0
- package/dist/blocks/zone/zone.extension.d.ts +2 -0
- package/dist/blocks/zone/zone.extension.js +237 -0
- package/dist/blocks/zone/zone.svelte.d.ts +221 -0
- package/dist/blocks/zone/zone.svelte.js +608 -0
- package/dist/files/files.svelte.d.ts +61 -0
- package/dist/files/files.svelte.js +93 -0
- package/dist/files/index.d.ts +1 -0
- package/dist/files/index.js +1 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +5 -2
- package/dist/nabu-theme.css +92 -0
- package/dist/overlays/context/Context.svelte +182 -0
- package/dist/overlays/context/Context.svelte.d.ts +17 -0
- package/dist/overlays/context/collect.d.ts +17 -0
- package/dist/overlays/context/collect.js +33 -0
- package/dist/overlays/context/context.extension.d.ts +2 -0
- package/dist/overlays/context/context.extension.js +8 -0
- package/dist/overlays/context/context.svelte.d.ts +82 -0
- package/dist/overlays/context/context.svelte.js +159 -0
- package/dist/overlays/context/index.d.ts +3 -0
- package/dist/overlays/context/index.js +3 -0
- package/dist/paste/html.paste.js +9 -1
- package/dist/presets.d.ts +3 -1
- package/dist/presets.js +22 -6
- package/dist/ui/DragHandle.svelte +141 -0
- package/dist/ui/DragHandle.svelte.d.ts +11 -0
- package/dist/ui/FloatingToolbar.svelte +117 -0
- package/dist/ui/FloatingToolbar.svelte.d.ts +17 -0
- package/dist/ui/SlashMenu.svelte +83 -0
- package/dist/ui/SlashMenu.svelte.d.ts +13 -0
- package/dist/ui/index.d.ts +4 -0
- package/dist/ui/index.js +4 -0
- package/dist/ui/slash-menu.svelte.d.ts +40 -0
- package/dist/ui/slash-menu.svelte.js +116 -0
- package/dist/utils/extensions.d.ts +54 -0
- package/dist/utils/extensions.js +40 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/placeholders.d.ts +10 -0
- package/dist/utils/placeholders.js +21 -0
- package/dist/vite.d.ts +16 -0
- package/dist/vite.js +23 -0
- package/package.json +10 -5
|
@@ -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 @@
|
|
|
1
|
+
export const CodeExtension: import("../..").Extension;
|
|
@@ -0,0 +1,68 @@
|
|
|
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.
|
|
14
|
+
* - Inside a non-code text block: ``` + Enter → transform to code block.
|
|
15
|
+
*
|
|
16
|
+
* @param {Nabu} nabu @param {InputEvent} event @param {Block} block
|
|
17
|
+
*/
|
|
18
|
+
const onBeforeInput = (nabu, event, block) => {
|
|
19
|
+
const { BREAK, CONTINUE } = nabu;
|
|
20
|
+
|
|
21
|
+
const behavior = block.behaviors.get('text');
|
|
22
|
+
if (!(behavior instanceof TextBehavior)) return CONTINUE;
|
|
23
|
+
const sel = behavior.selection;
|
|
24
|
+
if (!sel || !sel.isCollapsed) return CONTINUE;
|
|
25
|
+
|
|
26
|
+
if (block instanceof CodeBlock) {
|
|
27
|
+
// Backspace at position 0 in an empty code block → back to paragraph
|
|
28
|
+
if (event.inputType === 'deleteContentBackward' && sel.from === 0 && behavior.text.length === 0) {
|
|
29
|
+
const blockId = block.node.id.toString();
|
|
30
|
+
block.transformTo('paragraph');
|
|
31
|
+
tick().then(() => {
|
|
32
|
+
const fresh = nabu.blocks.get(blockId);
|
|
33
|
+
if (fresh) fresh.focus({ start: 0, end: 0 });
|
|
34
|
+
});
|
|
35
|
+
return BREAK;
|
|
36
|
+
}
|
|
37
|
+
return CONTINUE;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// ``` + Enter in a non-code text block → transform to code
|
|
41
|
+
if (event.inputType === 'insertParagraph') {
|
|
42
|
+
const m = behavior.text.match(/^```(\w*)$/);
|
|
43
|
+
if (!m) return CONTINUE;
|
|
44
|
+
const language = m[1] || null;
|
|
45
|
+
behavior.delete({ index: 0, length: behavior.text.length });
|
|
46
|
+
const blockId = block.node.id.toString();
|
|
47
|
+
block.transformTo('code', language ? { language } : {});
|
|
48
|
+
tick().then(() => {
|
|
49
|
+
const fresh = nabu.blocks.get(blockId);
|
|
50
|
+
if (fresh) fresh.focus({ start: 0, end: 0 });
|
|
51
|
+
});
|
|
52
|
+
return BREAK;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return CONTINUE;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export const CodeExtension = extension("code", {
|
|
59
|
+
// @ts-ignore — static-side variance on create() (same pattern as Paragraph/Heading)
|
|
60
|
+
block: CodeBlock,
|
|
61
|
+
component: CodeComponent,
|
|
62
|
+
actions: {
|
|
63
|
+
'code': (nabu) => nabu.exec('block:transform', { type: 'code' }),
|
|
64
|
+
},
|
|
65
|
+
hooks: {
|
|
66
|
+
onBeforeInput,
|
|
67
|
+
}
|
|
68
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export class CodeBlock extends TextBlock {
|
|
2
|
+
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
3
|
+
static toMarkdown(pb: import("../../utils/extensions.js").PasteBlock): string;
|
|
4
|
+
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
5
|
+
static toHtml(pb: import("../../utils/extensions.js").PasteBlock): string;
|
|
6
|
+
static markdownRules: {
|
|
7
|
+
priority: number;
|
|
8
|
+
detect: RegExp;
|
|
9
|
+
/**
|
|
10
|
+
* Consume from the opening fence to the closing fence (or EOF).
|
|
11
|
+
* @param {string[]} lines
|
|
12
|
+
* @param {number} i
|
|
13
|
+
* @returns {number}
|
|
14
|
+
*/
|
|
15
|
+
consume(lines: string[], i: number): number;
|
|
16
|
+
}[];
|
|
17
|
+
/**
|
|
18
|
+
* @param {string[]} lines
|
|
19
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
20
|
+
*/
|
|
21
|
+
static fromMarkdown(lines: string[]): import("../../utils/extensions.js").PasteBlock | null;
|
|
22
|
+
static htmlRules: {
|
|
23
|
+
selector: string;
|
|
24
|
+
priority: number;
|
|
25
|
+
}[];
|
|
26
|
+
/**
|
|
27
|
+
* @param {Element} el
|
|
28
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
29
|
+
*/
|
|
30
|
+
static fromHTML(el: Element): import("../../utils/extensions.js").PasteBlock | null;
|
|
31
|
+
/** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
32
|
+
static create(nabu: Nabu, type: string, props?: Object, parentId?: string | null, index?: number | null): CodeBlock;
|
|
33
|
+
/** @param {Nabu} nabu @param {CodeNode} node */
|
|
34
|
+
constructor(nabu: Nabu, node: CodeNode);
|
|
35
|
+
/** @type {string | null} */
|
|
36
|
+
language: string | null;
|
|
37
|
+
component: import("svelte").Component<any, any, string> | import("svelte").Component<{
|
|
38
|
+
block: import("./code.svelte.js").CodeBlock;
|
|
39
|
+
}, {}, "">;
|
|
40
|
+
/**
|
|
41
|
+
* Set the code language and commit.
|
|
42
|
+
* @param {string | null} lang
|
|
43
|
+
*/
|
|
44
|
+
setLanguage(lang: string | null): void;
|
|
45
|
+
/**
|
|
46
|
+
* Handle special keydown events: Tab, Shift+Tab, ArrowDown.
|
|
47
|
+
* Returns true if handled (engine should preventDefault).
|
|
48
|
+
* @param {KeyboardEvent} event
|
|
49
|
+
* @returns {boolean}
|
|
50
|
+
*/
|
|
51
|
+
keydown(event: KeyboardEvent): boolean;
|
|
52
|
+
}
|
|
53
|
+
export type CodeNode = NabuNode<{
|
|
54
|
+
type: "code";
|
|
55
|
+
text: LoroText;
|
|
56
|
+
language: string | null;
|
|
57
|
+
}>;
|
|
58
|
+
import { TextBlock } from "../text-block.svelte.js";
|
|
59
|
+
import type { Nabu } from "../nabu.svelte.js";
|
|
60
|
+
import { LoroText } from "loro-crdt";
|
|
61
|
+
import type { NabuNode } from "../nabu.svelte.js";
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { TextBlock } from "../text-block.svelte.js";
|
|
2
|
+
import { LoroText } from "loro-crdt";
|
|
3
|
+
import CodeComponent from "./Code.svelte";
|
|
4
|
+
import { tick } from "svelte";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @import { Nabu, NabuNode } from "../nabu.svelte.js";
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {NabuNode<{type: "code", text: LoroText, language: string | null}>} CodeNode
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Plain-text content of a PasteBlock (joins all string inserts, ignores embeds).
|
|
16
|
+
* @param {import('../../utils/extensions.js').PasteBlock} pb
|
|
17
|
+
* @returns {string}
|
|
18
|
+
*/
|
|
19
|
+
function pbText(pb) {
|
|
20
|
+
return (pb.delta || []).map(op => typeof op.insert === 'string' ? op.insert : '').join('');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Escape special HTML characters.
|
|
25
|
+
* @param {string} text
|
|
26
|
+
* @returns {string}
|
|
27
|
+
*/
|
|
28
|
+
function escapeHtml(text) {
|
|
29
|
+
return text
|
|
30
|
+
.replace(/&/g, '&')
|
|
31
|
+
.replace(/</g, '<')
|
|
32
|
+
.replace(/>/g, '>');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// @ts-ignore — static-side variance on create() (same pattern as Paragraph/Heading)
|
|
36
|
+
export class CodeBlock extends TextBlock {
|
|
37
|
+
/** @param {Nabu} nabu @param {CodeNode} node */
|
|
38
|
+
constructor(nabu, node) {
|
|
39
|
+
super(nabu, node);
|
|
40
|
+
|
|
41
|
+
this.language = node.data.get('language') || null;
|
|
42
|
+
|
|
43
|
+
this.serializers.set('markdown', () => {
|
|
44
|
+
const lang = this.language || '';
|
|
45
|
+
return '```' + lang + '\n' + this.text + '\n```';
|
|
46
|
+
});
|
|
47
|
+
this.serializers.set('json', () => ({
|
|
48
|
+
id: this.id,
|
|
49
|
+
type: 'code',
|
|
50
|
+
props: { language: this.language },
|
|
51
|
+
content: this.text,
|
|
52
|
+
}));
|
|
53
|
+
this.serializers.set('application/x-nabu+json', (ctx = {}) =>
|
|
54
|
+
this.behavior.toClipboardBlock({ ...ctx, props: { language: this.language } })
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
// Sync language from Loro data changes (mirrors Heading's level sync)
|
|
58
|
+
this.node.data.subscribe(() => {
|
|
59
|
+
this.language = this.node.data.get('language') || null;
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// ── Capability flags ─────────────────────────────────────────────────────
|
|
64
|
+
/** @type {boolean} */
|
|
65
|
+
splitsOnEnter = false;
|
|
66
|
+
/** @type {boolean} */
|
|
67
|
+
transformsEnabled = false;
|
|
68
|
+
/** @type {boolean} */
|
|
69
|
+
marksEnabled = false;
|
|
70
|
+
|
|
71
|
+
/** @type {string | null} */
|
|
72
|
+
language = $state(null);
|
|
73
|
+
|
|
74
|
+
component = $derived(this.nabu.components.get("code") || CodeComponent);
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Set the code language and commit.
|
|
78
|
+
* @param {string | null} lang
|
|
79
|
+
*/
|
|
80
|
+
setLanguage(lang) {
|
|
81
|
+
this.language = lang || null;
|
|
82
|
+
/** @type {any} */ (this.node.data).set('language', lang || null);
|
|
83
|
+
this.commit();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Handle special keydown events: Tab, Shift+Tab, ArrowDown.
|
|
88
|
+
* Returns true if handled (engine should preventDefault).
|
|
89
|
+
* @param {KeyboardEvent} event
|
|
90
|
+
* @returns {boolean}
|
|
91
|
+
*/
|
|
92
|
+
keydown(event) {
|
|
93
|
+
const sel = this.behavior.selection;
|
|
94
|
+
if (!sel) return false;
|
|
95
|
+
|
|
96
|
+
// Tab without shift — insert 2 spaces
|
|
97
|
+
if (event.key === 'Tab' && !event.shiftKey) {
|
|
98
|
+
if (!sel.isCollapsed) this.behavior.delete({ index: sel.from, length: sel.to - sel.from });
|
|
99
|
+
this.behavior.insert(sel.from, ' ');
|
|
100
|
+
this.commit();
|
|
101
|
+
tick().then(() => this.nabu.selection.setCursor(this, sel.from + 2));
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Shift+Tab — outdent (remove up to 2 leading spaces on current line)
|
|
106
|
+
if (event.key === 'Tab' && event.shiftKey) {
|
|
107
|
+
const ls = this.text.lastIndexOf('\n', sel.from - 1) + 1;
|
|
108
|
+
let n = 0;
|
|
109
|
+
while (n < 2 && this.text[ls + n] === ' ') n++;
|
|
110
|
+
if (n > 0) {
|
|
111
|
+
this.behavior.delete({ index: ls, length: n });
|
|
112
|
+
this.commit();
|
|
113
|
+
tick().then(() => this.nabu.selection.setCursor(this, Math.max(ls, sel.from - n)));
|
|
114
|
+
}
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ArrowDown (no modifiers, collapsed) — escape code block by creating a paragraph after
|
|
119
|
+
if (event.key === 'ArrowDown' && !event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey && sel.isCollapsed) {
|
|
120
|
+
const onLastLine = this.text.indexOf('\n', sel.from) === -1;
|
|
121
|
+
const hasNextTextBlock = !!this.findForward(b => b.behaviors?.has('text'));
|
|
122
|
+
if (onLastLine && !hasNextTextBlock) {
|
|
123
|
+
const p = this.nabu.insert('paragraph', {}, this.node.parent()?.id.toString() || null, (this.node.index() ?? 0) + 1);
|
|
124
|
+
this.nabu.commit().then(() => {
|
|
125
|
+
const fresh = this.nabu.blocks.get(p.id);
|
|
126
|
+
if (fresh) this.nabu.selection.setCursor(fresh, 0);
|
|
127
|
+
});
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// ── Statics ──────────────────────────────────────────────────────────────
|
|
137
|
+
|
|
138
|
+
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
139
|
+
static toMarkdown(pb) {
|
|
140
|
+
const lang = pb.props?.language || '';
|
|
141
|
+
return '```' + lang + '\n' + pbText(pb) + '\n```';
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
145
|
+
static toHtml(pb) {
|
|
146
|
+
return `<pre><code>${escapeHtml(pbText(pb))}</code></pre>`;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
static markdownRules = [
|
|
150
|
+
{
|
|
151
|
+
priority: 30,
|
|
152
|
+
detect: /^```/,
|
|
153
|
+
/**
|
|
154
|
+
* Consume from the opening fence to the closing fence (or EOF).
|
|
155
|
+
* @param {string[]} lines
|
|
156
|
+
* @param {number} i
|
|
157
|
+
* @returns {number}
|
|
158
|
+
*/
|
|
159
|
+
consume(lines, i) {
|
|
160
|
+
let j = i + 1;
|
|
161
|
+
while (j < lines.length && !/^```\s*$/.test(lines[j])) j++;
|
|
162
|
+
return Math.min(lines.length, j + 1) - i;
|
|
163
|
+
},
|
|
164
|
+
}
|
|
165
|
+
];
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* @param {string[]} lines
|
|
169
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
170
|
+
*/
|
|
171
|
+
static fromMarkdown(lines) {
|
|
172
|
+
const m = lines[0].match(/^```(\w*)/);
|
|
173
|
+
if (!m) return null;
|
|
174
|
+
const language = m[1] || null;
|
|
175
|
+
// Body is everything between first line and the trailing ``` line (if present)
|
|
176
|
+
let body = lines.slice(1);
|
|
177
|
+
// Strip trailing closing fence if present
|
|
178
|
+
if (body.length > 0 && /^```\s*$/.test(body[body.length - 1])) {
|
|
179
|
+
body = body.slice(0, -1);
|
|
180
|
+
}
|
|
181
|
+
const bodyText = body.join('\n');
|
|
182
|
+
// Empty body is still a valid code block
|
|
183
|
+
if (!bodyText && body.length === 0 && lines.length === 1) return null;
|
|
184
|
+
return {
|
|
185
|
+
type: 'code',
|
|
186
|
+
props: { language },
|
|
187
|
+
delta: [{ insert: bodyText }],
|
|
188
|
+
partial: false,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
static htmlRules = [
|
|
193
|
+
{ selector: 'pre', priority: 10 },
|
|
194
|
+
];
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* @param {Element} el
|
|
198
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
199
|
+
*/
|
|
200
|
+
static fromHTML(el) {
|
|
201
|
+
const text = el.textContent || '';
|
|
202
|
+
const language = el.querySelector('code')?.className.match(/language-(\w+)/)?.[1] || null;
|
|
203
|
+
return {
|
|
204
|
+
type: 'code',
|
|
205
|
+
props: { language },
|
|
206
|
+
delta: [{ insert: text }],
|
|
207
|
+
partial: false,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
212
|
+
static create(nabu, type, props = {}, parentId = null, index = null) {
|
|
213
|
+
const node = nabu.tree.createNode(/** @type {any} */ (parentId || undefined), index || undefined);
|
|
214
|
+
/** @type {any} */ (node.data).set('type', 'code');
|
|
215
|
+
if (props.language) /** @type {any} */ (node.data).set('language', props.language);
|
|
216
|
+
const container = /** @type {any} */ (node.data).setContainer('text', new LoroText());
|
|
217
|
+
if (props.text) container.insert(0, props.text);
|
|
218
|
+
if (props.delta) container.applyDelta([...props.delta]);
|
|
219
|
+
return new CodeBlock(nabu, /** @type {CodeNode} */ (/** @type {unknown} */ (node)));
|
|
220
|
+
}
|
|
221
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
<script>
|
|
2
2
|
import RichText from '../../behaviors/text/RichText.svelte';
|
|
3
|
+
import { getPlaceholder } from '../../utils/placeholders.js';
|
|
3
4
|
/** @type {{block: import('./dialogue.svelte.js').Dialogue}}*/
|
|
4
5
|
let {block} = $props();
|
|
5
6
|
</script>
|
|
@@ -8,6 +9,7 @@
|
|
|
8
9
|
bind:this={block.element}
|
|
9
10
|
data-block-id={block.id}
|
|
10
11
|
data-block-type="dialogue"
|
|
12
|
+
data-placeholder={block.text ? '' : getPlaceholder('dialogue')}
|
|
11
13
|
class="nabu-dialogue"
|
|
12
14
|
class:debug={block.nabu.debugging}
|
|
13
15
|
class:selected={block.selected}
|
|
@@ -4,38 +4,20 @@
|
|
|
4
4
|
/**
|
|
5
5
|
* @typedef {NabuNode<{type: "dialogue", text: LoroText}>} DialogueNode
|
|
6
6
|
*/
|
|
7
|
-
export class Dialogue extends
|
|
7
|
+
export class Dialogue extends TextBlock {
|
|
8
8
|
/** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
9
9
|
static create(nabu: Nabu, type: string, props?: Object, parentId?: string | null, index?: number | null): Dialogue;
|
|
10
10
|
/** @param {Nabu} nabu @param {DialogueNode} node */
|
|
11
11
|
constructor(nabu: Nabu, node: DialogueNode);
|
|
12
|
-
container: LoroText;
|
|
13
|
-
/** @type {TextBehavior} */
|
|
14
|
-
behavior: TextBehavior;
|
|
15
12
|
component: import("svelte").Component<any, any, string> | import("svelte").Component<{
|
|
16
13
|
block: import("./dialogue.svelte.js").Dialogue;
|
|
17
14
|
}, {}, "">;
|
|
18
|
-
get text(): string;
|
|
19
|
-
get delta(): import("loro-crdt").Delta<string>[];
|
|
20
|
-
selection: {
|
|
21
|
-
from: number;
|
|
22
|
-
to: number;
|
|
23
|
-
isCollapsed: boolean;
|
|
24
|
-
direction: "forward" | "backward" | "none";
|
|
25
|
-
} | null;
|
|
26
|
-
/** @param {Block} block */
|
|
27
|
-
absorbs(block: Block): boolean;
|
|
28
|
-
/** @param {import('loro-crdt').Delta<string>[]} data */
|
|
29
|
-
applyDelta(data?: import("loro-crdt").Delta<string>[]): void;
|
|
30
|
-
/** @param {Parameters<Block["split"]>[0]} [options] @returns {ReturnType<Block["split"]>} */
|
|
31
|
-
split(options?: Parameters<Block["split"]>[0]): ReturnType<Block["split"]>;
|
|
32
15
|
}
|
|
33
16
|
export type DialogueNode = NabuNode<{
|
|
34
17
|
type: "dialogue";
|
|
35
18
|
text: LoroText;
|
|
36
19
|
}>;
|
|
37
|
-
import {
|
|
38
|
-
import { LoroText } from "loro-crdt";
|
|
39
|
-
import { TextBehavior } from "../../behaviors/text";
|
|
20
|
+
import { TextBlock } from "../text-block.svelte.js";
|
|
40
21
|
import type { Nabu } from "../nabu.svelte.js";
|
|
22
|
+
import { LoroText } from "loro-crdt";
|
|
41
23
|
import type { NabuNode } from "../nabu.svelte.js";
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TextBlock } from "../text-block.svelte.js";
|
|
2
2
|
import { LoroText } from "loro-crdt";
|
|
3
3
|
import DialogueComponent from "./Dialogue.svelte";
|
|
4
|
-
import { TextBehavior } from "../../behaviors/text";
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* @import { Nabu, NabuNode } from "../nabu.svelte.js";
|
|
@@ -11,18 +10,11 @@ import { TextBehavior } from "../../behaviors/text";
|
|
|
11
10
|
* @typedef {NabuNode<{type: "dialogue", text: LoroText}>} DialogueNode
|
|
12
11
|
*/
|
|
13
12
|
|
|
14
|
-
export class Dialogue extends
|
|
13
|
+
export class Dialogue extends TextBlock {
|
|
15
14
|
/** @param {Nabu} nabu @param {DialogueNode} node */
|
|
16
15
|
constructor(nabu, node) {
|
|
17
16
|
super(nabu, node);
|
|
18
17
|
|
|
19
|
-
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
|
-
|
|
26
18
|
this.serializers.set('markdown', () => `— ${this.behavior.toMarkdown()}`);
|
|
27
19
|
this.serializers.set('json', () => ({
|
|
28
20
|
id: this.id,
|
|
@@ -34,46 +26,6 @@ export class Dialogue extends Block {
|
|
|
34
26
|
|
|
35
27
|
component = $derived(this.nabu.components.get("dialogue") || DialogueComponent);
|
|
36
28
|
|
|
37
|
-
get text() {
|
|
38
|
-
return this.behavior.text;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
get delta() {
|
|
42
|
-
return this.behavior.delta;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
selection = $derived(this.behavior.selection);
|
|
46
|
-
positions = $derived(this.behavior.positions);
|
|
47
|
-
|
|
48
|
-
/** @param {InputEvent} event */
|
|
49
|
-
beforeinput(event) {
|
|
50
|
-
return this.behavior.handleBeforeInput(event);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* @param {number} targetOffset
|
|
55
|
-
* @returns {{node: Node, offset: number} | null}
|
|
56
|
-
*/
|
|
57
|
-
getDOMPoint(targetOffset) {
|
|
58
|
-
if (!this.element) return null;
|
|
59
|
-
return this.behavior.getDOMPoint(targetOffset);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/** @param {Block} block */
|
|
63
|
-
absorbs(block) { return this.behavior.absorbs(block); }
|
|
64
|
-
|
|
65
|
-
/** @param {number} index @param {string} text */
|
|
66
|
-
insert(index, text) { return this.behavior.insert(index, text); }
|
|
67
|
-
|
|
68
|
-
/** @param {Parameters<Block["delete"]>[0]} [deletion] */
|
|
69
|
-
delete(deletion) { return this.behavior.delete(deletion); }
|
|
70
|
-
|
|
71
|
-
/** @param {import('loro-crdt').Delta<string>[]} data */
|
|
72
|
-
applyDelta(data = []) { return this.behavior.applyDelta(data); }
|
|
73
|
-
|
|
74
|
-
/** @param {Parameters<Block["split"]>[0]} [options] @returns {ReturnType<Block["split"]>} */
|
|
75
|
-
split(options) { return this.behavior.split(options); }
|
|
76
|
-
|
|
77
29
|
/** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
78
30
|
static create(nabu, type, props = {}, parentId = null, index = null) {
|
|
79
31
|
const node = nabu.tree.createNode(parentId || undefined, index || undefined);
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
/** @type {{block: import('./divider.svelte.js').DividerBlock}}*/
|
|
3
|
+
let {block} = $props();
|
|
4
|
+
</script>
|
|
5
|
+
|
|
6
|
+
<hr
|
|
7
|
+
bind:this={block.element}
|
|
8
|
+
data-block-id={block.id}
|
|
9
|
+
data-block-type="divider"
|
|
10
|
+
data-nabu-inert
|
|
11
|
+
contenteditable="false"
|
|
12
|
+
class="nabu-divider"
|
|
13
|
+
class:debug={block.nabu.debugging}
|
|
14
|
+
class:selected={block.selected}
|
|
15
|
+
/>
|
|
16
|
+
|
|
17
|
+
<style>
|
|
18
|
+
.nabu-divider {
|
|
19
|
+
border: none;
|
|
20
|
+
border-top: 1px solid var(--nb-border, rgba(255, 255, 255, 0.15));
|
|
21
|
+
margin: 1em 0;
|
|
22
|
+
user-select: none;
|
|
23
|
+
|
|
24
|
+
&.debug.selected {
|
|
25
|
+
background-color: rgba(59, 130, 246, 0.25);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
</style>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export default Divider;
|
|
2
|
+
type Divider = {
|
|
3
|
+
$on?(type: string, callback: (e: any) => void): () => void;
|
|
4
|
+
$set?(props: Partial<$$ComponentProps>): void;
|
|
5
|
+
};
|
|
6
|
+
declare const Divider: import("svelte").Component<{
|
|
7
|
+
block: import("./divider.svelte.js").DividerBlock;
|
|
8
|
+
}, {}, "">;
|
|
9
|
+
type $$ComponentProps = {
|
|
10
|
+
block: import("./divider.svelte.js").DividerBlock;
|
|
11
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const DividerExtension: import("../..").Extension;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { TextBehavior } from "../../behaviors/text";
|
|
2
|
+
import { extension } from "../../utils/extensions";
|
|
3
|
+
import { DividerBlock } from "./divider.svelte.js";
|
|
4
|
+
import DividerComponent from "./Divider.svelte";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @import { Nabu, Block } from "..";
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Divider hooks:
|
|
12
|
+
* - `---` + space in a block containing only `---` → replace it with a
|
|
13
|
+
* divider and insert an empty paragraph after.
|
|
14
|
+
* - Backspace at the start of a block whose previous sibling is a divider
|
|
15
|
+
* → delete the divider (otherwise the text merge would skip over it and
|
|
16
|
+
* leave it orphaned).
|
|
17
|
+
*
|
|
18
|
+
* @param {Nabu} nabu @param {InputEvent} event @param {Block} block
|
|
19
|
+
*/
|
|
20
|
+
const onBeforeInput = (nabu, event, block) => {
|
|
21
|
+
const { BREAK, CONTINUE } = nabu;
|
|
22
|
+
|
|
23
|
+
const behavior = block.behaviors.get("text");
|
|
24
|
+
if (!behavior || !(behavior instanceof TextBehavior)) return CONTINUE;
|
|
25
|
+
const sel = behavior.selection;
|
|
26
|
+
if (!sel || !sel.isCollapsed) return CONTINUE;
|
|
27
|
+
|
|
28
|
+
// Backspace right after a divider → delete the divider
|
|
29
|
+
if (event.inputType === "deleteContentBackward" && sel.from === 0) {
|
|
30
|
+
const { previous } = block.getAdjacentSiblings();
|
|
31
|
+
if (previous?.type !== 'divider') return CONTINUE;
|
|
32
|
+
const blockId = block.id;
|
|
33
|
+
nabu.tree.delete(previous.node.id);
|
|
34
|
+
nabu.commit().then(() => {
|
|
35
|
+
const fresh = nabu.blocks.get(blockId);
|
|
36
|
+
if (fresh) nabu.selection.setCursor(fresh, 0);
|
|
37
|
+
});
|
|
38
|
+
return BREAK;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// `--- ` markdown shortcut: block contains exactly `---` and a space is typed
|
|
42
|
+
if (event.inputType === "insertText" && event.data === " ") {
|
|
43
|
+
if (behavior.text !== "---" || sel.from !== 3) return CONTINUE;
|
|
44
|
+
|
|
45
|
+
const node = block.node;
|
|
46
|
+
const parentId = node.parent()?.id.toString() || null;
|
|
47
|
+
const index = node.index() ?? 0;
|
|
48
|
+
|
|
49
|
+
behavior.delete({ index: 0, length: 3 });
|
|
50
|
+
block.node.data.set("type", "divider");
|
|
51
|
+
|
|
52
|
+
const PClass = nabu.registry.get('paragraph');
|
|
53
|
+
const newParagraph = PClass ? PClass.create(nabu, 'paragraph', {}, parentId, index + 1) : null;
|
|
54
|
+
|
|
55
|
+
nabu.commit().then(() => {
|
|
56
|
+
if (!newParagraph) return;
|
|
57
|
+
const fresh = nabu.blocks.get(newParagraph.id);
|
|
58
|
+
if (fresh) nabu.selection.setCursor(fresh, 0);
|
|
59
|
+
});
|
|
60
|
+
return BREAK;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return CONTINUE;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const DividerExtension = extension("divider", {
|
|
67
|
+
// @ts-ignore — static-side variance on create() (same pattern as Paragraph/Heading)
|
|
68
|
+
block: DividerBlock,
|
|
69
|
+
component: DividerComponent,
|
|
70
|
+
actions: {
|
|
71
|
+
/**
|
|
72
|
+
* Insert a divider after the anchor block's position.
|
|
73
|
+
* @param {Nabu} nabu
|
|
74
|
+
*/
|
|
75
|
+
'divider:insert': (nabu) => {
|
|
76
|
+
const anchor = nabu.selection.anchorBlock;
|
|
77
|
+
if (!anchor) return;
|
|
78
|
+
const parentId = anchor.node.parent()?.id.toString() || null;
|
|
79
|
+
const index = anchor.node.index() ?? 0;
|
|
80
|
+
DividerBlock.create(nabu, 'divider', {}, parentId, index + 1);
|
|
81
|
+
return nabu.commit();
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
hooks: {
|
|
85
|
+
onBeforeInput,
|
|
86
|
+
}
|
|
87
|
+
});
|