uncial 3.0.0 → 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.
- package/README.md +6 -1
- package/dist/core/defineBlock.js +5 -2
- package/dist/core/types.d.ts +1 -1
- package/dist/editor/AttributeFieldControl.svelte +127 -3
- package/dist/editor/AttributeFieldControl.svelte.d.ts +4 -0
- package/dist/editor/BlockAttributesPanel.svelte +12 -3
- package/dist/editor/BlockAttributesPanel.svelte.d.ts +5 -0
- package/dist/editor/BlockNodeView.svelte +2 -1
- package/dist/editor/DocumentMetaPanel.svelte +5 -1
- package/dist/editor/Editor.svelte +20 -8
- package/dist/editor/Editor.svelte.d.ts +3 -0
- package/dist/editor/ImagePicker.svelte +85 -0
- package/dist/editor/ImagePicker.svelte.d.ts +13 -0
- package/dist/editor/bindEditor.d.ts +3 -0
- package/dist/editor/bindEditor.js +1 -1
- package/dist/editor/imageSource.d.ts +12 -0
- package/dist/editor/imageSource.js +1 -0
- package/dist/editor/index.d.ts +1 -0
- package/dist/editor/metaController.js +0 -5
- package/dist/render/SuperscriptMarkFixture.svelte +1 -1
- package/dist/render/SuperscriptMarkFixture.svelte.d.ts +1 -1
- package/dist/render/Tabs.svelte +1 -1
- package/dist/render/index.d.ts +1 -0
- package/dist/render/index.js +1 -0
- package/dist/render/resolveImageSrc.d.ts +6 -0
- package/dist/render/resolveImageSrc.js +10 -0
- package/dist/render/tabSelection.svelte.js +2 -0
- package/dist/shared/ImageBlockFixture.svelte +29 -0
- package/dist/shared/ImageBlockFixture.svelte.d.ts +11 -0
- package/dist/shared/imagePreviews.d.ts +21 -0
- package/dist/shared/imagePreviews.js +59 -0
- package/dist/shared/tiptap.d.ts +2 -1
- package/dist/shared/tiptap.js +16 -13
- package/dist/styles/base.css +89 -9
- package/dist/ui/ThemeSwitcher.svelte +1 -1
- package/dist/ui/theme.svelte.js +1 -1
- package/dist/web-components/UncialEditor.svelte +4 -0
- package/dist/web-components/UncialEditor.svelte.d.ts +2 -1
- package/dist/web-components/index.d.ts +4 -1
- package/dist/web-components/index.js +6 -0
- 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
|
|
package/dist/core/defineBlock.js
CHANGED
|
@@ -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) &&
|
|
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
|
}
|
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' | (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;
|
|
@@ -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 {
|
|
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) &&
|
|
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
|
|
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>;
|
|
@@ -61,7 +61,11 @@
|
|
|
61
61
|
>
|
|
62
62
|
Reset
|
|
63
63
|
</button>
|
|
64
|
-
<button
|
|
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
|
|
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>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ImagePreviews } from '../shared/imagePreviews.js';
|
|
2
|
+
interface Props {
|
|
3
|
+
browse: () => Promise<string[]>;
|
|
4
|
+
thumbnail?: (src: string) => string;
|
|
5
|
+
imagePreviews?: ImagePreviews;
|
|
6
|
+
current: string;
|
|
7
|
+
onPick: (value: string) => void;
|
|
8
|
+
}
|
|
9
|
+
declare const ImagePicker: import("svelte").Component<Props, {
|
|
10
|
+
open: () => void;
|
|
11
|
+
}, "">;
|
|
12
|
+
type ImagePicker = ReturnType<typeof ImagePicker>;
|
|
13
|
+
export default ImagePicker;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Editor as TiptapEditor, type AnyExtension, type JSONContent } from '@tiptap/core';
|
|
2
2
|
import type { ActionReturn } from 'svelte/action';
|
|
3
3
|
import type { BlockDefinition, BlockRegistry, ContentSchema, ValidationIssue } from '../core/types.js';
|
|
4
|
+
import type { ImagePreviews } from '../shared/imagePreviews.js';
|
|
4
5
|
import { type BlockAttributesController } from './attributesController.js';
|
|
5
6
|
export interface BindEditorOptions {
|
|
6
7
|
blocks?: BlockRegistry | BlockDefinition[];
|
|
@@ -16,6 +17,8 @@ export interface BindEditorOptions {
|
|
|
16
17
|
* gutter label opens it.
|
|
17
18
|
*/
|
|
18
19
|
autoOpenAttributes?: boolean;
|
|
20
|
+
/** Previews the canvas shows in place of just-uploaded `input: 'image'` values. */
|
|
21
|
+
imagePreviews?: ImagePreviews;
|
|
19
22
|
onIssue?: (issue: ValidationIssue) => void;
|
|
20
23
|
onChange?: (json: JSONContent) => void;
|
|
21
24
|
onMetaChange?: (meta: Record<string, unknown>) => void;
|
|
@@ -159,7 +159,7 @@ export function bindEditor(node, initialOptions = {}) {
|
|
|
159
159
|
}
|
|
160
160
|
}
|
|
161
161
|
function createEditor() {
|
|
162
|
-
const extensions = createEditorExtensions(registry, schema, (pos) => controller.openAttributesAt(pos), options.extensions ?? []);
|
|
162
|
+
const extensions = createEditorExtensions(registry, schema, (pos) => controller.openAttributesAt(pos), options.extensions ?? [], options.imagePreviews);
|
|
163
163
|
const initialContent = normalizeAndValidate(options.json ?? emptyDocument());
|
|
164
164
|
rememberReadOnlyAttrs(initialContent);
|
|
165
165
|
const sanitized = sanitizeForEditorSchema(toEditorDocument(initialContent), getSchema(extensions));
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where an `input: 'image'` attribute's values come from. The field offers
|
|
3
|
+
* only the actions the host supplies.
|
|
4
|
+
*/
|
|
5
|
+
export interface ImageSource {
|
|
6
|
+
/** Commit a file and answer the value to store. */
|
|
7
|
+
upload?: (file: File) => Promise<string>;
|
|
8
|
+
/** Values of existing images to choose from. */
|
|
9
|
+
browse?: () => Promise<string[]>;
|
|
10
|
+
/** A displayable URL for a stored value; identity when absent. */
|
|
11
|
+
thumbnail?: (src: string) => string;
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/editor/index.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export { createBlockAttributesController, createInitialState } from './attribute
|
|
|
10
10
|
export { createDocumentMetaController } from './metaController.js';
|
|
11
11
|
export { CHOOSE_ATTRIBUTE_EVENT } from './chooseAttribute.js';
|
|
12
12
|
export type { ChooseAttributeRequest } from './chooseAttribute.js';
|
|
13
|
+
export type { ImageSource } from './imageSource.js';
|
|
13
14
|
export { builtinToolbarFeatures, defaultToolbarFeatures, resolveToolbarFeatures } from './toolbarFeatures.js';
|
|
14
15
|
export type { BindEditorOptions } from './bindEditor.js';
|
|
15
16
|
export type { ToolbarFeature, ToolbarFeatureContext, ToolbarFeatureId, ToolbarFeatureSelection } from './toolbarFeatures.js';
|
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
import { get, writable } from 'svelte/store';
|
|
2
2
|
import { parseMetaDraftValues, toMetaDraftValues, validateMeta } from '../core/meta.js';
|
|
3
|
-
const INITIAL_STATE = {
|
|
4
|
-
draft: {},
|
|
5
|
-
errors: {},
|
|
6
|
-
dirty: false
|
|
7
|
-
};
|
|
8
3
|
function errorsFromIssues(issues) {
|
|
9
4
|
const errors = {};
|
|
10
5
|
for (const issue of issues) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { MarkComponentProps } from '../core/types.js';
|
|
2
|
-
declare const SuperscriptMarkFixture: import("svelte").Component<MarkComponentProps, {}, "">;
|
|
2
|
+
declare const SuperscriptMarkFixture: import("svelte").Component<Pick<MarkComponentProps, "children">, {}, "">;
|
|
3
3
|
type SuperscriptMarkFixture = ReturnType<typeof SuperscriptMarkFixture>;
|
|
4
4
|
export default SuperscriptMarkFixture;
|
package/dist/render/Tabs.svelte
CHANGED
package/dist/render/index.d.ts
CHANGED
|
@@ -3,3 +3,4 @@ export { default as RichText } from './RichText.svelte';
|
|
|
3
3
|
export { default as Tab } from './Tab.svelte';
|
|
4
4
|
export { default as Tabs } from './Tabs.svelte';
|
|
5
5
|
export { getCodeLanguageClass, highlightCodeToHtml } from '../shared/syntaxHighlight.js';
|
|
6
|
+
export { resolveImageSrc } from './resolveImageSrc.js';
|
package/dist/render/index.js
CHANGED
|
@@ -6,3 +6,4 @@ export { default as Tabs } from './Tabs.svelte';
|
|
|
6
6
|
// a document — has to read the same as a fence the renderer produced, so the
|
|
7
7
|
// highlighter the renderer uses is part of the render surface.
|
|
8
8
|
export { getCodeLanguageClass, highlightCodeToHtml } from '../shared/syntaxHighlight.js';
|
|
9
|
+
export { resolveImageSrc } from './resolveImageSrc.js';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Applies a SvelteKit-style `base` (`''` or `/sub`) to a root-relative image `src`.
|
|
3
|
+
* Anything else — `blob:` editor previews, `data:` URIs, protocol-relative and
|
|
4
|
+
* absolute URLs, relative paths — is already displayable and passes through.
|
|
5
|
+
*/
|
|
6
|
+
export declare function resolveImageSrc(src: string, base: string): string;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Applies a SvelteKit-style `base` (`''` or `/sub`) to a root-relative image `src`.
|
|
3
|
+
* Anything else — `blob:` editor previews, `data:` URIs, protocol-relative and
|
|
4
|
+
* absolute URLs, relative paths — is already displayable and passes through.
|
|
5
|
+
*/
|
|
6
|
+
export function resolveImageSrc(src, base) {
|
|
7
|
+
if (!src.startsWith('/') || src.startsWith('//'))
|
|
8
|
+
return src;
|
|
9
|
+
return base.replace(/\/+$/, '') + src;
|
|
10
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
// Test fixture: a block that renders exactly the image values it is handed.
|
|
3
|
+
interface Props {
|
|
4
|
+
src?: string;
|
|
5
|
+
items?: Array<{ image?: string; caption?: string }>;
|
|
6
|
+
updateAttributes?: (attrs: Record<string, unknown>) => void;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
let { src = '', items = [], updateAttributes }: Props = $props();
|
|
10
|
+
</script>
|
|
11
|
+
|
|
12
|
+
<div data-testid="image-block-fixture">
|
|
13
|
+
{#if src}
|
|
14
|
+
<img data-testid="canvas-image" {src} alt="" />
|
|
15
|
+
{/if}
|
|
16
|
+
{#each items as item, index (index)}
|
|
17
|
+
{#if item.image}
|
|
18
|
+
<img data-testid="canvas-gallery-image" src={item.image} alt="" />
|
|
19
|
+
{/if}
|
|
20
|
+
{/each}
|
|
21
|
+
<button
|
|
22
|
+
type="button"
|
|
23
|
+
data-testid="edit-captions"
|
|
24
|
+
onclick={() =>
|
|
25
|
+
updateAttributes?.({ items: items.map((item) => ({ ...item, caption: 'edited' })) })}
|
|
26
|
+
>
|
|
27
|
+
Edit captions
|
|
28
|
+
</button>
|
|
29
|
+
</div>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
interface Props {
|
|
2
|
+
src?: string;
|
|
3
|
+
items?: Array<{
|
|
4
|
+
image?: string;
|
|
5
|
+
caption?: string;
|
|
6
|
+
}>;
|
|
7
|
+
updateAttributes?: (attrs: Record<string, unknown>) => void;
|
|
8
|
+
}
|
|
9
|
+
declare const ImageBlockFixture: import("svelte").Component<Props, {}, "">;
|
|
10
|
+
type ImageBlockFixture = ReturnType<typeof ImageBlockFixture>;
|
|
11
|
+
export default ImageBlockFixture;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { BlockDefinition } from '../core/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Local `blob:` previews of images uploaded this session, keyed by the value
|
|
4
|
+
* the image source returned. An uploaded file is not served until the site
|
|
5
|
+
* redeploys, so the canvas shows the preview in the meantime.
|
|
6
|
+
*/
|
|
7
|
+
export interface ImagePreviews {
|
|
8
|
+
get(src: string): string | undefined;
|
|
9
|
+
/** The value a preview URL stands in for. */
|
|
10
|
+
sourceOf(url: string): string | undefined;
|
|
11
|
+
register(src: string, file: Blob): void;
|
|
12
|
+
revokeAll(): void;
|
|
13
|
+
}
|
|
14
|
+
export declare function createImagePreviews(): ImagePreviews;
|
|
15
|
+
/** The attrs a Block component renders on the canvas, with previews in place of image values. */
|
|
16
|
+
export declare function withImagePreviews(block: BlockDefinition, attrs: Record<string, unknown>, previews: ImagePreviews): Record<string, unknown>;
|
|
17
|
+
/**
|
|
18
|
+
* The attrs a Block component writes back, with its previews turned into the
|
|
19
|
+
* values they stand in for, so a preview is never stored.
|
|
20
|
+
*/
|
|
21
|
+
export declare function withoutImagePreviews(block: BlockDefinition, attrs: Record<string, unknown>, previews: ImagePreviews): Record<string, unknown>;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { attributeListFields, attributeListValueSpec, inferAttributeInputKind } from '../core/attributes.js';
|
|
2
|
+
export function createImagePreviews() {
|
|
3
|
+
const previews = new Map();
|
|
4
|
+
const sources = new Map();
|
|
5
|
+
return {
|
|
6
|
+
get: (src) => previews.get(src),
|
|
7
|
+
sourceOf: (url) => sources.get(url),
|
|
8
|
+
register(src, file) {
|
|
9
|
+
// A re-upload answers the same src, so nothing re-renders and the canvas
|
|
10
|
+
// and field keep the earlier URL; it is not revoked until teardown.
|
|
11
|
+
const url = URL.createObjectURL(file);
|
|
12
|
+
sources.set(url, src);
|
|
13
|
+
previews.set(src, url);
|
|
14
|
+
},
|
|
15
|
+
revokeAll() {
|
|
16
|
+
sources.forEach((_src, url) => URL.revokeObjectURL(url));
|
|
17
|
+
sources.clear();
|
|
18
|
+
previews.clear();
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function substituteValue(spec, value, lookup) {
|
|
23
|
+
const kind = inferAttributeInputKind(spec);
|
|
24
|
+
if (kind === 'image') {
|
|
25
|
+
return typeof value === 'string' ? (lookup(value) ?? value) : value;
|
|
26
|
+
}
|
|
27
|
+
if (kind !== 'list' || !spec.list || !Array.isArray(value))
|
|
28
|
+
return value;
|
|
29
|
+
const valueSpec = attributeListValueSpec(spec.list);
|
|
30
|
+
if (valueSpec)
|
|
31
|
+
return value.map((item) => substituteValue(valueSpec, item, lookup));
|
|
32
|
+
const fields = attributeListFields(spec.list);
|
|
33
|
+
return value.map((item) => {
|
|
34
|
+
if (typeof item !== 'object' || item === null)
|
|
35
|
+
return item;
|
|
36
|
+
const record = item;
|
|
37
|
+
return Object.fromEntries(Object.entries(record).map(([name, fieldValue]) => {
|
|
38
|
+
const fieldSpec = fields.find(([fieldName]) => fieldName === name)?.[1];
|
|
39
|
+
return [name, fieldSpec ? substituteValue(fieldSpec, fieldValue, lookup) : fieldValue];
|
|
40
|
+
}));
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function substituteAttrs(block, attrs, lookup) {
|
|
44
|
+
return Object.fromEntries(Object.entries(attrs).map(([name, value]) => {
|
|
45
|
+
const spec = block.attributes[name];
|
|
46
|
+
return [name, spec ? substituteValue(spec, value, lookup) : value];
|
|
47
|
+
}));
|
|
48
|
+
}
|
|
49
|
+
/** The attrs a Block component renders on the canvas, with previews in place of image values. */
|
|
50
|
+
export function withImagePreviews(block, attrs, previews) {
|
|
51
|
+
return substituteAttrs(block, attrs, (value) => previews.get(value));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The attrs a Block component writes back, with its previews turned into the
|
|
55
|
+
* values they stand in for, so a preview is never stored.
|
|
56
|
+
*/
|
|
57
|
+
export function withoutImagePreviews(block, attrs, previews) {
|
|
58
|
+
return substituteAttrs(block, attrs, (value) => previews.sourceOf(value));
|
|
59
|
+
}
|
package/dist/shared/tiptap.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { type AnyExtension } from '@tiptap/core';
|
|
2
2
|
import type { BlockDefinition, BlockRegistry, ContentSchema } from '../core/types.js';
|
|
3
3
|
import type { RichTextFeature } from '../core/types.js';
|
|
4
|
+
import { type ImagePreviews } from './imagePreviews.js';
|
|
4
5
|
export type BlockActivationCallback = (pos: number) => void;
|
|
5
6
|
export declare function getBlockDefaultAttrs(block: BlockDefinition): Record<string, unknown>;
|
|
6
7
|
export declare function createRichTextExtensions(features: ReadonlySet<RichTextFeature>): AnyExtension[];
|
|
7
|
-
export declare function createEditorExtensions(blocks?: BlockRegistry | BlockDefinition[], schema?: ContentSchema, onActivateBlock?: BlockActivationCallback, extensions?: AnyExtension[]): AnyExtension[];
|
|
8
|
+
export declare function createEditorExtensions(blocks?: BlockRegistry | BlockDefinition[], schema?: ContentSchema, onActivateBlock?: BlockActivationCallback, extensions?: AnyExtension[], imagePreviews?: ImagePreviews): AnyExtension[];
|
package/dist/shared/tiptap.js
CHANGED
|
@@ -7,6 +7,7 @@ import { DEFAULT_MARKS, resolveRegistry } from '../core/registry.js';
|
|
|
7
7
|
import { coerceAttributeValue, normalizeBlockAttributes, serializeBlockAttributes } from '../core/attributes.js';
|
|
8
8
|
import { lowlight } from './syntaxHighlight.js';
|
|
9
9
|
import { sanitizeHref } from '../render/sanitize.js';
|
|
10
|
+
import { withImagePreviews, withoutImagePreviews } from './imagePreviews.js';
|
|
10
11
|
function safeParseJSON(raw) {
|
|
11
12
|
if (!raw)
|
|
12
13
|
return {};
|
|
@@ -21,9 +22,10 @@ function safeParseJSON(raw) {
|
|
|
21
22
|
export function getBlockDefaultAttrs(block) {
|
|
22
23
|
return normalizeBlockAttributes(block, {});
|
|
23
24
|
}
|
|
24
|
-
function buildBlockEditorProps(block, node, editor, onActivate, getPos) {
|
|
25
|
+
function buildBlockEditorProps(block, node, editor, onActivate, getPos, imagePreviews) {
|
|
26
|
+
const attrs = node.attrs ?? {};
|
|
25
27
|
return {
|
|
26
|
-
attrs:
|
|
28
|
+
attrs: imagePreviews ? withImagePreviews(block, attrs, imagePreviews) : attrs,
|
|
27
29
|
content: (node.content?.toJSON() ?? []),
|
|
28
30
|
blockId: block.id,
|
|
29
31
|
label: block.label,
|
|
@@ -52,7 +54,8 @@ function buildBlockEditorProps(block, node, editor, onActivate, getPos) {
|
|
|
52
54
|
const current = editor.state.doc.nodeAt(pos);
|
|
53
55
|
if (!current)
|
|
54
56
|
return;
|
|
55
|
-
|
|
57
|
+
const stored = imagePreviews ? withoutImagePreviews(block, attrs, imagePreviews) : attrs;
|
|
58
|
+
editor.view.dispatch(editor.state.tr.setNodeMarkup(pos, undefined, { ...current.attrs, ...stored }));
|
|
56
59
|
},
|
|
57
60
|
// ProseMirror's getPos can return undefined when the node is not currently
|
|
58
61
|
// in the document (e.g. mid-transaction); skip activation in that case
|
|
@@ -66,7 +69,7 @@ function buildBlockEditorProps(block, node, editor, onActivate, getPos) {
|
|
|
66
69
|
: undefined
|
|
67
70
|
};
|
|
68
71
|
}
|
|
69
|
-
function mountBlockEditorComponent(block, node, container, contentDOM, mounted, editor, onActivate, getPos) {
|
|
72
|
+
function mountBlockEditorComponent(block, node, container, contentDOM, mounted, editor, onActivate, getPos, imagePreviews) {
|
|
70
73
|
mounted?.destroy();
|
|
71
74
|
const createEditorMount = block.components.editor.plugin.createEditorMount;
|
|
72
75
|
if (!createEditorMount) {
|
|
@@ -77,7 +80,7 @@ function mountBlockEditorComponent(block, node, container, contentDOM, mounted,
|
|
|
77
80
|
inline: block.behaviors.inline ?? false,
|
|
78
81
|
component: block.components.editor,
|
|
79
82
|
contentDOM,
|
|
80
|
-
props: buildBlockEditorProps(block, node, editor, onActivate, getPos)
|
|
83
|
+
props: buildBlockEditorProps(block, node, editor, onActivate, getPos, imagePreviews)
|
|
81
84
|
});
|
|
82
85
|
}
|
|
83
86
|
function isDragHandleEvent(event) {
|
|
@@ -90,7 +93,7 @@ function isInteractiveNodeViewEvent(event) {
|
|
|
90
93
|
return false;
|
|
91
94
|
return Boolean(target.closest('input, textarea, select, button:not([data-drag-handle]), a[href]'));
|
|
92
95
|
}
|
|
93
|
-
function createBlockNodeView(block, node, getPos, onActivate, editor) {
|
|
96
|
+
function createBlockNodeView(block, node, getPos, onActivate, editor, imagePreviews) {
|
|
94
97
|
const dom = document.createElement(block.behaviors.inline ? 'span' : 'div');
|
|
95
98
|
dom.className = 'uncial-nodeview';
|
|
96
99
|
dom.dataset.uncialBlockId = block.id;
|
|
@@ -111,7 +114,7 @@ function createBlockNodeView(block, node, getPos, onActivate, editor) {
|
|
|
111
114
|
contentDOM.className = 'uncial-nodeview-content';
|
|
112
115
|
}
|
|
113
116
|
let mounted = null;
|
|
114
|
-
mounted = mountBlockEditorComponent(block, node, dom, contentDOM, mounted, editor, onActivate, getPos);
|
|
117
|
+
mounted = mountBlockEditorComponent(block, node, dom, contentDOM, mounted, editor, onActivate, getPos, imagePreviews);
|
|
115
118
|
syncBlockPosition();
|
|
116
119
|
return {
|
|
117
120
|
dom,
|
|
@@ -136,12 +139,12 @@ function createBlockNodeView(block, node, getPos, onActivate, editor) {
|
|
|
136
139
|
// reach it — the block would keep rendering its old attributes until
|
|
137
140
|
// something else re-rendered it. A microtask is outside that window
|
|
138
141
|
// and still lands before paint.
|
|
139
|
-
const nextProps = buildBlockEditorProps(block, nextNode, editor, onActivate, getPos);
|
|
142
|
+
const nextProps = buildBlockEditorProps(block, nextNode, editor, onActivate, getPos, imagePreviews);
|
|
140
143
|
queueMicrotask(() => mounted?.update?.(nextProps));
|
|
141
144
|
}
|
|
142
145
|
else {
|
|
143
146
|
// Fall back to a full remount for runtimes without update support.
|
|
144
|
-
mounted = mountBlockEditorComponent(block, nextNode, dom, contentDOM, mounted, editor, onActivate, getPos);
|
|
147
|
+
mounted = mountBlockEditorComponent(block, nextNode, dom, contentDOM, mounted, editor, onActivate, getPos, imagePreviews);
|
|
145
148
|
}
|
|
146
149
|
return true;
|
|
147
150
|
},
|
|
@@ -186,7 +189,7 @@ function containerContentExpression(content) {
|
|
|
186
189
|
return 'block*';
|
|
187
190
|
}
|
|
188
191
|
}
|
|
189
|
-
function createBlockNodeExtension(block, onActivate) {
|
|
192
|
+
function createBlockNodeExtension(block, onActivate, imagePreviews) {
|
|
190
193
|
const isContainer = Boolean(block.content);
|
|
191
194
|
return Node.create({
|
|
192
195
|
name: block.id,
|
|
@@ -223,7 +226,7 @@ function createBlockNodeExtension(block, onActivate) {
|
|
|
223
226
|
return [{ tag: block.html?.parseTag ?? `div[data-uncial-block="${block.id}"]` }];
|
|
224
227
|
},
|
|
225
228
|
addNodeView() {
|
|
226
|
-
return ({ node, getPos, editor }) => createBlockNodeView(block, node, getPos, onActivate, editor);
|
|
229
|
+
return ({ node, getPos, editor }) => createBlockNodeView(block, node, getPos, onActivate, editor, imagePreviews);
|
|
227
230
|
},
|
|
228
231
|
renderHTML({ HTMLAttributes, node }) {
|
|
229
232
|
const normalizedAttrs = normalizeBlockAttributes(block, node.attrs ?? {});
|
|
@@ -375,11 +378,11 @@ export function createRichTextExtensions(features) {
|
|
|
375
378
|
...(features.has('codeBlock') ? [CodeBlockLowlight.configure({ lowlight })] : [])
|
|
376
379
|
];
|
|
377
380
|
}
|
|
378
|
-
export function createEditorExtensions(blocks = [], schema, onActivateBlock, extensions = []) {
|
|
381
|
+
export function createEditorExtensions(blocks = [], schema, onActivateBlock, extensions = [], imagePreviews) {
|
|
379
382
|
const registry = resolveRegistry(blocks);
|
|
380
383
|
const blockExtensions = registry.blocks
|
|
381
384
|
.filter((block) => !schema || schema.allowedBlocks.has(block.id))
|
|
382
|
-
.map((block) => createBlockNodeExtension(block, onActivateBlock));
|
|
385
|
+
.map((block) => createBlockNodeExtension(block, onActivateBlock, imagePreviews));
|
|
383
386
|
return [
|
|
384
387
|
...createBaseExtensions(schema),
|
|
385
388
|
createDocumentIdentityExtension(registry),
|
package/dist/styles/base.css
CHANGED
|
@@ -396,6 +396,25 @@
|
|
|
396
396
|
gap: var(--uncial-space-2);
|
|
397
397
|
}
|
|
398
398
|
|
|
399
|
+
.uncial-image-field {
|
|
400
|
+
display: grid;
|
|
401
|
+
gap: var(--uncial-space-2);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
.uncial-image-field__thumbnail {
|
|
405
|
+
max-width: 100%;
|
|
406
|
+
max-height: 8rem;
|
|
407
|
+
object-fit: contain;
|
|
408
|
+
justify-self: start;
|
|
409
|
+
border: 1px solid var(--uncial-color-border);
|
|
410
|
+
border-radius: var(--uncial-radius-lg);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
.uncial-image-field__actions {
|
|
414
|
+
display: flex;
|
|
415
|
+
gap: var(--uncial-space-2);
|
|
416
|
+
}
|
|
417
|
+
|
|
399
418
|
.uncial-list-item {
|
|
400
419
|
border: 1px solid var(--uncial-color-border);
|
|
401
420
|
border-radius: var(--uncial-radius-lg);
|
|
@@ -471,16 +490,18 @@
|
|
|
471
490
|
-1 *
|
|
472
491
|
(
|
|
473
492
|
max(
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
493
|
+
var(--uncial-gutter-width-sm),
|
|
494
|
+
var(--uncial-reserved-gutter-width, var(--uncial-gutter-width-sm))
|
|
495
|
+
) -
|
|
496
|
+
0.5rem
|
|
477
497
|
)
|
|
478
498
|
);
|
|
479
499
|
width: calc(
|
|
480
500
|
max(
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
501
|
+
var(--uncial-gutter-width-sm),
|
|
502
|
+
var(--uncial-reserved-gutter-width, var(--uncial-gutter-width-sm))
|
|
503
|
+
) -
|
|
504
|
+
1rem
|
|
484
505
|
);
|
|
485
506
|
}
|
|
486
507
|
|
|
@@ -488,9 +509,10 @@
|
|
|
488
509
|
font-size: 0.5rem;
|
|
489
510
|
max-width: calc(
|
|
490
511
|
max(
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
512
|
+
var(--uncial-gutter-width-sm),
|
|
513
|
+
var(--uncial-reserved-gutter-width, var(--uncial-gutter-width-sm))
|
|
514
|
+
) -
|
|
515
|
+
1rem
|
|
494
516
|
);
|
|
495
517
|
}
|
|
496
518
|
|
|
@@ -600,3 +622,61 @@
|
|
|
600
622
|
border-top: 1px solid var(--uncial-color-border);
|
|
601
623
|
}
|
|
602
624
|
}
|
|
625
|
+
|
|
626
|
+
.uncial-image-picker {
|
|
627
|
+
width: min(40rem, calc(100vw - 2rem));
|
|
628
|
+
max-height: calc(100vh - 4rem);
|
|
629
|
+
padding: var(--uncial-space-4);
|
|
630
|
+
border: 1px solid var(--uncial-color-border);
|
|
631
|
+
border-radius: var(--uncial-radius-lg);
|
|
632
|
+
background: var(--uncial-color-surface);
|
|
633
|
+
color: var(--uncial-color-text);
|
|
634
|
+
box-shadow: var(--uncial-shadow-md);
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
.uncial-image-picker::backdrop {
|
|
638
|
+
background: color-mix(in srgb, black 40%, transparent);
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
.uncial-image-picker__head {
|
|
642
|
+
display: flex;
|
|
643
|
+
align-items: center;
|
|
644
|
+
justify-content: space-between;
|
|
645
|
+
margin-bottom: var(--uncial-space-3);
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
.uncial-image-picker__head h2 {
|
|
649
|
+
margin: 0;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
.uncial-image-picker__grid {
|
|
653
|
+
display: grid;
|
|
654
|
+
grid-template-columns: repeat(auto-fill, minmax(7rem, 1fr));
|
|
655
|
+
gap: var(--uncial-space-2);
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
.uncial-image-picker__tile {
|
|
659
|
+
aspect-ratio: 1;
|
|
660
|
+
padding: 0;
|
|
661
|
+
overflow: hidden;
|
|
662
|
+
border: 1px solid var(--uncial-color-border);
|
|
663
|
+
border-radius: var(--uncial-radius-md);
|
|
664
|
+
background: var(--uncial-color-surface-elevated);
|
|
665
|
+
cursor: pointer;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
.uncial-image-picker__tile:focus-visible {
|
|
669
|
+
outline: 2px solid var(--uncial-color-focus-ring);
|
|
670
|
+
outline-offset: 2px;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
.uncial-image-picker__tile[aria-pressed='true'] {
|
|
674
|
+
border-color: var(--uncial-color-primary);
|
|
675
|
+
box-shadow: 0 0 0 2px var(--uncial-color-primary);
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
.uncial-image-picker__tile img {
|
|
679
|
+
width: 100%;
|
|
680
|
+
height: 100%;
|
|
681
|
+
object-fit: cover;
|
|
682
|
+
}
|
package/dist/ui/theme.svelte.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import Editor from '../editor/Editor.svelte';
|
|
4
4
|
import type {
|
|
5
5
|
BlockAttributesController,
|
|
6
|
+
ImageSource,
|
|
6
7
|
ToolbarFeature,
|
|
7
8
|
ToolbarFeatureSelection
|
|
8
9
|
} from '../editor/index.js';
|
|
@@ -28,6 +29,7 @@
|
|
|
28
29
|
attributesController?: BlockAttributesController | null;
|
|
29
30
|
attributesPanel?: boolean | 'docked' | 'overlay' | 'off';
|
|
30
31
|
presentation?: 'card' | 'bare';
|
|
32
|
+
imageSource?: ImageSource;
|
|
31
33
|
onIssue?: (issue: ValidationIssue) => void;
|
|
32
34
|
onChange?: (document: JSONContent) => void;
|
|
33
35
|
onMetaChange?: (meta: Record<string, unknown>) => void;
|
|
@@ -45,6 +47,7 @@
|
|
|
45
47
|
attributesController = null,
|
|
46
48
|
attributesPanel = true,
|
|
47
49
|
presentation = 'card',
|
|
50
|
+
imageSource,
|
|
48
51
|
onIssue,
|
|
49
52
|
onChange,
|
|
50
53
|
onMetaChange
|
|
@@ -70,6 +73,7 @@
|
|
|
70
73
|
{attributesController}
|
|
71
74
|
{attributesPanel}
|
|
72
75
|
{presentation}
|
|
76
|
+
{imageSource}
|
|
73
77
|
onIssue={dispatchIssue}
|
|
74
78
|
{onChange}
|
|
75
79
|
{onMetaChange}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { AnyExtension, JSONContent } from '@tiptap/core';
|
|
2
|
-
import type { BlockAttributesController, ToolbarFeature, ToolbarFeatureSelection } from '../editor/index.js';
|
|
2
|
+
import type { BlockAttributesController, ImageSource, ToolbarFeature, ToolbarFeatureSelection } from '../editor/index.js';
|
|
3
3
|
import type { AttributeSpec, BlockDefinition, BlockRegistry, ContentSchema, DocumentMetaSchema, ValidationIssue } from '../core/types.js';
|
|
4
4
|
interface Props {
|
|
5
5
|
blocks?: BlockRegistry | BlockDefinition[];
|
|
@@ -13,6 +13,7 @@ interface Props {
|
|
|
13
13
|
attributesController?: BlockAttributesController | null;
|
|
14
14
|
attributesPanel?: boolean | 'docked' | 'overlay' | 'off';
|
|
15
15
|
presentation?: 'card' | 'bare';
|
|
16
|
+
imageSource?: ImageSource;
|
|
16
17
|
onIssue?: (issue: ValidationIssue) => void;
|
|
17
18
|
onChange?: (document: JSONContent) => void;
|
|
18
19
|
onMetaChange?: (meta: Record<string, unknown>) => void;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import UncialEditor from './UncialEditor.svelte';
|
|
2
2
|
import UncialRenderer from './UncialRenderer.svelte';
|
|
3
3
|
import type { AnyExtension, JSONContent } from '@tiptap/core';
|
|
4
|
-
import type { BlockAttributesController, ToolbarFeature, ToolbarFeatureSelection } from '../editor/index.js';
|
|
4
|
+
import type { BlockAttributesController, ImageSource, ToolbarFeature, ToolbarFeatureSelection } from '../editor/index.js';
|
|
5
5
|
import type { AttributeSpec, BlockDefinition, BlockRegistry, ContentSchema, DocumentMetaSchema, ValidationIssue } from '../core/types.js';
|
|
6
6
|
type EditorProps = {
|
|
7
7
|
blocks?: BlockRegistry | BlockDefinition[];
|
|
@@ -15,6 +15,7 @@ type EditorProps = {
|
|
|
15
15
|
attributesController?: BlockAttributesController | null;
|
|
16
16
|
attributesPanel?: boolean | 'docked' | 'overlay' | 'off';
|
|
17
17
|
presentation?: 'card' | 'bare';
|
|
18
|
+
imageSource?: ImageSource;
|
|
18
19
|
};
|
|
19
20
|
type EditorHostProps = EditorProps & {
|
|
20
21
|
onIssue?: (issue: ValidationIssue) => void;
|
|
@@ -82,6 +83,8 @@ declare class UncialEditorElement extends UncialElement<EditorHostProps> {
|
|
|
82
83
|
set attributesPanel(value: EditorProps['attributesPanel']);
|
|
83
84
|
get presentation(): EditorProps['presentation'];
|
|
84
85
|
set presentation(value: EditorProps['presentation']);
|
|
86
|
+
get imageSource(): EditorProps['imageSource'];
|
|
87
|
+
set imageSource(value: EditorProps['imageSource']);
|
|
85
88
|
protected mountComponent(target: ShadowRoot): Record<string, unknown>;
|
|
86
89
|
}
|
|
87
90
|
declare class UncialRendererElement extends UncialElement<RendererHostProps> {
|
|
@@ -149,6 +149,12 @@ class UncialEditorElement extends UncialElement {
|
|
|
149
149
|
set presentation(value) {
|
|
150
150
|
this.setProp('presentation', value);
|
|
151
151
|
}
|
|
152
|
+
get imageSource() {
|
|
153
|
+
return this.props.imageSource;
|
|
154
|
+
}
|
|
155
|
+
set imageSource(value) {
|
|
156
|
+
this.setProp('imageSource', value);
|
|
157
|
+
}
|
|
152
158
|
mountComponent(target) {
|
|
153
159
|
return mount(UncialEditor, { target, props: this.props });
|
|
154
160
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "uncial",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -126,8 +126,8 @@
|
|
|
126
126
|
"release": "pnpm run prepack && changeset publish",
|
|
127
127
|
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
|
128
128
|
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
|
|
129
|
-
"lint": "prettier --check . && eslint .",
|
|
130
|
-
"format": "prettier --write .",
|
|
129
|
+
"lint": "prettier --check . --ignore-path ../../.gitignore --ignore-path ../../.prettierignore && eslint .",
|
|
130
|
+
"format": "prettier --write . --ignore-path ../../.gitignore --ignore-path ../../.prettierignore",
|
|
131
131
|
"test:unit": "vitest --project server",
|
|
132
132
|
"test:browser": "vitest --project client",
|
|
133
133
|
"test": "pnpm run test:unit -- --run && pnpm run test:browser -- --run && pnpm run test:e2e",
|