uncial 3.0.0 → 5.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 (45) hide show
  1. package/README.md +6 -1
  2. package/dist/core/defineBlock.js +5 -2
  3. package/dist/core/types.d.ts +5 -1
  4. package/dist/editor/AttributeFieldControl.svelte +156 -3
  5. package/dist/editor/AttributeFieldControl.svelte.d.ts +4 -0
  6. package/dist/editor/BlockAttributesPanel.svelte +12 -3
  7. package/dist/editor/BlockAttributesPanel.svelte.d.ts +5 -0
  8. package/dist/editor/BlockNodeView.svelte +2 -1
  9. package/dist/editor/DocumentMetaPanel.svelte +12 -2
  10. package/dist/editor/DocumentMetaPanel.svelte.d.ts +4 -0
  11. package/dist/editor/Editor.svelte +26 -9
  12. package/dist/editor/Editor.svelte.d.ts +3 -0
  13. package/dist/editor/ImagePicker.svelte +85 -0
  14. package/dist/editor/ImagePicker.svelte.d.ts +13 -0
  15. package/dist/editor/bindEditor.d.ts +3 -0
  16. package/dist/editor/bindEditor.js +1 -1
  17. package/dist/editor/imageSource.d.ts +14 -0
  18. package/dist/editor/imageSource.js +1 -0
  19. package/dist/editor/index.d.ts +1 -0
  20. package/dist/editor/metaController.js +0 -5
  21. package/dist/editor/zonedDateTime.d.ts +4 -0
  22. package/dist/editor/zonedDateTime.js +38 -0
  23. package/dist/render/SuperscriptMarkFixture.svelte +1 -1
  24. package/dist/render/SuperscriptMarkFixture.svelte.d.ts +1 -1
  25. package/dist/render/Tabs.svelte +1 -1
  26. package/dist/render/index.d.ts +1 -0
  27. package/dist/render/index.js +1 -0
  28. package/dist/render/resolveImageSrc.d.ts +6 -0
  29. package/dist/render/resolveImageSrc.js +10 -0
  30. package/dist/render/tabSelection.svelte.js +2 -0
  31. package/dist/shared/ImageBlockFixture.svelte +29 -0
  32. package/dist/shared/ImageBlockFixture.svelte.d.ts +11 -0
  33. package/dist/shared/imagePreviews.d.ts +24 -0
  34. package/dist/shared/imagePreviews.js +78 -0
  35. package/dist/shared/tiptap.d.ts +2 -1
  36. package/dist/shared/tiptap.js +16 -13
  37. package/dist/styles/base.css +89 -9
  38. package/dist/styles/controls.css +6 -1
  39. package/dist/ui/ThemeSwitcher.svelte +1 -1
  40. package/dist/ui/theme.svelte.js +1 -1
  41. package/dist/web-components/UncialEditor.svelte +4 -0
  42. package/dist/web-components/UncialEditor.svelte.d.ts +2 -1
  43. package/dist/web-components/index.d.ts +4 -1
  44. package/dist/web-components/index.js +6 -0
  45. 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
  }
@@ -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' | 'date' | 'datetime' | 'checkbox' | 'json' | 'list' | 'richtext' | 'select' | 'hidden' | 'image' | (string & {});
8
8
  export interface AttributeOption<T> {
9
9
  value: T;
10
10
  label?: string;
@@ -32,6 +32,10 @@ export interface AttributeSpec<T> {
32
32
  serialize?: AttrSerializer;
33
33
  input?: AttributeInputKind;
34
34
  placeholder?: string;
35
+ /** Advice shown under the field for a value that is valid but unwise; it never blocks a save. */
36
+ warn?: (value: unknown) => string | null | undefined;
37
+ /** IANA zone in which a `datetime` input shows and writes its wall clock; the browser's own by default. */
38
+ timeZone?: string;
35
39
  options?: ReadonlyArray<T | AttributeOption<T>>;
36
40
  /** Item shape for an array-valued attribute; implies `input: 'list'`. */
37
41
  list?: AttributeListSpec;
@@ -13,11 +13,16 @@
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';
25
+ import { fromWallClock, toWallClock } from './zonedDateTime.js';
21
26
  import RichTextAttributeEditor from './RichTextAttributeEditor.svelte';
22
27
  // A list item's fields are ordinary attributes, edited by this same control.
23
28
  import AttributeFieldControl from './AttributeFieldControl.svelte';
@@ -29,9 +34,20 @@
29
34
  error?: string;
30
35
  onChange: (value: unknown) => void;
31
36
  onCustom?: (name: string, inputKind: string) => void;
37
+ imageSource?: ImageSource;
38
+ imagePreviews?: ImagePreviews;
32
39
  }
33
40
 
34
- let { name, spec, value = undefined, error = '', onChange, onCustom }: Props = $props();
41
+ let {
42
+ name,
43
+ spec,
44
+ value = undefined,
45
+ error = '',
46
+ onChange,
47
+ onCustom,
48
+ imageSource,
49
+ imagePreviews
50
+ }: Props = $props();
35
51
 
36
52
  const fieldId = `uncial-field-${nextFieldId++}`;
37
53
  const inputKind = $derived(inferAttributeInputKind(spec));
@@ -39,9 +55,13 @@
39
55
  // neither of which a <label for> can address, so only native form controls
40
56
  // get an associated <label>.
41
57
  const hasLabelableControl = $derived(
42
- isBuiltInInputKind(inputKind) && inputKind !== 'richtext' && inputKind !== 'list'
58
+ isBuiltInInputKind(inputKind) &&
59
+ inputKind !== 'richtext' &&
60
+ inputKind !== 'list' &&
61
+ inputKind !== 'image'
43
62
  );
44
63
  const options = $derived(normalizeAttributeOptions(spec) ?? []);
64
+ const warning = $derived(spec.warn?.(value) ?? '');
45
65
  const stringValue = $derived.by(() => {
46
66
  if (typeof value === 'string') return value;
47
67
  if (value === undefined || value === null) return '';
@@ -63,17 +83,54 @@
63
83
  const itemLabel = $derived(list.itemLabel ?? 'item');
64
84
  const items = $derived(Array.isArray(value) ? (value as unknown[]) : []);
65
85
 
86
+ let fileInput = $state<HTMLInputElement>();
87
+ let picker = $state<ImagePicker>();
88
+ let uploading = $state(false);
89
+ let uploadError = $state('');
90
+ // The registry is not reactive, but an upload registers its preview before
91
+ // writing the value, so the value change is what recomputes this.
92
+ const thumbnailSrc = $derived(
93
+ stringValue
94
+ ? (imagePreviews?.get(stringValue) ?? imageSource?.thumbnail?.(stringValue) ?? stringValue)
95
+ : ''
96
+ );
97
+
98
+ // The panel remounts its controls when another block is selected, so an
99
+ // upload resolving after that would write into the wrong block. Its value is
100
+ // dropped instead; the preview is still registered so the committed file is
101
+ // usable through Choose existing before it is deployed.
102
+ let destroyed = false;
103
+ onDestroy(() => (destroyed = true));
104
+
105
+ async function uploadImage(file: File, upload: (file: File) => Promise<string>): Promise<void> {
106
+ uploading = true;
107
+ uploadError = '';
108
+ try {
109
+ const src = await upload(file);
110
+ imagePreviews?.register(src, file);
111
+ if (destroyed) return;
112
+ onChange(src);
113
+ } catch (reason) {
114
+ uploadError = reason instanceof Error ? reason.message : String(reason);
115
+ } finally {
116
+ uploading = false;
117
+ }
118
+ }
119
+
66
120
  function isBuiltInInputKind(kind: string): boolean {
67
121
  return [
68
122
  'checkbox',
69
123
  'number',
124
+ 'date',
125
+ 'datetime',
70
126
  'richtext',
71
127
  'select',
72
128
  'textarea',
73
129
  'json',
74
130
  'list',
75
131
  'text',
76
- 'hidden'
132
+ 'hidden',
133
+ 'image'
77
134
  ].includes(kind);
78
135
  }
79
136
 
@@ -141,6 +198,28 @@
141
198
  onChange(target.value === '' ? '' : target.valueAsNumber);
142
199
  }}
143
200
  />
201
+ {:else if inputKind === 'date'}
202
+ <input
203
+ id={fieldId}
204
+ class="uncial-input uncial-input--sm"
205
+ type="date"
206
+ value={stringValue}
207
+ onchange={(event) => {
208
+ const target = event.currentTarget as HTMLInputElement;
209
+ onChange(target.value);
210
+ }}
211
+ />
212
+ {:else if inputKind === 'datetime'}
213
+ <input
214
+ id={fieldId}
215
+ class="uncial-input uncial-input--sm"
216
+ type="datetime-local"
217
+ value={toWallClock(stringValue, spec.timeZone)}
218
+ onchange={(event) => {
219
+ const target = event.currentTarget as HTMLInputElement;
220
+ onChange(fromWallClock(target.value, spec.timeZone));
221
+ }}
222
+ />
144
223
  {:else if inputKind === 'richtext'}
145
224
  <RichTextAttributeEditor
146
225
  {value}
@@ -204,6 +283,8 @@
204
283
  value={item}
205
284
  onChange={(next) => replaceItem(index, next)}
206
285
  {onCustom}
286
+ {imageSource}
287
+ {imagePreviews}
207
288
  />
208
289
  {:else}
209
290
  {#each listFields as [fieldName, fieldSpec] (fieldName)}
@@ -213,6 +294,8 @@
213
294
  value={(item as Record<string, unknown>)?.[fieldName]}
214
295
  onChange={(next) => setItemField(index, fieldName, next)}
215
296
  {onCustom}
297
+ {imageSource}
298
+ {imagePreviews}
216
299
  />
217
300
  {/each}
218
301
  {/if}
@@ -229,6 +312,73 @@
229
312
  <span>Add {itemLabel}</span>
230
313
  </button>
231
314
  </div>
315
+ {:else if inputKind === 'image'}
316
+ {@const upload = imageSource?.upload}
317
+ {@const browse = imageSource?.browse}
318
+ <div class="uncial-image-field">
319
+ {#if thumbnailSrc}
320
+ <img class="uncial-image-field__thumbnail" src={thumbnailSrc} alt="" />
321
+ {/if}
322
+ <div class="uncial-image-field__actions">
323
+ {#if upload}
324
+ <input
325
+ bind:this={fileInput}
326
+ type="file"
327
+ accept="image/*"
328
+ hidden
329
+ onchange={(event) => {
330
+ const target = event.currentTarget as HTMLInputElement;
331
+ const file = target.files?.[0];
332
+ // Reset so picking the same file again still fires `change`.
333
+ target.value = '';
334
+ if (file) void uploadImage(file, upload);
335
+ }}
336
+ />
337
+ <button
338
+ type="button"
339
+ class="uncial-btn uncial-btn--outline uncial-btn--sm"
340
+ disabled={uploading}
341
+ onclick={() => fileInput?.click()}
342
+ >
343
+ Upload
344
+ </button>
345
+ {/if}
346
+ {#if browse}
347
+ <button
348
+ type="button"
349
+ class="uncial-btn uncial-btn--outline uncial-btn--sm"
350
+ disabled={uploading}
351
+ onclick={() => picker?.open()}
352
+ >
353
+ Choose existing
354
+ </button>
355
+ <ImagePicker
356
+ bind:this={picker}
357
+ {browse}
358
+ thumbnail={imageSource?.thumbnail}
359
+ {imagePreviews}
360
+ current={stringValue}
361
+ onPick={onChange}
362
+ />
363
+ {/if}
364
+ {#if stringValue}
365
+ <button
366
+ type="button"
367
+ class="uncial-btn uncial-btn--ghost uncial-btn--sm"
368
+ disabled={uploading}
369
+ onclick={() => onChange('')}
370
+ >
371
+ Clear
372
+ </button>
373
+ {/if}
374
+ </div>
375
+ {#if uploading}
376
+ <span class="uncial-help-text" role="status">Uploading…</span>
377
+ {/if}
378
+ {#if uploadError}
379
+ <span class="uncial-field__error" role="alert">{uploadError}</span>
380
+ {/if}
381
+ </div>
232
382
  {:else if inputKind === 'textarea' || inputKind === 'json'}
233
383
  <textarea
234
384
  id={fieldId}
@@ -254,6 +404,9 @@
254
404
  }}
255
405
  />
256
406
  {/if}
407
+ {#if warning}
408
+ <span class="uncial-field__warning" role="status">{warning}</span>
409
+ {/if}
257
410
  {#if error}
258
411
  <span class="uncial-field__error">{error}</span>
259
412
  {/if}
@@ -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>
@@ -1,15 +1,19 @@
1
1
  <script lang="ts">
2
2
  import type { AttributeSpec } from '../core/types.js';
3
+ import type { ImagePreviews } from '../shared/imagePreviews.js';
3
4
  import AttributeFieldControl from './AttributeFieldControl.svelte';
5
+ import type { ImageSource } from './imageSource.js';
4
6
  import type { DocumentMetaController, DocumentMetaState } from './metaController.js';
5
7
 
6
8
  interface Props {
7
9
  controller: DocumentMetaController;
8
10
  fields?: ReadonlyMap<string, AttributeSpec<unknown>>;
9
11
  onCommit?: (meta: Record<string, unknown>) => void;
12
+ imageSource?: ImageSource;
13
+ imagePreviews?: ImagePreviews;
10
14
  }
11
15
 
12
- let { controller, fields = new Map(), onCommit }: Props = $props();
16
+ let { controller, fields = new Map(), onCommit, imageSource, imagePreviews }: Props = $props();
13
17
 
14
18
  let controllerState = $state<DocumentMetaState>({
15
19
  draft: {},
@@ -47,6 +51,8 @@
47
51
  value={controllerState.draft[name]}
48
52
  error={controllerState.errors[name]}
49
53
  onChange={(value) => controller.setDraft(name, value)}
54
+ {imageSource}
55
+ {imagePreviews}
50
56
  />
51
57
  {:else}
52
58
  <p class="uncial-help-text">No metadata fields configured.</p>
@@ -61,7 +67,11 @@
61
67
  >
62
68
  Reset
63
69
  </button>
64
- <button type="button" class="uncial-btn uncial-btn--primary uncial-btn--sm" onclick={commitMeta}>
70
+ <button
71
+ type="button"
72
+ class="uncial-btn uncial-btn--primary uncial-btn--sm"
73
+ onclick={commitMeta}
74
+ >
65
75
  Save Metadata
66
76
  </button>
67
77
  </div>
@@ -1,9 +1,13 @@
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 type { DocumentMetaController } from './metaController.js';
3
5
  interface Props {
4
6
  controller: DocumentMetaController;
5
7
  fields?: ReadonlyMap<string, AttributeSpec<unknown>>;
6
8
  onCommit?: (meta: Record<string, unknown>) => void;
9
+ imageSource?: ImageSource;
10
+ imagePreviews?: ImagePreviews;
7
11
  }
8
12
  declare const DocumentMetaPanel: import("svelte").Component<Props, {}, "">;
9
13
  type DocumentMetaPanel = ReturnType<typeof DocumentMetaPanel>;
@@ -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';
@@ -26,8 +27,11 @@
26
27
  } from './metaController.js';
27
28
  import BlockAttributesPanel from './BlockAttributesPanel.svelte';
28
29
  import DocumentMetaPanel from './DocumentMetaPanel.svelte';
30
+ import { dropdownFloat } from './dropdownFloat.js';
29
31
  import Toolbar from './Toolbar.svelte';
30
32
  import type { ChooseAttributeRequest } from './chooseAttribute.js';
33
+ import type { ImageSource } from './imageSource.js';
34
+ import { createImagePreviews, missingImagePlaceholder } from '../shared/imagePreviews.js';
31
35
  import { dropdownDismiss } from './dropdownDismiss.js';
32
36
  import type { ToolbarFeature, ToolbarFeatureSelection } from './toolbarFeatures.js';
33
37
 
@@ -67,6 +71,8 @@
67
71
  presentation?: 'card' | 'bare';
68
72
  /** Forwarded to the built-in panel; see `BlockAttributesPanel`. */
69
73
  onChooseAttribute?: (request: ChooseAttributeRequest) => void;
74
+ /** Uploads, existing images and thumbnails for `input: 'image'` attributes. */
75
+ imageSource?: ImageSource;
70
76
  onIssue?: (issue: ValidationIssue) => void;
71
77
  /**
72
78
  * The document after an edit, and the metadata after one.
@@ -95,6 +101,7 @@
95
101
  attributesPanel = true,
96
102
  presentation = 'card',
97
103
  onChooseAttribute,
104
+ imageSource,
98
105
  onIssue,
99
106
  onChange,
100
107
  onMetaChange
@@ -108,6 +115,10 @@
108
115
  let metaTriggerEl = $state<HTMLElement | null>(null);
109
116
  const internalController = createBlockAttributesController();
110
117
  const internalMetaController = createDocumentMetaController();
118
+ // Per instance: two editors on one page must not see each other's uploads.
119
+ const imagePreviews = createImagePreviews((src) =>
120
+ imageSource?.missing?.(src) ? missingImagePlaceholder(src) : undefined
121
+ );
111
122
  let metaState = $state<DocumentMetaState>({
112
123
  draft: {},
113
124
  errors: {},
@@ -157,6 +168,7 @@
157
168
  extensions,
158
169
  attributesController: controller,
159
170
  autoOpenAttributes: panelMode !== 'overlay',
171
+ imagePreviews,
160
172
  onIssue,
161
173
  onChange: (nextDocument) => {
162
174
  setDocument(nextDocument);
@@ -189,9 +201,7 @@
189
201
  const editorGutterWidth = $derived.by(() => {
190
202
  const longestWord = registry.blocks.reduce(
191
203
  (longest, block) =>
192
- block.label
193
- .split(/\s+/)
194
- .reduce((widest, word) => Math.max(widest, word.length), longest),
204
+ block.label.split(/\s+/).reduce((widest, word) => Math.max(widest, word.length), longest),
195
205
  0
196
206
  );
197
207
 
@@ -287,6 +297,8 @@
287
297
  return unsubscribe;
288
298
  });
289
299
 
300
+ onDestroy(() => imagePreviews.revokeAll());
301
+
290
302
  $effect(() => {
291
303
  documentMetaController.setMetaFields(resolvedMetaFields);
292
304
  });
@@ -375,7 +387,7 @@
375
387
  {#if hasMetaFields || activeBlocks.length > 0}
376
388
  <div class="uncial-toolbar__actions">
377
389
  {#if hasMetaFields}
378
- <details class="uncial-dropdown uncial-dropdown--end" use:dropdownDismiss>
390
+ <details class="uncial-dropdown uncial-dropdown--end" use:dropdownDismiss use:dropdownFloat>
379
391
  <summary
380
392
  bind:this={metaTriggerEl}
381
393
  aria-label="Edit document metadata"
@@ -389,6 +401,8 @@
389
401
  controller={documentMetaController}
390
402
  fields={resolvedMetaFields}
391
403
  onCommit={commitMeta}
404
+ {imageSource}
405
+ {imagePreviews}
392
406
  />
393
407
  </div>
394
408
  </details>
@@ -428,10 +442,7 @@
428
442
  </div>
429
443
  {#if panelVisible}
430
444
  <aside
431
- class={[
432
- 'uncial-editor-sidebar',
433
- panelMode === 'overlay' && 'uncial-editor-sidebar--overlay'
434
- ]}
445
+ class={['uncial-editor-sidebar', panelMode === 'overlay' && 'uncial-editor-sidebar--overlay']}
435
446
  aria-label="Block attributes"
436
447
  >
437
448
  {#if panelMode === 'overlay'}
@@ -448,7 +459,13 @@
448
459
  </button>
449
460
  </div>
450
461
  {/if}
451
- <BlockAttributesPanel {controller} {blocks} {onChooseAttribute} />
462
+ <BlockAttributesPanel
463
+ {controller}
464
+ {blocks}
465
+ {onChooseAttribute}
466
+ {imageSource}
467
+ {imagePreviews}
468
+ />
452
469
  </aside>
453
470
  {/if}
454
471
  </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>