-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1543 lines (1267 loc) · 55.9 KB
/
script.js
File metadata and controls
1543 lines (1267 loc) · 55.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
let selectedOption = null;
let isBookmarked = false;
// Make dashboard toggle draggable with edge snapping
let dragStartX, dragStartY, dragStartLeft, dragStartTop;
let isDragging = false;
// 添加一个全局初始化函数,在DOM加载后调用
function initDashboardToggle() {
console.log('总体初始化仪表板功能');
// 获取元素
const dashboardToggle = document.getElementById('dashboard-toggle');
const dashboardPanel = document.getElementById('dashboard-panel');
if (!dashboardToggle) {
console.error('未找到仪表板切换按钮');
return;
}
if (!dashboardPanel) {
console.error('未找到仪表板面板');
return;
}
// 初始化拖动功能 - 如果已经调用过了,这里会再次加强一下
initDashboardDrag();
// 设置初始标题
dashboardToggle.setAttribute('title', dashboardPanel.classList.contains('hidden') ?
'Open Dashboard' : 'Close Dashboard');
// 设置初始状态
if (!dashboardPanel.classList.contains('hidden')) {
dashboardToggle.classList.add('active');
}
// 每2秒检查一次状态,以确保UI同步
setInterval(() => {
// 检查按钮是否在视口内
ensureDashboardToggleVisible();
// 确保面板和按钮状态同步
if (dashboardPanel.classList.contains('hidden')) {
dashboardToggle.classList.remove('active');
dashboardToggle.setAttribute('title', 'Open Dashboard');
} else {
dashboardToggle.classList.add('active');
dashboardToggle.setAttribute('title', 'Close Dashboard');
}
}, 2000);
}
// 修改init代码,在DOMContentLoaded最后添加调用
document.addEventListener('DOMContentLoaded', function() {
console.log('DOM fully loaded');
// 添加键盘快捷键支持
document.addEventListener('keydown', handleKeyPress);
// 鼠标移动光效
document.addEventListener('mousemove', function(e) {
const glowEffects = document.querySelectorAll('.glow-effect');
if (glowEffects.length > 0) {
const x = e.clientX;
const y = e.clientY;
// 让第一个光效跟随鼠标移动
glowEffects[0].style.top = (y - 150) + 'px';
glowEffects[0].style.left = (x - 150) + 'px';
}
});
// 初始化系统状态提示
updateSystemStatus('System ready', 'success');
// 加载设置
loadSettings();
// 初始化仪表板拖动和点击功能
setTimeout(initDashboardToggle, 500);
// 添加键盘快捷键
document.addEventListener('keydown', function(e) {
if (e.key.toLowerCase() === 'd') {
toggleDashboard();
} else if (e.key.toLowerCase() === 't') {
toggleHints();
}
});
});
// 处理键盘快捷键
function handleKeyPress(e) {
// 如果已经提交答案,部分快捷键不可用
const isExplanationVisible = !document.getElementById('explanation-section').classList.contains('hidden');
switch(e.key.toLowerCase()) {
case 'a':
case 'b':
case 'c':
case 'd':
if (!isExplanationVisible) {
selectOption(e.key.toLowerCase());
}
break;
case 'enter':
if (!isExplanationVisible && selectedOption) {
submitAnswer();
}
break;
case 'arrowleft':
// 前一题
navigateToPrevious();
break;
case 'arrowright':
// 后一题
navigateToNext();
break;
case 'h':
// 显示/隐藏帮助
showHelp();
break;
}
}
// 选择选项
function selectOption(option) {
// 如果已经提交答案,不允许再选择
if (document.getElementById('explanation-section').classList.contains('hidden') === false) {
return;
}
// 清除之前的选择
if (selectedOption) {
document.getElementById('option-' + selectedOption).classList.remove('option-selected');
}
// 设置新的选择
selectedOption = option;
document.getElementById('option-' + selectedOption).classList.add('option-selected');
// 更新系统状态
updateSystemStatus(`Option ${option.toUpperCase()} selected`, 'info');
}
// 提交答案
function submitAnswer() {
// 如果没有选择任何选项,不执行提交
if (!selectedOption) {
updateSystemStatus('Please select an option first', 'warning');
return;
}
// 显示提交中状态
updateSystemStatus('Submitting answer...', 'info');
// 模拟网络延迟
setTimeout(() => {
// 显示正确和错误的选项
document.getElementById('option-a').classList.add('option-correct');
document.getElementById('option-a').classList.add('option-answered');
let isCorrect = false;
if (selectedOption !== 'a') {
document.getElementById('option-' + selectedOption).classList.add('option-incorrect');
document.getElementById('option-' + selectedOption).classList.add('option-answered');
updateSystemStatus('Incorrect answer', 'error');
} else {
isCorrect = true;
updateSystemStatus('Correct answer!', 'success');
}
// 所有选项添加answered类
const allOptions = document.querySelectorAll('.option');
allOptions.forEach(option => {
option.classList.add('option-answered');
});
// 显示所有选项的机器人图标
document.querySelectorAll('.ask-about-option').forEach(el => {
el.classList.remove('hidden');
el.classList.add('visible');
});
// 显示解释部分
document.getElementById('explanation-section').classList.remove('hidden');
// 隐藏提交按钮
document.getElementById('submit-container').style.display = 'none';
// 显示AI聊天操作按钮
document.getElementById('ai-chat-actions').classList.remove('hidden');
// 更新进度条
document.querySelector('.progress-bar-fill').style.width = '40%';
}, 500);
// 检查是否开启了自动保存
const settings = JSON.parse(localStorage.getItem('userSettings'));
if (settings && settings.autoSave) {
// 延迟保存以确保UI更新完成
setTimeout(() => {
saveSession();
}, 1000);
}
}
// 更新系统状态提示
function updateSystemStatus(message, type = 'info') {
const statusElement = document.getElementById('status-indicator');
const iconElement = document.getElementById('status-icon');
const messageElement = document.getElementById('status-message');
// 清除之前的状态类
iconElement.classList.remove('success', 'error', 'warning', 'info');
// 设置图标
let iconClass = 'fa-info-circle';
switch(type) {
case 'success':
iconClass = 'fa-check-circle';
break;
case 'error':
iconClass = 'fa-times-circle';
break;
case 'warning':
iconClass = 'fa-exclamation-triangle';
break;
}
// 更新状态
iconElement.innerHTML = `<i class="fas ${iconClass}"></i>`;
iconElement.classList.add(type);
messageElement.textContent = message;
// 显示状态提示并设置自动消失(成功和信息类型)
statusElement.style.opacity = '1';
if (type === 'success' || type === 'info') {
setTimeout(() => {
statusElement.style.opacity = '0';
}, 3000);
}
}
// 切换收藏状态
function toggleBookmark() {
const bookmarkIcon = document.getElementById('bookmark-icon');
isBookmarked = !isBookmarked;
if (isBookmarked) {
bookmarkIcon.innerHTML = '<i class="fas fa-bookmark"></i>';
bookmarkIcon.classList.add('bookmarked');
updateSystemStatus('Question bookmarked', 'success');
} else {
bookmarkIcon.innerHTML = '<i class="far fa-bookmark"></i>';
bookmarkIcon.classList.remove('bookmarked');
updateSystemStatus('Bookmark removed', 'info');
}
}
// 显示帮助
function showHelp() {
document.getElementById('help-modal').classList.remove('hidden');
}
// 关闭模态窗口
function closeModal(modalId) {
document.getElementById(modalId).classList.add('hidden');
}
// 报告问题
function reportProblem() {
document.getElementById('feedback-modal').classList.remove('hidden');
}
// 提交反馈
function submitFeedback() {
const feedbackType = document.querySelector('.feedback-type').value;
const feedbackDetails = document.querySelector('.feedback-details').value;
if (!feedbackDetails.trim()) {
updateSystemStatus('Please provide feedback details', 'warning');
return;
}
// 这里应该是发送反馈到服务器的代码
// 模拟发送成功
updateSystemStatus('Thank you for your feedback!', 'success');
closeModal('feedback-modal');
// 清空反馈表单
document.querySelector('.feedback-details').value = '';
}
// 询问特定选项
function askAboutOption(option) {
const optionText = document.querySelector(`#option-${option} .option-text`).textContent;
// 强制打开AI对话
toggleAskAI(true);
// 设置问题文本并聚焦输入框
document.getElementById('ai-question').value = `Can you explain more about "${optionText}"?`;
document.getElementById('ai-question').focus();
// 滚动到AI对话区域
const askAiSection = document.querySelector('.ask-ai-section');
askAiSection.scrollIntoView({ behavior: 'smooth', block: 'start' });
// 高亮AI区域以引起注意
askAiSection.classList.add('highlight-section');
setTimeout(() => {
askAiSection.classList.remove('highlight-section');
}, 1000);
}
// 添加到错题本
function addToMistakeBook() {
updateSystemStatus('Added to mistake book for later review', 'success');
}
// 显示相似题目
function showSimilarProblems() {
updateSystemStatus('Loading similar problems...', 'info');
// 这里应该是加载相似题目的代码
setTimeout(() => {
updateSystemStatus('Similar problems loaded', 'success');
}, 1000);
}
// 保存对话到错题本
function saveConversationToMistakeBook() {
updateSystemStatus('Conversation saved to mistake book', 'success');
}
// 清除对话
function clearConversation() {
const chatContainer = document.getElementById('ai-chat-container');
chatContainer.innerHTML = '';
// 隐藏操作按钮
document.getElementById('ai-chat-actions').classList.add('hidden');
updateSystemStatus('Conversation cleared', 'info');
}
// 导航到上一题
function navigateToPrevious() {
updateSystemStatus('Loading previous question...', 'info');
// 这里应该是加载上一题的代码
}
// 导航到下一题
function navigateToNext() {
updateSystemStatus('Loading next question...', 'info');
// 这里应该是加载下一题的代码
}
// AI问答功能
function toggleAskAI(forceOpen = false) {
const content = document.getElementById('ask-ai-content');
const header = document.querySelector('.ask-ai-header');
if (forceOpen && content.classList.contains('hidden')) {
content.classList.remove('hidden');
header.classList.add('active');
// 确保聊天区域可见
setTimeout(() => {
const container = document.getElementById('ai-chat-container');
if (container) {
container.scrollTop = container.scrollHeight;
}
}, 100);
} else {
content.classList.toggle('hidden');
header.classList.toggle('active');
if (!content.classList.contains('hidden')) {
// 聚焦到输入框
setTimeout(() => {
document.getElementById('ai-question').focus();
const container = document.getElementById('ai-chat-container');
if (container) {
container.scrollTop = container.scrollHeight;
}
}, 100);
}
}
}
// 提交AI问题
function submitQuestion() {
const questionInput = document.getElementById('ai-question');
const question = questionInput.value.trim();
if (!question) {
updateSystemStatus('Please enter your question', 'warning');
return;
}
// 获取聊天容器
const chatContainer = document.getElementById('ai-chat-container');
// 添加用户问题
const userQueryElement = document.createElement('div');
userQueryElement.className = 'user-query';
userQueryElement.textContent = question;
chatContainer.appendChild(userQueryElement);
// 清除输入框
questionInput.value = '';
// 显示正在输入指示
const typingElement = document.createElement('div');
typingElement.className = 'ai-response ai-typing';
typingElement.innerHTML = '<span class="typing-dot"></span><span class="typing-dot"></span><span class="typing-dot"></span>';
chatContainer.appendChild(typingElement);
// 滚动到最新消息
chatContainer.scrollTop = chatContainer.scrollHeight;
// 显示操作按钮
document.getElementById('ai-chat-actions').classList.remove('hidden');
// 模拟AI响应延迟
setTimeout(() => {
// 移除正在输入指示
chatContainer.removeChild(typingElement);
// 根据问题内容生成响应
let response = generateResponseBasedOnQuestion(question);
// 添加AI响应
const aiResponseElement = document.createElement('div');
aiResponseElement.className = 'ai-response';
aiResponseElement.textContent = response;
chatContainer.appendChild(aiResponseElement);
// 滚动到最新消息
chatContainer.scrollTop = chatContainer.scrollHeight;
// 显示后续问题选项
addFollowUpOptions(question);
updateSystemStatus('AI responded to your question', 'success');
}, 1500);
}
// 会话管理相关函数
function saveSession() {
// 收集当前会话数据
const sessionData = {
id: 'session' + Date.now(),
name: 'Auto-saved session',
date: new Date().toLocaleString(),
progress: document.querySelector('.progress-bar-fill').style.width,
questions: [{
title: document.querySelector('.question-text').textContent,
selectedOption: selectedOption,
isCorrect: selectedOption === 'a'
}]
};
// 将会话数据保存到localStorage
const savedSessions = JSON.parse(localStorage.getItem('questionSessions') || '[]');
savedSessions.push(sessionData);
localStorage.setItem('questionSessions', JSON.stringify(savedSessions));
updateSystemStatus('Session saved', 'success');
}
function loadSessionModal() {
document.getElementById('load-session-modal').classList.remove('hidden');
}
function loadSession(sessionId) {
// 从localStorage获取会话数据
const savedSessions = JSON.parse(localStorage.getItem('questionSessions') || '[]');
const session = savedSessions.find(s => s.id === sessionId);
if (!session) {
updateSystemStatus('Unable to load session', 'error');
return;
}
// 应用会话数据到当前界面
// 此处应根据实际项目结构加载问题、选项等
updateSystemStatus(`Session loaded: ${session.name}`, 'success');
// 关闭模态框
closeModal('load-session-modal');
}
function confirmDeleteSession(event, sessionId) {
event.stopPropagation(); // 阻止冒泡,防止触发loadSession
// 存储要删除的会话ID
document.getElementById('confirm-delete-btn').dataset.sessionId = sessionId;
// 显示确认删除模态框
document.getElementById('confirm-delete-modal').classList.remove('hidden');
}
function deleteSession() {
const sessionId = document.getElementById('confirm-delete-btn').dataset.sessionId;
// 从localStorage删除会话
const savedSessions = JSON.parse(localStorage.getItem('questionSessions') || '[]');
const updatedSessions = savedSessions.filter(s => s.id !== sessionId);
localStorage.setItem('questionSessions', JSON.stringify(updatedSessions));
updateSystemStatus('Session deleted', 'info');
// 关闭模态框
closeModal('confirm-delete-modal');
// 如果在会话加载界面,更新列表
if (!document.getElementById('load-session-modal').classList.contains('hidden')) {
// 刷新会话列表 (实际应用中需要重新渲染列表)
}
// 如果在仪表板界面,更新最近会话
if (!document.getElementById('dashboard-panel').classList.contains('hidden')) {
// 刷新最近会话列表
}
}
// 仪表板相关函数
function toggleDashboard() {
console.log('toggleDashboard called');
const dashboardPanel = document.getElementById('dashboard-panel');
const dashboardToggle = document.getElementById('dashboard-toggle');
if (!dashboardPanel || !dashboardToggle) {
console.error('Dashboard elements not found!');
return;
}
// 切换hidden类
const wasHidden = dashboardPanel.classList.contains('hidden');
dashboardPanel.classList.toggle('hidden');
// 更新按钮状态
if (wasHidden) {
// 面板从隐藏变为显示
console.log('Opening dashboard panel');
dashboardToggle.classList.add('active');
dashboardToggle.setAttribute('title', 'Close Dashboard');
// 如果当前在视窗外,滚动到视图内
if (!isElementInViewport(dashboardPanel)) {
dashboardPanel.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
} else {
// 面板从显示变为隐藏
console.log('Closing dashboard panel');
dashboardToggle.classList.remove('active');
dashboardToggle.setAttribute('title', 'Open Dashboard');
}
// 记录最终状态,便于调试
console.log('Dashboard panel hidden:', dashboardPanel.classList.contains('hidden'));
console.log('Dashboard toggle active:', dashboardToggle.classList.contains('active'));
}
// 检查元素是否在视窗内
function isElementInViewport(el) {
const rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
// 增强边缘吸附功能
function checkEdgeSnap(left, top) {
const dashboardToggle = document.getElementById('dashboard-toggle');
const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;
const toggleWidth = dashboardToggle.offsetWidth;
const toggleHeight = dashboardToggle.offsetHeight;
const snapThreshold = 30; // 增大吸附距离阈值
// 移除所有吸附类
dashboardToggle.classList.remove('snap-left', 'snap-right', 'snap-top', 'snap-bottom');
let newLeft = left;
let newTop = top;
let snapped = false;
// 检查左边缘
if (left < snapThreshold) {
dashboardToggle.classList.add('snap-left');
newLeft = 0;
snapped = true;
}
// 检查右边缘
if (windowWidth - (left + toggleWidth) < snapThreshold) {
dashboardToggle.classList.add('snap-right');
newLeft = windowWidth - toggleWidth;
snapped = true;
}
// 检查上边缘
if (top < snapThreshold) {
dashboardToggle.classList.add('snap-top');
newTop = 0;
snapped = true;
}
// 检查下边缘
if (windowHeight - (top + toggleHeight) < snapThreshold) {
dashboardToggle.classList.add('snap-bottom');
newTop = windowHeight - toggleHeight;
snapped = true;
}
// 如果吸附了,应用新位置
if (snapped) {
dashboardToggle.style.left = newLeft + 'px';
dashboardToggle.style.top = newTop + 'px';
}
}
// 修改endDrag函数,记录拖动结束时间
function endDrag(e) {
if (!isDragging) return;
// 记录拖动结束时间
window.lastDragEnd = Date.now();
// 标记拖动为假
isDragging = false;
const dashboardToggle = document.getElementById('dashboard-toggle');
dashboardToggle.style.cursor = 'move';
dashboardToggle.style.transition = 'all 0.3s ease';
// 计算拖动距离
const dragDistance = 0; // 初始化拖动距离
if (window.dragStartTime) {
const dragTime = Date.now() - window.dragStartTime;
// 如果拖动时间很短且没有明显移动,可能是点击意图
if (dragTime < 200) {
console.log('Short drag detected, likely a click intention');
// 这里可以触发toggleDashboard,但我们依赖专门的click事件处理器来做这事
}
}
// 获取最终位置(考虑吸附后的位置)
const rect = dashboardToggle.getBoundingClientRect();
// 保存位置到localStorage
const position = {
top: rect.top,
left: rect.left,
snapClasses: []
};
// 添加吸附类到保存的位置
if (dashboardToggle.classList.contains('snap-left')) position.snapClasses.push('snap-left');
if (dashboardToggle.classList.contains('snap-right')) position.snapClasses.push('snap-right');
if (dashboardToggle.classList.contains('snap-top')) position.snapClasses.push('snap-top');
if (dashboardToggle.classList.contains('snap-bottom')) position.snapClasses.push('snap-bottom');
localStorage.setItem('dashboardPosition', JSON.stringify(position));
// 如果有事件对象,阻止进一步处理
if (e) e.stopPropagation();
}
// 用户设置相关函数
function toggleUserSettings() {
document.getElementById('user-settings-modal').classList.remove('hidden');
}
function saveSettings() {
// 获取设置值
const theme = document.querySelector('input[name="theme"]:checked').value;
const autoSave = document.getElementById('auto-save-toggle').checked;
// 保存设置到localStorage
const settings = { theme, autoSave };
localStorage.setItem('userSettings', JSON.stringify(settings));
// 应用设置
applySettings(settings);
updateSystemStatus('Settings saved', 'success');
closeModal('user-settings-modal');
}
function applySettings(settings) {
// 应用主题设置
if (settings.theme === 'dark') {
document.body.classList.add('dark-theme');
} else if (settings.theme === 'light') {
document.body.classList.remove('dark-theme');
} else if (settings.theme === 'system') {
// 根据系统主题设置
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.body.classList.add('dark-theme');
} else {
document.body.classList.remove('dark-theme');
}
}
}
function loadSettings() {
// 从localStorage加载设置
const settings = JSON.parse(localStorage.getItem('userSettings'));
if (settings) {
// 设置表单值
if (settings.theme) {
document.querySelector(`input[name="theme"][value="${settings.theme}"]`).checked = true;
}
if (settings.autoSave !== undefined) {
document.getElementById('auto-save-toggle').checked = settings.autoSave;
}
// 应用设置
applySettings(settings);
}
}
// 渐进式提示系统
function toggleHints() {
const hintsContainer = document.getElementById('hints-container');
hintsContainer.classList.toggle('hidden');
}
function toggleHintLevel(level) {
const hintHeader = document.querySelector(`.hint-level[data-level="${level}"] .hint-header`);
const hintContent = document.getElementById(`hint-content-${level}`);
hintHeader.classList.toggle('active');
hintContent.classList.toggle('hidden');
// 更新系统状态
if (!hintContent.classList.contains('hidden')) {
updateSystemStatus(`Level ${level} hint shown`, 'info');
}
}
// 解释深度切换
function changeExplanationLevel(level) {
// 移除所有级别的活动状态
document.querySelectorAll('.level-option').forEach(el => {
el.classList.remove('active');
});
// 隐藏所有解释文本
document.querySelectorAll('.explanation-text').forEach(el => {
el.classList.add('hidden');
});
// 激活选中的级别
document.getElementById(`level-${level}`).classList.add('active');
// 显示对应的解释文本
document.getElementById(`explanation-text-${level}`).classList.remove('hidden');
updateSystemStatus(`已切换到${level}级解释`, 'info');
}
// 确认关键操作
function confirmClearConversation() {
if (confirm('确定要清除当前对话吗?此操作无法撤销。')) {
clearConversation();
}
}
// 交互式教程
let tutorialStep = 1;
const tutorialSteps = [
{
element: '.popup-title',
content: 'Welcome to AI Smart Practice! This tutorial will guide you through the main features of the system.',
position: 'bottom'
},
{
element: '.difficulty-tag',
content: 'Each question is labeled with a difficulty level to help you understand its complexity.',
position: 'right'
},
{
element: '.bookmark-icon',
content: 'You can bookmark interesting questions for later review.',
position: 'bottom'
},
{
element: '.options-container',
content: 'Select the answer you think is correct. The system will provide immediate feedback.',
position: 'right'
},
{
element: '.hints-toggle',
content: 'If you need help, you can get progressive hints ranging from subtle clues to detailed guidance.',
position: 'top'
},
{
element: '.dashboard-toggle',
content: 'View your learning dashboard to track progress and statistics. You can drag this button to reposition it!',
position: 'left'
},
{
element: '.ask-ai-section',
content: 'Ask the AI assistant any questions for personalized help. The chat interface shows messages above the input area.',
position: 'top'
},
{
element: '.popup-control[title="User Settings"]',
content: 'Adjust interface and learning preferences in settings to customize your experience.',
position: 'bottom'
}
];
function startTutorial() {
// 首先关闭帮助模态框
closeModal('help-modal');
// 设置起始步骤
tutorialStep = 1;
// 移除隐藏类以显示教程界面
document.getElementById('tutorial-overlay').classList.remove('hidden');
document.getElementById('tutorial-container').classList.remove('hidden');
// 更新教程显示
updateTutorialStep();
}
function updateTutorialStep() {
const step = tutorialSteps[tutorialStep - 1];
const targetElement = document.querySelector(step.element);
if (!targetElement) {
console.error(`Tutorial target element not found: ${step.element}`);
nextTutorialStep();
return;
}
// 特殊处理第7步 (AI聊天区域)
if (tutorialStep === 7) {
// 确保AI聊天区域可见
const askAiContent = document.getElementById('ask-ai-content');
if (askAiContent.classList.contains('hidden')) {
toggleAskAI(true);
}
// 添加示例对话,使聊天区域更有内容
if (document.getElementById('ai-chat-container').children.length === 0) {
// 添加示例问题和回答
const exampleQuestion = document.createElement('div');
exampleQuestion.className = 'user-query';
exampleQuestion.textContent = "How do shortest path algorithms compare?";
const exampleResponse = document.createElement('div');
exampleResponse.className = 'ai-response';
exampleResponse.textContent = "When comparing shortest path algorithms: Dijkstra is fastest for non-negative edge weights, Bellman-Ford can handle negative edges, and Floyd-Warshall finds all-pairs shortest paths but is slower. The choice depends on your specific graph requirements.";
const container = document.getElementById('ai-chat-container');
container.appendChild(exampleQuestion);
container.appendChild(exampleResponse);
// 添加示例后续问题
addFollowUpOptions("How do shortest path algorithms compare?");
// 滚动到对话底部
container.scrollTop = container.scrollHeight;
}
}
const tutorialHighlight = document.getElementById('tutorial-highlight');
const tutorialTooltip = document.getElementById('tutorial-tooltip');
// Update step text
document.getElementById('tutorial-step').textContent = `${tutorialStep}/${tutorialSteps.length}`;
document.getElementById('tutorial-tooltip-content').textContent = step.content;
// Get target element position
const rect = targetElement.getBoundingClientRect();
// Check if element is in viewport
const isInView = (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= window.innerHeight &&
rect.right <= window.innerWidth
);
// Scroll element into view if needed
if (!isInView) {
targetElement.scrollIntoView({
behavior: 'smooth',
block: 'center'
});
// Give time for scrolling before positioning the highlight
setTimeout(() => {
positionTutorialElements(targetElement, step, tutorialHighlight, tutorialTooltip);
}, 500);
} else {
positionTutorialElements(targetElement, step, tutorialHighlight, tutorialTooltip);
}
}
function positionTutorialElements(targetElement, step, tutorialHighlight, tutorialTooltip) {
// Get updated position after possible scrolling
const rect = targetElement.getBoundingClientRect();
// Set highlight position and size
tutorialHighlight.style.top = `${rect.top - 5}px`;
tutorialHighlight.style.left = `${rect.left - 5}px`;
tutorialHighlight.style.width = `${rect.width + 10}px`;
tutorialHighlight.style.height = `${rect.height + 10}px`;
// Set tooltip position
let tooltipTop, tooltipLeft;
// Determine best position to avoid going off-screen
const tooltipWidth = 300; // Width defined in CSS
const tooltipHeight = 150; // Approximate height, adjust as needed
switch(step.position) {
case 'top':
if (rect.top < tooltipHeight + 20) {
// Not enough space on top, place at bottom
tooltipTop = rect.bottom + 15;
tooltipLeft = rect.left + rect.width/2 - tooltipWidth/2;
} else {
tooltipTop = rect.top - tooltipHeight - 15;
tooltipLeft = rect.left + rect.width/2 - tooltipWidth/2;
}
break;
case 'bottom':
if (window.innerHeight - rect.bottom < tooltipHeight + 20) {
// Not enough space at bottom, place on top
tooltipTop = rect.top - tooltipHeight - 15;
tooltipLeft = rect.left + rect.width/2 - tooltipWidth/2;
} else {
tooltipTop = rect.bottom + 15;
tooltipLeft = rect.left + rect.width/2 - tooltipWidth/2;
}
break;
case 'left':
if (rect.left < tooltipWidth + 20) {
// Not enough space on left, place on right
tooltipTop = rect.top + rect.height/2 - tooltipHeight/2;
tooltipLeft = rect.right + 15;
} else {
tooltipTop = rect.top + rect.height/2 - tooltipHeight/2;
tooltipLeft = rect.left - tooltipWidth - 15;
}
break;
case 'right':
if (window.innerWidth - rect.right < tooltipWidth + 20) {
// Not enough space on right, place on left
tooltipTop = rect.top + rect.height/2 - tooltipHeight/2;
tooltipLeft = rect.left - tooltipWidth - 15;
} else {
tooltipTop = rect.top + rect.height/2 - tooltipHeight/2;
tooltipLeft = rect.right + 15;
}
break;
}
// Ensure tooltip stays within viewport
if (tooltipTop < 10) tooltipTop = 10;
if (tooltipLeft < 10) tooltipLeft = 10;
if (tooltipTop + tooltipHeight > window.innerHeight - 10) {
tooltipTop = window.innerHeight - tooltipHeight - 10;
}
if (tooltipLeft + tooltipWidth > window.innerWidth - 10) {
tooltipLeft = window.innerWidth - tooltipWidth - 10;
}
tutorialTooltip.style.top = `${tooltipTop}px`;
tutorialTooltip.style.left = `${tooltipLeft}px`;
// Update prev/next button states
document.getElementById('tutorial-prev').disabled = tutorialStep === 1;
document.getElementById('tutorial-next').textContent = tutorialStep === tutorialSteps.length ? 'Finish' : 'Next';
}
function nextTutorialStep() {
if (tutorialStep < tutorialSteps.length) {
tutorialStep++;
updateTutorialStep();
} else {
closeTutorial();
}