-
Notifications
You must be signed in to change notification settings - Fork 3k
feat(web): drag files from the explorer into the chat composer #4140
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
yordis
wants to merge
2
commits into
pingdotgg:main
Choose a base branch
from
TrogonStack:yordis/feat-file-tree-drag-to-chat-upstream
base: main
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.
Open
Changes from all commits
Commits
Show all changes
2 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
123 changes: 123 additions & 0 deletions
123
apps/web/src/components/chat/composerMentionDrag.test.ts
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,123 @@ | ||
| import { describe, expect, it } from "@effect/vitest"; | ||
|
|
||
| import { | ||
| COMPOSER_MENTION_DRAG_TYPE, | ||
| type ComposerMentionDropHost, | ||
| composerMentionFromTreePath, | ||
| dataTransferHasComposerMention, | ||
| makeComposerMentionDragHandlers, | ||
| } from "./composerMentionDrag.ts"; | ||
|
|
||
| const makeDragEvent = (options?: { mention?: string; types?: ReadonlyArray<string> }) => { | ||
| const mention = options?.mention ?? "[index.md](docs/index.md)"; | ||
| const calls: Array<string> = []; | ||
| const event = { | ||
| dataTransfer: { | ||
| types: options?.types ?? [COMPOSER_MENTION_DRAG_TYPE, "text/plain"], | ||
| getData: (format: string) => (format === COMPOSER_MENTION_DRAG_TYPE ? mention : ""), | ||
| dropEffect: "none", | ||
| }, | ||
| nativeEvent: { | ||
| stopPropagation: () => void calls.push("nativeStopPropagation"), | ||
| }, | ||
| preventDefault: () => void calls.push("preventDefault"), | ||
| stopPropagation: () => void calls.push("stopPropagation"), | ||
| }; | ||
| return { event, calls }; | ||
| }; | ||
|
|
||
| const makeHost = (insertResult = true) => { | ||
| const log: Array<string> = []; | ||
| const host: ComposerMentionDropHost = { | ||
| insertMentionAtEnd: (text) => { | ||
| log.push(`insert:${text}`); | ||
| return insertResult; | ||
| }, | ||
| setDragActive: (active) => void log.push(`active:${active}`), | ||
| onInsertRejected: () => void log.push("rejected"), | ||
| }; | ||
| return { host, log }; | ||
| }; | ||
|
|
||
| describe("composerMentionFromTreePath", () => { | ||
| it("serializes a file path into a mention", () => { | ||
| expect(composerMentionFromTreePath("docs/index.md")).toBe("[index.md](docs/index.md)"); | ||
| }); | ||
|
|
||
| it("strips the trailing slash directory rows carry", () => { | ||
| expect(composerMentionFromTreePath("docs/architecture/")).toBe( | ||
| "[architecture](docs/architecture)", | ||
| ); | ||
| }); | ||
|
|
||
| it("rejects drags that carry no path", () => { | ||
| expect(composerMentionFromTreePath("")).toBeNull(); | ||
| expect(composerMentionFromTreePath("/")).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("dataTransferHasComposerMention", () => { | ||
| it("detects the mention payload among drag types", () => { | ||
| expect(dataTransferHasComposerMention([COMPOSER_MENTION_DRAG_TYPE, "text/plain"])).toBe(true); | ||
| expect(dataTransferHasComposerMention(["Files"])).toBe(false); | ||
| expect(dataTransferHasComposerMention([])).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("makeComposerMentionDragHandlers", () => { | ||
| it("leaves drags without the mention payload alone", () => { | ||
| const { host, log } = makeHost(); | ||
| const handlers = makeComposerMentionDragHandlers(host); | ||
| const { event, calls } = makeDragEvent({ types: ["Files"] }); | ||
| handlers.onDragEnter(event); | ||
| handlers.onDragOver(event); | ||
| handlers.onDrop(event); | ||
| expect(calls).toEqual([]); | ||
| expect(log).toEqual([]); | ||
| }); | ||
|
|
||
| it("stops the native event too, not just the synthetic one", () => { | ||
| // React's stopPropagation only halts synthetic dispatch; without the | ||
| // native stop, the editor's own DOM listeners process the drop and sync | ||
| // their stale state back over the inserted mention. | ||
| const { host } = makeHost(); | ||
| const handlers = makeComposerMentionDragHandlers(host); | ||
| const { event, calls } = makeDragEvent(); | ||
| handlers.onDrop(event); | ||
| expect(calls).toContain("preventDefault"); | ||
| expect(calls).toContain("stopPropagation"); | ||
| expect(calls).toContain("nativeStopPropagation"); | ||
| }); | ||
|
|
||
| it('answers dragover with the "move" effect the tree allows', () => { | ||
| // Naming an effect outside the source's effectAllowed makes the browser | ||
| // cancel the drop without ever firing it. | ||
| const { host } = makeHost(); | ||
| const handlers = makeComposerMentionDragHandlers(host); | ||
| const { event } = makeDragEvent(); | ||
| handlers.onDragOver(event); | ||
| expect(event.dataTransfer.dropEffect).toBe("move"); | ||
| }); | ||
|
|
||
| it("inserts the mention with its trailing space and clears the highlight", () => { | ||
| const { host, log } = makeHost(); | ||
| const handlers = makeComposerMentionDragHandlers(host); | ||
| handlers.onDragEnter(makeDragEvent().event); | ||
| handlers.onDrop(makeDragEvent().event); | ||
| expect(log).toEqual(["active:true", "active:false", "insert:[index.md](docs/index.md) "]); | ||
| }); | ||
|
|
||
| it("reports a rejected insert instead of failing silently", () => { | ||
| const { host, log } = makeHost(false); | ||
| const handlers = makeComposerMentionDragHandlers(host); | ||
| handlers.onDrop(makeDragEvent().event); | ||
| expect(log).toContain("rejected"); | ||
| }); | ||
|
|
||
| it("ignores a drop whose payload is empty", () => { | ||
| const { host, log } = makeHost(); | ||
| const handlers = makeComposerMentionDragHandlers(host); | ||
| handlers.onDrop(makeDragEvent({ mention: "" }).event); | ||
| expect(log).toEqual(["active: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,98 @@ | ||
| import { serializeComposerFileLink } from "@t3tools/shared/composerTrigger"; | ||
|
|
||
| /** | ||
| * Drag payload type carrying a serialized composer mention. Set on drags that | ||
| * start in the workspace file tree so the composer can tell them apart from | ||
| * OS file drags and plain text selections. | ||
| */ | ||
| export const COMPOSER_MENTION_DRAG_TYPE = "application/x-t3code-composer-mention"; | ||
|
|
||
| export function composerMentionFromTreePath(treePath: string): string | null { | ||
| const relativePath = treePath.replace(/\/+$/, ""); | ||
| if (relativePath.length === 0) { | ||
| return null; | ||
| } | ||
| return serializeComposerFileLink(relativePath); | ||
| } | ||
|
|
||
| export function dataTransferHasComposerMention(types: ReadonlyArray<string>): boolean { | ||
| return types.includes(COMPOSER_MENTION_DRAG_TYPE); | ||
| } | ||
|
|
||
| export interface ComposerMentionDragTransfer { | ||
| readonly types: ReadonlyArray<string>; | ||
| getData(format: string): string; | ||
| dropEffect: string; | ||
| } | ||
|
|
||
| export interface ComposerMentionDragEvent { | ||
| readonly dataTransfer: ComposerMentionDragTransfer; | ||
| readonly nativeEvent: { stopPropagation(): void }; | ||
| preventDefault(): void; | ||
| stopPropagation(): void; | ||
| } | ||
|
|
||
| /** | ||
| * What a mention drop is allowed to do to the composer. Deliberately narrow: | ||
| * there is no way to focus the editor from here. Focusing it synchronously | ||
| * during the drop makes the not-yet-reconciled editor sync its stale empty | ||
| * state back over the inserted mention; the insert path already focuses on | ||
| * the next frame, after the editor has caught up. | ||
| */ | ||
| export interface ComposerMentionDropHost { | ||
| insertMentionAtEnd(text: string): boolean; | ||
| setDragActive(active: boolean): void; | ||
| onInsertRejected(): void; | ||
| } | ||
|
|
||
| export interface ComposerMentionDragHandlers { | ||
| onDragEnter(event: ComposerMentionDragEvent): void; | ||
| onDragOver(event: ComposerMentionDragEvent): void; | ||
| onDrop(event: ComposerMentionDragEvent): void; | ||
| } | ||
|
|
||
| export function makeComposerMentionDragHandlers( | ||
| host: ComposerMentionDropHost, | ||
| ): ComposerMentionDragHandlers { | ||
| // Claim the event for the composer: React's stopPropagation only halts the | ||
| // synthetic dispatch, so the native event must be stopped too or the | ||
| // editor's own DOM listeners still process the drag. | ||
| const claim = (event: ComposerMentionDragEvent): boolean => { | ||
| if (!dataTransferHasComposerMention(event.dataTransfer.types)) { | ||
| return false; | ||
| } | ||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
| event.nativeEvent.stopPropagation(); | ||
| return true; | ||
| }; | ||
| return { | ||
| onDragEnter(event) { | ||
| if (claim(event)) { | ||
| host.setDragActive(true); | ||
| } | ||
| }, | ||
| onDragOver(event) { | ||
| if (!claim(event)) { | ||
| return; | ||
| } | ||
| // The tree constrains its drags to effectAllowed "move"; naming any | ||
| // other effect makes the browser cancel the drop without firing it. | ||
| event.dataTransfer.dropEffect = "move"; | ||
| host.setDragActive(true); | ||
| }, | ||
| onDrop(event) { | ||
| if (!claim(event)) { | ||
| return; | ||
| } | ||
| host.setDragActive(false); | ||
| const mention = event.dataTransfer.getData(COMPOSER_MENTION_DRAG_TYPE); | ||
| if (mention.length === 0) { | ||
| return; | ||
| } | ||
| if (!host.insertMentionAtEnd(`${mention} `)) { | ||
| host.onInsertRejected(); | ||
| } | ||
| }, | ||
| }; | ||
| } |
Oops, something went wrong.
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.