-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
fix(richtext-lexical): incorrect UploadData types #11288
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,34 +8,29 @@ export const UploadJSXConverter: JSXConverters<SerializedUploadNode> = { | |
upload: ({ node }) => { | ||
// TO-DO (v4): SerializedUploadNode should use UploadData_P4 | ||
const uploadDocument = node as UploadDataImproved | ||
if (typeof uploadDocument.value !== 'object') { | ||
if (typeof uploadDocument?.value !== 'object') { | ||
return null | ||
} | ||
const url = uploadDocument.value.url | ||
|
||
const value = uploadDocument.value | ||
const url = value.url | ||
|
||
/** | ||
* If the upload is not an image, return a link to the upload | ||
*/ | ||
if (!uploadDocument.value.mimeType.startsWith('image')) { | ||
if (!value.mimeType.startsWith('image')) { | ||
return ( | ||
<a href={url} rel="noopener noreferrer"> | ||
{uploadDocument.value.filename} | ||
{value.filename} | ||
</a> | ||
) | ||
} | ||
|
||
/** | ||
* If the upload is a simple image with no different sizes, return a simple img tag | ||
*/ | ||
if (!Object.keys(uploadDocument.value.sizes).length) { | ||
return ( | ||
<img | ||
alt={uploadDocument.value.filename} | ||
height={uploadDocument.value.height} | ||
src={url} | ||
width={uploadDocument.value.width} | ||
/> | ||
) | ||
if (!Object.keys(value.sizes).length) { | ||
return <img alt={value.filename} height={value.height} src={url} width={value.width} /> | ||
} | ||
Comment on lines
+15
to
34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now almost all things derived from value are "any" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added a type assertion in https://github.com/payloadcms/payload/pull/11292/files. We could have it fall back to |
||
|
||
/** | ||
|
@@ -44,8 +39,8 @@ 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 = uploadDocument.value.sizes[size] as FileSizeImproved | ||
for (const size in value.sizes) { | ||
const imageSize = value.sizes[size] as FileSizeImproved | ||
|
||
// Skip if any property of the size object is null | ||
if ( | ||
|
@@ -74,11 +69,11 @@ export const UploadJSXConverter: JSXConverters<SerializedUploadNode> = { | |
// Add the default img tag | ||
pictureJSX.push( | ||
<img | ||
alt={uploadDocument.value?.filename} | ||
height={uploadDocument.value?.height} | ||
alt={value?.filename} | ||
height={value?.height} | ||
key={'image'} | ||
src={url} | ||
width={uploadDocument.value?.width} | ||
width={value?.width} | ||
/>, | ||
) | ||
return <picture>{pictureJSX}</picture> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,13 @@ import type { | |
NodeKey, | ||
Spread, | ||
} from 'lexical' | ||
import type { FileData, JsonObject, TypedCollection, TypeWithID } from 'payload' | ||
import type { | ||
CollectionSlug, | ||
DataFromCollectionSlug, | ||
JsonObject, | ||
TypedUploadCollection, | ||
UploadCollectionSlug, | ||
} from 'payload' | ||
import type { JSX } from 'react' | ||
|
||
import { DecoratorBlockNode } from '@lexical/react/LexicalDecoratorBlockNode.js' | ||
|
@@ -17,28 +23,29 @@ import { $applyNodeReplacement } from 'lexical' | |
import * as React from 'react' | ||
|
||
export type UploadData<TUploadExtraFieldsData extends JsonObject = JsonObject> = { | ||
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 | ||
} | ||
[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 | ||
AlessioGr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
value: DataFromCollectionSlug<TCollectionSlug> | number | string | ||
} | ||
}[CollectionSlug] | ||
|
||
// TODO: deprecate in Payload v4. | ||
/** | ||
* UploadDataImproved is a more precise type, and will replace UploadData in Payload v4. | ||
* This type is for internal use only as it will be deprecated in the future. | ||
* @internal | ||
* @todo Replace UploadData with UploadDataImproved | ||
*/ | ||
AlessioGr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
export type UploadDataImproved<TUploadExtraFieldsData extends JsonObject = JsonObject> = { | ||
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 | ||
} | ||
[TCollectionSlug in UploadCollectionSlug]: { | ||
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: number | string | TypedUploadCollection[TCollectionSlug] | ||
} | ||
Comment on lines
-40
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will only be the case in our monorepo though - it will be more precise in a real project |
||
}[UploadCollectionSlug] | ||
|
||
export function isGoogleDocCheckboxImg(img: HTMLImageElement): boolean { | ||
return ( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess ts was asking for optional chaining here? any reason why it is needed now? afaik value should be defined