Skip to content

Commit ab564d3

Browse files
authored
feat(richtext-lexical): respect imageURL for blocks and inline blocks (#10532)
Block and inline block icons in the slash menu / toolbars can now be customized: ![CleanShot 2025-01-12 at 19 41 09@2x](https://github.com/user-attachments/assets/80f8bbac-0ad8-43fc-8a6c-eae055a8ebbc)
1 parent 26711a7 commit ab564d3

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from 'react'
2+
3+
import { BlockIcon } from '../../../lexical/ui/icons/Block/index.js'
4+
5+
export function getBlockImageComponent(imageURL?: string, imageAltText?: string) {
6+
if (!imageURL) {
7+
return BlockIcon
8+
}
9+
10+
return () => (
11+
<img
12+
alt={imageAltText ?? 'Block Image'}
13+
className="lexical-block-custom-image"
14+
src={imageURL}
15+
style={{ maxHeight: 20, maxWidth: 20 }}
16+
/>
17+
)
18+
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import type { ToolbarGroup, ToolbarGroupItem } from '../../toolbars/types.js'
1414
import { BlockIcon } from '../../../lexical/ui/icons/Block/index.js'
1515
import { InlineBlocksIcon } from '../../../lexical/ui/icons/InlineBlocks/index.js'
1616
import { createClientFeature } from '../../../utilities/createClientFeature.js'
17+
import { getBlockImageComponent } from './getBlockImageComponent.js'
1718
import { BlockNode } from './nodes/BlocksNode.js'
1819
import { InlineBlockNode } from './nodes/InlineBlocksNode.js'
1920
import { INSERT_BLOCK_COMMAND, INSERT_INLINE_BLOCK_COMMAND } from './plugin/commands.js'
2021
import { BlocksPlugin } from './plugin/index.js'
21-
2222
export const BlocksFeatureClient = createClientFeature(
2323
({ featureClientSchemaMap, props, schemaPath }) => {
2424
const schemaMapRenderedBlockPathPrefix = `${schemaPath}.lexical_internal_feature.blocks.lexical_blocks`
@@ -64,7 +64,7 @@ export const BlocksFeatureClient = createClientFeature(
6464
? {
6565
items: clientBlocks.map((block) => {
6666
return {
67-
Icon: BlockIcon,
67+
Icon: getBlockImageComponent(block.imageURL, block.imageAltText),
6868
key: 'block-' + block.slug,
6969
keywords: ['block', 'blocks', block.slug],
7070
label: ({ i18n }) => {
@@ -130,7 +130,7 @@ export const BlocksFeatureClient = createClientFeature(
130130
ChildComponent: BlockIcon,
131131
items: clientBlocks.map((block, index) => {
132132
return {
133-
ChildComponent: BlockIcon,
133+
ChildComponent: getBlockImageComponent(block.imageURL, block.imageAltText),
134134
isActive: undefined, // At this point, we would be inside a sub-richtext-editor. And at this point this will be run against the focused sub-editor, not the parent editor which has the actual block. Thus, no point in running this
135135
key: 'block-' + block.slug,
136136
label: ({ i18n }) => {
@@ -159,7 +159,9 @@ export const BlocksFeatureClient = createClientFeature(
159159
ChildComponent: InlineBlocksIcon,
160160
items: clientInlineBlocks.map((inlineBlock, index) => {
161161
return {
162-
ChildComponent: InlineBlocksIcon,
162+
ChildComponent: inlineBlock.imageURL
163+
? getBlockImageComponent(inlineBlock.imageURL, inlineBlock.imageAltText)
164+
: InlineBlocksIcon,
163165
isActive: undefined,
164166
key: 'inlineBlock-' + inlineBlock.slug,
165167
label: ({ i18n }) => {

0 commit comments

Comments
 (0)