From d2ff164c07788818ded9826c9e3811599272e69c Mon Sep 17 00:00:00 2001 From: Jermolene Date: Fri, 15 Dec 2017 15:08:18 +0000 Subject: [PATCH] Add support for tm-copy-to-clipboard message --- core/language/en-GB/Notifications.multids | 2 ++ core/modules/startup/rootwidget.js | 4 +++ core/modules/utils/dom/dom.js | 30 +++++++++++++++++++ .../WidgetMessage_ tm-copy-to-clipboard.tid | 25 ++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-copy-to-clipboard.tid diff --git a/core/language/en-GB/Notifications.multids b/core/language/en-GB/Notifications.multids index 0d9b891bd..df095b828 100644 --- a/core/language/en-GB/Notifications.multids +++ b/core/language/en-GB/Notifications.multids @@ -2,3 +2,5 @@ title: $:/language/Notifications/ Save/Done: Saved wiki Save/Starting: Starting to save wiki +CopiedToClipboard/Succeeded: Copied! +CopiedToClipboard/Failed: Failed to copy to clipboard! diff --git a/core/modules/startup/rootwidget.js b/core/modules/startup/rootwidget.js index 2b5111aff..5afd2d536 100644 --- a/core/modules/startup/rootwidget.js +++ b/core/modules/startup/rootwidget.js @@ -30,6 +30,10 @@ exports.startup = function() { $tw.rootWidget.addEventListener("tm-notify",function(event) { $tw.notifier.display(event.param,{variables: event.paramObject}); }); + // Install the copy-to-clipboard mechanism + $tw.rootWidget.addEventListener("tm-copy-to-clipboard",function(event) { + $tw.utils.copyToClipboard(event.param); + }); // Install the scroller $tw.pageScroller = new $tw.utils.PageScroller(); $tw.rootWidget.addEventListener("tm-scroll",function(event) { diff --git a/core/modules/utils/dom/dom.js b/core/modules/utils/dom/dom.js index 118498bc3..739344c98 100644 --- a/core/modules/utils/dom/dom.js +++ b/core/modules/utils/dom/dom.js @@ -231,4 +231,34 @@ exports.copyStyles = function(srcDomNode,dstDomNode) { $tw.utils.setStyles(dstDomNode,$tw.utils.getComputedStyles(srcDomNode)); }; +/* +Copy plain text to the clipboard on browsers that support it +*/ +exports.copyToClipboard = function(text,options) { + options = options || {}; + var textArea = document.createElement("textarea"); + textArea.style.position = "fixed"; + textArea.style.top = 0; + textArea.style.left = 0; + textArea.style.width = "2em"; + textArea.style.height = "2em"; + textArea.style.padding = 0; + textArea.style.border = "none"; + textArea.style.outline = "none"; + textArea.style.boxShadow = "none"; + textArea.style.background = "transparent"; + textArea.value = text; + document.body.appendChild(textArea); + textArea.select(); + var succeeded = false; + try { + succeeded = document.execCommand("copy"); + } catch (err) { + } + if(!options.doNotNotify) { + $tw.notifier.display(succeeded ? "$:/language/Notifications/CopiedToClipboard/Succeeded" : "$:/language/Notifications/CopiedToClipboard/Failed"); + } + document.body.removeChild(textArea); +}; + })(); diff --git a/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-copy-to-clipboard.tid b/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-copy-to-clipboard.tid new file mode 100644 index 000000000..70cf2a24a --- /dev/null +++ b/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-copy-to-clipboard.tid @@ -0,0 +1,25 @@ +caption: tm-copy-to-clipboard +created: 20171215150056004 +modified: 20171215150600888 +tags: Messages +title: WidgetMessage: tm-copy-to-clipboard +type: text/vnd.tiddlywiki + +The "copy to clipboard" message attempts to copy the specified text to the clipboard. If it succeeds, the tiddler [[$:/language/Notifications/CopiedToClipboard/Succeeded]] is displayed as a notification. If the browser doesn't permit the operation, the tiddler [[$:/language/Notifications/CopiedToClipboard/Failed]] is displayed instead. + +It requires the following properties on the `event` object: + +|!Name |!Description | +|param |Text to be copied to the clipboard | + +This message is usually generated with the ButtonWidget. It is handled by the TiddlyWiki core. + +! Example + +This example copies the current time to the clipboard: + +<$macrocall $name='wikitext-example-without-html' +src='<$button message="tm-copy-to-clipboard" param=<>> +Copy date to clipboard +'/> +