1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-17 02:49:54 +00:00

Add support for tm-copy-to-clipboard message

This commit is contained in:
Jermolene 2017-12-15 15:08:18 +00:00
parent e344c38349
commit d2ff164c07
4 changed files with 61 additions and 0 deletions

View File

@ -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!

View File

@ -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) {

View File

@ -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);
};
})();

View File

@ -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=<<now>>>
Copy date to clipboard
</$button>'/>