Skip to content

Commit a9225cb

Browse files
committed
refactor(App): remove unused color definitions and implement HSV color generation for timers
修复某些情况下停止计时会导致数据丢失或者残留的问题
1 parent f524c4c commit a9225cb

File tree

3 files changed

+30
-35
lines changed

3 files changed

+30
-35
lines changed

dist/assets/index-ivee2FPd.js dist/assets/index-BQaDE6DD.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<link rel="icon" href="./favicon.ico">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
77
<title>Vite App</title>
8-
<script type="module" crossorigin src="./assets/index-ivee2FPd.js"></script>
8+
<script type="module" crossorigin src="./assets/index-BQaDE6DD.js"></script>
99
<link rel="stylesheet" crossorigin href="./assets/index-DTGrfxsC.css">
1010
</head>
1111
<body>

src/App.vue

+26-31
Original file line numberDiff line numberDiff line change
@@ -247,30 +247,6 @@ export default {
247247
isDragging: false,
248248
lastClientX: 0,
249249
scrollLeft: 0,
250-
taskColors: {
251-
colorList: [
252-
'#409EFF', // 蓝色
253-
'#67C23A', // 绿色
254-
'#E6A23C', // 黄色
255-
'#F56C6C', // 红色
256-
'#626aef', // 靛蓝
257-
'#6f7ad3', // 紫色
258-
'#5cb87a', // 浅绿
259-
'#ff9f7f' // 橙色
260-
]
261-
},
262-
standardColors: [
263-
'#409EFF', // 蓝色
264-
'#67C23A', // 绿色
265-
'#E6A23C', // 黄色
266-
'#F56C6C', // 红色
267-
'#909399', // 灰色
268-
'#626aef', // 靛蓝
269-
'#6f7ad3', // 紫色
270-
'#1989fa', // 浅蓝
271-
'#5cb87a', // 浅绿
272-
'#ff9f7f' // 橙色
273-
],
274250
timerInterval: null,
275251
formattedTimeBlocks: {},
276252
editDialogVisible: false,
@@ -385,7 +361,29 @@ export default {
385361
return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
386362
},
387363
getRandomColor() {
388-
return this.taskColors.colorList[Math.floor(Math.random() * this.taskColors.colorList.length)];
364+
// 生成随机的 H (0-360), S (50-100), V (50-100)
365+
let h = Math.floor(Math.random() * 361);
366+
let s = Math.floor(Math.random() * 51) + 45;
367+
//75~95
368+
let v = Math.floor(Math.random() * 21) + 75;
369+
370+
// 将 HSV 转换为 RGB
371+
let rgb = this.hsvToRgb(h, s, v);
372+
373+
// 返回 RGB 颜色值
374+
return `rgb(${rgb.r}, ${rgb.g}, ${rgb.b})`;
375+
},
376+
377+
hsvToRgb(h, s, v) {
378+
s /= 100;
379+
v /= 100;
380+
let k = (n) => (n + h / 60) % 6;
381+
let f = (n) => v - v * s * Math.max(Math.min(k(n), 4 - k(n), 1), 0);
382+
return {
383+
r: Math.round(f(5) * 255),
384+
g: Math.round(f(3) * 255),
385+
b: Math.round(f(1) * 255)
386+
};
389387
},
390388
startTimer(taskId) {
391389
const task = this.tasks.find(t => t.id === taskId);
@@ -402,24 +400,21 @@ export default {
402400
stopTimer(taskId) {
403401
const task = this.tasks.find(t => t.id === taskId);
404402
const description = this.taskDescriptions[taskId]?.trim();
405-
406403
if (task) {
407404
const timer = task.timers.find(t => t.end === null);
408405
if (timer) {
409-
timer.end = new Date();
410-
timer.description = description;
411-
timer.color = this.getRandomColor(); // 结束时分配固定颜色
412-
413406
const duration = (timer.end - timer.start) / 1000; // Duration in seconds
414407
if (duration < 10) {
415408
ElMessage.warning('时间不足10秒,计时已丢弃');
409+
task.timers = task.timers.filter(t => t !== timer); // Remove the timer
416410
this.saveToStorage(); // 保存到本地存储以保持计时信息
417411
} else {
418412
if (!description) {
419413
ElMessage.error('请先填写任务说明再结束计时');
420-
task.timers = task.timers.filter(t => t !== timer); // Remove the timer
421414
return;
422415
}
416+
timer.end = new Date();
417+
timer.description = description;
423418
ElMessage.success('计时已结束');
424419
this.saveToStorage(); // 保存到本地存储以保持颜色信息
425420
// Only auto-export if enabled

0 commit comments

Comments
 (0)