Skip to content

Commit

Permalink
fix(pte): disable multiple blocks annotations (#5257)
Browse files Browse the repository at this point in the history
* fix(pte): disable multiple blocks annotations

* fix(PTE): disabled annotation message update
  • Loading branch information
pedrobonamin authored Dec 13, 2023
1 parent 41552c3 commit 7452848
Showing 1 changed file with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import React, {memo, useCallback, useMemo} from 'react'
import {Button, ButtonProps, PopoverProps} from '@sanity/ui'
import {EllipsisVerticalIcon} from '@sanity/icons'
import {PortableTextEditor, usePortableTextEditor} from '@sanity/portable-text-editor'
import {
PortableTextEditor,
usePortableTextEditor,
usePortableTextEditorSelection,
} from '@sanity/portable-text-editor'
import {isKeySegment} from '@sanity/types'
import {CollapseMenu, CollapseMenuButton} from '../../../../components/collapseMenu'
import {PTEToolbarAction, PTEToolbarActionGroup} from './types'
import {useActiveActionKeys, useFocusBlock} from './hooks'
Expand All @@ -22,7 +27,16 @@ interface ActionMenuProps {
export const ActionMenu = memo(function ActionMenu(props: ActionMenuProps) {
const {disabled: disabledProp, groups, isFullscreen, collapsed} = props
const focusBlock = useFocusBlock()

const editor = usePortableTextEditor()
const selection = usePortableTextEditorSelection()
const isSelectingMultipleBlocks =
// Path at 0 is the block level, by comparing those we can detect if the user is selecting multiple blocks
selection && isKeySegment(selection.anchor.path[0]) && isKeySegment(selection.focus.path[0])
? // In case of keyed segments
selection.anchor.path[0]._key !== selection?.focus.path[0]._key
: // In case of non-keyed segments
selection?.anchor.path[0] !== selection?.focus.path[0]

const isVoidBlock = focusBlock?._type !== editor.schemaTypes.block.name
const isEmptyTextBlock =
Expand Down Expand Up @@ -58,7 +72,8 @@ export const ActionMenu = memo(function ActionMenu(props: ActionMenuProps) {
const children = useMemo(
() =>
actions.map((action) => {
const annotationDisabled = action.type === 'annotation' && isEmptyTextBlock
const annotationDisabled =
action.type === 'annotation' && (isEmptyTextBlock || isSelectingMultipleBlocks)
const active = activeKeys.includes(action.key)
return (
<CollapseMenuButton
Expand All @@ -72,16 +87,22 @@ export const ActionMenu = memo(function ActionMenu(props: ActionMenuProps) {
onClick={() => action.handle(active)}
selected={active}
text={action.title || action.key}
tooltipText={action.title || action.key}
tooltipText={
annotationDisabled
? `Cannot apply ${action.title || action.key} to ${
isEmptyTextBlock ? 'empty block' : 'multiple blocks'
}`
: action.title || action.key
}
tooltipProps={{
disabled: disabled || annotationDisabled,
disabled: disabled,
placement: isFullscreen ? 'bottom' : 'top',
portal: 'default',
}}
/>
)
}),
[actions, activeKeys, disabled, isEmptyTextBlock, isFullscreen],
[actions, activeKeys, disabled, isEmptyTextBlock, isFullscreen, isSelectingMultipleBlocks],
)

const menuButtonProps = useMemo(
Expand Down

2 comments on commit 7452848

@vercel
Copy link

@vercel vercel bot commented on 7452848 Dec 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

performance-studio – ./

performance-studio.sanity.build
performance-studio-git-next.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 7452848 Dec 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

test-studio – ./

test-studio.sanity.build
test-studio-git-next.sanity.build

Please sign in to comment.