Skip to content

Commit 768eb9b

Browse files
refactor: Enhance popup interaction with close button and icons
1 parent fe6f364 commit 768eb9b

File tree

7 files changed

+112
-57
lines changed

7 files changed

+112
-57
lines changed

README.md

-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
## 即将实现的功能
1717

18-
- [ ] **固定窗口**:支持将回复窗口固定在网页上的特定位置,确保在滚动或与页面互动时窗口保持不动。
19-
2018
- [ ] **代码复制功能**:增强代码片段复制功能。
2119

2220
- [ ] **基于会话的身份验证**:实现基于会话的身份验证,允许用户在API请求时进行安全且持续的访问,而无需反复输入API Key。

src/content/popup.js

+105-54
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { dragMoveListener, resizeMoveListener } from "./drag";
44
import interact from "interactjs";
55
import { getAIResponse } from "./api";
66
import { setAllowAutoScroll, updateAllowAutoScroll } from "./scrollControl";
7+
78
export function createPopup(text, rect) {
89
const popup = document.createElement("div");
910

@@ -69,7 +70,7 @@ export function createPopup(text, rect) {
6970
abortController.signal,
7071
ps,
7172
iconContainer,
72-
aiResponseContainer,
73+
aiResponseContainer
7374
);
7475

7576
refreshIcon.addEventListener("click", (event) => {
@@ -83,7 +84,7 @@ export function createPopup(text, rect) {
8384
abortController.signal,
8485
ps,
8586
iconContainer,
86-
aiResponseContainer,
87+
aiResponseContainer
8788
);
8889
});
8990

@@ -92,18 +93,6 @@ export function createPopup(text, rect) {
9293
updateAllowAutoScroll(aiResponseContainer);
9394
});
9495

95-
const clickOutsideHandler = (e) => {
96-
if (!popup.contains(e.target)) {
97-
document.body.removeChild(popup);
98-
document.removeEventListener("click", clickOutsideHandler);
99-
abortController.abort();
100-
}
101-
};
102-
103-
setTimeout(() => {
104-
document.addEventListener("click", clickOutsideHandler);
105-
}, 0);
106-
10796
const dragHandle = createDragHandle();
10897
popup.appendChild(dragHandle);
10998

@@ -131,31 +120,34 @@ export function createPopup(text, rect) {
131120

132121
export function stylePopup(popup, rect) {
133122
popup.id = "ai-popup";
134-
popup.style.position = "absolute";
135-
popup.style.width = "580px";
136-
popup.style.height = "380px";
137-
popup.style.paddingTop = "20px";
138-
popup.style.backgroundColor = "#f6f6f6a8";
139-
popup.style.boxShadow =
140-
"0 0 1px #0009,0 0 2px #0000000d,0 38px 90px #00000040";
141-
popup.style.backdropFilter = "blur(10px)";
142-
popup.style.borderRadius = "12px";
143-
popup.style.zIndex = "1000";
144-
popup.style.fontFamily = "Arial, sans-serif";
145-
popup.style.overflow = "auto";
123+
Object.assign(popup.style, {
124+
position: "absolute",
125+
width: "580px",
126+
height: "380px",
127+
paddingTop: "20px",
128+
backgroundColor: "#f6f6f6a8",
129+
boxShadow: "0 0 1px #0009, 0 0 2px #0000000d, 0 38px 90px #00000040",
130+
backdropFilter: "blur(10px)",
131+
borderRadius: "12px",
132+
zIndex: "1000",
133+
fontFamily: "Arial, sans-serif",
134+
overflow: "auto",
135+
});
146136

147137
const { adjustedX, adjustedY } = adjustPopupPosition(rect, popup);
148138
popup.style.left = `${adjustedX}px`;
149139
popup.style.top = `${adjustedY}px`;
150140
}
151141

152142
export function styleResponseContainer(container) {
153-
container.style.position = "relative";
143+
Object.assign(container.style, {
144+
position: "relative",
145+
width: "100%",
146+
height: "calc(100% - 40px)",
147+
marginTop: "20px",
148+
overflow: "auto",
149+
});
154150
container.id = "ai-response-container";
155-
container.style.width = "100%";
156-
container.style.height = "calc(100% - 40px)";
157-
container.style.marginTop = "20px";
158-
container.style.overflow = "auto";
159151
}
160152

161153
function adjustPopupPosition(rect, popup) {
@@ -173,42 +165,101 @@ function adjustPopupPosition(rect, popup) {
173165
adjustedY = rect.top + scrollY - popupHeight;
174166
}
175167

176-
if (adjustedX < scrollX) adjustedX = scrollX;
177-
if (adjustedX + popupWidth > viewportWidth + scrollX)
178-
adjustedX = viewportWidth + scrollX - popupWidth;
179-
if (adjustedY < scrollY) adjustedY = scrollY;
180-
if (adjustedY + popupHeight > viewportHeight + scrollY)
181-
adjustedY = viewportHeight + scrollY - popupHeight;
168+
adjustedX = Math.max(
169+
scrollX,
170+
Math.min(adjustedX, viewportWidth + scrollX - popupWidth)
171+
);
172+
adjustedY = Math.max(
173+
scrollY,
174+
Math.min(adjustedY, viewportHeight + scrollY - popupHeight)
175+
);
182176

183177
return { adjustedX, adjustedY };
184178
}
185179

186180
function createDragHandle() {
187181
const dragHandle = document.createElement("div");
188-
dragHandle.style.position = "absolute";
189-
dragHandle.style.top = "0";
190-
dragHandle.style.left = "0";
191-
dragHandle.style.width = "100%";
192-
dragHandle.style.height = "40px";
193-
dragHandle.style.backgroundColor = "#F2F2F7";
194-
dragHandle.style.cursor = "move";
195-
dragHandle.style.display = "flex";
196-
dragHandle.style.alignItems = "center";
197-
dragHandle.style.justifyContent = "center";
198-
dragHandle.style.marginBottom = "10px";
199-
dragHandle.style.fontWeight = "bold";
200-
dragHandle.style.color = "#007AFF";
201-
dragHandle.style.fontSize = "16px";
182+
Object.assign(dragHandle.style, {
183+
position: "absolute",
184+
top: "0",
185+
left: "0",
186+
width: "100%",
187+
height: "40px",
188+
backgroundColor: "#F2F2F7",
189+
cursor: "move",
190+
display: "flex",
191+
alignItems: "center",
192+
justifyContent: "center",
193+
padding: "0 10px",
194+
boxSizing: "border-box",
195+
});
196+
197+
const titleContainer = document.createElement("div");
198+
titleContainer.style.display = "flex";
199+
titleContainer.style.alignItems = "center";
202200

203201
const logo = document.createElement("img");
204202
logo.src = chrome.runtime.getURL("icons/icon24.png");
205203
logo.style.height = "24px";
206204
logo.style.marginRight = "10px";
207205

208-
dragHandle.appendChild(logo);
209-
210206
const textNode = document.createTextNode("DeepSeek AI");
211-
dragHandle.appendChild(textNode);
207+
titleContainer.appendChild(logo);
208+
titleContainer.appendChild(textNode);
209+
210+
const closeButton = document.createElement("button");
211+
Object.assign(closeButton.style, {
212+
display: "none",
213+
background: "none",
214+
border: "none",
215+
cursor: "pointer",
216+
padding: "0",
217+
transition: "all 0.2s ease",
218+
position: "absolute",
219+
right: "10px",
220+
});
221+
222+
const closeIcon = document.createElement("img");
223+
closeIcon.src = chrome.runtime.getURL("icons/close.svg");
224+
closeIcon.style.width = "20px";
225+
closeIcon.style.height = "20px";
226+
227+
closeButton.appendChild(closeIcon);
228+
229+
dragHandle.appendChild(titleContainer);
230+
dragHandle.appendChild(closeButton);
231+
232+
dragHandle.addEventListener("mouseenter", () => {
233+
closeButton.style.display = "block";
234+
});
235+
236+
dragHandle.addEventListener("mouseleave", () => {
237+
closeButton.style.display = "none";
238+
// 重置关闭按钮状态
239+
closeIcon.src = chrome.runtime.getURL("icons/close.svg");
240+
closeButton.style.transform = "scale(1)";
241+
});
242+
243+
closeButton.addEventListener("mouseenter", () => {
244+
closeIcon.src = chrome.runtime.getURL("icons/closeClicked.svg");
245+
closeButton.style.transform = "scale(1.1)";
246+
});
247+
248+
closeButton.addEventListener("mouseleave", () => {
249+
closeIcon.src = chrome.runtime.getURL("icons/close.svg");
250+
closeButton.style.transform = "scale(1)";
251+
});
252+
253+
closeButton.addEventListener("click", (event) => {
254+
event.stopPropagation();
255+
256+
setTimeout(() => {
257+
const popup = document.getElementById("ai-popup");
258+
if (popup) {
259+
document.body.removeChild(popup);
260+
}
261+
}, 200);
262+
});
212263

213264
return dragHandle;
214265
}

src/icons/close.svg

+1
Loading

src/icons/closeClicked.svg

+1
Loading

src/icons/pin.svg

+1
Loading

src/icons/pinClicked.svg

+1
Loading

src/manifest.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
"icons/copy.svg",
3737
"icons/redo.svg",
3838
"icons/redoClicked.svg",
39-
"icons/copyClicked.svg"
39+
"icons/copyClicked.svg",
40+
"icons/close.svg",
41+
"icons/closeClicked.svg"
4042
],
4143

4244
"matches": ["<all_urls>"]

0 commit comments

Comments
 (0)