Files
lpt-fe/src/components/MarkerChips.vue
T

230 lines
5.3 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { ref } from "vue";
import { listTagCandidates, type Marker } from "@/api/markers";
/**
* 标记展示:系统标记灰底只读(成对显示为「名=值」),用户标签可点击筛选、可删除。
* 编辑态下从字典/用过的标签里搜索选择,也允许直接输入新标签;父组件负责真正调接口。
*/
const props = withDefaults(
defineProps<{
markers: Marker[];
/** 是否允许加/删用户标签(列表卡片默认可编辑) */
editable?: boolean;
}>(),
{ editable: true },
);
const emit = defineEmits<{
(e: "filter", marker: Marker): void;
(e: "add", markerName: string): void;
(e: "remove", marker: Marker): void;
}>();
const adding = ref(false);
const draft = ref("");
// 打标签的候选:字典标签 ∪ 用过的用户标签(含自动任务名标签),打开输入框时取一次
const candidates = ref<string[]>([]);
const candidatesLoading = ref(false);
const loadCandidates = async () => {
candidatesLoading.value = true;
try {
const res = await listTagCandidates();
candidates.value = res?.data || [];
} catch {
// 候选拉取失败不阻塞打标签,仍可直接输入新标签
candidates.value = [];
} finally {
candidatesLoading.value = false;
}
};
const startAdding = () => {
draft.value = "";
adding.value = true;
void loadCandidates();
};
const submitAdd = () => {
const name = draft.value.trim();
if (!name) {
adding.value = false;
return;
}
emit("add", name);
draft.value = "";
adding.value = false;
};
const cancelAdd = () => {
draft.value = "";
adding.value = false;
};
</script>
<template>
<span class="marker-chips">
<template v-for="marker in props.markers" :key="marker.id">
<span v-if="marker.kind === 'SYSTEM'" class="marker marker-system">
{{ marker.markerName }}<template v-if="marker.markerValue">={{ marker.markerValue }}</template>
</span>
<span
v-else
class="marker marker-user"
role="button"
tabindex="0"
:aria-label="`按标签 ${marker.markerName} 筛选`"
@click.stop="emit('filter', marker)"
@keydown.enter.stop.prevent="emit('filter', marker)"
@keydown.space.stop.prevent="emit('filter', marker)"
>
<span class="marker-user-name">{{ marker.markerName }}</span>
<button
v-if="props.editable"
type="button"
class="marker-remove"
:aria-label="`删除标签 ${marker.markerName}`"
@click.stop="emit('remove', marker)"
>×</button>
</span>
</template>
<span v-if="props.editable" class="marker-add" @click.stop>
<button
v-if="!adding"
type="button"
class="marker-add-toggle"
@click.stop="startAdding"
>+ 标签</button>
<span v-else class="marker-add-form">
<el-select
v-model="draft"
class="marker-add-select"
size="small"
filterable
allow-create
default-first-option
:reserve-keyword="false"
:loading="candidatesLoading"
placeholder="标签名"
aria-label="新标签名"
@keydown.enter.prevent="submitAdd"
@keydown.esc.prevent="cancelAdd"
>
<el-option v-for="name in candidates" :key="name" :label="name" :value="name" />
</el-select>
<button type="button" class="marker-add-confirm" @click.stop="submitAdd">确定</button>
</span>
</span>
</span>
</template>
<style scoped>
.marker-chips {
display: inline-flex;
align-items: center;
flex-wrap: wrap;
gap: 6px;
}
.marker {
display: inline-flex;
align-items: center;
gap: 4px;
font-size: 12px;
line-height: 1.4;
padding: 1px 7px;
border-radius: 10px;
white-space: nowrap;
}
/* 系统标记:灰底只读,不能点 */
.marker-system {
background: var(--surface);
border: 1px solid var(--border-soft);
color: var(--text-secondary);
}
/* 用户标签:绿色可点,点一下按它筛选 */
.marker-user {
background: var(--accent-success-soft);
border: 1px solid var(--accent-success-soft);
color: var(--green-700);
cursor: pointer;
}
.marker-user:hover {
border-color: var(--green-600);
}
.marker-user:focus-visible {
outline: 2px solid var(--green-700);
outline-offset: 1px;
}
.marker-user-name {
font-weight: 600;
}
.marker-remove {
border: none;
background: transparent;
color: var(--green-700);
font-size: 13px;
line-height: 1;
padding: 0 0 0 2px;
cursor: pointer;
}
.marker-remove:hover {
color: var(--accent-warning-strong);
}
.marker-add {
display: inline-flex;
align-items: center;
}
.marker-add-toggle {
border: 1px dashed var(--border-soft);
background: transparent;
color: var(--text-secondary);
font-size: 12px;
border-radius: 10px;
padding: 1px 8px;
cursor: pointer;
}
.marker-add-toggle:hover {
color: var(--green-700);
border-color: var(--green-600);
}
.marker-add-form {
display: inline-flex;
align-items: center;
gap: 4px;
}
.marker-add-select {
width: 150px;
}
.marker-add-select :deep(.el-select__wrapper) {
min-height: 24px;
font-size: 12px;
padding: 0 6px;
}
.marker-add-confirm {
border: none;
background: var(--green-700);
color: #fff;
font-size: 12px;
border-radius: 6px;
padding: 2px 8px;
cursor: pointer;
}
</style>