Skip to content

Commit 4d0f3aa

Browse files
committed
Remove textarea branch from insertText
1 parent dc804a9 commit 4d0f3aa

File tree

3 files changed

+29
-50
lines changed

3 files changed

+29
-50
lines changed

src/__tests__/snapshot-tests/__snapshots__/documenter.test.ts.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20614,7 +20614,8 @@ Requires React 18.",
2061420614
"returnType": "void",
2061520615
},
2061620616
{
20617-
"description": "Inserts text at a specified position. Triggers input events and menu detection when \`menus\` or \`tokens\` is defined.",
20617+
"description": "Inserts text at a specified position. Only supported when \`menus\` or \`tokens\` is defined.
20618+
Triggers input events and menu detection.",
2061820619
"name": "insertText",
2061920620
"parameters": [
2062020621
{

src/prompt-input/interfaces.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,8 @@ export namespace PromptInputProps {
506506
setSelectionRange(start: number | null, end: number | null, direction?: 'forward' | 'backward' | 'none'): void;
507507

508508
/**
509-
* Inserts text at a specified position. Triggers input events and menu detection when `menus` or `tokens` is defined.
509+
* Inserts text at a specified position. Only supported when `menus` or `tokens` is defined.
510+
* Triggers input events and menu detection.
510511
*
511512
* @param text The text to insert.
512513
* @param caretStart Position to insert at. Defaults to current caret position or 0.

src/prompt-input/internal.tsx

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -229,60 +229,37 @@ const InternalPromptInput = React.forwardRef(
229229
return;
230230
}
231231

232-
if (isTokenMode) {
233-
if (!editableElementRef.current || !tokens || !caretControllerRef.current) {
234-
return;
235-
}
236-
237-
let adjustedCaretStart: number;
238-
let adjustedCaretEnd: number | undefined;
239-
240-
if (caretStart === undefined) {
241-
const currentPos = caretControllerRef.current.getPosition();
242-
const pinnedCount = tokens.filter(isPinnedReferenceToken).length;
232+
if (!isTokenMode || !editableElementRef.current || !tokens || !caretControllerRef.current) {
233+
return;
234+
}
243235

244-
// If the caret is before or between pinned tokens, move it after them.
245-
// Text inserted here would get pushed after pinned tokens by enforcePinnedTokenOrdering,
246-
// but the caret wouldn't follow — so we preemptively position it correctly.
247-
adjustedCaretStart = pinnedCount > 0 && currentPos < pinnedCount ? pinnedCount : currentPos;
248-
adjustedCaretEnd = undefined;
249-
} else {
250-
const pinnedTokens = tokens.filter(isPinnedReferenceToken);
251-
const pinnedOffset = pinnedTokens.length;
236+
let adjustedCaretStart: number;
237+
let adjustedCaretEnd: number | undefined;
252238

253-
adjustedCaretStart = caretStart + pinnedOffset;
254-
adjustedCaretEnd = caretEnd !== undefined ? caretEnd + pinnedOffset : undefined;
255-
}
239+
if (caretStart === undefined) {
240+
const currentPos = caretControllerRef.current.getPosition();
241+
const pinnedCount = tokens.filter(isPinnedReferenceToken).length;
256242

257-
insertTextIntoContentEditable(
258-
editableElementRef.current,
259-
text,
260-
adjustedCaretStart,
261-
adjustedCaretEnd,
262-
caretControllerRef.current
263-
);
243+
// If the caret is before or between pinned tokens, move it after them.
244+
// Text inserted here would get pushed after pinned tokens by enforcePinnedTokenOrdering,
245+
// but the caret wouldn't follow — so we preemptively position it correctly.
246+
adjustedCaretStart = pinnedCount > 0 && currentPos < pinnedCount ? pinnedCount : currentPos;
247+
adjustedCaretEnd = undefined;
264248
} else {
265-
if (!textareaRef.current) {
266-
return;
267-
}
268-
269-
const textarea = textareaRef.current;
270-
textarea.focus();
271-
272-
const currentValue = textarea.value;
273-
const insertPosition = caretStart ?? textarea.selectionStart ?? 0;
274-
const newValue = currentValue.substring(0, insertPosition) + text + currentValue.substring(insertPosition);
275-
276-
textarea.value = newValue;
249+
const pinnedTokens = tokens.filter(isPinnedReferenceToken);
250+
const pinnedOffset = pinnedTokens.length;
277251

278-
const finalCursorPosition = caretEnd ?? insertPosition + text.length;
279-
textarea.setSelectionRange(finalCursorPosition, finalCursorPosition);
280-
281-
textarea.dispatchEvent(new Event('input', { bubbles: true }));
282-
fireNonCancelableEvent(onChange, {
283-
value: newValue,
284-
});
252+
adjustedCaretStart = caretStart + pinnedOffset;
253+
adjustedCaretEnd = caretEnd !== undefined ? caretEnd + pinnedOffset : undefined;
285254
}
255+
256+
insertTextIntoContentEditable(
257+
editableElementRef.current,
258+
text,
259+
adjustedCaretStart,
260+
adjustedCaretEnd,
261+
caretControllerRef.current
262+
);
286263
});
287264

288265
useImperativeHandle(

0 commit comments

Comments
 (0)