Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(ui): preserve focus selected by text submit handlers

yhh197c30740e6d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 81375e5d1..aa8deb08e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,8 @@ published separately; it ships inside the editor.
### Fixed
+- **Text submission preserves callback-selected focus.** A submit handler can focus the next field without the previous field's default blur clearing it. Submission still closes the editing session when the handler leaves focus unchanged.
+
- **Text input events stay with the active editing session.** Switching fields no longer lets the previous surface's blur clear the new focus or its queued input overwrite the next value. Web and native editors reset interrupted composition state when focus restarts, so normal typing resumes without requiring a composition-end event.
- **Text caret picking measures long strings once.** Pointer placement in single-line, multiline and password fields accumulates glyph advances in linear time instead of repeatedly measuring every prefix. UTF-16 caret boundaries, glyph scaling, missing glyphs and masked password widths retain their existing positions.
diff --git a/sdk/src/ui/text/text-input-plugin.ts b/sdk/src/ui/text/text-input-plugin.ts
index ccc5b59a7..44f4b031c 100644
--- a/sdk/src/ui/text/text-input-plugin.ts
+++ b/sdk/src/ui/text/text-input-plugin.ts
@@ -140,7 +140,7 @@ export class TextInputPlugin implements Plugin {
case 'submit':
if (focused !== null) {
(app.getResource(UIEvents) as UIEventQueue).emit(focused, 'submit');
- blurCurrent();
+ if (getEditingTextInput() === focused) blurCurrent();
}
break;
case 'cancel':
diff --git a/sdk/tests/text-input-focus-session.test.ts b/sdk/tests/text-input-focus-session.test.ts
index fa6f8d1fc..eccb4c23a 100644
--- a/sdk/tests/text-input-focus-session.test.ts
+++ b/sdk/tests/text-input-focus-session.test.ts
@@ -70,3 +70,30 @@ it('accepts normal input after an interrupted composition loses and regains focu
textarea.dispatchEvent(new Event('input'));
expect(app.world.get(first, TextInput).value).toBe('resumed');
});
+
+it('preserves focus assigned by a submit callback', async () => {
+ const { app, first, second, textarea } = build();
+ const fm = app.getResource(FocusManager);
+ fm.focus(first);
+ await app.tick(1 / 60);
+ app.getResource(UIEvents).on(first, 'submit', () => fm.focus(second));
+ textarea.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', cancelable: true }));
+ expect(fm.focusedEntity).toBe(second);
+ await app.tick(1 / 60);
+ textarea.value = 'submitted next';
+ textarea.dispatchEvent(new Event('input'));
+ expect(app.world.get(second, TextInput).value).toBe('submitted next');
+});
+
+it('releases focus after submission when the callback leaves the session unchanged', async () => {
+ const { app, first, textarea } = build();
+ const fm = app.getResource(FocusManager), onSubmit = vi.fn();
+ fm.focus(first);
+ await app.tick(1 / 60);
+ app.getResource(UIEvents).on(first, 'submit', onSubmit);
+ textarea.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', cancelable: true }));
+ expect(onSubmit).toHaveBeenCalledOnce();
+ expect(fm.focusedEntity).toBe(null);
+ expect(app.world.get(first, TextInput).focused).toBe(false);
+ expect(document.activeElement).not.toBe(textarea);
+});
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 121fa31a7..b1d7eef1f 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';
* the reader recognises anyway.
*/
export const NOTED = {
+ 'fix(ui): preserve focus selected by text submit handlers':
+ { note: 'Text submission preserves callback-selected focus.' },
'fix(ui): bind text input events to the active session':
{ note: 'Text input events stay with the active editing session.' },
'perf(ui): measure text caret prefixes in one pass':