@aionbuilders/nabu 0.1.0-alpha.9 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +523 -0
- package/LICENSE +21 -0
- package/README.md +82 -144
- package/dist/behaviors/text/RichText.svelte +25 -2
- package/dist/behaviors/text/index.d.ts +2 -0
- package/dist/behaviors/text/index.js +2 -0
- package/dist/behaviors/text/markdown-shortcuts.extension.d.ts +5 -0
- package/dist/behaviors/text/markdown-shortcuts.extension.js +79 -0
- package/dist/behaviors/text/markdown-syntax.d.ts +42 -0
- package/dist/behaviors/text/markdown-syntax.js +81 -0
- package/dist/behaviors/text/rich-text.extension.d.ts +3 -0
- package/dist/behaviors/text/rich-text.extension.js +135 -53
- package/dist/behaviors/text/text.behavior.svelte.d.ts +217 -42
- package/dist/behaviors/text/text.behavior.svelte.js +657 -55
- package/dist/behaviors/text/typography.extension.d.ts +7 -0
- package/dist/behaviors/text/typography.extension.js +25 -0
- package/dist/blocks/Block.svelte +5 -2
- package/dist/blocks/BlockList.svelte +48 -0
- package/dist/blocks/BlockList.svelte.d.ts +17 -0
- package/dist/blocks/Nabu.svelte +180 -14
- package/dist/blocks/Nabu.svelte.d.ts +13 -0
- package/dist/blocks/block.extension.d.ts +3 -0
- package/dist/blocks/block.extension.js +8 -9
- package/dist/blocks/block.svelte.d.ts +356 -68
- package/dist/blocks/block.svelte.js +388 -42
- package/dist/blocks/blockquote/Blockquote.svelte +39 -0
- package/dist/blocks/blockquote/Blockquote.svelte.d.ts +11 -0
- package/dist/blocks/blockquote/blockquote.extension.d.ts +4 -0
- package/dist/blocks/blockquote/blockquote.extension.js +80 -0
- package/dist/blocks/blockquote/blockquote.svelte.d.ts +63 -0
- package/dist/blocks/blockquote/blockquote.svelte.js +136 -0
- package/dist/blocks/blockquote/index.d.ts +3 -0
- package/dist/blocks/blockquote/index.js +3 -0
- package/dist/blocks/callout/Callout.svelte +67 -0
- package/dist/blocks/callout/Callout.svelte.d.ts +11 -0
- package/dist/blocks/callout/callout.extension.d.ts +4 -0
- package/dist/blocks/callout/callout.extension.js +84 -0
- package/dist/blocks/callout/callout.svelte.d.ts +80 -0
- package/dist/blocks/callout/callout.svelte.js +189 -0
- package/dist/blocks/callout/index.d.ts +3 -0
- package/dist/blocks/callout/index.js +3 -0
- package/dist/blocks/chunks.d.ts +49 -0
- package/dist/blocks/chunks.js +90 -0
- package/dist/blocks/code/Code.svelte +84 -0
- package/dist/blocks/code/Code.svelte.d.ts +11 -0
- package/dist/blocks/code/code.extension.d.ts +4 -0
- package/dist/blocks/code/code.extension.js +88 -0
- package/dist/blocks/code/code.svelte.d.ts +68 -0
- package/dist/blocks/code/code.svelte.js +245 -0
- package/dist/blocks/code/index.d.ts +3 -0
- package/dist/blocks/code/index.js +3 -0
- package/dist/blocks/container.utils.d.ts +85 -13
- package/dist/blocks/container.utils.js +338 -178
- package/dist/blocks/dialogue/Dialogue.svelte +8 -2
- package/dist/blocks/dialogue/dialogue.svelte.d.ts +36 -23
- package/dist/blocks/dialogue/dialogue.svelte.js +51 -45
- package/dist/blocks/dialogue/hooks/onBeforeInput.hook.d.ts +2 -2
- package/dist/blocks/dialogue/hooks/onBeforeInput.hook.js +6 -4
- package/dist/blocks/dialogue/index.d.ts +12 -1
- package/dist/blocks/dialogue/index.js +51 -35
- package/dist/blocks/divider/Divider.svelte +28 -0
- package/dist/blocks/divider/Divider.svelte.d.ts +11 -0
- package/dist/blocks/divider/divider.extension.d.ts +23 -0
- package/dist/blocks/divider/divider.extension.js +153 -0
- package/dist/blocks/divider/divider.svelte.d.ts +54 -0
- package/dist/blocks/divider/divider.svelte.js +89 -0
- package/dist/blocks/divider/index.d.ts +3 -0
- package/dist/blocks/divider/index.js +3 -0
- package/dist/blocks/file/File.svelte +56 -0
- package/dist/blocks/file/File.svelte.d.ts +11 -0
- package/dist/blocks/file/file.extension.d.ts +4 -0
- package/dist/blocks/file/file.extension.js +27 -0
- package/dist/blocks/file/file.svelte.d.ts +29 -0
- package/dist/blocks/file/file.svelte.js +77 -0
- package/dist/blocks/file/index.d.ts +3 -0
- package/dist/blocks/file/index.js +3 -0
- package/dist/blocks/heading/Heading.svelte +2 -1
- package/dist/blocks/heading/heading.svelte.d.ts +27 -34
- package/dist/blocks/heading/heading.svelte.js +72 -60
- package/dist/blocks/heading/hooks/onBeforeInput.hook.js +7 -5
- package/dist/blocks/heading/index.d.ts +2 -2
- package/dist/blocks/heading/index.js +33 -8
- package/dist/blocks/image/Image.svelte +41 -0
- package/dist/blocks/image/Image.svelte.d.ts +11 -0
- package/dist/blocks/image/image.extension.d.ts +5 -0
- package/dist/blocks/image/image.extension.js +34 -0
- package/dist/blocks/image/image.svelte.d.ts +61 -0
- package/dist/blocks/image/image.svelte.js +114 -0
- package/dist/blocks/image/index.d.ts +3 -0
- package/dist/blocks/image/index.js +3 -0
- package/dist/blocks/index.d.ts +11 -1
- package/dist/blocks/index.js +11 -1
- package/dist/blocks/list/List.svelte +9 -2
- package/dist/blocks/list/ListItem.svelte +44 -7
- package/dist/blocks/list/convert.d.ts +20 -0
- package/dist/blocks/list/convert.js +92 -0
- package/dist/blocks/list/hooks/onBeforeInput.hook.js +60 -26
- package/dist/blocks/list/html-flat.d.ts +78 -0
- package/dist/blocks/list/html-flat.js +244 -0
- package/dist/blocks/list/indent.d.ts +86 -0
- package/dist/blocks/list/indent.js +205 -0
- package/dist/blocks/list/index.d.ts +7 -3
- package/dist/blocks/list/index.js +43 -31
- package/dist/blocks/list/list-item.svelte.d.ts +43 -17
- package/dist/blocks/list/list-item.svelte.js +143 -93
- package/dist/blocks/list/list.behavior.svelte.d.ts +7 -4
- package/dist/blocks/list/list.behavior.svelte.js +10 -5
- package/dist/blocks/list/list.svelte.d.ts +101 -23
- package/dist/blocks/list/list.svelte.js +136 -114
- package/dist/blocks/list/markdown.d.ts +149 -0
- package/dist/blocks/list/markdown.js +442 -0
- package/dist/blocks/list/normalize.d.ts +11 -0
- package/dist/blocks/list/normalize.js +396 -0
- package/dist/blocks/list/paste.d.ts +45 -0
- package/dist/blocks/list/paste.js +242 -0
- package/dist/blocks/list/step-back.d.ts +24 -0
- package/dist/blocks/list/step-back.js +87 -0
- package/dist/blocks/list/to-list.d.ts +7 -0
- package/dist/blocks/list/to-list.js +44 -0
- package/dist/blocks/list/todos-toggle.d.ts +20 -0
- package/dist/blocks/list/todos-toggle.js +48 -0
- package/dist/blocks/list/todos.svelte.d.ts +35 -0
- package/dist/blocks/list/todos.svelte.js +76 -0
- package/dist/blocks/media/index.d.ts +2 -0
- package/dist/blocks/media/index.js +2 -0
- package/dist/blocks/media/markdown.d.ts +12 -0
- package/dist/blocks/media/markdown.js +26 -0
- package/dist/blocks/media/media-block.svelte.d.ts +107 -0
- package/dist/blocks/media/media-block.svelte.js +181 -0
- package/dist/blocks/media/media.helpers.d.ts +24 -0
- package/dist/blocks/media/media.helpers.js +88 -0
- package/dist/blocks/megablock.svelte.d.ts +50 -19
- package/dist/blocks/megablock.svelte.js +115 -74
- package/dist/blocks/move.d.ts +23 -0
- package/dist/blocks/move.js +92 -0
- package/dist/blocks/nabu.svelte.d.ts +846 -75
- package/dist/blocks/nabu.svelte.js +1990 -182
- package/dist/blocks/paragraph/Paragraph.svelte +1 -0
- package/dist/blocks/paragraph/index.d.ts +2 -2
- package/dist/blocks/paragraph/index.js +35 -16
- package/dist/blocks/paragraph/paragraph.svelte.d.ts +49 -35
- package/dist/blocks/paragraph/paragraph.svelte.js +81 -69
- package/dist/blocks/paragraph/table.html.d.ts +9 -0
- package/dist/blocks/paragraph/table.html.js +111 -0
- package/dist/blocks/paragraph/table.markdown.d.ts +32 -0
- package/dist/blocks/paragraph/table.markdown.js +90 -0
- package/dist/blocks/region/Region.svelte +264 -0
- package/dist/blocks/region/Region.svelte.d.ts +11 -0
- package/dist/blocks/region/actions.d.ts +135 -0
- package/dist/blocks/region/actions.js +402 -0
- package/dist/blocks/region/close.d.ts +35 -0
- package/dist/blocks/region/close.js +130 -0
- package/dist/blocks/region/commands.d.ts +6 -0
- package/dist/blocks/region/commands.js +277 -0
- package/dist/blocks/region/create.d.ts +72 -0
- package/dist/blocks/region/create.js +208 -0
- package/dist/blocks/region/end-marker.d.ts +35 -0
- package/dist/blocks/region/end-marker.js +51 -0
- package/dist/blocks/region/facts.d.ts +69 -0
- package/dist/blocks/region/facts.js +152 -0
- package/dist/blocks/region/format.d.ts +91 -0
- package/dist/blocks/region/format.js +260 -0
- package/dist/blocks/region/index.d.ts +14 -0
- package/dist/blocks/region/index.js +14 -0
- package/dist/blocks/region/insert.d.ts +59 -0
- package/dist/blocks/region/insert.js +102 -0
- package/dist/blocks/region/messages.d.ts +254 -0
- package/dist/blocks/region/messages.js +131 -0
- package/dist/blocks/region/normalize.d.ts +7 -0
- package/dist/blocks/region/normalize.js +170 -0
- package/dist/blocks/region/region.extension.d.ts +58 -0
- package/dist/blocks/region/region.extension.js +119 -0
- package/dist/blocks/region/region.svelte.d.ts +221 -0
- package/dist/blocks/region/region.svelte.js +582 -0
- package/dist/blocks/region/regions.svelte.d.ts +141 -0
- package/dist/blocks/region/regions.svelte.js +276 -0
- package/dist/blocks/region/serialize.d.ts +57 -0
- package/dist/blocks/region/serialize.js +112 -0
- package/dist/blocks/region/types.d.ts +255 -0
- package/dist/blocks/region/types.js +204 -0
- package/dist/blocks/selection.svelte.d.ts +106 -8
- package/dist/blocks/selection.svelte.js +632 -74
- package/dist/blocks/subthread.d.ts +80 -0
- package/dist/blocks/subthread.js +167 -0
- package/dist/blocks/text-block.svelte.d.ts +70 -0
- package/dist/blocks/text-block.svelte.js +108 -0
- package/dist/blocks/toggle/Toggle.svelte +73 -0
- package/dist/blocks/toggle/Toggle.svelte.d.ts +11 -0
- package/dist/blocks/toggle/index.d.ts +3 -0
- package/dist/blocks/toggle/index.js +3 -0
- package/dist/blocks/toggle/markdown.d.ts +20 -0
- package/dist/blocks/toggle/markdown.js +112 -0
- package/dist/blocks/toggle/toggle.extension.d.ts +4 -0
- package/dist/blocks/toggle/toggle.extension.js +18 -0
- package/dist/blocks/toggle/toggle.svelte.d.ts +135 -0
- package/dist/blocks/toggle/toggle.svelte.js +352 -0
- package/dist/blocks/undo-selection.d.ts +175 -0
- package/dist/blocks/undo-selection.js +360 -0
- package/dist/blocks/video/Video.svelte +42 -0
- package/dist/blocks/video/Video.svelte.d.ts +11 -0
- package/dist/blocks/video/index.d.ts +3 -0
- package/dist/blocks/video/index.js +3 -0
- package/dist/blocks/video/video.extension.d.ts +4 -0
- package/dist/blocks/video/video.extension.js +27 -0
- package/dist/blocks/video/video.svelte.d.ts +29 -0
- package/dist/blocks/video/video.svelte.js +80 -0
- package/dist/extend.d.ts +55 -0
- package/dist/extend.js +88 -0
- package/dist/files/assets.clipboard.d.ts +77 -0
- package/dist/files/assets.clipboard.js +152 -0
- package/dist/files/files.svelte.d.ts +363 -0
- package/dist/files/files.svelte.js +897 -0
- package/dist/files/index.d.ts +3 -0
- package/dist/files/index.js +3 -0
- package/dist/files/outbox.d.ts +69 -0
- package/dist/files/outbox.js +48 -0
- package/dist/files/status.d.ts +65 -0
- package/dist/files/status.js +87 -0
- package/dist/files/store.d.ts +97 -0
- package/dist/files/store.js +65 -0
- package/dist/files/uploads.svelte.d.ts +174 -0
- package/dist/files/uploads.svelte.js +504 -0
- package/dist/index.d.ts +68 -6
- package/dist/index.js +117 -7
- package/dist/nabu-theme.css +112 -0
- package/dist/paste/html.paste.d.ts +74 -3
- package/dist/paste/html.paste.js +555 -83
- package/dist/paste/markdown-inline.d.ts +86 -0
- package/dist/paste/markdown-inline.js +528 -0
- package/dist/paste/markdown.paste.d.ts +2 -8
- package/dist/paste/markdown.paste.js +78 -94
- package/dist/paste/nabu.paste.d.ts +3 -0
- package/dist/paste/nabu.paste.js +29 -3
- package/dist/paste/plain-text.paste.d.ts +2 -2
- package/dist/paste/plain-text.paste.js +3 -3
- package/dist/persistence/asset-store.d.ts +51 -0
- package/dist/persistence/asset-store.js +103 -0
- package/dist/persistence/autosave.d.ts +53 -0
- package/dist/persistence/autosave.js +62 -0
- package/dist/persistence/compact.d.ts +162 -0
- package/dist/persistence/compact.js +141 -0
- package/dist/persistence/history.d.ts +77 -0
- package/dist/persistence/history.js +176 -0
- package/dist/persistence/indexeddb.d.ts +190 -4
- package/dist/persistence/indexeddb.js +333 -29
- package/dist/persistence/merge.d.ts +38 -0
- package/dist/persistence/merge.js +36 -0
- package/dist/persistence/outbox.d.ts +25 -0
- package/dist/persistence/outbox.js +40 -0
- package/dist/persistence/persistence.d.ts +70 -9
- package/dist/persistence/persistence.js +507 -35
- package/dist/persistence/recovery.d.ts +79 -0
- package/dist/persistence/recovery.js +79 -0
- package/dist/persistence/release.d.ts +34 -0
- package/dist/persistence/release.js +64 -0
- package/dist/persistence/state.svelte.d.ts +53 -0
- package/dist/persistence/state.svelte.js +48 -0
- package/dist/persistence/sync.d.ts +39 -0
- package/dist/persistence/sync.js +138 -0
- package/dist/persistence/tail.d.ts +42 -0
- package/dist/persistence/tail.js +103 -0
- package/dist/presets.d.ts +29 -24
- package/dist/presets.js +51 -14
- package/dist/themes/atelier.css +318 -0
- package/dist/ui/CommandChooser.svelte +153 -0
- package/dist/ui/CommandChooser.svelte.d.ts +13 -0
- package/dist/ui/CommandInvite.svelte +99 -0
- package/dist/ui/CommandInvite.svelte.d.ts +12 -0
- package/dist/ui/DragHandle.svelte +217 -0
- package/dist/ui/DragHandle.svelte.d.ts +11 -0
- package/dist/ui/FloatingToolbar.svelte +165 -0
- package/dist/ui/FloatingToolbar.svelte.d.ts +27 -0
- package/dist/ui/SlashMenu.svelte +215 -0
- package/dist/ui/SlashMenu.svelte.d.ts +33 -0
- package/dist/ui/anchor.d.ts +112 -0
- package/dist/ui/anchor.js +136 -0
- package/dist/ui/index.d.ts +10 -0
- package/dist/ui/index.js +15 -0
- package/dist/ui/slash-menu.svelte.d.ts +112 -0
- package/dist/ui/slash-menu.svelte.js +150 -0
- package/dist/utils/command-confirm.svelte.d.ts +349 -0
- package/dist/utils/command-confirm.svelte.js +673 -0
- package/dist/utils/commands.d.ts +328 -0
- package/dist/utils/commands.js +416 -0
- package/dist/utils/dom-scope.d.ts +14 -0
- package/dist/utils/dom-scope.js +20 -0
- package/dist/utils/drop.d.ts +160 -0
- package/dist/utils/drop.js +305 -0
- package/dist/utils/extensions.d.ts +488 -40
- package/dist/utils/extensions.js +300 -19
- package/dist/utils/html.d.ts +44 -0
- package/dist/utils/html.js +183 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.js +4 -0
- package/dist/utils/keys.d.ts +120 -0
- package/dist/utils/keys.js +248 -0
- package/dist/utils/links.d.ts +22 -0
- package/dist/utils/links.js +34 -0
- package/dist/utils/placeholders.d.ts +88 -0
- package/dist/utils/placeholders.js +91 -0
- package/dist/utils/selection.svelte.d.ts +47 -19
- package/dist/utils/selection.svelte.js +60 -10
- package/dist/utils/spellcheck.d.ts +9 -0
- package/dist/utils/spellcheck.js +17 -0
- package/dist/utils/tab.d.ts +31 -0
- package/dist/utils/tab.js +117 -0
- package/dist/utils/words.d.ts +7 -0
- package/dist/utils/words.js +62 -0
- package/dist/vite.d.ts +22 -0
- package/dist/vite.js +34 -0
- package/package.json +61 -22
- package/dist/blocks/zone/Zone.svelte +0 -258
- package/dist/blocks/zone/Zone.svelte.d.ts +0 -11
- package/dist/blocks/zone/index.d.ts +0 -2
- package/dist/blocks/zone/index.js +0 -2
- package/dist/blocks/zone/zone.extension.d.ts +0 -2
- package/dist/blocks/zone/zone.extension.js +0 -221
- package/dist/blocks/zone/zone.svelte.d.ts +0 -112
- package/dist/blocks/zone/zone.svelte.js +0 -321
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates the declaration and returns the resolved configuration. Throws on an invalid declaration (a
|
|
3
|
+
* configuration error is the integrator's, and is reported when the extension is created).
|
|
4
|
+
* @param {{ types?: Record<string, RegionTypeInit> }} [options]
|
|
5
|
+
* @returns {RegionsConfig}
|
|
6
|
+
*/
|
|
7
|
+
export function resolveConfig(options?: {
|
|
8
|
+
types?: Record<string, RegionTypeInit>;
|
|
9
|
+
}): RegionsConfig;
|
|
10
|
+
/**
|
|
11
|
+
* The spec of a field of a region type: the base `title`, a declared field, or `undefined` (an unknown field,
|
|
12
|
+
* kept as-is).
|
|
13
|
+
* @param {RegionTypeDef | undefined} def @param {string} name
|
|
14
|
+
* @returns {FieldSpec | undefined}
|
|
15
|
+
*/
|
|
16
|
+
export function fieldSpec(def: RegionTypeDef | undefined, name: string): FieldSpec | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Whether `value` is a valid date string: ISO 8601 `YYYY-MM-DD`, optionally with a time (`YYYY-MM-DDTHH:mm[:ss[.sss]][Z|±HH:MM]`).
|
|
19
|
+
* @param {unknown} value
|
|
20
|
+
* @returns {value is string}
|
|
21
|
+
*/
|
|
22
|
+
export function isDateString(value: unknown): value is string;
|
|
23
|
+
/**
|
|
24
|
+
* Validates (and normalises) a value against a field spec.
|
|
25
|
+
* - `null` / `undefined` always validate: they CLEAR the field (`{ ok: true, value: undefined }`);
|
|
26
|
+
* - `text` a string; `number` a finite number; `boolean` a boolean; `enum` one of the declared values;
|
|
27
|
+
* - `date` a valid ISO date string, or a `Date` (stored as its ISO string);
|
|
28
|
+
* - `richtext` a string, or a Loro delta array (`[{ insert, attributes? }]`): normalised to a delta.
|
|
29
|
+
* Never throws and never coerces across kinds (`'3'` is not a number).
|
|
30
|
+
* @param {FieldSpec} spec @param {unknown} value
|
|
31
|
+
* @returns {{ ok: true, value: any } | { ok: false }}
|
|
32
|
+
*/
|
|
33
|
+
export function validateField(spec: FieldSpec, value: unknown): {
|
|
34
|
+
ok: true;
|
|
35
|
+
value: any;
|
|
36
|
+
} | {
|
|
37
|
+
ok: false;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Applies a label template. `{index}` (the number, empty when the type is not numbered), `{path}` ("2.3"), `{title}`,
|
|
41
|
+
* `{type}`, `{level}`; any other `{token}` is left as written. The result is trimmed and its whitespace collapsed,
|
|
42
|
+
* so an empty `{title}` leaves no dangling space ("1.1 " → "1.1").
|
|
43
|
+
* @param {string} template
|
|
44
|
+
* @param {{ index: number | null, path: string, title: string, type: string, level: number }} values
|
|
45
|
+
* @returns {string}
|
|
46
|
+
*/
|
|
47
|
+
export function formatLabel(template: string, values: {
|
|
48
|
+
index: number | null;
|
|
49
|
+
path: string;
|
|
50
|
+
title: string;
|
|
51
|
+
type: string;
|
|
52
|
+
level: number;
|
|
53
|
+
}): string;
|
|
54
|
+
/**
|
|
55
|
+
* Region types: declaration, normalisation and field validation (ADR 016 §3.2). Pure functions, no
|
|
56
|
+
* runes, no engine: everything here is testable on its own.
|
|
57
|
+
*
|
|
58
|
+
* A region TYPE is declared by the integrator:
|
|
59
|
+
*
|
|
60
|
+
* { chapter: { level: 2, label: 'Chapter {index}', numbering: 'document', fields: { pov: 'text' } } }
|
|
61
|
+
*
|
|
62
|
+
* - `level` depth of the division (1 = the highest). The extent of a region is deduced from the levels.
|
|
63
|
+
* - `name` readable name of the type ("Chapter"), shown by the region chooser and the slash menu; defaults to the type id.
|
|
64
|
+
* - `label` display template: `{index}`, `{path}` ("2.3"), `{title}`, `{type}`, `{level}`.
|
|
65
|
+
* - `numbering` `'parent'` (restarts at 1 in each parent region; the default), `'document'` (continues
|
|
66
|
+
* over the whole document, for that type) or `'none'` (no index).
|
|
67
|
+
* - `fields` typed fields: `text`, `richtext` (LoroText), `number`, `boolean`, `enum`, `date`.
|
|
68
|
+
* A field is declared by its kind (`'text'`) or by an object (`{ type: 'enum', values: [...] }`).
|
|
69
|
+
*
|
|
70
|
+
* @typedef {'text' | 'richtext' | 'number' | 'boolean' | 'enum' | 'date'} FieldKind
|
|
71
|
+
*
|
|
72
|
+
* @typedef {Object} FieldSpec
|
|
73
|
+
* @property {string} name
|
|
74
|
+
* @property {FieldKind} kind
|
|
75
|
+
* @property {string[]} [values] the allowed values of an `enum`
|
|
76
|
+
*
|
|
77
|
+
* @typedef {'parent' | 'document' | 'none'} Numbering
|
|
78
|
+
*
|
|
79
|
+
* @typedef {Object} RegionTypeInit
|
|
80
|
+
* @property {number} level
|
|
81
|
+
* @property {string} [name]
|
|
82
|
+
* @property {string} [label]
|
|
83
|
+
* @property {Numbering} [numbering]
|
|
84
|
+
* @property {Record<string, FieldKind | { type: FieldKind, values?: string[] }>} [fields]
|
|
85
|
+
*
|
|
86
|
+
* @typedef {Object} RegionTypeDef
|
|
87
|
+
* @property {string} id
|
|
88
|
+
* @property {number} level
|
|
89
|
+
* @property {string} name readable name; the id when none is declared
|
|
90
|
+
* @property {string} label
|
|
91
|
+
* @property {Numbering} numbering
|
|
92
|
+
* @property {Map<string, FieldSpec>} fields the DECLARED fields (the base `title` is not in it)
|
|
93
|
+
*
|
|
94
|
+
* @typedef {Object} RegionsConfig
|
|
95
|
+
* @property {Map<string, RegionTypeDef>} types in declaration order
|
|
96
|
+
* @property {string} defaultType the type `insertRegion` uses when none is given: the declared type of the lowest level
|
|
97
|
+
* @property {number} maxLevel the deepest declared level (the level given to a marker whose type is not declared)
|
|
98
|
+
*/
|
|
99
|
+
export const FIELD_KINDS: readonly ["text", "richtext", "number", "boolean", "enum", "date"];
|
|
100
|
+
export const NUMBERINGS: readonly ["parent", "document", "none"];
|
|
101
|
+
export namespace TITLE_SPEC {
|
|
102
|
+
let name: string;
|
|
103
|
+
let kind: FieldKind;
|
|
104
|
+
let values: string[] | undefined;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Default types: ONE generic `section` division over three levels, as three types (the level IS the type: changing
|
|
108
|
+
* the level of a region = changing its type, ADR 016 §3.4). Labels "1.", "1.1", "1.1.1".
|
|
109
|
+
* @type {Record<string, RegionTypeInit>}
|
|
110
|
+
*/
|
|
111
|
+
export const DEFAULT_TYPES: Record<string, RegionTypeInit>;
|
|
112
|
+
/**
|
|
113
|
+
* Region types: declaration, normalisation and field validation (ADR 016 §3.2). Pure functions, no
|
|
114
|
+
* runes, no engine: everything here is testable on its own.
|
|
115
|
+
*
|
|
116
|
+
* A region TYPE is declared by the integrator:
|
|
117
|
+
*
|
|
118
|
+
* { chapter: { level: 2, label: 'Chapter {index}', numbering: 'document', fields: { pov: 'text' } } }
|
|
119
|
+
*
|
|
120
|
+
* - `level` depth of the division (1 = the highest). The extent of a region is deduced from the levels.
|
|
121
|
+
* - `name` readable name of the type ("Chapter"), shown by the region chooser and the slash menu; defaults to the type id.
|
|
122
|
+
* - `label` display template: `{index}`, `{path}` ("2.3"), `{title}`, `{type}`, `{level}`.
|
|
123
|
+
* - `numbering` `'parent'` (restarts at 1 in each parent region; the default), `'document'` (continues
|
|
124
|
+
* over the whole document, for that type) or `'none'` (no index).
|
|
125
|
+
* - `fields` typed fields: `text`, `richtext` (LoroText), `number`, `boolean`, `enum`, `date`.
|
|
126
|
+
* A field is declared by its kind (`'text'`) or by an object (`{ type: 'enum', values: [...] }`).
|
|
127
|
+
*/
|
|
128
|
+
export type FieldKind = "text" | "richtext" | "number" | "boolean" | "enum" | "date";
|
|
129
|
+
/**
|
|
130
|
+
* Region types: declaration, normalisation and field validation (ADR 016 §3.2). Pure functions, no
|
|
131
|
+
* runes, no engine: everything here is testable on its own.
|
|
132
|
+
*
|
|
133
|
+
* A region TYPE is declared by the integrator:
|
|
134
|
+
*
|
|
135
|
+
* { chapter: { level: 2, label: 'Chapter {index}', numbering: 'document', fields: { pov: 'text' } } }
|
|
136
|
+
*
|
|
137
|
+
* - `level` depth of the division (1 = the highest). The extent of a region is deduced from the levels.
|
|
138
|
+
* - `name` readable name of the type ("Chapter"), shown by the region chooser and the slash menu; defaults to the type id.
|
|
139
|
+
* - `label` display template: `{index}`, `{path}` ("2.3"), `{title}`, `{type}`, `{level}`.
|
|
140
|
+
* - `numbering` `'parent'` (restarts at 1 in each parent region; the default), `'document'` (continues
|
|
141
|
+
* over the whole document, for that type) or `'none'` (no index).
|
|
142
|
+
* - `fields` typed fields: `text`, `richtext` (LoroText), `number`, `boolean`, `enum`, `date`.
|
|
143
|
+
* A field is declared by its kind (`'text'`) or by an object (`{ type: 'enum', values: [...] }`).
|
|
144
|
+
*/
|
|
145
|
+
export type FieldSpec = {
|
|
146
|
+
name: string;
|
|
147
|
+
kind: FieldKind;
|
|
148
|
+
/**
|
|
149
|
+
* the allowed values of an `enum`
|
|
150
|
+
*/
|
|
151
|
+
values?: string[] | undefined;
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* Region types: declaration, normalisation and field validation (ADR 016 §3.2). Pure functions, no
|
|
155
|
+
* runes, no engine: everything here is testable on its own.
|
|
156
|
+
*
|
|
157
|
+
* A region TYPE is declared by the integrator:
|
|
158
|
+
*
|
|
159
|
+
* { chapter: { level: 2, label: 'Chapter {index}', numbering: 'document', fields: { pov: 'text' } } }
|
|
160
|
+
*
|
|
161
|
+
* - `level` depth of the division (1 = the highest). The extent of a region is deduced from the levels.
|
|
162
|
+
* - `name` readable name of the type ("Chapter"), shown by the region chooser and the slash menu; defaults to the type id.
|
|
163
|
+
* - `label` display template: `{index}`, `{path}` ("2.3"), `{title}`, `{type}`, `{level}`.
|
|
164
|
+
* - `numbering` `'parent'` (restarts at 1 in each parent region; the default), `'document'` (continues
|
|
165
|
+
* over the whole document, for that type) or `'none'` (no index).
|
|
166
|
+
* - `fields` typed fields: `text`, `richtext` (LoroText), `number`, `boolean`, `enum`, `date`.
|
|
167
|
+
* A field is declared by its kind (`'text'`) or by an object (`{ type: 'enum', values: [...] }`).
|
|
168
|
+
*/
|
|
169
|
+
export type Numbering = "parent" | "document" | "none";
|
|
170
|
+
/**
|
|
171
|
+
* Region types: declaration, normalisation and field validation (ADR 016 §3.2). Pure functions, no
|
|
172
|
+
* runes, no engine: everything here is testable on its own.
|
|
173
|
+
*
|
|
174
|
+
* A region TYPE is declared by the integrator:
|
|
175
|
+
*
|
|
176
|
+
* { chapter: { level: 2, label: 'Chapter {index}', numbering: 'document', fields: { pov: 'text' } } }
|
|
177
|
+
*
|
|
178
|
+
* - `level` depth of the division (1 = the highest). The extent of a region is deduced from the levels.
|
|
179
|
+
* - `name` readable name of the type ("Chapter"), shown by the region chooser and the slash menu; defaults to the type id.
|
|
180
|
+
* - `label` display template: `{index}`, `{path}` ("2.3"), `{title}`, `{type}`, `{level}`.
|
|
181
|
+
* - `numbering` `'parent'` (restarts at 1 in each parent region; the default), `'document'` (continues
|
|
182
|
+
* over the whole document, for that type) or `'none'` (no index).
|
|
183
|
+
* - `fields` typed fields: `text`, `richtext` (LoroText), `number`, `boolean`, `enum`, `date`.
|
|
184
|
+
* A field is declared by its kind (`'text'`) or by an object (`{ type: 'enum', values: [...] }`).
|
|
185
|
+
*/
|
|
186
|
+
export type RegionTypeInit = {
|
|
187
|
+
level: number;
|
|
188
|
+
name?: string | undefined;
|
|
189
|
+
label?: string | undefined;
|
|
190
|
+
numbering?: Numbering | undefined;
|
|
191
|
+
fields?: Record<string, FieldKind | {
|
|
192
|
+
type: FieldKind;
|
|
193
|
+
values?: string[];
|
|
194
|
+
}> | undefined;
|
|
195
|
+
};
|
|
196
|
+
/**
|
|
197
|
+
* Region types: declaration, normalisation and field validation (ADR 016 §3.2). Pure functions, no
|
|
198
|
+
* runes, no engine: everything here is testable on its own.
|
|
199
|
+
*
|
|
200
|
+
* A region TYPE is declared by the integrator:
|
|
201
|
+
*
|
|
202
|
+
* { chapter: { level: 2, label: 'Chapter {index}', numbering: 'document', fields: { pov: 'text' } } }
|
|
203
|
+
*
|
|
204
|
+
* - `level` depth of the division (1 = the highest). The extent of a region is deduced from the levels.
|
|
205
|
+
* - `name` readable name of the type ("Chapter"), shown by the region chooser and the slash menu; defaults to the type id.
|
|
206
|
+
* - `label` display template: `{index}`, `{path}` ("2.3"), `{title}`, `{type}`, `{level}`.
|
|
207
|
+
* - `numbering` `'parent'` (restarts at 1 in each parent region; the default), `'document'` (continues
|
|
208
|
+
* over the whole document, for that type) or `'none'` (no index).
|
|
209
|
+
* - `fields` typed fields: `text`, `richtext` (LoroText), `number`, `boolean`, `enum`, `date`.
|
|
210
|
+
* A field is declared by its kind (`'text'`) or by an object (`{ type: 'enum', values: [...] }`).
|
|
211
|
+
*/
|
|
212
|
+
export type RegionTypeDef = {
|
|
213
|
+
id: string;
|
|
214
|
+
level: number;
|
|
215
|
+
/**
|
|
216
|
+
* readable name; the id when none is declared
|
|
217
|
+
*/
|
|
218
|
+
name: string;
|
|
219
|
+
label: string;
|
|
220
|
+
numbering: Numbering;
|
|
221
|
+
/**
|
|
222
|
+
* the DECLARED fields (the base `title` is not in it)
|
|
223
|
+
*/
|
|
224
|
+
fields: Map<string, FieldSpec>;
|
|
225
|
+
};
|
|
226
|
+
/**
|
|
227
|
+
* Region types: declaration, normalisation and field validation (ADR 016 §3.2). Pure functions, no
|
|
228
|
+
* runes, no engine: everything here is testable on its own.
|
|
229
|
+
*
|
|
230
|
+
* A region TYPE is declared by the integrator:
|
|
231
|
+
*
|
|
232
|
+
* { chapter: { level: 2, label: 'Chapter {index}', numbering: 'document', fields: { pov: 'text' } } }
|
|
233
|
+
*
|
|
234
|
+
* - `level` depth of the division (1 = the highest). The extent of a region is deduced from the levels.
|
|
235
|
+
* - `name` readable name of the type ("Chapter"), shown by the region chooser and the slash menu; defaults to the type id.
|
|
236
|
+
* - `label` display template: `{index}`, `{path}` ("2.3"), `{title}`, `{type}`, `{level}`.
|
|
237
|
+
* - `numbering` `'parent'` (restarts at 1 in each parent region; the default), `'document'` (continues
|
|
238
|
+
* over the whole document, for that type) or `'none'` (no index).
|
|
239
|
+
* - `fields` typed fields: `text`, `richtext` (LoroText), `number`, `boolean`, `enum`, `date`.
|
|
240
|
+
* A field is declared by its kind (`'text'`) or by an object (`{ type: 'enum', values: [...] }`).
|
|
241
|
+
*/
|
|
242
|
+
export type RegionsConfig = {
|
|
243
|
+
/**
|
|
244
|
+
* in declaration order
|
|
245
|
+
*/
|
|
246
|
+
types: Map<string, RegionTypeDef>;
|
|
247
|
+
/**
|
|
248
|
+
* the type `insertRegion` uses when none is given: the declared type of the lowest level
|
|
249
|
+
*/
|
|
250
|
+
defaultType: string;
|
|
251
|
+
/**
|
|
252
|
+
* the deepest declared level (the level given to a marker whose type is not declared)
|
|
253
|
+
*/
|
|
254
|
+
maxLevel: number;
|
|
255
|
+
};
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Region types: declaration, normalisation and field validation (ADR 016 §3.2). Pure functions, no
|
|
3
|
+
* runes, no engine: everything here is testable on its own.
|
|
4
|
+
*
|
|
5
|
+
* A region TYPE is declared by the integrator:
|
|
6
|
+
*
|
|
7
|
+
* { chapter: { level: 2, label: 'Chapter {index}', numbering: 'document', fields: { pov: 'text' } } }
|
|
8
|
+
*
|
|
9
|
+
* - `level` depth of the division (1 = the highest). The extent of a region is deduced from the levels.
|
|
10
|
+
* - `name` readable name of the type ("Chapter"), shown by the region chooser and the slash menu; defaults to the type id.
|
|
11
|
+
* - `label` display template: `{index}`, `{path}` ("2.3"), `{title}`, `{type}`, `{level}`.
|
|
12
|
+
* - `numbering` `'parent'` (restarts at 1 in each parent region; the default), `'document'` (continues
|
|
13
|
+
* over the whole document, for that type) or `'none'` (no index).
|
|
14
|
+
* - `fields` typed fields: `text`, `richtext` (LoroText), `number`, `boolean`, `enum`, `date`.
|
|
15
|
+
* A field is declared by its kind (`'text'`) or by an object (`{ type: 'enum', values: [...] }`).
|
|
16
|
+
*
|
|
17
|
+
* @typedef {'text' | 'richtext' | 'number' | 'boolean' | 'enum' | 'date'} FieldKind
|
|
18
|
+
*
|
|
19
|
+
* @typedef {Object} FieldSpec
|
|
20
|
+
* @property {string} name
|
|
21
|
+
* @property {FieldKind} kind
|
|
22
|
+
* @property {string[]} [values] the allowed values of an `enum`
|
|
23
|
+
*
|
|
24
|
+
* @typedef {'parent' | 'document' | 'none'} Numbering
|
|
25
|
+
*
|
|
26
|
+
* @typedef {Object} RegionTypeInit
|
|
27
|
+
* @property {number} level
|
|
28
|
+
* @property {string} [name]
|
|
29
|
+
* @property {string} [label]
|
|
30
|
+
* @property {Numbering} [numbering]
|
|
31
|
+
* @property {Record<string, FieldKind | { type: FieldKind, values?: string[] }>} [fields]
|
|
32
|
+
*
|
|
33
|
+
* @typedef {Object} RegionTypeDef
|
|
34
|
+
* @property {string} id
|
|
35
|
+
* @property {number} level
|
|
36
|
+
* @property {string} name readable name; the id when none is declared
|
|
37
|
+
* @property {string} label
|
|
38
|
+
* @property {Numbering} numbering
|
|
39
|
+
* @property {Map<string, FieldSpec>} fields the DECLARED fields (the base `title` is not in it)
|
|
40
|
+
*
|
|
41
|
+
* @typedef {Object} RegionsConfig
|
|
42
|
+
* @property {Map<string, RegionTypeDef>} types in declaration order
|
|
43
|
+
* @property {string} defaultType the type `insertRegion` uses when none is given: the declared type of the lowest level
|
|
44
|
+
* @property {number} maxLevel the deepest declared level (the level given to a marker whose type is not declared)
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
export const FIELD_KINDS = /** @type {const} */ (['text', 'richtext', 'number', 'boolean', 'enum', 'date']);
|
|
48
|
+
export const NUMBERINGS = /** @type {const} */ (['parent', 'document', 'none']);
|
|
49
|
+
|
|
50
|
+
/** The base field every region has. */
|
|
51
|
+
export const TITLE_SPEC = /** @type {FieldSpec} */ ({ name: 'title', kind: 'text' });
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Default types: ONE generic `section` division over three levels, as three types (the level IS the type: changing
|
|
55
|
+
* the level of a region = changing its type, ADR 016 §3.4). Labels "1.", "1.1", "1.1.1".
|
|
56
|
+
* @type {Record<string, RegionTypeInit>}
|
|
57
|
+
*/
|
|
58
|
+
export const DEFAULT_TYPES = {
|
|
59
|
+
'section-1': { level: 1, name: 'Section', label: '{path}. {title}' },
|
|
60
|
+
'section-2': { level: 2, name: 'Subsection', label: '{path} {title}' },
|
|
61
|
+
'section-3': { level: 3, name: 'Sub-subsection', label: '{path} {title}' },
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @param {string} id @param {string} name @param {unknown} raw
|
|
66
|
+
* @returns {FieldSpec}
|
|
67
|
+
*/
|
|
68
|
+
function normalizeField(id, name, raw) {
|
|
69
|
+
const spec = typeof raw === 'string' ? { type: raw } : /** @type {any} */ (raw);
|
|
70
|
+
const kind = spec?.type;
|
|
71
|
+
if (!FIELD_KINDS.includes(kind)) {
|
|
72
|
+
throw new Error(`Region type "${id}": field "${name}" has an unknown kind ${JSON.stringify(kind)} (expected one of ${FIELD_KINDS.join(', ')}).`);
|
|
73
|
+
}
|
|
74
|
+
/** @type {FieldSpec} */
|
|
75
|
+
const out = { name, kind };
|
|
76
|
+
if (kind === 'enum') {
|
|
77
|
+
if (!Array.isArray(spec.values) || !spec.values.length || spec.values.some((/** @type {unknown} */ v) => typeof v !== 'string')) {
|
|
78
|
+
throw new Error(`Region type "${id}": enum field "${name}" needs a non-empty \`values\` array of strings.`);
|
|
79
|
+
}
|
|
80
|
+
out.values = [...spec.values];
|
|
81
|
+
}
|
|
82
|
+
return out;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Validates the declaration and returns the resolved configuration. Throws on an invalid declaration (a
|
|
87
|
+
* configuration error is the integrator's, and is reported when the extension is created).
|
|
88
|
+
* @param {{ types?: Record<string, RegionTypeInit> }} [options]
|
|
89
|
+
* @returns {RegionsConfig}
|
|
90
|
+
*/
|
|
91
|
+
export function resolveConfig(options = {}) {
|
|
92
|
+
const declared = options.types && Object.keys(options.types).length ? options.types : DEFAULT_TYPES;
|
|
93
|
+
/** @type {Map<string, RegionTypeDef>} */
|
|
94
|
+
const types = new Map();
|
|
95
|
+
for (const [id, init] of Object.entries(declared)) {
|
|
96
|
+
if (!init || !Number.isInteger(init.level) || init.level < 1) {
|
|
97
|
+
throw new Error(`Region type "${id}": \`level\` must be an integer >= 1.`);
|
|
98
|
+
}
|
|
99
|
+
const numbering = init.numbering ?? 'parent';
|
|
100
|
+
if (!NUMBERINGS.includes(numbering)) {
|
|
101
|
+
throw new Error(`Region type "${id}": \`numbering\` must be one of ${NUMBERINGS.join(', ')}.`);
|
|
102
|
+
}
|
|
103
|
+
/** @type {Map<string, FieldSpec>} */
|
|
104
|
+
const fields = new Map();
|
|
105
|
+
for (const [name, raw] of Object.entries(init.fields ?? {})) {
|
|
106
|
+
if (name === TITLE_SPEC.name) continue; // `title` is a base field of every region
|
|
107
|
+
fields.set(name, normalizeField(id, name, raw));
|
|
108
|
+
}
|
|
109
|
+
types.set(id, {
|
|
110
|
+
id,
|
|
111
|
+
level: init.level,
|
|
112
|
+
name: typeof init.name === 'string' && init.name.trim() ? init.name.trim() : id,
|
|
113
|
+
label: typeof init.label === 'string' ? init.label : (numbering === 'none' ? '{title}' : '{path} {title}'),
|
|
114
|
+
numbering,
|
|
115
|
+
fields,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
let defaultType = '';
|
|
119
|
+
let maxLevel = 1;
|
|
120
|
+
for (const def of types.values()) {
|
|
121
|
+
if (!defaultType || def.level < /** @type {RegionTypeDef} */ (types.get(defaultType)).level) defaultType = def.id;
|
|
122
|
+
if (def.level > maxLevel) maxLevel = def.level;
|
|
123
|
+
}
|
|
124
|
+
return { types, defaultType, maxLevel };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The spec of a field of a region type: the base `title`, a declared field, or `undefined` (an unknown field,
|
|
129
|
+
* kept as-is).
|
|
130
|
+
* @param {RegionTypeDef | undefined} def @param {string} name
|
|
131
|
+
* @returns {FieldSpec | undefined}
|
|
132
|
+
*/
|
|
133
|
+
export function fieldSpec(def, name) {
|
|
134
|
+
return name === TITLE_SPEC.name ? TITLE_SPEC : def?.fields.get(name);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const DATE_RE = /^(\d{4})-(\d{2})-(\d{2})(?:[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:?\d{2})?)?$/;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Whether `value` is a valid date string: ISO 8601 `YYYY-MM-DD`, optionally with a time (`YYYY-MM-DDTHH:mm[:ss[.sss]][Z|±HH:MM]`).
|
|
141
|
+
* @param {unknown} value
|
|
142
|
+
* @returns {value is string}
|
|
143
|
+
*/
|
|
144
|
+
export function isDateString(value) {
|
|
145
|
+
if (typeof value !== 'string') return false;
|
|
146
|
+
const m = DATE_RE.exec(value);
|
|
147
|
+
if (!m) return false;
|
|
148
|
+
const [y, mo, d] = [Number(m[1]), Number(m[2]), Number(m[3])];
|
|
149
|
+
const probe = new Date(Date.UTC(y, mo - 1, d));
|
|
150
|
+
if (probe.getUTCFullYear() !== y || probe.getUTCMonth() !== mo - 1 || probe.getUTCDate() !== d) return false; // 2024-02-31
|
|
151
|
+
return !Number.isNaN(Date.parse(value.replace(' ', 'T')));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** @param {unknown} v @returns {v is Array<{ insert: string, attributes?: Record<string, any> }>} */
|
|
155
|
+
const isDelta = (v) => Array.isArray(v) && v.every((op) => op && typeof op === 'object' && typeof (/** @type {any} */ (op)).insert === 'string');
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Validates (and normalises) a value against a field spec.
|
|
159
|
+
* - `null` / `undefined` always validate: they CLEAR the field (`{ ok: true, value: undefined }`);
|
|
160
|
+
* - `text` a string; `number` a finite number; `boolean` a boolean; `enum` one of the declared values;
|
|
161
|
+
* - `date` a valid ISO date string, or a `Date` (stored as its ISO string);
|
|
162
|
+
* - `richtext` a string, or a Loro delta array (`[{ insert, attributes? }]`): normalised to a delta.
|
|
163
|
+
* Never throws and never coerces across kinds (`'3'` is not a number).
|
|
164
|
+
* @param {FieldSpec} spec @param {unknown} value
|
|
165
|
+
* @returns {{ ok: true, value: any } | { ok: false }}
|
|
166
|
+
*/
|
|
167
|
+
export function validateField(spec, value) {
|
|
168
|
+
if (value === undefined || value === null) return { ok: true, value: undefined };
|
|
169
|
+
switch (spec.kind) {
|
|
170
|
+
case 'text': return typeof value === 'string' ? { ok: true, value } : { ok: false };
|
|
171
|
+
case 'number': return typeof value === 'number' && Number.isFinite(value) ? { ok: true, value } : { ok: false };
|
|
172
|
+
case 'boolean': return typeof value === 'boolean' ? { ok: true, value } : { ok: false };
|
|
173
|
+
case 'enum': return typeof value === 'string' && spec.values?.includes(value) ? { ok: true, value } : { ok: false };
|
|
174
|
+
case 'date':
|
|
175
|
+
if (value instanceof Date) return Number.isNaN(value.getTime()) ? { ok: false } : { ok: true, value: value.toISOString() };
|
|
176
|
+
return isDateString(value) ? { ok: true, value } : { ok: false };
|
|
177
|
+
case 'richtext':
|
|
178
|
+
if (typeof value === 'string') return { ok: true, value: value ? [{ insert: value }] : [] };
|
|
179
|
+
return isDelta(value) ? { ok: true, value } : { ok: false };
|
|
180
|
+
default: return { ok: false };
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Applies a label template. `{index}` (the number, empty when the type is not numbered), `{path}` ("2.3"), `{title}`,
|
|
186
|
+
* `{type}`, `{level}`; any other `{token}` is left as written. The result is trimmed and its whitespace collapsed,
|
|
187
|
+
* so an empty `{title}` leaves no dangling space ("1.1 " → "1.1").
|
|
188
|
+
* @param {string} template
|
|
189
|
+
* @param {{ index: number | null, path: string, title: string, type: string, level: number }} values
|
|
190
|
+
* @returns {string}
|
|
191
|
+
*/
|
|
192
|
+
export function formatLabel(template, values) {
|
|
193
|
+
const map = /** @type {Record<string, string>} */ ({
|
|
194
|
+
index: values.index == null ? '' : String(values.index),
|
|
195
|
+
path: values.path,
|
|
196
|
+
title: values.title,
|
|
197
|
+
type: values.type,
|
|
198
|
+
level: String(values.level),
|
|
199
|
+
});
|
|
200
|
+
return template
|
|
201
|
+
.replace(/\{(index|path|title|type|level)\}/g, (_, token) => map[token])
|
|
202
|
+
.replace(/\s+/g, ' ')
|
|
203
|
+
.trim();
|
|
204
|
+
}
|
|
@@ -6,20 +6,102 @@
|
|
|
6
6
|
export class NabuSelection extends SvelteSelection {
|
|
7
7
|
/** @param {Nabu} nabu */
|
|
8
8
|
constructor(nabu: Nabu);
|
|
9
|
+
/** How many times the editor itself placed the selection: lets a deferred fix-up tell that someone else already did. */
|
|
10
|
+
placements: number;
|
|
11
|
+
/**
|
|
12
|
+
* The root element of this engine (set by `Nabu.svelte` through {@link NabuSelection#attachRoot}); null while no editor is
|
|
13
|
+
* mounted. Not part of the stable contract.
|
|
14
|
+
* @type {HTMLElement | null}
|
|
15
|
+
*/
|
|
16
|
+
root: HTMLElement | null;
|
|
17
|
+
/**
|
|
18
|
+
* Declare the element that renders this engine (the `[data-nabu-root]` div). Returns the function that detaches it (only
|
|
19
|
+
* clears the reference when it is still this element: a remount may already have set the next one).
|
|
20
|
+
* @param {HTMLElement} el
|
|
21
|
+
* @returns {() => void}
|
|
22
|
+
*/
|
|
23
|
+
attachRoot(el: HTMLElement): () => void;
|
|
24
|
+
/**
|
|
25
|
+
* How `node` stands relative to this engine's root (see above). A node of a detached tree is 'none'.
|
|
26
|
+
* @param {Node | null | undefined} node
|
|
27
|
+
* @returns {'mine' | 'foreign' | 'none'}
|
|
28
|
+
*/
|
|
29
|
+
kindOf(node: Node | null | undefined): "mine" | "foreign" | "none";
|
|
30
|
+
/** Whether `node` is inside this engine's own root (and not inside a nested editor). @param {Node | null | undefined} node */
|
|
31
|
+
owns(node: Node | null | undefined): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* The nearest ancestor-or-self of `node` matching `selector` that lies inside MY root: a `[data-nabu-ui]` island or an inert
|
|
34
|
+
* block AROUND my root (this editor nested in a block of another one) is not a marker of my content.
|
|
35
|
+
* @param {Node | null | undefined} node @param {string} selector
|
|
36
|
+
* @returns {Element | null}
|
|
37
|
+
*/
|
|
38
|
+
closestOwn(node: Node | null | undefined, selector: string): Element | null;
|
|
39
|
+
/**
|
|
40
|
+
* The scope of the live DOM selection for this engine (see above).
|
|
41
|
+
* @param {Selection | null} [live] defaults to `window.getSelection()`
|
|
42
|
+
* @returns {'mine' | 'outside' | 'foreign'}
|
|
43
|
+
*/
|
|
44
|
+
scope(live?: Selection | null): "mine" | "outside" | "foreign";
|
|
9
45
|
nabu: Nabu;
|
|
10
46
|
/** @type {Set<Block>} */
|
|
11
47
|
previous: Set<Block>;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Move a caret / focus that sits on a NON-TEXT position to the nearest valid text position, in the
|
|
50
|
+
* direction of travel (arrow keys; a heuristic otherwise): inert blocks (dividers, images…), element
|
|
51
|
+
* carets (container padding, between blocks, the editor root) and stray text nodes outside any text
|
|
52
|
+
* block (whitespace around a toggle's arrow…) are all "dead" positions where typing would be swallowed.
|
|
53
|
+
* Hidden content is never a target (it is not rendered). Works for collapsed carets and for ranges
|
|
54
|
+
* (shift+arrow): invalid anchor → collapse to the corrected anchor; only the focus invalid → keep the
|
|
55
|
+
* anchor, correct the focus.
|
|
56
|
+
*
|
|
57
|
+
* Synchronous and idempotent (reads the live DOM selection): call it right after `sync()`.
|
|
58
|
+
* When ArrowDown / ArrowRight leaves the document with no text position after the caret, the nabu
|
|
59
|
+
* "opens" a trailing caret (see `Nabu.openTrailingCaret`).
|
|
60
|
+
* @returns {boolean} True if the selection was moved
|
|
61
|
+
*/
|
|
62
|
+
normalize(): boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Blocks were removed from the document (a programmatic change, a collaborator, an import, an undo): once the dust has
|
|
65
|
+
* settled, a DOM selection left on a DEAD position (the editor root or a container, where the removed block was) is brought
|
|
66
|
+
* onto the nearest text, so that the next click, key or paste starts from a real model position (P-108): the browser fires
|
|
67
|
+
* no `selectionchange` when a click re-selects the very position the selection already has. Deferred to a task of its own, so
|
|
68
|
+
* that whoever removed the block, and the editor's own handlers (which place their caret right after the render), win.
|
|
69
|
+
* Debounced: one check per burst. A selection that is not dead (or not in an editor) is left alone.
|
|
70
|
+
*/
|
|
71
|
+
scheduleRepair(): void;
|
|
72
|
+
/** @returns {boolean} Whether the selection was moved */
|
|
73
|
+
repairAfterRemoval(): boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Selects exactly the text of `block` (Select All inside an isolated field).
|
|
76
|
+
* @param {Block} block
|
|
77
|
+
* @returns {boolean} False when the block has no DOM point to select
|
|
78
|
+
* @public
|
|
79
|
+
*/
|
|
80
|
+
public selectBlockText(block: Block): boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Selects the text of the document between two blocks (first block start -> last block end).
|
|
83
|
+
* @param {Block} first @param {Block} last
|
|
84
|
+
* @returns {boolean}
|
|
85
|
+
* @public
|
|
86
|
+
*/
|
|
87
|
+
public selectBetween(first: Block, last: Block): boolean;
|
|
88
|
+
/** @public */
|
|
89
|
+
public anchorBlock: Block | null | undefined;
|
|
90
|
+
/** @public */
|
|
91
|
+
public focusBlock: Block | null | undefined;
|
|
92
|
+
/** @public */
|
|
93
|
+
public startBlock: Block | null | undefined;
|
|
94
|
+
/** @public */
|
|
95
|
+
public endBlock: Block | null | undefined;
|
|
96
|
+
/** @public */
|
|
97
|
+
public start: {
|
|
17
98
|
from: number;
|
|
18
99
|
to: number;
|
|
19
100
|
direction: "forward" | "backward" | "none";
|
|
20
101
|
block: Block;
|
|
21
102
|
} | null | undefined;
|
|
22
|
-
|
|
103
|
+
/** @public */
|
|
104
|
+
public end: {
|
|
23
105
|
from: number;
|
|
24
106
|
to: number;
|
|
25
107
|
direction: "forward" | "backward" | "none";
|
|
@@ -29,9 +111,25 @@ export class NabuSelection extends SvelteSelection {
|
|
|
29
111
|
* Définit le curseur à un endroit précis du document (Modèle -> DOM)
|
|
30
112
|
* @param {Block} block
|
|
31
113
|
* @param {number} offset
|
|
114
|
+
* @public
|
|
115
|
+
*/
|
|
116
|
+
public setCursor(block: Block, offset: number): void;
|
|
117
|
+
/**
|
|
118
|
+
* The blocks the selection covers (flagged `selected`: highlighted, marked, …). A selection that does not lie entirely in
|
|
119
|
+
* one block leaves out the blocks that isolate their text (`Block#isolatesSelection`): a title is not part of a range.
|
|
120
|
+
* Nor does it hold the HIDDEN content of a block that hides its children (the body of a collapsed toggle, `hidesChildren`):
|
|
121
|
+
* a mark or a highlight never reaches text nobody sees (P-112). Deleting a range still removes such a body with its owner:
|
|
122
|
+
* the deletion walks the tree itself (`container.utils.js`), it does not read this set.
|
|
123
|
+
* @type {Set<Block>}
|
|
124
|
+
* @public
|
|
125
|
+
*/
|
|
126
|
+
public blocks: Set<Block>;
|
|
127
|
+
/**
|
|
128
|
+
* True while the selection is a non-collapsed range across several blocks. The editor mirrors it as `data-nabu-range` on its
|
|
129
|
+
* root, which the stylesheet uses to not highlight isolated blocks (the model leaves them out of the range, see `blocks`).
|
|
130
|
+
* @public
|
|
32
131
|
*/
|
|
33
|
-
|
|
34
|
-
blocks: Set<Block>;
|
|
132
|
+
public isRange: boolean;
|
|
35
133
|
#private;
|
|
36
134
|
}
|
|
37
135
|
import { SvelteSelection } from "../utils/selection.svelte.js";
|