Line data Source code
1 : /**
2 : * The drag half of the application-wide asset drag/drop contract.
3 : *
4 : * Every surface that lets a user drag an asset out of the assets panel writes
5 : * the same two flavours, so the drop side stays view-agnostic: the agent
6 : * composer accepts a drop by looking for {@link MIME_ASSET_INFO} in
7 : * `dataTransfer.types` alone, and the canvas loads the file from `text/uri-list`.
8 : * Keeping this in one place is what stops a view mode from silently shipping a
9 : * row that starts a drag carrying nothing.
10 : */
11 : import { getMediaTypeFromFilename } from '@/utils/formatUtil'
12 : import { api } from '@/scripts/api'
13 :
14 : import {
15 : getAssetType,
16 : isGeneratedTempOutput
17 : } from '@/platform/assets/composables/media/assetMappers'
18 : import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
19 : import { MIME_ASSET_INFO } from '@/platform/assets/schemas/mediaAssetSchema'
20 : import { getStoredOutputIdentity } from '@/platform/assets/utils/outputAssetUtil'
21 : import {
22 : getAssetStoredFilename,
23 : getOutputSubfolder
24 : } from './assetMetadataUtils'
25 : import { resolvePreviewUrl } from './assetPreviewUtil'
26 : import { getAssetFileUrl } from './assetUrlUtil'
27 : import { parseUrl } from '@/utils/urlSafety'
28 :
29 0 : function resolveThumbnailUrl(url: string): URL | null {
30 0 : const source = parseUrl(url) || url.startsWith('//') ? url : api.apiURL(url)
31 0 : return parseUrl(source, location.href)
32 : }
33 :
34 2 : function assetMediaSources(asset: AssetItem) {
35 2 : const mediaKind = getMediaTypeFromFilename(asset.name)
36 2 : const previewUrl = parseUrl(resolvePreviewUrl(asset), location.href)
37 : const playable = mediaKind === 'video' || mediaKind === 'audio'
38 2 : const mediaUrl = playable
39 : ? parseUrl(getAssetFileUrl(asset, { disposition: 'inline' }), location.href)
40 : : undefined
41 2 : const posterUrl = asset.preview_id
42 : ? previewUrl
43 : : asset.thumbnail_url && asset.thumbnail_url !== asset.preview_url
44 : ? resolveThumbnailUrl(asset.thumbnail_url)
45 : : undefined
46 2 : return {
47 : media_kind: mediaKind,
48 : preview_url:
49 : mediaKind === 'image'
50 : ? previewUrl?.toString()
51 : : mediaKind === 'video'
52 : ? posterUrl?.toString()
53 : : undefined,
54 : media_url: mediaUrl?.toString()
55 : }
56 : }
57 :
58 : /**
59 : * Start a native drag carrying `asset`.
60 : *
61 : * Ctrl/Meta cancels the drag rather than carrying the asset: those modifiers
62 : * belong to panel selection (marquee, range select), and a native drag would
63 : * swallow the gesture.
64 : *
65 : * @param event The `dragstart` event from the dragged row or card
66 : * @param asset The asset that row or card renders, if it has one yet
67 : */
68 6 : export function startAssetDrag(
69 : event: DragEvent,
70 : asset: AssetItem | undefined
71 : ): void {
72 6 : if (event.ctrlKey || event.metaKey) {
73 4 : event.preventDefault()
74 4 : return
75 : }
76 :
77 2 : if (!asset) return
78 :
79 2 : const { dataTransfer } = event
80 2 : if (!dataTransfer) return
81 :
82 6 : const storedHash = asset.hash ?? getStoredOutputIdentity(asset)?.hash
83 6 : const assetInfo = {
84 : filename: getAssetStoredFilename(asset, storedHash),
85 : subfolder: getOutputSubfolder(asset),
86 : type: isGeneratedTempOutput(asset) ? 'temp' : getAssetType(asset.tags),
87 : display_name: asset.display_name || asset.name,
88 : attachment_ref: storedHash ?? asset.name,
89 : ...assetMediaSources(asset)
90 : }
91 6 : dataTransfer.setData(MIME_ASSET_INFO, JSON.stringify(assetInfo))
92 :
93 6 : const fileUrl = parseUrl(getAssetFileUrl(asset), location.href)
94 6 : if (!fileUrl) return
95 :
96 2 : dataTransfer.setData('text/uri-list', fileUrl.toString())
97 : }
|