index.html 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  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 class="controls"> -->
  318. <!-- TODO: 实现“📄(导出数据文件)、⚙️(应用数据文件)和 🏠([源码](https://github.com/PJ-568/time-of-studying))”数据管理按钮和相关功能 -->
  319. <!-- </div> -->
  320. </div>
  321. <div class="timer-container">
  322. <div class="pomodoro">
  323. <h2>🍅</h2>
  324. <div class="circular-progress">
  325. <svg>
  326. <circle class="background" cx="100" cy="100" r="90"></circle>
  327. <circle class="progress" cx="100" cy="100" r="90"></circle>
  328. </svg>
  329. <div class="time-display">
  330. <input type="text" class="time-input" value="25:00" />
  331. </div>
  332. </div>
  333. <div class="controls">
  334. <button class="btn start-btn">▶️</button>
  335. <button class="btn pause-btn" style="display: none">⏸️</button>
  336. <button class="btn stop-btn" style="display: none">⏹️</button>
  337. </div>
  338. </div>
  339. <div class="idle">
  340. <h2>♨️</h2>
  341. <div class="linear-progress">
  342. <div class="progress"></div>
  343. </div>
  344. <div class="time-display">
  345. <input type="text" class="time-input" value="5:00" />
  346. </div>
  347. <div class="controls">
  348. <button class="btn start-btn">▶️</button>
  349. <button class="btn pause-btn" style="display: none">⏸️</button>
  350. <button class="btn stop-btn" style="display: none">⏹️</button>
  351. </div>
  352. </div>
  353. </div>
  354. </div>
  355. </div>
  356. <script>
  357. // 数据管理
  358. const dataManager = {
  359. // 初始化数据
  360. init() {
  361. const data = localStorage.getItem("timerData");
  362. if (data) {
  363. return JSON.parse(data);
  364. } else {
  365. return {
  366. totalStudyTime: 0,
  367. pomodoroTime: 1500, // 25 分钟
  368. idleTime: 300, // 5 分钟
  369. };
  370. }
  371. },
  372. // 保存数据
  373. save(data) {
  374. localStorage.setItem("timerData", JSON.stringify(data));
  375. },
  376. // 格式化时间(秒转 HH:MM)
  377. formatTime(seconds) {
  378. const hours = Math.floor(seconds / 3600);
  379. const minutes = Math.floor((seconds % 3600) / 60);
  380. return `${hours}:${minutes.toString().padStart(2, "0")}`;
  381. },
  382. // 格式化倒计时(秒转 MM:SS)
  383. formatCountdown(seconds) {
  384. const minutes = Math.floor(seconds / 60);
  385. const secs = seconds % 60;
  386. return `${minutes}:${secs.toString().padStart(2, "0")}`;
  387. },
  388. };
  389. // 番茄钟控制器
  390. class PomodoroTimer {
  391. constructor(element) {
  392. this.element = element;
  393. this.timeInput = element.querySelector(".time-input");
  394. this.startBtn = element.querySelector(".start-btn");
  395. this.pauseBtn = element.querySelector(".pause-btn");
  396. this.stopBtn = element.querySelector(".stop-btn");
  397. this.progressCircle = element.querySelector(
  398. ".circular-progress .progress"
  399. );
  400. this.circumference = 2 * Math.PI * 90; // 圆的周长
  401. this.data = dataManager.init();
  402. this.totalTime = this.data.pomodoroTime;
  403. this.remainingTime = this.totalTime;
  404. this.isRunning = false;
  405. this.interval = null;
  406. this.init();
  407. }
  408. init() {
  409. this.timeInput.value = dataManager.formatCountdown(
  410. this.remainingTime
  411. );
  412. this.updateProgress();
  413. // 确保初始状态下输入框可编辑
  414. this.timeInput.readOnly = false;
  415. // 事件监听
  416. this.startBtn.addEventListener("click", () => this.start());
  417. this.pauseBtn.addEventListener("click", () => this.pause());
  418. this.stopBtn.addEventListener("click", () => this.stop());
  419. // 允许编辑时间
  420. this.timeInput.addEventListener("blur", () => {
  421. if (!this.isRunning) {
  422. const timeParts = this.timeInput.value.split(":");
  423. if (timeParts.length === 2) {
  424. const minutes = parseInt(timeParts[0], 10);
  425. const seconds = parseInt(timeParts[1], 10);
  426. // 验证输入
  427. if (
  428. !isNaN(minutes) &&
  429. !isNaN(seconds) &&
  430. minutes >= 0 &&
  431. minutes <= 1440 &&
  432. seconds >= 0 &&
  433. seconds <= 59
  434. ) {
  435. this.totalTime = minutes * 60 + seconds;
  436. this.remainingTime = this.totalTime;
  437. this.data.pomodoroTime = this.totalTime;
  438. dataManager.save(this.data);
  439. this.updateProgress();
  440. } else {
  441. // 输入无效,重置为之前的有效值
  442. this.timeInput.value = dataManager.formatCountdown(
  443. this.remainingTime
  444. );
  445. }
  446. } else {
  447. // 格式无效,重置为之前的有效值
  448. this.timeInput.value = dataManager.formatCountdown(
  449. this.remainingTime
  450. );
  451. }
  452. }
  453. });
  454. // 限制输入格式
  455. this.timeInput.addEventListener("input", (e) => {
  456. let value = e.target.value;
  457. // 只允许数字和冒号
  458. value = value.replace(/[^0-9:]/g, "");
  459. // 处理冒号位置
  460. const colonIndex = value.indexOf(":");
  461. if (colonIndex === -1) {
  462. // 没有冒号,自动在倒数第二位前添加冒号
  463. if (value.length >= 3) {
  464. value = value.slice(0, -2) + ":" + value.slice(-2);
  465. }
  466. } else {
  467. // 有冒号,确保冒号后只有两位数字
  468. const parts = value.split(":");
  469. if (parts.length === 2) {
  470. const minutes = parts[0];
  471. let seconds = parts[1];
  472. // 限制秒数为两位
  473. if (seconds.length > 2) {
  474. seconds = seconds.slice(0, 2);
  475. }
  476. value = minutes + ":" + seconds;
  477. }
  478. }
  479. // 限制总长度(分钟最多4位 + 冒号 + 秒数2位 = 7位)
  480. if (value.length > 7) {
  481. value = value.slice(0, 7);
  482. }
  483. e.target.value = value;
  484. });
  485. }
  486. start() {
  487. if (!this.isRunning) {
  488. this.isRunning = true;
  489. this.timeInput.readOnly = true;
  490. this.startBtn.style.display = "none";
  491. this.pauseBtn.style.display = "inline-block";
  492. this.stopBtn.style.display = "inline-block";
  493. this.interval = setInterval(() => {
  494. this.remainingTime--;
  495. this.timeInput.value = dataManager.formatCountdown(
  496. this.remainingTime
  497. );
  498. this.updateProgress();
  499. if (this.remainingTime <= 0) {
  500. this.complete();
  501. }
  502. }, 1000);
  503. }
  504. }
  505. pause() {
  506. if (this.isRunning) {
  507. this.isRunning = false;
  508. clearInterval(this.interval);
  509. this.startBtn.style.display = "inline-block";
  510. this.pauseBtn.style.display = "none";
  511. }
  512. }
  513. stop() {
  514. this.isRunning = false;
  515. clearInterval(this.interval);
  516. // 将已进行的时间加入总学习时长
  517. const elapsed = this.totalTime - this.remainingTime;
  518. this.data.totalStudyTime += elapsed;
  519. dataManager.save(this.data);
  520. studyTimeManager.display.textContent = dataManager.formatTime(
  521. this.data.totalStudyTime
  522. );
  523. // 重置计时器
  524. this.remainingTime = this.totalTime;
  525. this.timeInput.value = dataManager.formatCountdown(
  526. this.remainingTime
  527. );
  528. this.timeInput.readOnly = false;
  529. this.updateProgress();
  530. this.startBtn.style.display = "inline-block";
  531. this.pauseBtn.style.display = "none";
  532. this.stopBtn.style.display = "none";
  533. }
  534. complete() {
  535. this.isRunning = false;
  536. clearInterval(this.interval);
  537. // 将全部时间加入总学习时长
  538. this.data.totalStudyTime += this.totalTime;
  539. dataManager.save(this.data);
  540. studyTimeManager.display.textContent = dataManager.formatTime(
  541. this.data.totalStudyTime
  542. );
  543. // 重置计时器
  544. this.remainingTime = this.totalTime;
  545. this.timeInput.value = dataManager.formatCountdown(
  546. this.remainingTime
  547. );
  548. this.timeInput.readOnly = false;
  549. this.updateProgress();
  550. this.startBtn.style.display = "inline-block";
  551. this.pauseBtn.style.display = "none";
  552. this.stopBtn.style.display = "none";
  553. // 发送通知
  554. this.sendNotification("番茄钟完成", "恭喜你完成了一个番茄钟!");
  555. }
  556. updateProgress() {
  557. const progress =
  558. (this.totalTime - this.remainingTime) / this.totalTime;
  559. const offset = this.circumference - progress * this.circumference;
  560. this.progressCircle.style.strokeDashoffset = offset;
  561. }
  562. sendNotification(title, message) {
  563. if ("Notification" in window) {
  564. if (Notification.permission === "granted") {
  565. new Notification(title, { body: message });
  566. } else if (Notification.permission !== "denied") {
  567. Notification.requestPermission().then((permission) => {
  568. if (permission === "granted") {
  569. new Notification(title, { body: message });
  570. }
  571. });
  572. }
  573. }
  574. // 播放声音
  575. const audio = new Audio(
  576. "data:audio/wav;base64,UklGRnoGAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQoGAACBhYqFbF1fdJivrJBhNjVgodDbq2EcBj+a2/LDciUFLIHO8tiJNwgZaLvt559NEAxQp+PwtmMcBjiR1/LMeSwFJHfH8N2QQAoUXrTp66hVFApGn+DyvmwhBSuBzvLZiTYIG2m98OScTgwOUarm7blmFgU7k9n1unEiBC13yO/eizEIHWq+8+OWT"
  577. );
  578. audio.play();
  579. }
  580. }
  581. // 空闲钟控制器
  582. class IdleTimer {
  583. constructor(element) {
  584. this.element = element;
  585. this.timeInput = element.querySelector(".time-input");
  586. this.startBtn = element.querySelector(".start-btn");
  587. this.pauseBtn = element.querySelector(".pause-btn");
  588. this.stopBtn = element.querySelector(".stop-btn");
  589. this.progressBar = element.querySelector(
  590. ".linear-progress .progress"
  591. );
  592. this.data = dataManager.init();
  593. this.totalTime = this.data.idleTime;
  594. this.remainingTime = this.totalTime;
  595. this.isRunning = false;
  596. this.interval = null;
  597. this.init();
  598. }
  599. init() {
  600. this.timeInput.value = dataManager.formatCountdown(
  601. this.remainingTime
  602. );
  603. this.updateProgress();
  604. // 确保初始状态下输入框可编辑
  605. this.timeInput.readOnly = false;
  606. // 事件监听
  607. this.startBtn.addEventListener("click", () => this.start());
  608. this.pauseBtn.addEventListener("click", () => this.pause());
  609. this.stopBtn.addEventListener("click", () => this.stop());
  610. // 允许编辑时间
  611. this.timeInput.addEventListener("blur", () => {
  612. if (!this.isRunning) {
  613. const timeParts = this.timeInput.value.split(":");
  614. if (timeParts.length === 2) {
  615. const minutes = parseInt(timeParts[0], 10);
  616. const seconds = parseInt(timeParts[1], 10);
  617. // 验证输入
  618. if (
  619. !isNaN(minutes) &&
  620. !isNaN(seconds) &&
  621. minutes >= 0 &&
  622. minutes <= 1440 &&
  623. seconds >= 0 &&
  624. seconds <= 59
  625. ) {
  626. this.totalTime = minutes * 60 + seconds;
  627. this.remainingTime = this.totalTime;
  628. this.data.idleTime = this.totalTime;
  629. dataManager.save(this.data);
  630. this.updateProgress();
  631. } else {
  632. // 输入无效,重置为之前的有效值
  633. this.timeInput.value = dataManager.formatCountdown(
  634. this.remainingTime
  635. );
  636. }
  637. } else {
  638. // 格式无效,重置为之前的有效值
  639. this.timeInput.value = dataManager.formatCountdown(
  640. this.remainingTime
  641. );
  642. }
  643. }
  644. });
  645. // 限制输入格式
  646. this.timeInput.addEventListener("input", (e) => {
  647. let value = e.target.value;
  648. // 只允许数字和冒号
  649. value = value.replace(/[^0-9:]/g, "");
  650. // 处理冒号位置
  651. const colonIndex = value.indexOf(":");
  652. if (colonIndex === -1) {
  653. // 没有冒号,自动在倒数第二位前添加冒号
  654. if (value.length >= 3) {
  655. value = value.slice(0, -2) + ":" + value.slice(-2);
  656. }
  657. } else {
  658. // 有冒号,确保冒号后只有两位数字
  659. const parts = value.split(":");
  660. if (parts.length === 2) {
  661. const minutes = parts[0];
  662. let seconds = parts[1];
  663. // 限制秒数为两位
  664. if (seconds.length > 2) {
  665. seconds = seconds.slice(0, 2);
  666. }
  667. value = minutes + ":" + seconds;
  668. }
  669. }
  670. // 限制总长度(分钟最多4位 + 冒号 + 秒数2位 = 7位)
  671. if (value.length > 7) {
  672. value = value.slice(0, 7);
  673. }
  674. e.target.value = value;
  675. });
  676. }
  677. start() {
  678. if (!this.isRunning) {
  679. this.isRunning = true;
  680. this.timeInput.readOnly = true;
  681. this.startBtn.style.display = "none";
  682. this.pauseBtn.style.display = "inline-block";
  683. this.stopBtn.style.display = "inline-block";
  684. this.interval = setInterval(() => {
  685. this.remainingTime--;
  686. this.timeInput.value = dataManager.formatCountdown(
  687. this.remainingTime
  688. );
  689. this.updateProgress();
  690. if (this.remainingTime <= 0) {
  691. this.complete();
  692. }
  693. }, 1000);
  694. }
  695. }
  696. pause() {
  697. if (this.isRunning) {
  698. this.isRunning = false;
  699. clearInterval(this.interval);
  700. this.startBtn.style.display = "inline-block";
  701. this.pauseBtn.style.display = "none";
  702. }
  703. }
  704. stop() {
  705. this.isRunning = false;
  706. clearInterval(this.interval);
  707. // 重置计时器
  708. this.remainingTime = this.totalTime;
  709. this.timeInput.value = dataManager.formatCountdown(
  710. this.remainingTime
  711. );
  712. this.timeInput.readOnly = false;
  713. this.updateProgress();
  714. this.startBtn.style.display = "inline-block";
  715. this.pauseBtn.style.display = "none";
  716. this.stopBtn.style.display = "none";
  717. }
  718. complete() {
  719. this.isRunning = false;
  720. clearInterval(this.interval);
  721. // 重置计时器
  722. this.remainingTime = this.totalTime;
  723. this.timeInput.value = dataManager.formatCountdown(
  724. this.remainingTime
  725. );
  726. this.timeInput.readOnly = false;
  727. this.updateProgress();
  728. this.startBtn.style.display = "inline-block";
  729. this.pauseBtn.style.display = "none";
  730. this.stopBtn.style.display = "none";
  731. // 发送通知
  732. this.sendNotification("空闲时间结束", "休息时间结束了!");
  733. }
  734. updateProgress() {
  735. const progress =
  736. (this.totalTime - this.remainingTime) / this.totalTime;
  737. this.progressBar.style.width = `${progress * 100}%`;
  738. }
  739. sendNotification(title, message) {
  740. if ("Notification" in window) {
  741. if (Notification.permission === "granted") {
  742. new Notification(title, { body: message });
  743. } else if (Notification.permission !== "denied") {
  744. Notification.requestPermission().then((permission) => {
  745. if (permission === "granted") {
  746. new Notification(title, { body: message });
  747. }
  748. });
  749. }
  750. }
  751. // 播放声音
  752. const audio = new Audio(
  753. "data:audio/wav;base64,UklGRnoGAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQoGAACBhYqFbF1fdJivrJBhNjVgodDbq2EcBj+a2/LDciUFLIHO8tiJNwgZaLvt559NEAxQp+PwtmMcBjiR1/LMeSwFJHfH8N2QQAoUXrTp66hVFApGn+DyvmwhBSuBzvLZiTYIG2m98OScTgwOUarm7blmFgU7k9n1unEiBC13yO/eizEIHWq+8+OWT"
  754. );
  755. audio.play();
  756. }
  757. }
  758. // 总学习时长控制器
  759. class StudyTimeManager {
  760. constructor() {
  761. this.display = document.querySelector(".study-time-display");
  762. this.clearBtn = document.querySelector(".clear-btn");
  763. this.confirmBtn = document.querySelector(".confirm-btn");
  764. this.cancelBtn = document.querySelector(".cancel-btn");
  765. this.data = dataManager.init();
  766. this.init();
  767. }
  768. init() {
  769. // 初始化显示
  770. this.display.textContent = dataManager.formatTime(
  771. this.data.totalStudyTime
  772. );
  773. // 事件监听
  774. this.clearBtn.addEventListener("click", () =>
  775. this.showConfirmDialog()
  776. );
  777. this.confirmBtn.addEventListener("click", () =>
  778. this.clearStudyTime()
  779. );
  780. this.cancelBtn.addEventListener("click", () =>
  781. this.hideConfirmDialog()
  782. );
  783. }
  784. showConfirmDialog() {
  785. // 显示确认和取消按钮,隐藏清空按钮
  786. this.clearBtn.style.display = "none";
  787. this.confirmBtn.style.display = "inline-block";
  788. this.cancelBtn.style.display = "inline-block";
  789. }
  790. hideConfirmDialog() {
  791. // 隐藏确认和取消按钮,显示清空按钮
  792. this.clearBtn.style.display = "inline-block";
  793. this.confirmBtn.style.display = "none";
  794. this.cancelBtn.style.display = "none";
  795. }
  796. clearStudyTime() {
  797. // 清空学习时长
  798. this.data.totalStudyTime = 0;
  799. dataManager.save(this.data);
  800. this.display.textContent = dataManager.formatTime(0);
  801. // 隐藏确认和取消按钮,显示清空按钮
  802. this.hideConfirmDialog();
  803. }
  804. }
  805. // 初始化应用
  806. const data = dataManager.init();
  807. const studyTimeManager = new StudyTimeManager();
  808. // 初始化番茄钟
  809. const pomodoroElement = document.querySelector(".pomodoro");
  810. const pomodoroTimer = new PomodoroTimer(pomodoroElement);
  811. // 初始化空闲钟
  812. const idleElement = document.querySelector(".idle");
  813. const idleTimer = new IdleTimer(idleElement);
  814. // 请求通知权限
  815. if ("Notification" in window && Notification.permission === "default") {
  816. Notification.requestPermission();
  817. }
  818. // 处理页面可见性变化
  819. let lastVisibleTime = Date.now();
  820. document.addEventListener("visibilitychange", () => {
  821. if (document.visibilityState === "visible") {
  822. // 页面变为可见时,计算经过的时间并更新计时器
  823. const now = Date.now();
  824. const elapsedSeconds = Math.floor((now - lastVisibleTime) / 1000);
  825. if (pomodoroTimer.isRunning) {
  826. pomodoroTimer.remainingTime = Math.max(
  827. 0,
  828. pomodoroTimer.remainingTime - elapsedSeconds
  829. );
  830. pomodoroTimer.timeInput.value = dataManager.formatCountdown(
  831. pomodoroTimer.remainingTime
  832. );
  833. pomodoroTimer.updateProgress();
  834. if (pomodoroTimer.remainingTime <= 0) {
  835. pomodoroTimer.complete();
  836. }
  837. }
  838. if (idleTimer.isRunning) {
  839. idleTimer.remainingTime = Math.max(
  840. 0,
  841. idleTimer.remainingTime - elapsedSeconds
  842. );
  843. idleTimer.timeInput.value = dataManager.formatCountdown(
  844. idleTimer.remainingTime
  845. );
  846. idleTimer.updateProgress();
  847. if (idleTimer.remainingTime <= 0) {
  848. idleTimer.complete();
  849. }
  850. }
  851. }
  852. lastVisibleTime = Date.now();
  853. });
  854. // 页面关闭前提醒
  855. window.addEventListener("beforeunload", (e) => {
  856. if (pomodoroTimer.isRunning || idleTimer.isRunning) {
  857. e.preventDefault();
  858. e.returnValue = "您有正在运行的计时器,确定要离开吗?";
  859. return e.returnValue;
  860. }
  861. });
  862. </script>
  863. </body>
  864. </html>