Skip to content

Commit

Permalink
feat: support link variants (#368)
Browse files Browse the repository at this point in the history
* refactor: link types

* refactor: clean up after self-review

* feat: support link variants

* test: remove unnecessary test

* refactor: fix name of `getOptionalLinkProperties` file

* fix(migration): only include optional link properties in links

* docs: add `MaybeLink` description

* test: update test

* fix: add optional link fields to link to media migration fields

* fix(migration): add missing `text` property on link to media

* Revert "fix: add optional link fields to link to media migration fields"

This reverts commit 6f89eee.

* Revert "fix(migration): add missing `text` property on link to media"

This reverts commit 27839cf.

---------

Co-authored-by: Daniel Martín <danimartinprieto@gmail.com>
  • Loading branch information
angeloashmore and dani-mp authored Dec 23, 2024
1 parent f4bf4e6 commit a9f4a25
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/lib/getOptionalLinkProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ export const getOptionalLinkProperties = (
res.text = input.text
}

if ("variant" in input) {
res.variant = input.variant
}

return res
}
1 change: 1 addition & 0 deletions src/types/model/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface CustomTypeModelLinkField {
allowText?: boolean
allowTargetBlank?: boolean
repeat?: boolean
variants?: string[]
}
}

Expand Down
1 change: 1 addition & 0 deletions src/types/value/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,5 @@ export type FilledLinkToWebField = {
// this type. The function should check for every property.
export type OptionalLinkProperties = {
text?: string
variant?: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,97 @@ exports[`patches content relationship fields (from Prismic) > withText > static
}
`;

exports[`patches content relationship fields (from Prismic) > withVariant > group 1`] = `
{
"group": [
{
"field": {
"id": "other.id-fromPrismic",
"link_type": "Document",
"variant": "Secondary",
},
},
],
}
`;

exports[`patches content relationship fields (from Prismic) > withVariant > shared slice 1`] = `
{
"slices": [
{
"id": "9b9dd0f2c3f",
"items": [
{
"field": {
"id": "other.id-fromPrismic",
"link_type": "Document",
"variant": "Secondary",
},
},
],
"primary": {
"field": {
"id": "other.id-fromPrismic",
"link_type": "Document",
"variant": "Secondary",
},
"group": [
{
"field": {
"id": "other.id-fromPrismic",
"link_type": "Document",
"variant": "Secondary",
},
},
],
},
"slice_label": null,
"slice_type": "nunc",
"variation": "ullamcorper",
"version": "8bfc905",
},
],
}
`;

exports[`patches content relationship fields (from Prismic) > withVariant > slice 1`] = `
{
"slices": [
{
"id": "5306297c5ed",
"items": [
{
"field": {
"id": "other.id-fromPrismic",
"link_type": "Document",
"variant": "Secondary",
},
},
],
"primary": {
"field": {
"id": "other.id-fromPrismic",
"link_type": "Document",
"variant": "Secondary",
},
},
"slice_label": "Vel",
"slice_type": "hac_habitasse",
},
],
}
`;

exports[`patches content relationship fields (from Prismic) > withVariant > static zone 1`] = `
{
"field": {
"id": "other.id-fromPrismic",
"link_type": "Document",
"variant": "Secondary",
},
}
`;

exports[`patches content relationship fields > existing > group 1`] = `
{
"group": [
Expand Down Expand Up @@ -525,6 +616,97 @@ exports[`patches content relationship fields > existingLongFormWithText > static
}
`;

exports[`patches content relationship fields > existingLongFormWithVariant > group 1`] = `
{
"group": [
{
"field": {
"id": "other.id-existing",
"link_type": "Document",
"variant": "Secondary",
},
},
],
}
`;

exports[`patches content relationship fields > existingLongFormWithVariant > shared slice 1`] = `
{
"slices": [
{
"id": "9b9dd0f2c3f",
"items": [
{
"field": {
"id": "other.id-existing",
"link_type": "Document",
"variant": "Secondary",
},
},
],
"primary": {
"field": {
"id": "other.id-existing",
"link_type": "Document",
"variant": "Secondary",
},
"group": [
{
"field": {
"id": "other.id-existing",
"link_type": "Document",
"variant": "Secondary",
},
},
],
},
"slice_label": null,
"slice_type": "nunc",
"variation": "ullamcorper",
"version": "8bfc905",
},
],
}
`;

exports[`patches content relationship fields > existingLongFormWithVariant > slice 1`] = `
{
"slices": [
{
"id": "5306297c5ed",
"items": [
{
"field": {
"id": "other.id-existing",
"link_type": "Document",
"variant": "Secondary",
},
},
],
"primary": {
"field": {
"id": "other.id-existing",
"link_type": "Document",
"variant": "Secondary",
},
},
"slice_label": "Vel",
"slice_type": "hac_habitasse",
},
],
}
`;

exports[`patches content relationship fields > existingLongFormWithVariant > static zone 1`] = `
{
"field": {
"id": "other.id-existing",
"link_type": "Document",
"variant": "Secondary",
},
}
`;

exports[`patches content relationship fields > lazyExisting > group 1`] = `
{
"group": [
Expand Down
10 changes: 10 additions & 0 deletions test/types/customType-link.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ expectType<prismic.CustomTypeModelLinkField>({
},
})

/**
* Supports optional `variants` property.
*/
expectType<prismic.CustomTypeModelLinkField>({
type: prismic.CustomTypeModelFieldType.Link,
config: {
variants: ["string"],
},
})

/**
* `@prismicio/types` extends `@prismicio/types-internal`
*/
Expand Down
17 changes: 17 additions & 0 deletions test/types/fields-link.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ expectType<prismic.LinkField>({
url: "string",
target: "string",
text: "string",
variant: "string",
})
expectType<prismic.LinkField>({
link_type: prismic.LinkType.Document,
Expand All @@ -54,6 +55,7 @@ expectType<prismic.LinkField>({
isBroken: true,
data: undefined,
text: "string",
variant: "string",
})
expectType<prismic.LinkField>({
link_type: prismic.LinkType.Media,
Expand All @@ -65,19 +67,22 @@ expectType<prismic.LinkField>({
height: "string",
width: "string",
text: "string",
variant: "string",
})
expectType<prismic.LinkField<string, string, never, "filled">>({
link_type: prismic.LinkType.Web,
url: "string",
target: "string",
text: "string",
variant: "string",
})
expectType<prismic.LinkField<string, string, never, "empty">>({
// @ts-expect-error - Empty fields cannot contain a filled link type.
link_type: prismic.LinkType.Web,
url: "string",
target: "string",
text: "string",
variant: "string",
})

/**
Expand Down Expand Up @@ -144,6 +149,18 @@ expectType<prismic.LinkField<string, string, never, "filled">>({
text: "string",
})

/**
* Empty state with variant.
*/
expectType<prismic.LinkField>({
link_type: prismic.LinkType.Any,
variant: "string",
})
expectType<prismic.LinkField<string, string, never, "empty">>({
link_type: prismic.LinkType.Any,
variant: "string",
})

/**
* Supports custom document type for document links.
*/
Expand Down
27 changes: 23 additions & 4 deletions test/writeClient-migrate-patch-contentRelationship.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ testMigrationFieldPatching<
text: "foo",
}
},
existingLongFormWithVariant: ({ existingDocuments }) => {
return {
link_type: LinkType.Document,
id: existingDocuments[0],
variant: "Secondary",
}
},
otherCreate: ({ otherCreateDocument }) => otherCreateDocument,
lazyExisting: ({ existingDocuments }) => {
return () => existingDocuments[0]
Expand Down Expand Up @@ -86,11 +93,23 @@ testMigrationFieldPatching<ContentRelationshipField>(
contentRelationship.id =
otherFromPrismicDocument.originalPrismicDocument!.id

// TODO: Remove when link text PR is merged
// @ts-expect-error - Future-proofing for link text
contentRelationship.text = "foo"
return {
...contentRelationship,
text: "foo",
}
},
withVariant: ({ ctx, otherFromPrismicDocument }) => {
const contentRelationship = ctx.mock.value.link({
type: LinkType.Document,
})
// `migrationDocuments` contains documents from "another repository"
contentRelationship.id =
otherFromPrismicDocument.originalPrismicDocument!.id

return contentRelationship
return {
...contentRelationship,
variant: "Secondary",
}
},
broken: () => {
return {
Expand Down

0 comments on commit a9f4a25

Please sign in to comment.