@aionbuilders/nabu 0.1.0-alpha.9 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +523 -0
- package/LICENSE +21 -0
- package/README.md +82 -144
- package/dist/behaviors/text/RichText.svelte +25 -2
- package/dist/behaviors/text/index.d.ts +2 -0
- package/dist/behaviors/text/index.js +2 -0
- package/dist/behaviors/text/markdown-shortcuts.extension.d.ts +5 -0
- package/dist/behaviors/text/markdown-shortcuts.extension.js +79 -0
- package/dist/behaviors/text/markdown-syntax.d.ts +42 -0
- package/dist/behaviors/text/markdown-syntax.js +81 -0
- package/dist/behaviors/text/rich-text.extension.d.ts +3 -0
- package/dist/behaviors/text/rich-text.extension.js +135 -53
- package/dist/behaviors/text/text.behavior.svelte.d.ts +217 -42
- package/dist/behaviors/text/text.behavior.svelte.js +657 -55
- package/dist/behaviors/text/typography.extension.d.ts +7 -0
- package/dist/behaviors/text/typography.extension.js +25 -0
- package/dist/blocks/Block.svelte +5 -2
- package/dist/blocks/BlockList.svelte +48 -0
- package/dist/blocks/BlockList.svelte.d.ts +17 -0
- package/dist/blocks/Nabu.svelte +180 -14
- package/dist/blocks/Nabu.svelte.d.ts +13 -0
- package/dist/blocks/block.extension.d.ts +3 -0
- package/dist/blocks/block.extension.js +8 -9
- package/dist/blocks/block.svelte.d.ts +356 -68
- package/dist/blocks/block.svelte.js +388 -42
- package/dist/blocks/blockquote/Blockquote.svelte +39 -0
- package/dist/blocks/blockquote/Blockquote.svelte.d.ts +11 -0
- package/dist/blocks/blockquote/blockquote.extension.d.ts +4 -0
- package/dist/blocks/blockquote/blockquote.extension.js +80 -0
- package/dist/blocks/blockquote/blockquote.svelte.d.ts +63 -0
- package/dist/blocks/blockquote/blockquote.svelte.js +136 -0
- package/dist/blocks/blockquote/index.d.ts +3 -0
- package/dist/blocks/blockquote/index.js +3 -0
- package/dist/blocks/callout/Callout.svelte +67 -0
- package/dist/blocks/callout/Callout.svelte.d.ts +11 -0
- package/dist/blocks/callout/callout.extension.d.ts +4 -0
- package/dist/blocks/callout/callout.extension.js +84 -0
- package/dist/blocks/callout/callout.svelte.d.ts +80 -0
- package/dist/blocks/callout/callout.svelte.js +189 -0
- package/dist/blocks/callout/index.d.ts +3 -0
- package/dist/blocks/callout/index.js +3 -0
- package/dist/blocks/chunks.d.ts +49 -0
- package/dist/blocks/chunks.js +90 -0
- package/dist/blocks/code/Code.svelte +84 -0
- package/dist/blocks/code/Code.svelte.d.ts +11 -0
- package/dist/blocks/code/code.extension.d.ts +4 -0
- package/dist/blocks/code/code.extension.js +88 -0
- package/dist/blocks/code/code.svelte.d.ts +68 -0
- package/dist/blocks/code/code.svelte.js +245 -0
- package/dist/blocks/code/index.d.ts +3 -0
- package/dist/blocks/code/index.js +3 -0
- package/dist/blocks/container.utils.d.ts +85 -13
- package/dist/blocks/container.utils.js +338 -178
- package/dist/blocks/dialogue/Dialogue.svelte +8 -2
- package/dist/blocks/dialogue/dialogue.svelte.d.ts +36 -23
- package/dist/blocks/dialogue/dialogue.svelte.js +51 -45
- package/dist/blocks/dialogue/hooks/onBeforeInput.hook.d.ts +2 -2
- package/dist/blocks/dialogue/hooks/onBeforeInput.hook.js +6 -4
- package/dist/blocks/dialogue/index.d.ts +12 -1
- package/dist/blocks/dialogue/index.js +51 -35
- package/dist/blocks/divider/Divider.svelte +28 -0
- package/dist/blocks/divider/Divider.svelte.d.ts +11 -0
- package/dist/blocks/divider/divider.extension.d.ts +23 -0
- package/dist/blocks/divider/divider.extension.js +153 -0
- package/dist/blocks/divider/divider.svelte.d.ts +54 -0
- package/dist/blocks/divider/divider.svelte.js +89 -0
- package/dist/blocks/divider/index.d.ts +3 -0
- package/dist/blocks/divider/index.js +3 -0
- package/dist/blocks/file/File.svelte +56 -0
- package/dist/blocks/file/File.svelte.d.ts +11 -0
- package/dist/blocks/file/file.extension.d.ts +4 -0
- package/dist/blocks/file/file.extension.js +27 -0
- package/dist/blocks/file/file.svelte.d.ts +29 -0
- package/dist/blocks/file/file.svelte.js +77 -0
- package/dist/blocks/file/index.d.ts +3 -0
- package/dist/blocks/file/index.js +3 -0
- package/dist/blocks/heading/Heading.svelte +2 -1
- package/dist/blocks/heading/heading.svelte.d.ts +27 -34
- package/dist/blocks/heading/heading.svelte.js +72 -60
- package/dist/blocks/heading/hooks/onBeforeInput.hook.js +7 -5
- package/dist/blocks/heading/index.d.ts +2 -2
- package/dist/blocks/heading/index.js +33 -8
- package/dist/blocks/image/Image.svelte +41 -0
- package/dist/blocks/image/Image.svelte.d.ts +11 -0
- package/dist/blocks/image/image.extension.d.ts +5 -0
- package/dist/blocks/image/image.extension.js +34 -0
- package/dist/blocks/image/image.svelte.d.ts +61 -0
- package/dist/blocks/image/image.svelte.js +114 -0
- package/dist/blocks/image/index.d.ts +3 -0
- package/dist/blocks/image/index.js +3 -0
- package/dist/blocks/index.d.ts +11 -1
- package/dist/blocks/index.js +11 -1
- package/dist/blocks/list/List.svelte +9 -2
- package/dist/blocks/list/ListItem.svelte +44 -7
- package/dist/blocks/list/convert.d.ts +20 -0
- package/dist/blocks/list/convert.js +92 -0
- package/dist/blocks/list/hooks/onBeforeInput.hook.js +60 -26
- package/dist/blocks/list/html-flat.d.ts +78 -0
- package/dist/blocks/list/html-flat.js +244 -0
- package/dist/blocks/list/indent.d.ts +86 -0
- package/dist/blocks/list/indent.js +205 -0
- package/dist/blocks/list/index.d.ts +7 -3
- package/dist/blocks/list/index.js +43 -31
- package/dist/blocks/list/list-item.svelte.d.ts +43 -17
- package/dist/blocks/list/list-item.svelte.js +143 -93
- package/dist/blocks/list/list.behavior.svelte.d.ts +7 -4
- package/dist/blocks/list/list.behavior.svelte.js +10 -5
- package/dist/blocks/list/list.svelte.d.ts +101 -23
- package/dist/blocks/list/list.svelte.js +136 -114
- package/dist/blocks/list/markdown.d.ts +149 -0
- package/dist/blocks/list/markdown.js +442 -0
- package/dist/blocks/list/normalize.d.ts +11 -0
- package/dist/blocks/list/normalize.js +396 -0
- package/dist/blocks/list/paste.d.ts +45 -0
- package/dist/blocks/list/paste.js +242 -0
- package/dist/blocks/list/step-back.d.ts +24 -0
- package/dist/blocks/list/step-back.js +87 -0
- package/dist/blocks/list/to-list.d.ts +7 -0
- package/dist/blocks/list/to-list.js +44 -0
- package/dist/blocks/list/todos-toggle.d.ts +20 -0
- package/dist/blocks/list/todos-toggle.js +48 -0
- package/dist/blocks/list/todos.svelte.d.ts +35 -0
- package/dist/blocks/list/todos.svelte.js +76 -0
- package/dist/blocks/media/index.d.ts +2 -0
- package/dist/blocks/media/index.js +2 -0
- package/dist/blocks/media/markdown.d.ts +12 -0
- package/dist/blocks/media/markdown.js +26 -0
- package/dist/blocks/media/media-block.svelte.d.ts +107 -0
- package/dist/blocks/media/media-block.svelte.js +181 -0
- package/dist/blocks/media/media.helpers.d.ts +24 -0
- package/dist/blocks/media/media.helpers.js +88 -0
- package/dist/blocks/megablock.svelte.d.ts +50 -19
- package/dist/blocks/megablock.svelte.js +115 -74
- package/dist/blocks/move.d.ts +23 -0
- package/dist/blocks/move.js +92 -0
- package/dist/blocks/nabu.svelte.d.ts +846 -75
- package/dist/blocks/nabu.svelte.js +1990 -182
- package/dist/blocks/paragraph/Paragraph.svelte +1 -0
- package/dist/blocks/paragraph/index.d.ts +2 -2
- package/dist/blocks/paragraph/index.js +35 -16
- package/dist/blocks/paragraph/paragraph.svelte.d.ts +49 -35
- package/dist/blocks/paragraph/paragraph.svelte.js +81 -69
- package/dist/blocks/paragraph/table.html.d.ts +9 -0
- package/dist/blocks/paragraph/table.html.js +111 -0
- package/dist/blocks/paragraph/table.markdown.d.ts +32 -0
- package/dist/blocks/paragraph/table.markdown.js +90 -0
- package/dist/blocks/region/Region.svelte +264 -0
- package/dist/blocks/region/Region.svelte.d.ts +11 -0
- package/dist/blocks/region/actions.d.ts +135 -0
- package/dist/blocks/region/actions.js +402 -0
- package/dist/blocks/region/close.d.ts +35 -0
- package/dist/blocks/region/close.js +130 -0
- package/dist/blocks/region/commands.d.ts +6 -0
- package/dist/blocks/region/commands.js +277 -0
- package/dist/blocks/region/create.d.ts +72 -0
- package/dist/blocks/region/create.js +208 -0
- package/dist/blocks/region/end-marker.d.ts +35 -0
- package/dist/blocks/region/end-marker.js +51 -0
- package/dist/blocks/region/facts.d.ts +69 -0
- package/dist/blocks/region/facts.js +152 -0
- package/dist/blocks/region/format.d.ts +91 -0
- package/dist/blocks/region/format.js +260 -0
- package/dist/blocks/region/index.d.ts +14 -0
- package/dist/blocks/region/index.js +14 -0
- package/dist/blocks/region/insert.d.ts +59 -0
- package/dist/blocks/region/insert.js +102 -0
- package/dist/blocks/region/messages.d.ts +254 -0
- package/dist/blocks/region/messages.js +131 -0
- package/dist/blocks/region/normalize.d.ts +7 -0
- package/dist/blocks/region/normalize.js +170 -0
- package/dist/blocks/region/region.extension.d.ts +58 -0
- package/dist/blocks/region/region.extension.js +119 -0
- package/dist/blocks/region/region.svelte.d.ts +221 -0
- package/dist/blocks/region/region.svelte.js +582 -0
- package/dist/blocks/region/regions.svelte.d.ts +141 -0
- package/dist/blocks/region/regions.svelte.js +276 -0
- package/dist/blocks/region/serialize.d.ts +57 -0
- package/dist/blocks/region/serialize.js +112 -0
- package/dist/blocks/region/types.d.ts +255 -0
- package/dist/blocks/region/types.js +204 -0
- package/dist/blocks/selection.svelte.d.ts +106 -8
- package/dist/blocks/selection.svelte.js +632 -74
- package/dist/blocks/subthread.d.ts +80 -0
- package/dist/blocks/subthread.js +167 -0
- package/dist/blocks/text-block.svelte.d.ts +70 -0
- package/dist/blocks/text-block.svelte.js +108 -0
- package/dist/blocks/toggle/Toggle.svelte +73 -0
- package/dist/blocks/toggle/Toggle.svelte.d.ts +11 -0
- package/dist/blocks/toggle/index.d.ts +3 -0
- package/dist/blocks/toggle/index.js +3 -0
- package/dist/blocks/toggle/markdown.d.ts +20 -0
- package/dist/blocks/toggle/markdown.js +112 -0
- package/dist/blocks/toggle/toggle.extension.d.ts +4 -0
- package/dist/blocks/toggle/toggle.extension.js +18 -0
- package/dist/blocks/toggle/toggle.svelte.d.ts +135 -0
- package/dist/blocks/toggle/toggle.svelte.js +352 -0
- package/dist/blocks/undo-selection.d.ts +175 -0
- package/dist/blocks/undo-selection.js +360 -0
- package/dist/blocks/video/Video.svelte +42 -0
- package/dist/blocks/video/Video.svelte.d.ts +11 -0
- package/dist/blocks/video/index.d.ts +3 -0
- package/dist/blocks/video/index.js +3 -0
- package/dist/blocks/video/video.extension.d.ts +4 -0
- package/dist/blocks/video/video.extension.js +27 -0
- package/dist/blocks/video/video.svelte.d.ts +29 -0
- package/dist/blocks/video/video.svelte.js +80 -0
- package/dist/extend.d.ts +55 -0
- package/dist/extend.js +88 -0
- package/dist/files/assets.clipboard.d.ts +77 -0
- package/dist/files/assets.clipboard.js +152 -0
- package/dist/files/files.svelte.d.ts +363 -0
- package/dist/files/files.svelte.js +897 -0
- package/dist/files/index.d.ts +3 -0
- package/dist/files/index.js +3 -0
- package/dist/files/outbox.d.ts +69 -0
- package/dist/files/outbox.js +48 -0
- package/dist/files/status.d.ts +65 -0
- package/dist/files/status.js +87 -0
- package/dist/files/store.d.ts +97 -0
- package/dist/files/store.js +65 -0
- package/dist/files/uploads.svelte.d.ts +174 -0
- package/dist/files/uploads.svelte.js +504 -0
- package/dist/index.d.ts +68 -6
- package/dist/index.js +117 -7
- package/dist/nabu-theme.css +112 -0
- package/dist/paste/html.paste.d.ts +74 -3
- package/dist/paste/html.paste.js +555 -83
- package/dist/paste/markdown-inline.d.ts +86 -0
- package/dist/paste/markdown-inline.js +528 -0
- package/dist/paste/markdown.paste.d.ts +2 -8
- package/dist/paste/markdown.paste.js +78 -94
- package/dist/paste/nabu.paste.d.ts +3 -0
- package/dist/paste/nabu.paste.js +29 -3
- package/dist/paste/plain-text.paste.d.ts +2 -2
- package/dist/paste/plain-text.paste.js +3 -3
- package/dist/persistence/asset-store.d.ts +51 -0
- package/dist/persistence/asset-store.js +103 -0
- package/dist/persistence/autosave.d.ts +53 -0
- package/dist/persistence/autosave.js +62 -0
- package/dist/persistence/compact.d.ts +162 -0
- package/dist/persistence/compact.js +141 -0
- package/dist/persistence/history.d.ts +77 -0
- package/dist/persistence/history.js +176 -0
- package/dist/persistence/indexeddb.d.ts +190 -4
- package/dist/persistence/indexeddb.js +333 -29
- package/dist/persistence/merge.d.ts +38 -0
- package/dist/persistence/merge.js +36 -0
- package/dist/persistence/outbox.d.ts +25 -0
- package/dist/persistence/outbox.js +40 -0
- package/dist/persistence/persistence.d.ts +70 -9
- package/dist/persistence/persistence.js +507 -35
- package/dist/persistence/recovery.d.ts +79 -0
- package/dist/persistence/recovery.js +79 -0
- package/dist/persistence/release.d.ts +34 -0
- package/dist/persistence/release.js +64 -0
- package/dist/persistence/state.svelte.d.ts +53 -0
- package/dist/persistence/state.svelte.js +48 -0
- package/dist/persistence/sync.d.ts +39 -0
- package/dist/persistence/sync.js +138 -0
- package/dist/persistence/tail.d.ts +42 -0
- package/dist/persistence/tail.js +103 -0
- package/dist/presets.d.ts +29 -24
- package/dist/presets.js +51 -14
- package/dist/themes/atelier.css +318 -0
- package/dist/ui/CommandChooser.svelte +153 -0
- package/dist/ui/CommandChooser.svelte.d.ts +13 -0
- package/dist/ui/CommandInvite.svelte +99 -0
- package/dist/ui/CommandInvite.svelte.d.ts +12 -0
- package/dist/ui/DragHandle.svelte +217 -0
- package/dist/ui/DragHandle.svelte.d.ts +11 -0
- package/dist/ui/FloatingToolbar.svelte +165 -0
- package/dist/ui/FloatingToolbar.svelte.d.ts +27 -0
- package/dist/ui/SlashMenu.svelte +215 -0
- package/dist/ui/SlashMenu.svelte.d.ts +33 -0
- package/dist/ui/anchor.d.ts +112 -0
- package/dist/ui/anchor.js +136 -0
- package/dist/ui/index.d.ts +10 -0
- package/dist/ui/index.js +15 -0
- package/dist/ui/slash-menu.svelte.d.ts +112 -0
- package/dist/ui/slash-menu.svelte.js +150 -0
- package/dist/utils/command-confirm.svelte.d.ts +349 -0
- package/dist/utils/command-confirm.svelte.js +673 -0
- package/dist/utils/commands.d.ts +328 -0
- package/dist/utils/commands.js +416 -0
- package/dist/utils/dom-scope.d.ts +14 -0
- package/dist/utils/dom-scope.js +20 -0
- package/dist/utils/drop.d.ts +160 -0
- package/dist/utils/drop.js +305 -0
- package/dist/utils/extensions.d.ts +488 -40
- package/dist/utils/extensions.js +300 -19
- package/dist/utils/html.d.ts +44 -0
- package/dist/utils/html.js +183 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.js +4 -0
- package/dist/utils/keys.d.ts +120 -0
- package/dist/utils/keys.js +248 -0
- package/dist/utils/links.d.ts +22 -0
- package/dist/utils/links.js +34 -0
- package/dist/utils/placeholders.d.ts +88 -0
- package/dist/utils/placeholders.js +91 -0
- package/dist/utils/selection.svelte.d.ts +47 -19
- package/dist/utils/selection.svelte.js +60 -10
- package/dist/utils/spellcheck.d.ts +9 -0
- package/dist/utils/spellcheck.js +17 -0
- package/dist/utils/tab.d.ts +31 -0
- package/dist/utils/tab.js +117 -0
- package/dist/utils/words.d.ts +7 -0
- package/dist/utils/words.js +62 -0
- package/dist/vite.d.ts +22 -0
- package/dist/vite.js +34 -0
- package/package.json +61 -22
- package/dist/blocks/zone/Zone.svelte +0 -258
- package/dist/blocks/zone/Zone.svelte.d.ts +0 -11
- package/dist/blocks/zone/index.d.ts +0 -2
- package/dist/blocks/zone/index.js +0 -2
- package/dist/blocks/zone/zone.extension.d.ts +0 -2
- package/dist/blocks/zone/zone.extension.js +0 -221
- package/dist/blocks/zone/zone.svelte.d.ts +0 -112
- package/dist/blocks/zone/zone.svelte.js +0 -321
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Command registry (ADR 017, lot 1): extensions DECLARE commands (label, keys, context, run); the core resolves the keys.
|
|
3
|
+
*
|
|
4
|
+
* - A command is ALSO an action of the bus: `nabu.exec(id, data)` runs it without any key (the engine wires that, see
|
|
5
|
+
* `Nabu#registerCommands`).
|
|
6
|
+
* - Key resolution (`handleKeydown`) runs from `Nabu#handleKeydown`, BEFORE the legacy `onKeyDown` hooks, and ignores IME
|
|
7
|
+
* compositions. See the doc of {@link CommandRegistry#handleKeydown} for the rules.
|
|
8
|
+
* - The registry names no block type: the context facts come from extensions (`Extension#commandContext`).
|
|
9
|
+
*
|
|
10
|
+
* @import { Nabu } from '../blocks/nabu.svelte.js'
|
|
11
|
+
* @import { Platform } from './keys.js'
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { detectPlatform, parseChord, chordId, eventChordId, formatChord, hasCommandModifier, isReservedChord } from './keys.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @typedef {Object} CommandContext
|
|
18
|
+
* What the engine builds at each key press (and for `nabu.exec` of a command, with `event: null`). Extensions add FACTS
|
|
19
|
+
* (any other property) through `Extension#commandContext`; facts never replace the fields below.
|
|
20
|
+
* @property {Nabu} nabu
|
|
21
|
+
* @property {KeyboardEvent | null} event the key press; `null` when the command runs from `exec` / `commands.run`
|
|
22
|
+
* @property {import('../blocks/block.svelte.js').Block | null} block the block holding the selection anchor
|
|
23
|
+
* @property {import('../blocks/selection.svelte.js').NabuSelection} selection
|
|
24
|
+
* @property {boolean} isCollapsed
|
|
25
|
+
* @property {string[]} ancestors types of the ancestors of `block`, nearest first (`[]` for a root block)
|
|
26
|
+
* @property {boolean} inRoot `block` is a direct child of the document (not nested in a container)
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* How a command that needs confirmation asks for it before a KEY PRESS runs it (`nabu.exec(id)` / `commands.run` run it
|
|
31
|
+
* directly, without confirmation: the accessible path). Controller: `command-confirm.svelte.js`.
|
|
32
|
+
*
|
|
33
|
+
* - `{ strategy: 'hold', holdMs?, target?(ctx), messages? }`: hold the chord for `holdMs` (default 700, global default: the
|
|
34
|
+
* `commandConfirm.holdMs` init option); `target(ctx)` is what draws the progress (a block, an element).
|
|
35
|
+
* - `{ strategy: 'invite', inviteMs?, messages? }`: first press asks, the same chord again confirms (timeout default 5000).
|
|
36
|
+
* - `{ strategy: 'choose', options(ctx), footer?, messages? }`: variants; `options` returns
|
|
37
|
+
* `[{ id, label, display?, group?, detail?, ...any }]` and `run(nabu, ctx, option)` receives the chosen one. `group` titles
|
|
38
|
+
* consecutive options under a small header, `detail` is a muted right-aligned text, `footer` (a string or `(ctx) => string`)
|
|
39
|
+
* a hint under the list. (Several commands tying on a key use `choose` automatically.)
|
|
40
|
+
* - `messages`: texts of the prompt / outcome for this command (see `ConfirmMessages`).
|
|
41
|
+
* @typedef {{ strategy: 'hold' | 'invite' | 'choose', [option: string]: any }} CommandConfirm
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @typedef {Object} CommandDef
|
|
46
|
+
* A command: a label, optional default keys, a `when` condition, a `scope` and a `run` function. Declared by extensions
|
|
47
|
+
* (`ExtensionInit.commands`); every command is also an action of the bus (`nabu.exec(id, data)`).
|
|
48
|
+
* @property {string | ((ctx: CommandContext) => string)} label text for help, prompts, pickers
|
|
49
|
+
* @property {string[]} [keys] default chords (`'Mod-Shift-Enter'`, see keys.js); overridable by the `keymap` option
|
|
50
|
+
* @property {(ctx: CommandContext) => boolean} [when] where the command applies; required for a key without modifier
|
|
51
|
+
* @property {string} [scope] block type the command belongs to: beats generic commands when the caret is in such a block,
|
|
52
|
+
* and limits the static conflict check to commands of the same scope
|
|
53
|
+
* @property {(nabu: Nabu, ctx: CommandContext, data?: any) => any} run a SYNCHRONOUS `false` from a key press means "not
|
|
54
|
+
* handled": resolution goes on (next command, legacy hooks, native behavior). Any other value (a promise included) = handled.
|
|
55
|
+
* @property {CommandConfirm} [confirm]
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @typedef {Object} CommandInfo
|
|
60
|
+
* What `nabu.commands.list()` / `get()` return: a plain snapshot (the registry is fixed once the editor is built, so it
|
|
61
|
+
* needs no reactivity; read it again after a `keymap` change, i.e. a new editor).
|
|
62
|
+
* @property {string} id
|
|
63
|
+
* @property {string} label resolved (a label function is called with the given / current context)
|
|
64
|
+
* @property {string[]} keys the chords bound to the command, as written (`'Mod-b'`)
|
|
65
|
+
* @property {string[]} display `keys` formatted for the platform (`'⌘B'` / `'Ctrl+B'`)
|
|
66
|
+
* @property {string | undefined} scope
|
|
67
|
+
* @property {string} extension name of the declaring extension (`'core'` for the engine's own commands)
|
|
68
|
+
* @property {boolean} available `when` is true in the given / current context (true without `when`)
|
|
69
|
+
* @property {CommandConfirm | null} confirm
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @typedef {Object} CommandEntry
|
|
74
|
+
* @property {string} id
|
|
75
|
+
* @property {CommandDef} def
|
|
76
|
+
* @property {string} extension
|
|
77
|
+
* @property {string | undefined} scope
|
|
78
|
+
* @property {string[]} keys accepted chords (as written)
|
|
79
|
+
* @property {number} order registration order (tie-break)
|
|
80
|
+
*/
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @typedef {Object} CommandRegistryOptions
|
|
84
|
+
* @property {Record<string, string[]>} [keymap] per-command replacement of the chords (`[]` disables the keys)
|
|
85
|
+
* @property {Platform} [platform] defaults to the detected platform
|
|
86
|
+
*/
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Holds the commands of an editor (`nabu.commands` is its public face): registers them from the extensions, resolves the keymap and
|
|
90
|
+
* the key chords at keydown, and answers `list`, `get`, `has` and `run`.
|
|
91
|
+
*/
|
|
92
|
+
export class CommandRegistry {
|
|
93
|
+
/** @param {Nabu} nabu @param {CommandRegistryOptions} [options] */
|
|
94
|
+
constructor(nabu, { keymap, platform } = {}) {
|
|
95
|
+
this.nabu = nabu;
|
|
96
|
+
/**
|
|
97
|
+
* @type {Platform} `Mod` is Cmd on `'mac'`, Ctrl on `'other'`
|
|
98
|
+
* @public
|
|
99
|
+
*/
|
|
100
|
+
this.platform = platform ?? detectPlatform();
|
|
101
|
+
/** @type {Record<string, string[]>} */
|
|
102
|
+
this.keymap = keymap && typeof keymap === 'object' ? keymap : {};
|
|
103
|
+
/** @type {Map<string, CommandEntry>} */
|
|
104
|
+
this.entries = new Map();
|
|
105
|
+
/** canonical chord -> entries bound to it, in registration order @type {Map<string, CommandEntry[]>} */
|
|
106
|
+
this.byChord = new Map();
|
|
107
|
+
/** @type {Array<{ extension: string, provide: (nabu: Nabu, ctx: CommandContext) => Record<string, any> | void }>} */
|
|
108
|
+
this.providers = [];
|
|
109
|
+
/** @type {Set<string>} ambiguities already reported (once per chord + commands) */
|
|
110
|
+
this.reported = new Set();
|
|
111
|
+
this.counter = 0;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Register a command. The command always exists (it is runnable through `exec`); only its KEYS can be refused:
|
|
116
|
+
* - invalid chord, or reserved by the browser / OS (see `RESERVED_CHORDS`) -> that chord is dropped, `nabu.warn`;
|
|
117
|
+
* - a chord without Ctrl / Alt / Meta (Backspace, Enter, a letter, Shift+letter…) on a command with no `when` -> dropped
|
|
118
|
+
* (it would swallow the key everywhere), `nabu.warn`;
|
|
119
|
+
* - the command has a `confirm` with an unknown strategy (or `choose` without `options`) -> ALL its keys dropped: a risky
|
|
120
|
+
* command is never run from a key without its confirmation.
|
|
121
|
+
* - another command with the same chord AND the same `scope` (or both unscoped) -> `nabu.warn`, both kept (the
|
|
122
|
+
* ambiguity is settled at key press, see {@link CommandRegistry#handleKeydown}).
|
|
123
|
+
* A duplicate id is refused (warning, the first registration wins). The `keymap` option replaces `def.keys`, with the
|
|
124
|
+
* same checks.
|
|
125
|
+
* @param {string} id
|
|
126
|
+
* @param {CommandDef} def
|
|
127
|
+
* @param {string} [extension]
|
|
128
|
+
* @returns {CommandEntry | null} null when the declaration is unusable (no `run`, duplicate id)
|
|
129
|
+
*/
|
|
130
|
+
register(id, def, extension = 'core') {
|
|
131
|
+
if (!def || typeof def.run !== 'function') { this.nabu.warn(`commands: "${id}" (${extension}) has no run(): ignored`); return null; }
|
|
132
|
+
if (this.entries.has(id)) { this.nabu.warn(`commands: duplicate id "${id}" (${extension}): the first declaration is kept`); return null; }
|
|
133
|
+
if (typeof def.label !== 'string' && typeof def.label !== 'function') this.nabu.warn(`commands: "${id}" has no label (string or function)`);
|
|
134
|
+
|
|
135
|
+
/** @type {CommandEntry} */
|
|
136
|
+
const entry = { id, def, extension, scope: typeof def.scope === 'string' ? def.scope : undefined, keys: [], order: this.counter++ };
|
|
137
|
+
this.entries.set(id, entry);
|
|
138
|
+
|
|
139
|
+
const override = Object.prototype.hasOwnProperty.call(this.keymap, id) ? this.keymap[id] : undefined;
|
|
140
|
+
if (override !== undefined && !Array.isArray(override)) this.nabu.warn(`commands: keymap["${id}"] must be an array of chords: ignored`);
|
|
141
|
+
const wanted = Array.isArray(override) ? override : Array.isArray(def.keys) ? def.keys : [];
|
|
142
|
+
if (def.confirm && wanted.length) {
|
|
143
|
+
const confirm = /** @type {any} */ (def.confirm);
|
|
144
|
+
const known = confirm.strategy === 'hold' || confirm.strategy === 'invite' || (confirm.strategy === 'choose' && (typeof confirm.options === 'function' || Array.isArray(confirm.options)));
|
|
145
|
+
if (!known) {
|
|
146
|
+
this.nabu.warn(`commands: "${id}": unknown \`confirm\` strategy ${JSON.stringify(confirm.strategy)} (or \`choose\` without \`options\`): its keys are not bound; it still runs through nabu.exec("${id}")`);
|
|
147
|
+
return entry;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
for (const text of wanted) this.#bind(entry, text);
|
|
151
|
+
return entry;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* @param {CommandEntry} entry
|
|
156
|
+
* @param {string} text
|
|
157
|
+
*/
|
|
158
|
+
#bind(entry, text) {
|
|
159
|
+
const { id, def } = entry;
|
|
160
|
+
const chord = parseChord(text, { platform: this.platform });
|
|
161
|
+
if (!chord) { this.nabu.warn(`commands: "${id}": invalid chord ${JSON.stringify(text)}: ignored`); return; }
|
|
162
|
+
const key = chordId(chord);
|
|
163
|
+
if (isReservedChord(chord, this.platform)) { this.nabu.warn(`commands: "${id}": chord "${text}" is reserved by the browser / OS: ignored`); return; }
|
|
164
|
+
if (!hasCommandModifier(chord) && typeof def.when !== 'function') {
|
|
165
|
+
this.nabu.warn(`commands: "${id}": chord "${text}" has no modifier and the command has no \`when\`: ignored (it would swallow the key everywhere)`);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
let list = this.byChord.get(key);
|
|
169
|
+
if (!list) this.byChord.set(key, list = []);
|
|
170
|
+
if (list.includes(entry)) return; // the same chord written twice
|
|
171
|
+
const twin = list.find((other) => other.scope === entry.scope);
|
|
172
|
+
if (twin) this.nabu.warn(`commands: "${id}" and "${twin.id}" both bind "${text}" with the same scope (${entry.scope ?? 'none'}): ambiguous, the first registered wins unless their \`when\` differ`);
|
|
173
|
+
list.push(entry);
|
|
174
|
+
entry.keys.push(text);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Warn about `keymap` entries that name no registered command (call once every extension is registered).
|
|
179
|
+
*/
|
|
180
|
+
checkKeymap() {
|
|
181
|
+
for (const id of Object.keys(this.keymap)) {
|
|
182
|
+
if (!this.entries.has(id)) this.nabu.warn(`commands: keymap["${id}"] names no registered command`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Let an extension contribute FACTS to the context (`Extension#commandContext`). Facts are merged after the core fields
|
|
188
|
+
* and never replace them; a provider that throws is ignored (warning).
|
|
189
|
+
* @param {(nabu: Nabu, ctx: CommandContext) => Record<string, any> | void} provide
|
|
190
|
+
* @param {string} [extension]
|
|
191
|
+
*/
|
|
192
|
+
addContextProvider(provide, extension = 'core') {
|
|
193
|
+
if (typeof provide === 'function') this.providers.push({ extension, provide });
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Build the context a command sees: the core fields, then the facts of the extensions.
|
|
198
|
+
* @param {KeyboardEvent | null} [event]
|
|
199
|
+
* @returns {CommandContext}
|
|
200
|
+
* @public
|
|
201
|
+
*/
|
|
202
|
+
context(event = null) {
|
|
203
|
+
const nabu = this.nabu;
|
|
204
|
+
const selection = nabu.selection;
|
|
205
|
+
const block = selection.anchorBlock ?? null;
|
|
206
|
+
/** @type {CommandContext} */
|
|
207
|
+
const ctx = {
|
|
208
|
+
nabu,
|
|
209
|
+
event,
|
|
210
|
+
block,
|
|
211
|
+
selection,
|
|
212
|
+
isCollapsed: !!selection.isCollapsed,
|
|
213
|
+
ancestors: block ? block.parents.map((parent) => parent.type) : [],
|
|
214
|
+
inRoot: !!block && !block.parent,
|
|
215
|
+
};
|
|
216
|
+
for (const { extension, provide } of this.providers) {
|
|
217
|
+
try {
|
|
218
|
+
const facts = provide(nabu, ctx);
|
|
219
|
+
if (facts && typeof facts === 'object') {
|
|
220
|
+
for (const [name, value] of Object.entries(facts)) if (!CORE_FIELDS.has(name)) /** @type {any} */ (ctx)[name] = value;
|
|
221
|
+
}
|
|
222
|
+
} catch (error) {
|
|
223
|
+
nabu.warn(`commands: commandContext of "${extension}" failed: ${error}`);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return ctx;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Key resolution, called by `Nabu#handleKeydown` before the legacy `onKeyDown` hooks. Returns whether a command
|
|
231
|
+
* handled the key (the event is then `preventDefault`ed); `false` = fall through to the hooks and the native behavior.
|
|
232
|
+
*
|
|
233
|
+
* 1. nothing happens during an IME composition, or when someone already `preventDefault`ed the event;
|
|
234
|
+
* 2. the chord of the event (keys.js) selects the commands bound to it; none -> return (no context is even built);
|
|
235
|
+
* 3. candidates are the commands whose `when(ctx)` is true (absent = true; a throwing `when` = false);
|
|
236
|
+
* 4. ONE -> it runs, through its `confirm` strategy when it has one (the controller keeps the key from there: see
|
|
237
|
+
* `command-confirm.svelte.js`). SEVERAL -> a command whose `scope` is the type of the caret's block beats generic ones;
|
|
238
|
+
* still tied -> the `choose` strategy lets the user pick (and the ambiguity is reported once with `nabu.warn`: the
|
|
239
|
+
* default keys should not step on each other);
|
|
240
|
+
* 5. a command whose `run` returns `false` synchronously did not handle the key: the next candidate (in priority order)
|
|
241
|
+
* is tried, then the legacy hooks.
|
|
242
|
+
* @param {KeyboardEvent} event
|
|
243
|
+
* @returns {boolean}
|
|
244
|
+
*/
|
|
245
|
+
handleKeydown(event) {
|
|
246
|
+
if (this.nabu.readonly) return false; // a read-only editor has no shortcut
|
|
247
|
+
if (event.isComposing || event.keyCode === 229 || this.nabu.composing) return false;
|
|
248
|
+
const confirmation = this.nabu.commandConfirm;
|
|
249
|
+
if (confirmation?.busy) {
|
|
250
|
+
if (event.defaultPrevented) confirmation.cancel('key'); // somebody else took the key (slash menu…): the pending one is moot
|
|
251
|
+
else if (confirmation.handleKeydown(event)) return true;
|
|
252
|
+
}
|
|
253
|
+
if (!this.byChord.size || event.defaultPrevented) return false;
|
|
254
|
+
const chord = eventChordId(event);
|
|
255
|
+
const bound = chord ? this.byChord.get(chord) : undefined;
|
|
256
|
+
if (!bound?.length) return false;
|
|
257
|
+
|
|
258
|
+
const ctx = this.context(event);
|
|
259
|
+
const type = ctx.block?.type;
|
|
260
|
+
const candidates = bound.filter((entry) => {
|
|
261
|
+
if (typeof entry.def.when !== 'function') return true;
|
|
262
|
+
try { return !!entry.def.when(ctx); } catch (error) { this.nabu.warn(`commands: when() of "${entry.id}" failed: ${error}`); return false; }
|
|
263
|
+
});
|
|
264
|
+
if (!candidates.length) return false;
|
|
265
|
+
|
|
266
|
+
const rank = (/** @type {CommandEntry} */ entry) => (entry.scope !== undefined && entry.scope === type ? 1 : 0);
|
|
267
|
+
candidates.sort((a, b) => rank(b) - rank(a) || a.order - b.order); // stable by registration order
|
|
268
|
+
if (candidates.length > 1 && rank(candidates[0]) === rank(candidates[1])) {
|
|
269
|
+
const ties = candidates.filter((entry) => rank(entry) === rank(candidates[0])).map((entry) => entry.id);
|
|
270
|
+
const signature = `${chord}|${ties.join(',')}`;
|
|
271
|
+
if (!this.reported.has(signature)) {
|
|
272
|
+
this.reported.add(signature);
|
|
273
|
+
this.nabu.warn(`commands: ambiguous key ${chord}: ${ties.join(', ')} all apply here; the user is asked to choose`);
|
|
274
|
+
}
|
|
275
|
+
if (confirmation && confirmation.beginTie(candidates.filter((entry) => rank(entry) === rank(candidates[0])), ctx, event)) return true;
|
|
276
|
+
}
|
|
277
|
+
for (const entry of candidates) {
|
|
278
|
+
// A command that asks for confirmation never runs from the key press itself: the controller takes over.
|
|
279
|
+
if (entry.def.confirm && confirmation) {
|
|
280
|
+
if (confirmation.begin(entry, ctx, event)) return true;
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
let result;
|
|
284
|
+
try {
|
|
285
|
+
result = entry.def.run(this.nabu, ctx);
|
|
286
|
+
} catch (error) {
|
|
287
|
+
console.error(`[Nabu] command "${entry.id}" failed`, error);
|
|
288
|
+
event.preventDefault();
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
if (result === false) continue;
|
|
292
|
+
if (result && typeof result.then === 'function') result.then(undefined, (/** @type {unknown} */ error) => console.error(`[Nabu] command "${entry.id}" failed`, error));
|
|
293
|
+
event.preventDefault();
|
|
294
|
+
return true;
|
|
295
|
+
}
|
|
296
|
+
return false;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Run a command from the bus (`nabu.exec(id, data)`): no key press, `ctx.event` is `null`, `when` is not checked.
|
|
301
|
+
* @param {CommandEntry} entry
|
|
302
|
+
* @param {any} [data]
|
|
303
|
+
* @public
|
|
304
|
+
*/
|
|
305
|
+
invoke(entry, data) {
|
|
306
|
+
if (this.nabu.readonly) return undefined; // read-only: commands are edits, they do nothing
|
|
307
|
+
return entry.def.run(this.nabu, this.context(null), data);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Run a command by id, like `nabu.exec(id, data)` (which it calls).
|
|
312
|
+
* @param {string} id
|
|
313
|
+
* @param {any} [data]
|
|
314
|
+
* @public
|
|
315
|
+
*/
|
|
316
|
+
run(id, data) {
|
|
317
|
+
if (!this.entries.has(id)) { this.nabu.warn(`commands.run: unknown command "${id}"`); return undefined; }
|
|
318
|
+
return this.nabu.exec(id, data);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Open the confirmation of a command from code (a button, a menu, a palette, a typed trigger), as if its key had been
|
|
323
|
+
* pressed where `when` holds. This is the public way to start a confirmation, since `nabu.exec(id)` runs at once:
|
|
324
|
+
*
|
|
325
|
+
* - the command has a `confirm`: its prompt opens (`hold`, `invite` or `choose`, see {@link CommandConfirm}), exactly as
|
|
326
|
+
* for a key press: `nabu.pendingCommand`, the `CommandInvite` / `CommandChooser` components, keyboard handling and
|
|
327
|
+
* cancellations. Nothing has run yet; the command runs when the user confirms. `invite` expects its first key again and
|
|
328
|
+
* `hold` expects its first key to be held (counting from that press); both lapse after the `invite` timeout (5 s) when
|
|
329
|
+
* no key comes, and need the command to have a key (else nothing opens);
|
|
330
|
+
* - the command has NO `confirm`: there is nothing to ask, so it simply runs, like `nabu.exec(id)`, `when` checked.
|
|
331
|
+
*
|
|
332
|
+
* Returns `true` when a confirmation opened, or when a command without confirmation ran; `false` when nothing happened:
|
|
333
|
+
* unknown id (warning), `when` is false, no option for a `choose`, no key for a `hold` / `invite`, or a command without
|
|
334
|
+
* confirmation whose `run` returned `false`.
|
|
335
|
+
* @param {string} id command id
|
|
336
|
+
* @param {CommandContext} [ctx] default: the current context (`event: null`)
|
|
337
|
+
* @returns {boolean}
|
|
338
|
+
* @public
|
|
339
|
+
*/
|
|
340
|
+
offer(id, ctx) {
|
|
341
|
+
if (this.nabu.readonly) return false;
|
|
342
|
+
const entry = this.entries.get(id);
|
|
343
|
+
if (!entry) { this.nabu.warn(`commands.offer: unknown command "${id}"`); return false; }
|
|
344
|
+
if (entry.def.confirm) return this.nabu.commandConfirm.offer(id, ctx);
|
|
345
|
+
const context = ctx ?? this.context(null);
|
|
346
|
+
if (typeof entry.def.when === 'function') {
|
|
347
|
+
let ok = false;
|
|
348
|
+
try { ok = !!entry.def.when(context); } catch (error) { this.nabu.warn(`commands: when() of "${id}" failed: ${error}`); }
|
|
349
|
+
if (!ok) return false;
|
|
350
|
+
}
|
|
351
|
+
let result;
|
|
352
|
+
try { result = entry.def.run(this.nabu, context); }
|
|
353
|
+
catch (error) { console.error(`[Nabu] command "${id}" failed`, error); return false; }
|
|
354
|
+
if (result && typeof result.then === 'function') result.then(undefined, (/** @type {unknown} */ error) => console.error(`[Nabu] command "${id}" failed`, error));
|
|
355
|
+
return result !== false;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* @param {CommandEntry} entry
|
|
360
|
+
* @param {() => CommandContext} getContext
|
|
361
|
+
* @returns {CommandInfo}
|
|
362
|
+
*/
|
|
363
|
+
#info(entry, getContext) {
|
|
364
|
+
const { def } = entry;
|
|
365
|
+
let label = entry.id;
|
|
366
|
+
if (typeof def.label === 'string') label = def.label;
|
|
367
|
+
else if (typeof def.label === 'function') { try { label = String(def.label(getContext())); } catch (error) { this.nabu.warn(`commands: label of "${entry.id}" failed: ${error}`); } }
|
|
368
|
+
let available = true;
|
|
369
|
+
if (typeof def.when === 'function') { try { available = !!def.when(getContext()); } catch { available = false; } }
|
|
370
|
+
return {
|
|
371
|
+
id: entry.id,
|
|
372
|
+
label,
|
|
373
|
+
keys: [...entry.keys],
|
|
374
|
+
display: entry.keys.map((text) => formatChord(text, { platform: this.platform })),
|
|
375
|
+
scope: entry.scope,
|
|
376
|
+
extension: entry.extension,
|
|
377
|
+
available,
|
|
378
|
+
confirm: def.confirm ?? null,
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Every command, in registration order, as a plain snapshot (for a help sheet, a palette). Function labels and `when`
|
|
384
|
+
* are evaluated with `ctx` (default: the current context, `event: null`).
|
|
385
|
+
* @param {CommandContext} [ctx]
|
|
386
|
+
* @returns {CommandInfo[]}
|
|
387
|
+
* @public
|
|
388
|
+
*/
|
|
389
|
+
list(ctx) {
|
|
390
|
+
/** @type {CommandContext | undefined} */
|
|
391
|
+
let current = ctx;
|
|
392
|
+
const getContext = () => (current ??= this.context(null));
|
|
393
|
+
return [...this.entries.values()].map((entry) => this.#info(entry, getContext));
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* @param {string} id
|
|
398
|
+
* @param {CommandContext} [ctx]
|
|
399
|
+
* @returns {CommandInfo | null}
|
|
400
|
+
* @public
|
|
401
|
+
*/
|
|
402
|
+
get(id, ctx) {
|
|
403
|
+
const entry = this.entries.get(id);
|
|
404
|
+
return entry ? this.#info(entry, () => ctx ?? this.context(null)) : null;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* @param {string} id
|
|
409
|
+
* @returns {boolean}
|
|
410
|
+
* @public
|
|
411
|
+
*/
|
|
412
|
+
has(id) { return this.entries.has(id); }
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/** Context fields the extensions' facts cannot replace. */
|
|
416
|
+
const CORE_FIELDS = new Set(['nabu', 'event', 'block', 'selection', 'isCollapsed', 'ancestors', 'inRoot']);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helpers for a DOM that may hold SEVERAL editors: side by side on a page, or one inside another (an editor mounted in a UI island of
|
|
3
|
+
* a block). A marker such as `[data-nabu-ui]` or `[data-nabu-inert]` found ABOVE an editor's root belongs to the editor around it
|
|
4
|
+
* (or to the page), never to that editor's own content.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* The nearest ancestor-or-self of `node` that matches `selector` AND lies inside `root` (the root itself included); null when
|
|
8
|
+
* the nearest match is outside, i.e. when nothing between `node` and `root` matches. Safe to call with no root (null).
|
|
9
|
+
* @param {Node | null | undefined} node
|
|
10
|
+
* @param {string} selector
|
|
11
|
+
* @param {Element | null | undefined} root
|
|
12
|
+
* @returns {Element | null}
|
|
13
|
+
*/
|
|
14
|
+
export function closestWithin(node: Node | null | undefined, selector: string, root: Element | null | undefined): Element | null;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helpers for a DOM that may hold SEVERAL editors: side by side on a page, or one inside another (an editor mounted in a UI island of
|
|
3
|
+
* a block). A marker such as `[data-nabu-ui]` or `[data-nabu-inert]` found ABOVE an editor's root belongs to the editor around it
|
|
4
|
+
* (or to the page), never to that editor's own content.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The nearest ancestor-or-self of `node` that matches `selector` AND lies inside `root` (the root itself included); null when
|
|
9
|
+
* the nearest match is outside, i.e. when nothing between `node` and `root` matches. Safe to call with no root (null).
|
|
10
|
+
* @param {Node | null | undefined} node
|
|
11
|
+
* @param {string} selector
|
|
12
|
+
* @param {Element | null | undefined} root
|
|
13
|
+
* @returns {Element | null}
|
|
14
|
+
*/
|
|
15
|
+
export function closestWithin(node, selector, root) {
|
|
16
|
+
if (!node || !root) return null;
|
|
17
|
+
const el = /** @type {Element | null} */ (node.nodeType === Node.ELEMENT_NODE ? node : node.parentElement);
|
|
18
|
+
const hit = el?.closest(selector) ?? null;
|
|
19
|
+
return hit && root.contains(hit) ? hit : null;
|
|
20
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a drag carries, from its `types` alone (the data itself is not readable before the drop).
|
|
3
|
+
* - an internal fragment is `content` even when files come along (the fragment already holds the bytes);
|
|
4
|
+
* - `Files` is `files` when some extension takes files, else it counts as nothing;
|
|
5
|
+
* - any format an interpreter reads is `content`.
|
|
6
|
+
* @param {ArrayLike<string> | null | undefined} types `DataTransfer#types`
|
|
7
|
+
* @param {{ formats: string[], files: boolean }} ctx `formats`: the formats the paste interpreters read; `files`: whether a file handler exists
|
|
8
|
+
* @returns {'files' | 'content' | null}
|
|
9
|
+
*/
|
|
10
|
+
export function transferKind(types: ArrayLike<string> | null | undefined, ctx: {
|
|
11
|
+
formats: string[];
|
|
12
|
+
files: boolean;
|
|
13
|
+
}): "files" | "content" | null;
|
|
14
|
+
/**
|
|
15
|
+
* The `dropEffect` to announce: `wanted` when the drag source's `effectAllowed` permits it, else the first effect it
|
|
16
|
+
* does allow (announcing a forbidden effect makes the browser refuse the drop).
|
|
17
|
+
* @param {string | undefined} effectAllowed
|
|
18
|
+
* @param {'copy' | 'move'} wanted
|
|
19
|
+
* @returns {'copy' | 'move' | 'link'}
|
|
20
|
+
*/
|
|
21
|
+
export function pickDropEffect(effectAllowed: string | undefined, wanted: "copy" | "move"): "copy" | "move" | "link";
|
|
22
|
+
/**
|
|
23
|
+
* Index of the box the pointer at `y` belongs to: the SMALLEST box containing `y` (the deepest block), else the box
|
|
24
|
+
* with the nearest edge (ties: the smallest). -1 for no box.
|
|
25
|
+
* @param {Array<{ top: number, bottom: number }>} rects
|
|
26
|
+
* @param {number} y
|
|
27
|
+
* @returns {number}
|
|
28
|
+
*/
|
|
29
|
+
export function nearestRect(rects: Array<{
|
|
30
|
+
top: number;
|
|
31
|
+
bottom: number;
|
|
32
|
+
}>, y: number): number;
|
|
33
|
+
/**
|
|
34
|
+
* Document order of two block elements, a block's own text coming before its children (pre-order): -1 when `a` is
|
|
35
|
+
* before `b`, 1 when after, 0 for the same block.
|
|
36
|
+
* @param {Element} a
|
|
37
|
+
* @param {Element} b
|
|
38
|
+
* @returns {-1 | 0 | 1}
|
|
39
|
+
*/
|
|
40
|
+
export function compareBlocks(a: Element, b: Element): -1 | 0 | 1;
|
|
41
|
+
/**
|
|
42
|
+
* Whether a drop position lies in a model range, its two ends included (dropping a selection on itself, or right at its
|
|
43
|
+
* edge, changes nothing). A position beside a block (before / after) is in the range when that block is.
|
|
44
|
+
* @param {{ startBlock: Block, startOffset: number, endBlock: Block, endOffset: number }} range
|
|
45
|
+
* @param {DropPosition} at
|
|
46
|
+
* @returns {boolean}
|
|
47
|
+
*/
|
|
48
|
+
export function positionInRange(range: {
|
|
49
|
+
startBlock: Block;
|
|
50
|
+
startOffset: number;
|
|
51
|
+
endBlock: Block;
|
|
52
|
+
endOffset: number;
|
|
53
|
+
}, at: DropPosition): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Where a drop position goes once `range` has been removed from the document (`deleteRange`): a position after the range
|
|
56
|
+
* in the same block shifts back by the removed length; one in the range's end block, when that block was merged into the
|
|
57
|
+
* start block, follows its text there. `endMerged`: the end block no longer exists.
|
|
58
|
+
* @param {{ startBlock: Block, startOffset: number, endBlock: Block, endOffset: number }} range
|
|
59
|
+
* @param {DropPosition} at a position outside the range
|
|
60
|
+
* @param {boolean} endMerged
|
|
61
|
+
* @returns {DropPosition}
|
|
62
|
+
*/
|
|
63
|
+
export function positionAfterRemoval(range: {
|
|
64
|
+
startBlock: Block;
|
|
65
|
+
startOffset: number;
|
|
66
|
+
endBlock: Block;
|
|
67
|
+
endOffset: number;
|
|
68
|
+
}, at: DropPosition, endMerged: boolean): DropPosition;
|
|
69
|
+
/**
|
|
70
|
+
* The DOM point under a viewport coordinate (`caretPositionFromPoint` — standard, Firefox / Chromium ≥ 128 — or the
|
|
71
|
+
* older `caretRangeFromPoint` — WebKit / Blink).
|
|
72
|
+
* @param {number} x
|
|
73
|
+
* @param {number} y
|
|
74
|
+
* @returns {{ node: Node, offset: number } | null}
|
|
75
|
+
*/
|
|
76
|
+
export function caretPointFromPoint(x: number, y: number): {
|
|
77
|
+
node: Node;
|
|
78
|
+
offset: number;
|
|
79
|
+
} | null;
|
|
80
|
+
/**
|
|
81
|
+
* The model position under the pointer. Text blocks: the caret position under the pointer (else the start / end of the
|
|
82
|
+
* block, by the pointer's half). Blocks without text or children (image, divider…): before / after, by the pointer's
|
|
83
|
+
* half. Anywhere else (padding of the editor, gaps between blocks, containers): the nearest block, so that a pointer
|
|
84
|
+
* above the first block lands at the start of the document and below the last at its end.
|
|
85
|
+
* @param {Nabu} nabu
|
|
86
|
+
* @param {HTMLElement} root the editor's root element
|
|
87
|
+
* @param {number} x viewport coordinates
|
|
88
|
+
* @param {number} y
|
|
89
|
+
* @returns {DropPosition | null}
|
|
90
|
+
*/
|
|
91
|
+
export function resolveDropPosition(nabu: Nabu, root: HTMLElement, x: number, y: number): DropPosition | null;
|
|
92
|
+
/**
|
|
93
|
+
* The drop position for a pointer that is on no block: the vertically nearest block that is a leaf (a text block or a
|
|
94
|
+
* block without text and children), the start / end of its text or beside it, by the pointer's half.
|
|
95
|
+
* @param {Nabu} nabu
|
|
96
|
+
* @param {number} y viewport ordinate
|
|
97
|
+
* @returns {DropPosition | null}
|
|
98
|
+
*/
|
|
99
|
+
export function nearestBlockPosition(nabu: Nabu, y: number): DropPosition | null;
|
|
100
|
+
/**
|
|
101
|
+
* The end of the document as a drop position: the end of the last visible block's text, or after it when it holds none.
|
|
102
|
+
* @param {Nabu} nabu
|
|
103
|
+
* @returns {DropPosition | null}
|
|
104
|
+
*/
|
|
105
|
+
export function documentEnd(nabu: Nabu): DropPosition | null;
|
|
106
|
+
/**
|
|
107
|
+
* @import { Nabu } from '../blocks/nabu.svelte.js';
|
|
108
|
+
* @import { Block } from '../blocks/block.svelte.js';
|
|
109
|
+
*/
|
|
110
|
+
/**
|
|
111
|
+
* @typedef {'inside' | 'before' | 'after'} DropSide
|
|
112
|
+
* `inside`: a caret position in the text of `block`; `before` / `after`: beside a block without text (image, divider…).
|
|
113
|
+
*/
|
|
114
|
+
/**
|
|
115
|
+
* @typedef {Object} DropPosition
|
|
116
|
+
* @property {Block} block
|
|
117
|
+
* @property {number} offset Text offset in `block` (0 when `side` is not `inside`).
|
|
118
|
+
* @property {DropSide} side
|
|
119
|
+
*/
|
|
120
|
+
/**
|
|
121
|
+
* @typedef {'files' | 'content' | 'move'} DropKind
|
|
122
|
+
* `files`: OS files (become media blocks); `content`: text / HTML / markdown / Nabu fragment from outside (or a copy);
|
|
123
|
+
* `move`: the editor's own text selection being moved.
|
|
124
|
+
*/
|
|
125
|
+
/** @typedef {DropPosition & { kind: DropKind }} DropTarget */
|
|
126
|
+
/** MIME type that marks a drag started inside an editor (its value is a per-drag token). */
|
|
127
|
+
export const DRAG_MARKER: "application/x-nabu-drag";
|
|
128
|
+
/** The Nabu clipboard / drag fragment format (see `Nabu#handleCopy`). */
|
|
129
|
+
export const NABU_FORMAT: "application/x-nabu+json";
|
|
130
|
+
export function wantsCopy(e: {
|
|
131
|
+
ctrlKey?: boolean;
|
|
132
|
+
altKey?: boolean;
|
|
133
|
+
}): boolean;
|
|
134
|
+
export function pointerHalf(y: number, rect: {
|
|
135
|
+
top: number;
|
|
136
|
+
bottom: number;
|
|
137
|
+
}): "before" | "after";
|
|
138
|
+
export function sameTarget(a: DropTarget | null, b: DropTarget | null): boolean;
|
|
139
|
+
/**
|
|
140
|
+
* `inside`: a caret position in the text of `block`; `before` / `after`: beside a block without text (image, divider…).
|
|
141
|
+
*/
|
|
142
|
+
export type DropSide = "inside" | "before" | "after";
|
|
143
|
+
export type DropPosition = {
|
|
144
|
+
block: Block;
|
|
145
|
+
/**
|
|
146
|
+
* Text offset in `block` (0 when `side` is not `inside`).
|
|
147
|
+
*/
|
|
148
|
+
offset: number;
|
|
149
|
+
side: DropSide;
|
|
150
|
+
};
|
|
151
|
+
/**
|
|
152
|
+
* `files`: OS files (become media blocks); `content`: text / HTML / markdown / Nabu fragment from outside (or a copy);
|
|
153
|
+
* `move`: the editor's own text selection being moved.
|
|
154
|
+
*/
|
|
155
|
+
export type DropKind = "files" | "content" | "move";
|
|
156
|
+
export type DropTarget = DropPosition & {
|
|
157
|
+
kind: DropKind;
|
|
158
|
+
};
|
|
159
|
+
import type { Block } from '../blocks/block.svelte.js';
|
|
160
|
+
import type { Nabu } from '../blocks/nabu.svelte.js';
|