Skip to content

Commit

Permalink
fix(core): unable to open block style select when in nested PTEs in f…
Browse files Browse the repository at this point in the history
…ullscreen (#6738)

* chore(dev): update schema

* fix(core): unable to open block style select when in nested PTEs

* test(@sanity, sanity, test): update e2e tests

* chore(sanity): remove unneded zindex

* test(test): update e2e to wait for the second full screen in pte
  • Loading branch information
RitaDias committed May 24, 2024
1 parent 3e1bc3e commit 9f7ab8b
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 6 deletions.
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}) => {
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
await nestedPTE.getByLabel('Expand editor').click()

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

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

0 comments on commit 9f7ab8b

Please sign in to comment.