Skip to content

Commit 454fa73

Browse files
authored
🐛 Fix for cursor offset issue in application name and description input fields
2 parents da7639c + c176bc8 commit 454fa73

File tree

1 file changed

+67
-4
lines changed

1 file changed

+67
-4
lines changed

frontend/app/[locale]/setup/modelSetup/appConfig.tsx

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ export const AppConfigSection: React.FC = () => {
3939
// 添加错误状态管理
4040
const [appNameError, setAppNameError] = useState(false);
4141

42+
// 添加用户输入状态跟踪
43+
const isUserTypingAppName = useRef(false);
44+
const isUserTypingDescription = useRef(false);
45+
const appNameUpdateTimer = useRef<NodeJS.Timeout | null>(null);
46+
const descriptionUpdateTimer = useRef<NodeJS.Timeout | null>(null);
47+
4248
// 头像相关状态
4349
const [isAvatarModalOpen, setIsAvatarModalOpen] = useState(false);
4450
const [selectedIconKey, setSelectedIconKey] = useState<string>(presetIcons[0].key);
@@ -59,8 +65,13 @@ export const AppConfigSection: React.FC = () => {
5965
const handleConfigChanged = (event: any) => {
6066
const { config } = event.detail;
6167
if (config?.app) {
62-
setLocalAppName(config.app.appName || "");
63-
setLocalAppDescription(config.app.appDescription || "");
68+
// 只有在用户未正在输入时才更新状态
69+
if (!isUserTypingAppName.current) {
70+
setLocalAppName(config.app.appName || "");
71+
}
72+
if (!isUserTypingDescription.current) {
73+
setLocalAppDescription(config.app.appDescription || "");
74+
}
6475
setAvatarType(config.app.iconType || "preset");
6576
setCustomAvatarUrl(config.app.customIconUrl || null);
6677

@@ -79,8 +90,13 @@ export const AppConfigSection: React.FC = () => {
7990

8091
// 监听appConfig变化,同步更新本地状态
8192
useEffect(() => {
82-
setLocalAppName(appConfig.appName);
83-
setLocalAppDescription(appConfig.appDescription);
93+
// 只有在用户未正在输入时才更新状态
94+
if (!isUserTypingAppName.current) {
95+
setLocalAppName(appConfig.appName);
96+
}
97+
if (!isUserTypingDescription.current) {
98+
setLocalAppDescription(appConfig.appDescription);
99+
}
84100
setAvatarType(appConfig.iconType);
85101
setCustomAvatarUrl(appConfig.customIconUrl);
86102
}, [appConfig.appName, appConfig.appDescription, appConfig.iconType, appConfig.customIconUrl]);
@@ -105,27 +121,74 @@ export const AppConfigSection: React.FC = () => {
105121
};
106122
}, []);
107123

124+
// 清理定时器
125+
useEffect(() => {
126+
return () => {
127+
if (appNameUpdateTimer.current) {
128+
clearTimeout(appNameUpdateTimer.current);
129+
}
130+
if (descriptionUpdateTimer.current) {
131+
clearTimeout(descriptionUpdateTimer.current);
132+
}
133+
};
134+
}, []);
135+
108136
// Handle basic app config changes
109137
const handleAppNameChange = (e: React.ChangeEvent<HTMLInputElement>) => {
110138
const newAppName = e.target.value;
139+
isUserTypingAppName.current = true;
111140
setLocalAppName(newAppName);
141+
112142
// 如果输入了值,清除错误状态
113143
if (newAppName.trim()) {
114144
setAppNameError(false);
115145
}
146+
147+
// 清除之前的定时器
148+
if (appNameUpdateTimer.current) {
149+
clearTimeout(appNameUpdateTimer.current);
150+
}
151+
152+
// 设置防抖更新
153+
appNameUpdateTimer.current = setTimeout(() => {
154+
updateAppConfig({ appName: newAppName });
155+
isUserTypingAppName.current = false;
156+
}, 500);
116157
};
117158

118159
const handleAppNameBlur = () => {
160+
// 清除定时器,立即更新
161+
if (appNameUpdateTimer.current) {
162+
clearTimeout(appNameUpdateTimer.current);
163+
}
119164
updateAppConfig({ appName: localAppName });
165+
isUserTypingAppName.current = false;
120166
};
121167

122168
const handleDescriptionChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
123169
const newDescription = e.target.value;
170+
isUserTypingDescription.current = true;
124171
setLocalAppDescription(newDescription);
172+
173+
// 清除之前的定时器
174+
if (descriptionUpdateTimer.current) {
175+
clearTimeout(descriptionUpdateTimer.current);
176+
}
177+
178+
// 设置防抖更新
179+
descriptionUpdateTimer.current = setTimeout(() => {
180+
updateAppConfig({ appDescription: newDescription });
181+
isUserTypingDescription.current = false;
182+
}, 500);
125183
};
126184

127185
const handleDescriptionBlur = () => {
186+
// 清除定时器,立即更新
187+
if (descriptionUpdateTimer.current) {
188+
clearTimeout(descriptionUpdateTimer.current);
189+
}
128190
updateAppConfig({ appDescription: localAppDescription });
191+
isUserTypingDescription.current = false;
129192
};
130193

131194
// 打开头像选择模态框

0 commit comments

Comments
 (0)