From ae8dede13d11ef71dc7500e0e338f8c48b2b223c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9C=B0=E4=BA=9A=E5=B0=94-IT?= <136627746+Diyar-IT@users.noreply.github.com> Date: Mon, 28 Aug 2023 23:47:52 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=9D=9E=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E5=9F=9F=E5=A4=8D=E5=88=B6=E5=A4=B1=E8=B4=A5=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/code.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/modules/code.js b/src/modules/code.js index ded85848..c1e01996 100644 --- a/src/modules/code.js +++ b/src/modules/code.js @@ -112,9 +112,17 @@ layui.define(['lay', 'util', 'element', 'form'], function(exports){ }); }); } catch(e) { - layer.msg('复制失败', { - icon: 2 - }); + var text = document.createElement("textarea"); + text.setAttribute("readonly", "readonly"); + text.value = util.unescape(finalCode); + document.body.appendChild(text); + text.select(); + if (document.execCommand("copy")){ + layer.msg('已复制', {icon: 1}); + }else { + layer.msg('复制失败', {icon: 2}); + } + document.body.removeChild(text); } }(); } @@ -369,4 +377,4 @@ layui.define(['lay', 'util', 'element', 'form'], function(exports){ // 若为源码版,则自动加载该组件依赖的 css 文件 if(!layui['layui.all']){ layui.addcss('modules/code.css?v=3', 'skincodecss'); -} \ No newline at end of file +} From c1c1ecbc0e2227f46a1cb59178b5e6c3433e84e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B4=A4=E5=BF=83?= <3277200+sentsim@users.noreply.github.com> Date: Tue, 29 Aug 2023 10:32:53 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=98=E5=8C=96=20code=20=E5=A4=8D?= =?UTF-8?q?=E5=88=B6=E5=8A=9F=E8=83=BD=E7=BB=86=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/code.js | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/modules/code.js b/src/modules/code.js index c1e01996..98adba03 100644 --- a/src/modules/code.js +++ b/src/modules/code.js @@ -104,27 +104,27 @@ layui.define(['lay', 'util', 'element', 'form'], function(exports){ className: 'file-b', title: ['复制代码'], event: function(el, type){ - typeof options.onCopy === 'function' ? options.onCopy(finalCode) : function(){ + var text = util.unescape(finalCode); + try { + navigator.clipboard.writeText(text).then(function(){ + layer.msg('已复制', {icon: 1}); + }); + } catch(e) { + var textarea = document.createElement('textarea'); + textarea.value = text; + textarea.style.position = 'absolute'; + textarea.style.opacity = '0'; + document.body.appendChild(textarea); + textarea.select(); try { - navigator.clipboard.writeText(util.unescape(finalCode)).then(function(){ - layer.msg('已复制', { - icon: 1 - }); - }); - } catch(e) { - var text = document.createElement("textarea"); - text.setAttribute("readonly", "readonly"); - text.value = util.unescape(finalCode); - document.body.appendChild(text); - text.select(); - if (document.execCommand("copy")){ - layer.msg('已复制', {icon: 1}); - }else { - layer.msg('复制失败', {icon: 2}); - } - document.body.removeChild(text); + document.execCommand('copy'); + layer.msg('已复制', {icon: 1}); + } catch(err) { + layer.msg('复制失败', {icon: 2}); } - }(); + textarea.remove(); + } + typeof options.onCopy === 'function' && options.onCopy(text); } } };