@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
package/dist/extend.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @aionbuilders/nabu/extend
|
|
3
|
+
*
|
|
4
|
+
* Writing your own blocks and extensions: the `extension()` factory, the block base classes, the text behavior and the
|
|
5
|
+
* helpers a block needs (keyboard chords, HTML escaping). Mount-time pieces (editor, presets, built-in extensions) are in
|
|
6
|
+
* `@aionbuilders/nabu`.
|
|
7
|
+
*
|
|
8
|
+
* Stability (audit P-119): the members of `Nabu`, `Block`, `TextBlock`, `MegaBlock`, `TextBehavior`, the selection and the
|
|
9
|
+
* command registry tagged `@public` in their JSDoc are the stable contract of a third-party block or extension (pinned by
|
|
10
|
+
* `tests/unit/extend-contract.test.js`). `nabu.internals` exposes the rest of the machinery and is UNSTABLE: it may change in
|
|
11
|
+
* any minor version. See `docs/guides/extensions.md`, "Stability".
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* import { extension, TextBlock, TextBehavior, RichText } from '@aionbuilders/nabu/extend';
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
// Extensions
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
|
|
21
|
+
export { extension, Extension } from './utils/extensions.js';
|
|
22
|
+
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
// Blocks
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
|
|
27
|
+
export { Block } from './blocks/block.svelte.js';
|
|
28
|
+
export { TextBlock } from './blocks/text-block.svelte.js';
|
|
29
|
+
export { MegaBlock } from './blocks/megablock.svelte.js';
|
|
30
|
+
export { default as BlockComponent } from './blocks/Block.svelte';
|
|
31
|
+
|
|
32
|
+
// ---------------------------------------------------------------------------
|
|
33
|
+
// Text behavior
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
|
|
36
|
+
export { TextBehavior } from './behaviors/text/index.js';
|
|
37
|
+
export { default as RichText } from './behaviors/text/RichText.svelte';
|
|
38
|
+
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
// Keyboard chords (command `keys`)
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
|
|
43
|
+
export {
|
|
44
|
+
parseChord,
|
|
45
|
+
canonicalChord,
|
|
46
|
+
chordId,
|
|
47
|
+
eventChord,
|
|
48
|
+
eventChordId,
|
|
49
|
+
isReservedChord,
|
|
50
|
+
RESERVED_CHORDS,
|
|
51
|
+
hasCommandModifier,
|
|
52
|
+
detectPlatform,
|
|
53
|
+
} from './utils/keys.js';
|
|
54
|
+
|
|
55
|
+
// ---------------------------------------------------------------------------
|
|
56
|
+
// HTML escaping (serializers)
|
|
57
|
+
// ---------------------------------------------------------------------------
|
|
58
|
+
|
|
59
|
+
export { escapeHtml, escapeAttr } from './utils/html.js';
|
|
60
|
+
|
|
61
|
+
// ---------------------------------------------------------------------------
|
|
62
|
+
// Types
|
|
63
|
+
// ---------------------------------------------------------------------------
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* A node of the document tree: `NabuNode<{ type: 'x', … }>`.
|
|
67
|
+
* @template {Object<string, any>} [T=Record<string, any>]
|
|
68
|
+
* @typedef {import('./blocks/nabu.svelte.js').NabuNode<T>} NabuNode
|
|
69
|
+
*/
|
|
70
|
+
/** @typedef {import('./utils/extensions.js').ExtensionInit} ExtensionInit */
|
|
71
|
+
/** @typedef {import('./utils/extensions.js').ExtensionHooks} ExtensionHooks */
|
|
72
|
+
/** @typedef {import('./utils/extensions.js').SplitData} SplitData What the `onSplit` hook receives: the caret offset and the tail of the text. */
|
|
73
|
+
/** @typedef {import('./utils/extensions.js').InputRule} InputRule */
|
|
74
|
+
/** @typedef {import('./utils/extensions.js').PasteBlock} PasteBlock */
|
|
75
|
+
/** @typedef {import('./utils/extensions.js').PasteFragment} PasteFragment */
|
|
76
|
+
/** @typedef {import('./utils/extensions.js').PasteInterpreter} PasteInterpreter */
|
|
77
|
+
/** @typedef {import('./utils/extensions.js').PasteInterpretOptions} PasteInterpretOptions */
|
|
78
|
+
/** @typedef {import('./utils/extensions.js').PasteFileHandler} PasteFileHandler */
|
|
79
|
+
/** @typedef {import('./utils/extensions.js').HtmlRule} HtmlRule A rule of a block's `static htmlRules`. */
|
|
80
|
+
/** @typedef {import('./utils/extensions.js').HtmlHelpers} HtmlHelpers What a block's `static fromHTML` receives. */
|
|
81
|
+
/** @typedef {import('./utils/extensions.js').MarkdownRule} MarkdownRule A rule of a block's `static markdownRules`. */
|
|
82
|
+
/** @typedef {import('./utils/extensions.js').MarkdownHelpers} MarkdownHelpers What a block's `static fromMarkdown` receives. */
|
|
83
|
+
/** @typedef {import('./utils/extensions.js').SerializeContext} SerializeContext The second argument of a block's `static toMarkdown` / `toHtml`. */
|
|
84
|
+
/** @typedef {import('./blocks/nabu.svelte.js').NabuInternals} NabuInternals The shape of `nabu.internals`. UNSTABLE: may change in any minor version. */
|
|
85
|
+
/** @typedef {import('./utils/commands.js').CommandDef} CommandDef */
|
|
86
|
+
/** @typedef {import('./utils/commands.js').CommandContext} CommandContext */
|
|
87
|
+
/** @typedef {import('./utils/keys.js').Chord} Chord */
|
|
88
|
+
/** @typedef {import('./utils/keys.js').Platform} Platform */
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Asset id of a `nabu-asset:` ref, or `null` for any other value.
|
|
3
|
+
* @param {unknown} ref
|
|
4
|
+
* @returns {string | null}
|
|
5
|
+
*/
|
|
6
|
+
export function assetIdOf(ref: unknown): string | null;
|
|
7
|
+
/**
|
|
8
|
+
* Whether `id` (the part after `nabu-asset:`) is a content hash.
|
|
9
|
+
* @param {unknown} id
|
|
10
|
+
* @returns {boolean}
|
|
11
|
+
*/
|
|
12
|
+
export function isHashId(id: unknown): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* SHA-256 of `bytes` as an asset id: `sha256-<64 hex>` (ADR 014 §3.1).
|
|
15
|
+
* @param {BufferSource} bytes
|
|
16
|
+
* @returns {Promise<string>}
|
|
17
|
+
*/
|
|
18
|
+
export function sha256Id(bytes: BufferSource): Promise<string>;
|
|
19
|
+
/**
|
|
20
|
+
* Walk PasteBlocks (children included) and call `fn(block, assetId)` for every block whose `props.ref` is a
|
|
21
|
+
* `nabu-asset:` ref. `fn` returns a props PATCH (merged into a COPY of the block) or nothing. Never mutates the input;
|
|
22
|
+
* returns the same array when nothing changed.
|
|
23
|
+
* @template {{ props?: Record<string, any>, children?: any[] }} B
|
|
24
|
+
* @param {B[]} blocks
|
|
25
|
+
* @param {(block: B, id: string) => Record<string, any> | null | undefined | void} fn
|
|
26
|
+
* @returns {B[]}
|
|
27
|
+
*/
|
|
28
|
+
export function mapAssetBlocks<B extends {
|
|
29
|
+
props?: Record<string, any>;
|
|
30
|
+
children?: any[];
|
|
31
|
+
}>(blocks: B[], fn: (block: B, id: string) => Record<string, any> | null | undefined | void): B[];
|
|
32
|
+
/**
|
|
33
|
+
* Walk PasteBlocks (children included) and call `fn(block, url)` for every block whose `props.ref` is an absolute
|
|
34
|
+
* `http(s)` URL (a remote file). `fn` returns a props PATCH or nothing. Same contract as {@link mapAssetBlocks}.
|
|
35
|
+
* @template {{ props?: Record<string, any>, children?: any[] }} B
|
|
36
|
+
* @param {B[]} blocks
|
|
37
|
+
* @param {(block: B, url: string) => Record<string, any> | null | undefined | void} fn
|
|
38
|
+
* @returns {B[]}
|
|
39
|
+
*/
|
|
40
|
+
export function mapRemoteBlocks<B extends {
|
|
41
|
+
props?: Record<string, any>;
|
|
42
|
+
children?: any[];
|
|
43
|
+
}>(blocks: B[], fn: (block: B, url: string) => Record<string, any> | null | undefined | void): B[];
|
|
44
|
+
/**
|
|
45
|
+
* A file name for a remote URL: the last segment of its path, or `file` + the extension of its type.
|
|
46
|
+
* @param {string} url
|
|
47
|
+
* @param {string} [mime]
|
|
48
|
+
* @returns {string}
|
|
49
|
+
*/
|
|
50
|
+
export function remoteFileName(url: string, mime?: string): string;
|
|
51
|
+
/**
|
|
52
|
+
* Ids of the assets referenced by PasteBlocks (children included), in order of appearance, deduplicated.
|
|
53
|
+
* @param {any[]} blocks
|
|
54
|
+
* @returns {string[]}
|
|
55
|
+
*/
|
|
56
|
+
export function collectAssetIds(blocks: any[]): string[];
|
|
57
|
+
/**
|
|
58
|
+
* @param {Uint8Array} bytes
|
|
59
|
+
* @returns {string}
|
|
60
|
+
*/
|
|
61
|
+
export function bytesToBase64(bytes: Uint8Array): string;
|
|
62
|
+
/**
|
|
63
|
+
* @param {string} b64
|
|
64
|
+
* @returns {Uint8Array | null} `null` when the string is not valid base64
|
|
65
|
+
*/
|
|
66
|
+
export function base64ToBytes(b64: string): Uint8Array | null;
|
|
67
|
+
/**
|
|
68
|
+
* Pure helpers for moving `nabu-asset:` media through the clipboard (P-51): walking the PasteBlocks that reference
|
|
69
|
+
* an asset, content hashing (ADR 014) and base64 for the bytes carried in `application/x-nabu+json`. No document
|
|
70
|
+
* access here — `Files` (files.svelte.js) does that.
|
|
71
|
+
*/
|
|
72
|
+
/** Scheme of the refs that point to a file held by the asset store: `nabu-asset:sha256-<hex>` (ADR 014), or `nabu-asset:<uuid>` (beta). */
|
|
73
|
+
export const ASSET_PREFIX: "nabu-asset:";
|
|
74
|
+
/** An asset id that is a content hash (what follows `nabu-asset:`). Anything else after the scheme is a legacy beta uuid. */
|
|
75
|
+
export const HASH_ID_RE: RegExp;
|
|
76
|
+
/** Scheme of a ref whose bytes are being re-created in this document (local, transient — see Files#adopt). */
|
|
77
|
+
export const PENDING_PREFIX: "nabu-pending:";
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure helpers for moving `nabu-asset:` media through the clipboard (P-51): walking the PasteBlocks that reference
|
|
3
|
+
* an asset, content hashing (ADR 014) and base64 for the bytes carried in `application/x-nabu+json`. No document
|
|
4
|
+
* access here — `Files` (files.svelte.js) does that.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** Scheme of the refs that point to a file held by the asset store: `nabu-asset:sha256-<hex>` (ADR 014), or `nabu-asset:<uuid>` (beta). */
|
|
8
|
+
export const ASSET_PREFIX = 'nabu-asset:';
|
|
9
|
+
/** An asset id that is a content hash (what follows `nabu-asset:`). Anything else after the scheme is a legacy beta uuid. */
|
|
10
|
+
export const HASH_ID_RE = /^sha256-[0-9a-f]{64}$/;
|
|
11
|
+
/** Scheme of a ref whose bytes are being re-created in this document (local, transient — see Files#adopt). */
|
|
12
|
+
export const PENDING_PREFIX = 'nabu-pending:';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Asset id of a `nabu-asset:` ref, or `null` for any other value.
|
|
16
|
+
* @param {unknown} ref
|
|
17
|
+
* @returns {string | null}
|
|
18
|
+
*/
|
|
19
|
+
export function assetIdOf(ref) {
|
|
20
|
+
return typeof ref === 'string' && ref.startsWith(ASSET_PREFIX) && ref.length > ASSET_PREFIX.length ? ref.slice(ASSET_PREFIX.length) : null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Whether `id` (the part after `nabu-asset:`) is a content hash.
|
|
25
|
+
* @param {unknown} id
|
|
26
|
+
* @returns {boolean}
|
|
27
|
+
*/
|
|
28
|
+
export function isHashId(id) {
|
|
29
|
+
return typeof id === 'string' && HASH_ID_RE.test(id);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* SHA-256 of `bytes` as an asset id: `sha256-<64 hex>` (ADR 014 §3.1).
|
|
34
|
+
* @param {BufferSource} bytes
|
|
35
|
+
* @returns {Promise<string>}
|
|
36
|
+
*/
|
|
37
|
+
export async function sha256Id(bytes) {
|
|
38
|
+
const digest = new Uint8Array(await globalThis.crypto.subtle.digest('SHA-256', bytes));
|
|
39
|
+
let hex = '';
|
|
40
|
+
for (const byte of digest) hex += byte.toString(16).padStart(2, '0');
|
|
41
|
+
return 'sha256-' + hex;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Walk PasteBlocks (children included) and call `fn(block, assetId)` for every block whose `props.ref` is a
|
|
46
|
+
* `nabu-asset:` ref. `fn` returns a props PATCH (merged into a COPY of the block) or nothing. Never mutates the input;
|
|
47
|
+
* returns the same array when nothing changed.
|
|
48
|
+
* @template {{ props?: Record<string, any>, children?: any[] }} B
|
|
49
|
+
* @param {B[]} blocks
|
|
50
|
+
* @param {(block: B, id: string) => Record<string, any> | null | undefined | void} fn
|
|
51
|
+
* @returns {B[]}
|
|
52
|
+
*/
|
|
53
|
+
export function mapAssetBlocks(blocks, fn) {
|
|
54
|
+
if (!Array.isArray(blocks)) return blocks;
|
|
55
|
+
let changed = false;
|
|
56
|
+
const out = blocks.map((pb) => {
|
|
57
|
+
if (!pb || typeof pb !== 'object') return pb;
|
|
58
|
+
let next = pb;
|
|
59
|
+
const id = assetIdOf(pb.props?.ref);
|
|
60
|
+
if (id !== null) {
|
|
61
|
+
const patch = fn(pb, id);
|
|
62
|
+
if (patch) next = { ...next, props: { ...pb.props, ...patch } };
|
|
63
|
+
}
|
|
64
|
+
if (Array.isArray(pb.children) && pb.children.length) {
|
|
65
|
+
const children = mapAssetBlocks(pb.children, fn);
|
|
66
|
+
if (children !== pb.children) next = { ...next, children };
|
|
67
|
+
}
|
|
68
|
+
if (next !== pb) changed = true;
|
|
69
|
+
return next;
|
|
70
|
+
});
|
|
71
|
+
return changed ? out : blocks;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Walk PasteBlocks (children included) and call `fn(block, url)` for every block whose `props.ref` is an absolute
|
|
76
|
+
* `http(s)` URL (a remote file). `fn` returns a props PATCH or nothing. Same contract as {@link mapAssetBlocks}.
|
|
77
|
+
* @template {{ props?: Record<string, any>, children?: any[] }} B
|
|
78
|
+
* @param {B[]} blocks
|
|
79
|
+
* @param {(block: B, url: string) => Record<string, any> | null | undefined | void} fn
|
|
80
|
+
* @returns {B[]}
|
|
81
|
+
*/
|
|
82
|
+
export function mapRemoteBlocks(blocks, fn) {
|
|
83
|
+
if (!Array.isArray(blocks)) return blocks;
|
|
84
|
+
let changed = false;
|
|
85
|
+
const out = blocks.map((pb) => {
|
|
86
|
+
if (!pb || typeof pb !== 'object') return pb;
|
|
87
|
+
let next = pb;
|
|
88
|
+
const ref = pb.props?.ref;
|
|
89
|
+
if (typeof ref === 'string' && /^https?:\/\//i.test(ref.trim())) {
|
|
90
|
+
const patch = fn(pb, ref.trim());
|
|
91
|
+
if (patch) next = { ...next, props: { ...pb.props, ...patch } };
|
|
92
|
+
}
|
|
93
|
+
if (Array.isArray(pb.children) && pb.children.length) {
|
|
94
|
+
const children = mapRemoteBlocks(pb.children, fn);
|
|
95
|
+
if (children !== pb.children) next = { ...next, children };
|
|
96
|
+
}
|
|
97
|
+
if (next !== pb) changed = true;
|
|
98
|
+
return next;
|
|
99
|
+
});
|
|
100
|
+
return changed ? out : blocks;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* A file name for a remote URL: the last segment of its path, or `file` + the extension of its type.
|
|
105
|
+
* @param {string} url
|
|
106
|
+
* @param {string} [mime]
|
|
107
|
+
* @returns {string}
|
|
108
|
+
*/
|
|
109
|
+
export function remoteFileName(url, mime = '') {
|
|
110
|
+
let name = '';
|
|
111
|
+
try { name = decodeURIComponent(new URL(url).pathname.split('/').filter(Boolean).pop() ?? ''); } catch { /* keep '' */ }
|
|
112
|
+
if (name) return name;
|
|
113
|
+
const ext = /^[a-z]+\/([a-z0-9.+-]+)/i.exec(mime)?.[1]?.replace('svg+xml', 'svg').replace('jpeg', 'jpg');
|
|
114
|
+
return ext ? 'file.' + ext : 'file';
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Ids of the assets referenced by PasteBlocks (children included), in order of appearance, deduplicated.
|
|
119
|
+
* @param {any[]} blocks
|
|
120
|
+
* @returns {string[]}
|
|
121
|
+
*/
|
|
122
|
+
export function collectAssetIds(blocks) {
|
|
123
|
+
/** @type {Set<string>} */
|
|
124
|
+
const ids = new Set();
|
|
125
|
+
mapAssetBlocks(blocks, (_pb, id) => { ids.add(id); });
|
|
126
|
+
return [...ids];
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @param {Uint8Array} bytes
|
|
131
|
+
* @returns {string}
|
|
132
|
+
*/
|
|
133
|
+
export function bytesToBase64(bytes) {
|
|
134
|
+
let binary = '';
|
|
135
|
+
for (let i = 0; i < bytes.length; i += 0x8000) binary += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
|
|
136
|
+
return btoa(binary);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* @param {string} b64
|
|
141
|
+
* @returns {Uint8Array | null} `null` when the string is not valid base64
|
|
142
|
+
*/
|
|
143
|
+
export function base64ToBytes(b64) {
|
|
144
|
+
try {
|
|
145
|
+
const binary = atob(b64);
|
|
146
|
+
const bytes = new Uint8Array(binary.length);
|
|
147
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
148
|
+
return bytes;
|
|
149
|
+
} catch {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
}
|