// SPDX-License-Identifier: LGPL-2.1-only // Copyright (C) 2026 Liberux Labs, S. L. //! Copies to the system clipboard through `navigator.clipboard`, which web-sys keeps behind its unstable APIs. Pasting needs no reading: the page's `paste` event carries the text (`input.rs`). use wasm_bindgen::closure::Closure; use wasm_bindgen::{ JsCast, JsValue }; /// Offer `text` to other applications. The browser allows it only shortly after the user acted on the page, which a copy always follows. pub( super ) fn write( text: &str ) { let Some( window ) = web_sys::window() else { return }; let Ok( clipboard ) = js_sys::Reflect::get( &window.navigator(), &JsValue::from_str( "clipboard" ) ) else { return }; if clipboard.is_undefined() { return; } let Ok( write_text ) = js_sys::Reflect::get( &clipboard, &JsValue::from_str( "writeText" ) ) else { return }; let Some( write_text ) = write_text.dyn_ref::() else { return }; let Ok( promise ) = write_text.call1( &clipboard, &JsValue::from_str( text ) ) else { return }; let refused = Closure::once_into_js( |_: JsValue| web_sys::console::warn_1( &"[ltk] the browser refused to write the clipboard".into() ) ); if let Ok( catch ) = js_sys::Reflect::get( &promise, &JsValue::from_str( "catch" ) ) { if let Some( catch ) = catch.dyn_ref::() { let _ = catch.call1( &promise, &refused ); } } }