@@ -2,15 +2,14 @@ import './styles/style.css';
2
2
import { createSvgIcon , createIcon } from "./components/IconManager" ;
3
3
import { createPopup } from "./popup" ;
4
4
import "perfect-scrollbar/css/perfect-scrollbar.css" ;
5
+ import { popupStateManager } from './utils/popupStateManager' ;
5
6
6
7
let currentIcon = null ;
7
- let isCreatingPopup = false ;
8
8
let isHandlingIconClick = false ;
9
9
let isSelectionEnabled = true ; // 默认启用
10
10
let selectedText = "" ;
11
11
let currentPopup = null ; // 新增:跟踪当前弹窗
12
12
let isRememberWindowSize = false ; // 默认不记住窗口大小
13
- let isPopupVisible = false ;
14
13
15
14
const link = document . createElement ( "link" ) ;
16
15
link . rel = "stylesheet" ;
@@ -52,10 +51,10 @@ function removeIcon() {
52
51
53
52
// 更新安全的弹窗移除函数
54
53
function safeRemovePopup ( ) {
54
+ // 立即重置所有状态
55
+ popupStateManager . reset ( ) ;
56
+
55
57
if ( ! currentPopup ) {
56
- // 即使没有当前弹窗,也要确保状态被重置
57
- isPopupVisible = false ;
58
- isCreatingPopup = false ;
59
58
window . aiResponseContainer = null ;
60
59
return ;
61
60
}
@@ -71,12 +70,15 @@ function safeRemovePopup() {
71
70
// 清理所有观察者和事件监听器
72
71
if ( currentPopup . _resizeObserver ) {
73
72
currentPopup . _resizeObserver . disconnect ( ) ;
73
+ delete currentPopup . _resizeObserver ;
74
74
}
75
75
if ( currentPopup . _mutationObserver ) {
76
76
currentPopup . _mutationObserver . disconnect ( ) ;
77
+ delete currentPopup . _mutationObserver ;
77
78
}
78
79
if ( currentPopup . _removeThemeListener ) {
79
80
currentPopup . _removeThemeListener ( ) ;
81
+ delete currentPopup . _removeThemeListener ;
80
82
}
81
83
82
84
// 清理滚动相关实例
@@ -86,9 +88,11 @@ function safeRemovePopup() {
86
88
}
87
89
if ( window . aiResponseContainer ?. scrollStateManager ?. cleanup ) {
88
90
window . aiResponseContainer . scrollStateManager . cleanup ( ) ;
91
+ delete window . aiResponseContainer . scrollStateManager ;
89
92
}
90
93
if ( window . aiResponseContainer ?. cleanup ) {
91
94
window . aiResponseContainer . cleanup ( ) ;
95
+ delete window . aiResponseContainer . cleanup ;
92
96
}
93
97
94
98
// 使用 try-catch 包装 DOM 操作
@@ -103,8 +107,6 @@ function safeRemovePopup() {
103
107
// 确保状态被重置
104
108
window . aiResponseContainer = null ;
105
109
currentPopup = null ;
106
- isPopupVisible = false ;
107
- isCreatingPopup = false ;
108
110
} catch ( error ) {
109
111
console . warn ( 'Failed to remove popup:' , error ) ;
110
112
// 确保在出错时也能重置所有状态
@@ -115,21 +117,28 @@ function safeRemovePopup() {
115
117
console . warn ( 'Error removing popup in catch block:' , e ) ;
116
118
}
117
119
}
120
+ // 重置所有状态
118
121
window . aiResponseContainer = null ;
119
122
currentPopup = null ;
120
- isPopupVisible = false ;
121
- isCreatingPopup = false ;
122
123
}
124
+
125
+ // 最后再次确保所有状态都被重置
126
+ popupStateManager . reset ( ) ;
123
127
}
124
128
125
129
function handlePopupCreation ( selectedText , rect , hideQuestion = false ) {
126
- if ( isCreatingPopup ) return ;
130
+ if ( popupStateManager . isCreating ( ) ) return ;
127
131
128
- isCreatingPopup = true ;
132
+ popupStateManager . setCreating ( true ) ;
129
133
130
134
try {
135
+ // 先移除快捷按钮
136
+ removeIcon ( ) ;
137
+ // 清除选中的文本
138
+ window . getSelection ( ) . removeAllRanges ( ) ;
139
+
131
140
safeRemovePopup ( ) ;
132
- currentPopup = createPopup ( selectedText , rect , hideQuestion ) ;
141
+ currentPopup = createPopup ( selectedText , rect , hideQuestion , safeRemovePopup ) ;
133
142
currentPopup . style . minWidth = '300px' ;
134
143
currentPopup . style . minHeight = '200px' ;
135
144
@@ -148,7 +157,7 @@ function handlePopupCreation(selectedText, rect, hideQuestion = false) {
148
157
}
149
158
150
159
document . body . appendChild ( currentPopup ) ;
151
- isPopupVisible = true ; // 更新状态
160
+ popupStateManager . setVisible ( true ) ; // 更新状态
152
161
153
162
// 设置窗口大小监听
154
163
if ( isRememberWindowSize && currentPopup ) {
@@ -159,25 +168,35 @@ function handlePopupCreation(selectedText, rect, hideQuestion = false) {
159
168
safeRemovePopup ( ) ;
160
169
} finally {
161
170
setTimeout ( ( ) => {
162
- isCreatingPopup = false ;
171
+ popupStateManager . setCreating ( false ) ;
163
172
} , 100 ) ;
164
173
}
165
174
}
166
175
167
- // 添加切换窗口显示状态的函数
168
176
function togglePopup ( selectedText , rect , hideQuestion = false ) {
177
+ // 如果正在处理中,直接返回
178
+ if ( popupStateManager . isCreating ( ) ) return ;
179
+
169
180
try {
170
- if ( isPopupVisible ) {
181
+ if ( popupStateManager . isVisible ( ) ) {
171
182
safeRemovePopup ( ) ;
183
+ // 添加一个短暂的延迟,确保状态完全重置
184
+ setTimeout ( ( ) => {
185
+ popupStateManager . reset ( ) ;
186
+ } , 100 ) ;
172
187
} else {
173
188
// 在创建新弹窗前确保清理旧的状态
174
189
safeRemovePopup ( ) ;
175
- handlePopupCreation ( selectedText , rect , hideQuestion ) ;
190
+ // 添加一个短暂的延迟,确保旧状态完全清理
191
+ setTimeout ( ( ) => {
192
+ handlePopupCreation ( selectedText , rect , hideQuestion ) ;
193
+ } , 100 ) ;
176
194
}
177
195
} catch ( error ) {
178
196
console . warn ( 'Error in togglePopup:' , error ) ;
179
197
// 确保在出错时重置状态
180
198
safeRemovePopup ( ) ;
199
+ popupStateManager . reset ( ) ;
181
200
}
182
201
}
183
202
@@ -237,7 +256,7 @@ function handleIconClick(e, selectedText, rect, selection) {
237
256
}
238
257
239
258
document . addEventListener ( "mouseup" , function ( event ) {
240
- if ( ! isSelectionEnabled || isCreatingPopup || isHandlingIconClick ) return ;
259
+ if ( ! isSelectionEnabled || popupStateManager . isCreating ( ) || isHandlingIconClick ) return ;
241
260
242
261
const selection = window . getSelection ( ) ;
243
262
const selectedText = selection . toString ( ) . trim ( ) ;
0 commit comments