Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(richtext-lexical): improve types of UploadData #10982

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/payload/src/uploads/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ export type FileSize = {
width: null | number
}

/**
* FileSize_P4 is a more precise type, and will replace FileSize in Payload v4.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this naming convention? Feels like _P4 doesn't feel meaningful. What is the intent?

Copy link
Contributor Author

@GermanJablo GermanJablo Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the comment says, this is to indicate that it will be the type in Payload v4. I originally used _V4, but changed it to _P4 so that no one thinks it is the version of this type, but the version of Payload.
Alternatively I could use _Payload_V4 or _Payload_v4. Any preferences?

* I am not encouraging users to use this type via tsdoc yet, in case we find more breaking changes to do prior to v4.
GermanJablo marked this conversation as resolved.
Show resolved Hide resolved
* @internal
*/
export type FileSize_P4 = {
url: null | string
} & FileSize

export type FileSizes = {
[size: string]: FileSize
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
import type { FileData, FileSize, TypeWithID } from 'payload'
import type { FileSize_P4 } from 'payload'

import type { UploadData_P4 } from '../../../../../../features/upload/server/nodes/UploadNode.js'
import type { SerializedUploadNode } from '../../../../../../nodeTypes.js'
import type { JSXConverters } from '../types.js'

export const UploadJSXConverter: JSXConverters<SerializedUploadNode> = {
upload: ({ node }) => {
const uploadDocument: {
value?: FileData & TypeWithID
} = node as any

const url = uploadDocument?.value?.url
// TO-DO (v4): SerializedUploadNode should use UploadData_P4
const uploadDocument = node as UploadData_P4
if (typeof uploadDocument.value !== 'object') {
return null
}
const url = uploadDocument.value.url

/**
* If the upload is not an image, return a link to the upload
*/
if (!uploadDocument?.value?.mimeType?.startsWith('image')) {
if (!uploadDocument.value.mimeType.startsWith('image')) {
return (
<a href={url} rel="noopener noreferrer">
{uploadDocument.value?.filename}
{uploadDocument.value.filename}
</a>
)
}

/**
* If the upload is a simple image with no different sizes, return a simple img tag
*/
if (!uploadDocument?.value?.sizes || !Object.keys(uploadDocument?.value?.sizes).length) {
if (!Object.keys(uploadDocument.value.sizes).length) {
return (
<img
alt={uploadDocument?.value?.filename}
height={uploadDocument?.value?.height}
alt={uploadDocument.value.filename}
height={uploadDocument.value.height}
src={url}
width={uploadDocument?.value?.width}
width={uploadDocument.value.width}
/>
)
}
Expand All @@ -42,13 +44,12 @@ export const UploadJSXConverter: JSXConverters<SerializedUploadNode> = {
const pictureJSX: React.ReactNode[] = []

// Iterate through each size in the data.sizes object
for (const size in uploadDocument.value?.sizes) {
const imageSize: {
url?: string
} & FileSize = uploadDocument.value?.sizes[size]
for (const size in uploadDocument.value.sizes) {
const imageSize = uploadDocument.value.sizes[size] as FileSize_P4

// Skip if any property of the size object is null
if (
!imageSize ||
!imageSize.width ||
!imageSize.height ||
!imageSize.mimeType ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
NodeKey,
Spread,
} from 'lexical'
import type { CollectionSlug, DataFromCollectionSlug, JsonObject } from 'payload'
import type { FileData, JsonObject, TypedCollection, TypeWithID } from 'payload'
import type { JSX } from 'react'

import { DecoratorBlockNode } from '@lexical/react/LexicalDecoratorBlockNode.js'
Expand All @@ -17,15 +17,27 @@ import { $applyNodeReplacement } from 'lexical'
import * as React from 'react'

export type UploadData<TUploadExtraFieldsData extends JsonObject = JsonObject> = {
[TCollectionSlug in CollectionSlug]: {
fields: TUploadExtraFieldsData
// Every lexical node that has sub-fields needs to have a unique ID. This is the ID of this upload node, not the ID of the linked upload document
id: string
relationTo: TCollectionSlug
// Value can be just the document ID, or the full, populated document
value: DataFromCollectionSlug<TCollectionSlug> | number | string
}
}[CollectionSlug]
fields: TUploadExtraFieldsData
/** Every lexical node that has sub-fields needs to have a unique ID. This is the ID of this upload node, not the ID of the linked upload document */
id: string
relationTo: string
/** Value can be just the document ID, or the full, populated document */
value: number | string | TypedCollection
}

/**
* UploadData_P4 is a more precise type, and will replace UploadData in Payload v4.
* I am not encouraging users to use this type via tsdoc yet, in case we find more breaking changes to do prior to v4.
GermanJablo marked this conversation as resolved.
Show resolved Hide resolved
* @internal
*/
export type UploadData_P4<TUploadExtraFieldsData extends JsonObject = JsonObject> = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above on _P4 naming.

fields: TUploadExtraFieldsData
// Every lexical node that has sub-fields needs to have a unique ID. This is the ID of this upload node, not the ID of the linked upload document
id: string
relationTo: string
// Value can be just the document ID, or the full, populated document
value: (FileData & TypeWithID) | number | string
}

export function isGoogleDocCheckboxImg(img: HTMLImageElement): boolean {
return (
Expand Down