Clipboard copy with formatting in Kotlin/JS

108 Views Asked by At

I am working on a Kotlin multiplatform project. I want to support copy action with formatting. There are a plenty of page in the web implementing it in javascript. All of them are almost same and do it like this:

const content = "SOMETHING!"
const clipboardItem = new
        ClipboardItem({
               'text/html':  new Blob([content],{type: 'text/html'}),
               'text/plain': new Blob([content],{type: 'text/plain'})
    });
    navigator.clipboard.write([clipboardItem])

But I don't know how should I implement it in Kotlin. Indeed, I could not construct an instance of ClipboardItem. How should I do it?

The following is my incomplete snippet written in Kotlin:

navigator.clipboard.write(
            arrayOf(
                ClipboardItem(  ..... ) // <--- What should be here?
            )
        )
1

There are 1 best solutions below

3
Khalid Elkhidir On

You don't need to create an instance yourself. There is already one available for you under Window class. You can access it using window.navigator. Here's more about the subject https://developer.mozilla.org/en-US/docs/Web/API/Navigator