Compare commits

...

15 Commits

Author SHA1 Message Date
Tim
e96db5d0d6 fix: keep dropdown at bottom after loading more 2025-09-24 16:14:45 +08:00
tim
1083c4241a fix: 修复语法问题 2025-09-24 16:06:17 +08:00
Tim
1eeabab41a feat: paginate tags across backend and ui 2025-09-24 15:58:24 +08:00
Tim
2b5f6f2208 Merge pull request #1022 from nagisa77/feature/user_list_and_avatar
Feature/user list and avatar
2025-09-24 01:51:51 +08:00
tim
bda377336d fix: 优化一些头像属性 2025-09-24 01:51:02 +08:00
tim
77507f7b18 Revert "style: enhance BaseUserAvatar presentation"
This reverts commit 229439aa05.
2025-09-24 01:38:41 +08:00
Tim
a39f2f7c00 Merge pull request #1021 from nagisa77/codex/improve-baseuseravatar-styling-ok22do
style: enhance BaseUserAvatar presentation
2025-09-24 01:31:46 +08:00
Tim
229439aa05 style: enhance BaseUserAvatar presentation 2025-09-24 01:31:31 +08:00
tim
612881f1b1 Revert "refine BaseUserAvatar styling"
This reverts commit c68c5985f6.
2025-09-24 01:31:05 +08:00
Tim
05c7bc18d7 Merge pull request #1020 from nagisa77/codex/improve-baseuseravatar-styling-z7f617
Enhance BaseUserAvatar aesthetics
2025-09-24 01:23:25 +08:00
Tim
c68c5985f6 refine BaseUserAvatar styling 2025-09-24 01:23:12 +08:00
tim
7d44791011 Revert "feat: refresh base user avatar styling"
This reverts commit 4b8229b0a1.
2025-09-24 01:22:50 +08:00
Tim
15b992b949 Merge pull request #1019 from nagisa77/codex/improve-baseuseravatar-styling
feat: refresh base user avatar styling
2025-09-24 01:21:29 +08:00
Tim
dc13b2941f Merge pull request #1016 from nagisa77/feature/vditor_layout
fix: 移动端--频道--表情无法显示完全 #994
2025-09-23 23:48:59 +08:00
tim
13c250d392 fix: 移动端--频道--表情无法显示完全 #994 2025-09-23 23:48:31 +08:00
8 changed files with 320 additions and 186 deletions

View File

@@ -100,18 +100,32 @@ public class TagController {
) )
public List<TagDto> list( public List<TagDto> list(
@RequestParam(value = "keyword", required = false) String keyword, @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 @RequestParam(value = "limit", required = false) Integer limit
) { ) {
List<Tag> tags = tagService.searchTags(keyword); List<Tag> tags = tagService.searchTags(keyword);
List<Long> tagIds = tags.stream().map(Tag::getId).toList(); List<Long> tagIds = tags.stream().map(Tag::getId).toList();
Map<Long, Long> postCntByTagIds = postService.countPostsByTagIds(tagIds); Map<Long, Long> postCntByTagIds = postService.countPostsByTagIds(tagIds);
if (postCntByTagIds == null) {
postCntByTagIds = java.util.Collections.emptyMap();
}
Map<Long, Long> finalPostCntByTagIds = postCntByTagIds;
List<TagDto> dtos = tags List<TagDto> dtos = tags
.stream() .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())) .sorted((a, b) -> Long.compare(b.getCount(), a.getCount()))
.collect(Collectors.toList()); .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) { if (limit != null && limit > 0 && dtos.size() > limit) {
return dtos.subList(0, limit); return new java.util.ArrayList<>(dtos.subList(0, limit));
} }
return dtos; return dtos;
} }

View File

@@ -82,6 +82,7 @@ class TagControllerTest {
t.setIcon("i2"); t.setIcon("i2");
t.setSmallIcon("s2"); t.setSmallIcon("s2");
Mockito.when(tagService.searchTags(null)).thenReturn(List.of(t)); Mockito.when(tagService.searchTags(null)).thenReturn(List.of(t));
Mockito.when(postService.countPostsByTagIds(List.of(2L))).thenReturn(java.util.Map.of());
mockMvc mockMvc
.perform(get("/api/tags")) .perform(get("/api/tags"))
@@ -93,6 +94,31 @@ class TagControllerTest {
.andExpect(jsonPath("$[0].smallIcon").value("s2")); .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 @Test
void updateTag() throws Exception { void updateTag() throws Exception {
Tag t = new Tag(); Tag t = new Tag();

View File

@@ -108,7 +108,6 @@ body {
.vditor-toolbar--pin { .vditor-toolbar--pin {
top: calc(var(--header-height) + 1px) !important; top: calc(var(--header-height) + 1px) !important;
z-index: 20;
} }
.vditor-panel { .vditor-panel {
@@ -134,26 +133,6 @@ body {
animation: spin 1s linear infinite; 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 渲染样式 * Markdown 渲染样式
*************************/ *************************/
@@ -333,10 +312,6 @@ body {
min-height: 100px; min-height: 100px;
} }
.vditor-toolbar {
overflow-x: auto;
}
.about-content h1, .about-content h1,
.info-content-text h1 { .info-content-text h1 {
font-size: 20px; font-size: 20px;
@@ -354,8 +329,8 @@ body {
margin-bottom: 3px; margin-bottom: 3px;
} }
.vditor-toolbar--pin { .vditor-panel {
top: 0 !important; min-width: 330px;
} }
.about-content li, .about-content li,
@@ -367,11 +342,6 @@ body {
line-height: 1.5; line-height: 1.5;
} }
.vditor-panel {
position: relative;
min-width: 0;
}
.d2h-file-name { .d2h-file-name {
font-size: 14px !important; font-size: 14px !important;
} }

View File

@@ -1,18 +1,13 @@
<template> <template>
<component <NuxtLink
:is="wrapperTag" :to="resolvedLink"
:to="isLink ? resolvedLink : undefined"
class="base-user-avatar" class="base-user-avatar"
:class="wrapperClass" :class="wrapperClass"
:style="wrapperStyle" :style="wrapperStyle"
v-bind="wrapperAttrs" 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" /> <BaseImage :src="currentSrc" :alt="altText" class="base-user-avatar-img" @error="onError" />
</component> </NuxtLink>
</template> </template>
<script setup> <script setup>
@@ -81,45 +76,15 @@ const sizeStyle = computed(() => {
return { width: value, height: value } 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 wrapperStyle = computed(() => {
const attrStyle = attrs.style const attrStyle = attrs.style
return [accentStyles.value, sizeStyle.value, attrStyle] return [sizeStyle.value, attrStyle]
}) })
const isLink = computed(() => !props.disableLink && !!resolvedLink.value) const wrapperClass = computed(() => [attrs.class, { 'is-rounded': props.rounded }])
const wrapperTag = computed(() => (isLink.value ? 'NuxtLink' : 'div'))
const wrapperClass = computed(() => [
attrs.class,
{ 'is-rounded': props.rounded, 'is-interactive': isLink.value },
])
const wrapperAttrs = computed(() => { const wrapperAttrs = computed(() => {
const { class: _class, style: _style, to: _to, href: _href, ...rest } = attrs const { class: _class, style: _style, ...rest } = attrs
return rest return rest
}) })
@@ -132,26 +97,24 @@ function onError() {
<style scoped> <style scoped>
.base-user-avatar { .base-user-avatar {
position: relative;
display: inline-flex; display: inline-flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
overflow: hidden; overflow: hidden;
border-radius: 16px; background-color: var(--avatar-placeholder-color, #f0f0f0);
background: linear-gradient( /* 先用box-sizing: border-box保证加border后宽高不变圆形不变形 */
140deg, box-sizing: border-box;
var(--avatar-accent-soft, rgba(17, 182, 197, 0.12)) 0%, border: 1.5px solid var(--normal-border-color);
var(--avatar-accent-light, rgba(17, 182, 197, 0.22)) 100% transition: all 0.6s ease;
); }
border: 1px solid var(--avatar-accent-border, rgba(17, 182, 197, 0.2));
box-shadow: .base-user-avatar:hover {
0 1px 2px rgba(15, 52, 67, 0.08), box-shadow: 0 4px 24px rgba(0, 0, 0, 0.1);
0 3px 8px var(--avatar-accent-shadow, rgba(17, 182, 197, 0.18)); transform: scale(1.05);
transition: }
transform 0.3s ease,
box-shadow 0.3s ease, .base-user-avatar:active {
border-color 0.3s ease, box-shadow: 0 4px 24px rgba(0, 0, 0, 0.1);
background 0.3s ease;
} }
.base-user-avatar.is-rounded { .base-user-avatar.is-rounded {
@@ -162,54 +125,10 @@ function onError() {
border-radius: 0; 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 { .base-user-avatar-img {
position: relative;
width: 100%; width: 100%;
height: 100%; height: 100%;
object-fit: cover; object-fit: cover;
display: block; 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> </style>

View File

@@ -52,6 +52,7 @@
v-if="open && !isMobile && (loading || filteredOptions.length > 0 || showSearch)" v-if="open && !isMobile && (loading || filteredOptions.length > 0 || showSearch)"
:class="['dropdown-menu', menuClass]" :class="['dropdown-menu', menuClass]"
v-click-outside="close" v-click-outside="close"
ref="menuRef"
> >
<div v-if="showSearch" class="dropdown-search"> <div v-if="showSearch" class="dropdown-search">
<search-icon class="search-icon" /> <search-icon class="search-icon" />
@@ -80,6 +81,7 @@
<span>{{ o.name }}</span> <span>{{ o.name }}</span>
</slot> </slot>
</div> </div>
<slot name="footer" :close="close" :loading="loading" />
</template> </template>
</div> </div>
<Teleport to="body"> <Teleport to="body">
@@ -88,7 +90,7 @@
<next class="back-icon" @click="close" /> <next class="back-icon" @click="close" />
<span class="mobile-title">{{ placeholder }}</span> <span class="mobile-title">{{ placeholder }}</span>
</div> </div>
<div class="dropdown-mobile-menu"> <div class="dropdown-mobile-menu" ref="mobileMenuRef">
<div v-if="showSearch" class="dropdown-search"> <div v-if="showSearch" class="dropdown-search">
<search-icon class="search-icon" /> <search-icon class="search-icon" />
<input type="text" v-model="search" placeholder="搜索" /> <input type="text" v-model="search" placeholder="搜索" />
@@ -116,6 +118,7 @@
<span>{{ o.name }}</span> <span>{{ o.name }}</span>
</slot> </slot>
</div> </div>
<slot name="footer" :close="close" :loading="loading" />
</template> </template>
</div> </div>
</div> </div>
@@ -151,6 +154,8 @@ export default {
const loaded = ref(false) const loaded = ref(false)
const loading = ref(false) const loading = ref(false)
const wrapper = ref(null) const wrapper = ref(null)
const menuRef = ref(null)
const mobileMenuRef = ref(null)
const isMobile = useIsMobile() const isMobile = useIsMobile()
const toggle = () => { 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( watch(
() => props.initialOptions, () => props.initialOptions,
(val) => { (val) => {
@@ -249,7 +265,7 @@ export default {
return /^https?:\/\//.test(icon) || icon.startsWith('/') return /^https?:\/\//.test(icon) || icon.startsWith('/')
} }
expose({ toggle, close }) expose({ toggle, close, reload, scrollToBottom })
return { return {
open, open,
@@ -259,6 +275,8 @@ export default {
search, search,
filteredOptions, filteredOptions,
wrapper, wrapper,
menuRef,
mobileMenuRef,
selectedLabels, selectedLabels,
isSelected, isSelected,
loading, loading,

View File

@@ -116,30 +116,42 @@
<div v-if="isLoadingTag" class="menu-loading-container"> <div v-if="isLoadingTag" class="menu-loading-container">
<l-hatch size="28" stroke="4" speed="3.5" color="var(--primary-color)"></l-hatch> <l-hatch size="28" stroke="4" speed="3.5" color="var(--primary-color)"></l-hatch>
</div> </div>
<div <template v-else>
v-else <div
v-for="t in tagData" v-for="t in tagData"
:key="t.id" :key="t.id"
class="section-item" class="section-item"
:class="{ selected: isTagSelected(t.id) }" :class="{ selected: isTagSelected(t.id) }"
@click="gotoTag(t)" @click="gotoTag(t)"
> >
<BaseImage <BaseImage
v-if="isImageIcon(t.smallIcon || t.icon)" v-if="isImageIcon(t.smallIcon || t.icon)"
:src="t.smallIcon || t.icon" :src="t.smallIcon || t.icon"
class="section-item-icon" class="section-item-icon"
:alt="t.name" :alt="t.name"
/> />
<component <component
v-else-if="t.smallIcon || t.icon" v-else-if="t.smallIcon || t.icon"
:is="t.smallIcon || t.icon" :is="t.smallIcon || t.icon"
class="section-item-icon" class="section-item-icon"
/> />
<tag-one v-else class="section-item-icon" /> <tag-one v-else class="section-item-icon" />
<span class="section-item-text" <span class="section-item-text"
>{{ t.name }} <span class="section-item-text-count">x {{ t.count }}</span></span >{{ t.name }} <span class="section-item-text-count">x {{ t.count }}</span></span
> >
</div> </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> </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 { const {
data: tagData, data: tagData,
pending: isLoadingTag, pending: isLoadingTag,
error: tagError, error: tagError,
} = await useAsyncData('menu:tags', () => $fetch(`${API_BASE_URL}/api/tags?limit=10`), { } = await useAsyncData('menu:tags', () => fetchTagPage(0), {
server: true, server: true,
default: () => [], default: () => [],
staleTime: 5 * 60 * 1000, 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(() => { const iconClass = computed(() => {
switch (themeState.mode) { switch (themeState.mode) {
@@ -433,6 +517,27 @@ const gotoTag = (t) => {
transition: background-color 0.5s ease; 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 { .section-item:hover {
background-color: var(--menu-selected-background-color-hover); background-color: var(--menu-selected-background-color-hover);
} }
@@ -441,7 +546,6 @@ const gotoTag = (t) => {
background-color: var(--menu-selected-background-color); background-color: var(--menu-selected-background-color);
} }
.section-item-text-count { .section-item-text-count {
font-size: 12px; font-size: 12px;
color: var(--menu-text-color); color: var(--menu-text-color);

View File

@@ -159,12 +159,6 @@ export default {
border: 1px solid var(--border-color); border: 1px solid var(--border-color);
border-radius: 8px; border-radius: 8px;
} }
.vditor {
min-height: 50px;
max-height: 150px;
}
.message-bottom-container { .message-bottom-container {
display: flex; display: flex;
flex-direction: row; flex-direction: row;

View File

@@ -1,5 +1,6 @@
<template> <template>
<Dropdown <Dropdown
ref="dropdownRef"
v-model="selected" v-model="selected"
:fetch-options="fetchTags" :fetch-options="fetchTags"
multiple multiple
@@ -25,11 +26,23 @@
<div v-if="option.description" class="option-desc">{{ option.description }}</div> <div v-if="option.description" class="option-desc">{{ option.description }}</div>
</div> </div>
</template> </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> </Dropdown>
</template> </template>
<script setup> <script setup>
import { computed, ref, watch } from 'vue' import { computed, reactive, ref, watch, nextTick } from 'vue'
import { toast } from '~/main' import { toast } from '~/main'
import Dropdown from '~/components/Dropdown.vue' import Dropdown from '~/components/Dropdown.vue'
const config = useRuntimeConfig() const config = useRuntimeConfig()
@@ -42,9 +55,19 @@ const props = defineProps({
options: { type: Array, default: () => [] }, options: { type: Array, default: () => [] },
}) })
const dropdownRef = ref(null)
const localTags = ref([]) const localTags = ref([])
const providedTags = ref(Array.isArray(props.options) ? [...props.options] : []) 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( watch(
() => props.options, () => props.options,
(val) => { (val) => {
@@ -53,7 +76,7 @@ watch(
) )
const mergedOptions = computed(() => { 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) 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('/') 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 base = API_BASE_URL || (import.meta.client ? window.location.origin : '')
const url = new URL('/api/tags', base) const url = new URL('/api/tags', base)
if (kw) url.searchParams.set('keyword', kw) 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() 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 fetchTags = async (kw = '') => {
const defaultOption = { id: 0, name: '无标签' } const defaultOption = { id: 0, name: '无标签' }
// 1) 先拼 URL自动兜底到 window.location.origin if (kw !== remoteState.keyword) {
const url = buildTagsUrl(kw) remoteState.keyword = kw
remoteState.nextPage = 0
// 2) 拉数据 remoteState.options = []
let data = [] remoteState.hasMore = true
try {
const res = await fetch(url)
if (res.ok) data = await res.json()
} catch {
toast.error('获取标签失败')
} }
// 3) 合并、去重、可创建 const shouldFetch = remoteState.options.length === 0 || loadMoreRequested.value
let options = [...data, ...localTags.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())) { if (props.creatable && kw && !options.some((t) => t.name.toLowerCase() === kw.toLowerCase())) {
options.push({ id: `__create__:${kw}`, name: `创建"${kw}"` }) options.push({ id: `__create__:${kw}`, name: `创建"${kw}"` })
} }
options = Array.from(new Map(options.map((t) => [t.id, t])).values())
// 4) 最终结果
return [defaultOption, ...options] 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({ const selected = computed({
get: () => props.modelValue, get: () => props.modelValue,
set: (v) => { set: (v) => {
@@ -151,4 +223,21 @@ const selected = computed({
font-weight: bold; font-weight: bold;
opacity: 0.4; 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> </style>