-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(selectors): add
getActiveStyle
and isActiveStyle
- Loading branch information
1 parent
8c548f3
commit 37ba256
Showing
4 changed files
with
60 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/editor/src/selectors/selector.get-active-style.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type {PortableTextTextBlock} from '@sanity/types' | ||
import {createGuards} from '../behavior-actions/behavior.guards' | ||
import type {EditorSelector} from '../editor/editor-selector' | ||
import {getSelectedBlocks} from './selectors' | ||
|
||
/** | ||
* @alpha | ||
*/ | ||
export const getActiveStyle: EditorSelector<PortableTextTextBlock['style']> = ({ | ||
context, | ||
}) => { | ||
if (!context.selection) { | ||
return undefined | ||
} | ||
|
||
const guards = createGuards(context) | ||
const selectedBlocks = getSelectedBlocks({context}).map((block) => block.node) | ||
const selectedTextBlocks = selectedBlocks.filter(guards.isTextBlock) | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type {EditorSelector} from '../editor/editor-selector' | ||
import {getActiveStyle} from './selector.get-active-style' | ||
|
||
/** | ||
* @alpha | ||
*/ | ||
export function isActiveStyle(style: string): EditorSelector<boolean> { | ||
return (snapshot) => { | ||
const activeStyle = getActiveStyle(snapshot) | ||
|
||
return activeStyle === style | ||
} | ||
} |