rimecms 1.1.2 → 1.1.4
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/adapter-sqlite/relations.server.d.ts +4 -4
- package/dist/core/adapter.d.ts +4 -4
- package/dist/core/fields/types.d.ts +34 -3
- package/dist/core/fields/types.test-d.d.ts +1 -0
- package/dist/core/fields/types.test-d.js +37 -0
- package/dist/core/i18n/en/fields.d.ts +1 -1
- package/dist/core/i18n/en/fields.js +1 -1
- package/dist/core/i18n/fr/fields.d.ts +1 -1
- package/dist/core/i18n/fr/fields.js +1 -1
- package/dist/core/pipeline/hooks/set-default-values.server.d.ts +1 -1
- package/dist/core/pipeline/hooks/set-default-values.server.js +25 -30
- package/dist/core/pipeline/persist/relations/diff.server.d.ts +4 -4
- package/dist/core/pipeline/persist/relations/extract.server.js +27 -45
- package/dist/core/prototype/area/api.server.d.ts +7 -1
- package/dist/core/prototype/area/api.server.js +3 -0
- package/dist/core/prototype/area/operations/update.d.ts +2 -1
- package/dist/core/prototype/collection/api.server.d.ts +16 -3
- package/dist/core/prototype/collection/api.server.js +6 -0
- package/dist/core/prototype/collection/operations/create.d.ts +2 -1
- package/dist/core/prototype/collection/operations/delete.js +6 -4
- package/dist/core/prototype/collection/operations/duplicate.js +2 -4
- package/dist/core/prototype/collection/operations/update-by-id.d.ts +2 -1
- package/dist/core/prototype/collection/thumbnail/hooks/set-document-thumbnail.server.js +9 -15
- package/dist/core/prototype/collection/upload/disk/delete.server.js +1 -1
- package/dist/core/prototype/shared/title/hooks/set-document-title.server.js +2 -2
- package/dist/core/prototype/shared/versions/hooks/handle-new-version.server.js +17 -1
- package/dist/fields/blocks/component/focus/BlocksFocus.svelte +21 -4
- package/dist/fields/blocks/component/focus/Inspector.svelte +12 -28
- package/dist/fields/blocks/component/focus/LayersList.svelte +4 -1
- package/dist/fields/blocks/component/focus/Renders.svelte +44 -28
- package/dist/fields/blocks/component/focus/Renders.svelte.d.ts +3 -0
- package/dist/fields/blocks/component/focus/StageAdd.svelte +42 -0
- package/dist/fields/blocks/component/focus/StageAdd.svelte.d.ts +6 -0
- package/dist/fields/blocks/component/focus/focus.svelte.d.ts +7 -13
- package/dist/fields/blocks/component/focus/focus.svelte.js +38 -33
- package/dist/fields/blocks/index.d.ts +26 -5
- package/dist/fields/blocks/index.js +8 -2
- package/dist/fields/relation/component/Relation.svelte +16 -3
- package/dist/fields/relation/component/Relation.svelte.d.ts +2 -1
- package/dist/fields/relation/component/RelationInline.svelte +6 -9
- package/dist/fields/relation/fetch.d.ts +12 -0
- package/dist/fields/relation/fetch.js +27 -0
- package/dist/fields/relation/index.d.ts +16 -7
- package/dist/fields/relation/index.js +20 -4
- package/dist/fields/relation/module.server.js +15 -18
- package/dist/fields/relation/populate.js +4 -21
- package/dist/fields/relation/relation.d.ts +38 -0
- package/dist/fields/relation/relation.js +55 -0
- package/dist/fields/relation/relation.test-d.d.ts +1 -0
- package/dist/fields/relation/relation.test-d.js +10 -0
- package/dist/fields/relation/value.d.ts +2 -2
- package/dist/fields/rich-text/component/Cell.svelte +2 -2
- package/dist/fields/rich-text/json.d.ts +4 -9
- package/dist/fields/rich-text/json.js +8 -6
- package/dist/fields/types.d.ts +4 -1
- package/dist/live/Consumer.svelte +3 -3
- package/dist/live/Consumer.svelte.d.ts +2 -2
- package/dist/panel/components/sections/collection/grid/grid-item/GridItem.svelte +7 -1
- package/dist/panel/components/ui/button/button.svelte +2 -3
- package/dist/panel/context/documentForm.svelte.js +2 -2
- package/dist/panel/style/index.css +4 -3
- package/dist/panel/style/palette.css +3 -3
- package/dist/panel/style/scheme.css +1 -0
- package/dist/public.d.ts +3 -3
- package/dist/public.js +3 -3
- package/dist/types.d.ts +1 -1
- package/dist/util/types.d.ts +1 -1
- package/dist/util/types.js +1 -1
- package/package.json +1 -1
- package/dist/fields/blocks/component/focus/StagePlaceholder.svelte +0 -86
- package/dist/fields/blocks/component/focus/StagePlaceholder.svelte.d.ts +0 -8
- package/dist/fields/relation/util.d.ts +0 -57
- package/dist/fields/relation/util.js +0 -86
|
@@ -76,11 +76,17 @@ export declare class BlockBuilder {
|
|
|
76
76
|
/**
|
|
77
77
|
* The component drawn for the block on the stage of focus mode.
|
|
78
78
|
* It gets the block value, its path, its fields and the form, so it can show the value,
|
|
79
|
-
*
|
|
79
|
+
* read its relations with `Relation.resolve` and mount panel fields with `RenderFields`.
|
|
80
|
+
*
|
|
81
|
+
* `controls: false` drops the bar above the selected block, for a small one like a button.
|
|
82
|
+
* Keys, ⌘K and the layers still move, duplicate and remove it.
|
|
80
83
|
* @example
|
|
81
84
|
* block('hero').fields(text('title'), richText('text')).render(HeroRender)
|
|
85
|
+
* block('button').render(ButtonRender, { controls: false })
|
|
82
86
|
*/
|
|
83
|
-
render(component: Component<BlockRenderProps
|
|
87
|
+
render(component: Component<BlockRenderProps>, options?: {
|
|
88
|
+
controls?: boolean;
|
|
89
|
+
}): this;
|
|
84
90
|
description(description: string): this;
|
|
85
91
|
label(label: string): this;
|
|
86
92
|
fields(...fields: FieldBuilder<Field>[]): this;
|
|
@@ -92,6 +98,8 @@ export declare class BlockBuilder {
|
|
|
92
98
|
thumbnail?: Component;
|
|
93
99
|
renderTitle?: BlocksFieldBlockRenderTitle;
|
|
94
100
|
render?: Component<BlockRenderProps>;
|
|
101
|
+
/** `false`: no bar above the block when it is selected on the stage. */
|
|
102
|
+
controls?: boolean;
|
|
95
103
|
fields: FieldBuilder<Field>[];
|
|
96
104
|
};
|
|
97
105
|
get name(): string;
|
|
@@ -111,15 +119,26 @@ export type BlocksFieldBlockRenderTitle = (args: {
|
|
|
111
119
|
}) => string;
|
|
112
120
|
/** What a block's render component receives. */
|
|
113
121
|
export type BlockRenderProps = {
|
|
114
|
-
/** The block value
|
|
122
|
+
/** The block value; read a relation with `Relation.resolve(block.image).first()`. */
|
|
115
123
|
block: GenericBlock;
|
|
116
124
|
/** `layout.sections.0` */
|
|
117
125
|
path: string;
|
|
118
126
|
/** The block's field builders, for `RenderFields`. */
|
|
119
127
|
fields: FieldBuilder<Field>[];
|
|
120
128
|
form: DocumentFormContext;
|
|
121
|
-
/**
|
|
122
|
-
|
|
129
|
+
/**
|
|
130
|
+
* The block's nested lists, each block in its own selectable wrapper, `.rz-renders`, and a `+`
|
|
131
|
+
* after them, `.rz-renders__add`. `children('items')` draws one list; `class` names its wrapper,
|
|
132
|
+
* for the render to lay it out.
|
|
133
|
+
* @example
|
|
134
|
+
* {@render children?.('items', { class: 'cards' })}
|
|
135
|
+
*
|
|
136
|
+
* :global(.cards) { display: flex; gap: 1rem; }
|
|
137
|
+
* :global(.cards > .rz-renders__add) { align-self: center; }
|
|
138
|
+
*/
|
|
139
|
+
children?: Snippet<[name?: string, options?: {
|
|
140
|
+
class?: string;
|
|
141
|
+
}]>;
|
|
123
142
|
};
|
|
124
143
|
export type BlocksFieldBlock = {
|
|
125
144
|
name: string;
|
|
@@ -129,6 +148,8 @@ export type BlocksFieldBlock = {
|
|
|
129
148
|
thumbnail?: Component;
|
|
130
149
|
renderTitle?: BlocksFieldBlockRenderTitle;
|
|
131
150
|
render?: Component<BlockRenderProps>;
|
|
151
|
+
/** `false`: no bar above the block when it is selected on the stage. */
|
|
152
|
+
controls?: boolean;
|
|
132
153
|
fields: FieldBuilder<Field>[];
|
|
133
154
|
};
|
|
134
155
|
export type BlocksFieldRaw = FormField & {
|
|
@@ -194,12 +194,18 @@ export class BlockBuilder {
|
|
|
194
194
|
/**
|
|
195
195
|
* The component drawn for the block on the stage of focus mode.
|
|
196
196
|
* It gets the block value, its path, its fields and the form, so it can show the value,
|
|
197
|
-
*
|
|
197
|
+
* read its relations with `Relation.resolve` and mount panel fields with `RenderFields`.
|
|
198
|
+
*
|
|
199
|
+
* `controls: false` drops the bar above the selected block, for a small one like a button.
|
|
200
|
+
* Keys, ⌘K and the layers still move, duplicate and remove it.
|
|
198
201
|
* @example
|
|
199
202
|
* block('hero').fields(text('title'), richText('text')).render(HeroRender)
|
|
203
|
+
* block('button').render(ButtonRender, { controls: false })
|
|
200
204
|
*/
|
|
201
|
-
render(component) {
|
|
205
|
+
render(component, options = {}) {
|
|
202
206
|
this.block.render = component;
|
|
207
|
+
if (options.controls === false)
|
|
208
|
+
this.block.controls = false;
|
|
203
209
|
return this;
|
|
204
210
|
}
|
|
205
211
|
description(description) {
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
import { Images, Plus, TextSearch } from '@lucide/svelte';
|
|
17
17
|
import { untrack } from 'svelte';
|
|
18
18
|
import { getAPIProxyContext } from '../../../panel/context/api-proxy.svelte.js';
|
|
19
|
-
import type {
|
|
19
|
+
import type { RelationFieldBuilder, RelationRow } from '../index.js';
|
|
20
|
+
import { Relation } from '../relation.js';
|
|
20
21
|
import { toRelationValue } from '../value.js';
|
|
21
22
|
import Default from './default/Default.svelte';
|
|
22
23
|
import type { RelationFieldItem } from './types.js';
|
|
@@ -42,7 +43,7 @@
|
|
|
42
43
|
// the fetched items
|
|
43
44
|
let initialItems: RelationFieldItem[] = $state([]);
|
|
44
45
|
// value from the form
|
|
45
|
-
let initialValue = $derived(form.getRawValue<
|
|
46
|
+
let initialValue = $derived(form.getRawValue<RelationRow[]>(path) || []);
|
|
46
47
|
// timestamp to force re-render
|
|
47
48
|
let stamp = $state(new Date().getTime().toString());
|
|
48
49
|
|
|
@@ -122,11 +123,23 @@
|
|
|
122
123
|
if (ressource.data) {
|
|
123
124
|
initialItems = ressource.data.docs.map((doc: GenericDoc) => documentToRelationFieldItem(doc));
|
|
124
125
|
if (!initialized) {
|
|
125
|
-
const findItem = (relation:
|
|
126
|
+
const findItem = (relation: RelationRow) => {
|
|
126
127
|
return initialItems.find((item) => item.documentId === relation.documentId);
|
|
127
128
|
};
|
|
128
129
|
selectedItems = initialValue.map(findItem).filter((item) => !!item);
|
|
129
130
|
initialized = true;
|
|
131
|
+
|
|
132
|
+
// A saved relation the candidates leave out, the field's `query()` no longer matching it,
|
|
133
|
+
// is read on its own: it stays picked, and the next change writes it back.
|
|
134
|
+
const missing = initialValue.filter((relation) => !findItem(relation));
|
|
135
|
+
if (missing.length) {
|
|
136
|
+
const order = initialValue.map((relation) => relation.documentId);
|
|
137
|
+
Promise.resolve(Relation.resolve<GenericDoc>(missing).all()).then((docs) => {
|
|
138
|
+
selectedItems = [...selectedItems, ...docs.map(documentToRelationFieldItem)].sort(
|
|
139
|
+
(a, b) => order.indexOf(a.documentId) - order.indexOf(b.documentId)
|
|
140
|
+
);
|
|
141
|
+
});
|
|
142
|
+
}
|
|
130
143
|
}
|
|
131
144
|
}
|
|
132
145
|
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type DocumentFormContext } from '../../../panel/context/documentForm.svelte.js';
|
|
2
2
|
import type { FormContext } from '../../../panel/context/form.svelte.js';
|
|
3
|
-
import type {
|
|
3
|
+
import type { RelationFieldBuilder } from '../index.js';
|
|
4
|
+
import { Relation } from '../relation.js';
|
|
4
5
|
type Props = {
|
|
5
6
|
path: string;
|
|
6
7
|
config: RelationFieldBuilder;
|
|
@@ -3,18 +3,18 @@
|
|
|
3
3
|
import type { UploadDoc } from '../../../core/prototype/collection/upload/types.js';
|
|
4
4
|
import { isUploadConfig } from '../../../core/prototype/collection/upload/util/config.js';
|
|
5
5
|
import { apiUrl } from '../../../core/routes/util.js';
|
|
6
|
+
import { Relation } from '../relation.js';
|
|
6
7
|
import FileDrop from '../../../panel/components/sections/collection/bulk-upload/FileDrop.svelte';
|
|
7
8
|
import SpinLoader from '../../../panel/components/ui/spin-loader/SpinLoader.svelte';
|
|
8
9
|
import { getAPIProxyContext } from '../../../panel/context/api-proxy.svelte.js';
|
|
9
10
|
import { getConfigContext } from '../../../panel/context/config.svelte.js';
|
|
10
11
|
import type { DocumentFormContext } from '../../../panel/context/documentForm.svelte.js';
|
|
11
12
|
import { getLocaleContext } from '../../../panel/context/locale.svelte.js';
|
|
12
|
-
import { populate } from '../populate.js';
|
|
13
13
|
import { uploadFiles, type UploadProgress } from '../../../panel/util/upload-file.js';
|
|
14
14
|
import { Image as ImageIcon, Trash2 } from '@lucide/svelte';
|
|
15
15
|
import type { Snippet } from 'svelte';
|
|
16
16
|
import { SvelteMap } from 'svelte/reactivity';
|
|
17
|
-
import type {
|
|
17
|
+
import type { RelationFieldBuilder, RelationRow } from '../index.js';
|
|
18
18
|
import { toRelationValue } from '../value.js';
|
|
19
19
|
import Browse from './upload/Browse.svelte';
|
|
20
20
|
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
const isUpload = $derived(isUploadConfig(relationConfig));
|
|
40
40
|
const many = $derived(!!config.get.many);
|
|
41
41
|
|
|
42
|
-
const field = $derived(form.useField<
|
|
42
|
+
const field = $derived(form.useField<RelationRow[]>(path, config));
|
|
43
43
|
const refs = $derived(Array.isArray(field.value) ? field.value : []);
|
|
44
44
|
const ids = $derived(refs.map((ref) => ref.documentId));
|
|
45
45
|
/** Files and the library only reach an editable field to an upload collection. */
|
|
@@ -51,10 +51,8 @@
|
|
|
51
51
|
for (const ref of refs) {
|
|
52
52
|
if (known.has(ref.documentId)) continue;
|
|
53
53
|
const lookup = { relationTo: ref.relationTo, documentId: ref.documentId };
|
|
54
|
-
|
|
55
|
-
if (doc
|
|
56
|
-
known.set(ref.documentId, doc as UploadDoc);
|
|
57
|
-
}
|
|
54
|
+
Promise.resolve(Relation.resolve<UploadDoc>([lookup]).first()).then((doc) => {
|
|
55
|
+
if (doc) known.set(ref.documentId, doc);
|
|
58
56
|
});
|
|
59
57
|
}
|
|
60
58
|
});
|
|
@@ -78,7 +76,7 @@
|
|
|
78
76
|
relationTo: config.get.relationTo,
|
|
79
77
|
path,
|
|
80
78
|
locale: config.get.localized ? locale.code : undefined
|
|
81
|
-
}) as
|
|
79
|
+
}) as RelationRow[];
|
|
82
80
|
}
|
|
83
81
|
|
|
84
82
|
/* ------------------------------------------------------------ uploading */
|
|
@@ -276,7 +274,6 @@
|
|
|
276
274
|
.rz-relation-inline :global(.rz-relation-inline__empty) {
|
|
277
275
|
width: 100%;
|
|
278
276
|
padding: var(--rz-size-11) var(--rz-size-4);
|
|
279
|
-
background-color: var(--rz-bg-raised);
|
|
280
277
|
}
|
|
281
278
|
|
|
282
279
|
.rz-relation-inline__loading {
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Dic } from '../../util/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* A document by its collection and id, as the API answers it at depth 1: one request per
|
|
4
|
+
* document for the page's life, `null` when the request fails.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* fetchDoc('blogPosts', 'p1') // GET /api/blog-posts/p1?depth=1
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
export declare function fetchDoc(slug: string, id: string): Promise<Dic | null>;
|
|
11
|
+
/** Forgets the documents fetched so far: the next request for each goes to the API again. */
|
|
12
|
+
export declare const clearFetchedDocs: () => void;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { API_PREFIX } from '../../core/routes/constants.js';
|
|
2
|
+
import { toKebabCase } from '../../util/string.js';
|
|
3
|
+
const cache = new Map();
|
|
4
|
+
/**
|
|
5
|
+
* A document by its collection and id, as the API answers it at depth 1: one request per
|
|
6
|
+
* document for the page's life, `null` when the request fails.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* fetchDoc('blogPosts', 'p1') // GET /api/blog-posts/p1?depth=1
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export function fetchDoc(slug, id) {
|
|
13
|
+
const key = `${slug}/${id}`;
|
|
14
|
+
if (!cache.has(key)) {
|
|
15
|
+
const url = `${API_PREFIX}/${toKebabCase(slug)}/${id}?depth=1`;
|
|
16
|
+
cache.set(key, fetch(url)
|
|
17
|
+
.then((response) => response.json())
|
|
18
|
+
.then((response) => response?.doc ?? null)
|
|
19
|
+
.catch((error) => {
|
|
20
|
+
console.error(error);
|
|
21
|
+
return null;
|
|
22
|
+
}));
|
|
23
|
+
}
|
|
24
|
+
return cache.get(key);
|
|
25
|
+
}
|
|
26
|
+
/** Forgets the documents fetched so far: the next request for each goes to the API again. */
|
|
27
|
+
export const clearFetchedDocs = () => cache.clear();
|
|
@@ -1,11 +1,21 @@
|
|
|
1
1
|
import type { DataType } from '../../core/fields/builders/form-field-builder.js';
|
|
2
|
-
import { FormFieldBuilder } from '../../core/fields/builders/form-field-builder.js';
|
|
2
|
+
import { FormFieldBuilder, type FormFieldUse } from '../../core/fields/builders/form-field-builder.js';
|
|
3
3
|
import type { CollectionSlug, GenericDoc } from '../../core/prototype/types.js';
|
|
4
|
-
import type { DefaultOptions, DefaultValueFn, FormField } from '../types.js';
|
|
4
|
+
import type { DefaultOptions, DefaultValueFn, FormField, RelationRef } from '../types.js';
|
|
5
5
|
import type { RegisterCollection } from '../../index.js';
|
|
6
6
|
import type { WithOptional } from '../../util/types.js';
|
|
7
|
+
import type { RequestEvent } from '@sveltejs/kit';
|
|
7
8
|
export declare class RelationFieldBuilder<Doc extends GenericDoc = GenericDoc> extends FormFieldBuilder<RelationField<Doc>> {
|
|
8
9
|
constructor(name: string);
|
|
10
|
+
/**
|
|
11
|
+
* The default as refs, as a read answers them: the panel's form and a new block hold
|
|
12
|
+
* `[{ relationTo, documentId }]`, not the ids the default was written with.
|
|
13
|
+
*/
|
|
14
|
+
get use(): Omit<FormFieldUse<RelationField<Doc>>, 'defaultValue'> & {
|
|
15
|
+
defaultValue(context?: {
|
|
16
|
+
event?: RequestEvent;
|
|
17
|
+
}): RelationRef[];
|
|
18
|
+
};
|
|
9
19
|
get component(): import("svelte").Component<{
|
|
10
20
|
path: string;
|
|
11
21
|
config: RelationFieldBuilder;
|
|
@@ -39,7 +49,7 @@ export type RelationField<Doc extends GenericDoc = GenericDoc> = FormField & {
|
|
|
39
49
|
query?: string | ((doc: WithOptional<Doc, 'id'>) => string);
|
|
40
50
|
isThumbnail?: boolean;
|
|
41
51
|
};
|
|
42
|
-
export type
|
|
52
|
+
export type RelationRow = {
|
|
43
53
|
id?: string;
|
|
44
54
|
ownerId: string;
|
|
45
55
|
path: string;
|
|
@@ -52,11 +62,10 @@ export type Relation = {
|
|
|
52
62
|
/**
|
|
53
63
|
* A relation before it is written, when the row it hangs off does not exist yet.
|
|
54
64
|
*
|
|
55
|
-
* The only difference from `
|
|
56
|
-
* after inserting the owner.
|
|
57
|
-
* relation diffing imported a type from an adapter to describe its own intermediate value.
|
|
65
|
+
* The only difference from `RelationRow` is that `ownerId` is not known — a create resolves it
|
|
66
|
+
* after inserting the owner.
|
|
58
67
|
*/
|
|
59
|
-
export type BeforeOperationRelation = Omit<
|
|
68
|
+
export type BeforeOperationRelation = Omit<RelationRow, 'ownerId'> & {
|
|
60
69
|
ownerId?: string;
|
|
61
70
|
};
|
|
62
71
|
type QueryResolver<Doc extends GenericDoc = GenericDoc> = (doc: WithOptional<Doc, 'id'>) => string;
|
|
@@ -13,6 +13,24 @@ export class RelationFieldBuilder extends FormFieldBuilder {
|
|
|
13
13
|
beforeValidate: [ensureRelationExists]
|
|
14
14
|
};
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* The default as refs, as a read answers them: the panel's form and a new block hold
|
|
18
|
+
* `[{ relationTo, documentId }]`, not the ids the default was written with.
|
|
19
|
+
*/
|
|
20
|
+
get use() {
|
|
21
|
+
const use = super.use;
|
|
22
|
+
return {
|
|
23
|
+
...use,
|
|
24
|
+
defaultValue: (context = {}) => {
|
|
25
|
+
const value = use.defaultValue(context);
|
|
26
|
+
const ids = typeof value === 'string' ? [value] : Array.isArray(value) ? value : [];
|
|
27
|
+
// Only a non-empty string names a document.
|
|
28
|
+
return ids
|
|
29
|
+
.filter((id) => typeof id === 'string' && id !== '')
|
|
30
|
+
.map((documentId) => ({ relationTo: this.field.relationTo, documentId }));
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
16
34
|
get component() {
|
|
17
35
|
return RelationComponent;
|
|
18
36
|
}
|
|
@@ -49,10 +67,8 @@ export class RelationFieldBuilder extends FormFieldBuilder {
|
|
|
49
67
|
const relationValueType = dedent `
|
|
50
68
|
//@shared:start RelationValue
|
|
51
69
|
export type RelationValue<T> =
|
|
52
|
-
| T[] //
|
|
53
|
-
| { id?: string; relationTo: string; documentId: string }[] //
|
|
54
|
-
| string[]
|
|
55
|
-
| string; // When sending data to update
|
|
70
|
+
| T[] // depth 1 or more: the documents
|
|
71
|
+
| { id?: string; relationTo: string; documentId: string }[]; // depth 0: refs
|
|
56
72
|
//@shared:end
|
|
57
73
|
`;
|
|
58
74
|
const fieldType = `${this.name}${this.get.required ? '' : '?'}: RelationValue<${capitalize(this.get.relationTo)}Doc>`;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { getRequestEvent } from '$app/server';
|
|
2
2
|
import { env } from '$env/dynamic/public';
|
|
3
3
|
import { PARAMS } from '../../core/constants.js';
|
|
4
|
+
import { Relation } from './relation.js';
|
|
4
5
|
import { trycatchFetch } from '../../util/function.js';
|
|
5
6
|
import { toKebabCase } from '../../util/string.js';
|
|
6
7
|
/** Real implementation — resolved server-side via `$rime/fields/relation` (see relation/index.ts,
|
|
@@ -24,24 +25,20 @@ export const ensureRelationExists = async (value, { config }) => {
|
|
|
24
25
|
const { doc } = await response.json();
|
|
25
26
|
return doc;
|
|
26
27
|
};
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const doc = await retrieveRelation(value);
|
|
42
|
-
if (doc) {
|
|
43
|
-
output.push(doc.id);
|
|
44
|
-
}
|
|
28
|
+
// One value outside an array is a list of one.
|
|
29
|
+
const entries = Array.isArray(value) ? value : value ? [value] : [];
|
|
30
|
+
for (const entry of entries) {
|
|
31
|
+
// A bare id, a ref, or a document read at depth 1 and sent back. A `null` element — what
|
|
32
|
+
// `[undefined]` becomes over JSON — names nothing, like an unknown id.
|
|
33
|
+
const documentId = typeof entry === 'string'
|
|
34
|
+
? entry
|
|
35
|
+
: Relation.isRef(entry)
|
|
36
|
+
? entry.documentId
|
|
37
|
+
: entry?.id;
|
|
38
|
+
if (!documentId)
|
|
39
|
+
continue;
|
|
40
|
+
if (await retrieveRelation(documentId))
|
|
41
|
+
output.push(entry);
|
|
45
42
|
}
|
|
46
43
|
return output;
|
|
47
44
|
};
|
|
@@ -1,27 +1,10 @@
|
|
|
1
|
-
import { API_PREFIX } from '../../core/routes/constants.js';
|
|
2
1
|
import { isObjectLiteral } from '../../util/object.js';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
/** One request per document for the page's life. */
|
|
6
|
-
function fetchDoc(slug, id) {
|
|
7
|
-
const key = `${slug}/${id}`;
|
|
8
|
-
if (!cache.has(key)) {
|
|
9
|
-
const url = `${API_PREFIX}/${toKebabCase(slug)}/${id}?depth=1`;
|
|
10
|
-
cache.set(key, fetch(url)
|
|
11
|
-
.then((response) => response.json())
|
|
12
|
-
.then((response) => response?.doc ?? null)
|
|
13
|
-
.catch((error) => {
|
|
14
|
-
console.error(error);
|
|
15
|
-
return null;
|
|
16
|
-
}));
|
|
17
|
-
}
|
|
18
|
-
return cache.get(key);
|
|
19
|
-
}
|
|
2
|
+
import { clearFetchedDocs, fetchDoc } from './fetch.js';
|
|
3
|
+
import { Relation } from './relation.js';
|
|
20
4
|
const isLink = (value) => 'value' in value &&
|
|
21
5
|
'target' in value &&
|
|
22
6
|
'type' in value &&
|
|
23
7
|
!['url', 'email', 'tel', 'anchor'].includes(value.type);
|
|
24
|
-
const isRelation = (value) => 'documentId' in value && 'relationTo' in value;
|
|
25
8
|
/**
|
|
26
9
|
* Relations and resource links resolved at depth 1, the shape the API answers with.
|
|
27
10
|
* A resolved document is returned as is; a relation that cannot be fetched stays a reference.
|
|
@@ -38,7 +21,7 @@ export async function populate(value) {
|
|
|
38
21
|
const doc = await fetchDoc(value.type, value.value);
|
|
39
22
|
return doc?.url ? { ...value, url: doc.url } : value;
|
|
40
23
|
}
|
|
41
|
-
if (
|
|
24
|
+
if (Relation.isRef(value)) {
|
|
42
25
|
if ('livePreview' in value)
|
|
43
26
|
return value.livePreview;
|
|
44
27
|
return ((await fetchDoc(value.relationTo, value.documentId)) ?? value);
|
|
@@ -46,4 +29,4 @@ export async function populate(value) {
|
|
|
46
29
|
const entries = await Promise.all(Object.entries(value).map(async ([key, item]) => [key, await populate(item)]));
|
|
47
30
|
return Object.fromEntries(entries);
|
|
48
31
|
}
|
|
49
|
-
populate.clear =
|
|
32
|
+
populate.clear = clearFetchedDocs;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { RelationRef, RelationValue } from '../types.js';
|
|
2
|
+
type Resolution<T> = {
|
|
3
|
+
/** The first document, or `null`. A promise when it has to be fetched. */
|
|
4
|
+
first(): T | null | Promise<T | null>;
|
|
5
|
+
/** Every document. A promise when some have to be fetched. */
|
|
6
|
+
all(): T[] | Promise<T[]>;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* The documents a relation value holds. A document is kept as it is, a ref carrying a live preview
|
|
10
|
+
* gives that preview, and any other ref is fetched from the API. Anything else, a bare id
|
|
11
|
+
* included, names no document and gives nothing.
|
|
12
|
+
*
|
|
13
|
+
* `first()` and `all()` answer the documents themselves when nothing has to be fetched, and a
|
|
14
|
+
* promise otherwise; `await` and `{#await}` take either. A ref that cannot be fetched is left out.
|
|
15
|
+
* During server rendering a ref is not fetched: its promise stays pending, and the browser fetches
|
|
16
|
+
* it once the page hydrates.
|
|
17
|
+
*
|
|
18
|
+
* ```svelte
|
|
19
|
+
* {#await Relation.resolve(block.image).first() then image}
|
|
20
|
+
* {#if image}<img src={image.sizes.md} alt={image.alt} />{/if}
|
|
21
|
+
* {/await}
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
declare function resolve<T>(value: RelationValue<T> | T | RelationRef | null | undefined | false): Resolution<T>;
|
|
25
|
+
/**
|
|
26
|
+
* Reads relation field values.
|
|
27
|
+
*
|
|
28
|
+
* ```ts
|
|
29
|
+
* await Relation.resolve(page.author).first(); // UsersDoc | null
|
|
30
|
+
* await Relation.resolve(block.images).all(); // MediasDoc[]
|
|
31
|
+
* Relation.isRef(entry); // true for { relationTo, documentId }, false for a document
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare const Relation: {
|
|
35
|
+
resolve: typeof resolve;
|
|
36
|
+
isRef: (value: unknown) => value is RelationRef;
|
|
37
|
+
};
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { isObjectLiteral } from '../../util/object.js';
|
|
2
|
+
import { fetchDoc } from './fetch.js';
|
|
3
|
+
/** `{ relationTo, documentId }`, whatever else it carries: a ref, not a document. */
|
|
4
|
+
const isRef = (value) => isObjectLiteral(value) &&
|
|
5
|
+
typeof value.relationTo === 'string' &&
|
|
6
|
+
typeof value.documentId === 'string';
|
|
7
|
+
/** Any other object with an `id`: a document, as a read at depth 1 or more hands it back. */
|
|
8
|
+
const isDocument = (value) => isObjectLiteral(value) && !isRef(value) && typeof value.id === 'string';
|
|
9
|
+
/** What a live-edit form puts on a ref: the document as it is being edited. */
|
|
10
|
+
const livePreviewOf = (ref) => ref.livePreview;
|
|
11
|
+
/**
|
|
12
|
+
* The documents a relation value holds. A document is kept as it is, a ref carrying a live preview
|
|
13
|
+
* gives that preview, and any other ref is fetched from the API. Anything else, a bare id
|
|
14
|
+
* included, names no document and gives nothing.
|
|
15
|
+
*
|
|
16
|
+
* `first()` and `all()` answer the documents themselves when nothing has to be fetched, and a
|
|
17
|
+
* promise otherwise; `await` and `{#await}` take either. A ref that cannot be fetched is left out.
|
|
18
|
+
* During server rendering a ref is not fetched: its promise stays pending, and the browser fetches
|
|
19
|
+
* it once the page hydrates.
|
|
20
|
+
*
|
|
21
|
+
* ```svelte
|
|
22
|
+
* {#await Relation.resolve(block.image).first() then image}
|
|
23
|
+
* {#if image}<img src={image.sizes.md} alt={image.alt} />{/if}
|
|
24
|
+
* {/await}
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
function resolve(value) {
|
|
28
|
+
const entries = (Array.isArray(value) ? value : [value]).filter((entry) => isRef(entry) || isDocument(entry));
|
|
29
|
+
const toFetch = (entry) => isRef(entry) && !livePreviewOf(entry);
|
|
30
|
+
const all = () => {
|
|
31
|
+
const read = (entry) => (isRef(entry) ? livePreviewOf(entry) : entry);
|
|
32
|
+
if (!entries.some(toFetch))
|
|
33
|
+
return entries.map(read);
|
|
34
|
+
if (typeof window === 'undefined')
|
|
35
|
+
return new Promise(() => { });
|
|
36
|
+
return Promise.all(entries.map((entry) => toFetch(entry)
|
|
37
|
+
? fetchDoc(entry.relationTo, entry.documentId)
|
|
38
|
+
: read(entry))).then((docs) => docs.filter((doc) => !!doc));
|
|
39
|
+
};
|
|
40
|
+
const first = () => {
|
|
41
|
+
const docs = all();
|
|
42
|
+
return docs instanceof Promise ? docs.then((list) => list[0] ?? null) : (docs[0] ?? null);
|
|
43
|
+
};
|
|
44
|
+
return { first, all };
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Reads relation field values.
|
|
48
|
+
*
|
|
49
|
+
* ```ts
|
|
50
|
+
* await Relation.resolve(page.author).first(); // UsersDoc | null
|
|
51
|
+
* await Relation.resolve(block.images).all(); // MediasDoc[]
|
|
52
|
+
* Relation.isRef(entry); // true for { relationTo, documentId }, false for a document
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export const Relation = { resolve, isRef };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { describe, expectTypeOf, it } from 'vitest';
|
|
2
|
+
import { Relation } from './relation.js';
|
|
3
|
+
const m1 = { id: 'm1', _type: 'medias' };
|
|
4
|
+
describe('Relation.resolve types', () => {
|
|
5
|
+
it('reads the document type off the value', () => {
|
|
6
|
+
expectTypeOf(Relation.resolve({}).first()).toEqualTypeOf();
|
|
7
|
+
expectTypeOf(Relation.resolve([m1]).all()).toEqualTypeOf();
|
|
8
|
+
expectTypeOf(Relation.resolve(m1).first()).toEqualTypeOf();
|
|
9
|
+
});
|
|
10
|
+
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { GenericDoc } from '../../core/prototype/types.js';
|
|
2
|
-
import type {
|
|
2
|
+
import type { RelationRow } from './index.js';
|
|
3
3
|
/** A related document as the field holds it before it is written: its id, and its row if any. */
|
|
4
4
|
export type RelationItem = {
|
|
5
5
|
documentId: string;
|
|
@@ -22,4 +22,4 @@ export declare function toRelationValue(items: RelationItem[], args: {
|
|
|
22
22
|
path: string;
|
|
23
23
|
locale?: string;
|
|
24
24
|
live?: boolean;
|
|
25
|
-
}): Omit<
|
|
25
|
+
}): Omit<RelationRow, 'ownerId'>[];
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import {
|
|
2
|
+
import { RichText } from '../json.js';
|
|
3
3
|
|
|
4
4
|
let { value }: { value: string } = $props();
|
|
5
5
|
|
|
6
|
-
let textValue: string = $derived(
|
|
6
|
+
let textValue: string = $derived(RichText.toText(value));
|
|
7
7
|
</script>
|
|
8
8
|
|
|
9
9
|
{#if textValue && textValue.length > 10}
|
|
@@ -7,12 +7,7 @@ import type { JSONContent } from '@tiptap/core';
|
|
|
7
7
|
* isJSONContent('Just a string') // false
|
|
8
8
|
*/
|
|
9
9
|
export declare const isJSONContent: (value: any) => value is JSONContent;
|
|
10
|
-
/**
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
* @example
|
|
15
|
-
* // Returns "Hello world"
|
|
16
|
-
* richTextJSONToText('{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"Hello world"}]}]}');
|
|
17
|
-
*/
|
|
18
|
-
export declare const richTextJSONToText: (value: string | JSONContent | undefined) => string;
|
|
10
|
+
/** Reads rich text field values. */
|
|
11
|
+
export declare const RichText: {
|
|
12
|
+
toText: (value: string | JSONContent | undefined) => string;
|
|
13
|
+
};
|
|
@@ -9,14 +9,14 @@ export const isJSONContent = (value) => {
|
|
|
9
9
|
return typeof value === 'object' && value !== null && 'content' in value;
|
|
10
10
|
};
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
* Extracts text content from a TipTap/ProseMirror JSON structure.
|
|
12
|
+
* The text of a rich text value, its nodes' text joined by spaces.
|
|
14
13
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* RichText.toText({ type: 'doc', content: [{ type: 'paragraph', content: [{ type: 'text', text: 'Hello' }] }] });
|
|
16
|
+
* // 'Hello'
|
|
17
|
+
* ```
|
|
18
18
|
*/
|
|
19
|
-
|
|
19
|
+
const toText = (value) => {
|
|
20
20
|
if (!value)
|
|
21
21
|
return '';
|
|
22
22
|
let textValue;
|
|
@@ -42,3 +42,5 @@ export const richTextJSONToText = (value) => {
|
|
|
42
42
|
}
|
|
43
43
|
return textValue;
|
|
44
44
|
};
|
|
45
|
+
/** Reads rich text field values. */
|
|
46
|
+
export const RichText = { toText };
|
package/dist/fields/types.d.ts
CHANGED
|
@@ -130,7 +130,10 @@ export type RelationRef = {
|
|
|
130
130
|
relationTo: string;
|
|
131
131
|
documentId: string;
|
|
132
132
|
};
|
|
133
|
-
|
|
133
|
+
/** A relation as a read answers it: the documents at depth 1 or more, refs at depth 0. */
|
|
134
|
+
export type RelationValue<T> = T[] | RelationRef[];
|
|
135
|
+
/** What a write takes for a relation: what a read answers, or bare ids. */
|
|
136
|
+
export type RelationInput<T> = RelationValue<T> | string[] | string;
|
|
134
137
|
export type BaseUseFieldReturn = {
|
|
135
138
|
path: string;
|
|
136
139
|
value: any;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
<script lang="ts" generics="T">
|
|
2
2
|
import { getLiveContext } from './context.svelte.js';
|
|
3
|
-
import type {
|
|
3
|
+
import type { WithRelationResolved } from '../core/fields/types.js';
|
|
4
4
|
import type { Snippet } from 'svelte';
|
|
5
5
|
|
|
6
6
|
let { child, data } = $props<{
|
|
7
|
-
child: Snippet<[doc:
|
|
7
|
+
child: Snippet<[doc: WithRelationResolved<T>]>;
|
|
8
8
|
data: { doc: T };
|
|
9
9
|
}>();
|
|
10
10
|
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
}
|
|
17
17
|
});
|
|
18
18
|
|
|
19
|
-
const doc = $derived(live.doc || data.doc) as
|
|
19
|
+
const doc = $derived(live.doc || data.doc) as WithRelationResolved<T>;
|
|
20
20
|
</script>
|
|
21
21
|
|
|
22
22
|
{@render child(doc)}
|