优化 code 复制代码,统一采用 `lay.clipboard.writeText()`

pull/1363/head
贤心 2023-09-01 23:45:00 +08:00
parent 4517ec7864
commit 5b56453489
1 changed files with 11 additions and 18 deletions

View File

@ -104,27 +104,20 @@ layui.define(['lay', 'util', 'element', 'form'], function(exports){
className: 'file-b', className: 'file-b',
title: ['复制代码'], title: ['复制代码'],
event: function(el, type){ event: function(el, type){
var text = util.unescape(finalCode); var code = util.unescape(finalCode);
try {
navigator.clipboard.writeText(text).then(function(){ // 写入剪切板
lay.clipboard.writeText({
text: code,
done: function() {
layer.msg('已复制', {icon: 1}); layer.msg('已复制', {icon: 1});
}); },
} catch(e) { error: function() {
var textarea = document.createElement('textarea');
textarea.value = text;
textarea.style.position = 'absolute';
textarea.style.opacity = '0';
document.body.appendChild(textarea);
textarea.select();
try {
document.execCommand('copy');
layer.msg('已复制', {icon: 1});
} catch(err) {
layer.msg('复制失败', {icon: 2}); layer.msg('复制失败', {icon: 2});
} }
textarea.remove(); });
}
typeof options.onCopy === 'function' && options.onCopy(text); typeof options.onCopy === 'function' && options.onCopy(code);
} }
} }
}; };