Skip to content

Commit 849ff19

Browse files
committed
Fix: add rate button to free position mode
1 parent afcf694 commit 849ff19

File tree

1 file changed

+137
-10
lines changed

1 file changed

+137
-10
lines changed

src/popup/script/submission.js

Lines changed: 137 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,39 +79,166 @@ export const addRecordButton = () => {
7979
position: fixed;
8080
bottom: 20px;
8181
right: 20px;
82-
padding: 12px 20px;
82+
padding: 8px 12px; /* 减小内边距 */
8383
background: #2563eb;
8484
color: white;
8585
border: none;
86-
border-radius: 8px;
86+
border-radius: 6px; /* 稍微减小圆角 */
8787
cursor: pointer;
88-
font-size: 14px;
89-
box-shadow: 0 2px 10px rgba(37, 99, 235, 0.2);
90-
transition: all 0.2s ease;
88+
font-size: 13px; /* 减小字体大小 */
89+
box-shadow: 0 2px 8px rgba(37, 99, 235, 0.2);
90+
transition: background 0.2s ease, box-shadow 0.2s ease;
9191
z-index: 9999;
92+
user-select: none;
93+
display: flex;
94+
align-items: center;
95+
line-height: 1;
9296
}
9397
9498
.Leetcode-Mastery-Scheduler-record-btn:hover {
95-
transform: translateY(-2px);
96-
box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);
99+
box-shadow: 0 3px 10px rgba(37, 99, 235, 0.3);
100+
}
101+
102+
.Leetcode-Mastery-Scheduler-record-btn.dragging {
103+
opacity: 0.8;
104+
cursor: grabbing;
105+
transition: none;
106+
}
107+
108+
.Leetcode-Mastery-Scheduler-record-btn .drag-handle {
109+
display: inline-block;
110+
margin-right: 6px; /* 减小间距 */
111+
cursor: grab;
112+
opacity: 0.7;
113+
font-size: 12px; /* 减小拖动手柄大小 */
114+
}
115+
116+
.Leetcode-Mastery-Scheduler-record-btn .drag-handle:hover {
117+
opacity: 1;
118+
}
119+
120+
.Leetcode-Mastery-Scheduler-record-btn .reset-position {
121+
margin-left: 6px; /* 减小间距 */
122+
opacity: 0.7;
123+
cursor: pointer;
124+
font-size: 12px; /* 减小重置按钮大小 */
125+
}
126+
127+
.Leetcode-Mastery-Scheduler-record-btn .reset-position:hover {
128+
opacity: 1;
129+
}
130+
131+
.Leetcode-Mastery-Scheduler-record-btn .star-icon {
132+
margin-right: 4px;
133+
font-size: 11px;
97134
}
98135
`;
99136
document.head.appendChild(style);
100137

138+
// 从localStorage获取保存的位置
139+
const savedPosition = JSON.parse(localStorage.getItem('LMS_rateButtonPosition') || '{"bottom": 20, "right": 20}');
140+
101141
// 创建按钮
102142
const button = document.createElement('button');
103143
button.className = 'Leetcode-Mastery-Scheduler-record-btn';
104-
button.textContent = 'Rate It';
144+
button.innerHTML = `
145+
<span class="drag-handle">⋮</span>
146+
<i class="fas fa-star star-icon"></i>Rate
147+
<span class="reset-position" title="Reset position">↺</span>
148+
`;
149+
150+
// 设置保存的位置
151+
button.style.bottom = `${savedPosition.bottom}px`;
152+
button.style.right = `${savedPosition.right}px`;
105153

106154
// 添加点击事件
107-
button.addEventListener('click', async () => {
155+
button.addEventListener('click', async (e) => {
156+
// 如果点击的是拖动手柄或重置按钮,不触发评分
157+
if (e.target.classList.contains('drag-handle') || e.target.classList.contains('reset-position')) {
158+
return;
159+
}
160+
108161
const result = await handleFeedbackSubmission();
109162
if (result) {
110163
console.log("Submission successfully tracked!");
111164
console.log("难度记录成功!");
112165
}
113166
});
114-
167+
168+
// 重置位置
169+
const resetButton = button.querySelector('.reset-position');
170+
resetButton.addEventListener('click', (e) => {
171+
e.stopPropagation();
172+
button.style.bottom = '20px';
173+
button.style.right = '20px';
174+
localStorage.setItem('LMS_rateButtonPosition', JSON.stringify({bottom: 20, right: 20}));
175+
});
176+
177+
// 添加拖拽功能
178+
let isDragging = false;
179+
let startX, startY, startBottom, startRight;
180+
181+
const dragHandle = button.querySelector('.drag-handle');
182+
183+
// 鼠标按下事件
184+
const onMouseDown = (e) => {
185+
e.preventDefault();
186+
e.stopPropagation();
187+
188+
isDragging = true;
189+
button.classList.add('dragging');
190+
191+
// 记录初始位置
192+
startX = e.clientX;
193+
startY = e.clientY;
194+
startBottom = parseInt(getComputedStyle(button).bottom);
195+
startRight = parseInt(getComputedStyle(button).right);
196+
197+
// 添加鼠标移动和松开事件
198+
document.addEventListener('mousemove', onMouseMove);
199+
document.addEventListener('mouseup', onMouseUp);
200+
};
201+
202+
// 鼠标移动事件
203+
const onMouseMove = (e) => {
204+
if (!isDragging) return;
205+
206+
// 计算新位置
207+
const deltaX = startX - e.clientX;
208+
const deltaY = e.clientY - startY; // 修正垂直方向
209+
210+
const newRight = Math.max(10, startRight + deltaX);
211+
const newBottom = Math.max(10, startBottom - deltaY);
212+
213+
// 确保按钮不会超出屏幕
214+
const maxRight = window.innerWidth - button.offsetWidth - 10;
215+
const maxBottom = window.innerHeight - button.offsetHeight - 10;
216+
217+
button.style.right = `${Math.min(newRight, maxRight)}px`;
218+
button.style.bottom = `${Math.min(newBottom, maxBottom)}px`;
219+
};
220+
221+
// 鼠标松开事件
222+
const onMouseUp = () => {
223+
if (!isDragging) return;
224+
225+
isDragging = false;
226+
button.classList.remove('dragging');
227+
228+
// 保存新位置到localStorage
229+
localStorage.setItem('LMS_rateButtonPosition', JSON.stringify({
230+
bottom: parseInt(button.style.bottom),
231+
right: parseInt(button.style.right)
232+
}));
233+
234+
// 移除事件监听器
235+
document.removeEventListener('mousemove', onMouseMove);
236+
document.removeEventListener('mouseup', onMouseUp);
237+
};
238+
239+
// 添加事件监听器
240+
dragHandle.addEventListener('mousedown', onMouseDown);
241+
115242
// 添加到页面
116243
document.body.appendChild(button);
117244
};

0 commit comments

Comments
 (0)