mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-04-21 03:17:28 +08:00
Merge pull request #1139 from nagisa77/feature/fix-category-back-history
Feature/fix category back history
This commit is contained in:
@@ -21,7 +21,7 @@ const props = defineProps({
|
|||||||
const gotoCategory = async () => {
|
const gotoCategory = async () => {
|
||||||
if (!props.category) return
|
if (!props.category) return
|
||||||
const value = encodeURIComponent(props.category.id ?? props.category.name)
|
const value = encodeURIComponent(props.category.id ?? props.category.name)
|
||||||
await navigateTo({ path: '/', query: { category: value } }, { replace: true })
|
await navigateTo({ path: '/', query: { category: value } })
|
||||||
}
|
}
|
||||||
|
|
||||||
const isImageIcon = (icon) => {
|
const isImageIcon = (icon) => {
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ defineProps({
|
|||||||
|
|
||||||
const gotoTag = async (tag) => {
|
const gotoTag = async (tag) => {
|
||||||
const value = encodeURIComponent(tag.id ?? tag.name)
|
const value = encodeURIComponent(tag.id ?? tag.name)
|
||||||
await navigateTo({ path: '/', query: { tags: value } }, { replace: true })
|
await navigateTo({ path: '/', query: { tags: value } })
|
||||||
}
|
}
|
||||||
|
|
||||||
const isImageIcon = (icon) => {
|
const isImageIcon = (icon) => {
|
||||||
|
|||||||
@@ -357,13 +357,13 @@ const isImageIcon = (icon) => {
|
|||||||
|
|
||||||
const gotoCategory = (c) => {
|
const gotoCategory = (c) => {
|
||||||
const value = encodeURIComponent(c.id ?? c.name)
|
const value = encodeURIComponent(c.id ?? c.name)
|
||||||
navigateTo({ path: '/', query: { category: value } }, { replace: true })
|
navigateTo({ path: '/', query: { category: value } })
|
||||||
handleItemClick()
|
handleItemClick()
|
||||||
}
|
}
|
||||||
|
|
||||||
const gotoTag = (t) => {
|
const gotoTag = (t) => {
|
||||||
const value = encodeURIComponent(t.id ?? t.name)
|
const value = encodeURIComponent(t.id ?? t.name)
|
||||||
navigateTo({ path: '/', query: { tags: value } }, { replace: true })
|
navigateTo({ path: '/', query: { tags: value } })
|
||||||
handleItemClick()
|
handleItemClick()
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -170,9 +170,9 @@ watch(selected, (val) => {
|
|||||||
navigateTo(`/posts/${opt.postId}#comment-${opt.id}`, { replace: true })
|
navigateTo(`/posts/${opt.postId}#comment-${opt.id}`, { replace: true })
|
||||||
}
|
}
|
||||||
} else if (opt.type === 'category') {
|
} else if (opt.type === 'category') {
|
||||||
navigateTo({ path: '/', query: { category: opt.id } }, { replace: true })
|
navigateTo({ path: '/', query: { category: opt.id } })
|
||||||
} else if (opt.type === 'tag') {
|
} else if (opt.type === 'tag') {
|
||||||
navigateTo({ path: '/', query: { tags: opt.id } }, { replace: true })
|
navigateTo({ path: '/', query: { tags: opt.id } })
|
||||||
}
|
}
|
||||||
selected.value = null
|
selected.value = null
|
||||||
keyword.value = ''
|
keyword.value = ''
|
||||||
|
|||||||
@@ -202,6 +202,28 @@ const selectedTagsSet = (tags) => {
|
|||||||
.map((v) => (isNaN(v) ? v : Number(v)))
|
.map((v) => (isNaN(v) ? v : Number(v)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const normalizeCategoryFromQuery = (category) => {
|
||||||
|
if (category == null || category === '') return ''
|
||||||
|
const raw = Array.isArray(category) ? category[0] : category
|
||||||
|
const decoded = decodeURIComponent(raw)
|
||||||
|
return isNaN(decoded) ? decoded : Number(decoded)
|
||||||
|
}
|
||||||
|
|
||||||
|
const normalizeTagsFromQuery = (tags) => {
|
||||||
|
if (tags == null || tags === '') return []
|
||||||
|
const raw = Array.isArray(tags) ? tags.join(',') : tags
|
||||||
|
return raw
|
||||||
|
.split(',')
|
||||||
|
.filter((v) => v)
|
||||||
|
.map((v) => decodeURIComponent(v))
|
||||||
|
.map((v) => (isNaN(v) ? v : Number(v)))
|
||||||
|
}
|
||||||
|
|
||||||
|
const arraysShallowEqual = (a = [], b = []) => {
|
||||||
|
if (a.length !== b.length) return false
|
||||||
|
return a.every((v, idx) => String(v) === String(b[idx]))
|
||||||
|
}
|
||||||
|
|
||||||
/** 初始化:仅在客户端首渲染时根据路由同步一次 **/
|
/** 初始化:仅在客户端首渲染时根据路由同步一次 **/
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
const { category, tags } = route.query
|
const { category, tags } = route.query
|
||||||
@@ -239,6 +261,32 @@ watch(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 从筛选器变更回写到 URL,确保浏览器历史可回退到上一个筛选状态。
|
||||||
|
watch([selectedCategory, selectedTags], async ([category, tags]) => {
|
||||||
|
const routeCategory = normalizeCategoryFromQuery(route.query.category)
|
||||||
|
const routeTags = normalizeTagsFromQuery(route.query.tags)
|
||||||
|
|
||||||
|
const categoryChanged = String(category ?? '') !== String(routeCategory ?? '')
|
||||||
|
const tagsChanged = !arraysShallowEqual(tags || [], routeTags)
|
||||||
|
if (!categoryChanged && !tagsChanged) return
|
||||||
|
|
||||||
|
const nextQuery = { ...route.query }
|
||||||
|
|
||||||
|
if (category == null || category === '') {
|
||||||
|
delete nextQuery.category
|
||||||
|
} else {
|
||||||
|
nextQuery.category = encodeURIComponent(String(category))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Array.isArray(tags) || tags.length === 0) {
|
||||||
|
delete nextQuery.tags
|
||||||
|
} else {
|
||||||
|
nextQuery.tags = tags.map((v) => encodeURIComponent(String(v))).join(',')
|
||||||
|
}
|
||||||
|
|
||||||
|
await navigateTo({ path: '/', query: nextQuery })
|
||||||
|
})
|
||||||
|
|
||||||
/** 选项加载(分类/标签名称回填) **/
|
/** 选项加载(分类/标签名称回填) **/
|
||||||
const loadOptions = async () => {
|
const loadOptions = async () => {
|
||||||
if (selectedCategory.value && !isNaN(selectedCategory.value)) {
|
if (selectedCategory.value && !isNaN(selectedCategory.value)) {
|
||||||
|
|||||||
@@ -643,7 +643,7 @@ const sendMessage = async () => {
|
|||||||
|
|
||||||
const gotoTag = (tag) => {
|
const gotoTag = (tag) => {
|
||||||
const value = encodeURIComponent(tag.id ?? tag.name)
|
const value = encodeURIComponent(tag.id ?? tag.name)
|
||||||
navigateTo({ path: '/', query: { tags: value } }, { replace: true })
|
navigateTo({ path: '/', query: { tags: value } })
|
||||||
}
|
}
|
||||||
|
|
||||||
const init = async () => {
|
const init = async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user