英文:
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 = "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
得分: 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论