okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9caf39495
main
1/** 2* Copy text for the person. The browser clipboard call can be refused (the desktop web view asks for permission, or has no clipboard 3* object); the older copy command through a hidden field still works there. The answer is always a toast: "Copied", or one plain 4* sentence and what to do next (#3731). 5*/ 6export interface ClipboardDeps { 7clipboard ?:{ writeText ( text :string ) :Promise < void > } | null ; 8legacyCopy ?:( text :string ) => boolean ; 9toast :( tone :'success' | 'warn' , title :string , body ?:string ) => void ; 10} 11 12function legacyCopy ( text :string ) :boolean { 13if ( typeof document === 'undefined' ) return false ; 14const field = document . createElement ( 'textarea' ); 15field . value = text ; 16field . setAttribute ( 'readonly' , '' ); 17field . style . cssText = 'position:fixed;top:0;left:0;opacity:0;pointer-events:none' ; 18document . body . appendChild ( field ); 19const active = document . activeElement as HTMLElement | null ; 20try { 21field . select (); 22return document . execCommand ( 'copy' ); 23} catch { return false ; } 24finally { field . remove (); active ?. focus ?.(); } 25} 26 27export async function copyText ( text :string , label :string , deps :ClipboardDeps ) :Promise < boolean > { 28const clipboard = deps . clipboard === undefined ?( typeof navigator === 'undefined' ?null :navigator . clipboard ) :deps . clipboard ; 29let done = false ; 30try { if ( clipboard ) { await clipboard . writeText ( text ); done = true ; } } catch { /* the fallback is next */ } 31if ( ! done ) { try { done = ( deps . legacyCopy ?? legacyCopy )( text ); } catch { done = false ; } } 32if ( done ) deps . toast ( 'success' , 'Copied' , label ); 33else deps . toast ( 'warn' , 'Couldn’t copy' , 'Select the text and copy it with Ctrl+C.' ); 34return done ; 35}