Skip to content

Commit e08a39d

Browse files
mfazekasclaude
andcommitted
feat: insert native file path when dropping or pasting non-image files
Previously, only image files could be attached to the composer. Dropping or pasting any other file type (e.g. .riv, .csv, .json) silently failed with an "unsupported file type" error, making it impossible to reference data or asset files by path. Closes #557. Implementation ────────────── contracts/ipc.ts Added getPathForFile(file) to the DesktopBridge interface. preload.ts Exposed webUtils.getPathForFile(file) via contextBridge. Electron's sandbox blocks the legacy file.path property on File objects; the webUtils API (available since Electron 32) is the official replacement. ComposerPromptEditor Added insertText(text) to ComposerPromptEditorHandle. Uses Lexical's native selection.insertText() to insert at the real cursor position, which correctly handles existing @mention nodes. Manipulating the plain-text snapshot and calling setPrompt() was not an option: Lexical's editor.update() is async, so a synchronous focusComposer() call after would read a stale snapshotRef and overwrite the inserted text via onChange. ChatView - COMPOSER_FILE_PATH_SEPARATOR constant replaces the magic " " literal. - addComposerAttachments(files) is a new unified helper (placed next to addComposerImages) that partitions files into images and non-images. Images are handled by the existing addComposerImages path. Non-image files have their native path resolved via desktopBridge.getPathForFile (fallback: file.name in non-Electron contexts), auto-quoted when the path contains spaces, joined by COMPOSER_FILE_PATH_SEPARATOR, and inserted into the composer via insertText(). - onComposerDrop and onComposerPaste both delegate to addComposerAttachments, eliminating the duplicated partition logic. onComposerDrop additionally calls focusComposer() only when no non-image files were dropped, since insertText() focuses the editor internally as a prerequisite for obtaining a Lexical selection. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
1 parent e6d9a27 commit e08a39d

4 files changed

Lines changed: 46 additions & 9 deletions

File tree

‎apps/desktop/src/preload.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { contextBridge, ipcRenderer } from "electron";
1+
import { contextBridge, ipcRenderer, webUtils } from "electron";
22
import type { DesktopBridge } from "@t3tools/contracts";
33

44
const PICK_FOLDER_CHANNEL = "desktop:pick-folder";
@@ -15,6 +15,7 @@ const wsUrl = process.env.T3CODE_DESKTOP_WS_URL ?? null;
1515

1616
contextBridge.exposeInMainWorld("desktopBridge", {
1717
getWsUrl: () => wsUrl,
18+
getPathForFile: (file: File) => webUtils.getPathForFile(file),
1819
pickFolder: () => ipcRenderer.invoke(PICK_FOLDER_CHANNEL),
1920
confirm: (message) => ipcRenderer.invoke(CONFIRM_CHANNEL, message),
2021
setTheme: (theme) => ipcRenderer.invoke(SET_THEME_CHANNEL, theme),

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ const EMPTY_AVAILABLE_EDITORS: EditorId[] = [];
171171
const EMPTY_PROVIDER_STATUSES: ServerProviderStatus[] = [];
172172
const EMPTY_PENDING_USER_INPUT_ANSWERS: Record<string, PendingUserInputDraftAnswer> = {};
173173
const COMPOSER_PATH_QUERY_DEBOUNCE_MS = 120;
174+
const COMPOSER_FILE_PATH_SEPARATOR = " ";
174175
const SCRIPT_TERMINAL_COLS = 120;
175176
const SCRIPT_TERMINAL_ROWS = 30;
176177

@@ -2097,17 +2098,32 @@ export default function ChatView({ threadId }: ChatViewProps) {
20972098
removeComposerImageFromDraft(imageId);
20982099
};
20992100

2101+
const addComposerAttachments = (files: File[]) => {
2102+
const imageFiles = files.filter((f) => f.type.startsWith("image/"));
2103+
const nonImageFiles = files.filter((f) => !f.type.startsWith("image/"));
2104+
2105+
if (imageFiles.length > 0) {
2106+
addComposerImages(imageFiles);
2107+
}
2108+
2109+
if (nonImageFiles.length > 0) {
2110+
const paths = nonImageFiles.map(
2111+
(file) => window.desktopBridge?.getPathForFile(file) ?? file.name,
2112+
);
2113+
const insertion = paths
2114+
.map((p) => (p.includes(" ") ? `"${p}"` : p))
2115+
.join(COMPOSER_FILE_PATH_SEPARATOR);
2116+
composerEditorRef.current?.insertTextAndFocus(insertion);
2117+
}
2118+
};
2119+
21002120
const onComposerPaste = (event: React.ClipboardEvent<HTMLElement>) => {
21012121
const files = Array.from(event.clipboardData.files);
21022122
if (files.length === 0) {
21032123
return;
21042124
}
2105-
const imageFiles = files.filter((file) => file.type.startsWith("image/"));
2106-
if (imageFiles.length === 0) {
2107-
return;
2108-
}
21092125
event.preventDefault();
2110-
addComposerImages(imageFiles);
2126+
addComposerAttachments(files);
21112127
};
21122128

21132129
const onComposerDragEnter = (event: React.DragEvent<HTMLDivElement>) => {
@@ -2151,8 +2167,9 @@ export default function ChatView({ threadId }: ChatViewProps) {
21512167
dragDepthRef.current = 0;
21522168
setIsDragOverComposer(false);
21532169
const files = Array.from(event.dataTransfer.files);
2154-
addComposerImages(files);
2155-
focusComposer();
2170+
const hasNonImageFiles = files.some((f) => !f.type.startsWith("image/"));
2171+
addComposerAttachments(files);
2172+
if (!hasNonImageFiles) focusComposer();
21562173
};
21572174

21582175
const onRevertToTurnCount = useCallback(

‎apps/web/src/components/ComposerPromptEditor.tsx‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ export interface ComposerPromptEditorHandle {
459459
focusAt: (cursor: number) => void;
460460
focusAtEnd: () => void;
461461
readSnapshot: () => { value: string; cursor: number; expandedCursor: number };
462+
insertTextAndFocus: (text: string) => void;
462463
}
463464

464465
interface ComposerPromptEditorProps {
@@ -813,6 +814,22 @@ function ComposerPromptEditorInner({
813814
return snapshot;
814815
}, [editor]);
815816

817+
const insertTextAndFocus = useCallback(
818+
(text: string) => {
819+
const rootElement = editor.getRootElement();
820+
if (rootElement) {
821+
rootElement.focus();
822+
}
823+
editor.update(() => {
824+
const selection = $getSelection();
825+
if ($isRangeSelection(selection)) {
826+
selection.insertText(text);
827+
}
828+
});
829+
},
830+
[editor],
831+
);
832+
816833
useImperativeHandle(
817834
editorRef,
818835
() => ({
@@ -829,8 +846,9 @@ function ComposerPromptEditorInner({
829846
);
830847
},
831848
readSnapshot,
849+
insertTextAndFocus,
832850
}),
833-
[focusAt, readSnapshot],
851+
[focusAt, readSnapshot, insertTextAndFocus],
834852
);
835853

836854
const handleEditorChange = useCallback((editorState: EditorState) => {

‎packages/contracts/src/ipc.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export interface DesktopUpdateActionResult {
9696

9797
export interface DesktopBridge {
9898
getWsUrl: () => string | null;
99+
getPathForFile: (file: File) => string;
99100
pickFolder: () => Promise<string | null>;
100101
confirm: (message: string) => Promise<boolean>;
101102
setTheme: (theme: DesktopTheme) => Promise<void>;

0 commit comments

Comments
 (0)