rimecms 1.1.3 → 1.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/dist/adapter-sqlite/relations.server.d.ts +4 -4
  2. package/dist/core/adapter.d.ts +4 -4
  3. package/dist/core/fields/types.d.ts +34 -3
  4. package/dist/core/fields/types.test-d.d.ts +1 -0
  5. package/dist/core/fields/types.test-d.js +37 -0
  6. package/dist/core/i18n/en/fields.d.ts +2 -0
  7. package/dist/core/i18n/en/fields.js +2 -0
  8. package/dist/core/i18n/fr/fields.d.ts +2 -0
  9. package/dist/core/i18n/fr/fields.js +2 -0
  10. package/dist/core/pipeline/hooks/set-default-values.server.d.ts +1 -1
  11. package/dist/core/pipeline/hooks/set-default-values.server.js +25 -30
  12. package/dist/core/pipeline/persist/relations/diff.server.d.ts +4 -4
  13. package/dist/core/pipeline/persist/relations/extract.server.js +27 -45
  14. package/dist/core/prototype/area/api.server.d.ts +7 -1
  15. package/dist/core/prototype/area/api.server.js +3 -0
  16. package/dist/core/prototype/area/operations/update.d.ts +2 -1
  17. package/dist/core/prototype/collection/api.server.d.ts +16 -3
  18. package/dist/core/prototype/collection/api.server.js +6 -0
  19. package/dist/core/prototype/collection/operations/create.d.ts +2 -1
  20. package/dist/core/prototype/collection/operations/delete.js +6 -4
  21. package/dist/core/prototype/collection/operations/duplicate.js +2 -4
  22. package/dist/core/prototype/collection/operations/update-by-id.d.ts +2 -1
  23. package/dist/core/prototype/collection/thumbnail/hooks/set-document-thumbnail.server.js +9 -15
  24. package/dist/core/prototype/collection/upload/disk/delete.server.js +1 -1
  25. package/dist/core/prototype/shared/title/hooks/set-document-title.server.js +2 -2
  26. package/dist/core/prototype/shared/versions/hooks/handle-new-version.server.js +17 -1
  27. package/dist/fields/blocks/index.d.ts +2 -2
  28. package/dist/fields/blocks/index.js +1 -1
  29. package/dist/fields/relation/component/Relation.svelte +16 -3
  30. package/dist/fields/relation/component/Relation.svelte.d.ts +2 -1
  31. package/dist/fields/relation/component/RelationInline.svelte +6 -8
  32. package/dist/fields/relation/component/upload/Upload.svelte +236 -46
  33. package/dist/fields/relation/fetch.d.ts +12 -0
  34. package/dist/fields/relation/fetch.js +27 -0
  35. package/dist/fields/relation/index.d.ts +16 -7
  36. package/dist/fields/relation/index.js +20 -4
  37. package/dist/fields/relation/module.server.js +15 -18
  38. package/dist/fields/relation/populate.js +4 -21
  39. package/dist/fields/relation/relation.d.ts +38 -0
  40. package/dist/fields/relation/relation.js +55 -0
  41. package/dist/fields/relation/relation.test-d.d.ts +1 -0
  42. package/dist/fields/relation/relation.test-d.js +10 -0
  43. package/dist/fields/relation/value.d.ts +2 -2
  44. package/dist/fields/rich-text/component/Cell.svelte +2 -2
  45. package/dist/fields/rich-text/component/RichText.svelte +3 -9
  46. package/dist/fields/rich-text/component/bubble-menu/bubble-menu.css +0 -1
  47. package/dist/fields/rich-text/component/props.d.ts +0 -1
  48. package/dist/fields/rich-text/core/build-editor-config.d.ts +0 -1
  49. package/dist/fields/rich-text/core/features/resource/index.js +6 -1
  50. package/dist/fields/rich-text/core/features/resource/resource-extension.d.ts +0 -8
  51. package/dist/fields/rich-text/core/features/resource/resource-extension.js +0 -10
  52. package/dist/fields/rich-text/json.d.ts +4 -9
  53. package/dist/fields/rich-text/json.js +8 -6
  54. package/dist/fields/tabs/component/Tabs.svelte +1 -9
  55. package/dist/fields/types.d.ts +4 -1
  56. package/dist/live/Consumer.svelte +3 -3
  57. package/dist/live/Consumer.svelte.d.ts +2 -2
  58. package/dist/panel/components/sections/collection/grid/grid-item/GridItem.svelte +7 -1
  59. package/dist/panel/context/documentForm.svelte.js +2 -2
  60. package/dist/public.d.ts +3 -3
  61. package/dist/public.js +3 -3
  62. package/dist/types.d.ts +1 -1
  63. package/dist/util/types.d.ts +1 -1
  64. package/dist/util/types.js +1 -1
  65. package/package.json +1 -1
  66. package/dist/fields/relation/util.d.ts +0 -57
  67. package/dist/fields/relation/util.js +0 -86
@@ -1,4 +1,5 @@
1
1
  import { RimeError } from '../../../../errors/index.js';
2
+ import { buildConfigMap } from '../../../../pipeline/config-map/index.js';
2
3
  import { Hooks } from '../../../../pipeline/define-hook.js';
3
4
  import { copyLocales } from '../../../../locale/copy.server.js';
4
5
  import { moveEditLock } from '../../metas/lock.server.js';
@@ -6,7 +7,9 @@ import { fileForDocument } from '../../../collection/upload/util/converter.serve
6
7
  import { VERSIONS_STATUS } from '../constant.js';
7
8
  import { withVersionsSuffix } from '../naming.js';
8
9
  import { VersionOperations } from '../strategy.js';
9
- import { omit, recursiveRemoveKeys } from '../../../../../util/object.js';
10
+ import { RelationFieldBuilder } from '../../../../../fields/relation/index.js';
11
+ import { Relation } from '../../../../../fields/relation/relation.js';
12
+ import { getValueAtPath, isObjectLiteral, omit, recursiveRemoveKeys, setValueAtPath } from '../../../../../util/object.js';
10
13
  import { retireAutoSaves } from '../retire-auto-saves.server.js';
11
14
  import { fallbackDataFromOriginal } from './fallback-data-from-original.js';
12
15
  /**
@@ -185,9 +188,22 @@ async function prepareDataForNewVersion(args) {
185
188
  if (!data.status) {
186
189
  data.status = VERSIONS_STATUS.DRAFT;
187
190
  }
191
+ // A relation sent back as documents, read at depth 1, keeps what it points to: the strip
192
+ // below takes every `id`, a document's own included.
193
+ for (const [path, field] of Object.entries(buildConfigMap(data, config.fields))) {
194
+ if (!(field instanceof RelationFieldBuilder))
195
+ continue;
196
+ const value = getValueAtPath(path, data);
197
+ if (!Array.isArray(value))
198
+ continue;
199
+ const refs = value.map((entry) => isDocument(entry) ? { relationTo: field.get.relationTo, documentId: entry.id } : entry);
200
+ data = setValueAtPath(path, data, refs);
201
+ }
188
202
  // Remove ownerId and id props from data this force new relation/blocks creations
189
203
  data = recursiveRemoveKeys('ownerId', 'id').from(data);
190
204
  data.ownerId = originalDoc.id;
191
205
  delete data.id;
192
206
  return data;
193
207
  }
208
+ /** An object with its own `id` that is not a ref: a document, as a read at depth 1 hands it back. */
209
+ const isDocument = (entry) => isObjectLiteral(entry) && !Relation.isRef(entry) && typeof entry.id === 'string';
@@ -76,7 +76,7 @@ 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
- * resolve its relations with `populate` and mount panel fields with `RenderFields`.
79
+ * read its relations with `Relation.resolve` and mount panel fields with `RenderFields`.
80
80
  *
81
81
  * `controls: false` drops the bar above the selected block, for a small one like a button.
82
82
  * Keys, ⌘K and the layers still move, duplicate and remove it.
@@ -119,7 +119,7 @@ export type BlocksFieldBlockRenderTitle = (args: {
119
119
  }) => string;
120
120
  /** What a block's render component receives. */
121
121
  export type BlockRenderProps = {
122
- /** The block value, relations as `{ relationTo, documentId }`. */
122
+ /** The block value; read a relation with `Relation.resolve(block.image).first()`. */
123
123
  block: GenericBlock;
124
124
  /** `layout.sections.0` */
125
125
  path: string;
@@ -194,7 +194,7 @@ 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
- * resolve its relations with `populate` and mount panel fields with `RenderFields`.
197
+ * read its relations with `Relation.resolve` and mount panel fields with `RenderFields`.
198
198
  *
199
199
  * `controls: false` drops the bar above the selected block, for a small one like a button.
200
200
  * Keys, ⌘K and the layers still move, duplicate and remove it.
@@ -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 { Relation, RelationFieldBuilder } from '../index.js';
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<Relation[]>(path) || []);
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: 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 { Relation, RelationFieldBuilder } from '../index.js';
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,7 +3,7 @@
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 { populate } from '../populate.js';
6
+ import { Relation } from '../relation.js';
7
7
  import FileDrop from '../../../panel/components/sections/collection/bulk-upload/FileDrop.svelte';
8
8
  import SpinLoader from '../../../panel/components/ui/spin-loader/SpinLoader.svelte';
9
9
  import { getAPIProxyContext } from '../../../panel/context/api-proxy.svelte.js';
@@ -14,7 +14,7 @@
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 { Relation, RelationFieldBuilder } from '../index.js';
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<Relation[]>(path, config));
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
- populate<unknown>(lookup).then((doc) => {
55
- if (doc && typeof doc === 'object' && '_type' in 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 Relation[];
79
+ }) as RelationRow[];
82
80
  }
83
81
 
84
82
  /* ------------------------------------------------------------ uploading */
@@ -6,6 +6,7 @@
6
6
  import { getAPIProxyContext } from '../../../../panel/context/api-proxy.svelte.js';
7
7
  import {
8
8
  acceptLabel,
9
+ mediaMeta,
9
10
  typeLabel,
10
11
  uploadFiles,
11
12
  type UploadProgress
@@ -41,6 +42,8 @@
41
42
  const APIProxy = getAPIProxyContext();
42
43
  /** What the picker marks, in order. */
43
44
  const selected = $derived(selectedItems.map((item) => item.documentId));
45
+ /** A field of one: its pick, drawn as a card in the zone's place. */
46
+ const picked = $derived(many ? undefined : selectedItems[0]);
44
47
  const accept = $derived(relationConfig.upload?.accept);
45
48
 
46
49
  /* ------------------------------------------------------------ uploading */
@@ -93,7 +96,81 @@
93
96
  const slots = $derived(many ? (SLOTS - (selectedItems.length % SLOTS)) % SLOTS : 0);
94
97
  </script>
95
98
 
96
- {#if !readOnly}
99
+ {#snippet uploading(current: UploadProgress)}
100
+ {t__(
101
+ 'fields.uploading',
102
+ String(Math.min(current.uploaded + current.failed.length + 1, current.total)),
103
+ String(current.total)
104
+ )}
105
+ {/snippet}
106
+
107
+ {#if picked}
108
+ <!--
109
+ One pick: its image, name and size, then upload another file or remove it. A file dropped on
110
+ the card replaces the pick.
111
+ -->
112
+ <FileDrop
113
+ class="rz-relation-upload__card"
114
+ {accept}
115
+ disabled={readOnly || !!progress}
116
+ data-error={hasError ? '' : undefined}
117
+ onfiles={upload}
118
+ >
119
+ {#snippet children({ browse })}
120
+ <a
121
+ class="rz-relation-upload__card-thumb"
122
+ href={panelPath(relationConfig.kebab, picked.documentId)}
123
+ aria-label={picked.title}
124
+ >
125
+ {#if progress}
126
+ <SpinLoader />
127
+ {:else if picked.isImage && picked.url}
128
+ <img src={picked.url} alt="" />
129
+ {:else}
130
+ <span class="rz-relation-upload__type">{typeLabel(picked.mimeType)}</span>
131
+ {/if}
132
+ </a>
133
+ <div class="rz-relation-upload__card-info">
134
+ <a
135
+ class="rz-relation-upload__card-name"
136
+ href={panelPath(relationConfig.kebab, picked.documentId)}
137
+ >
138
+ {picked.title}
139
+ </a>
140
+ <span class="rz-relation-upload__card-meta" aria-live="polite">
141
+ {#if progress}{@render uploading(progress)}{:else}{mediaMeta(picked)}{/if}
142
+ </span>
143
+ </div>
144
+ {#if !readOnly && !progress}
145
+ <div class="rz-relation-upload__card-actions">
146
+ <button
147
+ type="button"
148
+ title={t__('fields.upload_another')}
149
+ aria-label={t__('fields.upload_another')}
150
+ onclick={browse}
151
+ >
152
+ <Upload size={15} />
153
+ </button>
154
+ <button
155
+ type="button"
156
+ title={t__('fields.remove_item', picked.title)}
157
+ aria-label={t__('fields.remove_item', picked.title)}
158
+ onclick={() => removeValue(picked.documentId)}
159
+ >
160
+ <X size={15} />
161
+ </button>
162
+ </div>
163
+ {/if}
164
+ <span class="rz-relation-upload__card-drop" aria-hidden="true">
165
+ <Upload size={15} />
166
+ {t__('fields.drop_to_replace')}
167
+ </span>
168
+ {/snippet}
169
+ </FileDrop>
170
+ {#if failed.length}
171
+ <p class="rz-relation-upload__failed">{t__('fields.upload_failed', failed.join(', '))}</p>
172
+ {/if}
173
+ {:else if !readOnly}
97
174
  <FileDrop
98
175
  class="rz-relation-upload__drop"
99
176
  multiple={many}
@@ -105,13 +182,7 @@
105
182
  {#snippet children({ browse })}
106
183
  {#if progress}
107
184
  <SpinLoader />
108
- <span aria-live="polite">
109
- {t__(
110
- 'fields.uploading',
111
- String(Math.min(progress.uploaded + progress.failed.length + 1, progress.total)),
112
- String(progress.total)
113
- )}
114
- </span>
185
+ <span aria-live="polite">{@render uploading(progress)}</span>
115
186
  {:else}
116
187
  <Upload size={18} />
117
188
  <span>
@@ -132,43 +203,40 @@
132
203
  {/if}
133
204
  {/if}
134
205
 
135
- {#key stamp}
136
- <div
137
- bind:this={list}
138
- class="rz-relation-upload__list"
139
- data-many={many ? '' : null}
140
- data-error={hasError ? '' : null}
141
- >
142
- {#each selectedItems as item (item.documentId)}
143
- <div class="rz-relation-upload__thumb" title={item.title}>
144
- <a
145
- href={panelPath(relationConfig.kebab, item.documentId)}
146
- aria-label={item.title}
147
- draggable="false"
148
- >
149
- {#if item.isImage && item.url}
150
- <img src={item.url} alt="" draggable="false" />
151
- {:else}
152
- <span class="rz-relation-upload__type">{typeLabel(item.mimeType)}</span>
153
- {/if}
154
- </a>
155
- {#if !readOnly}
156
- <button
157
- type="button"
158
- class="rz-relation-upload__remove"
159
- aria-label={t__('fields.remove_item', item.title)}
160
- onclick={() => removeValue(item.documentId)}
206
+ {#if many}
207
+ {#key stamp}
208
+ <div bind:this={list} class="rz-relation-upload__list" data-error={hasError ? '' : null}>
209
+ {#each selectedItems as item (item.documentId)}
210
+ <div class="rz-relation-upload__thumb" title={item.title}>
211
+ <a
212
+ href={panelPath(relationConfig.kebab, item.documentId)}
213
+ aria-label={item.title}
214
+ draggable="false"
161
215
  >
162
- <X size={11} />
163
- </button>
164
- {/if}
165
- </div>
166
- {/each}
167
- {#each { length: slots }, index (index)}
168
- <div class="rz-relation-upload__slot" aria-hidden="true"></div>
169
- {/each}
170
- </div>
171
- {/key}
216
+ {#if item.isImage && item.url}
217
+ <img src={item.url} alt="" draggable="false" />
218
+ {:else}
219
+ <span class="rz-relation-upload__type">{typeLabel(item.mimeType)}</span>
220
+ {/if}
221
+ </a>
222
+ {#if !readOnly}
223
+ <button
224
+ type="button"
225
+ class="rz-relation-upload__remove"
226
+ aria-label={t__('fields.remove_item', item.title)}
227
+ onclick={() => removeValue(item.documentId)}
228
+ >
229
+ <X size={11} />
230
+ </button>
231
+ {/if}
232
+ </div>
233
+ {/each}
234
+ {#each { length: slots }, index (index)}
235
+ <div class="rz-relation-upload__slot" aria-hidden="true"></div>
236
+ {/each}
237
+ </div>
238
+ {/key}
239
+ {/if}
172
240
 
173
241
  <Browse
174
242
  bind:open={browsing}
@@ -199,7 +267,7 @@
199
267
  border-color: var(--rz-danger);
200
268
  }
201
269
 
202
- /* Narrow, in a side panel: the zone on one line. */
270
+ /* Narrow, in a side panel: the zone on one line, the card's image smaller. */
203
271
  @container rz-field-root (max-width: 20rem) {
204
272
  :global(.rz-relation-upload__drop) {
205
273
  flex-direction: row;
@@ -213,6 +281,128 @@
213
281
  :global(.rz-relation-upload__drop small) {
214
282
  flex-basis: 100%;
215
283
  }
284
+ .rz-relation-upload__card-thumb {
285
+ width: var(--rz-size-11);
286
+ height: var(--rz-size-11);
287
+ }
288
+ }
289
+
290
+ /*
291
+ * One pick, in the zone's place: the media's image, its name and size, then its actions, on a
292
+ * well with a hairline. `div.` outweighs the zone's own look.
293
+ */
294
+ :global(div.rz-file-drop.rz-relation-upload__card) {
295
+ flex-direction: row;
296
+ justify-content: flex-start;
297
+ gap: var(--rz-size-3);
298
+ padding: var(--rz-size-2);
299
+ border: 0;
300
+ border-radius: var(--rz-radius-lg);
301
+ box-shadow: inset 0 0 0 1px var(--rz-border);
302
+ color: var(--rz-fg);
303
+ text-align: left;
304
+ }
305
+
306
+ :global(div.rz-file-drop.rz-relation-upload__card[data-over]) {
307
+ box-shadow: inset 0 0 0 1px var(--rz-accent-border);
308
+ }
309
+
310
+ .rz-relation-upload__card-thumb {
311
+ display: grid;
312
+ place-items: center;
313
+ flex-shrink: 0;
314
+ width: var(--rz-size-14);
315
+ height: var(--rz-size-14);
316
+ overflow: hidden;
317
+ border-radius: var(--rz-radius-md);
318
+ background-color: var(--rz-bg-well);
319
+
320
+ img {
321
+ width: 100%;
322
+ height: 100%;
323
+ -o-object-fit: cover;
324
+ object-fit: cover;
325
+ }
326
+ &:focus-visible {
327
+ outline: 2px solid var(--rz-accent-border);
328
+ outline-offset: 2px;
329
+ }
330
+ }
331
+
332
+ .rz-relation-upload__card-info {
333
+ display: flex;
334
+ flex-direction: column;
335
+ gap: var(--rz-size-0-5);
336
+ flex: 1;
337
+ min-width: 0;
338
+ }
339
+
340
+ .rz-relation-upload__card-name {
341
+ font-variation-settings: 'wght' 500;
342
+ font-weight: 500;
343
+ overflow: hidden;
344
+ text-overflow: ellipsis;
345
+ white-space: nowrap;
346
+ &:hover {
347
+ text-decoration: underline;
348
+ text-underline-offset: 2px;
349
+ }
350
+ &:focus-visible {
351
+ outline: 2px solid var(--rz-accent-border);
352
+ outline-offset: 2px;
353
+ border-radius: var(--rz-radius-sm);
354
+ }
355
+ }
356
+
357
+ .rz-relation-upload__card-meta {
358
+ color: var(--rz-fg-subtle);
359
+ font-size: var(--rz-text-sm);
360
+ }
361
+
362
+ .rz-relation-upload__card-actions {
363
+ display: flex;
364
+ gap: var(--rz-size-0-5);
365
+
366
+ button {
367
+ display: grid;
368
+ place-items: center;
369
+ width: var(--rz-size-7);
370
+ height: var(--rz-size-7);
371
+ border-radius: var(--rz-radius-sm);
372
+ color: var(--rz-fg-muted);
373
+ &:hover {
374
+ background-color: var(--rz-bg-hover);
375
+ color: var(--rz-fg);
376
+ }
377
+ &:focus-visible {
378
+ outline: 2px solid var(--rz-accent-border);
379
+ outline-offset: 2px;
380
+ }
381
+ }
382
+ }
383
+
384
+ /* A file over the card: the image dims, "Drop to replace" takes the name's place. */
385
+ .rz-relation-upload__card-drop {
386
+ display: none;
387
+ align-items: center;
388
+ gap: var(--rz-size-2);
389
+ flex: 1;
390
+ color: var(--rz-accent-text);
391
+ font-variation-settings: 'wght' 500;
392
+ font-weight: 500;
393
+ }
394
+
395
+ :global(.rz-relation-upload__card[data-over]) {
396
+ .rz-relation-upload__card-drop {
397
+ display: flex;
398
+ }
399
+ .rz-relation-upload__card-info,
400
+ .rz-relation-upload__card-actions {
401
+ display: none;
402
+ }
403
+ .rz-relation-upload__card-thumb {
404
+ opacity: 0.45;
405
+ }
216
406
  }
217
407
 
218
408
  .rz-relation-upload__failed {
@@ -276,7 +466,7 @@
276
466
  }
277
467
  }
278
468
 
279
- .rz-relation-upload__list[data-many] .rz-relation-upload__thumb {
469
+ .rz-relation-upload__list .rz-relation-upload__thumb {
280
470
  cursor: grab;
281
471
  }
282
472
 
@@ -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 Relation = {
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 `Relation` is that `ownerId` is not known — a create resolves it
56
- * after inserting the owner. Declared in the sqlite adapter until now, which meant core's
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<Relation, 'ownerId'> & {
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[] // When depth > 0, fully populated docs
53
- | { id?: string; relationTo: string; documentId: string }[] // When depth = 0, relation objects
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>`;