Skip to content

feat(ui): fixed toolbar group customization #12108

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

Open
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ToolbarGroup } from '../../types.js'

import { createServerFeature } from '../../../../utilities/createServerFeature.js'

export type FixedToolbarFeatureProps = {
Expand All @@ -9,6 +11,12 @@ export type FixedToolbarFeatureProps = {
* This means that if the editor has a child-editor, and the child-editor is focused, the toolbar will apply to the child-editor, not the parent editor with this feature added.
*/
applyToFocusedEditor?: boolean
/**
* Custom configurations for toolbar groups
* Key is the group key (e.g. 'format', 'indent', 'align')
* Value is a partial ToolbarGroup object that will be merged with the default configuration
*/
customGroups?: Record<string, Partial<ToolbarGroup>>
/**
* @default false
*
Expand All @@ -26,6 +34,7 @@ export const FixedToolbarFeature = createServerFeature<
const sanitizedProps: FixedToolbarFeatureProps = {
applyToFocusedEditor:
props?.applyToFocusedEditor === undefined ? false : props.applyToFocusedEditor,
customGroups: props?.customGroups,
disableIfParentHasFixedToolbar:
props?.disableIfParentHasFixedToolbar === undefined
? false
Expand Down
25 changes: 25 additions & 0 deletions packages/richtext-lexical/src/lexical/config/client/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import type { EditorConfig as LexicalEditorConfig } from 'lexical'

import { deepMerge } from 'payload/shared'

import type { ToolbarGroup } from '../../../features/toolbars/types.js'
import type {
ResolvedClientFeatureMap,
SanitizedClientFeatures,
Expand Down Expand Up @@ -31,6 +34,17 @@ export const sanitizeClientFeatures = (
},
}

// Allow customization of groups for toolbarFixed
let customGroups: Record<string, Partial<ToolbarGroup>> = {}
features.forEach((feature) => {
if (feature.key === 'toolbarFixed' && feature.sanitizedClientFeatureProps?.customGroups) {
customGroups = {
...customGroups,
...feature.sanitizedClientFeatureProps.customGroups,
}
}
})

if (!features?.size) {
return sanitized
}
Expand Down Expand Up @@ -158,6 +172,17 @@ export const sanitizeClientFeatures = (
sanitized.enabledFeatures.push(feature.key)
})

// Apply custom group configurations to toolbarFixed groups
if (Object.keys(customGroups).length > 0) {
sanitized.toolbarFixed.groups = sanitized.toolbarFixed.groups.map((group) => {
const customConfig = customGroups[group.key]
if (customConfig) {
return deepMerge(group, customConfig)
}
return group
})
}

// Sort sanitized.toolbarInline.groups by order property
sanitized.toolbarInline.groups.sort((a, b) => {
if (a.order && b.order) {
Expand Down