uncial 1.0.0 → 2.0.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/README.md +7 -7
- package/dist/core/attributes.d.ts +14 -2
- package/dist/core/attributes.js +52 -0
- package/dist/core/defineBlock.js +19 -32
- package/dist/core/migrations.d.ts +1 -1
- package/dist/core/migrations.js +10 -3
- package/dist/core/normalize.js +19 -7
- package/dist/core/registry.js +13 -0
- package/dist/core/types.d.ts +23 -1
- package/dist/core/validate.js +5 -1
- package/dist/editor/AttributeFieldControl.svelte +125 -5
- package/dist/editor/AttributeFieldControl.svelte.d.ts +1 -0
- package/dist/editor/BlockAttributesPanel.svelte +42 -15
- package/dist/editor/Editor.svelte +144 -11
- package/dist/editor/Editor.svelte.d.ts +37 -0
- package/dist/editor/attributesController.d.ts +5 -0
- package/dist/editor/attributesController.js +26 -3
- package/dist/editor/bindEditor.d.ts +7 -0
- package/dist/editor/bindEditor.js +39 -1
- package/dist/editor/containerChildOps.js +22 -1
- package/dist/editor/dropdownFloat.d.ts +16 -0
- package/dist/editor/dropdownFloat.js +127 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/render/RichContent.svelte +10 -2
- package/dist/render/RichContent.svelte.d.ts +2 -0
- package/dist/render/RichNode.svelte +64 -8
- package/dist/render/RichNode.svelte.d.ts +2 -0
- package/dist/render/Tab.svelte +27 -0
- package/dist/render/Tab.svelte.d.ts +10 -0
- package/dist/render/Tabs.svelte +57 -0
- package/dist/render/Tabs.svelte.d.ts +10 -0
- package/dist/render/index.d.ts +3 -0
- package/dist/render/index.js +6 -0
- package/dist/render/tabSelection.svelte.d.ts +8 -0
- package/dist/render/tabSelection.svelte.js +40 -0
- package/dist/shared/documentIdentity.d.ts +6 -0
- package/dist/shared/documentIdentity.js +64 -0
- package/dist/shared/syntaxHighlight.d.ts +14 -0
- package/dist/shared/syntaxHighlight.js +36 -3
- package/dist/shared/tiptap.js +84 -6
- package/dist/styles/base.css +177 -0
- package/dist/styles/prose.css +26 -0
- package/dist/styles/tokens.css +53 -69
- package/dist/tabs.d.ts +6 -0
- package/dist/tabs.js +36 -0
- package/dist/web-components/UncialEditor.svelte +13 -12
- package/dist/web-components/UncialEditor.svelte.d.ts +2 -0
- package/dist/web-components/index.d.ts +6 -0
- package/dist/web-components/index.js +12 -0
- package/package.json +134 -133
package/README.md
CHANGED
|
@@ -449,25 +449,25 @@ importing from the root **does** pull the editor graph into your bundle. For SSR
|
|
|
449
449
|
## Development
|
|
450
450
|
|
|
451
451
|
```sh
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
452
|
+
pnpm run check
|
|
453
|
+
pnpm run test:unit -- --run
|
|
454
|
+
pnpm run build
|
|
455
455
|
```
|
|
456
456
|
|
|
457
457
|
Additional suites:
|
|
458
458
|
|
|
459
|
-
- `
|
|
460
|
-
- `
|
|
459
|
+
- `pnpm run test:browser -- --run` for browser-backed Svelte component tests
|
|
460
|
+
- `pnpm run test:e2e` for Playwright end-to-end tests
|
|
461
461
|
|
|
462
462
|
Browser-backed tests require Playwright browsers to be installed:
|
|
463
463
|
|
|
464
464
|
```sh
|
|
465
|
-
|
|
465
|
+
pnpm exec playwright install
|
|
466
466
|
```
|
|
467
467
|
|
|
468
468
|
## Releasing
|
|
469
469
|
|
|
470
|
-
For user-facing library changes, run `
|
|
470
|
+
For user-facing library changes, run `pnpm run changeset` and commit the generated changeset with your PR. After regular PRs merge into `main`, Changesets opens or updates a version PR. Merging that version PR publishes `uncial` to npm with trusted publishing/provenance and creates the GitHub release.
|
|
471
471
|
|
|
472
472
|
## Status
|
|
473
473
|
|
|
@@ -1,5 +1,17 @@
|
|
|
1
|
-
import type { AttributeInputKind, AttributeOption, AttributeSpec, BlockDefinition } from './types.js';
|
|
2
|
-
export type AttributeDefinition = Pick<BlockDefinition, 'attributes'
|
|
1
|
+
import type { AttributeConfig, AttributeInputKind, AttributeListSpec, AttributeOption, AttributeSpec, BlockDefinition } from './types.js';
|
|
2
|
+
export type AttributeDefinition = Pick<BlockDefinition, 'attributes'> & {
|
|
3
|
+
readOnly?: boolean;
|
|
4
|
+
};
|
|
5
|
+
export declare function isAttributeSpecConfig<T>(value: AttributeConfig<T>): value is AttributeSpec<T>;
|
|
6
|
+
export declare function toAttributeSpec<T>(value: AttributeConfig<T>): AttributeSpec<T>;
|
|
7
|
+
/**
|
|
8
|
+
* The fields of one list item, as specs. Empty for a list of single values,
|
|
9
|
+
* which {@link attributeListValueSpec} describes instead.
|
|
10
|
+
*/
|
|
11
|
+
export declare function attributeListFields(list: AttributeListSpec): Array<[string, AttributeSpec<unknown>]>;
|
|
12
|
+
export declare function attributeListValueSpec(list: AttributeListSpec): AttributeSpec<unknown> | null;
|
|
13
|
+
/** A new item carrying each declared default, ready to append to a list. */
|
|
14
|
+
export declare function createAttributeListItem(list: AttributeListSpec): unknown;
|
|
3
15
|
export declare function normalizeAttributeOptions<T>(spec: AttributeSpec<T>): AttributeOption<T>[] | undefined;
|
|
4
16
|
export declare function inferAttributeInputKind(spec: AttributeSpec<unknown>): AttributeInputKind;
|
|
5
17
|
export declare function coerceAttributeValue<T>(spec: AttributeSpec<T>, value: unknown): T;
|
package/dist/core/attributes.js
CHANGED
|
@@ -1,5 +1,48 @@
|
|
|
1
1
|
import { coerceRichTextDocument } from '../shared/richText.js';
|
|
2
2
|
import { isPlainObject, isAttributeOption } from '../shared/guards.js';
|
|
3
|
+
/**
|
|
4
|
+
* The keys that mark an object as an {@link AttributeSpec} *configuration*
|
|
5
|
+
* rather than a shorthand object default. An object carrying any of these is a
|
|
6
|
+
* config; to use an object literal AS the default, wrap it explicitly:
|
|
7
|
+
* `{ default: { ... } }`.
|
|
8
|
+
*/
|
|
9
|
+
const CONFIG_KEYS = [
|
|
10
|
+
'default',
|
|
11
|
+
'required',
|
|
12
|
+
'validate',
|
|
13
|
+
'parse',
|
|
14
|
+
'serialize',
|
|
15
|
+
'input',
|
|
16
|
+
'placeholder',
|
|
17
|
+
'options',
|
|
18
|
+
'list',
|
|
19
|
+
'richText'
|
|
20
|
+
];
|
|
21
|
+
export function isAttributeSpecConfig(value) {
|
|
22
|
+
return isPlainObject(value) && CONFIG_KEYS.some((key) => key in value);
|
|
23
|
+
}
|
|
24
|
+
export function toAttributeSpec(value) {
|
|
25
|
+
return isAttributeSpecConfig(value)
|
|
26
|
+
? { ...value }
|
|
27
|
+
: { default: value };
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The fields of one list item, as specs. Empty for a list of single values,
|
|
31
|
+
* which {@link attributeListValueSpec} describes instead.
|
|
32
|
+
*/
|
|
33
|
+
export function attributeListFields(list) {
|
|
34
|
+
return Object.entries(list.fields ?? {}).map(([name, config]) => [name, toAttributeSpec(config)]);
|
|
35
|
+
}
|
|
36
|
+
export function attributeListValueSpec(list) {
|
|
37
|
+
return list.fields ? null : list.value === undefined ? null : toAttributeSpec(list.value);
|
|
38
|
+
}
|
|
39
|
+
/** A new item carrying each declared default, ready to append to a list. */
|
|
40
|
+
export function createAttributeListItem(list) {
|
|
41
|
+
const valueSpec = attributeListValueSpec(list);
|
|
42
|
+
if (valueSpec)
|
|
43
|
+
return cloneDefault(valueSpec.default);
|
|
44
|
+
return Object.fromEntries(attributeListFields(list).map(([name, spec]) => [name, cloneDefault(spec.default)]));
|
|
45
|
+
}
|
|
3
46
|
function parseJsonValue(raw) {
|
|
4
47
|
try {
|
|
5
48
|
return JSON.parse(raw);
|
|
@@ -40,6 +83,8 @@ export function normalizeAttributeOptions(spec) {
|
|
|
40
83
|
export function inferAttributeInputKind(spec) {
|
|
41
84
|
if (spec.input)
|
|
42
85
|
return spec.input;
|
|
86
|
+
if (spec.list)
|
|
87
|
+
return 'list';
|
|
43
88
|
if (spec.options && spec.options.length > 0)
|
|
44
89
|
return 'select';
|
|
45
90
|
if (typeof spec.default === 'boolean')
|
|
@@ -123,6 +168,8 @@ export function coerceAttributeValue(spec, value) {
|
|
|
123
168
|
return value;
|
|
124
169
|
}
|
|
125
170
|
export function normalizeBlockAttributes(block, attrs = {}) {
|
|
171
|
+
if (block.readOnly)
|
|
172
|
+
return attrs;
|
|
126
173
|
return Object.fromEntries(Object.entries(block.attributes).map(([name, spec]) => [
|
|
127
174
|
name,
|
|
128
175
|
coerceAttributeValue(spec, attrs[name])
|
|
@@ -135,6 +182,8 @@ export function serializeAttributeValue(spec, value) {
|
|
|
135
182
|
return value;
|
|
136
183
|
}
|
|
137
184
|
export function serializeBlockAttributes(block, attrs) {
|
|
185
|
+
if (block.readOnly)
|
|
186
|
+
return JSON.stringify(attrs);
|
|
138
187
|
return JSON.stringify(Object.fromEntries(Object.entries(block.attributes).map(([name, spec]) => [
|
|
139
188
|
name,
|
|
140
189
|
serializeAttributeValue(spec, coerceAttributeValue(spec, attrs[name]))
|
|
@@ -151,6 +200,9 @@ export function toAttributeDraftValue(spec, value) {
|
|
|
151
200
|
return normalized;
|
|
152
201
|
if (input === 'json')
|
|
153
202
|
return JSON.stringify(normalized, null, 2);
|
|
203
|
+
// A list is edited as the array itself, one control per item.
|
|
204
|
+
if (input === 'list')
|
|
205
|
+
return Array.isArray(normalized) ? normalized : [];
|
|
154
206
|
return String(normalized ?? '');
|
|
155
207
|
}
|
|
156
208
|
export function toAttributeDraftValues(block, attrs) {
|
package/dist/core/defineBlock.js
CHANGED
|
@@ -1,36 +1,10 @@
|
|
|
1
|
-
import { isAttributeOption
|
|
2
|
-
|
|
3
|
-
// shorthand object default. An object carrying any of these is treated as a
|
|
4
|
-
// config object; to use an object literal AS the default value, wrap it
|
|
5
|
-
// explicitly: `{ default: { ... } }`.
|
|
6
|
-
const CONFIG_KEYS = [
|
|
7
|
-
'default',
|
|
8
|
-
'required',
|
|
9
|
-
'validate',
|
|
10
|
-
'parse',
|
|
11
|
-
'serialize',
|
|
12
|
-
'input',
|
|
13
|
-
'placeholder',
|
|
14
|
-
'options',
|
|
15
|
-
'richText'
|
|
16
|
-
];
|
|
17
|
-
/**
|
|
18
|
-
* Distinguishes a spec/config object (`{ default, input, ... }`) from a bare
|
|
19
|
-
* object used directly as the default value. Any plain object carrying a known
|
|
20
|
-
* config key is a config; everything else is a shorthand default. This makes a
|
|
21
|
-
* config object that forgot its `default` detectable (see {@link validateConfig})
|
|
22
|
-
* instead of being silently misparsed as an object-valued default.
|
|
23
|
-
*/
|
|
24
|
-
function looksLikeConfig(value) {
|
|
25
|
-
return isPlainObject(value) && CONFIG_KEYS.some((key) => key in value);
|
|
26
|
-
}
|
|
1
|
+
import { isAttributeOption } from '../shared/guards.js';
|
|
2
|
+
import { isAttributeSpecConfig, toAttributeSpec } from './attributes.js';
|
|
27
3
|
function optionValues(options) {
|
|
28
4
|
return options.map((option) => (isAttributeOption(option) ? option.value : option));
|
|
29
5
|
}
|
|
30
6
|
function normalizeAttribute(value) {
|
|
31
|
-
const spec =
|
|
32
|
-
? { ...value }
|
|
33
|
-
: { default: value };
|
|
7
|
+
const spec = toAttributeSpec(value);
|
|
34
8
|
if (spec.options && spec.options.length > 0 && !spec.validate) {
|
|
35
9
|
const allowed = optionValues(spec.options);
|
|
36
10
|
spec.validate = (candidate) => allowed.includes(candidate);
|
|
@@ -57,11 +31,17 @@ function validateConfig(config) {
|
|
|
57
31
|
// object that forgot its default (previously misparsed as an object-valued
|
|
58
32
|
// default, making this check unreachable). Objects with no config keys are
|
|
59
33
|
// valid shorthand object defaults and pass through.
|
|
60
|
-
if (
|
|
34
|
+
if (isAttributeSpecConfig(attr) && !('default' in attr)) {
|
|
61
35
|
throw new Error(`Attribute "${name}" in block "${config.id}" is a configuration object but does not ` +
|
|
62
36
|
`define a "default" value. If you meant an object as the default value, wrap it ` +
|
|
63
37
|
`explicitly as { default: { ... } }.`);
|
|
64
38
|
}
|
|
39
|
+
// A list with no item shape has nothing to render a field from, and would
|
|
40
|
+
// silently fall back to raw JSON — the thing `list` exists to replace.
|
|
41
|
+
if (isAttributeSpecConfig(attr) && attr.list && !attr.list.fields && attr.list.value === undefined) {
|
|
42
|
+
throw new Error(`Attribute "${name}" in block "${config.id}" declares "list" but neither ` +
|
|
43
|
+
`"list.fields" nor "list.value", so the editor has no item shape to render.`);
|
|
44
|
+
}
|
|
65
45
|
}
|
|
66
46
|
if (config.content && config.behaviors?.inline) {
|
|
67
47
|
throw new Error(`Block "${config.id}" cannot be inline when content.kind is "${config.content.kind}"`);
|
|
@@ -69,8 +49,8 @@ function validateConfig(config) {
|
|
|
69
49
|
}
|
|
70
50
|
function normalizeComponents(runtime, config) {
|
|
71
51
|
const shared = config.component;
|
|
72
|
-
const editor = config.components?.editor ?? shared ?? config.components?.render;
|
|
73
52
|
const render = config.components?.render ?? shared ?? config.components?.editor;
|
|
53
|
+
const editor = config.readOnly ? render : config.components?.editor ?? shared ?? render;
|
|
74
54
|
if (!editor || !render) {
|
|
75
55
|
throw new Error(`Block "${config.id}" must provide either "component" or both "components.editor" and "components.render"`);
|
|
76
56
|
}
|
|
@@ -80,8 +60,14 @@ function normalizeContent(content) {
|
|
|
80
60
|
if (!content) {
|
|
81
61
|
return undefined;
|
|
82
62
|
}
|
|
63
|
+
// An empty `allowedBlocks` names no block and cannot be meant as "admits
|
|
64
|
+
// nothing", so it normalizes to unconstrained.
|
|
65
|
+
const allowedBlocks = content.allowedBlocks?.length
|
|
66
|
+
? Object.freeze([...content.allowedBlocks])
|
|
67
|
+
: undefined;
|
|
83
68
|
return {
|
|
84
|
-
kind: content.kind
|
|
69
|
+
kind: content.kind,
|
|
70
|
+
...(allowedBlocks ? { allowedBlocks } : {})
|
|
85
71
|
};
|
|
86
72
|
}
|
|
87
73
|
export function defineRuntimeBlock(runtime, config) {
|
|
@@ -92,6 +78,7 @@ export function defineRuntimeBlock(runtime, config) {
|
|
|
92
78
|
label: config.label,
|
|
93
79
|
description: config.description,
|
|
94
80
|
icon: config.icon,
|
|
81
|
+
readOnly: config.readOnly ?? false,
|
|
95
82
|
attributes: normalizeAttributes(config.attributes),
|
|
96
83
|
components: normalizeComponents(runtime, config),
|
|
97
84
|
behaviors: {
|
|
@@ -4,7 +4,7 @@ import type { PMDoc } from '../shared/document.js';
|
|
|
4
4
|
* Bump this when the persisted document shape changes, and register a
|
|
5
5
|
* migration for each intermediate version so older documents can be upgraded.
|
|
6
6
|
*/
|
|
7
|
-
export declare const CURRENT_DOCUMENT_VERSION =
|
|
7
|
+
export declare const CURRENT_DOCUMENT_VERSION = 2;
|
|
8
8
|
export interface DocumentMigration {
|
|
9
9
|
/** The document version this migration upgrades from (to `from + 1`). */
|
|
10
10
|
from: number;
|
package/dist/core/migrations.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
+
import { stampDocumentIdentity } from '../shared/documentIdentity.js';
|
|
1
2
|
/**
|
|
2
3
|
* The document version stamped onto documents produced by `normalizeDocument`.
|
|
3
4
|
* Bump this when the persisted document shape changes, and register a
|
|
4
5
|
* migration for each intermediate version so older documents can be upgraded.
|
|
5
6
|
*/
|
|
6
|
-
export const CURRENT_DOCUMENT_VERSION =
|
|
7
|
-
const migrations = new Map();
|
|
7
|
+
export const CURRENT_DOCUMENT_VERSION = 2;
|
|
8
|
+
const migrations = new Map([[1, stampDocumentIdentity]]);
|
|
8
9
|
/**
|
|
9
10
|
* Registers a migration that upgrades documents from version `from` to
|
|
10
11
|
* `from + 1`. Returns a disposer that removes the registration (useful for
|
|
@@ -14,10 +15,16 @@ export function registerDocumentMigration(migration) {
|
|
|
14
15
|
if (!Number.isInteger(migration.from)) {
|
|
15
16
|
throw new Error(`Document migration "from" must be an integer, got ${migration.from}`);
|
|
16
17
|
}
|
|
18
|
+
const previous = migrations.get(migration.from);
|
|
17
19
|
migrations.set(migration.from, migration.migrate);
|
|
18
20
|
return () => {
|
|
19
21
|
if (migrations.get(migration.from) === migration.migrate) {
|
|
20
|
-
|
|
22
|
+
if (previous) {
|
|
23
|
+
migrations.set(migration.from, previous);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
migrations.delete(migration.from);
|
|
27
|
+
}
|
|
21
28
|
}
|
|
22
29
|
};
|
|
23
30
|
}
|
package/dist/core/normalize.js
CHANGED
|
@@ -3,6 +3,7 @@ import { normalizeBlockAttributes } from './attributes.js';
|
|
|
3
3
|
import { normalizeMeta } from './meta.js';
|
|
4
4
|
import { CURRENT_DOCUMENT_VERSION, runDocumentMigrations } from './migrations.js';
|
|
5
5
|
import { isPlainObject } from '../shared/guards.js';
|
|
6
|
+
import { stampNodeIdentity } from '../shared/documentIdentity.js';
|
|
6
7
|
export { CURRENT_DOCUMENT_VERSION } from './migrations.js';
|
|
7
8
|
function hasStringType(value) {
|
|
8
9
|
return isPlainObject(value) && typeof value.type === 'string';
|
|
@@ -27,18 +28,18 @@ function normalizeMarks(marks, schema) {
|
|
|
27
28
|
}
|
|
28
29
|
return normalized.length ? normalized : undefined;
|
|
29
30
|
}
|
|
30
|
-
function normalizeContent(content, registryBlocks, schema) {
|
|
31
|
+
function normalizeContent(content, registryBlocks, schema, ids) {
|
|
31
32
|
if (!Array.isArray(content))
|
|
32
33
|
return undefined;
|
|
33
34
|
const normalized = [];
|
|
34
35
|
for (const child of content) {
|
|
35
|
-
const node = normalizeNode(child, registryBlocks, schema);
|
|
36
|
+
const node = normalizeNode(child, registryBlocks, schema, ids);
|
|
36
37
|
if (node)
|
|
37
38
|
normalized.push(node);
|
|
38
39
|
}
|
|
39
40
|
return normalized;
|
|
40
41
|
}
|
|
41
|
-
function normalizeNode(node, registryBlocks, schema) {
|
|
42
|
+
function normalizeNode(node, registryBlocks, schema, ids) {
|
|
42
43
|
if (!hasStringType(node)) {
|
|
43
44
|
return undefined;
|
|
44
45
|
}
|
|
@@ -53,19 +54,30 @@ function normalizeNode(node, registryBlocks, schema) {
|
|
|
53
54
|
}
|
|
54
55
|
const attrs = isPlainObject(candidate.attrs) ? candidate.attrs : undefined;
|
|
55
56
|
const marks = normalizeMarks(candidate.marks, schema);
|
|
56
|
-
const content = normalizeContent(candidate.content, registryBlocks, schema);
|
|
57
|
+
const content = normalizeContent(candidate.content, registryBlocks, schema, ids);
|
|
57
58
|
const block = registryBlocks.get(candidate.type);
|
|
58
59
|
if (block) {
|
|
60
|
+
const identityAttrs = ids && !block.behaviors.inline
|
|
61
|
+
? stampNodeIdentity({ ...candidate, content }, attrs, ids)
|
|
62
|
+
: undefined;
|
|
63
|
+
const normalizedAttrs = normalizeBlockAttributes(block, attrs ?? {});
|
|
59
64
|
return {
|
|
60
65
|
type: candidate.type,
|
|
61
|
-
attrs:
|
|
66
|
+
attrs: identityAttrs
|
|
67
|
+
? {
|
|
68
|
+
...normalizedAttrs,
|
|
69
|
+
id: identityAttrs.id,
|
|
70
|
+
...(typeof identityAttrs.slug === 'string' ? { slug: identityAttrs.slug } : {})
|
|
71
|
+
}
|
|
72
|
+
: normalizedAttrs,
|
|
62
73
|
marks,
|
|
63
74
|
content: block.content ? content : undefined
|
|
64
75
|
};
|
|
65
76
|
}
|
|
77
|
+
const normalizedAttrs = ids ? stampNodeIdentity({ ...candidate, content }, attrs, ids) : attrs;
|
|
66
78
|
return {
|
|
67
79
|
...candidate,
|
|
68
|
-
attrs,
|
|
80
|
+
attrs: normalizedAttrs,
|
|
69
81
|
marks,
|
|
70
82
|
content
|
|
71
83
|
};
|
|
@@ -124,7 +136,7 @@ export function normalizeDocument(document, blocks, schema) {
|
|
|
124
136
|
// Never silently downgrade documents from a newer version; hosts are
|
|
125
137
|
// notified via validateDocument's UNSUPPORTED_VERSION issue.
|
|
126
138
|
version: sourceVersion > CURRENT_DOCUMENT_VERSION ? sourceVersion : CURRENT_DOCUMENT_VERSION,
|
|
127
|
-
content: normalizeContent(working.content, registryBlocks, effectiveSchema) ?? []
|
|
139
|
+
content: normalizeContent(working.content, registryBlocks, effectiveSchema, new Set()) ?? []
|
|
128
140
|
};
|
|
129
141
|
if (metaFields.size > 0) {
|
|
130
142
|
normalized.meta = normalizeMeta(working.meta, metaFields);
|
package/dist/core/registry.js
CHANGED
|
@@ -25,6 +25,19 @@ export function createBlockRegistry(blocks) {
|
|
|
25
25
|
icon: block.icon
|
|
26
26
|
});
|
|
27
27
|
}
|
|
28
|
+
// A container naming a child id nothing registers would silently admit
|
|
29
|
+
// nothing at all, so it fails here rather than at the reader's first click.
|
|
30
|
+
for (const block of blocks) {
|
|
31
|
+
const declared = block.content?.allowedBlocks;
|
|
32
|
+
if (!declared)
|
|
33
|
+
continue;
|
|
34
|
+
const unknown = declared.filter((id) => !byId.has(id));
|
|
35
|
+
if (unknown.length > 0) {
|
|
36
|
+
throw new Error(`Block "${block.id}" admits unregistered child block id(s): ${unknown
|
|
37
|
+
.map((id) => `"${id}"`)
|
|
38
|
+
.join(', ')}. Known ids: ${blocks.map((known) => `"${known.id}"`).join(', ')}.`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
28
41
|
return {
|
|
29
42
|
blocks,
|
|
30
43
|
byId,
|
package/dist/core/types.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { NormalizedBlockComponentDefinition } from './runtime.js';
|
|
|
4
4
|
export type AttrValidator<T> = (value: unknown) => value is T;
|
|
5
5
|
export type AttrParser<T> = (value: unknown) => T | undefined;
|
|
6
6
|
export type AttrSerializer = (value: unknown) => unknown;
|
|
7
|
-
export type AttributeInputKind = 'text' | 'textarea' | 'number' | 'checkbox' | 'json' | 'richtext' | 'select' | 'hidden' | (string & {});
|
|
7
|
+
export type AttributeInputKind = 'text' | 'textarea' | 'number' | 'checkbox' | 'json' | 'list' | 'richtext' | 'select' | 'hidden' | (string & {});
|
|
8
8
|
export interface AttributeOption<T> {
|
|
9
9
|
value: T;
|
|
10
10
|
label?: string;
|
|
@@ -12,6 +12,18 @@ export interface AttributeOption<T> {
|
|
|
12
12
|
}
|
|
13
13
|
export type RichTextFeature = 'bold' | 'italic' | 'strike' | 'code' | 'link' | 'heading' | 'bulletList' | 'orderedList' | 'blockquote' | 'codeBlock' | 'horizontalRule' | 'hardBreak';
|
|
14
14
|
export type RichTextFeatureSelection = '*' | '__all__' | RichTextFeature[];
|
|
15
|
+
/**
|
|
16
|
+
* The shape of one item in a list-valued attribute, which the editor renders as
|
|
17
|
+
* a stack of real fields with add/remove/reorder instead of raw JSON. Declare
|
|
18
|
+
* `fields` for items that are records and `value` for items that are single
|
|
19
|
+
* values; `defineBlock` rejects a list that declares neither.
|
|
20
|
+
*/
|
|
21
|
+
export interface AttributeListSpec {
|
|
22
|
+
fields?: Record<string, AttributeConfig<unknown>>;
|
|
23
|
+
value?: AttributeConfig<unknown>;
|
|
24
|
+
/** Singular noun for one item, used in the add button and item controls. */
|
|
25
|
+
itemLabel?: string;
|
|
26
|
+
}
|
|
15
27
|
export interface AttributeSpec<T> {
|
|
16
28
|
default: T;
|
|
17
29
|
required?: boolean;
|
|
@@ -21,6 +33,8 @@ export interface AttributeSpec<T> {
|
|
|
21
33
|
input?: AttributeInputKind;
|
|
22
34
|
placeholder?: string;
|
|
23
35
|
options?: ReadonlyArray<T | AttributeOption<T>>;
|
|
36
|
+
/** Item shape for an array-valued attribute; implies `input: 'list'`. */
|
|
37
|
+
list?: AttributeListSpec;
|
|
24
38
|
richText?: {
|
|
25
39
|
features?: RichTextFeatureSelection;
|
|
26
40
|
placeholder?: string;
|
|
@@ -42,6 +56,12 @@ export type BlockHtmlRenderer = (attrs: BlockAttributes) => HtmlSpec;
|
|
|
42
56
|
export type BlockContentKind = 'flow';
|
|
43
57
|
export interface BlockContentDefinition {
|
|
44
58
|
kind: BlockContentKind;
|
|
59
|
+
/**
|
|
60
|
+
* Block ids this container admits as children. Omitted means unconstrained:
|
|
61
|
+
* every registered block is admissible. Every id must name a block in the
|
|
62
|
+
* same registry; `createBlockRegistry` throws otherwise.
|
|
63
|
+
*/
|
|
64
|
+
allowedBlocks?: readonly string[];
|
|
45
65
|
}
|
|
46
66
|
export interface BlockComponentProps {
|
|
47
67
|
content?: import('../shared/document.js').PMNode[];
|
|
@@ -64,6 +84,7 @@ export interface BaseBlockConfig<Attrs extends BlockAttributes> {
|
|
|
64
84
|
label: string;
|
|
65
85
|
description?: string;
|
|
66
86
|
icon?: BlockIcon;
|
|
87
|
+
readOnly?: boolean;
|
|
67
88
|
attributes: {
|
|
68
89
|
[K in keyof Attrs]: AttributeConfig<Attrs[K]>;
|
|
69
90
|
};
|
|
@@ -88,6 +109,7 @@ export interface BlockDefinition<Attrs extends BlockAttributes = BlockAttributes
|
|
|
88
109
|
label: string;
|
|
89
110
|
description?: string;
|
|
90
111
|
icon?: BlockIcon;
|
|
112
|
+
readOnly: boolean;
|
|
91
113
|
attributes: NormalizedAttributes<Attrs>;
|
|
92
114
|
components: BlockComponents;
|
|
93
115
|
behaviors: BlockBehavior;
|
package/dist/core/validate.js
CHANGED
|
@@ -13,7 +13,11 @@ const BUILTIN_NODE_TYPES = new Set([
|
|
|
13
13
|
'hardBreak',
|
|
14
14
|
'bulletList',
|
|
15
15
|
'orderedList',
|
|
16
|
-
'listItem'
|
|
16
|
+
'listItem',
|
|
17
|
+
'table',
|
|
18
|
+
'tableRow',
|
|
19
|
+
'tableCell',
|
|
20
|
+
'tableHeader'
|
|
17
21
|
]);
|
|
18
22
|
function validateMarks(marks, path, schema, issues, options) {
|
|
19
23
|
if (marks === undefined || marks === null)
|
|
@@ -6,9 +6,21 @@
|
|
|
6
6
|
</script>
|
|
7
7
|
|
|
8
8
|
<script lang="ts">
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
attributeListFields,
|
|
11
|
+
attributeListValueSpec,
|
|
12
|
+
createAttributeListItem,
|
|
13
|
+
inferAttributeInputKind,
|
|
14
|
+
normalizeAttributeOptions
|
|
15
|
+
} from '../core/attributes.js';
|
|
10
16
|
import type { AttributeSpec } from '../core/types.js';
|
|
17
|
+
import ArrowUpIcon from 'phosphor-svelte/lib/ArrowUpIcon';
|
|
18
|
+
import ArrowDownIcon from 'phosphor-svelte/lib/ArrowDownIcon';
|
|
19
|
+
import PlusIcon from 'phosphor-svelte/lib/PlusIcon';
|
|
20
|
+
import TrashIcon from 'phosphor-svelte/lib/TrashIcon';
|
|
11
21
|
import RichTextAttributeEditor from './RichTextAttributeEditor.svelte';
|
|
22
|
+
// A list item's fields are ordinary attributes, edited by this same control.
|
|
23
|
+
import AttributeFieldControl from './AttributeFieldControl.svelte';
|
|
12
24
|
|
|
13
25
|
interface Props {
|
|
14
26
|
name: string;
|
|
@@ -26,7 +38,9 @@
|
|
|
26
38
|
// The rich-text editor and custom choosers render a component / <button>,
|
|
27
39
|
// neither of which a <label for> can address, so only native form controls
|
|
28
40
|
// get an associated <label>.
|
|
29
|
-
const hasLabelableControl = $derived(
|
|
41
|
+
const hasLabelableControl = $derived(
|
|
42
|
+
isBuiltInInputKind(inputKind) && inputKind !== 'richtext' && inputKind !== 'list'
|
|
43
|
+
);
|
|
30
44
|
const options = $derived(normalizeAttributeOptions(spec) ?? []);
|
|
31
45
|
const stringValue = $derived.by(() => {
|
|
32
46
|
if (typeof value === 'string') return value;
|
|
@@ -43,10 +57,49 @@
|
|
|
43
57
|
return [{ value: stringValue, label: `${stringValue} (current)` }, ...options];
|
|
44
58
|
});
|
|
45
59
|
|
|
60
|
+
const list = $derived(spec.list ?? {});
|
|
61
|
+
const listFields = $derived(attributeListFields(list));
|
|
62
|
+
const listValueSpec = $derived(attributeListValueSpec(list));
|
|
63
|
+
const itemLabel = $derived(list.itemLabel ?? 'item');
|
|
64
|
+
const items = $derived(Array.isArray(value) ? (value as unknown[]) : []);
|
|
65
|
+
|
|
46
66
|
function isBuiltInInputKind(kind: string): boolean {
|
|
47
|
-
return [
|
|
48
|
-
|
|
49
|
-
|
|
67
|
+
return [
|
|
68
|
+
'checkbox',
|
|
69
|
+
'number',
|
|
70
|
+
'richtext',
|
|
71
|
+
'select',
|
|
72
|
+
'textarea',
|
|
73
|
+
'json',
|
|
74
|
+
'list',
|
|
75
|
+
'text',
|
|
76
|
+
'hidden'
|
|
77
|
+
].includes(kind);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function replaceItem(index: number, next: unknown): void {
|
|
81
|
+
onChange(items.map((item, position) => (position === index ? next : item)));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function setItemField(index: number, field: string, next: unknown): void {
|
|
85
|
+
const item = items[index];
|
|
86
|
+
replaceItem(index, { ...(item as Record<string, unknown>), [field]: next });
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function moveItem(index: number, target: number): void {
|
|
90
|
+
if (target < 0 || target >= items.length) return;
|
|
91
|
+
const next = [...items];
|
|
92
|
+
const [moved] = next.splice(index, 1);
|
|
93
|
+
next.splice(target, 0, moved);
|
|
94
|
+
onChange(next);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function removeItem(index: number): void {
|
|
98
|
+
onChange(items.filter((_, position) => position !== index));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function addItem(): void {
|
|
102
|
+
onChange([...items, createAttributeListItem(list)]);
|
|
50
103
|
}
|
|
51
104
|
</script>
|
|
52
105
|
|
|
@@ -109,6 +162,73 @@
|
|
|
109
162
|
<option value={String(option.value)}>{option.label ?? String(option.value)}</option>
|
|
110
163
|
{/each}
|
|
111
164
|
</select>
|
|
165
|
+
{:else if inputKind === 'list'}
|
|
166
|
+
<div class="uncial-list-field">
|
|
167
|
+
{#each items as item, index (index)}
|
|
168
|
+
<div class="uncial-list-item">
|
|
169
|
+
<div class="uncial-list-item__head">
|
|
170
|
+
<span class="uncial-section-label">{itemLabel} {index + 1}</span>
|
|
171
|
+
<div class="uncial-child-item__actions">
|
|
172
|
+
<button
|
|
173
|
+
type="button"
|
|
174
|
+
class="uncial-btn uncial-btn--ghost uncial-btn--xs uncial-btn--square"
|
|
175
|
+
aria-label={`Move ${itemLabel} up`}
|
|
176
|
+
disabled={index === 0}
|
|
177
|
+
onclick={() => moveItem(index, index - 1)}
|
|
178
|
+
>
|
|
179
|
+
<ArrowUpIcon size={12} weight="bold" />
|
|
180
|
+
</button>
|
|
181
|
+
<button
|
|
182
|
+
type="button"
|
|
183
|
+
class="uncial-btn uncial-btn--ghost uncial-btn--xs uncial-btn--square"
|
|
184
|
+
aria-label={`Move ${itemLabel} down`}
|
|
185
|
+
disabled={index === items.length - 1}
|
|
186
|
+
onclick={() => moveItem(index, index + 1)}
|
|
187
|
+
>
|
|
188
|
+
<ArrowDownIcon size={12} weight="bold" />
|
|
189
|
+
</button>
|
|
190
|
+
<button
|
|
191
|
+
type="button"
|
|
192
|
+
class="uncial-btn uncial-btn--ghost uncial-btn--danger uncial-btn--xs uncial-btn--square"
|
|
193
|
+
aria-label={`Remove ${itemLabel}`}
|
|
194
|
+
onclick={() => removeItem(index)}
|
|
195
|
+
>
|
|
196
|
+
<TrashIcon size={12} weight="bold" />
|
|
197
|
+
</button>
|
|
198
|
+
</div>
|
|
199
|
+
</div>
|
|
200
|
+
{#if listValueSpec}
|
|
201
|
+
<AttributeFieldControl
|
|
202
|
+
name={itemLabel}
|
|
203
|
+
spec={listValueSpec}
|
|
204
|
+
value={item}
|
|
205
|
+
onChange={(next) => replaceItem(index, next)}
|
|
206
|
+
{onCustom}
|
|
207
|
+
/>
|
|
208
|
+
{:else}
|
|
209
|
+
{#each listFields as [fieldName, fieldSpec] (fieldName)}
|
|
210
|
+
<AttributeFieldControl
|
|
211
|
+
name={fieldName}
|
|
212
|
+
spec={fieldSpec}
|
|
213
|
+
value={(item as Record<string, unknown>)?.[fieldName]}
|
|
214
|
+
onChange={(next) => setItemField(index, fieldName, next)}
|
|
215
|
+
{onCustom}
|
|
216
|
+
/>
|
|
217
|
+
{/each}
|
|
218
|
+
{/if}
|
|
219
|
+
</div>
|
|
220
|
+
{:else}
|
|
221
|
+
<p class="uncial-help-text">No {itemLabel}s yet.</p>
|
|
222
|
+
{/each}
|
|
223
|
+
<button
|
|
224
|
+
type="button"
|
|
225
|
+
class="uncial-btn uncial-btn--primary uncial-btn--xs uncial-btn--start"
|
|
226
|
+
onclick={addItem}
|
|
227
|
+
>
|
|
228
|
+
<PlusIcon size={12} weight="bold" />
|
|
229
|
+
<span>Add {itemLabel}</span>
|
|
230
|
+
</button>
|
|
231
|
+
</div>
|
|
112
232
|
{:else if inputKind === 'textarea' || inputKind === 'json'}
|
|
113
233
|
<textarea
|
|
114
234
|
id={fieldId}
|