Files
Thing/src/modules/smarthome/components/EntityPicker.vue
T
2026-10-23 17:25:17 +08:00

122 lines
4.5 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">
/**
* 实体选择器(可搜索下拉)。
*
* 规则编辑器里"触发实体 / 动作实体"都用它。数据源就是设备页那份
* `store.entities`(P1 已缓存),因此不需要新命令。
*
* 与 `music/SourcePicker` 同一套写法:Popover + 搜索框 + ScrollArea 列表;
* 不做虚拟滚动 —— 真机 577 个实体、搜索框收敛后可见集很小(plan §10 已说明
* 数量级问题靠分组/搜索解决而不是虚拟滚动)。
*/
import { computed, ref } from 'vue'
import { ChevronDown, Search } from '@lucide/vue'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'
import { ScrollArea } from '@/components/ui/scroll-area'
import { useSmartHomeStore, type EntitySummary } from '@/stores/smartHomeStore'
import { compareByName, domainIcon, domainOf, isControllable, isOffline } from '../format'
const props = withDefaults(
defineProps<{
modelValue: string
placeholder?: string
/** 只列这些 domain(空 = 全部) */
domains?: string[]
/** 是否只列可控实体(动作实体默认要可控) */
controllableOnly?: boolean
disabled?: boolean
class?: string
}>(),
{ placeholder: '选择实体', domains: () => [], controllableOnly: false, disabled: false }
)
const emit = defineEmits<{ 'update:model-value': [string] }>()
const store = useSmartHomeStore()
const open = ref(false)
const query = ref('')
const label = computed(() => {
if (!props.modelValue) return props.placeholder
return store.entityLabel(props.modelValue)
})
const options = computed<EntitySummary[]>(() => {
const q = query.value.trim().toLowerCase()
return store.entities
.filter((e) => {
if (props.domains.length && !props.domains.includes(e.domain)) return false
// 动作的目标实体必须真能控制(`format.ts` 的 `isControllable` 与控件渲染同一口径)
if (props.controllableOnly && !isControllable(e)) return false
if (q && !e.name.toLowerCase().includes(q) && !e.entityId.toLowerCase().includes(q))
return false
return true
})
.sort(compareByName)
})
function pick(entityId: string) {
emit('update:model-value', entityId)
open.value = false
query.value = ''
}
/** 离线实体仍可选(规则可能在设备离线时被配置),但要标出来 */
function optionCaption(e: EntitySummary): string {
const parts = [domainOf(e.entityId)]
if (isOffline(e)) parts.push('离线')
return parts.join(' · ')
}
</script>
<template>
<Popover v-model:open="open">
<PopoverTrigger as-child>
<Button
variant="outline"
size="sm"
class="h-9 justify-between font-normal"
:class="[props.class, !modelValue && 'text-muted-foreground']"
:disabled="disabled"
>
<span class="truncate">{{ label }}</span>
<ChevronDown class="size-3.5 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent class="w-80 p-2" align="start">
<div class="relative mb-2">
<Search
class="pointer-events-none absolute top-1/2 left-2.5 size-3.5 -translate-y-1/2 text-muted-foreground"
:stroke-width="1.75"
/>
<Input v-model="query" placeholder="搜索名称 / 实体 ID" class="h-8 pl-8" spellcheck="false" />
</div>
<!-- ScrollArea 的 viewport 为 h-full,根节点要有确定高度才能滚动 -->
<ScrollArea class="h-72">
<div class="space-y-0.5 pr-2">
<button
v-for="e in options"
:key="e.entityId"
type="button"
class="flex w-full items-center gap-2 rounded-md px-2 py-1.5 text-left transition-colors hover:bg-row-hover"
:class="e.entityId === modelValue && 'bg-primary-soft'"
@click="pick(e.entityId)"
>
<component
:is="domainIcon(e)"
class="size-3.5 shrink-0 text-fg-secondary"
:stroke-width="1.75"
/>
<span class="min-w-0 flex-1 truncate text-[13px]">{{ e.name }}</span>
<span class="shrink-0 text-[11px] text-muted-foreground">{{ optionCaption(e) }}</span>
</button>
<p v-if="options.length === 0" class="px-2 py-3 text-[11.5px] text-muted-foreground">
没有匹配的实体(先在设备页确认实体已加载)。
</p>
</div>
</ScrollArea>
</PopoverContent>
</Popover>
</template>