mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-08 17:49:44 +00:00
add extensions in JS
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package dev.inmo.micro_utils.common
|
||||
|
||||
import kotlinx.browser.window
|
||||
|
||||
fun copyToClipboard(text: String): Boolean {
|
||||
return runCatching {
|
||||
window.navigator.clipboard.writeText(
|
||||
text
|
||||
)
|
||||
}.onFailure {
|
||||
it.printStackTrace()
|
||||
}.isSuccess
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
package dev.inmo.micro_utils.common
|
||||
|
||||
import kotlinx.browser.window
|
||||
import org.w3c.files.Blob
|
||||
import org.w3c.files.BlobPropertyBag
|
||||
import kotlin.js.json
|
||||
|
||||
external class ClipboardItem(data: dynamic)
|
||||
|
||||
inline fun Blob.convertToClipboardItem(): ClipboardItem {
|
||||
val itemData: dynamic = json(this.type to this)
|
||||
return ClipboardItem(itemData)
|
||||
}
|
||||
|
||||
suspend fun copyImageURLToClipboard(imageUrl: String): Boolean {
|
||||
return runCatching {
|
||||
val response = window.fetch(imageUrl).await()
|
||||
val blob = response.blob().await()
|
||||
val data = arrayOf(
|
||||
Blob(
|
||||
arrayOf(blob),
|
||||
BlobPropertyBag("image/png")
|
||||
).convertToClipboardItem()
|
||||
).asDynamic()
|
||||
window.navigator.clipboard.write(data)
|
||||
}.onFailure {
|
||||
it.printStackTrace()
|
||||
}.isSuccess
|
||||
}
|
Reference in New Issue
Block a user