Skip to content

Commit

Permalink
feat: add reference field (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenle authored Feb 22, 2024
1 parent 83824fc commit b0fed8a
Show file tree
Hide file tree
Showing 24 changed files with 1,331 additions and 740 deletions.
5 changes: 5 additions & 0 deletions .changeset/tiny-scissors-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@blinkk/root-cms': minor
---

feat: add reference field
12 changes: 12 additions & 0 deletions examples/blog/collections/BlogPosts.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ export default schema.collection({
label: 'Published Date Override',
help: 'Override for the "Published" date.'
}),
schema.reference({
id: 'parentPost',
label: 'Parent Post',
help: 'Optional parent post for breadcrumbs.',
collections: ['BlogPosts'],
}),
schema.array({
id: 'relatedPosts',
label: 'Related Posts',
help: 'Suggest related blog posts to read.',
of: schema.reference({collections: ['BlogPosts', 'BlogPostsSandbox']}),
}),
],
}),
],
Expand Down
19 changes: 18 additions & 1 deletion examples/blog/root-cms.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,20 @@ export interface RootCMSRichText {
blocks: RootCMSRichTextBlock[];
}

export interface RootCMSReference {
/** The id of the doc, e.g. "Pages/foo-bar". */
id: string;
/** The collection id of the doc, e.g. "Pages". */
collection: string;
/** The slug of the doc, e.g. "foo-bar". */
slug: string;
}

export interface RootCMSDoc<Fields extends {}> {
/** The id of the doc, e.g. "Pages/foo-bar". */
id: string;
/** The collection id of the doc, e.g. "Pages". */
collectionId: string;
collection: string;
/** The slug of the doc, e.g. "foo-bar". */
slug: string;
/** System-level metadata. */
Expand Down Expand Up @@ -80,6 +89,10 @@ export interface BlogPostsFields {
};
/** Published Date Override. Override for the "Published" date. */
publishedAtOverride?: number;
/** Parent Post. Optional parent post for breadcrumbs. */
parentPost?: RootCMSReference;
/** Related Posts. Suggest related blog posts to read. */
relatedPosts?: RootCMSReference[];
};
}

Expand Down Expand Up @@ -123,6 +136,10 @@ export interface BlogPostsSandboxFields {
};
/** Published Date Override. Override for the "Published" date. */
publishedAtOverride?: number;
/** Parent Post. Optional parent post for breadcrumbs. */
parentPost?: RootCMSReference;
/** Related Posts. Suggest related blog posts to read. */
relatedPosts?: RootCMSReference[];
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/create-root/src/root-version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// This file is autogenerated by update-root-version.mjs.
export const ROOT_VERSION = '1.0.0-rc.24';
export const ROOT_VERSION = '1.0.0-rc.25';
15 changes: 14 additions & 1 deletion packages/root-cms/cli/commands/generate-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,20 @@ export interface RootCMSRichText {
blocks: RootCMSRichTextBlock[];
}
export interface RootCMSReference {
/** The id of the doc, e.g. "Pages/foo-bar". */
id: string;
/** The collection id of the doc, e.g. "Pages". */
collection: string;
/** The slug of the doc, e.g. "foo-bar". */
slug: string;
}
export interface RootCMSDoc<Fields extends {}> {
/** The id of the doc, e.g. "Pages/foo-bar". */
id: string;
/** The collection id of the doc, e.g. "Pages". */
collectionId: string;
collection: string;
/** The slug of the doc, e.g. "foo-bar". */
slug: string;
/** System-level metadata. */
Expand Down Expand Up @@ -142,6 +151,10 @@ function fieldType(field: Field): dom.Type {
const oneofType = dom.create.namedTypeReference('RootCMSOneOf');
return oneofType;
}
if (field.type === 'reference') {
const richtextType = dom.create.namedTypeReference('RootCMSReference');
return richtextType;
}
if (field.type === 'richtext') {
const richtextType = dom.create.namedTypeReference('RootCMSRichText');
return richtextType;
Expand Down
24 changes: 22 additions & 2 deletions packages/root-cms/core/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@ export function richtext(field: Omit<RichTextField, 'type'>): RichTextField {
return {...field, type: 'richtext'};
}

export type ReferenceField = CommonFieldProps & {
type: 'reference';
/** List of collection ids the reference can be chosen from. */
collections?: string[];
/** Initial collection to show when picking a reference. */
initialCollection?: string;
/** Label for the button. Defaults to "Select". */
buttonLabel?: string;
};

export function reference(field: Omit<ReferenceField, 'type'>): ReferenceField {
return {...field, type: 'reference'};
}

export type Field =
| StringField
| NumberField
Expand All @@ -189,15 +203,21 @@ export type Field =
| ObjectField
| ArrayField
| OneOfField
| RichTextField;
| RichTextField
| ReferenceField;

/**
* Similar to {@link Field} but with a required `id`.
* TODO(stevenle): fix this.
*/
export type FieldWithId = Field;

export type ObjectLikeField = ImageField | FileField | ObjectField | OneOfField;
export type ObjectLikeField =
| ImageField
| FileField
| ObjectField
| OneOfField
| ReferenceField;

export interface Schema {
name: string;
Expand Down
Loading

0 comments on commit b0fed8a

Please sign in to comment.