From e1aef65c4fb635d4134c915bcd017a4c9132fe3f Mon Sep 17 00:00:00 2001 From: morning-star <26325820+Sight-wcg@users.noreply.github.com> Date: Wed, 25 Dec 2024 16:33:52 +0800 Subject: [PATCH] =?UTF-8?q?feat(code):=20=E6=94=AF=E6=8C=81=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=A4=8D=E5=88=B6=E7=8A=B6=E6=80=81=EF=BC=8C=E9=98=BB?= =?UTF-8?q?=E6=AD=A2=E9=BB=98=E8=AE=A4=E6=8F=90=E7=A4=BA=20(#2419)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(code): 新增 onCopy(code, copied) 函数签名 * update code --- docs/code/detail/options.md | 6 +++++- src/modules/code.js | 12 ++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/docs/code/detail/options.md b/docs/code/detail/options.md index af893b07..a572fbd9 100644 --- a/docs/code/detail/options.md +++ b/docs/code/detail/options.md @@ -384,9 +384,13 @@ done: function(obj){ ``` -onCopy: function(code){ +onCopy: function(code, copied){ console.log(code); // 得到当前 code 内容 + console.log(copied); // 是否复制成功(2.9.21+) + + return false; // 返回 false 阻止内置提示(2.9.21+) } + ``` diff --git a/src/modules/code.js b/src/modules/code.js index 9c8ecd91..dc4a795a 100644 --- a/src/modules/code.js +++ b/src/modules/code.js @@ -203,19 +203,27 @@ layui.define(['lay', 'util', 'element', 'form'], function(exports){ title: ['复制代码'], event: function(obj){ var code = util.unescape(finalCode(options.code)); + var hasOnCopy = typeof options.onCopy === 'function'; // 写入剪切板 lay.clipboard.writeText({ text: code, done: function() { + if(hasOnCopy){ + var ret = options.onCopy(code, true); + if(ret === false) return; + } + layer.msg('已复制', {icon: 1}); }, error: function() { + if(hasOnCopy){ + var ret = options.onCopy(code, false); + if(ret === false) return; + } layer.msg('复制失败', {icon: 2}); } }); - - typeof options.onCopy === 'function' && options.onCopy(code); } } };