fix(desktop): dragging a link from a browser attaches an @url chip
Dropping a link out of a browser onto the Desktop composer toasted
"Drop files — Could not attach <title>.url" and attached nothing. A browser
link drag carries `text/uri-list` plus, on Windows, a virtual `<title>.url`
shortcut File (`.webloc` on macOS) that has no on-disk path. The drop
pipeline only understood Files and in-app paths: the path-less stub went to
the upload branch, `attachContextFilePath('')` returned false, and the user
had to copy/paste the URL instead.
`extractDroppedFiles` now reads `text/uri-list`, drops the path-less
shortcut stub when a link is present, and emits `{ url }` entries;
`droppedFileInlineRef` turns them into the same `@url:` chip the "+ → Add
URL" dialog and paste-linkify produce, so every drop surface (composer
form, text box, conversation area, edit composer) gets it for free.
`dragHasAttachments` accepts `text/uri-list` so the form-level enter/over
handlers claim the drag at all. A path-less *image* dragged off a web page
keeps its bytes and wins over the link to its own src.
This commit is contained in:
@@ -38,11 +38,15 @@ export function dragHasAttachments(transfer: DataTransfer | null, pathsMime: str
|
||||
return false
|
||||
}
|
||||
|
||||
if (Array.from(transfer.types || []).includes(pathsMime)) {
|
||||
const types = Array.from(transfer.types || [])
|
||||
|
||||
// A browser link drag carries `text/uri-list` (on Windows also a virtual
|
||||
// shortcut File); accept it so the link lands as an `@url:` chip.
|
||||
if (types.includes(pathsMime) || types.includes('text/uri-list')) {
|
||||
return true
|
||||
}
|
||||
|
||||
if (Array.from(transfer.types || []).includes('Files')) {
|
||||
if (types.includes('Files')) {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -50,6 +54,10 @@ export function dragHasAttachments(transfer: DataTransfer | null, pathsMime: str
|
||||
}
|
||||
|
||||
export function droppedFileInlineRef(candidate: DroppedFile, cwd: string | null | undefined) {
|
||||
if (candidate.url) {
|
||||
return `@url:${formatRefValue(candidate.url)}`
|
||||
}
|
||||
|
||||
if (!candidate.path) {
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { $composerAttachments, type ComposerAttachment, updateComposerAttachment } from '@/store/composer'
|
||||
import { $connection } from '@/store/session'
|
||||
|
||||
import { droppedFileInlineRefs } from '../composer/inline-refs'
|
||||
|
||||
import {
|
||||
attachmentPreviewDataUrl,
|
||||
type DroppedFile,
|
||||
@@ -87,9 +89,14 @@ interface StubEntry {
|
||||
isDirectory: boolean
|
||||
}
|
||||
|
||||
function stubTransfer(entries: StubEntry[], internalRaw = ''): DataTransfer & { _pathByFile: Map<File, string> } {
|
||||
function stubTransfer(
|
||||
entries: StubEntry[],
|
||||
internalRaw = '',
|
||||
uriList = ''
|
||||
): DataTransfer & { _pathByFile: Map<File, string> } {
|
||||
const files = entries.map(entry => new File(['x'], entry.path.split('/').pop() || 'f'))
|
||||
const pathByFile = new Map(files.map((file, i) => [file, entries[i].path]))
|
||||
// A virtual shortcut File (browser link drag on Windows) has a name but no path.
|
||||
const pathByFile = new Map(files.map((file, i) => [file, entries[i].path.includes('/') ? entries[i].path : '']))
|
||||
|
||||
const items: Record<number | string, unknown> = { length: entries.length }
|
||||
entries.forEach((entry, i) => {
|
||||
@@ -101,7 +108,7 @@ function stubTransfer(entries: StubEntry[], internalRaw = ''): DataTransfer & {
|
||||
})
|
||||
|
||||
return {
|
||||
getData: (mime: string) => (mime === HERMES_PATHS_MIME ? internalRaw : ''),
|
||||
getData: (mime: string) => (mime === HERMES_PATHS_MIME ? internalRaw : mime === 'text/uri-list' ? uriList : ''),
|
||||
files: {
|
||||
length: files.length,
|
||||
item: (i: number) => files[i] ?? null
|
||||
@@ -174,6 +181,41 @@ describe('extractDroppedFiles', () => {
|
||||
expect(osDrops.map(entry => entry.path)).toEqual(['/abs/notes.txt'])
|
||||
})
|
||||
|
||||
it('turns a browser link drag into an @url chip instead of failing on the virtual .url stub', () => {
|
||||
// Dragging a link out of a browser on Windows lands as `text/uri-list` plus a
|
||||
// path-less `<title>.url` shortcut File. That stub used to reach the upload
|
||||
// pipeline and toast "Could not attach agent-wiki.url".
|
||||
const transfer = stubTransfer(
|
||||
[{ path: 'agent-wiki.url', isDirectory: false }],
|
||||
'',
|
||||
'https://example.com/wiki/agent?x=1\r\n'
|
||||
) as DataTransfer & { _pathByFile: Map<File, string> }
|
||||
|
||||
stubBridge(transfer)
|
||||
|
||||
const result = extractDroppedFiles(transfer)
|
||||
|
||||
expect(result).toEqual([{ path: '', url: 'https://example.com/wiki/agent?x=1' }])
|
||||
expect(partitionDroppedFiles(result).osDrops).toEqual([])
|
||||
expect(droppedFileInlineRefs(result, '/w')).toEqual(['@url:https://example.com/wiki/agent?x=1'])
|
||||
})
|
||||
|
||||
it('keeps a path-less image dragged off a web page as an upload, not a link chip', () => {
|
||||
const transfer = stubTransfer(
|
||||
[{ path: 'logo.png', isDirectory: false }],
|
||||
'',
|
||||
'https://example.com/logo.png'
|
||||
) as DataTransfer & { _pathByFile: Map<File, string> }
|
||||
|
||||
stubBridge(transfer)
|
||||
|
||||
const result = extractDroppedFiles(transfer)
|
||||
|
||||
expect(result).toHaveLength(1)
|
||||
expect(result[0]?.file).toBeInstanceOf(File)
|
||||
expect(result[0]?.url).toBeUndefined()
|
||||
})
|
||||
|
||||
it('does not duplicate a folder that appears in both items and files', () => {
|
||||
// Chromium lists a dropped folder in transfer.files too (as a size-0 File);
|
||||
// the items pass claims its path first so the files fallback skips it.
|
||||
|
||||
@@ -90,6 +90,8 @@ export interface DroppedFile {
|
||||
line?: number
|
||||
/** Last line number for line-range drags (`line..lineEnd` inclusive). */
|
||||
lineEnd?: number
|
||||
/** A link dragged out of a browser (`text/uri-list`). Path-less; becomes an `@url:` chip. */
|
||||
url?: string
|
||||
}
|
||||
|
||||
/** MIME emitted by in-app drag sources (project tree, gutter line numbers).
|
||||
@@ -110,6 +112,7 @@ export function extractDroppedFiles(transfer: DataTransfer): DroppedFile[] {
|
||||
const seenPaths = new Set<string>()
|
||||
const seenFiles = new Set<File>()
|
||||
const getPath = window.hermesDesktop?.getPathForFile
|
||||
const urls = droppedLinkUrls(transfer)
|
||||
|
||||
// In-app drags first — they carry richer metadata (isDirectory) than the
|
||||
// File-based fallback can provide, and produce no overlapping native files.
|
||||
@@ -169,6 +172,20 @@ export function extractDroppedFiles(transfer: DataTransfer): DroppedFile[] {
|
||||
}
|
||||
}
|
||||
|
||||
// A link dragged out of a browser rides along as a virtual shortcut File
|
||||
// (`<title>.url` on Windows, `.webloc` on macOS) with no on-disk path. It
|
||||
// is the same link `text/uri-list` already carries, so drop the stub and
|
||||
// let the URL become a chip instead of toasting "Could not attach X.url".
|
||||
// A path-less *image* (dragged off a web page) keeps its bytes and wins
|
||||
// over the link to its own src.
|
||||
if (!path && urls.length) {
|
||||
if (isImagePath(file.name) || file.type.startsWith('image/')) {
|
||||
urls.length = 0
|
||||
} else {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if (path && seenPaths.has(path)) {
|
||||
return
|
||||
}
|
||||
@@ -238,9 +255,37 @@ export function extractDroppedFiles(transfer: DataTransfer): DroppedFile[] {
|
||||
}
|
||||
}
|
||||
|
||||
for (const url of urls) {
|
||||
result.push({ path: '', url })
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/** `http(s)` links from a `text/uri-list` payload (one per line, `#` comments
|
||||
* skipped), deduped. Empty when the drag carried none. */
|
||||
function droppedLinkUrls(transfer: DataTransfer): string[] {
|
||||
let raw = ''
|
||||
|
||||
try {
|
||||
raw = transfer.getData('text/uri-list') || ''
|
||||
} catch {
|
||||
return []
|
||||
}
|
||||
|
||||
const urls: string[] = []
|
||||
|
||||
for (const line of raw.split(/\r?\n/)) {
|
||||
const url = line.trim()
|
||||
|
||||
if (/^https?:\/\/[^/\s]/i.test(url) && !urls.includes(url)) {
|
||||
urls.push(url)
|
||||
}
|
||||
}
|
||||
|
||||
return urls
|
||||
}
|
||||
|
||||
/**
|
||||
* Split dropped entries by origin. OS/Finder drops carry a native `File`
|
||||
* handle; in-app drags (project tree, gutter line refs) are path-only.
|
||||
|
||||
Reference in New Issue
Block a user