|
76 | 76 | outline: 1px solid var(--vscode-focusBorder); |
77 | 77 | outline-offset: 1px; |
78 | 78 | } |
| 79 | + .btn-clear.confirm { |
| 80 | + background-color: var(--vscode-button-destructiveBackground); |
| 81 | + color: var(--vscode-button-destructiveForeground); |
| 82 | + } |
| 83 | + .btn-clear.confirm:hover { |
| 84 | + background-color: var(--vscode-button-destructiveHoverBackground); |
| 85 | + } |
79 | 86 | </style> |
80 | 87 | </head> |
81 | 88 | <body> |
|
88 | 95 | <div class="footer"> |
89 | 96 | <span id="char-count" class="char-count" aria-live="polite">0 chars</span> |
90 | 97 | <div class="actions"> |
91 | | - <button id="btn-clear" class="btn-clear" aria-label="Clear Scratchpad"> |
| 98 | + <button id="btn-clear" class="btn-clear" aria-label="Clear Scratchpad" title="Clear all content"> |
92 | 99 | Clear |
93 | 100 | </button> |
94 | 101 | </div> |
|
98 | 105 | const textarea = document.getElementById('scratchpad'); |
99 | 106 | const charCount = document.getElementById('char-count'); |
100 | 107 | const btnClear = document.getElementById('btn-clear'); |
| 108 | + let clearTimeoutId; |
101 | 109 |
|
102 | 110 | // Debounce function |
103 | 111 | function debounce(func, delay) { |
|
113 | 121 | charCount.textContent = `${length} char${length !== 1 ? 's' : ''}`; |
114 | 122 | } |
115 | 123 |
|
| 124 | + function resetClearButton() { |
| 125 | + if (clearTimeoutId) { |
| 126 | + clearTimeout(clearTimeoutId); |
| 127 | + clearTimeoutId = null; |
| 128 | + } |
| 129 | + btnClear.textContent = 'Clear'; |
| 130 | + btnClear.classList.remove('confirm'); |
| 131 | + btnClear.setAttribute('aria-label', 'Clear Scratchpad'); |
| 132 | + btnClear.dataset.state = 'idle'; |
| 133 | + } |
| 134 | + |
116 | 135 | // 监听内容变化 |
117 | 136 | const onInput = debounce(() => { |
118 | 137 | vscode.postMessage({ |
|
127 | 146 | }); |
128 | 147 |
|
129 | 148 | btnClear.addEventListener('click', () => { |
130 | | - if (textarea.value.length > 0) { |
| 149 | + if (textarea.value.length === 0) return; |
| 150 | + |
| 151 | + if (btnClear.dataset.state !== 'confirm') { |
| 152 | + // First click: Request confirmation |
| 153 | + btnClear.textContent = 'Confirm?'; |
| 154 | + btnClear.classList.add('confirm'); |
| 155 | + btnClear.setAttribute('aria-label', 'Confirm Clear Scratchpad'); |
| 156 | + btnClear.dataset.state = 'confirm'; |
| 157 | + |
| 158 | + // Reset after 3 seconds |
| 159 | + clearTimeoutId = setTimeout(resetClearButton, 3000); |
| 160 | + } else { |
| 161 | + // Second click: Perform action |
131 | 162 | textarea.value = ''; |
132 | 163 | updateCharCount(); |
133 | 164 | onInput(); |
134 | 165 | textarea.focus(); |
| 166 | + resetClearButton(); |
135 | 167 | } |
136 | 168 | }); |
137 | 169 |
|
| 170 | + textarea.addEventListener('focus', resetClearButton); |
| 171 | + |
138 | 172 | // 接收来自扩展的消息 |
139 | 173 | window.addEventListener('message', (event) => { |
140 | 174 | const message = event.data; |
|
0 commit comments