Prechádzať zdrojové kódy

【新增】添加清空学习时长功能

- 增加"清空"、"确认"和"取消"按钮及相关功能
- 实现学习时长清空逻辑,包括显示确认对话框和取消操作
- 优化学习时长显示和控制按钮布局
PJ568 1 rok pred
rodič
commit
09f6e49edd
2 zmenil súbory, kde vykonal 79 pridanie a 9 odobranie
  1. 11 3
      docs/design.md
  2. 68 6
      index.html

+ 11 - 3
docs/design.md

@@ -12,6 +12,7 @@
   - 精确到秒,持续累加用户的有效学习时间。
   - 以“小时:分钟”(H:MM)的格式清晰展示。
   - 数据通过浏览器 `localStorage` 持久化,关闭页面不丢失。
+  - 提供“🧹(清空)、☑️(确认)和 ❎(取消)”按钮和相关功能
 - **番茄钟**
   - 用户可自定义倒计时长(分钟:秒)(默认为 25 分钟)。
   - 提供“▶️(开始)”、“⏸️(暂停)”、“⏹️(停止)”控制功能。
@@ -52,6 +53,13 @@
 
 应用将采用响应式网格布局,根据不同屏幕尺寸自动调整排列方式,确保所有功能在任何设备上都能一次性展示,无需滚动。
 
+当按下 🗑(清空)后,其消失,显示 ☑️(确认)和 ❎(取消)按钮。
+如果用户点击“取消”,则回到上一状态。
+当用户点击“确认”,则清空学习时长并返回初始状态。
+
+当番茄钟或空闲钟未处于运行状态时,“剩余时间”可编辑,操作按钮处显示“▶️(开始)”按钮。
+当番茄钟或空闲钟处于运行状态时,“剩余时间”不可编辑,操作按钮处显示“⏸️(暂停)”和“⏹️(停止)”按钮。
+
 ### 3.1 响应式布局策略
 
 #### 大屏幕设备(> 1200px)
@@ -69,6 +77,7 @@ graph TD
     B --> B3[控制按钮];
 
     C --> C1["小时:分钟"];
+    C --> C2[控制按钮];
 
     D --> D1[条形进度条];
     D --> D2[剩余时间];
@@ -90,6 +99,7 @@ graph TD
     B --> B3[控制按钮];
 
     C --> C1["小时:分钟"];
+    C --> C2[控制按钮];
 
     D --> D1[条形进度条];
     D --> D2[剩余时间];
@@ -107,6 +117,7 @@ graph TD
     A --> D[♨️空闲钟 - 紧凑];
 
     B --> B1["小时:分钟"];
+    B --> B2[控制按钮];
 
     C --> C1[圆形进度条];
     C --> C2[剩余时间];
@@ -127,9 +138,6 @@ graph TD
 
 屏幕尺寸变化时,应用平滑的过渡动画,保持用户操作状态的连续性,确保用户体验的一致性。
 
-当番茄钟或空闲钟未处于运行状态时,“剩余时间”可编辑,操作按钮处显示“▶️(开始)”按钮。
-当番茄钟或空闲钟处于运行状态时,“剩余时间”不可编辑,操作按钮处显示“⏸️(暂停)”和“⏹️(停止)”按钮。
-
 ## 4. 数据结构
 
 应用所需的数据结构非常简单,将使用一个 JSON 对象存储在 `localStorage` 中。

+ 68 - 6
index.html

@@ -114,6 +114,10 @@
         text-align: center;
         box-shadow: 0 2px 4px var(--shadow-primary);
         transition: var(--transition-base);
+        display: flex;
+        flex-direction: column;
+        align-items: center;
+        justify-content: center;
       }
 
       .study-time-display {
@@ -351,6 +355,11 @@
         <div class="study-time">
           <h2>🎓</h2>
           <div class="study-time-display">0:00</div>
+          <div class="controls">
+            <button class="btn clear-btn">🧹</button>
+            <button class="btn confirm-btn" style="display: none">☑️</button>
+            <button class="btn cancel-btn" style="display: none">❎</button>
+          </div>
         </div>
 
         <div class="timer-container">
@@ -578,7 +587,7 @@
           const elapsed = this.totalTime - this.remainingTime;
           this.data.totalStudyTime += elapsed;
           dataManager.save(this.data);
-          studyTimeDisplay.textContent = dataManager.formatTime(
+          studyTimeManager.display.textContent = dataManager.formatTime(
             this.data.totalStudyTime
           );
 
@@ -602,7 +611,7 @@
           // 将全部时间加入总学习时长
           this.data.totalStudyTime += this.totalTime;
           dataManager.save(this.data);
-          studyTimeDisplay.textContent = dataManager.formatTime(
+          studyTimeManager.display.textContent = dataManager.formatTime(
             this.data.totalStudyTime
           );
 
@@ -856,12 +865,65 @@
         }
       }
 
+      // 总学习时长控制器
+      class StudyTimeManager {
+        constructor() {
+          this.display = document.querySelector(".study-time-display");
+          this.clearBtn = document.querySelector(".clear-btn");
+          this.confirmBtn = document.querySelector(".confirm-btn");
+          this.cancelBtn = document.querySelector(".cancel-btn");
+
+          this.data = dataManager.init();
+
+          this.init();
+        }
+
+        init() {
+          // 初始化显示
+          this.display.textContent = dataManager.formatTime(
+            this.data.totalStudyTime
+          );
+
+          // 事件监听
+          this.clearBtn.addEventListener("click", () =>
+            this.showConfirmDialog()
+          );
+          this.confirmBtn.addEventListener("click", () =>
+            this.clearStudyTime()
+          );
+          this.cancelBtn.addEventListener("click", () =>
+            this.hideConfirmDialog()
+          );
+        }
+
+        showConfirmDialog() {
+          // 显示确认和取消按钮,隐藏清空按钮
+          this.clearBtn.style.display = "none";
+          this.confirmBtn.style.display = "inline-block";
+          this.cancelBtn.style.display = "inline-block";
+        }
+
+        hideConfirmDialog() {
+          // 隐藏确认和取消按钮,显示清空按钮
+          this.clearBtn.style.display = "inline-block";
+          this.confirmBtn.style.display = "none";
+          this.cancelBtn.style.display = "none";
+        }
+
+        clearStudyTime() {
+          // 清空学习时长
+          this.data.totalStudyTime = 0;
+          dataManager.save(this.data);
+          this.display.textContent = dataManager.formatTime(0);
+
+          // 隐藏确认和取消按钮,显示清空按钮
+          this.hideConfirmDialog();
+        }
+      }
+
       // 初始化应用
       const data = dataManager.init();
-      const studyTimeDisplay = document.querySelector(".study-time-display");
-      studyTimeDisplay.textContent = dataManager.formatTime(
-        data.totalStudyTime
-      );
+      const studyTimeManager = new StudyTimeManager();
 
       // 初始化番茄钟
       const pomodoroElement = document.querySelector(".pomodoro");