fix: multi-image drag-and-drop only showing last image
insertTextAtCursor read input and cursorOffset from the React closure, which is stale when called in a synchronous loop (e.g. onImagePaste for multiple dragged images). Now uses refs so each insertion chains on the previous one.
This commit is contained in:
@@ -1262,12 +1262,23 @@ function PromptInput({
|
|||||||
if (isNonSpacePrintable(input, key)) return ' ' + input;
|
if (isNonSpacePrintable(input, key)) return ' ' + input;
|
||||||
return input;
|
return input;
|
||||||
}, []);
|
}, []);
|
||||||
|
// Ref mirrors cursorOffset for use in synchronous loops (e.g. multi-image
|
||||||
|
// paste) where React batches state updates and the closure value is stale.
|
||||||
|
const cursorOffsetRef = useRef(cursorOffset);
|
||||||
|
cursorOffsetRef.current = cursorOffset;
|
||||||
|
|
||||||
function insertTextAtCursor(text: string) {
|
function insertTextAtCursor(text: string) {
|
||||||
// Push current state to buffer before inserting
|
// Use refs for input/cursor so back-to-back calls in the same event
|
||||||
pushToBuffer(input, cursorOffset, pastedContents);
|
// (e.g. onImagePaste loop for multiple dragged images) chain correctly
|
||||||
const newInput = input.slice(0, cursorOffset) + text + input.slice(cursorOffset);
|
// instead of each reading the same stale closure values.
|
||||||
|
const currentInput = lastInternalInputRef.current;
|
||||||
|
const currentOffset = cursorOffsetRef.current;
|
||||||
|
pushToBuffer(currentInput, currentOffset, pastedContents);
|
||||||
|
const newInput = currentInput.slice(0, currentOffset) + text + currentInput.slice(currentOffset);
|
||||||
trackAndSetInput(newInput);
|
trackAndSetInput(newInput);
|
||||||
setCursorOffset(cursorOffset + text.length);
|
const newOffset = currentOffset + text.length;
|
||||||
|
cursorOffsetRef.current = newOffset;
|
||||||
|
setCursorOffset(newOffset);
|
||||||
}
|
}
|
||||||
const doublePressEscFromEmpty = useDoublePress(() => {}, () => onShowMessageSelector());
|
const doublePressEscFromEmpty = useDoublePress(() => {}, () => onShowMessageSelector());
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user