mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-09 16:41:04 +08:00
Compare commits
15 Commits
codex/impr
...
codex/fix-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e96db5d0d6 | ||
|
|
1083c4241a | ||
|
|
1eeabab41a | ||
|
|
2b5f6f2208 | ||
|
|
bda377336d | ||
|
|
77507f7b18 | ||
|
|
a39f2f7c00 | ||
|
|
229439aa05 | ||
|
|
612881f1b1 | ||
|
|
05c7bc18d7 | ||
|
|
c68c5985f6 | ||
|
|
7d44791011 | ||
|
|
15b992b949 | ||
|
|
dc13b2941f | ||
|
|
13c250d392 |
@@ -100,18 +100,32 @@ public class TagController {
|
||||
)
|
||||
public List<TagDto> list(
|
||||
@RequestParam(value = "keyword", required = false) String keyword,
|
||||
@RequestParam(value = "page", required = false) Integer page,
|
||||
@RequestParam(value = "pageSize", required = false) Integer pageSize,
|
||||
@RequestParam(value = "limit", required = false) Integer limit
|
||||
) {
|
||||
List<Tag> tags = tagService.searchTags(keyword);
|
||||
List<Long> tagIds = tags.stream().map(Tag::getId).toList();
|
||||
Map<Long, Long> postCntByTagIds = postService.countPostsByTagIds(tagIds);
|
||||
if (postCntByTagIds == null) {
|
||||
postCntByTagIds = java.util.Collections.emptyMap();
|
||||
}
|
||||
Map<Long, Long> finalPostCntByTagIds = postCntByTagIds;
|
||||
List<TagDto> dtos = tags
|
||||
.stream()
|
||||
.map(t -> tagMapper.toDto(t, postCntByTagIds.getOrDefault(t.getId(), 0L)))
|
||||
.map(t -> tagMapper.toDto(t, finalPostCntByTagIds.getOrDefault(t.getId(), 0L)))
|
||||
.sorted((a, b) -> Long.compare(b.getCount(), a.getCount()))
|
||||
.collect(Collectors.toList());
|
||||
if (page != null && pageSize != null && page >= 0 && pageSize > 0) {
|
||||
int fromIndex = page * pageSize;
|
||||
if (fromIndex >= dtos.size()) {
|
||||
return java.util.Collections.emptyList();
|
||||
}
|
||||
int toIndex = Math.min(fromIndex + pageSize, dtos.size());
|
||||
return new java.util.ArrayList<>(dtos.subList(fromIndex, toIndex));
|
||||
}
|
||||
if (limit != null && limit > 0 && dtos.size() > limit) {
|
||||
return dtos.subList(0, limit);
|
||||
return new java.util.ArrayList<>(dtos.subList(0, limit));
|
||||
}
|
||||
return dtos;
|
||||
}
|
||||
|
||||
@@ -82,6 +82,7 @@ class TagControllerTest {
|
||||
t.setIcon("i2");
|
||||
t.setSmallIcon("s2");
|
||||
Mockito.when(tagService.searchTags(null)).thenReturn(List.of(t));
|
||||
Mockito.when(postService.countPostsByTagIds(List.of(2L))).thenReturn(java.util.Map.of());
|
||||
|
||||
mockMvc
|
||||
.perform(get("/api/tags"))
|
||||
@@ -93,6 +94,31 @@ class TagControllerTest {
|
||||
.andExpect(jsonPath("$[0].smallIcon").value("s2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listTagsWithPagination() throws Exception {
|
||||
Tag t1 = new Tag();
|
||||
t1.setId(1L);
|
||||
t1.setName("java");
|
||||
Tag t2 = new Tag();
|
||||
t2.setId(2L);
|
||||
t2.setName("spring");
|
||||
Mockito.when(tagService.searchTags(null)).thenReturn(List.of(t1, t2));
|
||||
Mockito.when(postService.countPostsByTagIds(List.of(1L, 2L))).thenReturn(
|
||||
java.util.Map.of(1L, 1L, 2L, 5L)
|
||||
);
|
||||
|
||||
mockMvc
|
||||
.perform(get("/api/tags").param("page", "1").param("pageSize", "1"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$", org.hamcrest.Matchers.hasSize(1)))
|
||||
.andExpect(jsonPath("$[0].id").value(1));
|
||||
|
||||
mockMvc
|
||||
.perform(get("/api/tags").param("page", "2").param("pageSize", "1"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$", org.hamcrest.Matchers.hasSize(0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateTag() throws Exception {
|
||||
Tag t = new Tag();
|
||||
|
||||
@@ -108,7 +108,6 @@ body {
|
||||
|
||||
.vditor-toolbar--pin {
|
||||
top: calc(var(--header-height) + 1px) !important;
|
||||
z-index: 20;
|
||||
}
|
||||
|
||||
.vditor-panel {
|
||||
@@ -134,26 +133,6 @@ body {
|
||||
animation: spin 1s linear infinite;
|
||||
}
|
||||
|
||||
/* .vditor {
|
||||
--textarea-background-color: transparent;
|
||||
border: none !important;
|
||||
box-shadow: none !important;
|
||||
}
|
||||
|
||||
.vditor-reset {
|
||||
color: var(--text-color);
|
||||
}
|
||||
|
||||
.vditor-toolbar {
|
||||
background: transparent !important;
|
||||
border: none !important;
|
||||
box-shadow: none !important;
|
||||
} */
|
||||
|
||||
/* .vditor-toolbar {
|
||||
position: relative !important;
|
||||
} */
|
||||
|
||||
/*************************
|
||||
* Markdown 渲染样式
|
||||
*************************/
|
||||
@@ -333,10 +312,6 @@ body {
|
||||
min-height: 100px;
|
||||
}
|
||||
|
||||
.vditor-toolbar {
|
||||
overflow-x: auto;
|
||||
}
|
||||
|
||||
.about-content h1,
|
||||
.info-content-text h1 {
|
||||
font-size: 20px;
|
||||
@@ -354,8 +329,8 @@ body {
|
||||
margin-bottom: 3px;
|
||||
}
|
||||
|
||||
.vditor-toolbar--pin {
|
||||
top: 0 !important;
|
||||
.vditor-panel {
|
||||
min-width: 330px;
|
||||
}
|
||||
|
||||
.about-content li,
|
||||
@@ -367,11 +342,6 @@ body {
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.vditor-panel {
|
||||
position: relative;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.d2h-file-name {
|
||||
font-size: 14px !important;
|
||||
}
|
||||
|
||||
@@ -1,18 +1,13 @@
|
||||
<template>
|
||||
<component
|
||||
:is="wrapperTag"
|
||||
:to="isLink ? resolvedLink : undefined"
|
||||
<NuxtLink
|
||||
:to="resolvedLink"
|
||||
class="base-user-avatar"
|
||||
:class="wrapperClass"
|
||||
:style="wrapperStyle"
|
||||
v-bind="wrapperAttrs"
|
||||
:role="isLink ? undefined : 'img'"
|
||||
:aria-label="altText"
|
||||
:title="altText"
|
||||
>
|
||||
<span class="base-user-avatar-backdrop" aria-hidden="true" />
|
||||
<BaseImage :src="currentSrc" :alt="altText" class="base-user-avatar-img" @error="onError" />
|
||||
</component>
|
||||
</NuxtLink>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
@@ -81,45 +76,15 @@ const sizeStyle = computed(() => {
|
||||
return { width: value, height: value }
|
||||
})
|
||||
|
||||
const accentHue = computed(() => {
|
||||
const seed = props.userId ?? props.alt
|
||||
const source = seed !== undefined && seed !== null ? String(seed) : ''
|
||||
if (!source) return 198
|
||||
let hash = 0
|
||||
for (let index = 0; index < source.length; index += 1) {
|
||||
hash = (hash << 5) - hash + source.charCodeAt(index)
|
||||
hash |= 0
|
||||
}
|
||||
return Math.abs(hash) % 360
|
||||
})
|
||||
|
||||
const accentStyles = computed(() => {
|
||||
const hue = accentHue.value
|
||||
return {
|
||||
'--avatar-accent': `hsl(${hue}, 74%, 54%)`,
|
||||
'--avatar-accent-light': `hsl(${hue}, 95%, 82%)`,
|
||||
'--avatar-accent-soft': `hsl(${hue}, 96%, 95%)`,
|
||||
'--avatar-accent-border': `hsla(${hue}, 70%, 48%, 0.28)`,
|
||||
'--avatar-accent-shadow': `hsla(${hue}, 68%, 36%, 0.2)`,
|
||||
}
|
||||
})
|
||||
|
||||
const wrapperStyle = computed(() => {
|
||||
const attrStyle = attrs.style
|
||||
return [accentStyles.value, sizeStyle.value, attrStyle]
|
||||
return [sizeStyle.value, attrStyle]
|
||||
})
|
||||
|
||||
const isLink = computed(() => !props.disableLink && !!resolvedLink.value)
|
||||
|
||||
const wrapperTag = computed(() => (isLink.value ? 'NuxtLink' : 'div'))
|
||||
|
||||
const wrapperClass = computed(() => [
|
||||
attrs.class,
|
||||
{ 'is-rounded': props.rounded, 'is-interactive': isLink.value },
|
||||
])
|
||||
const wrapperClass = computed(() => [attrs.class, { 'is-rounded': props.rounded }])
|
||||
|
||||
const wrapperAttrs = computed(() => {
|
||||
const { class: _class, style: _style, to: _to, href: _href, ...rest } = attrs
|
||||
const { class: _class, style: _style, ...rest } = attrs
|
||||
return rest
|
||||
})
|
||||
|
||||
@@ -132,26 +97,24 @@ function onError() {
|
||||
|
||||
<style scoped>
|
||||
.base-user-avatar {
|
||||
position: relative;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
overflow: hidden;
|
||||
border-radius: 16px;
|
||||
background: linear-gradient(
|
||||
140deg,
|
||||
var(--avatar-accent-soft, rgba(17, 182, 197, 0.12)) 0%,
|
||||
var(--avatar-accent-light, rgba(17, 182, 197, 0.22)) 100%
|
||||
);
|
||||
border: 1px solid var(--avatar-accent-border, rgba(17, 182, 197, 0.2));
|
||||
box-shadow:
|
||||
0 1px 2px rgba(15, 52, 67, 0.08),
|
||||
0 3px 8px var(--avatar-accent-shadow, rgba(17, 182, 197, 0.18));
|
||||
transition:
|
||||
transform 0.3s ease,
|
||||
box-shadow 0.3s ease,
|
||||
border-color 0.3s ease,
|
||||
background 0.3s ease;
|
||||
background-color: var(--avatar-placeholder-color, #f0f0f0);
|
||||
/* 先用box-sizing: border-box,保证加border后宽高不变,圆形不变形 */
|
||||
box-sizing: border-box;
|
||||
border: 1.5px solid var(--normal-border-color);
|
||||
transition: all 0.6s ease;
|
||||
}
|
||||
|
||||
.base-user-avatar:hover {
|
||||
box-shadow: 0 4px 24px rgba(0, 0, 0, 0.1);
|
||||
transform: scale(1.05);
|
||||
}
|
||||
|
||||
.base-user-avatar:active {
|
||||
box-shadow: 0 4px 24px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.base-user-avatar.is-rounded {
|
||||
@@ -162,54 +125,10 @@ function onError() {
|
||||
border-radius: 0;
|
||||
}
|
||||
|
||||
.base-user-avatar-backdrop {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
pointer-events: none;
|
||||
background:
|
||||
radial-gradient(circle at 28% 28%, rgba(255, 255, 255, 0.72), transparent 62%),
|
||||
linear-gradient(150deg, rgba(255, 255, 255, 0.08), transparent),
|
||||
linear-gradient(
|
||||
140deg,
|
||||
var(--avatar-accent-soft, rgba(17, 182, 197, 0.08)) 0%,
|
||||
var(--avatar-accent-light, rgba(17, 182, 197, 0.18)) 100%
|
||||
);
|
||||
opacity: 0.75;
|
||||
transition:
|
||||
opacity 0.35s ease,
|
||||
transform 0.35s ease;
|
||||
z-index: 0;
|
||||
}
|
||||
|
||||
.base-user-avatar-img {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
display: block;
|
||||
z-index: 1;
|
||||
border-radius: inherit;
|
||||
transition: transform 0.35s ease;
|
||||
}
|
||||
|
||||
.base-user-avatar.is-interactive:hover,
|
||||
.base-user-avatar.is-interactive:focus-visible {
|
||||
transform: translateY(-1px) scale(1.02);
|
||||
border-color: var(--avatar-accent, var(--primary-color, #0a6e78));
|
||||
box-shadow:
|
||||
0 6px 16px var(--avatar-accent-shadow, rgba(17, 182, 197, 0.24)),
|
||||
0 3px 6px rgba(15, 52, 67, 0.18);
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.base-user-avatar.is-interactive:hover .base-user-avatar-backdrop,
|
||||
.base-user-avatar.is-interactive:focus-visible .base-user-avatar-backdrop {
|
||||
opacity: 1;
|
||||
transform: scale(1.05);
|
||||
}
|
||||
|
||||
.base-user-avatar.is-interactive:hover .base-user-avatar-img,
|
||||
.base-user-avatar.is-interactive:focus-visible .base-user-avatar-img {
|
||||
transform: scale(1.02);
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -52,6 +52,7 @@
|
||||
v-if="open && !isMobile && (loading || filteredOptions.length > 0 || showSearch)"
|
||||
:class="['dropdown-menu', menuClass]"
|
||||
v-click-outside="close"
|
||||
ref="menuRef"
|
||||
>
|
||||
<div v-if="showSearch" class="dropdown-search">
|
||||
<search-icon class="search-icon" />
|
||||
@@ -80,6 +81,7 @@
|
||||
<span>{{ o.name }}</span>
|
||||
</slot>
|
||||
</div>
|
||||
<slot name="footer" :close="close" :loading="loading" />
|
||||
</template>
|
||||
</div>
|
||||
<Teleport to="body">
|
||||
@@ -88,7 +90,7 @@
|
||||
<next class="back-icon" @click="close" />
|
||||
<span class="mobile-title">{{ placeholder }}</span>
|
||||
</div>
|
||||
<div class="dropdown-mobile-menu">
|
||||
<div class="dropdown-mobile-menu" ref="mobileMenuRef">
|
||||
<div v-if="showSearch" class="dropdown-search">
|
||||
<search-icon class="search-icon" />
|
||||
<input type="text" v-model="search" placeholder="搜索" />
|
||||
@@ -116,6 +118,7 @@
|
||||
<span>{{ o.name }}</span>
|
||||
</slot>
|
||||
</div>
|
||||
<slot name="footer" :close="close" :loading="loading" />
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
@@ -151,6 +154,8 @@ export default {
|
||||
const loaded = ref(false)
|
||||
const loading = ref(false)
|
||||
const wrapper = ref(null)
|
||||
const menuRef = ref(null)
|
||||
const mobileMenuRef = ref(null)
|
||||
const isMobile = useIsMobile()
|
||||
|
||||
const toggle = () => {
|
||||
@@ -200,6 +205,17 @@ export default {
|
||||
}
|
||||
}
|
||||
|
||||
const scrollToBottom = () => {
|
||||
const el = isMobile.value ? mobileMenuRef.value : menuRef.value
|
||||
if (el) {
|
||||
el.scrollTop = el.scrollHeight
|
||||
}
|
||||
}
|
||||
|
||||
const reload = async () => {
|
||||
await loadOptions(props.remote ? search.value : undefined)
|
||||
}
|
||||
|
||||
watch(
|
||||
() => props.initialOptions,
|
||||
(val) => {
|
||||
@@ -249,7 +265,7 @@ export default {
|
||||
return /^https?:\/\//.test(icon) || icon.startsWith('/')
|
||||
}
|
||||
|
||||
expose({ toggle, close })
|
||||
expose({ toggle, close, reload, scrollToBottom })
|
||||
|
||||
return {
|
||||
open,
|
||||
@@ -259,6 +275,8 @@ export default {
|
||||
search,
|
||||
filteredOptions,
|
||||
wrapper,
|
||||
menuRef,
|
||||
mobileMenuRef,
|
||||
selectedLabels,
|
||||
isSelected,
|
||||
loading,
|
||||
|
||||
@@ -116,30 +116,42 @@
|
||||
<div v-if="isLoadingTag" class="menu-loading-container">
|
||||
<l-hatch size="28" stroke="4" speed="3.5" color="var(--primary-color)"></l-hatch>
|
||||
</div>
|
||||
<div
|
||||
v-else
|
||||
v-for="t in tagData"
|
||||
:key="t.id"
|
||||
class="section-item"
|
||||
:class="{ selected: isTagSelected(t.id) }"
|
||||
<template v-else>
|
||||
<div
|
||||
v-for="t in tagData"
|
||||
:key="t.id"
|
||||
class="section-item"
|
||||
:class="{ selected: isTagSelected(t.id) }"
|
||||
@click="gotoTag(t)"
|
||||
>
|
||||
<BaseImage
|
||||
v-if="isImageIcon(t.smallIcon || t.icon)"
|
||||
:src="t.smallIcon || t.icon"
|
||||
class="section-item-icon"
|
||||
:alt="t.name"
|
||||
/>
|
||||
<component
|
||||
v-else-if="t.smallIcon || t.icon"
|
||||
:is="t.smallIcon || t.icon"
|
||||
class="section-item-icon"
|
||||
/>
|
||||
<tag-one v-else class="section-item-icon" />
|
||||
<span class="section-item-text"
|
||||
>{{ t.name }} <span class="section-item-text-count">x {{ t.count }}</span></span
|
||||
>
|
||||
</div>
|
||||
<BaseImage
|
||||
v-if="isImageIcon(t.smallIcon || t.icon)"
|
||||
:src="t.smallIcon || t.icon"
|
||||
class="section-item-icon"
|
||||
:alt="t.name"
|
||||
/>
|
||||
<component
|
||||
v-else-if="t.smallIcon || t.icon"
|
||||
:is="t.smallIcon || t.icon"
|
||||
class="section-item-icon"
|
||||
/>
|
||||
<tag-one v-else class="section-item-icon" />
|
||||
<span class="section-item-text"
|
||||
>{{ t.name }} <span class="section-item-text-count">x {{ t.count }}</span></span
|
||||
>
|
||||
</div>
|
||||
<div v-if="hasMoreTags || isLoadingMoreTags" class="section-item more-item">
|
||||
<a
|
||||
v-if="hasMoreTags && !isLoadingMoreTags"
|
||||
href="#"
|
||||
class="more-link"
|
||||
@click.prevent="loadMoreTags"
|
||||
>
|
||||
查看更多
|
||||
</a>
|
||||
<span v-else class="more-loading">加载中...</span>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -207,16 +219,88 @@ const {
|
||||
},
|
||||
)
|
||||
|
||||
const TAG_PAGE_SIZE = 10
|
||||
const tagPage = ref(0)
|
||||
const hasMoreTags = ref(true)
|
||||
const isLoadingMoreTags = ref(false)
|
||||
|
||||
const buildTagUrl = (page = 0) => {
|
||||
const base = API_BASE_URL || (import.meta.client ? window.location.origin : '')
|
||||
const url = new URL('/api/tags', base)
|
||||
url.searchParams.set('page', String(page))
|
||||
url.searchParams.set('pageSize', String(TAG_PAGE_SIZE))
|
||||
return url.toString()
|
||||
}
|
||||
|
||||
const fetchTagPage = async (page = 0) => {
|
||||
try {
|
||||
return await $fetch(buildTagUrl(page))
|
||||
} catch (e) {
|
||||
console.error('Failed to fetch tags', e)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
const {
|
||||
data: tagData,
|
||||
pending: isLoadingTag,
|
||||
error: tagError,
|
||||
} = await useAsyncData('menu:tags', () => $fetch(`${API_BASE_URL}/api/tags?limit=10`), {
|
||||
} = await useAsyncData('menu:tags', () => fetchTagPage(0), {
|
||||
server: true,
|
||||
default: () => [],
|
||||
staleTime: 5 * 60 * 1000,
|
||||
})
|
||||
|
||||
const dedupeTags = (list) => Array.from(new Map(list.map((tag) => [tag.id, tag])).values())
|
||||
|
||||
const initializeTagState = (val) => {
|
||||
const initial = Array.isArray(val) ? val : []
|
||||
if (!Array.isArray(val)) {
|
||||
tagData.value = []
|
||||
}
|
||||
tagPage.value = 0
|
||||
hasMoreTags.value = initial.length === TAG_PAGE_SIZE
|
||||
}
|
||||
|
||||
initializeTagState(tagData.value)
|
||||
|
||||
watch(
|
||||
tagData,
|
||||
(val, oldVal) => {
|
||||
const next = Array.isArray(val) ? val : []
|
||||
if (!Array.isArray(val)) {
|
||||
tagData.value = []
|
||||
}
|
||||
const shouldReset =
|
||||
!Array.isArray(oldVal) || oldVal.length > next.length || next.length <= TAG_PAGE_SIZE
|
||||
if (shouldReset) {
|
||||
tagPage.value = 0
|
||||
hasMoreTags.value = next.length === TAG_PAGE_SIZE
|
||||
}
|
||||
},
|
||||
{ deep: false },
|
||||
)
|
||||
|
||||
const loadMoreTags = async () => {
|
||||
if (isLoadingMoreTags.value || !hasMoreTags.value) return
|
||||
isLoadingMoreTags.value = true
|
||||
const nextPage = tagPage.value + 1
|
||||
try {
|
||||
const result = await fetchTagPage(nextPage)
|
||||
const data = Array.isArray(result) ? result : []
|
||||
const existing = Array.isArray(tagData.value) ? tagData.value : []
|
||||
tagData.value = dedupeTags([...existing, ...data])
|
||||
tagPage.value = nextPage
|
||||
if (data.length < TAG_PAGE_SIZE) {
|
||||
hasMoreTags.value = false
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Failed to load more tags', e)
|
||||
} finally {
|
||||
isLoadingMoreTags.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/** 其余逻辑保持不变 */
|
||||
const iconClass = computed(() => {
|
||||
switch (themeState.mode) {
|
||||
@@ -433,6 +517,27 @@ const gotoTag = (t) => {
|
||||
transition: background-color 0.5s ease;
|
||||
}
|
||||
|
||||
.more-item {
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.more-link {
|
||||
color: var(--primary-color);
|
||||
text-decoration: none;
|
||||
font-size: 14px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.more-link:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.more-loading {
|
||||
font-size: 13px;
|
||||
color: var(--menu-text-color);
|
||||
opacity: 0.7;
|
||||
}
|
||||
|
||||
.section-item:hover {
|
||||
background-color: var(--menu-selected-background-color-hover);
|
||||
}
|
||||
@@ -441,7 +546,6 @@ const gotoTag = (t) => {
|
||||
background-color: var(--menu-selected-background-color);
|
||||
}
|
||||
|
||||
|
||||
.section-item-text-count {
|
||||
font-size: 12px;
|
||||
color: var(--menu-text-color);
|
||||
|
||||
@@ -159,12 +159,6 @@ export default {
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.vditor {
|
||||
min-height: 50px;
|
||||
max-height: 150px;
|
||||
}
|
||||
|
||||
.message-bottom-container {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<template>
|
||||
<Dropdown
|
||||
ref="dropdownRef"
|
||||
v-model="selected"
|
||||
:fetch-options="fetchTags"
|
||||
multiple
|
||||
@@ -25,11 +26,23 @@
|
||||
<div v-if="option.description" class="option-desc">{{ option.description }}</div>
|
||||
</div>
|
||||
</template>
|
||||
<template #footer>
|
||||
<div v-if="hasMoreRemoteTags" class="dropdown-footer">
|
||||
<a
|
||||
href="#"
|
||||
class="dropdown-more"
|
||||
:class="{ disabled: loadMoreRequested }"
|
||||
@click.prevent="loadMoreRemoteTags"
|
||||
>
|
||||
{{ loadMoreRequested ? '加载中...' : '查看更多' }}
|
||||
</a>
|
||||
</div>
|
||||
</template>
|
||||
</Dropdown>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { computed, ref, watch } from 'vue'
|
||||
import { computed, reactive, ref, watch, nextTick } from 'vue'
|
||||
import { toast } from '~/main'
|
||||
import Dropdown from '~/components/Dropdown.vue'
|
||||
const config = useRuntimeConfig()
|
||||
@@ -42,9 +55,19 @@ const props = defineProps({
|
||||
options: { type: Array, default: () => [] },
|
||||
})
|
||||
|
||||
const dropdownRef = ref(null)
|
||||
const localTags = ref([])
|
||||
const providedTags = ref(Array.isArray(props.options) ? [...props.options] : [])
|
||||
|
||||
const TAG_PAGE_SIZE = 10
|
||||
const remoteState = reactive({
|
||||
keyword: '',
|
||||
nextPage: 0,
|
||||
hasMore: true,
|
||||
options: [],
|
||||
})
|
||||
const loadMoreRequested = ref(false)
|
||||
|
||||
watch(
|
||||
() => props.options,
|
||||
(val) => {
|
||||
@@ -53,7 +76,7 @@ watch(
|
||||
)
|
||||
|
||||
const mergedOptions = computed(() => {
|
||||
const arr = [...providedTags.value, ...localTags.value]
|
||||
const arr = [...providedTags.value, ...localTags.value, ...remoteState.options]
|
||||
return arr.filter((v, i, a) => a.findIndex((t) => t.id === v.id) === i)
|
||||
})
|
||||
|
||||
@@ -62,44 +85,93 @@ const isImageIcon = (icon) => {
|
||||
return /^https?:\/\//.test(icon) || icon.startsWith('/')
|
||||
}
|
||||
|
||||
const buildTagsUrl = (kw = '') => {
|
||||
const buildTagsUrl = (kw = '', page = 0) => {
|
||||
const base = API_BASE_URL || (import.meta.client ? window.location.origin : '')
|
||||
const url = new URL('/api/tags', base)
|
||||
|
||||
if (kw) url.searchParams.set('keyword', kw)
|
||||
url.searchParams.set('limit', '10')
|
||||
url.searchParams.set('page', String(page))
|
||||
url.searchParams.set('pageSize', String(TAG_PAGE_SIZE))
|
||||
|
||||
return url.toString()
|
||||
}
|
||||
|
||||
const fetchRemoteTags = async (kw = '', page = 0) => {
|
||||
const url = buildTagsUrl(kw, page)
|
||||
try {
|
||||
const res = await fetch(url)
|
||||
if (res.ok) {
|
||||
const data = await res.json()
|
||||
return Array.isArray(data) ? data : []
|
||||
}
|
||||
throw new Error('failed to fetch tags')
|
||||
} catch (e) {
|
||||
console.error('Failed to fetch tags', e)
|
||||
toast.error('获取标签失败')
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
const combineOptions = (remoteOptions = []) => {
|
||||
const options = [...providedTags.value, ...localTags.value, ...remoteOptions]
|
||||
return Array.from(new Map(options.map((t) => [t.id, t])).values())
|
||||
}
|
||||
|
||||
const fetchTags = async (kw = '') => {
|
||||
const defaultOption = { id: 0, name: '无标签' }
|
||||
|
||||
// 1) 先拼 URL(自动兜底到 window.location.origin)
|
||||
const url = buildTagsUrl(kw)
|
||||
|
||||
// 2) 拉数据
|
||||
let data = []
|
||||
try {
|
||||
const res = await fetch(url)
|
||||
if (res.ok) data = await res.json()
|
||||
} catch {
|
||||
toast.error('获取标签失败')
|
||||
if (kw !== remoteState.keyword) {
|
||||
remoteState.keyword = kw
|
||||
remoteState.nextPage = 0
|
||||
remoteState.options = []
|
||||
remoteState.hasMore = true
|
||||
}
|
||||
|
||||
// 3) 合并、去重、可创建
|
||||
let options = [...data, ...localTags.value]
|
||||
const shouldFetch = remoteState.options.length === 0 || loadMoreRequested.value
|
||||
if (shouldFetch) {
|
||||
const pageToFetch = loadMoreRequested.value ? remoteState.nextPage : 0
|
||||
try {
|
||||
const data = await fetchRemoteTags(remoteState.keyword, pageToFetch)
|
||||
if (pageToFetch === 0) {
|
||||
remoteState.options = data
|
||||
} else {
|
||||
const existing = Array.isArray(remoteState.options) ? remoteState.options : []
|
||||
const merged = [...existing, ...data]
|
||||
remoteState.options = Array.from(new Map(merged.map((t) => [t.id, t])).values())
|
||||
}
|
||||
remoteState.hasMore = data.length === TAG_PAGE_SIZE
|
||||
remoteState.nextPage = pageToFetch + 1
|
||||
} catch (e) {
|
||||
return [defaultOption, ...combineOptions(remoteState.options)]
|
||||
} finally {
|
||||
loadMoreRequested.value = false
|
||||
}
|
||||
}
|
||||
|
||||
let options = combineOptions(remoteState.options)
|
||||
|
||||
if (props.creatable && kw && !options.some((t) => t.name.toLowerCase() === kw.toLowerCase())) {
|
||||
options.push({ id: `__create__:${kw}`, name: `创建"${kw}"` })
|
||||
}
|
||||
|
||||
options = Array.from(new Map(options.map((t) => [t.id, t])).values())
|
||||
|
||||
// 4) 最终结果
|
||||
return [defaultOption, ...options]
|
||||
}
|
||||
|
||||
const hasMoreRemoteTags = computed(() => remoteState.hasMore)
|
||||
|
||||
const loadMoreRemoteTags = async () => {
|
||||
if (!remoteState.hasMore || loadMoreRequested.value) return
|
||||
loadMoreRequested.value = true
|
||||
try {
|
||||
await dropdownRef.value?.reload()
|
||||
await nextTick()
|
||||
dropdownRef.value?.scrollToBottom?.()
|
||||
} catch (e) {
|
||||
console.error('Failed to load more tags', e)
|
||||
loadMoreRequested.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const selected = computed({
|
||||
get: () => props.modelValue,
|
||||
set: (v) => {
|
||||
@@ -151,4 +223,21 @@ const selected = computed({
|
||||
font-weight: bold;
|
||||
opacity: 0.4;
|
||||
}
|
||||
|
||||
.dropdown-footer {
|
||||
padding: 8px 20px;
|
||||
text-align: center;
|
||||
border-top: 1px solid var(--normal-border-color);
|
||||
}
|
||||
|
||||
.dropdown-more {
|
||||
color: var(--primary-color);
|
||||
text-decoration: none;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.dropdown-more.disabled {
|
||||
pointer-events: none;
|
||||
opacity: 0.6;
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user