Skip to content

Commit a891e98

Browse files
authored
fix(richtext-lexical): slash menu from + button did not trigger item select action (#9420)
Fixes a bug I introduced through #9396 and adds an e2e test
1 parent 38de760 commit a891e98

File tree

2 files changed

+84
-20
lines changed
  • packages/richtext-lexical/src/lexical/plugins/SlashMenu/LexicalTypeaheadMenuPlugin
  • test/fields/collections/Lexical/e2e/main

2 files changed

+84
-20
lines changed

packages/richtext-lexical/src/lexical/plugins/SlashMenu/LexicalTypeaheadMenuPlugin/LexicalMenu.tsx

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -254,26 +254,23 @@ export function LexicalMenu({
254254
(selectedItem: SlashMenuItem) => {
255255
close()
256256

257-
editor.update(
258-
() => {
259-
const textNodeContainingQuery =
260-
resolution.match != null && shouldSplitNodeWithQuery
261-
? $splitNodeContainingQuery(resolution.match)
262-
: null
263-
264-
if (textNodeContainingQuery) {
265-
textNodeContainingQuery.remove()
266-
}
267-
},
268-
{
269-
onUpdate() {
270-
selectedItem.onSelect({
271-
editor,
272-
queryString: resolution.match ? resolution.match.matchingString : '',
273-
})
274-
},
275-
},
276-
)
257+
editor.update(() => {
258+
const textNodeContainingQuery =
259+
resolution.match != null && shouldSplitNodeWithQuery
260+
? $splitNodeContainingQuery(resolution.match)
261+
: null
262+
263+
if (textNodeContainingQuery) {
264+
textNodeContainingQuery.remove()
265+
}
266+
})
267+
268+
setTimeout(() => {
269+
selectedItem.onSelect({
270+
editor,
271+
queryString: resolution.match ? resolution.match.matchingString : '',
272+
})
273+
}, 0)
277274
},
278275
[editor, shouldSplitNodeWithQuery, resolution.match, close],
279276
)

test/fields/collections/Lexical/e2e/main/e2e.spec.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,73 @@ describe('lexicalMain', () => {
10681068
await expect(page.locator('#blocks-row-1 .section-title__input')).toHaveValue('1') // block name
10691069
})
10701070

1071+
test('ensure blocks can be created from plus button', async () => {
1072+
await navigateToLexicalFields()
1073+
const richTextField = page.locator('.rich-text-lexical').first()
1074+
await richTextField.scrollIntoViewIfNeeded()
1075+
await expect(richTextField).toBeVisible()
1076+
// Wait until there at least 10 blocks visible in that richtext field - thus wait for it to be fully loaded
1077+
await expect(page.locator('.rich-text-lexical').nth(2).locator('.lexical-block')).toHaveCount(
1078+
10,
1079+
)
1080+
await expect(page.locator('.shimmer-effect')).toHaveCount(0)
1081+
1082+
// click contenteditable
1083+
await richTextField.locator('.ContentEditable__root').first().click()
1084+
1085+
const lastParagraph = richTextField.locator('p').first()
1086+
await lastParagraph.scrollIntoViewIfNeeded()
1087+
await expect(lastParagraph).toBeVisible()
1088+
1089+
/**
1090+
* Create new upload node
1091+
*/
1092+
// type / to open the slash menu
1093+
await lastParagraph.click()
1094+
// hover over the last paragraph to make the plus button visible
1095+
await lastParagraph.hover()
1096+
await wait(600)
1097+
//await richTextField.locator('.add-block-menu').first().click()
1098+
const plusButton = richTextField.locator('.add-block-menu').first()
1099+
1100+
// hover over plusButton
1101+
await plusButton.hover()
1102+
await wait(100)
1103+
// click the plus button
1104+
await plusButton.click()
1105+
1106+
await expect(richTextField.locator('.slash-menu-popup')).toBeVisible()
1107+
// click button with text "Text"
1108+
await richTextField.locator('.slash-menu-popup button').getByText('My Block').click()
1109+
1110+
await expect(richTextField.locator('.lexical-block')).toHaveCount(1)
1111+
await richTextField.locator('#field-someTextRequired').first().fill('test')
1112+
1113+
await saveDocAndAssert(page)
1114+
1115+
await expect(async () => {
1116+
const lexicalDoc: LexicalField = (
1117+
await payload.find({
1118+
collection: lexicalFieldsSlug,
1119+
depth: 0,
1120+
overrideAccess: true,
1121+
where: {
1122+
title: {
1123+
equals: lexicalDocData.title,
1124+
},
1125+
},
1126+
})
1127+
).docs[0] as never
1128+
1129+
const lexicalField: SerializedEditorState = lexicalDoc.lexicalRootEditor
1130+
1131+
// @ts-expect-error no need to type this
1132+
await expect(lexicalField?.root?.children[1].fields.someTextRequired).toEqual('test')
1133+
}).toPass({
1134+
timeout: POLL_TOPASS_TIMEOUT,
1135+
})
1136+
})
1137+
10711138
describe('localization', () => {
10721139
test.skip('ensure simple localized lexical field works', async () => {
10731140
await navigateToLexicalFields(true, true)

0 commit comments

Comments
 (0)