Browse Source

优化 code 复制功能细节

pull/1356/head
贤心 1 year ago committed by GitHub
parent
commit
c1c1ecbc0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 38
      src/modules/code.js

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

Loading…
Cancel
Save