@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,252 @@
|
|
|
1
|
+
import { MegaBlock } from "../megablock.svelte.js";
|
|
2
|
+
import { TextBehavior, deltaToHtml, deltaToMarkdown } from "../../behaviors/text";
|
|
3
|
+
import { LoroText } from "loro-crdt";
|
|
4
|
+
import ToggleComponent from "./Toggle.svelte";
|
|
5
|
+
import { tick } from "svelte";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @import { Nabu, NabuNode } from "../nabu.svelte.js";
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {NabuNode<{type: "toggle", text: LoroText, collapsed: boolean}>} ToggleNode
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
// @ts-ignore — static-side variance on create()
|
|
16
|
+
export class ToggleBlock extends MegaBlock {
|
|
17
|
+
/** @param {Nabu} nabu @param {ToggleNode} node */
|
|
18
|
+
constructor(nabu, node) {
|
|
19
|
+
super(nabu, node);
|
|
20
|
+
|
|
21
|
+
const data = node.data;
|
|
22
|
+
this.container = data.get("text") ?? data.setContainer("text", new LoroText());
|
|
23
|
+
|
|
24
|
+
/** @type {TextBehavior} */
|
|
25
|
+
this.behavior = new TextBehavior(this, this.container);
|
|
26
|
+
this.behaviors.set("text", this.behavior);
|
|
27
|
+
|
|
28
|
+
// Initialize collapsed from Loro metadata
|
|
29
|
+
this.collapsed = !!data.get('collapsed');
|
|
30
|
+
// Subscribe to data changes to keep collapsed in sync
|
|
31
|
+
this.node.data.subscribe(() => {
|
|
32
|
+
this.collapsed = !!this.node.data.get('collapsed');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
this.serializers.set('markdown', () => {
|
|
36
|
+
const head = this.behavior.toMarkdown();
|
|
37
|
+
const body = this.children.map(c => c.serialize('markdown')).filter(Boolean).join('\n\n');
|
|
38
|
+
return '<details><summary>' + head + '</summary>\n\n' + body + '\n\n</details>';
|
|
39
|
+
});
|
|
40
|
+
this.serializers.set('json', () => ({
|
|
41
|
+
id: this.id,
|
|
42
|
+
type: 'toggle',
|
|
43
|
+
props: { collapsed: this.collapsed },
|
|
44
|
+
content: this.behavior.toJSON(),
|
|
45
|
+
children: this.children.map(c => c.serialize('json')).filter(Boolean)
|
|
46
|
+
}));
|
|
47
|
+
this.serializers.set('application/x-nabu+json', () => ({
|
|
48
|
+
type: 'toggle',
|
|
49
|
+
props: { collapsed: this.collapsed },
|
|
50
|
+
delta: this.container.sliceDelta(0, this.container.length),
|
|
51
|
+
children: this.children.map(c => c.serialize('application/x-nabu+json')).filter(Boolean)
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ── Capability flags ────────────────────────────────────────────────────
|
|
56
|
+
// A typed "# " must not transform the toggle header away
|
|
57
|
+
transformsEnabled = false;
|
|
58
|
+
// marksEnabled stays true (default) — bold/italic work inside the header
|
|
59
|
+
// splitsOnEnter is not set (defaults true) but Enter is overridden in beforeinput
|
|
60
|
+
|
|
61
|
+
/** @type {boolean} */
|
|
62
|
+
collapsed = $state(false);
|
|
63
|
+
|
|
64
|
+
component = $derived(this.nabu.components.get("toggle") || ToggleComponent);
|
|
65
|
+
|
|
66
|
+
get text() { return this.behavior.text; }
|
|
67
|
+
get delta() { return this.behavior.delta; }
|
|
68
|
+
|
|
69
|
+
selection = $derived(this.behavior.selection);
|
|
70
|
+
|
|
71
|
+
// When collapsed, exclude children from positions so cursor cannot enter hidden body
|
|
72
|
+
positions = $derived(this.behavior.positions + (this.collapsed ? 0 : super.positions));
|
|
73
|
+
|
|
74
|
+
// Count body words regardless of collapse state
|
|
75
|
+
wordCount = $derived((this.behavior?.wordCount ?? 0) + this.children.reduce((s, c) => s + (c.wordCount ?? 0), 0));
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Mixed block: own text comes first, then children.
|
|
79
|
+
* When collapsed, children are excluded from navigation.
|
|
80
|
+
* @param {number} localOffset
|
|
81
|
+
* @returns {{ block: import('../block.svelte.js').Block, offset: number } | null}
|
|
82
|
+
*/
|
|
83
|
+
resolveOffset(localOffset) {
|
|
84
|
+
const textPositions = this.behavior.positions; // text.length + 1
|
|
85
|
+
if (this.collapsed || localOffset < textPositions || !this.children.length) {
|
|
86
|
+
return { block: this, offset: Math.min(localOffset, this.behavior.text.length) };
|
|
87
|
+
}
|
|
88
|
+
let remaining = localOffset - textPositions;
|
|
89
|
+
for (const child of this.children) {
|
|
90
|
+
const cp = child.positions ?? 0;
|
|
91
|
+
if (remaining < cp) return child.resolveOffset(remaining);
|
|
92
|
+
remaining -= cp;
|
|
93
|
+
}
|
|
94
|
+
return { block: this, offset: this.behavior.text.length };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// ── Collapse control ────────────────────────────────────────────────────
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Set the collapsed state and persist to Loro.
|
|
101
|
+
* @param {boolean} v
|
|
102
|
+
*/
|
|
103
|
+
setCollapsed(v) {
|
|
104
|
+
this.collapsed = !!v;
|
|
105
|
+
/** @type {any} */ (this.node.data).set('collapsed', !!v);
|
|
106
|
+
this.commit();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Toggle the collapsed state.
|
|
111
|
+
*/
|
|
112
|
+
toggleCollapsed() {
|
|
113
|
+
this.setCollapsed(!this.collapsed);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ── Proxies to TextBehavior ─────────────────────────────────────────────
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* @param {number} targetOffset
|
|
120
|
+
* @returns {{node: Node, offset: number} | null}
|
|
121
|
+
*/
|
|
122
|
+
getDOMPoint(targetOffset) { return this.behavior.getDOMPoint(targetOffset); }
|
|
123
|
+
|
|
124
|
+
/** @param {import('../block.svelte.js').Block} block */
|
|
125
|
+
absorbs(block) { return this.behavior.absorbs(block); }
|
|
126
|
+
|
|
127
|
+
/** @param {number} index @param {string} text */
|
|
128
|
+
insert(index, text) { return this.behavior.insert(index, text); }
|
|
129
|
+
|
|
130
|
+
/** @param {Parameters<import('../block.svelte.js').Block["delete"]>[0]} [deletion] */
|
|
131
|
+
delete(deletion) { return this.behavior.delete(deletion); }
|
|
132
|
+
|
|
133
|
+
/** @param {import('loro-crdt').Delta<string>[]} data */
|
|
134
|
+
applyDelta(data = []) { return this.behavior.applyDelta(data); }
|
|
135
|
+
|
|
136
|
+
// ── Custom beforeinput (handles Enter and Backspace-to-untoggle) ────────
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* @param {InputEvent} event
|
|
140
|
+
* @returns {boolean}
|
|
141
|
+
*/
|
|
142
|
+
beforeinput(event) {
|
|
143
|
+
const sel = this.behavior.selection;
|
|
144
|
+
|
|
145
|
+
// Enter in the header → split: keep before-cursor in header, move after-cursor
|
|
146
|
+
// into a new paragraph as the FIRST body child; expand the toggle; focus it.
|
|
147
|
+
// NOTE: Two commits happen — one from setCollapsed (if needed) + one from nabu.commit().
|
|
148
|
+
// This is intentional for simplicity; correctness over micro-optimization.
|
|
149
|
+
if (event.inputType === 'insertParagraph' && sel) {
|
|
150
|
+
const from = sel.from;
|
|
151
|
+
const to = sel.isCollapsed ? this.behavior.text.length : sel.to;
|
|
152
|
+
const delta = this.container.sliceDelta(from, to);
|
|
153
|
+
if (this.behavior.text.length > from) {
|
|
154
|
+
this.behavior.delete({ index: from, length: this.behavior.text.length - from });
|
|
155
|
+
}
|
|
156
|
+
if (this.collapsed) {
|
|
157
|
+
// setCollapsed commits; the insert below commits again — acceptable
|
|
158
|
+
this.setCollapsed(false);
|
|
159
|
+
}
|
|
160
|
+
const p = this.nabu.insert('paragraph', { delta }, this.node.id.toString(), 0);
|
|
161
|
+
this.nabu.commit().then(() => {
|
|
162
|
+
const f = this.nabu.blocks.get(p.id);
|
|
163
|
+
if (f) this.nabu.selection.setCursor(f, 0);
|
|
164
|
+
});
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Backspace at start of an EMPTY header with NO body → de-transform to paragraph.
|
|
169
|
+
if (event.inputType === 'deleteContentBackward' && sel?.isCollapsed && sel.from === 0
|
|
170
|
+
&& this.behavior.text.length === 0 && this.children.length === 0) {
|
|
171
|
+
const id = this.node.id.toString();
|
|
172
|
+
this.transformTo('paragraph');
|
|
173
|
+
tick().then(() => {
|
|
174
|
+
const f = this.nabu.blocks.get(id);
|
|
175
|
+
if (f) f.focus({ start: 0, end: 0 });
|
|
176
|
+
});
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return this.behavior.handleBeforeInput(event);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// ── Statics ─────────────────────────────────────────────────────────────
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* @param {import('../../utils/extensions.js').PasteBlock} pb
|
|
187
|
+
* @param {{ recurse: (child: import('../../utils/extensions.js').PasteBlock, ctx?: object) => string }} helpers
|
|
188
|
+
*/
|
|
189
|
+
static toMarkdown(pb, { recurse }) {
|
|
190
|
+
const head = deltaToMarkdown(pb.delta || []);
|
|
191
|
+
const body = (pb.children || []).map(c => recurse(c)).join('\n\n');
|
|
192
|
+
return '<details><summary>' + head + '</summary>\n\n' + body + '\n\n</details>';
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* @param {import('../../utils/extensions.js').PasteBlock} pb
|
|
197
|
+
* @param {{ recurse: (child: import('../../utils/extensions.js').PasteBlock) => string }} helpers
|
|
198
|
+
*/
|
|
199
|
+
static toHtml(pb, { recurse }) {
|
|
200
|
+
const open = pb.props?.collapsed ? '' : ' open';
|
|
201
|
+
const head = deltaToHtml(pb.delta || []);
|
|
202
|
+
const body = (pb.children || []).map(c => recurse(c)).join('');
|
|
203
|
+
return '<details' + open + '><summary>' + head + '</summary>' + body + '</details>';
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
static htmlRules = [{ selector: 'details' }];
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* @param {Element} el
|
|
210
|
+
* @param {{ parseInline: (el: Element) => import('loro-crdt').Delta<string>[], parseChildren?: (el: Element) => import('../../utils/extensions.js').PasteBlock[] }} helpers
|
|
211
|
+
* @returns {import('../../utils/extensions.js').PasteBlock | null}
|
|
212
|
+
*/
|
|
213
|
+
static fromHTML(el, { parseInline, parseChildren }) {
|
|
214
|
+
// Use 'summary' without :scope because happy-dom doesn't support :scope pseudo-class
|
|
215
|
+
const summary = /** @type {Element | null} */ ([...el.children].find(c => c.tagName.toLowerCase() === 'summary') || null);
|
|
216
|
+
const delta = summary ? parseInline(summary) : [];
|
|
217
|
+
const collapsed = !el.hasAttribute('open');
|
|
218
|
+
// Parse non-summary child elements as body blocks
|
|
219
|
+
const bodyEls = [...el.children].filter(c => c.tagName.toLowerCase() !== 'summary');
|
|
220
|
+
/** @type {import('../../utils/extensions.js').PasteBlock[]} */
|
|
221
|
+
let children = [];
|
|
222
|
+
if (parseChildren && bodyEls.length) {
|
|
223
|
+
// Create a temporary wrapper with only the non-summary children
|
|
224
|
+
const ownerDoc = el.ownerDocument;
|
|
225
|
+
if (ownerDoc) {
|
|
226
|
+
const wrapper = ownerDoc.createElement('div');
|
|
227
|
+
for (const bodyEl of bodyEls) {
|
|
228
|
+
wrapper.appendChild(bodyEl.cloneNode(true));
|
|
229
|
+
}
|
|
230
|
+
children = parseChildren(wrapper);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return { type: 'toggle', props: { collapsed }, delta, partial: false, ...(children.length ? { children } : {}) };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* @param {Nabu} nabu
|
|
238
|
+
* @param {string} type
|
|
239
|
+
* @param {Object} [props={}]
|
|
240
|
+
* @param {string|null} [parentId=null]
|
|
241
|
+
* @param {number|null} [index=null]
|
|
242
|
+
*/
|
|
243
|
+
static create(nabu, type, props = {}, parentId = null, index = null) {
|
|
244
|
+
const node = nabu.tree.createNode(parentId || undefined, index != null ? index : undefined);
|
|
245
|
+
/** @type {any} */ (node.data).set('type', 'toggle');
|
|
246
|
+
/** @type {any} */ (node.data).set('collapsed', props.collapsed ?? false);
|
|
247
|
+
const container = /** @type {any} */ (node.data).setContainer('text', new LoroText());
|
|
248
|
+
if (props.text) container.insert(0, props.text);
|
|
249
|
+
if (props.delta) container.applyDelta([...props.delta]);
|
|
250
|
+
return new ToggleBlock(nabu, /** @type {ToggleNode} */ (/** @type {unknown} */ (node)));
|
|
251
|
+
}
|
|
252
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import {Block} from "./block.svelte.js"
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* @template {Block} [TBlock=Block]
|
|
6
|
+
* @typedef {Object} VariableInit
|
|
7
|
+
* @property {"string"|"number"|"boolean"} type - The variable type (e.g., "string", "number", "boolean").
|
|
8
|
+
* @property {function(TBlock): any} get - A function that returns the current value of the variable.
|
|
9
|
+
* @property {function(TBlock, any): void} [set] - A function that updates the variable's value.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* @template {Block} [TBlock=Block]
|
|
13
|
+
*/
|
|
14
|
+
export class Variable<TBlock extends Block = Block> {
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new Variable instance.
|
|
17
|
+
* @param {TBlock} block - The block that owns this variable.
|
|
18
|
+
* @param {string} name
|
|
19
|
+
* @param {VariableInit<TBlock>} init
|
|
20
|
+
*/
|
|
21
|
+
constructor(block: TBlock, name: string, init: VariableInit<TBlock>);
|
|
22
|
+
block: TBlock;
|
|
23
|
+
name: string;
|
|
24
|
+
init: VariableInit<TBlock>;
|
|
25
|
+
type: "string" | "number" | "boolean";
|
|
26
|
+
get: (arg0: TBlock) => any;
|
|
27
|
+
set: ((arg0: TBlock, arg1: any) => void) | undefined;
|
|
28
|
+
writable: boolean;
|
|
29
|
+
set value(newValue: any);
|
|
30
|
+
get value(): any;
|
|
31
|
+
destroy(): void;
|
|
32
|
+
}
|
|
33
|
+
export type VariableInit<TBlock extends Block = Block> = {
|
|
34
|
+
/**
|
|
35
|
+
* - The variable type (e.g., "string", "number", "boolean").
|
|
36
|
+
*/
|
|
37
|
+
type: "string" | "number" | "boolean";
|
|
38
|
+
/**
|
|
39
|
+
* - A function that returns the current value of the variable.
|
|
40
|
+
*/
|
|
41
|
+
get: (arg0: TBlock) => any;
|
|
42
|
+
/**
|
|
43
|
+
* - A function that updates the variable's value.
|
|
44
|
+
*/
|
|
45
|
+
set?: ((arg0: TBlock, arg1: any) => void) | undefined;
|
|
46
|
+
};
|
|
47
|
+
import type { Block } from "./block.svelte.js";
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import {Block} from "./block.svelte.js"
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @template {Block} [TBlock=Block]
|
|
8
|
+
* @typedef {Object} VariableInit
|
|
9
|
+
* @property {"string"|"number"|"boolean"} type - The variable type (e.g., "string", "number", "boolean").
|
|
10
|
+
* @property {function(TBlock): any} get - A function that returns the current value of the variable.
|
|
11
|
+
* @property {function(TBlock, any): void} [set] - A function that updates the variable's value.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @template {Block} [TBlock=Block]
|
|
16
|
+
*/
|
|
17
|
+
export class Variable {
|
|
18
|
+
/**
|
|
19
|
+
* Creates a new Variable instance.
|
|
20
|
+
* @param {TBlock} block - The block that owns this variable.
|
|
21
|
+
* @param {string} name
|
|
22
|
+
* @param {VariableInit<TBlock>} init
|
|
23
|
+
*/
|
|
24
|
+
constructor(block, name, init) {
|
|
25
|
+
this.block = block;
|
|
26
|
+
this.name = name;
|
|
27
|
+
this.init = init;
|
|
28
|
+
this.type = init.type;
|
|
29
|
+
this.get = init.get;
|
|
30
|
+
this.set = init.set;
|
|
31
|
+
|
|
32
|
+
this.block.variables.set(name, this);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
writable = $derived(!!this.set);
|
|
36
|
+
|
|
37
|
+
set value(newValue) {
|
|
38
|
+
if (!this.set) {
|
|
39
|
+
throw new Error(`Variable "${this.name}" is read-only.`);
|
|
40
|
+
}
|
|
41
|
+
this.set(this.block, newValue);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
get value() {
|
|
45
|
+
return this.get(this.block);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
destroy() {
|
|
49
|
+
this.block.variables.delete(this.name);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
<script>
|
|
2
|
+
/** @type {{ block: import('./video.svelte.js').VideoBlock }} */
|
|
3
|
+
let { block } = $props();
|
|
4
|
+
const src = $derived(block.displaySrc);
|
|
5
|
+
</script>
|
|
6
|
+
|
|
7
|
+
<figure
|
|
8
|
+
bind:this={block.element}
|
|
9
|
+
data-block-id={block.id}
|
|
10
|
+
data-block-type="video"
|
|
11
|
+
data-nabu-inert
|
|
12
|
+
contenteditable="false"
|
|
13
|
+
class="nabu-video"
|
|
14
|
+
class:selected={block.selected}
|
|
15
|
+
class:pending={block.status === 'pending'}
|
|
16
|
+
class:error={block.status === 'error'}
|
|
17
|
+
>
|
|
18
|
+
{#if block.status === 'error'}
|
|
19
|
+
<div class="nabu-video-error">⚠️ Upload failed</div>
|
|
20
|
+
{:else if src}
|
|
21
|
+
<!-- svelte-ignore a11y_media_has_caption -->
|
|
22
|
+
<video src={src} controls draggable="false"></video>
|
|
23
|
+
{#if block.status === 'pending'}<div class="nabu-video-spinner">Uploading…</div>{/if}
|
|
24
|
+
{:else}
|
|
25
|
+
<div class="nabu-video-empty">No video</div>
|
|
26
|
+
{/if}
|
|
27
|
+
</figure>
|
|
28
|
+
|
|
29
|
+
<style>
|
|
30
|
+
.nabu-video { margin: 0.5em 0; user-select: none; position: relative; display: block; max-width: 100%; }
|
|
31
|
+
.nabu-video video { max-width: 100%; height: auto; border-radius: 6px; display: block; }
|
|
32
|
+
.nabu-video.selected video { outline: 2px solid var(--nb-accent, #4d9fff); }
|
|
33
|
+
.nabu-video.pending video { opacity: 0.6; }
|
|
34
|
+
.nabu-video-spinner, .nabu-video-error, .nabu-video-empty {
|
|
35
|
+
position: absolute; inset: auto 0 0 0; padding: 4px 8px; font-size: 12px;
|
|
36
|
+
background: var(--nb-bg-surface, rgba(0,0,0,0.6)); color: var(--nb-text-primary, #fff); border-radius: 0 0 6px 6px;
|
|
37
|
+
}
|
|
38
|
+
.nabu-video-empty, .nabu-video-error { position: static; padding: 1.5em; text-align: center; border: 1px dashed var(--nb-border, rgba(255,255,255,0.2)); border-radius: 6px; color: var(--nb-text-muted, rgba(255,255,255,0.4)); }
|
|
39
|
+
</style>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export default Video;
|
|
2
|
+
type Video = {
|
|
3
|
+
$on?(type: string, callback: (e: any) => void): () => void;
|
|
4
|
+
$set?(props: Partial<$$ComponentProps>): void;
|
|
5
|
+
};
|
|
6
|
+
declare const Video: import("svelte").Component<{
|
|
7
|
+
block: import("./video.svelte.js").VideoBlock;
|
|
8
|
+
}, {}, "">;
|
|
9
|
+
type $$ComponentProps = {
|
|
10
|
+
block: import("./video.svelte.js").VideoBlock;
|
|
11
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const VideoExtension: import("../..").Extension;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { extension } from "../../utils/extensions";
|
|
2
|
+
import { VideoBlock } from "./video.svelte.js";
|
|
3
|
+
import VideoComponent from "./Video.svelte";
|
|
4
|
+
import { pickMedia, insertMediaUrl } from "../media/media.helpers.js";
|
|
5
|
+
|
|
6
|
+
export const VideoExtension = extension("video", {
|
|
7
|
+
// @ts-ignore — static-side variance on create()
|
|
8
|
+
block: VideoBlock,
|
|
9
|
+
component: VideoComponent,
|
|
10
|
+
actions: {
|
|
11
|
+
'video:pick': (nabu) => pickMedia(nabu, 'video', 'video/*'),
|
|
12
|
+
/**
|
|
13
|
+
* Insert from a URL (external ref). data: { ref, alt?, name? }
|
|
14
|
+
* @param {import('../nabu.svelte.js').Nabu} nabu
|
|
15
|
+
* @param {{ ref: string, alt?: string, name?: string }} data
|
|
16
|
+
*/
|
|
17
|
+
'video:insert': (nabu, data) => insertMediaUrl(nabu, 'video', data),
|
|
18
|
+
},
|
|
19
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @import { Nabu, NabuNode } from "../nabu.svelte.js";
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Void video block. Extends MediaBlock for shared void/metadata behavior.
|
|
6
|
+
* Renders an inert <figure> with a <video> element.
|
|
7
|
+
*/
|
|
8
|
+
export class VideoBlock extends MediaBlock {
|
|
9
|
+
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
10
|
+
static toMarkdown(pb: import("../../utils/extensions.js").PasteBlock): string;
|
|
11
|
+
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
12
|
+
static toHtml(pb: import("../../utils/extensions.js").PasteBlock): string;
|
|
13
|
+
static htmlRules: {
|
|
14
|
+
selector: string;
|
|
15
|
+
}[];
|
|
16
|
+
/**
|
|
17
|
+
* @param {Element} el
|
|
18
|
+
* @returns {import('../../utils/extensions.js').PasteBlock}
|
|
19
|
+
*/
|
|
20
|
+
static fromHTML(el: Element): import("../../utils/extensions.js").PasteBlock;
|
|
21
|
+
/** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
22
|
+
static create(nabu: Nabu, type: string, props?: Object, parentId?: string | null, index?: number | null): VideoBlock;
|
|
23
|
+
component: import("svelte").Component<any, any, string> | import("svelte").Component<{
|
|
24
|
+
block: import("./video.svelte.js").VideoBlock;
|
|
25
|
+
}, {}, "">;
|
|
26
|
+
}
|
|
27
|
+
import { MediaBlock } from "../media/media-block.svelte.js";
|
|
28
|
+
import type { Nabu } from "../nabu.svelte.js";
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// @ts-ignore
|
|
2
|
+
import { MediaBlock } from "../media/media-block.svelte.js";
|
|
3
|
+
import VideoComponent from "./Video.svelte";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @import { Nabu, NabuNode } from "../nabu.svelte.js";
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Void video block. Extends MediaBlock for shared void/metadata behavior.
|
|
11
|
+
* Renders an inert <figure> with a <video> element.
|
|
12
|
+
*/
|
|
13
|
+
// @ts-ignore — static-side variance on create()
|
|
14
|
+
export class VideoBlock extends MediaBlock {
|
|
15
|
+
/** @param {Nabu} nabu @param {NabuNode} node */
|
|
16
|
+
constructor(nabu, node) {
|
|
17
|
+
super(nabu, node);
|
|
18
|
+
|
|
19
|
+
this.serializers.set('markdown', () => '[' + (this.alt || this.name || 'video') + '](' + this.ref + ')');
|
|
20
|
+
this.serializers.set('json', () => ({
|
|
21
|
+
id: this.id,
|
|
22
|
+
type: 'video',
|
|
23
|
+
props: { ref: this.ref, alt: this.alt, name: this.name },
|
|
24
|
+
}));
|
|
25
|
+
this.serializers.set('application/x-nabu+json', () => ({
|
|
26
|
+
type: 'video',
|
|
27
|
+
props: { ref: this.ref, alt: this.alt, name: this.name },
|
|
28
|
+
partial: false,
|
|
29
|
+
}));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
component = $derived(this.nabu.components.get("video") || VideoComponent);
|
|
33
|
+
|
|
34
|
+
// ── Statics ──────────────────────────────────────────────────────────────
|
|
35
|
+
|
|
36
|
+
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
37
|
+
static toMarkdown(pb) {
|
|
38
|
+
return '[' + (pb.props?.alt || pb.props?.name || 'video') + '](' + (pb.props?.ref || '') + ')';
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** @param {import('../../utils/extensions.js').PasteBlock} pb */
|
|
42
|
+
static toHtml(pb) {
|
|
43
|
+
return '<video src="' + (pb.props?.ref || '') + '" controls></video>';
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
static htmlRules = [
|
|
47
|
+
{ selector: 'video' },
|
|
48
|
+
];
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @param {Element} el
|
|
52
|
+
* @returns {import('../../utils/extensions.js').PasteBlock}
|
|
53
|
+
*/
|
|
54
|
+
static fromHTML(el) {
|
|
55
|
+
return {
|
|
56
|
+
type: 'video',
|
|
57
|
+
props: {
|
|
58
|
+
ref: el.getAttribute('src') || el.querySelector('source')?.getAttribute('src') || '',
|
|
59
|
+
alt: '',
|
|
60
|
+
},
|
|
61
|
+
partial: false,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** @param {Nabu} nabu @param {string} type @param {Object} [props={}] @param {string|null} [parentId=null] @param {number|null} [index=null] */
|
|
66
|
+
static create(nabu, type, props = {}, parentId = null, index = null) {
|
|
67
|
+
const node = MediaBlock.createNode(nabu, 'video', props, parentId, index);
|
|
68
|
+
const block = new VideoBlock(nabu, /** @type {any} */ (node));
|
|
69
|
+
return block;
|
|
70
|
+
}
|
|
71
|
+
}
|