From 8923cc60380baefc6af0cbde28d72db81d731d3e Mon Sep 17 00:00:00 2001 From: ASTRTA Date: Thu, 19 Nov 2026 18:09:20 +0900 Subject: [PATCH] fix: read the pressed submit button from the submit event --- .../EditMenu/SnippetForm/SnippetForm.tsx | 43 ++++++++- tests/e2e/editor-submit-actions.spec.ts | 90 +++++++++++++++++++ 2 files changed, 129 insertions(+), 4 deletions(-) create mode 100644 tests/e2e/editor-submit-actions.spec.ts diff --git a/src/js/components/EditMenu/SnippetForm/SnippetForm.tsx b/src/js/components/EditMenu/SnippetForm/SnippetForm.tsx index ee44023e4..9fb76462c 100644 --- a/src/js/components/EditMenu/SnippetForm/SnippetForm.tsx +++ b/src/js/components/EditMenu/SnippetForm/SnippetForm.tsx @@ -79,11 +79,48 @@ interface EditFormProps extends PropsWithChildren { className?: string } +const useSubmitAction = () => { + const formRef = useRef(null) + const isSubmittingFromTextField = useRef(false) + + // Pressing Enter in a text field submits through the form's first button, which can be Save and Deactivate. + useEffect(() => { + const form = formRef.current + + const handleKeyDown = (event: KeyboardEvent) => { + const { target } = event + if ('Enter' === event.key && target instanceof HTMLInputElement && + 'submit' !== target.type && 'button' !== target.type) { + isSubmittingFromTextField.current = true + setTimeout(() => { + isSubmittingFromTextField.current = false + }) + } + } + + form?.addEventListener('keydown', handleKeyDown) + return () => form?.removeEventListener('keydown', handleKeyDown) + }, []) + + const getSubmitAction = (event: React.FormEvent): SubmitSnippetAction | undefined => { + const submitter = isSubmittingFromTextField.current + ? null + : (event.nativeEvent as SubmitEvent).submitter + + return Object.values(SubmitSnippetAction).find(actionName => + actionName === submitter?.getAttribute('name')) + } + + return { formRef, getSubmitAction } +} + const EditForm: React.FC = ({ children, className }) => { const { submitSnippet } = useSubmitSnippet() const { snippet } = useSnippetForm() const { refreshSnippetsList } = useSnippetsList() + const { formRef, getSubmitAction } = useSubmitAction() + const [validationWarning, setValidationWarning] = useState() const [submitAction, setSubmitAction] = useState() @@ -108,9 +145,7 @@ const EditForm: React.FC = ({ children, className }) => { const handleSubmit = (event: React.FormEvent) => { event.preventDefault() - const action = Object.values(SubmitSnippetAction).find(actionName => - actionName === document.activeElement?.getAttribute('name')) - + const action = getSubmitAction(event) const validationWarning = validateSnippet(snippet) if (validationWarning) { @@ -123,7 +158,7 @@ const EditForm: React.FC = ({ children, className }) => { return ( <> -
+ {children}
diff --git a/tests/e2e/editor-submit-actions.spec.ts b/tests/e2e/editor-submit-actions.spec.ts new file mode 100644 index 000000000..02f996d0c --- /dev/null +++ b/tests/e2e/editor-submit-actions.spec.ts @@ -0,0 +1,90 @@ +import { expect, test } from '@playwright/test' +import { SnippetsTestHelper } from './helpers/SnippetsTestHelper' +import { SELECTORS, TIMEOUTS } from './helpers/constants' +import { wpCli } from './helpers/wpCli' +import type { Page } from '@playwright/test' + +/** + * Safari and Firefox on macOS do not focus a button when it is clicked, so the editor + * has to tell which submit button was pressed without relying on focus. + */ + +const PREFIX = 'E2E Submit Action' + +const getActiveState = async (name: string): Promise => + (await wpCli(['eval', ` + foreach (\\Code_Snippets\\get_snippets() as $snippet) { + if ($snippet->name === ${JSON.stringify(name)}) { + echo $snippet->active ? 'active' : 'inactive'; + exit; + } + } + echo 'missing'; + `])).trim() + +const createSnippet = async (name: string, active: boolean): Promise => { + await wpCli(['eval', ` + $snippet = new \\Code_Snippets\\Model\\Snippet([ + 'name' => ${JSON.stringify(name)}, + 'code' => ${JSON.stringify('// submit action\n')}, + 'scope' => 'global', + 'active' => ${active ? 'true' : 'false'}, + ]); + echo \\Code_Snippets\\save_snippet($snippet)->id; + `]) +} + +const clickWithoutFocus = async (page: Page, name: RegExp): Promise => { + const button = page.getByRole('button', { name }).first() + await button.evaluate(element => element.addEventListener('mousedown', event => event.preventDefault())) + await button.click() +} + +const waitForSave = async (page: Page): Promise => { + await page.locator(SELECTORS.SAVE_SETTLED_NOTICE).first().waitFor({ state: 'visible', timeout: TIMEOUTS.DEFAULT }) +} + +test.describe('Snippet editor submit actions', () => { + let helper: SnippetsTestHelper + let snippetName: string + + test.beforeEach(({ page }) => { + helper = new SnippetsTestHelper(page) + snippetName = SnippetsTestHelper.makeUniqueSnippetName(PREFIX) + }) + + test.afterEach(async () => { + await SnippetsTestHelper.cleanupSnippetsByPrefix(PREFIX) + }) + + test('Save and Activate activates a new snippet when the click does not move focus', async ({ page }) => { + await helper.clickAddNewSnippet() + await helper.fillSnippetForm({ name: snippetName, code: '// submit action\n' }) + + await clickWithoutFocus(page, /^Save and Activate$/) + await waitForSave(page) + + expect(await getActiveState(snippetName)).toBe('active') + }) + + test('Save and Deactivate deactivates a snippet when the click does not move focus', async ({ page }) => { + await createSnippet(snippetName, true) + await helper.openSnippet(snippetName) + + await clickWithoutFocus(page, /^Save and Deactivate$/) + await waitForSave(page) + + expect(await getActiveState(snippetName)).toBe('inactive') + }) + + test('pressing Enter in the title field saves without deactivating an active snippet', async ({ page }) => { + await createSnippet(snippetName, true) + await helper.openSnippet(snippetName) + + await page.focus(SELECTORS.TITLE_INPUT) + await page.keyboard.press('Enter') + await waitForSave(page) + + expect(await getActiveState(snippetName)).toBe('active') + }) +})