index.html 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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. <!-- PWA 相关元标签 -->
  9. <meta name="theme-color" content="#4caf50" />
  10. <meta name="apple-mobile-web-app-capable" content="yes" />
  11. <meta name="apple-mobile-web-app-status-bar-style" content="default" />
  12. <meta name="apple-mobile-web-app-title" content="番茄钟" />
  13. <meta name="application-name" content="番茄钟" />
  14. <meta name="msapplication-TileColor" content="#4caf50" />
  15. <meta name="msapplication-config" content="browserconfig.xml" />
  16. <!-- PWA Manifest 和图标 -->
  17. <link rel="manifest" href="manifest.json" />
  18. <link
  19. rel="icon"
  20. type="image/svg+xml"
  21. href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Crect width='100' height='100' fill='%234caf50'/%3E%3Ctext x='50' y='65' font-size='50' text-anchor='middle' fill='white'%3E🍅%3C/text%3E%3C/svg%3E"
  22. />
  23. <link
  24. rel="apple-touch-icon"
  25. href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 180 180'%3E%3Crect width='180' height='180' fill='%234caf50' rx='24'/%3E%3Ctext x='90' y='110' font-size='90' text-anchor='middle' fill='white'%3E🍅%3C/text%3E%3C/svg%3E"
  26. />
  27. <title>🎓🍅♨️</title>
  28. <style>
  29. /* CSS 变量定义 - 浅色主题 */
  30. :root {
  31. /* 背景色 */
  32. --bg-primary: #f5f5f5;
  33. --bg-secondary: #fff;
  34. --bg-tertiary: #f0f0f0;
  35. /* 文字颜色 */
  36. --text-primary: #333;
  37. --text-secondary: #4caf50;
  38. --text-accent-1: #ff5722;
  39. --text-accent-2: #2196f3;
  40. /* 边框和进度条 */
  41. --border-primary: #e0e0e0;
  42. /* 阴影 */
  43. --shadow-primary: rgba(0, 0, 0, 0.1);
  44. /* 透明度 */
  45. --opacity-disabled: 0.7;
  46. --opacity-button-disabled: 0.5;
  47. /* 过渡效果 */
  48. --transition-base: all 0.3s ease;
  49. --transition-progress: width 1s linear, stroke-dashoffset 1s linear;
  50. }
  51. /* 深色主题变量 */
  52. @media (prefers-color-scheme: dark) {
  53. :root {
  54. /* 背景色 */
  55. --bg-primary: #1a1a1a;
  56. --bg-secondary: #2d2d2d;
  57. --bg-tertiary: #3a3a3a;
  58. /* 文字颜色 */
  59. --text-primary: #e0e0e0;
  60. --text-secondary: #66bb6a;
  61. --text-accent-1: #ff7043;
  62. --text-accent-2: #42a5f5;
  63. /* 边框和进度条 */
  64. --border-primary: #4a4a4a;
  65. /* 阴影 */
  66. --shadow-primary: rgba(0, 0, 0, 0.3);
  67. }
  68. }
  69. /* 重置样式 */
  70. * {
  71. margin: 0;
  72. padding: 0;
  73. box-sizing: border-box;
  74. }
  75. body {
  76. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
  77. sans-serif;
  78. background-color: var(--bg-primary);
  79. color: var(--text-primary);
  80. height: 100vh;
  81. overflow: hidden;
  82. transition: var(--transition-base);
  83. }
  84. /* 主容器 */
  85. .container {
  86. width: 100%;
  87. height: 100vh;
  88. display: flex;
  89. flex-direction: column;
  90. padding: 20px;
  91. gap: 20px;
  92. overflow: scroll;
  93. }
  94. /* 标题 */
  95. .title {
  96. display: none;
  97. text-align: center;
  98. font-size: 2rem;
  99. margin-bottom: 10px;
  100. }
  101. /* 内容区域 */
  102. .content {
  103. flex: 1;
  104. display: flex;
  105. flex-direction: column;
  106. gap: 20px;
  107. }
  108. /* 总学习时长 */
  109. .study-time {
  110. background-color: var(--bg-secondary);
  111. border-radius: 8px;
  112. padding: 20px;
  113. text-align: center;
  114. box-shadow: 0 2px 4px var(--shadow-primary);
  115. transition: var(--transition-base);
  116. display: flex;
  117. flex-direction: column;
  118. align-items: center;
  119. justify-content: center;
  120. }
  121. .study-time-display {
  122. font-size: 3rem;
  123. font-weight: bold;
  124. color: var(--text-secondary);
  125. transition: var(--transition-base);
  126. }
  127. /* 番茄钟和空闲钟容器 */
  128. .timer-container {
  129. flex: 1;
  130. display: flex;
  131. flex-direction: column;
  132. gap: 20px;
  133. }
  134. /* 番茄钟 */
  135. .pomodoro {
  136. background-color: var(--bg-secondary);
  137. border-radius: 8px;
  138. padding: 20px;
  139. flex: 2;
  140. display: flex;
  141. flex-direction: column;
  142. align-items: center;
  143. justify-content: center;
  144. box-shadow: 0 2px 4px var(--shadow-primary);
  145. transition: var(--transition-base);
  146. }
  147. /* 空闲钟 */
  148. .idle {
  149. background-color: var(--bg-secondary);
  150. border-radius: 8px;
  151. padding: 20px;
  152. flex: 1;
  153. display: flex;
  154. flex-direction: column;
  155. align-items: center;
  156. justify-content: center;
  157. box-shadow: 0 2px 4px var(--shadow-primary);
  158. transition: var(--transition-base);
  159. }
  160. /* 圆形进度条容器 */
  161. .circular-progress {
  162. position: relative;
  163. width: 200px;
  164. height: 200px;
  165. margin: 20px 0;
  166. }
  167. .circular-progress svg {
  168. width: 100%;
  169. height: 100%;
  170. transform: rotate(-90deg);
  171. }
  172. .circular-progress .background {
  173. fill: none;
  174. stroke: var(--border-primary);
  175. stroke-width: 10;
  176. transition: var(--transition-base);
  177. }
  178. .circular-progress .progress {
  179. fill: none;
  180. stroke: var(--text-accent-1);
  181. stroke-width: 10;
  182. stroke-linecap: round;
  183. stroke-dasharray: 565.48;
  184. stroke-dashoffset: 565.48;
  185. transition: var(--transition-progress);
  186. }
  187. .circular-progress .time-display {
  188. position: absolute;
  189. top: 50%;
  190. left: 50%;
  191. transform: translate(-50%, -50%);
  192. font-size: 2rem;
  193. font-weight: bold;
  194. color: inherit;
  195. transition: var(--transition-base);
  196. }
  197. .idle .time-display {
  198. font-size: 1.5rem;
  199. font-weight: bold;
  200. color: inherit;
  201. transition: var(--transition-base);
  202. }
  203. /* 条形进度条 */
  204. .linear-progress {
  205. width: 100%;
  206. height: 20px;
  207. background-color: var(--border-primary);
  208. border-radius: 10px;
  209. margin: 20px 0;
  210. overflow: hidden;
  211. transition: var(--transition-base);
  212. }
  213. .linear-progress .progress {
  214. height: 100%;
  215. background-color: var(--text-accent-2);
  216. width: 0%;
  217. transition: var(--transition-progress);
  218. }
  219. /* 时间输入 */
  220. .time-input {
  221. font-size: 1.5rem;
  222. border: none;
  223. background: transparent;
  224. text-align: center;
  225. width: 120px;
  226. font-weight: bold;
  227. transition: var(--transition-base);
  228. color: inherit;
  229. }
  230. .time-input:focus {
  231. outline: none;
  232. background-color: var(--bg-tertiary);
  233. border-radius: 4px;
  234. transform: scale(1.05);
  235. }
  236. .time-input:disabled {
  237. cursor: not-allowed;
  238. opacity: var(--opacity-disabled);
  239. }
  240. /* 控制按钮 */
  241. .controls {
  242. display: flex;
  243. gap: 10px;
  244. margin-top: 20px;
  245. }
  246. .btn {
  247. background: none;
  248. border: none;
  249. font-size: 1.5rem;
  250. cursor: pointer;
  251. padding: 10px;
  252. border-radius: 50%;
  253. transition: var(--transition-base);
  254. color: inherit;
  255. }
  256. .btn:hover {
  257. background-color: var(--bg-tertiary);
  258. transform: scale(1.1);
  259. }
  260. .btn:active {
  261. transform: scale(0.95);
  262. }
  263. .btn:disabled {
  264. opacity: var(--opacity-button-disabled);
  265. cursor: not-allowed;
  266. transform: none;
  267. }
  268. /* 响应式布局 */
  269. @media (min-width: 768px) {
  270. .content {
  271. flex-direction: row;
  272. transition: flex-direction 0.3s ease;
  273. }
  274. .study-time {
  275. flex: 1;
  276. transition: flex 0.3s ease;
  277. }
  278. .timer-container {
  279. flex: 2;
  280. flex-direction: row;
  281. transition: flex 0.3s ease, flex-direction 0.3s ease;
  282. }
  283. }
  284. @media (min-width: 1200px) {
  285. .container {
  286. display: grid;
  287. grid-template-columns: 2fr 1fr;
  288. grid-template-rows: auto 1fr 1fr;
  289. gap: 20px;
  290. transition: var(--transition-base);
  291. }
  292. .title {
  293. grid-column: 1 / -1;
  294. }
  295. .pomodoro {
  296. grid-row: 2 / 4;
  297. grid-column: 1;
  298. transition: var(--transition-base);
  299. }
  300. .study-time {
  301. grid-row: 2;
  302. grid-column: 2;
  303. transition: var(--transition-base);
  304. }
  305. .idle {
  306. grid-row: 3;
  307. grid-column: 2;
  308. transition: var(--transition-base);
  309. }
  310. .content {
  311. display: contents;
  312. }
  313. .timer-container {
  314. display: contents;
  315. }
  316. }
  317. </style>
  318. </head>
  319. <body>
  320. <div class="container">
  321. <h1 class="title">🎓🍅♨️</h1>
  322. <div class="content">
  323. <div class="study-time">
  324. <h2>🎓</h2>
  325. <div class="study-time-display">0:00</div>
  326. <div class="controls">
  327. <button class="btn clear-btn">🧹</button>
  328. <button class="btn confirm-btn" style="display: none">☑️</button>
  329. <button class="btn cancel-btn" style="display: none">❎</button>
  330. </div>
  331. <!-- <div class="controls"> -->
  332. <!-- TODO: 实现“📄(导出数据文件)、⚙️(应用数据文件)和 🏠([源码](https://github.com/PJ-568/time-of-studying))”数据管理按钮和相关功能 -->
  333. <!-- </div> -->
  334. </div>
  335. <div class="timer-container">
  336. <div class="pomodoro">
  337. <h2>🍅</h2>
  338. <div class="circular-progress">
  339. <svg>
  340. <circle class="background" cx="100" cy="100" r="90"></circle>
  341. <circle class="progress" cx="100" cy="100" r="90"></circle>
  342. </svg>
  343. <div class="time-display">
  344. <input type="text" class="time-input" value="25:00" />
  345. </div>
  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 class="idle">
  354. <h2>♨️</h2>
  355. <div class="linear-progress">
  356. <div class="progress"></div>
  357. </div>
  358. <div class="time-display">
  359. <input type="text" class="time-input" value="5:00" />
  360. </div>
  361. <div class="controls">
  362. <button class="btn start-btn">▶️</button>
  363. <button class="btn pause-btn" style="display: none">⏸️</button>
  364. <button class="btn stop-btn" style="display: none">⏹️</button>
  365. </div>
  366. </div>
  367. </div>
  368. </div>
  369. </div>
  370. <script>
  371. // 数据管理
  372. const dataManager = {
  373. // 初始化数据
  374. init() {
  375. const data = localStorage.getItem("timerData");
  376. if (data) {
  377. return JSON.parse(data);
  378. } else {
  379. return {
  380. totalStudyTime: 0,
  381. pomodoroTime: 1500, // 25 分钟
  382. idleTime: 300, // 5 分钟
  383. };
  384. }
  385. },
  386. // 保存数据
  387. save(data) {
  388. localStorage.setItem("timerData", JSON.stringify(data));
  389. },
  390. // 格式化时间(秒转 HH:MM)
  391. formatTime(seconds) {
  392. const hours = Math.floor(seconds / 3600);
  393. const minutes = Math.floor((seconds % 3600) / 60);
  394. return `${hours}:${minutes.toString().padStart(2, "0")}`;
  395. },
  396. // 格式化倒计时(秒转 MM:SS)
  397. formatCountdown(seconds) {
  398. const minutes = Math.floor(seconds / 60);
  399. const secs = seconds % 60;
  400. return `${minutes}:${secs.toString().padStart(2, "0")}`;
  401. },
  402. };
  403. // 番茄钟控制器
  404. class PomodoroTimer {
  405. constructor(element) {
  406. this.element = element;
  407. this.timeInput = element.querySelector(".time-input");
  408. this.startBtn = element.querySelector(".start-btn");
  409. this.pauseBtn = element.querySelector(".pause-btn");
  410. this.stopBtn = element.querySelector(".stop-btn");
  411. this.progressCircle = element.querySelector(
  412. ".circular-progress .progress"
  413. );
  414. this.circumference = 2 * Math.PI * 90; // 圆的周长
  415. this.data = dataManager.init();
  416. this.totalTime = this.data.pomodoroTime;
  417. this.remainingTime = this.totalTime;
  418. this.isRunning = false;
  419. this.interval = null;
  420. this.init();
  421. }
  422. init() {
  423. this.timeInput.value = dataManager.formatCountdown(
  424. this.remainingTime
  425. );
  426. this.updateProgress();
  427. // 确保初始状态下输入框可编辑
  428. this.timeInput.readOnly = false;
  429. // 事件监听
  430. this.startBtn.addEventListener("click", () => this.start());
  431. this.pauseBtn.addEventListener("click", () => this.pause());
  432. this.stopBtn.addEventListener("click", () => this.stop());
  433. // 允许编辑时间
  434. this.timeInput.addEventListener("blur", () => {
  435. if (!this.isRunning) {
  436. const timeParts = this.timeInput.value.split(":");
  437. if (timeParts.length === 2) {
  438. const minutes = parseInt(timeParts[0], 10);
  439. const seconds = parseInt(timeParts[1], 10);
  440. // 验证输入
  441. if (
  442. !isNaN(minutes) &&
  443. !isNaN(seconds) &&
  444. minutes >= 0 &&
  445. minutes <= 1440 &&
  446. seconds >= 0 &&
  447. seconds <= 59
  448. ) {
  449. this.totalTime = minutes * 60 + seconds;
  450. this.remainingTime = this.totalTime;
  451. this.data.pomodoroTime = this.totalTime;
  452. dataManager.save(this.data);
  453. this.updateProgress();
  454. } else {
  455. // 输入无效,重置为之前的有效值
  456. this.timeInput.value = dataManager.formatCountdown(
  457. this.remainingTime
  458. );
  459. }
  460. } else {
  461. // 格式无效,重置为之前的有效值
  462. this.timeInput.value = dataManager.formatCountdown(
  463. this.remainingTime
  464. );
  465. }
  466. }
  467. });
  468. // 限制输入格式
  469. this.timeInput.addEventListener("input", (e) => {
  470. let value = e.target.value;
  471. // 只允许数字和冒号
  472. value = value.replace(/[^0-9:]/g, "");
  473. // 处理冒号位置
  474. const colonIndex = value.indexOf(":");
  475. if (colonIndex === -1) {
  476. // 没有冒号,自动在倒数第二位前添加冒号
  477. if (value.length >= 3) {
  478. value = value.slice(0, -2) + ":" + value.slice(-2);
  479. }
  480. } else {
  481. // 有冒号,确保冒号后只有两位数字
  482. const parts = value.split(":");
  483. if (parts.length === 2) {
  484. const minutes = parts[0];
  485. let seconds = parts[1];
  486. // 限制秒数为两位
  487. if (seconds.length > 2) {
  488. seconds = seconds.slice(0, 2);
  489. }
  490. value = minutes + ":" + seconds;
  491. }
  492. }
  493. // 限制总长度(分钟最多4位 + 冒号 + 秒数2位 = 7位)
  494. if (value.length > 7) {
  495. value = value.slice(0, 7);
  496. }
  497. e.target.value = value;
  498. });
  499. }
  500. start() {
  501. if (!this.isRunning) {
  502. this.isRunning = true;
  503. this.timeInput.readOnly = true;
  504. this.startBtn.style.display = "none";
  505. this.pauseBtn.style.display = "inline-block";
  506. this.stopBtn.style.display = "inline-block";
  507. this.interval = setInterval(() => {
  508. this.remainingTime--;
  509. this.timeInput.value = dataManager.formatCountdown(
  510. this.remainingTime
  511. );
  512. this.updateProgress();
  513. if (this.remainingTime <= 0) {
  514. this.complete();
  515. }
  516. }, 1000);
  517. }
  518. }
  519. pause() {
  520. if (this.isRunning) {
  521. this.isRunning = false;
  522. clearInterval(this.interval);
  523. this.startBtn.style.display = "inline-block";
  524. this.pauseBtn.style.display = "none";
  525. }
  526. }
  527. stop() {
  528. this.isRunning = false;
  529. clearInterval(this.interval);
  530. // 将已进行的时间加入总学习时长
  531. const elapsed = this.totalTime - this.remainingTime;
  532. this.data.totalStudyTime += elapsed;
  533. dataManager.save(this.data);
  534. studyTimeManager.display.textContent = dataManager.formatTime(
  535. this.data.totalStudyTime
  536. );
  537. // 重置计时器
  538. this.remainingTime = this.totalTime;
  539. this.timeInput.value = dataManager.formatCountdown(
  540. this.remainingTime
  541. );
  542. this.timeInput.readOnly = false;
  543. this.updateProgress();
  544. this.startBtn.style.display = "inline-block";
  545. this.pauseBtn.style.display = "none";
  546. this.stopBtn.style.display = "none";
  547. }
  548. complete() {
  549. this.isRunning = false;
  550. clearInterval(this.interval);
  551. // 将全部时间加入总学习时长
  552. this.data.totalStudyTime += this.totalTime;
  553. dataManager.save(this.data);
  554. studyTimeManager.display.textContent = dataManager.formatTime(
  555. this.data.totalStudyTime
  556. );
  557. // 重置计时器
  558. this.remainingTime = this.totalTime;
  559. this.timeInput.value = dataManager.formatCountdown(
  560. this.remainingTime
  561. );
  562. this.timeInput.readOnly = false;
  563. this.updateProgress();
  564. this.startBtn.style.display = "inline-block";
  565. this.pauseBtn.style.display = "none";
  566. this.stopBtn.style.display = "none";
  567. // 发送通知
  568. this.sendNotification("番茄钟完成", "恭喜你完成了一个番茄钟!");
  569. }
  570. updateProgress() {
  571. const progress =
  572. (this.totalTime - this.remainingTime) / this.totalTime;
  573. const offset = this.circumference - progress * this.circumference;
  574. this.progressCircle.style.strokeDashoffset = offset;
  575. }
  576. sendNotification(title, message) {
  577. if ("Notification" in window) {
  578. if (Notification.permission === "granted") {
  579. new Notification(title, { body: message });
  580. } else if (Notification.permission !== "denied") {
  581. Notification.requestPermission().then((permission) => {
  582. if (permission === "granted") {
  583. new Notification(title, { body: message });
  584. }
  585. });
  586. }
  587. }
  588. // 播放声音
  589. const audio = new Audio(
  590. "data:audio/wav;base64,UklGRnoGAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQoGAACBhYqFbF1fdJivrJBhNjVgodDbq2EcBj+a2/LDciUFLIHO8tiJNwgZaLvt559NEAxQp+PwtmMcBjiR1/LMeSwFJHfH8N2QQAoUXrTp66hVFApGn+DyvmwhBSuBzvLZiTYIG2m98OScTgwOUarm7blmFgU7k9n1unEiBC13yO/eizEIHWq+8+OWT"
  591. );
  592. audio.play();
  593. }
  594. }
  595. // 空闲钟控制器
  596. class IdleTimer {
  597. constructor(element) {
  598. this.element = element;
  599. this.timeInput = element.querySelector(".time-input");
  600. this.startBtn = element.querySelector(".start-btn");
  601. this.pauseBtn = element.querySelector(".pause-btn");
  602. this.stopBtn = element.querySelector(".stop-btn");
  603. this.progressBar = element.querySelector(
  604. ".linear-progress .progress"
  605. );
  606. this.data = dataManager.init();
  607. this.totalTime = this.data.idleTime;
  608. this.remainingTime = this.totalTime;
  609. this.isRunning = false;
  610. this.interval = null;
  611. this.init();
  612. }
  613. init() {
  614. this.timeInput.value = dataManager.formatCountdown(
  615. this.remainingTime
  616. );
  617. this.updateProgress();
  618. // 确保初始状态下输入框可编辑
  619. this.timeInput.readOnly = false;
  620. // 事件监听
  621. this.startBtn.addEventListener("click", () => this.start());
  622. this.pauseBtn.addEventListener("click", () => this.pause());
  623. this.stopBtn.addEventListener("click", () => this.stop());
  624. // 允许编辑时间
  625. this.timeInput.addEventListener("blur", () => {
  626. if (!this.isRunning) {
  627. const timeParts = this.timeInput.value.split(":");
  628. if (timeParts.length === 2) {
  629. const minutes = parseInt(timeParts[0], 10);
  630. const seconds = parseInt(timeParts[1], 10);
  631. // 验证输入
  632. if (
  633. !isNaN(minutes) &&
  634. !isNaN(seconds) &&
  635. minutes >= 0 &&
  636. minutes <= 1440 &&
  637. seconds >= 0 &&
  638. seconds <= 59
  639. ) {
  640. this.totalTime = minutes * 60 + seconds;
  641. this.remainingTime = this.totalTime;
  642. this.data.idleTime = this.totalTime;
  643. dataManager.save(this.data);
  644. this.updateProgress();
  645. } else {
  646. // 输入无效,重置为之前的有效值
  647. this.timeInput.value = dataManager.formatCountdown(
  648. this.remainingTime
  649. );
  650. }
  651. } else {
  652. // 格式无效,重置为之前的有效值
  653. this.timeInput.value = dataManager.formatCountdown(
  654. this.remainingTime
  655. );
  656. }
  657. }
  658. });
  659. // 限制输入格式
  660. this.timeInput.addEventListener("input", (e) => {
  661. let value = e.target.value;
  662. // 只允许数字和冒号
  663. value = value.replace(/[^0-9:]/g, "");
  664. // 处理冒号位置
  665. const colonIndex = value.indexOf(":");
  666. if (colonIndex === -1) {
  667. // 没有冒号,自动在倒数第二位前添加冒号
  668. if (value.length >= 3) {
  669. value = value.slice(0, -2) + ":" + value.slice(-2);
  670. }
  671. } else {
  672. // 有冒号,确保冒号后只有两位数字
  673. const parts = value.split(":");
  674. if (parts.length === 2) {
  675. const minutes = parts[0];
  676. let seconds = parts[1];
  677. // 限制秒数为两位
  678. if (seconds.length > 2) {
  679. seconds = seconds.slice(0, 2);
  680. }
  681. value = minutes + ":" + seconds;
  682. }
  683. }
  684. // 限制总长度(分钟最多4位 + 冒号 + 秒数2位 = 7位)
  685. if (value.length > 7) {
  686. value = value.slice(0, 7);
  687. }
  688. e.target.value = value;
  689. });
  690. }
  691. start() {
  692. if (!this.isRunning) {
  693. this.isRunning = true;
  694. this.timeInput.readOnly = true;
  695. this.startBtn.style.display = "none";
  696. this.pauseBtn.style.display = "inline-block";
  697. this.stopBtn.style.display = "inline-block";
  698. this.interval = setInterval(() => {
  699. this.remainingTime--;
  700. this.timeInput.value = dataManager.formatCountdown(
  701. this.remainingTime
  702. );
  703. this.updateProgress();
  704. if (this.remainingTime <= 0) {
  705. this.complete();
  706. }
  707. }, 1000);
  708. }
  709. }
  710. pause() {
  711. if (this.isRunning) {
  712. this.isRunning = false;
  713. clearInterval(this.interval);
  714. this.startBtn.style.display = "inline-block";
  715. this.pauseBtn.style.display = "none";
  716. }
  717. }
  718. stop() {
  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. complete() {
  733. this.isRunning = false;
  734. clearInterval(this.interval);
  735. // 重置计时器
  736. this.remainingTime = this.totalTime;
  737. this.timeInput.value = dataManager.formatCountdown(
  738. this.remainingTime
  739. );
  740. this.timeInput.readOnly = false;
  741. this.updateProgress();
  742. this.startBtn.style.display = "inline-block";
  743. this.pauseBtn.style.display = "none";
  744. this.stopBtn.style.display = "none";
  745. // 发送通知
  746. this.sendNotification("空闲时间结束", "休息时间结束了!");
  747. }
  748. updateProgress() {
  749. const progress =
  750. (this.totalTime - this.remainingTime) / this.totalTime;
  751. this.progressBar.style.width = `${progress * 100}%`;
  752. }
  753. sendNotification(title, message) {
  754. if ("Notification" in window) {
  755. if (Notification.permission === "granted") {
  756. new Notification(title, { body: message });
  757. } else if (Notification.permission !== "denied") {
  758. Notification.requestPermission().then((permission) => {
  759. if (permission === "granted") {
  760. new Notification(title, { body: message });
  761. }
  762. });
  763. }
  764. }
  765. // 播放声音
  766. const audio = new Audio(
  767. "data:audio/wav;base64,UklGRnoGAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQoGAACBhYqFbF1fdJivrJBhNjVgodDbq2EcBj+a2/LDciUFLIHO8tiJNwgZaLvt559NEAxQp+PwtmMcBjiR1/LMeSwFJHfH8N2QQAoUXrTp66hVFApGn+DyvmwhBSuBzvLZiTYIG2m98OScTgwOUarm7blmFgU7k9n1unEiBC13yO/eizEIHWq+8+OWT"
  768. );
  769. audio.play();
  770. }
  771. }
  772. // 总学习时长控制器
  773. class StudyTimeManager {
  774. constructor() {
  775. this.display = document.querySelector(".study-time-display");
  776. this.clearBtn = document.querySelector(".clear-btn");
  777. this.confirmBtn = document.querySelector(".confirm-btn");
  778. this.cancelBtn = document.querySelector(".cancel-btn");
  779. this.data = dataManager.init();
  780. this.init();
  781. }
  782. init() {
  783. // 初始化显示
  784. this.display.textContent = dataManager.formatTime(
  785. this.data.totalStudyTime
  786. );
  787. // 事件监听
  788. this.clearBtn.addEventListener("click", () =>
  789. this.showConfirmDialog()
  790. );
  791. this.confirmBtn.addEventListener("click", () =>
  792. this.clearStudyTime()
  793. );
  794. this.cancelBtn.addEventListener("click", () =>
  795. this.hideConfirmDialog()
  796. );
  797. }
  798. showConfirmDialog() {
  799. // 显示确认和取消按钮,隐藏清空按钮
  800. this.clearBtn.style.display = "none";
  801. this.confirmBtn.style.display = "inline-block";
  802. this.cancelBtn.style.display = "inline-block";
  803. }
  804. hideConfirmDialog() {
  805. // 隐藏确认和取消按钮,显示清空按钮
  806. this.clearBtn.style.display = "inline-block";
  807. this.confirmBtn.style.display = "none";
  808. this.cancelBtn.style.display = "none";
  809. }
  810. clearStudyTime() {
  811. // 清空学习时长
  812. this.data.totalStudyTime = 0;
  813. dataManager.save(this.data);
  814. this.display.textContent = dataManager.formatTime(0);
  815. // 隐藏确认和取消按钮,显示清空按钮
  816. this.hideConfirmDialog();
  817. }
  818. }
  819. // 初始化应用
  820. const data = dataManager.init();
  821. const studyTimeManager = new StudyTimeManager();
  822. // 初始化番茄钟
  823. const pomodoroElement = document.querySelector(".pomodoro");
  824. const pomodoroTimer = new PomodoroTimer(pomodoroElement);
  825. // 初始化空闲钟
  826. const idleElement = document.querySelector(".idle");
  827. const idleTimer = new IdleTimer(idleElement);
  828. // PWA 相关功能
  829. // 注册 Service Worker
  830. if ("serviceWorker" in navigator) {
  831. window.addEventListener("load", () => {
  832. navigator.serviceWorker
  833. .register("./sw.js")
  834. .then((registration) => {
  835. console.log("Service Worker 注册成功:", registration.scope);
  836. // 检查是否有更新
  837. registration.addEventListener("updatefound", () => {
  838. const newWorker = registration.installing;
  839. newWorker.addEventListener("statechange", () => {
  840. if (
  841. newWorker.state === "installed" &&
  842. navigator.serviceWorker.controller
  843. ) {
  844. // 有新版本可用
  845. console.log("发现新版本的应用");
  846. // 这里可以添加更新提示
  847. }
  848. });
  849. });
  850. })
  851. .catch((error) => {
  852. console.error("Service Worker 注册失败:", error);
  853. });
  854. });
  855. }
  856. // PWA 安装提示 - 使用图标而非文字
  857. let deferredPrompt;
  858. const installPromptElement = document.createElement("div");
  859. installPromptElement.id = "install-prompt";
  860. installPromptElement.style.cssText = `
  861. position: fixed;
  862. bottom: 20px;
  863. left: 50%;
  864. transform: translateX(-50%);
  865. background-color: var(--bg-secondary);
  866. color: var(--text-primary);
  867. padding: 16px;
  868. border-radius: 8px;
  869. box-shadow: 0 4px 12px var(--shadow-primary);
  870. display: none;
  871. align-items: center;
  872. gap: 16px;
  873. z-index: 1000;
  874. animation: slideUp 0.3s ease-out;
  875. `;
  876. // 使用图标表示安装应用
  877. const installIcon = document.createElement("span");
  878. installIcon.textContent = "📱";
  879. installIcon.style.cssText = "font-size: 24px;";
  880. installPromptElement.appendChild(installIcon);
  881. const installButton = document.createElement("button");
  882. installButton.textContent = "⬇️";
  883. installButton.className = "btn install-btn";
  884. installButton.title = "安装应用";
  885. installButton.style.cssText = `
  886. background-color: var(--text-secondary);
  887. color: white;
  888. border: none;
  889. padding: 8px 16px;
  890. border-radius: 50%;
  891. cursor: pointer;
  892. font-size: 20px;
  893. width: 44px;
  894. height: 44px;
  895. display: flex;
  896. align-items: center;
  897. justify-content: center;
  898. `;
  899. installPromptElement.appendChild(installButton);
  900. const dismissButton = document.createElement("button");
  901. dismissButton.textContent = "❌";
  902. dismissButton.className = "btn dismiss-btn";
  903. dismissButton.title = "忽略";
  904. dismissButton.style.cssText = `
  905. background: none;
  906. border: none;
  907. font-size: 18px;
  908. cursor: pointer;
  909. padding: 4px;
  910. color: var(--text-primary);
  911. `;
  912. installPromptElement.appendChild(dismissButton);
  913. // 添加动画样式
  914. const style = document.createElement("style");
  915. style.textContent = `
  916. @keyframes slideUp {
  917. from { transform: translateX(-50%) translateY(100px); opacity: 0; }
  918. to { transform: translateX(-50%) translateY(0); opacity: 1; }
  919. }
  920. @keyframes slideDown {
  921. from { transform: translateX(-50%) translateY(0); opacity: 1; }
  922. to { transform: translateX(-50%) translateY(100px); opacity: 0; }
  923. }
  924. .install-prompt-dismiss {
  925. animation: slideDown 0.3s ease-out forwards;
  926. }
  927. `;
  928. document.head.appendChild(style);
  929. document.body.appendChild(installPromptElement);
  930. // 监听安装提示事件
  931. window.addEventListener("beforeinstallprompt", (e) => {
  932. // 阻止默认的安装提示
  933. e.preventDefault();
  934. // 存储事件以便后续使用
  935. deferredPrompt = e;
  936. // 检查用户是否已经安装了应用
  937. if (!localStorage.getItem("pwa-install-dismissed")) {
  938. // 显示安装提示
  939. installPromptElement.style.display = "flex";
  940. }
  941. });
  942. // 安装按钮点击事件
  943. installButton.addEventListener("click", async () => {
  944. if (deferredPrompt) {
  945. // 显示安装提示
  946. deferredPrompt.prompt();
  947. // 等待用户响应
  948. const { outcome } = await deferredPrompt.userChoice;
  949. console.log(`用户${outcome === "accepted" ? "接受" : "拒绝"}了安装`);
  950. // 重置 deferredPrompt
  951. deferredPrompt = null;
  952. // 隐藏安装提示
  953. hideInstallPrompt();
  954. }
  955. });
  956. // 忽略按钮点击事件
  957. dismissButton.addEventListener("click", () => {
  958. hideInstallPrompt();
  959. // 记录用户已经忽略过安装提示
  960. localStorage.setItem("pwa-install-dismissed", "true");
  961. });
  962. // 隐藏安装提示的函数
  963. function hideInstallPrompt() {
  964. installPromptElement.classList.add("install-prompt-dismiss");
  965. setTimeout(() => {
  966. installPromptElement.style.display = "none";
  967. installPromptElement.classList.remove("install-prompt-dismiss");
  968. }, 300);
  969. }
  970. // 检查应用是否已经安装
  971. window.addEventListener("appinstalled", () => {
  972. console.log("应用已成功安装");
  973. hideInstallPrompt();
  974. localStorage.setItem("pwa-installed", "true");
  975. });
  976. // 处理应用启动时的 URL 参数
  977. function handleAppAction() {
  978. const urlParams = new URLSearchParams(window.location.search);
  979. const action = urlParams.get("action");
  980. if (action === "pomodoro") {
  981. // 自动开始番茄钟
  982. setTimeout(() => {
  983. if (!pomodoroTimer.isRunning) {
  984. pomodoroTimer.start();
  985. }
  986. }, 1000);
  987. } else if (action === "idle") {
  988. // 自动开始空闲钟
  989. setTimeout(() => {
  990. if (!idleTimer.isRunning) {
  991. idleTimer.start();
  992. }
  993. }, 1000);
  994. } else if (action === "stats") {
  995. // 高亮显示学习时长区域
  996. const studyTimeElement = document.querySelector(".study-time");
  997. studyTimeElement.style.animation = "pulse 2s ease-in-out 3";
  998. const pulseStyle = document.createElement("style");
  999. pulseStyle.textContent = `
  1000. @keyframes pulse {
  1001. 0%, 100% { transform: scale(1); }
  1002. 50% { transform: scale(1.05); }
  1003. }
  1004. `;
  1005. document.head.appendChild(pulseStyle);
  1006. setTimeout(() => {
  1007. studyTimeElement.style.animation = "";
  1008. }, 6000);
  1009. }
  1010. }
  1011. // 处理应用启动
  1012. handleAppAction();
  1013. // 请求通知权限
  1014. if ("Notification" in window && Notification.permission === "default") {
  1015. Notification.requestPermission();
  1016. }
  1017. // 处理页面可见性变化
  1018. let lastVisibleTime = Date.now();
  1019. document.addEventListener("visibilitychange", () => {
  1020. if (document.visibilityState === "visible") {
  1021. // 页面变为可见时,计算经过的时间并更新计时器
  1022. const now = Date.now();
  1023. const elapsedSeconds = Math.floor((now - lastVisibleTime) / 1000);
  1024. if (pomodoroTimer.isRunning) {
  1025. pomodoroTimer.remainingTime = Math.max(
  1026. 0,
  1027. pomodoroTimer.remainingTime - elapsedSeconds
  1028. );
  1029. pomodoroTimer.timeInput.value = dataManager.formatCountdown(
  1030. pomodoroTimer.remainingTime
  1031. );
  1032. pomodoroTimer.updateProgress();
  1033. if (pomodoroTimer.remainingTime <= 0) {
  1034. pomodoroTimer.complete();
  1035. }
  1036. }
  1037. if (idleTimer.isRunning) {
  1038. idleTimer.remainingTime = Math.max(
  1039. 0,
  1040. idleTimer.remainingTime - elapsedSeconds
  1041. );
  1042. idleTimer.timeInput.value = dataManager.formatCountdown(
  1043. idleTimer.remainingTime
  1044. );
  1045. idleTimer.updateProgress();
  1046. if (idleTimer.remainingTime <= 0) {
  1047. idleTimer.complete();
  1048. }
  1049. }
  1050. }
  1051. lastVisibleTime = Date.now();
  1052. });
  1053. // 页面关闭前提醒
  1054. window.addEventListener("beforeunload", (e) => {
  1055. if (pomodoroTimer.isRunning || idleTimer.isRunning) {
  1056. e.preventDefault();
  1057. e.returnValue = "您有正在运行的计时器,确定要离开吗?";
  1058. return e.returnValue;
  1059. }
  1060. });
  1061. </script>
  1062. </body>
  1063. </html>