Created
January 12, 2022 12:59
-
-
Save Koboo/5f1d101edff886a02cb475e4d0fc347b to your computer and use it in GitHub Desktop.
Viewless Clipboard utility for vaadin flow web-apps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.vaadin.flow.component.UI; | |
import com.vaadin.flow.component.notification.Notification; | |
import com.vaadin.flow.component.notification.NotificationVariant; | |
public class Clipboard { | |
public Clipboard() { | |
String javaScriptFunction = "window.copyToClipboard = (str) => {\n" | |
+ " const textarea = document.createElement(\"textarea\");\n" | |
+ " textarea.value = str;\n" | |
+ " textarea.style.position = \"absolute\";\n" | |
+ " textarea.style.opacity = \"0\";\n" | |
+ " document.body.appendChild(textarea);\n" | |
+ " textarea.select();\n" | |
+ " document.execCommand(\"copy\");\n" | |
+ " document.body.removeChild(textarea);\n" | |
+ "};"; | |
UI.getCurrent().getPage().executeJs(javaScriptFunction); | |
} | |
public void copyTo(String value) { | |
UI.getCurrent().getPage().executeJs("window.copyToClipboard($0)", value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment