Skip to content

Commit

Permalink
fix(richtext-lexical): upload, relationship and block node insertion …
Browse files Browse the repository at this point in the history
…fails sometimes (#6390)

Backport of #6389
  • Loading branch information
AlessioGr committed May 16, 2024
1 parent 82b88a3 commit 48a410e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ export function BlocksPlugin(): JSX.Element | null {
INSERT_BLOCK_COMMAND,
(payload: InsertBlockPayload) => {
editor.update(() => {
const blockNode = $createBlockNode(payload)

const selection = $getSelection() || $getPreviousSelection()

if ($isRangeSelection(selection)) {
const blockNode = $createBlockNode(payload)
$insertNodeToNearestRoot(blockNode)

const { focus } = selection
const focusNode = focus.getNode()

Expand All @@ -51,8 +52,6 @@ export function BlocksPlugin(): JSX.Element | null {
) {
focusNode.remove()
}

$insertNodeToNearestRoot(blockNode)
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ export function RelationshipPlugin(props?: RelationshipFeatureProps): JSX.Elemen
return editor.registerCommand<RelationshipData>(
INSERT_RELATIONSHIP_COMMAND,
(payload) => {
const relationshipNode = $createRelationshipNode(payload)

const selection = $getSelection() || $getPreviousSelection()

if ($isRangeSelection(selection)) {
const relationshipNode = $createRelationshipNode(payload)
$insertNodeToNearestRoot(relationshipNode)

const { focus } = selection
const focusNode = focus.getNode()

Expand All @@ -66,8 +67,6 @@ export function RelationshipPlugin(props?: RelationshipFeatureProps): JSX.Elemen
) {
focusNode.remove()
}

$insertNodeToNearestRoot(relationshipNode)
}

return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const ExtraFieldsUploadDrawer: React.FC<
const fieldSchema = useMemo(() => {
const fieldSchemaUnSanitized = (
editorConfig?.resolvedFeatureMap.get('upload')?.props as UploadFeatureProps
)?.collections?.[relatedCollection.slug].fields
)?.collections?.[relatedCollection.slug]?.fields

const validRelationships = config.collections.map((c) => c.slug) || []
// Sanitize custom fields here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,20 @@ export function UploadPlugin(): JSX.Element | null {
INSERT_UPLOAD_COMMAND,
(payload: InsertUploadPayload) => {
editor.update(() => {
const uploadNode = $createUploadNode({
data: {
fields: payload.fields,
relationTo: payload.relationTo,
value: {
id: payload.id,
},
},
})

const selection = $getSelection() || $getPreviousSelection()

if ($isRangeSelection(selection)) {
const uploadNode = $createUploadNode({
data: {
fields: payload.fields,
relationTo: payload.relationTo,
value: {
id: payload.id,
},
},
})
$insertNodeToNearestRoot(uploadNode)

const { focus } = selection
const focusNode = focus.getNode()

Expand All @@ -65,8 +66,6 @@ export function UploadPlugin(): JSX.Element | null {
) {
focusNode.remove()
}

$insertNodeToNearestRoot(uploadNode)
}
})

Expand Down

0 comments on commit 48a410e

Please sign in to comment.