uncial 0.0.7 → 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 +82 -15
- package/dist/core/attributes.d.ts +21 -3
- package/dist/core/attributes.js +86 -15
- package/dist/core/defineBlock.js +26 -12
- package/dist/core/index.d.ts +3 -1
- package/dist/core/index.js +2 -1
- package/dist/core/issues.d.ts +7 -0
- package/dist/core/issues.js +9 -0
- package/dist/core/meta.js +2 -7
- package/dist/core/migrations.d.ts +30 -0
- package/dist/core/migrations.js +50 -0
- package/dist/core/normalize.d.ts +12 -1
- package/dist/core/normalize.js +123 -22
- package/dist/core/registry.d.ts +7 -0
- package/dist/core/registry.js +35 -4
- package/dist/core/runtime.d.ts +7 -0
- package/dist/core/types.d.ts +31 -3
- package/dist/core/validate.js +27 -7
- package/dist/editor/AttributeFieldControl.svelte +155 -7
- package/dist/editor/AttributeFieldControl.svelte.d.ts +1 -0
- package/dist/editor/BlockAttributesPanel.svelte +76 -44
- package/dist/editor/BlockAttributesPanel.svelte.d.ts +9 -1
- package/dist/editor/Editor.svelte +149 -27
- package/dist/editor/Editor.svelte.d.ts +37 -0
- package/dist/editor/RichTextAttributeEditor.svelte +75 -145
- package/dist/editor/RichTextAttributeEditor.svelte.d.ts +2 -1
- package/dist/editor/Toolbar.svelte +2 -3
- package/dist/editor/attributeTargets.d.ts +23 -0
- package/dist/editor/attributeTargets.js +21 -0
- package/dist/editor/attributesController.d.ts +12 -0
- package/dist/editor/attributesController.js +142 -230
- package/dist/editor/bindEditor.d.ts +7 -0
- package/dist/editor/bindEditor.js +141 -27
- package/dist/editor/chooseAttribute.d.ts +26 -0
- package/dist/editor/chooseAttribute.js +10 -0
- package/dist/editor/containerChildOps.d.ts +33 -0
- package/dist/editor/containerChildOps.js +115 -0
- package/dist/editor/dropdownDismiss.d.ts +18 -0
- package/dist/editor/dropdownDismiss.js +46 -0
- package/dist/editor/dropdownFloat.d.ts +16 -0
- package/dist/editor/dropdownFloat.js +127 -0
- package/dist/editor/index.d.ts +3 -1
- package/dist/editor/index.js +2 -1
- package/dist/editor/linkController.d.ts +19 -0
- package/dist/editor/linkController.js +72 -0
- package/dist/editor/toolbarFeatures.d.ts +12 -0
- package/dist/editor/toolbarFeatures.js +36 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/render/Renderer.svelte +1 -1
- package/dist/render/RichContent.svelte +12 -4
- package/dist/render/RichContent.svelte.d.ts +2 -0
- package/dist/render/RichNode.svelte +80 -10
- 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/sanitize.d.ts +6 -0
- package/dist/render/sanitize.js +20 -1
- package/dist/render/tabSelection.svelte.d.ts +8 -0
- package/dist/render/tabSelection.svelte.js +40 -0
- package/dist/runtime/reactiveProps.svelte.d.ts +16 -0
- package/dist/runtime/reactiveProps.svelte.js +16 -0
- package/dist/runtime/svelte.d.ts +2 -1
- package/dist/runtime/svelte.js +26 -6
- package/dist/shared/EditorBlockFixture.svelte +24 -0
- package/dist/shared/EditorBlockFixture.svelte.d.ts +9 -0
- package/dist/shared/content.js +2 -1
- package/dist/shared/documentIdentity.d.ts +6 -0
- package/dist/shared/documentIdentity.js +64 -0
- package/dist/shared/guards.d.ts +13 -0
- package/dist/shared/guards.js +16 -0
- package/dist/shared/richText.js +20 -4
- package/dist/shared/runtimeId.d.ts +8 -0
- package/dist/shared/runtimeId.js +8 -0
- package/dist/shared/syntaxHighlight.d.ts +14 -0
- package/dist/shared/syntaxHighlight.js +36 -3
- package/dist/shared/tiptap.js +140 -27
- 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 +23 -7
- package/dist/web-components/UncialEditor.svelte.d.ts +7 -2
- package/dist/web-components/index.d.ts +45 -10
- package/dist/web-components/index.js +98 -36
- package/dist/web-components/reactiveProps.svelte.d.ts +10 -0
- package/dist/web-components/reactiveProps.svelte.js +13 -0
- package/package.json +134 -133
package/dist/core/normalize.js
CHANGED
|
@@ -1,44 +1,145 @@
|
|
|
1
|
-
import { resolveRegistry } from './registry.js';
|
|
1
|
+
import { createSchema, resolveRegistry } from './registry.js';
|
|
2
2
|
import { normalizeBlockAttributes } from './attributes.js';
|
|
3
3
|
import { normalizeMeta } from './meta.js';
|
|
4
|
-
|
|
4
|
+
import { CURRENT_DOCUMENT_VERSION, runDocumentMigrations } from './migrations.js';
|
|
5
|
+
import { isPlainObject } from '../shared/guards.js';
|
|
6
|
+
import { stampNodeIdentity } from '../shared/documentIdentity.js';
|
|
7
|
+
export { CURRENT_DOCUMENT_VERSION } from './migrations.js';
|
|
8
|
+
function hasStringType(value) {
|
|
9
|
+
return isPlainObject(value) && typeof value.type === 'string';
|
|
10
|
+
}
|
|
5
11
|
function normalizeMarks(marks, schema) {
|
|
6
|
-
if (!marks)
|
|
12
|
+
if (!Array.isArray(marks))
|
|
7
13
|
return undefined;
|
|
8
|
-
const normalized =
|
|
14
|
+
const normalized = [];
|
|
15
|
+
for (const mark of marks) {
|
|
16
|
+
if (!hasStringType(mark))
|
|
17
|
+
continue;
|
|
18
|
+
if (schema && !schema.allowedMarks.has(mark.type))
|
|
19
|
+
continue;
|
|
20
|
+
const candidate = mark;
|
|
21
|
+
if (candidate.attrs !== undefined && !isPlainObject(candidate.attrs)) {
|
|
22
|
+
const { attrs: _attrs, ...rest } = candidate;
|
|
23
|
+
void _attrs;
|
|
24
|
+
normalized.push(rest);
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
normalized.push(candidate);
|
|
28
|
+
}
|
|
9
29
|
return normalized.length ? normalized : undefined;
|
|
10
30
|
}
|
|
11
|
-
function
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
31
|
+
function normalizeContent(content, registryBlocks, schema, ids) {
|
|
32
|
+
if (!Array.isArray(content))
|
|
33
|
+
return undefined;
|
|
34
|
+
const normalized = [];
|
|
35
|
+
for (const child of content) {
|
|
36
|
+
const node = normalizeNode(child, registryBlocks, schema, ids);
|
|
37
|
+
if (node)
|
|
38
|
+
normalized.push(node);
|
|
39
|
+
}
|
|
40
|
+
return normalized;
|
|
41
|
+
}
|
|
42
|
+
function normalizeNode(node, registryBlocks, schema, ids) {
|
|
43
|
+
if (!hasStringType(node)) {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
const candidate = node;
|
|
47
|
+
// ProseMirror rejects text nodes whose `text` is missing or non-string
|
|
48
|
+
// ("Invalid text node in JSON") and empty-string text nodes ("Empty text
|
|
49
|
+
// nodes are not allowed"); either error makes Tiptap silently replace the
|
|
50
|
+
// entire document with empty content, so drop such nodes here.
|
|
51
|
+
if (candidate.type === 'text' &&
|
|
52
|
+
(typeof candidate.text !== 'string' || candidate.text.length === 0)) {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
const attrs = isPlainObject(candidate.attrs) ? candidate.attrs : undefined;
|
|
56
|
+
const marks = normalizeMarks(candidate.marks, schema);
|
|
57
|
+
const content = normalizeContent(candidate.content, registryBlocks, schema, ids);
|
|
58
|
+
const block = registryBlocks.get(candidate.type);
|
|
16
59
|
if (block) {
|
|
60
|
+
const identityAttrs = ids && !block.behaviors.inline
|
|
61
|
+
? stampNodeIdentity({ ...candidate, content }, attrs, ids)
|
|
62
|
+
: undefined;
|
|
63
|
+
const normalizedAttrs = normalizeBlockAttributes(block, attrs ?? {});
|
|
17
64
|
return {
|
|
18
|
-
type:
|
|
19
|
-
attrs:
|
|
20
|
-
|
|
21
|
-
|
|
65
|
+
type: candidate.type,
|
|
66
|
+
attrs: identityAttrs
|
|
67
|
+
? {
|
|
68
|
+
...normalizedAttrs,
|
|
69
|
+
id: identityAttrs.id,
|
|
70
|
+
...(typeof identityAttrs.slug === 'string' ? { slug: identityAttrs.slug } : {})
|
|
71
|
+
}
|
|
72
|
+
: normalizedAttrs,
|
|
73
|
+
marks,
|
|
74
|
+
content: block.content ? content : undefined
|
|
22
75
|
};
|
|
23
76
|
}
|
|
77
|
+
const normalizedAttrs = ids ? stampNodeIdentity({ ...candidate, content }, attrs, ids) : attrs;
|
|
24
78
|
return {
|
|
25
|
-
...
|
|
26
|
-
|
|
27
|
-
|
|
79
|
+
...candidate,
|
|
80
|
+
attrs: normalizedAttrs,
|
|
81
|
+
marks,
|
|
82
|
+
content
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
const NO_REGISTRY_BLOCKS = new Map();
|
|
86
|
+
/**
|
|
87
|
+
* Sanitizes the structural shape of a raw document (object nodes with string
|
|
88
|
+
* types, valid text nodes, array content/marks, object attrs — no schema or
|
|
89
|
+
* block-attribute normalization) and runs the registered migrations on the
|
|
90
|
+
* result, so migration callbacks never receive hostile shapes. A throwing
|
|
91
|
+
* migration never propagates: on throw the sanitized-but-unmigrated document
|
|
92
|
+
* is returned so normalization can proceed.
|
|
93
|
+
*
|
|
94
|
+
* Exported for tests; hosts run migrations via `normalizeDocument`.
|
|
95
|
+
*/
|
|
96
|
+
export function migrateDocument(source, fromVersion, toVersion) {
|
|
97
|
+
const sanitized = {
|
|
98
|
+
type: 'doc',
|
|
99
|
+
content: normalizeContent(source.content, NO_REGISTRY_BLOCKS) ?? []
|
|
28
100
|
};
|
|
101
|
+
if (isPlainObject(source.meta)) {
|
|
102
|
+
sanitized.meta = source.meta;
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
return runDocumentMigrations(sanitized, fromVersion, toVersion);
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
return sanitized;
|
|
109
|
+
}
|
|
29
110
|
}
|
|
30
111
|
export function normalizeDocument(document, blocks, schema) {
|
|
31
112
|
const registry = resolveRegistry(blocks);
|
|
32
|
-
const registryBlocks =
|
|
33
|
-
|
|
34
|
-
|
|
113
|
+
const registryBlocks = registry.byId;
|
|
114
|
+
// When the host passes no schema, fall back to the registry's default
|
|
115
|
+
// schema (default allowed-mark set) so arbitrary mark types are still
|
|
116
|
+
// filtered out — unknown marks crash ProseMirror's nodeFromJSON, which
|
|
117
|
+
// makes Tiptap replace the whole document with empty content.
|
|
118
|
+
const effectiveSchema = schema ?? createSchema(registry);
|
|
119
|
+
const source = isPlainObject(document) ? document : {};
|
|
120
|
+
const rawVersion = source.version;
|
|
121
|
+
// Only treat the stored version as meaningful when it is a positive safe
|
|
122
|
+
// integer; hostile or bogus values (negative, zero, fractional, NaN,
|
|
123
|
+
// Infinity, beyond MAX_SAFE_INTEGER) are handled like a missing version.
|
|
124
|
+
// This also keeps attacker-supplied versions from producing huge
|
|
125
|
+
// migration ranges.
|
|
126
|
+
const sourceVersion = typeof rawVersion === 'number' && Number.isSafeInteger(rawVersion) && rawVersion >= 1
|
|
127
|
+
? rawVersion
|
|
128
|
+
: 1;
|
|
129
|
+
let working = source;
|
|
130
|
+
if (sourceVersion < CURRENT_DOCUMENT_VERSION) {
|
|
131
|
+
working = migrateDocument(source, sourceVersion);
|
|
132
|
+
}
|
|
133
|
+
const metaFields = effectiveSchema.metaFields;
|
|
35
134
|
const normalized = {
|
|
36
135
|
type: 'doc',
|
|
37
|
-
version
|
|
38
|
-
|
|
136
|
+
// Never silently downgrade documents from a newer version; hosts are
|
|
137
|
+
// notified via validateDocument's UNSUPPORTED_VERSION issue.
|
|
138
|
+
version: sourceVersion > CURRENT_DOCUMENT_VERSION ? sourceVersion : CURRENT_DOCUMENT_VERSION,
|
|
139
|
+
content: normalizeContent(working.content, registryBlocks, effectiveSchema, new Set()) ?? []
|
|
39
140
|
};
|
|
40
141
|
if (metaFields.size > 0) {
|
|
41
|
-
normalized.meta = normalizeMeta(
|
|
142
|
+
normalized.meta = normalizeMeta(working.meta, metaFields);
|
|
42
143
|
}
|
|
43
144
|
return normalized;
|
|
44
145
|
}
|
package/dist/core/registry.d.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import type { BlockDefinition, BlockRegistry, ContentSchema, CreateSchemaOptions } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The default mark universe a schema allows when the host does not pass an
|
|
4
|
+
* explicit `allowedMarks`. This is the single source of truth for the default
|
|
5
|
+
* marks — the editor's base extensions (`shared/tiptap.ts`) consume it too, so
|
|
6
|
+
* the fallback schema and the fallback extension set never drift apart.
|
|
7
|
+
*/
|
|
8
|
+
export declare const DEFAULT_MARKS: readonly ["bold", "italic", "strike", "code", "link"];
|
|
2
9
|
export declare function createBlockRegistry(blocks: BlockDefinition[]): BlockRegistry;
|
|
3
10
|
export declare function createSchema(registry: BlockRegistry, options?: CreateSchemaOptions): ContentSchema;
|
|
4
11
|
export declare function resolveRegistry(blocks: BlockRegistry | BlockDefinition[]): BlockRegistry;
|
package/dist/core/registry.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* The default mark universe a schema allows when the host does not pass an
|
|
3
|
+
* explicit `allowedMarks`. This is the single source of truth for the default
|
|
4
|
+
* marks — the editor's base extensions (`shared/tiptap.ts`) consume it too, so
|
|
5
|
+
* the fallback schema and the fallback extension set never drift apart.
|
|
6
|
+
*/
|
|
7
|
+
export const DEFAULT_MARKS = ['bold', 'italic', 'strike', 'code', 'link'];
|
|
2
8
|
export function createBlockRegistry(blocks) {
|
|
3
9
|
const byId = new Map();
|
|
4
10
|
const metadata = [];
|
|
@@ -19,6 +25,19 @@ export function createBlockRegistry(blocks) {
|
|
|
19
25
|
icon: block.icon
|
|
20
26
|
});
|
|
21
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
|
+
}
|
|
22
41
|
return {
|
|
23
42
|
blocks,
|
|
24
43
|
byId,
|
|
@@ -32,9 +51,21 @@ export function createBlockRegistry(blocks) {
|
|
|
32
51
|
};
|
|
33
52
|
}
|
|
34
53
|
export function createSchema(registry, options = {}) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
54
|
+
let allowedBlocks;
|
|
55
|
+
if (options.allowedBlocks) {
|
|
56
|
+
const unknown = options.allowedBlocks.filter((id) => !registry.has(id));
|
|
57
|
+
if (unknown.length > 0) {
|
|
58
|
+
console.warn(`createSchema: ignoring unknown block id(s) in allowedBlocks: ${unknown
|
|
59
|
+
.map((id) => `"${id}"`)
|
|
60
|
+
.join(', ')}. Known ids: ${registry.blocks
|
|
61
|
+
.map((block) => `"${block.id}"`)
|
|
62
|
+
.join(', ')}.`);
|
|
63
|
+
}
|
|
64
|
+
allowedBlocks = new Set(options.allowedBlocks.filter((id) => registry.has(id)));
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
allowedBlocks = new Set(registry.blocks.map((block) => block.id));
|
|
68
|
+
}
|
|
38
69
|
const allowedMarks = new Set(options.allowedMarks ?? DEFAULT_MARKS);
|
|
39
70
|
const metaFields = new Map(Object.entries(options.metaFields ?? {}));
|
|
40
71
|
return {
|
package/dist/core/runtime.d.ts
CHANGED
|
@@ -2,6 +2,13 @@ export interface BlockEditorMountOptions {
|
|
|
2
2
|
target: HTMLElement;
|
|
3
3
|
inline: boolean;
|
|
4
4
|
component: NormalizedBlockComponentDefinition;
|
|
5
|
+
/**
|
|
6
|
+
* The ProseMirror-managed content host for container blocks (null for atomic
|
|
7
|
+
* blocks). Runtimes render this into the editor node view as the block's
|
|
8
|
+
* children slot. It is stable for the node view's lifetime, so it is only
|
|
9
|
+
* supplied at mount — never via `update()`.
|
|
10
|
+
*/
|
|
11
|
+
contentDOM?: HTMLElement | null;
|
|
5
12
|
props: Record<string, unknown>;
|
|
6
13
|
}
|
|
7
14
|
export interface BlockEditorMountHandle {
|
package/dist/core/types.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import type { Component, Snippet } from 'svelte';
|
|
1
2
|
import type { PMDoc, PMPath } from '../shared/document.js';
|
|
2
3
|
import type { NormalizedBlockComponentDefinition } from './runtime.js';
|
|
3
4
|
export type AttrValidator<T> = (value: unknown) => value is T;
|
|
4
5
|
export type AttrParser<T> = (value: unknown) => T | undefined;
|
|
5
6
|
export type AttrSerializer = (value: unknown) => unknown;
|
|
6
|
-
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 & {});
|
|
7
8
|
export interface AttributeOption<T> {
|
|
8
9
|
value: T;
|
|
9
10
|
label?: string;
|
|
@@ -11,6 +12,18 @@ export interface AttributeOption<T> {
|
|
|
11
12
|
}
|
|
12
13
|
export type RichTextFeature = 'bold' | 'italic' | 'strike' | 'code' | 'link' | 'heading' | 'bulletList' | 'orderedList' | 'blockquote' | 'codeBlock' | 'horizontalRule' | 'hardBreak';
|
|
13
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
|
+
}
|
|
14
27
|
export interface AttributeSpec<T> {
|
|
15
28
|
default: T;
|
|
16
29
|
required?: boolean;
|
|
@@ -20,6 +33,8 @@ export interface AttributeSpec<T> {
|
|
|
20
33
|
input?: AttributeInputKind;
|
|
21
34
|
placeholder?: string;
|
|
22
35
|
options?: ReadonlyArray<T | AttributeOption<T>>;
|
|
36
|
+
/** Item shape for an array-valued attribute; implies `input: 'list'`. */
|
|
37
|
+
list?: AttributeListSpec;
|
|
23
38
|
richText?: {
|
|
24
39
|
features?: RichTextFeatureSelection;
|
|
25
40
|
placeholder?: string;
|
|
@@ -29,13 +44,24 @@ export type AttributeConfig<T> = AttributeSpec<T> | T;
|
|
|
29
44
|
export type MetaFieldSpec<T> = AttributeSpec<T>;
|
|
30
45
|
export type DocumentMetaSchema = Record<string, MetaFieldSpec<unknown>>;
|
|
31
46
|
export type BlockAttributes = Record<string, unknown>;
|
|
32
|
-
|
|
47
|
+
/**
|
|
48
|
+
* A block's icon for editor UI (toolbar / insert menu). Either an icon name or
|
|
49
|
+
* inline markup as a string, or a Svelte component/snippet that renders the
|
|
50
|
+
* icon. (`string | unknown` collapsed to `unknown`, disabling type checking.)
|
|
51
|
+
*/
|
|
52
|
+
export type BlockIcon = string | Component | Snippet;
|
|
33
53
|
export type HtmlSpecValue = string | number | boolean | null | undefined;
|
|
34
54
|
export type HtmlSpec = [string, Record<string, unknown>?, ...(HtmlSpec | HtmlSpecValue)[]];
|
|
35
55
|
export type BlockHtmlRenderer = (attrs: BlockAttributes) => HtmlSpec;
|
|
36
56
|
export type BlockContentKind = 'flow';
|
|
37
57
|
export interface BlockContentDefinition {
|
|
38
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[];
|
|
39
65
|
}
|
|
40
66
|
export interface BlockComponentProps {
|
|
41
67
|
content?: import('../shared/document.js').PMNode[];
|
|
@@ -58,6 +84,7 @@ export interface BaseBlockConfig<Attrs extends BlockAttributes> {
|
|
|
58
84
|
label: string;
|
|
59
85
|
description?: string;
|
|
60
86
|
icon?: BlockIcon;
|
|
87
|
+
readOnly?: boolean;
|
|
61
88
|
attributes: {
|
|
62
89
|
[K in keyof Attrs]: AttributeConfig<Attrs[K]>;
|
|
63
90
|
};
|
|
@@ -82,6 +109,7 @@ export interface BlockDefinition<Attrs extends BlockAttributes = BlockAttributes
|
|
|
82
109
|
label: string;
|
|
83
110
|
description?: string;
|
|
84
111
|
icon?: BlockIcon;
|
|
112
|
+
readOnly: boolean;
|
|
85
113
|
attributes: NormalizedAttributes<Attrs>;
|
|
86
114
|
components: BlockComponents;
|
|
87
115
|
behaviors: BlockBehavior;
|
|
@@ -109,7 +137,7 @@ export interface ContentSchema {
|
|
|
109
137
|
allowedMarks: ReadonlySet<string>;
|
|
110
138
|
metaFields: ReadonlyMap<string, AttributeSpec<unknown>>;
|
|
111
139
|
}
|
|
112
|
-
export type ValidationCode = 'UNKNOWN_BLOCK' | 'INVALID_ATTR' | 'INVALID_META' | 'UNKNOWN_META' | 'INVALID_CONTENT' | 'DISALLOWED_BLOCK' | 'DISALLOWED_MARK' | 'MALFORMED_NODE';
|
|
140
|
+
export type ValidationCode = 'UNKNOWN_BLOCK' | 'INVALID_ATTR' | 'INVALID_META' | 'UNKNOWN_META' | 'INVALID_CONTENT' | 'DISALLOWED_BLOCK' | 'DISALLOWED_MARK' | 'MALFORMED_NODE' | 'UNSUPPORTED_VERSION';
|
|
113
141
|
export interface ValidationIssue {
|
|
114
142
|
code: ValidationCode;
|
|
115
143
|
path: PMPath;
|
package/dist/core/validate.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { resolveRegistry } from './registry.js';
|
|
2
2
|
import { validateMeta } from './meta.js';
|
|
3
|
+
import { pushIssue } from './issues.js';
|
|
4
|
+
import { CURRENT_DOCUMENT_VERSION } from './migrations.js';
|
|
3
5
|
const BUILTIN_NODE_TYPES = new Set([
|
|
4
6
|
'doc',
|
|
5
7
|
'paragraph',
|
|
@@ -11,15 +13,24 @@ const BUILTIN_NODE_TYPES = new Set([
|
|
|
11
13
|
'hardBreak',
|
|
12
14
|
'bulletList',
|
|
13
15
|
'orderedList',
|
|
14
|
-
'listItem'
|
|
16
|
+
'listItem',
|
|
17
|
+
'table',
|
|
18
|
+
'tableRow',
|
|
19
|
+
'tableCell',
|
|
20
|
+
'tableHeader'
|
|
15
21
|
]);
|
|
16
|
-
function pushIssue(issues, options, issue) {
|
|
17
|
-
issues.push(issue);
|
|
18
|
-
options?.onIssue?.(issue);
|
|
19
|
-
}
|
|
20
22
|
function validateMarks(marks, path, schema, issues, options) {
|
|
21
|
-
if (
|
|
23
|
+
if (marks === undefined || marks === null)
|
|
24
|
+
return;
|
|
25
|
+
if (!Array.isArray(marks)) {
|
|
26
|
+
pushIssue(issues, options, {
|
|
27
|
+
code: 'MALFORMED_NODE',
|
|
28
|
+
path: [...path, 'marks'],
|
|
29
|
+
message: 'Node marks must be an array when present',
|
|
30
|
+
severity: 'error'
|
|
31
|
+
});
|
|
22
32
|
return;
|
|
33
|
+
}
|
|
23
34
|
marks.forEach((mark, markIndex) => {
|
|
24
35
|
if (!mark?.type || typeof mark.type !== 'string') {
|
|
25
36
|
pushIssue(issues, options, {
|
|
@@ -162,6 +173,15 @@ export function validateDocument(document, blocks, schema, options) {
|
|
|
162
173
|
});
|
|
163
174
|
return { ok: false, issues };
|
|
164
175
|
}
|
|
176
|
+
if (typeof document.version === 'number' && document.version > CURRENT_DOCUMENT_VERSION) {
|
|
177
|
+
pushIssue(issues, options, {
|
|
178
|
+
code: 'UNSUPPORTED_VERSION',
|
|
179
|
+
path: ['version'],
|
|
180
|
+
message: `Document version ${document.version} is newer than the supported version ${CURRENT_DOCUMENT_VERSION}`,
|
|
181
|
+
severity: 'warning',
|
|
182
|
+
details: { version: document.version, supportedVersion: CURRENT_DOCUMENT_VERSION }
|
|
183
|
+
});
|
|
184
|
+
}
|
|
165
185
|
if (document.content && !Array.isArray(document.content)) {
|
|
166
186
|
pushIssue(issues, options, {
|
|
167
187
|
code: 'MALFORMED_NODE',
|
|
@@ -172,7 +192,7 @@ export function validateDocument(document, blocks, schema, options) {
|
|
|
172
192
|
return { ok: false, issues };
|
|
173
193
|
}
|
|
174
194
|
validateMeta(document.meta, schema.metaFields, issues, options);
|
|
175
|
-
const registryBlocks =
|
|
195
|
+
const registryBlocks = registry.byId;
|
|
176
196
|
document.content?.forEach((node, index) => {
|
|
177
197
|
validateNode(node, ['content', index], registryBlocks, schema, issues, options);
|
|
178
198
|
});
|
|
@@ -1,7 +1,26 @@
|
|
|
1
|
+
<script lang="ts" module>
|
|
2
|
+
// Per-instance id so each rendered field can associate its <label> with its
|
|
3
|
+
// control, even when several fields share the same attribute name across
|
|
4
|
+
// blocks. A module counter keeps ids unique and stable without crypto.
|
|
5
|
+
let nextFieldId = 0;
|
|
6
|
+
</script>
|
|
7
|
+
|
|
1
8
|
<script lang="ts">
|
|
2
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
attributeListFields,
|
|
11
|
+
attributeListValueSpec,
|
|
12
|
+
createAttributeListItem,
|
|
13
|
+
inferAttributeInputKind,
|
|
14
|
+
normalizeAttributeOptions
|
|
15
|
+
} from '../core/attributes.js';
|
|
3
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';
|
|
4
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';
|
|
5
24
|
|
|
6
25
|
interface Props {
|
|
7
26
|
name: string;
|
|
@@ -14,7 +33,14 @@
|
|
|
14
33
|
|
|
15
34
|
let { name, spec, value = undefined, error = '', onChange, onCustom }: Props = $props();
|
|
16
35
|
|
|
36
|
+
const fieldId = `uncial-field-${nextFieldId++}`;
|
|
17
37
|
const inputKind = $derived(inferAttributeInputKind(spec));
|
|
38
|
+
// The rich-text editor and custom choosers render a component / <button>,
|
|
39
|
+
// neither of which a <label for> can address, so only native form controls
|
|
40
|
+
// get an associated <label>.
|
|
41
|
+
const hasLabelableControl = $derived(
|
|
42
|
+
isBuiltInInputKind(inputKind) && inputKind !== 'richtext' && inputKind !== 'list'
|
|
43
|
+
);
|
|
18
44
|
const options = $derived(normalizeAttributeOptions(spec) ?? []);
|
|
19
45
|
const stringValue = $derived.by(() => {
|
|
20
46
|
if (typeof value === 'string') return value;
|
|
@@ -22,17 +48,68 @@
|
|
|
22
48
|
return String(value);
|
|
23
49
|
});
|
|
24
50
|
const customLabel = $derived(value ? `Change ${name}: ${value}` : `Choose ${name}`);
|
|
51
|
+
// A stored value outside the declared options (e.g. a legacy value from before the
|
|
52
|
+
// options list changed) must stay visible and re-selectable instead of being silently
|
|
53
|
+
// remapped to the first option on the next commit.
|
|
54
|
+
const selectOptions = $derived.by(() => {
|
|
55
|
+
if (stringValue === '') return options;
|
|
56
|
+
if (options.some((option) => String(option.value) === stringValue)) return options;
|
|
57
|
+
return [{ value: stringValue, label: `${stringValue} (current)` }, ...options];
|
|
58
|
+
});
|
|
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[]) : []);
|
|
25
65
|
|
|
26
66
|
function isBuiltInInputKind(kind: string): boolean {
|
|
27
|
-
return [
|
|
28
|
-
|
|
29
|
-
|
|
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)]);
|
|
30
103
|
}
|
|
31
104
|
</script>
|
|
32
105
|
|
|
33
106
|
{#if inputKind !== 'hidden'}
|
|
34
107
|
<div class="uncial-field">
|
|
35
|
-
|
|
108
|
+
{#if hasLabelableControl}
|
|
109
|
+
<label class="uncial-field__label" for={fieldId}>{name}</label>
|
|
110
|
+
{:else}
|
|
111
|
+
<span class="uncial-field__label">{name}</span>
|
|
112
|
+
{/if}
|
|
36
113
|
{#if !isBuiltInInputKind(inputKind)}
|
|
37
114
|
<button
|
|
38
115
|
type="button"
|
|
@@ -43,6 +120,7 @@
|
|
|
43
120
|
</button>
|
|
44
121
|
{:else if inputKind === 'checkbox'}
|
|
45
122
|
<input
|
|
123
|
+
id={fieldId}
|
|
46
124
|
class="uncial-checkbox uncial-checkbox--sm"
|
|
47
125
|
type="checkbox"
|
|
48
126
|
checked={Boolean(value)}
|
|
@@ -53,6 +131,7 @@
|
|
|
53
131
|
/>
|
|
54
132
|
{:else if inputKind === 'number'}
|
|
55
133
|
<input
|
|
134
|
+
id={fieldId}
|
|
56
135
|
class="uncial-input uncial-input--sm"
|
|
57
136
|
type="number"
|
|
58
137
|
placeholder={spec.placeholder ?? name}
|
|
@@ -71,20 +150,88 @@
|
|
|
71
150
|
/>
|
|
72
151
|
{:else if inputKind === 'select'}
|
|
73
152
|
<select
|
|
153
|
+
id={fieldId}
|
|
74
154
|
class="uncial-select uncial-select--sm"
|
|
75
|
-
aria-label={name}
|
|
76
155
|
value={stringValue}
|
|
77
156
|
onchange={(event) => {
|
|
78
157
|
const target = event.currentTarget as HTMLSelectElement;
|
|
79
158
|
onChange(target.value);
|
|
80
159
|
}}
|
|
81
160
|
>
|
|
82
|
-
{#each
|
|
161
|
+
{#each selectOptions as option (String(option.value))}
|
|
83
162
|
<option value={String(option.value)}>{option.label ?? String(option.value)}</option>
|
|
84
163
|
{/each}
|
|
85
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>
|
|
86
232
|
{:else if inputKind === 'textarea' || inputKind === 'json'}
|
|
87
233
|
<textarea
|
|
234
|
+
id={fieldId}
|
|
88
235
|
class="uncial-textarea uncial-textarea--tall"
|
|
89
236
|
placeholder={spec.placeholder ?? name}
|
|
90
237
|
spellcheck={inputKind !== 'json'}
|
|
@@ -96,6 +243,7 @@
|
|
|
96
243
|
></textarea>
|
|
97
244
|
{:else}
|
|
98
245
|
<input
|
|
246
|
+
id={fieldId}
|
|
99
247
|
class="uncial-input uncial-input--sm"
|
|
100
248
|
type="text"
|
|
101
249
|
placeholder={spec.placeholder ?? name}
|