Skip to content

Commit e45bd40

Browse files
committed
fix: add filename to existing text
Instead of clobbering the input text area, add to where the cursor last was. Fixes block#1179
1 parent 07b89c8 commit e45bd40

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

ui/desktop/src/components/Input.tsx

+15-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export default function Input({
2020
// State to track if the IME is composing (i.e., in the middle of Japanese IME input)
2121
const [isComposing, setIsComposing] = useState(false);
2222
const textAreaRef = useRef<HTMLTextAreaElement>(null);
23+
const [cursorPos, setCursorPos] = useState(0);
2324

2425
useEffect(() => {
2526
if (textAreaRef.current && !disabled) {
@@ -47,6 +48,12 @@ export default function Input({
4748
setValue(val);
4849
};
4950

51+
const handleCursorUpdate = (
52+
e: React.KeyboardEvent<HTMLTextAreaElement> | React.MouseEvent<HTMLTextAreaElement>
53+
) => {
54+
setCursorPos(e.currentTarget.selectionStart ?? 0);
55+
};
56+
5057
// Handlers for composition events, which are crucial for proper IME behavior
5158
const handleCompositionStart = (evt: React.CompositionEvent<HTMLTextAreaElement>) => {
5259
setIsComposing(true);
@@ -63,6 +70,7 @@ export default function Input({
6370
if (value.trim()) {
6471
handleSubmit(new CustomEvent('submit', { detail: { value } }));
6572
setValue('');
73+
setCursorPos(0);
6674
}
6775
}
6876
};
@@ -72,13 +80,17 @@ export default function Input({
7280
if (value.trim()) {
7381
handleSubmit(new CustomEvent('submit', { detail: { value } }));
7482
setValue('');
83+
setCursorPos(0);
7584
}
7685
};
7786

7887
const handleFileSelect = async () => {
7988
const path = await window.electron.selectFileOrDirectory();
8089
if (path) {
81-
setValue(path);
90+
const currentValue = value;
91+
const newValue = currentValue.slice(0, cursorPos) + path + currentValue.slice(cursorPos);
92+
93+
setValue(newValue);
8294
textAreaRef.current?.focus();
8395
}
8496
};
@@ -96,6 +108,8 @@ export default function Input({
96108
onChange={handleChange}
97109
onCompositionStart={handleCompositionStart}
98110
onCompositionEnd={handleCompositionEnd}
111+
onKeyUp={handleCursorUpdate}
112+
onClick={handleCursorUpdate}
99113
onKeyDown={handleKeyDown}
100114
disabled={disabled}
101115
ref={textAreaRef}

0 commit comments

Comments
 (0)