uncial 4.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.
- package/dist/core/types.d.ts +5 -1
- package/dist/editor/AttributeFieldControl.svelte +29 -0
- package/dist/editor/DocumentMetaPanel.svelte +7 -1
- package/dist/editor/DocumentMetaPanel.svelte.d.ts +4 -0
- package/dist/editor/Editor.svelte +8 -3
- package/dist/editor/imageSource.d.ts +2 -0
- package/dist/editor/zonedDateTime.d.ts +4 -0
- package/dist/editor/zonedDateTime.js +38 -0
- package/dist/shared/imagePreviews.d.ts +4 -1
- package/dist/shared/imagePreviews.js +22 -3
- package/dist/styles/controls.css +6 -1
- package/package.json +1 -1
package/dist/core/types.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { NormalizedBlockComponentDefinition } from './runtime.js';
|
|
|
4
4
|
export type AttrValidator<T> = (value: unknown) => value is T;
|
|
5
5
|
export type AttrParser<T> = (value: unknown) => T | undefined;
|
|
6
6
|
export type AttrSerializer = (value: unknown) => unknown;
|
|
7
|
-
export type AttributeInputKind = 'text' | 'textarea' | 'number' | 'checkbox' | 'json' | 'list' | 'richtext' | 'select' | 'hidden' | 'image' | (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;
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
import PlusIcon from 'phosphor-svelte/lib/PlusIcon';
|
|
23
23
|
import TrashIcon from 'phosphor-svelte/lib/TrashIcon';
|
|
24
24
|
import ImagePicker from './ImagePicker.svelte';
|
|
25
|
+
import { fromWallClock, toWallClock } from './zonedDateTime.js';
|
|
25
26
|
import RichTextAttributeEditor from './RichTextAttributeEditor.svelte';
|
|
26
27
|
// A list item's fields are ordinary attributes, edited by this same control.
|
|
27
28
|
import AttributeFieldControl from './AttributeFieldControl.svelte';
|
|
@@ -60,6 +61,7 @@
|
|
|
60
61
|
inputKind !== 'image'
|
|
61
62
|
);
|
|
62
63
|
const options = $derived(normalizeAttributeOptions(spec) ?? []);
|
|
64
|
+
const warning = $derived(spec.warn?.(value) ?? '');
|
|
63
65
|
const stringValue = $derived.by(() => {
|
|
64
66
|
if (typeof value === 'string') return value;
|
|
65
67
|
if (value === undefined || value === null) return '';
|
|
@@ -119,6 +121,8 @@
|
|
|
119
121
|
return [
|
|
120
122
|
'checkbox',
|
|
121
123
|
'number',
|
|
124
|
+
'date',
|
|
125
|
+
'datetime',
|
|
122
126
|
'richtext',
|
|
123
127
|
'select',
|
|
124
128
|
'textarea',
|
|
@@ -194,6 +198,28 @@
|
|
|
194
198
|
onChange(target.value === '' ? '' : target.valueAsNumber);
|
|
195
199
|
}}
|
|
196
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
|
+
/>
|
|
197
223
|
{:else if inputKind === 'richtext'}
|
|
198
224
|
<RichTextAttributeEditor
|
|
199
225
|
{value}
|
|
@@ -378,6 +404,9 @@
|
|
|
378
404
|
}}
|
|
379
405
|
/>
|
|
380
406
|
{/if}
|
|
407
|
+
{#if warning}
|
|
408
|
+
<span class="uncial-field__warning" role="status">{warning}</span>
|
|
409
|
+
{/if}
|
|
381
410
|
{#if error}
|
|
382
411
|
<span class="uncial-field__error">{error}</span>
|
|
383
412
|
{/if}
|
|
@@ -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>
|
|
@@ -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>;
|
|
@@ -27,10 +27,11 @@
|
|
|
27
27
|
} from './metaController.js';
|
|
28
28
|
import BlockAttributesPanel from './BlockAttributesPanel.svelte';
|
|
29
29
|
import DocumentMetaPanel from './DocumentMetaPanel.svelte';
|
|
30
|
+
import { dropdownFloat } from './dropdownFloat.js';
|
|
30
31
|
import Toolbar from './Toolbar.svelte';
|
|
31
32
|
import type { ChooseAttributeRequest } from './chooseAttribute.js';
|
|
32
33
|
import type { ImageSource } from './imageSource.js';
|
|
33
|
-
import { createImagePreviews } from '../shared/imagePreviews.js';
|
|
34
|
+
import { createImagePreviews, missingImagePlaceholder } from '../shared/imagePreviews.js';
|
|
34
35
|
import { dropdownDismiss } from './dropdownDismiss.js';
|
|
35
36
|
import type { ToolbarFeature, ToolbarFeatureSelection } from './toolbarFeatures.js';
|
|
36
37
|
|
|
@@ -115,7 +116,9 @@
|
|
|
115
116
|
const internalController = createBlockAttributesController();
|
|
116
117
|
const internalMetaController = createDocumentMetaController();
|
|
117
118
|
// Per instance: two editors on one page must not see each other's uploads.
|
|
118
|
-
const imagePreviews = createImagePreviews()
|
|
119
|
+
const imagePreviews = createImagePreviews((src) =>
|
|
120
|
+
imageSource?.missing?.(src) ? missingImagePlaceholder(src) : undefined
|
|
121
|
+
);
|
|
119
122
|
let metaState = $state<DocumentMetaState>({
|
|
120
123
|
draft: {},
|
|
121
124
|
errors: {},
|
|
@@ -384,7 +387,7 @@
|
|
|
384
387
|
{#if hasMetaFields || activeBlocks.length > 0}
|
|
385
388
|
<div class="uncial-toolbar__actions">
|
|
386
389
|
{#if hasMetaFields}
|
|
387
|
-
<details class="uncial-dropdown uncial-dropdown--end" use:dropdownDismiss>
|
|
390
|
+
<details class="uncial-dropdown uncial-dropdown--end" use:dropdownDismiss use:dropdownFloat>
|
|
388
391
|
<summary
|
|
389
392
|
bind:this={metaTriggerEl}
|
|
390
393
|
aria-label="Edit document metadata"
|
|
@@ -398,6 +401,8 @@
|
|
|
398
401
|
controller={documentMetaController}
|
|
399
402
|
fields={resolvedMetaFields}
|
|
400
403
|
onCommit={commitMeta}
|
|
404
|
+
{imageSource}
|
|
405
|
+
{imagePreviews}
|
|
401
406
|
/>
|
|
402
407
|
</div>
|
|
403
408
|
</details>
|
|
@@ -9,4 +9,6 @@ export interface ImageSource {
|
|
|
9
9
|
browse?: () => Promise<string[]>;
|
|
10
10
|
/** A displayable URL for a stored value; identity when absent. */
|
|
11
11
|
thumbnail?: (src: string) => string;
|
|
12
|
+
/** Whether a stored value names an image that no longer exists; the canvas shows a placeholder for it. */
|
|
13
|
+
missing?: (src: string) => boolean;
|
|
12
14
|
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** The wall-clock `YYYY-MM-DDTHH:mm` of an ISO instant in `timeZone`, or '' if it is not one. */
|
|
2
|
+
export declare function toWallClock(iso: string, timeZone?: string): string;
|
|
3
|
+
/** The ISO instant, with `timeZone`'s offset, of a `YYYY-MM-DDTHH:mm` wall clock there. */
|
|
4
|
+
export declare function fromWallClock(wallClock: string, timeZone?: string): string;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
function offsetMinutes(instant, timeZone) {
|
|
2
|
+
const parts = Object.fromEntries(new Intl.DateTimeFormat('en-US', {
|
|
3
|
+
timeZone,
|
|
4
|
+
hourCycle: 'h23',
|
|
5
|
+
year: 'numeric',
|
|
6
|
+
month: '2-digit',
|
|
7
|
+
day: '2-digit',
|
|
8
|
+
hour: '2-digit',
|
|
9
|
+
minute: '2-digit',
|
|
10
|
+
second: '2-digit'
|
|
11
|
+
})
|
|
12
|
+
.formatToParts(instant)
|
|
13
|
+
.map(({ type, value }) => [type, Number(value)]));
|
|
14
|
+
const wall = Date.UTC(parts.year, parts.month - 1, parts.day, parts.hour, parts.minute, parts.second);
|
|
15
|
+
return Math.round((wall - Math.floor(instant / 1000) * 1000) / 60_000);
|
|
16
|
+
}
|
|
17
|
+
function pad(n) {
|
|
18
|
+
return String(n).padStart(2, '0');
|
|
19
|
+
}
|
|
20
|
+
/** The wall-clock `YYYY-MM-DDTHH:mm` of an ISO instant in `timeZone`, or '' if it is not one. */
|
|
21
|
+
export function toWallClock(iso, timeZone) {
|
|
22
|
+
const instant = Date.parse(iso);
|
|
23
|
+
if (!iso || Number.isNaN(instant))
|
|
24
|
+
return '';
|
|
25
|
+
return new Date(instant + offsetMinutes(instant, timeZone) * 60_000).toISOString().slice(0, 16);
|
|
26
|
+
}
|
|
27
|
+
/** The ISO instant, with `timeZone`'s offset, of a `YYYY-MM-DDTHH:mm` wall clock there. */
|
|
28
|
+
export function fromWallClock(wallClock, timeZone) {
|
|
29
|
+
const wall = Date.parse(`${wallClock}Z`);
|
|
30
|
+
if (!wallClock || Number.isNaN(wall))
|
|
31
|
+
return '';
|
|
32
|
+
let instant = wall - offsetMinutes(wall, timeZone) * 60_000;
|
|
33
|
+
instant = wall - offsetMinutes(instant, timeZone) * 60_000;
|
|
34
|
+
const offset = offsetMinutes(instant, timeZone);
|
|
35
|
+
const sign = offset < 0 ? '-' : '+';
|
|
36
|
+
const zone = `${sign}${pad(Math.floor(Math.abs(offset) / 60))}:${pad(Math.abs(offset) % 60)}`;
|
|
37
|
+
return `${toWallClock(new Date(instant).toISOString(), timeZone)}:00${zone}`;
|
|
38
|
+
}
|
|
@@ -11,7 +11,10 @@ export interface ImagePreviews {
|
|
|
11
11
|
register(src: string, file: Blob): void;
|
|
12
12
|
revokeAll(): void;
|
|
13
13
|
}
|
|
14
|
-
|
|
14
|
+
/** Unique per value, so {@link ImagePreviews.sourceOf} can map a placeholder back. */
|
|
15
|
+
export declare function missingImagePlaceholder(src: string): string;
|
|
16
|
+
/** `placeholder` answers a stand-in URL for a value with no preview, e.g. a missing image. */
|
|
17
|
+
export declare function createImagePreviews(placeholder?: (src: string) => string | undefined): ImagePreviews;
|
|
15
18
|
/** The attrs a Block component renders on the canvas, with previews in place of image values. */
|
|
16
19
|
export declare function withImagePreviews(block: BlockDefinition, attrs: Record<string, unknown>, previews: ImagePreviews): Record<string, unknown>;
|
|
17
20
|
/**
|
|
@@ -1,9 +1,25 @@
|
|
|
1
1
|
import { attributeListFields, attributeListValueSpec, inferAttributeInputKind } from '../core/attributes.js';
|
|
2
|
-
|
|
2
|
+
const MISSING_IMAGE_SVG = `<svg xmlns="http://www.w3.org/2000/svg" width="320" height="200" viewBox="0 0 320 200"><rect width="320" height="200" fill="#f4f4f5" stroke="#d4d4d8" stroke-dasharray="6 4"/><text x="160" y="106" font-family="system-ui, sans-serif" font-size="16" fill="#71717a" text-anchor="middle">Missing image</text></svg>`;
|
|
3
|
+
/** Unique per value, so {@link ImagePreviews.sourceOf} can map a placeholder back. */
|
|
4
|
+
export function missingImagePlaceholder(src) {
|
|
5
|
+
return `data:image/svg+xml,${encodeURIComponent(MISSING_IMAGE_SVG)}#${encodeURIComponent(src)}`;
|
|
6
|
+
}
|
|
7
|
+
/** `placeholder` answers a stand-in URL for a value with no preview, e.g. a missing image. */
|
|
8
|
+
export function createImagePreviews(placeholder = () => undefined) {
|
|
3
9
|
const previews = new Map();
|
|
4
10
|
const sources = new Map();
|
|
5
11
|
return {
|
|
6
|
-
get
|
|
12
|
+
get(src) {
|
|
13
|
+
const known = previews.get(src);
|
|
14
|
+
if (known)
|
|
15
|
+
return known;
|
|
16
|
+
const url = placeholder(src);
|
|
17
|
+
if (url) {
|
|
18
|
+
previews.set(src, url);
|
|
19
|
+
sources.set(url, src);
|
|
20
|
+
}
|
|
21
|
+
return url;
|
|
22
|
+
},
|
|
7
23
|
sourceOf: (url) => sources.get(url),
|
|
8
24
|
register(src, file) {
|
|
9
25
|
// A re-upload answers the same src, so nothing re-renders and the canvas
|
|
@@ -13,7 +29,10 @@ export function createImagePreviews() {
|
|
|
13
29
|
previews.set(src, url);
|
|
14
30
|
},
|
|
15
31
|
revokeAll() {
|
|
16
|
-
sources.forEach((_src, url) =>
|
|
32
|
+
sources.forEach((_src, url) => {
|
|
33
|
+
if (url.startsWith('blob:'))
|
|
34
|
+
URL.revokeObjectURL(url);
|
|
35
|
+
});
|
|
17
36
|
sources.clear();
|
|
18
37
|
previews.clear();
|
|
19
38
|
}
|
package/dist/styles/controls.css
CHANGED
|
@@ -135,10 +135,15 @@
|
|
|
135
135
|
color: var(--uncial-color-text-muted);
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
-
.uncial-field__error
|
|
138
|
+
.uncial-field__error,
|
|
139
|
+
.uncial-field__warning {
|
|
139
140
|
font-size: 0.75rem;
|
|
140
141
|
}
|
|
141
142
|
|
|
143
|
+
.uncial-field__warning {
|
|
144
|
+
color: var(--uncial-color-accent);
|
|
145
|
+
}
|
|
146
|
+
|
|
142
147
|
.uncial-input,
|
|
143
148
|
.uncial-select,
|
|
144
149
|
.uncial-textarea {
|