| 156 | * Never throws unless textPromise is rejected. |
| 157 | */ |
| 158 | export async function copyToClipboard( |
| 159 | textPromise: Promise<string>, |
| 160 | ): Promise<void> { |
| 161 | // Try the new clipboard APIs first. If that fails, use textarea fallback. |
| 162 | try { |
| 163 | await navigator.clipboard.write([ |
| 164 | new ClipboardItem({ |
| 165 | "text/plain": textPromise, |
| 166 | }), |
| 167 | ]); |
| 168 | return; |
| 169 | } catch (err) { |
| 170 | console.warn(err); |
| 171 | } |
| 172 | |
| 173 | const text = await textPromise; |
| 174 | |
| 175 | try { |
| 176 | await navigator.clipboard.writeText(text); |
| 177 | return; |
| 178 | } catch (err) { |
| 179 | console.warn(err); |
| 180 | } |
| 181 | |
| 182 | const t = document.createElement("textarea"); |
| 183 | t.value = text; |
| 184 | t.style.position = "absolute"; |
| 185 | t.style.opacity = "0"; |
| 186 | document.body.appendChild(t); |
| 187 | try { |
| 188 | t.focus(); |
| 189 | t.select(); |
| 190 | if (!document.execCommand("copy")) { |
| 191 | throw "failed to copy"; |
| 192 | } |
| 193 | } catch { |
| 194 | alert(text); |
| 195 | } finally { |
| 196 | t.remove(); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | export async function copyViewContentDataToClipboard( |
| 201 | contentViewData: ContentViewData | undefined, |