Repository navigation
feat(desktop): drag files from the file tree into other Windows apps #65330
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
Open
Hona
wants to merge
3
commits into
dev
Choose a base branch
from
file-drag-out
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+169
−2
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { describe, expect, test } from "bun:test" | ||
| import { dragOutText, draggingOut, startDragOut } from "./drag-out" | ||
|
|
||
| const path = "C:\\repo\\src\\index.ts" | ||
|
|
||
| function rowDrag(row = document.createElement("button")) { | ||
| const dataTransfer = new DataTransfer() | ||
| dataTransfer.setData("text/plain", "file:src/index.ts") | ||
|
|
||
| const event = new DragEvent("dragstart", { dataTransfer, cancelable: true }) | ||
| // happy-dom drops `dataTransfer` from the init dictionary; a browser keeps it. | ||
| Object.defineProperty(event, "dataTransfer", { value: dataTransfer }) | ||
|
|
||
| row.dispatchEvent(event) | ||
|
|
||
| return event | ||
| } | ||
|
|
||
| describe("drag out", () => { | ||
| test("a drop of the dragged-out file reads as the row's drag until the system drag ends, which ends the row's drag", async () => { | ||
| const ended = Promise.withResolvers<boolean>() | ||
| const row = document.createElement("button") | ||
| const ends: string[] = [] | ||
| row.addEventListener("dragend", (end) => ends.push(end.type)) | ||
| const event = rowDrag(row) | ||
|
|
||
| expect(startDragOut({ startFileDrag: () => ended.promise }, event, path)).toBe(true) | ||
| expect(event.defaultPrevented).toBe(true) | ||
| expect(draggingOut()).toBe(true) | ||
| expect(dragOutText([path])).toBe("file:src/index.ts") | ||
| // Windows paths ignore case and take either separator. | ||
| expect(dragOutText(["c:/repo/src/INDEX.ts"])).toBe("file:src/index.ts") | ||
| expect(dragOutText(["C:\\repo\\other.ts"])).toBeUndefined() | ||
| expect(dragOutText([path, "C:\\repo\\other.ts"])).toBeUndefined() | ||
|
|
||
| ended.resolve(true) | ||
| await ended.promise | ||
| await Promise.resolve() | ||
|
|
||
| expect(draggingOut()).toBe(false) | ||
| expect(dragOutText([path])).toBeUndefined() | ||
| expect(ends).toEqual(["dragend"]) | ||
| }) | ||
|
|
||
| test("without a system drag the row keeps its own drag", () => { | ||
| const event = rowDrag() | ||
|
|
||
| expect(startDragOut({}, event, path)).toBe(false) | ||
| expect(event.defaultPrevented).toBe(false) | ||
| expect(draggingOut()).toBe(false) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import type { Platform } from "./platform" | ||
|
|
||
| type DragOut = { readonly path: string; readonly text: string } | ||
|
|
||
| type DragOutState = { current: DragOut | undefined } | ||
|
|
||
| // The file drag handed to the system, with the `text/plain` the app's own drag carried, until the drag ends. A drop | ||
| // back into the app then reads as that drag did, not as a file from another app. | ||
| const state: DragOutState = { current: undefined } | ||
|
|
||
| /** | ||
| * Hands a file row's `dragstart` to the system's drag, so the file drops into other apps as the file itself. False | ||
| * when the platform has no system drag; the event's own drag goes on then. When the system drag ends, the row gets | ||
| * the `dragend` its cancelled drag never fired, so whatever started dragging it can finish. | ||
| */ | ||
| export function startDragOut(platform: Pick<Platform, "startFileDrag">, event: DragEvent, path: string) { | ||
| if (!platform.startFileDrag || !event.dataTransfer) return false | ||
| const drag = { path, text: event.dataTransfer.getData("text/plain") } | ||
| const source = event.target | ||
| event.preventDefault() | ||
| state.current = drag | ||
|
|
||
| const end = () => { | ||
| if (state.current === drag) state.current = undefined | ||
| source?.dispatchEvent(new DragEvent("dragend", { bubbles: true })) | ||
| } | ||
|
|
||
| void platform.startFileDrag(path).then(end, end) | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| /** Whether a drag handed to the system is under way. */ | ||
| export function draggingOut() { | ||
| return state.current !== undefined | ||
| } | ||
|
|
||
| /** The text the app's own drag carried, when the dropped files are the drag handed to the system. */ | ||
| export function dragOutText(paths: readonly string[]) { | ||
| const drag = state.current | ||
|
|
||
| if (!drag || paths.length !== 1 || !sameFile(paths[0] ?? "", drag.path)) return | ||
|
|
||
| return drag.text | ||
| } | ||
|
|
||
| // The system drag is Windows-only, where paths ignore case and take either separator. | ||
| function sameFile(a: string, b: string) { | ||
| const key = (value: string) => value.replaceAll("/", "\\").toLowerCase() | ||
|
|
||
| return key(a) === key(b) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.