uncial 2.0.3 → 4.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.
Files changed (51) hide show
  1. package/README.md +6 -1
  2. package/dist/core/defineBlock.js +5 -2
  3. package/dist/core/index.d.ts +2 -2
  4. package/dist/core/index.js +1 -1
  5. package/dist/core/registry.d.ts +8 -1
  6. package/dist/core/registry.js +27 -0
  7. package/dist/core/types.d.ts +21 -1
  8. package/dist/editor/AttributeFieldControl.svelte +127 -3
  9. package/dist/editor/AttributeFieldControl.svelte.d.ts +4 -0
  10. package/dist/editor/BlockAttributesPanel.svelte +12 -3
  11. package/dist/editor/BlockAttributesPanel.svelte.d.ts +5 -0
  12. package/dist/editor/BlockNodeView.svelte +2 -1
  13. package/dist/editor/DocumentMetaPanel.svelte +5 -1
  14. package/dist/editor/Editor.svelte +20 -8
  15. package/dist/editor/Editor.svelte.d.ts +3 -0
  16. package/dist/editor/ImagePicker.svelte +85 -0
  17. package/dist/editor/ImagePicker.svelte.d.ts +13 -0
  18. package/dist/editor/bindEditor.d.ts +3 -0
  19. package/dist/editor/bindEditor.js +1 -1
  20. package/dist/editor/imageSource.d.ts +12 -0
  21. package/dist/editor/imageSource.js +1 -0
  22. package/dist/editor/index.d.ts +1 -0
  23. package/dist/editor/metaController.js +0 -5
  24. package/dist/render/Renderer.svelte +12 -2
  25. package/dist/render/Renderer.svelte.d.ts +2 -1
  26. package/dist/render/RichContent.svelte +4 -2
  27. package/dist/render/RichContent.svelte.d.ts +2 -1
  28. package/dist/render/RichNode.svelte +36 -17
  29. package/dist/render/RichNode.svelte.d.ts +2 -1
  30. package/dist/render/SuperscriptMarkFixture.svelte +7 -0
  31. package/dist/render/SuperscriptMarkFixture.svelte.d.ts +4 -0
  32. package/dist/render/Tabs.svelte +1 -1
  33. package/dist/render/index.d.ts +1 -0
  34. package/dist/render/index.js +1 -0
  35. package/dist/render/resolveImageSrc.d.ts +6 -0
  36. package/dist/render/resolveImageSrc.js +10 -0
  37. package/dist/render/tabSelection.svelte.js +2 -0
  38. package/dist/shared/ImageBlockFixture.svelte +29 -0
  39. package/dist/shared/ImageBlockFixture.svelte.d.ts +11 -0
  40. package/dist/shared/imagePreviews.d.ts +21 -0
  41. package/dist/shared/imagePreviews.js +59 -0
  42. package/dist/shared/tiptap.d.ts +2 -1
  43. package/dist/shared/tiptap.js +16 -13
  44. package/dist/styles/base.css +89 -9
  45. package/dist/ui/ThemeSwitcher.svelte +1 -1
  46. package/dist/ui/theme.svelte.js +1 -1
  47. package/dist/web-components/UncialEditor.svelte +4 -0
  48. package/dist/web-components/UncialEditor.svelte.d.ts +2 -1
  49. package/dist/web-components/index.d.ts +4 -1
  50. package/dist/web-components/index.js +6 -0
  51. package/package.json +3 -3
package/README.md CHANGED
@@ -344,11 +344,16 @@ spec supports:
344
344
  - `validate`: custom validation predicate
345
345
  - `parse`: custom coercion from editor or serialized input
346
346
  - `serialize`: custom serialization for HTML persistence
347
- - `input`: editor control hint — `text`, `textarea`, `number`, `checkbox`, `json`, `select`, `richtext`, or `hidden`. When omitted it is inferred from the default value (and from `options`, which implies `select`).
347
+ - `input`: editor control hint — `text`, `textarea`, `number`, `checkbox`, `json`, `select`, `richtext`, `image`, or `hidden`. When omitted it is inferred from the default value (and from `options`, which implies `select`).
348
348
  - `options`: allowed values for `select` inputs, either plain values or `{ value, label, description }` objects
349
349
  - `richText`: for `richtext` inputs, a `{ features, placeholder }` object controlling the nested rich-text feature allowlist
350
350
  - `placeholder`: optional editor placeholder
351
351
 
352
+ An `image` attribute stores a plain URL string. Its field uploads or chooses
353
+ images through the `imageSource` passed to `Editor` (the `imageSource` property
354
+ of `uncial-editor`); render it with `resolveImageSrc(src, base)` from
355
+ `uncial/render`, which leaves the editor's `blob:` previews alone.
356
+
352
357
  Blocks themselves also accept an optional `description` and `icon` (an icon name,
353
358
  inline markup, or a Svelte component) that surface in the editor's insert menu.
354
359
 
@@ -38,7 +38,10 @@ function validateConfig(config) {
38
38
  }
39
39
  // A list with no item shape has nothing to render a field from, and would
40
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) {
41
+ if (isAttributeSpecConfig(attr) &&
42
+ attr.list &&
43
+ !attr.list.fields &&
44
+ attr.list.value === undefined) {
42
45
  throw new Error(`Attribute "${name}" in block "${config.id}" declares "list" but neither ` +
43
46
  `"list.fields" nor "list.value", so the editor has no item shape to render.`);
44
47
  }
@@ -50,7 +53,7 @@ function validateConfig(config) {
50
53
  function normalizeComponents(runtime, config) {
51
54
  const shared = config.component;
52
55
  const render = config.components?.render ?? shared ?? config.components?.editor;
53
- const editor = config.readOnly ? render : config.components?.editor ?? shared ?? render;
56
+ const editor = config.readOnly ? render : (config.components?.editor ?? shared ?? render);
54
57
  if (!editor || !render) {
55
58
  throw new Error(`Block "${config.id}" must provide either "component" or both "components.editor" and "components.render"`);
56
59
  }
@@ -1,11 +1,11 @@
1
1
  export { defineRuntimeBlock } from './defineBlock.js';
2
2
  export { coerceAttributeValue, inferAttributeInputKind, normalizeAttributeOptions, normalizeBlockAttributes, parseBlockDraftAttributes, serializeBlockAttributes, toAttributeDraftValues } from './attributes.js';
3
3
  export { normalizeMeta, parseMetaDraftValues, serializeMeta, toMetaDraftValues, validateMeta } from './meta.js';
4
- export { createBlockRegistry, createSchema, resolveRegistry } from './registry.js';
4
+ export { createBlockRegistry, createMarkRegistry, createSchema, resolveMarkRegistry, resolveRegistry } from './registry.js';
5
5
  export { CURRENT_DOCUMENT_VERSION, registerDocumentMigration, runDocumentMigrations } from './migrations.js';
6
6
  export type { DocumentMigration } from './migrations.js';
7
7
  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, 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, RichTextFeature, RichTextFeatureSelection, RuntimeBlockConfig, ValidateDocumentOptions, ValidationCode, ValidationIssue, ValidationResult } from './types.js';
@@ -1,7 +1,7 @@
1
1
  export { defineRuntimeBlock } from './defineBlock.js';
2
2
  export { coerceAttributeValue, inferAttributeInputKind, normalizeAttributeOptions, normalizeBlockAttributes, parseBlockDraftAttributes, serializeBlockAttributes, toAttributeDraftValues } from './attributes.js';
3
3
  export { normalizeMeta, parseMetaDraftValues, serializeMeta, toMetaDraftValues, validateMeta } from './meta.js';
4
- export { createBlockRegistry, createSchema, resolveRegistry } from './registry.js';
4
+ export { createBlockRegistry, createMarkRegistry, createSchema, resolveMarkRegistry, resolveRegistry } from './registry.js';
5
5
  export { CURRENT_DOCUMENT_VERSION, registerDocumentMigration, runDocumentMigrations } from './migrations.js';
6
6
  export { normalizeDocument } from './normalize.js';
7
7
  export { validateDocument } from './validate.js';
@@ -1,4 +1,4 @@
1
- import type { BlockDefinition, BlockRegistry, ContentSchema, CreateSchemaOptions } from './types.js';
1
+ import type { BlockDefinition, BlockRegistry, ContentSchema, CreateSchemaOptions, MarkDefinition, MarkRegistry } 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
@@ -7,5 +7,12 @@ import type { BlockDefinition, BlockRegistry, ContentSchema, CreateSchemaOptions
7
7
  */
8
8
  export declare const DEFAULT_MARKS: readonly ["bold", "italic", "strike", "code", "link"];
9
9
  export declare function createBlockRegistry(blocks: BlockDefinition[]): BlockRegistry;
10
+ /**
11
+ * Registered renderers are what the renderer can *draw*; `allowedMarks` is what
12
+ * a schema *permits*. They stay separate: registering a mark does not admit it
13
+ * through normalization, and permitting one does not give it a wrapper element.
14
+ */
15
+ export declare function createMarkRegistry(marks: MarkDefinition[]): MarkRegistry;
10
16
  export declare function createSchema(registry: BlockRegistry, options?: CreateSchemaOptions): ContentSchema;
11
17
  export declare function resolveRegistry(blocks: BlockRegistry | BlockDefinition[]): BlockRegistry;
18
+ export declare function resolveMarkRegistry(marks: MarkRegistry | MarkDefinition[]): MarkRegistry;
@@ -50,6 +50,30 @@ export function createBlockRegistry(blocks) {
50
50
  }
51
51
  };
52
52
  }
53
+ /**
54
+ * Registered renderers are what the renderer can *draw*; `allowedMarks` is what
55
+ * a schema *permits*. They stay separate: registering a mark does not admit it
56
+ * through normalization, and permitting one does not give it a wrapper element.
57
+ */
58
+ export function createMarkRegistry(marks) {
59
+ const byId = new Map();
60
+ for (const mark of marks) {
61
+ if (byId.has(mark.id)) {
62
+ throw new Error(`Duplicate mark id "${mark.id}"`);
63
+ }
64
+ byId.set(mark.id, mark);
65
+ }
66
+ return {
67
+ marks,
68
+ byId,
69
+ get(id) {
70
+ return byId.get(id);
71
+ },
72
+ has(id) {
73
+ return byId.has(id);
74
+ }
75
+ };
76
+ }
53
77
  export function createSchema(registry, options = {}) {
54
78
  let allowedBlocks;
55
79
  if (options.allowedBlocks) {
@@ -77,3 +101,6 @@ export function createSchema(registry, options = {}) {
77
101
  export function resolveRegistry(blocks) {
78
102
  return Array.isArray(blocks) ? createBlockRegistry(blocks) : blocks;
79
103
  }
104
+ export function resolveMarkRegistry(marks) {
105
+ return Array.isArray(marks) ? createMarkRegistry(marks) : marks;
106
+ }
@@ -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' | 'list' | 'richtext' | 'select' | 'hidden' | (string & {});
7
+ export type AttributeInputKind = 'text' | 'textarea' | 'number' | 'checkbox' | 'json' | 'list' | 'richtext' | 'select' | 'hidden' | 'image' | (string & {});
8
8
  export interface AttributeOption<T> {
9
9
  value: T;
10
10
  label?: string;
@@ -132,6 +132,26 @@ export interface BlockRegistry {
132
132
  get(id: string): BlockDefinition | undefined;
133
133
  has(id: string): boolean;
134
134
  }
135
+ /**
136
+ * Props a custom mark's render component receives: the mark's own attrs
137
+ * spread as individual props, plus the marked text (and any marks nested
138
+ * inside it) as children.
139
+ */
140
+ export interface MarkComponentProps {
141
+ children: Snippet;
142
+ [name: string]: unknown;
143
+ }
144
+ export interface MarkDefinition {
145
+ /** The ProseMirror mark type this renders, e.g. `superscript`. */
146
+ id: string;
147
+ component: Component<MarkComponentProps>;
148
+ }
149
+ export interface MarkRegistry {
150
+ marks: MarkDefinition[];
151
+ byId: ReadonlyMap<string, MarkDefinition>;
152
+ get(id: string): MarkDefinition | undefined;
153
+ has(id: string): boolean;
154
+ }
135
155
  export interface ContentSchema {
136
156
  allowedBlocks: ReadonlySet<string>;
137
157
  allowedMarks: ReadonlySet<string>;
@@ -13,11 +13,15 @@
13
13
  inferAttributeInputKind,
14
14
  normalizeAttributeOptions
15
15
  } from '../core/attributes.js';
16
+ import { onDestroy } from 'svelte';
16
17
  import type { AttributeSpec } from '../core/types.js';
18
+ import type { ImagePreviews } from '../shared/imagePreviews.js';
19
+ import type { ImageSource } from './imageSource.js';
17
20
  import ArrowUpIcon from 'phosphor-svelte/lib/ArrowUpIcon';
18
21
  import ArrowDownIcon from 'phosphor-svelte/lib/ArrowDownIcon';
19
22
  import PlusIcon from 'phosphor-svelte/lib/PlusIcon';
20
23
  import TrashIcon from 'phosphor-svelte/lib/TrashIcon';
24
+ import ImagePicker from './ImagePicker.svelte';
21
25
  import RichTextAttributeEditor from './RichTextAttributeEditor.svelte';
22
26
  // A list item's fields are ordinary attributes, edited by this same control.
23
27
  import AttributeFieldControl from './AttributeFieldControl.svelte';
@@ -29,9 +33,20 @@
29
33
  error?: string;
30
34
  onChange: (value: unknown) => void;
31
35
  onCustom?: (name: string, inputKind: string) => void;
36
+ imageSource?: ImageSource;
37
+ imagePreviews?: ImagePreviews;
32
38
  }
33
39
 
34
- let { name, spec, value = undefined, error = '', onChange, onCustom }: Props = $props();
40
+ let {
41
+ name,
42
+ spec,
43
+ value = undefined,
44
+ error = '',
45
+ onChange,
46
+ onCustom,
47
+ imageSource,
48
+ imagePreviews
49
+ }: Props = $props();
35
50
 
36
51
  const fieldId = `uncial-field-${nextFieldId++}`;
37
52
  const inputKind = $derived(inferAttributeInputKind(spec));
@@ -39,7 +54,10 @@
39
54
  // neither of which a <label for> can address, so only native form controls
40
55
  // get an associated <label>.
41
56
  const hasLabelableControl = $derived(
42
- isBuiltInInputKind(inputKind) && inputKind !== 'richtext' && inputKind !== 'list'
57
+ isBuiltInInputKind(inputKind) &&
58
+ inputKind !== 'richtext' &&
59
+ inputKind !== 'list' &&
60
+ inputKind !== 'image'
43
61
  );
44
62
  const options = $derived(normalizeAttributeOptions(spec) ?? []);
45
63
  const stringValue = $derived.by(() => {
@@ -63,6 +81,40 @@
63
81
  const itemLabel = $derived(list.itemLabel ?? 'item');
64
82
  const items = $derived(Array.isArray(value) ? (value as unknown[]) : []);
65
83
 
84
+ let fileInput = $state<HTMLInputElement>();
85
+ let picker = $state<ImagePicker>();
86
+ let uploading = $state(false);
87
+ let uploadError = $state('');
88
+ // The registry is not reactive, but an upload registers its preview before
89
+ // writing the value, so the value change is what recomputes this.
90
+ const thumbnailSrc = $derived(
91
+ stringValue
92
+ ? (imagePreviews?.get(stringValue) ?? imageSource?.thumbnail?.(stringValue) ?? stringValue)
93
+ : ''
94
+ );
95
+
96
+ // The panel remounts its controls when another block is selected, so an
97
+ // upload resolving after that would write into the wrong block. Its value is
98
+ // dropped instead; the preview is still registered so the committed file is
99
+ // usable through Choose existing before it is deployed.
100
+ let destroyed = false;
101
+ onDestroy(() => (destroyed = true));
102
+
103
+ async function uploadImage(file: File, upload: (file: File) => Promise<string>): Promise<void> {
104
+ uploading = true;
105
+ uploadError = '';
106
+ try {
107
+ const src = await upload(file);
108
+ imagePreviews?.register(src, file);
109
+ if (destroyed) return;
110
+ onChange(src);
111
+ } catch (reason) {
112
+ uploadError = reason instanceof Error ? reason.message : String(reason);
113
+ } finally {
114
+ uploading = false;
115
+ }
116
+ }
117
+
66
118
  function isBuiltInInputKind(kind: string): boolean {
67
119
  return [
68
120
  'checkbox',
@@ -73,7 +125,8 @@
73
125
  'json',
74
126
  'list',
75
127
  'text',
76
- 'hidden'
128
+ 'hidden',
129
+ 'image'
77
130
  ].includes(kind);
78
131
  }
79
132
 
@@ -204,6 +257,8 @@
204
257
  value={item}
205
258
  onChange={(next) => replaceItem(index, next)}
206
259
  {onCustom}
260
+ {imageSource}
261
+ {imagePreviews}
207
262
  />
208
263
  {:else}
209
264
  {#each listFields as [fieldName, fieldSpec] (fieldName)}
@@ -213,6 +268,8 @@
213
268
  value={(item as Record<string, unknown>)?.[fieldName]}
214
269
  onChange={(next) => setItemField(index, fieldName, next)}
215
270
  {onCustom}
271
+ {imageSource}
272
+ {imagePreviews}
216
273
  />
217
274
  {/each}
218
275
  {/if}
@@ -229,6 +286,73 @@
229
286
  <span>Add {itemLabel}</span>
230
287
  </button>
231
288
  </div>
289
+ {:else if inputKind === 'image'}
290
+ {@const upload = imageSource?.upload}
291
+ {@const browse = imageSource?.browse}
292
+ <div class="uncial-image-field">
293
+ {#if thumbnailSrc}
294
+ <img class="uncial-image-field__thumbnail" src={thumbnailSrc} alt="" />
295
+ {/if}
296
+ <div class="uncial-image-field__actions">
297
+ {#if upload}
298
+ <input
299
+ bind:this={fileInput}
300
+ type="file"
301
+ accept="image/*"
302
+ hidden
303
+ onchange={(event) => {
304
+ const target = event.currentTarget as HTMLInputElement;
305
+ const file = target.files?.[0];
306
+ // Reset so picking the same file again still fires `change`.
307
+ target.value = '';
308
+ if (file) void uploadImage(file, upload);
309
+ }}
310
+ />
311
+ <button
312
+ type="button"
313
+ class="uncial-btn uncial-btn--outline uncial-btn--sm"
314
+ disabled={uploading}
315
+ onclick={() => fileInput?.click()}
316
+ >
317
+ Upload
318
+ </button>
319
+ {/if}
320
+ {#if browse}
321
+ <button
322
+ type="button"
323
+ class="uncial-btn uncial-btn--outline uncial-btn--sm"
324
+ disabled={uploading}
325
+ onclick={() => picker?.open()}
326
+ >
327
+ Choose existing
328
+ </button>
329
+ <ImagePicker
330
+ bind:this={picker}
331
+ {browse}
332
+ thumbnail={imageSource?.thumbnail}
333
+ {imagePreviews}
334
+ current={stringValue}
335
+ onPick={onChange}
336
+ />
337
+ {/if}
338
+ {#if stringValue}
339
+ <button
340
+ type="button"
341
+ class="uncial-btn uncial-btn--ghost uncial-btn--sm"
342
+ disabled={uploading}
343
+ onclick={() => onChange('')}
344
+ >
345
+ Clear
346
+ </button>
347
+ {/if}
348
+ </div>
349
+ {#if uploading}
350
+ <span class="uncial-help-text" role="status">Uploading…</span>
351
+ {/if}
352
+ {#if uploadError}
353
+ <span class="uncial-field__error" role="alert">{uploadError}</span>
354
+ {/if}
355
+ </div>
232
356
  {:else if inputKind === 'textarea' || inputKind === 'json'}
233
357
  <textarea
234
358
  id={fieldId}
@@ -1,4 +1,6 @@
1
1
  import type { AttributeSpec } from '../core/types.js';
2
+ import type { ImagePreviews } from '../shared/imagePreviews.js';
3
+ import type { ImageSource } from './imageSource.js';
2
4
  import AttributeFieldControl from './AttributeFieldControl.svelte';
3
5
  interface Props {
4
6
  name: string;
@@ -7,6 +9,8 @@ interface Props {
7
9
  error?: string;
8
10
  onChange: (value: unknown) => void;
9
11
  onCustom?: (name: string, inputKind: string) => void;
12
+ imageSource?: ImageSource;
13
+ imagePreviews?: ImagePreviews;
10
14
  }
11
15
  declare const AttributeFieldControl: import("svelte").Component<Props, {}, "">;
12
16
  type AttributeFieldControl = ReturnType<typeof AttributeFieldControl>;
@@ -19,6 +19,8 @@
19
19
  import { dropdownDismiss } from './dropdownDismiss.js';
20
20
  import { dropdownFloat } from './dropdownFloat.js';
21
21
  import { CHOOSE_ATTRIBUTE_EVENT, type ChooseAttributeRequest } from './chooseAttribute.js';
22
+ import type { ImageSource } from './imageSource.js';
23
+ import type { ImagePreviews } from '../shared/imagePreviews.js';
22
24
 
23
25
  interface Props {
24
26
  controller: BlockAttributesController;
@@ -30,9 +32,12 @@
30
32
  * the panel falls back to dispatching that window event for back-compat.
31
33
  */
32
34
  onChooseAttribute?: (request: ChooseAttributeRequest) => void;
35
+ imageSource?: ImageSource;
36
+ /** Where a successful upload registers its preview; see `Editor`. */
37
+ imagePreviews?: ImagePreviews;
33
38
  }
34
39
 
35
- let { controller, blocks = [], onChooseAttribute }: Props = $props();
40
+ let { controller, blocks = [], onChooseAttribute, imageSource, imagePreviews }: Props = $props();
36
41
 
37
42
  let controllerState = $state<BlockAttributesState>(createInitialState());
38
43
 
@@ -70,7 +75,9 @@
70
75
  // a container declaring nothing offers everything the schema allows.
71
76
  const admissibleChildBlocks = $derived.by(() => {
72
77
  const declared =
73
- selectedBlock && 'content' in selectedBlock ? selectedBlock.content?.allowedBlocks : undefined;
78
+ selectedBlock && 'content' in selectedBlock
79
+ ? selectedBlock.content?.allowedBlocks
80
+ : undefined;
74
81
  return declared ? activeBlocks.filter((block) => declared.includes(block.id)) : activeBlocks;
75
82
  });
76
83
  const filteredChildBlocks = $derived.by(() => {
@@ -182,7 +189,7 @@
182
189
  {selectedBlock.label || controllerState.selectedBlockId || 'block'}
183
190
  </p>
184
191
  {#if !selectedBlockReadOnly}
185
- {#each selectedAttributeSpecs as [name, spec] (name)}
192
+ {#each selectedAttributeSpecs as [name, spec] (`${controllerState.activeBlock?.pos}:${name}`)}
186
193
  <AttributeFieldControl
187
194
  {name}
188
195
  {spec}
@@ -190,6 +197,8 @@
190
197
  error={controllerState.validationErrors[name]}
191
198
  onChange={(value) => controller.setDraftAttr(name, value)}
192
199
  onCustom={chooseCustomAttribute}
200
+ {imageSource}
201
+ {imagePreviews}
193
202
  />
194
203
  {/each}
195
204
  {/if}
@@ -1,6 +1,8 @@
1
1
  import type { BlockDefinition, BlockRegistry } from '../core/types.js';
2
2
  import { type BlockAttributesController } from './attributesController.js';
3
3
  import { type ChooseAttributeRequest } from './chooseAttribute.js';
4
+ import type { ImageSource } from './imageSource.js';
5
+ import type { ImagePreviews } from '../shared/imagePreviews.js';
4
6
  interface Props {
5
7
  controller: BlockAttributesController;
6
8
  blocks?: BlockRegistry | BlockDefinition[];
@@ -11,6 +13,9 @@ interface Props {
11
13
  * the panel falls back to dispatching that window event for back-compat.
12
14
  */
13
15
  onChooseAttribute?: (request: ChooseAttributeRequest) => void;
16
+ imageSource?: ImageSource;
17
+ /** Where a successful upload registers its preview; see `Editor`. */
18
+ imagePreviews?: ImagePreviews;
14
19
  }
15
20
  declare const BlockAttributesPanel: import("svelte").Component<Props, {}, "">;
16
21
  type BlockAttributesPanel = ReturnType<typeof BlockAttributesPanel>;
@@ -85,7 +85,8 @@
85
85
  onpointerup={handleActivationPointerUp}
86
86
  onclick={stopActivationClick}>{label}</button
87
87
  >
88
- </div><div class="uncial-nodeview-body">
88
+ </div>
89
+ <div class="uncial-nodeview-body">
89
90
  <BlockComponent {...componentProps} />
90
91
  </div>
91
92
  </div>
@@ -61,7 +61,11 @@
61
61
  >
62
62
  Reset
63
63
  </button>
64
- <button type="button" class="uncial-btn uncial-btn--primary uncial-btn--sm" onclick={commitMeta}>
64
+ <button
65
+ type="button"
66
+ class="uncial-btn uncial-btn--primary uncial-btn--sm"
67
+ onclick={commitMeta}
68
+ >
65
69
  Save Metadata
66
70
  </button>
67
71
  </div>
@@ -1,5 +1,6 @@
1
1
  <script lang="ts">
2
2
  import type { AnyExtension, Editor as TiptapEditor, JSONContent } from '@tiptap/core';
3
+ import { onDestroy } from 'svelte';
3
4
  import PlusIcon from 'phosphor-svelte/lib/PlusIcon';
4
5
  import CaretDownIcon from 'phosphor-svelte/lib/CaretDownIcon';
5
6
  import { resolveRegistry } from '../core/registry.js';
@@ -28,6 +29,8 @@
28
29
  import DocumentMetaPanel from './DocumentMetaPanel.svelte';
29
30
  import Toolbar from './Toolbar.svelte';
30
31
  import type { ChooseAttributeRequest } from './chooseAttribute.js';
32
+ import type { ImageSource } from './imageSource.js';
33
+ import { createImagePreviews } from '../shared/imagePreviews.js';
31
34
  import { dropdownDismiss } from './dropdownDismiss.js';
32
35
  import type { ToolbarFeature, ToolbarFeatureSelection } from './toolbarFeatures.js';
33
36
 
@@ -67,6 +70,8 @@
67
70
  presentation?: 'card' | 'bare';
68
71
  /** Forwarded to the built-in panel; see `BlockAttributesPanel`. */
69
72
  onChooseAttribute?: (request: ChooseAttributeRequest) => void;
73
+ /** Uploads, existing images and thumbnails for `input: 'image'` attributes. */
74
+ imageSource?: ImageSource;
70
75
  onIssue?: (issue: ValidationIssue) => void;
71
76
  /**
72
77
  * The document after an edit, and the metadata after one.
@@ -95,6 +100,7 @@
95
100
  attributesPanel = true,
96
101
  presentation = 'card',
97
102
  onChooseAttribute,
103
+ imageSource,
98
104
  onIssue,
99
105
  onChange,
100
106
  onMetaChange
@@ -108,6 +114,8 @@
108
114
  let metaTriggerEl = $state<HTMLElement | null>(null);
109
115
  const internalController = createBlockAttributesController();
110
116
  const internalMetaController = createDocumentMetaController();
117
+ // Per instance: two editors on one page must not see each other's uploads.
118
+ const imagePreviews = createImagePreviews();
111
119
  let metaState = $state<DocumentMetaState>({
112
120
  draft: {},
113
121
  errors: {},
@@ -157,6 +165,7 @@
157
165
  extensions,
158
166
  attributesController: controller,
159
167
  autoOpenAttributes: panelMode !== 'overlay',
168
+ imagePreviews,
160
169
  onIssue,
161
170
  onChange: (nextDocument) => {
162
171
  setDocument(nextDocument);
@@ -189,9 +198,7 @@
189
198
  const editorGutterWidth = $derived.by(() => {
190
199
  const longestWord = registry.blocks.reduce(
191
200
  (longest, block) =>
192
- block.label
193
- .split(/\s+/)
194
- .reduce((widest, word) => Math.max(widest, word.length), longest),
201
+ block.label.split(/\s+/).reduce((widest, word) => Math.max(widest, word.length), longest),
195
202
  0
196
203
  );
197
204
 
@@ -287,6 +294,8 @@
287
294
  return unsubscribe;
288
295
  });
289
296
 
297
+ onDestroy(() => imagePreviews.revokeAll());
298
+
290
299
  $effect(() => {
291
300
  documentMetaController.setMetaFields(resolvedMetaFields);
292
301
  });
@@ -428,10 +437,7 @@
428
437
  </div>
429
438
  {#if panelVisible}
430
439
  <aside
431
- class={[
432
- 'uncial-editor-sidebar',
433
- panelMode === 'overlay' && 'uncial-editor-sidebar--overlay'
434
- ]}
440
+ class={['uncial-editor-sidebar', panelMode === 'overlay' && 'uncial-editor-sidebar--overlay']}
435
441
  aria-label="Block attributes"
436
442
  >
437
443
  {#if panelMode === 'overlay'}
@@ -448,7 +454,13 @@
448
454
  </button>
449
455
  </div>
450
456
  {/if}
451
- <BlockAttributesPanel {controller} {blocks} {onChooseAttribute} />
457
+ <BlockAttributesPanel
458
+ {controller}
459
+ {blocks}
460
+ {onChooseAttribute}
461
+ {imageSource}
462
+ {imagePreviews}
463
+ />
452
464
  </aside>
453
465
  {/if}
454
466
  </div>
@@ -3,6 +3,7 @@ import type { AttributeSpec, BlockDefinition, BlockRegistry, ContentSchema, Docu
3
3
  import { type BlockAttributesController } from './attributesController.js';
4
4
  import { type DocumentMetaController } from './metaController.js';
5
5
  import type { ChooseAttributeRequest } from './chooseAttribute.js';
6
+ import type { ImageSource } from './imageSource.js';
6
7
  import type { ToolbarFeature, ToolbarFeatureSelection } from './toolbarFeatures.js';
7
8
  interface Props {
8
9
  blocks?: BlockRegistry | BlockDefinition[];
@@ -40,6 +41,8 @@ interface Props {
40
41
  presentation?: 'card' | 'bare';
41
42
  /** Forwarded to the built-in panel; see `BlockAttributesPanel`. */
42
43
  onChooseAttribute?: (request: ChooseAttributeRequest) => void;
44
+ /** Uploads, existing images and thumbnails for `input: 'image'` attributes. */
45
+ imageSource?: ImageSource;
43
46
  onIssue?: (issue: ValidationIssue) => void;
44
47
  /**
45
48
  * The document after an edit, and the metadata after one.
@@ -0,0 +1,85 @@
1
+ <script lang="ts" module>
2
+ let nextPickerId = 0;
3
+ </script>
4
+
5
+ <script lang="ts">
6
+ import XIcon from 'phosphor-svelte/lib/XIcon';
7
+ import type { ImagePreviews } from '../shared/imagePreviews.js';
8
+
9
+ interface Props {
10
+ browse: () => Promise<string[]>;
11
+ thumbnail?: (src: string) => string;
12
+ imagePreviews?: ImagePreviews;
13
+ current: string;
14
+ onPick: (value: string) => void;
15
+ }
16
+
17
+ let { browse, thumbnail, imagePreviews, current, onPick }: Props = $props();
18
+
19
+ const titleId = `uncial-image-picker-${nextPickerId++}`;
20
+ let dialog = $state<HTMLDialogElement>();
21
+ let images = $state<Promise<string[]>>();
22
+
23
+ export function open(): void {
24
+ images = browse();
25
+ dialog?.showModal();
26
+ }
27
+
28
+ function tileSrc(value: string): string {
29
+ return imagePreviews?.get(value) ?? thumbnail?.(value) ?? value;
30
+ }
31
+
32
+ // Stored names may be content hashes, so the last path segment is the most
33
+ // a tile can say about itself.
34
+ function fileName(value: string): string {
35
+ return value.split(/[?#]/)[0].split('/').filter(Boolean).at(-1) ?? value;
36
+ }
37
+
38
+ function pick(value: string): void {
39
+ onPick(value);
40
+ dialog?.close();
41
+ }
42
+
43
+ function message(reason: unknown): string {
44
+ return reason instanceof Error ? reason.message : String(reason);
45
+ }
46
+ </script>
47
+
48
+ <dialog bind:this={dialog} class="uncial-image-picker" aria-labelledby={titleId}>
49
+ <div class="uncial-image-picker__head">
50
+ <h2 id={titleId} class="uncial-section-label">Choose an image</h2>
51
+ <button
52
+ type="button"
53
+ class="uncial-btn uncial-btn--ghost uncial-btn--xs uncial-btn--square"
54
+ aria-label="Close"
55
+ onclick={() => dialog?.close()}
56
+ >
57
+ <XIcon size={12} weight="bold" />
58
+ </button>
59
+ </div>
60
+ {#if images}
61
+ {#await images}
62
+ <p class="uncial-help-text" role="status">Loading images…</p>
63
+ {:then values}
64
+ {#if values.length === 0}
65
+ <p class="uncial-help-text">No images yet.</p>
66
+ {:else}
67
+ <div class="uncial-image-picker__grid">
68
+ {#each values as value (value)}
69
+ <button
70
+ type="button"
71
+ class="uncial-image-picker__tile"
72
+ aria-label={fileName(value)}
73
+ aria-pressed={value === current}
74
+ onclick={() => pick(value)}
75
+ >
76
+ <img src={tileSrc(value)} alt="" loading="lazy" />
77
+ </button>
78
+ {/each}
79
+ </div>
80
+ {/if}
81
+ {:catch reason}
82
+ <p class="uncial-field__error" role="alert">{message(reason)}</p>
83
+ {/await}
84
+ {/if}
85
+ </dialog>