fix(prompt-editor): prevent premature variable path merge during typing (#16811)

### Summary

fix(prompt-editor): prevent premature variable path merge during typing

---------

Co-authored-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
chanx
2026-07-10 20:57:59 +08:00
committed by GitHub
parent 0aa2993d55
commit ea9b3789ce
2 changed files with 26 additions and 2 deletions

View File

@@ -153,7 +153,7 @@ function PromptContent({
<div className="absolute inset-y-0 right-2 z-10 flex items-center">
<Tooltip>
<TooltipTrigger asChild>
<label className="flex cursor-pointer items-center rounded-sm border border-border bg-bg-base/95 px-1 py-0.5 shadow-sm backdrop-blur-sm">
<label className="flex cursor-pointer items-center rounded-sm px-1 py-0.5 shadow-sm backdrop-blur-sm">
<span className="sr-only">{t('flow.mergePath')}</span>
<div className="origin-right scale-75">
<Switch

View File

@@ -1,4 +1,4 @@
import { TextNode } from 'lexical';
import { $getSelection, $isRangeSelection, TextNode } from 'lexical';
import {
appendPromptVariablePath,
extractLeadingPromptVariablePath,
@@ -19,6 +19,30 @@ export function mergeLeadingVariablePathTextNode(textNode: TextNode): boolean {
return false;
}
// Don't merge while the user may still be typing the path.
//
// Case 1: The path suffix reaches the end of the text (no remaining text).
// The user may type more identifier characters to extend the path.
//
// Case 2: The cursor sits right after the matched path suffix even though
// there is trailing text (e.g. a space left over from a previous
// merge). This means the user is actively typing the path before
// that pre-existing text, and the path may still grow. Merging now
// would lock the path to its current length.
if (leadingPath.remainingText === '') {
return false;
}
const selection = $getSelection();
if (
$isRangeSelection(selection) &&
selection.isCollapsed() &&
selection.anchor.key === textNode.getKey() &&
selection.anchor.offset === leadingPath.pathSuffix.length
) {
return false;
}
const nextVariable = appendPromptVariablePath(
{
value: previousSibling.__value,