Skip to content
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

fix(core): unable to open block style select when in nested PTEs in fullscreen #6738

Merged
merged 7 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -335,5 +335,50 @@ export const ptAllTheBellsAndWhistlesType = defineType({
}),
],
}),
defineField({
name: 'content',
type: 'array',
of: [
defineArrayMember({
name: 'something',
type: 'block',
of: [
defineArrayMember({
name: 'nested',
type: 'object',
fields: [
defineField({
name: 'items',
type: 'array',
of: [
defineArrayMember({
name: 'item',
type: 'object',
fields: [
defineField({
name: 'deep',
type: 'array',
of: [
defineArrayMember({
type: 'block',
styles: [
{title: 'Normal', value: 'normal'},
{title: 'H2', value: 'h2'},
{title: 'H3', value: 'h3'},
{title: 'H4', value: 'h4'},
],
}),
],
}),
],
}),
],
}),
],
}),
],
}),
],
}),
],
})
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export const Element: FunctionComponent<ElementProps> = ({
<span
draggable={!readOnly}
className="pt-inline-object"
data-testid="pt-inline-object"
ref={inlineBlockObjectRef}
key={element._key}
style={inlineBlockStyle}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const MenuButtonMemo = memo(MenuButton)
interface BlockStyleSelectProps {
disabled: boolean
items: BlockStyleItem[]
boundaryElement: HTMLDivElement | null
}

const StyledMenuItem = styled(MenuItem)`
Expand Down Expand Up @@ -70,11 +71,18 @@ const emptyStyle: BlockStyleItem = {
export const BlockStyleSelect = memo(function BlockStyleSelect(
props: BlockStyleSelectProps,
): JSX.Element {
const {disabled, items: itemsProp} = props
const {disabled, items: itemsProp, boundaryElement} = props
const editor = usePortableTextEditor()
const focusBlock = useFocusBlock()
const {t} = useTranslation()

const popoverProperties: MenuButtonProps['popover'] = {
constrainSize: true,
placement: 'bottom-start',
portal: 'default',
referenceBoundary: boundaryElement,
}

const _disabled =
disabled || (focusBlock ? editor.schemaTypes.block.name !== focusBlock._type : false)

Expand Down Expand Up @@ -177,7 +185,7 @@ export const BlockStyleSelect = memo(function BlockStyleSelect(

return (
<MenuButtonMemo
popover={MENU_POPOVER_PROPS}
popover={popoverProperties}
id="block-style-select"
button={button}
menu={menu}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,13 @@ const InnerToolbar = memo(function InnerToolbar({
<RootFlex align="center" ref={setRootElement} onMouseDown={preventEditorBlurOnToolbarMouseDown}>
{showBlockStyleSelect && (
<StyleSelectFlex flex={collapsed ? 1 : undefined}>
<StyleSelectBox padding={isFullscreen ? 2 : 1}>
<BlockStyleSelect disabled={disabled} items={blockStyles} />
<StyleSelectBox padding={isFullscreen ? 2 : 1} data-testid="block-style-select">
<BlockStyleSelect
disabled={disabled}
items={blockStyles}
// send the boundary in cases of PTEs within PTEs
boundaryElement={rootElement}
/>
</StyleSelectBox>
</StyleSelectFlex>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ export function ArrayOfObjectsFunctions<
return (
<Tooltip portal content={t('inputs.array.read-only-label')}>
<Grid>
<Button icon={AddIcon} mode="ghost" disabled size="large" text={t(addItemI18nKey)} />
<Button
icon={AddIcon}
mode="ghost"
disabled
size="large"
data-testid="add-read-object-button"
text={t(addItemI18nKey)}
/>
</Grid>
</Tooltip>
)
Expand All @@ -68,11 +75,20 @@ export function ArrayOfObjectsFunctions<
mode="ghost"
onClick={handleAddBtnClick}
size="large"
data-testid="add-single-object-button"
text={t(addItemI18nKey)}
/>
) : (
<MenuButton
button={<Button icon={AddIcon} mode="ghost" size="large" text={t(addItemI18nKey)} />}
button={
<Button
icon={AddIcon}
mode="ghost"
size="large"
data-testid="add-multiple-object-button"
text={t(addItemI18nKey)}
/>
}
id={menuButtonId || ''}
menu={
<Menu>
Expand Down
58 changes: 58 additions & 0 deletions test/e2e/tests/pte/Toolbar.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {expect, type Locator} from '@playwright/test'
import {test} from '@sanity/test'

test.describe('Portable Text Input - Open Block Style Select', () => {
let pteInput: Locator

test.beforeEach(async ({page, createDraftDocument}) => {
RitaDias marked this conversation as resolved.
Show resolved Hide resolved
await createDraftDocument(
'/test/content/input-standard;portable-text;pt_allTheBellsAndWhistles',
)

pteInput = page.getByTestId('field-content')

// set up the portable text editor
await pteInput.focus()
await pteInput.click()
})

test('on a simple editor', async ({page}) => {
await pteInput.getByTestId('block-style-select').click()

expect(await page.locator('[data-ui="MenuButton__popover"]')).toBeVisible()
})

test('on a full screen simple editor', async ({page}) => {
await pteInput.getByLabel('Expand editor').click()
await page.locator('[data-testid="block-style-select"]').click()

await expect(await page.locator('[data-ui="MenuButton__popover"]')).toBeVisible()
})

test('on a full screen multi nested PTE', async ({page}) => {
await pteInput.getByLabel('Expand editor').click()

// add a object with a nested PTE
await page.getByRole('button', {name: 'Insert Nested (inline)'}).click()

// click the block
await page.getByTestId('pt-inline-object').click()

// set up object
await page.getByTestId('add-single-object-button').click()

// set up nested PTE
const nestedPTE = page.locator('[data-testid^="field-content"][data-testid$=".deep"]')

await nestedPTE.focus()
await nestedPTE.click()

// nested block full screen
nestedPTE.getByLabel('Expand editor').click()

// click the block style select
await nestedPTE.locator('[data-testid="block-style-select"]').click()

await expect(await page.locator('[data-ui="MenuButton__popover"]')).toBeVisible()
})
})
Loading