@portabletext/editor
Version:
Portable Text Editor made in React
39 lines (30 loc) • 908 B
text/typescript
import type {PortableTextTextBlock} from '@sanity/types'
import type {EditorSelector} from '../editor/editor-selector'
import {isTextBlock} from '../internal-utils/parse-blocks'
import {getSelectedBlocks} from './selectors'
/**
* @public
*/
export const getActiveStyle: EditorSelector<PortableTextTextBlock['style']> = (
snapshot,
) => {
if (!snapshot.context.selection) {
return undefined
}
const selectedBlocks = getSelectedBlocks(snapshot).map((block) => block.node)
const selectedTextBlocks = selectedBlocks.filter((block) =>
isTextBlock(snapshot.context, block),
)
const firstTextBlock = selectedTextBlocks.at(0)
if (!firstTextBlock) {
return undefined
}
const firstStyle = firstTextBlock.style
if (!firstStyle) {
return undefined
}
if (selectedTextBlocks.every((block) => block.style === firstStyle)) {
return firstStyle
}
return undefined
}