chore: Use clipboard API directly (#14227)

* chore: Use clipboard API directly

* fix: Fix lint
This commit is contained in:
Chocolate Pie
2024-07-17 21:52:05 +09:00
committed by GitHub
parent 8b4933cc48
commit 68bcd91d57
22 changed files with 23 additions and 50 deletions

View File

@@ -6,33 +6,6 @@
/**
* Clipboardに値をコピー(TODO: 文字列以外も対応)
*/
export default val => {
// 空div 生成
const tmp = document.createElement('div');
// 選択用のタグ生成
const pre = document.createElement('pre');
// 親要素のCSSで user-select: none だとコピーできないので書き換える
pre.style.webkitUserSelect = 'auto';
pre.style.userSelect = 'auto';
tmp.appendChild(pre).textContent = val;
// 要素を画面外へ
const s = tmp.style;
s.position = 'fixed';
s.right = '200%';
// body に追加
document.body.appendChild(tmp);
// 要素を選択
document.getSelection().selectAllChildren(tmp);
// クリップボードにコピー
const result = document.execCommand('copy');
// 要素削除
document.body.removeChild(tmp);
return result;
export function copyToClipboard(input: string | null) {
if (input) navigator.clipboard.writeText(input);
};