Skip to content

Commit

Permalink
fix: add filename to existing text
Browse files Browse the repository at this point in the history
Instead of clobbering the input text area, add to where the cursor last was.

Fixes block#1179
  • Loading branch information
mhickman committed Feb 13, 2025
1 parent 07b89c8 commit e45bd40
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion ui/desktop/src/components/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default function Input({
// State to track if the IME is composing (i.e., in the middle of Japanese IME input)
const [isComposing, setIsComposing] = useState(false);
const textAreaRef = useRef<HTMLTextAreaElement>(null);
const [cursorPos, setCursorPos] = useState(0);

useEffect(() => {
if (textAreaRef.current && !disabled) {
Expand Down Expand Up @@ -47,6 +48,12 @@ export default function Input({
setValue(val);
};

const handleCursorUpdate = (
e: React.KeyboardEvent<HTMLTextAreaElement> | React.MouseEvent<HTMLTextAreaElement>
) => {
setCursorPos(e.currentTarget.selectionStart ?? 0);
};

// Handlers for composition events, which are crucial for proper IME behavior
const handleCompositionStart = (evt: React.CompositionEvent<HTMLTextAreaElement>) => {
setIsComposing(true);
Expand All @@ -63,6 +70,7 @@ export default function Input({
if (value.trim()) {
handleSubmit(new CustomEvent('submit', { detail: { value } }));
setValue('');
setCursorPos(0);
}
}
};
Expand All @@ -72,13 +80,17 @@ export default function Input({
if (value.trim()) {
handleSubmit(new CustomEvent('submit', { detail: { value } }));
setValue('');
setCursorPos(0);
}
};

const handleFileSelect = async () => {
const path = await window.electron.selectFileOrDirectory();
if (path) {
setValue(path);
const currentValue = value;
const newValue = currentValue.slice(0, cursorPos) + path + currentValue.slice(cursorPos);

setValue(newValue);
textAreaRef.current?.focus();
}
};
Expand All @@ -96,6 +108,8 @@ export default function Input({
onChange={handleChange}
onCompositionStart={handleCompositionStart}
onCompositionEnd={handleCompositionEnd}
onKeyUp={handleCursorUpdate}
onClick={handleCursorUpdate}
onKeyDown={handleKeyDown}
disabled={disabled}
ref={textAreaRef}
Expand Down

0 comments on commit e45bd40

Please sign in to comment.