@aionbuilders/nabu 0.1.0-beta.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +523 -0
- package/LICENSE +21 -0
- package/README.md +72 -341
- package/dist/behaviors/text/RichText.svelte +24 -4
- package/dist/behaviors/text/markdown-shortcuts.extension.d.ts +3 -0
- package/dist/behaviors/text/markdown-shortcuts.extension.js +35 -10
- package/dist/behaviors/text/markdown-syntax.d.ts +42 -0
- package/dist/behaviors/text/markdown-syntax.js +81 -0
- package/dist/behaviors/text/rich-text.extension.d.ts +3 -0
- package/dist/behaviors/text/rich-text.extension.js +95 -38
- package/dist/behaviors/text/text.behavior.svelte.d.ts +199 -48
- package/dist/behaviors/text/text.behavior.svelte.js +549 -83
- package/dist/behaviors/text/typography.extension.d.ts +2 -1
- package/dist/behaviors/text/typography.extension.js +3 -2
- package/dist/blocks/BlockList.svelte +48 -0
- package/dist/blocks/BlockList.svelte.d.ts +17 -0
- package/dist/blocks/Nabu.svelte +169 -25
- package/dist/blocks/Nabu.svelte.d.ts +13 -0
- package/dist/blocks/block.extension.d.ts +3 -0
- package/dist/blocks/block.extension.js +4 -7
- package/dist/blocks/block.svelte.d.ts +350 -86
- package/dist/blocks/block.svelte.js +370 -59
- package/dist/blocks/blockquote/Blockquote.svelte +1 -2
- package/dist/blocks/blockquote/blockquote.extension.d.ts +4 -1
- package/dist/blocks/blockquote/blockquote.extension.js +14 -5
- package/dist/blocks/blockquote/blockquote.svelte.d.ts +19 -11
- package/dist/blocks/blockquote/blockquote.svelte.js +58 -13
- package/dist/blocks/callout/Callout.svelte +6 -8
- package/dist/blocks/callout/callout.extension.d.ts +4 -1
- package/dist/blocks/callout/callout.extension.js +19 -3
- package/dist/blocks/callout/callout.svelte.d.ts +5 -3
- package/dist/blocks/callout/callout.svelte.js +11 -6
- package/dist/blocks/chunks.d.ts +49 -0
- package/dist/blocks/chunks.js +90 -0
- package/dist/blocks/code/Code.svelte +7 -8
- package/dist/blocks/code/code.extension.d.ts +4 -1
- package/dist/blocks/code/code.extension.js +22 -2
- package/dist/blocks/code/code.svelte.d.ts +9 -2
- package/dist/blocks/code/code.svelte.js +49 -25
- package/dist/blocks/container.utils.d.ts +85 -13
- package/dist/blocks/container.utils.js +338 -178
- package/dist/blocks/dialogue/Dialogue.svelte +8 -4
- package/dist/blocks/dialogue/dialogue.svelte.d.ts +33 -2
- package/dist/blocks/dialogue/dialogue.svelte.js +58 -4
- package/dist/blocks/dialogue/hooks/onBeforeInput.hook.d.ts +2 -2
- package/dist/blocks/dialogue/hooks/onBeforeInput.hook.js +6 -4
- package/dist/blocks/dialogue/index.d.ts +12 -1
- package/dist/blocks/dialogue/index.js +51 -35
- package/dist/blocks/divider/divider.extension.d.ts +23 -1
- package/dist/blocks/divider/divider.extension.js +85 -19
- package/dist/blocks/divider/divider.svelte.d.ts +7 -2
- package/dist/blocks/divider/divider.svelte.js +9 -4
- package/dist/blocks/file/File.svelte +13 -6
- package/dist/blocks/file/file.extension.d.ts +4 -1
- package/dist/blocks/file/file.extension.js +9 -1
- package/dist/blocks/file/file.svelte.d.ts +3 -3
- package/dist/blocks/file/file.svelte.js +20 -14
- package/dist/blocks/heading/Heading.svelte +1 -2
- package/dist/blocks/heading/heading.svelte.d.ts +23 -11
- package/dist/blocks/heading/heading.svelte.js +68 -13
- package/dist/blocks/heading/hooks/onBeforeInput.hook.js +7 -5
- package/dist/blocks/heading/index.d.ts +2 -2
- package/dist/blocks/heading/index.js +33 -8
- package/dist/blocks/image/Image.svelte +5 -2
- package/dist/blocks/image/image.extension.d.ts +4 -1
- package/dist/blocks/image/image.extension.js +9 -1
- package/dist/blocks/image/image.svelte.d.ts +17 -6
- package/dist/blocks/image/image.svelte.js +33 -11
- package/dist/blocks/index.d.ts +1 -1
- package/dist/blocks/index.js +1 -1
- package/dist/blocks/list/List.svelte +1 -0
- package/dist/blocks/list/ListItem.svelte +1 -0
- package/dist/blocks/list/convert.d.ts +20 -0
- package/dist/blocks/list/convert.js +92 -0
- package/dist/blocks/list/hooks/onBeforeInput.hook.js +49 -28
- package/dist/blocks/list/html-flat.d.ts +78 -0
- package/dist/blocks/list/html-flat.js +244 -0
- package/dist/blocks/list/indent.d.ts +86 -0
- package/dist/blocks/list/indent.js +205 -0
- package/dist/blocks/list/index.d.ts +6 -3
- package/dist/blocks/list/index.js +42 -31
- package/dist/blocks/list/list-item.svelte.d.ts +31 -17
- package/dist/blocks/list/list-item.svelte.js +99 -100
- package/dist/blocks/list/list.behavior.svelte.d.ts +4 -1
- package/dist/blocks/list/list.behavior.svelte.js +6 -1
- package/dist/blocks/list/list.svelte.d.ts +89 -18
- package/dist/blocks/list/list.svelte.js +129 -125
- package/dist/blocks/list/markdown.d.ts +149 -0
- package/dist/blocks/list/markdown.js +442 -0
- package/dist/blocks/list/normalize.d.ts +11 -0
- package/dist/blocks/list/normalize.js +396 -0
- package/dist/blocks/list/paste.d.ts +45 -0
- package/dist/blocks/list/paste.js +242 -0
- package/dist/blocks/list/step-back.d.ts +24 -0
- package/dist/blocks/list/step-back.js +87 -0
- package/dist/blocks/list/to-list.d.ts +7 -0
- package/dist/blocks/list/to-list.js +44 -0
- package/dist/blocks/list/todos-toggle.d.ts +20 -0
- package/dist/blocks/list/todos-toggle.js +48 -0
- package/dist/blocks/media/markdown.d.ts +12 -0
- package/dist/blocks/media/markdown.js +26 -0
- package/dist/blocks/media/media-block.svelte.d.ts +77 -2
- package/dist/blocks/media/media-block.svelte.js +124 -12
- package/dist/blocks/media/media.helpers.d.ts +6 -1
- package/dist/blocks/media/media.helpers.js +43 -6
- package/dist/blocks/megablock.svelte.d.ts +50 -19
- package/dist/blocks/megablock.svelte.js +114 -74
- package/dist/blocks/move.d.ts +23 -0
- package/dist/blocks/move.js +92 -0
- package/dist/blocks/nabu.svelte.d.ts +764 -71
- package/dist/blocks/nabu.svelte.js +1746 -189
- package/dist/blocks/paragraph/Paragraph.svelte +1 -2
- package/dist/blocks/paragraph/index.d.ts +2 -2
- package/dist/blocks/paragraph/index.js +35 -13
- package/dist/blocks/paragraph/paragraph.svelte.d.ts +46 -14
- package/dist/blocks/paragraph/paragraph.svelte.js +77 -16
- package/dist/blocks/paragraph/table.html.d.ts +9 -0
- package/dist/blocks/paragraph/table.html.js +111 -0
- package/dist/blocks/paragraph/table.markdown.d.ts +32 -0
- package/dist/blocks/paragraph/table.markdown.js +90 -0
- package/dist/blocks/region/Region.svelte +264 -0
- package/dist/blocks/region/Region.svelte.d.ts +11 -0
- package/dist/blocks/region/actions.d.ts +135 -0
- package/dist/blocks/region/actions.js +402 -0
- package/dist/blocks/region/close.d.ts +35 -0
- package/dist/blocks/region/close.js +130 -0
- package/dist/blocks/region/commands.d.ts +6 -0
- package/dist/blocks/region/commands.js +277 -0
- package/dist/blocks/region/create.d.ts +72 -0
- package/dist/blocks/region/create.js +208 -0
- package/dist/blocks/region/end-marker.d.ts +35 -0
- package/dist/blocks/region/end-marker.js +51 -0
- package/dist/blocks/region/facts.d.ts +69 -0
- package/dist/blocks/region/facts.js +152 -0
- package/dist/blocks/region/format.d.ts +91 -0
- package/dist/blocks/region/format.js +260 -0
- package/dist/blocks/region/index.d.ts +14 -0
- package/dist/blocks/region/index.js +14 -0
- package/dist/blocks/region/insert.d.ts +59 -0
- package/dist/blocks/region/insert.js +102 -0
- package/dist/blocks/region/messages.d.ts +254 -0
- package/dist/blocks/region/messages.js +131 -0
- package/dist/blocks/region/normalize.d.ts +7 -0
- package/dist/blocks/region/normalize.js +170 -0
- package/dist/blocks/region/region.extension.d.ts +58 -0
- package/dist/blocks/region/region.extension.js +119 -0
- package/dist/blocks/region/region.svelte.d.ts +221 -0
- package/dist/blocks/region/region.svelte.js +582 -0
- package/dist/blocks/region/regions.svelte.d.ts +141 -0
- package/dist/blocks/region/regions.svelte.js +276 -0
- package/dist/blocks/region/serialize.d.ts +57 -0
- package/dist/blocks/region/serialize.js +112 -0
- package/dist/blocks/region/types.d.ts +255 -0
- package/dist/blocks/region/types.js +204 -0
- package/dist/blocks/selection.svelte.d.ts +106 -8
- package/dist/blocks/selection.svelte.js +622 -74
- package/dist/blocks/subthread.d.ts +80 -0
- package/dist/blocks/subthread.js +167 -0
- package/dist/blocks/text-block.svelte.d.ts +34 -13
- package/dist/blocks/text-block.svelte.js +42 -8
- package/dist/blocks/toggle/Toggle.svelte +4 -15
- package/dist/blocks/toggle/markdown.d.ts +20 -0
- package/dist/blocks/toggle/markdown.js +112 -0
- package/dist/blocks/toggle/toggle.extension.d.ts +4 -1
- package/dist/blocks/toggle/toggle.extension.js +6 -0
- package/dist/blocks/toggle/toggle.svelte.d.ts +54 -7
- package/dist/blocks/toggle/toggle.svelte.js +134 -34
- package/dist/blocks/undo-selection.d.ts +175 -0
- package/dist/blocks/undo-selection.js +360 -0
- package/dist/blocks/video/Video.svelte +5 -2
- package/dist/blocks/video/video.extension.d.ts +4 -1
- package/dist/blocks/video/video.extension.js +9 -1
- package/dist/blocks/video/video.svelte.d.ts +6 -5
- package/dist/blocks/video/video.svelte.js +18 -9
- package/dist/extend.d.ts +55 -0
- package/dist/extend.js +88 -0
- package/dist/files/assets.clipboard.d.ts +77 -0
- package/dist/files/assets.clipboard.js +152 -0
- package/dist/files/files.svelte.d.ts +321 -19
- package/dist/files/files.svelte.js +847 -43
- package/dist/files/index.d.ts +3 -1
- package/dist/files/index.js +3 -1
- package/dist/files/outbox.d.ts +69 -0
- package/dist/files/outbox.js +48 -0
- package/dist/files/status.d.ts +65 -0
- package/dist/files/status.js +87 -0
- package/dist/files/store.d.ts +97 -0
- package/dist/files/store.js +65 -0
- package/dist/files/uploads.svelte.d.ts +174 -0
- package/dist/files/uploads.svelte.js +504 -0
- package/dist/index.d.ts +68 -9
- package/dist/index.js +117 -10
- package/dist/nabu-theme.css +21 -1
- package/dist/paste/html.paste.d.ts +74 -3
- package/dist/paste/html.paste.js +550 -86
- package/dist/paste/markdown-inline.d.ts +86 -0
- package/dist/paste/markdown-inline.js +528 -0
- package/dist/paste/markdown.paste.d.ts +2 -8
- package/dist/paste/markdown.paste.js +78 -94
- package/dist/paste/nabu.paste.d.ts +3 -0
- package/dist/paste/nabu.paste.js +29 -3
- package/dist/paste/plain-text.paste.d.ts +2 -2
- package/dist/paste/plain-text.paste.js +3 -3
- package/dist/persistence/asset-store.d.ts +51 -0
- package/dist/persistence/asset-store.js +103 -0
- package/dist/persistence/autosave.d.ts +53 -0
- package/dist/persistence/autosave.js +62 -0
- package/dist/persistence/compact.d.ts +162 -0
- package/dist/persistence/compact.js +141 -0
- package/dist/persistence/history.d.ts +77 -0
- package/dist/persistence/history.js +176 -0
- package/dist/persistence/indexeddb.d.ts +190 -4
- package/dist/persistence/indexeddb.js +333 -29
- package/dist/persistence/merge.d.ts +38 -0
- package/dist/persistence/merge.js +36 -0
- package/dist/persistence/outbox.d.ts +25 -0
- package/dist/persistence/outbox.js +40 -0
- package/dist/persistence/persistence.d.ts +70 -9
- package/dist/persistence/persistence.js +507 -35
- package/dist/persistence/recovery.d.ts +79 -0
- package/dist/persistence/recovery.js +79 -0
- package/dist/persistence/release.d.ts +34 -0
- package/dist/persistence/release.js +64 -0
- package/dist/persistence/state.svelte.d.ts +53 -0
- package/dist/persistence/state.svelte.js +48 -0
- package/dist/persistence/sync.d.ts +39 -0
- package/dist/persistence/sync.js +138 -0
- package/dist/persistence/tail.d.ts +42 -0
- package/dist/persistence/tail.js +103 -0
- package/dist/presets.d.ts +29 -26
- package/dist/presets.js +35 -13
- package/dist/themes/atelier.css +318 -0
- package/dist/ui/CommandChooser.svelte +153 -0
- package/dist/ui/CommandChooser.svelte.d.ts +13 -0
- package/dist/ui/CommandInvite.svelte +99 -0
- package/dist/ui/CommandInvite.svelte.d.ts +12 -0
- package/dist/ui/DragHandle.svelte +97 -21
- package/dist/ui/FloatingToolbar.svelte +75 -27
- package/dist/ui/FloatingToolbar.svelte.d.ts +10 -0
- package/dist/ui/SlashMenu.svelte +147 -15
- package/dist/ui/SlashMenu.svelte.d.ts +20 -0
- package/dist/ui/anchor.d.ts +112 -0
- package/dist/ui/anchor.js +136 -0
- package/dist/ui/index.d.ts +7 -1
- package/dist/ui/index.js +12 -1
- package/dist/ui/slash-menu.svelte.d.ts +79 -7
- package/dist/ui/slash-menu.svelte.js +85 -51
- package/dist/utils/command-confirm.svelte.d.ts +349 -0
- package/dist/utils/command-confirm.svelte.js +673 -0
- package/dist/utils/commands.d.ts +328 -0
- package/dist/utils/commands.js +416 -0
- package/dist/utils/dom-scope.d.ts +14 -0
- package/dist/utils/dom-scope.js +20 -0
- package/dist/utils/drop.d.ts +160 -0
- package/dist/utils/drop.js +305 -0
- package/dist/utils/extensions.d.ts +439 -45
- package/dist/utils/extensions.js +271 -29
- package/dist/utils/html.d.ts +44 -0
- package/dist/utils/html.js +183 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +3 -0
- package/dist/utils/keys.d.ts +120 -0
- package/dist/utils/keys.js +248 -0
- package/dist/utils/links.d.ts +22 -0
- package/dist/utils/links.js +34 -0
- package/dist/utils/placeholders.d.ts +81 -3
- package/dist/utils/placeholders.js +73 -3
- package/dist/utils/selection.svelte.d.ts +47 -19
- package/dist/utils/selection.svelte.js +60 -10
- package/dist/utils/spellcheck.d.ts +9 -0
- package/dist/utils/spellcheck.js +17 -0
- package/dist/utils/tab.d.ts +31 -0
- package/dist/utils/tab.js +117 -0
- package/dist/utils/words.d.ts +7 -0
- package/dist/utils/words.js +62 -0
- package/dist/vite.d.ts +9 -3
- package/dist/vite.js +19 -8
- package/package.json +54 -20
- package/dist/blocks/variable.svelte.d.ts +0 -47
- package/dist/blocks/variable.svelte.js +0 -51
- package/dist/blocks/zone/TemplateEditor.svelte +0 -308
- package/dist/blocks/zone/TemplateEditor.svelte.d.ts +0 -11
- package/dist/blocks/zone/Zone.svelte +0 -272
- package/dist/blocks/zone/Zone.svelte.d.ts +0 -11
- package/dist/blocks/zone/ZoneEditor.svelte +0 -289
- package/dist/blocks/zone/ZoneEditor.svelte.d.ts +0 -11
- package/dist/blocks/zone/ZoneTemplatesList.svelte +0 -188
- package/dist/blocks/zone/ZoneTemplatesList.svelte.d.ts +0 -11
- package/dist/blocks/zone/hooks/onBeforeInput.hook.d.ts +0 -3
- package/dist/blocks/zone/hooks/onBeforeInput.hook.js +0 -30
- package/dist/blocks/zone/index.d.ts +0 -2
- package/dist/blocks/zone/index.js +0 -2
- package/dist/blocks/zone/zone.extension.d.ts +0 -2
- package/dist/blocks/zone/zone.extension.js +0 -237
- package/dist/blocks/zone/zone.svelte.d.ts +0 -221
- package/dist/blocks/zone/zone.svelte.js +0 -608
- package/dist/overlays/context/Context.svelte +0 -182
- package/dist/overlays/context/Context.svelte.d.ts +0 -17
- package/dist/overlays/context/collect.d.ts +0 -17
- package/dist/overlays/context/collect.js +0 -33
- package/dist/overlays/context/context.extension.d.ts +0 -2
- package/dist/overlays/context/context.extension.js +0 -8
- package/dist/overlays/context/context.svelte.d.ts +0 -82
- package/dist/overlays/context/context.svelte.js +0 -159
- package/dist/overlays/context/index.d.ts +0 -3
- package/dist/overlays/context/index.js +0 -3
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Drag & drop toolbox (ADR 014, lot 4): everything the editor needs to turn a pointer over the document into a MODEL
|
|
3
|
+
* position, and to classify what is being dragged. Agnostic: it knows blocks only through their `text` behavior and
|
|
4
|
+
* their `children`, never through a concrete type.
|
|
5
|
+
*
|
|
6
|
+
* Vocabulary
|
|
7
|
+
* - A {@link DropPosition} is where a drop lands: a caret position in the text of a block (`side: 'inside'`), or
|
|
8
|
+
* "beside" a block that holds no text and no children (a divider, an image… `side: 'before' | 'after'`).
|
|
9
|
+
* - A {@link DropTarget} is a position + what the drop will do (`kind`). It is what `nabu.dropTarget` exposes.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { closestWithin } from './dom-scope.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @import { Nabu } from '../blocks/nabu.svelte.js';
|
|
16
|
+
* @import { Block } from '../blocks/block.svelte.js';
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @typedef {'inside' | 'before' | 'after'} DropSide
|
|
21
|
+
* `inside`: a caret position in the text of `block`; `before` / `after`: beside a block without text (image, divider…).
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @typedef {Object} DropPosition
|
|
26
|
+
* @property {Block} block
|
|
27
|
+
* @property {number} offset Text offset in `block` (0 when `side` is not `inside`).
|
|
28
|
+
* @property {DropSide} side
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @typedef {'files' | 'content' | 'move'} DropKind
|
|
33
|
+
* `files`: OS files (become media blocks); `content`: text / HTML / markdown / Nabu fragment from outside (or a copy);
|
|
34
|
+
* `move`: the editor's own text selection being moved.
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
/** @typedef {DropPosition & { kind: DropKind }} DropTarget */
|
|
38
|
+
|
|
39
|
+
/** MIME type that marks a drag started inside an editor (its value is a per-drag token). */
|
|
40
|
+
export const DRAG_MARKER = 'application/x-nabu-drag';
|
|
41
|
+
/** The Nabu clipboard / drag fragment format (see `Nabu#handleCopy`). */
|
|
42
|
+
export const NABU_FORMAT = 'application/x-nabu+json';
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* What a drag carries, from its `types` alone (the data itself is not readable before the drop).
|
|
46
|
+
* - an internal fragment is `content` even when files come along (the fragment already holds the bytes);
|
|
47
|
+
* - `Files` is `files` when some extension takes files, else it counts as nothing;
|
|
48
|
+
* - any format an interpreter reads is `content`.
|
|
49
|
+
* @param {ArrayLike<string> | null | undefined} types `DataTransfer#types`
|
|
50
|
+
* @param {{ formats: string[], files: boolean }} ctx `formats`: the formats the paste interpreters read; `files`: whether a file handler exists
|
|
51
|
+
* @returns {'files' | 'content' | null}
|
|
52
|
+
*/
|
|
53
|
+
export function transferKind(types, ctx) {
|
|
54
|
+
const list = Array.from(types ?? []);
|
|
55
|
+
if (!list.length) return null;
|
|
56
|
+
if (list.includes(NABU_FORMAT) && ctx.formats.includes(NABU_FORMAT)) return 'content';
|
|
57
|
+
if (ctx.files && list.includes('Files')) return 'files';
|
|
58
|
+
return ctx.formats.some((format) => list.includes(format)) ? 'content' : null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Whether the pointer's modifiers ask for a COPY instead of a move. Platform conventions: Ctrl on Windows / Linux,
|
|
63
|
+
* Option (Alt) on macOS — both are accepted everywhere (Ctrl+drag is a secondary click on macOS and never reaches us).
|
|
64
|
+
* @param {{ ctrlKey?: boolean, altKey?: boolean }} e
|
|
65
|
+
*/
|
|
66
|
+
export const wantsCopy = (e) => !!(e.ctrlKey || e.altKey);
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The `dropEffect` to announce: `wanted` when the drag source's `effectAllowed` permits it, else the first effect it
|
|
70
|
+
* does allow (announcing a forbidden effect makes the browser refuse the drop).
|
|
71
|
+
* @param {string | undefined} effectAllowed
|
|
72
|
+
* @param {'copy' | 'move'} wanted
|
|
73
|
+
* @returns {'copy' | 'move' | 'link'}
|
|
74
|
+
*/
|
|
75
|
+
export function pickDropEffect(effectAllowed, wanted) {
|
|
76
|
+
const allowed = effectAllowed || 'uninitialized';
|
|
77
|
+
if (allowed === 'uninitialized' || allowed === 'all') return wanted;
|
|
78
|
+
const has = (/** @type {string} */ effect) => allowed.toLowerCase().includes(effect);
|
|
79
|
+
if (has(wanted)) return wanted;
|
|
80
|
+
if (has('copy')) return 'copy';
|
|
81
|
+
if (has('move')) return 'move';
|
|
82
|
+
return 'link';
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Which half of a box the pointer is in.
|
|
87
|
+
* @param {number} y
|
|
88
|
+
* @param {{ top: number, bottom: number }} rect
|
|
89
|
+
* @returns {'before' | 'after'}
|
|
90
|
+
*/
|
|
91
|
+
export const pointerHalf = (y, rect) => (y > (rect.top + rect.bottom) / 2 ? 'after' : 'before');
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Index of the box the pointer at `y` belongs to: the SMALLEST box containing `y` (the deepest block), else the box
|
|
95
|
+
* with the nearest edge (ties: the smallest). -1 for no box.
|
|
96
|
+
* @param {Array<{ top: number, bottom: number }>} rects
|
|
97
|
+
* @param {number} y
|
|
98
|
+
* @returns {number}
|
|
99
|
+
*/
|
|
100
|
+
export function nearestRect(rects, y) {
|
|
101
|
+
let best = -1;
|
|
102
|
+
let bestDistance = Infinity;
|
|
103
|
+
let bestHeight = Infinity;
|
|
104
|
+
rects.forEach((rect, i) => {
|
|
105
|
+
const distance = y < rect.top ? rect.top - y : y > rect.bottom ? y - rect.bottom : 0;
|
|
106
|
+
const height = rect.bottom - rect.top;
|
|
107
|
+
if (distance < bestDistance || (distance === bestDistance && height < bestHeight)) {
|
|
108
|
+
best = i; bestDistance = distance; bestHeight = height;
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
return best;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Whether two targets are the same drop (to avoid re-publishing reactive state on every `dragover`).
|
|
116
|
+
* @param {DropTarget | null} a
|
|
117
|
+
* @param {DropTarget | null} b
|
|
118
|
+
*/
|
|
119
|
+
export const sameTarget = (a, b) => a === b || (!!a && !!b && a.block === b.block && a.offset === b.offset && a.side === b.side && a.kind === b.kind);
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Document order of two block elements, a block's own text coming before its children (pre-order): -1 when `a` is
|
|
123
|
+
* before `b`, 1 when after, 0 for the same block.
|
|
124
|
+
* @param {Element} a
|
|
125
|
+
* @param {Element} b
|
|
126
|
+
* @returns {-1 | 0 | 1}
|
|
127
|
+
*/
|
|
128
|
+
export function compareBlocks(a, b) {
|
|
129
|
+
if (a === b) return 0;
|
|
130
|
+
if (a.contains(b)) return -1;
|
|
131
|
+
if (b.contains(a)) return 1;
|
|
132
|
+
return a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Whether a drop position lies in a model range, its two ends included (dropping a selection on itself, or right at its
|
|
137
|
+
* edge, changes nothing). A position beside a block (before / after) is in the range when that block is.
|
|
138
|
+
* @param {{ startBlock: Block, startOffset: number, endBlock: Block, endOffset: number }} range
|
|
139
|
+
* @param {DropPosition} at
|
|
140
|
+
* @returns {boolean}
|
|
141
|
+
*/
|
|
142
|
+
export function positionInRange(range, at) {
|
|
143
|
+
const { startBlock, startOffset, endBlock, endOffset } = range;
|
|
144
|
+
const { block } = at;
|
|
145
|
+
if (at.side === 'inside') {
|
|
146
|
+
if (block === startBlock && block === endBlock) return at.offset >= startOffset && at.offset <= endOffset;
|
|
147
|
+
if (block === startBlock) return at.offset >= startOffset;
|
|
148
|
+
if (block === endBlock) return at.offset <= endOffset;
|
|
149
|
+
} else if (block === startBlock || block === endBlock) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
const x = /** @type {Element | null | undefined} */ (block.element);
|
|
153
|
+
const s = /** @type {Element | null | undefined} */ (startBlock.element);
|
|
154
|
+
const e = /** @type {Element | null | undefined} */ (endBlock.element);
|
|
155
|
+
if (!x || !s || !e) return false;
|
|
156
|
+
return compareBlocks(s, x) < 0 && compareBlocks(x, e) < 0;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Where a drop position goes once `range` has been removed from the document (`deleteRange`): a position after the range
|
|
161
|
+
* in the same block shifts back by the removed length; one in the range's end block, when that block was merged into the
|
|
162
|
+
* start block, follows its text there. `endMerged`: the end block no longer exists.
|
|
163
|
+
* @param {{ startBlock: Block, startOffset: number, endBlock: Block, endOffset: number }} range
|
|
164
|
+
* @param {DropPosition} at a position outside the range
|
|
165
|
+
* @param {boolean} endMerged
|
|
166
|
+
* @returns {DropPosition}
|
|
167
|
+
*/
|
|
168
|
+
export function positionAfterRemoval(range, at, endMerged) {
|
|
169
|
+
if (at.side !== 'inside') return at;
|
|
170
|
+
const { startBlock, startOffset, endBlock, endOffset } = range;
|
|
171
|
+
if (startBlock === endBlock) {
|
|
172
|
+
return at.block === startBlock && at.offset >= endOffset ? { ...at, offset: at.offset - (endOffset - startOffset) } : at;
|
|
173
|
+
}
|
|
174
|
+
if (at.block !== endBlock) return at;
|
|
175
|
+
return endMerged
|
|
176
|
+
? { ...at, block: startBlock, offset: startOffset + at.offset - endOffset }
|
|
177
|
+
: { ...at, offset: at.offset - endOffset };
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* The DOM point under a viewport coordinate (`caretPositionFromPoint` — standard, Firefox / Chromium ≥ 128 — or the
|
|
182
|
+
* older `caretRangeFromPoint` — WebKit / Blink).
|
|
183
|
+
* @param {number} x
|
|
184
|
+
* @param {number} y
|
|
185
|
+
* @returns {{ node: Node, offset: number } | null}
|
|
186
|
+
*/
|
|
187
|
+
export function caretPointFromPoint(x, y) {
|
|
188
|
+
if (typeof document === 'undefined') return null;
|
|
189
|
+
const doc = /** @type {any} */ (document);
|
|
190
|
+
try {
|
|
191
|
+
if (typeof doc.caretPositionFromPoint === 'function') {
|
|
192
|
+
const pos = doc.caretPositionFromPoint(x, y);
|
|
193
|
+
if (pos?.offsetNode) return { node: pos.offsetNode, offset: pos.offset };
|
|
194
|
+
}
|
|
195
|
+
if (typeof doc.caretRangeFromPoint === 'function') {
|
|
196
|
+
const range = doc.caretRangeFromPoint(x, y);
|
|
197
|
+
if (range) return { node: range.startContainer, offset: range.startOffset };
|
|
198
|
+
}
|
|
199
|
+
} catch { /* outside the viewport, detached… */ }
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** A block that holds no text and no children: it can only be dropped before / after, never into. @param {Block} block */
|
|
204
|
+
const isVoid = (block) => !block.behaviors?.has('text') && !/** @type {any} */ (block).children?.length;
|
|
205
|
+
|
|
206
|
+
/** @param {Block} block @returns {number} */
|
|
207
|
+
const textLength = (block) => block.behaviors?.get('text')?.text?.length ?? 0;
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* The innermost block whose element is at a viewport point, inside `root` (UI islands are not blocks).
|
|
211
|
+
* @param {Nabu} nabu
|
|
212
|
+
* @param {HTMLElement} root
|
|
213
|
+
* @param {number} x
|
|
214
|
+
* @param {number} y
|
|
215
|
+
* @returns {Block | null}
|
|
216
|
+
*/
|
|
217
|
+
function blockAtPoint(nabu, root, x, y) {
|
|
218
|
+
const el = /** @type {Element | null} */ (document.elementFromPoint(x, y));
|
|
219
|
+
if (!el || !root.contains(el) || closestWithin(el, '[data-nabu-ui]', root)) return null;
|
|
220
|
+
const id = el.closest('[data-block-id]')?.getAttribute('data-block-id');
|
|
221
|
+
return (id && nabu.blocks.get(id)) || null;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* The position of a DOM point in the text of `block`, or null when it is not on the block's own text (a nested child
|
|
226
|
+
* block's text, an inert part, a UI island, an element-level caret).
|
|
227
|
+
* @param {Block} block
|
|
228
|
+
* @param {Node} node
|
|
229
|
+
* @param {number} offset
|
|
230
|
+
* @returns {number | null}
|
|
231
|
+
*/
|
|
232
|
+
function textOffsetOf(block, node, offset) {
|
|
233
|
+
const text = block.behaviors?.get('text');
|
|
234
|
+
const el = /** @type {HTMLElement | null | undefined} */ (block.element);
|
|
235
|
+
if (!text || !el || node.nodeType !== Node.TEXT_NODE || !el.contains(node)) return null;
|
|
236
|
+
const host = node.parentElement;
|
|
237
|
+
if (host?.closest('[data-nabu-inert], [data-nabu-ui]')) return null;
|
|
238
|
+
if (host?.closest('[data-block-id]') !== el) return null; // the text of a child block, not of this one
|
|
239
|
+
return Math.min(text.calculateOffset(node, offset, el), text.text.length);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* The model position under the pointer. Text blocks: the caret position under the pointer (else the start / end of the
|
|
244
|
+
* block, by the pointer's half). Blocks without text or children (image, divider…): before / after, by the pointer's
|
|
245
|
+
* half. Anywhere else (padding of the editor, gaps between blocks, containers): the nearest block, so that a pointer
|
|
246
|
+
* above the first block lands at the start of the document and below the last at its end.
|
|
247
|
+
* @param {Nabu} nabu
|
|
248
|
+
* @param {HTMLElement} root the editor's root element
|
|
249
|
+
* @param {number} x viewport coordinates
|
|
250
|
+
* @param {number} y
|
|
251
|
+
* @returns {DropPosition | null}
|
|
252
|
+
*/
|
|
253
|
+
export function resolveDropPosition(nabu, root, x, y) {
|
|
254
|
+
const under = blockAtPoint(nabu, root, x, y);
|
|
255
|
+
const el = /** @type {HTMLElement | null | undefined} */ (under?.element);
|
|
256
|
+
if (under && el?.isConnected) {
|
|
257
|
+
if (isVoid(under)) return { block: under, offset: 0, side: pointerHalf(y, el.getBoundingClientRect()) };
|
|
258
|
+
if (under.behaviors?.has('text')) {
|
|
259
|
+
const caret = caretPointFromPoint(x, y);
|
|
260
|
+
const offset = caret ? textOffsetOf(under, caret.node, caret.offset) : null;
|
|
261
|
+
if (offset !== null) return { block: under, offset, side: 'inside' };
|
|
262
|
+
return { block: under, offset: pointerHalf(y, el.getBoundingClientRect()) === 'after' ? textLength(under) : 0, side: 'inside' };
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return nearestBlockPosition(nabu, y);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* The drop position for a pointer that is on no block: the vertically nearest block that is a leaf (a text block or a
|
|
270
|
+
* block without text and children), the start / end of its text or beside it, by the pointer's half.
|
|
271
|
+
* @param {Nabu} nabu
|
|
272
|
+
* @param {number} y viewport ordinate
|
|
273
|
+
* @returns {DropPosition | null}
|
|
274
|
+
*/
|
|
275
|
+
export function nearestBlockPosition(nabu, y) {
|
|
276
|
+
/** @type {Array<{ block: Block, top: number, bottom: number }>} */
|
|
277
|
+
const candidates = [];
|
|
278
|
+
for (const block of nabu.blocks.values()) {
|
|
279
|
+
const el = /** @type {HTMLElement | null | undefined} */ (block.element);
|
|
280
|
+
if (!el?.isConnected || block.node?.isDeleted?.()) continue;
|
|
281
|
+
if (!block.behaviors?.has('text') && !isVoid(block)) continue; // a container without text: its children are the candidates
|
|
282
|
+
const rect = el.getBoundingClientRect();
|
|
283
|
+
if (!rect.height && !rect.width) continue; // not rendered
|
|
284
|
+
candidates.push({ block, top: rect.top, bottom: rect.bottom });
|
|
285
|
+
}
|
|
286
|
+
const at = nearestRect(candidates, y);
|
|
287
|
+
if (at < 0) return null;
|
|
288
|
+
const { block, ...rect } = candidates[at];
|
|
289
|
+
const half = pointerHalf(y, rect);
|
|
290
|
+
if (isVoid(block)) return { block, offset: 0, side: half };
|
|
291
|
+
return { block, offset: half === 'after' ? textLength(block) : 0, side: 'inside' };
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* 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.
|
|
296
|
+
* @param {Nabu} nabu
|
|
297
|
+
* @returns {DropPosition | null}
|
|
298
|
+
*/
|
|
299
|
+
export function documentEnd(nabu) {
|
|
300
|
+
const last = nabu.lastVisibleBlock;
|
|
301
|
+
if (!last) return null;
|
|
302
|
+
return last.behaviors?.has('text')
|
|
303
|
+
? { block: last, offset: textLength(last), side: 'inside' }
|
|
304
|
+
: { block: last, offset: 0, side: 'after' };
|
|
305
|
+
}
|