|
11 | 11 | authKeyIssuedValue: '', |
12 | 12 | authKeyDeactivatingID: '', |
13 | 13 | authKeyCopied: false, |
| 14 | + authKeyCopyError: false, |
| 15 | + authKeyCopyResetTimer: null, |
14 | 16 | authKeyForm: { |
15 | 17 | name: '', |
16 | 18 | description: '', |
|
52 | 54 | this.authKeyError = ''; |
53 | 55 | this.authKeyNotice = ''; |
54 | 56 | this.authKeyIssuedValue = ''; |
| 57 | + this.resetAuthKeyCopyFeedback(); |
55 | 58 | this.authKeyForm = this.defaultAuthKeyForm(); |
56 | 59 | }, |
57 | 60 |
|
58 | 61 | closeAuthKeyForm() { |
59 | 62 | this.authKeyFormOpen = false; |
60 | 63 | this.authKeyError = ''; |
61 | 64 | this.authKeyIssuedValue = ''; |
62 | | - this.authKeyCopied = false; |
| 65 | + this.resetAuthKeyCopyFeedback(); |
63 | 66 | this.authKeyForm = this.defaultAuthKeyForm(); |
64 | 67 | }, |
65 | 68 |
|
| 69 | + clearAuthKeyCopyResetTimer() { |
| 70 | + if (this.authKeyCopyResetTimer !== null) { |
| 71 | + clearTimeout(this.authKeyCopyResetTimer); |
| 72 | + this.authKeyCopyResetTimer = null; |
| 73 | + } |
| 74 | + }, |
| 75 | + |
| 76 | + scheduleAuthKeyCopyFeedbackReset() { |
| 77 | + this.clearAuthKeyCopyResetTimer(); |
| 78 | + this.authKeyCopyResetTimer = setTimeout(() => { |
| 79 | + this.authKeyCopied = false; |
| 80 | + this.authKeyCopyError = false; |
| 81 | + this.authKeyCopyResetTimer = null; |
| 82 | + }, 2000); |
| 83 | + }, |
| 84 | + |
| 85 | + resetAuthKeyCopyFeedback() { |
| 86 | + this.clearAuthKeyCopyResetTimer(); |
| 87 | + this.authKeyCopied = false; |
| 88 | + this.authKeyCopyError = false; |
| 89 | + }, |
| 90 | + |
| 91 | + setAuthKeyCopyFeedback(copied, hasError) { |
| 92 | + this.authKeyCopied = copied; |
| 93 | + this.authKeyCopyError = hasError; |
| 94 | + this.scheduleAuthKeyCopyFeedbackReset(); |
| 95 | + }, |
| 96 | + |
66 | 97 | copyAuthKeyValue() { |
67 | | - navigator.clipboard.writeText(this.authKeyIssuedValue).then(() => { |
68 | | - this.authKeyCopied = true; |
69 | | - setTimeout(() => { this.authKeyCopied = false; }, 2000); |
70 | | - }); |
| 98 | + const value = String(this.authKeyIssuedValue || ''); |
| 99 | + const clipboard = global.navigator && global.navigator.clipboard; |
| 100 | + |
| 101 | + this.resetAuthKeyCopyFeedback(); |
| 102 | + |
| 103 | + if (clipboard && typeof clipboard.writeText === 'function') { |
| 104 | + return clipboard.writeText(value).then(() => { |
| 105 | + this.setAuthKeyCopyFeedback(true, false); |
| 106 | + }).catch((error) => { |
| 107 | + console.error('Failed to copy auth key:', error); |
| 108 | + this.setAuthKeyCopyFeedback(false, true); |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + const doc = global.document; |
| 113 | + if (!doc || !doc.body || typeof doc.createElement !== 'function' || typeof doc.execCommand !== 'function') { |
| 114 | + this.setAuthKeyCopyFeedback(false, true); |
| 115 | + return Promise.resolve(); |
| 116 | + } |
| 117 | + |
| 118 | + const textarea = doc.createElement('textarea'); |
| 119 | + textarea.value = value; |
| 120 | + textarea.setAttribute('readonly', ''); |
| 121 | + textarea.style.position = 'fixed'; |
| 122 | + textarea.style.top = '0'; |
| 123 | + textarea.style.left = '0'; |
| 124 | + textarea.style.opacity = '0'; |
| 125 | + |
| 126 | + try { |
| 127 | + doc.body.appendChild(textarea); |
| 128 | + if (typeof textarea.focus === 'function') { |
| 129 | + textarea.focus(); |
| 130 | + } |
| 131 | + if (typeof textarea.select === 'function') { |
| 132 | + textarea.select(); |
| 133 | + } |
| 134 | + if (typeof textarea.setSelectionRange === 'function') { |
| 135 | + textarea.setSelectionRange(0, textarea.value.length); |
| 136 | + } |
| 137 | + const copied = !!doc.execCommand('copy'); |
| 138 | + this.setAuthKeyCopyFeedback(copied, !copied); |
| 139 | + } catch (error) { |
| 140 | + console.error('Failed to copy auth key:', error); |
| 141 | + this.setAuthKeyCopyFeedback(false, true); |
| 142 | + } finally { |
| 143 | + if (textarea.parentNode) { |
| 144 | + textarea.parentNode.removeChild(textarea); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + return Promise.resolve(); |
71 | 149 | }, |
72 | 150 |
|
73 | 151 | dismissIssuedKey() { |
74 | 152 | this.authKeyIssuedValue = ''; |
75 | | - this.authKeyCopied = false; |
| 153 | + this.resetAuthKeyCopyFeedback(); |
76 | 154 | this.authKeyForm = this.defaultAuthKeyForm(); |
77 | 155 | }, |
78 | 156 |
|
|
0 commit comments