优化:《我的归档》筛选功能

This commit is contained in:
2022-08-12 19:03:36 +08:00
parent 0696bd5295
commit 82a5a8207e
2 changed files with 112 additions and 63 deletions

View File

@@ -26,7 +26,7 @@
</span>
<span class="meta-content">
<span v-for="(category, index) in categories" :key="index">
<a :href="'/archives.html?category=' + category" target="_self">{{ category }}</a>
<a href="javascript:void(0);" @click="router.go('/archives.html?category=' + category)" target="_self" title="按分类归档">{{ category }}</a>
<span v-if="index != categories.length - 1">, </span>
</span>
</span>
@@ -37,7 +37,7 @@
</span>
<span class="meta-content">
<span v-for="(tag, index) in tags" :key="index">
<a :href="'/archives.html?tag=' + tag" target="_self">{{ tag }}</a>
<a href="javascript:void(0);" @click="router.go('/archives.html?tag=' + tag)" target="_self" title="按标签归档">{{ tag }}</a>
<span v-if="index != tags.length - 1">, </span>
</span>
</span>
@@ -47,7 +47,7 @@
<script lang="ts" setup>
import { reactive, toRefs } from 'vue'
import { useData } from 'vitepress'
import { useData, useRouter } from 'vitepress'
import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn'
import relativeTime from 'dayjs/plugin/relativeTime'
@@ -55,6 +55,8 @@ import relativeTime from 'dayjs/plugin/relativeTime'
dayjs.extend(relativeTime)
dayjs.locale('zh-cn')
const router = useRouter()
// 定义文章属性
const props = defineProps({
article: Object,

View File

@@ -1,32 +1,55 @@
<template>
<div class="timeline-wrap">
<div class="timeline-header" v-if="category">
<!-- 时间轴头部 -->
<div class="timeline-header">
<a-tag
v-if="$category"
class="content"
closable
@close="handleUncheck('/archives.html')"
@close="router.go('/archives.html')"
>
<template #icon>
<svg class="icon" role="img" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"><title>所属分类</title><path d="M928 444H820V330.4c0-17.7-14.3-32-32-32H473L355.7 186.2a8.15 8.15 0 0 0-5.5-2.2H96c-17.7 0-32 14.3-32 32v592c0 17.7 14.3 32 32 32h698c13 0 24.8-7.9 29.7-20l134-332c1.5-3.8 2.3-7.9 2.3-12 0-17.7-14.3-32-32-32zM136 256h188.5l119.6 114.4H748V444H238c-13 0-24.8 7.9-29.7 20L136 643.2V256z m635.3 512H159l103.3-256h612.4L771.3 768z"></path></svg>
</template>
{{ category }} ({{ $articleData.length }})
{{ $category }} ( {{ $articleData.length }} )
</a-tag>
</div>
<div class="timeline-header" v-else-if="tag">
<a-tag
v-else-if="$tag"
class="content"
closable
@close="handleUncheck('/archives.html')"
@close="router.go('/archives.html')"
>
<template #icon>
<svg class="icon" role="img" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"><title>标签列表</title><path d="M483.2 790.3L861.4 412c1.7-1.7 2.5-4 2.3-6.3l-25.5-301.4c-0.7-7.8-6.8-13.9-14.6-14.6L522.2 64.3c-2.3-0.2-4.7 0.6-6.3 2.3L137.7 444.8a8.03 8.03 0 0 0 0 11.3l334.2 334.2c3.1 3.2 8.2 3.2 11.3 0z m62.6-651.7l224.6 19 19 224.6L477.5 694 233.9 450.5l311.9-311.9z m60.16 186.23a48 48 0 1 0 67.88-67.89 48 48 0 1 0-67.88 67.89zM889.7 539.8l-39.6-39.5a8.03 8.03 0 0 0-11.3 0l-362 361.3-237.6-237a8.03 8.03 0 0 0-11.3 0l-39.6 39.5a8.03 8.03 0 0 0 0 11.3l243.2 242.8 39.6 39.5c3.1 3.1 8.2 3.1 11.3 0l407.3-406.6c3.1-3.1 3.1-8.2 0-11.3z"></path></svg>
</template>
{{ tag }} ({{ $articleData.length }})
{{ $tag }} ( {{ $articleData.length }} )
</a-tag>
<a-tag
v-else-if="$year"
class="content"
closable
@close="router.go('/archives.html')"
>
<template #icon>
<svg class="icon" role="img" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"><path d="M832 64H192c-17.7 0-32 14.3-32 32v832c0 17.7 14.3 32 32 32h640c17.7 0 32-14.3 32-32V96c0-17.7-14.3-32-32-32z m-260 72h96v209.9L621.5 312 572 347.4V136z m220 752H232V136h280v296.9c0 3.3 1 6.6 3 9.3a15.9 15.9 0 0 0 22.3 3.7l83.8-59.9 81.4 59.4c2.7 2 6 3.1 9.4 3.1 8.8 0 16-7.2 16-16V136h64v752z"></path></svg>
</template>
{{ $year }} ( {{ $articleData.length }} )
</a-tag>
<a-tag
v-else
class="content"
>
<template #icon>
<svg class="icon" role="img" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"><path d="M832 64H192c-17.7 0-32 14.3-32 32v832c0 17.7 14.3 32 32 32h640c17.7 0 32-14.3 32-32V96c0-17.7-14.3-32-32-32z m-260 72h96v209.9L621.5 312 572 347.4V136z m220 752H232V136h280v296.9c0 3.3 1 6.6 3 9.3a15.9 15.9 0 0 0 22.3 3.7l83.8-59.9 81.4 59.4c2.7 2 6 3.1 9.4 3.1 8.8 0 16-7.2 16-16V136h64v752z"></path></svg>
</template>
{{ articleData.length }} 未完待续······
</a-tag>
</div>
<!-- 时间轴主体 -->
<div class="timeline-item" v-for="(item, year) in archiveData">
<div class="year">
<img class="chinese-zodiac" :src="'/img/svg/chinese-zodiac/' + getChineseZodiac(year.replace('年', '')) + '.svg'" :title="getChineseZodiac(year.replace('年', ''))" alt="生肖">
<img class="chinese-zodiac" @click="router.go('/archives.html?year=' + year.replace('年', ''))" :src="'/img/svg/chinese-zodiac/' + getChineseZodiac(year.replace('年', '')) + '.svg'" :title="getChineseZodiac(year.replace('年', ''))" alt="生肖">
<span>{{ year }}</span>
</div>
<div class="timeline-item-content">
@@ -36,13 +59,15 @@
</span>
<div class="articles">
<span v-for="article in articles" class="article">
<svg v-if="article.categories.includes('Bug万象集')" role="img" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" class="arco-icon arco-icon-bug" stroke-width="4" stroke-linecap="butt" stroke-linejoin="miter" style="color: #f53f3f;"><title>Bug万象集</title><path d="M24 42c-6.075 0-11-4.925-11-11V18h22v13c0 6.075-4.925 11-11 11Zm0 0V23m11 4h8M5 27h8M7 14a4 4 0 0 0 4 4h26a4 4 0 0 0 4-4m0 28v-.5a6.5 6.5 0 0 0-6.5-6.5M7 42v-.5a6.5 6.5 0 0 1 6.5-6.5M17 14a7 7 0 1 1 14 0"></path></svg>
<svg v-else-if="article.categories.includes('杂碎逆袭史')" role="img" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" class="arco-icon arco-icon-bulb" stroke-width="4" stroke-linecap="butt" stroke-linejoin="miter" style="color: #ff7d00;"><title>杂碎逆袭史</title><path d="M17 42h14m6-24c0 2.823-.9 5.437-2.43 7.568-1.539 2.147-3.185 4.32-3.77 6.897l-.623 2.756A1 1 0 0 1 29.2 36H18.8a1 1 0 0 1-.976-.779l-.624-2.756c-.584-2.576-2.23-4.75-3.77-6.897A12.94 12.94 0 0 1 11 18c0-7.18 5.82-13 13-13s13 5.82 13 13Z"></path></svg>
<svg v-else-if="article.categories.includes('方案春秋志')" role="img" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" class="arco-icon arco-icon-code" stroke-width="4" stroke-linecap="butt" stroke-linejoin="miter" style="color: #165dff;"><title>方案春秋志</title><path d="M16.734 12.686 5.42 24l11.314 11.314m14.521-22.628L42.57 24 31.255 35.314M27.2 6.28l-6.251 35.453"></path></svg>
<svg v-else role="img" viewBox="0 0 48 48" fill="none" stroke="currentColor" xmlns="http://www.w3.org/2000/svg" class="arco-icon arco-icon-bookmark" stroke-width="4" stroke-linecap="butt" stroke-linejoin="miter" style="color: #00b42a;"><path d="M16 16h16M16 24h8"></path><path d="M24 41H8V6h32v17"></path><path d="M30 29h11v13l-5.5-3.5L30 42V29Z"></path></svg>
<svg v-if="article.categories.includes('Bug万象集')" @click="router.go('/archives.html?category=' + article.categories[0])" role="img" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" class="arco-icon arco-icon-bug" stroke-width="4" stroke-linecap="butt" stroke-linejoin="miter" style="color: #f53f3f;"><title>Bug万象集</title><path d="M24 42c-6.075 0-11-4.925-11-11V18h22v13c0 6.075-4.925 11-11 11Zm0 0V23m11 4h8M5 27h8M7 14a4 4 0 0 0 4 4h26a4 4 0 0 0 4-4m0 28v-.5a6.5 6.5 0 0 0-6.5-6.5M7 42v-.5a6.5 6.5 0 0 1 6.5-6.5M17 14a7 7 0 1 1 14 0"></path></svg>
<svg v-else-if="article.categories.includes('杂碎逆袭史')" @click="router.go('/archives.html?category=' + article.categories[0])" role="img" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" class="arco-icon arco-icon-bulb" stroke-width="4" stroke-linecap="butt" stroke-linejoin="miter" style="color: #ff7d00;"><title>杂碎逆袭史</title><path d="M17 42h14m6-24c0 2.823-.9 5.437-2.43 7.568-1.539 2.147-3.185 4.32-3.77 6.897l-.623 2.756A1 1 0 0 1 29.2 36H18.8a1 1 0 0 1-.976-.779l-.624-2.756c-.584-2.576-2.23-4.75-3.77-6.897A12.94 12.94 0 0 1 11 18c0-7.18 5.82-13 13-13s13 5.82 13 13Z"></path></svg>
<svg v-else-if="article.categories.includes('方案春秋志')" @click="router.go('/archives.html?category=' + article.categories[0])" role="img" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg" stroke="currentColor" class="arco-icon arco-icon-code" stroke-width="4" stroke-linecap="butt" stroke-linejoin="miter" style="color: #165dff;"><title>方案春秋志</title><path d="M16.734 12.686 5.42 24l11.314 11.314m14.521-22.628L42.57 24 31.255 35.314M27.2 6.28l-6.251 35.453"></path></svg>
<svg v-else @click="router.go('/archives.html?category=' + article.categories[0])" role="img" viewBox="0 0 48 48" fill="none" stroke="currentColor" xmlns="http://www.w3.org/2000/svg" class="arco-icon arco-icon-bookmark" stroke-width="4" stroke-linecap="butt" stroke-linejoin="miter" style="color: #00b42a;"><path d="M16 16h16M16 24h8"></path><path d="M24 41H8V6h32v17"></path><path d="M30 29h11v13l-5.5-3.5L30 42V29Z"></path></svg>
<a :href="article.path" class="title" target="_blank">{{ article.title }}</a>
<br>
<!-- <span>{{ formatDate(article.date) }}</span> -->
<ArticleMetadata :article="article" :showCategory="false" />
</span>
</div>
@@ -54,53 +79,81 @@
</template>
<script lang="ts" setup>
import { computed } from 'vue'
import { computed, watchEffect } from 'vue'
import { useRouter } from 'vitepress'
import articleData from '../../../../../article-data.json'
import { formatDate, getQueryParam } from '../../utils.ts'
const $articleData = []
const router = useRouter()
// 如果URL路径有category或tag参数, 默认筛选出指定category或tag
// 例如: /archives.html?category=Bug万象集
// 例如: /archives.html?tag=JVM
let category = getQueryParam('category')
let tag = getQueryParam('tag')
if (category && category.trim() != '') {
for (let i = 0; i < articleData.length; i++) {
let article = articleData[i]
if (article.categories && article.categories.includes(category)) {
$articleData.push(article)
// 文章原始数据和归档数据
let $articleData
let archiveData
// 要筛选的分类、标签、年份
let $category
let $tag
let $year
initTimeline()
/**
* 初始化时间轴
*/
function initTimeline() {
$articleData = []
archiveData = {}
// 如果URL路径有category或tag或year参数, 默认筛选出指定category或tag或year的文章数据
// 例如: /archives.html?category=Bug万象集
// 例如: /archives.html?tag=JVM
// 例如: /archives.html?year=2020
$category = getQueryParam('category')
$tag = getQueryParam('tag')
$year = getQueryParam('year')
if ($category && $category.trim() != '') {
for (let i = 0; i < articleData.length; i++) {
let article = articleData[i]
if (article.categories && article.categories.includes($category)) {
$articleData.push(article)
}
}
}
} else if (tag && tag.trim() != '') {
for (let i = 0; i < articleData.length; i++) {
let article = articleData[i]
if (article.tags && article.tags.includes(tag)) {
$articleData.push(article)
} else if ($tag && $tag.trim() != '') {
for (let i = 0; i < articleData.length; i++) {
let article = articleData[i]
if (article.tags && article.tags.includes($tag)) {
$articleData.push(article)
}
}
} else if ($year && $year.trim() != '') {
for (let i = 0; i < articleData.length; i++) {
let article = articleData[i]
if (article.date && new Date(article.date).getFullYear() == $year) {
$articleData.push(article)
}
}
} else {
$articleData.push(...articleData)
}
} else {
$articleData.push(...articleData)
}
// 文章数据归档处理
// 1.对文章数据进行降序排序
$articleData.sort((a, b) => b.date.localeCompare(a.date))
// 2.按年、月进行归档
for (let i = 0; i < $articleData.length; i++) {
const article = $articleData[i]
let year = (new Date(article.date).getFullYear()) + '年'
let month = (new Date(article.date).getMonth() + 1) + '月'
// 对文章数据进行降序排序
$articleData.sort((a, b) => b.date.localeCompare(a.date))
const archiveData = {}
for (let i = 0; i < $articleData.length; i++) {
const article = $articleData[i]
let year = (new Date(article.date).getFullYear()) + '年'
let month = (new Date(article.date).getMonth() + 1) + '月'
if (!archiveData[year]) {
archiveData[year] = {}
}
if (!(archiveData[year][month])) {
archiveData[year][month] = []
}
archiveData[year][month].push(article)
if (!archiveData[year]) {
archiveData[year] = {}
}
if (!(archiveData[year][month])) {
archiveData[year][month] = []
}
archiveData[year][month].push(article)
}
}
/**
@@ -134,14 +187,6 @@ function getChineseZodiac(year) {
return 'goat'
}
}
/**
* 取消选中
*/
const handleUncheck = (url) => {
location.href = url
}
</script>
<style scoped>
@@ -155,7 +200,7 @@ const handleUncheck = (url) => {
}
.timeline-wrap {
margin-top: 24px;
margin-top: 18px;
word-break: break-all;
}
@@ -202,6 +247,7 @@ const handleUncheck = (url) => {
background: #fff;
border: 1px solid #84b9e5;
border-radius: 50%;
cursor: pointer;
}
.timeline-wrap .timeline-item .timeline-item-time {
@@ -244,6 +290,7 @@ const handleUncheck = (url) => {
background: #fff;
border: 1px solid #84b9e5;
border-radius: 50%;
cursor: pointer;
}
.timeline-wrap .timeline-item .articles .article span {