From 6973441b737de113f4ffad6845dabe3280bbdc4f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Wed, 28 Nov 2018 14:13:09 +0100 Subject: [PATCH] Add WoltLabSuite/Core/Clipboard see #2752 --- .../files/js/WoltLabSuite/Core/Clipboard.js | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 wcfsetup/install/files/js/WoltLabSuite/Core/Clipboard.js diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Clipboard.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Clipboard.js new file mode 100644 index 0000000000..535e50b0f0 --- /dev/null +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Clipboard.js @@ -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 + * @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"); + } + }) + } + }; +}); -- 2.20.1