Skip to content

Commit e413e1d

Browse files
authored
chore(richtext-lexical): fix unchecked indexed acess in lexical blocks feature (#11013)
This PR is part of the process of fixing `noUncheckedIndexedAccess` in richtext-lexical.
1 parent bdbb999 commit e413e1d

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

packages/richtext-lexical/src/features/blocks/client/component/index.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export const BlockComponent: React.FC<Props> = (props) => {
174174

175175
const clientSchemaMap = featureClientSchemaMap['blocks']
176176

177-
const blocksField: BlocksFieldClient | undefined = clientSchemaMap[
177+
const blocksField: BlocksFieldClient | undefined = clientSchemaMap?.[
178178
componentMapRenderedBlockPath
179179
]?.[0] as BlocksFieldClient
180180

@@ -208,7 +208,9 @@ export const BlockComponent: React.FC<Props> = (props) => {
208208
return prevFormState
209209
}
210210

211-
newFormState.blockName = prevFormState.blockName
211+
if (prevFormState.blockName) {
212+
newFormState.blockName = prevFormState.blockName
213+
}
212214

213215
const newFormStateData: BlockFields = reduceFieldsToValues(
214216
removeEmptyArrayValues({
@@ -436,6 +438,8 @@ export const BlockComponent: React.FC<Props> = (props) => {
436438
],
437439
)
438440

441+
const clientBlockFields = clientBlock?.fields ?? []
442+
439443
const BlockDrawer = useMemo(
440444
() => () => (
441445
<EditDepthProvider>
@@ -449,7 +453,7 @@ export const BlockComponent: React.FC<Props> = (props) => {
449453
{initialState ? (
450454
<>
451455
<RenderFields
452-
fields={clientBlock?.fields}
456+
fields={clientBlockFields}
453457
forceRender
454458
parentIndexPath=""
455459
parentPath="" // See Blocks feature path for details as for why this is empty
@@ -488,7 +492,7 @@ export const BlockComponent: React.FC<Props> = (props) => {
488492
return await onChange({ formState, submit: true })
489493
},
490494
]}
491-
fields={clientBlock?.fields}
495+
fields={clientBlockFields}
492496
initialState={initialState}
493497
onChange={[onChange]}
494498
onSubmit={(formState, newData) => {
@@ -512,7 +516,7 @@ export const BlockComponent: React.FC<Props> = (props) => {
512516
CustomBlock={CustomBlock}
513517
EditButton={EditButton}
514518
errorCount={errorCount}
515-
formSchema={clientBlock?.fields}
519+
formSchema={clientBlockFields}
516520
initialState={initialState}
517521
nodeKey={nodeKey}
518522
RemoveButton={RemoveButton}
@@ -523,6 +527,7 @@ export const BlockComponent: React.FC<Props> = (props) => {
523527
BlockCollapsible,
524528
BlockDrawer,
525529
CustomBlock,
530+
clientBlockFields,
526531
RemoveButton,
527532
EditButton,
528533
editor,

packages/richtext-lexical/src/features/blocks/client/component/removeEmptyArrayValues.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { FormState } from 'payload'
1010
export function removeEmptyArrayValues({ fields }: { fields: FormState }): FormState {
1111
for (const key in fields) {
1212
const field = fields[key]
13-
if (Array.isArray(field.rows) && 'value' in field) {
13+
if (Array.isArray(field?.rows) && 'value' in field) {
1414
field.disableFormData = true
1515
}
1616
}

packages/richtext-lexical/src/features/blocks/client/componentInline/index.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,29 +112,25 @@ export const InlineBlockComponent: React.FC<Props> = (props) => {
112112

113113
const clientSchemaMap = featureClientSchemaMap['blocks']
114114

115-
const blocksField: BlocksFieldClient = clientSchemaMap[
115+
const blocksField: BlocksFieldClient = clientSchemaMap?.[
116116
componentMapRenderedBlockPath
117117
]?.[0] as BlocksFieldClient
118118

119119
const clientBlock = blocksField?.blocks?.[0]
120120

121+
const clientBlockFields = clientBlock?.fields ?? []
122+
121123
// Open drawer on "mount"
122124
useEffect(() => {
123125
if (!firstTimeDrawer.current && createdInlineBlock?.getKey() === nodeKey) {
124126
// > 2 because they always have "id" and "blockName" fields
125-
if (clientBlock?.fields?.length > 2) {
127+
if (clientBlockFields.length > 2) {
126128
toggleDrawer()
127129
}
128130
setCreatedInlineBlock?.(undefined)
129131
firstTimeDrawer.current = true
130132
}
131-
}, [
132-
clientBlock?.fields?.length,
133-
createdInlineBlock,
134-
nodeKey,
135-
setCreatedInlineBlock,
136-
toggleDrawer,
137-
])
133+
}, [clientBlockFields.length, createdInlineBlock, nodeKey, setCreatedInlineBlock, toggleDrawer])
138134

139135
const removeInlineBlock = useCallback(() => {
140136
editor.update(() => {

packages/richtext-lexical/src/features/blocks/client/index.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,37 @@ export const BlocksFeatureClient = createClientFeature(
2525
const schemaMapRenderedInlineBlockPathPrefix = `${schemaPath}.lexical_internal_feature.blocks.lexical_inline_blocks`
2626
const clientSchema = featureClientSchemaMap['blocks']
2727

28+
if (!clientSchema) {
29+
return {}
30+
}
31+
2832
const blocksFields: BlocksFieldClient[] = Object.entries(clientSchema)
2933
.filter(
3034
([key]) =>
3135
key.startsWith(schemaMapRenderedBlockPathPrefix + '.') &&
3236
!key.replace(schemaMapRenderedBlockPathPrefix + '.', '').includes('.'),
3337
)
34-
.map(([key, value]) => value[0] as BlocksFieldClient)
38+
.map(([, value]) => value[0] as BlocksFieldClient)
3539

3640
const inlineBlocksFields: BlocksFieldClient[] = Object.entries(clientSchema)
3741
.filter(
3842
([key]) =>
3943
key.startsWith(schemaMapRenderedInlineBlockPathPrefix + '.') &&
4044
!key.replace(schemaMapRenderedInlineBlockPathPrefix + '.', '').includes('.'),
4145
)
42-
.map(([key, value]) => value[0] as BlocksFieldClient)
46+
.map(([, value]) => value[0] as BlocksFieldClient)
4347

44-
const clientBlocks: ClientBlock[] = blocksFields.map((field) => {
45-
return field.blocks[0]
46-
})
48+
const clientBlocks: ClientBlock[] = blocksFields
49+
.map((field) => {
50+
return field.blocks[0]
51+
})
52+
.filter((block) => block !== undefined)
4753

48-
const clientInlineBlocks: ClientBlock[] = inlineBlocksFields.map((field) => {
49-
return field.blocks[0]
50-
})
54+
const clientInlineBlocks: ClientBlock[] = inlineBlocksFields
55+
.map((field) => {
56+
return field.blocks[0]
57+
})
58+
.filter((block) => block !== undefined)
5159

5260
return {
5361
nodes: [BlockNode, InlineBlockNode],

0 commit comments

Comments
 (0)