index.html 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. <!DOCTYPE html>
  2. <html lang="zh-Hans-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <meta name="description" content="🎓🍅♨️ - 高效记录进度的番茄钟" />
  7. <meta name="keywords" content="进度跟踪,效率工具,时间管理" />
  8. <link
  9. rel="icon"
  10. type="image/svg+xml"
  11. 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"
  12. />
  13. <title>🎓🍅♨️</title>
  14. <style>
  15. /* CSS 变量定义 - 浅色主题 */
  16. :root {
  17. /* 背景色 */
  18. --bg-primary: #f5f5f5;
  19. --bg-secondary: #fff;
  20. --bg-tertiary: #f0f0f0;
  21. /* 文字颜色 */
  22. --text-primary: #333;
  23. --text-secondary: #4caf50;
  24. --text-accent-1: #ff5722;
  25. --text-accent-2: #2196f3;
  26. /* 边框和进度条 */
  27. --border-primary: #e0e0e0;
  28. /* 阴影 */
  29. --shadow-primary: rgba(0, 0, 0, 0.1);
  30. /* 透明度 */
  31. --opacity-disabled: 0.7;
  32. --opacity-button-disabled: 0.5;
  33. /* 过渡效果 */
  34. --transition-base: all 0.3s ease;
  35. --transition-progress: width 1s linear, stroke-dashoffset 1s linear;
  36. }
  37. /* 深色主题变量 */
  38. @media (prefers-color-scheme: dark) {
  39. :root {
  40. /* 背景色 */
  41. --bg-primary: #1a1a1a;
  42. --bg-secondary: #2d2d2d;
  43. --bg-tertiary: #3a3a3a;
  44. /* 文字颜色 */
  45. --text-primary: #e0e0e0;
  46. --text-secondary: #66bb6a;
  47. --text-accent-1: #ff7043;
  48. --text-accent-2: #42a5f5;
  49. /* 边框和进度条 */
  50. --border-primary: #4a4a4a;
  51. /* 阴影 */
  52. --shadow-primary: rgba(0, 0, 0, 0.3);
  53. }
  54. }
  55. /* 重置样式 */
  56. * {
  57. margin: 0;
  58. padding: 0;
  59. box-sizing: border-box;
  60. }
  61. body {
  62. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
  63. sans-serif;
  64. background-color: var(--bg-primary);
  65. color: var(--text-primary);
  66. height: 100vh;
  67. overflow: hidden;
  68. transition: var(--transition-base);
  69. }
  70. /* 主容器 */
  71. .container {
  72. width: 100%;
  73. height: 100vh;
  74. display: flex;
  75. flex-direction: column;
  76. padding: 20px;
  77. gap: 20px;
  78. overflow: scroll;
  79. }
  80. /* 标题 */
  81. .title {
  82. display: none;
  83. text-align: center;
  84. font-size: 2rem;
  85. margin-bottom: 10px;
  86. }
  87. /* 内容区域 */
  88. .content {
  89. flex: 1;
  90. display: flex;
  91. flex-direction: column;
  92. gap: 20px;
  93. }
  94. /* 总学习时长 */
  95. .study-time {
  96. background-color: var(--bg-secondary);
  97. border-radius: 8px;
  98. padding: 20px;
  99. text-align: center;
  100. box-shadow: 0 2px 4px var(--shadow-primary);
  101. transition: var(--transition-base);
  102. display: flex;
  103. flex-direction: column;
  104. align-items: center;
  105. justify-content: center;
  106. }
  107. .study-time-display {
  108. font-size: 3rem;
  109. font-weight: bold;
  110. color: var(--text-secondary);
  111. transition: var(--transition-base);
  112. }
  113. /* 番茄钟和空闲钟容器 */
  114. .timer-container {
  115. flex: 1;
  116. display: flex;
  117. flex-direction: column;
  118. gap: 20px;
  119. }
  120. /* 番茄钟 */
  121. .pomodoro {
  122. background-color: var(--bg-secondary);
  123. border-radius: 8px;
  124. padding: 20px;
  125. flex: 2;
  126. display: flex;
  127. flex-direction: column;
  128. align-items: center;
  129. justify-content: center;
  130. box-shadow: 0 2px 4px var(--shadow-primary);
  131. transition: var(--transition-base);
  132. }
  133. /* 空闲钟 */
  134. .idle {
  135. background-color: var(--bg-secondary);
  136. border-radius: 8px;
  137. padding: 20px;
  138. flex: 1;
  139. display: flex;
  140. flex-direction: column;
  141. align-items: center;
  142. justify-content: center;
  143. box-shadow: 0 2px 4px var(--shadow-primary);
  144. transition: var(--transition-base);
  145. }
  146. /* 圆形进度条容器 */
  147. .circular-progress {
  148. position: relative;
  149. width: 200px;
  150. height: 200px;
  151. margin: 20px 0;
  152. }
  153. .circular-progress svg {
  154. width: 100%;
  155. height: 100%;
  156. transform: rotate(-90deg);
  157. }
  158. .circular-progress .background {
  159. fill: none;
  160. stroke: var(--border-primary);
  161. stroke-width: 10;
  162. transition: var(--transition-base);
  163. }
  164. .circular-progress .progress {
  165. fill: none;
  166. stroke: var(--text-accent-1);
  167. stroke-width: 10;
  168. stroke-linecap: round;
  169. stroke-dasharray: 565.48;
  170. stroke-dashoffset: 565.48;
  171. transition: var(--transition-progress);
  172. }
  173. .circular-progress .time-display {
  174. position: absolute;
  175. top: 50%;
  176. left: 50%;
  177. transform: translate(-50%, -50%);
  178. font-size: 2rem;
  179. font-weight: bold;
  180. color: inherit;
  181. transition: var(--transition-base);
  182. }
  183. .idle .time-display {
  184. font-size: 1.5rem;
  185. font-weight: bold;
  186. color: inherit;
  187. transition: var(--transition-base);
  188. }
  189. /* 条形进度条 */
  190. .linear-progress {
  191. width: 100%;
  192. height: 20px;
  193. background-color: var(--border-primary);
  194. border-radius: 10px;
  195. margin: 20px 0;
  196. overflow: hidden;
  197. transition: var(--transition-base);
  198. }
  199. .linear-progress .progress {
  200. height: 100%;
  201. background-color: var(--text-accent-2);
  202. width: 0%;
  203. transition: var(--transition-progress);
  204. }
  205. /* 时间输入 */
  206. .time-input {
  207. font-size: 1.5rem;
  208. border: none;
  209. background: transparent;
  210. text-align: center;
  211. width: 120px;
  212. font-weight: bold;
  213. transition: var(--transition-base);
  214. color: inherit;
  215. }
  216. .time-input:focus {
  217. outline: none;
  218. background-color: var(--bg-tertiary);
  219. border-radius: 4px;
  220. transform: scale(1.05);
  221. }
  222. .time-input:disabled {
  223. cursor: not-allowed;
  224. opacity: var(--opacity-disabled);
  225. }
  226. /* 控制按钮 */
  227. .controls {
  228. display: flex;
  229. gap: 10px;
  230. margin-top: 20px;
  231. }
  232. .btn {
  233. background: none;
  234. border: none;
  235. font-size: 1.5rem;
  236. cursor: pointer;
  237. padding: 10px;
  238. border-radius: 50%;
  239. transition: var(--transition-base);
  240. color: inherit;
  241. }
  242. .btn:hover {
  243. background-color: var(--bg-tertiary);
  244. transform: scale(1.1);
  245. }
  246. .btn:active {
  247. transform: scale(0.95);
  248. }
  249. .btn:disabled {
  250. opacity: var(--opacity-button-disabled);
  251. cursor: not-allowed;
  252. transform: none;
  253. }
  254. /* 响应式布局 */
  255. @media (min-width: 768px) {
  256. .content {
  257. flex-direction: row;
  258. transition: flex-direction 0.3s ease;
  259. }
  260. .study-time {
  261. flex: 1;
  262. transition: flex 0.3s ease;
  263. }
  264. .timer-container {
  265. flex: 2;
  266. flex-direction: row;
  267. transition: flex 0.3s ease, flex-direction 0.3s ease;
  268. }
  269. }
  270. @media (min-width: 1200px) {
  271. .container {
  272. display: grid;
  273. grid-template-columns: 2fr 1fr;
  274. grid-template-rows: auto 1fr 1fr;
  275. gap: 20px;
  276. transition: var(--transition-base);
  277. }
  278. .title {
  279. grid-column: 1 / -1;
  280. }
  281. .pomodoro {
  282. grid-row: 2 / 4;
  283. grid-column: 1;
  284. transition: var(--transition-base);
  285. }
  286. .study-time {
  287. grid-row: 2;
  288. grid-column: 2;
  289. transition: var(--transition-base);
  290. }
  291. .idle {
  292. grid-row: 3;
  293. grid-column: 2;
  294. transition: var(--transition-base);
  295. }
  296. .content {
  297. display: contents;
  298. }
  299. .timer-container {
  300. display: contents;
  301. }
  302. }
  303. </style>
  304. </head>
  305. <body>
  306. <div class="container">
  307. <h1 class="title">🎓🍅♨️</h1>
  308. <div class="content">
  309. <div class="study-time">
  310. <h2>🎓</h2>
  311. <div class="study-time-display">0:00</div>
  312. <div class="controls">
  313. <button class="btn clear-btn">🧹</button>
  314. <button class="btn confirm-btn" style="display: none">☑️</button>
  315. <button class="btn cancel-btn" style="display: none">❎</button>
  316. </div>
  317. </div>
  318. <div class="timer-container">
  319. <div class="pomodoro">
  320. <h2>🍅</h2>
  321. <div class="circular-progress">
  322. <svg>
  323. <circle class="background" cx="100" cy="100" r="90"></circle>
  324. <circle class="progress" cx="100" cy="100" r="90"></circle>
  325. </svg>
  326. <div class="time-display">
  327. <input type="text" class="time-input" value="25:00" />
  328. </div>
  329. </div>
  330. <div class="controls">
  331. <button class="btn start-btn">▶️</button>
  332. <button class="btn pause-btn" style="display: none">⏸️</button>
  333. <button class="btn stop-btn" style="display: none">⏹️</button>
  334. </div>
  335. </div>
  336. <div class="idle">
  337. <h2>♨️</h2>
  338. <div class="linear-progress">
  339. <div class="progress"></div>
  340. </div>
  341. <div class="time-display">
  342. <input type="text" class="time-input" value="5:00" />
  343. </div>
  344. <div class="controls">
  345. <button class="btn start-btn">▶️</button>
  346. <button class="btn pause-btn" style="display: none">⏸️</button>
  347. <button class="btn stop-btn" style="display: none">⏹️</button>
  348. </div>
  349. </div>
  350. </div>
  351. </div>
  352. </div>
  353. <script>
  354. // 数据管理
  355. const dataManager = {
  356. // 初始化数据
  357. init() {
  358. const data = localStorage.getItem("timerData");
  359. if (data) {
  360. return JSON.parse(data);
  361. } else {
  362. return {
  363. totalStudyTime: 0,
  364. pomodoroTime: 1500, // 25 分钟
  365. idleTime: 300, // 5 分钟
  366. };
  367. }
  368. },
  369. // 保存数据
  370. save(data) {
  371. localStorage.setItem("timerData", JSON.stringify(data));
  372. },
  373. // 格式化时间(秒转 HH:MM)
  374. formatTime(seconds) {
  375. const hours = Math.floor(seconds / 3600);
  376. const minutes = Math.floor((seconds % 3600) / 60);
  377. return `${hours}:${minutes.toString().padStart(2, "0")}`;
  378. },
  379. // 格式化倒计时(秒转 MM:SS)
  380. formatCountdown(seconds) {
  381. const minutes = Math.floor(seconds / 60);
  382. const secs = seconds % 60;
  383. return `${minutes}:${secs.toString().padStart(2, "0")}`;
  384. },
  385. };
  386. // 番茄钟控制器
  387. class PomodoroTimer {
  388. constructor(element) {
  389. this.element = element;
  390. this.timeInput = element.querySelector(".time-input");
  391. this.startBtn = element.querySelector(".start-btn");
  392. this.pauseBtn = element.querySelector(".pause-btn");
  393. this.stopBtn = element.querySelector(".stop-btn");
  394. this.progressCircle = element.querySelector(
  395. ".circular-progress .progress"
  396. );
  397. this.circumference = 2 * Math.PI * 90; // 圆的周长
  398. this.data = dataManager.init();
  399. this.totalTime = this.data.pomodoroTime;
  400. this.remainingTime = this.totalTime;
  401. this.isRunning = false;
  402. this.interval = null;
  403. this.init();
  404. }
  405. init() {
  406. this.timeInput.value = dataManager.formatCountdown(
  407. this.remainingTime
  408. );
  409. this.updateProgress();
  410. // 确保初始状态下输入框可编辑
  411. this.timeInput.readOnly = false;
  412. // 事件监听
  413. this.startBtn.addEventListener("click", () => this.start());
  414. this.pauseBtn.addEventListener("click", () => this.pause());
  415. this.stopBtn.addEventListener("click", () => this.stop());
  416. // 允许编辑时间
  417. this.timeInput.addEventListener("blur", () => {
  418. if (!this.isRunning) {
  419. const timeParts = this.timeInput.value.split(":");
  420. if (timeParts.length === 2) {
  421. const minutes = parseInt(timeParts[0], 10);
  422. const seconds = parseInt(timeParts[1], 10);
  423. // 验证输入
  424. if (
  425. !isNaN(minutes) &&
  426. !isNaN(seconds) &&
  427. minutes >= 0 &&
  428. minutes <= 1440 &&
  429. seconds >= 0 &&
  430. seconds <= 59
  431. ) {
  432. this.totalTime = minutes * 60 + seconds;
  433. this.remainingTime = this.totalTime;
  434. this.data.pomodoroTime = this.totalTime;
  435. dataManager.save(this.data);
  436. this.updateProgress();
  437. } else {
  438. // 输入无效,重置为之前的有效值
  439. this.timeInput.value = dataManager.formatCountdown(
  440. this.remainingTime
  441. );
  442. }
  443. } else {
  444. // 格式无效,重置为之前的有效值
  445. this.timeInput.value = dataManager.formatCountdown(
  446. this.remainingTime
  447. );
  448. }
  449. }
  450. });
  451. // 限制输入格式
  452. this.timeInput.addEventListener("input", (e) => {
  453. let value = e.target.value;
  454. // 只允许数字和冒号
  455. value = value.replace(/[^0-9:]/g, "");
  456. // 处理冒号位置
  457. const colonIndex = value.indexOf(":");
  458. if (colonIndex === -1) {
  459. // 没有冒号,自动在倒数第二位前添加冒号
  460. if (value.length >= 3) {
  461. value = value.slice(0, -2) + ":" + value.slice(-2);
  462. }
  463. } else {
  464. // 有冒号,确保冒号后只有两位数字
  465. const parts = value.split(":");
  466. if (parts.length === 2) {
  467. const minutes = parts[0];
  468. let seconds = parts[1];
  469. // 限制秒数为两位
  470. if (seconds.length > 2) {
  471. seconds = seconds.slice(0, 2);
  472. }
  473. value = minutes + ":" + seconds;
  474. }
  475. }
  476. // 限制总长度(分钟最多4位 + 冒号 + 秒数2位 = 7位)
  477. if (value.length > 7) {
  478. value = value.slice(0, 7);
  479. }
  480. e.target.value = value;
  481. });
  482. }
  483. start() {
  484. if (!this.isRunning) {
  485. this.isRunning = true;
  486. this.timeInput.readOnly = true;
  487. this.startBtn.style.display = "none";
  488. this.pauseBtn.style.display = "inline-block";
  489. this.stopBtn.style.display = "inline-block";
  490. this.interval = setInterval(() => {
  491. this.remainingTime--;
  492. this.timeInput.value = dataManager.formatCountdown(
  493. this.remainingTime
  494. );
  495. this.updateProgress();
  496. if (this.remainingTime <= 0) {
  497. this.complete();
  498. }
  499. }, 1000);
  500. }
  501. }
  502. pause() {
  503. if (this.isRunning) {
  504. this.isRunning = false;
  505. clearInterval(this.interval);
  506. this.startBtn.style.display = "inline-block";
  507. this.pauseBtn.style.display = "none";
  508. }
  509. }
  510. stop() {
  511. this.isRunning = false;
  512. clearInterval(this.interval);
  513. // 将已进行的时间加入总学习时长
  514. const elapsed = this.totalTime - this.remainingTime;
  515. this.data.totalStudyTime += elapsed;
  516. dataManager.save(this.data);
  517. studyTimeManager.display.textContent = dataManager.formatTime(
  518. this.data.totalStudyTime
  519. );
  520. // 重置计时器
  521. this.remainingTime = this.totalTime;
  522. this.timeInput.value = dataManager.formatCountdown(
  523. this.remainingTime
  524. );
  525. this.timeInput.readOnly = false;
  526. this.updateProgress();
  527. this.startBtn.style.display = "inline-block";
  528. this.pauseBtn.style.display = "none";
  529. this.stopBtn.style.display = "none";
  530. }
  531. complete() {
  532. this.isRunning = false;
  533. clearInterval(this.interval);
  534. // 将全部时间加入总学习时长
  535. this.data.totalStudyTime += this.totalTime;
  536. dataManager.save(this.data);
  537. studyTimeManager.display.textContent = dataManager.formatTime(
  538. this.data.totalStudyTime
  539. );
  540. // 重置计时器
  541. this.remainingTime = this.totalTime;
  542. this.timeInput.value = dataManager.formatCountdown(
  543. this.remainingTime
  544. );
  545. this.timeInput.readOnly = false;
  546. this.updateProgress();
  547. this.startBtn.style.display = "inline-block";
  548. this.pauseBtn.style.display = "none";
  549. this.stopBtn.style.display = "none";
  550. // 发送通知
  551. this.sendNotification("番茄钟完成", "恭喜你完成了一个番茄钟!");
  552. }
  553. updateProgress() {
  554. const progress =
  555. (this.totalTime - this.remainingTime) / this.totalTime;
  556. const offset = this.circumference - progress * this.circumference;
  557. this.progressCircle.style.strokeDashoffset = offset;
  558. }
  559. sendNotification(title, message) {
  560. if ("Notification" in window) {
  561. if (Notification.permission === "granted") {
  562. new Notification(title, { body: message });
  563. } else if (Notification.permission !== "denied") {
  564. Notification.requestPermission().then((permission) => {
  565. if (permission === "granted") {
  566. new Notification(title, { body: message });
  567. }
  568. });
  569. }
  570. }
  571. // 播放声音
  572. const audio = new Audio(
  573. "data:audio/wav;base64,UklGRnoGAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQoGAACBhYqFbF1fdJivrJBhNjVgodDbq2EcBj+a2/LDciUFLIHO8tiJNwgZaLvt559NEAxQp+PwtmMcBjiR1/LMeSwFJHfH8N2QQAoUXrTp66hVFApGn+DyvmwhBSuBzvLZiTYIG2m98OScTgwOUarm7blmFgU7k9n1unEiBC13yO/eizEIHWq+8+OWT"
  574. );
  575. audio.play();
  576. }
  577. }
  578. // 空闲钟控制器
  579. class IdleTimer {
  580. constructor(element) {
  581. this.element = element;
  582. this.timeInput = element.querySelector(".time-input");
  583. this.startBtn = element.querySelector(".start-btn");
  584. this.pauseBtn = element.querySelector(".pause-btn");
  585. this.stopBtn = element.querySelector(".stop-btn");
  586. this.progressBar = element.querySelector(
  587. ".linear-progress .progress"
  588. );
  589. this.data = dataManager.init();
  590. this.totalTime = this.data.idleTime;
  591. this.remainingTime = this.totalTime;
  592. this.isRunning = false;
  593. this.interval = null;
  594. this.init();
  595. }
  596. init() {
  597. this.timeInput.value = dataManager.formatCountdown(
  598. this.remainingTime
  599. );
  600. this.updateProgress();
  601. // 确保初始状态下输入框可编辑
  602. this.timeInput.readOnly = false;
  603. // 事件监听
  604. this.startBtn.addEventListener("click", () => this.start());
  605. this.pauseBtn.addEventListener("click", () => this.pause());
  606. this.stopBtn.addEventListener("click", () => this.stop());
  607. // 允许编辑时间
  608. this.timeInput.addEventListener("blur", () => {
  609. if (!this.isRunning) {
  610. const timeParts = this.timeInput.value.split(":");
  611. if (timeParts.length === 2) {
  612. const minutes = parseInt(timeParts[0], 10);
  613. const seconds = parseInt(timeParts[1], 10);
  614. // 验证输入
  615. if (
  616. !isNaN(minutes) &&
  617. !isNaN(seconds) &&
  618. minutes >= 0 &&
  619. minutes <= 1440 &&
  620. seconds >= 0 &&
  621. seconds <= 59
  622. ) {
  623. this.totalTime = minutes * 60 + seconds;
  624. this.remainingTime = this.totalTime;
  625. this.data.idleTime = this.totalTime;
  626. dataManager.save(this.data);
  627. this.updateProgress();
  628. } else {
  629. // 输入无效,重置为之前的有效值
  630. this.timeInput.value = dataManager.formatCountdown(
  631. this.remainingTime
  632. );
  633. }
  634. } else {
  635. // 格式无效,重置为之前的有效值
  636. this.timeInput.value = dataManager.formatCountdown(
  637. this.remainingTime
  638. );
  639. }
  640. }
  641. });
  642. // 限制输入格式
  643. this.timeInput.addEventListener("input", (e) => {
  644. let value = e.target.value;
  645. // 只允许数字和冒号
  646. value = value.replace(/[^0-9:]/g, "");
  647. // 处理冒号位置
  648. const colonIndex = value.indexOf(":");
  649. if (colonIndex === -1) {
  650. // 没有冒号,自动在倒数第二位前添加冒号
  651. if (value.length >= 3) {
  652. value = value.slice(0, -2) + ":" + value.slice(-2);
  653. }
  654. } else {
  655. // 有冒号,确保冒号后只有两位数字
  656. const parts = value.split(":");
  657. if (parts.length === 2) {
  658. const minutes = parts[0];
  659. let seconds = parts[1];
  660. // 限制秒数为两位
  661. if (seconds.length > 2) {
  662. seconds = seconds.slice(0, 2);
  663. }
  664. value = minutes + ":" + seconds;
  665. }
  666. }
  667. // 限制总长度(分钟最多4位 + 冒号 + 秒数2位 = 7位)
  668. if (value.length > 7) {
  669. value = value.slice(0, 7);
  670. }
  671. e.target.value = value;
  672. });
  673. }
  674. start() {
  675. if (!this.isRunning) {
  676. this.isRunning = true;
  677. this.timeInput.readOnly = true;
  678. this.startBtn.style.display = "none";
  679. this.pauseBtn.style.display = "inline-block";
  680. this.stopBtn.style.display = "inline-block";
  681. this.interval = setInterval(() => {
  682. this.remainingTime--;
  683. this.timeInput.value = dataManager.formatCountdown(
  684. this.remainingTime
  685. );
  686. this.updateProgress();
  687. if (this.remainingTime <= 0) {
  688. this.complete();
  689. }
  690. }, 1000);
  691. }
  692. }
  693. pause() {
  694. if (this.isRunning) {
  695. this.isRunning = false;
  696. clearInterval(this.interval);
  697. this.startBtn.style.display = "inline-block";
  698. this.pauseBtn.style.display = "none";
  699. }
  700. }
  701. stop() {
  702. this.isRunning = false;
  703. clearInterval(this.interval);
  704. // 重置计时器
  705. this.remainingTime = this.totalTime;
  706. this.timeInput.value = dataManager.formatCountdown(
  707. this.remainingTime
  708. );
  709. this.timeInput.readOnly = false;
  710. this.updateProgress();
  711. this.startBtn.style.display = "inline-block";
  712. this.pauseBtn.style.display = "none";
  713. this.stopBtn.style.display = "none";
  714. }
  715. complete() {
  716. this.isRunning = false;
  717. clearInterval(this.interval);
  718. // 重置计时器
  719. this.remainingTime = this.totalTime;
  720. this.timeInput.value = dataManager.formatCountdown(
  721. this.remainingTime
  722. );
  723. this.timeInput.readOnly = false;
  724. this.updateProgress();
  725. this.startBtn.style.display = "inline-block";
  726. this.pauseBtn.style.display = "none";
  727. this.stopBtn.style.display = "none";
  728. // 发送通知
  729. this.sendNotification("空闲时间结束", "休息时间结束了!");
  730. }
  731. updateProgress() {
  732. const progress =
  733. (this.totalTime - this.remainingTime) / this.totalTime;
  734. this.progressBar.style.width = `${progress * 100}%`;
  735. }
  736. sendNotification(title, message) {
  737. if ("Notification" in window) {
  738. if (Notification.permission === "granted") {
  739. new Notification(title, { body: message });
  740. } else if (Notification.permission !== "denied") {
  741. Notification.requestPermission().then((permission) => {
  742. if (permission === "granted") {
  743. new Notification(title, { body: message });
  744. }
  745. });
  746. }
  747. }
  748. // 播放声音
  749. const audio = new Audio(
  750. "data:audio/wav;base64,UklGRnoGAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQoGAACBhYqFbF1fdJivrJBhNjVgodDbq2EcBj+a2/LDciUFLIHO8tiJNwgZaLvt559NEAxQp+PwtmMcBjiR1/LMeSwFJHfH8N2QQAoUXrTp66hVFApGn+DyvmwhBSuBzvLZiTYIG2m98OScTgwOUarm7blmFgU7k9n1unEiBC13yO/eizEIHWq+8+OWT"
  751. );
  752. audio.play();
  753. }
  754. }
  755. // 总学习时长控制器
  756. class StudyTimeManager {
  757. constructor() {
  758. this.display = document.querySelector(".study-time-display");
  759. this.clearBtn = document.querySelector(".clear-btn");
  760. this.confirmBtn = document.querySelector(".confirm-btn");
  761. this.cancelBtn = document.querySelector(".cancel-btn");
  762. this.data = dataManager.init();
  763. this.init();
  764. }
  765. init() {
  766. // 初始化显示
  767. this.display.textContent = dataManager.formatTime(
  768. this.data.totalStudyTime
  769. );
  770. // 事件监听
  771. this.clearBtn.addEventListener("click", () =>
  772. this.showConfirmDialog()
  773. );
  774. this.confirmBtn.addEventListener("click", () =>
  775. this.clearStudyTime()
  776. );
  777. this.cancelBtn.addEventListener("click", () =>
  778. this.hideConfirmDialog()
  779. );
  780. }
  781. showConfirmDialog() {
  782. // 显示确认和取消按钮,隐藏清空按钮
  783. this.clearBtn.style.display = "none";
  784. this.confirmBtn.style.display = "inline-block";
  785. this.cancelBtn.style.display = "inline-block";
  786. }
  787. hideConfirmDialog() {
  788. // 隐藏确认和取消按钮,显示清空按钮
  789. this.clearBtn.style.display = "inline-block";
  790. this.confirmBtn.style.display = "none";
  791. this.cancelBtn.style.display = "none";
  792. }
  793. clearStudyTime() {
  794. // 清空学习时长
  795. this.data.totalStudyTime = 0;
  796. dataManager.save(this.data);
  797. this.display.textContent = dataManager.formatTime(0);
  798. // 隐藏确认和取消按钮,显示清空按钮
  799. this.hideConfirmDialog();
  800. }
  801. }
  802. // 初始化应用
  803. const data = dataManager.init();
  804. const studyTimeManager = new StudyTimeManager();
  805. // 初始化番茄钟
  806. const pomodoroElement = document.querySelector(".pomodoro");
  807. const pomodoroTimer = new PomodoroTimer(pomodoroElement);
  808. // 初始化空闲钟
  809. const idleElement = document.querySelector(".idle");
  810. const idleTimer = new IdleTimer(idleElement);
  811. // 请求通知权限
  812. if ("Notification" in window && Notification.permission === "default") {
  813. Notification.requestPermission();
  814. }
  815. // 处理页面可见性变化
  816. let lastVisibleTime = Date.now();
  817. document.addEventListener("visibilitychange", () => {
  818. if (document.visibilityState === "visible") {
  819. // 页面变为可见时,计算经过的时间并更新计时器
  820. const now = Date.now();
  821. const elapsedSeconds = Math.floor((now - lastVisibleTime) / 1000);
  822. if (pomodoroTimer.isRunning) {
  823. pomodoroTimer.remainingTime = Math.max(
  824. 0,
  825. pomodoroTimer.remainingTime - elapsedSeconds
  826. );
  827. pomodoroTimer.timeInput.value = dataManager.formatCountdown(
  828. pomodoroTimer.remainingTime
  829. );
  830. pomodoroTimer.updateProgress();
  831. if (pomodoroTimer.remainingTime <= 0) {
  832. pomodoroTimer.complete();
  833. }
  834. }
  835. if (idleTimer.isRunning) {
  836. idleTimer.remainingTime = Math.max(
  837. 0,
  838. idleTimer.remainingTime - elapsedSeconds
  839. );
  840. idleTimer.timeInput.value = dataManager.formatCountdown(
  841. idleTimer.remainingTime
  842. );
  843. idleTimer.updateProgress();
  844. if (idleTimer.remainingTime <= 0) {
  845. idleTimer.complete();
  846. }
  847. }
  848. }
  849. lastVisibleTime = Date.now();
  850. });
  851. // 页面关闭前提醒
  852. window.addEventListener("beforeunload", (e) => {
  853. if (pomodoroTimer.isRunning || idleTimer.isRunning) {
  854. e.preventDefault();
  855. e.returnValue = "您有正在运行的计时器,确定要离开吗?";
  856. return e.returnValue;
  857. }
  858. });
  859. </script>
  860. </body>
  861. </html>