uncial 5.0.0 → 5.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.
@@ -8,4 +8,4 @@ export { normalizeDocument } from './normalize.js';
8
8
  export { validateDocument } from './validate.js';
9
9
  export type { BlockEditorMountHandle, BlockEditorMountOptions, BlockRuntimePlugin, NormalizedBlockComponentDefinition } from './runtime.js';
10
10
  export { coerceRichTextDocument, emptyRichTextDocument, hasRichTextContent, isRichTextDocument, richTextDocument, resolveRichTextFeatures } from '../shared/richText.js';
11
- export type { AttributeConfig, AttributeInputKind, AttributeOption, AttrParser, AttrSerializer, AttributeSpec, BlockAttributes, BlockBehavior, BlockComponentProps, BlockConfig, BlockContentDefinition, BlockContentKind, BlockDefinition, BlockIcon, BlockMetadata, BlockRegistry, ContentDocument, ContentSchema, CreateSchemaOptions, DocumentMetaSchema, MarkComponentProps, MarkDefinition, MarkRegistry, MetaFieldSpec, RichTextFeature, RichTextFeatureSelection, RuntimeBlockConfig, ValidateDocumentOptions, ValidationCode, ValidationIssue, ValidationResult } from './types.js';
11
+ export type { AttributeConfig, AttributeInputKind, AttributeOption, AttrParser, AttrSerializer, AttributeSpec, BlockAttributes, BlockBehavior, BlockComponentProps, BlockConfig, BlockContentDefinition, BlockContentKind, BlockDefinition, BlockIcon, BlockMetadata, BlockRegistry, ContentDocument, ContentSchema, CreateSchemaOptions, DocumentMetaSchema, MarkComponentProps, MarkDefinition, MarkRegistry, MetaFieldSpec, MetaValues, RichTextFeature, RichTextFeatureSelection, RuntimeBlockConfig, ValidateDocumentOptions, ValidationCode, ValidationIssue, ValidationResult } from './types.js';
package/dist/core/meta.js CHANGED
@@ -1,4 +1,4 @@
1
- import { coerceAttributeValue, serializeAttributeValue, toAttributeDraftValue } from './attributes.js';
1
+ import { attributeListFields, attributeListValueSpec, coerceAttributeValue, serializeAttributeValue, toAttributeDraftValue } from './attributes.js';
2
2
  import { pushIssue } from './issues.js';
3
3
  import { isPlainObject as isRecord } from '../shared/guards.js';
4
4
  export function normalizeMeta(meta, metaFields) {
@@ -28,29 +28,51 @@ export function validateMeta(meta, metaFields, issues, options, path = ['meta'])
28
28
  });
29
29
  }
30
30
  }
31
- for (const [name, spec] of metaFields) {
32
- const value = source[name];
33
- const missing = value === undefined || value === null || value === '';
34
- if (missing && spec.required) {
31
+ function validateValue(spec, value, label, valuePath) {
32
+ if (spec.validate && !spec.validate(value)) {
35
33
  pushIssue(issues, options, {
36
34
  code: 'INVALID_META',
37
- path: [...path, name],
38
- message: `Required metadata field "${name}" is missing`,
39
- severity: 'error'
35
+ path: valuePath,
36
+ message: `Metadata field "${label}" is invalid`,
37
+ severity: 'error',
38
+ details: { value }
40
39
  });
41
- continue;
42
40
  }
43
- if (missing)
44
- continue;
45
- if (spec.validate && !spec.validate(value)) {
41
+ if (!spec.list || !Array.isArray(value))
42
+ return;
43
+ const valueSpec = attributeListValueSpec(spec.list);
44
+ const fields = attributeListFields(spec.list);
45
+ value.forEach((item, index) => {
46
+ if (valueSpec) {
47
+ validateValue(valueSpec, item, `${label}.${index}`, [...valuePath, index]);
48
+ return;
49
+ }
50
+ const record = isRecord(item) ? item : {};
51
+ for (const [name, fieldSpec] of fields) {
52
+ validateField(fieldSpec, record[name], `${label}.${index}.${name}`, [
53
+ ...valuePath,
54
+ index,
55
+ name
56
+ ]);
57
+ }
58
+ });
59
+ }
60
+ function validateField(spec, value, label, fieldPath) {
61
+ const missing = value === undefined || value === null || value === '';
62
+ if (missing && spec.required) {
46
63
  pushIssue(issues, options, {
47
64
  code: 'INVALID_META',
48
- path: [...path, name],
49
- message: `Metadata field "${name}" is invalid`,
50
- severity: 'error',
51
- details: { value }
65
+ path: fieldPath,
66
+ message: `Required metadata field "${label}" is missing`,
67
+ severity: 'error'
52
68
  });
69
+ return;
53
70
  }
71
+ if (!missing)
72
+ validateValue(spec, value, label, fieldPath);
73
+ }
74
+ for (const [name, spec] of metaFields) {
75
+ validateField(spec, source[name], name, [...path, name]);
54
76
  }
55
77
  }
56
78
  export function toMetaDraftValues(metaFields, meta = {}) {
@@ -1,4 +1,4 @@
1
- import type { BlockDefinition, BlockRegistry, ContentSchema, CreateSchemaOptions, MarkDefinition, MarkRegistry } from './types.js';
1
+ import type { BlockDefinition, BlockRegistry, ContentSchema, CreateSchemaOptions, DocumentMetaSchema, MarkDefinition, MarkRegistry, MetaValues } from './types.js';
2
2
  /**
3
3
  * The default mark universe a schema allows when the host does not pass an
4
4
  * explicit `allowedMarks`. This is the single source of truth for the default
@@ -13,6 +13,6 @@ export declare function createBlockRegistry(blocks: BlockDefinition[]): BlockReg
13
13
  * through normalization, and permitting one does not give it a wrapper element.
14
14
  */
15
15
  export declare function createMarkRegistry(marks: MarkDefinition[]): MarkRegistry;
16
- export declare function createSchema(registry: BlockRegistry, options?: CreateSchemaOptions): ContentSchema;
16
+ export declare function createSchema<Fields extends DocumentMetaSchema = DocumentMetaSchema>(registry: BlockRegistry, options?: CreateSchemaOptions<Fields>): ContentSchema<MetaValues<Fields>>;
17
17
  export declare function resolveRegistry(blocks: BlockRegistry | BlockDefinition[]): BlockRegistry;
18
18
  export declare function resolveMarkRegistry(marks: MarkRegistry | MarkDefinition[]): MarkRegistry;
@@ -92,6 +92,12 @@ export function createSchema(registry, options = {}) {
92
92
  }
93
93
  const allowedMarks = new Set(options.allowedMarks ?? DEFAULT_MARKS);
94
94
  const metaFields = new Map(Object.entries(options.metaFields ?? {}));
95
+ for (const [name, spec] of metaFields) {
96
+ if (spec.list && !spec.list.fields && spec.list.value === undefined) {
97
+ throw new Error(`Metadata field "${name}" declares "list" but neither ` +
98
+ `"list.fields" nor "list.value", so the editor has no item shape to render.`);
99
+ }
100
+ }
95
101
  return {
96
102
  allowedBlocks,
97
103
  allowedMarks,
@@ -16,7 +16,7 @@ export type RichTextFeatureSelection = '*' | '__all__' | RichTextFeature[];
16
16
  * The shape of one item in a list-valued attribute, which the editor renders as
17
17
  * a stack of real fields with add/remove/reorder instead of raw JSON. Declare
18
18
  * `fields` for items that are records and `value` for items that are single
19
- * values; `defineBlock` rejects a list that declares neither.
19
+ * values; `defineBlock` and `createSchema` reject a list that declares neither.
20
20
  */
21
21
  export interface AttributeListSpec {
22
22
  fields?: Record<string, AttributeConfig<unknown>>;
@@ -47,6 +47,9 @@ export interface AttributeSpec<T> {
47
47
  export type AttributeConfig<T> = AttributeSpec<T> | T;
48
48
  export type MetaFieldSpec<T> = AttributeSpec<T>;
49
49
  export type DocumentMetaSchema = Record<string, MetaFieldSpec<unknown>>;
50
+ export type MetaValues<Fields extends DocumentMetaSchema> = {
51
+ [Name in keyof Fields]: Fields[Name]['default'];
52
+ };
50
53
  export type BlockAttributes = Record<string, unknown>;
51
54
  /**
52
55
  * A block's icon for editor UI (toolbar / insert menu). Either an icon name or
@@ -156,10 +159,13 @@ export interface MarkRegistry {
156
159
  get(id: string): MarkDefinition | undefined;
157
160
  has(id: string): boolean;
158
161
  }
159
- export interface ContentSchema {
162
+ declare const metaValues: unique symbol;
163
+ export interface ContentSchema<Meta extends Record<string, unknown> = Record<string, unknown>> {
160
164
  allowedBlocks: ReadonlySet<string>;
161
165
  allowedMarks: ReadonlySet<string>;
162
166
  metaFields: ReadonlyMap<string, AttributeSpec<unknown>>;
167
+ /** Never set: carries the meta type `createSchema` infers from `metaFields`. */
168
+ readonly [metaValues]?: Meta;
163
169
  }
164
170
  export type ValidationCode = 'UNKNOWN_BLOCK' | 'INVALID_ATTR' | 'INVALID_META' | 'UNKNOWN_META' | 'INVALID_CONTENT' | 'DISALLOWED_BLOCK' | 'DISALLOWED_MARK' | 'MALFORMED_NODE' | 'UNSUPPORTED_VERSION';
165
171
  export interface ValidationIssue {
@@ -173,10 +179,10 @@ export interface ValidationResult {
173
179
  ok: boolean;
174
180
  issues: ValidationIssue[];
175
181
  }
176
- export interface CreateSchemaOptions {
182
+ export interface CreateSchemaOptions<Fields extends DocumentMetaSchema = DocumentMetaSchema> {
177
183
  allowedBlocks?: string[];
178
184
  allowedMarks?: string[];
179
- metaFields?: DocumentMetaSchema;
185
+ metaFields?: Fields;
180
186
  }
181
187
  export interface ValidateDocumentOptions {
182
188
  onIssue?: (issue: ValidationIssue) => void;
@@ -190,3 +196,4 @@ export interface EditorOptions {
190
196
  onIssue?: (issue: ValidationIssue) => void;
191
197
  }
192
198
  export type ContentDocument = PMDoc;
199
+ export {};
@@ -32,6 +32,8 @@
32
32
  spec: AttributeSpec<unknown>;
33
33
  value?: unknown;
34
34
  error?: string;
35
+ path?: string;
36
+ errors?: Record<string, string>;
35
37
  onChange: (value: unknown) => void;
36
38
  onCustom?: (name: string, inputKind: string) => void;
37
39
  imageSource?: ImageSource;
@@ -43,6 +45,8 @@
43
45
  spec,
44
46
  value = undefined,
45
47
  error = '',
48
+ path,
49
+ errors = {},
46
50
  onChange,
47
51
  onCustom,
48
52
  imageSource,
@@ -82,6 +86,7 @@
82
86
  const listValueSpec = $derived(attributeListValueSpec(list));
83
87
  const itemLabel = $derived(list.itemLabel ?? 'item');
84
88
  const items = $derived(Array.isArray(value) ? (value as unknown[]) : []);
89
+ const fieldPath = $derived(path ?? name);
85
90
 
86
91
  let fileInput = $state<HTMLInputElement>();
87
92
  let picker = $state<ImagePicker>();
@@ -244,6 +249,7 @@
244
249
  {:else if inputKind === 'list'}
245
250
  <div class="uncial-list-field">
246
251
  {#each items as item, index (index)}
252
+ {@const itemPath = `${fieldPath}.${index}`}
247
253
  <div class="uncial-list-item">
248
254
  <div class="uncial-list-item__head">
249
255
  <span class="uncial-section-label">{itemLabel} {index + 1}</span>
@@ -255,7 +261,7 @@
255
261
  disabled={index === 0}
256
262
  onclick={() => moveItem(index, index - 1)}
257
263
  >
258
- <ArrowUpIcon size={12} weight="bold" />
264
+ <ArrowUpIcon size={12} weight="bold" aria-hidden="true" />
259
265
  </button>
260
266
  <button
261
267
  type="button"
@@ -264,7 +270,7 @@
264
270
  disabled={index === items.length - 1}
265
271
  onclick={() => moveItem(index, index + 1)}
266
272
  >
267
- <ArrowDownIcon size={12} weight="bold" />
273
+ <ArrowDownIcon size={12} weight="bold" aria-hidden="true" />
268
274
  </button>
269
275
  <button
270
276
  type="button"
@@ -272,7 +278,7 @@
272
278
  aria-label={`Remove ${itemLabel}`}
273
279
  onclick={() => removeItem(index)}
274
280
  >
275
- <TrashIcon size={12} weight="bold" />
281
+ <TrashIcon size={12} weight="bold" aria-hidden="true" />
276
282
  </button>
277
283
  </div>
278
284
  </div>
@@ -281,6 +287,9 @@
281
287
  name={itemLabel}
282
288
  spec={listValueSpec}
283
289
  value={item}
290
+ path={itemPath}
291
+ error={errors[itemPath]}
292
+ {errors}
284
293
  onChange={(next) => replaceItem(index, next)}
285
294
  {onCustom}
286
295
  {imageSource}
@@ -288,10 +297,14 @@
288
297
  />
289
298
  {:else}
290
299
  {#each listFields as [fieldName, fieldSpec] (fieldName)}
300
+ {@const subPath = `${itemPath}.${fieldName}`}
291
301
  <AttributeFieldControl
292
302
  name={fieldName}
293
303
  spec={fieldSpec}
294
304
  value={(item as Record<string, unknown>)?.[fieldName]}
305
+ path={subPath}
306
+ error={errors[subPath]}
307
+ {errors}
295
308
  onChange={(next) => setItemField(index, fieldName, next)}
296
309
  {onCustom}
297
310
  {imageSource}
@@ -308,7 +321,7 @@
308
321
  class="uncial-btn uncial-btn--primary uncial-btn--xs uncial-btn--start"
309
322
  onclick={addItem}
310
323
  >
311
- <PlusIcon size={12} weight="bold" />
324
+ <PlusIcon size={12} weight="bold" aria-hidden="true" />
312
325
  <span>Add {itemLabel}</span>
313
326
  </button>
314
327
  </div>
@@ -7,6 +7,8 @@ interface Props {
7
7
  spec: AttributeSpec<unknown>;
8
8
  value?: unknown;
9
9
  error?: string;
10
+ path?: string;
11
+ errors?: Record<string, string>;
10
12
  onChange: (value: unknown) => void;
11
13
  onCustom?: (name: string, inputKind: string) => void;
12
14
  imageSource?: ImageSource;
@@ -50,6 +50,7 @@
50
50
  {spec}
51
51
  value={controllerState.draft[name]}
52
52
  error={controllerState.errors[name]}
53
+ errors={controllerState.errors}
53
54
  onChange={(value) => controller.setDraft(name, value)}
54
55
  {imageSource}
55
56
  {imagePreviews}
@@ -5,7 +5,7 @@ function errorsFromIssues(issues) {
5
5
  for (const issue of issues) {
6
6
  if (issue.severity !== 'error')
7
7
  continue;
8
- const field = issue.path[0] === 'meta' ? String(issue.path[1] ?? '') : '';
8
+ const field = issue.path[0] === 'meta' ? issue.path.slice(1).join('.') : '';
9
9
  if (field)
10
10
  errors[field] = issue.message;
11
11
  }
@@ -28,7 +28,10 @@ export function createDocumentMetaController(fields = new Map(), meta = {}) {
28
28
  state.update((current) => ({
29
29
  ...current,
30
30
  draft: { ...current.draft, [name]: value },
31
- errors: { ...current.errors, [name]: '' },
31
+ errors: Object.fromEntries(Object.entries({ ...current.errors, [name]: '' }).map(([key, message]) => [
32
+ key,
33
+ key.startsWith(`${name}.`) ? '' : message
34
+ ])),
32
35
  dirty: true
33
36
  }));
34
37
  },
@@ -97,4 +97,4 @@ declare class UncialRendererElement extends UncialElement<RendererHostProps> {
97
97
  set schema(value: RendererProps['schema']);
98
98
  protected mountComponent(target: ShadowRoot): Record<string, unknown>;
99
99
  }
100
- export { UncialEditor, UncialEditorElement, UncialRenderer, UncialRendererElement };
100
+ export { UncialEditor, UncialEditorElement, UncialElement, UncialRenderer, UncialRendererElement };
@@ -194,4 +194,4 @@ if (typeof customElements !== 'undefined') {
194
194
  customElements.define('uncial-renderer', UncialRendererElement);
195
195
  }
196
196
  }
197
- export { UncialEditor, UncialEditorElement, UncialRenderer, UncialRendererElement };
197
+ export { UncialEditor, UncialEditorElement, UncialElement, UncialRenderer, UncialRendererElement };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "uncial",
3
- "version": "5.0.0",
3
+ "version": "5.1.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",