xonecas.io
70160
Ce

cel-tui / commit 8f764d1

fix(core): sync TextInput cursor on external inserts

xonecas2026-04-19 8f764d1046184fdb3009d213e91ea95063987404 parent 40f4117

3 files changed

packages/core/src/cel.ts+1−1
@@ -814,7 +814,7 @@ function commitTextInputEdit(
814814 nextState: EditState,
815815 ): void {
816816 batchTextInputEdits?.set(props, nextState);
817- setTextInputCursor(props, nextState.cursor);
817+ setTextInputCursor(props, nextState.cursor, nextState.value);
818818 if (nextState.value !== previousState.value) {
819819 props.onChange(nextState.value);
820820 }
packages/core/src/paint.ts+78−7
@@ -570,9 +570,80 @@ function paintTextInput(
570570 * each frame.
571571 */
572572 type OnChangeFn = (value: string) => void;
573-const textInputCursors = new WeakMap<OnChangeFn, number>();
573+type TextInputState = { value: string; cursor: number };
574+const textInputStates = new WeakMap<OnChangeFn, TextInputState>();
574575 const textInputScrolls = new WeakMap<OnChangeFn, number>();
575576
577+function clampCursor(cursor: number, value: string): number {
578+ return Math.max(0, Math.min(cursor, value.length));
579+}
580+
581+/**
582+ * Remap a stored cursor through an external value change by anchoring it to the
583+ * unchanged prefix/suffix around the edit. Insertions at the cursor land after
584+ * the inserted text; edits strictly after the cursor leave it untouched.
585+ */
586+function remapCursorOffset(
587+ previousValue: string,
588+ previousCursor: number,
589+ nextValue: string,
590+): number {
591+ const cursor = clampCursor(previousCursor, previousValue);
592+ if (previousValue === nextValue) return clampCursor(cursor, nextValue);
593+
594+ let prefix = 0;
595+ const maxPrefix = Math.min(previousValue.length, nextValue.length);
596+ while (
597+ prefix < maxPrefix &&
598+ previousValue.charCodeAt(prefix) === nextValue.charCodeAt(prefix)
599+ ) {
600+ prefix++;
601+ }
602+
603+ let previousEnd = previousValue.length;
604+ let nextEnd = nextValue.length;
605+ while (
606+ previousEnd > prefix &&
607+ nextEnd > prefix &&
608+ previousValue.charCodeAt(previousEnd - 1) ===
609+ nextValue.charCodeAt(nextEnd - 1)
610+ ) {
611+ previousEnd--;
612+ nextEnd--;
613+ }
614+
615+ if (cursor < prefix) return cursor;
616+ if (cursor > previousEnd) {
617+ return clampCursor(cursor + (nextEnd - previousEnd), nextValue);
618+ }
619+ return nextEnd;
620+}
621+
622+function getTextInputState(props: TextInputProps): TextInputState {
623+ const stored = textInputStates.get(props.onChange);
624+ if (stored === undefined) {
625+ const initial = { value: props.value, cursor: props.value.length };
626+ textInputStates.set(props.onChange, initial);
627+ return initial;
628+ }
629+
630+ if (stored.value === props.value) {
631+ const clampedCursor = clampCursor(stored.cursor, props.value);
632+ if (clampedCursor === stored.cursor) return stored;
633+
634+ const clampedState = { value: props.value, cursor: clampedCursor };
635+ textInputStates.set(props.onChange, clampedState);
636+ return clampedState;
637+ }
638+
639+ const syncedState = {
640+ value: props.value,
641+ cursor: remapCursorOffset(stored.value, stored.cursor, props.value),
642+ };
643+ textInputStates.set(props.onChange, syncedState);
644+ return syncedState;
645+}
646+
576647 /**
577648 * Compute the screen position of the cursor for a focused TextInput.
578649 * Returns `{ x, y }` in 0-indexed screen coordinates, or `null` if
@@ -614,19 +685,19 @@ export function getTextInputCursorScreenPos(
614685
615686 /** Get the cursor offset for a TextInput (framework-managed). */
616687 export function getTextInputCursor(props: TextInputProps): number {
617- const stored = textInputCursors.get(props.onChange);
618- if (stored === undefined) return props.value.length;
619- // Clamp to value length — the app may have cleared or shortened the value
620- // externally (e.g. after submit) while the WeakMap still holds the old cursor.
621- return Math.min(stored, props.value.length);
688+ return getTextInputState(props).cursor;
622689 }
623690
624691 /** Set the cursor offset for a TextInput. */
625692 export function setTextInputCursor(
626693 props: TextInputProps,
627694 cursor: number,
695+ value = props.value,
628696 ): void {
629- textInputCursors.set(props.onChange, cursor);
697+ textInputStates.set(props.onChange, {
698+ value,
699+ cursor: clampCursor(cursor, value),
700+ });
630701 }
631702
632703 /** Get the scroll offset for a TextInput (framework-managed). */
packages/core/src/text-input.test.ts+34−0
@@ -1042,6 +1042,40 @@ describe("TextInput integration", () => {
10421042 expect(found).toBe(true);
10431043 });
10441044
1045+ test("programmatic insert at the cursor advances the cursor", async () => {
1046+ const term = setup(30, 3);
1047+ let value = "abcd";
1048+ const onChange = (v: string) => {
1049+ value = v;
1050+ cel.render();
1051+ };
1052+
1053+ cel.viewport(() =>
1054+ VStack({}, [
1055+ TextInput({
1056+ value,
1057+ focused: true,
1058+ onChange,
1059+ }),
1060+ ]),
1061+ );
1062+ await waitForRender();
1063+
1064+ term.sendInput(LEFT);
1065+ await waitForRender();
1066+ term.sendInput(LEFT);
1067+ await waitForRender();
1068+
1069+ value = "abXcd";
1070+ cel.render();
1071+ await waitForRender();
1072+
1073+ term.sendInput("!");
1074+ await waitForRender();
1075+
1076+ expect(value).toBe("abX!cd");
1077+ });
1078+
10451079 test("padding affects intrinsic height", async () => {
10461080 setup(20, 10);
10471081 let value = "line1";