Kotlin/JS中带格式的剪贴板复制

huangapple go评论89阅读模式
英文:

Clipboard copy with formatting in Kotlin/JS

问题

我正在开发一个Kotlin多平台项目。
我想要支持带格式的复制操作。
网上有很多使用JavaScript实现它的网页,它们几乎都是相同的,并且像这样执行:

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])

但我不知道如何在Kotlin中实现它。事实上,我无法构造ClipboardItem的实例。我应该怎么做?

以下是我在Kotlin中编写的不完整代码片段:

navigator.clipboard.write(
    arrayOf(
        ClipboardItem(  ..... ) // <--- 这里应该是什么?
    )
)
英文:

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 = &quot;SOMETHING!&quot;
const clipboardItem = new
        ClipboardItem({
               &#39;text/html&#39;:  new Blob([content],{type: &#39;text/html&#39;}),
               &#39;text/plain&#39;: new Blob([content],{type: &#39;text/plain&#39;})
    });
    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(  ..... ) // &lt;--- What should be here?
            )
        )

答案1

得分: 1

你不需要自己创建一个实例。已经有一个可用的实例在Window类下。你可以使用window.navigator来访问它。有关这个主题的更多信息,请参阅https://developer.mozilla.org/en-US/docs/Web/API/Navigator

英文:

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

huangapple
  • 本文由 发表于 2023年6月2日 02:01:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定