|
31 | 31 | padding: 0 16px; |
32 | 32 | color: white; |
33 | 33 | font-weight: 600; |
34 | | - cursor: move; |
35 | 34 | } |
36 | 35 |
|
37 | 36 | #title { |
38 | 37 | font-size: 14px; |
| 38 | + flex: 1; |
| 39 | + cursor: move; |
39 | 40 | } |
40 | 41 |
|
41 | 42 | #close-btn { |
|
132 | 133 | </style> |
133 | 134 | </head> |
134 | 135 | <body> |
135 | | - <div id="drag-region" data-tauri-drag-region> |
136 | | - <span id="title">快速笔记</span> |
137 | | - <button id="close-btn">×</button> |
| 136 | + <div id="drag-region"> |
| 137 | + <span id="title" data-tauri-drag-region>快速笔记</span> |
| 138 | + <!-- <button id="close-btn">×</button> --> |
138 | 139 | </div> |
139 | 140 |
|
140 | 141 | <div id="content"> |
|
147 | 148 | </div> |
148 | 149 |
|
149 | 150 | <script> |
150 | | - const textarea = document.getElementById('note-textarea'); |
151 | | - const charCount = document.getElementById('char-count'); |
152 | | - const clearBtn = document.getElementById('clear-btn'); |
153 | | - const closeBtn = document.getElementById('close-btn'); |
154 | | - |
155 | | - // 从 localStorage 加载内容 |
156 | | - function loadNote() { |
157 | | - const savedNote = localStorage.getItem('quickNote'); |
158 | | - if (savedNote) { |
159 | | - textarea.value = savedNote; |
160 | | - updateCharCount(); |
| 151 | + // 页面加载时初始化 |
| 152 | + window.addEventListener('DOMContentLoaded', () => { |
| 153 | + const textarea = document.getElementById('note-textarea'); |
| 154 | + const charCount = document.getElementById('char-count'); |
| 155 | + const closeBtn = document.getElementById('close-btn'); |
| 156 | + |
| 157 | + // 获取 Tauri invoke 函数 |
| 158 | + const invoke = window.__TAURI_INTERNALS__.invoke; |
| 159 | + |
| 160 | + console.log('Panel loaded, Tauri API:', !!invoke); |
| 161 | + |
| 162 | + // 从 localStorage 加载内容 |
| 163 | + function loadNote() { |
| 164 | + const savedNote = localStorage.getItem('quickNote'); |
| 165 | + if (savedNote) { |
| 166 | + textarea.value = savedNote; |
| 167 | + updateCharCount(); |
| 168 | + } |
161 | 169 | } |
162 | | - } |
163 | | - |
164 | | - // 保存到 localStorage |
165 | | - function saveNote() { |
166 | | - localStorage.setItem('quickNote', textarea.value); |
167 | | - } |
168 | | - |
169 | | - // 更新字符计数 |
170 | | - function updateCharCount() { |
171 | | - const count = textarea.value.length; |
172 | | - charCount.textContent = `${count} 字符`; |
173 | | - } |
174 | | - |
175 | | - // 监听输入事件 |
176 | | - textarea.addEventListener('input', () => { |
177 | | - saveNote(); |
178 | | - updateCharCount(); |
179 | | - }); |
180 | | - |
181 | | - // 关闭按钮 |
182 | | - closeBtn.addEventListener('click', async () => { |
183 | | - console.log('Close button clicked'); |
184 | | - try { |
185 | | - const invoke = window.__TAURI_INTERNALS__.invoke; |
186 | | - // 调用 toggle_panel 来隐藏窗口 |
187 | | - await invoke('toggle_panel'); |
188 | | - console.log('Panel hidden successfully'); |
189 | | - } catch (error) { |
190 | | - console.error('Failed to hide window:', error); |
| 170 | + |
| 171 | + // 保存到 localStorage |
| 172 | + function saveNote() { |
| 173 | + localStorage.setItem('quickNote', textarea.value); |
191 | 174 | } |
192 | | - }); |
193 | | - |
194 | | - // 页面加载时恢复内容 |
195 | | - window.addEventListener('DOMContentLoaded', () => { |
196 | | - loadNote(); |
197 | | - }); |
198 | | - |
199 | | - // 当窗口显示时,自动聚焦到 textarea |
200 | | - window.addEventListener('focus', () => { |
201 | | - textarea.focus(); |
202 | | - }); |
203 | | - |
204 | | - // 监听窗口失去焦点事件 - 点击外部时自动关闭 |
205 | | - window.addEventListener('blur', () => { |
206 | | - console.log('Window lost focus, hiding panel'); |
207 | | - // 延迟一点关闭,避免与点击事件冲突 |
208 | | - setTimeout(async () => { |
| 175 | + |
| 176 | + // 更新字符计数 |
| 177 | + function updateCharCount() { |
| 178 | + const count = textarea.value.length; |
| 179 | + charCount.textContent = `${count} 字符`; |
| 180 | + } |
| 181 | + |
| 182 | + // 监听输入事件 |
| 183 | + textarea.addEventListener('input', () => { |
| 184 | + saveNote(); |
| 185 | + updateCharCount(); |
| 186 | + }); |
| 187 | + |
| 188 | + // 关闭按钮 - 使用 mousedown 事件更可靠 |
| 189 | + closeBtn.addEventListener('mousedown', async (e) => { |
| 190 | + e.preventDefault(); |
| 191 | + e.stopPropagation(); |
| 192 | + console.log('Close button clicked (mousedown)'); |
209 | 193 | try { |
210 | | - const invoke = window.__TAURI_INTERNALS__.invoke; |
211 | 194 | await invoke('toggle_panel'); |
| 195 | + console.log('Panel hidden successfully'); |
212 | 196 | } catch (error) { |
213 | | - console.error('Failed to hide panel:', error); |
| 197 | + console.error('Failed to hide window:', error); |
214 | 198 | } |
215 | | - }, 100); |
| 199 | + }); |
| 200 | + |
| 201 | + // 也监听 click 事件作为备用 |
| 202 | + closeBtn.addEventListener('click', async (e) => { |
| 203 | + e.preventDefault(); |
| 204 | + e.stopPropagation(); |
| 205 | + console.log('Close button clicked (click)'); |
| 206 | + }); |
| 207 | + |
| 208 | + // 当窗口显示时,自动聚焦到 textarea |
| 209 | + window.addEventListener('focus', () => { |
| 210 | + textarea.focus(); |
| 211 | + }); |
| 212 | + |
| 213 | + // 监听窗口失去焦点事件 - 点击外部时自动关闭 |
| 214 | + window.addEventListener('blur', () => { |
| 215 | + console.log('Window lost focus, hiding panel'); |
| 216 | + // 延迟一点关闭,避免与点击事件冲突 |
| 217 | + setTimeout(async () => { |
| 218 | + try { |
| 219 | + await invoke('toggle_panel'); |
| 220 | + } catch (error) { |
| 221 | + console.error('Failed to hide panel:', error); |
| 222 | + } |
| 223 | + }, 100); |
| 224 | + }); |
| 225 | + |
| 226 | + // 加载笔记内容 |
| 227 | + loadNote(); |
216 | 228 | }); |
217 | 229 | </script> |
218 | 230 | </body> |
|
0 commit comments