okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8667a3dbf
main
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 [] { 13if ( ! Array . isArray ( detail )) return []; 14return detail . flatMap ( x => { 15const r = x as Record < string , unknown >; 16return 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 []> { 23const files :File [] = []; 24for ( const ref of refs ) { 25try { 26const response = await fetcher ( `/api/app/drop/ ${ ref . id } ` ); 27if ( response . ok ) files . push ( new File ([ await response . blob ()], ref . name , { type :ref . type })); 28} catch { /* skipped */ } 29} 30return 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' );