-
-
Notifications
You must be signed in to change notification settings - Fork 946
Add drag-and-drop file support to terminal #2742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
664bbef
417106b
757dc56
12b35b4
ee80fa8
9eac3c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,7 +8,7 @@ import { waveEventSubscribe } from "@/app/store/wps"; | |||||||||||||||||||||||
| import { RpcApi } from "@/app/store/wshclientapi"; | ||||||||||||||||||||||||
| import { TabRpcClient } from "@/app/store/wshrpcutil"; | ||||||||||||||||||||||||
| import type { TermViewModel } from "@/app/view/term/term-model"; | ||||||||||||||||||||||||
| import { atoms, getOverrideConfigAtom, getSettingsPrefixAtom, globalStore, WOS } from "@/store/global"; | ||||||||||||||||||||||||
| import { atoms, getApi, getOverrideConfigAtom, getSettingsPrefixAtom, globalStore, WOS } from "@/store/global"; | ||||||||||||||||||||||||
| import { fireAndForget, useAtomValueSafe } from "@/util/util"; | ||||||||||||||||||||||||
| import { computeBgStyleFromMeta } from "@/util/waveutil"; | ||||||||||||||||||||||||
| import { ISearchOptions } from "@xterm/addon-search"; | ||||||||||||||||||||||||
|
|
@@ -353,8 +353,78 @@ const TerminalView = ({ blockId, model }: ViewComponentProps<TermViewModel>) => | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const termBg = computeBgStyleFromMeta(blockData?.meta); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Handle drag and drop | ||||||||||||||||||||||||
| const handleDragOver = React.useCallback((e: React.DragEvent) => { | ||||||||||||||||||||||||
| e.preventDefault(); | ||||||||||||||||||||||||
| e.stopPropagation(); | ||||||||||||||||||||||||
| // Indicate that we can accept the drop | ||||||||||||||||||||||||
| e.dataTransfer.dropEffect = "copy"; | ||||||||||||||||||||||||
| }, []); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const handleDrop = React.useCallback((e: React.DragEvent) => { | ||||||||||||||||||||||||
| e.preventDefault(); | ||||||||||||||||||||||||
| e.stopPropagation(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Get files from the drop | ||||||||||||||||||||||||
| const files = Array.from(e.dataTransfer.files); | ||||||||||||||||||||||||
| if (files.length === 0) { | ||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| console.log("Drop files:", files); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Get the file path(s) using the Electron API | ||||||||||||||||||||||||
| const paths = files.map((file: File) => { | ||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| // Use the exposed Electron API to get the full path | ||||||||||||||||||||||||
| const fullPath = getApi().getPathForFile(file); | ||||||||||||||||||||||||
| console.log("File:", file.name, "-> Full path:", fullPath); | ||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Electron’s Useful? React with 👍 / 👎. |
||||||||||||||||||||||||
| return fullPath; | ||||||||||||||||||||||||
| } catch (err) { | ||||||||||||||||||||||||
| console.error("Could not get path for file:", file.name, err); | ||||||||||||||||||||||||
| return file.name; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| console.log("Paths to insert:", paths); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Insert the path(s) into the terminal | ||||||||||||||||||||||||
| // If multiple files, separate with spaces and quote if necessary | ||||||||||||||||||||||||
| const pathString = paths.map(path => { | ||||||||||||||||||||||||
| // Quote paths that contain spaces | ||||||||||||||||||||||||
| if (path.includes(" ")) { | ||||||||||||||||||||||||
| return `"${path}"`; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| return path; | ||||||||||||||||||||||||
| }).join(" "); | ||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Path quoting is incomplete and may cause command injection risks. The current implementation only quotes paths containing spaces, but shell commands require escaping many other special characters such as:
Without proper escaping, a malicious filename could inject shell commands. Consider implementing proper shell escaping or using a library for this purpose. 🔎 Example of improved shell escapingFor POSIX shells, you could use single-quote escaping where internal single quotes are replaced with // Insert the path(s) into the terminal
// If multiple files, separate with spaces and quote if necessary
const pathString = paths.map(path => {
- // Quote paths that contain spaces
- if (path.includes(" ")) {
- return `"${path}"`;
- }
- return path;
+ // Escape for POSIX shells: wrap in single quotes and escape any single quotes
+ return "'" + path.replace(/'/g, "'\\''") + "'";
}).join(" ");Note: This assumes a POSIX-compatible shell. For Windows Command Prompt or PowerShell, different escaping rules apply. You may need to detect the shell type or use a cross-platform escaping library. 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| console.log("Final path string:", pathString); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Send the path to the terminal | ||||||||||||||||||||||||
| if (model.termRef.current && pathString) { | ||||||||||||||||||||||||
| model.sendDataToController(pathString); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| }, [model]); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const handleDragEnter = React.useCallback((e: React.DragEvent) => { | ||||||||||||||||||||||||
| e.preventDefault(); | ||||||||||||||||||||||||
| e.stopPropagation(); | ||||||||||||||||||||||||
| }, []); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const handleDragLeave = React.useCallback((e: React.DragEvent) => { | ||||||||||||||||||||||||
| e.preventDefault(); | ||||||||||||||||||||||||
| e.stopPropagation(); | ||||||||||||||||||||||||
| }, []); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||
| <div className={clsx("view-term", "term-mode-" + termMode)} ref={viewRef}> | ||||||||||||||||||||||||
| <div | ||||||||||||||||||||||||
| className={clsx("view-term", "term-mode-" + termMode)} | ||||||||||||||||||||||||
| ref={viewRef} | ||||||||||||||||||||||||
| onDragOver={handleDragOver} | ||||||||||||||||||||||||
| onDrop={handleDrop} | ||||||||||||||||||||||||
| onDragEnter={handleDragEnter} | ||||||||||||||||||||||||
| onDragLeave={handleDragLeave} | ||||||||||||||||||||||||
| > | ||||||||||||||||||||||||
| {termBg && <div className="absolute inset-0 z-0 pointer-events-none" style={termBg} />} | ||||||||||||||||||||||||
| <TermResyncHandler blockId={blockId} model={model} /> | ||||||||||||||||||||||||
| <TermThemeUpdater blockId={blockId} model={model} termRef={model.termRef} /> | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.