Add WoltLabSuite/Core/Clipboard
authorTim Düsterhus <duesterhus@woltlab.com>
Wed, 28 Nov 2018 13:13:09 +0000 (14:13 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Thu, 29 Nov 2018 14:42:46 +0000 (15:42 +0100)
see #2752

wcfsetup/install/files/js/WoltLabSuite/Core/Clipboard.js [new file with mode: 0644]

diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Clipboard.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Clipboard.js
new file mode 100644 (file)
index 0000000..535e50b
--- /dev/null
@@ -0,0 +1,34 @@
+/**
+ * Wrapper around the web browser's various clipboard APIs.
+ * 
+ * @author     Tim Duesterhus
+ * @copyright  2001-2018 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module     WoltLabSuite/Core/Clipboard
+ */
+define([], function() {
+       "use strict";
+       
+       return {
+               copyElementTextToClipboard: function (element) {
+                       var promise;
+                       if (navigator.clipboard) {
+                               promise = navigator.clipboard.writeText(element.innerText);
+                       }
+                       else {
+                               promise = Promise.reject('navigator.clipboard is not supported');
+                       }
+                       
+                       return promise.catch(function () {
+                               if (!window.getSelection) throw new Error('window.getSelection is not supported');
+                               
+                               var range = document.createRange();
+                               range.selectNode(element);
+                               window.getSelection().addRange(range);
+                               if (!document.execCommand('copy')) {
+                                       throw new Error("execCommand('copy') failed");
+                               }
+                       })
+               }
+       };
+});