@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,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Programmatic creation of a region (ADR 018). The neutral building block of the region commands (see actions.js) and what
|
|
3
|
+
* tests / playgrounds call. A region is a container block: it is created EMPTY (a title, no body), at the root or in the
|
|
4
|
+
* body of another region.
|
|
5
|
+
*
|
|
6
|
+
* @import { Nabu } from "../nabu.svelte.js";
|
|
7
|
+
* @import { Block } from "../block.svelte.js";
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Where to insert, relative to the BODY of the parent (the root of the document by default):
|
|
12
|
+
* - `'start'` | `'end'` first / last position;
|
|
13
|
+
* - a number that index (clamped);
|
|
14
|
+
* - `{ after: Block }` right after that block (a child of the parent; a deeper block counts as its ancestor in the parent);
|
|
15
|
+
* - `{ before: Block }` right before it.
|
|
16
|
+
* @typedef {'start' | 'end' | number | { after: Block } | { before: Block }} InsertRegionAt
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** The class of the region block, or a thrown error when the extension is not installed. @param {Nabu} nabu @returns {any} */
|
|
20
|
+
function regionClass(nabu) {
|
|
21
|
+
const BlockClass = /** @type {any} */ (nabu.registry.get('region'));
|
|
22
|
+
if (!BlockClass) throw new Error('insertRegion: the Regions extension is not installed.');
|
|
23
|
+
return BlockClass;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The blocks of a body: the children of the parent block, or the root blocks. @param {Nabu} nabu @param {Block | null} parent @returns {Block[]} */
|
|
27
|
+
const siblingsOf = (nabu, parent) => /** @type {Block[]} */ (parent ? /** @type {any} */ (parent).children ?? [] : nabu.children);
|
|
28
|
+
|
|
29
|
+
/** Index of the child of `parent` that holds `block` (itself, or its closest ancestor in that body); -1 when it is not under `parent`. @param {Nabu} nabu @param {Block | null} parent @param {Block} block @returns {number} */
|
|
30
|
+
function indexUnder(nabu, parent, block) {
|
|
31
|
+
/** @type {Block | null} */
|
|
32
|
+
let current = block;
|
|
33
|
+
while (current && (current.parent ?? null) !== parent) current = current.parent ?? null;
|
|
34
|
+
return current ? siblingsOf(nabu, parent).indexOf(current) : -1;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Index in the body of `parent` for an `at`.
|
|
39
|
+
* @param {Nabu} nabu @param {Block | null} parent @param {InsertRegionAt} at @returns {number}
|
|
40
|
+
*/
|
|
41
|
+
function resolveIndex(nabu, parent, at) {
|
|
42
|
+
const count = siblingsOf(nabu, parent).length;
|
|
43
|
+
if (at === 'start') return 0;
|
|
44
|
+
if (at === 'end') return count;
|
|
45
|
+
if (typeof at === 'number') return Math.max(0, Math.min(count, Math.trunc(at)));
|
|
46
|
+
if (at && typeof at === 'object' && 'before' in at) { const i = indexUnder(nabu, parent, at.before); return i < 0 ? count : i; }
|
|
47
|
+
if (at && typeof at === 'object' && 'after' in at) { const i = indexUnder(nabu, parent, at.after); return i < 0 ? count : i + 1; }
|
|
48
|
+
return count;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Creates a region WITHOUT committing: the caller owns the transaction (an action that splits a block and creates a region is
|
|
53
|
+
* one undo step). `parentId` is the id of a region block (its body), `null` for the root; `index` is clamped. The title is
|
|
54
|
+
* the header text. Throws on an unknown type or when the extension is not installed.
|
|
55
|
+
* @param {Nabu} nabu
|
|
56
|
+
* @param {{ type?: string, parentId?: string | null, index?: number | null, title?: string, fields?: Record<string, any> }} [options]
|
|
57
|
+
* `type` defaults to the lowest-level declared type; `index` defaults to the end of the body.
|
|
58
|
+
* @returns {import("./region.svelte.js").RegionBlock}
|
|
59
|
+
*/
|
|
60
|
+
export function createRegion(nabu, { type, parentId = null, index = null, title, fields } = {}) {
|
|
61
|
+
const BlockClass = regionClass(nabu);
|
|
62
|
+
if (type !== undefined && !BlockClass.config.types.has(type)) throw new Error(`insertRegion: region type "${type}" is not declared.`);
|
|
63
|
+
const count = parentId ? (/** @type {any} */ (nabu.blocks.get(parentId))?.children?.length ?? 0) : nabu.tree.roots().length;
|
|
64
|
+
const at = index == null ? count : Math.max(0, Math.min(count, Math.trunc(index)));
|
|
65
|
+
return BlockClass.create(nabu, 'region', { regionType: type, title, fields }, parentId, at);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Inserts a region as one undo step.
|
|
70
|
+
* @param {Nabu} nabu
|
|
71
|
+
* @param {{ type?: string, at?: InsertRegionAt, title?: string, fields?: Record<string, any>, parent?: Block | null }} [options]
|
|
72
|
+
* `type` defaults to the lowest-level declared type; `at` defaults to `'end'`; `parent` is the region whose body receives
|
|
73
|
+
* the new one (the root of the document by default).
|
|
74
|
+
* @returns {Promise<import("./region.svelte.js").RegionBlock>}
|
|
75
|
+
*/
|
|
76
|
+
export async function insertRegion(nabu, { type, at = 'end', title, fields, parent = null } = {}) {
|
|
77
|
+
const index = resolveIndex(nabu, parent, at);
|
|
78
|
+
const block = createRegion(nabu, { type, parentId: parent?.id ?? null, index, title, fields });
|
|
79
|
+
await nabu.commit();
|
|
80
|
+
return /** @type {import("./region.svelte.js").RegionBlock} */ (nabu.blocks.get(block.id) ?? block);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// ── Root helpers of the first region commands (ADR 016), until their rewrite on the tree (ADR 018 lot A2) ──
|
|
84
|
+
|
|
85
|
+
/** Root index of the top-level block holding `block` (the end of the document when it is not found). @param {Nabu} nabu @param {Block} block @returns {number} */
|
|
86
|
+
export function rootIndexOf(nabu, block) {
|
|
87
|
+
/** @type {Block} */
|
|
88
|
+
let root = block;
|
|
89
|
+
while (root.parent) root = /** @type {Block} */ (root.parent);
|
|
90
|
+
const index = nabu.children.indexOf(root);
|
|
91
|
+
return index < 0 ? nabu.children.length : index;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Creates a region at a ROOT index WITHOUT committing.
|
|
96
|
+
* @param {Nabu} nabu
|
|
97
|
+
* @param {{ type?: string, index: number, title?: string, fields?: Record<string, any> }} options
|
|
98
|
+
* @returns {import("./region.svelte.js").RegionBlock}
|
|
99
|
+
*/
|
|
100
|
+
export function createMarker(nabu, { type, index, title, fields }) {
|
|
101
|
+
return createRegion(nabu, { type, parentId: null, index, title, fields });
|
|
102
|
+
}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves the messages of an integrator over the defaults: `text('mergePrevious', params)`.
|
|
3
|
+
* @param {RegionMessages | undefined} custom
|
|
4
|
+
* @returns {<K extends keyof RegionMessages>(key: K, params?: any) => string}
|
|
5
|
+
*/
|
|
6
|
+
export function createMessages(custom: RegionMessages | undefined): <K extends keyof RegionMessages>(key: K, params?: any) => string;
|
|
7
|
+
/**
|
|
8
|
+
* Texts of the region commands (ADR 016 lot 2, ADR 018 lot A2): the labels of the commands (help sheet, palette, the prompt of
|
|
9
|
+
* a hold) and the prompt of the two protected merges. English by default; override any of them with the `messages` option of
|
|
10
|
+
* `RegionsExtension({ messages })` (partial object; a value is a string or a function of the parameters below).
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {Object} RegionMessageParams
|
|
14
|
+
* @property {string} title the title of the region concerned, else its label, else its type id (never empty)
|
|
15
|
+
* @property {string} label its computed label ("1.1 The bank"; may be empty)
|
|
16
|
+
* @property {string} type its type id
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* @typedef {Object} RegionNameParams
|
|
20
|
+
* @property {string} name the region named for a reader: its numbering prefix ("1.2"), else its title, else the name of its type
|
|
21
|
+
* @property {string} prefix its numbering prefix ("1.2"; may be empty)
|
|
22
|
+
* @property {string} title its plain-text title (may be empty)
|
|
23
|
+
* @property {string} label its computed label ("1.2 The bank"; may be empty)
|
|
24
|
+
* @property {string} type its type id
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* @typedef {Object} RegionTypeParams
|
|
28
|
+
* @property {string} name the readable name of the type (its `name`, else its id)
|
|
29
|
+
* @property {string} type its type id
|
|
30
|
+
*/
|
|
31
|
+
/**
|
|
32
|
+
* @typedef {Object} RegionMessages
|
|
33
|
+
* @property {string | (() => string)} [split] label of `region:split`
|
|
34
|
+
* @property {string | ((p: { type: string }) => string)} [insert] label of `region:insert`
|
|
35
|
+
* @property {string | ((p: RegionMessageParams) => string)} [mergePrevious] label of `region:merge-previous`, "the previous region" being `p`
|
|
36
|
+
* @property {string | ((p: RegionMessageParams) => string)} [mergeNext] label of `region:merge-next` when the next region (`p`, the one that disappears) joins the previous region
|
|
37
|
+
* @property {string | (() => string)} [mergePreviousGeneric] label of `region:merge-previous` when no region is known (`commands.list()` without a caret): "Merge with the previous region"
|
|
38
|
+
* @property {string | (() => string)} [mergeNextGeneric] label of `region:merge-next` when no region is known (`commands.list()` without a caret)
|
|
39
|
+
* @property {string | ((p: RegionMessageParams) => string)} [dissolve] label of a protected merge that has no region to join: the region `p` is dissolved in place
|
|
40
|
+
* (its body stays where it is, its title goes away)
|
|
41
|
+
* @property {string | (() => string)} [exit] label of `region:exit`
|
|
42
|
+
* @property {string | (() => string)} [unwrap] label of `region:unwrap`
|
|
43
|
+
* @property {string | (() => string)} [move]
|
|
44
|
+
* @property {string | (() => string)} [moveUp]
|
|
45
|
+
* @property {string | (() => string)} [moveDown]
|
|
46
|
+
* @property {string | (() => string)} [promote]
|
|
47
|
+
* @property {string | (() => string)} [demote]
|
|
48
|
+
* @property {string | (() => string)} [next]
|
|
49
|
+
* @property {string | (() => string)} [prev]
|
|
50
|
+
* @property {string | (() => string)} [parent]
|
|
51
|
+
* @property {string | (() => string)} [create] label of `region:create`
|
|
52
|
+
* @property {string | ((p: { count: number }) => string)} [chooser] prompt of the `:::` chooser (announced to screen readers; `count` entries)
|
|
53
|
+
* @property {string | ((p: RegionTypeParams) => string)} [createOption] entry of the chooser for a type that fits here ("Section")
|
|
54
|
+
* @property {string | ((p: RegionTypeParams) => string)} [createNext] entry for a type created as a SIBLING after the current region ("Next section")
|
|
55
|
+
* @property {string | ((p: RegionTypeParams) => string)} [slash] title of the slash-menu entry of a type ("Section")
|
|
56
|
+
* @property {string | (() => string)} [untitled] placeholder of an empty title (ADR 018)
|
|
57
|
+
* @property {string | (() => string)} [closeCommand] label of `region:close`
|
|
58
|
+
* @property {string | ((p: RegionNameParams & { typeName: string }) => string)} [closeOption] entry of the `:::` + Enter chooser ("Close Scene 2.2 — The ferryman"). `p.typeName`
|
|
59
|
+
* is the readable name of the type, `p.prefix` its numbering prefix, `p.title` its plain-text title (all may be empty but the type name)
|
|
60
|
+
* @property {string | ((p: { count: number }) => string)} [closeChooser] prompt of the `:::` + Enter chooser (announced to screen readers)
|
|
61
|
+
* @property {string | (() => string)} [closeFooter] hint under the `:::` + Enter chooser ("↑↓ choose · ↵ close · Esc cancel")
|
|
62
|
+
* @property {string | (() => string)} [groupHere] header of the chooser group of the types created INSIDE the current region (or at the root) ("Here")
|
|
63
|
+
* @property {string | (() => string)} [groupNext] header of the chooser group of the sibling entries ("Next")
|
|
64
|
+
* @property {string | (() => string)} [chooserFooter] hint under the chooser ("↑↓ choose · ↵ create · Esc cancel")
|
|
65
|
+
* @property {string | ((p: RegionNameParams) => string)} [endOf] first half of the end marker, drawn after a region that is followed by
|
|
66
|
+
* text of its parent ("End of 1.2"). `p.name` is the prefix, else the title, else the type name.
|
|
67
|
+
* @property {string | ((p: RegionNameParams) => string)} [backTo] second half of the end marker when the parent is a region ("back to 1. The ferryman");
|
|
68
|
+
* `p` describes the PARENT region (`p.name` is its label)
|
|
69
|
+
* @property {string | (() => string)} [outside] second half of the end marker when the parent is the root ("outside regions")
|
|
70
|
+
* @property {string | ((p: { keys: string, label: string }) => string)} [hold] the prompt of a protected merge ("Hold ⌫ to merge with “…”"):
|
|
71
|
+
* shown above the seam and announced to screen readers
|
|
72
|
+
*/
|
|
73
|
+
/**
|
|
74
|
+
* The default texts of the regions: command labels, the seam and end marker wording, the chooser and the hold prompt (English).
|
|
75
|
+
* Override any key with `RegionsExtension({ messages })`.
|
|
76
|
+
* @type {Required<RegionMessages>}
|
|
77
|
+
*/
|
|
78
|
+
export const DEFAULT_REGION_MESSAGES: Required<RegionMessages>;
|
|
79
|
+
export type RegionMessageParams = {
|
|
80
|
+
/**
|
|
81
|
+
* the title of the region concerned, else its label, else its type id (never empty)
|
|
82
|
+
*/
|
|
83
|
+
title: string;
|
|
84
|
+
/**
|
|
85
|
+
* its computed label ("1.1 The bank"; may be empty)
|
|
86
|
+
*/
|
|
87
|
+
label: string;
|
|
88
|
+
/**
|
|
89
|
+
* its type id
|
|
90
|
+
*/
|
|
91
|
+
type: string;
|
|
92
|
+
};
|
|
93
|
+
export type RegionNameParams = {
|
|
94
|
+
/**
|
|
95
|
+
* the region named for a reader: its numbering prefix ("1.2"), else its title, else the name of its type
|
|
96
|
+
*/
|
|
97
|
+
name: string;
|
|
98
|
+
/**
|
|
99
|
+
* its numbering prefix ("1.2"; may be empty)
|
|
100
|
+
*/
|
|
101
|
+
prefix: string;
|
|
102
|
+
/**
|
|
103
|
+
* its plain-text title (may be empty)
|
|
104
|
+
*/
|
|
105
|
+
title: string;
|
|
106
|
+
/**
|
|
107
|
+
* its computed label ("1.2 The bank"; may be empty)
|
|
108
|
+
*/
|
|
109
|
+
label: string;
|
|
110
|
+
/**
|
|
111
|
+
* its type id
|
|
112
|
+
*/
|
|
113
|
+
type: string;
|
|
114
|
+
};
|
|
115
|
+
export type RegionTypeParams = {
|
|
116
|
+
/**
|
|
117
|
+
* the readable name of the type (its `name`, else its id)
|
|
118
|
+
*/
|
|
119
|
+
name: string;
|
|
120
|
+
/**
|
|
121
|
+
* its type id
|
|
122
|
+
*/
|
|
123
|
+
type: string;
|
|
124
|
+
};
|
|
125
|
+
export type RegionMessages = {
|
|
126
|
+
/**
|
|
127
|
+
* label of `region:split`
|
|
128
|
+
*/
|
|
129
|
+
split?: string | (() => string) | undefined;
|
|
130
|
+
/**
|
|
131
|
+
* label of `region:insert`
|
|
132
|
+
*/
|
|
133
|
+
insert?: string | ((p: {
|
|
134
|
+
type: string;
|
|
135
|
+
}) => string) | undefined;
|
|
136
|
+
/**
|
|
137
|
+
* label of `region:merge-previous`, "the previous region" being `p`
|
|
138
|
+
*/
|
|
139
|
+
mergePrevious?: string | ((p: RegionMessageParams) => string) | undefined;
|
|
140
|
+
/**
|
|
141
|
+
* label of `region:merge-next` when the next region (`p`, the one that disappears) joins the previous region
|
|
142
|
+
*/
|
|
143
|
+
mergeNext?: string | ((p: RegionMessageParams) => string) | undefined;
|
|
144
|
+
/**
|
|
145
|
+
* label of `region:merge-previous` when no region is known (`commands.list()` without a caret): "Merge with the previous region"
|
|
146
|
+
*/
|
|
147
|
+
mergePreviousGeneric?: string | (() => string) | undefined;
|
|
148
|
+
/**
|
|
149
|
+
* label of `region:merge-next` when no region is known (`commands.list()` without a caret)
|
|
150
|
+
*/
|
|
151
|
+
mergeNextGeneric?: string | (() => string) | undefined;
|
|
152
|
+
/**
|
|
153
|
+
* label of a protected merge that has no region to join: the region `p` is dissolved in place
|
|
154
|
+
* (its body stays where it is, its title goes away)
|
|
155
|
+
*/
|
|
156
|
+
dissolve?: string | ((p: RegionMessageParams) => string) | undefined;
|
|
157
|
+
/**
|
|
158
|
+
* label of `region:exit`
|
|
159
|
+
*/
|
|
160
|
+
exit?: string | (() => string) | undefined;
|
|
161
|
+
/**
|
|
162
|
+
* label of `region:unwrap`
|
|
163
|
+
*/
|
|
164
|
+
unwrap?: string | (() => string) | undefined;
|
|
165
|
+
move?: string | (() => string) | undefined;
|
|
166
|
+
moveUp?: string | (() => string) | undefined;
|
|
167
|
+
moveDown?: string | (() => string) | undefined;
|
|
168
|
+
promote?: string | (() => string) | undefined;
|
|
169
|
+
demote?: string | (() => string) | undefined;
|
|
170
|
+
next?: string | (() => string) | undefined;
|
|
171
|
+
prev?: string | (() => string) | undefined;
|
|
172
|
+
parent?: string | (() => string) | undefined;
|
|
173
|
+
/**
|
|
174
|
+
* label of `region:create`
|
|
175
|
+
*/
|
|
176
|
+
create?: string | (() => string) | undefined;
|
|
177
|
+
/**
|
|
178
|
+
* prompt of the `:::` chooser (announced to screen readers; `count` entries)
|
|
179
|
+
*/
|
|
180
|
+
chooser?: string | ((p: {
|
|
181
|
+
count: number;
|
|
182
|
+
}) => string) | undefined;
|
|
183
|
+
/**
|
|
184
|
+
* entry of the chooser for a type that fits here ("Section")
|
|
185
|
+
*/
|
|
186
|
+
createOption?: string | ((p: RegionTypeParams) => string) | undefined;
|
|
187
|
+
/**
|
|
188
|
+
* entry for a type created as a SIBLING after the current region ("Next section")
|
|
189
|
+
*/
|
|
190
|
+
createNext?: string | ((p: RegionTypeParams) => string) | undefined;
|
|
191
|
+
/**
|
|
192
|
+
* title of the slash-menu entry of a type ("Section")
|
|
193
|
+
*/
|
|
194
|
+
slash?: string | ((p: RegionTypeParams) => string) | undefined;
|
|
195
|
+
/**
|
|
196
|
+
* placeholder of an empty title (ADR 018)
|
|
197
|
+
*/
|
|
198
|
+
untitled?: string | (() => string) | undefined;
|
|
199
|
+
/**
|
|
200
|
+
* label of `region:close`
|
|
201
|
+
*/
|
|
202
|
+
closeCommand?: string | (() => string) | undefined;
|
|
203
|
+
/**
|
|
204
|
+
* entry of the `:::` + Enter chooser ("Close Scene 2.2 — The ferryman"). `p.typeName`
|
|
205
|
+
* is the readable name of the type, `p.prefix` its numbering prefix, `p.title` its plain-text title (all may be empty but the type name)
|
|
206
|
+
*/
|
|
207
|
+
closeOption?: string | ((p: RegionNameParams & {
|
|
208
|
+
typeName: string;
|
|
209
|
+
}) => string) | undefined;
|
|
210
|
+
/**
|
|
211
|
+
* prompt of the `:::` + Enter chooser (announced to screen readers)
|
|
212
|
+
*/
|
|
213
|
+
closeChooser?: string | ((p: {
|
|
214
|
+
count: number;
|
|
215
|
+
}) => string) | undefined;
|
|
216
|
+
/**
|
|
217
|
+
* hint under the `:::` + Enter chooser ("↑↓ choose · ↵ close · Esc cancel")
|
|
218
|
+
*/
|
|
219
|
+
closeFooter?: string | (() => string) | undefined;
|
|
220
|
+
/**
|
|
221
|
+
* header of the chooser group of the types created INSIDE the current region (or at the root) ("Here")
|
|
222
|
+
*/
|
|
223
|
+
groupHere?: string | (() => string) | undefined;
|
|
224
|
+
/**
|
|
225
|
+
* header of the chooser group of the sibling entries ("Next")
|
|
226
|
+
*/
|
|
227
|
+
groupNext?: string | (() => string) | undefined;
|
|
228
|
+
/**
|
|
229
|
+
* hint under the chooser ("↑↓ choose · ↵ create · Esc cancel")
|
|
230
|
+
*/
|
|
231
|
+
chooserFooter?: string | (() => string) | undefined;
|
|
232
|
+
/**
|
|
233
|
+
* first half of the end marker, drawn after a region that is followed by
|
|
234
|
+
* text of its parent ("End of 1.2"). `p.name` is the prefix, else the title, else the type name.
|
|
235
|
+
*/
|
|
236
|
+
endOf?: string | ((p: RegionNameParams) => string) | undefined;
|
|
237
|
+
/**
|
|
238
|
+
* second half of the end marker when the parent is a region ("back to 1. The ferryman");
|
|
239
|
+
* `p` describes the PARENT region (`p.name` is its label)
|
|
240
|
+
*/
|
|
241
|
+
backTo?: string | ((p: RegionNameParams) => string) | undefined;
|
|
242
|
+
/**
|
|
243
|
+
* second half of the end marker when the parent is the root ("outside regions")
|
|
244
|
+
*/
|
|
245
|
+
outside?: string | (() => string) | undefined;
|
|
246
|
+
/**
|
|
247
|
+
* the prompt of a protected merge ("Hold ⌫ to merge with “…”"):
|
|
248
|
+
* shown above the seam and announced to screen readers
|
|
249
|
+
*/
|
|
250
|
+
hold?: string | ((p: {
|
|
251
|
+
keys: string;
|
|
252
|
+
label: string;
|
|
253
|
+
}) => string) | undefined;
|
|
254
|
+
};
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Texts of the region commands (ADR 016 lot 2, ADR 018 lot A2): the labels of the commands (help sheet, palette, the prompt of
|
|
3
|
+
* a hold) and the prompt of the two protected merges. English by default; override any of them with the `messages` option of
|
|
4
|
+
* `RegionsExtension({ messages })` (partial object; a value is a string or a function of the parameters below).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {Object} RegionMessageParams
|
|
9
|
+
* @property {string} title the title of the region concerned, else its label, else its type id (never empty)
|
|
10
|
+
* @property {string} label its computed label ("1.1 The bank"; may be empty)
|
|
11
|
+
* @property {string} type its type id
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @typedef {Object} RegionNameParams
|
|
16
|
+
* @property {string} name the region named for a reader: its numbering prefix ("1.2"), else its title, else the name of its type
|
|
17
|
+
* @property {string} prefix its numbering prefix ("1.2"; may be empty)
|
|
18
|
+
* @property {string} title its plain-text title (may be empty)
|
|
19
|
+
* @property {string} label its computed label ("1.2 The bank"; may be empty)
|
|
20
|
+
* @property {string} type its type id
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @typedef {Object} RegionTypeParams
|
|
25
|
+
* @property {string} name the readable name of the type (its `name`, else its id)
|
|
26
|
+
* @property {string} type its type id
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @typedef {Object} RegionMessages
|
|
31
|
+
* @property {string | (() => string)} [split] label of `region:split`
|
|
32
|
+
* @property {string | ((p: { type: string }) => string)} [insert] label of `region:insert`
|
|
33
|
+
* @property {string | ((p: RegionMessageParams) => string)} [mergePrevious] label of `region:merge-previous`, "the previous region" being `p`
|
|
34
|
+
* @property {string | ((p: RegionMessageParams) => string)} [mergeNext] label of `region:merge-next` when the next region (`p`, the one that disappears) joins the previous region
|
|
35
|
+
* @property {string | (() => string)} [mergePreviousGeneric] label of `region:merge-previous` when no region is known (`commands.list()` without a caret): "Merge with the previous region"
|
|
36
|
+
* @property {string | (() => string)} [mergeNextGeneric] label of `region:merge-next` when no region is known (`commands.list()` without a caret)
|
|
37
|
+
* @property {string | ((p: RegionMessageParams) => string)} [dissolve] label of a protected merge that has no region to join: the region `p` is dissolved in place
|
|
38
|
+
* (its body stays where it is, its title goes away)
|
|
39
|
+
* @property {string | (() => string)} [exit] label of `region:exit`
|
|
40
|
+
* @property {string | (() => string)} [unwrap] label of `region:unwrap`
|
|
41
|
+
* @property {string | (() => string)} [move]
|
|
42
|
+
* @property {string | (() => string)} [moveUp]
|
|
43
|
+
* @property {string | (() => string)} [moveDown]
|
|
44
|
+
* @property {string | (() => string)} [promote]
|
|
45
|
+
* @property {string | (() => string)} [demote]
|
|
46
|
+
* @property {string | (() => string)} [next]
|
|
47
|
+
* @property {string | (() => string)} [prev]
|
|
48
|
+
* @property {string | (() => string)} [parent]
|
|
49
|
+
* @property {string | (() => string)} [create] label of `region:create`
|
|
50
|
+
* @property {string | ((p: { count: number }) => string)} [chooser] prompt of the `:::` chooser (announced to screen readers; `count` entries)
|
|
51
|
+
* @property {string | ((p: RegionTypeParams) => string)} [createOption] entry of the chooser for a type that fits here ("Section")
|
|
52
|
+
* @property {string | ((p: RegionTypeParams) => string)} [createNext] entry for a type created as a SIBLING after the current region ("Next section")
|
|
53
|
+
* @property {string | ((p: RegionTypeParams) => string)} [slash] title of the slash-menu entry of a type ("Section")
|
|
54
|
+
* @property {string | (() => string)} [untitled] placeholder of an empty title (ADR 018)
|
|
55
|
+
* @property {string | (() => string)} [closeCommand] label of `region:close`
|
|
56
|
+
* @property {string | ((p: RegionNameParams & { typeName: string }) => string)} [closeOption] entry of the `:::` + Enter chooser ("Close Scene 2.2 — The ferryman"). `p.typeName`
|
|
57
|
+
* is the readable name of the type, `p.prefix` its numbering prefix, `p.title` its plain-text title (all may be empty but the type name)
|
|
58
|
+
* @property {string | ((p: { count: number }) => string)} [closeChooser] prompt of the `:::` + Enter chooser (announced to screen readers)
|
|
59
|
+
* @property {string | (() => string)} [closeFooter] hint under the `:::` + Enter chooser ("↑↓ choose · ↵ close · Esc cancel")
|
|
60
|
+
* @property {string | (() => string)} [groupHere] header of the chooser group of the types created INSIDE the current region (or at the root) ("Here")
|
|
61
|
+
* @property {string | (() => string)} [groupNext] header of the chooser group of the sibling entries ("Next")
|
|
62
|
+
* @property {string | (() => string)} [chooserFooter] hint under the chooser ("↑↓ choose · ↵ create · Esc cancel")
|
|
63
|
+
* @property {string | ((p: RegionNameParams) => string)} [endOf] first half of the end marker, drawn after a region that is followed by
|
|
64
|
+
* text of its parent ("End of 1.2"). `p.name` is the prefix, else the title, else the type name.
|
|
65
|
+
* @property {string | ((p: RegionNameParams) => string)} [backTo] second half of the end marker when the parent is a region ("back to 1. The ferryman");
|
|
66
|
+
* `p` describes the PARENT region (`p.name` is its label)
|
|
67
|
+
* @property {string | (() => string)} [outside] second half of the end marker when the parent is the root ("outside regions")
|
|
68
|
+
* @property {string | ((p: { keys: string, label: string }) => string)} [hold] the prompt of a protected merge ("Hold ⌫ to merge with “…”"):
|
|
69
|
+
* shown above the seam and announced to screen readers
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The default texts of the regions: command labels, the seam and end marker wording, the chooser and the hold prompt (English).
|
|
74
|
+
* Override any key with `RegionsExtension({ messages })`.
|
|
75
|
+
* @type {Required<RegionMessages>}
|
|
76
|
+
*/
|
|
77
|
+
export const DEFAULT_REGION_MESSAGES = {
|
|
78
|
+
split: 'Split here',
|
|
79
|
+
insert: 'Insert a region',
|
|
80
|
+
mergePrevious: ({ title }) => `Merge with “${title}”`,
|
|
81
|
+
mergeNext: ({ title }) => `Merge “${title}” into this region`,
|
|
82
|
+
mergePreviousGeneric: 'Merge with the previous region',
|
|
83
|
+
mergeNextGeneric: 'Merge the next region into this one',
|
|
84
|
+
dissolve: ({ title }) => `Dissolve “${title}”`,
|
|
85
|
+
exit: 'Leave the region',
|
|
86
|
+
unwrap: 'Dissolve the region',
|
|
87
|
+
move: 'Move region',
|
|
88
|
+
moveUp: 'Move region up',
|
|
89
|
+
moveDown: 'Move region down',
|
|
90
|
+
promote: 'Promote region',
|
|
91
|
+
demote: 'Demote region',
|
|
92
|
+
next: 'Go to the next region',
|
|
93
|
+
prev: 'Go to the previous region',
|
|
94
|
+
parent: 'Go to the parent region',
|
|
95
|
+
create: 'Create a region',
|
|
96
|
+
chooser: ({ count }) => `Create a region: ${count} types. Arrow keys and Enter, or a number, to choose. Escape cancels.`,
|
|
97
|
+
createOption: ({ name }) => name,
|
|
98
|
+
createNext: ({ name }) => `Next ${name.charAt(0).toLowerCase()}${name.slice(1)}`,
|
|
99
|
+
slash: ({ name }) => name,
|
|
100
|
+
closeCommand: 'Close the region here',
|
|
101
|
+
// The prefix often holds the type name already ("Chapter 2"): it is not repeated.
|
|
102
|
+
closeOption: ({ typeName, prefix, title }) => `Close ${prefix && prefix.toLowerCase().startsWith(typeName.toLowerCase()) ? prefix : `${typeName}${prefix ? ` ${prefix}` : ''}`}${title ? ` — ${title}` : ''}`,
|
|
103
|
+
closeChooser: ({ count }) => `Close a region: ${count} choices. Arrow keys and Enter, or a number, to choose. Escape cancels.`,
|
|
104
|
+
closeFooter: '↑↓ choose · ↵ close · Esc cancel',
|
|
105
|
+
groupHere: 'Here',
|
|
106
|
+
groupNext: 'Next',
|
|
107
|
+
chooserFooter: '↑↓ choose · ↵ create · Esc cancel',
|
|
108
|
+
endOf: ({ name }) => `End of ${name}`,
|
|
109
|
+
backTo: ({ name }) => `back to ${name}`,
|
|
110
|
+
outside: 'outside regions',
|
|
111
|
+
untitled: 'Untitled',
|
|
112
|
+
hold: ({ keys, label }) => `Hold ${keys} to ${label.charAt(0).toLowerCase()}${label.slice(1)}`,
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Resolves the messages of an integrator over the defaults: `text('mergePrevious', params)`.
|
|
117
|
+
* @param {RegionMessages | undefined} custom
|
|
118
|
+
* @returns {<K extends keyof RegionMessages>(key: K, params?: any) => string}
|
|
119
|
+
*/
|
|
120
|
+
export function createMessages(custom) {
|
|
121
|
+
const own = custom && typeof custom === 'object' ? custom : {};
|
|
122
|
+
return (key, params) => {
|
|
123
|
+
const value = /** @type {any} */ (own)[key] ?? DEFAULT_REGION_MESSAGES[key];
|
|
124
|
+
try {
|
|
125
|
+
return String(typeof value === 'function' ? value(params) : value);
|
|
126
|
+
} catch {
|
|
127
|
+
const fallback = DEFAULT_REGION_MESSAGES[key];
|
|
128
|
+
return String(typeof fallback === 'function' ? /** @type {Function} */ (fallback)(params) : fallback);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Brings the regions back to where they may live (R1), then nests them by level (R2). Cheap when the document holds no
|
|
3
|
+
* region or when nothing structural is pending.
|
|
4
|
+
* @param {Nabu} nabu
|
|
5
|
+
*/
|
|
6
|
+
export function normalizeRegions(nabu: Nabu): void;
|
|
7
|
+
import type { Nabu } from "../nabu.svelte.js";
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Region schema normalizer (ADR 018 §3.1, §3.2: where a region may live, and how regions nest).
|
|
3
|
+
*
|
|
4
|
+
* Runs from the Regions extension's `onBeforeTransaction` hook, i.e. BEFORE `doc.commit()`: the move belongs
|
|
5
|
+
* to the transaction (hence the undo step) of the operation that displaced the region (a paste, a drop, a retype, a
|
|
6
|
+
* move). Works on Loro tree nodes and MOVES, never delete + recreate: the block id (and the caret) survive, and the
|
|
7
|
+
* region takes its title and its whole body along. Same mechanism as the list normalizer (`../list/normalize.js`);
|
|
8
|
+
* knows no other block type.
|
|
9
|
+
*
|
|
10
|
+
* Rule R1 (placement) — a region lives at the root or directly in the body of another region. A region whose parent
|
|
11
|
+
* is neither is moved RIGHT AFTER its highest ancestor that is still below the nearest region ancestor (or below the
|
|
12
|
+
* root): a region pasted in a list inside chapter C goes right after that list, inside C.
|
|
13
|
+
*
|
|
14
|
+
* Rule R2 (nesting) — a sub-region has a level strictly deeper than its parent region. A DECLARED region whose nearest
|
|
15
|
+
* region parent is declared and has a level >= its own is moved right after that parent; repeated until it is valid
|
|
16
|
+
* (it may climb several levels, up to the root). A region whose type is not declared is valid anywhere under a region
|
|
17
|
+
* and does not constrain its own children.
|
|
18
|
+
*
|
|
19
|
+
* Several regions displaced from one container keep their order (each one goes after the previous one). The target of
|
|
20
|
+
* R1 is isolated in `hoistTarget`, so that the rule is one line to change.
|
|
21
|
+
*
|
|
22
|
+
* @import { Nabu, NabuNode } from "../nabu.svelte.js";
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/** Block type of a region. */
|
|
26
|
+
const REGION_TYPE = 'region';
|
|
27
|
+
|
|
28
|
+
/** @param {NabuNode} node @returns {string | undefined} */
|
|
29
|
+
const typeOf = (node) => /** @type {any} */ (node.data.get("type"));
|
|
30
|
+
|
|
31
|
+
/** @param {NabuNode} node */
|
|
32
|
+
const isAlive = (node) => {
|
|
33
|
+
try { return !node.isDeleted(); } catch { return false; }
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** @param {NabuNode | null | undefined} node @returns {node is NabuNode} */
|
|
37
|
+
const isRegion = (node) => !!node && typeOf(node) === REGION_TYPE;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Inspects the pending (uncommitted) transaction. Returns null when it does not change the structure (no tree create /
|
|
41
|
+
* move / delete, no block `type` or `regionType` change): no region can have been displaced. Otherwise returns the ids of
|
|
42
|
+
* the nodes this transaction RETYPED into a region (their Block instance does not exist yet: it is created when the
|
|
43
|
+
* transaction commits).
|
|
44
|
+
* @param {Nabu} nabu
|
|
45
|
+
* @returns {{ retyped: Set<string> } | null}
|
|
46
|
+
*/
|
|
47
|
+
function inspectPendingOps(nabu) {
|
|
48
|
+
const pending = nabu.doc.getUncommittedOpsAsJson();
|
|
49
|
+
if (!pending) return null;
|
|
50
|
+
const treeId = nabu.tree.id;
|
|
51
|
+
let structural = false;
|
|
52
|
+
/** @type {Set<string>} */
|
|
53
|
+
const retyped = new Set();
|
|
54
|
+
for (const change of pending.changes) {
|
|
55
|
+
for (const op of change.ops) {
|
|
56
|
+
const content = /** @type {any} */ (op.content);
|
|
57
|
+
if (op.container === treeId) {
|
|
58
|
+
if (content.type === "create" || content.type === "move" || content.type === "delete") structural = true;
|
|
59
|
+
} else if (content.type === "insert" && (content.key === "type" || content.key === "regionType") && op.container.endsWith(":Map")) {
|
|
60
|
+
structural = true;
|
|
61
|
+
if (content.key === "type" && content.value === REGION_TYPE) retyped.add(op.container.slice(4, -4));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return structural ? { retyped } : null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Index path from the root, to compare two nodes in document order. @param {NabuNode} node @returns {number[]} */
|
|
69
|
+
function treePath(node) {
|
|
70
|
+
/** @type {number[]} */
|
|
71
|
+
const path = [];
|
|
72
|
+
for (let current = node; current; ) {
|
|
73
|
+
path.unshift(current.index() ?? 0);
|
|
74
|
+
current = /** @type {NabuNode} */ (current.parent());
|
|
75
|
+
}
|
|
76
|
+
return path;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** @param {number[]} a @param {number[]} b */
|
|
80
|
+
function comparePaths(a, b) {
|
|
81
|
+
for (let i = 0; i < Math.min(a.length, b.length); i++) if (a[i] !== b[i]) return a[i] - b[i];
|
|
82
|
+
return a.length - b.length;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Where a misplaced region goes: the node it is moved right after. THE place to change the placement rule.
|
|
87
|
+
* @param {NabuNode} region the misplaced region
|
|
88
|
+
* @param {NabuNode} anchor the highest ancestor of the region that is a child of its nearest region ancestor (or of the root)
|
|
89
|
+
* @param {NabuNode | undefined} lastPlaced the region of the same container hoisted just before, if any
|
|
90
|
+
* @returns {NabuNode}
|
|
91
|
+
*/
|
|
92
|
+
function hoistTarget(region, anchor, lastPlaced) {
|
|
93
|
+
return lastPlaced ?? anchor;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The declared level of a region node, or undefined when its type is not declared.
|
|
98
|
+
* @param {import('./types.js').RegionsConfig} config @param {NabuNode} node @returns {number | undefined}
|
|
99
|
+
*/
|
|
100
|
+
function levelOf(config, node) {
|
|
101
|
+
return config.types.get(/** @type {any} */ (/** @type {any} */ (node.data).get('regionType')))?.level;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Brings the regions back to where they may live (R1), then nests them by level (R2). Cheap when the document holds no
|
|
106
|
+
* region or when nothing structural is pending.
|
|
107
|
+
* @param {Nabu} nabu
|
|
108
|
+
*/
|
|
109
|
+
export function normalizeRegions(nabu) {
|
|
110
|
+
const config = /** @type {import('./types.js').RegionsConfig | undefined} */ (/** @type {any} */ (nabu.registry.get(REGION_TYPE))?.config);
|
|
111
|
+
if (!config) return;
|
|
112
|
+
const known = nabu.blocksByType.get(REGION_TYPE);
|
|
113
|
+
const pending = inspectPendingOps(nabu);
|
|
114
|
+
if (!pending) return;
|
|
115
|
+
|
|
116
|
+
/** @type {Map<string, NabuNode>} */
|
|
117
|
+
const candidates = new Map();
|
|
118
|
+
for (const block of known ?? []) {
|
|
119
|
+
const node = /** @type {NabuNode | undefined} */ (block.node);
|
|
120
|
+
if (node) candidates.set(node.id.toString(), node);
|
|
121
|
+
}
|
|
122
|
+
for (const id of pending.retyped) {
|
|
123
|
+
if (candidates.has(id) || !nabu.tree.has(/** @type {any} */ (id))) continue;
|
|
124
|
+
candidates.set(id, /** @type {NabuNode} */ (/** @type {unknown} */ (nabu.tree.getNodeByID(/** @type {any} */ (id)))));
|
|
125
|
+
}
|
|
126
|
+
if (!candidates.size) return;
|
|
127
|
+
|
|
128
|
+
/** Alive region nodes in document order, read from the CURRENT state of the tree. @returns {NabuNode[]} */
|
|
129
|
+
const regionNodes = () => {
|
|
130
|
+
const nodes = [...candidates.values()].filter((node) => node && isAlive(node) && isRegion(node));
|
|
131
|
+
const paths = new Map(nodes.map((node) => [node, treePath(node)]));
|
|
132
|
+
return nodes.sort((a, b) => comparePaths(/** @type {number[]} */ (paths.get(a)), /** @type {number[]} */ (paths.get(b))));
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
// ── R1: a region is at the root or in a region body ──
|
|
136
|
+
/** @type {{ node: NabuNode, anchor: NabuNode }[]} */
|
|
137
|
+
const misplaced = [];
|
|
138
|
+
for (const node of regionNodes()) {
|
|
139
|
+
const parent = node.parent();
|
|
140
|
+
if (!parent || isRegion(parent)) continue;
|
|
141
|
+
/** @type {NabuNode} */
|
|
142
|
+
let anchor = parent;
|
|
143
|
+
for (let up = anchor.parent(); up && !isRegion(up); up = anchor.parent()) anchor = up;
|
|
144
|
+
misplaced.push({ node, anchor });
|
|
145
|
+
}
|
|
146
|
+
/** @type {Map<string, NabuNode>} */
|
|
147
|
+
const lastPlaced = new Map();
|
|
148
|
+
for (const { node, anchor } of misplaced) {
|
|
149
|
+
const key = anchor.id.toString();
|
|
150
|
+
node.moveAfter(hoistTarget(node, anchor, lastPlaced.get(key)));
|
|
151
|
+
lastPlaced.set(key, node);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// ── R2: a sub-region is strictly deeper than its parent region ──
|
|
155
|
+
/** @type {Map<string, NabuNode>} */
|
|
156
|
+
const placedAfter = new Map();
|
|
157
|
+
for (const node of regionNodes()) {
|
|
158
|
+
const level = levelOf(config, node);
|
|
159
|
+
if (level === undefined) continue; // an undeclared type is valid anywhere under a region
|
|
160
|
+
for (;;) {
|
|
161
|
+
const parent = node.parent();
|
|
162
|
+
if (!parent || !isRegion(parent)) break;
|
|
163
|
+
const parentLevel = levelOf(config, parent);
|
|
164
|
+
if (parentLevel === undefined || parentLevel < level) break; // valid
|
|
165
|
+
const key = parent.id.toString();
|
|
166
|
+
node.moveAfter(placedAfter.get(key) ?? parent);
|
|
167
|
+
placedAfter.set(key, node);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|