@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,402 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The operations behind the region commands (ADR 018 lot A2): split, insert, merge, unwrap, exit, move, promote / demote,
|
|
3
|
+
* navigate. A region is a CONTAINER: every structural operation is a handful of tree moves (a block that survives is MOVED,
|
|
4
|
+
* never deleted and recreated, so its id, its text and the caret survive), committed ONCE = one undo step (the undo restores
|
|
5
|
+
* the selection, ADR 015: the selection captured before the operation is the one the step gives back). They put the caret
|
|
6
|
+
* back where it belongs. No key handling here (see commands.js); a function returns whether it changed something.
|
|
7
|
+
*
|
|
8
|
+
* Reads go through the registry and the blocks (the committed, rendered state); writes go through the Loro tree nodes. An
|
|
9
|
+
* operation reads EVERYTHING it needs before its first write.
|
|
10
|
+
*
|
|
11
|
+
* @import { Nabu, NabuNode } from "../nabu.svelte.js";
|
|
12
|
+
* @import { Block } from "../block.svelte.js";
|
|
13
|
+
* @import { Region } from "./regions.svelte.js";
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { tick } from "svelte";
|
|
17
|
+
import { Regions } from "./regions.svelte.js";
|
|
18
|
+
import { createRegion } from "./insert.js";
|
|
19
|
+
import { hasText, isBodyTextBlock, mergePlan, neighbourOf, siblingsOf } from "./facts.js";
|
|
20
|
+
|
|
21
|
+
/** @typedef {{ block: Block, offset: number }} Caret */
|
|
22
|
+
|
|
23
|
+
/** The children nodes of a node (a snapshot, in order). @param {NabuNode} node @returns {NabuNode[]} */
|
|
24
|
+
export const kids = (node) => /** @type {NabuNode[]} */ (node.children() ?? []);
|
|
25
|
+
|
|
26
|
+
/** The id of a node's parent, `null` at the root. @param {NabuNode} node @returns {string | null} */
|
|
27
|
+
export const parentIdOf = (node) => node.parent()?.id.toString() ?? null;
|
|
28
|
+
|
|
29
|
+
/** The region of a `Region`, of a region block, or of the id of a region block. @param {Nabu} nabu @param {unknown} target @returns {Region | null} */
|
|
30
|
+
export function toRegion(nabu, target) {
|
|
31
|
+
if (!target) return null;
|
|
32
|
+
const regions = Regions.get(nabu);
|
|
33
|
+
if (typeof target === 'string') return regions.of(target);
|
|
34
|
+
const block = /** @type {any} */ (target).block ?? /** @type {any} */ (target).marker ?? target;
|
|
35
|
+
return typeof block?.id === 'string' ? regions.of(block.id) : null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The type of the blocks the editor creates by default (a paragraph). @param {Nabu} nabu @returns {string} */
|
|
39
|
+
export const defaultType = (nabu) => (nabu.defaultBlockType && nabu.registry.has(nabu.defaultBlockType) ? nabu.defaultBlockType : 'paragraph');
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Creates a block of the default text type WITHOUT committing (the caller owns the transaction).
|
|
43
|
+
* @param {Nabu} nabu @param {Record<string, any>} props @param {string | null} parentId @param {number} index @returns {Block}
|
|
44
|
+
*/
|
|
45
|
+
export function createDefaultBlock(nabu, props, parentId, index) {
|
|
46
|
+
const type = defaultType(nabu);
|
|
47
|
+
return /** @type {any} */ (nabu.registry.get(type)).create(nabu, type, props, parentId, index);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Puts the caret back after a commit (the DOM is re-rendered by then). @param {Nabu} nabu @param {Block | null | undefined} block @param {number} offset */
|
|
51
|
+
export async function restoreCaret(nabu, block, offset) {
|
|
52
|
+
if (!block) return;
|
|
53
|
+
await tick();
|
|
54
|
+
const fresh = nabu.blocks.get(block.id);
|
|
55
|
+
if (fresh) nabu.selection.setCursor(fresh, offset);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The first block holding text, in document order, among `blocks` and their descendants (a sub-region's title counts). @param {Block[]} blocks @returns {Block | null} */
|
|
59
|
+
export function firstTextBlock(blocks) {
|
|
60
|
+
for (const block of blocks) {
|
|
61
|
+
if (hasText(block)) return block;
|
|
62
|
+
const found = firstTextBlock(/** @type {any} */ (block).children ?? []);
|
|
63
|
+
if (found) return found;
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** The first declared type of a level (several types may share one level: the first declared wins). @param {Regions} regions @param {number} level */
|
|
69
|
+
const typeOfLevel = (regions, level) => [...regions.config.types.values()].find((def) => def.level === level);
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* "Split here" (ADR 018 §3.5): the region C whose body directly holds the caret block (none at the root) gets a new sibling N
|
|
73
|
+
* of the same type (the default type at the root) right after it. The caret block is cut like Enter does (a heading cut at its
|
|
74
|
+
* end gives a paragraph, mid-text both halves keep their type; at offset 0 there is no cut), then the tail and ALL the blocks
|
|
75
|
+
* that follow it move, in order, into the body of N. At the root N takes the tail and the root blocks that follow it up to
|
|
76
|
+
* (excluding) the next root region. The caret goes to the start of the tail.
|
|
77
|
+
* A block that does not split on Enter (a code block, where Mod-Enter is the user's own) is left alone. One undo step: the
|
|
78
|
+
* block split commits itself (twice), and the engine merges those commits with ours, which follows in the same synchronous turn.
|
|
79
|
+
* @param {Nabu} nabu
|
|
80
|
+
* @param {{ block: Block | null }} ctx
|
|
81
|
+
* @returns {Promise<boolean>} false when the caret is not in a splittable text block
|
|
82
|
+
*/
|
|
83
|
+
export async function splitRegion(nabu, ctx) {
|
|
84
|
+
const block = ctx.block;
|
|
85
|
+
if (!block || !isBodyTextBlock(block) || block.splitsOnEnter === false) return false;
|
|
86
|
+
const sel = block.selection;
|
|
87
|
+
if (!sel) return false;
|
|
88
|
+
const regions = Regions.get(nabu);
|
|
89
|
+
const current = block.parent ? regions.of(block.parent) : null;
|
|
90
|
+
const type = current && regions.config.types.has(current.type) ? current.type : regions.config.defaultType;
|
|
91
|
+
const length = /** @type {any} */ (block.behaviors.get('text')).text.length;
|
|
92
|
+
const offset = Math.min(sel.from, length);
|
|
93
|
+
|
|
94
|
+
// The first block that moves: the caret block itself at offset 0, else the tail of the cut.
|
|
95
|
+
let first = block;
|
|
96
|
+
if (offset > 0) {
|
|
97
|
+
const cut = block.split({ offset, to: length });
|
|
98
|
+
if (!cut?.block) return false;
|
|
99
|
+
first = cut.block;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const node = first.node;
|
|
103
|
+
const siblings = node.parent() ? kids(/** @type {NabuNode} */ (node.parent())) : /** @type {NabuNode[]} */ (nabu.tree.roots());
|
|
104
|
+
const from = siblings.findIndex((n) => n.id === node.id);
|
|
105
|
+
let movers = siblings.slice(from);
|
|
106
|
+
if (!current) {
|
|
107
|
+
const stop = movers.findIndex((n) => n.data.get('type') === 'region');
|
|
108
|
+
if (stop >= 0) movers = movers.slice(0, stop);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Right after the region (a sibling), or at the cut at the root.
|
|
112
|
+
const fresh = current
|
|
113
|
+
? createRegion(nabu, { type, parentId: parentIdOf(current.block.node), index: (current.block.node.index() ?? 0) + 1 })
|
|
114
|
+
: createRegion(nabu, { type, parentId: null, index: from });
|
|
115
|
+
movers.forEach((n, i) => n.move(fresh.node, i));
|
|
116
|
+
|
|
117
|
+
await nabu.commit();
|
|
118
|
+
await restoreCaret(nabu, first, 0);
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Inserts an empty region next to a region, or inside its body, as one undo step; the caret stays where it is.
|
|
124
|
+
* - `at: 'after'` (default): right after `region`, in its parent (a sibling; its whole body stays with it); `'before'`: right before it;
|
|
125
|
+
* - `at: 'inside'`: a SUB-region, in the body of `region`, at the end of the body or at `index` (clamped). The nesting rule
|
|
126
|
+
* holds: the type must be strictly deeper than `region`, and without `type` the shallowest declared deeper type is used (the
|
|
127
|
+
* first declared of its level). A warning and `false` when no type is deeper (a region of the deepest level) or `type` is
|
|
128
|
+
* not valid there; `region` is required;
|
|
129
|
+
* - without `region` (the caret is outside any region): at the caret's position among the root blocks (`from` = the caret
|
|
130
|
+
* block): after / before the root block that holds it.
|
|
131
|
+
* @param {Nabu} nabu
|
|
132
|
+
* @param {Region | null} region the region the position is relative to
|
|
133
|
+
* @param {{ type?: string, at?: 'after' | 'before' | 'inside', index?: number, title?: string, fields?: Record<string, any> }} [options]
|
|
134
|
+
* `type` defaults to the type of `region` (`'inside'`: see above), else the default (top-level) type. `index` only counts for `'inside'`.
|
|
135
|
+
* @param {Block | null} [from] the caret block, for the case without region
|
|
136
|
+
* @returns {Promise<boolean>}
|
|
137
|
+
*/
|
|
138
|
+
export async function insertNearRegion(nabu, region, { type, at = 'after', index: bodyIndex, title, fields } = {}, from = null) {
|
|
139
|
+
const regions = Regions.get(nabu);
|
|
140
|
+
const { types, defaultType: topType } = regions.config;
|
|
141
|
+
if (at !== 'after' && at !== 'before' && at !== 'inside') { nabu.warn(`region:insert: "at" must be "after", "before" or "inside", got ${JSON.stringify(at)}`); return false; }
|
|
142
|
+
|
|
143
|
+
if (at === 'inside') {
|
|
144
|
+
if (!region) { nabu.warn('region:insert: at "inside" needs a region (none given, none holds the caret)'); return false; }
|
|
145
|
+
const deeper = [...types.values()].filter((def) => def.level > region.level);
|
|
146
|
+
if (!deeper.length) { nabu.warn(`region:insert: no region type is deeper than "${region.type}" (level ${region.level}): nothing can be nested in it`); return false; }
|
|
147
|
+
const def = type === undefined
|
|
148
|
+
? deeper.reduce((best, d) => (d.level < best.level ? d : best))
|
|
149
|
+
: types.get(type);
|
|
150
|
+
if (!def) { nabu.warn(`region:insert: region type "${type}" is not declared`); return false; }
|
|
151
|
+
if (def.level <= region.level) { nabu.warn(`region:insert: "${def.id}" (level ${def.level}) cannot be nested in "${region.type}" (level ${region.level}): a sub-region must be deeper`); return false; }
|
|
152
|
+
createRegion(nabu, {
|
|
153
|
+
type: def.id, parentId: region.block.id, title, fields,
|
|
154
|
+
index: typeof bodyIndex === 'number' && Number.isFinite(bodyIndex) ? bodyIndex : null,
|
|
155
|
+
});
|
|
156
|
+
await nabu.commit();
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const wanted = type ?? (region && types.has(region.type) ? region.type : topType);
|
|
161
|
+
if (!types.has(wanted)) { nabu.warn(`region:insert: region type "${wanted}" is not declared`); return false; }
|
|
162
|
+
|
|
163
|
+
/** @type {string | null} */
|
|
164
|
+
let parentId = null;
|
|
165
|
+
let index;
|
|
166
|
+
if (region) {
|
|
167
|
+
parentId = parentIdOf(region.block.node);
|
|
168
|
+
index = (region.block.node.index() ?? 0) + (at === 'before' ? 0 : 1);
|
|
169
|
+
} else {
|
|
170
|
+
/** @type {Block | null} */
|
|
171
|
+
let root = from;
|
|
172
|
+
while (root?.parent) root = /** @type {Block} */ (root.parent);
|
|
173
|
+
const position = root ? nabu.children.indexOf(root) : -1;
|
|
174
|
+
index = position < 0 ? (at === 'before' ? 0 : nabu.children.length) : position + (at === 'before' ? 0 : 1);
|
|
175
|
+
}
|
|
176
|
+
createRegion(nabu, { type: wanted, parentId, index, title, fields });
|
|
177
|
+
await nabu.commit();
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Where the caret goes once `region`'s title is gone (merge, unwrap): the start of the first text block of its body, else null.
|
|
183
|
+
* @param {Region} region @returns {Caret | null}
|
|
184
|
+
*/
|
|
185
|
+
function bodyLanding(region) {
|
|
186
|
+
const first = firstTextBlock(region.block.children);
|
|
187
|
+
return first ? { block: first, offset: 0 } : null;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Dissolves a region node in place: its children take its position in its parent, in order, and the node is deleted by the
|
|
192
|
+
* caller. Writes only.
|
|
193
|
+
* @param {NabuNode} node
|
|
194
|
+
*/
|
|
195
|
+
function spill(node) {
|
|
196
|
+
/** @type {NabuNode} */
|
|
197
|
+
let anchor = node;
|
|
198
|
+
for (const child of kids(node)) {
|
|
199
|
+
child.moveAfter(anchor);
|
|
200
|
+
anchor = child;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Merges a region with what is before it (ADR 018 §3.5, see `mergePlan`): into its previous sibling when that one is a region
|
|
206
|
+
* of the same level (its body joins the end of that body), else the region is dissolved in place (its body takes its
|
|
207
|
+
* position in its parent). The title goes away in both cases (an undo gives it back). One undo step. The caret, if given,
|
|
208
|
+
* is put back; when it was in the title that went away it goes to the start of the body (else the end of the region joined).
|
|
209
|
+
* @param {Nabu} nabu
|
|
210
|
+
* @param {Region} region
|
|
211
|
+
* @param {Caret | null} [caret]
|
|
212
|
+
* @returns {Promise<boolean>} false when there is nothing to merge with (the first region of the document)
|
|
213
|
+
*/
|
|
214
|
+
export async function mergeRegion(nabu, region, caret = null) {
|
|
215
|
+
const plan = mergePlan(nabu, region);
|
|
216
|
+
if (!plan) return false;
|
|
217
|
+
const landing = caret && caret.block !== region.block
|
|
218
|
+
? caret
|
|
219
|
+
: (bodyLanding(region) ?? (plan.kind === 'merge' ? { block: plan.into.block, offset: plan.into.block.title.length } : null));
|
|
220
|
+
|
|
221
|
+
if (plan.kind === 'merge') {
|
|
222
|
+
const target = plan.into.block.node;
|
|
223
|
+
let at = kids(target).length;
|
|
224
|
+
for (const child of kids(region.block.node)) child.move(target, at++);
|
|
225
|
+
} else {
|
|
226
|
+
spill(region.block.node);
|
|
227
|
+
}
|
|
228
|
+
nabu.delete(region.block);
|
|
229
|
+
await nabu.commit();
|
|
230
|
+
if (landing) await restoreCaret(nabu, landing.block, landing.offset);
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Dissolves a region (ADR 018 §3.5): its title, when not empty, becomes a default paragraph at the region's position (delta and
|
|
236
|
+
* marks kept), followed by the children of its body, in order; the region is deleted. One undo step. The caret goes to the
|
|
237
|
+
* same offset in the paragraph if it was in the title, else it is put back.
|
|
238
|
+
* @param {Nabu} nabu
|
|
239
|
+
* @param {Region} region
|
|
240
|
+
* @param {Caret | null} [caret]
|
|
241
|
+
* @returns {Promise<boolean>}
|
|
242
|
+
*/
|
|
243
|
+
export async function unwrapRegion(nabu, region, caret = null) {
|
|
244
|
+
const node = region.block.node;
|
|
245
|
+
/** @type {Caret | null} */
|
|
246
|
+
let landing = caret && caret.block !== region.block ? caret : bodyLanding(region);
|
|
247
|
+
|
|
248
|
+
/** @type {NabuNode} */
|
|
249
|
+
let anchor = node;
|
|
250
|
+
if (region.block.title.length) {
|
|
251
|
+
const paragraph = createDefaultBlock(nabu, { delta: region.block.container.toDelta() }, parentIdOf(node), node.index() ?? 0);
|
|
252
|
+
anchor = paragraph.node;
|
|
253
|
+
if (caret && caret.block === region.block) landing = { block: paragraph, offset: caret.offset };
|
|
254
|
+
}
|
|
255
|
+
for (const child of kids(node)) {
|
|
256
|
+
child.moveAfter(anchor);
|
|
257
|
+
anchor = child;
|
|
258
|
+
}
|
|
259
|
+
nabu.delete(region.block);
|
|
260
|
+
await nabu.commit();
|
|
261
|
+
if (landing) await restoreCaret(nabu, landing.block, landing.offset);
|
|
262
|
+
return true;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Leaves a region (ADR 018 §3.5, `region:exit`): the caret ends up in an empty default paragraph right after the region, in the
|
|
267
|
+
* region's parent (one level up).
|
|
268
|
+
* - the caret block is an EMPTY default paragraph that is the LAST child of the region's body: that very paragraph moves out,
|
|
269
|
+
* right after the region (so repeating the command climbs scene, chapter, part, root);
|
|
270
|
+
* - otherwise a new empty paragraph is created there.
|
|
271
|
+
* One undo step.
|
|
272
|
+
* @param {Nabu} nabu
|
|
273
|
+
* @param {Region} region
|
|
274
|
+
* @param {Block | null} [from] the block holding the caret
|
|
275
|
+
* @returns {Promise<boolean>}
|
|
276
|
+
*/
|
|
277
|
+
export async function exitRegion(nabu, region, from = null) {
|
|
278
|
+
const node = region.block.node;
|
|
279
|
+
const last = region.block.children[region.block.children.length - 1];
|
|
280
|
+
const movable = !!from && from === last && from.type === defaultType(nabu)
|
|
281
|
+
&& /** @type {any} */ (from.behaviors.get('text'))?.text === '' && !(/** @type {any} */ (from).children?.length);
|
|
282
|
+
|
|
283
|
+
/** @type {Block} */
|
|
284
|
+
let landing;
|
|
285
|
+
if (movable && from) {
|
|
286
|
+
from.node.moveAfter(node);
|
|
287
|
+
landing = from;
|
|
288
|
+
} else {
|
|
289
|
+
landing = createDefaultBlock(nabu, {}, parentIdOf(node), (node.index() ?? 0) + 1);
|
|
290
|
+
}
|
|
291
|
+
await nabu.commit();
|
|
292
|
+
await restoreCaret(nabu, landing, 0);
|
|
293
|
+
return true;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Swaps a region with the previous / next sibling node that is a region of the SAME level, in the same parent: they trade
|
|
298
|
+
* places with their bodies (a region is a container, it moves with its subtree); the text siblings in between stay where
|
|
299
|
+
* they are. At an edge, or when the next sibling region has another level, nothing moves (no commit). One undo step.
|
|
300
|
+
* @param {Nabu} nabu
|
|
301
|
+
* @param {Region} region
|
|
302
|
+
* @param {'up' | 'down'} direction
|
|
303
|
+
* @param {Caret | null} [caret]
|
|
304
|
+
* @returns {Promise<boolean>}
|
|
305
|
+
*/
|
|
306
|
+
export async function moveRegion(nabu, region, direction, caret = null) {
|
|
307
|
+
const siblings = siblingsOf(nabu, region.block);
|
|
308
|
+
const step = direction === 'up' ? -1 : 1;
|
|
309
|
+
let j = siblings.indexOf(region.block) + step;
|
|
310
|
+
while (siblings[j] && siblings[j].type !== 'region') j += step;
|
|
311
|
+
const other = siblings[j] ? Regions.get(nabu).of(siblings[j]) : null;
|
|
312
|
+
if (!other || other.level !== region.level) return false;
|
|
313
|
+
|
|
314
|
+
const [first, second] = direction === 'up' ? [other, region] : [region, other];
|
|
315
|
+
const beforeSecond = neighbourOf(nabu, second.block, -1);
|
|
316
|
+
second.block.node.moveBefore(first.block.node);
|
|
317
|
+
// Text between the two: the first one goes after it, i.e. to where the second one was.
|
|
318
|
+
if (beforeSecond && beforeSecond !== first.block) first.block.node.moveAfter(beforeSecond.node);
|
|
319
|
+
|
|
320
|
+
await nabu.commit();
|
|
321
|
+
if (caret) await restoreCaret(nabu, caret.block, caret.offset);
|
|
322
|
+
return true;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Demotes a region (ADR 018 §3.5), structurally like a list indent: it is retyped to the first type of the level below and
|
|
327
|
+
* becomes the LAST child of its previous sibling node, which must be a region shallower than the new type. The sub-regions of
|
|
328
|
+
* its body that are now too shallow for it (level <= its new level) are moved out, in order, right after it (inside the
|
|
329
|
+
* sibling). No-op (no commit) when there is no deeper type or no such sibling. One undo step.
|
|
330
|
+
* @param {Nabu} nabu
|
|
331
|
+
* @param {Region} region
|
|
332
|
+
* @param {Caret | null} [caret]
|
|
333
|
+
* @returns {Promise<boolean>}
|
|
334
|
+
*/
|
|
335
|
+
export async function demoteRegion(nabu, region, caret = null) {
|
|
336
|
+
const regions = Regions.get(nabu);
|
|
337
|
+
const target = region.def && typeOfLevel(regions, region.level + 1);
|
|
338
|
+
if (!target) return false;
|
|
339
|
+
const previous = neighbourOf(nabu, region.block, -1);
|
|
340
|
+
const into = previous?.type === 'region' ? regions.of(previous) : null;
|
|
341
|
+
if (!into || into.level >= target.level) return false;
|
|
342
|
+
const out = region.children.filter((child) => child.def && child.level <= target.level);
|
|
343
|
+
|
|
344
|
+
const node = region.block.node;
|
|
345
|
+
region.block.setRegionType(target.id, { commit: false });
|
|
346
|
+
node.move(into.block.node, kids(into.block.node).length);
|
|
347
|
+
/** @type {NabuNode} */
|
|
348
|
+
let anchor = node;
|
|
349
|
+
for (const child of out) {
|
|
350
|
+
child.block.node.moveAfter(anchor);
|
|
351
|
+
anchor = child.block.node;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
await nabu.commit();
|
|
355
|
+
if (caret) await restoreCaret(nabu, caret.block, caret.offset);
|
|
356
|
+
return true;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Promotes a region (ADR 018 §3.5, Q4), the inverse of a demotion: it is retyped to the first type of the level above and
|
|
361
|
+
* leaves its parent region P to sit right after it (in P's parent); every block that FOLLOWED it in P's body, text and
|
|
362
|
+
* regions, follows it into its body, in order, appended after its own. No-op (no commit) when it has no parent region or
|
|
363
|
+
* there is no type one level up. One undo step.
|
|
364
|
+
* @param {Nabu} nabu
|
|
365
|
+
* @param {Region} region
|
|
366
|
+
* @param {Caret | null} [caret]
|
|
367
|
+
* @returns {Promise<boolean>}
|
|
368
|
+
*/
|
|
369
|
+
export async function promoteRegion(nabu, region, caret = null) {
|
|
370
|
+
const regions = Regions.get(nabu);
|
|
371
|
+
const parent = region.parent;
|
|
372
|
+
const target = region.def && parent && typeOfLevel(regions, region.level - 1);
|
|
373
|
+
if (!parent || !target) return false;
|
|
374
|
+
const siblings = parent.block.children;
|
|
375
|
+
const followers = siblings.slice(siblings.indexOf(region.block) + 1);
|
|
376
|
+
|
|
377
|
+
const node = region.block.node;
|
|
378
|
+
region.block.setRegionType(target.id, { commit: false });
|
|
379
|
+
node.moveAfter(parent.block.node);
|
|
380
|
+
let at = kids(node).length;
|
|
381
|
+
for (const follower of followers) follower.node.move(node, at++);
|
|
382
|
+
|
|
383
|
+
await nabu.commit();
|
|
384
|
+
if (caret) await restoreCaret(nabu, caret.block, caret.offset);
|
|
385
|
+
return true;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Puts the caret at the first text position of the BODY of `region` (the first text block after its title, inside it), else
|
|
390
|
+
* at the end of its title, and scrolls it into view.
|
|
391
|
+
* @param {Nabu} nabu
|
|
392
|
+
* @param {Region} region
|
|
393
|
+
* @returns {Promise<boolean>}
|
|
394
|
+
*/
|
|
395
|
+
export async function goToRegion(nabu, region) {
|
|
396
|
+
await tick();
|
|
397
|
+
const first = firstTextBlock(region.block.children);
|
|
398
|
+
if (first) nabu.selection.setCursor(first, 0);
|
|
399
|
+
else nabu.selection.setCursor(region.block, region.block.title.length);
|
|
400
|
+
/** @type {any} */ ((first ?? region.block).element)?.scrollIntoView?.({ block: 'center' });
|
|
401
|
+
return true;
|
|
402
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The regions that contain `block` when it is a closing mark: a default paragraph without children, a DIRECT child of a region
|
|
3
|
+
* body, whose text is `:::` (trailing spaces allowed) or empty. Innermost first. Empty when `block` is not such a mark.
|
|
4
|
+
* @param {Nabu} nabu @param {Block | null | undefined} block
|
|
5
|
+
* @returns {Region[]}
|
|
6
|
+
*/
|
|
7
|
+
export function closeChain(nabu: Nabu, block: Block | null | undefined): Region[];
|
|
8
|
+
/**
|
|
9
|
+
* The options of the `region:close` chooser: one per region of `chain`, innermost first. `region` is what the command runs on.
|
|
10
|
+
* @param {Region[]} chain
|
|
11
|
+
* @param {ReturnType<typeof createMessages>} text the region messages (`createMessages`)
|
|
12
|
+
* @returns {Array<{ id: string, label: string, detail: string, region: Region }>}
|
|
13
|
+
*/
|
|
14
|
+
export function closeOptions(chain: Region[], text: ReturnType<typeof createMessages>): Array<{
|
|
15
|
+
id: string;
|
|
16
|
+
label: string;
|
|
17
|
+
detail: string;
|
|
18
|
+
region: Region;
|
|
19
|
+
}>;
|
|
20
|
+
/**
|
|
21
|
+
* Closes `region` (an ancestor of the closing mark `mark`, see the module doc). Resolves to false (nothing changed) when `mark` is
|
|
22
|
+
* not a closing mark inside `region`.
|
|
23
|
+
* @param {Nabu} nabu @param {Region} region @param {Block} mark
|
|
24
|
+
* @returns {Promise<boolean>}
|
|
25
|
+
*/
|
|
26
|
+
export function closeRegionAt(nabu: Nabu, region: Region, mark: Block): Promise<boolean>;
|
|
27
|
+
/**
|
|
28
|
+
* `Extension#hooks.onBeforeInput` of the `:::` + Enter trigger (see the module doc). Returns BREAK when it took the Enter.
|
|
29
|
+
* @param {Nabu} nabu @param {InputEvent} event @param {Block} block
|
|
30
|
+
*/
|
|
31
|
+
export function closeOnEnter(nabu: Nabu, event: InputEvent, block: Block): symbol;
|
|
32
|
+
import type { Nabu } from "../nabu.svelte.js";
|
|
33
|
+
import type { Block } from "../block.svelte.js";
|
|
34
|
+
import type { Region } from "./regions.svelte.js";
|
|
35
|
+
import type { createMessages } from "./messages.js";
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Closing a region from inside it (ADR 018 §3.5, "Fermer une région"): the operation behind `region:close` and the `:::` + Enter
|
|
3
|
+
* trigger (the counterpart of `:::` + Space, which creates one: see create.js).
|
|
4
|
+
*
|
|
5
|
+
* The TRIGGER is an Enter (`insertParagraph`) in a default paragraph that holds exactly `:::` (trailing spaces allowed), the
|
|
6
|
+
* collapsed caret at its end, and that is a DIRECT child of a region body. Anywhere else (outside any region, in a list, with other
|
|
7
|
+
* text) Enter is an ordinary Enter. The regions that CONTAIN the caret, innermost first, are the things that can be closed:
|
|
8
|
+
*
|
|
9
|
+
* - one region: it is closed at once;
|
|
10
|
+
* - several: the `choose` confirmation of `region:close` lists them ("Close Scene 2.2 — The ferryman", detail = the numbering
|
|
11
|
+
* prefix). While it is open the Enter is NOT applied: Escape (or a click away) leaves the paragraph with its `:::` as it was,
|
|
12
|
+
* and the user can press Enter again or type on.
|
|
13
|
+
*
|
|
14
|
+
* THE EFFECT of closing X (R0 the innermost region, p the `:::` paragraph, a child of R0; X = R_k): p is deleted; the blocks that
|
|
15
|
+
* FOLLOWED p in the body of R0, then those that followed R0 in the body of R1, … up to those that followed R_{k-1} in the body of X
|
|
16
|
+
* (document order) leave their bodies and are moved, in that order, right after X, in the parent of X. The caret goes to the first
|
|
17
|
+
* text position of the first block that moved (a sub-region: its title); when nothing followed, an empty default paragraph is created
|
|
18
|
+
* right after X for it. Moved sub-regions stay valid for the nesting rule: they are deeper than the region they were in, which was
|
|
19
|
+
* deeper than X, which is deeper than the parent of X. One undo step; undo gives back the typed `:::` (and the caret after it).
|
|
20
|
+
*
|
|
21
|
+
* At the end of a region this is the same as `region:exit` (Mod-Shift-Enter): nothing follows, a paragraph comes after the region.
|
|
22
|
+
*
|
|
23
|
+
* @import { Nabu } from "../nabu.svelte.js";
|
|
24
|
+
* @import { Block } from "../block.svelte.js";
|
|
25
|
+
* @import { Region } from "./regions.svelte.js";
|
|
26
|
+
* @import { createMessages } from "./messages.js";
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import { tick } from "svelte";
|
|
30
|
+
import { Regions } from "./regions.svelte.js";
|
|
31
|
+
import { createDefaultBlock, defaultType, firstTextBlock, parentIdOf } from "./actions.js";
|
|
32
|
+
import { siblingsOf } from "./facts.js";
|
|
33
|
+
|
|
34
|
+
/** The text of a block, `null` when it holds none. @param {Block} block @returns {string | null} */
|
|
35
|
+
const textOf = (block) => /** @type {any} */ (block.behaviors.get('text'))?.text ?? null;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The regions that contain `block` when it is a closing mark: a default paragraph without children, a DIRECT child of a region
|
|
39
|
+
* body, whose text is `:::` (trailing spaces allowed) or empty. Innermost first. Empty when `block` is not such a mark.
|
|
40
|
+
* @param {Nabu} nabu @param {Block | null | undefined} block
|
|
41
|
+
* @returns {Region[]}
|
|
42
|
+
*/
|
|
43
|
+
export function closeChain(nabu, block) {
|
|
44
|
+
if (!block || block.type !== defaultType(nabu) || /** @type {any} */ (block).children?.length) return [];
|
|
45
|
+
if (!block.parent || block.parent.type !== 'region') return [];
|
|
46
|
+
const text = textOf(block);
|
|
47
|
+
if (text === null || !/^(::: *)?$/.test(text)) return [];
|
|
48
|
+
/** @type {Region[]} */
|
|
49
|
+
const chain = [];
|
|
50
|
+
for (let r = Regions.get(nabu).of(block.parent); r; r = r.parent) chain.push(r);
|
|
51
|
+
return chain;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The options of the `region:close` chooser: one per region of `chain`, innermost first. `region` is what the command runs on.
|
|
56
|
+
* @param {Region[]} chain
|
|
57
|
+
* @param {ReturnType<typeof createMessages>} text the region messages (`createMessages`)
|
|
58
|
+
* @returns {Array<{ id: string, label: string, detail: string, region: Region }>}
|
|
59
|
+
*/
|
|
60
|
+
export function closeOptions(chain, text) {
|
|
61
|
+
return chain.map((region) => ({
|
|
62
|
+
id: region.block.id,
|
|
63
|
+
label: text('closeOption', {
|
|
64
|
+
typeName: region.def?.name ?? region.type, name: region.prefix || region.title || region.def?.name || region.type,
|
|
65
|
+
prefix: region.prefix, title: region.title, label: region.label, type: region.type,
|
|
66
|
+
}),
|
|
67
|
+
detail: region.prefix,
|
|
68
|
+
region,
|
|
69
|
+
}));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Closes `region` (an ancestor of the closing mark `mark`, see the module doc). Resolves to false (nothing changed) when `mark` is
|
|
74
|
+
* not a closing mark inside `region`.
|
|
75
|
+
* @param {Nabu} nabu @param {Region} region @param {Block} mark
|
|
76
|
+
* @returns {Promise<boolean>}
|
|
77
|
+
*/
|
|
78
|
+
export async function closeRegionAt(nabu, region, mark) {
|
|
79
|
+
const chain = closeChain(nabu, mark);
|
|
80
|
+
const k = chain.findIndex((r) => r.block.id === region.block.id);
|
|
81
|
+
if (k < 0) return false;
|
|
82
|
+
|
|
83
|
+
/** The blocks that follow `block` in its parent. @param {Block} block @returns {Block[]} */
|
|
84
|
+
const after = (block) => { const all = siblingsOf(nabu, block); return all.slice(all.indexOf(block) + 1); };
|
|
85
|
+
// Everything is read before the first write.
|
|
86
|
+
const moved = [...after(mark)];
|
|
87
|
+
for (let j = 0; j < k; j++) moved.push(...after(chain[j].block));
|
|
88
|
+
const target = chain[k].block.node;
|
|
89
|
+
const landing = firstTextBlock(moved);
|
|
90
|
+
|
|
91
|
+
/** @type {Block | null} */
|
|
92
|
+
let created = null;
|
|
93
|
+
const mutate = () => {
|
|
94
|
+
/** @type {import("../nabu.svelte.js").NabuNode} */
|
|
95
|
+
let anchor = target;
|
|
96
|
+
for (const block of moved) { block.node.moveAfter(anchor); anchor = block.node; }
|
|
97
|
+
if (!landing) created = createDefaultBlock(nabu, {}, parentIdOf(target), (target.index() ?? 0) + 1 + moved.length);
|
|
98
|
+
nabu.delete(mark);
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
// The typed `:::` is already in the document and the Enter was not applied: `shortcut` (no trigger text) makes the closing its
|
|
102
|
+
// own undo step, whose undo gives the `:::` back with the caret after it. Same technique as `createRegionHere`.
|
|
103
|
+
if (mark.transformsEnabled !== false && /** @type {any} */ (mark.behaviors.get('text'))?.selection) await nabu.shortcut(mark, '', mutate);
|
|
104
|
+
else { mutate(); await nabu.commit(); }
|
|
105
|
+
|
|
106
|
+
const caretBlock = landing ?? created;
|
|
107
|
+
const fresh = caretBlock && nabu.blocks.get(caretBlock.id);
|
|
108
|
+
if (fresh) { await tick(); nabu.selection.setCursor(fresh, 0); }
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* `Extension#hooks.onBeforeInput` of the `:::` + Enter trigger (see the module doc). Returns BREAK when it took the Enter.
|
|
114
|
+
* @param {Nabu} nabu @param {InputEvent} event @param {Block} block
|
|
115
|
+
*/
|
|
116
|
+
export function closeOnEnter(nabu, event, block) {
|
|
117
|
+
const { BREAK, CONTINUE } = nabu;
|
|
118
|
+
if (event.inputType !== 'insertParagraph') return CONTINUE;
|
|
119
|
+
const behavior = /** @type {any} */ (block.behaviors.get('text'));
|
|
120
|
+
const sel = behavior?.selection;
|
|
121
|
+
if (!sel || !sel.isCollapsed || sel.from !== behavior.text.length || !/^::: *$/.test(behavior.text)) return CONTINUE;
|
|
122
|
+
const chain = closeChain(nabu, block);
|
|
123
|
+
if (!chain.length) return CONTINUE;
|
|
124
|
+
if (chain.length === 1) {
|
|
125
|
+
closeRegionAt(nabu, chain[0], block);
|
|
126
|
+
return BREAK;
|
|
127
|
+
}
|
|
128
|
+
nabu.selection.sync?.(); // the command reads selection.anchorBlock: don't wait for the async `selectionchange`
|
|
129
|
+
return nabu.commandConfirm.offer('region:close') ? BREAK : CONTINUE;
|
|
130
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {import("./messages.js").RegionMessages | undefined} messages
|
|
3
|
+
* @returns {Record<string, CommandDef>}
|
|
4
|
+
*/
|
|
5
|
+
export function createRegionCommands(messages: import("./messages.js").RegionMessages | undefined): Record<string, CommandDef>;
|
|
6
|
+
import type { CommandDef } from "../../utils/commands.js";
|