优化 code 复制功能细节

pull/1356/head
贤心 2023-08-29 10:32:53 +08:00 committed by GitHub
parent ae8dede13d
commit c1c1ecbc0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 19 deletions

View File

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