feat(code): 支持获取复制状态,阻止默认提示 (#2419)

* feat(code): 新增 onCopy(code, copied) 函数签名

* update code
pull/2428/head^2
morning-star 2024-12-25 16:33:52 +08:00 committed by GitHub
parent 34d00e9c40
commit e1aef65c4f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 3 deletions

View File

@ -384,9 +384,13 @@ done: function(obj){
</div>
```
onCopy: function(code){
onCopy: function(code, copied){
console.log(code); // 得到当前 code 内容
console.log(copied); // 是否复制成功(2.9.21+)
return false; // 返回 false 阻止内置提示(2.9.21+)
}
```
</td>

View File

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