简体   繁体   English

浏览器复制/粘贴组件

[英]Browser Copy/Paste the components

We are using new clipboard API to implement the browser/operating system wide copy paste.我们正在使用新的剪贴板 API 来实现浏览器/操作系统范围的复制粘贴。

We have a set of components (Assume it like a flow charts which have connected div) and that is build from a simple json.我们有一组组件(假设它就像一个连接了 div 的流程图),它是从一个简单的 json 构建的。

Our goal is to implement Copy and paste.我们的目标是实现复制和粘贴。 We have a underlying JsON in hand, i tried to save the Json file in clipboard.我们手头有一个底层的 JsON,我试图将 Json 文件保存在剪贴板中。

Now the real problem starts:现在真正的问题开始了:

1: Copy and Paste is operating system wide, so how can i know that, currently copied element is json and that is what we needed to build the flow. 1:复制和粘贴是操作系统范围的,所以我怎么知道,当前复制的元素是 json,这是我们构建流程所需的。 Eg: a user can copy whatever they want, but i only want the data which my system can parse.例如:用户可以复制他们想要的任何内容,但我只想要我的系统可以解析的数据。

2: How generally these type of applications works, for example, on Slack, i copied a formatted markdown message into my clipboard and i pasted the same into a text editor, but i don't see any markdown command on the selected text, but somehow i pasted the same thing in slack, i got the same message which i copied earlier. 2:这些类型的应用程序通常如何工作,例如,在 Slack 上,我将格式化的降价消息复制到剪贴板中,然后将其粘贴到文本编辑器中,但在所选文本上看不到任何降价命令,但是不知何故,我在 slack 中粘贴了相同的内容,我收到了我之前复制的相同消息。

Is anyone have done Copy/Paste of components, Any help highly appreciated.有没有人做过组件的复制/粘贴,非常感谢任何帮助。

Here's a basic example for how to do clipboard manipulation.这是如何进行剪贴板操作的基本示例。 Read up on paste and copy events for more detailed info.阅读粘贴复制事件以获取更多详细信息。 You could also try to set a different content-type for the clipboard data.您还可以尝试为剪贴板数据设置不同的content-type This is probably how Slack does it: Set one clipboard entry in plain text (without markdown formatting) and one in Markdown.这可能是 Slack 的做法:在纯文本中设置一个剪贴板条目(没有 Markdown 格式),在 Markdown 中设置一个。

 const input = document.getElementById("testInput"); input.addEventListener("copy", (e) => { console.log("copied!"); e.clipboardData.setData('text/plain', JSON.stringify({ test: "value" })); e.preventDefault(); }); input.addEventListener("paste", (e) => { console.log("pasted!"); //console.log(e); if (e.clipboardData.types[0] == "text/plain") { const txt = e.clipboardData.getData('text') try { const json = JSON.parse(txt); // TODO: validate that object has correct keys console.log(json); e.preventDefault(); // I added prevent default, since you probably want to have your own logic for rendering the clipboard content } catch (e) { console.error("text is not JSON parseable: " + txt); } } });
 <input id="testInput"></input>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM