2022-11-23 15:58:12 +00:00
|
|
|
diff --git a/packages/client/src/scripts/copy-to-clipboard.ts b/packages/client/src/scripts/copy-to-clipboard.ts
|
2022-11-29 09:07:25 +00:00
|
|
|
index ab13cab..6dc5b74 100644
|
2022-11-23 15:58:12 +00:00
|
|
|
--- a/packages/client/src/scripts/copy-to-clipboard.ts
|
|
|
|
+++ b/packages/client/src/scripts/copy-to-clipboard.ts
|
2022-11-29 09:07:25 +00:00
|
|
|
@@ -2,32 +2,27 @@
|
|
|
|
* Clipboardに値をコピー(TODO: 文字列以外も対応)
|
|
|
|
*/
|
|
|
|
export default val => {
|
|
|
|
- // 空div 生成
|
|
|
|
- const tmp = document.createElement('div');
|
|
|
|
- // 選択用のタグ生成
|
2022-11-23 15:58:12 +00:00
|
|
|
- const pre = document.createElement('pre');
|
2022-11-29 09:07:25 +00:00
|
|
|
+ if (navigator.clipboard) {
|
|
|
|
+ navigator.clipboard.writeText(String(val)).then(() => {
|
|
|
|
+ console.log('clipboard: success');
|
|
|
|
+ }, (err) => {
|
|
|
|
+ console.error('clipboard_failure: ', err);
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ var textArea = document.createElement("input");
|
|
|
|
+ textArea.value = val;
|
|
|
|
+ textArea.style.top = "0";
|
|
|
|
+ textArea.style.left = "0";
|
|
|
|
+ textArea.style.position = "fixed";
|
2022-11-23 15:58:12 +00:00
|
|
|
|
2022-11-29 09:07:25 +00:00
|
|
|
- // 親要素の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;
|
|
|
|
+ document.body.appendChild(textArea);
|
|
|
|
+ textArea.focus();
|
|
|
|
+ textArea.select();
|
|
|
|
+ try {
|
|
|
|
+ const success = document.execCommand('copy') ? "success" : "failure";
|
|
|
|
+ console.log('clipboard: ', success);
|
|
|
|
+ } catch (err) {
|
|
|
|
+ console.error('clipboard_failure: ', err);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
};
|