@aionbuilders/nabu 0.1.0-beta.0 → 0.1.1
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 +531 -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 +195 -55
- package/dist/behaviors/text/text.behavior.svelte.js +531 -103
- 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 +32 -18
- 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 +1753 -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 +35 -14
- 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 +55 -8
- 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/composition.d.ts +28 -0
- package/dist/utils/composition.js +51 -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 +48 -20
- 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 +42 -0
- package/dist/utils/words.js +147 -0
- package/dist/vite.d.ts +9 -3
- package/dist/vite.js +19 -8
- package/package.json +56 -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
|
@@ -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,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chromium workaround for IME compositions that start after an EMPTY block (issue #6).
|
|
3
|
+
*
|
|
4
|
+
* An empty block renders a lone "\n" text node (one line, see RichText.svelte). Chromium locates the running composition by
|
|
5
|
+
* PLAIN-TEXT offsets from the editable root, and the two directions of that mapping disagree on such a node: every lone "\n"
|
|
6
|
+
* block BEFORE the composition drifts its start back by one, so the composed text lands in the wrong (previous) block and the
|
|
7
|
+
* next `compositionupdate` deletes the blocks in between — the commit never lands. A `<br>` in the same place maps cleanly.
|
|
8
|
+
*
|
|
9
|
+
* So for the duration of a composition the lone "\n" nodes before the composing block are swapped for a `<br>` (same single
|
|
10
|
+
* line), and put back at its end, before the model catches up. Nothing in the model or the Svelte-owned nodes changes.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Swap, in the document before `element`, every empty block's lone "\n" text node for a `<br>`.
|
|
14
|
+
* @param {Element | null | undefined} element the block holding the composition
|
|
15
|
+
* @returns {Array<{ br: Element, text: Text }>} what {@link restoreEmptyBlocks} puts back
|
|
16
|
+
*/
|
|
17
|
+
export function swapEmptyBlocks(element: Element | null | undefined): Array<{
|
|
18
|
+
br: Element;
|
|
19
|
+
text: Text;
|
|
20
|
+
}>;
|
|
21
|
+
/**
|
|
22
|
+
* Put the "\n" text nodes back (Svelte keeps references to them). A `<br>` the browser or Svelte already removed is skipped.
|
|
23
|
+
* @param {Array<{ br: Element, text: Text }>} swapped
|
|
24
|
+
*/
|
|
25
|
+
export function restoreEmptyBlocks(swapped: Array<{
|
|
26
|
+
br: Element;
|
|
27
|
+
text: Text;
|
|
28
|
+
}>): void;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chromium workaround for IME compositions that start after an EMPTY block (issue #6).
|
|
3
|
+
*
|
|
4
|
+
* An empty block renders a lone "\n" text node (one line, see RichText.svelte). Chromium locates the running composition by
|
|
5
|
+
* PLAIN-TEXT offsets from the editable root, and the two directions of that mapping disagree on such a node: every lone "\n"
|
|
6
|
+
* block BEFORE the composition drifts its start back by one, so the composed text lands in the wrong (previous) block and the
|
|
7
|
+
* next `compositionupdate` deletes the blocks in between — the commit never lands. A `<br>` in the same place maps cleanly.
|
|
8
|
+
*
|
|
9
|
+
* So for the duration of a composition the lone "\n" nodes before the composing block are swapped for a `<br>` (same single
|
|
10
|
+
* line), and put back at its end, before the model catches up. Nothing in the model or the Svelte-owned nodes changes.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Swap, in the document before `element`, every empty block's lone "\n" text node for a `<br>`.
|
|
15
|
+
* @param {Element | null | undefined} element the block holding the composition
|
|
16
|
+
* @returns {Array<{ br: Element, text: Text }>} what {@link restoreEmptyBlocks} puts back
|
|
17
|
+
*/
|
|
18
|
+
export function swapEmptyBlocks(element) {
|
|
19
|
+
/** @type {Array<{ br: Element, text: Text }>} */
|
|
20
|
+
const swapped = [];
|
|
21
|
+
const root = element?.closest('[data-nabu-root]');
|
|
22
|
+
if (!element || !root) return swapped;
|
|
23
|
+
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
|
|
24
|
+
/** @type {Text[]} */
|
|
25
|
+
const lone = [];
|
|
26
|
+
for (let n = /** @type {Text | null} */ (walker.nextNode()); n; n = /** @type {Text | null} */ (walker.nextNode())) {
|
|
27
|
+
if (!(element.compareDocumentPosition(n) & Node.DOCUMENT_POSITION_PRECEDING)) break; // reached the composing block
|
|
28
|
+
if (n.data !== '\n' || !n.parentNode) continue;
|
|
29
|
+
// Only an empty block's placeholder: nothing but Svelte's anchors (comments, empty text nodes) beside it (no filler <br>, no other text).
|
|
30
|
+
let alone = true;
|
|
31
|
+
for (const s of n.parentNode.childNodes) {
|
|
32
|
+
if (s === n || s.nodeType === Node.COMMENT_NODE || (s.nodeType === Node.TEXT_NODE && !/** @type {Text} */ (s).data)) continue;
|
|
33
|
+
alone = false; break;
|
|
34
|
+
}
|
|
35
|
+
if (alone) lone.push(n);
|
|
36
|
+
}
|
|
37
|
+
for (const text of lone) {
|
|
38
|
+
const br = document.createElement('br');
|
|
39
|
+
text.replaceWith(br);
|
|
40
|
+
swapped.push({ br, text });
|
|
41
|
+
}
|
|
42
|
+
return swapped;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Put the "\n" text nodes back (Svelte keeps references to them). A `<br>` the browser or Svelte already removed is skipped.
|
|
47
|
+
* @param {Array<{ br: Element, text: Text }>} swapped
|
|
48
|
+
*/
|
|
49
|
+
export function restoreEmptyBlocks(swapped) {
|
|
50
|
+
for (const { br, text } of swapped) if (br.parentNode) br.replaceWith(text);
|
|
51
|
+
}
|
|
@@ -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
|
+
}
|