新增 `lay.clipboard.writeText()` 方法,用于写入剪切板

pull/1363/head
贤心 2023-09-01 23:44:10 +08:00
parent 352cbe2613
commit 4517ec7864
1 changed files with 31 additions and 0 deletions

View File

@ -309,6 +309,37 @@
return matched;
};
// 剪切板
lay.clipboard = {
// 写入文本
writeText: function(options) {
var text = String(options.text);
try {
navigator.clipboard.writeText(text).then(
options.done
).catch(options.error);
} catch(e) {
var elem = document.createElement('textarea');
elem.value = text;
elem.style.position = 'absolute';
elem.style.opacity = '0';
document.body.appendChild(elem);
elem.select();
try {
document.execCommand('copy');
typeof options.done === 'function' && options.done();
} catch(err) {
typeof options.error === 'function' && options.error(err);
}
elem.remove();
}
}
};
/*
* lay 元素操作