okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-split-int (a3024ed10) into f-split-qa1: qa2's s3237-s3238 kept, my 21 strings renumbered s3260-s32808667a3dbf

main
2.0 KiB34 linesraw
1/**
2 * Files dropped on the app window on Linux and macOS. The page gets the drag events there but not the files (WebKit hides them), so the app
3 * shell reads the drop, hands the paths to the host, and tells the page with these two events (apps/desktop/src-tauri/src/main.rs):
4 *   strata:native-drag  detail 'enter' | 'leave'      the drop box shows or goes away
5 *   strata:native-drop  detail [{ id, name, type }]    the host holds each file once under its id
6 */
7export const NATIVE_DRAG_EVENT = 'strata:native-drag';
8export const NATIVE_DROP_EVENT = 'strata:native-drop';
9export interface DroppedRef { id: string; name: string; type: string }
10
11/** The files the shell announced; anything that is not a well-formed entry is left out. */
12export function readDroppedRefs(detail: unknown): DroppedRef[] {
13  if (!Array.isArray(detail)) return [];
14  return detail.flatMap(x => {
15    const r = x as Record<string, unknown>;
16    return typeof r?.id === 'string' && /^[0-9a-f-]{36}$/.test(r.id) && typeof r.name === 'string' && r.name
17      ? [{ id: r.id, name: r.name, type: typeof r.type === 'string' ? r.type : 'application/octet-stream' }] : [];
18  }).slice(0, 10);
19}
20
21/** Fetch each announced file from the host and make it a File, the way a browser drop would have delivered it. A file that cannot be fetched is skipped. */
22export async function fetchDropped(refs: DroppedRef[], fetcher: (url: string) => Promise<{ ok: boolean; blob(): Promise<Blob> }> = url => fetch(url)): Promise<File[]> {
23  const files: File[] = [];
24  for (const ref of refs) {
25    try {
26      const response = await fetcher(`/api/app/drop/${ref.id}`);
27      if (response.ok) files.push(new File([await response.blob()], ref.name, { type: ref.type }));
28    } catch { /* skipped */ }
29  }
30  return files;
31}
32
33/** True for a drag that carries files, as far as the page can tell (WebKit lists them as text/uri-list, others as Files). */
34export const dragHasFiles = (types: ArrayLike<string> | null | undefined) => Array.from(types ?? []).some(t => t === 'Files' || t === 'text/uri-list');