Skip to content

Commit 404e915

Browse files
committed
feat: 优化日期跳转体验
1 parent 407a19d commit 404e915

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

src/App.vue

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<script setup lang="ts">
2+
import { start } from 'node:repl'
23
import LoadingIndicator from '@/components/common/LoadingIndicator.vue'
34
import Switch from '@/components/common/Switch.vue'
45
import Header from '@/components/Header.vue'
@@ -136,6 +137,21 @@ const newsDataSorted = computed(() => {
136137
return data as NewsData[]
137138
})
138139
140+
const dateRange = computed(() => {
141+
const range = { min: '', max: '' }
142+
if (newsDataSorted.value.length) {
143+
if (sortBy.value === 'asc') {
144+
range.min = formatTime(newsDataSorted.value[0].startTime, true)
145+
range.max = formatTime(newsDataSorted.value[newsDataSorted.value.length - 1].startTime, true)
146+
}
147+
else {
148+
range.min = formatTime(newsDataSorted.value[newsDataSorted.value.length - 1].startTime, true)
149+
range.max = formatTime(newsDataSorted.value[0].startTime, true)
150+
}
151+
}
152+
return range
153+
})
154+
139155
onMounted(() => {
140156
document.addEventListener('click', (event) => {
141157
if (showSetting.value) {
@@ -350,6 +366,7 @@ function handleScrollByDate() {
350366
<div class="my-2 flex items-center">
351367
<input
352368
v-model="jumpDate"
369+
v-bind="dateRange"
353370
type="date"
354371
class="rounded-md border border-black/20 bg-transparent px-1 transition-colors hover:border-blue-500"
355372
>

src/components/NewsList.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { ITEM_GAP, SHADOW_ITEM, VISIT_PERSIST_KEY } from '@/constants'
44
import { state } from '@/state'
55
import { limitSetSize } from '@/utils'
66
import { useElementBounding, useElementSize, useThrottle } from '@vueuse/core'
7-
import { useToast } from 'vue-toastification'
87
98
const props = defineProps<{
109
config: NewsItemConfig
@@ -50,9 +49,15 @@ function scrollByDate(date: string) {
5049
let target
5150
if (props.sortBy === 'desc') {
5251
target = newsList.value.find(news => new Date(news.startTime) <= new Date(`${date} 23:59:59`))
52+
if (!target) {
53+
target = newsList.value[newsList.value.length - 1]
54+
}
5355
}
5456
else {
5557
target = newsList.value.find(news => new Date(news.startTime) >= new Date(`${date} 00:00:00`))
58+
if (!target) {
59+
target = newsList.value[newsList.value.length - 1]
60+
}
5661
}
5762
if (target) {
5863
const containerTop = containerRef.value?.offsetTop || 0

src/utils/time.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
export function formatTime(timestamp: number) {
2-
if (timestamp.toString().length === 10)
3-
timestamp *= 1000
4-
const date = new Date(timestamp)
1+
export function formatTime(time: number | string, dateOnly: boolean = false) {
2+
if (typeof time === 'number') {
3+
if (time.toString().length === 10)
4+
time *= 1000
5+
}
6+
const date = new Date(time)
57
const year = date.getFullYear()
6-
const month = date.getMonth() + 1
7-
const day = date.getDate()
8+
const month = String(date.getMonth() + 1).padStart(2, '0')
9+
const day = String(date.getDate()).padStart(2, '0')
10+
if (dateOnly) {
11+
return `${year}-${month}-${day}`
12+
}
813
const hour = String(date.getHours()).padStart(2, '0')
914
const minute = String(date.getMinutes()).padStart(2, '0')
1015
const second = String(date.getSeconds()).padStart(2, '0')

0 commit comments

Comments
 (0)