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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(ui): keep pointer carets outside surrogate pairs
yhhbd4b7ecfa23c
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 791e9562b..049f8d417 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,6 +16,8 @@ published separately; it ships inside the editor.### Fixed+- **Pointer carets keep supplementary characters intact.** Clicking single-line, multiline or password fields no longer places the caret between the UTF-16 halves of an emoji or another supplementary code point. DOM selection offsets remain UTF-16-based, and existing prefix measurement behavior is preserved.+- **Native text-input notifications survive frame boundaries.** Update systems can read browser and native Change and Submit events after UI interaction clears the previous frame. Subscription callbacks remain synchronous, including focus changes made on Submit; their records reach frame consumers without replaying callbacks. Destroyed fields and disposed editors discard queued records.- **Rich-text colors no longer recolor shadows and outlines.** Effect passes use their authored color and alpha on both bitmap and SDF atlases, including blurred shadows. The fill retains per-run colors, and inherited UI opacity continues to fade every pass.diff --git a/sdk/src/ui/text/text-input-plugin.ts b/sdk/src/ui/text/text-input-plugin.tsindex 5c296bcc1..3060f72c0 100644--- a/sdk/src/ui/text/text-input-plugin.ts+++ b/sdk/src/ui/text/text-input-plugin.ts@@ -245,7 +245,7 @@ export class TextInputPlugin implements Plugin {const textX = pointer.x - fieldLeft - ti.padding- alignOffset(ti.textAlign, innerW, mw(line.text));const prefixes = measurePrefixWidths(line.text, atlas, family, ti.fontSize, 0);- return line.start + nearestCaretIndex(prefixes, textX);+ return line.start + nearestCaretIndex(prefixes, textX, line.text);}const scrollX = scrollXOf.get(entity) ?? 0;@@ -256,7 +256,7 @@ export class TextInputPlugin implements Plugin {const textX = pointer.x - fieldLeft - ti.padding - ao + scrollX;const display = ti.password ? PASSWORD_CHAR.repeat(val.length) : val;const prefixes = measurePrefixWidths(display, atlas, family, ti.fontSize, 0);- return nearestCaretIndex(prefixes, textX);+ return nearestCaretIndex(prefixes, textX, val);}function activateEditor(entity: Entity): void {diff --git a/sdk/src/ui/text/text-input-view.ts b/sdk/src/ui/text/text-input-view.tsindex e820acac8..b0691d7f7 100644--- a/sdk/src/ui/text/text-input-view.ts+++ b/sdk/src/ui/text/text-input-view.ts@@ -77,15 +77,15 @@ export function fieldSelection(selStart: number, selEnd: number, backward: boole}/**- * The caret index whose position is closest to text-space offset `x`, given- * `prefixWidths[i]` = the measured width of the first `i` characters- * (`prefixWidths[0] === 0`, length = value length + 1). Used to place the caret- * where a pointer clicks inside the field. Pure. Returns 0 for an empty field.+ * Choose the closest measured UTF-16 prefix to text-space offset `x`.+ * Supplying the original value excludes surrogate-pair interiors, including+ * when the measured prefixes belong to password bullets. Empty fields return 0.*/-export function nearestCaretIndex(prefixWidths: readonly number[], x: number): number {+export function nearestCaretIndex(prefixWidths: readonly number[], x: number, value?: string): number {let best = 0;let bestDist = Infinity;for (let i = 0; i < prefixWidths.length; i++) {+ if (value && i > 0 && (value.codePointAt(i - 1) ?? 0) > 0xffff) continue;const d = Math.abs(prefixWidths[i] - x);if (d < bestDist) {bestDist = d;diff --git a/sdk/tests/ui-text-input-view.test.ts b/sdk/tests/ui-text-input-view.test.tsindex 76fd3e30d..5d024554a 100644--- a/sdk/tests/ui-text-input-view.test.ts+++ b/sdk/tests/ui-text-input-view.test.ts@@ -85,6 +85,16 @@ describe('fieldSelection', () => {});describe('nearestCaretIndex', () => {+ it('skips surrogate interiors while keeping UTF-16 offsets for plain and masked text', () => {+ expect(nearestCaretIndex([0, 10, 14, 24, 34], 10, '😀中X')).toBe(2);+ expect(nearestCaretIndex([0, 10, 20, 30, 40], 12, '😀中X')).toBe(2);+ expect(nearestCaretIndex([0, 10, 14, 24, 34], 5, '😀中X')).toBe(0);+ expect(nearestCaretIndex([0, 10, 14, 24, 34], 24, '😀中X')).toBe(3);+ expect(nearestCaretIndex([0, 10, 14, 24, 34], 100, '😀中X')).toBe(4);+ expect(nearestCaretIndex([0, 8, 16, 24, 32], 24, '😀😎')).toBe(2);+ expect(nearestCaretIndex([0, 8, 16], 8, '\ud83dX')).toBe(1);+ });+// Prefix widths for a 4-char field where each char is 10px wide.const widths = [0, 10, 20, 30, 40];diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 76e4b9b59..eb058da01 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): keep pointer carets outside surrogate pairs':+ { note: 'Pointer carets keep supplementary characters intact.' },'fix(ui): deliver native text input events within the UI frame':{ note: 'Native text-input notifications survive frame boundaries.' },'fix(ui): preserve rich text shadow and outline colors':