Преглед на файлове

【新增】增加深色主题支持并优化用户体验

- 在 README 和 设计文档中添加深色主题支持说明
- 实现深色主题的 CSS 变量和样式
- 优化时间输入验证和格式化功能
- 添加页面可见性处理和关闭提醒功能
- 调整 UI 样式,提升视觉效果和可用性
PJ568 преди 1 година
родител
ревизия
26e88a9258
променени са 4 файла, в които са добавени 207 реда и са изтрити 59 реда
  1. 1 1
      README.en.md
  2. 1 1
      README.md
  3. 21 6
      docs/design.md
  4. 184 51
      index.html

+ 1 - 1
README.en.md

@@ -63,7 +63,7 @@ The application uses responsive layout that automatically adapts to different sc
 
 - **Frontend**: Native HTML, CSS, JavaScript
 - **Data Storage**: Browser `localStorage`
-- **Styling**: Native CSS, no external dependencies
+- **Styling**: Native CSS, no external dependencies, with support for dark and light themes
 
 ### Core Architecture
 

+ 1 - 1
README.md

@@ -63,7 +63,7 @@
 
 - **前端**:原生 HTML、CSS、JavaScript
 - **数据存储**:浏览器 `localStorage`
-- **样式**:原生 CSS,无外部依赖
+- **样式**:原生 CSS,无外部依赖,支持深色和浅色主题
 
 ### 核心架构
 

+ 21 - 6
docs/design.md

@@ -10,26 +10,41 @@
 
 - **已学习时长**
   - 精确到秒,持续累加用户的有效学习时间。
-  - 以“X:X”(小时:分钟)的格式清晰展示。
+  - 以“小时:分钟”(H:MM)的格式清晰展示。
   - 数据通过浏览器 `localStorage` 持久化,关闭页面不丢失。
 - **番茄钟**
-  - 用户可自定义倒计时长(小时:分钟:秒)(默认为 25 分钟)。
+  - 用户可自定义倒计时长(分钟:秒)(默认为 25 分钟)。
   - 提供“▶️(开始)”、“⏸️(暂停)”、“⏹️(停止)”控制功能。
   - 倒计时结束或取消时,将已进行的时间计入“已学习时长”。
   - 界面需包含一个直观的圆形进度条,实时反馈剩余时间。
   - 倒计时结束时,通过浏览器通知或声音进行提醒。
 - **空闲钟**
   - 一个独立的、简单的倒计时工具。
-  - 用户可自定义倒计时长(小时:分钟:秒)(默认为 5 分钟)。
+  - 用户可自定义倒计时长(分钟:秒)(默认为 5 分钟)。
   - 提供“▶️(开始)”、“⏸️(暂停)”、“⏹️(停止)”功能。
   - 界面需包含一个直观的条形进度条,实时反馈剩余时间。
   - 此倒计时不计入总学习时长。
 
-### 2.2 技术选型
+### 2.2 高级功能
+
+- **时间输入验证与格式化**
+  - 时间输入框具有智能格式化功能,自动处理用户输入
+  - 支持自动添加冒号分隔符
+  - 限制输入长度和格式,确保时间值的有效性
+  - 在输入框失去焦点时验证时间值的有效性
+- **页面可见性处理**
+  - 当页面不可见时暂停计时器计算
+  - 当页面重新可见时,根据经过的时间更新计时器状态
+  - 确保计时器在页面切换时的准确性
+- **页面关闭提醒**
+  - 当用户尝试关闭有运行中计时器的页面时,显示提醒确认
+  - 防止用户意外关闭页面导致计时数据丢失
+
+### 2.3 技术选型
 
 - **前端框架**:无。使用原生 HTML、CSS 和 JavaScript,确保应用轻量、快速。遵循最小、最简代码原则。
-- **数据存储**:`localStorage`,用于持久化存储总学习时长。
-- **样式**:原生 CSS,实现简洁、方角、无阴影且现代化的界面风格。
+- **数据存储**:`localStorage`,用于持久化存储总学习时长和各计时器的预设时间。
+- **样式**:原生 CSS,实现简洁、现代化的界面风格,支持深色和浅色主题。
 
 ## 3. 界面与交互设计 (UI/UX)
 

+ 184 - 51
index.html

@@ -1,10 +1,67 @@
 <!DOCTYPE html>
-<html lang="zh-CN">
+<html lang="zh-Hans-CN">
   <head>
     <meta charset="UTF-8" />
     <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+    <meta name="description" content="🎓🍅♨️ - 高效记录进度的番茄钟" />
+    <meta name="keywords" content="进度跟踪,效率工具,时间管理" />
+    <link
+      rel="icon"
+      type="image/svg+xml"
+      href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Ctext y='.9em' font-size='90'%3E⌚%3C/text%3E%3C/svg%3E"
+    />
     <title>🎓🍅♨️</title>
     <style>
+      /* CSS 变量定义 - 浅色主题 */
+      :root {
+        /* 背景色 */
+        --bg-primary: #f5f5f5;
+        --bg-secondary: #fff;
+        --bg-tertiary: #f0f0f0;
+
+        /* 文字颜色 */
+        --text-primary: #333;
+        --text-secondary: #4caf50;
+        --text-accent-1: #ff5722;
+        --text-accent-2: #2196f3;
+
+        /* 边框和进度条 */
+        --border-primary: #e0e0e0;
+
+        /* 阴影 */
+        --shadow-primary: rgba(0, 0, 0, 0.1);
+
+        /* 透明度 */
+        --opacity-disabled: 0.7;
+        --opacity-button-disabled: 0.5;
+
+        /* 过渡效果 */
+        --transition-base: all 0.3s ease;
+        --transition-progress: width 1s linear, stroke-dashoffset 1s linear;
+      }
+
+      /* 深色主题变量 */
+      @media (prefers-color-scheme: dark) {
+        :root {
+          /* 背景色 */
+          --bg-primary: #1a1a1a;
+          --bg-secondary: #2d2d2d;
+          --bg-tertiary: #3a3a3a;
+
+          /* 文字颜色 */
+          --text-primary: #e0e0e0;
+          --text-secondary: #66bb6a;
+          --text-accent-1: #ff7043;
+          --text-accent-2: #42a5f5;
+
+          /* 边框和进度条 */
+          --border-primary: #4a4a4a;
+
+          /* 阴影 */
+          --shadow-primary: rgba(0, 0, 0, 0.3);
+        }
+      }
+
       /* 重置样式 */
       * {
         margin: 0;
@@ -15,10 +72,11 @@
       body {
         font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
           sans-serif;
-        background-color: #f5f5f5;
-        color: #333;
+        background-color: var(--bg-primary);
+        color: var(--text-primary);
         height: 100vh;
         overflow: hidden;
+        transition: var(--transition-base);
       }
 
       /* 主容器 */
@@ -29,10 +87,12 @@
         flex-direction: column;
         padding: 20px;
         gap: 20px;
+        overflow: scroll;
       }
 
       /* 标题 */
       .title {
+        display: none;
         text-align: center;
         font-size: 2rem;
         margin-bottom: 10px;
@@ -48,17 +108,19 @@
 
       /* 总学习时长 */
       .study-time {
-        background-color: #fff;
+        background-color: var(--bg-secondary);
         border-radius: 8px;
         padding: 20px;
         text-align: center;
-        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
+        box-shadow: 0 2px 4px var(--shadow-primary);
+        transition: var(--transition-base);
       }
 
       .study-time-display {
         font-size: 3rem;
         font-weight: bold;
-        color: #4caf50;
+        color: var(--text-secondary);
+        transition: var(--transition-base);
       }
 
       /* 番茄钟和空闲钟容器 */
@@ -71,7 +133,7 @@
 
       /* 番茄钟 */
       .pomodoro {
-        background-color: #fff;
+        background-color: var(--bg-secondary);
         border-radius: 8px;
         padding: 20px;
         flex: 2;
@@ -79,12 +141,13 @@
         flex-direction: column;
         align-items: center;
         justify-content: center;
-        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
+        box-shadow: 0 2px 4px var(--shadow-primary);
+        transition: var(--transition-base);
       }
 
       /* 空闲钟 */
       .idle {
-        background-color: #fff;
+        background-color: var(--bg-secondary);
         border-radius: 8px;
         padding: 20px;
         flex: 1;
@@ -92,7 +155,8 @@
         flex-direction: column;
         align-items: center;
         justify-content: center;
-        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
+        box-shadow: 0 2px 4px var(--shadow-primary);
+        transition: var(--transition-base);
       }
 
       /* 圆形进度条容器 */
@@ -111,18 +175,19 @@
 
       .circular-progress .background {
         fill: none;
-        stroke: #e0e0e0;
+        stroke: var(--border-primary);
         stroke-width: 10;
+        transition: var(--transition-base);
       }
 
       .circular-progress .progress {
         fill: none;
-        stroke: #ff5722;
+        stroke: var(--text-accent-1);
         stroke-width: 10;
         stroke-linecap: round;
         stroke-dasharray: 565.48;
         stroke-dashoffset: 565.48;
-        transition: stroke-dashoffset 1s linear;
+        transition: var(--transition-progress);
       }
 
       .circular-progress .time-display {
@@ -132,23 +197,33 @@
         transform: translate(-50%, -50%);
         font-size: 2rem;
         font-weight: bold;
+        color: inherit;
+        transition: var(--transition-base);
+      }
+
+      .idle .time-display {
+        font-size: 1.5rem;
+        font-weight: bold;
+        color: inherit;
+        transition: var(--transition-base);
       }
 
       /* 条形进度条 */
       .linear-progress {
         width: 100%;
         height: 20px;
-        background-color: #e0e0e0;
+        background-color: var(--border-primary);
         border-radius: 10px;
         margin: 20px 0;
         overflow: hidden;
+        transition: var(--transition-base);
       }
 
       .linear-progress .progress {
         height: 100%;
-        background-color: #2196f3;
+        background-color: var(--text-accent-2);
         width: 0%;
-        transition: width 1s linear;
+        transition: var(--transition-progress);
       }
 
       /* 时间输入 */
@@ -157,21 +232,22 @@
         border: none;
         background: transparent;
         text-align: center;
-        width: 100px;
+        width: 120px;
         font-weight: bold;
-        transition: all 0.3s ease;
+        transition: var(--transition-base);
+        color: inherit;
       }
 
       .time-input:focus {
         outline: none;
-        background-color: #f0f0f0;
+        background-color: var(--bg-tertiary);
         border-radius: 4px;
         transform: scale(1.05);
       }
 
       .time-input:disabled {
         cursor: not-allowed;
-        opacity: 0.7;
+        opacity: var(--opacity-disabled);
       }
 
       /* 控制按钮 */
@@ -188,11 +264,12 @@
         cursor: pointer;
         padding: 10px;
         border-radius: 50%;
-        transition: all 0.3s ease;
+        transition: var(--transition-base);
+        color: inherit;
       }
 
       .btn:hover {
-        background-color: #f0f0f0;
+        background-color: var(--bg-tertiary);
         transform: scale(1.1);
       }
 
@@ -201,7 +278,7 @@
       }
 
       .btn:disabled {
-        opacity: 0.5;
+        opacity: var(--opacity-button-disabled);
         cursor: not-allowed;
         transform: none;
       }
@@ -231,7 +308,7 @@
           grid-template-columns: 2fr 1fr;
           grid-template-rows: auto 1fr 1fr;
           gap: 20px;
-          transition: all 0.3s ease;
+          transition: var(--transition-base);
         }
 
         .title {
@@ -241,19 +318,19 @@
         .pomodoro {
           grid-row: 2 / 4;
           grid-column: 1;
-          transition: all 0.3s ease;
+          transition: var(--transition-base);
         }
 
         .study-time {
           grid-row: 2;
           grid-column: 2;
-          transition: all 0.3s ease;
+          transition: var(--transition-base);
         }
 
         .idle {
           grid-row: 3;
           grid-column: 2;
-          transition: all 0.3s ease;
+          transition: var(--transition-base);
         }
 
         .content {
@@ -285,7 +362,7 @@
                 <circle class="progress" cx="100" cy="100" r="90"></circle>
               </svg>
               <div class="time-display">
-                <input type="text" class="time-input" value="25:00" readonly />
+                <input type="text" class="time-input" value="25:00" />
               </div>
             </div>
             <div class="controls">
@@ -301,7 +378,7 @@
               <div class="progress"></div>
             </div>
             <div class="time-display">
-              <input type="text" class="time-input" value="5:00" readonly />
+              <input type="text" class="time-input" value="5:00" />
             </div>
             <div class="controls">
               <button class="btn start-btn">▶️</button>
@@ -378,6 +455,9 @@
           );
           this.updateProgress();
 
+          // 确保初始状态下输入框可编辑
+          this.timeInput.readOnly = false;
+
           // 事件监听
           this.startBtn.addEventListener("click", () => this.start());
           this.pauseBtn.addEventListener("click", () => this.pause());
@@ -396,7 +476,7 @@
                   !isNaN(minutes) &&
                   !isNaN(seconds) &&
                   minutes >= 0 &&
-                  minutes <= 99 &&
+                  minutes <= 1440 &&
                   seconds >= 0 &&
                   seconds <= 59
                 ) {
@@ -405,12 +485,18 @@
                   this.data.pomodoroTime = this.totalTime;
                   dataManager.save(this.data);
                   this.updateProgress();
+                } else {
+                  // 输入无效,重置为之前的有效值
+                  this.timeInput.value = dataManager.formatCountdown(
+                    this.remainingTime
+                  );
                 }
+              } else {
+                // 格式无效,重置为之前的有效值
+                this.timeInput.value = dataManager.formatCountdown(
+                  this.remainingTime
+                );
               }
-              // 重置为有效格式
-              this.timeInput.value = dataManager.formatCountdown(
-                this.remainingTime
-              );
             }
           });
 
@@ -420,14 +506,33 @@
             // 只允许数字和冒号
             value = value.replace(/[^0-9:]/g, "");
 
-            // 自动格式化
-            if (value.length > 2 && value.indexOf(":") === -1) {
-              value = value.slice(0, 2) + ":" + value.slice(2);
+            // 处理冒号位置
+            const colonIndex = value.indexOf(":");
+
+            if (colonIndex === -1) {
+              // 没有冒号,自动在倒数第二位前添加冒号
+              if (value.length >= 3) {
+                value = value.slice(0, -2) + ":" + value.slice(-2);
+              }
+            } else {
+              // 有冒号,确保冒号后只有两位数字
+              const parts = value.split(":");
+              if (parts.length === 2) {
+                const minutes = parts[0];
+                let seconds = parts[1];
+
+                // 限制秒数为两位
+                if (seconds.length > 2) {
+                  seconds = seconds.slice(0, 2);
+                }
+
+                value = minutes + ":" + seconds;
+              }
             }
 
-            // 限制长度
-            if (value.length > 5) {
-              value = value.slice(0, 5);
+            // 限制总长度(分钟最多4位 + 冒号 + 秒数2位 = 7位)
+            if (value.length > 7) {
+              value = value.slice(0, 7);
             }
 
             e.target.value = value;
@@ -572,6 +677,9 @@
           );
           this.updateProgress();
 
+          // 确保初始状态下输入框可编辑
+          this.timeInput.readOnly = false;
+
           // 事件监听
           this.startBtn.addEventListener("click", () => this.start());
           this.pauseBtn.addEventListener("click", () => this.pause());
@@ -590,7 +698,7 @@
                   !isNaN(minutes) &&
                   !isNaN(seconds) &&
                   minutes >= 0 &&
-                  minutes <= 99 &&
+                  minutes <= 1440 &&
                   seconds >= 0 &&
                   seconds <= 59
                 ) {
@@ -599,12 +707,18 @@
                   this.data.idleTime = this.totalTime;
                   dataManager.save(this.data);
                   this.updateProgress();
+                } else {
+                  // 输入无效,重置为之前的有效值
+                  this.timeInput.value = dataManager.formatCountdown(
+                    this.remainingTime
+                  );
                 }
+              } else {
+                // 格式无效,重置为之前的有效值
+                this.timeInput.value = dataManager.formatCountdown(
+                  this.remainingTime
+                );
               }
-              // 重置为有效格式
-              this.timeInput.value = dataManager.formatCountdown(
-                this.remainingTime
-              );
             }
           });
 
@@ -614,14 +728,33 @@
             // 只允许数字和冒号
             value = value.replace(/[^0-9:]/g, "");
 
-            // 自动格式化
-            if (value.length > 2 && value.indexOf(":") === -1) {
-              value = value.slice(0, 2) + ":" + value.slice(2);
+            // 处理冒号位置
+            const colonIndex = value.indexOf(":");
+
+            if (colonIndex === -1) {
+              // 没有冒号,自动在倒数第二位前添加冒号
+              if (value.length >= 3) {
+                value = value.slice(0, -2) + ":" + value.slice(-2);
+              }
+            } else {
+              // 有冒号,确保冒号后只有两位数字
+              const parts = value.split(":");
+              if (parts.length === 2) {
+                const minutes = parts[0];
+                let seconds = parts[1];
+
+                // 限制秒数为两位
+                if (seconds.length > 2) {
+                  seconds = seconds.slice(0, 2);
+                }
+
+                value = minutes + ":" + seconds;
+              }
             }
 
-            // 限制长度
-            if (value.length > 5) {
-              value = value.slice(0, 5);
+            // 限制总长度(分钟最多4位 + 冒号 + 秒数2位 = 7位)
+            if (value.length > 7) {
+              value = value.slice(0, 7);
             }
 
             e.target.value = value;