Skip to content

feat(ui): custom ordering for rte fixed toolbar #12095

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
Expand Up @@ -9,6 +9,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 ordering for toolbar groups
* Key is the group key (e.g. 'format', 'text', 'align')
* Value is the new order number
*/
customGroupOrders?: Record<string, number>
/**
* @default false
*
Expand All @@ -26,6 +32,7 @@ export const FixedToolbarFeature = createServerFeature<
const sanitizedProps: FixedToolbarFeatureProps = {
applyToFocusedEditor:
props?.applyToFocusedEditor === undefined ? false : props.applyToFocusedEditor,
customGroupOrders: props?.customGroupOrders,
disableIfParentHasFixedToolbar:
props?.disableIfParentHasFixedToolbar === undefined
? false
Expand Down
29 changes: 25 additions & 4 deletions packages/richtext-lexical/src/lexical/config/client/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ export const sanitizeClientFeatures = (
},
}

// Find custom group orders if they exist in any feature
let customGroupOrders: Record<string, number> = {}

// First pass to collect custom orders from features
features.forEach((feature) => {
if (feature.key === 'toolbarFixed' && feature.sanitizedClientFeatureProps?.customGroupOrders) {
customGroupOrders = {
...customGroupOrders,
...feature.sanitizedClientFeatureProps.customGroupOrders,
}
}
})

if (!features?.size) {
return sanitized
}
Expand Down Expand Up @@ -170,13 +183,21 @@ export const sanitizeClientFeatures = (
return 0
}
})

// Sort sanitized.toolbarFixed.groups by order property
sanitized.toolbarFixed.groups.sort((a, b) => {
if (a.order && b.order) {
return a.order - b.order
} else if (a.order) {
const aCustomOrder = customGroupOrders[a.key]
const bCustomOrder = customGroupOrders[b.key]

// Use custom orders if available, otherwise fall back to default logic
const aOrder = aCustomOrder !== undefined ? aCustomOrder : a.order
const bOrder = bCustomOrder !== undefined ? bCustomOrder : b.order

if (aOrder && bOrder) {
return aOrder - bOrder
} else if (aOrder) {
return -1
} else if (b.order) {
} else if (bOrder) {
return 1
} else {
return 0
Expand Down