From 67e8670c73a944c4c9dab8bb101704878c6ff4cc Mon Sep 17 00:00:00 2001 From: Leilei332 Date: Tue, 28 Jan 2025 00:45:26 +0800 Subject: [PATCH] Extend copy to clipborad function to support customized mime types (#8912) * Extend copy to clipborad function to support customized mime types * Remove function default parameter syntax * Add plainText option * Use plainText name * Set "text/plain" data only when it exists * Docs update * Docs update --- core/modules/startup/rootwidget.js | 5 +++-- core/modules/utils/dom/dom.js | 15 +++++++++++---- core/wiki/macros/copy-to-clipboard.tid | 18 ++++++++++-------- .../macros/copy-to-clipboard Macro.tid | 6 +++++- .../copy-to-clipboard Macro (Examples).tid | 10 ++++++++-- .../WidgetMessage_ tm-copy-to-clipboard.tid | 4 +++- 6 files changed, 40 insertions(+), 18 deletions(-) diff --git a/core/modules/startup/rootwidget.js b/core/modules/startup/rootwidget.js index d96d569c3..8496b48df 100644 --- a/core/modules/startup/rootwidget.js +++ b/core/modules/startup/rootwidget.js @@ -77,8 +77,9 @@ exports.startup = function() { $tw.rootWidget.addEventListener("tm-copy-to-clipboard",function(event) { $tw.utils.copyToClipboard(event.param,{ successNotification: event.paramObject && event.paramObject.successNotification, - failureNotification: event.paramObject && event.paramObject.failureNotification - }); + failureNotification: event.paramObject && event.paramObject.failureNotification, + plainText: event.paramObject && event.paramObject.plainText + },event.paramObject.type); }); // Install the tm-focus-selector message $tw.rootWidget.addEventListener("tm-focus-selector",function(event) { diff --git a/core/modules/utils/dom/dom.js b/core/modules/utils/dom/dom.js index 0b71e128c..5f33bbeea 100644 --- a/core/modules/utils/dom/dom.js +++ b/core/modules/utils/dom/dom.js @@ -268,9 +268,10 @@ exports.copyStyles = function(srcDomNode,dstDomNode) { /* Copy plain text to the clipboard on browsers that support it */ -exports.copyToClipboard = function(text,options) { - options = options || {}; - text = text || ""; +exports.copyToClipboard = function(text,options,type) { + var text = text || ""; + var options = options || {}; + var type = type || "text/plain"; var textArea = document.createElement("textarea"); textArea.style.position = "fixed"; textArea.style.top = 0; @@ -283,10 +284,16 @@ exports.copyToClipboard = function(text,options) { textArea.style.outline = "none"; textArea.style.boxShadow = "none"; textArea.style.background = "transparent"; - textArea.value = text; document.body.appendChild(textArea); textArea.select(); textArea.setSelectionRange(0,text.length); + textArea.addEventListener("copy",function(event) { + event.preventDefault(); + if (options.plainText) { + event.clipboardData.setData("text/plain",options.plainText); + } + event.clipboardData.setData(type,text); + }); var succeeded = false; try { succeeded = document.execCommand("copy"); diff --git a/core/wiki/macros/copy-to-clipboard.tid b/core/wiki/macros/copy-to-clipboard.tid index d05d014e2..f299cf955 100644 --- a/core/wiki/macros/copy-to-clipboard.tid +++ b/core/wiki/macros/copy-to-clipboard.tid @@ -3,9 +3,11 @@ tags: $:/tags/Macro \whitespace trim -\procedure copy-to-clipboard(src,class:"tc-btn-invisible",style) -<$button message="tm-copy-to-clipboard" - param=<> +\procedure copy-to-clipboard(src,class:"tc-btn-invisible",style,type:"text/plain",plain) +\procedure copy-to-clipboard-actions() +<$action-sendmessage $message="tm-copy-to-clipboard" $param=<> type=<> plainText=<>/> +\end copy-to-clipboard-actions +<$button actions=<> class=<> style=<